From 1fef63ba457d0a455fefbb6f686ab93728eac3cc Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 9 Jan 2023 10:34:41 +0300 Subject: [PATCH 1/3] gh-100871: Improve `copy` module tests --- Lib/test/test_copy.py | 10 ++++++++-- Lib/test/test_slice.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index cc95a319d35d78..826e46824e004c 100644 --- a/Lib/test/test_copy.py +++ b/Lib/test/test_copy.py @@ -51,6 +51,9 @@ def pickle_C(obj): self.assertRaises(TypeError, copy.copy, x) copyreg.pickle(C, pickle_C, C) y = copy.copy(x) + self.assertIsNot(x, y) + self.assertEqual(type(y), C) + self.assertEqual(y.foo, x.foo) def test_copy_reduce_ex(self): class C(object): @@ -311,6 +314,9 @@ def pickle_C(obj): self.assertRaises(TypeError, copy.deepcopy, x) copyreg.pickle(C, pickle_C, C) y = copy.deepcopy(x) + self.assertIsNot(x, y) + self.assertEqual(type(y), C) + self.assertEqual(y.foo, x.foo) def test_deepcopy_reduce_ex(self): class C(object): @@ -352,8 +358,8 @@ class NewStyle: pass def f(): pass - tests = [None, 42, 2**100, 3.14, True, False, 1j, - "hello", "hello\u1234", f.__code__, + tests = [None, ..., NotImplemented, 42, 2**100, 3.14, True, False, 1j, + b"bytes", "hello", "hello\u1234", f.__code__, NewStyle, range(10), max, property()] for x in tests: self.assertIs(copy.deepcopy(x), x) diff --git a/Lib/test/test_slice.py b/Lib/test/test_slice.py index 4ae4142c60c8a8..9628e863ae0178 100644 --- a/Lib/test/test_slice.py +++ b/Lib/test/test_slice.py @@ -5,6 +5,7 @@ import sys import unittest import weakref +import copy from pickle import loads, dumps from test import support @@ -242,6 +243,43 @@ def test_pickle(self): self.assertEqual(s.indices(15), t.indices(15)) self.assertNotEqual(id(s), id(t)) + def test_copy(self): + s = slice(1, 10) + c = copy.copy(s) + self.assertIs(s, c) + + s = slice(1, 10, 2) + c = copy.copy(s) + self.assertIs(s, c) + + # Corner case for mutable indicies: + s = slice([1, 2], [3, 4], [5, 6]) + c = copy.copy(s) + self.assertIs(s, c) + self.assertIs(s.start, c.start) + self.assertIs(s.stop, c.stop) + self.assertIs(s.step, c.step) + + def test_deepcopy(self): + s = slice(1, 10) + c = copy.deepcopy(s) + self.assertIsNot(s, c) + self.assertEqual(s, c) + + s = slice(1, 10, 2) + c = copy.deepcopy(s) + self.assertIsNot(s, c) + self.assertEqual(s, c) + + # Corner case for mutable indicies: + s = slice([1, 2], [3, 4], [5, 6]) + c = copy.deepcopy(s) + self.assertIsNot(s, c) + self.assertEqual(s, c) + self.assertIsNot(s.start, c.start) + self.assertIsNot(s.stop, c.stop) + self.assertIsNot(s.step, c.step) + def test_cycle(self): class myobj(): pass o = myobj() From 9d3e604e7bddf9e46ec881493ad1233cbd91a9a4 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 9 Jan 2023 12:23:56 -0800 Subject: [PATCH 2/3] Fix typo in comment --- Lib/test/test_slice.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_slice.py b/Lib/test/test_slice.py index 9628e863ae0178..a24c8f8d059fb8 100644 --- a/Lib/test/test_slice.py +++ b/Lib/test/test_slice.py @@ -252,7 +252,7 @@ def test_copy(self): c = copy.copy(s) self.assertIs(s, c) - # Corner case for mutable indicies: + # Corner case for mutable indices: s = slice([1, 2], [3, 4], [5, 6]) c = copy.copy(s) self.assertIs(s, c) @@ -271,7 +271,7 @@ def test_deepcopy(self): self.assertIsNot(s, c) self.assertEqual(s, c) - # Corner case for mutable indicies: + # Corner case for mutable indices: s = slice([1, 2], [3, 4], [5, 6]) c = copy.deepcopy(s) self.assertIsNot(s, c) From 2fd1a25b7639df19c728ee031df84143479ac8d4 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Wed, 11 Jan 2023 19:47:11 +0300 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Alex Waygood --- Lib/test/test_slice.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_slice.py b/Lib/test/test_slice.py index a24c8f8d059fb8..584e5c7f17c2f6 100644 --- a/Lib/test/test_slice.py +++ b/Lib/test/test_slice.py @@ -263,12 +263,10 @@ def test_copy(self): def test_deepcopy(self): s = slice(1, 10) c = copy.deepcopy(s) - self.assertIsNot(s, c) self.assertEqual(s, c) s = slice(1, 10, 2) c = copy.deepcopy(s) - self.assertIsNot(s, c) self.assertEqual(s, c) # Corner case for mutable indices: