New submission from Matt McEwen <mmce...@google.com>:

The Generator ABC in the standard library lets users define objects that follow 
the Generator specification given in PEP342, and which can be used in the place 
of builtin generator objects. 

This was originally added in issue 24018

The ABC enforces that the methods `send`, `throw` and `close` are implemented, 
as per the spec. It doesn't enforce that `__del__` is implemented. PEP342 
specifies that `__del__` be a wrapper for `close`, such that when a generator 
object is garbage collected `close` is called.

For a user (like me) implementing such a generator object by inheriting the 
Generator ABC, the resulting objects do not have `close` called when they are 
garbage collected. Fixing this requires manually implementing a `__del__` that 
calls `close`. 

Ideally from the user perspective, inheriting from Generator should be enough 
to guarantee that the object behaves like a generator object, provided the 
abstract methods are correctly implemented. This could be easily achieved by 
implementing `__del__` concretely in the ABC as a wrapper for `close`:

def __del__(self):
    self.close()

----------
components: Library (Lib)
messages: 357467
nosy: Matt McEwen
priority: normal
severity: normal
status: open
title: include __del__ in Generator ABC
type: enhancement
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38911>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to