]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-100871: Improve `copy` module tests (GH-100872) (#100976)
authorNikita Sobolev <mail@sobolevn.me>
Thu, 12 Jan 2023 11:15:00 +0000 (14:15 +0300)
committerGitHub <noreply@github.com>
Thu, 12 Jan 2023 11:15:00 +0000 (11:15 +0000)
(cherry picked from commit 729ab9b622957fef0e9b494af9a71ab02986c741)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/test/test_copy.py
Lib/test/test_slice.py

index f4d91c168689866f936c115581ecf1e3ce39b33d..7b1a927b4e5692d9cb0f79497cd900febeb9f8d1 100644 (file)
@@ -51,6 +51,9 @@ class TestCopy(unittest.TestCase):
         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):
@@ -313,6 +316,9 @@ class TestCopy(unittest.TestCase):
         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):
@@ -356,8 +362,8 @@ class TestCopy(unittest.TestCase):
             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), Classic, max, property()]
         for x in tests:
             self.assertIs(copy.deepcopy(x), x)
index 4ae4142c60c8a878ba5ae104def96d7f005b947c..584e5c7f17c2f6dd6a7bbf7ed5b104697476b629 100644 (file)
@@ -5,6 +5,7 @@ import operator
 import sys
 import unittest
 import weakref
+import copy
 
 from pickle import loads, dumps
 from test import support
@@ -242,6 +243,41 @@ class SliceTest(unittest.TestCase):
             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 indices:
+        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.assertEqual(s, c)
+
+        s = slice(1, 10, 2)
+        c = copy.deepcopy(s)
+        self.assertEqual(s, c)
+
+        # Corner case for mutable indices:
+        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()