Skip to content

Update copy.py from CPython 3.11 #4674

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 2 commits into from
Mar 9, 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
7 changes: 4 additions & 3 deletions Lib/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class instances).
set of components copied

This version does not copy types like module, class, function, method,
nor stack trace, stack frame, nor file, socket, window, nor array, nor
any similar types.
nor stack trace, stack frame, nor file, socket, window, nor any
similar types.

Classes can use the same interfaces to control copying that they use
to control pickling: they can define methods called __getinitargs__(),
Expand Down Expand Up @@ -192,6 +192,7 @@ def _deepcopy_atomic(x, memo):
d[str] = _deepcopy_atomic
d[types.CodeType] = _deepcopy_atomic
d[type] = _deepcopy_atomic
d[range] = _deepcopy_atomic
d[types.BuiltinFunctionType] = _deepcopy_atomic
d[types.FunctionType] = _deepcopy_atomic
d[weakref.ref] = _deepcopy_atomic
Expand Down Expand Up @@ -257,7 +258,7 @@ def _keep_alive(x, memo):

def _reconstruct(x, memo, func, args,
state=None, listiter=None, dictiter=None,
deepcopy=deepcopy):
*, deepcopy=deepcopy):
deep = memo is not None
if deep and args:
args = (deepcopy(arg, memo) for arg in args)
Expand Down
34 changes: 30 additions & 4 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 @@ -315,6 +318,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 @@ -351,17 +357,15 @@ def __getattribute__(self, name):

# Type-specific _deepcopy_xxx() methods

# TODO: RUSTPYTHON
@unittest.expectedFailure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this magically fixed? great!

def test_deepcopy_atomic(self):
class Classic:
pass
class NewStyle(object):
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)
Expand Down Expand Up @@ -684,6 +688,28 @@ def __eq__(self, other):
self.assertIsNot(x, y)
self.assertIsNot(x["foo"], y["foo"])

def test_reduce_6tuple(self):
def state_setter(*args, **kwargs):
self.fail("shouldn't call this")
class C:
def __reduce__(self):
return C, (), self.__dict__, None, None, state_setter
x = C()
with self.assertRaises(TypeError):
copy.copy(x)
with self.assertRaises(TypeError):
copy.deepcopy(x)

def test_reduce_6tuple_none(self):
class C:
def __reduce__(self):
return C, (), self.__dict__, None, None, None
x = C()
with self.assertRaises(TypeError):
copy.copy(x)
with self.assertRaises(TypeError):
copy.deepcopy(x)

def test_copy_slots(self):
class C(object):
__slots__ = ["foo"]
Expand Down