mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Fix for SF bug ##497426: can't deepcopy recursive new objects
deepcopy(), _reconstruct(): pass the memo to the other function, so that recursive data structures built out of new-style objects may be deeply copied correctly. 2.2.1 bugfix!
This commit is contained in:
parent
1baeba6839
commit
1e91c1444a
1 changed files with 6 additions and 4 deletions
10
Lib/copy.py
10
Lib/copy.py
|
@ -172,7 +172,7 @@ def deepcopy(x, memo = None):
|
||||||
raise error, \
|
raise error, \
|
||||||
"un-deep-copyable object of type %s" % type(x)
|
"un-deep-copyable object of type %s" % type(x)
|
||||||
else:
|
else:
|
||||||
y = _reconstruct(x, reductor(), 1)
|
y = _reconstruct(x, reductor(), 1, memo)
|
||||||
else:
|
else:
|
||||||
y = copier(memo)
|
y = copier(memo)
|
||||||
else:
|
else:
|
||||||
|
@ -279,10 +279,12 @@ def _deepcopy_inst(x, memo):
|
||||||
return y
|
return y
|
||||||
d[types.InstanceType] = _deepcopy_inst
|
d[types.InstanceType] = _deepcopy_inst
|
||||||
|
|
||||||
def _reconstruct(x, info, deep):
|
def _reconstruct(x, info, deep, memo=None):
|
||||||
if isinstance(info, str):
|
if isinstance(info, str):
|
||||||
return x
|
return x
|
||||||
assert isinstance(info, tuple)
|
assert isinstance(info, tuple)
|
||||||
|
if memo is None:
|
||||||
|
memo = {}
|
||||||
n = len(info)
|
n = len(info)
|
||||||
assert n in (2, 3)
|
assert n in (2, 3)
|
||||||
callable, args = info[:2]
|
callable, args = info[:2]
|
||||||
|
@ -291,11 +293,11 @@ def _reconstruct(x, info, deep):
|
||||||
else:
|
else:
|
||||||
state = {}
|
state = {}
|
||||||
if deep:
|
if deep:
|
||||||
args = deepcopy(args)
|
args = deepcopy(args, memo)
|
||||||
y = callable(*args)
|
y = callable(*args)
|
||||||
if state:
|
if state:
|
||||||
if deep:
|
if deep:
|
||||||
state = deepcopy(state)
|
state = deepcopy(state, memo)
|
||||||
y.__dict__.update(state)
|
y.__dict__.update(state)
|
||||||
return y
|
return y
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue