Python Forum
Mock call to subprocess.Popen failing with StopIteration - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Mock call to subprocess.Popen failing with StopIteration (/thread-22315.html)



Mock call to subprocess.Popen failing with StopIteration - tharpa - Nov-07-2019

A unit test that I have inherited is now failing on

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
logging.warning('Not hitting here.')
with a traceback of

Error:
File "/usr1/tsmith/miniconda3/lib/python3.7/unittest/mock.py", line 960, in __call__ return _mock_self._mock_call(*args, **kwargs) File "/usr1/tsmith/miniconda3/lib/python3.7/unittest/mock.py", line 1022, in _mock_call result = next(effect)
StopIteration`

Any idea how to investigate this?


RE: subprocess.Popen failing with StopIteration - Gribouillis - Nov-07-2019

StopIteration is the normal exception sent by an iterator at the end of its iteration. For example
>>> L = [1, 2, 3]
>>> effect = iter(L)
>>> next(effect)
1
>>> next(effect)
2
>>> next(effect)
3
>>> next(effect)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
It means that the iterable 'effect' was exhausted.


RE: subprocess.Popen failing with StopIteration - tharpa - Nov-07-2019

(Nov-07-2019, 08:57 PM)Gribouillis Wrote: StopIteration is the normal exception sent by an iterator at the end of its iteration.
It means that the iterable 'effect' was exhausted.

If it was not intentional that it be called too many times, does it imply an error?


RE: subprocess.Popen failing with StopIteration - Gribouillis - Nov-07-2019

Yes it implies an error because nobody writes applications that crash. The error comes from the code that calls the unittest.mock module. I suppose the error traceback is longer than what you showed. Look for the last line from the application code in that traceback.


RE: subprocess.Popen failing with StopIteration - tharpa - Nov-07-2019

Yes, the line before it is about application code.
Error:
File "/usr1/tsmith/git/gg_database.py", line 89, in get_database_size_via_subprocess p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
And line 89 of the application code is:
side_effect = [
            MockPopenResponse(stdout=stdout, status_code=self.returncode)
        ]
followed by
 logging.warning('td90 se count = ' + str(len(side_effect)))
which returns
Output:
WARNING:root:td90 se count = 1
But I cannot seem to figure out where it is calling the mocked function when side_effect is empty.


RE: subprocess.Popen failing with StopIteration - Gribouillis - Nov-07-2019

So the title of the thread is incorrect, it should be 'mocked call to subprocess.Popen failing...'. I'm afraid this is a little too hard for me, I don't have much experience with the mock module, but perhaps someone else in the forum can answer this. It really looks like a issue with the testing rather than with the applicatino code.


RE: subprocess.Popen failing with StopIteration - tharpa - Nov-08-2019

(Nov-07-2019, 10:23 PM)Gribouillis Wrote: So the title of the thread is incorrect, it should be 'mocked call to subprocess.Popen failing...'.
It doesn't look like I'm able to change the title, but as a moderator, I would bet that you're able to. You're welcome to as far as I'm concerned.


RE: Mock call to subprocess.Popen failing with StopIteration - Gribouillis - Nov-08-2019

tharpa Wrote:You're welcome to as far as I'm concerned.
Done.