Commit 5c1c3b4f authored by Alexandre Vassalotti's avatar Alexandre Vassalotti

Issue #11480: Fixed copy.copy to work with classes with custom metaclasses.

Patch by Daniel Urban.
parent 361e30c1
...@@ -76,6 +76,14 @@ def copy(x): ...@@ -76,6 +76,14 @@ def copy(x):
if copier: if copier:
return copier(x) 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) copier = getattr(cls, "__copy__", None)
if copier: if copier:
return copier(x) return copier(x)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import copy import copy
import copyreg import copyreg
import weakref import weakref
import abc
from operator import le, lt, ge, gt, eq, ne from operator import le, lt, ge, gt, eq, ne
import unittest import unittest
...@@ -93,9 +94,11 @@ class TestCopy(unittest.TestCase): ...@@ -93,9 +94,11 @@ class TestCopy(unittest.TestCase):
pass pass
def f(): def f():
pass pass
class WithMetaclass(metaclass=abc.ABCMeta):
pass
tests = [None, 42, 2**100, 3.14, True, False, 1j, tests = [None, 42, 2**100, 3.14, True, False, 1j,
"hello", "hello\u1234", f.__code__, "hello", "hello\u1234", f.__code__,
NewStyle, range(10), Classic, max] NewStyle, range(10), Classic, max, WithMetaclass]
for x in tests: for x in tests:
self.assertIs(copy.copy(x), x) self.assertIs(copy.copy(x), x)
......
...@@ -24,6 +24,9 @@ Library ...@@ -24,6 +24,9 @@ Library
- Fixed _pickle.Unpickler to not fail when loading empty strings as - Fixed _pickle.Unpickler to not fail when loading empty strings as
persistent IDs. 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 - Issue #6477: Added support for pickling the types of built-in singletons
(i.e., Ellipsis, NotImplemented, None). (i.e., Ellipsis, NotImplemented, None).
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment