Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | /* Frame object implementation */ |
| 33 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | |
| 36 | #include "compile.h" |
| 37 | #include "frameobject.h" |
| 38 | #include "opcode.h" |
| 39 | #include "structmember.h" |
| 40 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 41 | #define OFF(x) offsetof(PyFrameObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | |
| 43 | static struct memberlist frame_memberlist[] = { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 44 | {"f_back", T_OBJECT, OFF(f_back), RO}, |
| 45 | {"f_code", T_OBJECT, OFF(f_code), RO}, |
Guido van Rossum | c113482 | 1995-01-10 10:39:16 +0000 | [diff] [blame] | 46 | {"f_builtins", T_OBJECT, OFF(f_builtins),RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 47 | {"f_globals", T_OBJECT, OFF(f_globals), RO}, |
| 48 | {"f_locals", T_OBJECT, OFF(f_locals), RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 49 | {"f_lasti", T_INT, OFF(f_lasti), RO}, |
| 50 | {"f_lineno", T_INT, OFF(f_lineno), RO}, |
Guido van Rossum | c113482 | 1995-01-10 10:39:16 +0000 | [diff] [blame] | 51 | {"f_restricted",T_INT, OFF(f_restricted),RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 52 | {"f_trace", T_OBJECT, OFF(f_trace)}, |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 53 | {"f_exc_type", T_OBJECT, OFF(f_exc_type)}, |
| 54 | {"f_exc_value", T_OBJECT, OFF(f_exc_value)}, |
| 55 | {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 56 | {NULL} /* Sentinel */ |
| 57 | }; |
| 58 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 59 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 60 | frame_getattr(f, name) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 61 | PyFrameObject *f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 62 | char *name; |
| 63 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 64 | if (strcmp(name, "f_locals") == 0) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 65 | PyFrame_FastToLocals(f); |
| 66 | return PyMember_Get((char *)f, frame_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 69 | static int |
| 70 | frame_setattr(f, name, value) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 71 | PyFrameObject *f; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 72 | char *name; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 73 | PyObject *value; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 74 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 75 | return PyMember_Set((char *)f, frame_memberlist, name, value); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 78 | /* Stack frames are allocated and deallocated at a considerable rate. |
| 79 | In an attempt to improve the speed of function calls, we maintain a |
| 80 | separate free list of stack frames (just like integers are |
| 81 | allocated in a special way -- see intobject.c). When a stack frame |
| 82 | is on the free list, only the following members have a meaning: |
| 83 | ob_type == &Frametype |
| 84 | f_back next item on free list, or NULL |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 85 | f_nlocals number of locals |
| 86 | f_stacksize size of value stack |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 87 | Note that the value and block stacks are preserved -- this can save |
| 88 | another malloc() call or two (and two free() calls as well!). |
| 89 | Also note that, unlike for integers, each frame object is a |
| 90 | malloc'ed object in its own right -- it is only the actual calls to |
| 91 | malloc() that we are trying to save here, not the administration. |
| 92 | After all, while a typical program may make millions of calls, a |
| 93 | call depth of more than 20 or 30 is probably already exceptional |
| 94 | unless the program contains run-away recursion. I hope. |
| 95 | */ |
| 96 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 97 | static PyFrameObject *free_list = NULL; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 99 | static void |
| 100 | frame_dealloc(f) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 101 | PyFrameObject *f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 102 | { |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 103 | int i; |
| 104 | PyObject **fastlocals; |
| 105 | |
| 106 | /* Kill all local variables */ |
| 107 | fastlocals = f->f_localsplus; |
| 108 | for (i = f->f_nlocals; --i >= 0; ++fastlocals) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 109 | Py_XDECREF(*fastlocals); |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 112 | Py_XDECREF(f->f_back); |
| 113 | Py_XDECREF(f->f_code); |
| 114 | Py_XDECREF(f->f_builtins); |
| 115 | Py_XDECREF(f->f_globals); |
| 116 | Py_XDECREF(f->f_locals); |
| 117 | Py_XDECREF(f->f_trace); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 118 | Py_XDECREF(f->f_exc_type); |
| 119 | Py_XDECREF(f->f_exc_value); |
| 120 | Py_XDECREF(f->f_exc_traceback); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 121 | f->f_back = free_list; |
| 122 | free_list = f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 125 | PyTypeObject PyFrame_Type = { |
| 126 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 127 | 0, |
| 128 | "frame", |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 129 | sizeof(PyFrameObject), |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 130 | 0, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 131 | (destructor)frame_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 132 | 0, /*tp_print*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 133 | (getattrfunc)frame_getattr, /*tp_getattr*/ |
| 134 | (setattrfunc)frame_setattr, /*tp_setattr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 135 | 0, /*tp_compare*/ |
| 136 | 0, /*tp_repr*/ |
| 137 | 0, /*tp_as_number*/ |
| 138 | 0, /*tp_as_sequence*/ |
| 139 | 0, /*tp_as_mapping*/ |
| 140 | }; |
| 141 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 142 | PyFrameObject * |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 143 | PyFrame_New(tstate, code, globals, locals) |
| 144 | PyThreadState *tstate; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 145 | PyCodeObject *code; |
| 146 | PyObject *globals; |
| 147 | PyObject *locals; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 148 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 149 | PyFrameObject *back = tstate->frame; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 150 | static PyObject *builtin_object; |
| 151 | PyFrameObject *f; |
| 152 | PyObject *builtins; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 153 | int extras = code->co_stacksize + code->co_nlocals; |
| 154 | |
Sjoerd Mullender | 5b7f3cd | 1995-04-04 11:47:41 +0000 | [diff] [blame] | 155 | if (builtin_object == NULL) { |
Guido van Rossum | b56933e | 1997-01-18 07:58:41 +0000 | [diff] [blame] | 156 | builtin_object = PyString_InternFromString("__builtins__"); |
Sjoerd Mullender | 5b7f3cd | 1995-04-04 11:47:41 +0000 | [diff] [blame] | 157 | if (builtin_object == NULL) |
| 158 | return NULL; |
| 159 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 160 | if ((back != NULL && !PyFrame_Check(back)) || |
| 161 | code == NULL || !PyCode_Check(code) || |
| 162 | globals == NULL || !PyDict_Check(globals) || |
| 163 | (locals != NULL && !PyDict_Check(locals))) { |
| 164 | PyErr_BadInternalCall(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 167 | builtins = PyDict_GetItem(globals, builtin_object); |
| 168 | if (builtins != NULL && PyModule_Check(builtins)) |
| 169 | builtins = PyModule_GetDict(builtins); |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 170 | if (builtins != NULL && !PyDict_Check(builtins)) |
| 171 | builtins = NULL; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 172 | if (free_list == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 173 | f = (PyFrameObject *) |
| 174 | malloc(sizeof(PyFrameObject) + |
| 175 | extras*sizeof(PyObject *)); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 176 | if (f == NULL) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 177 | return (PyFrameObject *)PyErr_NoMemory(); |
| 178 | f->ob_type = &PyFrame_Type; |
| 179 | _Py_NewReference(f); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 180 | } |
| 181 | else { |
| 182 | f = free_list; |
| 183 | free_list = free_list->f_back; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 184 | if (f->f_nlocals + f->f_stacksize < extras) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 185 | f = realloc(f, sizeof(PyFrameObject) + |
| 186 | extras*sizeof(PyObject *)); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 187 | if (f == NULL) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 188 | return (PyFrameObject *)PyErr_NoMemory(); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 189 | } |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 190 | else |
| 191 | extras = f->f_nlocals + f->f_stacksize; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 192 | f->ob_type = &PyFrame_Type; |
| 193 | _Py_NewReference(f); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 194 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 195 | if (builtins == NULL) { |
| 196 | builtins = PyDict_New(); |
| 197 | if (builtins == NULL) |
| 198 | return NULL; |
| 199 | } |
| 200 | else |
| 201 | Py_XINCREF(builtins); |
| 202 | f->f_builtins = builtins; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 203 | Py_XINCREF(back); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 204 | f->f_back = back; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 205 | Py_INCREF(code); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 206 | f->f_code = code; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 207 | Py_INCREF(globals); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 208 | f->f_globals = globals; |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 209 | if (code->co_flags & CO_NEWLOCALS) { |
| 210 | if (code->co_flags & CO_OPTIMIZED) |
| 211 | locals = NULL; /* Let fast_2_locals handle it */ |
| 212 | else { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 213 | locals = PyDict_New(); |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 214 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 215 | Py_DECREF(f); |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 216 | return NULL; |
| 217 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 218 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 219 | } |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 220 | else { |
| 221 | if (locals == NULL) |
| 222 | locals = globals; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 223 | Py_INCREF(locals); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 224 | } |
| 225 | f->f_locals = locals; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 226 | f->f_trace = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 227 | f->f_exc_type = f->f_exc_value = f->f_exc_traceback = NULL; |
Guido van Rossum | eb46d67 | 1997-08-02 02:59:08 +0000 | [diff] [blame] | 228 | f->f_tstate = tstate; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 230 | f->f_lasti = 0; |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 231 | f->f_lineno = code->co_firstlineno; |
Guido van Rossum | eb46d67 | 1997-08-02 02:59:08 +0000 | [diff] [blame] | 232 | f->f_restricted = (builtins != tstate->interp->builtins); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 233 | f->f_iblock = 0; |
| 234 | f->f_nlocals = code->co_nlocals; |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 235 | f->f_stacksize = extras - code->co_nlocals; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 236 | |
| 237 | while (--extras >= 0) |
| 238 | f->f_localsplus[extras] = NULL; |
| 239 | |
| 240 | f->f_valuestack = f->f_localsplus + f->f_nlocals; |
| 241 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 242 | return f; |
| 243 | } |
| 244 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 245 | /* Block management */ |
| 246 | |
| 247 | void |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 248 | PyFrame_BlockSetup(f, type, handler, level) |
| 249 | PyFrameObject *f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 250 | int type; |
| 251 | int handler; |
| 252 | int level; |
| 253 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 254 | PyTryBlock *b; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 255 | if (f->f_iblock >= CO_MAXBLOCKS) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 256 | Py_FatalError("XXX block stack overflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 257 | b = &f->f_blockstack[f->f_iblock++]; |
| 258 | b->b_type = type; |
| 259 | b->b_level = level; |
| 260 | b->b_handler = handler; |
| 261 | } |
| 262 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 263 | PyTryBlock * |
| 264 | PyFrame_BlockPop(f) |
| 265 | PyFrameObject *f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 266 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 267 | PyTryBlock *b; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 268 | if (f->f_iblock <= 0) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 269 | Py_FatalError("XXX block stack underflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 270 | b = &f->f_blockstack[--f->f_iblock]; |
| 271 | return b; |
| 272 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 273 | |
| 274 | /* Convert between "fast" version of locals and dictionary version */ |
| 275 | |
| 276 | void |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 277 | PyFrame_FastToLocals(f) |
| 278 | PyFrameObject *f; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 279 | { |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 280 | /* Merge fast locals into f->f_locals */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 281 | PyObject *locals, *map; |
| 282 | PyObject **fast; |
| 283 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 284 | int j; |
| 285 | if (f == NULL) |
| 286 | return; |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 287 | locals = f->f_locals; |
| 288 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 289 | locals = f->f_locals = PyDict_New(); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 290 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 291 | PyErr_Clear(); /* Can't report it :-( */ |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | } |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 295 | if (f->f_nlocals == 0) |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 296 | return; |
| 297 | map = f->f_code->co_varnames; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 298 | if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 299 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 300 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 301 | fast = f->f_localsplus; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 302 | j = PyTuple_Size(map); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 303 | if (j > f->f_nlocals) |
| 304 | j = f->f_nlocals; |
| 305 | for (; --j >= 0; ) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 306 | PyObject *key = PyTuple_GetItem(map, j); |
| 307 | PyObject *value = fast[j]; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 308 | if (value == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 309 | PyErr_Clear(); |
| 310 | if (PyDict_DelItem(locals, key) != 0) |
| 311 | PyErr_Clear(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 312 | } |
| 313 | else { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 314 | if (PyDict_SetItem(locals, key, value) != 0) |
| 315 | PyErr_Clear(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 318 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 322 | PyFrame_LocalsToFast(f, clear) |
| 323 | PyFrameObject *f; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 324 | int clear; |
| 325 | { |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 326 | /* Merge f->f_locals into fast locals */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 327 | PyObject *locals, *map; |
| 328 | PyObject **fast; |
| 329 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 330 | int j; |
| 331 | if (f == NULL) |
| 332 | return; |
| 333 | locals = f->f_locals; |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 334 | map = f->f_code->co_varnames; |
Guido van Rossum | 3bb63a8 | 1997-01-20 04:29:16 +0000 | [diff] [blame] | 335 | if (locals == NULL || f->f_code->co_nlocals == 0) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 336 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 337 | if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 338 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 339 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 340 | fast = f->f_localsplus; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 341 | j = PyTuple_Size(map); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 342 | if (j > f->f_nlocals) |
| 343 | j = f->f_nlocals; |
| 344 | for (; --j >= 0; ) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 345 | PyObject *key = PyTuple_GetItem(map, j); |
| 346 | PyObject *value = PyDict_GetItem(locals, key); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 347 | if (value == NULL) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 348 | PyErr_Clear(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 349 | else |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 350 | Py_INCREF(value); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 351 | if (value != NULL || clear) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 352 | Py_XDECREF(fast[j]); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 353 | fast[j] = value; |
| 354 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 355 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 356 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 357 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 358 | |
| 359 | /* Clear out the free list */ |
| 360 | |
| 361 | void |
| 362 | PyFrame_Fini() |
| 363 | { |
| 364 | while (free_list != NULL) { |
| 365 | PyFrameObject *f = free_list; |
| 366 | free_list = free_list->f_back; |
| 367 | PyMem_DEL(f); |
| 368 | } |
| 369 | } |