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