[issue14934] generator objects can clear their weakrefs before being resurrected

Nick Coghlan report at bugs.python.org
Sat Mar 30 08:39:58 EDT 2019


Nick Coghlan <ncoghlan at gmail.com> added the comment:

If I recall correctly, it's the generator destructor that handles throwing in ``GeneratorExit`` to get the generator to terminate. So this code can resurrect a generator as it's being collected by the GC:

    def resurrecting(resurrected):
        self = yield
        try:
            yield
        finally:
            resurrected.append(self)

    storage = []
    g = resurrecting(storage)
    g.send(g) # Give the generator a reference to itself
    del g # Now the generator is in a cycle with itself
    gc.collect()
    gc.collect()
    gc.collect()
    # Generator has been added to the storage instead of collected
    assert len(storage) == 1
    # Clear the storage to kill it for real this time
    storage.clear()
    # Weakrefs shouldn't get called until here

Antoine, does that sound right to you?

----------

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


More information about the Python-bugs-list mailing list