[Python-checkins] cpython (3.3): Issue #11480: Fixed copy.copy to work with classes with custom metaclasses.

alexandre.vassalotti python-checkins at python.org
Sun Dec 1 22:26:59 CET 2013


http://hg.python.org/cpython/rev/c6bb6f304f75
changeset:   87681:c6bb6f304f75
branch:      3.3
parent:      87674:efcdf2a70f2a
user:        Alexandre Vassalotti <alexandre at peadrop.com>
date:        Sun Dec 01 13:25:26 2013 -0800
summary:
  Issue #11480: Fixed copy.copy to work with classes with custom metaclasses.

Patch by Daniel Urban.

files:
  Lib/copy.py           |  8 ++++++++
  Lib/test/test_copy.py |  5 ++++-
  Misc/NEWS             |  3 +++
  3 files changed, 15 insertions(+), 1 deletions(-)


diff --git a/Lib/copy.py b/Lib/copy.py
--- a/Lib/copy.py
+++ b/Lib/copy.py
@@ -76,6 +76,14 @@
     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)
diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py
--- a/Lib/test/test_copy.py
+++ b/Lib/test/test_copy.py
@@ -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 @@
             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)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@
 - 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).
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list