mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
Merge 9aaf916dbf
into 61dd9fdad7
This commit is contained in:
commit
739d137bfc
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:
|
||||
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 deep:
|
||||
state = deepcopy(state, memo)
|
||||
|
|
|
@ -767,6 +767,18 @@ class TestCopy(unittest.TestCase):
|
|||
self.assertIsNot(x[0], y[0])
|
||||
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):
|
||||
class C(tuple):
|
||||
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