Message 339923 - Python tracker (original) (raw)

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