Skip to content

Commit 86d0ec2

Browse files
committed
use ellipsis as sentinel for deepcopy memo
1 parent 7b3592a commit 86d0ec2

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

Lib/copy.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ def deepcopy(x, memo=None):
120120

121121
d = id(x)
122122
if memo is None:
123-
memo = {}
124-
memo[id(memo)] = []
123+
memo = {...: []}
125124
else:
126125
y = memo.get(d, None)
127126
if y is not None:
@@ -160,7 +159,7 @@ def deepcopy(x, memo=None):
160159
# If is its own copy, don't memoize.
161160
if y is not x:
162161
memo[d] = y
163-
memo[id(memo)].append(x) # Make sure x lives at least as long as d
162+
memo[...].append(x) # Make sure x lives at least as long as d
164163
return y
165164

166165
_atomic_types = frozenset({types.NoneType, types.EllipsisType, types.NotImplementedType,

Lib/test/test_copy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,22 +435,22 @@ def test_deepcopy_reflexive_dict(self):
435435

436436
def test_deepcopy_keepalive(self):
437437
memo = {}
438-
memo[id(memo)] = []
438+
memo[...] = []
439439
x = []
440440
y = copy.deepcopy(x, memo)
441-
self.assertIs(memo[id(memo)][0], x)
441+
self.assertIs(memo[...][0], x)
442442

443443
def test_deepcopy_dont_memo_immutable(self):
444444
memo = {}
445-
memo[id(memo)] = []
445+
memo[...] = []
446446
x = [1, 2, 3, 4]
447447
y = copy.deepcopy(x, memo)
448448
self.assertEqual(y, x)
449449
# There's the entry for the new list, and the keep alive.
450450
self.assertEqual(len(memo), 2)
451451

452452
memo = {}
453-
memo[id(memo)] = []
453+
memo[...] = []
454454
x = [(1, 2)]
455455
y = copy.deepcopy(x, memo)
456456
self.assertEqual(y, x)

0 commit comments

Comments
 (0)