Skip to content

gh-100871: Improve copy module tests #100872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Lib/test/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import unittest
import weakref
import copy

from pickle import loads, dumps
from test import support
Expand Down Expand Up @@ -242,6 +243,41 @@ 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 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()
Expand Down