gh-120321: Add gi_state, cr_state, and ag_state attributes#144409
Merged
colesbury merged 6 commits intopython:mainfrom Feb 3, 2026
Merged
gh-120321: Add gi_state, cr_state, and ag_state attributes#144409colesbury merged 6 commits intopython:mainfrom
colesbury merged 6 commits intopython:mainfrom
Conversation
Add `gi_state`, `cr_state`, and `ag_state` attributes to generators, coroutines, and async generators respectively. These attributes return the current state as a string (e.g., `GEN_RUNNING`, `CORO_SUSPENDED`). The `inspect.getgeneratorstate()`, `inspect.getcoroutinestate()`, and `inspect.getasyncgenstate()` functions now return these attributes directly. This is in preparation for making `gi_frame` thread-safe, which may involve stop-the-world synchronization. The new state attributes avoid potential performance cliffs in `inspect.getgeneratorstate()` and similar functions by not requiring frame access.
vstinner
reviewed
Feb 3, 2026
| | | | one of ``GEN_CREATED``, | | ||
| | | | ``GEN_RUNNING``, | | ||
| | | | ``GEN_SUSPENDED``, or | | ||
| | | | ``GEN_CLOSED`` | |
Member
There was a problem hiding this comment.
Please add a .. versionchanged:: next markup at the end of the table to mention that gi_state, ag_state, cr_state are added in Python 3.15.
| FRAME_SUSPENDED_YIELD_FROM = -2, | ||
| FRAME_SUSPENDED_YIELD_FROM_LOCKED = -1, | ||
| FRAME_EXECUTING = 0, | ||
| FRAME_COMPLETED = 1, |
Member
There was a problem hiding this comment.
Don't forget to mention FRAME_COMPLETED removal in the commit message. I'm fine with the removal, the constant was only used in one place: in FRAME_STATE_FINISHED().
Contributor
Author
There was a problem hiding this comment.
I've edited the PR description and will use that when squash + merging.
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
gi_state,cr_state, andag_stateattributes to generators, coroutines, and async generators respectively. These attributes return the current state as a string (e.g.,GEN_RUNNING,CORO_SUSPENDED).The
inspect.getgeneratorstate(),inspect.getcoroutinestate(), andinspect.getasyncgenstate()functions now return these attributes directly.This is in preparation for making
gi_framethread-safe, which may involve stop-the-world synchronization. The new state attributes avoid potential performance cliffs ininspect.getgeneratorstate()and similar functions by not requiring frame access.Also removes unused
FRAME_COMPLETEDstate and renumbers the frame state enum to start at 0 instead of -1.📚 Documentation preview 📚: https://cpython-previews--144409.org.readthedocs.build/