Skip to content

Commit 4be9726

Browse files
bpo-38293: Allow shallow and deep copying of property objects (GH-16438)
Copying property objects results in a TypeError. Steps to reproduce: ``` >>> import copy >>> obj = property() >>> copy.copy(obj) ```` This affects both shallow and deep copying. My idea for a fix is to add property objects to the list of "atomic" objects in the copy module. These already include types like functions and type objects. I also added property objects to the unit tests test_copy_atomic and test_deepcopy_atomic. This is my first PR, and it's highly likely I've made some mistake, so please be kind :) https://bugs.python.org/issue38293 (cherry picked from commit 9f3fc6c) Co-authored-by: Guðni Natan Gunnarsson <1493259+GudniNatan@users.noreply.github.com>
1 parent a993658 commit 4be9726

3 files changed

Lines changed: 5 additions & 3 deletions

File tree

Lib/copy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def copy(x):
111111
def _copy_immutable(x):
112112
return x
113113
for t in (type(None), int, float, bool, complex, str, tuple,
114-
bytes, frozenset, type, range, slice,
114+
bytes, frozenset, type, range, slice, property,
115115
types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented),
116116
types.FunctionType, weakref.ref):
117117
d[t] = _copy_immutable
@@ -206,6 +206,7 @@ def _deepcopy_atomic(x, memo):
206206
d[types.BuiltinFunctionType] = _deepcopy_atomic
207207
d[types.FunctionType] = _deepcopy_atomic
208208
d[weakref.ref] = _deepcopy_atomic
209+
d[property] = _deepcopy_atomic
209210

210211
def _deepcopy_list(x, memo, deepcopy=deepcopy):
211212
y = []

Lib/test/test_copy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class WithMetaclass(metaclass=abc.ABCMeta):
9999
42, 2**100, 3.14, True, False, 1j,
100100
"hello", "hello\u1234", f.__code__,
101101
b"world", bytes(range(256)), range(10), slice(1, 10, 2),
102-
NewStyle, Classic, max, WithMetaclass]
102+
NewStyle, Classic, max, WithMetaclass, property()]
103103
for x in tests:
104104
self.assertIs(copy.copy(x), x)
105105

@@ -357,7 +357,7 @@ def f():
357357
pass
358358
tests = [None, 42, 2**100, 3.14, True, False, 1j,
359359
"hello", "hello\u1234", f.__code__,
360-
NewStyle, Classic, max]
360+
NewStyle, Classic, max, property()]
361361
for x in tests:
362362
self.assertIs(copy.deepcopy(x), x)
363363

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :func:`copy.copy` and :func:`copy.deepcopy` support to :func:`property` objects.

0 commit comments

Comments
 (0)