Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Frame object implementation */ |
| 3 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
| 6 | #include "compile.h" |
| 7 | #include "frameobject.h" |
| 8 | #include "opcode.h" |
| 9 | #include "structmember.h" |
| 10 | |
Neal Norwitz | 91787cb | 2002-12-18 23:33:35 +0000 | [diff] [blame] | 11 | #undef MIN |
| 12 | #undef MAX |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 13 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 14 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 15 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 16 | #define OFF(x) offsetof(PyFrameObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 18 | static PyMemberDef frame_memberlist[] = { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 19 | {"f_back", T_OBJECT, OFF(f_back), RO}, |
| 20 | {"f_code", T_OBJECT, OFF(f_code), RO}, |
Guido van Rossum | c113482 | 1995-01-10 10:39:16 +0000 | [diff] [blame] | 21 | {"f_builtins", T_OBJECT, OFF(f_builtins),RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 22 | {"f_globals", T_OBJECT, OFF(f_globals), RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 23 | {"f_lasti", T_INT, OFF(f_lasti), RO}, |
Guido van Rossum | c113482 | 1995-01-10 10:39:16 +0000 | [diff] [blame] | 24 | {"f_restricted",T_INT, OFF(f_restricted),RO}, |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 25 | {"f_exc_type", T_OBJECT, OFF(f_exc_type)}, |
| 26 | {"f_exc_value", T_OBJECT, OFF(f_exc_value)}, |
| 27 | {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 28 | {NULL} /* Sentinel */ |
| 29 | }; |
| 30 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 31 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 32 | frame_getlocals(PyFrameObject *f, void *closure) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 33 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 34 | PyFrame_FastToLocals(f); |
| 35 | Py_INCREF(f->f_locals); |
| 36 | return f->f_locals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 39 | static PyObject * |
| 40 | frame_getlineno(PyFrameObject *f, void *closure) |
| 41 | { |
| 42 | int lineno; |
| 43 | |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 44 | if (f->f_trace) |
| 45 | lineno = f->f_lineno; |
| 46 | else |
| 47 | lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 48 | |
| 49 | return PyInt_FromLong(lineno); |
| 50 | } |
| 51 | |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 52 | /* Setter for f_lineno - you can set f_lineno from within a trace function in |
| 53 | * order to jump to a given line of code, subject to some restrictions. Most |
| 54 | * lines are OK to jump to because they don't make any assumptions about the |
| 55 | * state of the stack (obvious because you could remove the line and the code |
| 56 | * would still work without any stack errors), but there are some constructs |
| 57 | * that limit jumping: |
| 58 | * |
| 59 | * o Lines with an 'except' statement on them can't be jumped to, because |
| 60 | * they expect an exception to be on the top of the stack. |
| 61 | * o Lines that live in a 'finally' block can't be jumped from or to, since |
| 62 | * the END_FINALLY expects to clean up the stack after the 'try' block. |
| 63 | * o 'try'/'for'/'while' blocks can't be jumped into because the blockstack |
| 64 | * needs to be set up before their code runs, and for 'for' loops the |
| 65 | * iterator needs to be on the stack. |
| 66 | */ |
| 67 | static int |
| 68 | frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno) |
| 69 | { |
| 70 | int new_lineno = 0; /* The new value of f_lineno */ |
| 71 | int new_lasti = 0; /* The new value of f_lasti */ |
| 72 | int new_iblock = 0; /* The new value of f_iblock */ |
| 73 | char *code = NULL; /* The bytecode for the frame... */ |
| 74 | int code_len = 0; /* ...and its length */ |
| 75 | char *lnotab = NULL; /* Iterating over co_lnotab */ |
| 76 | int lnotab_len = 0; /* (ditto) */ |
| 77 | int offset = 0; /* (ditto) */ |
| 78 | int line = 0; /* (ditto) */ |
| 79 | int addr = 0; /* (ditto) */ |
| 80 | int min_addr = 0; /* Scanning the SETUPs and POPs */ |
| 81 | int max_addr = 0; /* (ditto) */ |
| 82 | int delta_iblock = 0; /* (ditto) */ |
| 83 | int min_delta_iblock = 0; /* (ditto) */ |
| 84 | int min_iblock = 0; /* (ditto) */ |
| 85 | int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */ |
| 86 | int new_lasti_setup_addr = 0; /* (ditto) */ |
| 87 | int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */ |
| 88 | int in_finally[CO_MAXBLOCKS]; /* (ditto) */ |
| 89 | int blockstack_top = 0; /* (ditto) */ |
| 90 | int setup_op = 0; /* (ditto) */ |
| 91 | |
| 92 | /* f_lineno must be an integer. */ |
| 93 | if (!PyInt_Check(p_new_lineno)) { |
| 94 | PyErr_SetString(PyExc_ValueError, |
| 95 | "lineno must be an integer"); |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | /* You can only do this from within a trace function, not via |
| 100 | * _getframe or similar hackery. */ |
| 101 | if (!f->f_trace) |
| 102 | { |
| 103 | PyErr_Format(PyExc_ValueError, |
| 104 | "f_lineno can only be set by a trace function"); |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | /* Fail if the line comes before the start of the code block. */ |
| 109 | new_lineno = (int) PyInt_AsLong(p_new_lineno); |
| 110 | if (new_lineno < f->f_code->co_firstlineno) { |
| 111 | PyErr_Format(PyExc_ValueError, |
| 112 | "line %d comes before the current code block", |
| 113 | new_lineno); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | /* Find the bytecode offset for the start of the given line, or the |
| 118 | * first code-owning line after it. */ |
| 119 | PyString_AsStringAndSize(f->f_code->co_lnotab, &lnotab, &lnotab_len); |
| 120 | addr = 0; |
| 121 | line = f->f_code->co_firstlineno; |
| 122 | new_lasti = -1; |
| 123 | for (offset = 0; offset < lnotab_len; offset += 2) { |
| 124 | addr += lnotab[offset]; |
| 125 | line += lnotab[offset+1]; |
| 126 | if (line >= new_lineno) { |
| 127 | new_lasti = addr; |
| 128 | new_lineno = line; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* If we didn't reach the requested line, return an error. */ |
| 134 | if (new_lasti == -1) { |
| 135 | PyErr_Format(PyExc_ValueError, |
| 136 | "line %d comes after the current code block", |
| 137 | new_lineno); |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | /* We're now ready to look at the bytecode. */ |
| 142 | PyString_AsStringAndSize(f->f_code->co_code, &code, &code_len); |
| 143 | min_addr = MIN(new_lasti, f->f_lasti); |
| 144 | max_addr = MAX(new_lasti, f->f_lasti); |
| 145 | |
| 146 | /* You can't jump onto a line with an 'except' statement on it - |
| 147 | * they expect to have an exception on the top of the stack, which |
| 148 | * won't be true if you jump to them. They always start with code |
| 149 | * that either pops the exception using POP_TOP (plain 'except:' |
| 150 | * lines do this) or duplicates the exception on the stack using |
| 151 | * DUP_TOP (if there's an exception type specified). See compile.c, |
| 152 | * 'com_try_except' for the full details. There aren't any other |
| 153 | * cases (AFAIK) where a line's code can start with DUP_TOP or |
| 154 | * POP_TOP, but if any ever appear, they'll be subject to the same |
| 155 | * restriction (but with a different error message). */ |
| 156 | if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) { |
| 157 | PyErr_SetString(PyExc_ValueError, |
| 158 | "can't jump to 'except' line as there's no exception"); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | /* You can't jump into or out of a 'finally' block because the 'try' |
| 163 | * block leaves something on the stack for the END_FINALLY to clean |
| 164 | * up. So we walk the bytecode, maintaining a simulated blockstack. |
| 165 | * When we reach the old or new address and it's in a 'finally' block |
| 166 | * we note the address of the corresponding SETUP_FINALLY. The jump |
| 167 | * is only legal if neither address is in a 'finally' block or |
| 168 | * they're both in the same one. 'blockstack' is a stack of the |
| 169 | * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks |
| 170 | * whether we're in a 'finally' block at each blockstack level. */ |
| 171 | f_lasti_setup_addr = -1; |
| 172 | new_lasti_setup_addr = -1; |
| 173 | memset(blockstack, '\0', sizeof(blockstack)); |
| 174 | memset(in_finally, '\0', sizeof(in_finally)); |
| 175 | blockstack_top = 0; |
| 176 | for (addr = 0; addr < code_len; addr++) { |
| 177 | unsigned char op = code[addr]; |
| 178 | switch (op) { |
| 179 | case SETUP_LOOP: |
| 180 | case SETUP_EXCEPT: |
| 181 | case SETUP_FINALLY: |
| 182 | blockstack[blockstack_top++] = addr; |
| 183 | in_finally[blockstack_top-1] = 0; |
| 184 | break; |
| 185 | |
| 186 | case POP_BLOCK: |
Neal Norwitz | ee65e22 | 2002-12-19 18:16:57 +0000 | [diff] [blame] | 187 | assert(blockstack_top > 0); |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 188 | setup_op = code[blockstack[blockstack_top-1]]; |
| 189 | if (setup_op == SETUP_FINALLY) { |
| 190 | in_finally[blockstack_top-1] = 1; |
| 191 | } |
| 192 | else { |
| 193 | blockstack_top--; |
| 194 | } |
| 195 | break; |
| 196 | |
| 197 | case END_FINALLY: |
| 198 | /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist |
| 199 | * in the bytecode but don't correspond to an actual |
Neal Norwitz | ee65e22 | 2002-12-19 18:16:57 +0000 | [diff] [blame] | 200 | * 'finally' block. (If blockstack_top is 0, we must |
| 201 | * be seeing such an END_FINALLY.) */ |
| 202 | if (blockstack_top > 0) { |
| 203 | setup_op = code[blockstack[blockstack_top-1]]; |
| 204 | if (setup_op == SETUP_FINALLY) { |
| 205 | blockstack_top--; |
| 206 | } |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 207 | } |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | /* For the addresses we're interested in, see whether they're |
| 212 | * within a 'finally' block and if so, remember the address |
| 213 | * of the SETUP_FINALLY. */ |
| 214 | if (addr == new_lasti || addr == f->f_lasti) { |
| 215 | int i = 0; |
| 216 | int setup_addr = -1; |
| 217 | for (i = blockstack_top-1; i >= 0; i--) { |
| 218 | if (in_finally[i]) { |
| 219 | setup_addr = blockstack[i]; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (setup_addr != -1) { |
| 225 | if (addr == new_lasti) { |
| 226 | new_lasti_setup_addr = setup_addr; |
| 227 | } |
| 228 | |
| 229 | if (addr == f->f_lasti) { |
| 230 | f_lasti_setup_addr = setup_addr; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (op >= HAVE_ARGUMENT) { |
| 236 | addr += 2; |
| 237 | } |
| 238 | } |
| 239 | |
Neal Norwitz | ee65e22 | 2002-12-19 18:16:57 +0000 | [diff] [blame] | 240 | /* Verify that the blockstack tracking code didn't get lost. */ |
| 241 | assert(blockstack_top == 0); |
| 242 | |
| 243 | /* After all that, are we jumping into / out of a 'finally' block? */ |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 244 | if (new_lasti_setup_addr != f_lasti_setup_addr) { |
| 245 | PyErr_SetString(PyExc_ValueError, |
| 246 | "can't jump into or out of a 'finally' block"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /* Police block-jumping (you can't jump into the middle of a block) |
| 252 | * and ensure that the blockstack finishes up in a sensible state (by |
| 253 | * popping any blocks we're jumping out of). We look at all the |
| 254 | * blockstack operations between the current position and the new |
| 255 | * one, and keep track of how many blocks we drop out of on the way. |
| 256 | * By also keeping track of the lowest blockstack position we see, we |
| 257 | * can tell whether the jump goes into any blocks without coming out |
| 258 | * again - in that case we raise an exception below. */ |
| 259 | delta_iblock = 0; |
| 260 | for (addr = min_addr; addr < max_addr; addr++) { |
| 261 | unsigned char op = code[addr]; |
| 262 | switch (op) { |
| 263 | case SETUP_LOOP: |
| 264 | case SETUP_EXCEPT: |
| 265 | case SETUP_FINALLY: |
| 266 | delta_iblock++; |
| 267 | break; |
| 268 | |
| 269 | case POP_BLOCK: |
| 270 | delta_iblock--; |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | min_delta_iblock = MIN(min_delta_iblock, delta_iblock); |
| 275 | |
| 276 | if (op >= HAVE_ARGUMENT) { |
| 277 | addr += 2; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* Derive the absolute iblock values from the deltas. */ |
| 282 | min_iblock = f->f_iblock + min_delta_iblock; |
| 283 | if (new_lasti > f->f_lasti) { |
| 284 | /* Forwards jump. */ |
| 285 | new_iblock = f->f_iblock + delta_iblock; |
| 286 | } |
| 287 | else { |
| 288 | /* Backwards jump. */ |
| 289 | new_iblock = f->f_iblock - delta_iblock; |
| 290 | } |
| 291 | |
| 292 | /* Are we jumping into a block? */ |
| 293 | if (new_iblock > min_iblock) { |
| 294 | PyErr_SetString(PyExc_ValueError, |
| 295 | "can't jump into the middle of a block"); |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | /* Pop any blocks that we're jumping out of. */ |
| 300 | while (f->f_iblock > new_iblock) { |
| 301 | PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; |
| 302 | while ((f->f_stacktop - f->f_valuestack) > b->b_level) { |
| 303 | PyObject *v = (*--f->f_stacktop); |
| 304 | Py_DECREF(v); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* Finally set the new f_lineno and f_lasti and return OK. */ |
| 309 | f->f_lineno = new_lineno; |
| 310 | f->f_lasti = new_lasti; |
| 311 | return 0; |
| 312 | } |
| 313 | |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 314 | static PyObject * |
| 315 | frame_gettrace(PyFrameObject *f, void *closure) |
| 316 | { |
| 317 | PyObject* trace = f->f_trace; |
| 318 | |
| 319 | if (trace == NULL) |
| 320 | trace = Py_None; |
| 321 | |
| 322 | Py_INCREF(trace); |
| 323 | |
| 324 | return trace; |
| 325 | } |
| 326 | |
| 327 | static int |
| 328 | frame_settrace(PyFrameObject *f, PyObject* v, void *closure) |
| 329 | { |
| 330 | /* We rely on f_lineno being accurate when f_trace is set. */ |
| 331 | |
| 332 | PyObject* old_value = f->f_trace; |
| 333 | |
| 334 | Py_XINCREF(v); |
| 335 | f->f_trace = v; |
| 336 | |
| 337 | if (v != NULL) |
| 338 | f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
| 339 | |
| 340 | Py_XDECREF(old_value); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 345 | static PyGetSetDef frame_getsetlist[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 346 | {"f_locals", (getter)frame_getlocals, NULL, NULL}, |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 347 | {"f_lineno", (getter)frame_getlineno, |
| 348 | (setter)frame_setlineno, NULL}, |
Michael W. Hudson | 02ff6a9 | 2002-09-11 15:36:32 +0000 | [diff] [blame] | 349 | {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 350 | {0} |
| 351 | }; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 353 | /* Stack frames are allocated and deallocated at a considerable rate. |
| 354 | In an attempt to improve the speed of function calls, we maintain a |
| 355 | separate free list of stack frames (just like integers are |
| 356 | allocated in a special way -- see intobject.c). When a stack frame |
| 357 | is on the free list, only the following members have a meaning: |
| 358 | ob_type == &Frametype |
| 359 | f_back next item on free list, or NULL |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 360 | f_nlocals number of locals |
| 361 | f_stacksize size of value stack |
Neil Schemenauer | 4f4817f | 2001-08-29 23:52:17 +0000 | [diff] [blame] | 362 | ob_size size of localsplus |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 363 | Note that the value and block stacks are preserved -- this can save |
| 364 | another malloc() call or two (and two free() calls as well!). |
| 365 | Also note that, unlike for integers, each frame object is a |
| 366 | malloc'ed object in its own right -- it is only the actual calls to |
| 367 | malloc() that we are trying to save here, not the administration. |
| 368 | After all, while a typical program may make millions of calls, a |
| 369 | call depth of more than 20 or 30 is probably already exceptional |
| 370 | unless the program contains run-away recursion. I hope. |
Tim Peters | b7ba743 | 2002-04-13 05:21:47 +0000 | [diff] [blame] | 371 | |
| 372 | Later, MAXFREELIST was added to bound the # of frames saved on |
| 373 | free_list. Else programs creating lots of cyclic trash involving |
| 374 | frames could provoke free_list into growing without bound. |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 375 | */ |
| 376 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 377 | static PyFrameObject *free_list = NULL; |
Tim Peters | b7ba743 | 2002-04-13 05:21:47 +0000 | [diff] [blame] | 378 | static int numfree = 0; /* number of frames currently in free_list */ |
| 379 | #define MAXFREELIST 200 /* max value for numfree */ |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 380 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 381 | static void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 382 | frame_dealloc(PyFrameObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 383 | { |
Jeremy Hylton | 30c9f39 | 2001-03-13 01:58:22 +0000 | [diff] [blame] | 384 | int i, slots; |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 385 | PyObject **fastlocals; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 386 | PyObject **p; |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 387 | |
Guido van Rossum | ff413af | 2002-03-28 20:34:59 +0000 | [diff] [blame] | 388 | PyObject_GC_UnTrack(f); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 389 | Py_TRASHCAN_SAFE_BEGIN(f) |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 390 | /* Kill all local variables */ |
Jeremy Hylton | 30c9f39 | 2001-03-13 01:58:22 +0000 | [diff] [blame] | 391 | slots = f->f_nlocals + f->f_ncells + f->f_nfreevars; |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 392 | fastlocals = f->f_localsplus; |
Jeremy Hylton | 30c9f39 | 2001-03-13 01:58:22 +0000 | [diff] [blame] | 393 | for (i = slots; --i >= 0; ++fastlocals) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 394 | Py_XDECREF(*fastlocals); |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 397 | /* Free stack */ |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 398 | if (f->f_stacktop != NULL) { |
| 399 | for (p = f->f_valuestack; p < f->f_stacktop; p++) |
| 400 | Py_XDECREF(*p); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 401 | } |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 402 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 403 | Py_XDECREF(f->f_back); |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 404 | Py_DECREF(f->f_code); |
| 405 | Py_DECREF(f->f_builtins); |
| 406 | Py_DECREF(f->f_globals); |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 407 | Py_XDECREF(f->f_locals); |
| 408 | Py_XDECREF(f->f_trace); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 409 | Py_XDECREF(f->f_exc_type); |
| 410 | Py_XDECREF(f->f_exc_value); |
| 411 | Py_XDECREF(f->f_exc_traceback); |
Tim Peters | b7ba743 | 2002-04-13 05:21:47 +0000 | [diff] [blame] | 412 | if (numfree < MAXFREELIST) { |
| 413 | ++numfree; |
| 414 | f->f_back = free_list; |
| 415 | free_list = f; |
| 416 | } |
| 417 | else |
| 418 | PyObject_GC_Del(f); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 419 | Py_TRASHCAN_SAFE_END(f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 422 | static int |
| 423 | frame_traverse(PyFrameObject *f, visitproc visit, void *arg) |
| 424 | { |
| 425 | PyObject **fastlocals, **p; |
| 426 | int i, err, slots; |
| 427 | #define VISIT(o) if (o) {if ((err = visit((PyObject *)(o), arg))) return err;} |
| 428 | |
| 429 | VISIT(f->f_back); |
| 430 | VISIT(f->f_code); |
| 431 | VISIT(f->f_builtins); |
| 432 | VISIT(f->f_globals); |
| 433 | VISIT(f->f_locals); |
| 434 | VISIT(f->f_trace); |
| 435 | VISIT(f->f_exc_type); |
| 436 | VISIT(f->f_exc_value); |
| 437 | VISIT(f->f_exc_traceback); |
| 438 | |
| 439 | /* locals */ |
| 440 | slots = f->f_nlocals + f->f_ncells + f->f_nfreevars; |
| 441 | fastlocals = f->f_localsplus; |
| 442 | for (i = slots; --i >= 0; ++fastlocals) { |
| 443 | VISIT(*fastlocals); |
| 444 | } |
| 445 | |
| 446 | /* stack */ |
| 447 | if (f->f_stacktop != NULL) { |
| 448 | for (p = f->f_valuestack; p < f->f_stacktop; p++) |
| 449 | VISIT(*p); |
| 450 | } |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | frame_clear(PyFrameObject *f) |
| 456 | { |
| 457 | PyObject **fastlocals, **p; |
| 458 | int i, slots; |
| 459 | |
| 460 | Py_XDECREF(f->f_exc_type); |
| 461 | f->f_exc_type = NULL; |
| 462 | |
| 463 | Py_XDECREF(f->f_exc_value); |
| 464 | f->f_exc_value = NULL; |
| 465 | |
| 466 | Py_XDECREF(f->f_exc_traceback); |
| 467 | f->f_exc_traceback = NULL; |
| 468 | |
| 469 | Py_XDECREF(f->f_trace); |
| 470 | f->f_trace = NULL; |
| 471 | |
| 472 | /* locals */ |
| 473 | slots = f->f_nlocals + f->f_ncells + f->f_nfreevars; |
| 474 | fastlocals = f->f_localsplus; |
| 475 | for (i = slots; --i >= 0; ++fastlocals) { |
| 476 | if (*fastlocals != NULL) { |
| 477 | Py_XDECREF(*fastlocals); |
| 478 | *fastlocals = NULL; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* stack */ |
| 483 | if (f->f_stacktop != NULL) { |
| 484 | for (p = f->f_valuestack; p < f->f_stacktop; p++) { |
| 485 | Py_XDECREF(*p); |
| 486 | *p = NULL; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 492 | PyTypeObject PyFrame_Type = { |
| 493 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 494 | 0, |
| 495 | "frame", |
Neil Schemenauer | 4f4817f | 2001-08-29 23:52:17 +0000 | [diff] [blame] | 496 | sizeof(PyFrameObject), |
| 497 | sizeof(PyObject *), |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 498 | (destructor)frame_dealloc, /* tp_dealloc */ |
| 499 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 500 | 0, /* tp_getattr */ |
| 501 | 0, /* tp_setattr */ |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 502 | 0, /* tp_compare */ |
| 503 | 0, /* tp_repr */ |
| 504 | 0, /* tp_as_number */ |
| 505 | 0, /* tp_as_sequence */ |
| 506 | 0, /* tp_as_mapping */ |
| 507 | 0, /* tp_hash */ |
| 508 | 0, /* tp_call */ |
| 509 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 510 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 511 | PyObject_GenericSetAttr, /* tp_setattro */ |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 512 | 0, /* tp_as_buffer */ |
Neil Schemenauer | 4f4817f | 2001-08-29 23:52:17 +0000 | [diff] [blame] | 513 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Neil Schemenauer | 19cd292 | 2001-07-12 13:27:11 +0000 | [diff] [blame] | 514 | 0, /* tp_doc */ |
| 515 | (traverseproc)frame_traverse, /* tp_traverse */ |
| 516 | (inquiry)frame_clear, /* tp_clear */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 517 | 0, /* tp_richcompare */ |
| 518 | 0, /* tp_weaklistoffset */ |
| 519 | 0, /* tp_iter */ |
| 520 | 0, /* tp_iternext */ |
| 521 | 0, /* tp_methods */ |
| 522 | frame_memberlist, /* tp_members */ |
| 523 | frame_getsetlist, /* tp_getset */ |
| 524 | 0, /* tp_base */ |
| 525 | 0, /* tp_dict */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 526 | }; |
| 527 | |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 528 | static PyObject *builtin_object; |
| 529 | |
Neal Norwitz | b2501f4 | 2002-12-31 03:42:13 +0000 | [diff] [blame] | 530 | int _PyFrame_Init() |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 531 | { |
| 532 | builtin_object = PyString_InternFromString("__builtins__"); |
| 533 | return (builtin_object != NULL); |
| 534 | } |
| 535 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 536 | PyFrameObject * |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 537 | PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals, |
Jeremy Hylton | 30c9f39 | 2001-03-13 01:58:22 +0000 | [diff] [blame] | 538 | PyObject *locals) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 539 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 540 | PyFrameObject *back = tstate->frame; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 541 | PyFrameObject *f; |
| 542 | PyObject *builtins; |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 543 | int extras, ncells, nfrees; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 544 | |
Michael W. Hudson | 69734a5 | 2002-08-19 16:54:08 +0000 | [diff] [blame] | 545 | #ifdef Py_DEBUG |
| 546 | if (code == NULL || globals == NULL || !PyDict_Check(globals) || |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 547 | (locals != NULL && !PyDict_Check(locals))) { |
| 548 | PyErr_BadInternalCall(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 549 | return NULL; |
| 550 | } |
Michael W. Hudson | 69734a5 | 2002-08-19 16:54:08 +0000 | [diff] [blame] | 551 | #endif |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 552 | ncells = PyTuple_GET_SIZE(code->co_cellvars); |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 553 | nfrees = PyTuple_GET_SIZE(code->co_freevars); |
| 554 | extras = code->co_stacksize + code->co_nlocals + ncells + nfrees; |
Guido van Rossum | bde6ff7 | 1998-02-19 20:48:26 +0000 | [diff] [blame] | 555 | if (back == NULL || back->f_globals != globals) { |
| 556 | builtins = PyDict_GetItem(globals, builtin_object); |
Jeremy Hylton | bd5cbf8 | 2003-02-05 22:39:29 +0000 | [diff] [blame] | 557 | if (builtins) { |
| 558 | if (PyModule_Check(builtins)) { |
| 559 | builtins = PyModule_GetDict(builtins); |
| 560 | assert(!builtins || PyDict_Check(builtins)); |
| 561 | } |
| 562 | else if (!PyDict_Check(builtins)) |
| 563 | builtins = NULL; |
| 564 | } |
| 565 | if (builtins == NULL) { |
| 566 | /* No builtins! Make up a minimal one |
| 567 | Give them 'None', at least. */ |
| 568 | builtins = PyDict_New(); |
| 569 | if (builtins == NULL || |
| 570 | PyDict_SetItemString( |
| 571 | builtins, "None", Py_None) < 0) |
| 572 | return NULL; |
| 573 | } |
| 574 | else |
| 575 | Py_INCREF(builtins); |
| 576 | |
Guido van Rossum | bde6ff7 | 1998-02-19 20:48:26 +0000 | [diff] [blame] | 577 | } |
| 578 | else { |
| 579 | /* If we share the globals, we share the builtins. |
| 580 | Save a lookup and a call. */ |
| 581 | builtins = back->f_builtins; |
Jeremy Hylton | bd5cbf8 | 2003-02-05 22:39:29 +0000 | [diff] [blame] | 582 | assert(builtins != NULL && PyDict_Check(builtins)); |
| 583 | Py_INCREF(builtins); |
Guido van Rossum | bde6ff7 | 1998-02-19 20:48:26 +0000 | [diff] [blame] | 584 | } |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 585 | if (free_list == NULL) { |
Neil Schemenauer | 4f4817f | 2001-08-29 23:52:17 +0000 | [diff] [blame] | 586 | f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, extras); |
Armin Rigo | 76beca9 | 2004-01-27 16:08:07 +0000 | [diff] [blame] | 587 | if (f == NULL) { |
| 588 | Py_DECREF(builtins); |
Neil Schemenauer | 4f4817f | 2001-08-29 23:52:17 +0000 | [diff] [blame] | 589 | return NULL; |
Armin Rigo | 76beca9 | 2004-01-27 16:08:07 +0000 | [diff] [blame] | 590 | } |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 591 | } |
| 592 | else { |
Tim Peters | b7ba743 | 2002-04-13 05:21:47 +0000 | [diff] [blame] | 593 | assert(numfree > 0); |
| 594 | --numfree; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 595 | f = free_list; |
| 596 | free_list = free_list->f_back; |
Neil Schemenauer | 4f4817f | 2001-08-29 23:52:17 +0000 | [diff] [blame] | 597 | if (f->ob_size < extras) { |
| 598 | f = PyObject_GC_Resize(PyFrameObject, f, extras); |
Armin Rigo | 76beca9 | 2004-01-27 16:08:07 +0000 | [diff] [blame] | 599 | if (f == NULL) { |
| 600 | Py_DECREF(builtins); |
Neil Schemenauer | 4f4817f | 2001-08-29 23:52:17 +0000 | [diff] [blame] | 601 | return NULL; |
Armin Rigo | 76beca9 | 2004-01-27 16:08:07 +0000 | [diff] [blame] | 602 | } |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 603 | } |
Tim Peters | deb77e8 | 2001-08-30 00:32:51 +0000 | [diff] [blame] | 604 | _Py_NewReference((PyObject *)f); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 605 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 606 | f->f_builtins = builtins; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 607 | Py_XINCREF(back); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 608 | f->f_back = back; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 609 | Py_INCREF(code); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 610 | f->f_code = code; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 611 | Py_INCREF(globals); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 612 | f->f_globals = globals; |
Jeremy Hylton | bd5cbf8 | 2003-02-05 22:39:29 +0000 | [diff] [blame] | 613 | /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */ |
| 614 | if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) == |
| 615 | (CO_NEWLOCALS | CO_OPTIMIZED)) |
Michael W. Hudson | bdc6ea1 | 2003-08-11 16:14:06 +0000 | [diff] [blame] | 616 | locals = NULL; /* PyFrame_FastToLocals() will set. */ |
Jeremy Hylton | bd5cbf8 | 2003-02-05 22:39:29 +0000 | [diff] [blame] | 617 | else if (code->co_flags & CO_NEWLOCALS) { |
| 618 | locals = PyDict_New(); |
| 619 | if (locals == NULL) { |
| 620 | Py_DECREF(f); |
| 621 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 622 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 623 | } |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 624 | else { |
| 625 | if (locals == NULL) |
| 626 | locals = globals; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 627 | Py_INCREF(locals); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 628 | } |
| 629 | f->f_locals = locals; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 630 | f->f_trace = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 631 | f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL; |
Guido van Rossum | eb46d67 | 1997-08-02 02:59:08 +0000 | [diff] [blame] | 632 | f->f_tstate = tstate; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 633 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 634 | f->f_lasti = -1; |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 635 | f->f_lineno = code->co_firstlineno; |
Guido van Rossum | eb46d67 | 1997-08-02 02:59:08 +0000 | [diff] [blame] | 636 | f->f_restricted = (builtins != tstate->interp->builtins); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 637 | f->f_iblock = 0; |
| 638 | f->f_nlocals = code->co_nlocals; |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 639 | f->f_stacksize = code->co_stacksize; |
| 640 | f->f_ncells = ncells; |
| 641 | f->f_nfreevars = nfrees; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 642 | |
Guido van Rossum | f4be427 | 2002-08-01 18:50:33 +0000 | [diff] [blame] | 643 | extras = f->f_nlocals + ncells + nfrees; |
| 644 | memset(f->f_localsplus, 0, extras * sizeof(f->f_localsplus[0])); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 645 | |
Guido van Rossum | f4be427 | 2002-08-01 18:50:33 +0000 | [diff] [blame] | 646 | f->f_valuestack = f->f_localsplus + extras; |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 647 | f->f_stacktop = f->f_valuestack; |
Neil Schemenauer | 4f4817f | 2001-08-29 23:52:17 +0000 | [diff] [blame] | 648 | _PyObject_GC_TRACK(f); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 649 | return f; |
| 650 | } |
| 651 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 652 | /* Block management */ |
| 653 | |
| 654 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 655 | PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 656 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 657 | PyTryBlock *b; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 658 | if (f->f_iblock >= CO_MAXBLOCKS) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 659 | Py_FatalError("XXX block stack overflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 660 | b = &f->f_blockstack[f->f_iblock++]; |
| 661 | b->b_type = type; |
| 662 | b->b_level = level; |
| 663 | b->b_handler = handler; |
| 664 | } |
| 665 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 666 | PyTryBlock * |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 667 | PyFrame_BlockPop(PyFrameObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 668 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 669 | PyTryBlock *b; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 670 | if (f->f_iblock <= 0) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 671 | Py_FatalError("XXX block stack underflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 672 | b = &f->f_blockstack[--f->f_iblock]; |
| 673 | return b; |
| 674 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 675 | |
| 676 | /* Convert between "fast" version of locals and dictionary version */ |
| 677 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 678 | static void |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 679 | map_to_dict(PyObject *map, int nmap, PyObject *dict, PyObject **values, |
| 680 | int deref) |
| 681 | { |
| 682 | int j; |
| 683 | for (j = nmap; --j >= 0; ) { |
Jeremy Hylton | 1a48ca8 | 2001-12-06 15:48:16 +0000 | [diff] [blame] | 684 | PyObject *key = PyTuple_GET_ITEM(map, j); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 685 | PyObject *value = values[j]; |
| 686 | if (deref) |
| 687 | value = PyCell_GET(value); |
| 688 | if (value == NULL) { |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 689 | if (PyDict_DelItem(dict, key) != 0) |
| 690 | PyErr_Clear(); |
| 691 | } |
| 692 | else { |
| 693 | if (PyDict_SetItem(dict, key, value) != 0) |
| 694 | PyErr_Clear(); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
Guido van Rossum | 6b356e7 | 2001-04-14 17:55:41 +0000 | [diff] [blame] | 699 | static void |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 700 | dict_to_map(PyObject *map, int nmap, PyObject *dict, PyObject **values, |
| 701 | int deref, int clear) |
| 702 | { |
| 703 | int j; |
| 704 | for (j = nmap; --j >= 0; ) { |
Jeremy Hylton | 1a48ca8 | 2001-12-06 15:48:16 +0000 | [diff] [blame] | 705 | PyObject *key = PyTuple_GET_ITEM(map, j); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 706 | PyObject *value = PyDict_GetItem(dict, key); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 707 | if (deref) { |
Jeremy Hylton | 4c88901 | 2001-05-08 04:08:59 +0000 | [diff] [blame] | 708 | if (value || clear) { |
Jeremy Hylton | 1a48ca8 | 2001-12-06 15:48:16 +0000 | [diff] [blame] | 709 | if (PyCell_GET(values[j]) != value) { |
| 710 | if (PyCell_Set(values[j], value) < 0) |
| 711 | PyErr_Clear(); |
| 712 | } |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 713 | } |
| 714 | } else if (value != NULL || clear) { |
Jeremy Hylton | 1a48ca8 | 2001-12-06 15:48:16 +0000 | [diff] [blame] | 715 | if (values[j] != value) { |
| 716 | Py_XINCREF(value); |
| 717 | Py_XDECREF(values[j]); |
| 718 | values[j] = value; |
| 719 | } |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | } |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 723 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 724 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 725 | PyFrame_FastToLocals(PyFrameObject *f) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 726 | { |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 727 | /* Merge fast locals into f->f_locals */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 728 | PyObject *locals, *map; |
| 729 | PyObject **fast; |
| 730 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 731 | int j; |
| 732 | if (f == NULL) |
| 733 | return; |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 734 | locals = f->f_locals; |
| 735 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 736 | locals = f->f_locals = PyDict_New(); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 737 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 738 | PyErr_Clear(); /* Can't report it :-( */ |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 739 | return; |
| 740 | } |
| 741 | } |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 742 | map = f->f_code->co_varnames; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 743 | if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 744 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 745 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 746 | fast = f->f_localsplus; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 747 | j = PyTuple_Size(map); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 748 | if (j > f->f_nlocals) |
| 749 | j = f->f_nlocals; |
Jeremy Hylton | 24ea8d3 | 2002-04-20 04:46:55 +0000 | [diff] [blame] | 750 | if (f->f_nlocals) |
Jeremy Hylton | 174d276 | 2003-10-21 18:10:28 +0000 | [diff] [blame] | 751 | map_to_dict(map, j, locals, fast, 0); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 752 | if (f->f_ncells || f->f_nfreevars) { |
| 753 | if (!(PyTuple_Check(f->f_code->co_cellvars) |
| 754 | && PyTuple_Check(f->f_code->co_freevars))) { |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 755 | return; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 756 | } |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 757 | map_to_dict(f->f_code->co_cellvars, |
| 758 | PyTuple_GET_SIZE(f->f_code->co_cellvars), |
| 759 | locals, fast + f->f_nlocals, 1); |
| 760 | map_to_dict(f->f_code->co_freevars, |
| 761 | PyTuple_GET_SIZE(f->f_code->co_freevars), |
| 762 | locals, fast + f->f_nlocals + f->f_ncells, 1); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 763 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 764 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 768 | PyFrame_LocalsToFast(PyFrameObject *f, int clear) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 769 | { |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 770 | /* Merge f->f_locals into fast locals */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 771 | PyObject *locals, *map; |
| 772 | PyObject **fast; |
| 773 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 774 | int j; |
| 775 | if (f == NULL) |
| 776 | return; |
| 777 | locals = f->f_locals; |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 778 | map = f->f_code->co_varnames; |
Jeremy Hylton | 24ea8d3 | 2002-04-20 04:46:55 +0000 | [diff] [blame] | 779 | if (locals == NULL) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 780 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 781 | if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 782 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 783 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 784 | fast = f->f_localsplus; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 785 | j = PyTuple_Size(map); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 786 | if (j > f->f_nlocals) |
| 787 | j = f->f_nlocals; |
Jeremy Hylton | 24ea8d3 | 2002-04-20 04:46:55 +0000 | [diff] [blame] | 788 | if (f->f_nlocals) |
| 789 | dict_to_map(f->f_code->co_varnames, j, locals, fast, 0, clear); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 790 | if (f->f_ncells || f->f_nfreevars) { |
| 791 | if (!(PyTuple_Check(f->f_code->co_cellvars) |
| 792 | && PyTuple_Check(f->f_code->co_freevars))) |
| 793 | return; |
| 794 | dict_to_map(f->f_code->co_cellvars, |
| 795 | PyTuple_GET_SIZE(f->f_code->co_cellvars), |
Jeremy Hylton | 4c88901 | 2001-05-08 04:08:59 +0000 | [diff] [blame] | 796 | locals, fast + f->f_nlocals, 1, clear); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 797 | dict_to_map(f->f_code->co_freevars, |
| 798 | PyTuple_GET_SIZE(f->f_code->co_freevars), |
Jeremy Hylton | 24ea8d3 | 2002-04-20 04:46:55 +0000 | [diff] [blame] | 799 | locals, fast + f->f_nlocals + f->f_ncells, 1, |
| 800 | clear); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 801 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 802 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 803 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 804 | |
| 805 | /* Clear out the free list */ |
| 806 | |
| 807 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 808 | PyFrame_Fini(void) |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 809 | { |
| 810 | while (free_list != NULL) { |
| 811 | PyFrameObject *f = free_list; |
| 812 | free_list = free_list->f_back; |
Neil Schemenauer | 4f4817f | 2001-08-29 23:52:17 +0000 | [diff] [blame] | 813 | PyObject_GC_Del(f); |
Tim Peters | b7ba743 | 2002-04-13 05:21:47 +0000 | [diff] [blame] | 814 | --numfree; |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 815 | } |
Tim Peters | b7ba743 | 2002-04-13 05:21:47 +0000 | [diff] [blame] | 816 | assert(numfree == 0); |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 817 | Py_XDECREF(builtin_object); |
| 818 | builtin_object = NULL; |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 819 | } |