2025-03-19 18:17:44 +01:00
|
|
|
/* See InternalDocs/frames.md for an explanation of the frame stack
|
|
|
|
|
* including explanation of the PyFrameObject and _PyInterpreterFrame
|
|
|
|
|
* structs. */
|
|
|
|
|
|
2021-05-21 10:57:35 +01:00
|
|
|
#ifndef Py_INTERNAL_FRAME_H
|
|
|
|
|
#define Py_INTERNAL_FRAME_H
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-08-21 19:15:52 +02:00
|
|
|
#ifndef Py_BUILD_CORE
|
|
|
|
|
# error "this header requires Py_BUILD_CORE define"
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-03-19 15:23:32 +01:00
|
|
|
#include "pycore_typedefs.h" // _PyInterpreterFrame
|
2022-01-05 03:30:26 -08:00
|
|
|
|
2022-04-11 16:05:20 +01:00
|
|
|
|
2022-02-25 12:53:19 +01:00
|
|
|
struct _frame {
|
|
|
|
|
PyObject_HEAD
|
|
|
|
|
PyFrameObject *f_back; /* previous frame, or NULL */
|
2025-03-19 15:23:32 +01:00
|
|
|
_PyInterpreterFrame *f_frame; /* points to the frame data */
|
2022-02-25 12:53:19 +01:00
|
|
|
PyObject *f_trace; /* Trace function */
|
|
|
|
|
int f_lineno; /* Current line number. Only valid if non-zero */
|
|
|
|
|
char f_trace_lines; /* Emit per-line trace events? */
|
|
|
|
|
char f_trace_opcodes; /* Emit per-opcode trace events? */
|
2024-05-04 04:12:10 -07:00
|
|
|
PyObject *f_extra_locals; /* Dict for locals set by users using f_locals, could be NULL */
|
2024-07-16 12:17:47 -07:00
|
|
|
/* This is purely for backwards compatibility for PyEval_GetLocals.
|
|
|
|
|
PyEval_GetLocals requires a borrowed reference so the actual reference
|
|
|
|
|
is stored here */
|
|
|
|
|
PyObject *f_locals_cache;
|
2025-04-01 10:18:42 -07:00
|
|
|
/* A tuple containing strong references to fast locals that were overwritten
|
|
|
|
|
* via f_locals. Borrowed references to these locals may exist in frames
|
|
|
|
|
* closer to the top of the stack. The references in this tuple act as
|
|
|
|
|
* "support" for the borrowed references, ensuring that they remain valid.
|
|
|
|
|
*/
|
|
|
|
|
PyObject *f_overwritten_fast_locals;
|
2022-02-25 12:53:19 +01:00
|
|
|
/* The frame data, if this frame object owns the frame */
|
|
|
|
|
PyObject *_f_frame_data[1];
|
|
|
|
|
};
|
2021-12-09 12:59:26 -07:00
|
|
|
|
2022-02-25 16:07:14 +01:00
|
|
|
extern PyFrameObject* _PyFrame_New_NoTrack(PyCodeObject *code);
|
|
|
|
|
|
2021-12-09 12:59:26 -07:00
|
|
|
|
|
|
|
|
/* other API */
|
|
|
|
|
|
2022-03-22 12:57:19 +00:00
|
|
|
typedef enum _framestate {
|
gh-120321: Add gi_state, cr_state, and ag_state attributes (gh-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.
2026-02-03 13:06:32 -05:00
|
|
|
FRAME_CREATED = 0,
|
|
|
|
|
FRAME_SUSPENDED = 1,
|
|
|
|
|
FRAME_SUSPENDED_YIELD_FROM = 2,
|
|
|
|
|
FRAME_SUSPENDED_YIELD_FROM_LOCKED = 3,
|
|
|
|
|
FRAME_EXECUTING = 4,
|
|
|
|
|
FRAME_CLEARED = 5
|
2022-03-22 12:57:19 +00:00
|
|
|
} PyFrameState;
|
2021-05-21 10:57:35 +01:00
|
|
|
|
2026-01-30 12:20:27 -05:00
|
|
|
#define FRAME_STATE_SUSPENDED(S) ((S) >= FRAME_SUSPENDED && (S) <= FRAME_SUSPENDED_YIELD_FROM_LOCKED)
|
gh-120321: Add gi_state, cr_state, and ag_state attributes (gh-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.
2026-02-03 13:06:32 -05:00
|
|
|
#define FRAME_STATE_FINISHED(S) ((S) == FRAME_CLEARED)
|
2021-05-21 10:57:35 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif /* !Py_INTERNAL_FRAME_H */
|