cpython
cpython copied to clipboard
`generator.close()` doesn't clear frame on newly created generators
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
cc @markshannon - I think this is a bug, but I want to check in case this was an intentional behavior change.
It's a bug. Want to fix it or shall I?
I can make the PR