blob: 63240b5b6d5ccb9ced2fbd46c50e9f564d538b31 [file] [log] [blame]
Nick Coghlan1e420f82020-01-21 08:21:35 +10001/* Frame object interface */
2
3#ifndef Py_CPYTHON_FRAMEOBJECT_H
4# error "this header file must not be included directly"
5#endif
6
Mark Shannoncb9879b2020-07-17 11:44:23 +01007/* These values are chosen so that the inline functions below all
8 * compare f_state to zero.
9 */
10enum _framestate {
11 FRAME_CREATED = -2,
12 FRAME_SUSPENDED = -1,
13 FRAME_EXECUTING = 0,
14 FRAME_RETURNED = 1,
15 FRAME_UNWINDING = 2,
16 FRAME_RAISED = 3,
17 FRAME_CLEARED = 4
18};
19
20typedef signed char PyFrameState;
21
Nick Coghlan1e420f82020-01-21 08:21:35 +100022typedef struct {
23 int b_type; /* what kind of block this is */
24 int b_handler; /* where to jump to find handler */
25 int b_level; /* value stack level to pop to */
26} PyTryBlock;
27
Victor Stinner7c59d7c2020-04-28 16:32:48 +020028struct _frame {
Nick Coghlan1e420f82020-01-21 08:21:35 +100029 PyObject_VAR_HEAD
30 struct _frame *f_back; /* previous frame, or NULL */
31 PyCodeObject *f_code; /* code segment */
32 PyObject *f_builtins; /* builtin symbol table (PyDictObject) */
33 PyObject *f_globals; /* global symbol table (PyDictObject) */
34 PyObject *f_locals; /* local symbol table (any mapping) */
35 PyObject **f_valuestack; /* points after the last local */
Nick Coghlan1e420f82020-01-21 08:21:35 +100036 PyObject *f_trace; /* Trace function */
Mark Shannoncb9879b2020-07-17 11:44:23 +010037 int f_stackdepth; /* Depth of value stack */
Nick Coghlan1e420f82020-01-21 08:21:35 +100038 char f_trace_lines; /* Emit per-line trace events? */
39 char f_trace_opcodes; /* Emit per-opcode trace events? */
40
41 /* Borrowed reference to a generator, or NULL */
42 PyObject *f_gen;
43
44 int f_lasti; /* Last instruction if called */
45 /* Call PyFrame_GetLineNumber() instead of reading this field
46 directly. As of 2.3 f_lineno is only valid when tracing is
47 active (i.e. when f_trace is set). At other times we use
48 PyCode_Addr2Line to calculate the line from the current
49 bytecode index. */
50 int f_lineno; /* Current line number */
51 int f_iblock; /* index in f_blockstack */
Mark Shannoncb9879b2020-07-17 11:44:23 +010052 PyFrameState f_state; /* What state the frame is in */
Nick Coghlan1e420f82020-01-21 08:21:35 +100053 PyTryBlock f_blockstack[CO_MAXBLOCKS]; /* for try and loop blocks */
54 PyObject *f_localsplus[1]; /* locals+stack, dynamically sized */
Victor Stinner7c59d7c2020-04-28 16:32:48 +020055};
Nick Coghlan1e420f82020-01-21 08:21:35 +100056
Mark Shannoncb9879b2020-07-17 11:44:23 +010057static inline int _PyFrame_IsRunnable(struct _frame *f) {
58 return f->f_state < FRAME_EXECUTING;
59}
60
61static inline int _PyFrame_IsExecuting(struct _frame *f) {
62 return f->f_state == FRAME_EXECUTING;
63}
64
65static inline int _PyFrameHasCompleted(struct _frame *f) {
66 return f->f_state > FRAME_EXECUTING;
67}
Nick Coghlan1e420f82020-01-21 08:21:35 +100068
69/* Standard object interface */
70
71PyAPI_DATA(PyTypeObject) PyFrame_Type;
72
Dong-hee Na1b55b652020-02-17 19:09:15 +090073#define PyFrame_Check(op) Py_IS_TYPE(op, &PyFrame_Type)
Nick Coghlan1e420f82020-01-21 08:21:35 +100074
75PyAPI_FUNC(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *,
76 PyObject *, PyObject *);
77
78/* only internal use */
79PyFrameObject* _PyFrame_New_NoTrack(PyThreadState *, PyCodeObject *,
80 PyObject *, PyObject *);
81
82
83/* The rest of the interface is specific for frame objects */
84
85/* Block management functions */
86
87PyAPI_FUNC(void) PyFrame_BlockSetup(PyFrameObject *, int, int, int);
88PyAPI_FUNC(PyTryBlock *) PyFrame_BlockPop(PyFrameObject *);
89
90/* Conversions between "fast locals" and locals in dictionary */
91
92PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int);
93
94PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f);
95PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);
96
Nick Coghlan1e420f82020-01-21 08:21:35 +100097PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out);
98
Victor Stinner70364772020-04-29 03:28:46 +020099PyAPI_FUNC(PyFrameObject *) PyFrame_GetBack(PyFrameObject *frame);