[issue36598] mock side_effect should be checked for iterable not callable

2019-04-12 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

Thanks, I am closing this as not a bug. Feel free to reopen this if I have 
missed any.

--
resolution:  -> not a bug
stage: patch review -> 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



[issue36598] mock side_effect should be checked for iterable not callable

2019-04-11 Thread Gregory Ronin


Gregory Ronin  added the comment:

You are right. I was not calling generator the right way in mock. After I tried 
your suggestion it works.

--

___
Python tracker 

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



[issue36598] mock side_effect should be checked for iterable not callable

2019-04-11 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
pull_requests:  -12717

___
Python tracker 

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



[issue36598] mock side_effect should be checked for iterable not callable

2019-04-11 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
keywords: +patch
pull_requests: +12717
stage:  -> patch review

___
Python tracker 

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



[issue36598] mock side_effect should be checked for iterable not callable

2019-04-10 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

I am not sure if the snippets you are referring to are from testing-cabal/mock 
repo which could be different from master branch. Current code is at [0]

if effect is not None:
if _is_exception(effect):
raise effect
elif not _callable(effect):
result = next(effect)
if _is_exception(result):
raise result
else:
result = effect(*args, **kwargs)

if result is not DEFAULT:
return result

> 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.

This does seem to work for generator function as below. Sorry, maybe I am 
getting it wrong with respect to terminologies and understanding the issue. Can 
you add a short script around what you are expecting?

$ cat ../backups/bpo36598.py
from unittest.mock import patch

def gen(i):
while i < 5:
yield i
i += 1

def foo():
return 1

with patch('__main__.foo', side_effect=gen(0)):
for _ in range(2):
print(foo())
for _ in range(2):
print(foo())
$ ./python.exe ../backups/bpo36598.py
0
1
2
3

[0] 
https://github.com/python/cpython/blob/a9bd8925c7fa50dd3cfab125b824ec192133ef49/Lib/unittest/mock.py#L1043

--

___
Python tracker 

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



[issue36598] mock side_effect should be checked for iterable not callable

2019-04-10 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +xtreak
versions: +Python 3.8 -Python 2.7

___
Python tracker 

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



[issue36598] mock side_effect should be checked for iterable not callable

2019-04-10 Thread Gregory Ronin


New submission from Gregory Ronin :

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 

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