Yes, the GC is calling the __del__
cleanup hook of the generator, which in turn raises a GeneratorExit
in the generator function to exit the generator (by calling generator.close()
).
This means the context manager __exit__
hook will be called whenever a generator function is cleared from memory.
You can manually close the generator yourself first, with generator.close()
:
b.close()