This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author stestagg
Recipients stestagg, xxm
Date 2020-12-29.16:06:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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():
        print("!!! WE SHOULDN'T BE HERE!!!")

x = foo()
    for _ in x:
except NameError:

print("LOOP DONE")
del x   # <--- We shouldn't be here printed on this line.

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!).
Date User Action Args
2020-12-29 16:06:13stestaggsetrecipients: + stestagg, xxm
2020-12-29 16:06:13stestaggsetmessageid: <>
2020-12-29 16:06:13stestagglinkissue42762 messages
2020-12-29 16:06:13stestaggcreate