From 0863cf49034a82c875be78b45680c1dc19c62e00 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Tue, 31 Jan 2023 18:58:52 +0300 Subject: [PATCH 1/4] Fix duplicate values in the list when copying a subclass list --- Lib/copy.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Lib/copy.py b/Lib/copy.py index da2908ef623d8c..6490706732bc7f 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -254,6 +254,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) From 3e3916d4a9610aecf05b59c22ed0d70062acdc52 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Tue, 31 Jan 2023 19:04:42 +0300 Subject: [PATCH 2/4] Add new list subclass test cases to the copy module --- Lib/test/test_copy.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index 826e46824e004c..68465bcfdda95f 100644 --- a/Lib/test/test_copy.py +++ b/Lib/test/test_copy.py @@ -761,6 +761,18 @@ class C(list): 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(list(l1), list(l2)) + self.assertEqual(len(l1), len(l2)) + def test_copy_tuple_subclass(self): class C(tuple): pass From 9d4d7a6c408e9eeb3a77782130ac33396921be9b Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Tue, 31 Jan 2023 19:08:58 +0300 Subject: [PATCH 3/4] don't cast list to list --- Lib/test/test_copy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index 68465bcfdda95f..5b9899804249f6 100644 --- a/Lib/test/test_copy.py +++ b/Lib/test/test_copy.py @@ -770,7 +770,7 @@ def __setstate__(self, state): l1 = L([1, 2]) l2 = copy.deepcopy(l1) - self.assertEqual(list(l1), list(l2)) + self.assertEqual(l1, l2) self.assertEqual(len(l1), len(l2)) def test_copy_tuple_subclass(self): From 6131f8a42763a0d69dedb8ddf72b603a29626ba1 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 31 Jan 2023 16:48:57 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-01-31-16-48-54.gh-issue-48962.RvwDsB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-01-31-16-48-54.gh-issue-48962.RvwDsB.rst diff --git a/Misc/NEWS.d/next/Library/2023-01-31-16-48-54.gh-issue-48962.RvwDsB.rst b/Misc/NEWS.d/next/Library/2023-01-31-16-48-54.gh-issue-48962.RvwDsB.rst new file mode 100644 index 00000000000000..235929e5fbe333 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-01-31-16-48-54.gh-issue-48962.RvwDsB.rst @@ -0,0 +1 @@ +Fix duplicate values in the list when deepcopying a subclass list in the copy module.