Open
Description
Specifically, use of with_iter is dangerous for pypy users because if the entire iterator isn't consumed the __exit__
statement won't be called until the next major garbage collection. This could be never and the program can exit without ever calling __exit__
. We have already seen some resource starvation in a production application from this.
Unfortunately this is just a quite niche difference between cpython and pypy, which is not going to change, so it seems best just to warn folks about this.
To see the difference, run the following under python3 / pypy3:
from more_itertools import with_iter
class Manager:
def __iter__(self):
yield
def __enter__(self, *args, **kwargs):
print("enter")
return self
def __exit__(self, *args, **kwargs):
print("exit")
for _ in with_iter(Manager()):
break # exit called under python3, but not pypy3
try:
for _ in with_iter(Manager()):
raise Exception("oops") # same
except Exception:
pass
Metadata
Metadata
Assignees
Labels
No labels