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