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