diff options
author | Michael W. Hudson <mwh@python.net> | 2002-01-04 12:28:43 (GMT) |
---|---|---|
committer | Michael W. Hudson <mwh@python.net> | 2002-01-04 12:28:43 (GMT) |
commit | c15958c37606da43889ab68359045e43d374fd14 (patch) | |
tree | 4ab4df52db6fec719b802a04638d714c8dbab151 /Lib | |
parent | d941bad6171c69bf713191fd7553638fa19a5a8a (diff) | |
download | cpython-c15958c37606da43889ab68359045e43d374fd14.zip cpython-c15958c37606da43889ab68359045e43d374fd14.tar.gz cpython-c15958c37606da43889ab68359045e43d374fd14.tar.bz2 |
Backport gvanrossum's checkin of revisions
copy.py, 1.23 & test_descr.py, 1.114:
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!
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/copy.py | 10 | ||||
-rw-r--r-- | Lib/test/test_descr.py | 11 |
2 files changed, 16 insertions, 5 deletions
diff --git a/Lib/copy.py b/Lib/copy.py index 14eff05..86fc978 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -172,7 +172,7 @@ def deepcopy(x, memo = None): raise error, \ "un-deep-copyable object of type %s" % type(x) else: - y = _reconstruct(x, reductor(), 1) + y = _reconstruct(x, reductor(), 1, memo) else: y = copier(memo) else: @@ -279,10 +279,12 @@ def _deepcopy_inst(x, memo): return y d[types.InstanceType] = _deepcopy_inst -def _reconstruct(x, info, deep): +def _reconstruct(x, info, deep, memo=None): if isinstance(info, str): return x assert isinstance(info, tuple) + if memo is None: + memo = {} n = len(info) assert n in (2, 3) callable, args = info[:2] @@ -291,11 +293,11 @@ def _reconstruct(x, info, deep): else: state = {} if deep: - args = deepcopy(args) + args = deepcopy(args, memo) y = callable(*args) if state: if deep: - state = deepcopy(state) + state = deepcopy(state, memo) y.__dict__.update(state) return y diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index ea987f2..dd95dde 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -2702,7 +2702,15 @@ def strops(): vereq('%c' % 5, '\x05') vereq('%c' % '5', '5') - +def deepcopyrecursive(): + if verbose: print "Testing deepcopy of recursive objects..." + class Node: + pass + a = Node() + b = Node() + a.b = b + b.a = a + z = deepcopy(a) # This blew up before def test_main(): @@ -2759,6 +2767,7 @@ def test_main(): delhook() hashinherit() strops() + deepcopyrecursive() if verbose: print "All OK" if __name__ == "__main__": |