Steve Stagg <[email protected]> added the comment:
I'm sorry, I did get a bit confused earlier, I'd mentally switched to context
managers.
I agree this is a bug, and a kinda weird one!
I've narrowed it down to this:
If an exception causes flow to exit a for-loop that's powered by a generator,
then when the generator object is deleted, GeneratorExit() is incorrectly
raised in the generator.
This can be shown with the following example (easier to debug without the
infinite loop):
---
def foo():
try:
yield
except:
print("!!! WE SHOULDN'T BE HERE!!!")
x = foo()
try:
for _ in x:
print(i)
except NameError:
pass
print("LOOP DONE")
del x # <--- We shouldn't be here printed on this line.
print("FINAL")
---
As you discovered, if you change print(i) to print(1), then the "shouldn't be
here" line is NOT printed, but if you leave it as print(i) then the exception
is printed.
You can see that the error doesn't happen until after LOOP DONE, which is
because `del x` is finalizing the generator object, and the invalid exception
logic happens then.
I'm trying to get more info here, if I don't by the time you come online, I'd
recommend creating a *new* issue, with the non-loop example above, and
explanation because I think on this issue, I've caused a lot of noise (sorry
again!).
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue42762>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com