[issue41954] [mock] Recursion on mocking inspect.isfunction

2020-10-20 Thread Stanislav Levin


Stanislav Levin  added the comment:

I see, thank you for your time.

--
resolution:  -> wont fix
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41954] [mock] Recursion on mocking inspect.isfunction

2020-10-19 Thread Lisa Roach


Lisa Roach  added the comment:

Oof yeah, sorry my commit broke this but it is pretty hard to avoid. Mario is 
right in that it would be messy and challenging to ensure anything mocked from 
the stdlib doesn't also break mock itself.  

I want to mark this as `wont fix`, unless there is a compelling reason for the 
stdlib's `inspect.isfunction` to be mockable. But I agree with Mario again 
here, it would be better to mock the module that is using the function rather 
than the built-in function itself.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41954] [mock] Recursion on mocking inspect.isfunction

2020-10-06 Thread Mario Corchero


Mario Corchero  added the comment:

Oh, well spotted. We could add some guards around the code in mock to still 
work when key stdlib functionality is mocked out, but this might make the mock 
code quite messy. Specially as we will have to make sure that not only the 
function we call are safe to call (which is easy by just doing import from), 
but that all dependencies of those function are also safe to call.

I'd instead suggest for users to patch in the module where they are using the 
function instead.

I think that once any part of the stdlib is patched by mock, there are no 
guarantees that anything will work unless the patch provides a functioning 
implmentation.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41954] [mock] Recursion on mocking inspect.isfunction

2020-10-06 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +cjw296, lisroach, mariocj89, michael.foord, xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue41954] [mock] Recursion on mocking inspect.isfunction

2020-10-06 Thread Stanislav Levin


New submission from Stanislav Levin :

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com