Skip to content

gh-120321: Add gi_state, cr_state, and ag_state attributes - #144409

Merged
colesbury merged 6 commits into
python:mainfrom
colesbury:gh-120321-gen-state
Feb 3, 2026
Merged

gh-120321: Add gi_state, cr_state, and ag_state attributes#144409
colesbury merged 6 commits into
python:mainfrom
colesbury:gh-120321-gen-state

Conversation

@colesbury

@colesburycolesbury commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

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.

Also removes unused FRAME_COMPLETED state and renumbers the frame state enum to start at 0 instead of -1.


📚 Documentation preview 📚: https://cpython-previews--144409.org.readthedocs.build/

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.
Comment threadObjects/genobject.c Outdated
Comment threadObjects/genobject.c Outdated
Comment threadObjects/genobject.c Outdated
| | | one of ``GEN_CREATED``, |
| | | ``GEN_RUNNING``, |
| | | ``GEN_SUSPENDED``, or |
| | | ``GEN_CLOSED`` |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've edited the PR description and will use that when squash + merging.

colesburyand others added 3 commits February 3, 2026 10:18

@kumaraditya303kumaraditya303 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@colesbury
colesbury merged commit 7e2c9bd into python:mainFeb 3, 2026
47 checks passed
@colesbury
colesbury deleted the gh-120321-gen-state branch February 3, 2026 18:06
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
…hongh-144409)
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.
Also removes unused `FRAME_COMPLETED` state and renumbers the frame state enum
to start at 0 instead of -1.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@colesbury@vstinner@kumaraditya303