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