blob: b7b30214dfd56e3446532d97fe1d511426eeee37 [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
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000011#define MIN(a, b) ((a) < (b) ? (a) : (b))
12#define MAX(a, b) ((a) > (b) ? (a) : (b))
13
Guido van Rossum18752471997-04-29 14:49:28 +000014#define OFF(x) offsetof(PyFrameObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000015
Guido van Rossum6f799372001-09-20 20:46:19 +000016static PyMemberDef frame_memberlist[] = {
Guido van Rossum1d5735e1994-08-30 08:27:36 +000017 {"f_back", T_OBJECT, OFF(f_back), RO},
18 {"f_code", T_OBJECT, OFF(f_code), RO},
Guido van Rossumc1134821995-01-10 10:39:16 +000019 {"f_builtins", T_OBJECT, OFF(f_builtins),RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000020 {"f_globals", T_OBJECT, OFF(f_globals), RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000021 {"f_lasti", T_INT, OFF(f_lasti), RO},
Guido van Rossumc1134821995-01-10 10:39:16 +000022 {"f_restricted",T_INT, OFF(f_restricted),RO},
Guido van Rossuma027efa1997-05-05 20:56:21 +000023 {"f_exc_type", T_OBJECT, OFF(f_exc_type)},
24 {"f_exc_value", T_OBJECT, OFF(f_exc_value)},
25 {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)},
Guido van Rossum3f5da241990-12-20 15:06:42 +000026 {NULL} /* Sentinel */
27};
28
Guido van Rossum18752471997-04-29 14:49:28 +000029static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000030frame_getlocals(PyFrameObject *f, void *closure)
Guido van Rossum3f5da241990-12-20 15:06:42 +000031{
Tim Peters6d6c1a32001-08-02 04:15:00 +000032 PyFrame_FastToLocals(f);
33 Py_INCREF(f->f_locals);
34 return f->f_locals;
Guido van Rossum3f5da241990-12-20 15:06:42 +000035}
36
Michael W. Hudsondd32a912002-08-15 14:59:02 +000037static PyObject *
38frame_getlineno(PyFrameObject *f, void *closure)
39{
40 int lineno;
41
Michael W. Hudson02ff6a92002-09-11 15:36:32 +000042 if (f->f_trace)
43 lineno = f->f_lineno;
44 else
45 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000046
47 return PyInt_FromLong(lineno);
48}
49
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000050/* Setter for f_lineno - you can set f_lineno from within a trace function in
51 * order to jump to a given line of code, subject to some restrictions. Most
52 * lines are OK to jump to because they don't make any assumptions about the
53 * state of the stack (obvious because you could remove the line and the code
54 * would still work without any stack errors), but there are some constructs
55 * that limit jumping:
56 *
57 * o Lines with an 'except' statement on them can't be jumped to, because
58 * they expect an exception to be on the top of the stack.
59 * o Lines that live in a 'finally' block can't be jumped from or to, since
60 * the END_FINALLY expects to clean up the stack after the 'try' block.
61 * o 'try'/'for'/'while' blocks can't be jumped into because the blockstack
62 * needs to be set up before their code runs, and for 'for' loops the
63 * iterator needs to be on the stack.
64 */
65static int
66frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
67{
68 int new_lineno = 0; /* The new value of f_lineno */
69 int new_lasti = 0; /* The new value of f_lasti */
70 int new_iblock = 0; /* The new value of f_iblock */
71 char *code = NULL; /* The bytecode for the frame... */
72 int code_len = 0; /* ...and its length */
73 char *lnotab = NULL; /* Iterating over co_lnotab */
74 int lnotab_len = 0; /* (ditto) */
75 int offset = 0; /* (ditto) */
76 int line = 0; /* (ditto) */
77 int addr = 0; /* (ditto) */
78 int min_addr = 0; /* Scanning the SETUPs and POPs */
79 int max_addr = 0; /* (ditto) */
80 int delta_iblock = 0; /* (ditto) */
81 int min_delta_iblock = 0; /* (ditto) */
82 int min_iblock = 0; /* (ditto) */
83 int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */
84 int new_lasti_setup_addr = 0; /* (ditto) */
85 int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */
86 int in_finally[CO_MAXBLOCKS]; /* (ditto) */
87 int blockstack_top = 0; /* (ditto) */
88 int setup_op = 0; /* (ditto) */
89
90 /* f_lineno must be an integer. */
91 if (!PyInt_Check(p_new_lineno)) {
92 PyErr_SetString(PyExc_ValueError,
93 "lineno must be an integer");
94 return -1;
95 }
96
97 /* You can only do this from within a trace function, not via
98 * _getframe or similar hackery. */
99 if (!f->f_trace)
100 {
101 PyErr_Format(PyExc_ValueError,
102 "f_lineno can only be set by a trace function");
103 return -1;
104 }
105
106 /* Fail if the line comes before the start of the code block. */
107 new_lineno = (int) PyInt_AsLong(p_new_lineno);
108 if (new_lineno < f->f_code->co_firstlineno) {
109 PyErr_Format(PyExc_ValueError,
110 "line %d comes before the current code block",
111 new_lineno);
112 return -1;
113 }
114
115 /* Find the bytecode offset for the start of the given line, or the
116 * first code-owning line after it. */
117 PyString_AsStringAndSize(f->f_code->co_lnotab, &lnotab, &lnotab_len);
118 addr = 0;
119 line = f->f_code->co_firstlineno;
120 new_lasti = -1;
121 for (offset = 0; offset < lnotab_len; offset += 2) {
122 addr += lnotab[offset];
123 line += lnotab[offset+1];
124 if (line >= new_lineno) {
125 new_lasti = addr;
126 new_lineno = line;
127 break;
128 }
129 }
130
131 /* If we didn't reach the requested line, return an error. */
132 if (new_lasti == -1) {
133 PyErr_Format(PyExc_ValueError,
134 "line %d comes after the current code block",
135 new_lineno);
136 return -1;
137 }
138
139 /* We're now ready to look at the bytecode. */
140 PyString_AsStringAndSize(f->f_code->co_code, &code, &code_len);
141 min_addr = MIN(new_lasti, f->f_lasti);
142 max_addr = MAX(new_lasti, f->f_lasti);
143
144 /* You can't jump onto a line with an 'except' statement on it -
145 * they expect to have an exception on the top of the stack, which
146 * won't be true if you jump to them. They always start with code
147 * that either pops the exception using POP_TOP (plain 'except:'
148 * lines do this) or duplicates the exception on the stack using
149 * DUP_TOP (if there's an exception type specified). See compile.c,
150 * 'com_try_except' for the full details. There aren't any other
151 * cases (AFAIK) where a line's code can start with DUP_TOP or
152 * POP_TOP, but if any ever appear, they'll be subject to the same
153 * restriction (but with a different error message). */
154 if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
155 PyErr_SetString(PyExc_ValueError,
156 "can't jump to 'except' line as there's no exception");
157 return -1;
158 }
159
160 /* You can't jump into or out of a 'finally' block because the 'try'
161 * block leaves something on the stack for the END_FINALLY to clean
162 * up. So we walk the bytecode, maintaining a simulated blockstack.
163 * When we reach the old or new address and it's in a 'finally' block
164 * we note the address of the corresponding SETUP_FINALLY. The jump
165 * is only legal if neither address is in a 'finally' block or
166 * they're both in the same one. 'blockstack' is a stack of the
167 * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks
168 * whether we're in a 'finally' block at each blockstack level. */
169 f_lasti_setup_addr = -1;
170 new_lasti_setup_addr = -1;
171 memset(blockstack, '\0', sizeof(blockstack));
172 memset(in_finally, '\0', sizeof(in_finally));
173 blockstack_top = 0;
174 for (addr = 0; addr < code_len; addr++) {
175 unsigned char op = code[addr];
176 switch (op) {
177 case SETUP_LOOP:
178 case SETUP_EXCEPT:
179 case SETUP_FINALLY:
180 blockstack[blockstack_top++] = addr;
181 in_finally[blockstack_top-1] = 0;
182 break;
183
184 case POP_BLOCK:
185 setup_op = code[blockstack[blockstack_top-1]];
186 if (setup_op == SETUP_FINALLY) {
187 in_finally[blockstack_top-1] = 1;
188 }
189 else {
190 blockstack_top--;
191 }
192 break;
193
194 case END_FINALLY:
195 /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist
196 * in the bytecode but don't correspond to an actual
197 * 'finally' block. */
198 setup_op = code[blockstack[blockstack_top-1]];
199 if (setup_op == SETUP_FINALLY) {
200 blockstack_top--;
201 }
202 break;
203 }
204
205 /* For the addresses we're interested in, see whether they're
206 * within a 'finally' block and if so, remember the address
207 * of the SETUP_FINALLY. */
208 if (addr == new_lasti || addr == f->f_lasti) {
209 int i = 0;
210 int setup_addr = -1;
211 for (i = blockstack_top-1; i >= 0; i--) {
212 if (in_finally[i]) {
213 setup_addr = blockstack[i];
214 break;
215 }
216 }
217
218 if (setup_addr != -1) {
219 if (addr == new_lasti) {
220 new_lasti_setup_addr = setup_addr;
221 }
222
223 if (addr == f->f_lasti) {
224 f_lasti_setup_addr = setup_addr;
225 }
226 }
227 }
228
229 if (op >= HAVE_ARGUMENT) {
230 addr += 2;
231 }
232 }
233
234 if (new_lasti_setup_addr != f_lasti_setup_addr) {
235 PyErr_SetString(PyExc_ValueError,
236 "can't jump into or out of a 'finally' block");
237 return -1;
238 }
239
240
241 /* Police block-jumping (you can't jump into the middle of a block)
242 * and ensure that the blockstack finishes up in a sensible state (by
243 * popping any blocks we're jumping out of). We look at all the
244 * blockstack operations between the current position and the new
245 * one, and keep track of how many blocks we drop out of on the way.
246 * By also keeping track of the lowest blockstack position we see, we
247 * can tell whether the jump goes into any blocks without coming out
248 * again - in that case we raise an exception below. */
249 delta_iblock = 0;
250 for (addr = min_addr; addr < max_addr; addr++) {
251 unsigned char op = code[addr];
252 switch (op) {
253 case SETUP_LOOP:
254 case SETUP_EXCEPT:
255 case SETUP_FINALLY:
256 delta_iblock++;
257 break;
258
259 case POP_BLOCK:
260 delta_iblock--;
261 break;
262 }
263
264 min_delta_iblock = MIN(min_delta_iblock, delta_iblock);
265
266 if (op >= HAVE_ARGUMENT) {
267 addr += 2;
268 }
269 }
270
271 /* Derive the absolute iblock values from the deltas. */
272 min_iblock = f->f_iblock + min_delta_iblock;
273 if (new_lasti > f->f_lasti) {
274 /* Forwards jump. */
275 new_iblock = f->f_iblock + delta_iblock;
276 }
277 else {
278 /* Backwards jump. */
279 new_iblock = f->f_iblock - delta_iblock;
280 }
281
282 /* Are we jumping into a block? */
283 if (new_iblock > min_iblock) {
284 PyErr_SetString(PyExc_ValueError,
285 "can't jump into the middle of a block");
286 return -1;
287 }
288
289 /* Pop any blocks that we're jumping out of. */
290 while (f->f_iblock > new_iblock) {
291 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
292 while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
293 PyObject *v = (*--f->f_stacktop);
294 Py_DECREF(v);
295 }
296 }
297
298 /* Finally set the new f_lineno and f_lasti and return OK. */
299 f->f_lineno = new_lineno;
300 f->f_lasti = new_lasti;
301 return 0;
302}
303
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000304static PyObject *
305frame_gettrace(PyFrameObject *f, void *closure)
306{
307 PyObject* trace = f->f_trace;
308
309 if (trace == NULL)
310 trace = Py_None;
311
312 Py_INCREF(trace);
313
314 return trace;
315}
316
317static int
318frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
319{
320 /* We rely on f_lineno being accurate when f_trace is set. */
321
322 PyObject* old_value = f->f_trace;
323
324 Py_XINCREF(v);
325 f->f_trace = v;
326
327 if (v != NULL)
328 f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
329
330 Py_XDECREF(old_value);
331
332 return 0;
333}
334
Guido van Rossum32d34c82001-09-20 21:45:26 +0000335static PyGetSetDef frame_getsetlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000336 {"f_locals", (getter)frame_getlocals, NULL, NULL},
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000337 {"f_lineno", (getter)frame_getlineno,
338 (setter)frame_setlineno, NULL},
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000339 {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000340 {0}
341};
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000342
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000343/* Stack frames are allocated and deallocated at a considerable rate.
344 In an attempt to improve the speed of function calls, we maintain a
345 separate free list of stack frames (just like integers are
346 allocated in a special way -- see intobject.c). When a stack frame
347 is on the free list, only the following members have a meaning:
348 ob_type == &Frametype
349 f_back next item on free list, or NULL
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000350 f_nlocals number of locals
351 f_stacksize size of value stack
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000352 ob_size size of localsplus
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000353 Note that the value and block stacks are preserved -- this can save
354 another malloc() call or two (and two free() calls as well!).
355 Also note that, unlike for integers, each frame object is a
356 malloc'ed object in its own right -- it is only the actual calls to
357 malloc() that we are trying to save here, not the administration.
358 After all, while a typical program may make millions of calls, a
359 call depth of more than 20 or 30 is probably already exceptional
360 unless the program contains run-away recursion. I hope.
Tim Petersb7ba7432002-04-13 05:21:47 +0000361
362 Later, MAXFREELIST was added to bound the # of frames saved on
363 free_list. Else programs creating lots of cyclic trash involving
364 frames could provoke free_list into growing without bound.
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000365*/
366
Guido van Rossum18752471997-04-29 14:49:28 +0000367static PyFrameObject *free_list = NULL;
Tim Petersb7ba7432002-04-13 05:21:47 +0000368static int numfree = 0; /* number of frames currently in free_list */
369#define MAXFREELIST 200 /* max value for numfree */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000370
Guido van Rossum3f5da241990-12-20 15:06:42 +0000371static void
Fred Drake1b190b42000-07-09 05:40:56 +0000372frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000373{
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000374 int i, slots;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000375 PyObject **fastlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +0000376 PyObject **p;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000377
Guido van Rossumff413af2002-03-28 20:34:59 +0000378 PyObject_GC_UnTrack(f);
Guido van Rossumd724b232000-03-13 16:01:29 +0000379 Py_TRASHCAN_SAFE_BEGIN(f)
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000380 /* Kill all local variables */
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000381 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000382 fastlocals = f->f_localsplus;
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000383 for (i = slots; --i >= 0; ++fastlocals) {
Guido van Rossum18752471997-04-29 14:49:28 +0000384 Py_XDECREF(*fastlocals);
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000385 }
386
Tim Peters5ca576e2001-06-18 22:08:13 +0000387 /* Free stack */
Tim Peters8c963692001-06-23 05:26:56 +0000388 if (f->f_stacktop != NULL) {
389 for (p = f->f_valuestack; p < f->f_stacktop; p++)
390 Py_XDECREF(*p);
Tim Peters5ca576e2001-06-18 22:08:13 +0000391 }
Tim Peters8c963692001-06-23 05:26:56 +0000392
Guido van Rossum18752471997-04-29 14:49:28 +0000393 Py_XDECREF(f->f_back);
394 Py_XDECREF(f->f_code);
395 Py_XDECREF(f->f_builtins);
396 Py_XDECREF(f->f_globals);
397 Py_XDECREF(f->f_locals);
398 Py_XDECREF(f->f_trace);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000399 Py_XDECREF(f->f_exc_type);
400 Py_XDECREF(f->f_exc_value);
401 Py_XDECREF(f->f_exc_traceback);
Tim Petersb7ba7432002-04-13 05:21:47 +0000402 if (numfree < MAXFREELIST) {
403 ++numfree;
404 f->f_back = free_list;
405 free_list = f;
406 }
407 else
408 PyObject_GC_Del(f);
Guido van Rossumd724b232000-03-13 16:01:29 +0000409 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000410}
411
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000412static int
413frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
414{
415 PyObject **fastlocals, **p;
416 int i, err, slots;
417#define VISIT(o) if (o) {if ((err = visit((PyObject *)(o), arg))) return err;}
418
419 VISIT(f->f_back);
420 VISIT(f->f_code);
421 VISIT(f->f_builtins);
422 VISIT(f->f_globals);
423 VISIT(f->f_locals);
424 VISIT(f->f_trace);
425 VISIT(f->f_exc_type);
426 VISIT(f->f_exc_value);
427 VISIT(f->f_exc_traceback);
428
429 /* locals */
430 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
431 fastlocals = f->f_localsplus;
432 for (i = slots; --i >= 0; ++fastlocals) {
433 VISIT(*fastlocals);
434 }
435
436 /* stack */
437 if (f->f_stacktop != NULL) {
438 for (p = f->f_valuestack; p < f->f_stacktop; p++)
439 VISIT(*p);
440 }
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000441 return 0;
442}
443
444static void
445frame_clear(PyFrameObject *f)
446{
447 PyObject **fastlocals, **p;
448 int i, slots;
449
450 Py_XDECREF(f->f_exc_type);
451 f->f_exc_type = NULL;
452
453 Py_XDECREF(f->f_exc_value);
454 f->f_exc_value = NULL;
455
456 Py_XDECREF(f->f_exc_traceback);
457 f->f_exc_traceback = NULL;
458
459 Py_XDECREF(f->f_trace);
460 f->f_trace = NULL;
461
462 /* locals */
463 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
464 fastlocals = f->f_localsplus;
465 for (i = slots; --i >= 0; ++fastlocals) {
466 if (*fastlocals != NULL) {
467 Py_XDECREF(*fastlocals);
468 *fastlocals = NULL;
469 }
470 }
471
472 /* stack */
473 if (f->f_stacktop != NULL) {
474 for (p = f->f_valuestack; p < f->f_stacktop; p++) {
475 Py_XDECREF(*p);
476 *p = NULL;
477 }
478 }
479}
480
481
Guido van Rossum18752471997-04-29 14:49:28 +0000482PyTypeObject PyFrame_Type = {
483 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000484 0,
485 "frame",
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000486 sizeof(PyFrameObject),
487 sizeof(PyObject *),
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000488 (destructor)frame_dealloc, /* tp_dealloc */
489 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000490 0, /* tp_getattr */
491 0, /* tp_setattr */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000492 0, /* tp_compare */
493 0, /* tp_repr */
494 0, /* tp_as_number */
495 0, /* tp_as_sequence */
496 0, /* tp_as_mapping */
497 0, /* tp_hash */
498 0, /* tp_call */
499 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000500 PyObject_GenericGetAttr, /* tp_getattro */
501 PyObject_GenericSetAttr, /* tp_setattro */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000502 0, /* tp_as_buffer */
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000503 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000504 0, /* tp_doc */
505 (traverseproc)frame_traverse, /* tp_traverse */
506 (inquiry)frame_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000507 0, /* tp_richcompare */
508 0, /* tp_weaklistoffset */
509 0, /* tp_iter */
510 0, /* tp_iternext */
511 0, /* tp_methods */
512 frame_memberlist, /* tp_members */
513 frame_getsetlist, /* tp_getset */
514 0, /* tp_base */
515 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000516};
517
Guido van Rossum18752471997-04-29 14:49:28 +0000518PyFrameObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000519PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000520 PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000521{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000522 PyFrameObject *back = tstate->frame;
Guido van Rossum18752471997-04-29 14:49:28 +0000523 static PyObject *builtin_object;
524 PyFrameObject *f;
525 PyObject *builtins;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000526 int extras, ncells, nfrees;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000527
Sjoerd Mullender5b7f3cd1995-04-04 11:47:41 +0000528 if (builtin_object == NULL) {
Guido van Rossumb56933e1997-01-18 07:58:41 +0000529 builtin_object = PyString_InternFromString("__builtins__");
Sjoerd Mullender5b7f3cd1995-04-04 11:47:41 +0000530 if (builtin_object == NULL)
531 return NULL;
532 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000533#ifdef Py_DEBUG
534 if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
Guido van Rossum18752471997-04-29 14:49:28 +0000535 (locals != NULL && !PyDict_Check(locals))) {
536 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000537 return NULL;
538 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000539#endif
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000540 ncells = PyTuple_GET_SIZE(code->co_cellvars);
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000541 nfrees = PyTuple_GET_SIZE(code->co_freevars);
542 extras = code->co_stacksize + code->co_nlocals + ncells + nfrees;
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000543 if (back == NULL || back->f_globals != globals) {
544 builtins = PyDict_GetItem(globals, builtin_object);
545 if (builtins != NULL && PyModule_Check(builtins))
546 builtins = PyModule_GetDict(builtins);
547 }
548 else {
549 /* If we share the globals, we share the builtins.
550 Save a lookup and a call. */
551 builtins = back->f_builtins;
552 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000553 if (builtins != NULL && !PyDict_Check(builtins))
554 builtins = NULL;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000555 if (free_list == NULL) {
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000556 f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, extras);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000557 if (f == NULL)
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000558 return NULL;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000559 }
560 else {
Tim Petersb7ba7432002-04-13 05:21:47 +0000561 assert(numfree > 0);
562 --numfree;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000563 f = free_list;
564 free_list = free_list->f_back;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000565 if (f->ob_size < extras) {
566 f = PyObject_GC_Resize(PyFrameObject, f, extras);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000567 if (f == NULL)
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000568 return NULL;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000569 }
Tim Petersdeb77e82001-08-30 00:32:51 +0000570 _Py_NewReference((PyObject *)f);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000571 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000572 if (builtins == NULL) {
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000573 /* No builtins! Make up a minimal one. */
Guido van Rossum404b95d1997-08-05 02:09:46 +0000574 builtins = PyDict_New();
Guido van Rossumf61618c1998-10-19 14:20:20 +0000575 if (builtins == NULL || /* Give them 'None', at least. */
576 PyDict_SetItemString(builtins, "None", Py_None) < 0) {
577 Py_DECREF(f);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000578 return NULL;
Guido van Rossumf61618c1998-10-19 14:20:20 +0000579 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000580 }
581 else
Neal Norwitzd94c28e2002-08-29 20:25:46 +0000582 Py_INCREF(builtins);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000583 f->f_builtins = builtins;
Guido van Rossum18752471997-04-29 14:49:28 +0000584 Py_XINCREF(back);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000585 f->f_back = back;
Guido van Rossum18752471997-04-29 14:49:28 +0000586 Py_INCREF(code);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000587 f->f_code = code;
Guido van Rossum18752471997-04-29 14:49:28 +0000588 Py_INCREF(globals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000589 f->f_globals = globals;
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000590 if (code->co_flags & CO_NEWLOCALS) {
591 if (code->co_flags & CO_OPTIMIZED)
592 locals = NULL; /* Let fast_2_locals handle it */
593 else {
Guido van Rossum18752471997-04-29 14:49:28 +0000594 locals = PyDict_New();
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000595 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000596 Py_DECREF(f);
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000597 return NULL;
598 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000599 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000600 }
Guido van Rossum2271bf71995-07-18 14:30:34 +0000601 else {
602 if (locals == NULL)
603 locals = globals;
Guido van Rossum18752471997-04-29 14:49:28 +0000604 Py_INCREF(locals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000605 }
606 f->f_locals = locals;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000607 f->f_trace = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000608 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000609 f->f_tstate = tstate;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000610
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000611 f->f_lasti = -1;
Guido van Rossum747596a1997-01-24 04:00:21 +0000612 f->f_lineno = code->co_firstlineno;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000613 f->f_restricted = (builtins != tstate->interp->builtins);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000614 f->f_iblock = 0;
615 f->f_nlocals = code->co_nlocals;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000616 f->f_stacksize = code->co_stacksize;
617 f->f_ncells = ncells;
618 f->f_nfreevars = nfrees;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000619
Guido van Rossumf4be4272002-08-01 18:50:33 +0000620 extras = f->f_nlocals + ncells + nfrees;
621 memset(f->f_localsplus, 0, extras * sizeof(f->f_localsplus[0]));
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000622
Guido van Rossumf4be4272002-08-01 18:50:33 +0000623 f->f_valuestack = f->f_localsplus + extras;
Tim Peters8c963692001-06-23 05:26:56 +0000624 f->f_stacktop = f->f_valuestack;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000625 _PyObject_GC_TRACK(f);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000626 return f;
627}
628
Guido van Rossum3f5da241990-12-20 15:06:42 +0000629/* Block management */
630
631void
Fred Drake1b190b42000-07-09 05:40:56 +0000632PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000633{
Guido van Rossum18752471997-04-29 14:49:28 +0000634 PyTryBlock *b;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000635 if (f->f_iblock >= CO_MAXBLOCKS)
Guido van Rossum18752471997-04-29 14:49:28 +0000636 Py_FatalError("XXX block stack overflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000637 b = &f->f_blockstack[f->f_iblock++];
638 b->b_type = type;
639 b->b_level = level;
640 b->b_handler = handler;
641}
642
Guido van Rossum18752471997-04-29 14:49:28 +0000643PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000644PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000645{
Guido van Rossum18752471997-04-29 14:49:28 +0000646 PyTryBlock *b;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000647 if (f->f_iblock <= 0)
Guido van Rossum18752471997-04-29 14:49:28 +0000648 Py_FatalError("XXX block stack underflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000649 b = &f->f_blockstack[--f->f_iblock];
650 return b;
651}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000652
653/* Convert between "fast" version of locals and dictionary version */
654
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000655static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000656map_to_dict(PyObject *map, int nmap, PyObject *dict, PyObject **values,
657 int deref)
658{
659 int j;
660 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000661 PyObject *key = PyTuple_GET_ITEM(map, j);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000662 PyObject *value = values[j];
663 if (deref)
664 value = PyCell_GET(value);
665 if (value == NULL) {
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000666 if (PyDict_DelItem(dict, key) != 0)
667 PyErr_Clear();
668 }
669 else {
670 if (PyDict_SetItem(dict, key, value) != 0)
671 PyErr_Clear();
672 }
673 }
674}
675
Guido van Rossum6b356e72001-04-14 17:55:41 +0000676static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000677dict_to_map(PyObject *map, int nmap, PyObject *dict, PyObject **values,
678 int deref, int clear)
679{
680 int j;
681 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000682 PyObject *key = PyTuple_GET_ITEM(map, j);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000683 PyObject *value = PyDict_GetItem(dict, key);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000684 if (deref) {
Jeremy Hylton4c889012001-05-08 04:08:59 +0000685 if (value || clear) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000686 if (PyCell_GET(values[j]) != value) {
687 if (PyCell_Set(values[j], value) < 0)
688 PyErr_Clear();
689 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000690 }
691 } else if (value != NULL || clear) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000692 if (values[j] != value) {
693 Py_XINCREF(value);
694 Py_XDECREF(values[j]);
695 values[j] = value;
696 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000697 }
698 }
699}
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000700
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000701void
Fred Drake1b190b42000-07-09 05:40:56 +0000702PyFrame_FastToLocals(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000703{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000704 /* Merge fast locals into f->f_locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000705 PyObject *locals, *map;
706 PyObject **fast;
707 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000708 int j;
709 if (f == NULL)
710 return;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000711 locals = f->f_locals;
712 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000713 locals = f->f_locals = PyDict_New();
Guido van Rossum2271bf71995-07-18 14:30:34 +0000714 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000715 PyErr_Clear(); /* Can't report it :-( */
Guido van Rossum2271bf71995-07-18 14:30:34 +0000716 return;
717 }
718 }
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000719 map = f->f_code->co_varnames;
Guido van Rossum18752471997-04-29 14:49:28 +0000720 if (!PyDict_Check(locals) || !PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000721 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000722 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000723 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000724 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000725 if (j > f->f_nlocals)
726 j = f->f_nlocals;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000727 if (f->f_nlocals)
728 map_to_dict(map, j, locals, fast, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000729 if (f->f_ncells || f->f_nfreevars) {
730 if (!(PyTuple_Check(f->f_code->co_cellvars)
731 && PyTuple_Check(f->f_code->co_freevars))) {
732 Py_DECREF(locals);
733 return;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000734 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000735 map_to_dict(f->f_code->co_cellvars,
736 PyTuple_GET_SIZE(f->f_code->co_cellvars),
737 locals, fast + f->f_nlocals, 1);
738 map_to_dict(f->f_code->co_freevars,
739 PyTuple_GET_SIZE(f->f_code->co_freevars),
740 locals, fast + f->f_nlocals + f->f_ncells, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000741 }
Guido van Rossum18752471997-04-29 14:49:28 +0000742 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000743}
744
745void
Fred Drake1b190b42000-07-09 05:40:56 +0000746PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000747{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000748 /* Merge f->f_locals into fast locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000749 PyObject *locals, *map;
750 PyObject **fast;
751 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000752 int j;
753 if (f == NULL)
754 return;
755 locals = f->f_locals;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000756 map = f->f_code->co_varnames;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000757 if (locals == NULL)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000758 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000759 if (!PyDict_Check(locals) || !PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000760 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000761 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000762 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000763 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000764 if (j > f->f_nlocals)
765 j = f->f_nlocals;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000766 if (f->f_nlocals)
767 dict_to_map(f->f_code->co_varnames, j, locals, fast, 0, clear);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000768 if (f->f_ncells || f->f_nfreevars) {
769 if (!(PyTuple_Check(f->f_code->co_cellvars)
770 && PyTuple_Check(f->f_code->co_freevars)))
771 return;
772 dict_to_map(f->f_code->co_cellvars,
773 PyTuple_GET_SIZE(f->f_code->co_cellvars),
Jeremy Hylton4c889012001-05-08 04:08:59 +0000774 locals, fast + f->f_nlocals, 1, clear);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000775 dict_to_map(f->f_code->co_freevars,
776 PyTuple_GET_SIZE(f->f_code->co_freevars),
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000777 locals, fast + f->f_nlocals + f->f_ncells, 1,
778 clear);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000779 }
Guido van Rossum18752471997-04-29 14:49:28 +0000780 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000781}
Guido van Rossum404b95d1997-08-05 02:09:46 +0000782
783/* Clear out the free list */
784
785void
Fred Drake1b190b42000-07-09 05:40:56 +0000786PyFrame_Fini(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +0000787{
788 while (free_list != NULL) {
789 PyFrameObject *f = free_list;
790 free_list = free_list->f_back;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000791 PyObject_GC_Del(f);
Tim Petersb7ba7432002-04-13 05:21:47 +0000792 --numfree;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000793 }
Tim Petersb7ba7432002-04-13 05:21:47 +0000794 assert(numfree == 0);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000795}