[New-bugs-announce] [issue41954] [mock] Recursion on mocking inspect.isfunction
Stanislav Levin
report at bugs.python.org
Tue Oct 6 03:12:22 EDT 2020
New submission from Stanislav Levin <slev at altlinux.org>:
With Python 3.8 the mocking of `inspect.isfunction` results in recursion.
Please, consider a code snippet:
```python
from unittest import TestCase
from unittest.mock import patch
import inspect
class TestClass(TestCase):
def setUp(self):
patcher = patch('inspect.isfunction')
self.addCleanup(patcher.stop)
self.patched_func = patcher.start()
def test_m(self):
def f():
pass
inspect.isfunction(f)
```
Output:
```
ERROR: test_m (test.TestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/test.py", line 16, in test_m
inspect.isfunction(f)
File "/usr/lib64/python3.8/unittest/mock.py", line 1081, in __call__
return self._mock_call(*args, **kwargs)
File "/usr/lib64/python3.8/unittest/mock.py", line 1085, in _mock_call
return self._execute_mock_call(*args, **kwargs)
File "/usr/lib64/python3.8/unittest/mock.py", line 1157, in _execute_mock_call
return self.return_value
File "/usr/lib64/python3.8/unittest/mock.py", line 526, in __get_return_value
ret = self._get_child_mock(
File "/usr/lib64/python3.8/unittest/mock.py", line 1025, in _get_child_mock
return klass(**kw)
File "/usr/lib64/python3.8/unittest/mock.py", line 408, in __new__
sig = inspect.signature(NonCallableMock.__init__)
File "/usr/lib64/python3.8/inspect.py", line 3093, in signature
return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
File "/usr/lib64/python3.8/inspect.py", line 2842, in from_callable
return _signature_from_callable(obj, sigcls=cls,
File "/usr/lib64/python3.8/inspect.py", line 2289, in _signature_from_callable
if isfunction(obj) or _signature_is_functionlike(obj):
File "/usr/lib64/python3.8/unittest/mock.py", line 1081, in __call__
return self._mock_call(*args, **kwargs)
File "/usr/lib64/python3.8/unittest/mock.py", line 1085, in _mock_call
return self._execute_mock_call(*args, **kwargs)
File "/usr/lib64/python3.8/unittest/mock.py", line 1157, in _execute_mock_call
return self.return_value
File "/usr/lib64/python3.8/unittest/mock.py", line 526, in __get_return_value
ret = self._get_child_mock(
File "/usr/lib64/python3.8/unittest/mock.py", line 1025, in _get_child_mock
return klass(**kw)
...
return _signature_from_callable(obj, sigcls=cls,
RecursionError: maximum recursion depth exceeded
```
breaking commit: https://github.com/python/cpython/commit/77b3b7701a34ecf6316469e05b79bb91de2addfa
The pre-77b3b7701a34ecf6316469e05b79bb91de2addfa state of `Lib/unittest/mock.py` works as expected.
----------
messages: 378094
nosy: stanislavlevin
priority: normal
severity: normal
status: open
title: [mock] Recursion on mocking inspect.isfunction
type: behavior
versions: Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41954>
_______________________________________
More information about the New-bugs-announce
mailing list