[Python-checkins] python/dist/src/Lib copy.py,1.42,1.42.8.1

anthonybaxter at users.sourceforge.net anthonybaxter at users.sourceforge.net
Tue Jan 25 13:52:21 CET 2005


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

Modified Files:
      Tag: release23-maint
	copy.py 
Log Message:
copy.py fixed to first lookup __copy__ from the instance being copied, 
rather than only looking at the type - this was broken in 2.3. 


Index: copy.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/copy.py,v
retrieving revision 1.42
retrieving revision 1.42.8.1
diff -u -d -r1.42 -r1.42.8.1
--- copy.py	14 Jun 2003 07:10:06 -0000	1.42
+++ copy.py	25 Jan 2005 12:52:18 -0000	1.42.8.1
@@ -62,6 +62,15 @@
 
 __all__ = ["Error", "copy", "deepcopy"]
 
+def _getspecial(cls, name):
+    for basecls in cls.__mro__:
+        try:
+            return basecls.__dict__[name]
+        except:
+            pass
+    else:
+        return None
+
 def copy(x):
     """Shallow copy operation on arbitrary Python objects.
 
@@ -74,7 +83,7 @@
     if copier:
         return copier(x)
 
-    copier = getattr(cls, "__copy__", None)
+    copier = _getspecial(cls, "__copy__")
     if copier:
         return copier(x)
 
@@ -90,6 +99,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)
@@ -185,9 +197,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:
@@ -201,6 +213,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