cpython icon indicating copy to clipboard operation
cpython copied to clipboard

`generator.close()` doesn't clear frame on newly created generators

Open colesbury opened this issue 2 weeks ago • 3 comments

Bug report

Bug description:

Since Python 3.12, generator.close() and generator.gi_frame.clear() do not clear the generator's frame, if the generator is in the FRAME_CREATED state.

The following passes on Python 3.11 and earlier and fails on Python 3.12+

class DetectDeletion:
    deleted = False

    def __del__(self):
        DetectDeletion.deleted = True

def gen(x): yield 1

g = gen(DetectDeletion())
g.close()  # or alternatively: g.gi_frame.clear()
assert DetectDeletion.deleted
del g

Introduced in:

  • https://github.com/python/cpython/pull/101013

CPython versions tested on:

3.11, 3.12, 3.14, 3.13, 3.15

Operating systems tested on:

No response

Linked PRs

  • gh-142838

colesbury avatar Dec 15 '25 20:12 colesbury

cc @markshannon - I think this is a bug, but I want to check in case this was an intentional behavior change.

colesbury avatar Dec 15 '25 20:12 colesbury

It's a bug. Want to fix it or shall I?

markshannon avatar Dec 16 '25 11:12 markshannon

I can make the PR

colesbury avatar Dec 16 '25 16:12 colesbury