This commit was manufactured by cvs2svn to create tag 'r221'.
[python/dscho.git] / Objects / frameobject.c
blob8e4c60ff2722a2af20498998122e8c981544b71e
2 /* Frame object implementation */
4 #include "Python.h"
6 #include "compile.h"
7 #include "frameobject.h"
8 #include "opcode.h"
9 #include "structmember.h"
11 #define OFF(x) offsetof(PyFrameObject, x)
13 static PyMemberDef frame_memberlist[] = {
14 {"f_back", T_OBJECT, OFF(f_back), RO},
15 {"f_code", T_OBJECT, OFF(f_code), RO},
16 {"f_builtins", T_OBJECT, OFF(f_builtins),RO},
17 {"f_globals", T_OBJECT, OFF(f_globals), RO},
18 {"f_lasti", T_INT, OFF(f_lasti), RO},
19 {"f_lineno", T_INT, OFF(f_lineno), RO},
20 {"f_restricted",T_INT, OFF(f_restricted),RO},
21 {"f_trace", T_OBJECT, OFF(f_trace)},
22 {"f_exc_type", T_OBJECT, OFF(f_exc_type)},
23 {"f_exc_value", T_OBJECT, OFF(f_exc_value)},
24 {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)},
25 {NULL} /* Sentinel */
28 static PyObject *
29 frame_getlocals(PyFrameObject *f, void *closure)
31 PyFrame_FastToLocals(f);
32 Py_INCREF(f->f_locals);
33 return f->f_locals;
36 static PyGetSetDef frame_getsetlist[] = {
37 {"f_locals", (getter)frame_getlocals, NULL, NULL},
38 {0}
41 /* Stack frames are allocated and deallocated at a considerable rate.
42 In an attempt to improve the speed of function calls, we maintain a
43 separate free list of stack frames (just like integers are
44 allocated in a special way -- see intobject.c). When a stack frame
45 is on the free list, only the following members have a meaning:
46 ob_type == &Frametype
47 f_back next item on free list, or NULL
48 f_nlocals number of locals
49 f_stacksize size of value stack
50 ob_size size of localsplus
51 Note that the value and block stacks are preserved -- this can save
52 another malloc() call or two (and two free() calls as well!).
53 Also note that, unlike for integers, each frame object is a
54 malloc'ed object in its own right -- it is only the actual calls to
55 malloc() that we are trying to save here, not the administration.
56 After all, while a typical program may make millions of calls, a
57 call depth of more than 20 or 30 is probably already exceptional
58 unless the program contains run-away recursion. I hope.
61 static PyFrameObject *free_list = NULL;
63 static void
64 frame_dealloc(PyFrameObject *f)
66 int i, slots;
67 PyObject **fastlocals;
68 PyObject **p;
70 PyObject_GC_UnTrack(f);
71 Py_TRASHCAN_SAFE_BEGIN(f)
72 /* Kill all local variables */
73 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
74 fastlocals = f->f_localsplus;
75 for (i = slots; --i >= 0; ++fastlocals) {
76 Py_XDECREF(*fastlocals);
79 /* Free stack */
80 if (f->f_stacktop != NULL) {
81 for (p = f->f_valuestack; p < f->f_stacktop; p++)
82 Py_XDECREF(*p);
85 Py_XDECREF(f->f_back);
86 Py_XDECREF(f->f_code);
87 Py_XDECREF(f->f_builtins);
88 Py_XDECREF(f->f_globals);
89 Py_XDECREF(f->f_locals);
90 Py_XDECREF(f->f_trace);
91 Py_XDECREF(f->f_exc_type);
92 Py_XDECREF(f->f_exc_value);
93 Py_XDECREF(f->f_exc_traceback);
94 f->f_back = free_list;
95 free_list = f;
96 Py_TRASHCAN_SAFE_END(f)
99 static int
100 frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
102 PyObject **fastlocals, **p;
103 int i, err, slots;
104 #define VISIT(o) if (o) {if ((err = visit((PyObject *)(o), arg))) return err;}
106 VISIT(f->f_back);
107 VISIT(f->f_code);
108 VISIT(f->f_builtins);
109 VISIT(f->f_globals);
110 VISIT(f->f_locals);
111 VISIT(f->f_trace);
112 VISIT(f->f_exc_type);
113 VISIT(f->f_exc_value);
114 VISIT(f->f_exc_traceback);
116 /* locals */
117 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
118 fastlocals = f->f_localsplus;
119 for (i = slots; --i >= 0; ++fastlocals) {
120 VISIT(*fastlocals);
123 /* stack */
124 if (f->f_stacktop != NULL) {
125 for (p = f->f_valuestack; p < f->f_stacktop; p++)
126 VISIT(*p);
128 return 0;
131 static void
132 frame_clear(PyFrameObject *f)
134 PyObject **fastlocals, **p;
135 int i, slots;
137 Py_XDECREF(f->f_exc_type);
138 f->f_exc_type = NULL;
140 Py_XDECREF(f->f_exc_value);
141 f->f_exc_value = NULL;
143 Py_XDECREF(f->f_exc_traceback);
144 f->f_exc_traceback = NULL;
146 Py_XDECREF(f->f_trace);
147 f->f_trace = NULL;
149 /* locals */
150 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
151 fastlocals = f->f_localsplus;
152 for (i = slots; --i >= 0; ++fastlocals) {
153 if (*fastlocals != NULL) {
154 Py_XDECREF(*fastlocals);
155 *fastlocals = NULL;
159 /* stack */
160 if (f->f_stacktop != NULL) {
161 for (p = f->f_valuestack; p < f->f_stacktop; p++) {
162 Py_XDECREF(*p);
163 *p = NULL;
169 PyTypeObject PyFrame_Type = {
170 PyObject_HEAD_INIT(&PyType_Type)
172 "frame",
173 sizeof(PyFrameObject),
174 sizeof(PyObject *),
175 (destructor)frame_dealloc, /* tp_dealloc */
176 0, /* tp_print */
177 0, /* tp_getattr */
178 0, /* tp_setattr */
179 0, /* tp_compare */
180 0, /* tp_repr */
181 0, /* tp_as_number */
182 0, /* tp_as_sequence */
183 0, /* tp_as_mapping */
184 0, /* tp_hash */
185 0, /* tp_call */
186 0, /* tp_str */
187 PyObject_GenericGetAttr, /* tp_getattro */
188 PyObject_GenericSetAttr, /* tp_setattro */
189 0, /* tp_as_buffer */
190 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
191 0, /* tp_doc */
192 (traverseproc)frame_traverse, /* tp_traverse */
193 (inquiry)frame_clear, /* tp_clear */
194 0, /* tp_richcompare */
195 0, /* tp_weaklistoffset */
196 0, /* tp_iter */
197 0, /* tp_iternext */
198 0, /* tp_methods */
199 frame_memberlist, /* tp_members */
200 frame_getsetlist, /* tp_getset */
201 0, /* tp_base */
202 0, /* tp_dict */
205 PyFrameObject *
206 PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
207 PyObject *locals)
209 PyFrameObject *back = tstate->frame;
210 static PyObject *builtin_object;
211 PyFrameObject *f;
212 PyObject *builtins;
213 int extras, ncells, nfrees;
215 if (builtin_object == NULL) {
216 builtin_object = PyString_InternFromString("__builtins__");
217 if (builtin_object == NULL)
218 return NULL;
220 if ((back != NULL && !PyFrame_Check(back)) ||
221 code == NULL || !PyCode_Check(code) ||
222 globals == NULL || !PyDict_Check(globals) ||
223 (locals != NULL && !PyDict_Check(locals))) {
224 PyErr_BadInternalCall();
225 return NULL;
227 ncells = PyTuple_GET_SIZE(code->co_cellvars);
228 nfrees = PyTuple_GET_SIZE(code->co_freevars);
229 extras = code->co_stacksize + code->co_nlocals + ncells + nfrees;
230 if (back == NULL || back->f_globals != globals) {
231 builtins = PyDict_GetItem(globals, builtin_object);
232 if (builtins != NULL && PyModule_Check(builtins))
233 builtins = PyModule_GetDict(builtins);
235 else {
236 /* If we share the globals, we share the builtins.
237 Save a lookup and a call. */
238 builtins = back->f_builtins;
240 if (builtins != NULL && !PyDict_Check(builtins))
241 builtins = NULL;
242 if (free_list == NULL) {
243 f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, extras);
244 if (f == NULL)
245 return NULL;
247 else {
248 f = free_list;
249 free_list = free_list->f_back;
250 if (f->ob_size < extras) {
251 f = PyObject_GC_Resize(PyFrameObject, f, extras);
252 if (f == NULL)
253 return NULL;
255 else
256 extras = f->ob_size;
257 _Py_NewReference((PyObject *)f);
259 if (builtins == NULL) {
260 /* No builtins! Make up a minimal one. */
261 builtins = PyDict_New();
262 if (builtins == NULL || /* Give them 'None', at least. */
263 PyDict_SetItemString(builtins, "None", Py_None) < 0) {
264 Py_DECREF(f);
265 return NULL;
268 else
269 Py_XINCREF(builtins);
270 f->f_builtins = builtins;
271 Py_XINCREF(back);
272 f->f_back = back;
273 Py_INCREF(code);
274 f->f_code = code;
275 Py_INCREF(globals);
276 f->f_globals = globals;
277 if (code->co_flags & CO_NEWLOCALS) {
278 if (code->co_flags & CO_OPTIMIZED)
279 locals = NULL; /* Let fast_2_locals handle it */
280 else {
281 locals = PyDict_New();
282 if (locals == NULL) {
283 Py_DECREF(f);
284 return NULL;
288 else {
289 if (locals == NULL)
290 locals = globals;
291 Py_INCREF(locals);
293 f->f_locals = locals;
294 f->f_trace = NULL;
295 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
296 f->f_tstate = tstate;
298 f->f_lasti = 0;
299 f->f_lineno = code->co_firstlineno;
300 f->f_restricted = (builtins != tstate->interp->builtins);
301 f->f_iblock = 0;
302 f->f_nlocals = code->co_nlocals;
303 f->f_stacksize = code->co_stacksize;
304 f->f_ncells = ncells;
305 f->f_nfreevars = nfrees;
307 while (--extras >= 0)
308 f->f_localsplus[extras] = NULL;
310 f->f_valuestack = f->f_localsplus + (f->f_nlocals + ncells + nfrees);
311 f->f_stacktop = f->f_valuestack;
312 _PyObject_GC_TRACK(f);
313 return f;
316 /* Block management */
318 void
319 PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
321 PyTryBlock *b;
322 if (f->f_iblock >= CO_MAXBLOCKS)
323 Py_FatalError("XXX block stack overflow");
324 b = &f->f_blockstack[f->f_iblock++];
325 b->b_type = type;
326 b->b_level = level;
327 b->b_handler = handler;
330 PyTryBlock *
331 PyFrame_BlockPop(PyFrameObject *f)
333 PyTryBlock *b;
334 if (f->f_iblock <= 0)
335 Py_FatalError("XXX block stack underflow");
336 b = &f->f_blockstack[--f->f_iblock];
337 return b;
340 /* Convert between "fast" version of locals and dictionary version */
342 static void
343 map_to_dict(PyObject *map, int nmap, PyObject *dict, PyObject **values,
344 int deref)
346 int j;
347 for (j = nmap; --j >= 0; ) {
348 PyObject *key = PyTuple_GET_ITEM(map, j);
349 PyObject *value = values[j];
350 if (deref)
351 value = PyCell_GET(value);
352 if (value == NULL) {
353 if (PyDict_DelItem(dict, key) != 0)
354 PyErr_Clear();
356 else {
357 if (PyDict_SetItem(dict, key, value) != 0)
358 PyErr_Clear();
363 static void
364 dict_to_map(PyObject *map, int nmap, PyObject *dict, PyObject **values,
365 int deref, int clear)
367 int j;
368 for (j = nmap; --j >= 0; ) {
369 PyObject *key = PyTuple_GET_ITEM(map, j);
370 PyObject *value = PyDict_GetItem(dict, key);
371 if (deref) {
372 if (value || clear) {
373 if (PyCell_GET(values[j]) != value) {
374 if (PyCell_Set(values[j], value) < 0)
375 PyErr_Clear();
378 } else if (value != NULL || clear) {
379 if (values[j] != value) {
380 Py_XINCREF(value);
381 Py_XDECREF(values[j]);
382 values[j] = value;
388 void
389 PyFrame_FastToLocals(PyFrameObject *f)
391 /* Merge fast locals into f->f_locals */
392 PyObject *locals, *map;
393 PyObject **fast;
394 PyObject *error_type, *error_value, *error_traceback;
395 int j;
396 if (f == NULL)
397 return;
398 locals = f->f_locals;
399 if (locals == NULL) {
400 locals = f->f_locals = PyDict_New();
401 if (locals == NULL) {
402 PyErr_Clear(); /* Can't report it :-( */
403 return;
406 if (f->f_nlocals == 0)
407 return;
408 map = f->f_code->co_varnames;
409 if (!PyDict_Check(locals) || !PyTuple_Check(map))
410 return;
411 PyErr_Fetch(&error_type, &error_value, &error_traceback);
412 fast = f->f_localsplus;
413 j = PyTuple_Size(map);
414 if (j > f->f_nlocals)
415 j = f->f_nlocals;
416 map_to_dict(map, j, locals, fast, 0);
417 if (f->f_ncells || f->f_nfreevars) {
418 if (!(PyTuple_Check(f->f_code->co_cellvars)
419 && PyTuple_Check(f->f_code->co_freevars))) {
420 Py_DECREF(locals);
421 return;
423 map_to_dict(f->f_code->co_cellvars,
424 PyTuple_GET_SIZE(f->f_code->co_cellvars),
425 locals, fast + f->f_nlocals, 1);
426 map_to_dict(f->f_code->co_freevars,
427 PyTuple_GET_SIZE(f->f_code->co_freevars),
428 locals, fast + f->f_nlocals + f->f_ncells, 1);
430 PyErr_Restore(error_type, error_value, error_traceback);
433 void
434 PyFrame_LocalsToFast(PyFrameObject *f, int clear)
436 /* Merge f->f_locals into fast locals */
437 PyObject *locals, *map;
438 PyObject **fast;
439 PyObject *error_type, *error_value, *error_traceback;
440 int j;
441 if (f == NULL)
442 return;
443 locals = f->f_locals;
444 map = f->f_code->co_varnames;
445 if (locals == NULL || f->f_code->co_nlocals == 0)
446 return;
447 if (!PyDict_Check(locals) || !PyTuple_Check(map))
448 return;
449 PyErr_Fetch(&error_type, &error_value, &error_traceback);
450 fast = f->f_localsplus;
451 j = PyTuple_Size(map);
452 if (j > f->f_nlocals)
453 j = f->f_nlocals;
454 dict_to_map(f->f_code->co_varnames, j, locals, fast, 0, clear);
455 if (f->f_ncells || f->f_nfreevars) {
456 if (!(PyTuple_Check(f->f_code->co_cellvars)
457 && PyTuple_Check(f->f_code->co_freevars)))
458 return;
459 dict_to_map(f->f_code->co_cellvars,
460 PyTuple_GET_SIZE(f->f_code->co_cellvars),
461 locals, fast + f->f_nlocals, 1, clear);
462 dict_to_map(f->f_code->co_freevars,
463 PyTuple_GET_SIZE(f->f_code->co_freevars),
464 locals, fast + f->f_nlocals + f->f_ncells, 1, clear);
466 PyErr_Restore(error_type, error_value, error_traceback);
469 /* Clear out the free list */
471 void
472 PyFrame_Fini(void)
474 while (free_list != NULL) {
475 PyFrameObject *f = free_list;
476 free_list = free_list->f_back;
477 PyObject_GC_Del(f);