◐ Shell
clean mode source ↗

gh-120321: Add gi_state, cr_state, and ag_state attributes by colesbury · Pull Request #144409 · python/cpython

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.

kumaraditya303

kumaraditya303

kumaraditya303

vstinner

Co-authored-by: Kumar Aditya <kumaraditya@python.org>

kumaraditya303

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.