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