mirror of
https://github.com/python/cpython.git
synced 2026-01-06 15:32:22 +00:00
gh-108751: Add copy.replace() function (GH-108752)
It creates a modified copy of an object by calling the object's __replace__() method. It is a generalization of dataclasses.replace(), named tuple's _replace() method and replace() methods in various classes, and supports all these stdlib classes.
This commit is contained in:
parent
9f0c0a46f0
commit
6f3c138dfa
19 changed files with 311 additions and 68 deletions
|
|
@ -125,6 +125,7 @@
|
|||
|
||||
"""
|
||||
|
||||
import copy
|
||||
import inspect
|
||||
import sys
|
||||
import threading
|
||||
|
|
@ -280,11 +281,17 @@ def func2():
|
|||
with self.subTest(attr=attr, value=value):
|
||||
new_code = code.replace(**{attr: value})
|
||||
self.assertEqual(getattr(new_code, attr), value)
|
||||
new_code = copy.replace(code, **{attr: value})
|
||||
self.assertEqual(getattr(new_code, attr), value)
|
||||
|
||||
new_code = code.replace(co_varnames=code2.co_varnames,
|
||||
co_nlocals=code2.co_nlocals)
|
||||
self.assertEqual(new_code.co_varnames, code2.co_varnames)
|
||||
self.assertEqual(new_code.co_nlocals, code2.co_nlocals)
|
||||
new_code = copy.replace(code, co_varnames=code2.co_varnames,
|
||||
co_nlocals=code2.co_nlocals)
|
||||
self.assertEqual(new_code.co_varnames, code2.co_varnames)
|
||||
self.assertEqual(new_code.co_nlocals, code2.co_nlocals)
|
||||
|
||||
def test_nlocals_mismatch(self):
|
||||
def func():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue