blob: 049133406c632bdc5a934a907864eba5809f6b4c [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +00004
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00006#include "frameobject.h"
7#include "opcode.h"
8#include "structmember.h"
9
Neal Norwitz91787cb2002-12-18 23:33:35 +000010#undef MIN
11#undef MAX
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000012#define MIN(a, b) ((a) < (b) ? (a) : (b))
13#define MAX(a, b) ((a) > (b) ? (a) : (b))
14
Guido van Rossum18752471997-04-29 14:49:28 +000015#define OFF(x) offsetof(PyFrameObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000016
Guido van Rossum6f799372001-09-20 20:46:19 +000017static PyMemberDef frame_memberlist[] = {
Guido van Rossum1d5735e1994-08-30 08:27:36 +000018 {"f_back", T_OBJECT, OFF(f_back), RO},
19 {"f_code", T_OBJECT, OFF(f_code), RO},
Guido van Rossumc1134821995-01-10 10:39:16 +000020 {"f_builtins", T_OBJECT, OFF(f_builtins),RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000021 {"f_globals", T_OBJECT, OFF(f_globals), RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000022 {"f_lasti", T_INT, OFF(f_lasti), RO},
Guido van Rossuma027efa1997-05-05 20:56:21 +000023 {"f_exc_type", T_OBJECT, OFF(f_exc_type)},
24 {"f_exc_value", T_OBJECT, OFF(f_exc_value)},
25 {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)},
Guido van Rossum3f5da241990-12-20 15:06:42 +000026 {NULL} /* Sentinel */
27};
28
Guido van Rossum18752471997-04-29 14:49:28 +000029static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000030frame_getlocals(PyFrameObject *f, void *closure)
Guido van Rossum3f5da241990-12-20 15:06:42 +000031{
Tim Peters6d6c1a32001-08-02 04:15:00 +000032 PyFrame_FastToLocals(f);
33 Py_INCREF(f->f_locals);
34 return f->f_locals;
Guido van Rossum3f5da241990-12-20 15:06:42 +000035}
36
Michael W. Hudsondd32a912002-08-15 14:59:02 +000037static PyObject *
38frame_getlineno(PyFrameObject *f, void *closure)
39{
40 int lineno;
41
Michael W. Hudson02ff6a92002-09-11 15:36:32 +000042 if (f->f_trace)
43 lineno = f->f_lineno;
44 else
45 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000046
47 return PyInt_FromLong(lineno);
48}
49
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000050/* Setter for f_lineno - you can set f_lineno from within a trace function in
Jeremy Hylton18623e22007-02-27 16:00:06 +000051 * order to jump to a given line of code, subject to some restrictions. Most
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000052 * lines are OK to jump to because they don't make any assumptions about the
53 * state of the stack (obvious because you could remove the line and the code
54 * would still work without any stack errors), but there are some constructs
55 * that limit jumping:
56 *
57 * o Lines with an 'except' statement on them can't be jumped to, because
58 * they expect an exception to be on the top of the stack.
59 * o Lines that live in a 'finally' block can't be jumped from or to, since
60 * the END_FINALLY expects to clean up the stack after the 'try' block.
61 * o 'try'/'for'/'while' blocks can't be jumped into because the blockstack
62 * needs to be set up before their code runs, and for 'for' loops the
63 * iterator needs to be on the stack.
64 */
65static int
66frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
67{
68 int new_lineno = 0; /* The new value of f_lineno */
69 int new_lasti = 0; /* The new value of f_lasti */
70 int new_iblock = 0; /* The new value of f_iblock */
Kristján Valur Jónsson2f2f5792007-04-13 22:07:33 +000071 unsigned char *code = NULL; /* The bytecode for the frame... */
Martin v. Löwis18e16552006-02-15 17:27:45 +000072 Py_ssize_t code_len = 0; /* ...and its length */
Amaury Forgeot d'Arc99be0812009-06-01 22:04:41 +000073 unsigned char *lnotab = NULL; /* Iterating over co_lnotab */
Martin v. Löwis18e16552006-02-15 17:27:45 +000074 Py_ssize_t lnotab_len = 0; /* (ditto) */
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000075 int offset = 0; /* (ditto) */
76 int line = 0; /* (ditto) */
77 int addr = 0; /* (ditto) */
78 int min_addr = 0; /* Scanning the SETUPs and POPs */
79 int max_addr = 0; /* (ditto) */
80 int delta_iblock = 0; /* (ditto) */
81 int min_delta_iblock = 0; /* (ditto) */
82 int min_iblock = 0; /* (ditto) */
83 int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */
84 int new_lasti_setup_addr = 0; /* (ditto) */
85 int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */
86 int in_finally[CO_MAXBLOCKS]; /* (ditto) */
87 int blockstack_top = 0; /* (ditto) */
Kristján Valur Jónsson2f2f5792007-04-13 22:07:33 +000088 unsigned char setup_op = 0; /* (ditto) */
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000089
90 /* f_lineno must be an integer. */
91 if (!PyInt_Check(p_new_lineno)) {
92 PyErr_SetString(PyExc_ValueError,
93 "lineno must be an integer");
94 return -1;
95 }
96
97 /* You can only do this from within a trace function, not via
98 * _getframe or similar hackery. */
99 if (!f->f_trace)
100 {
101 PyErr_Format(PyExc_ValueError,
102 "f_lineno can only be set by a trace function");
103 return -1;
104 }
105
106 /* Fail if the line comes before the start of the code block. */
107 new_lineno = (int) PyInt_AsLong(p_new_lineno);
108 if (new_lineno < f->f_code->co_firstlineno) {
109 PyErr_Format(PyExc_ValueError,
110 "line %d comes before the current code block",
111 new_lineno);
112 return -1;
113 }
114
115 /* Find the bytecode offset for the start of the given line, or the
116 * first code-owning line after it. */
Amaury Forgeot d'Arc99be0812009-06-01 22:04:41 +0000117 PyString_AsStringAndSize((char*)f->f_code->co_lnotab,
118 &lnotab, &lnotab_len);
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000119 addr = 0;
120 line = f->f_code->co_firstlineno;
121 new_lasti = -1;
122 for (offset = 0; offset < lnotab_len; offset += 2) {
123 addr += lnotab[offset];
124 line += lnotab[offset+1];
125 if (line >= new_lineno) {
126 new_lasti = addr;
127 new_lineno = line;
128 break;
129 }
130 }
131
132 /* If we didn't reach the requested line, return an error. */
133 if (new_lasti == -1) {
134 PyErr_Format(PyExc_ValueError,
135 "line %d comes after the current code block",
136 new_lineno);
137 return -1;
138 }
139
140 /* We're now ready to look at the bytecode. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000141 PyString_AsStringAndSize(f->f_code->co_code, (char **)&code, &code_len);
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000142 min_addr = MIN(new_lasti, f->f_lasti);
143 max_addr = MAX(new_lasti, f->f_lasti);
144
145 /* You can't jump onto a line with an 'except' statement on it -
146 * they expect to have an exception on the top of the stack, which
147 * won't be true if you jump to them. They always start with code
148 * that either pops the exception using POP_TOP (plain 'except:'
149 * lines do this) or duplicates the exception on the stack using
150 * DUP_TOP (if there's an exception type specified). See compile.c,
151 * 'com_try_except' for the full details. There aren't any other
152 * cases (AFAIK) where a line's code can start with DUP_TOP or
153 * POP_TOP, but if any ever appear, they'll be subject to the same
154 * restriction (but with a different error message). */
155 if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
156 PyErr_SetString(PyExc_ValueError,
157 "can't jump to 'except' line as there's no exception");
158 return -1;
159 }
160
161 /* You can't jump into or out of a 'finally' block because the 'try'
162 * block leaves something on the stack for the END_FINALLY to clean
Jeremy Hylton18623e22007-02-27 16:00:06 +0000163 * up. So we walk the bytecode, maintaining a simulated blockstack.
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000164 * When we reach the old or new address and it's in a 'finally' block
165 * we note the address of the corresponding SETUP_FINALLY. The jump
166 * is only legal if neither address is in a 'finally' block or
167 * they're both in the same one. 'blockstack' is a stack of the
168 * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks
169 * whether we're in a 'finally' block at each blockstack level. */
170 f_lasti_setup_addr = -1;
171 new_lasti_setup_addr = -1;
172 memset(blockstack, '\0', sizeof(blockstack));
173 memset(in_finally, '\0', sizeof(in_finally));
174 blockstack_top = 0;
175 for (addr = 0; addr < code_len; addr++) {
176 unsigned char op = code[addr];
177 switch (op) {
178 case SETUP_LOOP:
179 case SETUP_EXCEPT:
180 case SETUP_FINALLY:
181 blockstack[blockstack_top++] = addr;
182 in_finally[blockstack_top-1] = 0;
183 break;
184
185 case POP_BLOCK:
Neal Norwitzee65e222002-12-19 18:16:57 +0000186 assert(blockstack_top > 0);
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000187 setup_op = code[blockstack[blockstack_top-1]];
188 if (setup_op == SETUP_FINALLY) {
189 in_finally[blockstack_top-1] = 1;
190 }
191 else {
192 blockstack_top--;
193 }
194 break;
195
196 case END_FINALLY:
197 /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist
198 * in the bytecode but don't correspond to an actual
Neal Norwitzee65e222002-12-19 18:16:57 +0000199 * 'finally' block. (If blockstack_top is 0, we must
200 * be seeing such an END_FINALLY.) */
201 if (blockstack_top > 0) {
202 setup_op = code[blockstack[blockstack_top-1]];
203 if (setup_op == SETUP_FINALLY) {
204 blockstack_top--;
205 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000206 }
207 break;
208 }
209
210 /* For the addresses we're interested in, see whether they're
211 * within a 'finally' block and if so, remember the address
212 * of the SETUP_FINALLY. */
213 if (addr == new_lasti || addr == f->f_lasti) {
214 int i = 0;
215 int setup_addr = -1;
216 for (i = blockstack_top-1; i >= 0; i--) {
217 if (in_finally[i]) {
218 setup_addr = blockstack[i];
219 break;
220 }
221 }
222
223 if (setup_addr != -1) {
224 if (addr == new_lasti) {
225 new_lasti_setup_addr = setup_addr;
226 }
227
228 if (addr == f->f_lasti) {
229 f_lasti_setup_addr = setup_addr;
230 }
231 }
232 }
233
234 if (op >= HAVE_ARGUMENT) {
235 addr += 2;
236 }
237 }
238
Neal Norwitzee65e222002-12-19 18:16:57 +0000239 /* Verify that the blockstack tracking code didn't get lost. */
240 assert(blockstack_top == 0);
241
242 /* After all that, are we jumping into / out of a 'finally' block? */
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000243 if (new_lasti_setup_addr != f_lasti_setup_addr) {
244 PyErr_SetString(PyExc_ValueError,
245 "can't jump into or out of a 'finally' block");
246 return -1;
247 }
248
249
250 /* Police block-jumping (you can't jump into the middle of a block)
251 * and ensure that the blockstack finishes up in a sensible state (by
252 * popping any blocks we're jumping out of). We look at all the
253 * blockstack operations between the current position and the new
254 * one, and keep track of how many blocks we drop out of on the way.
255 * By also keeping track of the lowest blockstack position we see, we
256 * can tell whether the jump goes into any blocks without coming out
257 * again - in that case we raise an exception below. */
258 delta_iblock = 0;
259 for (addr = min_addr; addr < max_addr; addr++) {
260 unsigned char op = code[addr];
261 switch (op) {
262 case SETUP_LOOP:
263 case SETUP_EXCEPT:
264 case SETUP_FINALLY:
265 delta_iblock++;
266 break;
267
268 case POP_BLOCK:
269 delta_iblock--;
270 break;
271 }
272
273 min_delta_iblock = MIN(min_delta_iblock, delta_iblock);
274
275 if (op >= HAVE_ARGUMENT) {
276 addr += 2;
277 }
278 }
279
280 /* Derive the absolute iblock values from the deltas. */
281 min_iblock = f->f_iblock + min_delta_iblock;
282 if (new_lasti > f->f_lasti) {
283 /* Forwards jump. */
284 new_iblock = f->f_iblock + delta_iblock;
285 }
286 else {
287 /* Backwards jump. */
288 new_iblock = f->f_iblock - delta_iblock;
289 }
290
291 /* Are we jumping into a block? */
292 if (new_iblock > min_iblock) {
293 PyErr_SetString(PyExc_ValueError,
294 "can't jump into the middle of a block");
295 return -1;
296 }
297
298 /* Pop any blocks that we're jumping out of. */
299 while (f->f_iblock > new_iblock) {
300 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
301 while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
302 PyObject *v = (*--f->f_stacktop);
303 Py_DECREF(v);
304 }
305 }
306
307 /* Finally set the new f_lineno and f_lasti and return OK. */
308 f->f_lineno = new_lineno;
309 f->f_lasti = new_lasti;
310 return 0;
311}
312
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000313static PyObject *
314frame_gettrace(PyFrameObject *f, void *closure)
315{
316 PyObject* trace = f->f_trace;
317
318 if (trace == NULL)
319 trace = Py_None;
320
321 Py_INCREF(trace);
322
323 return trace;
324}
325
326static int
327frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
328{
329 /* We rely on f_lineno being accurate when f_trace is set. */
330
331 PyObject* old_value = f->f_trace;
332
333 Py_XINCREF(v);
334 f->f_trace = v;
Tim Petersa13131c2006-04-15 03:15:24 +0000335
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000336 if (v != NULL)
337 f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
338
339 Py_XDECREF(old_value);
340
341 return 0;
342}
343
Neal Norwitzb9845e72006-06-12 02:11:18 +0000344static PyObject *
345frame_getrestricted(PyFrameObject *f, void *closure)
346{
347 return PyBool_FromLong(PyFrame_IsRestricted(f));
348}
349
Guido van Rossum32d34c82001-09-20 21:45:26 +0000350static PyGetSetDef frame_getsetlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000351 {"f_locals", (getter)frame_getlocals, NULL, NULL},
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000352 {"f_lineno", (getter)frame_getlineno,
353 (setter)frame_setlineno, NULL},
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000354 {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
Neal Norwitzb9845e72006-06-12 02:11:18 +0000355 {"f_restricted",(getter)frame_getrestricted,NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000356 {0}
357};
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000358
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000359/* Stack frames are allocated and deallocated at a considerable rate.
Richard Jones7c88dcc2006-05-23 10:37:38 +0000360 In an attempt to improve the speed of function calls, we:
361
362 1. Hold a single "zombie" frame on each code object. This retains
363 the allocated and initialised frame object from an invocation of
364 the code object. The zombie is reanimated the next time we need a
365 frame object for that code object. Doing this saves the malloc/
366 realloc required when using a free_list frame that isn't the
367 correct size. It also saves some field initialisation.
368
369 In zombie mode, no field of PyFrameObject holds a reference, but
370 the following fields are still valid:
371
Richard Jonesa3727112006-05-23 18:32:11 +0000372 * ob_type, ob_size, f_code, f_valuestack;
Richard Jones7c88dcc2006-05-23 10:37:38 +0000373
374 * f_locals, f_trace,
375 f_exc_type, f_exc_value, f_exc_traceback are NULL;
376
377 * f_localsplus does not require re-allocation and
378 the local variables in f_localsplus are NULL.
379
380 2. We also maintain a separate free list of stack frames (just like
381 integers are allocated in a special way -- see intobject.c). When
382 a stack frame is on the free list, only the following members have
383 a meaning:
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000384 ob_type == &Frametype
385 f_back next item on free list, or NULL
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000386 f_stacksize size of value stack
Jeremy Hylton18623e22007-02-27 16:00:06 +0000387 ob_size size of localsplus
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000388 Note that the value and block stacks are preserved -- this can save
389 another malloc() call or two (and two free() calls as well!).
390 Also note that, unlike for integers, each frame object is a
391 malloc'ed object in its own right -- it is only the actual calls to
392 malloc() that we are trying to save here, not the administration.
393 After all, while a typical program may make millions of calls, a
394 call depth of more than 20 or 30 is probably already exceptional
395 unless the program contains run-away recursion. I hope.
Tim Petersb7ba7432002-04-13 05:21:47 +0000396
Christian Heimes5b970ad2008-02-06 13:33:44 +0000397 Later, PyFrame_MAXFREELIST was added to bound the # of frames saved on
Tim Petersb7ba7432002-04-13 05:21:47 +0000398 free_list. Else programs creating lots of cyclic trash involving
399 frames could provoke free_list into growing without bound.
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000400*/
401
Guido van Rossum18752471997-04-29 14:49:28 +0000402static PyFrameObject *free_list = NULL;
Tim Petersb7ba7432002-04-13 05:21:47 +0000403static int numfree = 0; /* number of frames currently in free_list */
Christian Heimes5b970ad2008-02-06 13:33:44 +0000404/* max value for numfree */
405#define PyFrame_MAXFREELIST 200
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000406
Guido van Rossum3f5da241990-12-20 15:06:42 +0000407static void
Fred Drake1b190b42000-07-09 05:40:56 +0000408frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000409{
Richard Jones7c88dcc2006-05-23 10:37:38 +0000410 PyObject **p, **valuestack;
411 PyCodeObject *co;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000412
Jeremy Hylton18623e22007-02-27 16:00:06 +0000413 PyObject_GC_UnTrack(f);
Guido van Rossumd724b232000-03-13 16:01:29 +0000414 Py_TRASHCAN_SAFE_BEGIN(f)
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000415 /* Kill all local variables */
Jeremy Hylton18623e22007-02-27 16:00:06 +0000416 valuestack = f->f_valuestack;
417 for (p = f->f_localsplus; p < valuestack; p++)
418 Py_CLEAR(*p);
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000419
Tim Peters5ca576e2001-06-18 22:08:13 +0000420 /* Free stack */
Tim Peters8c963692001-06-23 05:26:56 +0000421 if (f->f_stacktop != NULL) {
Richard Jones7c88dcc2006-05-23 10:37:38 +0000422 for (p = valuestack; p < f->f_stacktop; p++)
Tim Peters8c963692001-06-23 05:26:56 +0000423 Py_XDECREF(*p);
Tim Peters5ca576e2001-06-18 22:08:13 +0000424 }
Tim Petersa13131c2006-04-15 03:15:24 +0000425
Guido van Rossum18752471997-04-29 14:49:28 +0000426 Py_XDECREF(f->f_back);
Neal Norwitzc91ed402002-12-30 22:29:22 +0000427 Py_DECREF(f->f_builtins);
428 Py_DECREF(f->f_globals);
Richard Jones7c88dcc2006-05-23 10:37:38 +0000429 Py_CLEAR(f->f_locals);
430 Py_CLEAR(f->f_trace);
431 Py_CLEAR(f->f_exc_type);
432 Py_CLEAR(f->f_exc_value);
433 Py_CLEAR(f->f_exc_traceback);
434
Jeremy Hylton18623e22007-02-27 16:00:06 +0000435 co = f->f_code;
436 if (co->co_zombieframe == NULL)
437 co->co_zombieframe = f;
Christian Heimes5b970ad2008-02-06 13:33:44 +0000438 else if (numfree < PyFrame_MAXFREELIST) {
Tim Petersb7ba7432002-04-13 05:21:47 +0000439 ++numfree;
440 f->f_back = free_list;
441 free_list = f;
Jeremy Hylton18623e22007-02-27 16:00:06 +0000442 }
Richard Jones7c88dcc2006-05-23 10:37:38 +0000443 else
Tim Petersb7ba7432002-04-13 05:21:47 +0000444 PyObject_GC_Del(f);
Richard Jones7c88dcc2006-05-23 10:37:38 +0000445
Jeremy Hylton18623e22007-02-27 16:00:06 +0000446 Py_DECREF(co);
Guido van Rossumd724b232000-03-13 16:01:29 +0000447 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000448}
449
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000450static int
451frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
452{
453 PyObject **fastlocals, **p;
Tim Petersde2acf62006-04-15 03:22:46 +0000454 int i, slots;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000455
Tim Petersde2acf62006-04-15 03:22:46 +0000456 Py_VISIT(f->f_back);
457 Py_VISIT(f->f_code);
458 Py_VISIT(f->f_builtins);
459 Py_VISIT(f->f_globals);
460 Py_VISIT(f->f_locals);
461 Py_VISIT(f->f_trace);
462 Py_VISIT(f->f_exc_type);
463 Py_VISIT(f->f_exc_value);
464 Py_VISIT(f->f_exc_traceback);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000465
466 /* locals */
Richard Jonescebbefc2006-05-23 18:28:17 +0000467 slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000468 fastlocals = f->f_localsplus;
Tim Petersde2acf62006-04-15 03:22:46 +0000469 for (i = slots; --i >= 0; ++fastlocals)
470 Py_VISIT(*fastlocals);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000471
472 /* stack */
473 if (f->f_stacktop != NULL) {
474 for (p = f->f_valuestack; p < f->f_stacktop; p++)
Tim Petersde2acf62006-04-15 03:22:46 +0000475 Py_VISIT(*p);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000476 }
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000477 return 0;
478}
479
480static void
481frame_clear(PyFrameObject *f)
482{
Phillip J. Eby8ebb28d2006-04-15 01:02:17 +0000483 PyObject **fastlocals, **p, **oldtop;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000484 int i, slots;
485
Tim Petersa13131c2006-04-15 03:15:24 +0000486 /* Before anything else, make sure that this frame is clearly marked
Jeremy Hylton18623e22007-02-27 16:00:06 +0000487 * as being defunct! Else, e.g., a generator reachable from this
488 * frame may also point to this frame, believe itself to still be
489 * active, and try cleaning up this frame again.
490 */
Tim Petersadcd25e2006-04-15 03:30:08 +0000491 oldtop = f->f_stacktop;
Jeremy Hylton18623e22007-02-27 16:00:06 +0000492 f->f_stacktop = NULL;
Phillip J. Eby8ebb28d2006-04-15 01:02:17 +0000493
Tim Petersadcd25e2006-04-15 03:30:08 +0000494 Py_CLEAR(f->f_exc_type);
495 Py_CLEAR(f->f_exc_value);
496 Py_CLEAR(f->f_exc_traceback);
497 Py_CLEAR(f->f_trace);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000498
499 /* locals */
Richard Jonescebbefc2006-05-23 18:28:17 +0000500 slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000501 fastlocals = f->f_localsplus;
Tim Petersadcd25e2006-04-15 03:30:08 +0000502 for (i = slots; --i >= 0; ++fastlocals)
Phillip J. Eby8ebb28d2006-04-15 01:02:17 +0000503 Py_CLEAR(*fastlocals);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000504
505 /* stack */
Phillip J. Eby8ebb28d2006-04-15 01:02:17 +0000506 if (oldtop != NULL) {
Tim Petersadcd25e2006-04-15 03:30:08 +0000507 for (p = f->f_valuestack; p < oldtop; p++)
Phillip J. Eby8ebb28d2006-04-15 01:02:17 +0000508 Py_CLEAR(*p);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000509 }
510}
511
Robert Schuppenies9be2ec12008-07-10 15:24:04 +0000512static PyObject *
513frame_sizeof(PyFrameObject *f)
514{
515 Py_ssize_t res, extras, ncells, nfrees;
516
517 ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
518 nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
519 extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
520 ncells + nfrees;
Christian Heimesc7f315a2008-10-02 18:39:50 +0000521 /* subtract one as it is already included in PyFrameObject */
Robert Schuppenies9be2ec12008-07-10 15:24:04 +0000522 res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);
523
524 return PyInt_FromSsize_t(res);
525}
526
527PyDoc_STRVAR(sizeof__doc__,
528"F.__sizeof__() -> size of F in memory, in bytes");
529
530static PyMethodDef frame_methods[] = {
531 {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
532 sizeof__doc__},
533 {NULL, NULL} /* sentinel */
534};
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000535
Guido van Rossum18752471997-04-29 14:49:28 +0000536PyTypeObject PyFrame_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000537 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000538 "frame",
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000539 sizeof(PyFrameObject),
540 sizeof(PyObject *),
Jeremy Hylton18623e22007-02-27 16:00:06 +0000541 (destructor)frame_dealloc, /* tp_dealloc */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000542 0, /* tp_print */
Jeremy Hylton18623e22007-02-27 16:00:06 +0000543 0, /* tp_getattr */
544 0, /* tp_setattr */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000545 0, /* tp_compare */
546 0, /* tp_repr */
547 0, /* tp_as_number */
548 0, /* tp_as_sequence */
549 0, /* tp_as_mapping */
550 0, /* tp_hash */
551 0, /* tp_call */
552 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000553 PyObject_GenericGetAttr, /* tp_getattro */
554 PyObject_GenericSetAttr, /* tp_setattro */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000555 0, /* tp_as_buffer */
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000556 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Jeremy Hylton18623e22007-02-27 16:00:06 +0000557 0, /* tp_doc */
558 (traverseproc)frame_traverse, /* tp_traverse */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000559 (inquiry)frame_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000560 0, /* tp_richcompare */
561 0, /* tp_weaklistoffset */
562 0, /* tp_iter */
563 0, /* tp_iternext */
Robert Schuppenies9be2ec12008-07-10 15:24:04 +0000564 frame_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000565 frame_memberlist, /* tp_members */
566 frame_getsetlist, /* tp_getset */
567 0, /* tp_base */
568 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000569};
570
Neal Norwitzc91ed402002-12-30 22:29:22 +0000571static PyObject *builtin_object;
572
Neal Norwitzb2501f42002-12-31 03:42:13 +0000573int _PyFrame_Init()
Neal Norwitzc91ed402002-12-30 22:29:22 +0000574{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000575 builtin_object = PyString_InternFromString("__builtins__");
Benjamin Peterson53f098c2009-03-18 21:49:29 +0000576 if (builtin_object == NULL)
577 return 0;
Benjamin Peterson53f098c2009-03-18 21:49:29 +0000578 return 1;
Neal Norwitzc91ed402002-12-30 22:29:22 +0000579}
580
Guido van Rossum18752471997-04-29 14:49:28 +0000581PyFrameObject *
Tim Petersa13131c2006-04-15 03:15:24 +0000582PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000583 PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000584{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000585 PyFrameObject *back = tstate->frame;
Guido van Rossum18752471997-04-29 14:49:28 +0000586 PyFrameObject *f;
587 PyObject *builtins;
Richard Jones7c88dcc2006-05-23 10:37:38 +0000588 Py_ssize_t i;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000589
Michael W. Hudson69734a52002-08-19 16:54:08 +0000590#ifdef Py_DEBUG
591 if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000592 (locals != NULL && !PyMapping_Check(locals))) {
Guido van Rossum18752471997-04-29 14:49:28 +0000593 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000594 return NULL;
595 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000596#endif
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000597 if (back == NULL || back->f_globals != globals) {
598 builtins = PyDict_GetItem(globals, builtin_object);
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000599 if (builtins) {
600 if (PyModule_Check(builtins)) {
601 builtins = PyModule_GetDict(builtins);
602 assert(!builtins || PyDict_Check(builtins));
603 }
604 else if (!PyDict_Check(builtins))
605 builtins = NULL;
606 }
607 if (builtins == NULL) {
Jeremy Hylton18623e22007-02-27 16:00:06 +0000608 /* No builtins! Make up a minimal one
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000609 Give them 'None', at least. */
610 builtins = PyDict_New();
Tim Petersa13131c2006-04-15 03:15:24 +0000611 if (builtins == NULL ||
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000612 PyDict_SetItemString(
613 builtins, "None", Py_None) < 0)
614 return NULL;
615 }
616 else
617 Py_INCREF(builtins);
618
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000619 }
620 else {
621 /* If we share the globals, we share the builtins.
622 Save a lookup and a call. */
623 builtins = back->f_builtins;
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000624 assert(builtins != NULL && PyDict_Check(builtins));
625 Py_INCREF(builtins);
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000626 }
Richard Jones7c88dcc2006-05-23 10:37:38 +0000627 if (code->co_zombieframe != NULL) {
Jeremy Hylton18623e22007-02-27 16:00:06 +0000628 f = code->co_zombieframe;
629 code->co_zombieframe = NULL;
630 _Py_NewReference((PyObject *)f);
631 assert(f->f_code == code);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000632 }
Jeremy Hylton18623e22007-02-27 16:00:06 +0000633 else {
634 Py_ssize_t extras, ncells, nfrees;
635 ncells = PyTuple_GET_SIZE(code->co_cellvars);
636 nfrees = PyTuple_GET_SIZE(code->co_freevars);
637 extras = code->co_stacksize + code->co_nlocals + ncells +
638 nfrees;
639 if (free_list == NULL) {
640 f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type,
641 extras);
642 if (f == NULL) {
643 Py_DECREF(builtins);
644 return NULL;
645 }
646 }
647 else {
648 assert(numfree > 0);
649 --numfree;
650 f = free_list;
651 free_list = free_list->f_back;
Christian Heimese93237d2007-12-19 02:37:44 +0000652 if (Py_SIZE(f) < extras) {
Jeremy Hylton18623e22007-02-27 16:00:06 +0000653 f = PyObject_GC_Resize(PyFrameObject, f, extras);
654 if (f == NULL) {
655 Py_DECREF(builtins);
656 return NULL;
657 }
658 }
659 _Py_NewReference((PyObject *)f);
660 }
Richard Jones7c88dcc2006-05-23 10:37:38 +0000661
662 f->f_code = code;
Richard Jonesa3727112006-05-23 18:32:11 +0000663 extras = code->co_nlocals + ncells + nfrees;
Richard Jones7c88dcc2006-05-23 10:37:38 +0000664 f->f_valuestack = f->f_localsplus + extras;
665 for (i=0; i<extras; i++)
666 f->f_localsplus[i] = NULL;
667 f->f_locals = NULL;
668 f->f_trace = NULL;
Jeremy Hylton18623e22007-02-27 16:00:06 +0000669 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000670 }
Neal Norwitz1adbb502006-07-21 05:31:02 +0000671 f->f_stacktop = f->f_valuestack;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000672 f->f_builtins = builtins;
Guido van Rossum18752471997-04-29 14:49:28 +0000673 Py_XINCREF(back);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000674 f->f_back = back;
Guido van Rossum18752471997-04-29 14:49:28 +0000675 Py_INCREF(code);
Guido van Rossum18752471997-04-29 14:49:28 +0000676 Py_INCREF(globals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000677 f->f_globals = globals;
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000678 /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
Tim Petersa13131c2006-04-15 03:15:24 +0000679 if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000680 (CO_NEWLOCALS | CO_OPTIMIZED))
Richard Jones7c88dcc2006-05-23 10:37:38 +0000681 ; /* f_locals = NULL; will be set by PyFrame_FastToLocals() */
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000682 else if (code->co_flags & CO_NEWLOCALS) {
683 locals = PyDict_New();
684 if (locals == NULL) {
685 Py_DECREF(f);
686 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000687 }
Jeremy Hylton18623e22007-02-27 16:00:06 +0000688 f->f_locals = locals;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000689 }
Guido van Rossum2271bf71995-07-18 14:30:34 +0000690 else {
691 if (locals == NULL)
692 locals = globals;
Guido van Rossum18752471997-04-29 14:49:28 +0000693 Py_INCREF(locals);
Jeremy Hylton18623e22007-02-27 16:00:06 +0000694 f->f_locals = locals;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000695 }
Guido van Rossumeb46d671997-08-02 02:59:08 +0000696 f->f_tstate = tstate;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000697
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000698 f->f_lasti = -1;
Guido van Rossum747596a1997-01-24 04:00:21 +0000699 f->f_lineno = code->co_firstlineno;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000700 f->f_iblock = 0;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000701
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000702 _PyObject_GC_TRACK(f);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000703 return f;
704}
705
Guido van Rossum3f5da241990-12-20 15:06:42 +0000706/* Block management */
707
708void
Fred Drake1b190b42000-07-09 05:40:56 +0000709PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000710{
Guido van Rossum18752471997-04-29 14:49:28 +0000711 PyTryBlock *b;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000712 if (f->f_iblock >= CO_MAXBLOCKS)
Guido van Rossum18752471997-04-29 14:49:28 +0000713 Py_FatalError("XXX block stack overflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000714 b = &f->f_blockstack[f->f_iblock++];
715 b->b_type = type;
716 b->b_level = level;
717 b->b_handler = handler;
718}
719
Guido van Rossum18752471997-04-29 14:49:28 +0000720PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000721PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000722{
Guido van Rossum18752471997-04-29 14:49:28 +0000723 PyTryBlock *b;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000724 if (f->f_iblock <= 0)
Guido van Rossum18752471997-04-29 14:49:28 +0000725 Py_FatalError("XXX block stack underflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000726 b = &f->f_blockstack[--f->f_iblock];
727 return b;
728}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000729
Jeremy Hylton759410b2007-02-26 18:41:18 +0000730/* Convert between "fast" version of locals and dictionary version.
731
Jeremy Hylton18623e22007-02-27 16:00:06 +0000732 map and values are input arguments. map is a tuple of strings.
Jeremy Hylton759410b2007-02-26 18:41:18 +0000733 values is an array of PyObject*. At index i, map[i] is the name of
734 the variable with value values[i]. The function copies the first
735 nmap variable from map/values into dict. If values[i] is NULL,
736 the variable is deleted from dict.
737
738 If deref is true, then the values being copied are cell variables
739 and the value is extracted from the cell variable before being put
740 in dict.
741
742 Exceptions raised while modifying the dict are silently ignored,
743 because there is no good way to report them.
744 */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000745
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000746static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000747map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
Jeremy Hylton759410b2007-02-26 18:41:18 +0000748 int deref)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000749{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000750 Py_ssize_t j;
Jeremy Hylton18623e22007-02-27 16:00:06 +0000751 assert(PyTuple_Check(map));
752 assert(PyDict_Check(dict));
753 assert(PyTuple_Size(map) >= nmap);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000754 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000755 PyObject *key = PyTuple_GET_ITEM(map, j);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000756 PyObject *value = values[j];
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000757 assert(PyString_Check(key));
Jeremy Hylton759410b2007-02-26 18:41:18 +0000758 if (deref) {
Jeremy Hylton18623e22007-02-27 16:00:06 +0000759 assert(PyCell_Check(value));
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000760 value = PyCell_GET(value);
Jeremy Hylton18623e22007-02-27 16:00:06 +0000761 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000762 if (value == NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000763 if (PyObject_DelItem(dict, key) != 0)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000764 PyErr_Clear();
765 }
766 else {
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000767 if (PyObject_SetItem(dict, key, value) != 0)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000768 PyErr_Clear();
769 }
770 }
771}
772
Jeremy Hylton759410b2007-02-26 18:41:18 +0000773/* Copy values from the "locals" dict into the fast locals.
774
775 dict is an input argument containing string keys representing
776 variables names and arbitrary PyObject* as values.
777
Jeremy Hylton18623e22007-02-27 16:00:06 +0000778 map and values are input arguments. map is a tuple of strings.
Jeremy Hylton759410b2007-02-26 18:41:18 +0000779 values is an array of PyObject*. At index i, map[i] is the name of
780 the variable with value values[i]. The function copies the first
781 nmap variable from map/values into dict. If values[i] is NULL,
782 the variable is deleted from dict.
783
784 If deref is true, then the values being copied are cell variables
785 and the value is extracted from the cell variable before being put
786 in dict. If clear is true, then variables in map but not in dict
787 are set to NULL in map; if clear is false, variables missing in
788 dict are ignored.
789
790 Exceptions raised while modifying the dict are silently ignored,
791 because there is no good way to report them.
792*/
793
Guido van Rossum6b356e72001-04-14 17:55:41 +0000794static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000795dict_to_map(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
Jeremy Hylton759410b2007-02-26 18:41:18 +0000796 int deref, int clear)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000797{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000798 Py_ssize_t j;
Jeremy Hylton18623e22007-02-27 16:00:06 +0000799 assert(PyTuple_Check(map));
800 assert(PyDict_Check(dict));
801 assert(PyTuple_Size(map) >= nmap);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000802 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000803 PyObject *key = PyTuple_GET_ITEM(map, j);
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000804 PyObject *value = PyObject_GetItem(dict, key);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000805 assert(PyString_Check(key));
Jeremy Hylton18623e22007-02-27 16:00:06 +0000806 /* We only care about NULLs if clear is true. */
Jeremy Hylton759410b2007-02-26 18:41:18 +0000807 if (value == NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000808 PyErr_Clear();
Jeremy Hylton18623e22007-02-27 16:00:06 +0000809 if (!clear)
810 continue;
811 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000812 if (deref) {
Jeremy Hylton18623e22007-02-27 16:00:06 +0000813 assert(PyCell_Check(values[j]));
814 if (PyCell_GET(values[j]) != value) {
815 if (PyCell_Set(values[j], value) < 0)
816 PyErr_Clear();
817 }
Jeremy Hylton759410b2007-02-26 18:41:18 +0000818 } else if (values[j] != value) {
Jeremy Hylton18623e22007-02-27 16:00:06 +0000819 Py_XINCREF(value);
820 Py_XDECREF(values[j]);
821 values[j] = value;
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000822 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000823 Py_XDECREF(value);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000824 }
825}
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000826
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000827void
Fred Drake1b190b42000-07-09 05:40:56 +0000828PyFrame_FastToLocals(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000829{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000830 /* Merge fast locals into f->f_locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000831 PyObject *locals, *map;
832 PyObject **fast;
833 PyObject *error_type, *error_value, *error_traceback;
Richard Jonescebbefc2006-05-23 18:28:17 +0000834 PyCodeObject *co;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000835 Py_ssize_t j;
Jeremy Hylton18623e22007-02-27 16:00:06 +0000836 int ncells, nfreevars;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000837 if (f == NULL)
838 return;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000839 locals = f->f_locals;
840 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000841 locals = f->f_locals = PyDict_New();
Guido van Rossum2271bf71995-07-18 14:30:34 +0000842 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000843 PyErr_Clear(); /* Can't report it :-( */
Guido van Rossum2271bf71995-07-18 14:30:34 +0000844 return;
845 }
846 }
Richard Jonescebbefc2006-05-23 18:28:17 +0000847 co = f->f_code;
848 map = co->co_varnames;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000849 if (!PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000850 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000851 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000852 fast = f->f_localsplus;
Neal Norwitz2aa9a5d2006-03-20 01:53:23 +0000853 j = PyTuple_GET_SIZE(map);
Richard Jonescebbefc2006-05-23 18:28:17 +0000854 if (j > co->co_nlocals)
855 j = co->co_nlocals;
856 if (co->co_nlocals)
Jeremy Hylton174d2762003-10-21 18:10:28 +0000857 map_to_dict(map, j, locals, fast, 0);
Richard Jonescebbefc2006-05-23 18:28:17 +0000858 ncells = PyTuple_GET_SIZE(co->co_cellvars);
859 nfreevars = PyTuple_GET_SIZE(co->co_freevars);
860 if (ncells || nfreevars) {
861 map_to_dict(co->co_cellvars, ncells,
862 locals, fast + co->co_nlocals, 1);
Jeremy Hylton18623e22007-02-27 16:00:06 +0000863 /* If the namespace is unoptimized, then one of the
864 following cases applies:
865 1. It does not contain free variables, because it
866 uses import * or is a top-level namespace.
867 2. It is a class namespace.
868 We don't want to accidentally copy free variables
869 into the locals dict used by the class.
870 */
871 if (co->co_flags & CO_OPTIMIZED) {
872 map_to_dict(co->co_freevars, nfreevars,
873 locals, fast + co->co_nlocals + ncells, 1);
874 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000875 }
Guido van Rossum18752471997-04-29 14:49:28 +0000876 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000877}
878
879void
Fred Drake1b190b42000-07-09 05:40:56 +0000880PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000881{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000882 /* Merge f->f_locals into fast locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000883 PyObject *locals, *map;
884 PyObject **fast;
885 PyObject *error_type, *error_value, *error_traceback;
Richard Jonescebbefc2006-05-23 18:28:17 +0000886 PyCodeObject *co;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000887 Py_ssize_t j;
Richard Jonescebbefc2006-05-23 18:28:17 +0000888 int ncells, nfreevars;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000889 if (f == NULL)
890 return;
891 locals = f->f_locals;
Richard Jonescebbefc2006-05-23 18:28:17 +0000892 co = f->f_code;
893 map = co->co_varnames;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000894 if (locals == NULL)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000895 return;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000896 if (!PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000897 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000898 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000899 fast = f->f_localsplus;
Neal Norwitz2aa9a5d2006-03-20 01:53:23 +0000900 j = PyTuple_GET_SIZE(map);
Richard Jonescebbefc2006-05-23 18:28:17 +0000901 if (j > co->co_nlocals)
902 j = co->co_nlocals;
903 if (co->co_nlocals)
904 dict_to_map(co->co_varnames, j, locals, fast, 0, clear);
905 ncells = PyTuple_GET_SIZE(co->co_cellvars);
906 nfreevars = PyTuple_GET_SIZE(co->co_freevars);
907 if (ncells || nfreevars) {
908 dict_to_map(co->co_cellvars, ncells,
909 locals, fast + co->co_nlocals, 1, clear);
Amaury Forgeot d'Arce4921fe2008-07-21 22:00:38 +0000910 /* Same test as in PyFrame_FastToLocals() above. */
911 if (co->co_flags & CO_OPTIMIZED) {
912 dict_to_map(co->co_freevars, nfreevars,
913 locals, fast + co->co_nlocals + ncells, 1,
914 clear);
915 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000916 }
Guido van Rossum18752471997-04-29 14:49:28 +0000917 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000918}
Guido van Rossum404b95d1997-08-05 02:09:46 +0000919
920/* Clear out the free list */
Christian Heimes3b718a72008-02-14 12:47:33 +0000921int
922PyFrame_ClearFreeList(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +0000923{
Christian Heimes3b718a72008-02-14 12:47:33 +0000924 int freelist_size = numfree;
925
Guido van Rossum404b95d1997-08-05 02:09:46 +0000926 while (free_list != NULL) {
927 PyFrameObject *f = free_list;
928 free_list = free_list->f_back;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000929 PyObject_GC_Del(f);
Tim Petersb7ba7432002-04-13 05:21:47 +0000930 --numfree;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000931 }
Tim Petersb7ba7432002-04-13 05:21:47 +0000932 assert(numfree == 0);
Christian Heimes3b718a72008-02-14 12:47:33 +0000933 return freelist_size;
934}
935
936void
937PyFrame_Fini(void)
938{
939 (void)PyFrame_ClearFreeList();
Neal Norwitzc91ed402002-12-30 22:29:22 +0000940 Py_XDECREF(builtin_object);
941 builtin_object = NULL;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000942}