[issue38911] include __del__ in Generator ABC

Matt McEwen report at bugs.python.org
Mon Nov 25 22:32:44 EST 2019


Matt McEwen <mmcewen at google.com> added the comment:

My interpretation of issue 24018 was that the Generator ABC was trying to follow the PEP as much as possible, so that users were able to produce a custom generator object and have it behave just like a builtin generator object.

I know that subclassing Generator is unusual; In my case that is a custom object that needs to be an iterator and also enter context managers internally. This seemed to me a very similar problem that the `close` machinery for generators was aimed to address, and subclassing generator worked really nicely, with one exception.

It took me some time to understand why the context managers weren't being exited correctly when the generator was interrupted, and it was because my `close` wasn't being called when the object fell out of scope, as it would have been if my object was a builtin generator object. Manually implementing __del__ fixed this.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38911>
_______________________________________


More information about the Python-bugs-list mailing list