diff options
author | Benjamin Peterson <benjamin@python.org> | 2011-06-27 21:22:46 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2011-06-27 21:22:46 (GMT) |
commit | e90ec366fb1f29705f3f6ed970091c5b3fa131a8 (patch) | |
tree | cfb0d7d582bdc169893a8530f2bfa25625d78bfd /Lib/copy.py | |
parent | 31877c9d0e4e005ef7faff45b4689358bd2aa93e (diff) | |
download | cpython-e90ec366fb1f29705f3f6ed970091c5b3fa131a8.zip cpython-e90ec366fb1f29705f3f6ed970091c5b3fa131a8.tar.gz cpython-e90ec366fb1f29705f3f6ed970091c5b3fa131a8.tar.bz2 |
don't memoize objects that are their own copies (closes #12422)
Patch mostly by Alex Gaynor.
Diffstat (limited to 'Lib/copy.py')
-rw-r--r-- | Lib/copy.py | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/Lib/copy.py b/Lib/copy.py index 089d101..497f21f 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -173,8 +173,10 @@ def deepcopy(x, memo=None, _nil=[]): "un(deep)copyable object of type %s" % cls) y = _reconstruct(x, rv, 1, memo) - memo[d] = y - _keep_alive(x, memo) # Make sure x lives at least as long as d + # If is its own copy, don't memoize. + if y is not x: + memo[d] = y + _keep_alive(x, memo) # Make sure x lives at least as long as d return y _deepcopy_dispatch = d = {} @@ -214,9 +216,10 @@ def _deepcopy_tuple(x, memo): y = [] for a in x: y.append(deepcopy(a, memo)) - d = id(x) + # We're not going to put the tuple in the memo, but it's still important we + # check for it, in case the tuple contains recursive mutable structures. try: - return memo[d] + return memo[id(x)] except KeyError: pass for i in range(len(x)): @@ -225,7 +228,6 @@ def _deepcopy_tuple(x, memo): break else: y = x - memo[d] = y return y d[tuple] = _deepcopy_tuple |