blob: f541f1e9c79e38a2472d0c13f93927669bac1cfa [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
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000052 Note that the value and block stacks are preserved -- this can save
53 another malloc() call or two (and two free() calls as well!).
54 Also note that, unlike for integers, each frame object is a
55 malloc'ed object in its own right -- it is only the actual calls to
56 malloc() that we are trying to save here, not the administration.
57 After all, while a typical program may make millions of calls, a
58 call depth of more than 20 or 30 is probably already exceptional
59 unless the program contains run-away recursion. I hope.
60*/
61
Guido van Rossum18752471997-04-29 14:49:28 +000062static PyFrameObject *free_list = NULL;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000063
Guido van Rossum3f5da241990-12-20 15:06:42 +000064static void
Fred Drake1b190b42000-07-09 05:40:56 +000065frame_dealloc(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +000066{
Guido van Rossum7582bfb1997-02-14 16:27:29 +000067 int i;
68 PyObject **fastlocals;
69
Guido van Rossumd724b232000-03-13 16:01:29 +000070 Py_TRASHCAN_SAFE_BEGIN(f)
Guido van Rossum7582bfb1997-02-14 16:27:29 +000071 /* Kill all local variables */
72 fastlocals = f->f_localsplus;
73 for (i = f->f_nlocals; --i >= 0; ++fastlocals) {
Guido van Rossum18752471997-04-29 14:49:28 +000074 Py_XDECREF(*fastlocals);
Guido van Rossum7582bfb1997-02-14 16:27:29 +000075 }
76
Guido van Rossum18752471997-04-29 14:49:28 +000077 Py_XDECREF(f->f_back);
78 Py_XDECREF(f->f_code);
79 Py_XDECREF(f->f_builtins);
80 Py_XDECREF(f->f_globals);
81 Py_XDECREF(f->f_locals);
82 Py_XDECREF(f->f_trace);
Guido van Rossuma027efa1997-05-05 20:56:21 +000083 Py_XDECREF(f->f_exc_type);
84 Py_XDECREF(f->f_exc_value);
85 Py_XDECREF(f->f_exc_traceback);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000086 f->f_back = free_list;
87 free_list = f;
Guido van Rossumd724b232000-03-13 16:01:29 +000088 Py_TRASHCAN_SAFE_END(f)
Guido van Rossum3f5da241990-12-20 15:06:42 +000089}
90
Guido van Rossum18752471997-04-29 14:49:28 +000091PyTypeObject PyFrame_Type = {
92 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum3f5da241990-12-20 15:06:42 +000093 0,
94 "frame",
Guido van Rossum18752471997-04-29 14:49:28 +000095 sizeof(PyFrameObject),
Guido van Rossum3f5da241990-12-20 15:06:42 +000096 0,
Guido van Rossum1d5735e1994-08-30 08:27:36 +000097 (destructor)frame_dealloc, /*tp_dealloc*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000098 0, /*tp_print*/
Guido van Rossum1d5735e1994-08-30 08:27:36 +000099 (getattrfunc)frame_getattr, /*tp_getattr*/
100 (setattrfunc)frame_setattr, /*tp_setattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +0000101 0, /*tp_compare*/
102 0, /*tp_repr*/
103 0, /*tp_as_number*/
104 0, /*tp_as_sequence*/
105 0, /*tp_as_mapping*/
106};
107
Guido van Rossum18752471997-04-29 14:49:28 +0000108PyFrameObject *
Fred Drake1b190b42000-07-09 05:40:56 +0000109PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
110 PyObject *globals, PyObject *locals)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000112 PyFrameObject *back = tstate->frame;
Guido van Rossum18752471997-04-29 14:49:28 +0000113 static PyObject *builtin_object;
114 PyFrameObject *f;
115 PyObject *builtins;
Guido van Rossum67906af1998-09-25 14:11:46 +0000116 int extras;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000117
Sjoerd Mullender5b7f3cd1995-04-04 11:47:41 +0000118 if (builtin_object == NULL) {
Guido van Rossumb56933e1997-01-18 07:58:41 +0000119 builtin_object = PyString_InternFromString("__builtins__");
Sjoerd Mullender5b7f3cd1995-04-04 11:47:41 +0000120 if (builtin_object == NULL)
121 return NULL;
122 }
Guido van Rossum18752471997-04-29 14:49:28 +0000123 if ((back != NULL && !PyFrame_Check(back)) ||
124 code == NULL || !PyCode_Check(code) ||
125 globals == NULL || !PyDict_Check(globals) ||
126 (locals != NULL && !PyDict_Check(locals))) {
127 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000128 return NULL;
129 }
Guido van Rossum67906af1998-09-25 14:11:46 +0000130 extras = code->co_stacksize + code->co_nlocals;
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000131 if (back == NULL || back->f_globals != globals) {
132 builtins = PyDict_GetItem(globals, builtin_object);
133 if (builtins != NULL && PyModule_Check(builtins))
134 builtins = PyModule_GetDict(builtins);
135 }
136 else {
137 /* If we share the globals, we share the builtins.
138 Save a lookup and a call. */
139 builtins = back->f_builtins;
140 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000141 if (builtins != NULL && !PyDict_Check(builtins))
142 builtins = NULL;
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000143 if (free_list == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000144 /* PyObject_New is inlined */
Guido van Rossum18752471997-04-29 14:49:28 +0000145 f = (PyFrameObject *)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000146 PyObject_MALLOC(sizeof(PyFrameObject) +
147 extras*sizeof(PyObject *));
Guido van Rossum2271bf71995-07-18 14:30:34 +0000148 if (f == NULL)
Guido van Rossum18752471997-04-29 14:49:28 +0000149 return (PyFrameObject *)PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000150 PyObject_INIT(f, &PyFrame_Type);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000151 }
152 else {
153 f = free_list;
154 free_list = free_list->f_back;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000155 if (f->f_nlocals + f->f_stacksize < extras) {
Guido van Rossum6345ac61997-10-31 20:32:13 +0000156 f = (PyFrameObject *)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000157 PyObject_REALLOC(f, sizeof(PyFrameObject) +
158 extras*sizeof(PyObject *));
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000159 if (f == NULL)
Guido van Rossum18752471997-04-29 14:49:28 +0000160 return (PyFrameObject *)PyErr_NoMemory();
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000161 }
Guido van Rossum747596a1997-01-24 04:00:21 +0000162 else
163 extras = f->f_nlocals + f->f_stacksize;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000164 PyObject_INIT(f, &PyFrame_Type);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000165 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000166 if (builtins == NULL) {
Guido van Rossumbde6ff71998-02-19 20:48:26 +0000167 /* No builtins! Make up a minimal one. */
Guido van Rossum404b95d1997-08-05 02:09:46 +0000168 builtins = PyDict_New();
Guido van Rossumf61618c1998-10-19 14:20:20 +0000169 if (builtins == NULL || /* Give them 'None', at least. */
170 PyDict_SetItemString(builtins, "None", Py_None) < 0) {
171 Py_DECREF(f);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000172 return NULL;
Guido van Rossumf61618c1998-10-19 14:20:20 +0000173 }
Guido van Rossum404b95d1997-08-05 02:09:46 +0000174 }
175 else
176 Py_XINCREF(builtins);
177 f->f_builtins = builtins;
Guido van Rossum18752471997-04-29 14:49:28 +0000178 Py_XINCREF(back);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000179 f->f_back = back;
Guido van Rossum18752471997-04-29 14:49:28 +0000180 Py_INCREF(code);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000181 f->f_code = code;
Guido van Rossum18752471997-04-29 14:49:28 +0000182 Py_INCREF(globals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000183 f->f_globals = globals;
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000184 if (code->co_flags & CO_NEWLOCALS) {
185 if (code->co_flags & CO_OPTIMIZED)
186 locals = NULL; /* Let fast_2_locals handle it */
187 else {
Guido van Rossum18752471997-04-29 14:49:28 +0000188 locals = PyDict_New();
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000189 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000190 Py_DECREF(f);
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000191 return NULL;
192 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000193 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000194 }
Guido van Rossum2271bf71995-07-18 14:30:34 +0000195 else {
196 if (locals == NULL)
197 locals = globals;
Guido van Rossum18752471997-04-29 14:49:28 +0000198 Py_INCREF(locals);
Guido van Rossum2271bf71995-07-18 14:30:34 +0000199 }
200 f->f_locals = locals;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000201 f->f_trace = NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000202 f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000203 f->f_tstate = tstate;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000204
Guido van Rossum2271bf71995-07-18 14:30:34 +0000205 f->f_lasti = 0;
Guido van Rossum747596a1997-01-24 04:00:21 +0000206 f->f_lineno = code->co_firstlineno;
Guido van Rossumeb46d671997-08-02 02:59:08 +0000207 f->f_restricted = (builtins != tstate->interp->builtins);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000208 f->f_iblock = 0;
209 f->f_nlocals = code->co_nlocals;
Guido van Rossum747596a1997-01-24 04:00:21 +0000210 f->f_stacksize = extras - code->co_nlocals;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000211
212 while (--extras >= 0)
213 f->f_localsplus[extras] = NULL;
214
215 f->f_valuestack = f->f_localsplus + f->f_nlocals;
216
Guido van Rossum3f5da241990-12-20 15:06:42 +0000217 return f;
218}
219
Guido van Rossum3f5da241990-12-20 15:06:42 +0000220/* Block management */
221
222void
Fred Drake1b190b42000-07-09 05:40:56 +0000223PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000224{
Guido van Rossum18752471997-04-29 14:49:28 +0000225 PyTryBlock *b;
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000226 if (f->f_iblock >= CO_MAXBLOCKS)
Guido van Rossum18752471997-04-29 14:49:28 +0000227 Py_FatalError("XXX block stack overflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000228 b = &f->f_blockstack[f->f_iblock++];
229 b->b_type = type;
230 b->b_level = level;
231 b->b_handler = handler;
232}
233
Guido van Rossum18752471997-04-29 14:49:28 +0000234PyTryBlock *
Fred Drake1b190b42000-07-09 05:40:56 +0000235PyFrame_BlockPop(PyFrameObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000236{
Guido van Rossum18752471997-04-29 14:49:28 +0000237 PyTryBlock *b;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000238 if (f->f_iblock <= 0)
Guido van Rossum18752471997-04-29 14:49:28 +0000239 Py_FatalError("XXX block stack underflow");
Guido van Rossum3f5da241990-12-20 15:06:42 +0000240 b = &f->f_blockstack[--f->f_iblock];
241 return b;
242}
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000243
244/* Convert between "fast" version of locals and dictionary version */
245
246void
Fred Drake1b190b42000-07-09 05:40:56 +0000247PyFrame_FastToLocals(PyFrameObject *f)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000248{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000249 /* Merge fast locals into f->f_locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000250 PyObject *locals, *map;
251 PyObject **fast;
252 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000253 int j;
254 if (f == NULL)
255 return;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000256 locals = f->f_locals;
257 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000258 locals = f->f_locals = PyDict_New();
Guido van Rossum2271bf71995-07-18 14:30:34 +0000259 if (locals == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000260 PyErr_Clear(); /* Can't report it :-( */
Guido van Rossum2271bf71995-07-18 14:30:34 +0000261 return;
262 }
263 }
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000264 if (f->f_nlocals == 0)
Guido van Rossumbdd207a1995-07-26 16:14:30 +0000265 return;
266 map = f->f_code->co_varnames;
Guido van Rossum18752471997-04-29 14:49:28 +0000267 if (!PyDict_Check(locals) || !PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000268 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000269 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000270 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000271 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000272 if (j > f->f_nlocals)
273 j = f->f_nlocals;
274 for (; --j >= 0; ) {
Guido van Rossum18752471997-04-29 14:49:28 +0000275 PyObject *key = PyTuple_GetItem(map, j);
276 PyObject *value = fast[j];
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000277 if (value == NULL) {
Guido van Rossum18752471997-04-29 14:49:28 +0000278 PyErr_Clear();
279 if (PyDict_DelItem(locals, key) != 0)
280 PyErr_Clear();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000281 }
282 else {
Guido van Rossum18752471997-04-29 14:49:28 +0000283 if (PyDict_SetItem(locals, key, value) != 0)
284 PyErr_Clear();
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000285 }
286 }
Guido van Rossum18752471997-04-29 14:49:28 +0000287 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000288}
289
290void
Fred Drake1b190b42000-07-09 05:40:56 +0000291PyFrame_LocalsToFast(PyFrameObject *f, int clear)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000292{
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000293 /* Merge f->f_locals into fast locals */
Guido van Rossum18752471997-04-29 14:49:28 +0000294 PyObject *locals, *map;
295 PyObject **fast;
296 PyObject *error_type, *error_value, *error_traceback;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000297 int j;
298 if (f == NULL)
299 return;
300 locals = f->f_locals;
Guido van Rossum2271bf71995-07-18 14:30:34 +0000301 map = f->f_code->co_varnames;
Guido van Rossum3bb63a81997-01-20 04:29:16 +0000302 if (locals == NULL || f->f_code->co_nlocals == 0)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000303 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000304 if (!PyDict_Check(locals) || !PyTuple_Check(map))
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000305 return;
Guido van Rossum18752471997-04-29 14:49:28 +0000306 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000307 fast = f->f_localsplus;
Guido van Rossum18752471997-04-29 14:49:28 +0000308 j = PyTuple_Size(map);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000309 if (j > f->f_nlocals)
310 j = f->f_nlocals;
311 for (; --j >= 0; ) {
Guido van Rossum18752471997-04-29 14:49:28 +0000312 PyObject *key = PyTuple_GetItem(map, j);
313 PyObject *value = PyDict_GetItem(locals, key);
Guido van Rossume23eb571998-05-14 01:49:48 +0000314 Py_XINCREF(value);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000315 if (value != NULL || clear) {
Guido van Rossum18752471997-04-29 14:49:28 +0000316 Py_XDECREF(fast[j]);
Guido van Rossumf3e85a01997-01-20 04:20:52 +0000317 fast[j] = value;
318 }
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000319 }
Guido van Rossum18752471997-04-29 14:49:28 +0000320 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000321}
Guido van Rossum404b95d1997-08-05 02:09:46 +0000322
323/* Clear out the free list */
324
325void
Fred Drake1b190b42000-07-09 05:40:56 +0000326PyFrame_Fini(void)
Guido van Rossum404b95d1997-08-05 02:09:46 +0000327{
328 while (free_list != NULL) {
329 PyFrameObject *f = free_list;
330 free_list = free_list->f_back;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000331 PyObject_DEL(f);
Guido van Rossum404b95d1997-08-05 02:09:46 +0000332 }
333}