This commit is contained in:
Furkan Onder 2025-07-10 06:57:37 +03:00 committed by GitHub
commit 739d137bfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 0 deletions

View file

@ -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)

View file

@ -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

View file

@ -0,0 +1 @@
Fix duplicate values in the list when deepcopying a subclass list in the copy module.