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
This commit is contained in:
Michael Foord 2009-04-02 05:51:54 +00:00
parent 7152f6d915
commit e2942d073d
2 changed files with 27 additions and 4 deletions

View file

@ -11,6 +11,7 @@ from test import test_support
import unittest import unittest
from unittest import TestCase from unittest import TestCase
import types import types
from copy import deepcopy
### Support code ### Support code
################################################################ ################################################################
@ -2688,6 +2689,17 @@ test case
self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam') self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam')
self.failIf(False) 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): class Test_TestSkipping(TestCase):

View file

@ -160,7 +160,6 @@ def expectedFailure(func):
raise _UnexpectedSuccess raise _UnexpectedSuccess
return wrapper return wrapper
__unittest = 1 __unittest = 1
class TestResult(object): class TestResult(object):
@ -289,6 +288,16 @@ class _AssertRaisesContext(object):
return True 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): class TestCase(object):
"""A class whose instances are single test cases. """A class whose instances are single test cases.
@ -361,7 +370,7 @@ class TestCase(object):
msg= argument that raises self.failureException with a msg= argument that raises self.failureException with a
useful error message when the two arguments are not equal. 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): def setUp(self):
"Hook method for setting up the test fixture before exercising it." "Hook method for setting up the test fixture before exercising it."
@ -542,8 +551,10 @@ class TestCase(object):
# See the discussion in http://bugs.python.org/issue2578. # See the discussion in http://bugs.python.org/issue2578.
# #
if type(first) is type(second): if type(first) is type(second):
return self._type_equality_funcs.get(type(first), asserter = self._type_equality_funcs.get(type(first))
self._baseAssertEqual) if asserter is not None:
return asserter.function
return self._baseAssertEqual return self._baseAssertEqual
def _baseAssertEqual(self, first, second, msg=None): def _baseAssertEqual(self, first, second, msg=None):