[New-bugs-announce] [issue5660] Cannot deepcopy unittest.TestCase instances

Andrew Bennetts report at bugs.python.org
Thu Apr 2 02:27:54 CEST 2009


New submission from Andrew Bennetts <spiv at users.sourceforge.net>:

Here's a demonstration of the bug:

>>> from unittest import TestCase
>>> class MyTest(TestCase):
...     def test_foo(self): pass
... 
>>> tc = MyTest('test_foo')
>>> import copy
>>> copy.deepcopy(tc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/stow/py2.7/lib/python2.7/copy.py", line 189, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/local/stow/py2.7/lib/python2.7/copy.py", line 338, in
_reconstruct
    state = deepcopy(state, memo)
  File "/usr/local/stow/py2.7/lib/python2.7/copy.py", line 162, in deepcopy
    y = copier(x, memo)
  File "/usr/local/stow/py2.7/lib/python2.7/copy.py", line 255, in
_deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/local/stow/py2.7/lib/python2.7/copy.py", line 162, in deepcopy
    y = copier(x, memo)
  File "/usr/local/stow/py2.7/lib/python2.7/copy.py", line 255, in
_deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/local/stow/py2.7/lib/python2.7/copy.py", line 189, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/local/stow/py2.7/lib/python2.7/copy.py", line 323, in
_reconstruct
    y = callable(*args)
  File "/usr/local/bin/../stow/py2.7/lib/python2.7/copy_reg.py", line
93, in __newobj__
    return cls.__new__(cls, *args)
TypeError: instancemethod expected at least 2 arguments, got 0


This regression breaks bzr's test suite, which copies test objects to
run the same test against multiple scenarios (e.g. to run all the same
tests against all the implementations of bzrlib.transport.Transport,
such as HTTPTransport and SFTPTransport.) 
<https://launchpad.net/testtools> also implements test parameterisation
in this way, and it is extremely useful.

I suspect the __test_equality_funcs on TestCase is the problem:

>>> tc.__dict__
{'_testMethodDoc': None, '_TestCase__type_equality_funcs': {<type
'dict'>: <bound method MyTest.assertDictEqual of <__main__.MyTest
testMethod=test_foo>>, <type 'tuple'>: <bound method
MyTest.assertTupleEqual of <__main__.MyTest testMethod=test_foo>>, <type
'frozenset'>: <bound method MyTest.assertSetEqual of <__main__.MyTest
testMethod=test_foo>>, <type 'list'>: <bound method
MyTest.assertListEqual of <__main__.MyTest testMethod=test_foo>>, <type
'set'>: <bound method MyTest.assertSetEqual of <__main__.MyTest
testMethod=test_foo>>}, '_testMethodName': 'test_foo'}

copy.deepcopy can't deepcopy bound methods, reasonably enough.

----------
components: Library (Lib)
messages: 85123
nosy: spiv
severity: normal
status: open
title: Cannot deepcopy unittest.TestCase instances
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5660>
_______________________________________


More information about the New-bugs-announce mailing list