diff --git a/Lib/copy.py b/Lib/copy.py index f27e109973cfb7..feb8ce1a1abd77 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -57,6 +57,8 @@ class Error(Exception): pass error = Error # backward compatibility +_NoValue = object() + __all__ = ["Error", "copy", "deepcopy", "replace"] def copy(x): @@ -75,20 +77,20 @@ def copy(x): # treat it as a regular class: return _copy_immutable(x) - copier = getattr(cls, "__copy__", None) - if copier is not None: + copier = getattr(cls, "__copy__", _NoValue) + if copier is not _NoValue: return copier(x) - reductor = dispatch_table.get(cls) - if reductor is not None: + reductor = dispatch_table.get(cls, _NoValue) + if reductor is not _NoValue: rv = reductor(x) else: - reductor = getattr(x, "__reduce_ex__", None) - if reductor is not None: + reductor = getattr(x, "__reduce_ex__", _NoValue) + if reductor is not _NoValue: rv = reductor(4) else: - reductor = getattr(x, "__reduce__", None) - if reductor: + reductor = getattr(x, "__reduce__", _NoValue) + if reductor is not _NoValue: rv = reductor() else: raise Error("un(shallow)copyable object of type %s" % cls) @@ -142,20 +144,20 @@ def deepcopy(x, memo=None, _nil=[]): if issubclass(cls, type): y = x # atomic copy else: - copier = getattr(x, "__deepcopy__", None) - if copier is not None: + copier = getattr(x, "__deepcopy__", _NoValue) + if copier is not _NoValue: y = copier(memo) else: reductor = dispatch_table.get(cls) if reductor: rv = reductor(x) else: - reductor = getattr(x, "__reduce_ex__", None) - if reductor is not None: + reductor = getattr(x, "__reduce_ex__", _NoValue) + if reductor is not _NoValue: rv = reductor(4) else: - reductor = getattr(x, "__reduce__", None) - if reductor: + reductor = getattr(x, "__reduce__", _NoValue) + if reductor is not _NoValue: rv = reductor() else: raise Error( @@ -289,7 +291,7 @@ def replace(obj, /, **changes): frozen dataclasses. """ cls = obj.__class__ - func = getattr(cls, '__replace__', None) - if func is None: + func = getattr(cls, '__replace__', _NoValue) + if func is _NoValue: raise TypeError(f"replace() does not support {cls.__name__} objects") return func(obj, **changes) diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index d76341417e9bef..1fc71880b7f958 100644 --- a/Lib/test/test_copy.py +++ b/Lib/test/test_copy.py @@ -68,6 +68,26 @@ def __reduce__(self): self.assertIs(y, x) self.assertEqual(c, [1]) + def test_copy_invalid_reduction_methods(self): + class C(object): + __copy__ = None + x = C() + with self.assertRaises(TypeError): + copy.copy(x) + + class C(object): + __reduce_ex__ = None + x = C() + with self.assertRaises(TypeError): + copy.copy(x) + + class C(object): + __reduce_ex__ = copy._NoValue + __reduce__ = None + x = C() + with self.assertRaises(TypeError): + copy.copy(x) + def test_copy_reduce(self): class C(object): def __reduce__(self): @@ -974,6 +994,13 @@ class C: with self.assertRaisesRegex(TypeError, 'unexpected keyword argument'): copy.replace(c, x=1, error=2) + def test_invalid_replace_method(self): + class A: + __replace__ = None + a = A() + with self.assertRaises(TypeError): + copy.replace(a) + class MiscTestCase(unittest.TestCase): def test__all__(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2023-09-16-19-19-47.gh-issue-93627.fkb529.rst b/Misc/NEWS.d/next/Core_and_Builtins/2023-09-16-19-19-47.gh-issue-93627.fkb529.rst new file mode 100644 index 00000000000000..37d9d2539d79bf --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2023-09-16-19-19-47.gh-issue-93627.fkb529.rst @@ -0,0 +1 @@ +Update the Python copy module implementation to match the implementation of the pickle module. For objects setting reduction methods like :meth:`~object.__copy__` , :meth:`~object.__reduce_ex__` or :meth:`~object.__reduce__` to ``None``, a call to :meth:`copy.copy` or :meth:`copy.deepcopy` will result in a :exc:`TypeError`.