(py-indent-right, py-outdent-left): new commands, bound to C-c C-r and
[python/dscho.git] / Objects / frameobject.c
blobbeb4c2db6454ab92e7bf8548acb2f2d41ad69472
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Frame object implementation */
27 #include "allobjects.h"
29 #include "compile.h"
30 #include "frameobject.h"
31 #include "opcode.h"
32 #include "structmember.h"
33 #include "bltinmodule.h"
35 #define OFF(x) offsetof(frameobject, x)
37 static struct memberlist frame_memberlist[] = {
38 {"f_back", T_OBJECT, OFF(f_back), RO},
39 {"f_code", T_OBJECT, OFF(f_code), RO},
40 {"f_builtins", T_OBJECT, OFF(f_builtins),RO},
41 {"f_globals", T_OBJECT, OFF(f_globals), RO},
42 {"f_locals", T_OBJECT, OFF(f_locals), RO},
43 {"f_owner", T_OBJECT, OFF(f_owner), RO},
44 #if 0
45 {"f_fastlocals",T_OBJECT, OFF(f_fastlocals),RO}, /* XXX Unsafe */
46 #endif
47 {"f_localmap", T_OBJECT, OFF(f_localmap),RO},
48 {"f_lasti", T_INT, OFF(f_lasti), RO},
49 {"f_lineno", T_INT, OFF(f_lineno), RO},
50 {"f_restricted",T_INT, OFF(f_restricted),RO},
51 {"f_trace", T_OBJECT, OFF(f_trace)},
52 {NULL} /* Sentinel */
55 static object *
56 frame_getattr(f, name)
57 frameobject *f;
58 char *name;
60 if (strcmp(name, "f_locals") == 0)
61 fast_2_locals(f);
62 return getmember((char *)f, frame_memberlist, name);
65 static int
66 frame_setattr(f, name, value)
67 frameobject *f;
68 char *name;
69 object *value;
71 return setmember((char *)f, frame_memberlist, name, value);
74 /* Stack frames are allocated and deallocated at a considerable rate.
75 In an attempt to improve the speed of function calls, we maintain a
76 separate free list of stack frames (just like integers are
77 allocated in a special way -- see intobject.c). When a stack frame
78 is on the free list, only the following members have a meaning:
79 ob_type == &Frametype
80 f_back next item on free list, or NULL
81 f_nvalues size of f_valuestack
82 f_valuestack array of (f_nvalues+1) object pointers, or NULL
83 f_nblocks size of f_blockstack
84 f_blockstack array of (f_nblocks+1) blocks, or NULL
85 Note that the value and block stacks are preserved -- this can save
86 another malloc() call or two (and two free() calls as well!).
87 Also note that, unlike for integers, each frame object is a
88 malloc'ed object in its own right -- it is only the actual calls to
89 malloc() that we are trying to save here, not the administration.
90 After all, while a typical program may make millions of calls, a
91 call depth of more than 20 or 30 is probably already exceptional
92 unless the program contains run-away recursion. I hope.
95 static frameobject *free_list = NULL;
97 static void
98 frame_dealloc(f)
99 frameobject *f;
101 XDECREF(f->f_back);
102 XDECREF(f->f_code);
103 XDECREF(f->f_builtins);
104 XDECREF(f->f_globals);
105 XDECREF(f->f_locals);
106 XDECREF(f->f_owner);
107 XDECREF(f->f_fastlocals);
108 XDECREF(f->f_localmap);
109 XDECREF(f->f_trace);
110 f->f_back = free_list;
111 free_list = f;
114 typeobject Frametype = {
115 OB_HEAD_INIT(&Typetype)
117 "frame",
118 sizeof(frameobject),
120 (destructor)frame_dealloc, /*tp_dealloc*/
121 0, /*tp_print*/
122 (getattrfunc)frame_getattr, /*tp_getattr*/
123 (setattrfunc)frame_setattr, /*tp_setattr*/
124 0, /*tp_compare*/
125 0, /*tp_repr*/
126 0, /*tp_as_number*/
127 0, /*tp_as_sequence*/
128 0, /*tp_as_mapping*/
131 frameobject *
132 newframeobject(back, code, globals, locals, owner, nvalues, nblocks)
133 frameobject *back;
134 codeobject *code;
135 object *globals;
136 object *locals;
137 object *owner;
138 int nvalues;
139 int nblocks;
141 frameobject *f;
142 object *builtins;
143 if ((back != NULL && !is_frameobject(back)) ||
144 code == NULL || !is_codeobject(code) ||
145 globals == NULL || !is_dictobject(globals) ||
146 locals == NULL || !is_dictobject(locals) ||
147 nvalues < 0 || nblocks < 0) {
148 err_badcall();
149 return NULL;
151 builtins = dictlookup(globals, "__builtins__");
152 if (builtins != NULL && is_moduleobject(builtins))
153 builtins = getmoduledict(builtins);
154 if (builtins == NULL || !is_mappingobject(builtins)) {
155 err_setstr(TypeError, "bad __builtins__ dictionary");
156 return NULL;
158 if (free_list == NULL) {
159 f = NEWOBJ(frameobject, &Frametype);
160 f->f_nvalues = f->f_nblocks = 0;
161 f->f_valuestack = NULL;
162 f->f_blockstack = NULL;
164 else {
165 f = free_list;
166 free_list = free_list->f_back;
167 f->ob_type = &Frametype;
168 NEWREF(f);
170 if (f != NULL) {
171 XINCREF(back);
172 f->f_back = back;
173 INCREF(code);
174 f->f_code = code;
175 XINCREF(builtins);
176 f->f_builtins = builtins;
177 INCREF(globals);
178 f->f_globals = globals;
179 INCREF(locals);
180 f->f_locals = locals;
181 XINCREF(owner);
182 f->f_owner = owner;
183 f->f_fastlocals = NULL;
184 f->f_localmap = NULL;
185 if (nvalues > f->f_nvalues || f->f_valuestack == NULL) {
186 XDEL(f->f_valuestack);
187 f->f_valuestack = NEW(object *, nvalues+1);
188 f->f_nvalues = nvalues;
190 if (nblocks > f->f_nblocks || f->f_blockstack == NULL) {
191 XDEL(f->f_blockstack);
192 f->f_blockstack = NEW(block, nblocks+1);
193 f->f_nblocks = nblocks;
195 f->f_iblock = 0;
196 f->f_lasti = 0;
197 f->f_lineno = -1;
198 f->f_restricted = (builtins != getbuiltindict());
199 f->f_trace = NULL;
200 if (f->f_valuestack == NULL || f->f_blockstack == NULL) {
201 err_nomem();
202 DECREF(f);
203 f = NULL;
206 return f;
209 object **
210 extend_stack(f, level, incr)
211 frameobject *f;
212 int level;
213 int incr;
215 f->f_nvalues = level + incr + 10;
216 f->f_valuestack =
217 (object **) realloc((ANY *)f->f_valuestack,
218 sizeof(object *) * (f->f_nvalues + 1));
219 if (f->f_valuestack == NULL) {
220 err_nomem();
221 return NULL;
223 return f->f_valuestack + level;
226 /* Block management */
228 void
229 setup_block(f, type, handler, level)
230 frameobject *f;
231 int type;
232 int handler;
233 int level;
235 block *b;
236 if (f->f_iblock >= f->f_nblocks)
237 fatal("XXX block stack overflow");
238 b = &f->f_blockstack[f->f_iblock++];
239 b->b_type = type;
240 b->b_level = level;
241 b->b_handler = handler;
244 block *
245 pop_block(f)
246 frameobject *f;
248 block *b;
249 if (f->f_iblock <= 0)
250 fatal("XXX block stack underflow");
251 b = &f->f_blockstack[--f->f_iblock];
252 return b;
255 /* Convert between "fast" version of locals and dictionary version */
257 void
258 fast_2_locals(f)
259 frameobject *f;
261 /* Merge f->f_fastlocals into f->f_locals */
262 object *locals, *fast, *map;
263 object *error_type, *error_value, *error_traceback;
264 int j;
265 if (f == NULL)
266 return;
267 locals = f->f_locals;
268 fast = f->f_fastlocals;
269 map = f->f_localmap;
270 if (locals == NULL || fast == NULL || map == NULL)
271 return;
272 if (!is_dictobject(locals) || !is_listobject(fast) ||
273 !is_tupleobject(map))
274 return;
275 err_fetch(&error_type, &error_value, &error_traceback);
276 for (j = gettuplesize(map); --j >= 0; ) {
277 object *key = gettupleitem(map, j);
278 object *value = getlistitem(fast, j);
279 if (value == NULL) {
280 err_clear();
281 if (dict2remove(locals, key) != 0)
282 err_clear();
284 else {
285 if (dict2insert(locals, key, value) != 0)
286 err_clear();
289 err_restore(error_type, error_value, error_traceback);
292 void
293 locals_2_fast(f, clear)
294 frameobject *f;
295 int clear;
297 /* Merge f->f_locals into f->f_fastlocals */
298 object *locals, *fast, *map;
299 object *error_type, *error_value, *error_traceback;
300 int j;
301 if (f == NULL)
302 return;
303 locals = f->f_locals;
304 fast = f->f_fastlocals;
305 map = f->f_localmap;
306 if (locals == NULL || fast == NULL || map == NULL)
307 return;
308 if (!is_dictobject(locals) || !is_listobject(fast) ||
309 !is_tupleobject(map))
310 return;
311 err_fetch(&error_type, &error_value, &error_traceback);
312 for (j = gettuplesize(map); --j >= 0; ) {
313 object *key = gettupleitem(map, j);
314 object *value = dict2lookup(locals, key);
315 if (value == NULL)
316 err_clear();
317 else
318 INCREF(value);
319 if (value != NULL || clear)
320 if (setlistitem(fast, j, value) != 0)
321 err_clear();
323 err_restore(error_type, error_value, error_traceback);