blob: bc8cae9fe618ed62a4fb8243aa12d81e8237dd29 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Frame object implementation */
3
Guido van Rossum18752471997-04-29 14:49:28 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
6#include "compile.h"
7#include "frameobject.h"
8#include "opcode.h"
9#include "structmember.h"
10
Neal Norwitz91787cb2002-12-18 23:33:35 +000011#undef MIN
12#undef MAX
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000013#define MIN(a, b) ((a) < (b) ? (a) : (b))
14#define MAX(a, b) ((a) > (b) ? (a) : (b))
15
Guido van Rossum18752471997-04-29 14:49:28 +000016#define OFF(x) offsetof(PyFrameObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000017
Guido van Rossum6f799372001-09-20 20:46:19 +000018static PyMemberDef frame_memberlist[] = {
Guido van Rossum1d5735e1994-08-30 08:27:36 +000019 {"f_back", T_OBJECT, OFF(f_back), RO},
20 {"f_code", T_OBJECT, OFF(f_code), RO},
Guido van Rossumc1134821995-01-10 10:39:16 +000021 {"f_builtins", T_OBJECT, OFF(f_builtins),RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000022 {"f_globals", T_OBJECT, OFF(f_globals), RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023 {"f_lasti", T_INT, OFF(f_lasti), RO},
Guido van Rossumc1134821995-01-10 10:39:16 +000024 {"f_restricted",T_INT, OFF(f_restricted),RO},
Guido van Rossuma027efa1997-05-05 20:56:21 +000025 {"f_exc_type", T_OBJECT, OFF(f_exc_type)},
26 {"f_exc_value", T_OBJECT, OFF(f_exc_value)},
27 {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)},
Guido van Rossum3f5da241990-12-20 15:06:42 +000028 {NULL} /* Sentinel */
29};
30
Guido van Rossum18752471997-04-29 14:49:28 +000031static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000032frame_getlocals(PyFrameObject *f, void *closure)
Guido van Rossum3f5da241990-12-20 15:06:42 +000033{
Tim Peters6d6c1a32001-08-02 04:15:00 +000034 PyFrame_FastToLocals(f);
35 Py_INCREF(f->f_locals);
36 return f->f_locals;
Guido van Rossum3f5da241990-12-20 15:06:42 +000037}
38
Michael W. Hudsondd32a912002-08-15 14:59:02 +000039static PyObject *
40frame_getlineno(PyFrameObject *f, void *closure)
41{
42 int lineno;
43
Michael W. Hudson02ff6a92002-09-11 15:36:32 +000044 if (f->f_trace)
45 lineno = f->f_lineno;
46 else
47 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000048
49 return PyInt_FromLong(lineno);
50}
51
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000052/* Setter for f_lineno - you can set f_lineno from within a trace function in
53 * order to jump to a given line of code, subject to some restrictions. Most
54 * lines are OK to jump to because they don't make any assumptions about the
55 * state of the stack (obvious because you could remove the line and the code
56 * would still work without any stack errors), but there are some constructs
57 * that limit jumping:
58 *
59 * o Lines with an 'except' statement on them can't be jumped to, because
60 * they expect an exception to be on the top of the stack.
61 * o Lines that live in a 'finally' block can't be jumped from or to, since
62 * the END_FINALLY expects to clean up the stack after the 'try' block.
63 * o 'try'/'for'/'while' blocks can't be jumped into because the blockstack
64 * needs to be set up before their code runs, and for 'for' loops the
65 * iterator needs to be on the stack.
66 */
67static int
68frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
69{
70 int new_lineno = 0; /* The new value of f_lineno */
71 int new_lasti = 0; /* The new value of f_lasti */
72 int new_iblock = 0; /* The new value of f_iblock */
73 char *code = NULL; /* The bytecode for the frame... */
74 int code_len = 0; /* ...and its length */
75 char *lnotab = NULL; /* Iterating over co_lnotab */
76 int 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 int setup_op = 0; /* (ditto) */
91
92 /* f_lineno must be an integer. */
93 if (!PyInt_Check(p_new_lineno)) {
94 PyErr_SetString(PyExc_ValueError,
95 "lineno must be an integer");
96 return -1;
97 }
98
99 /* 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 trace function");
105 return -1;
106 }
107
108 /* Fail if the line comes before the start of the code block. */
109 new_lineno = (int) PyInt_AsLong(p_new_lineno);
110 if (new_lineno < f->f_code->co_firstlineno) {
111 PyErr_Format(PyExc_ValueError,
112 "line %d comes before the current code block",
113 new_lineno);
114 return -1;
115 }
116
117 /* Find the bytecode offset for the start of the given line, or the
118 * first code-owning line after it. */
119 PyString_AsStringAndSize(f->f_code->co_lnotab, &lnotab, &lnotab_len);
120 addr = 0;
121 line = f->f_code->co_firstlineno;
122 new_lasti = -1;
123 for (offset = 0; offset < lnotab_len; offset += 2) {
124 addr += lnotab[offset];
125 line += lnotab[offset+1];
126 if (line >= new_lineno) {
127 new_lasti = addr;
128 new_lineno = line;
129 break;
130 }
131 }
132
133 /* If we didn't reach the requested line, return an error. */
134 if (new_lasti == -1) {
135 PyErr_Format(PyExc_ValueError,
136 "line %d comes after the current code block",
137 new_lineno);
138 return -1;
139 }
140
141 /* We're now ready to look at the bytecode. */
142 PyString_AsStringAndSize(f->f_code->co_code, &code, &code_len);
143 min_addr = MIN(new_lasti, f->f_lasti);
144 max_addr = MAX(new_lasti, f->f_lasti);
145
146 /* You can't jump onto a line with an 'except' statement on it -
147 * they expect to have an exception on the top of the stack, which
148 * won't be true if you jump to them. They always start with code
149 * that either pops the exception using POP_TOP (plain 'except:'
150 * lines do this) or duplicates the exception on the stack using
151 * DUP_TOP (if there's an exception type specified). See compile.c,
152 * 'com_try_except' for the full details. There aren't any other
153 * cases (AFAIK) where a line's code can start with DUP_TOP or
154 * POP_TOP, but if any ever appear, they'll be subject to the same
155 * restriction (but with a different error message). */
156 if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
157 PyErr_SetString(PyExc_ValueError,
158 "can't jump to 'except' line as there's no exception");
159 return -1;
160 }
161
162 /* You can't jump into or out of a 'finally' block because the 'try'
163 * block leaves something on the stack for the END_FINALLY to clean
164 * up. So we walk the bytecode, maintaining a simulated blockstack.
165 * When we reach the old or new address and it's in a 'finally' block
166 * we note the address of the corresponding SETUP_FINALLY. The jump
167 * is only legal if neither address is in a 'finally' block or
168 * they're both in the same one. 'blockstack' is a stack of the
169 * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks
170 * whether we're in a 'finally' block at each blockstack level. */
171 f_lasti_setup_addr = -1;
172 new_lasti_setup_addr = -1;
173 memset(blockstack, '\0', sizeof(blockstack));
174 memset(in_finally, '\0', sizeof(in_finally));
175 blockstack_top = 0;
176 for (addr = 0; addr < code_len; addr++) {
177 unsigned char op = code[addr];
178 switch (op) {
179 case SETUP_LOOP:
180 case SETUP_EXCEPT:
181 case SETUP_FINALLY:
182 blockstack[blockstack_top++] = addr;
183 in_finally[blockstack_top-1] = 0;
184 break;
185
186 case POP_BLOCK:
Neal Norwitzee65e222002-12-19 18:16:57 +0000187 assert(blockstack_top > 0);
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000188 setup_op = code[blockstack[blockstack_top-1]];
189 if (setup_op == SETUP_FINALLY) {
190 in_finally[blockstack_top-1] = 1;
191 }
192 else {
193 blockstack_top--;
194 }
195 break;
196
197 case END_FINALLY:
198 /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist
199 * in the bytecode but don't correspond to an actual
Neal Norwitzee65e222002-12-19 18:16:57 +0000200 * 'finally' block. (If blockstack_top is 0, we must
201 * be seeing such an END_FINALLY.) */
202 if (blockstack_top > 0) {
203 setup_op = code[blockstack[blockstack_top-1]];
204 if (setup_op == SETUP_FINALLY) {
205 blockstack_top--;
206 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000207 }
208 break;
209 }
210
211 /* For the addresses we're interested in, see whether they're
212 * within a 'finally' block and if so, remember the address
213 * of the SETUP_FINALLY. */
214 if (addr == new_lasti || addr == f->f_lasti) {
215 int i = 0;
216 int setup_addr = -1;
217 for (i = blockstack_top-1; i >= 0; i--) {
218 if (in_finally[i]) {
219 setup_addr = blockstack[i];
220 break;
221 }
222 }
223
224 if (setup_addr != -1) {
225 if (addr == new_lasti) {
226 new_lasti_setup_addr = setup_addr;
227 }
228
229 if (addr == f->f_lasti) {
230 f_lasti_setup_addr = setup_addr;
231 }
232 }
233 }
234
235 if (op >= HAVE_ARGUMENT) {
236 addr += 2;
237 }
238 }
239
Neal Norwitzee65e222002-12-19 18:16:57 +0000240 /* Verify that the blockstack tracking code didn't get lost. */
241 assert(blockstack_top == 0);
242
243 /* After all that, are we jumping into / out of a 'finally' block? */
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000244 if (new_lasti_setup_addr != f_lasti_setup_addr) {
245 PyErr_SetString(PyExc_ValueError,
246 "can't jump into or out of a 'finally' block");
247 return -1;
248 }
249
250
251 /* Police block-jumping (you can't jump into the middle of a block)
252 * and ensure that the blockstack finishes up in a sensible state (by
253 * popping any blocks we're jumping out of). We look at all the
254 * blockstack operations between the current position and the new
255 * one, and keep track of how many blocks we drop out of on the way.
256 * By also keeping track of the lowest blockstack position we see, we
257 * can tell whether the jump goes into any blocks without coming out
258 * again - in that case we raise an exception below. */
259 delta_iblock = 0;
260 for (addr = min_addr; addr < max_addr; addr++) {
261 unsigned char op = code[addr];
262 switch (op) {
263 case SETUP_LOOP:
264 case SETUP_EXCEPT:
265 case SETUP_FINALLY:
266 delta_iblock++;
267 break;
268
269 case POP_BLOCK:
270 delta_iblock--;
271 break;
272 }
273
274 min_delta_iblock = MIN(min_delta_iblock, delta_iblock);
275
276 if (op >= HAVE_ARGUMENT) {
277 addr += 2;
278 }
279 }
280
281 /* Derive the absolute iblock values from the deltas. */
282 min_iblock = f->f_iblock + min_delta_iblock;
283 if (new_lasti > f->f_lasti) {
284 /* Forwards jump. */
285 new_iblock = f->f_iblock + delta_iblock;
286 }
287 else {
288 /* Backwards jump. */
289 new_iblock = f->f_iblock - delta_iblock;
290 }
291
292 /* Are we jumping into a block? */
293 if (new_iblock > min_iblock) {
294 PyErr_SetString(PyExc_ValueError,
295 "can't jump into the middle of a block");
296 return -1;
297 }
298
299 /* Pop any blocks that we're jumping out of. */
300 while (f->f_iblock > new_iblock) {
301 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
302 while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
303 PyObject *v = (*--f->f_stacktop);
304 Py_DECREF(v);
305 }
306 }
307
308 /* Finally set the new f_lineno and f_lasti and return OK. */
309 f->f_lineno = new_lineno;
310 f->f_lasti = new_lasti;
311 return 0;
312}
313
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000314static PyObject *
315frame_gettrace(PyFrameObject *f, void *closure)
316{
317 PyObject* trace = f->f_trace;
318
319 if (trace == NULL)
320 trace = Py_None;
321
322 Py_INCREF(trace);
323
324 return trace;
325}
326
327static int
328frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
329{
330 /* We rely on f_lineno being accurate when f_trace is set. */
331
332 PyObject* old_value = f->f_trace;
333
334 Py_XINCREF(v);
335 f->f_trace = v;
336
337 if (v != NULL)
338 f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
339
340 Py_XDECREF(old_value);
341
342 return 0;
343}
344
Guido van Rossum32d34c82001-09-20 21:45:26 +0000345static PyGetSetDef frame_getsetlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000346 {"f_locals", (getter)frame_getlocals, NULL, NULL},
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000347 {"f_lineno", (getter)frame_getlineno,
348 (setter)frame_setlineno, NULL},
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000349 {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000350 {0}
351};
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000352
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000353/* Stack frames are allocated and deallocated at a considerable rate.
354 In an attempt to improve the speed of function calls, we maintain a
355 separate free list of stack frames (just like integers are
356 allocated in a special way -- see intobject.c). When a stack frame
357 is on the free list, only the following members have a meaning:
358 ob_type == &Frametype
359 f_back next item on free list, or NULL
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000360 f_nlocals number of locals
361 f_stacksize size of value stack
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000362 ob_size size of localsplus
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000363 Note that the value and block stacks are preserved -- this can save
364 another malloc() call or two (and two free() calls as well!).
365 Also note that, unlike for integers, each frame object is a
366 malloc'ed object in its own right -- it is only the actual calls to
367 malloc() that we are trying to save here, not the administration.
368 After all, while a typical program may make millions of calls, a
369 call depth of more than 20 or 30 is probably already exceptional
370 unless the program contains run-away recursion. I hope.
Tim Petersb7ba7432002-04-13 05:21:47 +0000371
372 Later, MAXFREELIST was added to bound the # of frames saved on
373 free_list. Else programs creating lots of cyclic trash involving
374 frames could provoke free_list into growing without bound.
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000375*/
376
Guido van Rossum18752471997-04-29 14:49:28 +0000377static PyFrameObject *free_list = NULL;
Tim Petersb7ba7432002-04-13 05:21:47 +0000378static int numfree = 0; /* number of frames currently in free_list */
379#define MAXFREELIST 200 /* max value for numfree */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000380
Guido van Rossum3f5da241990-12-20 15:06:42 +0000381static void
Fred Drake1b190b42000-07-09 05:40:56 +0000382frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000383{
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000384 int i, slots;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000385 PyObject **fastlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +0000386 PyObject **p;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000387
Guido van Rossumff413af2002-03-28 20:34:59 +0000388 PyObject_GC_UnTrack(f);
Guido van Rossumd724b232000-03-13 16:01:29 +0000389 Py_TRASHCAN_SAFE_BEGIN(f)
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000390 /* Kill all local variables */
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000391 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000392 fastlocals = f->f_localsplus;
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000393 for (i = slots; --i >= 0; ++fastlocals) {
Guido van Rossum18752471997-04-29 14:49:28 +0000394 Py_XDECREF(*fastlocals);
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000395 }
396
Tim Peters5ca576e2001-06-18 22:08:13 +0000397 /* Free stack */
Tim Peters8c963692001-06-23 05:26:56 +0000398 if (f->f_stacktop != NULL) {
399 for (p = f->f_valuestack; p < f->f_stacktop; p++)
400 Py_XDECREF(*p);
Tim Peters5ca576e2001-06-18 22:08:13 +0000401 }
Tim Peters8c963692001-06-23 05:26:56 +0000402
Guido van Rossum18752471997-04-29 14:49:28 +0000403 Py_XDECREF(f->f_back);
Neal Norwitzc91ed402002-12-30 22:29:22 +0000404 Py_DECREF(f->f_code);
405 Py_DECREF(f->f_builtins);
406 Py_DECREF(f->f_globals);
Guido van Rossum18752471997-04-29 14:49:28 +0000407 Py_XDECREF(f->f_locals);
408 Py_XDECREF(f->f_trace);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000409 Py_XDECREF(f->f_exc_type);
410 Py_XDECREF(f->f_exc_value);
411 Py_XDECREF(f->f_exc_traceback);
Tim Petersb7ba7432002-04-13 05:21:47 +0000412 if (numfree < MAXFREELIST) {
413 ++numfree;
414 f->f_back = free_list;
415 free_list = f;
416 }
417 else
418 PyObject_GC_Del(f);
Guido van Rossumd724b232000-03-13 16:01:29 +0000419 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000420}
421
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000422static int
423frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
424{
425 PyObject **fastlocals, **p;
426 int i, err, slots;
427#define VISIT(o) if (o) {if ((err = visit((PyObject *)(o), arg))) return err;}
428
429 VISIT(f->f_back);
430 VISIT(f->f_code);
431 VISIT(f->f_builtins);
432 VISIT(f->f_globals);
433 VISIT(f->f_locals);
434 VISIT(f->f_trace);
435 VISIT(f->f_exc_type);
436 VISIT(f->f_exc_value);
437 VISIT(f->f_exc_traceback);
438
439 /* locals */
440 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
441 fastlocals = f->f_localsplus;
442 for (i = slots; --i >= 0; ++fastlocals) {
443 VISIT(*fastlocals);
444 }
445
446 /* stack */
447 if (f->f_stacktop != NULL) {
448 for (p = f->f_valuestack; p < f->f_stacktop; p++)
449 VISIT(*p);
450 }
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000451 return 0;
452}
453
454static void
455frame_clear(PyFrameObject *f)
456{
457 PyObject **fastlocals, **p;
458 int i, slots;
459
460 Py_XDECREF(f->f_exc_type);
461 f->f_exc_type = NULL;
462
463 Py_XDECREF(f->f_exc_value);
464 f->f_exc_value = NULL;
465
466 Py_XDECREF(f->f_exc_traceback);
467 f->f_exc_traceback = NULL;
468
469 Py_XDECREF(f->f_trace);
470 f->f_trace = NULL;
471
472 /* locals */
473 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
474 fastlocals = f->f_localsplus;
475 for (i = slots; --i >= 0; ++fastlocals) {
476 if (*fastlocals != NULL) {
477 Py_XDECREF(*fastlocals);
478 *fastlocals = NULL;
479 }
480 }
481
482 /* stack */
483 if (f->f_stacktop != NULL) {
484 for (p = f->f_valuestack; p < f->f_stacktop; p++) {
485 Py_XDECREF(*p);
486 *p = NULL;
487 }
488 }
489}
490
491
Guido van Rossum18752471997-04-29 14:49:28 +0000492PyTypeObject PyFrame_Type = {
493 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000494 0,
495 "frame",
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000496 sizeof(PyFrameObject),
497 sizeof(PyObject *),
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000498 (destructor)frame_dealloc, /* tp_dealloc */
499 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000500 0, /* tp_getattr */
501 0, /* tp_setattr */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000502 0, /* tp_compare */
503 0, /* tp_repr */
504 0, /* tp_as_number */
505 0, /* tp_as_sequence */
506 0, /* tp_as_mapping */
507 0, /* tp_hash */
508 0, /* tp_call */
509 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000510 PyObject_GenericGetAttr, /* tp_getattro */
511 PyObject_GenericSetAttr, /* tp_setattro */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000512 0, /* tp_as_buffer */
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000513 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000514 0, /* tp_doc */
515 (traverseproc)frame_traverse, /* tp_traverse */
516 (inquiry)frame_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000517 0, /* tp_richcompare */
518 0, /* tp_weaklistoffset */
519 0, /* tp_iter */
520 0, /* tp_iternext */
521 0, /* tp_methods */
522 frame_memberlist, /* tp_members */
523 frame_getsetlist, /* tp_getset */
524 0, /* tp_base */
525 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000526};
527
Neal Norwitzc91ed402002-12-30 22:29:22 +0000528static PyObject *builtin_object;
529
Neal Norwitzb2501f42002-12-31 03:42:13 +0000530int _PyFrame_Init()
Neal Norwitzc91ed402002-12-30 22:29:22 +0000531{
532 builtin_object = PyString_InternFromString("__builtins__");
533 return (builtin_object != NULL);
534}
535
Guido van Rossum18752471997-04-29 14:49:28 +0000536PyFrameObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000537PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000538 PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000539{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000540 PyFrameObject *back = tstate->frame;
Guido van Rossum18752471997-04-29 14:49:28 +0000541 PyFrameObject *f;
542 PyObject *builtins;
Armin Rigo75be0122004-03-20 21:10:27 +0000543 int extras, ncells, nfrees, i;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000544
Michael W. Hudson69734a52002-08-19 16:54:08 +0000545#ifdef Py_DEBUG
546 if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000547 (locals != NULL && !PyMapping_Check(locals))) {
Guido van Rossum18752471997-04-29 14:49:28 +0000548 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000549 return NULL;
550 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000551#endif
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000552 ncells = PyTuple_GET_SIZE(code->co_cellvars);
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000553 nfrees = PyTuple_GET_SIZE(code->co_freevars);
554 extras = code->co_stacksize + code->co_nlocals + ncells + nfrees;
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000555 if (back == NULL || back->f_globals != globals) {
556 builtins = PyDict_GetItem(globals, builtin_object);
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000557 if (builtins) {
558 if (PyModule_Check(builtins)) {
559 builtins = PyModule_GetDict(builtins);
560 assert(!builtins || PyDict_Check(builtins));
561 }
562 else if (!PyDict_Check(builtins))
563 builtins = NULL;
564 }
565 if (builtins == NULL) {
566 /* No builtins! Make up a minimal one
567 Give them 'None', at least. */
568 builtins = PyDict_New();
569 if (builtins == NULL ||
570 PyDict_SetItemString(
571 builtins, "None", Py_None) < 0)
572 return NULL;
573 }
574 else
575 Py_INCREF(builtins);
576
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000577 }
578 else {
579 /* If we share the globals, we share the builtins.
580 Save a lookup and a call. */
581 builtins = back->f_builtins;
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000582 assert(builtins != NULL && PyDict_Check(builtins));
583 Py_INCREF(builtins);
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000584 }
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000585 if (free_list == NULL) {
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000586 f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, extras);
Armin Rigo76beca92004-01-27 16:08:07 +0000587 if (f == NULL) {
588 Py_DECREF(builtins);
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000589 return NULL;
Armin Rigo76beca92004-01-27 16:08:07 +0000590 }
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000591 }
592 else {
Tim Petersb7ba7432002-04-13 05:21:47 +0000593 assert(numfree > 0);
594 --numfree;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000595 f = free_list;
596 free_list = free_list->f_back;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000597 if (f->ob_size < extras) {
598 f = PyObject_GC_Resize(PyFrameObject, f, extras);
Armin Rigo76beca92004-01-27 16:08:07 +0000599 if (f == NULL) {
600 Py_DECREF(builtins);
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000601 return NULL;
Armin Rigo76beca92004-01-27 16:08:07 +0000602 }
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000603 }
Tim Petersdeb77e82001-08-30 00:32:51 +0000604 _Py_NewReference((PyObject *)f);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000605 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000606 f->f_builtins = builtins;
Guido van Rossum18752471997-04-29 14:49:28 +0000607 Py_XINCREF(back);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000608 f->f_back = back;
Guido van Rossum18752471997-04-29 14:49:28 +0000609 Py_INCREF(code);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000610 f->f_code = code;
Guido van Rossum18752471997-04-29 14:49:28 +0000611 Py_INCREF(globals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000612 f->f_globals = globals;
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000613 /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
614 if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
615 (CO_NEWLOCALS | CO_OPTIMIZED))
Michael W. Hudsonbdc6ea12003-08-11 16:14:06 +0000616 locals = NULL; /* PyFrame_FastToLocals() will set. */
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000617 else if (code->co_flags & CO_NEWLOCALS) {
618 locals = PyDict_New();
619 if (locals == NULL) {
620 Py_DECREF(f);
621 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000622 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000623 }
Guido van Rossum2271bf71995-07-18 14:30:34 +0000624 else {
625 if (locals == NULL)
626 locals = globals;
Guido van Rossum18752471997-04-29 14:49:28 +0000627 Py_INCREF(locals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000628 }
629 f->f_locals = locals;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000630 f->f_trace = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000631 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000632 f->f_tstate = tstate;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000633
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000634 f->f_lasti = -1;
Guido van Rossum747596a1997-01-24 04:00:21 +0000635 f->f_lineno = code->co_firstlineno;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000636 f->f_restricted = (builtins != tstate->interp->builtins);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000637 f->f_iblock = 0;
638 f->f_nlocals = code->co_nlocals;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000639 f->f_stacksize = code->co_stacksize;
640 f->f_ncells = ncells;
641 f->f_nfreevars = nfrees;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000642
Guido van Rossumf4be4272002-08-01 18:50:33 +0000643 extras = f->f_nlocals + ncells + nfrees;
Armin Rigo75be0122004-03-20 21:10:27 +0000644 /* Tim said it's ok to replace memset */
645 for (i=0; i<extras; i++)
646 f->f_localsplus[i] = NULL;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000647
Guido van Rossumf4be4272002-08-01 18:50:33 +0000648 f->f_valuestack = f->f_localsplus + extras;
Tim Peters8c963692001-06-23 05:26:56 +0000649 f->f_stacktop = f->f_valuestack;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000650 _PyObject_GC_TRACK(f);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000651 return f;
652}
653
Guido van Rossum3f5da241990-12-20 15:06:42 +0000654/* Block management */
655
656void
Fred Drake1b190b42000-07-09 05:40:56 +0000657PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000658{
Guido van Rossum18752471997-04-29 14:49:28 +0000659 PyTryBlock *b;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000660 if (f->f_iblock >= CO_MAXBLOCKS)
Guido van Rossum18752471997-04-29 14:49:28 +0000661 Py_FatalError("XXX block stack overflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000662 b = &f->f_blockstack[f->f_iblock++];
663 b->b_type = type;
664 b->b_level = level;
665 b->b_handler = handler;
666}
667
Guido van Rossum18752471997-04-29 14:49:28 +0000668PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000669PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000670{
Guido van Rossum18752471997-04-29 14:49:28 +0000671 PyTryBlock *b;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000672 if (f->f_iblock <= 0)
Guido van Rossum18752471997-04-29 14:49:28 +0000673 Py_FatalError("XXX block stack underflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000674 b = &f->f_blockstack[--f->f_iblock];
675 return b;
676}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000677
678/* Convert between "fast" version of locals and dictionary version */
679
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000680static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000681map_to_dict(PyObject *map, int nmap, PyObject *dict, PyObject **values,
682 int deref)
683{
684 int j;
685 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000686 PyObject *key = PyTuple_GET_ITEM(map, j);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000687 PyObject *value = values[j];
688 if (deref)
689 value = PyCell_GET(value);
690 if (value == NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000691 if (PyObject_DelItem(dict, key) != 0)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000692 PyErr_Clear();
693 }
694 else {
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000695 if (PyObject_SetItem(dict, key, value) != 0)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000696 PyErr_Clear();
697 }
698 }
699}
700
Guido van Rossum6b356e72001-04-14 17:55:41 +0000701static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000702dict_to_map(PyObject *map, int nmap, PyObject *dict, PyObject **values,
703 int deref, int clear)
704{
705 int j;
706 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000707 PyObject *key = PyTuple_GET_ITEM(map, j);
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000708 PyObject *value = PyObject_GetItem(dict, key);
709 if (value == NULL)
710 PyErr_Clear();
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000711 if (deref) {
Jeremy Hylton4c889012001-05-08 04:08:59 +0000712 if (value || clear) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000713 if (PyCell_GET(values[j]) != value) {
714 if (PyCell_Set(values[j], value) < 0)
715 PyErr_Clear();
716 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000717 }
718 } else if (value != NULL || clear) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000719 if (values[j] != value) {
720 Py_XINCREF(value);
721 Py_XDECREF(values[j]);
722 values[j] = value;
723 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000724 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000725 Py_XDECREF(value);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000726 }
727}
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000728
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000729void
Fred Drake1b190b42000-07-09 05:40:56 +0000730PyFrame_FastToLocals(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000731{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000732 /* Merge fast locals into f->f_locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000733 PyObject *locals, *map;
734 PyObject **fast;
735 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000736 int j;
737 if (f == NULL)
738 return;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000739 locals = f->f_locals;
740 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000741 locals = f->f_locals = PyDict_New();
Guido van Rossum2271bf71995-07-18 14:30:34 +0000742 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000743 PyErr_Clear(); /* Can't report it :-( */
Guido van Rossum2271bf71995-07-18 14:30:34 +0000744 return;
745 }
746 }
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000747 map = f->f_code->co_varnames;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000748 if (!PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000749 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000750 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000751 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000752 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000753 if (j > f->f_nlocals)
754 j = f->f_nlocals;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000755 if (f->f_nlocals)
Jeremy Hylton174d2762003-10-21 18:10:28 +0000756 map_to_dict(map, j, locals, fast, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000757 if (f->f_ncells || f->f_nfreevars) {
758 if (!(PyTuple_Check(f->f_code->co_cellvars)
759 && PyTuple_Check(f->f_code->co_freevars))) {
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000760 return;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000761 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000762 map_to_dict(f->f_code->co_cellvars,
763 PyTuple_GET_SIZE(f->f_code->co_cellvars),
764 locals, fast + f->f_nlocals, 1);
765 map_to_dict(f->f_code->co_freevars,
766 PyTuple_GET_SIZE(f->f_code->co_freevars),
767 locals, fast + f->f_nlocals + f->f_ncells, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000768 }
Guido van Rossum18752471997-04-29 14:49:28 +0000769 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000770}
771
772void
Fred Drake1b190b42000-07-09 05:40:56 +0000773PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000774{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000775 /* Merge f->f_locals into fast locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000776 PyObject *locals, *map;
777 PyObject **fast;
778 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000779 int j;
780 if (f == NULL)
781 return;
782 locals = f->f_locals;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000783 map = f->f_code->co_varnames;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000784 if (locals == NULL)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000785 return;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000786 if (!PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000787 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000788 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000789 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000790 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000791 if (j > f->f_nlocals)
792 j = f->f_nlocals;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000793 if (f->f_nlocals)
794 dict_to_map(f->f_code->co_varnames, j, locals, fast, 0, clear);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000795 if (f->f_ncells || f->f_nfreevars) {
796 if (!(PyTuple_Check(f->f_code->co_cellvars)
797 && PyTuple_Check(f->f_code->co_freevars)))
798 return;
799 dict_to_map(f->f_code->co_cellvars,
800 PyTuple_GET_SIZE(f->f_code->co_cellvars),
Jeremy Hylton4c889012001-05-08 04:08:59 +0000801 locals, fast + f->f_nlocals, 1, clear);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000802 dict_to_map(f->f_code->co_freevars,
803 PyTuple_GET_SIZE(f->f_code->co_freevars),
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000804 locals, fast + f->f_nlocals + f->f_ncells, 1,
805 clear);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000806 }
Guido van Rossum18752471997-04-29 14:49:28 +0000807 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000808}
Guido van Rossum404b95d1997-08-05 02:09:46 +0000809
810/* Clear out the free list */
811
812void
Fred Drake1b190b42000-07-09 05:40:56 +0000813PyFrame_Fini(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +0000814{
815 while (free_list != NULL) {
816 PyFrameObject *f = free_list;
817 free_list = free_list->f_back;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000818 PyObject_GC_Del(f);
Tim Petersb7ba7432002-04-13 05:21:47 +0000819 --numfree;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000820 }
Tim Petersb7ba7432002-04-13 05:21:47 +0000821 assert(numfree == 0);
Neal Norwitzc91ed402002-12-30 22:29:22 +0000822 Py_XDECREF(builtin_object);
823 builtin_object = NULL;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000824}