Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 1 | /* Frame object interface */ |
| 2 | |
| 3 | #ifndef Py_CPYTHON_FRAMEOBJECT_H |
| 4 | # error "this header file must not be included directly" |
| 5 | #endif |
| 6 | |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 7 | /* These values are chosen so that the inline functions below all |
| 8 | * compare f_state to zero. |
| 9 | */ |
| 10 | enum _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 | |
| 20 | typedef signed char PyFrameState; |
| 21 | |
Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 22 | typedef 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 Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 28 | struct _frame { |
Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 29 | 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 Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 36 | PyObject *f_trace; /* Trace function */ |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 37 | int f_stackdepth; /* Depth of value stack */ |
Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 38 | 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 */ |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame^] | 45 | int f_lineno; /* Current line number. Only valid if non-zero */ |
Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 46 | int f_iblock; /* index in f_blockstack */ |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 47 | PyFrameState f_state; /* What state the frame is in */ |
Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 48 | PyTryBlock f_blockstack[CO_MAXBLOCKS]; /* for try and loop blocks */ |
| 49 | PyObject *f_localsplus[1]; /* locals+stack, dynamically sized */ |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 50 | }; |
Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 51 | |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 52 | static inline int _PyFrame_IsRunnable(struct _frame *f) { |
| 53 | return f->f_state < FRAME_EXECUTING; |
| 54 | } |
| 55 | |
| 56 | static inline int _PyFrame_IsExecuting(struct _frame *f) { |
| 57 | return f->f_state == FRAME_EXECUTING; |
| 58 | } |
| 59 | |
| 60 | static inline int _PyFrameHasCompleted(struct _frame *f) { |
| 61 | return f->f_state > FRAME_EXECUTING; |
| 62 | } |
Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 63 | |
| 64 | /* Standard object interface */ |
| 65 | |
| 66 | PyAPI_DATA(PyTypeObject) PyFrame_Type; |
| 67 | |
Dong-hee Na | 1b55b65 | 2020-02-17 19:09:15 +0900 | [diff] [blame] | 68 | #define PyFrame_Check(op) Py_IS_TYPE(op, &PyFrame_Type) |
Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 69 | |
| 70 | PyAPI_FUNC(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *, |
| 71 | PyObject *, PyObject *); |
| 72 | |
| 73 | /* only internal use */ |
| 74 | PyFrameObject* _PyFrame_New_NoTrack(PyThreadState *, PyCodeObject *, |
| 75 | PyObject *, PyObject *); |
| 76 | |
| 77 | |
| 78 | /* The rest of the interface is specific for frame objects */ |
| 79 | |
| 80 | /* Block management functions */ |
| 81 | |
| 82 | PyAPI_FUNC(void) PyFrame_BlockSetup(PyFrameObject *, int, int, int); |
| 83 | PyAPI_FUNC(PyTryBlock *) PyFrame_BlockPop(PyFrameObject *); |
| 84 | |
| 85 | /* Conversions between "fast locals" and locals in dictionary */ |
| 86 | |
| 87 | PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int); |
| 88 | |
| 89 | PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f); |
| 90 | PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *); |
| 91 | |
Nick Coghlan | 1e420f8 | 2020-01-21 08:21:35 +1000 | [diff] [blame] | 92 | PyAPI_FUNC(void) _PyFrame_DebugMallocStats(FILE *out); |
| 93 | |
Victor Stinner | 7036477 | 2020-04-29 03:28:46 +0200 | [diff] [blame] | 94 | PyAPI_FUNC(PyFrameObject *) PyFrame_GetBack(PyFrameObject *frame); |