[Python-checkins] python/dist/src/Lib copy.py,1.43,1.43.4.1

aleax at users.sourceforge.net aleax at users.sourceforge.net
Mon Feb 7 13:39:58 CET 2005


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3361/Lib

Modified Files:
      Tag: release24-maint
	copy.py 
Log Message:
forwardport of 2.3.5 fixes to copy.py

Index: copy.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/copy.py,v
retrieving revision 1.43
retrieving revision 1.43.4.1
diff -u -d -r1.43 -r1.43.4.1
--- copy.py	8 Mar 2004 05:59:33 -0000	1.43
+++ copy.py	7 Feb 2005 12:39:54 -0000	1.43.4.1
@@ -62,6 +62,16 @@
 
 __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 @@
     if copier:
         return copier(x)
 
-    copier = getattr(cls, "__copy__", None)
+    copier = _getspecial(cls, "__copy__")
     if copier:
         return copier(x)
 
@@ -90,6 +100,9 @@
             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 @@
         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 @@
                         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)



More information about the Python-checkins mailing list