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