]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #11480: Fixed copy.copy to work with classes with custom metaclasses.
authorAlexandre Vassalotti <alexandre@peadrop.com>
Sun, 1 Dec 2013 21:25:26 +0000 (13:25 -0800)
committerAlexandre Vassalotti <alexandre@peadrop.com>
Sun, 1 Dec 2013 21:25:26 +0000 (13:25 -0800)
Patch by Daniel Urban.

Lib/copy.py
Lib/test/test_copy.py
Misc/NEWS

index d96201ea98eaf66ffc89de6927dd9f1d082ab94f..d26bcdbff6ae5e0938a3befcdcc75ffac5ac5b26 100644 (file)
@@ -76,6 +76,14 @@ def copy(x):
     if copier:
         return copier(x)
 
+    try:
+        issc = issubclass(cls, type)
+    except TypeError: # cls is not a class
+        issc = False
+    if issc:
+        # treat it as a regular class:
+        return _copy_immutable(x)
+
     copier = getattr(cls, "__copy__", None)
     if copier:
         return copier(x)
index c4baae48611c373ed27d146e3a948f31f03941b7..cde0baecad6234136728b2be4c8091b3dbec5ac9 100644 (file)
@@ -3,6 +3,7 @@
 import copy
 import copyreg
 import weakref
+import abc
 from operator import le, lt, ge, gt, eq, ne
 
 import unittest
@@ -93,9 +94,11 @@ class TestCopy(unittest.TestCase):
             pass
         def f():
             pass
+        class WithMetaclass(metaclass=abc.ABCMeta):
+            pass
         tests = [None, 42, 2**100, 3.14, True, False, 1j,
                  "hello", "hello\u1234", f.__code__,
-                 NewStyle, range(10), Classic, max]
+                 NewStyle, range(10), Classic, max, WithMetaclass]
         for x in tests:
             self.assertIs(copy.copy(x), x)
 
index 0acbafd600b02072359f89a4b7b0685532961ddf..3aa4ad6a843551b947ed0cc5d821291e35c55408 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Library
 - Fixed _pickle.Unpickler to not fail when loading empty strings as
   persistent IDs.
 
+- Issue #11480: Fixed copy.copy to work with classes with custom metaclasses.
+  Patch by Daniel Urban.
+
 - Issue #6477: Added support for pickling the types of built-in singletons
   (i.e., Ellipsis, NotImplemented, None).