[issue36598] mock side_effect should be checked for iterable not callable
Gregory Ronin
report at bugs.python.org
Thu Apr 11 00:12:58 EDT 2019
New submission from Gregory Ronin <gregoryronin at yahoo.com>:
In mock.py, in method:
def _mock_call(_mock_self, *args, **kwargs):
There is a following piece of code:
if not _callable(effect):
result = next(effect)
if _is_exception(result):
raise result
if result is DEFAULT:
result = self.return_value
return result
ret_val = effect(*args, **kwargs)
This works correctly for iterables (such as lists) that are not defined as generators.
However, if one defined a generator as a function this would not work.
It seems like the check should be not for callable, but for iterable:
try:
iter(effect)
except TypeError:
# If not iterable then callable or exception
if _callable(effect):
ret_val = effect(*args, **kwargs)
else:
raise effect
else: # Iterable
result = next(effect)
if _is_exception(result):
raise result
if result is DEFAULT:
result = self.return_value
return result
----------
components: Tests
messages: 339923
nosy: jazzblue
priority: normal
severity: normal
status: open
title: mock side_effect should be checked for iterable not callable
type: behavior
versions: Python 2.7
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36598>
_______________________________________
More information about the Python-bugs-list
mailing list