|
| 1 | +// This is a redefinition of the private/opaque frame object. |
| 2 | +// |
| 3 | +// https://github.com/python/cpython/blob/3.8/Include/frameobject.h#L16 |
| 4 | +// |
| 5 | +// In Python <= 3.10, `struct _frame` is both the PyFrameObject and |
| 6 | +// PyInterpreterFrame. From Python 3.11 onwards, the two were split with the |
| 7 | +// PyFrameObject (struct _frame) pointing to struct _PyInterpreterFrame. |
| 8 | +struct Frame { |
| 9 | + PyObject_VAR_HEAD |
| 10 | + struct Frame *f_back; // struct _frame |
| 11 | + PyCodeObject *f_code; |
| 12 | + PyObject *f_builtins; |
| 13 | + PyObject *f_globals; |
| 14 | + PyObject *f_locals; |
| 15 | + PyObject **f_valuestack; |
| 16 | + PyObject **f_stacktop; |
| 17 | + PyObject *f_trace; |
| 18 | + char f_trace_lines; |
| 19 | + char f_trace_opcodes; |
| 20 | + PyObject *f_gen; |
| 21 | + int f_lasti; |
| 22 | + int f_lineno; |
| 23 | + int f_iblock; |
| 24 | + char f_executing; |
| 25 | + PyTryBlock f_blockstack[CO_MAXBLOCKS]; |
| 26 | + PyObject *f_localsplus[1]; |
| 27 | +}; |
| 28 | + |
| 29 | +// Python 3.9 and prior didn't have an explicit enum of frame states, |
| 30 | +// but we can derive them based on the presence of a frame, and other |
| 31 | +// information found on the frame, for compatibility with later versions. |
| 32 | +typedef enum _framestate { |
| 33 | + FRAME_CREATED = -2, |
| 34 | + FRAME_EXECUTING = 0, |
| 35 | + FRAME_CLEARED = 4 |
| 36 | +} FrameState; |
| 37 | + |
| 38 | +/* |
| 39 | +// This is the definition of PyGenObject for reference to developers |
| 40 | +// working on the extension. |
| 41 | +// |
| 42 | +// Note that PyCoroObject and PyAsyncGenObject have the same layout as |
| 43 | +// PyGenObject, however the struct fields have a cr_ and ag_ prefix |
| 44 | +// (respectively) rather than a gi_ prefix. In Python <= 3.10, PyCoroObject |
| 45 | +// and PyAsyncGenObject have extra fields compared to PyGenObject. In Python |
| 46 | +// 3.11 onwards, the three objects are identical (except for field name |
| 47 | +// prefixes). The extra fields in Python <= 3.10 are not applicable to the |
| 48 | +// extension at this time. |
| 49 | +// |
| 50 | +// https://github.com/python/cpython/blob/3.8/Include/genobject.h#L17 |
| 51 | +typedef struct { |
| 52 | + PyObject_HEAD |
| 53 | + PyFrameObject *gi_frame; |
| 54 | + char gi_running; |
| 55 | + PyObject *gi_code; |
| 56 | + PyObject *gi_weakreflist; |
| 57 | + PyObject *gi_name; |
| 58 | + PyObject *gi_qualname; |
| 59 | + _PyErr_StackItem gi_exc_state; |
| 60 | +} PyGenObject; |
| 61 | +*/ |
| 62 | + |
| 63 | +static Frame *get_frame(PyGenObject *gen_like) { |
| 64 | + Frame *frame = (Frame *)(gen_like->gi_frame); |
| 65 | + assert(frame); |
| 66 | + return frame; |
| 67 | +} |
| 68 | + |
| 69 | +static PyCodeObject *get_frame_code(Frame *frame) { |
| 70 | + PyCodeObject *code = frame->f_code; |
| 71 | + assert(code); |
| 72 | + return code; |
| 73 | +} |
| 74 | + |
| 75 | +static int get_frame_lasti(Frame *frame) { |
| 76 | + return frame->f_lasti; |
| 77 | +} |
| 78 | + |
| 79 | +static void set_frame_lasti(Frame *frame, int lasti) { |
| 80 | + frame->f_lasti = lasti; |
| 81 | +} |
| 82 | + |
| 83 | +static int get_frame_state(PyGenObject *gen_like) { |
| 84 | + // Python 3.8 doesn't have frame states, but we can derive |
| 85 | + // some for compatibility with later versions and to simplify |
| 86 | + // the extension. |
| 87 | + Frame *frame = (Frame *)(gen_like->gi_frame); |
| 88 | + if (!frame) { |
| 89 | + return FRAME_CLEARED; |
| 90 | + } |
| 91 | + return frame->f_executing ? FRAME_EXECUTING : FRAME_CREATED; |
| 92 | +} |
| 93 | + |
| 94 | +static void set_frame_state(PyGenObject *gen_like, int fs) { |
| 95 | + Frame *frame = get_frame(gen_like); |
| 96 | + frame->f_executing = (fs == FRAME_EXECUTING); |
| 97 | +} |
| 98 | + |
| 99 | +static int valid_frame_state(int fs) { |
| 100 | + return fs == FRAME_CREATED || fs == FRAME_EXECUTING || fs == FRAME_CLEARED; |
| 101 | +} |
| 102 | + |
| 103 | +static int get_frame_stacktop_limit(Frame *frame) { |
| 104 | + PyCodeObject *code = get_frame_code(frame); |
| 105 | + return code->co_stacksize + code->co_nlocals; |
| 106 | +} |
| 107 | + |
| 108 | +static int get_frame_stacktop(Frame *frame) { |
| 109 | + assert(frame->f_localsplus); |
| 110 | + int stacktop = (int)(frame->f_stacktop - frame->f_localsplus); |
| 111 | + assert(stacktop >= 0 && stacktop < get_frame_stacktop_limit(frame)); |
| 112 | + return stacktop; |
| 113 | +} |
| 114 | + |
| 115 | +static void set_frame_stacktop(Frame *frame, int stacktop) { |
| 116 | + assert(stacktop >= 0 && stacktop < get_frame_stacktop_limit(frame)); |
| 117 | + assert(frame->f_localsplus); |
| 118 | + frame->f_stacktop = frame->f_localsplus + stacktop; |
| 119 | +} |
| 120 | + |
| 121 | +static PyObject **get_frame_localsplus(Frame *frame) { |
| 122 | + PyObject **localsplus = frame->f_localsplus; |
| 123 | + assert(localsplus); |
| 124 | + return localsplus; |
| 125 | +} |
| 126 | + |
| 127 | +static int get_frame_iblock_limit(Frame *frame) { |
| 128 | + return CO_MAXBLOCKS; |
| 129 | +} |
| 130 | + |
| 131 | +static int get_frame_iblock(Frame *frame) { |
| 132 | + return frame->f_iblock; |
| 133 | +} |
| 134 | + |
| 135 | +static void set_frame_iblock(Frame *frame, int iblock) { |
| 136 | + assert(iblock >= 0 && iblock < get_frame_iblock_limit(frame)); |
| 137 | + frame->f_iblock = iblock; |
| 138 | +} |
| 139 | + |
| 140 | +static PyTryBlock *get_frame_blockstack(Frame *frame) { |
| 141 | + PyTryBlock *blockstack = frame->f_blockstack; |
| 142 | + assert(blockstack); |
| 143 | + return blockstack; |
| 144 | +} |
| 145 | + |
0 commit comments