blob: b9a481224f871f938478bbab0f955a65a0956e85 [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:
187 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
199 * 'finally' block. */
200 setup_op = code[blockstack[blockstack_top-1]];
201 if (setup_op == SETUP_FINALLY) {
202 blockstack_top--;
203 }
204 break;
205 }
206
207 /* For the addresses we're interested in, see whether they're
208 * within a 'finally' block and if so, remember the address
209 * of the SETUP_FINALLY. */
210 if (addr == new_lasti || addr == f->f_lasti) {
211 int i = 0;
212 int setup_addr = -1;
213 for (i = blockstack_top-1; i >= 0; i--) {
214 if (in_finally[i]) {
215 setup_addr = blockstack[i];
216 break;
217 }
218 }
219
220 if (setup_addr != -1) {
221 if (addr == new_lasti) {
222 new_lasti_setup_addr = setup_addr;
223 }
224
225 if (addr == f->f_lasti) {
226 f_lasti_setup_addr = setup_addr;
227 }
228 }
229 }
230
231 if (op >= HAVE_ARGUMENT) {
232 addr += 2;
233 }
234 }
235
236 if (new_lasti_setup_addr != f_lasti_setup_addr) {
237 PyErr_SetString(PyExc_ValueError,
238 "can't jump into or out of a 'finally' block");
239 return -1;
240 }
241
242
243 /* Police block-jumping (you can't jump into the middle of a block)
244 * and ensure that the blockstack finishes up in a sensible state (by
245 * popping any blocks we're jumping out of). We look at all the
246 * blockstack operations between the current position and the new
247 * one, and keep track of how many blocks we drop out of on the way.
248 * By also keeping track of the lowest blockstack position we see, we
249 * can tell whether the jump goes into any blocks without coming out
250 * again - in that case we raise an exception below. */
251 delta_iblock = 0;
252 for (addr = min_addr; addr < max_addr; addr++) {
253 unsigned char op = code[addr];
254 switch (op) {
255 case SETUP_LOOP:
256 case SETUP_EXCEPT:
257 case SETUP_FINALLY:
258 delta_iblock++;
259 break;
260
261 case POP_BLOCK:
262 delta_iblock--;
263 break;
264 }
265
266 min_delta_iblock = MIN(min_delta_iblock, delta_iblock);
267
268 if (op >= HAVE_ARGUMENT) {
269 addr += 2;
270 }
271 }
272
273 /* Derive the absolute iblock values from the deltas. */
274 min_iblock = f->f_iblock + min_delta_iblock;
275 if (new_lasti > f->f_lasti) {
276 /* Forwards jump. */
277 new_iblock = f->f_iblock + delta_iblock;
278 }
279 else {
280 /* Backwards jump. */
281 new_iblock = f->f_iblock - delta_iblock;
282 }
283
284 /* Are we jumping into a block? */
285 if (new_iblock > min_iblock) {
286 PyErr_SetString(PyExc_ValueError,
287 "can't jump into the middle of a block");
288 return -1;
289 }
290
291 /* Pop any blocks that we're jumping out of. */
292 while (f->f_iblock > new_iblock) {
293 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
294 while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
295 PyObject *v = (*--f->f_stacktop);
296 Py_DECREF(v);
297 }
298 }
299
300 /* Finally set the new f_lineno and f_lasti and return OK. */
301 f->f_lineno = new_lineno;
302 f->f_lasti = new_lasti;
303 return 0;
304}
305
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000306static PyObject *
307frame_gettrace(PyFrameObject *f, void *closure)
308{
309 PyObject* trace = f->f_trace;
310
311 if (trace == NULL)
312 trace = Py_None;
313
314 Py_INCREF(trace);
315
316 return trace;
317}
318
319static int
320frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
321{
322 /* We rely on f_lineno being accurate when f_trace is set. */
323
324 PyObject* old_value = f->f_trace;
325
326 Py_XINCREF(v);
327 f->f_trace = v;
328
329 if (v != NULL)
330 f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
331
332 Py_XDECREF(old_value);
333
334 return 0;
335}
336
Guido van Rossum32d34c82001-09-20 21:45:26 +0000337static PyGetSetDef frame_getsetlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000338 {"f_locals", (getter)frame_getlocals, NULL, NULL},
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000339 {"f_lineno", (getter)frame_getlineno,
340 (setter)frame_setlineno, NULL},
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000341 {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000342 {0}
343};
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000344
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000345/* Stack frames are allocated and deallocated at a considerable rate.
346 In an attempt to improve the speed of function calls, we maintain a
347 separate free list of stack frames (just like integers are
348 allocated in a special way -- see intobject.c). When a stack frame
349 is on the free list, only the following members have a meaning:
350 ob_type == &Frametype
351 f_back next item on free list, or NULL
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000352 f_nlocals number of locals
353 f_stacksize size of value stack
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000354 ob_size size of localsplus
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000355 Note that the value and block stacks are preserved -- this can save
356 another malloc() call or two (and two free() calls as well!).
357 Also note that, unlike for integers, each frame object is a
358 malloc'ed object in its own right -- it is only the actual calls to
359 malloc() that we are trying to save here, not the administration.
360 After all, while a typical program may make millions of calls, a
361 call depth of more than 20 or 30 is probably already exceptional
362 unless the program contains run-away recursion. I hope.
Tim Petersb7ba7432002-04-13 05:21:47 +0000363
364 Later, MAXFREELIST was added to bound the # of frames saved on
365 free_list. Else programs creating lots of cyclic trash involving
366 frames could provoke free_list into growing without bound.
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000367*/
368
Guido van Rossum18752471997-04-29 14:49:28 +0000369static PyFrameObject *free_list = NULL;
Tim Petersb7ba7432002-04-13 05:21:47 +0000370static int numfree = 0; /* number of frames currently in free_list */
371#define MAXFREELIST 200 /* max value for numfree */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000372
Guido van Rossum3f5da241990-12-20 15:06:42 +0000373static void
Fred Drake1b190b42000-07-09 05:40:56 +0000374frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000375{
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000376 int i, slots;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000377 PyObject **fastlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +0000378 PyObject **p;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000379
Guido van Rossumff413af2002-03-28 20:34:59 +0000380 PyObject_GC_UnTrack(f);
Guido van Rossumd724b232000-03-13 16:01:29 +0000381 Py_TRASHCAN_SAFE_BEGIN(f)
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000382 /* Kill all local variables */
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000383 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000384 fastlocals = f->f_localsplus;
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000385 for (i = slots; --i >= 0; ++fastlocals) {
Guido van Rossum18752471997-04-29 14:49:28 +0000386 Py_XDECREF(*fastlocals);
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000387 }
388
Tim Peters5ca576e2001-06-18 22:08:13 +0000389 /* Free stack */
Tim Peters8c963692001-06-23 05:26:56 +0000390 if (f->f_stacktop != NULL) {
391 for (p = f->f_valuestack; p < f->f_stacktop; p++)
392 Py_XDECREF(*p);
Tim Peters5ca576e2001-06-18 22:08:13 +0000393 }
Tim Peters8c963692001-06-23 05:26:56 +0000394
Guido van Rossum18752471997-04-29 14:49:28 +0000395 Py_XDECREF(f->f_back);
396 Py_XDECREF(f->f_code);
397 Py_XDECREF(f->f_builtins);
398 Py_XDECREF(f->f_globals);
399 Py_XDECREF(f->f_locals);
400 Py_XDECREF(f->f_trace);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000401 Py_XDECREF(f->f_exc_type);
402 Py_XDECREF(f->f_exc_value);
403 Py_XDECREF(f->f_exc_traceback);
Tim Petersb7ba7432002-04-13 05:21:47 +0000404 if (numfree < MAXFREELIST) {
405 ++numfree;
406 f->f_back = free_list;
407 free_list = f;
408 }
409 else
410 PyObject_GC_Del(f);
Guido van Rossumd724b232000-03-13 16:01:29 +0000411 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000412}
413
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000414static int
415frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
416{
417 PyObject **fastlocals, **p;
418 int i, err, slots;
419#define VISIT(o) if (o) {if ((err = visit((PyObject *)(o), arg))) return err;}
420
421 VISIT(f->f_back);
422 VISIT(f->f_code);
423 VISIT(f->f_builtins);
424 VISIT(f->f_globals);
425 VISIT(f->f_locals);
426 VISIT(f->f_trace);
427 VISIT(f->f_exc_type);
428 VISIT(f->f_exc_value);
429 VISIT(f->f_exc_traceback);
430
431 /* locals */
432 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
433 fastlocals = f->f_localsplus;
434 for (i = slots; --i >= 0; ++fastlocals) {
435 VISIT(*fastlocals);
436 }
437
438 /* stack */
439 if (f->f_stacktop != NULL) {
440 for (p = f->f_valuestack; p < f->f_stacktop; p++)
441 VISIT(*p);
442 }
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000443 return 0;
444}
445
446static void
447frame_clear(PyFrameObject *f)
448{
449 PyObject **fastlocals, **p;
450 int i, slots;
451
452 Py_XDECREF(f->f_exc_type);
453 f->f_exc_type = NULL;
454
455 Py_XDECREF(f->f_exc_value);
456 f->f_exc_value = NULL;
457
458 Py_XDECREF(f->f_exc_traceback);
459 f->f_exc_traceback = NULL;
460
461 Py_XDECREF(f->f_trace);
462 f->f_trace = NULL;
463
464 /* locals */
465 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
466 fastlocals = f->f_localsplus;
467 for (i = slots; --i >= 0; ++fastlocals) {
468 if (*fastlocals != NULL) {
469 Py_XDECREF(*fastlocals);
470 *fastlocals = NULL;
471 }
472 }
473
474 /* stack */
475 if (f->f_stacktop != NULL) {
476 for (p = f->f_valuestack; p < f->f_stacktop; p++) {
477 Py_XDECREF(*p);
478 *p = NULL;
479 }
480 }
481}
482
483
Guido van Rossum18752471997-04-29 14:49:28 +0000484PyTypeObject PyFrame_Type = {
485 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000486 0,
487 "frame",
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000488 sizeof(PyFrameObject),
489 sizeof(PyObject *),
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000490 (destructor)frame_dealloc, /* tp_dealloc */
491 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000492 0, /* tp_getattr */
493 0, /* tp_setattr */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000494 0, /* tp_compare */
495 0, /* tp_repr */
496 0, /* tp_as_number */
497 0, /* tp_as_sequence */
498 0, /* tp_as_mapping */
499 0, /* tp_hash */
500 0, /* tp_call */
501 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000502 PyObject_GenericGetAttr, /* tp_getattro */
503 PyObject_GenericSetAttr, /* tp_setattro */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000504 0, /* tp_as_buffer */
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000505 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000506 0, /* tp_doc */
507 (traverseproc)frame_traverse, /* tp_traverse */
508 (inquiry)frame_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000509 0, /* tp_richcompare */
510 0, /* tp_weaklistoffset */
511 0, /* tp_iter */
512 0, /* tp_iternext */
513 0, /* tp_methods */
514 frame_memberlist, /* tp_members */
515 frame_getsetlist, /* tp_getset */
516 0, /* tp_base */
517 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000518};
519
Guido van Rossum18752471997-04-29 14:49:28 +0000520PyFrameObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000521PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000522 PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000523{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000524 PyFrameObject *back = tstate->frame;
Guido van Rossum18752471997-04-29 14:49:28 +0000525 static PyObject *builtin_object;
526 PyFrameObject *f;
527 PyObject *builtins;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000528 int extras, ncells, nfrees;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000529
Sjoerd Mullender5b7f3cd1995-04-04 11:47:41 +0000530 if (builtin_object == NULL) {
Guido van Rossumb56933e1997-01-18 07:58:41 +0000531 builtin_object = PyString_InternFromString("__builtins__");
Sjoerd Mullender5b7f3cd1995-04-04 11:47:41 +0000532 if (builtin_object == NULL)
533 return NULL;
534 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000535#ifdef Py_DEBUG
536 if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
Guido van Rossum18752471997-04-29 14:49:28 +0000537 (locals != NULL && !PyDict_Check(locals))) {
538 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000539 return NULL;
540 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000541#endif
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000542 ncells = PyTuple_GET_SIZE(code->co_cellvars);
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000543 nfrees = PyTuple_GET_SIZE(code->co_freevars);
544 extras = code->co_stacksize + code->co_nlocals + ncells + nfrees;
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000545 if (back == NULL || back->f_globals != globals) {
546 builtins = PyDict_GetItem(globals, builtin_object);
547 if (builtins != NULL && PyModule_Check(builtins))
548 builtins = PyModule_GetDict(builtins);
549 }
550 else {
551 /* If we share the globals, we share the builtins.
552 Save a lookup and a call. */
553 builtins = back->f_builtins;
554 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000555 if (builtins != NULL && !PyDict_Check(builtins))
556 builtins = NULL;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000557 if (free_list == NULL) {
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000558 f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, extras);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000559 if (f == NULL)
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000560 return NULL;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000561 }
562 else {
Tim Petersb7ba7432002-04-13 05:21:47 +0000563 assert(numfree > 0);
564 --numfree;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000565 f = free_list;
566 free_list = free_list->f_back;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000567 if (f->ob_size < extras) {
568 f = PyObject_GC_Resize(PyFrameObject, f, extras);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000569 if (f == NULL)
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000570 return NULL;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000571 }
Tim Petersdeb77e82001-08-30 00:32:51 +0000572 _Py_NewReference((PyObject *)f);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000573 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000574 if (builtins == NULL) {
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000575 /* No builtins! Make up a minimal one. */
Guido van Rossum404b95d1997-08-05 02:09:46 +0000576 builtins = PyDict_New();
Guido van Rossumf61618c1998-10-19 14:20:20 +0000577 if (builtins == NULL || /* Give them 'None', at least. */
578 PyDict_SetItemString(builtins, "None", Py_None) < 0) {
579 Py_DECREF(f);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000580 return NULL;
Guido van Rossumf61618c1998-10-19 14:20:20 +0000581 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000582 }
583 else
Neal Norwitzd94c28e2002-08-29 20:25:46 +0000584 Py_INCREF(builtins);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000585 f->f_builtins = builtins;
Guido van Rossum18752471997-04-29 14:49:28 +0000586 Py_XINCREF(back);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000587 f->f_back = back;
Guido van Rossum18752471997-04-29 14:49:28 +0000588 Py_INCREF(code);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000589 f->f_code = code;
Guido van Rossum18752471997-04-29 14:49:28 +0000590 Py_INCREF(globals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000591 f->f_globals = globals;
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000592 if (code->co_flags & CO_NEWLOCALS) {
593 if (code->co_flags & CO_OPTIMIZED)
594 locals = NULL; /* Let fast_2_locals handle it */
595 else {
Guido van Rossum18752471997-04-29 14:49:28 +0000596 locals = PyDict_New();
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000597 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000598 Py_DECREF(f);
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000599 return NULL;
600 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000601 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000602 }
Guido van Rossum2271bf71995-07-18 14:30:34 +0000603 else {
604 if (locals == NULL)
605 locals = globals;
Guido van Rossum18752471997-04-29 14:49:28 +0000606 Py_INCREF(locals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000607 }
608 f->f_locals = locals;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000609 f->f_trace = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000610 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000611 f->f_tstate = tstate;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000612
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000613 f->f_lasti = -1;
Guido van Rossum747596a1997-01-24 04:00:21 +0000614 f->f_lineno = code->co_firstlineno;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000615 f->f_restricted = (builtins != tstate->interp->builtins);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000616 f->f_iblock = 0;
617 f->f_nlocals = code->co_nlocals;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000618 f->f_stacksize = code->co_stacksize;
619 f->f_ncells = ncells;
620 f->f_nfreevars = nfrees;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000621
Guido van Rossumf4be4272002-08-01 18:50:33 +0000622 extras = f->f_nlocals + ncells + nfrees;
623 memset(f->f_localsplus, 0, extras * sizeof(f->f_localsplus[0]));
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000624
Guido van Rossumf4be4272002-08-01 18:50:33 +0000625 f->f_valuestack = f->f_localsplus + extras;
Tim Peters8c963692001-06-23 05:26:56 +0000626 f->f_stacktop = f->f_valuestack;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000627 _PyObject_GC_TRACK(f);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000628 return f;
629}
630
Guido van Rossum3f5da241990-12-20 15:06:42 +0000631/* Block management */
632
633void
Fred Drake1b190b42000-07-09 05:40:56 +0000634PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000635{
Guido van Rossum18752471997-04-29 14:49:28 +0000636 PyTryBlock *b;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000637 if (f->f_iblock >= CO_MAXBLOCKS)
Guido van Rossum18752471997-04-29 14:49:28 +0000638 Py_FatalError("XXX block stack overflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000639 b = &f->f_blockstack[f->f_iblock++];
640 b->b_type = type;
641 b->b_level = level;
642 b->b_handler = handler;
643}
644
Guido van Rossum18752471997-04-29 14:49:28 +0000645PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000646PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000647{
Guido van Rossum18752471997-04-29 14:49:28 +0000648 PyTryBlock *b;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000649 if (f->f_iblock <= 0)
Guido van Rossum18752471997-04-29 14:49:28 +0000650 Py_FatalError("XXX block stack underflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000651 b = &f->f_blockstack[--f->f_iblock];
652 return b;
653}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000654
655/* Convert between "fast" version of locals and dictionary version */
656
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000657static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000658map_to_dict(PyObject *map, int nmap, PyObject *dict, PyObject **values,
659 int deref)
660{
661 int j;
662 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000663 PyObject *key = PyTuple_GET_ITEM(map, j);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000664 PyObject *value = values[j];
665 if (deref)
666 value = PyCell_GET(value);
667 if (value == NULL) {
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000668 if (PyDict_DelItem(dict, key) != 0)
669 PyErr_Clear();
670 }
671 else {
672 if (PyDict_SetItem(dict, key, value) != 0)
673 PyErr_Clear();
674 }
675 }
676}
677
Guido van Rossum6b356e72001-04-14 17:55:41 +0000678static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000679dict_to_map(PyObject *map, int nmap, PyObject *dict, PyObject **values,
680 int deref, int clear)
681{
682 int j;
683 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000684 PyObject *key = PyTuple_GET_ITEM(map, j);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000685 PyObject *value = PyDict_GetItem(dict, key);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000686 if (deref) {
Jeremy Hylton4c889012001-05-08 04:08:59 +0000687 if (value || clear) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000688 if (PyCell_GET(values[j]) != value) {
689 if (PyCell_Set(values[j], value) < 0)
690 PyErr_Clear();
691 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000692 }
693 } else if (value != NULL || clear) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000694 if (values[j] != value) {
695 Py_XINCREF(value);
696 Py_XDECREF(values[j]);
697 values[j] = value;
698 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000699 }
700 }
701}
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000702
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000703void
Fred Drake1b190b42000-07-09 05:40:56 +0000704PyFrame_FastToLocals(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000705{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000706 /* Merge fast locals into f->f_locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000707 PyObject *locals, *map;
708 PyObject **fast;
709 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000710 int j;
711 if (f == NULL)
712 return;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000713 locals = f->f_locals;
714 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000715 locals = f->f_locals = PyDict_New();
Guido van Rossum2271bf71995-07-18 14:30:34 +0000716 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000717 PyErr_Clear(); /* Can't report it :-( */
Guido van Rossum2271bf71995-07-18 14:30:34 +0000718 return;
719 }
720 }
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000721 map = f->f_code->co_varnames;
Guido van Rossum18752471997-04-29 14:49:28 +0000722 if (!PyDict_Check(locals) || !PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000723 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000724 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000725 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000726 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000727 if (j > f->f_nlocals)
728 j = f->f_nlocals;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000729 if (f->f_nlocals)
730 map_to_dict(map, j, locals, fast, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000731 if (f->f_ncells || f->f_nfreevars) {
732 if (!(PyTuple_Check(f->f_code->co_cellvars)
733 && PyTuple_Check(f->f_code->co_freevars))) {
734 Py_DECREF(locals);
735 return;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000736 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000737 map_to_dict(f->f_code->co_cellvars,
738 PyTuple_GET_SIZE(f->f_code->co_cellvars),
739 locals, fast + f->f_nlocals, 1);
740 map_to_dict(f->f_code->co_freevars,
741 PyTuple_GET_SIZE(f->f_code->co_freevars),
742 locals, fast + f->f_nlocals + f->f_ncells, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000743 }
Guido van Rossum18752471997-04-29 14:49:28 +0000744 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000745}
746
747void
Fred Drake1b190b42000-07-09 05:40:56 +0000748PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000749{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000750 /* Merge f->f_locals into fast locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000751 PyObject *locals, *map;
752 PyObject **fast;
753 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000754 int j;
755 if (f == NULL)
756 return;
757 locals = f->f_locals;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000758 map = f->f_code->co_varnames;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000759 if (locals == NULL)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000760 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000761 if (!PyDict_Check(locals) || !PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000762 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000763 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000764 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000765 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000766 if (j > f->f_nlocals)
767 j = f->f_nlocals;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000768 if (f->f_nlocals)
769 dict_to_map(f->f_code->co_varnames, j, locals, fast, 0, clear);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000770 if (f->f_ncells || f->f_nfreevars) {
771 if (!(PyTuple_Check(f->f_code->co_cellvars)
772 && PyTuple_Check(f->f_code->co_freevars)))
773 return;
774 dict_to_map(f->f_code->co_cellvars,
775 PyTuple_GET_SIZE(f->f_code->co_cellvars),
Jeremy Hylton4c889012001-05-08 04:08:59 +0000776 locals, fast + f->f_nlocals, 1, clear);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000777 dict_to_map(f->f_code->co_freevars,
778 PyTuple_GET_SIZE(f->f_code->co_freevars),
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000779 locals, fast + f->f_nlocals + f->f_ncells, 1,
780 clear);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000781 }
Guido van Rossum18752471997-04-29 14:49:28 +0000782 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000783}
Guido van Rossum404b95d1997-08-05 02:09:46 +0000784
785/* Clear out the free list */
786
787void
Fred Drake1b190b42000-07-09 05:40:56 +0000788PyFrame_Fini(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +0000789{
790 while (free_list != NULL) {
791 PyFrameObject *f = free_list;
792 free_list = free_list->f_back;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000793 PyObject_GC_Del(f);
Tim Petersb7ba7432002-04-13 05:21:47 +0000794 --numfree;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000795 }
Tim Petersb7ba7432002-04-13 05:21:47 +0000796 assert(numfree == 0);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000797}