Python 生成器抽象基础 class 没有实现必要的 __del__,这是故意的吗?

The Python Generator abstract base class doesn't implement the necessary __del__, is this intentional?

Python 中的生成器对象需要有一个 close 方法,以确保上下文管理器退出并且 try...finally: 块在对象被 运行 之前存在收集垃圾。

PEP 342 定义生成器必须实现的方法 sendthrowclose__del__。具体来说,它指出:

g.__del__() is a wrapper for g.close(). This will be called when the generator object is garbage-collected (in CPython, this is when its reference count goes to zero).

生成器的抽象类型在collections.abc

class Generator(Iterator):

    __slots__ = ()

    def __next__(self):
        """Return the next item from the generator.
        When exhausted, raise StopIteration.
        """
        return self.send(None)

    @abstractmethod
    def send(self, value):
        """Send a value into the generator.
        Return next yielded value or raise StopIteration.
        """
        raise StopIteration

    @abstractmethod
    def throw(self, typ, val=None, tb=None):
        """Raise an exception in the generator.
        Return next yielded value or raise StopIteration.
        """
        if val is None:
            if tb is None:
                raise typ
            val = typ()
        if tb is not None:
            val = val.with_traceback(tb)
        raise val

    def close(self):
        """Raise GeneratorExit inside generator.
        """
        try:
            self.throw(GeneratorExit)
        except (GeneratorExit, StopIteration):
            pass
        else:
            raise RuntimeError("generator ignored GeneratorExit")

    @classmethod
    def __subclasshook__(cls, C):
        if cls is Generator:
            return _check_methods(C, '__iter__', '__next__',
                                  'send', 'throw', 'close')
        return NotImplemented

此抽象类型强制 sendthrowclose 在子类中实现,但不抽象或具体地实现 __del__,或强制执行它已实施。它的元类也没有。

天真地,生成一个不手动定义 __del__ 的子类,它包装 close 给出了之后没有正确清理的生成器。垃圾收集器只调用 __del__,因此如果 __del__ 不存在,则不会调用 close

这是故意的吗?

, snakecharmerb pointed out to me that __del__ can be fraught to implement, as indicated by the language reference 中,但我不明白为什么这也不适用于 __del__ 作为 [=52= 中 close 的包装器的正确实现]的原生生成器对象。

好像是故意的。在 python bugtracker 上的 issue 讨论这个问题时,Guido 说

The presence of a __del__ method can cause subtle behavior changes to the GC, so I worry that adding __del__ to that class now is going to break currently-working code.

Let's not destabilize the Generator class.