blob: 8e78f0f7f79947b2a98a8b12e5495944385623fd [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
Guido van Rossum18752471997-04-29 14:49:28 +000011#define OFF(x) offsetof(PyFrameObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000012
13static struct memberlist frame_memberlist[] = {
Guido van Rossum1d5735e1994-08-30 08:27:36 +000014 {"f_back", T_OBJECT, OFF(f_back), RO},
15 {"f_code", T_OBJECT, OFF(f_code), RO},
Guido van Rossumc1134821995-01-10 10:39:16 +000016 {"f_builtins", T_OBJECT, OFF(f_builtins),RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000017 {"f_globals", T_OBJECT, OFF(f_globals), RO},
18 {"f_locals", T_OBJECT, OFF(f_locals), RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000019 {"f_lasti", T_INT, OFF(f_lasti), RO},
20 {"f_lineno", T_INT, OFF(f_lineno), RO},
Guido van Rossumc1134821995-01-10 10:39:16 +000021 {"f_restricted",T_INT, OFF(f_restricted),RO},
Guido van Rossum1d5735e1994-08-30 08:27:36 +000022 {"f_trace", T_OBJECT, OFF(f_trace)},
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 *
Fred Drake1b190b42000-07-09 05:40:56 +000030frame_getattr(PyFrameObject *f, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +000031{
Guido van Rossum1d5735e1994-08-30 08:27:36 +000032 if (strcmp(name, "f_locals") == 0)
Guido van Rossum18752471997-04-29 14:49:28 +000033 PyFrame_FastToLocals(f);
34 return PyMember_Get((char *)f, frame_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000035}
36
Guido van Rossum1d5735e1994-08-30 08:27:36 +000037static int
Fred Drake1b190b42000-07-09 05:40:56 +000038frame_setattr(PyFrameObject *f, char *name, PyObject *value)
Guido van Rossum1d5735e1994-08-30 08:27:36 +000039{
Guido van Rossum18752471997-04-29 14:49:28 +000040 return PyMember_Set((char *)f, frame_memberlist, name, value);
Guido van Rossum1d5735e1994-08-30 08:27:36 +000041}
42
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000043/* Stack frames are allocated and deallocated at a considerable rate.
44 In an attempt to improve the speed of function calls, we maintain a
45 separate free list of stack frames (just like integers are
46 allocated in a special way -- see intobject.c). When a stack frame
47 is on the free list, only the following members have a meaning:
48 ob_type == &Frametype
49 f_back next item on free list, or NULL
Guido van Rossumf3e85a01997-01-20 04:20:52 +000050 f_nlocals number of locals
51 f_stacksize size of value stack
Jeremy Hylton2b724da2001-01-29 22:51:52 +000052 f_size size of localsplus
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000053 Note that the value and block stacks are preserved -- this can save
54 another malloc() call or two (and two free() calls as well!).
55 Also note that, unlike for integers, each frame object is a
56 malloc'ed object in its own right -- it is only the actual calls to
57 malloc() that we are trying to save here, not the administration.
58 After all, while a typical program may make millions of calls, a
59 call depth of more than 20 or 30 is probably already exceptional
60 unless the program contains run-away recursion. I hope.
61*/
62
Guido van Rossum18752471997-04-29 14:49:28 +000063static PyFrameObject *free_list = NULL;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000064
Guido van Rossum3f5da241990-12-20 15:06:42 +000065static void
Fred Drake1b190b42000-07-09 05:40:56 +000066frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +000067{
Jeremy Hylton30c9f392001-03-13 01:58:22 +000068 int i, slots;
Guido van Rossum7582bfb1997-02-14 16:27:29 +000069 PyObject **fastlocals;
70
Guido van Rossumd724b232000-03-13 16:01:29 +000071 Py_TRASHCAN_SAFE_BEGIN(f)
Guido van Rossum7582bfb1997-02-14 16:27:29 +000072 /* Kill all local variables */
Jeremy Hylton30c9f392001-03-13 01:58:22 +000073 slots = f->f_nlocals + f->f_ncells + f->f_nfreevars;
Guido van Rossum7582bfb1997-02-14 16:27:29 +000074 fastlocals = f->f_localsplus;
Jeremy Hylton30c9f392001-03-13 01:58:22 +000075 for (i = slots; --i >= 0; ++fastlocals) {
Guido van Rossum18752471997-04-29 14:49:28 +000076 Py_XDECREF(*fastlocals);
Guido van Rossum7582bfb1997-02-14 16:27:29 +000077 }
78
Guido van Rossum18752471997-04-29 14:49:28 +000079 Py_XDECREF(f->f_back);
80 Py_XDECREF(f->f_code);
81 Py_XDECREF(f->f_builtins);
82 Py_XDECREF(f->f_globals);
83 Py_XDECREF(f->f_locals);
84 Py_XDECREF(f->f_trace);
Guido van Rossuma027efa1997-05-05 20:56:21 +000085 Py_XDECREF(f->f_exc_type);
86 Py_XDECREF(f->f_exc_value);
87 Py_XDECREF(f->f_exc_traceback);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000088 f->f_back = free_list;
89 free_list = f;
Guido van Rossumd724b232000-03-13 16:01:29 +000090 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +000091}
92
Guido van Rossum18752471997-04-29 14:49:28 +000093PyTypeObject PyFrame_Type = {
94 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3f5da241990-12-20 15:06:42 +000095 0,
96 "frame",
Guido van Rossum18752471997-04-29 14:49:28 +000097 sizeof(PyFrameObject),
Guido van Rossum3f5da241990-12-20 15:06:42 +000098 0,
Guido van Rossum1d5735e1994-08-30 08:27:36 +000099 (destructor)frame_dealloc, /*tp_dealloc*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000101 (getattrfunc)frame_getattr, /*tp_getattr*/
102 (setattrfunc)frame_setattr, /*tp_setattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103 0, /*tp_compare*/
104 0, /*tp_repr*/
105 0, /*tp_as_number*/
106 0, /*tp_as_sequence*/
107 0, /*tp_as_mapping*/
108};
109
Guido van Rossum18752471997-04-29 14:49:28 +0000110PyFrameObject *
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000111PyFrame_New(PyThreadState *tstate, PyCodeObject *code, PyObject *globals,
Jeremy Hylton30c9f392001-03-13 01:58:22 +0000112 PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000114 PyFrameObject *back = tstate->frame;
Guido van Rossum18752471997-04-29 14:49:28 +0000115 static PyObject *builtin_object;
116 PyFrameObject *f;
117 PyObject *builtins;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000118 int extras, ncells, nfrees;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000119
Sjoerd Mullender5b7f3cd1995-04-04 11:47:41 +0000120 if (builtin_object == NULL) {
Guido van Rossumb56933e1997-01-18 07:58:41 +0000121 builtin_object = PyString_InternFromString("__builtins__");
Sjoerd Mullender5b7f3cd1995-04-04 11:47:41 +0000122 if (builtin_object == NULL)
123 return NULL;
124 }
Guido van Rossum18752471997-04-29 14:49:28 +0000125 if ((back != NULL && !PyFrame_Check(back)) ||
126 code == NULL || !PyCode_Check(code) ||
127 globals == NULL || !PyDict_Check(globals) ||
128 (locals != NULL && !PyDict_Check(locals))) {
129 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000130 return NULL;
131 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000132 ncells = PyTuple_GET_SIZE(code->co_cellvars);
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000133 nfrees = PyTuple_GET_SIZE(code->co_freevars);
134 extras = code->co_stacksize + code->co_nlocals + ncells + nfrees;
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000135 if (back == NULL || back->f_globals != globals) {
136 builtins = PyDict_GetItem(globals, builtin_object);
137 if (builtins != NULL && PyModule_Check(builtins))
138 builtins = PyModule_GetDict(builtins);
139 }
140 else {
141 /* If we share the globals, we share the builtins.
142 Save a lookup and a call. */
143 builtins = back->f_builtins;
144 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000145 if (builtins != NULL && !PyDict_Check(builtins))
146 builtins = NULL;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000147 if (free_list == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000148 /* PyObject_New is inlined */
Guido van Rossum18752471997-04-29 14:49:28 +0000149 f = (PyFrameObject *)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150 PyObject_MALLOC(sizeof(PyFrameObject) +
151 extras*sizeof(PyObject *));
Guido van Rossum2271bf71995-07-18 14:30:34 +0000152 if (f == NULL)
Guido van Rossum18752471997-04-29 14:49:28 +0000153 return (PyFrameObject *)PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000154 PyObject_INIT(f, &PyFrame_Type);
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000155 f->f_size = extras;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000156 }
157 else {
158 f = free_list;
159 free_list = free_list->f_back;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000160 if (f->f_size < extras) {
Guido van Rossum6345ac61997-10-31 20:32:13 +0000161 f = (PyFrameObject *)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000162 PyObject_REALLOC(f, sizeof(PyFrameObject) +
163 extras*sizeof(PyObject *));
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000164 if (f == NULL)
Guido van Rossum18752471997-04-29 14:49:28 +0000165 return (PyFrameObject *)PyErr_NoMemory();
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000166 f->f_size = extras;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000167 }
Guido van Rossum747596a1997-01-24 04:00:21 +0000168 else
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000169 extras = f->f_size;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000170 PyObject_INIT(f, &PyFrame_Type);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000171 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000172 if (builtins == NULL) {
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000173 /* No builtins! Make up a minimal one. */
Guido van Rossum404b95d1997-08-05 02:09:46 +0000174 builtins = PyDict_New();
Guido van Rossumf61618c1998-10-19 14:20:20 +0000175 if (builtins == NULL || /* Give them 'None', at least. */
176 PyDict_SetItemString(builtins, "None", Py_None) < 0) {
177 Py_DECREF(f);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000178 return NULL;
Guido van Rossumf61618c1998-10-19 14:20:20 +0000179 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000180 }
181 else
182 Py_XINCREF(builtins);
183 f->f_builtins = builtins;
Guido van Rossum18752471997-04-29 14:49:28 +0000184 Py_XINCREF(back);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000185 f->f_back = back;
Guido van Rossum18752471997-04-29 14:49:28 +0000186 Py_INCREF(code);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000187 f->f_code = code;
Guido van Rossum18752471997-04-29 14:49:28 +0000188 Py_INCREF(globals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000189 f->f_globals = globals;
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000190 if (code->co_flags & CO_NEWLOCALS) {
191 if (code->co_flags & CO_OPTIMIZED)
192 locals = NULL; /* Let fast_2_locals handle it */
193 else {
Guido van Rossum18752471997-04-29 14:49:28 +0000194 locals = PyDict_New();
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000195 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000196 Py_DECREF(f);
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000197 return NULL;
198 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000199 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000200 }
Guido van Rossum2271bf71995-07-18 14:30:34 +0000201 else {
202 if (locals == NULL)
203 locals = globals;
Guido van Rossum18752471997-04-29 14:49:28 +0000204 Py_INCREF(locals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000205 }
206 f->f_locals = locals;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000207 f->f_trace = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000209 f->f_tstate = tstate;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000210
Guido van Rossum2271bf71995-07-18 14:30:34 +0000211 f->f_lasti = 0;
Guido van Rossum747596a1997-01-24 04:00:21 +0000212 f->f_lineno = code->co_firstlineno;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000213 f->f_restricted = (builtins != tstate->interp->builtins);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000214 f->f_iblock = 0;
215 f->f_nlocals = code->co_nlocals;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000216 f->f_stacksize = code->co_stacksize;
217 f->f_ncells = ncells;
218 f->f_nfreevars = nfrees;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000219
220 while (--extras >= 0)
221 f->f_localsplus[extras] = NULL;
222
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000223 f->f_valuestack = f->f_localsplus + (f->f_nlocals + ncells + nfrees);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000224
Guido van Rossum3f5da241990-12-20 15:06:42 +0000225 return f;
226}
227
Guido van Rossum3f5da241990-12-20 15:06:42 +0000228/* Block management */
229
230void
Fred Drake1b190b42000-07-09 05:40:56 +0000231PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000232{
Guido van Rossum18752471997-04-29 14:49:28 +0000233 PyTryBlock *b;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000234 if (f->f_iblock >= CO_MAXBLOCKS)
Guido van Rossum18752471997-04-29 14:49:28 +0000235 Py_FatalError("XXX block stack overflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000236 b = &f->f_blockstack[f->f_iblock++];
237 b->b_type = type;
238 b->b_level = level;
239 b->b_handler = handler;
240}
241
Guido van Rossum18752471997-04-29 14:49:28 +0000242PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000243PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000244{
Guido van Rossum18752471997-04-29 14:49:28 +0000245 PyTryBlock *b;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000246 if (f->f_iblock <= 0)
Guido van Rossum18752471997-04-29 14:49:28 +0000247 Py_FatalError("XXX block stack underflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000248 b = &f->f_blockstack[--f->f_iblock];
249 return b;
250}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000251
252/* Convert between "fast" version of locals and dictionary version */
253
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000254static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000255map_to_dict(PyObject *map, int nmap, PyObject *dict, PyObject **values,
256 int deref)
257{
258 int j;
259 for (j = nmap; --j >= 0; ) {
260 PyObject *key = PyTuple_GetItem(map, j);
261 PyObject *value = values[j];
262 if (deref)
263 value = PyCell_GET(value);
264 if (value == NULL) {
265 PyErr_Clear();
266 if (PyDict_DelItem(dict, key) != 0)
267 PyErr_Clear();
268 }
269 else {
270 if (PyDict_SetItem(dict, key, value) != 0)
271 PyErr_Clear();
272 }
273 }
274}
275
Guido van Rossum6b356e72001-04-14 17:55:41 +0000276static void
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000277dict_to_map(PyObject *map, int nmap, PyObject *dict, PyObject **values,
278 int deref, int clear)
279{
280 int j;
281 for (j = nmap; --j >= 0; ) {
282 PyObject *key = PyTuple_GetItem(map, j);
283 PyObject *value = PyDict_GetItem(dict, key);
284 Py_XINCREF(value);
285 if (deref) {
286 if (value) {
287 if (PyCell_Set(values[j], value) < 0)
288 PyErr_Clear();
289 } else if (clear) {
290 Py_XDECREF(values[j]);
291 values[j] = value;
292 }
293 } else if (value != NULL || clear) {
294 Py_XDECREF(values[j]);
295 values[j] = value;
296 }
297 }
298}
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000299
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000300void
Fred Drake1b190b42000-07-09 05:40:56 +0000301PyFrame_FastToLocals(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000302{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000303 /* Merge fast locals into f->f_locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000304 PyObject *locals, *map;
305 PyObject **fast;
306 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000307 int j;
308 if (f == NULL)
309 return;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000310 locals = f->f_locals;
311 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000312 locals = f->f_locals = PyDict_New();
Guido van Rossum2271bf71995-07-18 14:30:34 +0000313 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000314 PyErr_Clear(); /* Can't report it :-( */
Guido van Rossum2271bf71995-07-18 14:30:34 +0000315 return;
316 }
317 }
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000318 if (f->f_nlocals == 0)
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000319 return;
320 map = f->f_code->co_varnames;
Guido van Rossum18752471997-04-29 14:49:28 +0000321 if (!PyDict_Check(locals) || !PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000322 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000323 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000324 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000325 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000326 if (j > f->f_nlocals)
327 j = f->f_nlocals;
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000328 map_to_dict(map, j, locals, fast, 0);
329 if (f->f_ncells || f->f_nfreevars) {
330 if (!(PyTuple_Check(f->f_code->co_cellvars)
331 && PyTuple_Check(f->f_code->co_freevars))) {
332 Py_DECREF(locals);
333 return;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000334 }
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000335 map_to_dict(f->f_code->co_cellvars,
336 PyTuple_GET_SIZE(f->f_code->co_cellvars),
337 locals, fast + f->f_nlocals, 1);
338 map_to_dict(f->f_code->co_freevars,
339 PyTuple_GET_SIZE(f->f_code->co_freevars),
340 locals, fast + f->f_nlocals + f->f_ncells, 1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000341 }
Guido van Rossum18752471997-04-29 14:49:28 +0000342 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000343}
344
345void
Fred Drake1b190b42000-07-09 05:40:56 +0000346PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000347{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000348 /* Merge f->f_locals into fast locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000349 PyObject *locals, *map;
350 PyObject **fast;
351 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000352 int j;
353 if (f == NULL)
354 return;
355 locals = f->f_locals;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000356 map = f->f_code->co_varnames;
Guido van Rossum3bb63a81997-01-20 04:29:16 +0000357 if (locals == NULL || f->f_code->co_nlocals == 0)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000358 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000359 if (!PyDict_Check(locals) || !PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000360 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000361 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000362 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000363 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000364 if (j > f->f_nlocals)
365 j = f->f_nlocals;
Jeremy Hylton220ae7c2001-03-21 16:43:47 +0000366 dict_to_map(f->f_code->co_varnames, j, locals, fast, 0, clear);
367 if (f->f_ncells || f->f_nfreevars) {
368 if (!(PyTuple_Check(f->f_code->co_cellvars)
369 && PyTuple_Check(f->f_code->co_freevars)))
370 return;
371 dict_to_map(f->f_code->co_cellvars,
372 PyTuple_GET_SIZE(f->f_code->co_cellvars),
373 locals, fast, 1, clear);
374 dict_to_map(f->f_code->co_freevars,
375 PyTuple_GET_SIZE(f->f_code->co_freevars),
376 locals, fast, 1, clear);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000377 }
Guido van Rossum18752471997-04-29 14:49:28 +0000378 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000379}
Guido van Rossum404b95d1997-08-05 02:09:46 +0000380
381/* Clear out the free list */
382
383void
Fred Drake1b190b42000-07-09 05:40:56 +0000384PyFrame_Fini(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +0000385{
386 while (free_list != NULL) {
387 PyFrameObject *f = free_list;
388 free_list = free_list->f_back;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000389 PyObject_DEL(f);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000390 }
391}