[Python-checkins] r71043 - in python/trunk/Lib: test/test_unittest.py unittest.py

michael.foord python-checkins at python.org
Thu Apr 2 07:51:54 CEST 2009


Author: michael.foord
Date: Thu Apr  2 07:51:54 2009
New Revision: 71043

Log:
Store the functions in the _type_equality_funcs as wrapped objects that are deep copyable.

This allows for the deep copying of TestCase instances.

Issue 5660



Modified:
   python/trunk/Lib/test/test_unittest.py
   python/trunk/Lib/unittest.py

Modified: python/trunk/Lib/test/test_unittest.py
==============================================================================
--- python/trunk/Lib/test/test_unittest.py	(original)
+++ python/trunk/Lib/test/test_unittest.py	Thu Apr  2 07:51:54 2009
@@ -11,6 +11,7 @@
 import unittest
 from unittest import TestCase
 import types
+from copy import deepcopy
 
 ### Support code
 ################################################################
@@ -2688,6 +2689,17 @@
         self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam')
         self.failIf(False)
 
+    def testDeepcopy(self):
+        # Issue: 5660
+        class TestableTest(TestCase):
+            def testNothing(self):
+                pass
+
+        test = TestableTest('testNothing')
+
+        # This shouldn't blow up
+        deepcopy(test)
+
 
 class Test_TestSkipping(TestCase):
 

Modified: python/trunk/Lib/unittest.py
==============================================================================
--- python/trunk/Lib/unittest.py	(original)
+++ python/trunk/Lib/unittest.py	Thu Apr  2 07:51:54 2009
@@ -160,7 +160,6 @@
         raise _UnexpectedSuccess
     return wrapper
 
-
 __unittest = 1
 
 class TestResult(object):
@@ -289,6 +288,16 @@
         return True
 
 
+class _AssertWrapper(object):
+    """Wrap entries in the _type_equality_funcs registry to make them deep
+    copyable."""
+
+    def __init__(self, function):
+        self.function = function
+
+    def __deepcopy__(self, memo):
+        memo[id(self)] = self
+
 
 class TestCase(object):
     """A class whose instances are single test cases.
@@ -361,7 +370,7 @@
                     msg= argument that raises self.failureException with a
                     useful error message when the two arguments are not equal.
         """
-        self._type_equality_funcs[typeobj] = function
+        self._type_equality_funcs[typeobj] = _AssertWrapper(function)
 
     def setUp(self):
         "Hook method for setting up the test fixture before exercising it."
@@ -542,8 +551,10 @@
         # See the discussion in http://bugs.python.org/issue2578.
         #
         if type(first) is type(second):
-            return self._type_equality_funcs.get(type(first),
-                                                 self._baseAssertEqual)
+            asserter = self._type_equality_funcs.get(type(first))
+            if asserter is not None:
+                return asserter.function
+
         return self._baseAssertEqual
 
     def _baseAssertEqual(self, first, second, msg=None):


More information about the Python-checkins mailing list