Skip to content

Commit abc037e

Browse files
committed
Use 'd in memo' lookup instead of a sentinel
Addresses review: clearer, no sentinel, and the test treats memo as opaque.
1 parent 17c0eb9 commit abc037e

3 files changed

Lines changed: 6 additions & 9 deletions

File tree

Lib/copy.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ class Error(Exception):
5959

6060
__all__ = ["Error", "copy", "deepcopy", "replace"]
6161

62-
_MEMO_MISS = object()
63-
6462
def copy(x):
6563
"""Shallow copy operation on arbitrary Python objects.
6664
@@ -124,9 +122,8 @@ def deepcopy(x, memo=None):
124122
if memo is None:
125123
memo = {}
126124
else:
127-
y = memo.get(d, _MEMO_MISS)
128-
if y is not _MEMO_MISS:
129-
return y
125+
if d in memo:
126+
return memo[d]
130127

131128
copier = _deepcopy_dispatch.get(cls)
132129
if copier is not None:

Lib/test/test_copy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,6 @@ class C:
946946
def __deepcopy__(self, memo):
947947
nonlocal call_count
948948
call_count += 1
949-
memo[id(self)] = None
950949
return None
951950
obj = C()
952951
copy.deepcopy([obj, obj, obj])
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
Fix :func:`copy.deepcopy` memo lookup using ``None`` as the miss sentinel,
2-
which prevented memoization of objects whose deep copy result is ``None``.
3-
Use a private sentinel object instead. Patch by tonghuaroot.
1+
Fix :func:`copy.deepcopy` so that an object whose deep copy is ``None`` is
2+
still memoized. The memo miss was detected with ``None``, which is a valid
3+
result, so such objects were copied again on every reference. Patch by
4+
tonghuaroot.

0 commit comments

Comments
 (0)