From: Alexandre Vassalotti Date: Sun, 1 Dec 2013 21:25:26 +0000 (-0800) Subject: Issue #11480: Fixed copy.copy to work with classes with custom metaclasses. X-Git-Tag: v3.4.0b2~411^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5c1c3b4f197c57952760be37d77d73669284a607;p=thirdparty%2FPython%2Fcpython.git Issue #11480: Fixed copy.copy to work with classes with custom metaclasses. Patch by Daniel Urban. --- diff --git a/Lib/copy.py b/Lib/copy.py index d96201ea98ea..d26bcdbff6ae 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -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) diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index c4baae48611c..cde0baecad62 100644 --- 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 @@ 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) diff --git a/Misc/NEWS b/Misc/NEWS index 0acbafd600b0..3aa4ad6a8435 100644 --- 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).