| 1 | n/a | /* Frame object implementation */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #include "Python.h" |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | #include "code.h" |
|---|
| 6 | n/a | #include "frameobject.h" |
|---|
| 7 | n/a | #include "opcode.h" |
|---|
| 8 | n/a | #include "structmember.h" |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | #define OFF(x) offsetof(PyFrameObject, x) |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | static PyMemberDef frame_memberlist[] = { |
|---|
| 13 | n/a | {"f_back", T_OBJECT, OFF(f_back), READONLY}, |
|---|
| 14 | n/a | {"f_code", T_OBJECT, OFF(f_code), READONLY}, |
|---|
| 15 | n/a | {"f_builtins", T_OBJECT, OFF(f_builtins), READONLY}, |
|---|
| 16 | n/a | {"f_globals", T_OBJECT, OFF(f_globals), READONLY}, |
|---|
| 17 | n/a | {"f_lasti", T_INT, OFF(f_lasti), READONLY}, |
|---|
| 18 | n/a | {NULL} /* Sentinel */ |
|---|
| 19 | n/a | }; |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | static PyObject * |
|---|
| 22 | n/a | frame_getlocals(PyFrameObject *f, void *closure) |
|---|
| 23 | n/a | { |
|---|
| 24 | n/a | if (PyFrame_FastToLocalsWithError(f) < 0) |
|---|
| 25 | n/a | return NULL; |
|---|
| 26 | n/a | Py_INCREF(f->f_locals); |
|---|
| 27 | n/a | return f->f_locals; |
|---|
| 28 | n/a | } |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | int |
|---|
| 31 | n/a | PyFrame_GetLineNumber(PyFrameObject *f) |
|---|
| 32 | n/a | { |
|---|
| 33 | n/a | if (f->f_trace) |
|---|
| 34 | n/a | return f->f_lineno; |
|---|
| 35 | n/a | else |
|---|
| 36 | n/a | return PyCode_Addr2Line(f->f_code, f->f_lasti); |
|---|
| 37 | n/a | } |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | static PyObject * |
|---|
| 40 | n/a | frame_getlineno(PyFrameObject *f, void *closure) |
|---|
| 41 | n/a | { |
|---|
| 42 | n/a | return PyLong_FromLong(PyFrame_GetLineNumber(f)); |
|---|
| 43 | n/a | } |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | /* Setter for f_lineno - you can set f_lineno from within a trace function in |
|---|
| 46 | n/a | * order to jump to a given line of code, subject to some restrictions. Most |
|---|
| 47 | n/a | * lines are OK to jump to because they don't make any assumptions about the |
|---|
| 48 | n/a | * state of the stack (obvious because you could remove the line and the code |
|---|
| 49 | n/a | * would still work without any stack errors), but there are some constructs |
|---|
| 50 | n/a | * that limit jumping: |
|---|
| 51 | n/a | * |
|---|
| 52 | n/a | * o Lines with an 'except' statement on them can't be jumped to, because |
|---|
| 53 | n/a | * they expect an exception to be on the top of the stack. |
|---|
| 54 | n/a | * o Lines that live in a 'finally' block can't be jumped from or to, since |
|---|
| 55 | n/a | * the END_FINALLY expects to clean up the stack after the 'try' block. |
|---|
| 56 | n/a | * o 'try'/'for'/'while' blocks can't be jumped into because the blockstack |
|---|
| 57 | n/a | * needs to be set up before their code runs, and for 'for' loops the |
|---|
| 58 | n/a | * iterator needs to be on the stack. |
|---|
| 59 | n/a | */ |
|---|
| 60 | n/a | static int |
|---|
| 61 | n/a | frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno) |
|---|
| 62 | n/a | { |
|---|
| 63 | n/a | int new_lineno = 0; /* The new value of f_lineno */ |
|---|
| 64 | n/a | long l_new_lineno; |
|---|
| 65 | n/a | int overflow; |
|---|
| 66 | n/a | int new_lasti = 0; /* The new value of f_lasti */ |
|---|
| 67 | n/a | int new_iblock = 0; /* The new value of f_iblock */ |
|---|
| 68 | n/a | unsigned char *code = NULL; /* The bytecode for the frame... */ |
|---|
| 69 | n/a | Py_ssize_t code_len = 0; /* ...and its length */ |
|---|
| 70 | n/a | unsigned char *lnotab = NULL; /* Iterating over co_lnotab */ |
|---|
| 71 | n/a | Py_ssize_t lnotab_len = 0; /* (ditto) */ |
|---|
| 72 | n/a | int offset = 0; /* (ditto) */ |
|---|
| 73 | n/a | int line = 0; /* (ditto) */ |
|---|
| 74 | n/a | int addr = 0; /* (ditto) */ |
|---|
| 75 | n/a | int min_addr = 0; /* Scanning the SETUPs and POPs */ |
|---|
| 76 | n/a | int max_addr = 0; /* (ditto) */ |
|---|
| 77 | n/a | int delta_iblock = 0; /* (ditto) */ |
|---|
| 78 | n/a | int min_delta_iblock = 0; /* (ditto) */ |
|---|
| 79 | n/a | int min_iblock = 0; /* (ditto) */ |
|---|
| 80 | n/a | int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */ |
|---|
| 81 | n/a | int new_lasti_setup_addr = 0; /* (ditto) */ |
|---|
| 82 | n/a | int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */ |
|---|
| 83 | n/a | int in_finally[CO_MAXBLOCKS]; /* (ditto) */ |
|---|
| 84 | n/a | int blockstack_top = 0; /* (ditto) */ |
|---|
| 85 | n/a | unsigned char setup_op = 0; /* (ditto) */ |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | /* f_lineno must be an integer. */ |
|---|
| 88 | n/a | if (!PyLong_CheckExact(p_new_lineno)) { |
|---|
| 89 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 90 | n/a | "lineno must be an integer"); |
|---|
| 91 | n/a | return -1; |
|---|
| 92 | n/a | } |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | /* You can only do this from within a trace function, not via |
|---|
| 95 | n/a | * _getframe or similar hackery. */ |
|---|
| 96 | n/a | if (!f->f_trace) |
|---|
| 97 | n/a | { |
|---|
| 98 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 99 | n/a | "f_lineno can only be set by a" |
|---|
| 100 | n/a | " line trace function"); |
|---|
| 101 | n/a | return -1; |
|---|
| 102 | n/a | } |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | /* Fail if the line comes before the start of the code block. */ |
|---|
| 105 | n/a | l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow); |
|---|
| 106 | n/a | if (overflow |
|---|
| 107 | n/a | #if SIZEOF_LONG > SIZEOF_INT |
|---|
| 108 | n/a | || l_new_lineno > INT_MAX |
|---|
| 109 | n/a | || l_new_lineno < INT_MIN |
|---|
| 110 | n/a | #endif |
|---|
| 111 | n/a | ) { |
|---|
| 112 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 113 | n/a | "lineno out of range"); |
|---|
| 114 | n/a | return -1; |
|---|
| 115 | n/a | } |
|---|
| 116 | n/a | new_lineno = (int)l_new_lineno; |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | if (new_lineno < f->f_code->co_firstlineno) { |
|---|
| 119 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 120 | n/a | "line %d comes before the current code block", |
|---|
| 121 | n/a | new_lineno); |
|---|
| 122 | n/a | return -1; |
|---|
| 123 | n/a | } |
|---|
| 124 | n/a | else if (new_lineno == f->f_code->co_firstlineno) { |
|---|
| 125 | n/a | new_lasti = 0; |
|---|
| 126 | n/a | new_lineno = f->f_code->co_firstlineno; |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | else { |
|---|
| 129 | n/a | /* Find the bytecode offset for the start of the given |
|---|
| 130 | n/a | * line, or the first code-owning line after it. */ |
|---|
| 131 | n/a | char *tmp; |
|---|
| 132 | n/a | PyBytes_AsStringAndSize(f->f_code->co_lnotab, |
|---|
| 133 | n/a | &tmp, &lnotab_len); |
|---|
| 134 | n/a | lnotab = (unsigned char *) tmp; |
|---|
| 135 | n/a | addr = 0; |
|---|
| 136 | n/a | line = f->f_code->co_firstlineno; |
|---|
| 137 | n/a | new_lasti = -1; |
|---|
| 138 | n/a | for (offset = 0; offset < lnotab_len; offset += 2) { |
|---|
| 139 | n/a | addr += lnotab[offset]; |
|---|
| 140 | n/a | line += (signed char)lnotab[offset+1]; |
|---|
| 141 | n/a | if (line >= new_lineno) { |
|---|
| 142 | n/a | new_lasti = addr; |
|---|
| 143 | n/a | new_lineno = line; |
|---|
| 144 | n/a | break; |
|---|
| 145 | n/a | } |
|---|
| 146 | n/a | } |
|---|
| 147 | n/a | } |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | /* If we didn't reach the requested line, return an error. */ |
|---|
| 150 | n/a | if (new_lasti == -1) { |
|---|
| 151 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 152 | n/a | "line %d comes after the current code block", |
|---|
| 153 | n/a | new_lineno); |
|---|
| 154 | n/a | return -1; |
|---|
| 155 | n/a | } |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | /* We're now ready to look at the bytecode. */ |
|---|
| 158 | n/a | PyBytes_AsStringAndSize(f->f_code->co_code, (char **)&code, &code_len); |
|---|
| 159 | n/a | min_addr = Py_MIN(new_lasti, f->f_lasti); |
|---|
| 160 | n/a | max_addr = Py_MAX(new_lasti, f->f_lasti); |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | /* You can't jump onto a line with an 'except' statement on it - |
|---|
| 163 | n/a | * they expect to have an exception on the top of the stack, which |
|---|
| 164 | n/a | * won't be true if you jump to them. They always start with code |
|---|
| 165 | n/a | * that either pops the exception using POP_TOP (plain 'except:' |
|---|
| 166 | n/a | * lines do this) or duplicates the exception on the stack using |
|---|
| 167 | n/a | * DUP_TOP (if there's an exception type specified). See compile.c, |
|---|
| 168 | n/a | * 'com_try_except' for the full details. There aren't any other |
|---|
| 169 | n/a | * cases (AFAIK) where a line's code can start with DUP_TOP or |
|---|
| 170 | n/a | * POP_TOP, but if any ever appear, they'll be subject to the same |
|---|
| 171 | n/a | * restriction (but with a different error message). */ |
|---|
| 172 | n/a | if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) { |
|---|
| 173 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 174 | n/a | "can't jump to 'except' line as there's no exception"); |
|---|
| 175 | n/a | return -1; |
|---|
| 176 | n/a | } |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | /* You can't jump into or out of a 'finally' block because the 'try' |
|---|
| 179 | n/a | * block leaves something on the stack for the END_FINALLY to clean |
|---|
| 180 | n/a | * up. So we walk the bytecode, maintaining a simulated blockstack. |
|---|
| 181 | n/a | * When we reach the old or new address and it's in a 'finally' block |
|---|
| 182 | n/a | * we note the address of the corresponding SETUP_FINALLY. The jump |
|---|
| 183 | n/a | * is only legal if neither address is in a 'finally' block or |
|---|
| 184 | n/a | * they're both in the same one. 'blockstack' is a stack of the |
|---|
| 185 | n/a | * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks |
|---|
| 186 | n/a | * whether we're in a 'finally' block at each blockstack level. */ |
|---|
| 187 | n/a | f_lasti_setup_addr = -1; |
|---|
| 188 | n/a | new_lasti_setup_addr = -1; |
|---|
| 189 | n/a | memset(blockstack, '\0', sizeof(blockstack)); |
|---|
| 190 | n/a | memset(in_finally, '\0', sizeof(in_finally)); |
|---|
| 191 | n/a | blockstack_top = 0; |
|---|
| 192 | n/a | for (addr = 0; addr < code_len; addr += sizeof(_Py_CODEUNIT)) { |
|---|
| 193 | n/a | unsigned char op = code[addr]; |
|---|
| 194 | n/a | switch (op) { |
|---|
| 195 | n/a | case SETUP_LOOP: |
|---|
| 196 | n/a | case SETUP_EXCEPT: |
|---|
| 197 | n/a | case SETUP_FINALLY: |
|---|
| 198 | n/a | case SETUP_WITH: |
|---|
| 199 | n/a | case SETUP_ASYNC_WITH: |
|---|
| 200 | n/a | blockstack[blockstack_top++] = addr; |
|---|
| 201 | n/a | in_finally[blockstack_top-1] = 0; |
|---|
| 202 | n/a | break; |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | case POP_BLOCK: |
|---|
| 205 | n/a | assert(blockstack_top > 0); |
|---|
| 206 | n/a | setup_op = code[blockstack[blockstack_top-1]]; |
|---|
| 207 | n/a | if (setup_op == SETUP_FINALLY || setup_op == SETUP_WITH |
|---|
| 208 | n/a | || setup_op == SETUP_ASYNC_WITH) { |
|---|
| 209 | n/a | in_finally[blockstack_top-1] = 1; |
|---|
| 210 | n/a | } |
|---|
| 211 | n/a | else { |
|---|
| 212 | n/a | blockstack_top--; |
|---|
| 213 | n/a | } |
|---|
| 214 | n/a | break; |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | case END_FINALLY: |
|---|
| 217 | n/a | /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist |
|---|
| 218 | n/a | * in the bytecode but don't correspond to an actual |
|---|
| 219 | n/a | * 'finally' block. (If blockstack_top is 0, we must |
|---|
| 220 | n/a | * be seeing such an END_FINALLY.) */ |
|---|
| 221 | n/a | if (blockstack_top > 0) { |
|---|
| 222 | n/a | setup_op = code[blockstack[blockstack_top-1]]; |
|---|
| 223 | n/a | if (setup_op == SETUP_FINALLY || setup_op == SETUP_WITH |
|---|
| 224 | n/a | || setup_op == SETUP_ASYNC_WITH) { |
|---|
| 225 | n/a | blockstack_top--; |
|---|
| 226 | n/a | } |
|---|
| 227 | n/a | } |
|---|
| 228 | n/a | break; |
|---|
| 229 | n/a | } |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | /* For the addresses we're interested in, see whether they're |
|---|
| 232 | n/a | * within a 'finally' block and if so, remember the address |
|---|
| 233 | n/a | * of the SETUP_FINALLY. */ |
|---|
| 234 | n/a | if (addr == new_lasti || addr == f->f_lasti) { |
|---|
| 235 | n/a | int i = 0; |
|---|
| 236 | n/a | int setup_addr = -1; |
|---|
| 237 | n/a | for (i = blockstack_top-1; i >= 0; i--) { |
|---|
| 238 | n/a | if (in_finally[i]) { |
|---|
| 239 | n/a | setup_addr = blockstack[i]; |
|---|
| 240 | n/a | break; |
|---|
| 241 | n/a | } |
|---|
| 242 | n/a | } |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | if (setup_addr != -1) { |
|---|
| 245 | n/a | if (addr == new_lasti) { |
|---|
| 246 | n/a | new_lasti_setup_addr = setup_addr; |
|---|
| 247 | n/a | } |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | if (addr == f->f_lasti) { |
|---|
| 250 | n/a | f_lasti_setup_addr = setup_addr; |
|---|
| 251 | n/a | } |
|---|
| 252 | n/a | } |
|---|
| 253 | n/a | } |
|---|
| 254 | n/a | } |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | /* Verify that the blockstack tracking code didn't get lost. */ |
|---|
| 257 | n/a | assert(blockstack_top == 0); |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | /* After all that, are we jumping into / out of a 'finally' block? */ |
|---|
| 260 | n/a | if (new_lasti_setup_addr != f_lasti_setup_addr) { |
|---|
| 261 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 262 | n/a | "can't jump into or out of a 'finally' block"); |
|---|
| 263 | n/a | return -1; |
|---|
| 264 | n/a | } |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | /* Police block-jumping (you can't jump into the middle of a block) |
|---|
| 268 | n/a | * and ensure that the blockstack finishes up in a sensible state (by |
|---|
| 269 | n/a | * popping any blocks we're jumping out of). We look at all the |
|---|
| 270 | n/a | * blockstack operations between the current position and the new |
|---|
| 271 | n/a | * one, and keep track of how many blocks we drop out of on the way. |
|---|
| 272 | n/a | * By also keeping track of the lowest blockstack position we see, we |
|---|
| 273 | n/a | * can tell whether the jump goes into any blocks without coming out |
|---|
| 274 | n/a | * again - in that case we raise an exception below. */ |
|---|
| 275 | n/a | delta_iblock = 0; |
|---|
| 276 | n/a | for (addr = min_addr; addr < max_addr; addr += sizeof(_Py_CODEUNIT)) { |
|---|
| 277 | n/a | unsigned char op = code[addr]; |
|---|
| 278 | n/a | switch (op) { |
|---|
| 279 | n/a | case SETUP_LOOP: |
|---|
| 280 | n/a | case SETUP_EXCEPT: |
|---|
| 281 | n/a | case SETUP_FINALLY: |
|---|
| 282 | n/a | case SETUP_WITH: |
|---|
| 283 | n/a | case SETUP_ASYNC_WITH: |
|---|
| 284 | n/a | delta_iblock++; |
|---|
| 285 | n/a | break; |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | case POP_BLOCK: |
|---|
| 288 | n/a | delta_iblock--; |
|---|
| 289 | n/a | break; |
|---|
| 290 | n/a | } |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | min_delta_iblock = Py_MIN(min_delta_iblock, delta_iblock); |
|---|
| 293 | n/a | } |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | /* Derive the absolute iblock values from the deltas. */ |
|---|
| 296 | n/a | min_iblock = f->f_iblock + min_delta_iblock; |
|---|
| 297 | n/a | if (new_lasti > f->f_lasti) { |
|---|
| 298 | n/a | /* Forwards jump. */ |
|---|
| 299 | n/a | new_iblock = f->f_iblock + delta_iblock; |
|---|
| 300 | n/a | } |
|---|
| 301 | n/a | else { |
|---|
| 302 | n/a | /* Backwards jump. */ |
|---|
| 303 | n/a | new_iblock = f->f_iblock - delta_iblock; |
|---|
| 304 | n/a | } |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | /* Are we jumping into a block? */ |
|---|
| 307 | n/a | if (new_iblock > min_iblock) { |
|---|
| 308 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 309 | n/a | "can't jump into the middle of a block"); |
|---|
| 310 | n/a | return -1; |
|---|
| 311 | n/a | } |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | /* Pop any blocks that we're jumping out of. */ |
|---|
| 314 | n/a | while (f->f_iblock > new_iblock) { |
|---|
| 315 | n/a | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
|---|
| 316 | n/a | while ((f->f_stacktop - f->f_valuestack) > b->b_level) { |
|---|
| 317 | n/a | PyObject *v = (*--f->f_stacktop); |
|---|
| 318 | n/a | Py_DECREF(v); |
|---|
| 319 | n/a | } |
|---|
| 320 | n/a | } |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | /* Finally set the new f_lineno and f_lasti and return OK. */ |
|---|
| 323 | n/a | f->f_lineno = new_lineno; |
|---|
| 324 | n/a | f->f_lasti = new_lasti; |
|---|
| 325 | n/a | return 0; |
|---|
| 326 | n/a | } |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | static PyObject * |
|---|
| 329 | n/a | frame_gettrace(PyFrameObject *f, void *closure) |
|---|
| 330 | n/a | { |
|---|
| 331 | n/a | PyObject* trace = f->f_trace; |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | if (trace == NULL) |
|---|
| 334 | n/a | trace = Py_None; |
|---|
| 335 | n/a | |
|---|
| 336 | n/a | Py_INCREF(trace); |
|---|
| 337 | n/a | |
|---|
| 338 | n/a | return trace; |
|---|
| 339 | n/a | } |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | static int |
|---|
| 342 | n/a | frame_settrace(PyFrameObject *f, PyObject* v, void *closure) |
|---|
| 343 | n/a | { |
|---|
| 344 | n/a | /* We rely on f_lineno being accurate when f_trace is set. */ |
|---|
| 345 | n/a | f->f_lineno = PyFrame_GetLineNumber(f); |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | if (v == Py_None) |
|---|
| 348 | n/a | v = NULL; |
|---|
| 349 | n/a | Py_XINCREF(v); |
|---|
| 350 | n/a | Py_XSETREF(f->f_trace, v); |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | return 0; |
|---|
| 353 | n/a | } |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | |
|---|
| 356 | n/a | static PyGetSetDef frame_getsetlist[] = { |
|---|
| 357 | n/a | {"f_locals", (getter)frame_getlocals, NULL, NULL}, |
|---|
| 358 | n/a | {"f_lineno", (getter)frame_getlineno, |
|---|
| 359 | n/a | (setter)frame_setlineno, NULL}, |
|---|
| 360 | n/a | {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL}, |
|---|
| 361 | n/a | {0} |
|---|
| 362 | n/a | }; |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | /* Stack frames are allocated and deallocated at a considerable rate. |
|---|
| 365 | n/a | In an attempt to improve the speed of function calls, we: |
|---|
| 366 | n/a | |
|---|
| 367 | n/a | 1. Hold a single "zombie" frame on each code object. This retains |
|---|
| 368 | n/a | the allocated and initialised frame object from an invocation of |
|---|
| 369 | n/a | the code object. The zombie is reanimated the next time we need a |
|---|
| 370 | n/a | frame object for that code object. Doing this saves the malloc/ |
|---|
| 371 | n/a | realloc required when using a free_list frame that isn't the |
|---|
| 372 | n/a | correct size. It also saves some field initialisation. |
|---|
| 373 | n/a | |
|---|
| 374 | n/a | In zombie mode, no field of PyFrameObject holds a reference, but |
|---|
| 375 | n/a | the following fields are still valid: |
|---|
| 376 | n/a | |
|---|
| 377 | n/a | * ob_type, ob_size, f_code, f_valuestack; |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | * f_locals, f_trace, |
|---|
| 380 | n/a | f_exc_type, f_exc_value, f_exc_traceback are NULL; |
|---|
| 381 | n/a | |
|---|
| 382 | n/a | * f_localsplus does not require re-allocation and |
|---|
| 383 | n/a | the local variables in f_localsplus are NULL. |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | 2. We also maintain a separate free list of stack frames (just like |
|---|
| 386 | n/a | floats are allocated in a special way -- see floatobject.c). When |
|---|
| 387 | n/a | a stack frame is on the free list, only the following members have |
|---|
| 388 | n/a | a meaning: |
|---|
| 389 | n/a | ob_type == &Frametype |
|---|
| 390 | n/a | f_back next item on free list, or NULL |
|---|
| 391 | n/a | f_stacksize size of value stack |
|---|
| 392 | n/a | ob_size size of localsplus |
|---|
| 393 | n/a | Note that the value and block stacks are preserved -- this can save |
|---|
| 394 | n/a | another malloc() call or two (and two free() calls as well!). |
|---|
| 395 | n/a | Also note that, unlike for integers, each frame object is a |
|---|
| 396 | n/a | malloc'ed object in its own right -- it is only the actual calls to |
|---|
| 397 | n/a | malloc() that we are trying to save here, not the administration. |
|---|
| 398 | n/a | After all, while a typical program may make millions of calls, a |
|---|
| 399 | n/a | call depth of more than 20 or 30 is probably already exceptional |
|---|
| 400 | n/a | unless the program contains run-away recursion. I hope. |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | Later, PyFrame_MAXFREELIST was added to bound the # of frames saved on |
|---|
| 403 | n/a | free_list. Else programs creating lots of cyclic trash involving |
|---|
| 404 | n/a | frames could provoke free_list into growing without bound. |
|---|
| 405 | n/a | */ |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | static PyFrameObject *free_list = NULL; |
|---|
| 408 | n/a | static int numfree = 0; /* number of frames currently in free_list */ |
|---|
| 409 | n/a | /* max value for numfree */ |
|---|
| 410 | n/a | #define PyFrame_MAXFREELIST 200 |
|---|
| 411 | n/a | |
|---|
| 412 | n/a | static void _Py_HOT_FUNCTION |
|---|
| 413 | n/a | frame_dealloc(PyFrameObject *f) |
|---|
| 414 | n/a | { |
|---|
| 415 | n/a | PyObject **p, **valuestack; |
|---|
| 416 | n/a | PyCodeObject *co; |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | if (_PyObject_GC_IS_TRACKED(f)) |
|---|
| 419 | n/a | _PyObject_GC_UNTRACK(f); |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | Py_TRASHCAN_SAFE_BEGIN(f) |
|---|
| 422 | n/a | /* Kill all local variables */ |
|---|
| 423 | n/a | valuestack = f->f_valuestack; |
|---|
| 424 | n/a | for (p = f->f_localsplus; p < valuestack; p++) |
|---|
| 425 | n/a | Py_CLEAR(*p); |
|---|
| 426 | n/a | |
|---|
| 427 | n/a | /* Free stack */ |
|---|
| 428 | n/a | if (f->f_stacktop != NULL) { |
|---|
| 429 | n/a | for (p = valuestack; p < f->f_stacktop; p++) |
|---|
| 430 | n/a | Py_XDECREF(*p); |
|---|
| 431 | n/a | } |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | Py_XDECREF(f->f_back); |
|---|
| 434 | n/a | Py_DECREF(f->f_builtins); |
|---|
| 435 | n/a | Py_DECREF(f->f_globals); |
|---|
| 436 | n/a | Py_CLEAR(f->f_locals); |
|---|
| 437 | n/a | Py_CLEAR(f->f_trace); |
|---|
| 438 | n/a | Py_CLEAR(f->f_exc_type); |
|---|
| 439 | n/a | Py_CLEAR(f->f_exc_value); |
|---|
| 440 | n/a | Py_CLEAR(f->f_exc_traceback); |
|---|
| 441 | n/a | |
|---|
| 442 | n/a | co = f->f_code; |
|---|
| 443 | n/a | if (co->co_zombieframe == NULL) |
|---|
| 444 | n/a | co->co_zombieframe = f; |
|---|
| 445 | n/a | else if (numfree < PyFrame_MAXFREELIST) { |
|---|
| 446 | n/a | ++numfree; |
|---|
| 447 | n/a | f->f_back = free_list; |
|---|
| 448 | n/a | free_list = f; |
|---|
| 449 | n/a | } |
|---|
| 450 | n/a | else |
|---|
| 451 | n/a | PyObject_GC_Del(f); |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | Py_DECREF(co); |
|---|
| 454 | n/a | Py_TRASHCAN_SAFE_END(f) |
|---|
| 455 | n/a | } |
|---|
| 456 | n/a | |
|---|
| 457 | n/a | static int |
|---|
| 458 | n/a | frame_traverse(PyFrameObject *f, visitproc visit, void *arg) |
|---|
| 459 | n/a | { |
|---|
| 460 | n/a | PyObject **fastlocals, **p; |
|---|
| 461 | n/a | Py_ssize_t i, slots; |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | Py_VISIT(f->f_back); |
|---|
| 464 | n/a | Py_VISIT(f->f_code); |
|---|
| 465 | n/a | Py_VISIT(f->f_builtins); |
|---|
| 466 | n/a | Py_VISIT(f->f_globals); |
|---|
| 467 | n/a | Py_VISIT(f->f_locals); |
|---|
| 468 | n/a | Py_VISIT(f->f_trace); |
|---|
| 469 | n/a | Py_VISIT(f->f_exc_type); |
|---|
| 470 | n/a | Py_VISIT(f->f_exc_value); |
|---|
| 471 | n/a | Py_VISIT(f->f_exc_traceback); |
|---|
| 472 | n/a | |
|---|
| 473 | n/a | /* locals */ |
|---|
| 474 | n/a | slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars); |
|---|
| 475 | n/a | fastlocals = f->f_localsplus; |
|---|
| 476 | n/a | for (i = slots; --i >= 0; ++fastlocals) |
|---|
| 477 | n/a | Py_VISIT(*fastlocals); |
|---|
| 478 | n/a | |
|---|
| 479 | n/a | /* stack */ |
|---|
| 480 | n/a | if (f->f_stacktop != NULL) { |
|---|
| 481 | n/a | for (p = f->f_valuestack; p < f->f_stacktop; p++) |
|---|
| 482 | n/a | Py_VISIT(*p); |
|---|
| 483 | n/a | } |
|---|
| 484 | n/a | return 0; |
|---|
| 485 | n/a | } |
|---|
| 486 | n/a | |
|---|
| 487 | n/a | static void |
|---|
| 488 | n/a | frame_tp_clear(PyFrameObject *f) |
|---|
| 489 | n/a | { |
|---|
| 490 | n/a | PyObject **fastlocals, **p, **oldtop; |
|---|
| 491 | n/a | Py_ssize_t i, slots; |
|---|
| 492 | n/a | |
|---|
| 493 | n/a | /* Before anything else, make sure that this frame is clearly marked |
|---|
| 494 | n/a | * as being defunct! Else, e.g., a generator reachable from this |
|---|
| 495 | n/a | * frame may also point to this frame, believe itself to still be |
|---|
| 496 | n/a | * active, and try cleaning up this frame again. |
|---|
| 497 | n/a | */ |
|---|
| 498 | n/a | oldtop = f->f_stacktop; |
|---|
| 499 | n/a | f->f_stacktop = NULL; |
|---|
| 500 | n/a | f->f_executing = 0; |
|---|
| 501 | n/a | |
|---|
| 502 | n/a | Py_CLEAR(f->f_exc_type); |
|---|
| 503 | n/a | Py_CLEAR(f->f_exc_value); |
|---|
| 504 | n/a | Py_CLEAR(f->f_exc_traceback); |
|---|
| 505 | n/a | Py_CLEAR(f->f_trace); |
|---|
| 506 | n/a | |
|---|
| 507 | n/a | /* locals */ |
|---|
| 508 | n/a | slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars); |
|---|
| 509 | n/a | fastlocals = f->f_localsplus; |
|---|
| 510 | n/a | for (i = slots; --i >= 0; ++fastlocals) |
|---|
| 511 | n/a | Py_CLEAR(*fastlocals); |
|---|
| 512 | n/a | |
|---|
| 513 | n/a | /* stack */ |
|---|
| 514 | n/a | if (oldtop != NULL) { |
|---|
| 515 | n/a | for (p = f->f_valuestack; p < oldtop; p++) |
|---|
| 516 | n/a | Py_CLEAR(*p); |
|---|
| 517 | n/a | } |
|---|
| 518 | n/a | } |
|---|
| 519 | n/a | |
|---|
| 520 | n/a | static PyObject * |
|---|
| 521 | n/a | frame_clear(PyFrameObject *f) |
|---|
| 522 | n/a | { |
|---|
| 523 | n/a | if (f->f_executing) { |
|---|
| 524 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 525 | n/a | "cannot clear an executing frame"); |
|---|
| 526 | n/a | return NULL; |
|---|
| 527 | n/a | } |
|---|
| 528 | n/a | if (f->f_gen) { |
|---|
| 529 | n/a | _PyGen_Finalize(f->f_gen); |
|---|
| 530 | n/a | assert(f->f_gen == NULL); |
|---|
| 531 | n/a | } |
|---|
| 532 | n/a | frame_tp_clear(f); |
|---|
| 533 | n/a | Py_RETURN_NONE; |
|---|
| 534 | n/a | } |
|---|
| 535 | n/a | |
|---|
| 536 | n/a | PyDoc_STRVAR(clear__doc__, |
|---|
| 537 | n/a | "F.clear(): clear most references held by the frame"); |
|---|
| 538 | n/a | |
|---|
| 539 | n/a | static PyObject * |
|---|
| 540 | n/a | frame_sizeof(PyFrameObject *f) |
|---|
| 541 | n/a | { |
|---|
| 542 | n/a | Py_ssize_t res, extras, ncells, nfrees; |
|---|
| 543 | n/a | |
|---|
| 544 | n/a | ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars); |
|---|
| 545 | n/a | nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars); |
|---|
| 546 | n/a | extras = f->f_code->co_stacksize + f->f_code->co_nlocals + |
|---|
| 547 | n/a | ncells + nfrees; |
|---|
| 548 | n/a | /* subtract one as it is already included in PyFrameObject */ |
|---|
| 549 | n/a | res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *); |
|---|
| 550 | n/a | |
|---|
| 551 | n/a | return PyLong_FromSsize_t(res); |
|---|
| 552 | n/a | } |
|---|
| 553 | n/a | |
|---|
| 554 | n/a | PyDoc_STRVAR(sizeof__doc__, |
|---|
| 555 | n/a | "F.__sizeof__() -> size of F in memory, in bytes"); |
|---|
| 556 | n/a | |
|---|
| 557 | n/a | static PyMethodDef frame_methods[] = { |
|---|
| 558 | n/a | {"clear", (PyCFunction)frame_clear, METH_NOARGS, |
|---|
| 559 | n/a | clear__doc__}, |
|---|
| 560 | n/a | {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS, |
|---|
| 561 | n/a | sizeof__doc__}, |
|---|
| 562 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 563 | n/a | }; |
|---|
| 564 | n/a | |
|---|
| 565 | n/a | PyTypeObject PyFrame_Type = { |
|---|
| 566 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 567 | n/a | "frame", |
|---|
| 568 | n/a | sizeof(PyFrameObject), |
|---|
| 569 | n/a | sizeof(PyObject *), |
|---|
| 570 | n/a | (destructor)frame_dealloc, /* tp_dealloc */ |
|---|
| 571 | n/a | 0, /* tp_print */ |
|---|
| 572 | n/a | 0, /* tp_getattr */ |
|---|
| 573 | n/a | 0, /* tp_setattr */ |
|---|
| 574 | n/a | 0, /* tp_reserved */ |
|---|
| 575 | n/a | 0, /* tp_repr */ |
|---|
| 576 | n/a | 0, /* tp_as_number */ |
|---|
| 577 | n/a | 0, /* tp_as_sequence */ |
|---|
| 578 | n/a | 0, /* tp_as_mapping */ |
|---|
| 579 | n/a | 0, /* tp_hash */ |
|---|
| 580 | n/a | 0, /* tp_call */ |
|---|
| 581 | n/a | 0, /* tp_str */ |
|---|
| 582 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 583 | n/a | PyObject_GenericSetAttr, /* tp_setattro */ |
|---|
| 584 | n/a | 0, /* tp_as_buffer */ |
|---|
| 585 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|---|
| 586 | n/a | 0, /* tp_doc */ |
|---|
| 587 | n/a | (traverseproc)frame_traverse, /* tp_traverse */ |
|---|
| 588 | n/a | (inquiry)frame_tp_clear, /* tp_clear */ |
|---|
| 589 | n/a | 0, /* tp_richcompare */ |
|---|
| 590 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 591 | n/a | 0, /* tp_iter */ |
|---|
| 592 | n/a | 0, /* tp_iternext */ |
|---|
| 593 | n/a | frame_methods, /* tp_methods */ |
|---|
| 594 | n/a | frame_memberlist, /* tp_members */ |
|---|
| 595 | n/a | frame_getsetlist, /* tp_getset */ |
|---|
| 596 | n/a | 0, /* tp_base */ |
|---|
| 597 | n/a | 0, /* tp_dict */ |
|---|
| 598 | n/a | }; |
|---|
| 599 | n/a | |
|---|
| 600 | n/a | _Py_IDENTIFIER(__builtins__); |
|---|
| 601 | n/a | |
|---|
| 602 | n/a | int _PyFrame_Init() |
|---|
| 603 | n/a | { |
|---|
| 604 | n/a | /* Before, PyId___builtins__ was a string created explicitly in |
|---|
| 605 | n/a | this function. Now there is nothing to initialize anymore, but |
|---|
| 606 | n/a | the function is kept for backward compatibility. */ |
|---|
| 607 | n/a | return 1; |
|---|
| 608 | n/a | } |
|---|
| 609 | n/a | |
|---|
| 610 | n/a | PyFrameObject* _Py_HOT_FUNCTION |
|---|
| 611 | n/a | _PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code, |
|---|
| 612 | n/a | PyObject *globals, PyObject *locals) |
|---|
| 613 | n/a | { |
|---|
| 614 | n/a | PyFrameObject *back = tstate->frame; |
|---|
| 615 | n/a | PyFrameObject *f; |
|---|
| 616 | n/a | PyObject *builtins; |
|---|
| 617 | n/a | Py_ssize_t i; |
|---|
| 618 | n/a | |
|---|
| 619 | n/a | #ifdef Py_DEBUG |
|---|
| 620 | n/a | if (code == NULL || globals == NULL || !PyDict_Check(globals) || |
|---|
| 621 | n/a | (locals != NULL && !PyMapping_Check(locals))) { |
|---|
| 622 | n/a | PyErr_BadInternalCall(); |
|---|
| 623 | n/a | return NULL; |
|---|
| 624 | n/a | } |
|---|
| 625 | n/a | #endif |
|---|
| 626 | n/a | if (back == NULL || back->f_globals != globals) { |
|---|
| 627 | n/a | builtins = _PyDict_GetItemId(globals, &PyId___builtins__); |
|---|
| 628 | n/a | if (builtins) { |
|---|
| 629 | n/a | if (PyModule_Check(builtins)) { |
|---|
| 630 | n/a | builtins = PyModule_GetDict(builtins); |
|---|
| 631 | n/a | assert(builtins != NULL); |
|---|
| 632 | n/a | } |
|---|
| 633 | n/a | } |
|---|
| 634 | n/a | if (builtins == NULL) { |
|---|
| 635 | n/a | /* No builtins! Make up a minimal one |
|---|
| 636 | n/a | Give them 'None', at least. */ |
|---|
| 637 | n/a | builtins = PyDict_New(); |
|---|
| 638 | n/a | if (builtins == NULL || |
|---|
| 639 | n/a | PyDict_SetItemString( |
|---|
| 640 | n/a | builtins, "None", Py_None) < 0) |
|---|
| 641 | n/a | return NULL; |
|---|
| 642 | n/a | } |
|---|
| 643 | n/a | else |
|---|
| 644 | n/a | Py_INCREF(builtins); |
|---|
| 645 | n/a | |
|---|
| 646 | n/a | } |
|---|
| 647 | n/a | else { |
|---|
| 648 | n/a | /* If we share the globals, we share the builtins. |
|---|
| 649 | n/a | Save a lookup and a call. */ |
|---|
| 650 | n/a | builtins = back->f_builtins; |
|---|
| 651 | n/a | assert(builtins != NULL); |
|---|
| 652 | n/a | Py_INCREF(builtins); |
|---|
| 653 | n/a | } |
|---|
| 654 | n/a | if (code->co_zombieframe != NULL) { |
|---|
| 655 | n/a | f = code->co_zombieframe; |
|---|
| 656 | n/a | code->co_zombieframe = NULL; |
|---|
| 657 | n/a | _Py_NewReference((PyObject *)f); |
|---|
| 658 | n/a | assert(f->f_code == code); |
|---|
| 659 | n/a | } |
|---|
| 660 | n/a | else { |
|---|
| 661 | n/a | Py_ssize_t extras, ncells, nfrees; |
|---|
| 662 | n/a | ncells = PyTuple_GET_SIZE(code->co_cellvars); |
|---|
| 663 | n/a | nfrees = PyTuple_GET_SIZE(code->co_freevars); |
|---|
| 664 | n/a | extras = code->co_stacksize + code->co_nlocals + ncells + |
|---|
| 665 | n/a | nfrees; |
|---|
| 666 | n/a | if (free_list == NULL) { |
|---|
| 667 | n/a | f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, |
|---|
| 668 | n/a | extras); |
|---|
| 669 | n/a | if (f == NULL) { |
|---|
| 670 | n/a | Py_DECREF(builtins); |
|---|
| 671 | n/a | return NULL; |
|---|
| 672 | n/a | } |
|---|
| 673 | n/a | } |
|---|
| 674 | n/a | else { |
|---|
| 675 | n/a | assert(numfree > 0); |
|---|
| 676 | n/a | --numfree; |
|---|
| 677 | n/a | f = free_list; |
|---|
| 678 | n/a | free_list = free_list->f_back; |
|---|
| 679 | n/a | if (Py_SIZE(f) < extras) { |
|---|
| 680 | n/a | PyFrameObject *new_f = PyObject_GC_Resize(PyFrameObject, f, extras); |
|---|
| 681 | n/a | if (new_f == NULL) { |
|---|
| 682 | n/a | PyObject_GC_Del(f); |
|---|
| 683 | n/a | Py_DECREF(builtins); |
|---|
| 684 | n/a | return NULL; |
|---|
| 685 | n/a | } |
|---|
| 686 | n/a | f = new_f; |
|---|
| 687 | n/a | } |
|---|
| 688 | n/a | _Py_NewReference((PyObject *)f); |
|---|
| 689 | n/a | } |
|---|
| 690 | n/a | |
|---|
| 691 | n/a | f->f_code = code; |
|---|
| 692 | n/a | extras = code->co_nlocals + ncells + nfrees; |
|---|
| 693 | n/a | f->f_valuestack = f->f_localsplus + extras; |
|---|
| 694 | n/a | for (i=0; i<extras; i++) |
|---|
| 695 | n/a | f->f_localsplus[i] = NULL; |
|---|
| 696 | n/a | f->f_locals = NULL; |
|---|
| 697 | n/a | f->f_trace = NULL; |
|---|
| 698 | n/a | f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL; |
|---|
| 699 | n/a | } |
|---|
| 700 | n/a | f->f_stacktop = f->f_valuestack; |
|---|
| 701 | n/a | f->f_builtins = builtins; |
|---|
| 702 | n/a | Py_XINCREF(back); |
|---|
| 703 | n/a | f->f_back = back; |
|---|
| 704 | n/a | Py_INCREF(code); |
|---|
| 705 | n/a | Py_INCREF(globals); |
|---|
| 706 | n/a | f->f_globals = globals; |
|---|
| 707 | n/a | /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */ |
|---|
| 708 | n/a | if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) == |
|---|
| 709 | n/a | (CO_NEWLOCALS | CO_OPTIMIZED)) |
|---|
| 710 | n/a | ; /* f_locals = NULL; will be set by PyFrame_FastToLocals() */ |
|---|
| 711 | n/a | else if (code->co_flags & CO_NEWLOCALS) { |
|---|
| 712 | n/a | locals = PyDict_New(); |
|---|
| 713 | n/a | if (locals == NULL) { |
|---|
| 714 | n/a | Py_DECREF(f); |
|---|
| 715 | n/a | return NULL; |
|---|
| 716 | n/a | } |
|---|
| 717 | n/a | f->f_locals = locals; |
|---|
| 718 | n/a | } |
|---|
| 719 | n/a | else { |
|---|
| 720 | n/a | if (locals == NULL) |
|---|
| 721 | n/a | locals = globals; |
|---|
| 722 | n/a | Py_INCREF(locals); |
|---|
| 723 | n/a | f->f_locals = locals; |
|---|
| 724 | n/a | } |
|---|
| 725 | n/a | |
|---|
| 726 | n/a | f->f_lasti = -1; |
|---|
| 727 | n/a | f->f_lineno = code->co_firstlineno; |
|---|
| 728 | n/a | f->f_iblock = 0; |
|---|
| 729 | n/a | f->f_executing = 0; |
|---|
| 730 | n/a | f->f_gen = NULL; |
|---|
| 731 | n/a | |
|---|
| 732 | n/a | return f; |
|---|
| 733 | n/a | } |
|---|
| 734 | n/a | |
|---|
| 735 | n/a | PyFrameObject* |
|---|
| 736 | n/a | PyFrame_New(PyThreadState *tstate, PyCodeObject *code, |
|---|
| 737 | n/a | PyObject *globals, PyObject *locals) |
|---|
| 738 | n/a | { |
|---|
| 739 | n/a | PyFrameObject *f = _PyFrame_New_NoTrack(tstate, code, globals, locals); |
|---|
| 740 | n/a | if (f) |
|---|
| 741 | n/a | _PyObject_GC_TRACK(f); |
|---|
| 742 | n/a | return f; |
|---|
| 743 | n/a | } |
|---|
| 744 | n/a | |
|---|
| 745 | n/a | |
|---|
| 746 | n/a | /* Block management */ |
|---|
| 747 | n/a | |
|---|
| 748 | n/a | void |
|---|
| 749 | n/a | PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level) |
|---|
| 750 | n/a | { |
|---|
| 751 | n/a | PyTryBlock *b; |
|---|
| 752 | n/a | if (f->f_iblock >= CO_MAXBLOCKS) |
|---|
| 753 | n/a | Py_FatalError("XXX block stack overflow"); |
|---|
| 754 | n/a | b = &f->f_blockstack[f->f_iblock++]; |
|---|
| 755 | n/a | b->b_type = type; |
|---|
| 756 | n/a | b->b_level = level; |
|---|
| 757 | n/a | b->b_handler = handler; |
|---|
| 758 | n/a | } |
|---|
| 759 | n/a | |
|---|
| 760 | n/a | PyTryBlock * |
|---|
| 761 | n/a | PyFrame_BlockPop(PyFrameObject *f) |
|---|
| 762 | n/a | { |
|---|
| 763 | n/a | PyTryBlock *b; |
|---|
| 764 | n/a | if (f->f_iblock <= 0) |
|---|
| 765 | n/a | Py_FatalError("XXX block stack underflow"); |
|---|
| 766 | n/a | b = &f->f_blockstack[--f->f_iblock]; |
|---|
| 767 | n/a | return b; |
|---|
| 768 | n/a | } |
|---|
| 769 | n/a | |
|---|
| 770 | n/a | /* Convert between "fast" version of locals and dictionary version. |
|---|
| 771 | n/a | |
|---|
| 772 | n/a | map and values are input arguments. map is a tuple of strings. |
|---|
| 773 | n/a | values is an array of PyObject*. At index i, map[i] is the name of |
|---|
| 774 | n/a | the variable with value values[i]. The function copies the first |
|---|
| 775 | n/a | nmap variable from map/values into dict. If values[i] is NULL, |
|---|
| 776 | n/a | the variable is deleted from dict. |
|---|
| 777 | n/a | |
|---|
| 778 | n/a | If deref is true, then the values being copied are cell variables |
|---|
| 779 | n/a | and the value is extracted from the cell variable before being put |
|---|
| 780 | n/a | in dict. |
|---|
| 781 | n/a | */ |
|---|
| 782 | n/a | |
|---|
| 783 | n/a | static int |
|---|
| 784 | n/a | map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values, |
|---|
| 785 | n/a | int deref) |
|---|
| 786 | n/a | { |
|---|
| 787 | n/a | Py_ssize_t j; |
|---|
| 788 | n/a | assert(PyTuple_Check(map)); |
|---|
| 789 | n/a | assert(PyDict_Check(dict)); |
|---|
| 790 | n/a | assert(PyTuple_Size(map) >= nmap); |
|---|
| 791 | n/a | for (j = nmap; --j >= 0; ) { |
|---|
| 792 | n/a | PyObject *key = PyTuple_GET_ITEM(map, j); |
|---|
| 793 | n/a | PyObject *value = values[j]; |
|---|
| 794 | n/a | assert(PyUnicode_Check(key)); |
|---|
| 795 | n/a | if (deref && value != NULL) { |
|---|
| 796 | n/a | assert(PyCell_Check(value)); |
|---|
| 797 | n/a | value = PyCell_GET(value); |
|---|
| 798 | n/a | } |
|---|
| 799 | n/a | if (value == NULL) { |
|---|
| 800 | n/a | if (PyObject_DelItem(dict, key) != 0) { |
|---|
| 801 | n/a | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
|---|
| 802 | n/a | PyErr_Clear(); |
|---|
| 803 | n/a | else |
|---|
| 804 | n/a | return -1; |
|---|
| 805 | n/a | } |
|---|
| 806 | n/a | } |
|---|
| 807 | n/a | else { |
|---|
| 808 | n/a | if (PyObject_SetItem(dict, key, value) != 0) |
|---|
| 809 | n/a | return -1; |
|---|
| 810 | n/a | } |
|---|
| 811 | n/a | } |
|---|
| 812 | n/a | return 0; |
|---|
| 813 | n/a | } |
|---|
| 814 | n/a | |
|---|
| 815 | n/a | /* Copy values from the "locals" dict into the fast locals. |
|---|
| 816 | n/a | |
|---|
| 817 | n/a | dict is an input argument containing string keys representing |
|---|
| 818 | n/a | variables names and arbitrary PyObject* as values. |
|---|
| 819 | n/a | |
|---|
| 820 | n/a | map and values are input arguments. map is a tuple of strings. |
|---|
| 821 | n/a | values is an array of PyObject*. At index i, map[i] is the name of |
|---|
| 822 | n/a | the variable with value values[i]. The function copies the first |
|---|
| 823 | n/a | nmap variable from map/values into dict. If values[i] is NULL, |
|---|
| 824 | n/a | the variable is deleted from dict. |
|---|
| 825 | n/a | |
|---|
| 826 | n/a | If deref is true, then the values being copied are cell variables |
|---|
| 827 | n/a | and the value is extracted from the cell variable before being put |
|---|
| 828 | n/a | in dict. If clear is true, then variables in map but not in dict |
|---|
| 829 | n/a | are set to NULL in map; if clear is false, variables missing in |
|---|
| 830 | n/a | dict are ignored. |
|---|
| 831 | n/a | |
|---|
| 832 | n/a | Exceptions raised while modifying the dict are silently ignored, |
|---|
| 833 | n/a | because there is no good way to report them. |
|---|
| 834 | n/a | */ |
|---|
| 835 | n/a | |
|---|
| 836 | n/a | static void |
|---|
| 837 | n/a | dict_to_map(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values, |
|---|
| 838 | n/a | int deref, int clear) |
|---|
| 839 | n/a | { |
|---|
| 840 | n/a | Py_ssize_t j; |
|---|
| 841 | n/a | assert(PyTuple_Check(map)); |
|---|
| 842 | n/a | assert(PyDict_Check(dict)); |
|---|
| 843 | n/a | assert(PyTuple_Size(map) >= nmap); |
|---|
| 844 | n/a | for (j = nmap; --j >= 0; ) { |
|---|
| 845 | n/a | PyObject *key = PyTuple_GET_ITEM(map, j); |
|---|
| 846 | n/a | PyObject *value = PyObject_GetItem(dict, key); |
|---|
| 847 | n/a | assert(PyUnicode_Check(key)); |
|---|
| 848 | n/a | /* We only care about NULLs if clear is true. */ |
|---|
| 849 | n/a | if (value == NULL) { |
|---|
| 850 | n/a | PyErr_Clear(); |
|---|
| 851 | n/a | if (!clear) |
|---|
| 852 | n/a | continue; |
|---|
| 853 | n/a | } |
|---|
| 854 | n/a | if (deref) { |
|---|
| 855 | n/a | assert(PyCell_Check(values[j])); |
|---|
| 856 | n/a | if (PyCell_GET(values[j]) != value) { |
|---|
| 857 | n/a | if (PyCell_Set(values[j], value) < 0) |
|---|
| 858 | n/a | PyErr_Clear(); |
|---|
| 859 | n/a | } |
|---|
| 860 | n/a | } else if (values[j] != value) { |
|---|
| 861 | n/a | Py_XINCREF(value); |
|---|
| 862 | n/a | Py_XSETREF(values[j], value); |
|---|
| 863 | n/a | } |
|---|
| 864 | n/a | Py_XDECREF(value); |
|---|
| 865 | n/a | } |
|---|
| 866 | n/a | } |
|---|
| 867 | n/a | |
|---|
| 868 | n/a | int |
|---|
| 869 | n/a | PyFrame_FastToLocalsWithError(PyFrameObject *f) |
|---|
| 870 | n/a | { |
|---|
| 871 | n/a | /* Merge fast locals into f->f_locals */ |
|---|
| 872 | n/a | PyObject *locals, *map; |
|---|
| 873 | n/a | PyObject **fast; |
|---|
| 874 | n/a | PyCodeObject *co; |
|---|
| 875 | n/a | Py_ssize_t j; |
|---|
| 876 | n/a | Py_ssize_t ncells, nfreevars; |
|---|
| 877 | n/a | |
|---|
| 878 | n/a | if (f == NULL) { |
|---|
| 879 | n/a | PyErr_BadInternalCall(); |
|---|
| 880 | n/a | return -1; |
|---|
| 881 | n/a | } |
|---|
| 882 | n/a | locals = f->f_locals; |
|---|
| 883 | n/a | if (locals == NULL) { |
|---|
| 884 | n/a | locals = f->f_locals = PyDict_New(); |
|---|
| 885 | n/a | if (locals == NULL) |
|---|
| 886 | n/a | return -1; |
|---|
| 887 | n/a | } |
|---|
| 888 | n/a | co = f->f_code; |
|---|
| 889 | n/a | map = co->co_varnames; |
|---|
| 890 | n/a | if (!PyTuple_Check(map)) { |
|---|
| 891 | n/a | PyErr_Format(PyExc_SystemError, |
|---|
| 892 | n/a | "co_varnames must be a tuple, not %s", |
|---|
| 893 | n/a | Py_TYPE(map)->tp_name); |
|---|
| 894 | n/a | return -1; |
|---|
| 895 | n/a | } |
|---|
| 896 | n/a | fast = f->f_localsplus; |
|---|
| 897 | n/a | j = PyTuple_GET_SIZE(map); |
|---|
| 898 | n/a | if (j > co->co_nlocals) |
|---|
| 899 | n/a | j = co->co_nlocals; |
|---|
| 900 | n/a | if (co->co_nlocals) { |
|---|
| 901 | n/a | if (map_to_dict(map, j, locals, fast, 0) < 0) |
|---|
| 902 | n/a | return -1; |
|---|
| 903 | n/a | } |
|---|
| 904 | n/a | ncells = PyTuple_GET_SIZE(co->co_cellvars); |
|---|
| 905 | n/a | nfreevars = PyTuple_GET_SIZE(co->co_freevars); |
|---|
| 906 | n/a | if (ncells || nfreevars) { |
|---|
| 907 | n/a | if (map_to_dict(co->co_cellvars, ncells, |
|---|
| 908 | n/a | locals, fast + co->co_nlocals, 1)) |
|---|
| 909 | n/a | return -1; |
|---|
| 910 | n/a | |
|---|
| 911 | n/a | /* If the namespace is unoptimized, then one of the |
|---|
| 912 | n/a | following cases applies: |
|---|
| 913 | n/a | 1. It does not contain free variables, because it |
|---|
| 914 | n/a | uses import * or is a top-level namespace. |
|---|
| 915 | n/a | 2. It is a class namespace. |
|---|
| 916 | n/a | We don't want to accidentally copy free variables |
|---|
| 917 | n/a | into the locals dict used by the class. |
|---|
| 918 | n/a | */ |
|---|
| 919 | n/a | if (co->co_flags & CO_OPTIMIZED) { |
|---|
| 920 | n/a | if (map_to_dict(co->co_freevars, nfreevars, |
|---|
| 921 | n/a | locals, fast + co->co_nlocals + ncells, 1) < 0) |
|---|
| 922 | n/a | return -1; |
|---|
| 923 | n/a | } |
|---|
| 924 | n/a | } |
|---|
| 925 | n/a | return 0; |
|---|
| 926 | n/a | } |
|---|
| 927 | n/a | |
|---|
| 928 | n/a | void |
|---|
| 929 | n/a | PyFrame_FastToLocals(PyFrameObject *f) |
|---|
| 930 | n/a | { |
|---|
| 931 | n/a | int res; |
|---|
| 932 | n/a | |
|---|
| 933 | n/a | assert(!PyErr_Occurred()); |
|---|
| 934 | n/a | |
|---|
| 935 | n/a | res = PyFrame_FastToLocalsWithError(f); |
|---|
| 936 | n/a | if (res < 0) |
|---|
| 937 | n/a | PyErr_Clear(); |
|---|
| 938 | n/a | } |
|---|
| 939 | n/a | |
|---|
| 940 | n/a | void |
|---|
| 941 | n/a | PyFrame_LocalsToFast(PyFrameObject *f, int clear) |
|---|
| 942 | n/a | { |
|---|
| 943 | n/a | /* Merge f->f_locals into fast locals */ |
|---|
| 944 | n/a | PyObject *locals, *map; |
|---|
| 945 | n/a | PyObject **fast; |
|---|
| 946 | n/a | PyObject *error_type, *error_value, *error_traceback; |
|---|
| 947 | n/a | PyCodeObject *co; |
|---|
| 948 | n/a | Py_ssize_t j; |
|---|
| 949 | n/a | Py_ssize_t ncells, nfreevars; |
|---|
| 950 | n/a | if (f == NULL) |
|---|
| 951 | n/a | return; |
|---|
| 952 | n/a | locals = f->f_locals; |
|---|
| 953 | n/a | co = f->f_code; |
|---|
| 954 | n/a | map = co->co_varnames; |
|---|
| 955 | n/a | if (locals == NULL) |
|---|
| 956 | n/a | return; |
|---|
| 957 | n/a | if (!PyTuple_Check(map)) |
|---|
| 958 | n/a | return; |
|---|
| 959 | n/a | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
|---|
| 960 | n/a | fast = f->f_localsplus; |
|---|
| 961 | n/a | j = PyTuple_GET_SIZE(map); |
|---|
| 962 | n/a | if (j > co->co_nlocals) |
|---|
| 963 | n/a | j = co->co_nlocals; |
|---|
| 964 | n/a | if (co->co_nlocals) |
|---|
| 965 | n/a | dict_to_map(co->co_varnames, j, locals, fast, 0, clear); |
|---|
| 966 | n/a | ncells = PyTuple_GET_SIZE(co->co_cellvars); |
|---|
| 967 | n/a | nfreevars = PyTuple_GET_SIZE(co->co_freevars); |
|---|
| 968 | n/a | if (ncells || nfreevars) { |
|---|
| 969 | n/a | dict_to_map(co->co_cellvars, ncells, |
|---|
| 970 | n/a | locals, fast + co->co_nlocals, 1, clear); |
|---|
| 971 | n/a | /* Same test as in PyFrame_FastToLocals() above. */ |
|---|
| 972 | n/a | if (co->co_flags & CO_OPTIMIZED) { |
|---|
| 973 | n/a | dict_to_map(co->co_freevars, nfreevars, |
|---|
| 974 | n/a | locals, fast + co->co_nlocals + ncells, 1, |
|---|
| 975 | n/a | clear); |
|---|
| 976 | n/a | } |
|---|
| 977 | n/a | } |
|---|
| 978 | n/a | PyErr_Restore(error_type, error_value, error_traceback); |
|---|
| 979 | n/a | } |
|---|
| 980 | n/a | |
|---|
| 981 | n/a | /* Clear out the free list */ |
|---|
| 982 | n/a | int |
|---|
| 983 | n/a | PyFrame_ClearFreeList(void) |
|---|
| 984 | n/a | { |
|---|
| 985 | n/a | int freelist_size = numfree; |
|---|
| 986 | n/a | |
|---|
| 987 | n/a | while (free_list != NULL) { |
|---|
| 988 | n/a | PyFrameObject *f = free_list; |
|---|
| 989 | n/a | free_list = free_list->f_back; |
|---|
| 990 | n/a | PyObject_GC_Del(f); |
|---|
| 991 | n/a | --numfree; |
|---|
| 992 | n/a | } |
|---|
| 993 | n/a | assert(numfree == 0); |
|---|
| 994 | n/a | return freelist_size; |
|---|
| 995 | n/a | } |
|---|
| 996 | n/a | |
|---|
| 997 | n/a | void |
|---|
| 998 | n/a | PyFrame_Fini(void) |
|---|
| 999 | n/a | { |
|---|
| 1000 | n/a | (void)PyFrame_ClearFreeList(); |
|---|
| 1001 | n/a | } |
|---|
| 1002 | n/a | |
|---|
| 1003 | n/a | /* Print summary info about the state of the optimized allocator */ |
|---|
| 1004 | n/a | void |
|---|
| 1005 | n/a | _PyFrame_DebugMallocStats(FILE *out) |
|---|
| 1006 | n/a | { |
|---|
| 1007 | n/a | _PyDebugAllocatorStats(out, |
|---|
| 1008 | n/a | "free PyFrameObject", |
|---|
| 1009 | n/a | numfree, sizeof(PyFrameObject)); |
|---|
| 1010 | n/a | } |
|---|
| 1011 | n/a | |
|---|