Skip to content

Commit 5c1c3b4

Browse files
committed
Issue python#11480: Fixed copy.copy to work with classes with custom metaclasses.
Patch by Daniel Urban.
1 parent 361e30c commit 5c1c3b4

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

Lib/copy.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ def copy(x):
7676
if copier:
7777
return copier(x)
7878

79+
try:
80+
issc = issubclass(cls, type)
81+
except TypeError: # cls is not a class
82+
issc = False
83+
if issc:
84+
# treat it as a regular class:
85+
return _copy_immutable(x)
86+
7987
copier = getattr(cls, "__copy__", None)
8088
if copier:
8189
return copier(x)

Lib/test/test_copy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
import copyreg
55
import weakref
6+
import abc
67
from operator import le, lt, ge, gt, eq, ne
78

89
import unittest
@@ -93,9 +94,11 @@ class NewStyle(object):
9394
pass
9495
def f():
9596
pass
97+
class WithMetaclass(metaclass=abc.ABCMeta):
98+
pass
9699
tests = [None, 42, 2**100, 3.14, True, False, 1j,
97100
"hello", "hello\u1234", f.__code__,
98-
NewStyle, range(10), Classic, max]
101+
NewStyle, range(10), Classic, max, WithMetaclass]
99102
for x in tests:
100103
self.assertIs(copy.copy(x), x)
101104

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Library
2424
- Fixed _pickle.Unpickler to not fail when loading empty strings as
2525
persistent IDs.
2626

27+
- Issue #11480: Fixed copy.copy to work with classes with custom metaclasses.
28+
Patch by Daniel Urban.
29+
2730
- Issue #6477: Added support for pickling the types of built-in singletons
2831
(i.e., Ellipsis, NotImplemented, None).
2932

0 commit comments

Comments
 (0)