mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Merge 9aaf916dbf into 7099af8f5e
This commit is contained in:
commit
de9833efff
3 changed files with 40 additions and 0 deletions
27
Lib/copy.py
27
Lib/copy.py
|
|
@ -235,6 +235,33 @@ def _reconstruct(x, memo, func, args,
|
||||||
if deep:
|
if deep:
|
||||||
memo[id(x)] = y
|
memo[id(x)] = y
|
||||||
|
|
||||||
|
if isinstance(state, list):
|
||||||
|
if listiter is not None:
|
||||||
|
if deep:
|
||||||
|
for item in listiter:
|
||||||
|
item = deepcopy(item, memo)
|
||||||
|
y.append(item)
|
||||||
|
else:
|
||||||
|
for item in listiter:
|
||||||
|
y.append(item)
|
||||||
|
|
||||||
|
if state is not None:
|
||||||
|
if deep:
|
||||||
|
state = deepcopy(state, memo)
|
||||||
|
if hasattr(y, '__setstate__'):
|
||||||
|
y.__setstate__(state)
|
||||||
|
else:
|
||||||
|
if isinstance(state, tuple) and len(state) == 2:
|
||||||
|
state, slotstate = state
|
||||||
|
else:
|
||||||
|
slotstate = None
|
||||||
|
if state is not None:
|
||||||
|
y.__dict__.update(state)
|
||||||
|
if slotstate is not None:
|
||||||
|
for key, value in slotstate.items():
|
||||||
|
setattr(y, key, value)
|
||||||
|
return y
|
||||||
|
|
||||||
if state is not None:
|
if state is not None:
|
||||||
if deep:
|
if deep:
|
||||||
state = deepcopy(state, memo)
|
state = deepcopy(state, memo)
|
||||||
|
|
|
||||||
|
|
@ -767,6 +767,18 @@ class C(list):
|
||||||
self.assertIsNot(x[0], y[0])
|
self.assertIsNot(x[0], y[0])
|
||||||
self.assertIsNot(x.foo, y.foo)
|
self.assertIsNot(x.foo, y.foo)
|
||||||
|
|
||||||
|
class L(list):
|
||||||
|
def __getstate__(self):
|
||||||
|
return list(self)
|
||||||
|
|
||||||
|
def __setstate__(self, state):
|
||||||
|
self[:] = state
|
||||||
|
|
||||||
|
l1 = L([1, 2])
|
||||||
|
l2 = copy.deepcopy(l1)
|
||||||
|
self.assertEqual(l1, l2)
|
||||||
|
self.assertEqual(len(l1), len(l2))
|
||||||
|
|
||||||
def test_copy_tuple_subclass(self):
|
def test_copy_tuple_subclass(self):
|
||||||
class C(tuple):
|
class C(tuple):
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Fix duplicate values in the list when deepcopying a subclass list in the copy module.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue