blob: 9b05b9d09303e333e9ee88e3da0c3d0b62610135 [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[] = {
Nick Coghlan1f7ce622012-01-13 21:43:40 +100018 {"f_back", T_OBJECT, OFF(f_back), READONLY},
19 {"f_code", T_OBJECT, OFF(f_code), READONLY},
20 {"f_builtins", T_OBJECT, OFF(f_builtins), READONLY},
21 {"f_globals", T_OBJECT, OFF(f_globals), READONLY},
22 {"f_lasti", T_INT, OFF(f_lasti), READONLY},
23 {"f_yieldfrom", T_OBJECT, OFF(f_yieldfrom), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +000025};
26
Guido van Rossum18752471997-04-29 14:49:28 +000027static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000028frame_getlocals(PyFrameObject *f, void *closure)
Guido van Rossum3f5da241990-12-20 15:06:42 +000029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PyFrame_FastToLocals(f);
31 Py_INCREF(f->f_locals);
32 return f->f_locals;
Guido van Rossum3f5da241990-12-20 15:06:42 +000033}
34
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000035int
36PyFrame_GetLineNumber(PyFrameObject *f)
37{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 if (f->f_trace)
39 return f->f_lineno;
40 else
41 return PyCode_Addr2Line(f->f_code, f->f_lasti);
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000042}
43
Michael W. Hudsondd32a912002-08-15 14:59:02 +000044static PyObject *
45frame_getlineno(PyFrameObject *f, void *closure)
46{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 return PyLong_FromLong(PyFrame_GetLineNumber(f));
Michael W. Hudsondd32a912002-08-15 14:59:02 +000048}
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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 int new_lineno = 0; /* The new value of f_lineno */
69 long l_new_lineno;
70 int overflow;
71 int new_lasti = 0; /* The new value of f_lasti */
72 int new_iblock = 0; /* The new value of f_iblock */
73 unsigned char *code = NULL; /* The bytecode for the frame... */
74 Py_ssize_t code_len = 0; /* ...and its length */
75 unsigned char *lnotab = NULL; /* Iterating over co_lnotab */
76 Py_ssize_t lnotab_len = 0; /* (ditto) */
77 int offset = 0; /* (ditto) */
78 int line = 0; /* (ditto) */
79 int addr = 0; /* (ditto) */
80 int min_addr = 0; /* Scanning the SETUPs and POPs */
81 int max_addr = 0; /* (ditto) */
82 int delta_iblock = 0; /* (ditto) */
83 int min_delta_iblock = 0; /* (ditto) */
84 int min_iblock = 0; /* (ditto) */
85 int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */
86 int new_lasti_setup_addr = 0; /* (ditto) */
87 int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */
88 int in_finally[CO_MAXBLOCKS]; /* (ditto) */
89 int blockstack_top = 0; /* (ditto) */
90 unsigned char setup_op = 0; /* (ditto) */
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 /* f_lineno must be an integer. */
93 if (!PyLong_CheckExact(p_new_lineno)) {
94 PyErr_SetString(PyExc_ValueError,
95 "lineno must be an integer");
96 return -1;
97 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 /* You can only do this from within a trace function, not via
100 * _getframe or similar hackery. */
101 if (!f->f_trace)
102 {
103 PyErr_Format(PyExc_ValueError,
104 "f_lineno can only be set by a"
105 " line trace function");
106 return -1;
107 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 /* Fail if the line comes before the start of the code block. */
110 l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
111 if (overflow
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000112#if SIZEOF_LONG > SIZEOF_INT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 || l_new_lineno > INT_MAX
114 || l_new_lineno < INT_MIN
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000115#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 ) {
117 PyErr_SetString(PyExc_ValueError,
118 "lineno out of range");
119 return -1;
120 }
121 new_lineno = (int)l_new_lineno;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 if (new_lineno < f->f_code->co_firstlineno) {
124 PyErr_Format(PyExc_ValueError,
125 "line %d comes before the current code block",
126 new_lineno);
127 return -1;
128 }
129 else if (new_lineno == f->f_code->co_firstlineno) {
130 new_lasti = 0;
131 new_lineno = f->f_code->co_firstlineno;
132 }
133 else {
134 /* Find the bytecode offset for the start of the given
135 * line, or the first code-owning line after it. */
136 char *tmp;
137 PyBytes_AsStringAndSize(f->f_code->co_lnotab,
138 &tmp, &lnotab_len);
139 lnotab = (unsigned char *) tmp;
140 addr = 0;
141 line = f->f_code->co_firstlineno;
142 new_lasti = -1;
143 for (offset = 0; offset < lnotab_len; offset += 2) {
144 addr += lnotab[offset];
145 line += lnotab[offset+1];
146 if (line >= new_lineno) {
147 new_lasti = addr;
148 new_lineno = line;
149 break;
150 }
151 }
152 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 /* If we didn't reach the requested line, return an error. */
155 if (new_lasti == -1) {
156 PyErr_Format(PyExc_ValueError,
157 "line %d comes after the current code block",
158 new_lineno);
159 return -1;
160 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 /* We're now ready to look at the bytecode. */
163 PyBytes_AsStringAndSize(f->f_code->co_code, (char **)&code, &code_len);
164 min_addr = MIN(new_lasti, f->f_lasti);
165 max_addr = MAX(new_lasti, f->f_lasti);
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 /* You can't jump onto a line with an 'except' statement on it -
168 * they expect to have an exception on the top of the stack, which
169 * won't be true if you jump to them. They always start with code
170 * that either pops the exception using POP_TOP (plain 'except:'
171 * lines do this) or duplicates the exception on the stack using
172 * DUP_TOP (if there's an exception type specified). See compile.c,
173 * 'com_try_except' for the full details. There aren't any other
174 * cases (AFAIK) where a line's code can start with DUP_TOP or
175 * POP_TOP, but if any ever appear, they'll be subject to the same
176 * restriction (but with a different error message). */
177 if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
178 PyErr_SetString(PyExc_ValueError,
179 "can't jump to 'except' line as there's no exception");
180 return -1;
181 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 /* You can't jump into or out of a 'finally' block because the 'try'
184 * block leaves something on the stack for the END_FINALLY to clean
185 * up. So we walk the bytecode, maintaining a simulated blockstack.
186 * When we reach the old or new address and it's in a 'finally' block
187 * we note the address of the corresponding SETUP_FINALLY. The jump
188 * is only legal if neither address is in a 'finally' block or
189 * they're both in the same one. 'blockstack' is a stack of the
190 * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks
191 * whether we're in a 'finally' block at each blockstack level. */
192 f_lasti_setup_addr = -1;
193 new_lasti_setup_addr = -1;
194 memset(blockstack, '\0', sizeof(blockstack));
195 memset(in_finally, '\0', sizeof(in_finally));
196 blockstack_top = 0;
197 for (addr = 0; addr < code_len; addr++) {
198 unsigned char op = code[addr];
199 switch (op) {
200 case SETUP_LOOP:
201 case SETUP_EXCEPT:
202 case SETUP_FINALLY:
203 blockstack[blockstack_top++] = addr;
204 in_finally[blockstack_top-1] = 0;
205 break;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 case POP_BLOCK:
208 assert(blockstack_top > 0);
209 setup_op = code[blockstack[blockstack_top-1]];
210 if (setup_op == SETUP_FINALLY) {
211 in_finally[blockstack_top-1] = 1;
212 }
213 else {
214 blockstack_top--;
215 }
216 break;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 case END_FINALLY:
219 /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist
220 * in the bytecode but don't correspond to an actual
221 * 'finally' block. (If blockstack_top is 0, we must
222 * be seeing such an END_FINALLY.) */
223 if (blockstack_top > 0) {
224 setup_op = code[blockstack[blockstack_top-1]];
225 if (setup_op == SETUP_FINALLY) {
226 blockstack_top--;
227 }
228 }
229 break;
230 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 /* For the addresses we're interested in, see whether they're
233 * within a 'finally' block and if so, remember the address
234 * of the SETUP_FINALLY. */
235 if (addr == new_lasti || addr == f->f_lasti) {
236 int i = 0;
237 int setup_addr = -1;
238 for (i = blockstack_top-1; i >= 0; i--) {
239 if (in_finally[i]) {
240 setup_addr = blockstack[i];
241 break;
242 }
243 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 if (setup_addr != -1) {
246 if (addr == new_lasti) {
247 new_lasti_setup_addr = setup_addr;
248 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 if (addr == f->f_lasti) {
251 f_lasti_setup_addr = setup_addr;
252 }
253 }
254 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (op >= HAVE_ARGUMENT) {
257 addr += 2;
258 }
259 }
Neal Norwitzee65e222002-12-19 18:16:57 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* Verify that the blockstack tracking code didn't get lost. */
262 assert(blockstack_top == 0);
263
264 /* After all that, are we jumping into / out of a 'finally' block? */
265 if (new_lasti_setup_addr != f_lasti_setup_addr) {
266 PyErr_SetString(PyExc_ValueError,
267 "can't jump into or out of a 'finally' block");
268 return -1;
269 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000270
271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* Police block-jumping (you can't jump into the middle of a block)
273 * and ensure that the blockstack finishes up in a sensible state (by
274 * popping any blocks we're jumping out of). We look at all the
275 * blockstack operations between the current position and the new
276 * one, and keep track of how many blocks we drop out of on the way.
277 * By also keeping track of the lowest blockstack position we see, we
278 * can tell whether the jump goes into any blocks without coming out
279 * again - in that case we raise an exception below. */
280 delta_iblock = 0;
281 for (addr = min_addr; addr < max_addr; addr++) {
282 unsigned char op = code[addr];
283 switch (op) {
284 case SETUP_LOOP:
285 case SETUP_EXCEPT:
286 case SETUP_FINALLY:
287 delta_iblock++;
288 break;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 case POP_BLOCK:
291 delta_iblock--;
292 break;
293 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 min_delta_iblock = MIN(min_delta_iblock, delta_iblock);
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 if (op >= HAVE_ARGUMENT) {
298 addr += 2;
299 }
300 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* Derive the absolute iblock values from the deltas. */
303 min_iblock = f->f_iblock + min_delta_iblock;
304 if (new_lasti > f->f_lasti) {
305 /* Forwards jump. */
306 new_iblock = f->f_iblock + delta_iblock;
307 }
308 else {
309 /* Backwards jump. */
310 new_iblock = f->f_iblock - delta_iblock;
311 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 /* Are we jumping into a block? */
314 if (new_iblock > min_iblock) {
315 PyErr_SetString(PyExc_ValueError,
316 "can't jump into the middle of a block");
317 return -1;
318 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 /* Pop any blocks that we're jumping out of. */
321 while (f->f_iblock > new_iblock) {
322 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
323 while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
324 PyObject *v = (*--f->f_stacktop);
325 Py_DECREF(v);
326 }
327 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* Finally set the new f_lineno and f_lasti and return OK. */
330 f->f_lineno = new_lineno;
331 f->f_lasti = new_lasti;
332 return 0;
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000333}
334
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000335static PyObject *
336frame_gettrace(PyFrameObject *f, void *closure)
337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 PyObject* trace = f->f_trace;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (trace == NULL)
341 trace = Py_None;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_INCREF(trace);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 return trace;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000346}
347
348static int
349frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 PyObject* old_value;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 /* We rely on f_lineno being accurate when f_trace is set. */
354 f->f_lineno = PyFrame_GetLineNumber(f);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 old_value = f->f_trace;
357 Py_XINCREF(v);
358 f->f_trace = v;
359 Py_XDECREF(old_value);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 return 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000362}
363
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000364
Guido van Rossum32d34c82001-09-20 21:45:26 +0000365static PyGetSetDef frame_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 {"f_locals", (getter)frame_getlocals, NULL, NULL},
367 {"f_lineno", (getter)frame_getlineno,
368 (setter)frame_setlineno, NULL},
369 {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
370 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000371};
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000372
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000373/* Stack frames are allocated and deallocated at a considerable rate.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000374 In an attempt to improve the speed of function calls, we:
375
376 1. Hold a single "zombie" frame on each code object. This retains
377 the allocated and initialised frame object from an invocation of
378 the code object. The zombie is reanimated the next time we need a
379 frame object for that code object. Doing this saves the malloc/
380 realloc required when using a free_list frame that isn't the
381 correct size. It also saves some field initialisation.
382
383 In zombie mode, no field of PyFrameObject holds a reference, but
384 the following fields are still valid:
385
386 * ob_type, ob_size, f_code, f_valuestack;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387
Thomas Wouters477c8d52006-05-27 19:21:47 +0000388 * f_locals, f_trace,
389 f_exc_type, f_exc_value, f_exc_traceback are NULL;
390
391 * f_localsplus does not require re-allocation and
392 the local variables in f_localsplus are NULL.
393
394 2. We also maintain a separate free list of stack frames (just like
Mark Dickinsond19052c2010-06-27 18:19:09 +0000395 floats are allocated in a special way -- see floatobject.c). When
Thomas Wouters477c8d52006-05-27 19:21:47 +0000396 a stack frame is on the free list, only the following members have
397 a meaning:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 ob_type == &Frametype
399 f_back next item on free list, or NULL
400 f_stacksize size of value stack
401 ob_size size of localsplus
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000402 Note that the value and block stacks are preserved -- this can save
403 another malloc() call or two (and two free() calls as well!).
404 Also note that, unlike for integers, each frame object is a
405 malloc'ed object in its own right -- it is only the actual calls to
406 malloc() that we are trying to save here, not the administration.
407 After all, while a typical program may make millions of calls, a
408 call depth of more than 20 or 30 is probably already exceptional
409 unless the program contains run-away recursion. I hope.
Tim Petersb7ba7432002-04-13 05:21:47 +0000410
Christian Heimes2202f872008-02-06 14:31:34 +0000411 Later, PyFrame_MAXFREELIST was added to bound the # of frames saved on
Tim Petersb7ba7432002-04-13 05:21:47 +0000412 free_list. Else programs creating lots of cyclic trash involving
413 frames could provoke free_list into growing without bound.
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000414*/
415
Guido van Rossum18752471997-04-29 14:49:28 +0000416static PyFrameObject *free_list = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417static int numfree = 0; /* number of frames currently in free_list */
Christian Heimes2202f872008-02-06 14:31:34 +0000418/* max value for numfree */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419#define PyFrame_MAXFREELIST 200
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000420
Guido van Rossum3f5da241990-12-20 15:06:42 +0000421static void
Fred Drake1b190b42000-07-09 05:40:56 +0000422frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 PyObject **p, **valuestack;
425 PyCodeObject *co;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 PyObject_GC_UnTrack(f);
428 Py_TRASHCAN_SAFE_BEGIN(f)
429 /* Kill all local variables */
430 valuestack = f->f_valuestack;
431 for (p = f->f_localsplus; p < valuestack; p++)
432 Py_CLEAR(*p);
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 /* Free stack */
435 if (f->f_stacktop != NULL) {
436 for (p = valuestack; p < f->f_stacktop; p++)
437 Py_XDECREF(*p);
438 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 Py_XDECREF(f->f_back);
441 Py_DECREF(f->f_builtins);
442 Py_DECREF(f->f_globals);
443 Py_CLEAR(f->f_locals);
444 Py_CLEAR(f->f_trace);
445 Py_CLEAR(f->f_exc_type);
446 Py_CLEAR(f->f_exc_value);
447 Py_CLEAR(f->f_exc_traceback);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000448 Py_CLEAR(f->f_yieldfrom);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 co = f->f_code;
451 if (co->co_zombieframe == NULL)
452 co->co_zombieframe = f;
453 else if (numfree < PyFrame_MAXFREELIST) {
454 ++numfree;
455 f->f_back = free_list;
456 free_list = f;
457 }
458 else
459 PyObject_GC_Del(f);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 Py_DECREF(co);
462 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000463}
464
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000465static int
466frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyObject **fastlocals, **p;
469 int i, slots;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 Py_VISIT(f->f_back);
472 Py_VISIT(f->f_code);
473 Py_VISIT(f->f_builtins);
474 Py_VISIT(f->f_globals);
475 Py_VISIT(f->f_locals);
476 Py_VISIT(f->f_trace);
477 Py_VISIT(f->f_exc_type);
478 Py_VISIT(f->f_exc_value);
479 Py_VISIT(f->f_exc_traceback);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000480 Py_VISIT(f->f_yieldfrom);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 /* locals */
483 slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
484 fastlocals = f->f_localsplus;
485 for (i = slots; --i >= 0; ++fastlocals)
486 Py_VISIT(*fastlocals);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 /* stack */
489 if (f->f_stacktop != NULL) {
490 for (p = f->f_valuestack; p < f->f_stacktop; p++)
491 Py_VISIT(*p);
492 }
493 return 0;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000494}
495
496static void
497frame_clear(PyFrameObject *f)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 PyObject **fastlocals, **p, **oldtop;
500 int i, slots;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* Before anything else, make sure that this frame is clearly marked
503 * as being defunct! Else, e.g., a generator reachable from this
504 * frame may also point to this frame, believe itself to still be
505 * active, and try cleaning up this frame again.
506 */
507 oldtop = f->f_stacktop;
508 f->f_stacktop = NULL;
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 Py_CLEAR(f->f_exc_type);
511 Py_CLEAR(f->f_exc_value);
512 Py_CLEAR(f->f_exc_traceback);
513 Py_CLEAR(f->f_trace);
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000514 Py_CLEAR(f->f_yieldfrom);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 /* locals */
517 slots = f->f_code->co_nlocals + PyTuple_GET_SIZE(f->f_code->co_cellvars) + PyTuple_GET_SIZE(f->f_code->co_freevars);
518 fastlocals = f->f_localsplus;
519 for (i = slots; --i >= 0; ++fastlocals)
520 Py_CLEAR(*fastlocals);
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 /* stack */
523 if (oldtop != NULL) {
524 for (p = f->f_valuestack; p < oldtop; p++)
525 Py_CLEAR(*p);
526 }
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000527}
528
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000529static PyObject *
530frame_sizeof(PyFrameObject *f)
531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 Py_ssize_t res, extras, ncells, nfrees;
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 ncells = PyTuple_GET_SIZE(f->f_code->co_cellvars);
535 nfrees = PyTuple_GET_SIZE(f->f_code->co_freevars);
536 extras = f->f_code->co_stacksize + f->f_code->co_nlocals +
537 ncells + nfrees;
538 /* subtract one as it is already included in PyFrameObject */
539 res = sizeof(PyFrameObject) + (extras-1) * sizeof(PyObject *);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 return PyLong_FromSsize_t(res);
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000542}
543
544PyDoc_STRVAR(sizeof__doc__,
545"F.__sizeof__() -> size of F in memory, in bytes");
546
547static PyMethodDef frame_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 {"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
549 sizeof__doc__},
550 {NULL, NULL} /* sentinel */
Robert Schuppeniesfbe94c52008-07-14 10:13:31 +0000551};
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000552
Guido van Rossum18752471997-04-29 14:49:28 +0000553PyTypeObject PyFrame_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 PyVarObject_HEAD_INIT(&PyType_Type, 0)
555 "frame",
556 sizeof(PyFrameObject),
557 sizeof(PyObject *),
558 (destructor)frame_dealloc, /* tp_dealloc */
559 0, /* tp_print */
560 0, /* tp_getattr */
561 0, /* tp_setattr */
562 0, /* tp_reserved */
563 0, /* tp_repr */
564 0, /* tp_as_number */
565 0, /* tp_as_sequence */
566 0, /* tp_as_mapping */
567 0, /* tp_hash */
568 0, /* tp_call */
569 0, /* tp_str */
570 PyObject_GenericGetAttr, /* tp_getattro */
571 PyObject_GenericSetAttr, /* tp_setattro */
572 0, /* tp_as_buffer */
573 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
574 0, /* tp_doc */
575 (traverseproc)frame_traverse, /* tp_traverse */
576 (inquiry)frame_clear, /* tp_clear */
577 0, /* tp_richcompare */
578 0, /* tp_weaklistoffset */
579 0, /* tp_iter */
580 0, /* tp_iternext */
581 frame_methods, /* tp_methods */
582 frame_memberlist, /* tp_members */
583 frame_getsetlist, /* tp_getset */
584 0, /* tp_base */
585 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000586};
587
Neal Norwitzc91ed402002-12-30 22:29:22 +0000588static PyObject *builtin_object;
589
Neal Norwitzb2501f42002-12-31 03:42:13 +0000590int _PyFrame_Init()
Neal Norwitzc91ed402002-12-30 22:29:22 +0000591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 builtin_object = PyUnicode_InternFromString("__builtins__");
593 if (builtin_object == NULL)
594 return 0;
595 return 1;
Neal Norwitzc91ed402002-12-30 22:29:22 +0000596}
597
Guido van Rossum18752471997-04-29 14:49:28 +0000598PyFrameObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000599PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 PyFrameObject *back = tstate->frame;
603 PyFrameObject *f;
604 PyObject *builtins;
605 Py_ssize_t i;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000606
Michael W. Hudson69734a52002-08-19 16:54:08 +0000607#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
609 (locals != NULL && !PyMapping_Check(locals))) {
610 PyErr_BadInternalCall();
611 return NULL;
612 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000613#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 if (back == NULL || back->f_globals != globals) {
615 builtins = PyDict_GetItem(globals, builtin_object);
616 if (builtins) {
617 if (PyModule_Check(builtins)) {
618 builtins = PyModule_GetDict(builtins);
619 assert(!builtins || PyDict_Check(builtins));
620 }
621 else if (!PyDict_Check(builtins))
622 builtins = NULL;
623 }
624 if (builtins == NULL) {
625 /* No builtins! Make up a minimal one
626 Give them 'None', at least. */
627 builtins = PyDict_New();
628 if (builtins == NULL ||
629 PyDict_SetItemString(
630 builtins, "None", Py_None) < 0)
631 return NULL;
632 }
633 else
634 Py_INCREF(builtins);
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 }
637 else {
638 /* If we share the globals, we share the builtins.
639 Save a lookup and a call. */
640 builtins = back->f_builtins;
641 assert(builtins != NULL && PyDict_Check(builtins));
642 Py_INCREF(builtins);
643 }
644 if (code->co_zombieframe != NULL) {
645 f = code->co_zombieframe;
646 code->co_zombieframe = NULL;
647 _Py_NewReference((PyObject *)f);
648 assert(f->f_code == code);
649 }
650 else {
651 Py_ssize_t extras, ncells, nfrees;
652 ncells = PyTuple_GET_SIZE(code->co_cellvars);
653 nfrees = PyTuple_GET_SIZE(code->co_freevars);
654 extras = code->co_stacksize + code->co_nlocals + ncells +
655 nfrees;
656 if (free_list == NULL) {
657 f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type,
658 extras);
659 if (f == NULL) {
660 Py_DECREF(builtins);
661 return NULL;
662 }
663 }
664 else {
665 assert(numfree > 0);
666 --numfree;
667 f = free_list;
668 free_list = free_list->f_back;
669 if (Py_SIZE(f) < extras) {
670 f = PyObject_GC_Resize(PyFrameObject, f, extras);
671 if (f == NULL) {
672 Py_DECREF(builtins);
673 return NULL;
674 }
675 }
676 _Py_NewReference((PyObject *)f);
677 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 f->f_code = code;
680 extras = code->co_nlocals + ncells + nfrees;
681 f->f_valuestack = f->f_localsplus + extras;
682 for (i=0; i<extras; i++)
683 f->f_localsplus[i] = NULL;
684 f->f_locals = NULL;
685 f->f_trace = NULL;
686 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
687 }
688 f->f_stacktop = f->f_valuestack;
689 f->f_builtins = builtins;
690 Py_XINCREF(back);
691 f->f_back = back;
692 Py_INCREF(code);
693 Py_INCREF(globals);
694 f->f_globals = globals;
695 /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
696 if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
697 (CO_NEWLOCALS | CO_OPTIMIZED))
698 ; /* f_locals = NULL; will be set by PyFrame_FastToLocals() */
699 else if (code->co_flags & CO_NEWLOCALS) {
700 locals = PyDict_New();
701 if (locals == NULL) {
702 Py_DECREF(f);
703 return NULL;
704 }
705 f->f_locals = locals;
706 }
707 else {
708 if (locals == NULL)
709 locals = globals;
710 Py_INCREF(locals);
711 f->f_locals = locals;
712 }
713 f->f_tstate = tstate;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 f->f_lasti = -1;
716 f->f_lineno = code->co_firstlineno;
717 f->f_iblock = 0;
Nick Coghlan1f7ce622012-01-13 21:43:40 +1000718 f->f_yieldfrom = NULL;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 _PyObject_GC_TRACK(f);
721 return f;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000722}
723
Guido van Rossum3f5da241990-12-20 15:06:42 +0000724/* Block management */
725
726void
Fred Drake1b190b42000-07-09 05:40:56 +0000727PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 PyTryBlock *b;
730 if (f->f_iblock >= CO_MAXBLOCKS)
731 Py_FatalError("XXX block stack overflow");
732 b = &f->f_blockstack[f->f_iblock++];
733 b->b_type = type;
734 b->b_level = level;
735 b->b_handler = handler;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000736}
737
Guido van Rossum18752471997-04-29 14:49:28 +0000738PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000739PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 PyTryBlock *b;
742 if (f->f_iblock <= 0)
743 Py_FatalError("XXX block stack underflow");
744 b = &f->f_blockstack[--f->f_iblock];
745 return b;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000746}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000747
Guido van Rossumd8faa362007-04-27 19:54:29 +0000748/* Convert between "fast" version of locals and dictionary version.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749
750 map and values are input arguments. map is a tuple of strings.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000751 values is an array of PyObject*. At index i, map[i] is the name of
752 the variable with value values[i]. The function copies the first
753 nmap variable from map/values into dict. If values[i] is NULL,
754 the variable is deleted from dict.
755
756 If deref is true, then the values being copied are cell variables
757 and the value is extracted from the cell variable before being put
758 in dict.
759
760 Exceptions raised while modifying the dict are silently ignored,
761 because there is no good way to report them.
762 */
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000763
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000764static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000765map_to_dict(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 int deref)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 Py_ssize_t j;
769 assert(PyTuple_Check(map));
770 assert(PyDict_Check(dict));
771 assert(PyTuple_Size(map) >= nmap);
772 for (j = nmap; --j >= 0; ) {
773 PyObject *key = PyTuple_GET_ITEM(map, j);
774 PyObject *value = values[j];
775 assert(PyUnicode_Check(key));
776 if (deref) {
777 assert(PyCell_Check(value));
778 value = PyCell_GET(value);
779 }
780 if (value == NULL) {
781 if (PyObject_DelItem(dict, key) != 0)
782 PyErr_Clear();
783 }
784 else {
785 if (PyObject_SetItem(dict, key, value) != 0)
786 PyErr_Clear();
787 }
788 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000789}
790
Guido van Rossumd8faa362007-04-27 19:54:29 +0000791/* Copy values from the "locals" dict into the fast locals.
792
793 dict is an input argument containing string keys representing
794 variables names and arbitrary PyObject* as values.
795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 map and values are input arguments. map is a tuple of strings.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000797 values is an array of PyObject*. At index i, map[i] is the name of
798 the variable with value values[i]. The function copies the first
799 nmap variable from map/values into dict. If values[i] is NULL,
800 the variable is deleted from dict.
801
802 If deref is true, then the values being copied are cell variables
803 and the value is extracted from the cell variable before being put
804 in dict. If clear is true, then variables in map but not in dict
805 are set to NULL in map; if clear is false, variables missing in
806 dict are ignored.
807
808 Exceptions raised while modifying the dict are silently ignored,
809 because there is no good way to report them.
810*/
811
Guido van Rossum6b356e72001-04-14 17:55:41 +0000812static void
Martin v. Löwis18e16552006-02-15 17:27:45 +0000813dict_to_map(PyObject *map, Py_ssize_t nmap, PyObject *dict, PyObject **values,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 int deref, int clear)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 Py_ssize_t j;
817 assert(PyTuple_Check(map));
818 assert(PyDict_Check(dict));
819 assert(PyTuple_Size(map) >= nmap);
820 for (j = nmap; --j >= 0; ) {
821 PyObject *key = PyTuple_GET_ITEM(map, j);
822 PyObject *value = PyObject_GetItem(dict, key);
823 assert(PyUnicode_Check(key));
824 /* We only care about NULLs if clear is true. */
825 if (value == NULL) {
826 PyErr_Clear();
827 if (!clear)
828 continue;
829 }
830 if (deref) {
831 assert(PyCell_Check(values[j]));
832 if (PyCell_GET(values[j]) != value) {
833 if (PyCell_Set(values[j], value) < 0)
834 PyErr_Clear();
835 }
836 } else if (values[j] != value) {
837 Py_XINCREF(value);
838 Py_XDECREF(values[j]);
839 values[j] = value;
840 }
841 Py_XDECREF(value);
842 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000843}
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000844
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000845void
Fred Drake1b190b42000-07-09 05:40:56 +0000846PyFrame_FastToLocals(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 /* Merge fast locals into f->f_locals */
849 PyObject *locals, *map;
850 PyObject **fast;
851 PyObject *error_type, *error_value, *error_traceback;
852 PyCodeObject *co;
853 Py_ssize_t j;
854 int ncells, nfreevars;
855 if (f == NULL)
856 return;
857 locals = f->f_locals;
858 if (locals == NULL) {
859 locals = f->f_locals = PyDict_New();
860 if (locals == NULL) {
861 PyErr_Clear(); /* Can't report it :-( */
862 return;
863 }
864 }
865 co = f->f_code;
866 map = co->co_varnames;
867 if (!PyTuple_Check(map))
868 return;
869 PyErr_Fetch(&error_type, &error_value, &error_traceback);
870 fast = f->f_localsplus;
871 j = PyTuple_GET_SIZE(map);
872 if (j > co->co_nlocals)
873 j = co->co_nlocals;
874 if (co->co_nlocals)
875 map_to_dict(map, j, locals, fast, 0);
876 ncells = PyTuple_GET_SIZE(co->co_cellvars);
877 nfreevars = PyTuple_GET_SIZE(co->co_freevars);
878 if (ncells || nfreevars) {
879 map_to_dict(co->co_cellvars, ncells,
880 locals, fast + co->co_nlocals, 1);
881 /* If the namespace is unoptimized, then one of the
882 following cases applies:
883 1. It does not contain free variables, because it
884 uses import * or is a top-level namespace.
885 2. It is a class namespace.
886 We don't want to accidentally copy free variables
887 into the locals dict used by the class.
888 */
889 if (co->co_flags & CO_OPTIMIZED) {
890 map_to_dict(co->co_freevars, nfreevars,
891 locals, fast + co->co_nlocals + ncells, 1);
892 }
893 }
894 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000895}
896
897void
Fred Drake1b190b42000-07-09 05:40:56 +0000898PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 /* Merge f->f_locals into fast locals */
901 PyObject *locals, *map;
902 PyObject **fast;
903 PyObject *error_type, *error_value, *error_traceback;
904 PyCodeObject *co;
905 Py_ssize_t j;
906 int ncells, nfreevars;
907 if (f == NULL)
908 return;
909 locals = f->f_locals;
910 co = f->f_code;
911 map = co->co_varnames;
912 if (locals == NULL)
913 return;
914 if (!PyTuple_Check(map))
915 return;
916 PyErr_Fetch(&error_type, &error_value, &error_traceback);
917 fast = f->f_localsplus;
918 j = PyTuple_GET_SIZE(map);
919 if (j > co->co_nlocals)
920 j = co->co_nlocals;
921 if (co->co_nlocals)
922 dict_to_map(co->co_varnames, j, locals, fast, 0, clear);
923 ncells = PyTuple_GET_SIZE(co->co_cellvars);
924 nfreevars = PyTuple_GET_SIZE(co->co_freevars);
925 if (ncells || nfreevars) {
926 dict_to_map(co->co_cellvars, ncells,
927 locals, fast + co->co_nlocals, 1, clear);
928 /* Same test as in PyFrame_FastToLocals() above. */
929 if (co->co_flags & CO_OPTIMIZED) {
930 dict_to_map(co->co_freevars, nfreevars,
931 locals, fast + co->co_nlocals + ncells, 1,
932 clear);
933 }
934 }
935 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000936}
Guido van Rossum404b95d1997-08-05 02:09:46 +0000937
938/* Clear out the free list */
Christian Heimesa156e092008-02-16 07:38:31 +0000939int
940PyFrame_ClearFreeList(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +0000941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 int freelist_size = numfree;
943
944 while (free_list != NULL) {
945 PyFrameObject *f = free_list;
946 free_list = free_list->f_back;
947 PyObject_GC_Del(f);
948 --numfree;
949 }
950 assert(numfree == 0);
951 return freelist_size;
Christian Heimesa156e092008-02-16 07:38:31 +0000952}
953
954void
955PyFrame_Fini(void)
956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 (void)PyFrame_ClearFreeList();
958 Py_XDECREF(builtin_object);
959 builtin_object = NULL;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000960}