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