Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1 | /* Frame object implementation */ |
| 2 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 4 | #include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals() |
| 5 | #include "pycore_object.h" // _PyObject_GC_UNTRACK() |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 6 | |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 7 | #include "frameobject.h" // PyFrameObject |
| 8 | #include "opcode.h" // EXTENDED_ARG |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 9 | #include "structmember.h" // PyMemberDef |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 11 | #define OFF(x) offsetof(PyFrameObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 13 | static PyMemberDef frame_memberlist[] = { |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 14 | {"f_back", T_OBJECT, OFF(f_back), READONLY}, |
| 15 | {"f_code", T_OBJECT, OFF(f_code), READONLY}, |
| 16 | {"f_builtins", T_OBJECT, OFF(f_builtins), READONLY}, |
| 17 | {"f_globals", T_OBJECT, OFF(f_globals), READONLY}, |
| 18 | {"f_lasti", T_INT, OFF(f_lasti), READONLY}, |
Nick Coghlan | 5a85167 | 2017-09-08 10:14:16 +1000 | [diff] [blame] | 19 | {"f_trace_lines", T_BOOL, OFF(f_trace_lines), 0}, |
| 20 | {"f_trace_opcodes", T_BOOL, OFF(f_trace_opcodes), 0}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 21 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 22 | }; |
| 23 | |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 24 | static struct _Py_frame_state * |
| 25 | get_frame_state(void) |
| 26 | { |
| 27 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 28 | return &interp->frame; |
| 29 | } |
| 30 | |
| 31 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 32 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 33 | frame_getlocals(PyFrameObject *f, void *closure) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 34 | { |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 35 | if (PyFrame_FastToLocalsWithError(f) < 0) |
| 36 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | Py_INCREF(f->f_locals); |
| 38 | return f->f_locals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 41 | int |
| 42 | PyFrame_GetLineNumber(PyFrameObject *f) |
| 43 | { |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 44 | assert(f != NULL); |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 45 | if (f->f_lineno != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | return f->f_lineno; |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 47 | } |
| 48 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | return PyCode_Addr2Line(f->f_code, f->f_lasti); |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 50 | } |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 53 | static PyObject * |
| 54 | frame_getlineno(PyFrameObject *f, void *closure) |
| 55 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | return PyLong_FromLong(PyFrame_GetLineNumber(f)); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 59 | |
| 60 | /* Given the index of the effective opcode, |
| 61 | scan back to construct the oparg with EXTENDED_ARG */ |
| 62 | static unsigned int |
| 63 | get_arg(const _Py_CODEUNIT *codestr, Py_ssize_t i) |
| 64 | { |
| 65 | _Py_CODEUNIT word; |
| 66 | unsigned int oparg = _Py_OPARG(codestr[i]); |
| 67 | if (i >= 1 && _Py_OPCODE(word = codestr[i-1]) == EXTENDED_ARG) { |
| 68 | oparg |= _Py_OPARG(word) << 8; |
| 69 | if (i >= 2 && _Py_OPCODE(word = codestr[i-2]) == EXTENDED_ARG) { |
| 70 | oparg |= _Py_OPARG(word) << 16; |
| 71 | if (i >= 3 && _Py_OPCODE(word = codestr[i-3]) == EXTENDED_ARG) { |
| 72 | oparg |= _Py_OPARG(word) << 24; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return oparg; |
| 77 | } |
| 78 | |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 79 | typedef enum kind { |
| 80 | With = 1, |
| 81 | Loop = 2, |
| 82 | Try = 3, |
| 83 | Except = 4, |
| 84 | } Kind; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 85 | |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 86 | #define BITS_PER_BLOCK 3 |
| 87 | |
| 88 | static inline int64_t |
| 89 | push_block(int64_t stack, Kind kind) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 90 | { |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 91 | assert(stack < ((int64_t)1)<<(BITS_PER_BLOCK*CO_MAXBLOCKS)); |
| 92 | return (stack << BITS_PER_BLOCK) | kind; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 95 | static inline int64_t |
| 96 | pop_block(int64_t stack) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 97 | { |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 98 | assert(stack > 0); |
| 99 | return stack >> BITS_PER_BLOCK; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 102 | static inline Kind |
| 103 | top_block(int64_t stack) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 104 | { |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 105 | return stack & ((1<<BITS_PER_BLOCK)-1); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 108 | static int64_t * |
| 109 | markblocks(PyCodeObject *code_obj, int len) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 110 | { |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 111 | const _Py_CODEUNIT *code = |
| 112 | (const _Py_CODEUNIT *)PyBytes_AS_STRING(code_obj->co_code); |
| 113 | int64_t *blocks = PyMem_New(int64_t, len+1); |
| 114 | int i, j, opcode; |
| 115 | |
| 116 | if (blocks == NULL) { |
| 117 | PyErr_NoMemory(); |
| 118 | return NULL; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 119 | } |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 120 | memset(blocks, -1, (len+1)*sizeof(int64_t)); |
| 121 | blocks[0] = 0; |
| 122 | int todo = 1; |
| 123 | while (todo) { |
| 124 | todo = 0; |
| 125 | for (i = 0; i < len; i++) { |
| 126 | int64_t block_stack = blocks[i]; |
| 127 | int64_t except_stack; |
| 128 | if (block_stack == -1) { |
| 129 | continue; |
| 130 | } |
| 131 | opcode = _Py_OPCODE(code[i]); |
| 132 | switch (opcode) { |
| 133 | case JUMP_IF_FALSE_OR_POP: |
| 134 | case JUMP_IF_TRUE_OR_POP: |
| 135 | case POP_JUMP_IF_FALSE: |
| 136 | case POP_JUMP_IF_TRUE: |
| 137 | case JUMP_IF_NOT_EXC_MATCH: |
| 138 | j = get_arg(code, i) / sizeof(_Py_CODEUNIT); |
| 139 | assert(j < len); |
| 140 | if (blocks[j] == -1 && j < i) { |
| 141 | todo = 1; |
| 142 | } |
| 143 | assert(blocks[j] == -1 || blocks[j] == block_stack); |
| 144 | blocks[j] = block_stack; |
| 145 | blocks[i+1] = block_stack; |
| 146 | break; |
| 147 | case JUMP_ABSOLUTE: |
| 148 | j = get_arg(code, i) / sizeof(_Py_CODEUNIT); |
| 149 | assert(j < len); |
| 150 | if (blocks[j] == -1 && j < i) { |
| 151 | todo = 1; |
| 152 | } |
| 153 | assert(blocks[j] == -1 || blocks[j] == block_stack); |
| 154 | blocks[j] = block_stack; |
| 155 | break; |
| 156 | case SETUP_FINALLY: |
| 157 | j = get_arg(code, i) / sizeof(_Py_CODEUNIT) + i + 1; |
| 158 | assert(j < len); |
| 159 | except_stack = push_block(block_stack, Except); |
| 160 | assert(blocks[j] == -1 || blocks[j] == except_stack); |
| 161 | blocks[j] = except_stack; |
| 162 | block_stack = push_block(block_stack, Try); |
| 163 | blocks[i+1] = block_stack; |
| 164 | break; |
| 165 | case SETUP_WITH: |
| 166 | case SETUP_ASYNC_WITH: |
| 167 | j = get_arg(code, i) / sizeof(_Py_CODEUNIT) + i + 1; |
| 168 | assert(j < len); |
| 169 | except_stack = push_block(block_stack, Except); |
| 170 | assert(blocks[j] == -1 || blocks[j] == except_stack); |
| 171 | blocks[j] = except_stack; |
| 172 | block_stack = push_block(block_stack, With); |
| 173 | blocks[i+1] = block_stack; |
| 174 | break; |
| 175 | case JUMP_FORWARD: |
| 176 | j = get_arg(code, i) / sizeof(_Py_CODEUNIT) + i + 1; |
| 177 | assert(j < len); |
| 178 | assert(blocks[j] == -1 || blocks[j] == block_stack); |
| 179 | blocks[j] = block_stack; |
| 180 | break; |
| 181 | case GET_ITER: |
| 182 | case GET_AITER: |
| 183 | block_stack = push_block(block_stack, Loop); |
| 184 | blocks[i+1] = block_stack; |
| 185 | break; |
| 186 | case FOR_ITER: |
| 187 | blocks[i+1] = block_stack; |
| 188 | block_stack = pop_block(block_stack); |
| 189 | j = get_arg(code, i) / sizeof(_Py_CODEUNIT) + i + 1; |
| 190 | assert(j < len); |
| 191 | assert(blocks[j] == -1 || blocks[j] == block_stack); |
| 192 | blocks[j] = block_stack; |
| 193 | break; |
| 194 | case POP_BLOCK: |
| 195 | case POP_EXCEPT: |
| 196 | block_stack = pop_block(block_stack); |
| 197 | blocks[i+1] = block_stack; |
| 198 | break; |
| 199 | case END_ASYNC_FOR: |
| 200 | block_stack = pop_block(pop_block(block_stack)); |
| 201 | blocks[i+1] = block_stack; |
| 202 | break; |
| 203 | case RETURN_VALUE: |
| 204 | case RAISE_VARARGS: |
| 205 | case RERAISE: |
| 206 | /* End of block */ |
| 207 | break; |
| 208 | default: |
| 209 | blocks[i+1] = block_stack; |
| 210 | |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | return blocks; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static int |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 218 | compatible_block_stack(int64_t from_stack, int64_t to_stack) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 219 | { |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 220 | if (to_stack < 0) { |
| 221 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 222 | } |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 223 | while(from_stack > to_stack) { |
| 224 | from_stack = pop_block(from_stack); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 225 | } |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 226 | return from_stack == to_stack; |
| 227 | } |
| 228 | |
| 229 | static const char * |
| 230 | explain_incompatible_block_stack(int64_t to_stack) |
| 231 | { |
| 232 | Kind target_kind = top_block(to_stack); |
| 233 | switch(target_kind) { |
| 234 | case Except: |
| 235 | return "can't jump into an 'except' block as there's no exception"; |
| 236 | case Try: |
| 237 | return "can't jump into the body of a try statement"; |
| 238 | case With: |
| 239 | return "can't jump into the body of a with statement"; |
| 240 | case Loop: |
| 241 | return "can't jump into the body of a for loop"; |
| 242 | default: |
| 243 | Py_UNREACHABLE(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | static int * |
| 248 | marklines(PyCodeObject *code, int len) |
| 249 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 250 | PyCodeAddressRange bounds; |
| 251 | _PyCode_InitAddressRange(code, &bounds); |
| 252 | assert (bounds.ar_end == 0); |
| 253 | |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 254 | int *linestarts = PyMem_New(int, len); |
| 255 | if (linestarts == NULL) { |
| 256 | return NULL; |
| 257 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 258 | for (int i = 0; i < len; i++) { |
| 259 | linestarts[i] = -1; |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 260 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 261 | |
| 262 | while (PyLineTable_NextAddressRange(&bounds)) { |
| 263 | assert(bounds.ar_start/2 < len); |
| 264 | linestarts[bounds.ar_start/2] = bounds.ar_line; |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 265 | } |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 266 | return linestarts; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static int |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 270 | first_line_not_before(int *lines, int len, int line) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 271 | { |
| 272 | int result = INT_MAX; |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 273 | for (int i = 0; i < len; i++) { |
| 274 | if (lines[i] < result && lines[i] >= line) { |
| 275 | result = lines[i]; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 276 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 277 | } |
| 278 | if (result == INT_MAX) { |
| 279 | return -1; |
| 280 | } |
| 281 | return result; |
| 282 | } |
| 283 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 284 | static void |
| 285 | frame_stack_pop(PyFrameObject *f) |
| 286 | { |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 287 | assert(f->f_stackdepth >= 0); |
| 288 | f->f_stackdepth--; |
| 289 | PyObject *v = f->f_valuestack[f->f_stackdepth]; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 290 | Py_DECREF(v); |
| 291 | } |
| 292 | |
| 293 | static void |
| 294 | frame_block_unwind(PyFrameObject *f) |
| 295 | { |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 296 | assert(f->f_stackdepth >= 0); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 297 | assert(f->f_iblock > 0); |
| 298 | f->f_iblock--; |
| 299 | PyTryBlock *b = &f->f_blockstack[f->f_iblock]; |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 300 | intptr_t delta = f->f_stackdepth - b->b_level; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 301 | while (delta > 0) { |
| 302 | frame_stack_pop(f); |
| 303 | delta--; |
| 304 | } |
| 305 | } |
| 306 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 307 | |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 308 | /* Setter for f_lineno - you can set f_lineno from within a trace function in |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | * order to jump to a given line of code, subject to some restrictions. Most |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 310 | * lines are OK to jump to because they don't make any assumptions about the |
| 311 | * state of the stack (obvious because you could remove the line and the code |
| 312 | * would still work without any stack errors), but there are some constructs |
| 313 | * that limit jumping: |
| 314 | * |
| 315 | * o Lines with an 'except' statement on them can't be jumped to, because |
| 316 | * they expect an exception to be on the top of the stack. |
| 317 | * o Lines that live in a 'finally' block can't be jumped from or to, since |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 318 | * we cannot be sure which state the interpreter was in or would be in |
| 319 | * during execution of the finally block. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 320 | * o 'try', 'with' and 'async with' blocks can't be jumped into because |
| 321 | * the blockstack needs to be set up before their code runs. |
| 322 | * o 'for' and 'async for' loops can't be jumped into because the |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 323 | * iterator needs to be on the stack. |
xdegaye | b8e9d6c | 2018-03-13 18:31:31 +0100 | [diff] [blame] | 324 | * o Jumps cannot be made from within a trace function invoked with a |
| 325 | * 'return' or 'exception' event since the eval loop has been exited at |
| 326 | * that time. |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 327 | */ |
| 328 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 329 | frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignored)) |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 330 | { |
Zackery Spytz | 842acaa | 2018-12-17 07:52:45 -0700 | [diff] [blame] | 331 | if (p_new_lineno == NULL) { |
| 332 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 333 | return -1; |
| 334 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | /* f_lineno must be an integer. */ |
| 336 | if (!PyLong_CheckExact(p_new_lineno)) { |
| 337 | PyErr_SetString(PyExc_ValueError, |
| 338 | "lineno must be an integer"); |
| 339 | return -1; |
| 340 | } |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 341 | |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 342 | /* |
| 343 | * This code preserves the historical restrictions on |
| 344 | * setting the line number of a frame. |
| 345 | * Jumps are forbidden on a 'return' trace event (except after a yield). |
| 346 | * Jumps from 'call' trace events are also forbidden. |
| 347 | * In addition, jumps are forbidden when not tracing, |
| 348 | * as this is a debugging feature. |
| 349 | */ |
| 350 | switch(f->f_state) { |
| 351 | case FRAME_CREATED: |
| 352 | PyErr_Format(PyExc_ValueError, |
xdegaye | b8e9d6c | 2018-03-13 18:31:31 +0100 | [diff] [blame] | 353 | "can't jump from the 'call' trace event of a new frame"); |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 354 | return -1; |
| 355 | case FRAME_RETURNED: |
| 356 | case FRAME_UNWINDING: |
| 357 | case FRAME_RAISED: |
| 358 | case FRAME_CLEARED: |
| 359 | PyErr_SetString(PyExc_ValueError, |
xdegaye | b8e9d6c | 2018-03-13 18:31:31 +0100 | [diff] [blame] | 360 | "can only jump from a 'line' trace event"); |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 361 | return -1; |
| 362 | case FRAME_EXECUTING: |
| 363 | case FRAME_SUSPENDED: |
| 364 | /* You can only do this from within a trace function, not via |
| 365 | * _getframe or similar hackery. */ |
| 366 | if (!f->f_trace) { |
| 367 | PyErr_Format(PyExc_ValueError, |
| 368 | "f_lineno can only be set by a trace function"); |
| 369 | return -1; |
| 370 | } |
| 371 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | } |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 373 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 374 | int new_lineno; |
| 375 | |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 376 | /* Fail if the line falls outside the code block and |
| 377 | select first line with actual code. */ |
| 378 | int overflow; |
| 379 | long l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow); |
| 380 | if (overflow |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 381 | #if SIZEOF_LONG > SIZEOF_INT |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 382 | || l_new_lineno > INT_MAX |
| 383 | || l_new_lineno < INT_MIN |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 384 | #endif |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 385 | ) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 386 | PyErr_SetString(PyExc_ValueError, |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 387 | "lineno out of range"); |
| 388 | return -1; |
| 389 | } |
| 390 | new_lineno = (int)l_new_lineno; |
| 391 | |
| 392 | if (new_lineno < f->f_code->co_firstlineno) { |
| 393 | PyErr_Format(PyExc_ValueError, |
| 394 | "line %d comes before the current code block", |
| 395 | new_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | return -1; |
| 397 | } |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 398 | |
Ammar Askar | 3b3b83c | 2020-06-10 23:31:22 +0000 | [diff] [blame] | 399 | /* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this |
| 400 | * should never overflow. */ |
| 401 | int len = (int)(PyBytes_GET_SIZE(f->f_code->co_code) / sizeof(_Py_CODEUNIT)); |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 402 | int *lines = marklines(f->f_code, len); |
| 403 | if (lines == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | return -1; |
| 405 | } |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 406 | |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 407 | new_lineno = first_line_not_before(lines, len, new_lineno); |
| 408 | if (new_lineno < 0) { |
| 409 | PyErr_Format(PyExc_ValueError, |
| 410 | "line %d comes after the current code block", |
| 411 | (int)l_new_lineno); |
| 412 | PyMem_Free(lines); |
| 413 | return -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 416 | int64_t *blocks = markblocks(f->f_code, len); |
| 417 | if (blocks == NULL) { |
| 418 | PyMem_Free(lines); |
| 419 | return -1; |
| 420 | } |
| 421 | |
| 422 | int64_t target_block_stack = -1; |
| 423 | int64_t best_block_stack = -1; |
| 424 | int best_addr = -1; |
| 425 | int64_t start_block_stack = blocks[f->f_lasti/sizeof(_Py_CODEUNIT)]; |
| 426 | const char *msg = "cannot find bytecode for specified line"; |
| 427 | for (int i = 0; i < len; i++) { |
| 428 | if (lines[i] == new_lineno) { |
| 429 | target_block_stack = blocks[i]; |
| 430 | if (compatible_block_stack(start_block_stack, target_block_stack)) { |
| 431 | msg = NULL; |
| 432 | if (target_block_stack > best_block_stack) { |
| 433 | best_block_stack = target_block_stack; |
| 434 | best_addr = i*sizeof(_Py_CODEUNIT); |
| 435 | } |
| 436 | } |
| 437 | else if (msg) { |
| 438 | if (target_block_stack >= 0) { |
| 439 | msg = explain_incompatible_block_stack(target_block_stack); |
| 440 | } |
| 441 | else { |
| 442 | msg = "code may be unreachable."; |
| 443 | } |
| 444 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 447 | PyMem_Free(blocks); |
| 448 | PyMem_Free(lines); |
| 449 | if (msg != NULL) { |
| 450 | PyErr_SetString(PyExc_ValueError, msg); |
| 451 | return -1; |
| 452 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 453 | |
| 454 | /* Unwind block stack. */ |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 455 | while (start_block_stack > best_block_stack) { |
| 456 | Kind kind = top_block(start_block_stack); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 457 | switch(kind) { |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 458 | case Loop: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 459 | frame_stack_pop(f); |
| 460 | break; |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 461 | case Try: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 462 | frame_block_unwind(f); |
| 463 | break; |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 464 | case With: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 465 | frame_block_unwind(f); |
| 466 | // Pop the exit function |
| 467 | frame_stack_pop(f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | break; |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 469 | case Except: |
| 470 | PyErr_SetString(PyExc_ValueError, |
| 471 | "can't jump out of an 'except' block"); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 472 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | } |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 474 | start_block_stack = pop_block(start_block_stack); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 476 | |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 477 | /* Finally set the new f_lasti and return OK. */ |
| 478 | f->f_lineno = 0; |
Mark Shannon | 5769724 | 2020-04-29 16:49:45 +0100 | [diff] [blame] | 479 | f->f_lasti = best_addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | return 0; |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 483 | static PyObject * |
| 484 | frame_gettrace(PyFrameObject *f, void *closure) |
| 485 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | PyObject* trace = f->f_trace; |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | if (trace == NULL) |
| 489 | trace = Py_None; |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | Py_INCREF(trace); |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | return trace; |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static int |
| 497 | frame_settrace(PyFrameObject *f, PyObject* v, void *closure) |
| 498 | { |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 499 | if (v == Py_None) { |
Serhiy Storchaka | 64a263a | 2016-06-04 20:32:36 +0300 | [diff] [blame] | 500 | v = NULL; |
Mark Shannon | ee9f98d | 2021-01-05 12:04:10 +0000 | [diff] [blame] | 501 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | Py_XINCREF(v); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 503 | Py_XSETREF(f->f_trace, v); |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | return 0; |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 508 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 509 | static PyGetSetDef frame_getsetlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | {"f_locals", (getter)frame_getlocals, NULL, NULL}, |
| 511 | {"f_lineno", (getter)frame_getlineno, |
| 512 | (setter)frame_setlineno, NULL}, |
| 513 | {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL}, |
| 514 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 515 | }; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 516 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 517 | /* Stack frames are allocated and deallocated at a considerable rate. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 518 | In an attempt to improve the speed of function calls, we: |
| 519 | |
| 520 | 1. Hold a single "zombie" frame on each code object. This retains |
| 521 | the allocated and initialised frame object from an invocation of |
| 522 | the code object. The zombie is reanimated the next time we need a |
| 523 | frame object for that code object. Doing this saves the malloc/ |
| 524 | realloc required when using a free_list frame that isn't the |
| 525 | correct size. It also saves some field initialisation. |
| 526 | |
| 527 | In zombie mode, no field of PyFrameObject holds a reference, but |
| 528 | the following fields are still valid: |
| 529 | |
| 530 | * ob_type, ob_size, f_code, f_valuestack; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | |
Mark Shannon | ae3087c | 2017-10-22 22:41:51 +0100 | [diff] [blame] | 532 | * f_locals, f_trace are NULL; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 533 | |
| 534 | * f_localsplus does not require re-allocation and |
| 535 | the local variables in f_localsplus are NULL. |
| 536 | |
| 537 | 2. We also maintain a separate free list of stack frames (just like |
Mark Dickinson | d19052c | 2010-06-27 18:19:09 +0000 | [diff] [blame] | 538 | floats are allocated in a special way -- see floatobject.c). When |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 539 | a stack frame is on the free list, only the following members have |
| 540 | a meaning: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | ob_type == &Frametype |
| 542 | f_back next item on free list, or NULL |
| 543 | f_stacksize size of value stack |
| 544 | ob_size size of localsplus |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 545 | Note that the value and block stacks are preserved -- this can save |
| 546 | another malloc() call or two (and two free() calls as well!). |
| 547 | Also note that, unlike for integers, each frame object is a |
| 548 | malloc'ed object in its own right -- it is only the actual calls to |
| 549 | malloc() that we are trying to save here, not the administration. |
| 550 | After all, while a typical program may make millions of calls, a |
| 551 | call depth of more than 20 or 30 is probably already exceptional |
| 552 | unless the program contains run-away recursion. I hope. |
Tim Peters | b7ba743 | 2002-04-13 05:21:47 +0000 | [diff] [blame] | 553 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 554 | Later, PyFrame_MAXFREELIST was added to bound the # of frames saved on |
Tim Peters | b7ba743 | 2002-04-13 05:21:47 +0000 | [diff] [blame] | 555 | free_list. Else programs creating lots of cyclic trash involving |
| 556 | frames could provoke free_list into growing without bound. |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 557 | */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 558 | /* max value for numfree */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | #define PyFrame_MAXFREELIST 200 |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 560 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 561 | static void _Py_HOT_FUNCTION |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 562 | frame_dealloc(PyFrameObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 563 | { |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 564 | if (_PyObject_GC_IS_TRACKED(f)) { |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 565 | _PyObject_GC_UNTRACK(f); |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 566 | } |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | Py_TRASHCAN_SAFE_BEGIN(f) |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 569 | /* Kill all local variables */ |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 570 | PyObject **valuestack = f->f_valuestack; |
| 571 | for (PyObject **p = f->f_localsplus; p < valuestack; p++) { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 572 | Py_CLEAR(*p); |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 573 | } |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 574 | |
| 575 | /* Free stack */ |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 576 | for (int i = 0; i < f->f_stackdepth; i++) { |
| 577 | Py_XDECREF(f->f_valuestack[i]); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 578 | } |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 579 | f->f_stackdepth = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | Py_XDECREF(f->f_back); |
| 582 | Py_DECREF(f->f_builtins); |
| 583 | Py_DECREF(f->f_globals); |
| 584 | Py_CLEAR(f->f_locals); |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 585 | Py_CLEAR(f->f_trace); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 586 | |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 587 | PyCodeObject *co = f->f_code; |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 588 | if (co->co_zombieframe == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | co->co_zombieframe = f; |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 590 | } |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 591 | else { |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 592 | struct _Py_frame_state *state = get_frame_state(); |
Victor Stinner | bcb1983 | 2020-06-08 02:14:47 +0200 | [diff] [blame] | 593 | #ifdef Py_DEBUG |
| 594 | // frame_dealloc() must not be called after _PyFrame_Fini() |
| 595 | assert(state->numfree != -1); |
| 596 | #endif |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 597 | if (state->numfree < PyFrame_MAXFREELIST) { |
| 598 | ++state->numfree; |
| 599 | f->f_back = state->free_list; |
| 600 | state->free_list = f; |
| 601 | } |
| 602 | else { |
| 603 | PyObject_GC_Del(f); |
| 604 | } |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 605 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | Py_DECREF(co); |
| 608 | Py_TRASHCAN_SAFE_END(f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 611 | static inline Py_ssize_t |
| 612 | frame_nslots(PyFrameObject *frame) |
| 613 | { |
| 614 | PyCodeObject *code = frame->f_code; |
| 615 | return (code->co_nlocals |
| 616 | + PyTuple_GET_SIZE(code->co_cellvars) |
| 617 | + PyTuple_GET_SIZE(code->co_freevars)); |
| 618 | } |
| 619 | |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 620 | static int |
| 621 | frame_traverse(PyFrameObject *f, visitproc visit, void *arg) |
| 622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | Py_VISIT(f->f_back); |
| 624 | Py_VISIT(f->f_code); |
| 625 | Py_VISIT(f->f_builtins); |
| 626 | Py_VISIT(f->f_globals); |
| 627 | Py_VISIT(f->f_locals); |
| 628 | Py_VISIT(f->f_trace); |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | /* locals */ |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 631 | PyObject **fastlocals = f->f_localsplus; |
| 632 | for (Py_ssize_t i = frame_nslots(f); --i >= 0; ++fastlocals) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | Py_VISIT(*fastlocals); |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 634 | } |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | /* stack */ |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 637 | for (int i = 0; i < f->f_stackdepth; i++) { |
| 638 | Py_VISIT(f->f_valuestack[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | } |
| 640 | return 0; |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 643 | static int |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 644 | frame_tp_clear(PyFrameObject *f) |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 645 | { |
Antoine Pitrou | 9396356 | 2013-05-14 20:37:52 +0200 | [diff] [blame] | 646 | /* Before anything else, make sure that this frame is clearly marked |
| 647 | * as being defunct! Else, e.g., a generator reachable from this |
| 648 | * frame may also point to this frame, believe itself to still be |
| 649 | * active, and try cleaning up this frame again. |
| 650 | */ |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 651 | f->f_state = FRAME_CLEARED; |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | Py_CLEAR(f->f_trace); |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | /* locals */ |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 656 | PyObject **fastlocals = f->f_localsplus; |
| 657 | for (Py_ssize_t i = frame_nslots(f); --i >= 0; ++fastlocals) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | Py_CLEAR(*fastlocals); |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 659 | } |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | /* stack */ |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 662 | for (int i = 0; i < f->f_stackdepth; i++) { |
| 663 | Py_CLEAR(f->f_valuestack[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | } |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 665 | f->f_stackdepth = 0; |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 666 | return 0; |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 669 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 670 | frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored)) |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 671 | { |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 672 | if (_PyFrame_IsExecuting(f)) { |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 673 | PyErr_SetString(PyExc_RuntimeError, |
| 674 | "cannot clear an executing frame"); |
| 675 | return NULL; |
| 676 | } |
| 677 | if (f->f_gen) { |
| 678 | _PyGen_Finalize(f->f_gen); |
| 679 | assert(f->f_gen == NULL); |
| 680 | } |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 681 | (void)frame_tp_clear(f); |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 682 | Py_RETURN_NONE; |
| 683 | } |
| 684 | |
| 685 | PyDoc_STRVAR(clear__doc__, |
| 686 | "F.clear(): clear most references held by the frame"); |
| 687 | |
| 688 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 689 | frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored)) |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | Py_ssize_t res, extras, ncells, nfrees; |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 692 | |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 693 | PyCodeObject *code = f->f_code; |
| 694 | ncells = PyTuple_GET_SIZE(code->co_cellvars); |
| 695 | nfrees = PyTuple_GET_SIZE(code->co_freevars); |
| 696 | extras = code->co_stacksize + code->co_nlocals + ncells + nfrees; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | /* subtract one as it is already included in PyFrameObject */ |
| 698 | res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | return PyLong_FromSsize_t(res); |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | PyDoc_STRVAR(sizeof__doc__, |
| 704 | "F.__sizeof__() -> size of F in memory, in bytes"); |
| 705 | |
Antoine Pitrou | 1470914 | 2017-12-31 22:35:22 +0100 | [diff] [blame] | 706 | static PyObject * |
| 707 | frame_repr(PyFrameObject *f) |
| 708 | { |
| 709 | int lineno = PyFrame_GetLineNumber(f); |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 710 | PyCodeObject *code = f->f_code; |
Antoine Pitrou | 1470914 | 2017-12-31 22:35:22 +0100 | [diff] [blame] | 711 | return PyUnicode_FromFormat( |
| 712 | "<frame at %p, file %R, line %d, code %S>", |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 713 | f, code->co_filename, lineno, code->co_name); |
Antoine Pitrou | 1470914 | 2017-12-31 22:35:22 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 716 | static PyMethodDef frame_methods[] = { |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 717 | {"clear", (PyCFunction)frame_clear, METH_NOARGS, |
| 718 | clear__doc__}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS, |
| 720 | sizeof__doc__}, |
| 721 | {NULL, NULL} /* sentinel */ |
Robert Schuppenies | fbe94c5 | 2008-07-14 10:13:31 +0000 | [diff] [blame] | 722 | }; |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 723 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 724 | PyTypeObject PyFrame_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 726 | "frame", |
| 727 | sizeof(PyFrameObject), |
| 728 | sizeof(PyObject *), |
| 729 | (destructor)frame_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 730 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | 0, /* tp_getattr */ |
| 732 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 733 | 0, /* tp_as_async */ |
Antoine Pitrou | 1470914 | 2017-12-31 22:35:22 +0100 | [diff] [blame] | 734 | (reprfunc)frame_repr, /* tp_repr */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | 0, /* tp_as_number */ |
| 736 | 0, /* tp_as_sequence */ |
| 737 | 0, /* tp_as_mapping */ |
| 738 | 0, /* tp_hash */ |
| 739 | 0, /* tp_call */ |
| 740 | 0, /* tp_str */ |
| 741 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 742 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 743 | 0, /* tp_as_buffer */ |
| 744 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 745 | 0, /* tp_doc */ |
| 746 | (traverseproc)frame_traverse, /* tp_traverse */ |
Antoine Pitrou | 58720d6 | 2013-08-05 23:26:40 +0200 | [diff] [blame] | 747 | (inquiry)frame_tp_clear, /* tp_clear */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | 0, /* tp_richcompare */ |
| 749 | 0, /* tp_weaklistoffset */ |
| 750 | 0, /* tp_iter */ |
| 751 | 0, /* tp_iternext */ |
| 752 | frame_methods, /* tp_methods */ |
| 753 | frame_memberlist, /* tp_members */ |
| 754 | frame_getsetlist, /* tp_getset */ |
| 755 | 0, /* tp_base */ |
| 756 | 0, /* tp_dict */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 757 | }; |
| 758 | |
Victor Stinner | 07e9e38 | 2013-11-07 22:22:39 +0100 | [diff] [blame] | 759 | _Py_IDENTIFIER(__builtins__); |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 760 | |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 761 | static inline PyFrameObject* |
| 762 | frame_alloc(PyCodeObject *code) |
| 763 | { |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 764 | PyFrameObject *f = code->co_zombieframe; |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 765 | if (f != NULL) { |
| 766 | code->co_zombieframe = NULL; |
| 767 | _Py_NewReference((PyObject *)f); |
| 768 | assert(f->f_code == code); |
| 769 | return f; |
| 770 | } |
| 771 | |
| 772 | Py_ssize_t ncells = PyTuple_GET_SIZE(code->co_cellvars); |
| 773 | Py_ssize_t nfrees = PyTuple_GET_SIZE(code->co_freevars); |
| 774 | Py_ssize_t extras = code->co_stacksize + code->co_nlocals + ncells + nfrees; |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 775 | struct _Py_frame_state *state = get_frame_state(); |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 776 | if (state->free_list == NULL) |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 777 | { |
| 778 | f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, extras); |
| 779 | if (f == NULL) { |
| 780 | return NULL; |
| 781 | } |
| 782 | } |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 783 | else { |
Victor Stinner | bcb1983 | 2020-06-08 02:14:47 +0200 | [diff] [blame] | 784 | #ifdef Py_DEBUG |
| 785 | // frame_alloc() must not be called after _PyFrame_Fini() |
| 786 | assert(state->numfree != -1); |
| 787 | #endif |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 788 | assert(state->numfree > 0); |
| 789 | --state->numfree; |
| 790 | f = state->free_list; |
| 791 | state->free_list = state->free_list->f_back; |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 792 | if (Py_SIZE(f) < extras) { |
| 793 | PyFrameObject *new_f = PyObject_GC_Resize(PyFrameObject, f, extras); |
| 794 | if (new_f == NULL) { |
| 795 | PyObject_GC_Del(f); |
| 796 | return NULL; |
| 797 | } |
| 798 | f = new_f; |
| 799 | } |
| 800 | _Py_NewReference((PyObject *)f); |
| 801 | } |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 802 | |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 803 | extras = code->co_nlocals + ncells + nfrees; |
| 804 | f->f_valuestack = f->f_localsplus + extras; |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 805 | for (Py_ssize_t i=0; i < extras; i++) { |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 806 | f->f_localsplus[i] = NULL; |
| 807 | } |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 808 | return f; |
| 809 | } |
| 810 | |
| 811 | |
Victor Stinner | c6944e7 | 2016-11-11 02:13:35 +0100 | [diff] [blame] | 812 | PyFrameObject* _Py_HOT_FUNCTION |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 813 | _PyFrame_New_NoTrack(PyThreadState *tstate, PyFrameConstructor *con, PyObject *locals) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 814 | { |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 815 | assert(con != NULL); |
| 816 | assert(con->fc_globals != NULL); |
| 817 | assert(con->fc_builtins != NULL); |
| 818 | assert(con->fc_code != NULL); |
| 819 | assert(locals == NULL || PyMapping_Check(locals)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 820 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 821 | PyFrameObject *f = frame_alloc((PyCodeObject *)con->fc_code); |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 822 | if (f == NULL) { |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 823 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | } |
Victor Stinner | b4b5386 | 2020-05-05 19:55:29 +0200 | [diff] [blame] | 825 | |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 826 | f->f_back = (PyFrameObject*)Py_XNewRef(tstate->frame); |
| 827 | f->f_code = (PyCodeObject *)Py_NewRef(con->fc_code); |
| 828 | f->f_builtins = Py_NewRef(con->fc_builtins); |
| 829 | f->f_globals = Py_NewRef(con->fc_globals); |
| 830 | f->f_locals = Py_XNewRef(locals); |
| 831 | // f_valuestack initialized by frame_alloc() |
| 832 | f->f_trace = NULL; |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 833 | f->f_stackdepth = 0; |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 834 | f->f_trace_lines = 1; |
| 835 | f->f_trace_opcodes = 0; |
| 836 | f->f_gen = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 837 | f->f_lasti = -1; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 838 | f->f_lineno = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | f->f_iblock = 0; |
Mark Shannon | cb9879b | 2020-07-17 11:44:23 +0100 | [diff] [blame] | 840 | f->f_state = FRAME_CREATED; |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 841 | // f_blockstack and f_localsplus initialized by frame_alloc() |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | return f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 845 | /* Legacy API */ |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 846 | PyFrameObject* |
| 847 | PyFrame_New(PyThreadState *tstate, PyCodeObject *code, |
| 848 | PyObject *globals, PyObject *locals) |
| 849 | { |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 850 | PyObject *builtins = _PyEval_BuiltinsFromGlobals(globals); |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 851 | if (builtins == NULL) { |
| 852 | return NULL; |
| 853 | } |
Mark Shannon | 0332e56 | 2021-02-01 10:42:03 +0000 | [diff] [blame] | 854 | PyFrameConstructor desc = { |
| 855 | .fc_globals = globals, |
| 856 | .fc_builtins = builtins, |
| 857 | .fc_name = code->co_name, |
| 858 | .fc_qualname = code->co_name, |
| 859 | .fc_code = (PyObject *)code, |
| 860 | .fc_defaults = NULL, |
| 861 | .fc_kwdefaults = NULL, |
| 862 | .fc_closure = NULL |
| 863 | }; |
| 864 | PyFrameObject *f = _PyFrame_New_NoTrack(tstate, &desc, locals); |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 865 | Py_DECREF(builtins); |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 866 | if (f) { |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 867 | _PyObject_GC_TRACK(f); |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 868 | } |
INADA Naoki | 5a625d0 | 2016-12-24 20:19:08 +0900 | [diff] [blame] | 869 | return f; |
| 870 | } |
| 871 | |
| 872 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 873 | /* Block management */ |
| 874 | |
| 875 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 876 | PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 877 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | PyTryBlock *b; |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 879 | if (f->f_iblock >= CO_MAXBLOCKS) { |
| 880 | Py_FatalError("block stack overflow"); |
| 881 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | b = &f->f_blockstack[f->f_iblock++]; |
| 883 | b->b_type = type; |
| 884 | b->b_level = level; |
| 885 | b->b_handler = handler; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 888 | PyTryBlock * |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 889 | PyFrame_BlockPop(PyFrameObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 890 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | PyTryBlock *b; |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 892 | if (f->f_iblock <= 0) { |
| 893 | Py_FatalError("block stack underflow"); |
| 894 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | b = &f->f_blockstack[--f->f_iblock]; |
| 896 | return b; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 897 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 898 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 899 | /* Convert between "fast" version of locals and dictionary version. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | |
| 901 | map and values are input arguments. map is a tuple of strings. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 902 | values is an array of PyObject*. At index i, map[i] is the name of |
| 903 | the variable with value values[i]. The function copies the first |
| 904 | nmap variable from map/values into dict. If values[i] is NULL, |
| 905 | the variable is deleted from dict. |
| 906 | |
| 907 | If deref is true, then the values being copied are cell variables |
| 908 | and the value is extracted from the cell variable before being put |
| 909 | in dict. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 910 | */ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 911 | |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 912 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 913 | map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | int deref) |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | Py_ssize_t j; |
| 917 | assert(PyTuple_Check(map)); |
| 918 | assert(PyDict_Check(dict)); |
| 919 | assert(PyTuple_Size(map) >= nmap); |
Raymond Hettinger | a4d0001 | 2018-01-28 09:40:24 -0800 | [diff] [blame] | 920 | for (j=0; j < nmap; j++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | PyObject *key = PyTuple_GET_ITEM(map, j); |
| 922 | PyObject *value = values[j]; |
| 923 | assert(PyUnicode_Check(key)); |
Antoine Pitrou | acc8cf2 | 2014-07-04 20:24:13 -0400 | [diff] [blame] | 924 | if (deref && value != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | assert(PyCell_Check(value)); |
| 926 | value = PyCell_GET(value); |
| 927 | } |
| 928 | if (value == NULL) { |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 929 | if (PyObject_DelItem(dict, key) != 0) { |
| 930 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 931 | PyErr_Clear(); |
| 932 | else |
| 933 | return -1; |
| 934 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | } |
| 936 | else { |
| 937 | if (PyObject_SetItem(dict, key, value) != 0) |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 938 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 941 | return 0; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 944 | /* Copy values from the "locals" dict into the fast locals. |
| 945 | |
| 946 | dict is an input argument containing string keys representing |
| 947 | variables names and arbitrary PyObject* as values. |
| 948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | map and values are input arguments. map is a tuple of strings. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 950 | values is an array of PyObject*. At index i, map[i] is the name of |
| 951 | the variable with value values[i]. The function copies the first |
| 952 | nmap variable from map/values into dict. If values[i] is NULL, |
| 953 | the variable is deleted from dict. |
| 954 | |
| 955 | If deref is true, then the values being copied are cell variables |
| 956 | and the value is extracted from the cell variable before being put |
| 957 | in dict. If clear is true, then variables in map but not in dict |
| 958 | are set to NULL in map; if clear is false, variables missing in |
| 959 | dict are ignored. |
| 960 | |
| 961 | Exceptions raised while modifying the dict are silently ignored, |
| 962 | because there is no good way to report them. |
| 963 | */ |
| 964 | |
Guido van Rossum | 6b356e7 | 2001-04-14 17:55:41 +0000 | [diff] [blame] | 965 | static void |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 966 | dict_to_map(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | int deref, int clear) |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | Py_ssize_t j; |
| 970 | assert(PyTuple_Check(map)); |
| 971 | assert(PyDict_Check(dict)); |
| 972 | assert(PyTuple_Size(map) >= nmap); |
Raymond Hettinger | a4d0001 | 2018-01-28 09:40:24 -0800 | [diff] [blame] | 973 | for (j=0; j < nmap; j++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | PyObject *key = PyTuple_GET_ITEM(map, j); |
| 975 | PyObject *value = PyObject_GetItem(dict, key); |
| 976 | assert(PyUnicode_Check(key)); |
| 977 | /* We only care about NULLs if clear is true. */ |
| 978 | if (value == NULL) { |
| 979 | PyErr_Clear(); |
| 980 | if (!clear) |
| 981 | continue; |
| 982 | } |
| 983 | if (deref) { |
| 984 | assert(PyCell_Check(values[j])); |
| 985 | if (PyCell_GET(values[j]) != value) { |
| 986 | if (PyCell_Set(values[j], value) < 0) |
| 987 | PyErr_Clear(); |
| 988 | } |
| 989 | } else if (values[j] != value) { |
| 990 | Py_XINCREF(value); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 991 | Py_XSETREF(values[j], value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | } |
| 993 | Py_XDECREF(value); |
| 994 | } |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 995 | } |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 996 | |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 997 | int |
| 998 | PyFrame_FastToLocalsWithError(PyFrameObject *f) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 999 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | /* Merge fast locals into f->f_locals */ |
| 1001 | PyObject *locals, *map; |
| 1002 | PyObject **fast; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | PyCodeObject *co; |
| 1004 | Py_ssize_t j; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 1005 | Py_ssize_t ncells, nfreevars; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 1006 | |
| 1007 | if (f == NULL) { |
| 1008 | PyErr_BadInternalCall(); |
| 1009 | return -1; |
| 1010 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | locals = f->f_locals; |
| 1012 | if (locals == NULL) { |
| 1013 | locals = f->f_locals = PyDict_New(); |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 1014 | if (locals == NULL) |
| 1015 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | } |
| 1017 | co = f->f_code; |
| 1018 | map = co->co_varnames; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 1019 | if (!PyTuple_Check(map)) { |
| 1020 | PyErr_Format(PyExc_SystemError, |
| 1021 | "co_varnames must be a tuple, not %s", |
| 1022 | Py_TYPE(map)->tp_name); |
| 1023 | return -1; |
| 1024 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | fast = f->f_localsplus; |
| 1026 | j = PyTuple_GET_SIZE(map); |
| 1027 | if (j > co->co_nlocals) |
| 1028 | j = co->co_nlocals; |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 1029 | if (co->co_nlocals) { |
| 1030 | if (map_to_dict(map, j, locals, fast, 0) < 0) |
| 1031 | return -1; |
| 1032 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | ncells = PyTuple_GET_SIZE(co->co_cellvars); |
| 1034 | nfreevars = PyTuple_GET_SIZE(co->co_freevars); |
| 1035 | if (ncells || nfreevars) { |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 1036 | if (map_to_dict(co->co_cellvars, ncells, |
| 1037 | locals, fast + co->co_nlocals, 1)) |
| 1038 | return -1; |
| 1039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | /* If the namespace is unoptimized, then one of the |
| 1041 | following cases applies: |
| 1042 | 1. It does not contain free variables, because it |
| 1043 | uses import * or is a top-level namespace. |
| 1044 | 2. It is a class namespace. |
| 1045 | We don't want to accidentally copy free variables |
| 1046 | into the locals dict used by the class. |
| 1047 | */ |
| 1048 | if (co->co_flags & CO_OPTIMIZED) { |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 1049 | if (map_to_dict(co->co_freevars, nfreevars, |
| 1050 | locals, fast + co->co_nlocals + ncells, 1) < 0) |
| 1051 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | } |
| 1053 | } |
Victor Stinner | 41bb43a | 2013-10-29 01:19:37 +0100 | [diff] [blame] | 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | void |
| 1058 | PyFrame_FastToLocals(PyFrameObject *f) |
| 1059 | { |
| 1060 | int res; |
| 1061 | |
| 1062 | assert(!PyErr_Occurred()); |
| 1063 | |
| 1064 | res = PyFrame_FastToLocalsWithError(f); |
| 1065 | if (res < 0) |
| 1066 | PyErr_Clear(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 1070 | PyFrame_LocalsToFast(PyFrameObject *f, int clear) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 1071 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | /* Merge f->f_locals into fast locals */ |
| 1073 | PyObject *locals, *map; |
| 1074 | PyObject **fast; |
| 1075 | PyObject *error_type, *error_value, *error_traceback; |
| 1076 | PyCodeObject *co; |
| 1077 | Py_ssize_t j; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 1078 | Py_ssize_t ncells, nfreevars; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | if (f == NULL) |
| 1080 | return; |
| 1081 | locals = f->f_locals; |
| 1082 | co = f->f_code; |
| 1083 | map = co->co_varnames; |
| 1084 | if (locals == NULL) |
| 1085 | return; |
| 1086 | if (!PyTuple_Check(map)) |
| 1087 | return; |
| 1088 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 1089 | fast = f->f_localsplus; |
| 1090 | j = PyTuple_GET_SIZE(map); |
| 1091 | if (j > co->co_nlocals) |
| 1092 | j = co->co_nlocals; |
| 1093 | if (co->co_nlocals) |
| 1094 | dict_to_map(co->co_varnames, j, locals, fast, 0, clear); |
| 1095 | ncells = PyTuple_GET_SIZE(co->co_cellvars); |
| 1096 | nfreevars = PyTuple_GET_SIZE(co->co_freevars); |
| 1097 | if (ncells || nfreevars) { |
| 1098 | dict_to_map(co->co_cellvars, ncells, |
| 1099 | locals, fast + co->co_nlocals, 1, clear); |
| 1100 | /* Same test as in PyFrame_FastToLocals() above. */ |
| 1101 | if (co->co_flags & CO_OPTIMIZED) { |
| 1102 | dict_to_map(co->co_freevars, nfreevars, |
| 1103 | locals, fast + co->co_nlocals + ncells, 1, |
| 1104 | clear); |
| 1105 | } |
| 1106 | } |
| 1107 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 1108 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 1109 | |
| 1110 | /* Clear out the free list */ |
Victor Stinner | ae00a5a | 2020-04-29 02:29:20 +0200 | [diff] [blame] | 1111 | void |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 1112 | _PyFrame_ClearFreeList(PyThreadState *tstate) |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 1113 | { |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 1114 | struct _Py_frame_state *state = &tstate->interp->frame; |
| 1115 | while (state->free_list != NULL) { |
| 1116 | PyFrameObject *f = state->free_list; |
| 1117 | state->free_list = state->free_list->f_back; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | PyObject_GC_Del(f); |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 1119 | --state->numfree; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | } |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 1121 | assert(state->numfree == 0); |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | void |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 1125 | _PyFrame_Fini(PyThreadState *tstate) |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 1126 | { |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 1127 | _PyFrame_ClearFreeList(tstate); |
Victor Stinner | bcb1983 | 2020-06-08 02:14:47 +0200 | [diff] [blame] | 1128 | #ifdef Py_DEBUG |
| 1129 | struct _Py_frame_state *state = &tstate->interp->frame; |
| 1130 | state->numfree = -1; |
| 1131 | #endif |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 1132 | } |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1133 | |
| 1134 | /* Print summary info about the state of the optimized allocator */ |
| 1135 | void |
| 1136 | _PyFrame_DebugMallocStats(FILE *out) |
| 1137 | { |
Victor Stinner | 522691c | 2020-06-23 16:40:40 +0200 | [diff] [blame] | 1138 | struct _Py_frame_state *state = get_frame_state(); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1139 | _PyDebugAllocatorStats(out, |
| 1140 | "free PyFrameObject", |
Victor Stinner | 3744ed2 | 2020-06-05 01:39:24 +0200 | [diff] [blame] | 1141 | state->numfree, sizeof(PyFrameObject)); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1142 | } |
| 1143 | |
Victor Stinner | a42ca74 | 2020-04-28 19:01:31 +0200 | [diff] [blame] | 1144 | |
| 1145 | PyCodeObject * |
| 1146 | PyFrame_GetCode(PyFrameObject *frame) |
| 1147 | { |
| 1148 | assert(frame != NULL); |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 1149 | PyCodeObject *code = frame->f_code; |
| 1150 | assert(code != NULL); |
Victor Stinner | 8852ad4 | 2020-04-29 01:28:13 +0200 | [diff] [blame] | 1151 | Py_INCREF(code); |
Victor Stinner | 6d86a23 | 2020-04-29 00:56:58 +0200 | [diff] [blame] | 1152 | return code; |
Victor Stinner | a42ca74 | 2020-04-28 19:01:31 +0200 | [diff] [blame] | 1153 | } |
Victor Stinner | 7036477 | 2020-04-29 03:28:46 +0200 | [diff] [blame] | 1154 | |
| 1155 | |
| 1156 | PyFrameObject* |
| 1157 | PyFrame_GetBack(PyFrameObject *frame) |
| 1158 | { |
| 1159 | assert(frame != NULL); |
| 1160 | PyFrameObject *back = frame->f_back; |
| 1161 | Py_XINCREF(back); |
| 1162 | return back; |
| 1163 | } |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 1164 | |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 1165 | PyObject* |
| 1166 | _PyEval_BuiltinsFromGlobals(PyObject *globals) |
| 1167 | { |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 1168 | PyObject *builtins = _PyDict_GetItemIdWithError(globals, &PyId___builtins__); |
| 1169 | if (builtins) { |
| 1170 | if (PyModule_Check(builtins)) { |
| 1171 | builtins = PyModule_GetDict(builtins); |
| 1172 | assert(builtins != NULL); |
| 1173 | } |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 1174 | return Py_NewRef(builtins); |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 1175 | } |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 1176 | |
| 1177 | if (PyErr_Occurred()) { |
| 1178 | return NULL; |
| 1179 | } |
| 1180 | |
| 1181 | /* No builtins! Make up a minimal one. Give them 'None', at least. */ |
| 1182 | builtins = PyDict_New(); |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 1183 | if (builtins == NULL) { |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 1184 | return NULL; |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 1185 | } |
Victor Stinner | 44085a3 | 2021-02-18 19:20:16 +0100 | [diff] [blame^] | 1186 | if (PyDict_SetItemString(builtins, "None", Py_None) < 0) { |
| 1187 | Py_DECREF(builtins); |
| 1188 | return NULL; |
| 1189 | } |
Mark Shannon | d6c33fb | 2021-01-29 13:24:55 +0000 | [diff] [blame] | 1190 | return builtins; |
| 1191 | } |