blob: 8ebf5004ab902160d3fcdfdd1f706527fab7c07b [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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "compile.h"
8#include "frameobject.h"
9#include "opcode.h"
10#include "structmember.h"
11
Neal Norwitz91787cb2002-12-18 23:33:35 +000012#undef MIN
13#undef MAX
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000014#define MIN(a, b) ((a) < (b) ? (a) : (b))
15#define MAX(a, b) ((a) > (b) ? (a) : (b))
16
Guido van Rossum18752471997-04-29 14:49:28 +000017#define OFF(x) offsetof(PyFrameObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000018
Guido van Rossum6f799372001-09-20 20:46:19 +000019static PyMemberDef frame_memberlist[] = {
Guido van Rossum1d5735e1994-08-30 08:27:36 +000020 {"f_back", T_OBJECT, OFF(f_back), RO},
21 {"f_code", T_OBJECT, OFF(f_code), RO},
Guido van Rossumc1134821995-01-10 10:39:16 +000022 {"f_builtins", T_OBJECT, OFF(f_builtins),RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000023 {"f_globals", T_OBJECT, OFF(f_globals), RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000024 {"f_lasti", T_INT, OFF(f_lasti), RO},
Guido van Rossumc1134821995-01-10 10:39:16 +000025 {"f_restricted",T_INT, OFF(f_restricted),RO},
Guido van Rossuma027efa1997-05-05 20:56:21 +000026 {"f_exc_type", T_OBJECT, OFF(f_exc_type)},
27 {"f_exc_value", T_OBJECT, OFF(f_exc_value)},
28 {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)},
Guido van Rossum3f5da241990-12-20 15:06:42 +000029 {NULL} /* Sentinel */
30};
31
Guido van Rossum18752471997-04-29 14:49:28 +000032static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000033frame_getlocals(PyFrameObject *f, void *closure)
Guido van Rossum3f5da241990-12-20 15:06:42 +000034{
Tim Peters6d6c1a32001-08-02 04:15:00 +000035 PyFrame_FastToLocals(f);
36 Py_INCREF(f->f_locals);
37 return f->f_locals;
Guido van Rossum3f5da241990-12-20 15:06:42 +000038}
39
Michael W. Hudsondd32a912002-08-15 14:59:02 +000040static PyObject *
41frame_getlineno(PyFrameObject *f, void *closure)
42{
43 int lineno;
44
Michael W. Hudson02ff6a92002-09-11 15:36:32 +000045 if (f->f_trace)
46 lineno = f->f_lineno;
47 else
48 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000049
50 return PyInt_FromLong(lineno);
51}
52
Michael W. Hudsoncfd38842002-12-17 16:15:34 +000053/* Setter for f_lineno - you can set f_lineno from within a trace function in
54 * order to jump to a given line of code, subject to some restrictions. Most
55 * lines are OK to jump to because they don't make any assumptions about the
56 * state of the stack (obvious because you could remove the line and the code
57 * would still work without any stack errors), but there are some constructs
58 * that limit jumping:
59 *
60 * o Lines with an 'except' statement on them can't be jumped to, because
61 * they expect an exception to be on the top of the stack.
62 * o Lines that live in a 'finally' block can't be jumped from or to, since
63 * the END_FINALLY expects to clean up the stack after the 'try' block.
64 * o 'try'/'for'/'while' blocks can't be jumped into because the blockstack
65 * needs to be set up before their code runs, and for 'for' loops the
66 * iterator needs to be on the stack.
67 */
68static int
69frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
70{
71 int new_lineno = 0; /* The new value of f_lineno */
72 int new_lasti = 0; /* The new value of f_lasti */
73 int new_iblock = 0; /* The new value of f_iblock */
74 char *code = NULL; /* The bytecode for the frame... */
75 int code_len = 0; /* ...and its length */
76 char *lnotab = NULL; /* Iterating over co_lnotab */
77 int lnotab_len = 0; /* (ditto) */
78 int offset = 0; /* (ditto) */
79 int line = 0; /* (ditto) */
80 int addr = 0; /* (ditto) */
81 int min_addr = 0; /* Scanning the SETUPs and POPs */
82 int max_addr = 0; /* (ditto) */
83 int delta_iblock = 0; /* (ditto) */
84 int min_delta_iblock = 0; /* (ditto) */
85 int min_iblock = 0; /* (ditto) */
86 int f_lasti_setup_addr = 0; /* Policing no-jump-into-finally */
87 int new_lasti_setup_addr = 0; /* (ditto) */
88 int blockstack[CO_MAXBLOCKS]; /* Walking the 'finally' blocks */
89 int in_finally[CO_MAXBLOCKS]; /* (ditto) */
90 int blockstack_top = 0; /* (ditto) */
91 int setup_op = 0; /* (ditto) */
92
93 /* f_lineno must be an integer. */
94 if (!PyInt_Check(p_new_lineno)) {
95 PyErr_SetString(PyExc_ValueError,
96 "lineno must be an integer");
97 return -1;
98 }
99
100 /* You can only do this from within a trace function, not via
101 * _getframe or similar hackery. */
102 if (!f->f_trace)
103 {
104 PyErr_Format(PyExc_ValueError,
105 "f_lineno can only be set by a trace function");
106 return -1;
107 }
108
109 /* Fail if the line comes before the start of the code block. */
110 new_lineno = (int) PyInt_AsLong(p_new_lineno);
111 if (new_lineno < f->f_code->co_firstlineno) {
112 PyErr_Format(PyExc_ValueError,
113 "line %d comes before the current code block",
114 new_lineno);
115 return -1;
116 }
117
118 /* Find the bytecode offset for the start of the given line, or the
119 * first code-owning line after it. */
120 PyString_AsStringAndSize(f->f_code->co_lnotab, &lnotab, &lnotab_len);
121 addr = 0;
122 line = f->f_code->co_firstlineno;
123 new_lasti = -1;
124 for (offset = 0; offset < lnotab_len; offset += 2) {
125 addr += lnotab[offset];
126 line += lnotab[offset+1];
127 if (line >= new_lineno) {
128 new_lasti = addr;
129 new_lineno = line;
130 break;
131 }
132 }
133
134 /* If we didn't reach the requested line, return an error. */
135 if (new_lasti == -1) {
136 PyErr_Format(PyExc_ValueError,
137 "line %d comes after the current code block",
138 new_lineno);
139 return -1;
140 }
141
142 /* We're now ready to look at the bytecode. */
143 PyString_AsStringAndSize(f->f_code->co_code, &code, &code_len);
144 min_addr = MIN(new_lasti, f->f_lasti);
145 max_addr = MAX(new_lasti, f->f_lasti);
146
147 /* You can't jump onto a line with an 'except' statement on it -
148 * they expect to have an exception on the top of the stack, which
149 * won't be true if you jump to them. They always start with code
150 * that either pops the exception using POP_TOP (plain 'except:'
151 * lines do this) or duplicates the exception on the stack using
152 * DUP_TOP (if there's an exception type specified). See compile.c,
153 * 'com_try_except' for the full details. There aren't any other
154 * cases (AFAIK) where a line's code can start with DUP_TOP or
155 * POP_TOP, but if any ever appear, they'll be subject to the same
156 * restriction (but with a different error message). */
157 if (code[new_lasti] == DUP_TOP || code[new_lasti] == POP_TOP) {
158 PyErr_SetString(PyExc_ValueError,
159 "can't jump to 'except' line as there's no exception");
160 return -1;
161 }
162
163 /* You can't jump into or out of a 'finally' block because the 'try'
164 * block leaves something on the stack for the END_FINALLY to clean
165 * up. So we walk the bytecode, maintaining a simulated blockstack.
166 * When we reach the old or new address and it's in a 'finally' block
167 * we note the address of the corresponding SETUP_FINALLY. The jump
168 * is only legal if neither address is in a 'finally' block or
169 * they're both in the same one. 'blockstack' is a stack of the
170 * bytecode addresses of the SETUP_X opcodes, and 'in_finally' tracks
171 * whether we're in a 'finally' block at each blockstack level. */
172 f_lasti_setup_addr = -1;
173 new_lasti_setup_addr = -1;
174 memset(blockstack, '\0', sizeof(blockstack));
175 memset(in_finally, '\0', sizeof(in_finally));
176 blockstack_top = 0;
177 for (addr = 0; addr < code_len; addr++) {
178 unsigned char op = code[addr];
179 switch (op) {
180 case SETUP_LOOP:
181 case SETUP_EXCEPT:
182 case SETUP_FINALLY:
183 blockstack[blockstack_top++] = addr;
184 in_finally[blockstack_top-1] = 0;
185 break;
186
187 case POP_BLOCK:
Neal Norwitzee65e222002-12-19 18:16:57 +0000188 assert(blockstack_top > 0);
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000189 setup_op = code[blockstack[blockstack_top-1]];
190 if (setup_op == SETUP_FINALLY) {
191 in_finally[blockstack_top-1] = 1;
192 }
193 else {
194 blockstack_top--;
195 }
196 break;
197
198 case END_FINALLY:
199 /* Ignore END_FINALLYs for SETUP_EXCEPTs - they exist
200 * in the bytecode but don't correspond to an actual
Neal Norwitzee65e222002-12-19 18:16:57 +0000201 * 'finally' block. (If blockstack_top is 0, we must
202 * be seeing such an END_FINALLY.) */
203 if (blockstack_top > 0) {
204 setup_op = code[blockstack[blockstack_top-1]];
205 if (setup_op == SETUP_FINALLY) {
206 blockstack_top--;
207 }
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000208 }
209 break;
210 }
211
212 /* For the addresses we're interested in, see whether they're
213 * within a 'finally' block and if so, remember the address
214 * of the SETUP_FINALLY. */
215 if (addr == new_lasti || addr == f->f_lasti) {
216 int i = 0;
217 int setup_addr = -1;
218 for (i = blockstack_top-1; i >= 0; i--) {
219 if (in_finally[i]) {
220 setup_addr = blockstack[i];
221 break;
222 }
223 }
224
225 if (setup_addr != -1) {
226 if (addr == new_lasti) {
227 new_lasti_setup_addr = setup_addr;
228 }
229
230 if (addr == f->f_lasti) {
231 f_lasti_setup_addr = setup_addr;
232 }
233 }
234 }
235
236 if (op >= HAVE_ARGUMENT) {
237 addr += 2;
238 }
239 }
240
Neal Norwitzee65e222002-12-19 18:16:57 +0000241 /* Verify that the blockstack tracking code didn't get lost. */
242 assert(blockstack_top == 0);
243
244 /* After all that, are we jumping into / out of a 'finally' block? */
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000245 if (new_lasti_setup_addr != f_lasti_setup_addr) {
246 PyErr_SetString(PyExc_ValueError,
247 "can't jump into or out of a 'finally' block");
248 return -1;
249 }
250
251
252 /* Police block-jumping (you can't jump into the middle of a block)
253 * and ensure that the blockstack finishes up in a sensible state (by
254 * popping any blocks we're jumping out of). We look at all the
255 * blockstack operations between the current position and the new
256 * one, and keep track of how many blocks we drop out of on the way.
257 * By also keeping track of the lowest blockstack position we see, we
258 * can tell whether the jump goes into any blocks without coming out
259 * again - in that case we raise an exception below. */
260 delta_iblock = 0;
261 for (addr = min_addr; addr < max_addr; addr++) {
262 unsigned char op = code[addr];
263 switch (op) {
264 case SETUP_LOOP:
265 case SETUP_EXCEPT:
266 case SETUP_FINALLY:
267 delta_iblock++;
268 break;
269
270 case POP_BLOCK:
271 delta_iblock--;
272 break;
273 }
274
275 min_delta_iblock = MIN(min_delta_iblock, delta_iblock);
276
277 if (op >= HAVE_ARGUMENT) {
278 addr += 2;
279 }
280 }
281
282 /* Derive the absolute iblock values from the deltas. */
283 min_iblock = f->f_iblock + min_delta_iblock;
284 if (new_lasti > f->f_lasti) {
285 /* Forwards jump. */
286 new_iblock = f->f_iblock + delta_iblock;
287 }
288 else {
289 /* Backwards jump. */
290 new_iblock = f->f_iblock - delta_iblock;
291 }
292
293 /* Are we jumping into a block? */
294 if (new_iblock > min_iblock) {
295 PyErr_SetString(PyExc_ValueError,
296 "can't jump into the middle of a block");
297 return -1;
298 }
299
300 /* Pop any blocks that we're jumping out of. */
301 while (f->f_iblock > new_iblock) {
302 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
303 while ((f->f_stacktop - f->f_valuestack) > b->b_level) {
304 PyObject *v = (*--f->f_stacktop);
305 Py_DECREF(v);
306 }
307 }
308
309 /* Finally set the new f_lineno and f_lasti and return OK. */
310 f->f_lineno = new_lineno;
311 f->f_lasti = new_lasti;
312 return 0;
313}
314
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000315static PyObject *
316frame_gettrace(PyFrameObject *f, void *closure)
317{
318 PyObject* trace = f->f_trace;
319
320 if (trace == NULL)
321 trace = Py_None;
322
323 Py_INCREF(trace);
324
325 return trace;
326}
327
328static int
329frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
330{
331 /* We rely on f_lineno being accurate when f_trace is set. */
332
333 PyObject* old_value = f->f_trace;
334
335 Py_XINCREF(v);
336 f->f_trace = v;
337
338 if (v != NULL)
339 f->f_lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
340
341 Py_XDECREF(old_value);
342
343 return 0;
344}
345
Guido van Rossum32d34c82001-09-20 21:45:26 +0000346static PyGetSetDef frame_getsetlist[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000347 {"f_locals", (getter)frame_getlocals, NULL, NULL},
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000348 {"f_lineno", (getter)frame_getlineno,
349 (setter)frame_setlineno, NULL},
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000350 {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000351 {0}
352};
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000353
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000354/* Stack frames are allocated and deallocated at a considerable rate.
355 In an attempt to improve the speed of function calls, we maintain a
356 separate free list of stack frames (just like integers are
357 allocated in a special way -- see intobject.c). When a stack frame
358 is on the free list, only the following members have a meaning:
359 ob_type == &Frametype
360 f_back next item on free list, or NULL
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000361 f_nlocals number of locals
362 f_stacksize size of value stack
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000363 ob_size size of localsplus
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000364 Note that the value and block stacks are preserved -- this can save
365 another malloc() call or two (and two free() calls as well!).
366 Also note that, unlike for integers, each frame object is a
367 malloc'ed object in its own right -- it is only the actual calls to
368 malloc() that we are trying to save here, not the administration.
369 After all, while a typical program may make millions of calls, a
370 call depth of more than 20 or 30 is probably already exceptional
371 unless the program contains run-away recursion. I hope.
Tim Petersb7ba7432002-04-13 05:21:47 +0000372
373 Later, MAXFREELIST was added to bound the # of frames saved on
374 free_list. Else programs creating lots of cyclic trash involving
375 frames could provoke free_list into growing without bound.
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000376*/
377
Guido van Rossum18752471997-04-29 14:49:28 +0000378static PyFrameObject *free_list = NULL;
Tim Petersb7ba7432002-04-13 05:21:47 +0000379static int numfree = 0; /* number of frames currently in free_list */
380#define MAXFREELIST 200 /* max value for numfree */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000381
Guido van Rossum3f5da241990-12-20 15:06:42 +0000382static void
Fred Drake1b190b42000-07-09 05:40:56 +0000383frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000384{
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000385 int i, slots;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000386 PyObject **fastlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +0000387 PyObject **p;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000388
Guido van Rossumff413af2002-03-28 20:34:59 +0000389 PyObject_GC_UnTrack(f);
Guido van Rossumd724b232000-03-13 16:01:29 +0000390 Py_TRASHCAN_SAFE_BEGIN(f)
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000391 /* Kill all local variables */
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000392 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000393 fastlocals = f->f_localsplus;
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000394 for (i = slots; --i >= 0; ++fastlocals) {
Guido van Rossum18752471997-04-29 14:49:28 +0000395 Py_XDECREF(*fastlocals);
Guido van Rossum7582bfb1997-02-14 16:27:29 +0000396 }
397
Tim Peters5ca576e2001-06-18 22:08:13 +0000398 /* Free stack */
Tim Peters8c963692001-06-23 05:26:56 +0000399 if (f->f_stacktop != NULL) {
400 for (p = f->f_valuestack; p < f->f_stacktop; p++)
401 Py_XDECREF(*p);
Tim Peters5ca576e2001-06-18 22:08:13 +0000402 }
Tim Peters8c963692001-06-23 05:26:56 +0000403
Guido van Rossum18752471997-04-29 14:49:28 +0000404 Py_XDECREF(f->f_back);
Neal Norwitzc91ed402002-12-30 22:29:22 +0000405 Py_DECREF(f->f_code);
406 Py_DECREF(f->f_builtins);
407 Py_DECREF(f->f_globals);
Guido van Rossum18752471997-04-29 14:49:28 +0000408 Py_XDECREF(f->f_locals);
409 Py_XDECREF(f->f_trace);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000410 Py_XDECREF(f->f_exc_type);
411 Py_XDECREF(f->f_exc_value);
412 Py_XDECREF(f->f_exc_traceback);
Tim Petersb7ba7432002-04-13 05:21:47 +0000413 if (numfree < MAXFREELIST) {
414 ++numfree;
415 f->f_back = free_list;
416 free_list = f;
417 }
418 else
419 PyObject_GC_Del(f);
Guido van Rossumd724b232000-03-13 16:01:29 +0000420 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000421}
422
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000423static int
424frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
425{
426 PyObject **fastlocals, **p;
427 int i, err, slots;
428#define VISIT(o) if (o) {if ((err = visit((PyObject *)(o), arg))) return err;}
429
430 VISIT(f->f_back);
431 VISIT(f->f_code);
432 VISIT(f->f_builtins);
433 VISIT(f->f_globals);
434 VISIT(f->f_locals);
435 VISIT(f->f_trace);
436 VISIT(f->f_exc_type);
437 VISIT(f->f_exc_value);
438 VISIT(f->f_exc_traceback);
439
440 /* locals */
441 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
442 fastlocals = f->f_localsplus;
443 for (i = slots; --i >= 0; ++fastlocals) {
444 VISIT(*fastlocals);
445 }
446
447 /* stack */
448 if (f->f_stacktop != NULL) {
449 for (p = f->f_valuestack; p < f->f_stacktop; p++)
450 VISIT(*p);
451 }
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000452 return 0;
453}
454
455static void
456frame_clear(PyFrameObject *f)
457{
458 PyObject **fastlocals, **p;
459 int i, slots;
460
461 Py_XDECREF(f->f_exc_type);
462 f->f_exc_type = NULL;
463
464 Py_XDECREF(f->f_exc_value);
465 f->f_exc_value = NULL;
466
467 Py_XDECREF(f->f_exc_traceback);
468 f->f_exc_traceback = NULL;
469
470 Py_XDECREF(f->f_trace);
471 f->f_trace = NULL;
472
473 /* locals */
474 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
475 fastlocals = f->f_localsplus;
476 for (i = slots; --i >= 0; ++fastlocals) {
477 if (*fastlocals != NULL) {
478 Py_XDECREF(*fastlocals);
479 *fastlocals = NULL;
480 }
481 }
482
483 /* stack */
484 if (f->f_stacktop != NULL) {
485 for (p = f->f_valuestack; p < f->f_stacktop; p++) {
486 Py_XDECREF(*p);
487 *p = NULL;
488 }
489 }
490}
491
492
Guido van Rossum18752471997-04-29 14:49:28 +0000493PyTypeObject PyFrame_Type = {
494 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000495 0,
496 "frame",
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000497 sizeof(PyFrameObject),
498 sizeof(PyObject *),
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000499 (destructor)frame_dealloc, /* tp_dealloc */
500 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000501 0, /* tp_getattr */
502 0, /* tp_setattr */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000503 0, /* tp_compare */
504 0, /* tp_repr */
505 0, /* tp_as_number */
506 0, /* tp_as_sequence */
507 0, /* tp_as_mapping */
508 0, /* tp_hash */
509 0, /* tp_call */
510 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000511 PyObject_GenericGetAttr, /* tp_getattro */
512 PyObject_GenericSetAttr, /* tp_setattro */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000513 0, /* tp_as_buffer */
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000514 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Neil Schemenauer19cd2922001-07-12 13:27:11 +0000515 0, /* tp_doc */
516 (traverseproc)frame_traverse, /* tp_traverse */
517 (inquiry)frame_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000518 0, /* tp_richcompare */
519 0, /* tp_weaklistoffset */
520 0, /* tp_iter */
521 0, /* tp_iternext */
522 0, /* tp_methods */
523 frame_memberlist, /* tp_members */
524 frame_getsetlist, /* tp_getset */
525 0, /* tp_base */
526 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000527};
528
Neal Norwitzc91ed402002-12-30 22:29:22 +0000529static PyObject *builtin_object;
530
Neal Norwitzb2501f42002-12-31 03:42:13 +0000531int _PyFrame_Init()
Neal Norwitzc91ed402002-12-30 22:29:22 +0000532{
533 builtin_object = PyString_InternFromString("__builtins__");
534 return (builtin_object != NULL);
535}
536
Guido van Rossum18752471997-04-29 14:49:28 +0000537PyFrameObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000538PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000539 PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000540{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000541 PyFrameObject *back = tstate->frame;
Guido van Rossum18752471997-04-29 14:49:28 +0000542 PyFrameObject *f;
543 PyObject *builtins;
Armin Rigo75be0122004-03-20 21:10:27 +0000544 int extras, ncells, nfrees, i;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000545
Michael W. Hudson69734a52002-08-19 16:54:08 +0000546#ifdef Py_DEBUG
547 if (code == NULL || globals == NULL || !PyDict_Check(globals) ||
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000548 (locals != NULL && !PyMapping_Check(locals))) {
Guido van Rossum18752471997-04-29 14:49:28 +0000549 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000550 return NULL;
551 }
Michael W. Hudson69734a52002-08-19 16:54:08 +0000552#endif
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000553 ncells = PyTuple_GET_SIZE(code->co_cellvars);
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000554 nfrees = PyTuple_GET_SIZE(code->co_freevars);
555 extras = code->co_stacksize + code->co_nlocals + ncells + nfrees;
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000556 if (back == NULL || back->f_globals != globals) {
557 builtins = PyDict_GetItem(globals, builtin_object);
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000558 if (builtins) {
559 if (PyModule_Check(builtins)) {
560 builtins = PyModule_GetDict(builtins);
561 assert(!builtins || PyDict_Check(builtins));
562 }
563 else if (!PyDict_Check(builtins))
564 builtins = NULL;
565 }
566 if (builtins == NULL) {
567 /* No builtins! Make up a minimal one
568 Give them 'None', at least. */
569 builtins = PyDict_New();
570 if (builtins == NULL ||
571 PyDict_SetItemString(
572 builtins, "None", Py_None) < 0)
573 return NULL;
574 }
575 else
576 Py_INCREF(builtins);
577
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000578 }
579 else {
580 /* If we share the globals, we share the builtins.
581 Save a lookup and a call. */
582 builtins = back->f_builtins;
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000583 assert(builtins != NULL && PyDict_Check(builtins));
584 Py_INCREF(builtins);
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000585 }
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000586 if (free_list == NULL) {
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000587 f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, extras);
Armin Rigo76beca92004-01-27 16:08:07 +0000588 if (f == NULL) {
589 Py_DECREF(builtins);
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000590 return NULL;
Armin Rigo76beca92004-01-27 16:08:07 +0000591 }
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000592 }
593 else {
Tim Petersb7ba7432002-04-13 05:21:47 +0000594 assert(numfree > 0);
595 --numfree;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000596 f = free_list;
597 free_list = free_list->f_back;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000598 if (f->ob_size < extras) {
599 f = PyObject_GC_Resize(PyFrameObject, f, extras);
Armin Rigo76beca92004-01-27 16:08:07 +0000600 if (f == NULL) {
601 Py_DECREF(builtins);
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000602 return NULL;
Armin Rigo76beca92004-01-27 16:08:07 +0000603 }
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000604 }
Tim Petersdeb77e82001-08-30 00:32:51 +0000605 _Py_NewReference((PyObject *)f);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000606 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000607 f->f_builtins = builtins;
Guido van Rossum18752471997-04-29 14:49:28 +0000608 Py_XINCREF(back);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000609 f->f_back = back;
Guido van Rossum18752471997-04-29 14:49:28 +0000610 Py_INCREF(code);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000611 f->f_code = code;
Guido van Rossum18752471997-04-29 14:49:28 +0000612 Py_INCREF(globals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000613 f->f_globals = globals;
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000614 /* Most functions have CO_NEWLOCALS and CO_OPTIMIZED set. */
615 if ((code->co_flags & (CO_NEWLOCALS | CO_OPTIMIZED)) ==
616 (CO_NEWLOCALS | CO_OPTIMIZED))
Michael W. Hudsonbdc6ea12003-08-11 16:14:06 +0000617 locals = NULL; /* PyFrame_FastToLocals() will set. */
Jeremy Hyltonbd5cbf82003-02-05 22:39:29 +0000618 else if (code->co_flags & CO_NEWLOCALS) {
619 locals = PyDict_New();
620 if (locals == NULL) {
621 Py_DECREF(f);
622 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000623 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000624 }
Guido van Rossum2271bf71995-07-18 14:30:34 +0000625 else {
626 if (locals == NULL)
627 locals = globals;
Guido van Rossum18752471997-04-29 14:49:28 +0000628 Py_INCREF(locals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000629 }
630 f->f_locals = locals;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000631 f->f_trace = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000632 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000633 f->f_tstate = tstate;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000634
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000635 f->f_lasti = -1;
Guido van Rossum747596a1997-01-24 04:00:21 +0000636 f->f_lineno = code->co_firstlineno;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000637 f->f_restricted = (builtins != tstate->interp->builtins);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000638 f->f_iblock = 0;
639 f->f_nlocals = code->co_nlocals;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000640 f->f_stacksize = code->co_stacksize;
641 f->f_ncells = ncells;
642 f->f_nfreevars = nfrees;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000643
Guido van Rossumf4be4272002-08-01 18:50:33 +0000644 extras = f->f_nlocals + ncells + nfrees;
Armin Rigo75be0122004-03-20 21:10:27 +0000645 /* Tim said it's ok to replace memset */
646 for (i=0; i<extras; i++)
647 f->f_localsplus[i] = NULL;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000648
Guido van Rossumf4be4272002-08-01 18:50:33 +0000649 f->f_valuestack = f->f_localsplus + extras;
Tim Peters8c963692001-06-23 05:26:56 +0000650 f->f_stacktop = f->f_valuestack;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000651 _PyObject_GC_TRACK(f);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000652 return f;
653}
654
Guido van Rossum3f5da241990-12-20 15:06:42 +0000655/* Block management */
656
657void
Fred Drake1b190b42000-07-09 05:40:56 +0000658PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000659{
Guido van Rossum18752471997-04-29 14:49:28 +0000660 PyTryBlock *b;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000661 if (f->f_iblock >= CO_MAXBLOCKS)
Guido van Rossum18752471997-04-29 14:49:28 +0000662 Py_FatalError("XXX block stack overflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000663 b = &f->f_blockstack[f->f_iblock++];
664 b->b_type = type;
665 b->b_level = level;
666 b->b_handler = handler;
667}
668
Guido van Rossum18752471997-04-29 14:49:28 +0000669PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000670PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000671{
Guido van Rossum18752471997-04-29 14:49:28 +0000672 PyTryBlock *b;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000673 if (f->f_iblock <= 0)
Guido van Rossum18752471997-04-29 14:49:28 +0000674 Py_FatalError("XXX block stack underflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000675 b = &f->f_blockstack[--f->f_iblock];
676 return b;
677}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000678
679/* Convert between "fast" version of locals and dictionary version */
680
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000681static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000682map_to_dict(PyObject *map, int nmap, PyObject *dict, PyObject **values,
683 int deref)
684{
685 int j;
686 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000687 PyObject *key = PyTuple_GET_ITEM(map, j);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000688 PyObject *value = values[j];
689 if (deref)
690 value = PyCell_GET(value);
691 if (value == NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000692 if (PyObject_DelItem(dict, key) != 0)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000693 PyErr_Clear();
694 }
695 else {
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000696 if (PyObject_SetItem(dict, key, value) != 0)
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000697 PyErr_Clear();
698 }
699 }
700}
701
Guido van Rossum6b356e72001-04-14 17:55:41 +0000702static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000703dict_to_map(PyObject *map, int nmap, PyObject *dict, PyObject **values,
704 int deref, int clear)
705{
706 int j;
707 for (j = nmap; --j >= 0; ) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000708 PyObject *key = PyTuple_GET_ITEM(map, j);
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000709 PyObject *value = PyObject_GetItem(dict, key);
710 if (value == NULL)
711 PyErr_Clear();
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000712 if (deref) {
Jeremy Hylton4c889012001-05-08 04:08:59 +0000713 if (value || clear) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000714 if (PyCell_GET(values[j]) != value) {
715 if (PyCell_Set(values[j], value) < 0)
716 PyErr_Clear();
717 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000718 }
719 } else if (value != NULL || clear) {
Jeremy Hylton1a48ca82001-12-06 15:48:16 +0000720 if (values[j] != value) {
721 Py_XINCREF(value);
722 Py_XDECREF(values[j]);
723 values[j] = value;
724 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000725 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000726 Py_XDECREF(value);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000727 }
728}
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000729
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000730void
Fred Drake1b190b42000-07-09 05:40:56 +0000731PyFrame_FastToLocals(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000732{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000733 /* Merge fast locals into f->f_locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000734 PyObject *locals, *map;
735 PyObject **fast;
736 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000737 int j;
738 if (f == NULL)
739 return;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000740 locals = f->f_locals;
741 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000742 locals = f->f_locals = PyDict_New();
Guido van Rossum2271bf71995-07-18 14:30:34 +0000743 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000744 PyErr_Clear(); /* Can't report it :-( */
Guido van Rossum2271bf71995-07-18 14:30:34 +0000745 return;
746 }
747 }
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000748 map = f->f_code->co_varnames;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000749 if (!PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000750 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000751 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000752 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000753 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000754 if (j > f->f_nlocals)
755 j = f->f_nlocals;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000756 if (f->f_nlocals)
Jeremy Hylton174d2762003-10-21 18:10:28 +0000757 map_to_dict(map, j, locals, fast, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000758 if (f->f_ncells || f->f_nfreevars) {
759 if (!(PyTuple_Check(f->f_code->co_cellvars)
760 && PyTuple_Check(f->f_code->co_freevars))) {
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000761 return;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000762 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000763 map_to_dict(f->f_code->co_cellvars,
764 PyTuple_GET_SIZE(f->f_code->co_cellvars),
765 locals, fast + f->f_nlocals, 1);
766 map_to_dict(f->f_code->co_freevars,
767 PyTuple_GET_SIZE(f->f_code->co_freevars),
768 locals, fast + f->f_nlocals + f->f_ncells, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000769 }
Guido van Rossum18752471997-04-29 14:49:28 +0000770 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000771}
772
773void
Fred Drake1b190b42000-07-09 05:40:56 +0000774PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000775{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000776 /* Merge f->f_locals into fast locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000777 PyObject *locals, *map;
778 PyObject **fast;
779 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000780 int j;
781 if (f == NULL)
782 return;
783 locals = f->f_locals;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000784 map = f->f_code->co_varnames;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000785 if (locals == NULL)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000786 return;
Raymond Hettinger214b1c32004-07-02 06:41:07 +0000787 if (!PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000788 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000789 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000790 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000791 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000792 if (j > f->f_nlocals)
793 j = f->f_nlocals;
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000794 if (f->f_nlocals)
795 dict_to_map(f->f_code->co_varnames, j, locals, fast, 0, clear);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000796 if (f->f_ncells || f->f_nfreevars) {
797 if (!(PyTuple_Check(f->f_code->co_cellvars)
798 && PyTuple_Check(f->f_code->co_freevars)))
799 return;
800 dict_to_map(f->f_code->co_cellvars,
801 PyTuple_GET_SIZE(f->f_code->co_cellvars),
Jeremy Hylton4c889012001-05-08 04:08:59 +0000802 locals, fast + f->f_nlocals, 1, clear);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000803 dict_to_map(f->f_code->co_freevars,
804 PyTuple_GET_SIZE(f->f_code->co_freevars),
Jeremy Hylton24ea8d32002-04-20 04:46:55 +0000805 locals, fast + f->f_nlocals + f->f_ncells, 1,
806 clear);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000807 }
Guido van Rossum18752471997-04-29 14:49:28 +0000808 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000809}
Guido van Rossum404b95d1997-08-05 02:09:46 +0000810
811/* Clear out the free list */
812
813void
Fred Drake1b190b42000-07-09 05:40:56 +0000814PyFrame_Fini(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +0000815{
816 while (free_list != NULL) {
817 PyFrameObject *f = free_list;
818 free_list = free_list->f_back;
Neil Schemenauer4f4817f2001-08-29 23:52:17 +0000819 PyObject_GC_Del(f);
Tim Petersb7ba7432002-04-13 05:21:47 +0000820 --numfree;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000821 }
Tim Petersb7ba7432002-04-13 05:21:47 +0000822 assert(numfree == 0);
Neal Norwitzc91ed402002-12-30 22:29:22 +0000823 Py_XDECREF(builtin_object);
824 builtin_object = NULL;
Guido van Rossum404b95d1997-08-05 02:09:46 +0000825}