diff options
author | Alex Martelli <aleaxit@gmail.com> | 2005-02-07 12:39:55 (GMT) |
---|---|---|
committer | Alex Martelli <aleaxit@gmail.com> | 2005-02-07 12:39:55 (GMT) |
commit | 4c337993c5afdfd9ebb40ff0847cf6015a875f4f (patch) | |
tree | b41dab2d8307066e1df34d7d4eab210ac1a2f6d1 /Lib/copy.py | |
parent | 339870121926f56294d8ccbca381d5d29c07cac0 (diff) | |
download | cpython-4c337993c5afdfd9ebb40ff0847cf6015a875f4f.zip cpython-4c337993c5afdfd9ebb40ff0847cf6015a875f4f.tar.gz cpython-4c337993c5afdfd9ebb40ff0847cf6015a875f4f.tar.bz2 |
forwardport of 2.3.5 fixes to copy.py
Diffstat (limited to 'Lib/copy.py')
-rw-r--r-- | Lib/copy.py | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/Lib/copy.py b/Lib/copy.py index af905f3..45fc32d 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -62,6 +62,16 @@ except ImportError: __all__ = ["Error", "copy", "deepcopy"] +import inspect +def _getspecial(cls, name): + for basecls in inspect.getmro(cls): + try: + return basecls.__dict__[name] + except: + pass + else: + return None + def copy(x): """Shallow copy operation on arbitrary Python objects. @@ -74,7 +84,7 @@ def copy(x): if copier: return copier(x) - copier = getattr(cls, "__copy__", None) + copier = _getspecial(cls, "__copy__") if copier: return copier(x) @@ -90,6 +100,9 @@ def copy(x): if reductor: rv = reductor() else: + copier = getattr(x, "__copy__", None) + if copier: + return copier() raise Error("un(shallow)copyable object of type %s" % cls) return _reconstruct(x, rv, 0) @@ -167,9 +180,9 @@ def deepcopy(x, memo=None, _nil=[]): if issc: y = _deepcopy_atomic(x, memo) else: - copier = getattr(x, "__deepcopy__", None) + copier = _getspecial(cls, "__deepcopy__") if copier: - y = copier(memo) + y = copier(x, memo) else: reductor = dispatch_table.get(cls) if reductor: @@ -183,6 +196,9 @@ def deepcopy(x, memo=None, _nil=[]): if reductor: rv = reductor() else: + copier = getattr(x, "__deepcopy__", None) + if copier: + return copier(memo) raise Error( "un(deep)copyable object of type %s" % cls) y = _reconstruct(x, rv, 1, memo) |