Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot deepcopy unittest.TestCase instances #49910

Closed
spiv mannequin opened this issue Apr 2, 2009 · 4 comments
Closed

Cannot deepcopy unittest.TestCase instances #49910

spiv mannequin opened this issue Apr 2, 2009 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@spiv
Copy link
Mannequin

spiv mannequin commented Apr 2, 2009

BPO 5660
Nosy @gpshead, @voidspace

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/gpshead'
closed_at = <Date 2009-04-02.05:52:35.022>
created_at = <Date 2009-04-02.00:27:53.138>
labels = ['library']
title = 'Cannot deepcopy unittest.TestCase instances'
updated_at = <Date 2009-04-02.05:52:35.021>
user = 'https://bugs.python.org/spiv'

bugs.python.org fields:

activity = <Date 2009-04-02.05:52:35.021>
actor = 'michael.foord'
assignee = 'gregory.p.smith'
closed = True
closed_date = <Date 2009-04-02.05:52:35.022>
closer = 'michael.foord'
components = ['Library (Lib)']
creation = <Date 2009-04-02.00:27:53.138>
creator = 'spiv'
dependencies = []
files = []
hgrepos = []
issue_num = 5660
keywords = []
message_count = 4.0
messages = ['85123', '85163', '85166', '85171']
nosy_count = 3.0
nosy_names = ['gregory.p.smith', 'spiv', 'michael.foord']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue5660'
versions = ['Python 2.7']

@spiv
Copy link
Mannequin Author

spiv mannequin commented Apr 2, 2009

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.

@spiv spiv mannequin added the stdlib Python modules in the Lib dir label Apr 2, 2009
@voidspace
Copy link
Contributor

This is a workaround:

import copy
copy._deepcopy_dispatch[types.MethodType] = copy._deepcopy_atomic

@voidspace
Copy link
Contributor

We can fix this by wrapping the assert functions in our assert register
as deep-copyable objects. (And then unwrapping when we fetch them.)

@voidspace
Copy link
Contributor

Fixed in revision 71043.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

2 participants