blob: 27ef9ff259024832322166819d7ab55448663c83 [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001/* Frame object implementation */
2
Guido van Rossum18752471997-04-29 14:49:28 +00003#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01004#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pystate.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008#include "frameobject.h"
9#include "opcode.h"
10#include "structmember.h"
11
Guido van Rossum18752471997-04-29 14:49:28 +000012#define OFF(x) offsetof(PyFrameObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000013
Guido van Rossum6f799372001-09-20 20:46:19 +000014static PyMemberDef frame_memberlist[] = {
Nick Coghlan1f7ce622012-01-13 21:43:40 +100015 {"f_back", T_OBJECT, OFF(f_back), READONLY},
16 {"f_code", T_OBJECT, OFF(f_code), READONLY},
17 {"f_builtins", T_OBJECT, OFF(f_builtins), READONLY},
18 {"f_globals", T_OBJECT, OFF(f_globals), READONLY},
19 {"f_lasti", T_INT, OFF(f_lasti), READONLY},
Nick Coghlan5a851672017-09-08 10:14:16 +100020 {"f_trace_lines", T_BOOL, OFF(f_trace_lines), 0},
21 {"f_trace_opcodes", T_BOOL, OFF(f_trace_opcodes), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +000023};
24
Guido van Rossum18752471997-04-29 14:49:28 +000025static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000026frame_getlocals(PyFrameObject *f, void *closure)
Guido van Rossum3f5da241990-12-20 15:06:42 +000027{
Victor Stinner41bb43a2013-10-29 01:19:37 +010028 if (PyFrame_FastToLocalsWithError(f) < 0)
29 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 Py_INCREF(f->f_locals);
31 return f->f_locals;
Guido van Rossum3f5da241990-12-20 15:06:42 +000032}
33
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000034int
35PyFrame_GetLineNumber(PyFrameObject *f)
36{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 if (f->f_trace)
38 return f->f_lineno;
39 else
40 return PyCode_Addr2Line(f->f_code, f->f_lasti);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000041}
42
Michael W. Hudsondd32a912002-08-15 14:59:02 +000043static PyObject *
44frame_getlineno(PyFrameObject *f, void *closure)
45{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 return PyLong_FromLong(PyFrame_GetLineNumber(f));
Michael W. Hudsondd32a912002-08-15 14:59:02 +000047}
48
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +020049
50/* Given the index of the effective opcode,
51 scan back to construct the oparg with EXTENDED_ARG */
52static unsigned int
53get_arg(const _Py_CODEUNIT *codestr, Py_ssize_t i)
54{
55 _Py_CODEUNIT word;
56 unsigned int oparg = _Py_OPARG(codestr[i]);
57 if (i >= 1 && _Py_OPCODE(word = codestr[i-1]) == EXTENDED_ARG) {
58 oparg |= _Py_OPARG(word) << 8;
59 if (i >= 2 && _Py_OPCODE(word = codestr[i-2]) == EXTENDED_ARG) {
60 oparg |= _Py_OPARG(word) << 16;
61 if (i >= 3 && _Py_OPCODE(word = codestr[i-3]) == EXTENDED_ARG) {
62 oparg |= _Py_OPARG(word) << 24;
63 }
64 }
65 }
66 return oparg;
67}
68
69
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000070/* Setter for f_lineno - you can set f_lineno from within a trace function in
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 * order to jump to a given line of code, subject to some restrictions. Most
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000072 * lines are OK to jump to because they don't make any assumptions about the
73 * state of the stack (obvious because you could remove the line and the code
74 * would still work without any stack errors), but there are some constructs
75 * that limit jumping:
76 *
77 * o Lines with an 'except' statement on them can't be jumped to, because
78 * they expect an exception to be on the top of the stack.
79 * o Lines that live in a 'finally' block can't be jumped from or to, since
80 * the END_FINALLY expects to clean up the stack after the 'try' block.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +020081 * o 'try', 'with' and 'async with' blocks can't be jumped into because
82 * the blockstack needs to be set up before their code runs.
83 * o 'for' and 'async for' loops can't be jumped into because the
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000084 * iterator needs to be on the stack.
xdegayeb8e9d6c2018-03-13 18:31:31 +010085 * o Jumps cannot be made from within a trace function invoked with a
86 * 'return' or 'exception' event since the eval loop has been exited at
87 * that time.
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000088 */
89static int
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +020090frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignored))
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 int new_lineno = 0; /* The new value of f_lineno */
93 long l_new_lineno;
94 int overflow;
95 int new_lasti = 0; /* The new value of f_lasti */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 unsigned char *code = NULL; /* The bytecode for the frame... */
97 Py_ssize_t code_len = 0; /* ...and its length */
98 unsigned char *lnotab = NULL; /* Iterating over co_lnotab */
99 Py_ssize_t lnotab_len = 0; /* (ditto) */
100 int offset = 0; /* (ditto) */
101 int line = 0; /* (ditto) */
102 int addr = 0; /* (ditto) */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200103 int delta_iblock = 0; /* Scanning the SETUPs and POPs */
Serhiy Storchaka702f8f32018-03-23 14:34:35 +0200104 int delta = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 int blockstack_top = 0; /* (ditto) */
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000107
Zackery Spytz842acaa2018-12-17 07:52:45 -0700108 if (p_new_lineno == NULL) {
109 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
110 return -1;
111 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 /* f_lineno must be an integer. */
113 if (!PyLong_CheckExact(p_new_lineno)) {
114 PyErr_SetString(PyExc_ValueError,
115 "lineno must be an integer");
116 return -1;
117 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000118
xdegayeb8e9d6c2018-03-13 18:31:31 +0100119 /* Upon the 'call' trace event of a new frame, f->f_lasti is -1 and
120 * f->f_trace is NULL, check first on the first condition.
121 * Forbidding jumps from the 'call' event of a new frame is a side effect
122 * of allowing to set f_lineno only from trace functions. */
123 if (f->f_lasti == -1) {
124 PyErr_Format(PyExc_ValueError,
125 "can't jump from the 'call' trace event of a new frame");
126 return -1;
127 }
128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 /* You can only do this from within a trace function, not via
130 * _getframe or similar hackery. */
xdegayeb8e9d6c2018-03-13 18:31:31 +0100131 if (!f->f_trace) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 PyErr_Format(PyExc_ValueError,
xdegayeb8e9d6c2018-03-13 18:31:31 +0100133 "f_lineno can only be set by a trace function");
134 return -1;
135 }
136
137 /* Forbid jumps upon a 'return' trace event (except after executing a
138 * YIELD_VALUE or YIELD_FROM opcode, f_stacktop is not NULL in that case)
139 * and upon an 'exception' trace event.
140 * Jumps from 'call' trace events have already been forbidden above for new
141 * frames, so this check does not change anything for 'call' events. */
142 if (f->f_stacktop == NULL) {
143 PyErr_SetString(PyExc_ValueError,
144 "can only jump from a 'line' trace event");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return -1;
146 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 /* Fail if the line comes before the start of the code block. */
149 l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
150 if (overflow
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000151#if SIZEOF_LONG > SIZEOF_INT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 || l_new_lineno > INT_MAX
153 || l_new_lineno < INT_MIN
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000154#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 ) {
156 PyErr_SetString(PyExc_ValueError,
157 "lineno out of range");
158 return -1;
159 }
160 new_lineno = (int)l_new_lineno;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 if (new_lineno < f->f_code->co_firstlineno) {
163 PyErr_Format(PyExc_ValueError,
164 "line %d comes before the current code block",
165 new_lineno);
166 return -1;
167 }
168 else if (new_lineno == f->f_code->co_firstlineno) {
169 new_lasti = 0;
170 new_lineno = f->f_code->co_firstlineno;
171 }
172 else {
173 /* Find the bytecode offset for the start of the given
174 * line, or the first code-owning line after it. */
175 char *tmp;
176 PyBytes_AsStringAndSize(f->f_code->co_lnotab,
177 &tmp, &lnotab_len);
178 lnotab = (unsigned char *) tmp;
179 addr = 0;
180 line = f->f_code->co_firstlineno;
181 new_lasti = -1;
182 for (offset = 0; offset < lnotab_len; offset += 2) {
183 addr += lnotab[offset];
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100184 line += (signed char)lnotab[offset+1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 if (line >= new_lineno) {
186 new_lasti = addr;
187 new_lineno = line;
188 break;
189 }
190 }
191 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 /* If we didn't reach the requested line, return an error. */
194 if (new_lasti == -1) {
195 PyErr_Format(PyExc_ValueError,
196 "line %d comes after the current code block",
197 new_lineno);
198 return -1;
199 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 /* We're now ready to look at the bytecode. */
202 PyBytes_AsStringAndSize(f->f_code->co_code, (char **)&code, &code_len);
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000203
xdegayeb8e9d6c2018-03-13 18:31:31 +0100204 /* The trace function is called with a 'return' trace event after the
205 * execution of a yield statement. */
206 assert(f->f_lasti != -1);
207 if (code[f->f_lasti] == YIELD_VALUE || code[f->f_lasti] == YIELD_FROM) {
208 PyErr_SetString(PyExc_ValueError,
209 "can't jump from a yield statement");
210 return -1;
211 }
212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 /* You can't jump onto a line with an 'except' statement on it -
214 * they expect to have an exception on the top of the stack, which
215 * won't be true if you jump to them. They always start with code
216 * that either pops the exception using POP_TOP (plain 'except:'
217 * lines do this) or duplicates the exception on the stack using
218 * DUP_TOP (if there's an exception type specified). See compile.c,
219 * 'com_try_except' for the full details. There aren't any other
220 * cases (AFAIK) where a line's code can start with DUP_TOP or
221 * POP_TOP, but if any ever appear, they'll be subject to the same
222 * restriction (but with a different error message). */
223 if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
224 PyErr_SetString(PyExc_ValueError,
225 "can't jump to 'except' line as there's no exception");
226 return -1;
227 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 /* You can't jump into or out of a 'finally' block because the 'try'
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200230 * block leaves something on the stack for the END_FINALLY to clean up.
231 * So we walk the bytecode, maintaining a simulated blockstack.
232 * 'blockstack' is a stack of the bytecode addresses of the starts of
233 * the 'finally' blocks. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 memset(blockstack, '\0', sizeof(blockstack));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 blockstack_top = 0;
Serhiy Storchakaef61c522019-08-24 13:11:52 +0300236 unsigned char prevop = NOP;
Serhiy Storchakaab874002016-09-11 13:48:15 +0300237 for (addr = 0; addr < code_len; addr += sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 unsigned char op = code[addr];
239 switch (op) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 case SETUP_FINALLY:
Benjamin Petersone42fb302012-04-18 11:14:31 -0400241 case SETUP_WITH:
Yury Selivanov75445082015-05-11 22:57:16 -0400242 case SETUP_ASYNC_WITH:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200243 case FOR_ITER: {
244 unsigned int oparg = get_arg((const _Py_CODEUNIT *)code,
245 addr / sizeof(_Py_CODEUNIT));
246 int target_addr = addr + oparg + sizeof(_Py_CODEUNIT);
247 assert(target_addr < code_len);
248 /* Police block-jumping (you can't jump into the middle of a block)
249 * and ensure that the blockstack finishes up in a sensible state (by
250 * popping any blocks we're jumping out of). We look at all the
251 * blockstack operations between the current position and the new
252 * one, and keep track of how many blocks we drop out of on the way.
253 * By also keeping track of the lowest blockstack position we see, we
254 * can tell whether the jump goes into any blocks without coming out
255 * again - in that case we raise an exception below. */
256 int first_in = addr < f->f_lasti && f->f_lasti < target_addr;
257 int second_in = addr < new_lasti && new_lasti < target_addr;
258 if (!first_in && second_in) {
259 PyErr_SetString(PyExc_ValueError,
260 "can't jump into the middle of a block");
261 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +0300263 int in_for_loop = op == FOR_ITER || code[target_addr] == END_ASYNC_FOR;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200264 if (first_in && !second_in) {
Serhiy Storchakaef61c522019-08-24 13:11:52 +0300265 if (!delta_iblock) {
266 if (in_for_loop) {
267 /* Pop the iterators of any 'for' and 'async for' loop
268 * we're jumping out of. */
269 delta++;
270 }
271 else if (prevop == LOAD_CONST) {
272 /* Pops None pushed before SETUP_FINALLY. */
273 delta++;
274 }
275 }
276 if (!in_for_loop) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200277 delta_iblock++;
278 }
279 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +0300280 if (!in_for_loop) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200281 blockstack[blockstack_top++] = target_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 }
283 break;
284 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000285
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200286 case END_FINALLY: {
287 assert(blockstack_top > 0);
288 int target_addr = blockstack[--blockstack_top];
289 assert(target_addr <= addr);
290 int first_in = target_addr <= f->f_lasti && f->f_lasti <= addr;
291 int second_in = target_addr <= new_lasti && new_lasti <= addr;
292 if (first_in != second_in) {
Serhiy Storchaka397466d2018-03-23 14:46:45 +0200293 op = code[target_addr];
294 PyErr_Format(PyExc_ValueError,
295 "can't jump %s %s block",
296 second_in ? "into" : "out of",
297 (op == DUP_TOP || op == POP_TOP) ?
298 "an 'except'" : "a 'finally'");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200299 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200301 break;
302 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +0300304 prevop = op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 }
Neal Norwitzee65e222002-12-19 18:16:57 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 /* Verify that the blockstack tracking code didn't get lost. */
308 assert(blockstack_top == 0);
309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Pop any blocks that we're jumping out of. */
Serhiy Storchaka26c9f562018-03-11 08:32:47 +0200311 if (delta_iblock > 0) {
312 f->f_iblock -= delta_iblock;
313 PyTryBlock *b = &f->f_blockstack[f->f_iblock];
Victor Stinner078c4e32018-04-27 14:30:01 +0200314 delta += (int)(f->f_stacktop - f->f_valuestack) - b->b_level;
Serhiy Storchaka26c9f562018-03-11 08:32:47 +0200315 if (b->b_type == SETUP_FINALLY &&
316 code[b->b_handler] == WITH_CLEANUP_START)
317 {
318 /* Pop the exit function. */
319 delta++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 }
321 }
Serhiy Storchaka26c9f562018-03-11 08:32:47 +0200322 while (delta > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200323 PyObject *v = (*--f->f_stacktop);
324 Py_DECREF(v);
Serhiy Storchaka26c9f562018-03-11 08:32:47 +0200325 delta--;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200326 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 /* Finally set the new f_lineno and f_lasti and return OK. */
329 f->f_lineno = new_lineno;
330 f->f_lasti = new_lasti;
331 return 0;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000332}
333
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000334static PyObject *
335frame_gettrace(PyFrameObject *f, void *closure)
336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyObject* trace = f->f_trace;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (trace == NULL)
340 trace = Py_None;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 Py_INCREF(trace);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 return trace;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000345}
346
347static int
348frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 /* We rely on f_lineno being accurate when f_trace is set. */
351 f->f_lineno = PyFrame_GetLineNumber(f);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000352
Serhiy Storchaka64a263a2016-06-04 20:32:36 +0300353 if (v == Py_None)
354 v = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 Py_XINCREF(v);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300356 Py_XSETREF(f->f_trace, v);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 return 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000359}
360
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000361
Guido van Rossum32d34c82001-09-20 21:45:26 +0000362static PyGetSetDef frame_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 {"f_locals", (getter)frame_getlocals, NULL, NULL},
364 {"f_lineno", (getter)frame_getlineno,
365 (setter)frame_setlineno, NULL},
366 {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
367 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000368};
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000369
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000370/* Stack frames are allocated and deallocated at a considerable rate.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000371 In an attempt to improve the speed of function calls, we:
372
373 1. Hold a single "zombie" frame on each code object. This retains
374 the allocated and initialised frame object from an invocation of
375 the code object. The zombie is reanimated the next time we need a
376 frame object for that code object. Doing this saves the malloc/
377 realloc required when using a free_list frame that isn't the
378 correct size. It also saves some field initialisation.
379
380 In zombie mode, no field of PyFrameObject holds a reference, but
381 the following fields are still valid:
382
383 * ob_type, ob_size, f_code, f_valuestack;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384
Mark Shannonae3087c2017-10-22 22:41:51 +0100385 * f_locals, f_trace are NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386
387 * f_localsplus does not require re-allocation and
388 the local variables in f_localsplus are NULL.
389
390 2. We also maintain a separate free list of stack frames (just like
Mark Dickinsond19052c2010-06-27 18:19:09 +0000391 floats are allocated in a special way -- see floatobject.c). When
Thomas Wouters477c8d52006-05-27 19:21:47 +0000392 a stack frame is on the free list, only the following members have
393 a meaning:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 ob_type == &Frametype
395 f_back next item on free list, or NULL
396 f_stacksize size of value stack
397 ob_size size of localsplus
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000398 Note that the value and block stacks are preserved -- this can save
399 another malloc() call or two (and two free() calls as well!).
400 Also note that, unlike for integers, each frame object is a
401 malloc'ed object in its own right -- it is only the actual calls to
402 malloc() that we are trying to save here, not the administration.
403 After all, while a typical program may make millions of calls, a
404 call depth of more than 20 or 30 is probably already exceptional
405 unless the program contains run-away recursion. I hope.
Tim Petersb7ba7432002-04-13 05:21:47 +0000406
Christian Heimes2202f872008-02-06 14:31:34 +0000407 Later, PyFrame_MAXFREELIST was added to bound the # of frames saved on
Tim Petersb7ba7432002-04-13 05:21:47 +0000408 free_list. Else programs creating lots of cyclic trash involving
409 frames could provoke free_list into growing without bound.
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000410*/
411
Guido van Rossum18752471997-04-29 14:49:28 +0000412static PyFrameObject *free_list = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413static int numfree = 0; /* number of frames currently in free_list */
Christian Heimes2202f872008-02-06 14:31:34 +0000414/* max value for numfree */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415#define PyFrame_MAXFREELIST 200
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000416
Victor Stinnerc6944e72016-11-11 02:13:35 +0100417static void _Py_HOT_FUNCTION
Fred Drake1b190b42000-07-09 05:40:56 +0000418frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000419{
Antoine Pitrou93963562013-05-14 20:37:52 +0200420 PyObject **p, **valuestack;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 PyCodeObject *co;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000422
INADA Naoki5a625d02016-12-24 20:19:08 +0900423 if (_PyObject_GC_IS_TRACKED(f))
424 _PyObject_GC_UNTRACK(f);
425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 Py_TRASHCAN_SAFE_BEGIN(f)
Antoine Pitrou93963562013-05-14 20:37:52 +0200427 /* Kill all local variables */
428 valuestack = f->f_valuestack;
429 for (p = f->f_localsplus; p < valuestack; p++)
430 Py_CLEAR(*p);
431
432 /* Free stack */
433 if (f->f_stacktop != NULL) {
434 for (p = valuestack; p < f->f_stacktop; p++)
435 Py_XDECREF(*p);
436 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 Py_XDECREF(f->f_back);
439 Py_DECREF(f->f_builtins);
440 Py_DECREF(f->f_globals);
441 Py_CLEAR(f->f_locals);
Antoine Pitrou93963562013-05-14 20:37:52 +0200442 Py_CLEAR(f->f_trace);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 co = f->f_code;
445 if (co->co_zombieframe == NULL)
446 co->co_zombieframe = f;
447 else if (numfree < PyFrame_MAXFREELIST) {
448 ++numfree;
449 f->f_back = free_list;
450 free_list = f;
451 }
452 else
453 PyObject_GC_Del(f);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 Py_DECREF(co);
456 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000457}
458
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000459static int
460frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 PyObject **fastlocals, **p;
Victor Stinner7a6d7cf2012-10-31 00:37:41 +0100463 Py_ssize_t i, slots;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 Py_VISIT(f->f_back);
466 Py_VISIT(f->f_code);
467 Py_VISIT(f->f_builtins);
468 Py_VISIT(f->f_globals);
469 Py_VISIT(f->f_locals);
470 Py_VISIT(f->f_trace);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 /* locals */
473 slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
474 fastlocals = f->f_localsplus;
475 for (i = slots; --i >= 0; ++fastlocals)
476 Py_VISIT(*fastlocals);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 /* stack */
479 if (f->f_stacktop != NULL) {
480 for (p = f->f_valuestack; p < f->f_stacktop; p++)
481 Py_VISIT(*p);
482 }
483 return 0;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000484}
485
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200486static int
Antoine Pitrou58720d62013-08-05 23:26:40 +0200487frame_tp_clear(PyFrameObject *f)
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyObject **fastlocals, **p, **oldtop;
Victor Stinner7a6d7cf2012-10-31 00:37:41 +0100490 Py_ssize_t i, slots;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000491
Antoine Pitrou93963562013-05-14 20:37:52 +0200492 /* Before anything else, make sure that this frame is clearly marked
493 * as being defunct! Else, e.g., a generator reachable from this
494 * frame may also point to this frame, believe itself to still be
495 * active, and try cleaning up this frame again.
496 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 oldtop = f->f_stacktop;
498 f->f_stacktop = NULL;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200499 f->f_executing = 0;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 Py_CLEAR(f->f_trace);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 /* locals */
504 slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
505 fastlocals = f->f_localsplus;
506 for (i = slots; --i >= 0; ++fastlocals)
507 Py_CLEAR(*fastlocals);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 /* stack */
510 if (oldtop != NULL) {
511 for (p = f->f_valuestack; p < oldtop; p++)
512 Py_CLEAR(*p);
513 }
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200514 return 0;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000515}
516
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000517static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530518frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
Antoine Pitrou58720d62013-08-05 23:26:40 +0200519{
520 if (f->f_executing) {
521 PyErr_SetString(PyExc_RuntimeError,
522 "cannot clear an executing frame");
523 return NULL;
524 }
525 if (f->f_gen) {
526 _PyGen_Finalize(f->f_gen);
527 assert(f->f_gen == NULL);
528 }
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200529 (void)frame_tp_clear(f);
Antoine Pitrou58720d62013-08-05 23:26:40 +0200530 Py_RETURN_NONE;
531}
532
533PyDoc_STRVAR(clear__doc__,
534"F.clear(): clear most references held by the frame");
535
536static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530537frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 Py_ssize_t res, extras, ncells, nfrees;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
542 nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
543 extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
544 ncells + nfrees;
545 /* subtract one as it is already included in PyFrameObject */
546 res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000549}
550
551PyDoc_STRVAR(sizeof__doc__,
552"F.__sizeof__() -> size of F in memory, in bytes");
553
Antoine Pitrou14709142017-12-31 22:35:22 +0100554static PyObject *
555frame_repr(PyFrameObject *f)
556{
557 int lineno = PyFrame_GetLineNumber(f);
558 return PyUnicode_FromFormat(
559 "<frame at %p, file %R, line %d, code %S>",
560 f, f->f_code->co_filename, lineno, f->f_code->co_name);
561}
562
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000563static PyMethodDef frame_methods[] = {
Antoine Pitrou58720d62013-08-05 23:26:40 +0200564 {"clear", (PyCFunction)frame_clear, METH_NOARGS,
565 clear__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
567 sizeof__doc__},
568 {NULL, NULL} /* sentinel */
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000569};
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000570
Guido van Rossum18752471997-04-29 14:49:28 +0000571PyTypeObject PyFrame_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 PyVarObject_HEAD_INIT(&PyType_Type, 0)
573 "frame",
574 sizeof(PyFrameObject),
575 sizeof(PyObject *),
576 (destructor)frame_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200577 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 0, /* tp_getattr */
579 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200580 0, /* tp_as_async */
Antoine Pitrou14709142017-12-31 22:35:22 +0100581 (reprfunc)frame_repr, /* tp_repr */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 0, /* tp_as_number */
583 0, /* tp_as_sequence */
584 0, /* tp_as_mapping */
585 0, /* tp_hash */
586 0, /* tp_call */
587 0, /* tp_str */
588 PyObject_GenericGetAttr, /* tp_getattro */
589 PyObject_GenericSetAttr, /* tp_setattro */
590 0, /* tp_as_buffer */
591 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
592 0, /* tp_doc */
593 (traverseproc)frame_traverse, /* tp_traverse */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200594 (inquiry)frame_tp_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 0, /* tp_richcompare */
596 0, /* tp_weaklistoffset */
597 0, /* tp_iter */
598 0, /* tp_iternext */
599 frame_methods, /* tp_methods */
600 frame_memberlist, /* tp_members */
601 frame_getsetlist, /* tp_getset */
602 0, /* tp_base */
603 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000604};
605
Victor Stinner07e9e382013-11-07 22:22:39 +0100606_Py_IDENTIFIER(__builtins__);
Neal Norwitzc91ed402002-12-30 22:29:22 +0000607
Victor Stinnerc6944e72016-11-11 02:13:35 +0100608PyFrameObject* _Py_HOT_FUNCTION
INADA Naoki5a625d02016-12-24 20:19:08 +0900609_PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code,
610 PyObject *globals, PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 PyFrameObject *back = tstate->frame;
613 PyFrameObject *f;
614 PyObject *builtins;
615 Py_ssize_t i;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000616
Michael W. Hudson69734a52002-08-19 16:54:08 +0000617#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
619 (locals != NULL && !PyMapping_Check(locals))) {
620 PyErr_BadInternalCall();
621 return NULL;
622 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000623#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 if (back == NULL || back->f_globals != globals) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200625 builtins = _PyDict_GetItemIdWithError(globals, &PyId___builtins__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 if (builtins) {
627 if (PyModule_Check(builtins)) {
628 builtins = PyModule_GetDict(builtins);
Victor Stinnerb0b22422012-04-19 00:57:45 +0200629 assert(builtins != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 }
632 if (builtins == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200633 if (PyErr_Occurred()) {
634 return NULL;
635 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 /* No builtins! Make up a minimal one
637 Give them 'None', at least. */
638 builtins = PyDict_New();
639 if (builtins == NULL ||
640 PyDict_SetItemString(
641 builtins, "None", Py_None) < 0)
642 return NULL;
643 }
644 else
645 Py_INCREF(builtins);
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 }
648 else {
649 /* If we share the globals, we share the builtins.
650 Save a lookup and a call. */
651 builtins = back->f_builtins;
Victor Stinnerb0b22422012-04-19 00:57:45 +0200652 assert(builtins != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 Py_INCREF(builtins);
654 }
655 if (code->co_zombieframe != NULL) {
656 f = code->co_zombieframe;
657 code->co_zombieframe = NULL;
658 _Py_NewReference((PyObject *)f);
659 assert(f->f_code == code);
660 }
661 else {
662 Py_ssize_t extras, ncells, nfrees;
663 ncells = PyTuple_GET_SIZE(code->co_cellvars);
664 nfrees = PyTuple_GET_SIZE(code->co_freevars);
665 extras = code->co_stacksize + code->co_nlocals + ncells +
666 nfrees;
667 if (free_list == NULL) {
668 f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type,
669 extras);
670 if (f == NULL) {
671 Py_DECREF(builtins);
672 return NULL;
673 }
674 }
675 else {
676 assert(numfree > 0);
677 --numfree;
678 f = free_list;
679 free_list = free_list->f_back;
680 if (Py_SIZE(f) < extras) {
Kristjan Valur Jonsson85634d72012-05-31 09:37:31 +0000681 PyFrameObject *new_f = PyObject_GC_Resize(PyFrameObject, f, extras);
682 if (new_f == NULL) {
683 PyObject_GC_Del(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 Py_DECREF(builtins);
685 return NULL;
686 }
Kristjan Valur Jonsson85634d72012-05-31 09:37:31 +0000687 f = new_f;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 }
689 _Py_NewReference((PyObject *)f);
690 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 f->f_code = code;
693 extras = code->co_nlocals + ncells + nfrees;
694 f->f_valuestack = f->f_localsplus + extras;
695 for (i=0; i<extras; i++)
696 f->f_localsplus[i] = NULL;
697 f->f_locals = NULL;
698 f->f_trace = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 }
700 f->f_stacktop = f->f_valuestack;
701 f->f_builtins = builtins;
702 Py_XINCREF(back);
703 f->f_back = back;
704 Py_INCREF(code);
705 Py_INCREF(globals);
706 f->f_globals = globals;
707 /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
708 if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
709 (CO_NEWLOCALS | CO_OPTIMIZED))
710 ; /* f_locals = NULL; will be set by PyFrame_FastToLocals() */
711 else if (code->co_flags & CO_NEWLOCALS) {
712 locals = PyDict_New();
713 if (locals == NULL) {
714 Py_DECREF(f);
715 return NULL;
716 }
717 f->f_locals = locals;
718 }
719 else {
720 if (locals == NULL)
721 locals = globals;
722 Py_INCREF(locals);
723 f->f_locals = locals;
724 }
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 f->f_lasti = -1;
727 f->f_lineno = code->co_firstlineno;
728 f->f_iblock = 0;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200729 f->f_executing = 0;
730 f->f_gen = NULL;
Nick Coghlan5a851672017-09-08 10:14:16 +1000731 f->f_trace_opcodes = 0;
732 f->f_trace_lines = 1;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000735}
736
INADA Naoki5a625d02016-12-24 20:19:08 +0900737PyFrameObject*
738PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
739 PyObject *globals, PyObject *locals)
740{
741 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, code, globals, locals);
742 if (f)
743 _PyObject_GC_TRACK(f);
744 return f;
745}
746
747
Guido van Rossum3f5da241990-12-20 15:06:42 +0000748/* Block management */
749
750void
Fred Drake1b190b42000-07-09 05:40:56 +0000751PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PyTryBlock *b;
754 if (f->f_iblock >= CO_MAXBLOCKS)
755 Py_FatalError("XXX block stack overflow");
756 b = &f->f_blockstack[f->f_iblock++];
757 b->b_type = type;
758 b->b_level = level;
759 b->b_handler = handler;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000760}
761
Guido van Rossum18752471997-04-29 14:49:28 +0000762PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000763PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 PyTryBlock *b;
766 if (f->f_iblock <= 0)
767 Py_FatalError("XXX block stack underflow");
768 b = &f->f_blockstack[--f->f_iblock];
769 return b;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000770}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000771
Guido van Rossumd8faa362007-04-27 19:54:29 +0000772/* Convert between "fast" version of locals and dictionary version.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773
774 map and values are input arguments. map is a tuple of strings.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000775 values is an array of PyObject*. At index i, map[i] is the name of
776 the variable with value values[i]. The function copies the first
777 nmap variable from map/values into dict. If values[i] is NULL,
778 the variable is deleted from dict.
779
780 If deref is true, then the values being copied are cell variables
781 and the value is extracted from the cell variable before being put
782 in dict.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000783 */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000784
Victor Stinner41bb43a2013-10-29 01:19:37 +0100785static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000786map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 int deref)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 Py_ssize_t j;
790 assert(PyTuple_Check(map));
791 assert(PyDict_Check(dict));
792 assert(PyTuple_Size(map) >= nmap);
Raymond Hettingera4d00012018-01-28 09:40:24 -0800793 for (j=0; j < nmap; j++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 PyObject *key = PyTuple_GET_ITEM(map, j);
795 PyObject *value = values[j];
796 assert(PyUnicode_Check(key));
Antoine Pitrouacc8cf22014-07-04 20:24:13 -0400797 if (deref && value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 assert(PyCell_Check(value));
799 value = PyCell_GET(value);
800 }
801 if (value == NULL) {
Victor Stinner41bb43a2013-10-29 01:19:37 +0100802 if (PyObject_DelItem(dict, key) != 0) {
803 if (PyErr_ExceptionMatches(PyExc_KeyError))
804 PyErr_Clear();
805 else
806 return -1;
807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 }
809 else {
810 if (PyObject_SetItem(dict, key, value) != 0)
Victor Stinner41bb43a2013-10-29 01:19:37 +0100811 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 }
813 }
Victor Stinner41bb43a2013-10-29 01:19:37 +0100814 return 0;
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000815}
816
Guido van Rossumd8faa362007-04-27 19:54:29 +0000817/* Copy values from the "locals" dict into the fast locals.
818
819 dict is an input argument containing string keys representing
820 variables names and arbitrary PyObject* as values.
821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 map and values are input arguments. map is a tuple of strings.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000823 values is an array of PyObject*. At index i, map[i] is the name of
824 the variable with value values[i]. The function copies the first
825 nmap variable from map/values into dict. If values[i] is NULL,
826 the variable is deleted from dict.
827
828 If deref is true, then the values being copied are cell variables
829 and the value is extracted from the cell variable before being put
830 in dict. If clear is true, then variables in map but not in dict
831 are set to NULL in map; if clear is false, variables missing in
832 dict are ignored.
833
834 Exceptions raised while modifying the dict are silently ignored,
835 because there is no good way to report them.
836*/
837
Guido van Rossum6b356e72001-04-14 17:55:41 +0000838static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000839dict_to_map(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 int deref, int clear)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 Py_ssize_t j;
843 assert(PyTuple_Check(map));
844 assert(PyDict_Check(dict));
845 assert(PyTuple_Size(map) >= nmap);
Raymond Hettingera4d00012018-01-28 09:40:24 -0800846 for (j=0; j < nmap; j++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 PyObject *key = PyTuple_GET_ITEM(map, j);
848 PyObject *value = PyObject_GetItem(dict, key);
849 assert(PyUnicode_Check(key));
850 /* We only care about NULLs if clear is true. */
851 if (value == NULL) {
852 PyErr_Clear();
853 if (!clear)
854 continue;
855 }
856 if (deref) {
857 assert(PyCell_Check(values[j]));
858 if (PyCell_GET(values[j]) != value) {
859 if (PyCell_Set(values[j], value) < 0)
860 PyErr_Clear();
861 }
862 } else if (values[j] != value) {
863 Py_XINCREF(value);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300864 Py_XSETREF(values[j], value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 }
866 Py_XDECREF(value);
867 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000868}
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000869
Victor Stinner41bb43a2013-10-29 01:19:37 +0100870int
871PyFrame_FastToLocalsWithError(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 /* Merge fast locals into f->f_locals */
874 PyObject *locals, *map;
875 PyObject **fast;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 PyCodeObject *co;
877 Py_ssize_t j;
Victor Stinner7a6d7cf2012-10-31 00:37:41 +0100878 Py_ssize_t ncells, nfreevars;
Victor Stinner41bb43a2013-10-29 01:19:37 +0100879
880 if (f == NULL) {
881 PyErr_BadInternalCall();
882 return -1;
883 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 locals = f->f_locals;
885 if (locals == NULL) {
886 locals = f->f_locals = PyDict_New();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100887 if (locals == NULL)
888 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 }
890 co = f->f_code;
891 map = co->co_varnames;
Victor Stinner41bb43a2013-10-29 01:19:37 +0100892 if (!PyTuple_Check(map)) {
893 PyErr_Format(PyExc_SystemError,
894 "co_varnames must be a tuple, not %s",
895 Py_TYPE(map)->tp_name);
896 return -1;
897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 fast = f->f_localsplus;
899 j = PyTuple_GET_SIZE(map);
900 if (j > co->co_nlocals)
901 j = co->co_nlocals;
Victor Stinner41bb43a2013-10-29 01:19:37 +0100902 if (co->co_nlocals) {
903 if (map_to_dict(map, j, locals, fast, 0) < 0)
904 return -1;
905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 ncells = PyTuple_GET_SIZE(co->co_cellvars);
907 nfreevars = PyTuple_GET_SIZE(co->co_freevars);
908 if (ncells || nfreevars) {
Victor Stinner41bb43a2013-10-29 01:19:37 +0100909 if (map_to_dict(co->co_cellvars, ncells,
910 locals, fast + co->co_nlocals, 1))
911 return -1;
912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 /* If the namespace is unoptimized, then one of the
914 following cases applies:
915 1. It does not contain free variables, because it
916 uses import * or is a top-level namespace.
917 2. It is a class namespace.
918 We don't want to accidentally copy free variables
919 into the locals dict used by the class.
920 */
921 if (co->co_flags & CO_OPTIMIZED) {
Victor Stinner41bb43a2013-10-29 01:19:37 +0100922 if (map_to_dict(co->co_freevars, nfreevars,
923 locals, fast + co->co_nlocals + ncells, 1) < 0)
924 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 }
926 }
Victor Stinner41bb43a2013-10-29 01:19:37 +0100927 return 0;
928}
929
930void
931PyFrame_FastToLocals(PyFrameObject *f)
932{
933 int res;
934
935 assert(!PyErr_Occurred());
936
937 res = PyFrame_FastToLocalsWithError(f);
938 if (res < 0)
939 PyErr_Clear();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000940}
941
942void
Fred Drake1b190b42000-07-09 05:40:56 +0000943PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 /* Merge f->f_locals into fast locals */
946 PyObject *locals, *map;
947 PyObject **fast;
948 PyObject *error_type, *error_value, *error_traceback;
949 PyCodeObject *co;
950 Py_ssize_t j;
Victor Stinner7a6d7cf2012-10-31 00:37:41 +0100951 Py_ssize_t ncells, nfreevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 if (f == NULL)
953 return;
954 locals = f->f_locals;
955 co = f->f_code;
956 map = co->co_varnames;
957 if (locals == NULL)
958 return;
959 if (!PyTuple_Check(map))
960 return;
961 PyErr_Fetch(&error_type, &error_value, &error_traceback);
962 fast = f->f_localsplus;
963 j = PyTuple_GET_SIZE(map);
964 if (j > co->co_nlocals)
965 j = co->co_nlocals;
966 if (co->co_nlocals)
967 dict_to_map(co->co_varnames, j, locals, fast, 0, clear);
968 ncells = PyTuple_GET_SIZE(co->co_cellvars);
969 nfreevars = PyTuple_GET_SIZE(co->co_freevars);
970 if (ncells || nfreevars) {
971 dict_to_map(co->co_cellvars, ncells,
972 locals, fast + co->co_nlocals, 1, clear);
973 /* Same test as in PyFrame_FastToLocals() above. */
974 if (co->co_flags & CO_OPTIMIZED) {
975 dict_to_map(co->co_freevars, nfreevars,
976 locals, fast + co->co_nlocals + ncells, 1,
977 clear);
978 }
979 }
980 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000981}
Guido van Rossum404b95d1997-08-05 02:09:46 +0000982
983/* Clear out the free list */
Christian Heimesa156e092008-02-16 07:38:31 +0000984int
985PyFrame_ClearFreeList(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +0000986{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 int freelist_size = numfree;
988
989 while (free_list != NULL) {
990 PyFrameObject *f = free_list;
991 free_list = free_list->f_back;
992 PyObject_GC_Del(f);
993 --numfree;
994 }
995 assert(numfree == 0);
996 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000997}
998
999void
Victor Stinnerbed48172019-08-27 00:12:32 +02001000_PyFrame_Fini(void)
Christian Heimesa156e092008-02-16 07:38:31 +00001001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 (void)PyFrame_ClearFreeList();
Guido van Rossum404b95d1997-08-05 02:09:46 +00001003}
David Malcolm49526f42012-06-22 14:55:41 -04001004
1005/* Print summary info about the state of the optimized allocator */
1006void
1007_PyFrame_DebugMallocStats(FILE *out)
1008{
1009 _PyDebugAllocatorStats(out,
1010 "free PyFrameObject",
1011 numfree, sizeof(PyFrameObject));
1012}
1013