[Python-checkins] python/dist/src/Lib/test test_copy.py, 1.11, 1.11.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/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4976/Lib/test

Modified Files:
      Tag: release23-maint
	test_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: test_copy.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_copy.py,v
retrieving revision 1.11
retrieving revision 1.11.8.1
diff -u -d -r1.11 -r1.11.8.1
--- test_copy.py	14 Jun 2003 07:10:06 -0000	1.11
+++ test_copy.py	25 Jan 2005 12:52:18 -0000	1.11.8.1
@@ -166,8 +166,64 @@
         x = C(42)
         self.assertEqual(copy.copy(x), x)
 
-    # The deepcopy() method
+    # tests for copying extension types, iff module trycopy is installed
+    def test_copy_classictype(self):
+        from _testcapi import make_copyable
+        x = make_copyable([23])
+        y = copy.copy(x)
+        self.assertEqual(x, y)
+        self.assertEqual(x.tag, y.tag)
+        self.assert_(x is not y)
+        self.assert_(x.tag is y.tag)
+
+    def test_deepcopy_classictype(self):
+        from _testcapi import make_copyable
+        x = make_copyable([23])
+        y = copy.deepcopy(x)
+        self.assertEqual(x, y)
+        self.assertEqual(x.tag, y.tag)
+        self.assert_(x is not y)
+        self.assert_(x.tag is not y.tag)
+
+    # regression tests for metaclass-confusion
+    def test_copy_metaclassconfusion(self):
+        class MyOwnError(copy.Error):
+            pass
+        class Meta(type):
+            def __copy__(cls):
+                raise MyOwnError("can't copy classes w/this metaclass")
+        class C:
+            __metaclass__ = Meta
+            def __init__(self, tag):
+                self.tag = tag
+            def __cmp__(self, other):
+                return -cmp(other, self.tag)
+        # the metaclass can forbid shallow copying of its classes
+        self.assertRaises(MyOwnError, copy.copy, C)
+        # check that there is no interference with instances
+        x = C(23)
+        self.assertEqual(copy.copy(x), x)
 
+    def test_deepcopy_metaclassconfusion(self):
+        class MyOwnError(copy.Error):
+            pass
+        class Meta(type):
+            def __deepcopy__(cls, memo):
+                raise MyOwnError("can't deepcopy classes w/this metaclass")
+        class C:
+            __metaclass__ = Meta
+            def __init__(self, tag):
+                self.tag = tag
+            def __cmp__(self, other):
+                return -cmp(other, self.tag)
+        # types are ALWAYS deepcopied atomically, no matter what
+        self.assertEqual(copy.deepcopy(C), C)
+        # check that there is no interference with instances
+        x = C(23)
+        self.assertEqual(copy.deepcopy(x), x)
+
+
+    # The deepcopy() method
     def test_deepcopy_basic(self):
         x = 42
         y = copy.deepcopy(x)



More information about the Python-checkins mailing list