blob: 4362615cb08986671c20a5540c20533403caf475 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06004#include "internal/pystate.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "frameobject.h"
8#include "opcode.h"
9#include "structmember.h"
10
Guido van Rossum18752471997-04-29 14:49:28 +000011#define OFF(x) offsetof(PyFrameObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000012
Guido van Rossum6f799372001-09-20 20:46:19 +000013static PyMemberDef frame_memberlist[] = {
Nick Coghlan1f7ce622012-01-13 21:43:40 +100014 {"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 Coghlan5a851672017-09-08 10:14:16 +100019 {"f_trace_lines", T_BOOL, OFF(f_trace_lines), 0},
20 {"f_trace_opcodes", T_BOOL, OFF(f_trace_opcodes), 0},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +000022};
23
Guido van Rossum18752471997-04-29 14:49:28 +000024static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000025frame_getlocals(PyFrameObject *f, void *closure)
Guido van Rossum3f5da241990-12-20 15:06:42 +000026{
Victor Stinner41bb43a2013-10-29 01:19:37 +010027 if (PyFrame_FastToLocalsWithError(f) < 0)
28 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 Py_INCREF(f->f_locals);
30 return f->f_locals;
Guido van Rossum3f5da241990-12-20 15:06:42 +000031}
32
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000033int
34PyFrame_GetLineNumber(PyFrameObject *f)
35{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 if (f->f_trace)
37 return f->f_lineno;
38 else
39 return PyCode_Addr2Line(f->f_code, f->f_lasti);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000040}
41
Michael W. Hudsondd32a912002-08-15 14:59:02 +000042static PyObject *
43frame_getlineno(PyFrameObject *f, void *closure)
44{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 return PyLong_FromLong(PyFrame_GetLineNumber(f));
Michael W. Hudsondd32a912002-08-15 14:59:02 +000046}
47
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000048/* Setter for f_lineno - you can set f_lineno from within a trace function in
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 * order to jump to a given line of code, subject to some restrictions. Most
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000050 * lines are OK to jump to because they don't make any assumptions about the
51 * state of the stack (obvious because you could remove the line and the code
52 * would still work without any stack errors), but there are some constructs
53 * that limit jumping:
54 *
55 * o Lines with an 'except' statement on them can't be jumped to, because
56 * they expect an exception to be on the top of the stack.
57 * o Lines that live in a 'finally' block can't be jumped from or to, since
58 * the END_FINALLY expects to clean up the stack after the 'try' block.
59 * o 'try'/'for'/'while' blocks can't be jumped into because the blockstack
60 * needs to be set up before their code runs, and for 'for' loops the
61 * iterator needs to be on the stack.
xdegayee32bbaf2018-03-13 09:52:35 +010062 * o Jumps cannot be made from within a trace function invoked with a
63 * 'return' or 'exception' event since the eval loop has been exited at
64 * that time.
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000065 */
66static int
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -080067frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignored))
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 int new_lineno = 0; /* The new value of f_lineno */
70 long l_new_lineno;
71 int overflow;
72 int new_lasti = 0; /* The new value of f_lasti */
73 int new_iblock = 0; /* The new value of f_iblock */
74 unsigned char *code = NULL; /* The bytecode for the frame... */
75 Py_ssize_t code_len = 0; /* ...and its length */
76 unsigned char *lnotab = NULL; /* Iterating over co_lnotab */
77 Py_ssize_t lnotab_len = 0; /* (ditto) */
78 int offset = 0; /* (ditto) */
79 int line = 0; /* (ditto) */
80 int addr = 0; /* (ditto) */
81 int min_addr = 0; /* Scanning the SETUPs and POPs */
82 int max_addr = 0; /* (ditto) */
83 int delta_iblock = 0; /* (ditto) */
84 int min_delta_iblock = 0; /* (ditto) */
85 int min_iblock = 0; /* (ditto) */
86 int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */
87 int new_lasti_setup_addr = 0; /* (ditto) */
88 int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */
89 int in_finally[CO_MAXBLOCKS]; /* (ditto) */
90 int blockstack_top = 0; /* (ditto) */
91 unsigned char setup_op = 0; /* (ditto) */
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000092
Miss Islington (bot)cb272842018-12-17 07:10:20 -080093 if (p_new_lineno == NULL) {
94 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
95 return -1;
96 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 /* f_lineno must be an integer. */
98 if (!PyLong_CheckExact(p_new_lineno)) {
99 PyErr_SetString(PyExc_ValueError,
100 "lineno must be an integer");
101 return -1;
102 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000103
xdegayee32bbaf2018-03-13 09:52:35 +0100104 /* Upon the 'call' trace event of a new frame, f->f_lasti is -1 and
105 * f->f_trace is NULL, check first on the first condition.
106 * Forbidding jumps from the 'call' event of a new frame is a side effect
107 * of allowing to set f_lineno only from trace functions. */
108 if (f->f_lasti == -1) {
109 PyErr_Format(PyExc_ValueError,
110 "can't jump from the 'call' trace event of a new frame");
111 return -1;
112 }
113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 /* You can only do this from within a trace function, not via
115 * _getframe or similar hackery. */
xdegayee32bbaf2018-03-13 09:52:35 +0100116 if (!f->f_trace) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 PyErr_Format(PyExc_ValueError,
xdegayee32bbaf2018-03-13 09:52:35 +0100118 "f_lineno can only be set by a trace function");
119 return -1;
120 }
121
122 /* Forbid jumps upon a 'return' trace event (except after executing a
123 * YIELD_VALUE or YIELD_FROM opcode, f_stacktop is not NULL in that case)
124 * and upon an 'exception' trace event.
125 * Jumps from 'call' trace events have already been forbidden above for new
126 * frames, so this check does not change anything for 'call' events. */
127 if (f->f_stacktop == NULL) {
128 PyErr_SetString(PyExc_ValueError,
129 "can only jump from a 'line' trace event");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 return -1;
131 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 /* Fail if the line comes before the start of the code block. */
134 l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
135 if (overflow
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000136#if SIZEOF_LONG > SIZEOF_INT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 || l_new_lineno > INT_MAX
138 || l_new_lineno < INT_MIN
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000139#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 ) {
141 PyErr_SetString(PyExc_ValueError,
142 "lineno out of range");
143 return -1;
144 }
145 new_lineno = (int)l_new_lineno;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (new_lineno < f->f_code->co_firstlineno) {
148 PyErr_Format(PyExc_ValueError,
149 "line %d comes before the current code block",
150 new_lineno);
151 return -1;
152 }
153 else if (new_lineno == f->f_code->co_firstlineno) {
154 new_lasti = 0;
155 new_lineno = f->f_code->co_firstlineno;
156 }
157 else {
158 /* Find the bytecode offset for the start of the given
159 * line, or the first code-owning line after it. */
160 char *tmp;
161 PyBytes_AsStringAndSize(f->f_code->co_lnotab,
162 &tmp, &lnotab_len);
163 lnotab = (unsigned char *) tmp;
164 addr = 0;
165 line = f->f_code->co_firstlineno;
166 new_lasti = -1;
167 for (offset = 0; offset < lnotab_len; offset += 2) {
168 addr += lnotab[offset];
Victor Stinnerf3914eb2016-01-20 12:16:21 +0100169 line += (signed char)lnotab[offset+1];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (line >= new_lineno) {
171 new_lasti = addr;
172 new_lineno = line;
173 break;
174 }
175 }
176 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 /* If we didn't reach the requested line, return an error. */
179 if (new_lasti == -1) {
180 PyErr_Format(PyExc_ValueError,
181 "line %d comes after the current code block",
182 new_lineno);
183 return -1;
184 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 /* We're now ready to look at the bytecode. */
187 PyBytes_AsStringAndSize(f->f_code->co_code, (char **)&code, &code_len);
xdegayee32bbaf2018-03-13 09:52:35 +0100188
189 /* The trace function is called with a 'return' trace event after the
190 * execution of a yield statement. */
191 assert(f->f_lasti != -1);
192 if (code[f->f_lasti] == YIELD_VALUE || code[f->f_lasti] == YIELD_FROM) {
193 PyErr_SetString(PyExc_ValueError,
194 "can't jump from a yield statement");
195 return -1;
196 }
197
Victor Stinner640c35c2013-06-04 23:14:37 +0200198 min_addr = Py_MIN(new_lasti, f->f_lasti);
199 max_addr = Py_MAX(new_lasti, f->f_lasti);
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 /* You can't jump onto a line with an 'except' statement on it -
202 * they expect to have an exception on the top of the stack, which
203 * won't be true if you jump to them. They always start with code
204 * that either pops the exception using POP_TOP (plain 'except:'
205 * lines do this) or duplicates the exception on the stack using
206 * DUP_TOP (if there's an exception type specified). See compile.c,
207 * 'com_try_except' for the full details. There aren't any other
208 * cases (AFAIK) where a line's code can start with DUP_TOP or
209 * POP_TOP, but if any ever appear, they'll be subject to the same
210 * restriction (but with a different error message). */
211 if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
212 PyErr_SetString(PyExc_ValueError,
213 "can't jump to 'except' line as there's no exception");
214 return -1;
215 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 /* You can't jump into or out of a 'finally' block because the 'try'
218 * block leaves something on the stack for the END_FINALLY to clean
219 * up. So we walk the bytecode, maintaining a simulated blockstack.
220 * When we reach the old or new address and it's in a 'finally' block
221 * we note the address of the corresponding SETUP_FINALLY. The jump
222 * is only legal if neither address is in a 'finally' block or
223 * they're both in the same one. 'blockstack' is a stack of the
224 * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks
225 * whether we're in a 'finally' block at each blockstack level. */
226 f_lasti_setup_addr = -1;
227 new_lasti_setup_addr = -1;
228 memset(blockstack, '\0', sizeof(blockstack));
229 memset(in_finally, '\0', sizeof(in_finally));
230 blockstack_top = 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +0300231 for (addr = 0; addr < code_len; addr += sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 unsigned char op = code[addr];
233 switch (op) {
234 case SETUP_LOOP:
235 case SETUP_EXCEPT:
236 case SETUP_FINALLY:
Benjamin Petersone42fb302012-04-18 11:14:31 -0400237 case SETUP_WITH:
Yury Selivanov75445082015-05-11 22:57:16 -0400238 case SETUP_ASYNC_WITH:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 blockstack[blockstack_top++] = addr;
240 in_finally[blockstack_top-1] = 0;
241 break;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 case POP_BLOCK:
244 assert(blockstack_top > 0);
245 setup_op = code[blockstack[blockstack_top-1]];
Yury Selivanov75445082015-05-11 22:57:16 -0400246 if (setup_op == SETUP_FINALLY || setup_op == SETUP_WITH
247 || setup_op == SETUP_ASYNC_WITH) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 in_finally[blockstack_top-1] = 1;
249 }
250 else {
251 blockstack_top--;
252 }
253 break;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 case END_FINALLY:
256 /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist
257 * in the bytecode but don't correspond to an actual
258 * 'finally' block. (If blockstack_top is 0, we must
259 * be seeing such an END_FINALLY.) */
260 if (blockstack_top > 0) {
261 setup_op = code[blockstack[blockstack_top-1]];
Yury Selivanov75445082015-05-11 22:57:16 -0400262 if (setup_op == SETUP_FINALLY || setup_op == SETUP_WITH
263 || setup_op == SETUP_ASYNC_WITH) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 blockstack_top--;
265 }
266 }
267 break;
268 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 /* For the addresses we're interested in, see whether they're
271 * within a 'finally' block and if so, remember the address
272 * of the SETUP_FINALLY. */
273 if (addr == new_lasti || addr == f->f_lasti) {
274 int i = 0;
275 int setup_addr = -1;
276 for (i = blockstack_top-1; i >= 0; i--) {
277 if (in_finally[i]) {
278 setup_addr = blockstack[i];
279 break;
280 }
281 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 if (setup_addr != -1) {
284 if (addr == new_lasti) {
285 new_lasti_setup_addr = setup_addr;
286 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 if (addr == f->f_lasti) {
289 f_lasti_setup_addr = setup_addr;
290 }
291 }
292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 }
Neal Norwitzee65e222002-12-19 18:16:57 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* Verify that the blockstack tracking code didn't get lost. */
296 assert(blockstack_top == 0);
297
298 /* After all that, are we jumping into / out of a 'finally' block? */
299 if (new_lasti_setup_addr != f_lasti_setup_addr) {
300 PyErr_SetString(PyExc_ValueError,
301 "can't jump into or out of a 'finally' block");
302 return -1;
303 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000304
305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 /* Police block-jumping (you can't jump into the middle of a block)
307 * and ensure that the blockstack finishes up in a sensible state (by
308 * popping any blocks we're jumping out of). We look at all the
309 * blockstack operations between the current position and the new
310 * one, and keep track of how many blocks we drop out of on the way.
311 * By also keeping track of the lowest blockstack position we see, we
312 * can tell whether the jump goes into any blocks without coming out
313 * again - in that case we raise an exception below. */
314 delta_iblock = 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +0300315 for (addr = min_addr; addr < max_addr; addr += sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 unsigned char op = code[addr];
317 switch (op) {
318 case SETUP_LOOP:
319 case SETUP_EXCEPT:
320 case SETUP_FINALLY:
Benjamin Petersone42fb302012-04-18 11:14:31 -0400321 case SETUP_WITH:
Yury Selivanov75445082015-05-11 22:57:16 -0400322 case SETUP_ASYNC_WITH:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 delta_iblock++;
324 break;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 case POP_BLOCK:
327 delta_iblock--;
328 break;
329 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000330
Victor Stinner640c35c2013-06-04 23:14:37 +0200331 min_delta_iblock = Py_MIN(min_delta_iblock, delta_iblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 /* Derive the absolute iblock values from the deltas. */
335 min_iblock = f->f_iblock + min_delta_iblock;
336 if (new_lasti > f->f_lasti) {
337 /* Forwards jump. */
338 new_iblock = f->f_iblock + delta_iblock;
339 }
340 else {
341 /* Backwards jump. */
342 new_iblock = f->f_iblock - delta_iblock;
343 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 /* Are we jumping into a block? */
346 if (new_iblock > min_iblock) {
347 PyErr_SetString(PyExc_ValueError,
348 "can't jump into the middle of a block");
349 return -1;
350 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 /* Pop any blocks that we're jumping out of. */
353 while (f->f_iblock > new_iblock) {
354 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
355 while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
356 PyObject *v = (*--f->f_stacktop);
357 Py_DECREF(v);
358 }
Serhiy Storchaka04aadf22018-03-11 09:30:13 +0200359 if (b->b_type == SETUP_FINALLY &&
360 code[b->b_handler] == WITH_CLEANUP_START)
361 {
362 /* Pop the exit function. */
363 PyObject *v = (*--f->f_stacktop);
364 Py_DECREF(v);
365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* Finally set the new f_lineno and f_lasti and return OK. */
369 f->f_lineno = new_lineno;
370 f->f_lasti = new_lasti;
371 return 0;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000372}
373
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000374static PyObject *
375frame_gettrace(PyFrameObject *f, void *closure)
376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 PyObject* trace = f->f_trace;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (trace == NULL)
380 trace = Py_None;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 Py_INCREF(trace);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 return trace;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000385}
386
387static int
388frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 /* We rely on f_lineno being accurate when f_trace is set. */
391 f->f_lineno = PyFrame_GetLineNumber(f);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000392
Serhiy Storchaka64a263a2016-06-04 20:32:36 +0300393 if (v == Py_None)
394 v = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 Py_XINCREF(v);
Serhiy Storchakaec397562016-04-06 09:50:03 +0300396 Py_XSETREF(f->f_trace, v);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 return 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000399}
400
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000401
Guido van Rossum32d34c82001-09-20 21:45:26 +0000402static PyGetSetDef frame_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 {"f_locals", (getter)frame_getlocals, NULL, NULL},
404 {"f_lineno", (getter)frame_getlineno,
405 (setter)frame_setlineno, NULL},
406 {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
407 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000408};
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000409
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000410/* Stack frames are allocated and deallocated at a considerable rate.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000411 In an attempt to improve the speed of function calls, we:
412
413 1. Hold a single "zombie" frame on each code object. This retains
414 the allocated and initialised frame object from an invocation of
415 the code object. The zombie is reanimated the next time we need a
416 frame object for that code object. Doing this saves the malloc/
417 realloc required when using a free_list frame that isn't the
418 correct size. It also saves some field initialisation.
419
420 In zombie mode, no field of PyFrameObject holds a reference, but
421 the following fields are still valid:
422
423 * ob_type, ob_size, f_code, f_valuestack;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424
Mark Shannonae3087c2017-10-22 22:41:51 +0100425 * f_locals, f_trace are NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000426
427 * f_localsplus does not require re-allocation and
428 the local variables in f_localsplus are NULL.
429
430 2. We also maintain a separate free list of stack frames (just like
Mark Dickinsond19052c2010-06-27 18:19:09 +0000431 floats are allocated in a special way -- see floatobject.c). When
Thomas Wouters477c8d52006-05-27 19:21:47 +0000432 a stack frame is on the free list, only the following members have
433 a meaning:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 ob_type == &Frametype
435 f_back next item on free list, or NULL
436 f_stacksize size of value stack
437 ob_size size of localsplus
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000438 Note that the value and block stacks are preserved -- this can save
439 another malloc() call or two (and two free() calls as well!).
440 Also note that, unlike for integers, each frame object is a
441 malloc'ed object in its own right -- it is only the actual calls to
442 malloc() that we are trying to save here, not the administration.
443 After all, while a typical program may make millions of calls, a
444 call depth of more than 20 or 30 is probably already exceptional
445 unless the program contains run-away recursion. I hope.
Tim Petersb7ba7432002-04-13 05:21:47 +0000446
Christian Heimes2202f872008-02-06 14:31:34 +0000447 Later, PyFrame_MAXFREELIST was added to bound the # of frames saved on
Tim Petersb7ba7432002-04-13 05:21:47 +0000448 free_list. Else programs creating lots of cyclic trash involving
449 frames could provoke free_list into growing without bound.
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000450*/
451
Guido van Rossum18752471997-04-29 14:49:28 +0000452static PyFrameObject *free_list = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453static int numfree = 0; /* number of frames currently in free_list */
Christian Heimes2202f872008-02-06 14:31:34 +0000454/* max value for numfree */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455#define PyFrame_MAXFREELIST 200
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000456
Victor Stinnerc6944e72016-11-11 02:13:35 +0100457static void _Py_HOT_FUNCTION
Fred Drake1b190b42000-07-09 05:40:56 +0000458frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000459{
Antoine Pitrou93963562013-05-14 20:37:52 +0200460 PyObject **p, **valuestack;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 PyCodeObject *co;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000462
INADA Naoki5a625d02016-12-24 20:19:08 +0900463 if (_PyObject_GC_IS_TRACKED(f))
464 _PyObject_GC_UNTRACK(f);
465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 Py_TRASHCAN_SAFE_BEGIN(f)
Antoine Pitrou93963562013-05-14 20:37:52 +0200467 /* Kill all local variables */
468 valuestack = f->f_valuestack;
469 for (p = f->f_localsplus; p < valuestack; p++)
470 Py_CLEAR(*p);
471
472 /* Free stack */
473 if (f->f_stacktop != NULL) {
474 for (p = valuestack; p < f->f_stacktop; p++)
475 Py_XDECREF(*p);
476 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 Py_XDECREF(f->f_back);
479 Py_DECREF(f->f_builtins);
480 Py_DECREF(f->f_globals);
481 Py_CLEAR(f->f_locals);
Antoine Pitrou93963562013-05-14 20:37:52 +0200482 Py_CLEAR(f->f_trace);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 co = f->f_code;
485 if (co->co_zombieframe == NULL)
486 co->co_zombieframe = f;
487 else if (numfree < PyFrame_MAXFREELIST) {
488 ++numfree;
489 f->f_back = free_list;
490 free_list = f;
491 }
492 else
493 PyObject_GC_Del(f);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 Py_DECREF(co);
496 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000497}
498
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000499static int
500frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyObject **fastlocals, **p;
Victor Stinner7a6d7cf2012-10-31 00:37:41 +0100503 Py_ssize_t i, slots;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 Py_VISIT(f->f_back);
506 Py_VISIT(f->f_code);
507 Py_VISIT(f->f_builtins);
508 Py_VISIT(f->f_globals);
509 Py_VISIT(f->f_locals);
510 Py_VISIT(f->f_trace);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 /* locals */
513 slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
514 fastlocals = f->f_localsplus;
515 for (i = slots; --i >= 0; ++fastlocals)
516 Py_VISIT(*fastlocals);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 /* stack */
519 if (f->f_stacktop != NULL) {
520 for (p = f->f_valuestack; p < f->f_stacktop; p++)
521 Py_VISIT(*p);
522 }
523 return 0;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000524}
525
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -0800526static int
Antoine Pitrou58720d62013-08-05 23:26:40 +0200527frame_tp_clear(PyFrameObject *f)
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyObject **fastlocals, **p, **oldtop;
Victor Stinner7a6d7cf2012-10-31 00:37:41 +0100530 Py_ssize_t i, slots;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000531
Antoine Pitrou93963562013-05-14 20:37:52 +0200532 /* Before anything else, make sure that this frame is clearly marked
533 * as being defunct! Else, e.g., a generator reachable from this
534 * frame may also point to this frame, believe itself to still be
535 * active, and try cleaning up this frame again.
536 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 oldtop = f->f_stacktop;
538 f->f_stacktop = NULL;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200539 f->f_executing = 0;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 Py_CLEAR(f->f_trace);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 /* locals */
544 slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
545 fastlocals = f->f_localsplus;
546 for (i = slots; --i >= 0; ++fastlocals)
547 Py_CLEAR(*fastlocals);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 /* stack */
550 if (oldtop != NULL) {
551 for (p = f->f_valuestack; p < oldtop; p++)
552 Py_CLEAR(*p);
553 }
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -0800554 return 0;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000555}
556
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000557static PyObject *
Antoine Pitrou58720d62013-08-05 23:26:40 +0200558frame_clear(PyFrameObject *f)
559{
560 if (f->f_executing) {
561 PyErr_SetString(PyExc_RuntimeError,
562 "cannot clear an executing frame");
563 return NULL;
564 }
565 if (f->f_gen) {
566 _PyGen_Finalize(f->f_gen);
567 assert(f->f_gen == NULL);
568 }
Miss Islington (bot)5ceb7012018-11-27 09:58:07 -0800569 (void)frame_tp_clear(f);
Antoine Pitrou58720d62013-08-05 23:26:40 +0200570 Py_RETURN_NONE;
571}
572
573PyDoc_STRVAR(clear__doc__,
574"F.clear(): clear most references held by the frame");
575
576static PyObject *
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000577frame_sizeof(PyFrameObject *f)
578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 Py_ssize_t res, extras, ncells, nfrees;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
582 nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
583 extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
584 ncells + nfrees;
585 /* subtract one as it is already included in PyFrameObject */
586 res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000589}
590
591PyDoc_STRVAR(sizeof__doc__,
592"F.__sizeof__() -> size of F in memory, in bytes");
593
Antoine Pitrou14709142017-12-31 22:35:22 +0100594static PyObject *
595frame_repr(PyFrameObject *f)
596{
597 int lineno = PyFrame_GetLineNumber(f);
598 return PyUnicode_FromFormat(
599 "<frame at %p, file %R, line %d, code %S>",
600 f, f->f_code->co_filename, lineno, f->f_code->co_name);
601}
602
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000603static PyMethodDef frame_methods[] = {
Antoine Pitrou58720d62013-08-05 23:26:40 +0200604 {"clear", (PyCFunction)frame_clear, METH_NOARGS,
605 clear__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
607 sizeof__doc__},
608 {NULL, NULL} /* sentinel */
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000609};
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000610
Guido van Rossum18752471997-04-29 14:49:28 +0000611PyTypeObject PyFrame_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 PyVarObject_HEAD_INIT(&PyType_Type, 0)
613 "frame",
614 sizeof(PyFrameObject),
615 sizeof(PyObject *),
616 (destructor)frame_dealloc, /* tp_dealloc */
617 0, /* tp_print */
618 0, /* tp_getattr */
619 0, /* tp_setattr */
620 0, /* tp_reserved */
Antoine Pitrou14709142017-12-31 22:35:22 +0100621 (reprfunc)frame_repr, /* tp_repr */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 0, /* tp_as_number */
623 0, /* tp_as_sequence */
624 0, /* tp_as_mapping */
625 0, /* tp_hash */
626 0, /* tp_call */
627 0, /* tp_str */
628 PyObject_GenericGetAttr, /* tp_getattro */
629 PyObject_GenericSetAttr, /* tp_setattro */
630 0, /* tp_as_buffer */
631 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
632 0, /* tp_doc */
633 (traverseproc)frame_traverse, /* tp_traverse */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200634 (inquiry)frame_tp_clear, /* tp_clear */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 0, /* tp_richcompare */
636 0, /* tp_weaklistoffset */
637 0, /* tp_iter */
638 0, /* tp_iternext */
639 frame_methods, /* tp_methods */
640 frame_memberlist, /* tp_members */
641 frame_getsetlist, /* tp_getset */
642 0, /* tp_base */
643 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000644};
645
Victor Stinner07e9e382013-11-07 22:22:39 +0100646_Py_IDENTIFIER(__builtins__);
Neal Norwitzc91ed402002-12-30 22:29:22 +0000647
Neal Norwitzb2501f42002-12-31 03:42:13 +0000648int _PyFrame_Init()
Neal Norwitzc91ed402002-12-30 22:29:22 +0000649{
Victor Stinner07e9e382013-11-07 22:22:39 +0100650 /* Before, PyId___builtins__ was a string created explicitly in
651 this function. Now there is nothing to initialize anymore, but
652 the function is kept for backward compatibility. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 return 1;
Neal Norwitzc91ed402002-12-30 22:29:22 +0000654}
655
Victor Stinnerc6944e72016-11-11 02:13:35 +0100656PyFrameObject* _Py_HOT_FUNCTION
INADA Naoki5a625d02016-12-24 20:19:08 +0900657_PyFrame_New_NoTrack(PyThreadState *tstate, PyCodeObject *code,
658 PyObject *globals, PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 PyFrameObject *back = tstate->frame;
661 PyFrameObject *f;
662 PyObject *builtins;
663 Py_ssize_t i;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000664
Michael W. Hudson69734a52002-08-19 16:54:08 +0000665#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
667 (locals != NULL && !PyMapping_Check(locals))) {
668 PyErr_BadInternalCall();
669 return NULL;
670 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000671#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 if (back == NULL || back->f_globals != globals) {
Victor Stinner07e9e382013-11-07 22:22:39 +0100673 builtins = _PyDict_GetItemId(globals, &PyId___builtins__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 if (builtins) {
675 if (PyModule_Check(builtins)) {
676 builtins = PyModule_GetDict(builtins);
Victor Stinnerb0b22422012-04-19 00:57:45 +0200677 assert(builtins != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 }
680 if (builtins == NULL) {
681 /* No builtins! Make up a minimal one
682 Give them 'None', at least. */
683 builtins = PyDict_New();
684 if (builtins == NULL ||
685 PyDict_SetItemString(
686 builtins, "None", Py_None) < 0)
687 return NULL;
688 }
689 else
690 Py_INCREF(builtins);
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 }
693 else {
694 /* If we share the globals, we share the builtins.
695 Save a lookup and a call. */
696 builtins = back->f_builtins;
Victor Stinnerb0b22422012-04-19 00:57:45 +0200697 assert(builtins != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 Py_INCREF(builtins);
699 }
700 if (code->co_zombieframe != NULL) {
701 f = code->co_zombieframe;
702 code->co_zombieframe = NULL;
703 _Py_NewReference((PyObject *)f);
704 assert(f->f_code == code);
705 }
706 else {
707 Py_ssize_t extras, ncells, nfrees;
708 ncells = PyTuple_GET_SIZE(code->co_cellvars);
709 nfrees = PyTuple_GET_SIZE(code->co_freevars);
710 extras = code->co_stacksize + code->co_nlocals + ncells +
711 nfrees;
712 if (free_list == NULL) {
713 f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type,
714 extras);
715 if (f == NULL) {
716 Py_DECREF(builtins);
717 return NULL;
718 }
719 }
720 else {
721 assert(numfree > 0);
722 --numfree;
723 f = free_list;
724 free_list = free_list->f_back;
725 if (Py_SIZE(f) < extras) {
Kristjan Valur Jonsson85634d72012-05-31 09:37:31 +0000726 PyFrameObject *new_f = PyObject_GC_Resize(PyFrameObject, f, extras);
727 if (new_f == NULL) {
728 PyObject_GC_Del(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 Py_DECREF(builtins);
730 return NULL;
731 }
Kristjan Valur Jonsson85634d72012-05-31 09:37:31 +0000732 f = new_f;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 }
734 _Py_NewReference((PyObject *)f);
735 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 f->f_code = code;
738 extras = code->co_nlocals + ncells + nfrees;
739 f->f_valuestack = f->f_localsplus + extras;
740 for (i=0; i<extras; i++)
741 f->f_localsplus[i] = NULL;
742 f->f_locals = NULL;
743 f->f_trace = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 }
745 f->f_stacktop = f->f_valuestack;
746 f->f_builtins = builtins;
747 Py_XINCREF(back);
748 f->f_back = back;
749 Py_INCREF(code);
750 Py_INCREF(globals);
751 f->f_globals = globals;
752 /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
753 if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
754 (CO_NEWLOCALS | CO_OPTIMIZED))
755 ; /* f_locals = NULL; will be set by PyFrame_FastToLocals() */
756 else if (code->co_flags & CO_NEWLOCALS) {
757 locals = PyDict_New();
758 if (locals == NULL) {
759 Py_DECREF(f);
760 return NULL;
761 }
762 f->f_locals = locals;
763 }
764 else {
765 if (locals == NULL)
766 locals = globals;
767 Py_INCREF(locals);
768 f->f_locals = locals;
769 }
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 f->f_lasti = -1;
772 f->f_lineno = code->co_firstlineno;
773 f->f_iblock = 0;
Antoine Pitrou58720d62013-08-05 23:26:40 +0200774 f->f_executing = 0;
775 f->f_gen = NULL;
Nick Coghlan5a851672017-09-08 10:14:16 +1000776 f->f_trace_opcodes = 0;
777 f->f_trace_lines = 1;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000780}
781
INADA Naoki5a625d02016-12-24 20:19:08 +0900782PyFrameObject*
783PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
784 PyObject *globals, PyObject *locals)
785{
786 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, code, globals, locals);
787 if (f)
788 _PyObject_GC_TRACK(f);
789 return f;
790}
791
792
Guido van Rossum3f5da241990-12-20 15:06:42 +0000793/* Block management */
794
795void
Fred Drake1b190b42000-07-09 05:40:56 +0000796PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 PyTryBlock *b;
799 if (f->f_iblock >= CO_MAXBLOCKS)
800 Py_FatalError("XXX block stack overflow");
801 b = &f->f_blockstack[f->f_iblock++];
802 b->b_type = type;
803 b->b_level = level;
804 b->b_handler = handler;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000805}
806
Guido van Rossum18752471997-04-29 14:49:28 +0000807PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000808PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyTryBlock *b;
811 if (f->f_iblock <= 0)
812 Py_FatalError("XXX block stack underflow");
813 b = &f->f_blockstack[--f->f_iblock];
814 return b;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000815}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000816
Guido van Rossumd8faa362007-04-27 19:54:29 +0000817/* Convert between "fast" version of locals and dictionary version.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818
819 map and values are input arguments. map is a tuple of strings.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000820 values is an array of PyObject*. At index i, map[i] is the name of
821 the variable with value values[i]. The function copies the first
822 nmap variable from map/values into dict. If values[i] is NULL,
823 the variable is deleted from dict.
824
825 If deref is true, then the values being copied are cell variables
826 and the value is extracted from the cell variable before being put
827 in dict.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000828 */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000829
Victor Stinner41bb43a2013-10-29 01:19:37 +0100830static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000831map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 int deref)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 Py_ssize_t j;
835 assert(PyTuple_Check(map));
836 assert(PyDict_Check(dict));
837 assert(PyTuple_Size(map) >= nmap);
Raymond Hettingera4d00012018-01-28 09:40:24 -0800838 for (j=0; j < nmap; j++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 PyObject *key = PyTuple_GET_ITEM(map, j);
840 PyObject *value = values[j];
841 assert(PyUnicode_Check(key));
Antoine Pitrouacc8cf22014-07-04 20:24:13 -0400842 if (deref && value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 assert(PyCell_Check(value));
844 value = PyCell_GET(value);
845 }
846 if (value == NULL) {
Victor Stinner41bb43a2013-10-29 01:19:37 +0100847 if (PyObject_DelItem(dict, key) != 0) {
848 if (PyErr_ExceptionMatches(PyExc_KeyError))
849 PyErr_Clear();
850 else
851 return -1;
852 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 }
854 else {
855 if (PyObject_SetItem(dict, key, value) != 0)
Victor Stinner41bb43a2013-10-29 01:19:37 +0100856 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 }
858 }
Victor Stinner41bb43a2013-10-29 01:19:37 +0100859 return 0;
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000860}
861
Guido van Rossumd8faa362007-04-27 19:54:29 +0000862/* Copy values from the "locals" dict into the fast locals.
863
864 dict is an input argument containing string keys representing
865 variables names and arbitrary PyObject* as values.
866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 map and values are input arguments. map is a tuple of strings.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000868 values is an array of PyObject*. At index i, map[i] is the name of
869 the variable with value values[i]. The function copies the first
870 nmap variable from map/values into dict. If values[i] is NULL,
871 the variable is deleted from dict.
872
873 If deref is true, then the values being copied are cell variables
874 and the value is extracted from the cell variable before being put
875 in dict. If clear is true, then variables in map but not in dict
876 are set to NULL in map; if clear is false, variables missing in
877 dict are ignored.
878
879 Exceptions raised while modifying the dict are silently ignored,
880 because there is no good way to report them.
881*/
882
Guido van Rossum6b356e72001-04-14 17:55:41 +0000883static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000884dict_to_map(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 int deref, int clear)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 Py_ssize_t j;
888 assert(PyTuple_Check(map));
889 assert(PyDict_Check(dict));
890 assert(PyTuple_Size(map) >= nmap);
Raymond Hettingera4d00012018-01-28 09:40:24 -0800891 for (j=0; j < nmap; j++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 PyObject *key = PyTuple_GET_ITEM(map, j);
893 PyObject *value = PyObject_GetItem(dict, key);
894 assert(PyUnicode_Check(key));
895 /* We only care about NULLs if clear is true. */
896 if (value == NULL) {
897 PyErr_Clear();
898 if (!clear)
899 continue;
900 }
901 if (deref) {
902 assert(PyCell_Check(values[j]));
903 if (PyCell_GET(values[j]) != value) {
904 if (PyCell_Set(values[j], value) < 0)
905 PyErr_Clear();
906 }
907 } else if (values[j] != value) {
908 Py_XINCREF(value);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300909 Py_XSETREF(values[j], value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 }
911 Py_XDECREF(value);
912 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000913}
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000914
Victor Stinner41bb43a2013-10-29 01:19:37 +0100915int
916PyFrame_FastToLocalsWithError(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 /* Merge fast locals into f->f_locals */
919 PyObject *locals, *map;
920 PyObject **fast;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 PyCodeObject *co;
922 Py_ssize_t j;
Victor Stinner7a6d7cf2012-10-31 00:37:41 +0100923 Py_ssize_t ncells, nfreevars;
Victor Stinner41bb43a2013-10-29 01:19:37 +0100924
925 if (f == NULL) {
926 PyErr_BadInternalCall();
927 return -1;
928 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 locals = f->f_locals;
930 if (locals == NULL) {
931 locals = f->f_locals = PyDict_New();
Victor Stinner41bb43a2013-10-29 01:19:37 +0100932 if (locals == NULL)
933 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 }
935 co = f->f_code;
936 map = co->co_varnames;
Victor Stinner41bb43a2013-10-29 01:19:37 +0100937 if (!PyTuple_Check(map)) {
938 PyErr_Format(PyExc_SystemError,
939 "co_varnames must be a tuple, not %s",
940 Py_TYPE(map)->tp_name);
941 return -1;
942 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 fast = f->f_localsplus;
944 j = PyTuple_GET_SIZE(map);
945 if (j > co->co_nlocals)
946 j = co->co_nlocals;
Victor Stinner41bb43a2013-10-29 01:19:37 +0100947 if (co->co_nlocals) {
948 if (map_to_dict(map, j, locals, fast, 0) < 0)
949 return -1;
950 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 ncells = PyTuple_GET_SIZE(co->co_cellvars);
952 nfreevars = PyTuple_GET_SIZE(co->co_freevars);
953 if (ncells || nfreevars) {
Victor Stinner41bb43a2013-10-29 01:19:37 +0100954 if (map_to_dict(co->co_cellvars, ncells,
955 locals, fast + co->co_nlocals, 1))
956 return -1;
957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 /* If the namespace is unoptimized, then one of the
959 following cases applies:
960 1. It does not contain free variables, because it
961 uses import * or is a top-level namespace.
962 2. It is a class namespace.
963 We don't want to accidentally copy free variables
964 into the locals dict used by the class.
965 */
966 if (co->co_flags & CO_OPTIMIZED) {
Victor Stinner41bb43a2013-10-29 01:19:37 +0100967 if (map_to_dict(co->co_freevars, nfreevars,
968 locals, fast + co->co_nlocals + ncells, 1) < 0)
969 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 }
971 }
Victor Stinner41bb43a2013-10-29 01:19:37 +0100972 return 0;
973}
974
975void
976PyFrame_FastToLocals(PyFrameObject *f)
977{
978 int res;
979
980 assert(!PyErr_Occurred());
981
982 res = PyFrame_FastToLocalsWithError(f);
983 if (res < 0)
984 PyErr_Clear();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000985}
986
987void
Fred Drake1b190b42000-07-09 05:40:56 +0000988PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 /* Merge f->f_locals into fast locals */
991 PyObject *locals, *map;
992 PyObject **fast;
993 PyObject *error_type, *error_value, *error_traceback;
994 PyCodeObject *co;
995 Py_ssize_t j;
Victor Stinner7a6d7cf2012-10-31 00:37:41 +0100996 Py_ssize_t ncells, nfreevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (f == NULL)
998 return;
999 locals = f->f_locals;
1000 co = f->f_code;
1001 map = co->co_varnames;
1002 if (locals == NULL)
1003 return;
1004 if (!PyTuple_Check(map))
1005 return;
1006 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1007 fast = f->f_localsplus;
1008 j = PyTuple_GET_SIZE(map);
1009 if (j > co->co_nlocals)
1010 j = co->co_nlocals;
1011 if (co->co_nlocals)
1012 dict_to_map(co->co_varnames, j, locals, fast, 0, clear);
1013 ncells = PyTuple_GET_SIZE(co->co_cellvars);
1014 nfreevars = PyTuple_GET_SIZE(co->co_freevars);
1015 if (ncells || nfreevars) {
1016 dict_to_map(co->co_cellvars, ncells,
1017 locals, fast + co->co_nlocals, 1, clear);
1018 /* Same test as in PyFrame_FastToLocals() above. */
1019 if (co->co_flags & CO_OPTIMIZED) {
1020 dict_to_map(co->co_freevars, nfreevars,
1021 locals, fast + co->co_nlocals + ncells, 1,
1022 clear);
1023 }
1024 }
1025 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +00001026}
Guido van Rossum404b95d1997-08-05 02:09:46 +00001027
1028/* Clear out the free list */
Christian Heimesa156e092008-02-16 07:38:31 +00001029int
1030PyFrame_ClearFreeList(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +00001031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 int freelist_size = numfree;
1033
1034 while (free_list != NULL) {
1035 PyFrameObject *f = free_list;
1036 free_list = free_list->f_back;
1037 PyObject_GC_Del(f);
1038 --numfree;
1039 }
1040 assert(numfree == 0);
1041 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +00001042}
1043
1044void
1045PyFrame_Fini(void)
1046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 (void)PyFrame_ClearFreeList();
Guido van Rossum404b95d1997-08-05 02:09:46 +00001048}
David Malcolm49526f42012-06-22 14:55:41 -04001049
1050/* Print summary info about the state of the optimized allocator */
1051void
1052_PyFrame_DebugMallocStats(FILE *out)
1053{
1054 _PyDebugAllocatorStats(out,
1055 "free PyFrameObject",
1056 numfree, sizeof(PyFrameObject));
1057}
1058