Catch StopIteration explicitly in group_by_object generator (#264)

Python 3.7 changed the behaviour of generators so that a StopIteration exception which bubbles up inside a generator is transformed into a RuntimeException. This means that when calling next() inside a generator, we have to explicitly catch the StopIteration and exit the function instead of relying on it to implicitly stop the generator. Otherwise the program will crash. Sigh.

For more information, see PEP 479.

https://www.python.org/dev/peps/pep-0479/
https://stackoverflow.com/questions/51700960/runtimeerror-generator-raised-stopiteration-every-time-i-try-to-run-app/51701040#51701040
This commit is contained in:
Kip Yin 2019-07-16 04:23:37 +08:00 committed by Andrew Ekstedt
parent c63b6522bf
commit 11f972f817

View file

@ -377,7 +377,10 @@ def group_by_object(stream):
Yields ((class name, object ID), (list of messages)) pairs.
"""
stream = iter(stream)
current = next(stream)
try:
current = next(stream)
except StopIteration:
return
current_key = current.cls, current.id
group = [current]
for message in stream: