Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Frame object implementation */ |
| 3 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
| 6 | #include "compile.h" |
| 7 | #include "frameobject.h" |
| 8 | #include "opcode.h" |
| 9 | #include "structmember.h" |
| 10 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 11 | #define OFF(x) offsetof(PyFrameObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 12 | |
| 13 | static struct memberlist frame_memberlist[] = { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 14 | {"f_back", T_OBJECT, OFF(f_back), RO}, |
| 15 | {"f_code", T_OBJECT, OFF(f_code), RO}, |
Guido van Rossum | c113482 | 1995-01-10 10:39:16 +0000 | [diff] [blame] | 16 | {"f_builtins", T_OBJECT, OFF(f_builtins),RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 17 | {"f_globals", T_OBJECT, OFF(f_globals), RO}, |
| 18 | {"f_locals", T_OBJECT, OFF(f_locals), RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 19 | {"f_lasti", T_INT, OFF(f_lasti), RO}, |
| 20 | {"f_lineno", T_INT, OFF(f_lineno), RO}, |
Guido van Rossum | c113482 | 1995-01-10 10:39:16 +0000 | [diff] [blame] | 21 | {"f_restricted",T_INT, OFF(f_restricted),RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 22 | {"f_trace", T_OBJECT, OFF(f_trace)}, |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 23 | {"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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 26 | {NULL} /* Sentinel */ |
| 27 | }; |
| 28 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 29 | static PyObject * |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 30 | frame_getattr(PyFrameObject *f, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 31 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 32 | if (strcmp(name, "f_locals") == 0) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 33 | PyFrame_FastToLocals(f); |
| 34 | return PyMember_Get((char *)f, frame_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 37 | static int |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 38 | frame_setattr(PyFrameObject *f, char *name, PyObject *value) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 39 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 40 | return PyMember_Set((char *)f, frame_memberlist, name, value); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 43 | /* 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 Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 50 | f_nlocals number of locals |
| 51 | f_stacksize size of value stack |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 52 | 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 Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 62 | static PyFrameObject *free_list = NULL; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 64 | static void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 65 | frame_dealloc(PyFrameObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 66 | { |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 67 | int i; |
| 68 | PyObject **fastlocals; |
| 69 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 70 | Py_TRASHCAN_SAFE_BEGIN(f) |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 71 | /* Kill all local variables */ |
| 72 | fastlocals = f->f_localsplus; |
| 73 | for (i = f->f_nlocals; --i >= 0; ++fastlocals) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 74 | Py_XDECREF(*fastlocals); |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 77 | 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 Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 83 | Py_XDECREF(f->f_exc_type); |
| 84 | Py_XDECREF(f->f_exc_value); |
| 85 | Py_XDECREF(f->f_exc_traceback); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 86 | f->f_back = free_list; |
| 87 | free_list = f; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 88 | Py_TRASHCAN_SAFE_END(f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 91 | PyTypeObject PyFrame_Type = { |
| 92 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 93 | 0, |
| 94 | "frame", |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 95 | sizeof(PyFrameObject), |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 96 | 0, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 97 | (destructor)frame_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 98 | 0, /*tp_print*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 99 | (getattrfunc)frame_getattr, /*tp_getattr*/ |
| 100 | (setattrfunc)frame_setattr, /*tp_setattr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 101 | 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 Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 108 | PyFrameObject * |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 109 | PyFrame_New(PyThreadState *tstate, PyCodeObject *code, |
| 110 | PyObject *globals, PyObject *locals) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 111 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 112 | PyFrameObject *back = tstate->frame; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 113 | static PyObject *builtin_object; |
| 114 | PyFrameObject *f; |
| 115 | PyObject *builtins; |
Guido van Rossum | 67906af | 1998-09-25 14:11:46 +0000 | [diff] [blame] | 116 | int extras; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 117 | |
Sjoerd Mullender | 5b7f3cd | 1995-04-04 11:47:41 +0000 | [diff] [blame] | 118 | if (builtin_object == NULL) { |
Guido van Rossum | b56933e | 1997-01-18 07:58:41 +0000 | [diff] [blame] | 119 | builtin_object = PyString_InternFromString("__builtins__"); |
Sjoerd Mullender | 5b7f3cd | 1995-04-04 11:47:41 +0000 | [diff] [blame] | 120 | if (builtin_object == NULL) |
| 121 | return NULL; |
| 122 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 123 | 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 128 | return NULL; |
| 129 | } |
Guido van Rossum | 67906af | 1998-09-25 14:11:46 +0000 | [diff] [blame] | 130 | extras = code->co_stacksize + code->co_nlocals; |
Guido van Rossum | bde6ff7 | 1998-02-19 20:48:26 +0000 | [diff] [blame] | 131 | 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 Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 141 | if (builtins != NULL && !PyDict_Check(builtins)) |
| 142 | builtins = NULL; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 143 | if (free_list == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 144 | /* PyObject_New is inlined */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 145 | f = (PyFrameObject *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 146 | PyObject_MALLOC(sizeof(PyFrameObject) + |
| 147 | extras*sizeof(PyObject *)); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 148 | if (f == NULL) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 149 | return (PyFrameObject *)PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 150 | PyObject_INIT(f, &PyFrame_Type); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 151 | } |
| 152 | else { |
| 153 | f = free_list; |
| 154 | free_list = free_list->f_back; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 155 | if (f->f_nlocals + f->f_stacksize < extras) { |
Guido van Rossum | 6345ac6 | 1997-10-31 20:32:13 +0000 | [diff] [blame] | 156 | f = (PyFrameObject *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 157 | PyObject_REALLOC(f, sizeof(PyFrameObject) + |
| 158 | extras*sizeof(PyObject *)); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 159 | if (f == NULL) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 160 | return (PyFrameObject *)PyErr_NoMemory(); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 161 | } |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 162 | else |
| 163 | extras = f->f_nlocals + f->f_stacksize; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 164 | PyObject_INIT(f, &PyFrame_Type); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 165 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 166 | if (builtins == NULL) { |
Guido van Rossum | bde6ff7 | 1998-02-19 20:48:26 +0000 | [diff] [blame] | 167 | /* No builtins! Make up a minimal one. */ |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 168 | builtins = PyDict_New(); |
Guido van Rossum | f61618c | 1998-10-19 14:20:20 +0000 | [diff] [blame] | 169 | if (builtins == NULL || /* Give them 'None', at least. */ |
| 170 | PyDict_SetItemString(builtins, "None", Py_None) < 0) { |
| 171 | Py_DECREF(f); |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 172 | return NULL; |
Guido van Rossum | f61618c | 1998-10-19 14:20:20 +0000 | [diff] [blame] | 173 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 174 | } |
| 175 | else |
| 176 | Py_XINCREF(builtins); |
| 177 | f->f_builtins = builtins; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 178 | Py_XINCREF(back); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 179 | f->f_back = back; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 180 | Py_INCREF(code); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 181 | f->f_code = code; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 182 | Py_INCREF(globals); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 183 | f->f_globals = globals; |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 184 | 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 Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 188 | locals = PyDict_New(); |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 189 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 190 | Py_DECREF(f); |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 193 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 194 | } |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 195 | else { |
| 196 | if (locals == NULL) |
| 197 | locals = globals; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 198 | Py_INCREF(locals); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 199 | } |
| 200 | f->f_locals = locals; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 201 | f->f_trace = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 202 | 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] | 203 | f->f_tstate = tstate; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 205 | f->f_lasti = 0; |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 206 | f->f_lineno = code->co_firstlineno; |
Guido van Rossum | eb46d67 | 1997-08-02 02:59:08 +0000 | [diff] [blame] | 207 | f->f_restricted = (builtins != tstate->interp->builtins); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 208 | f->f_iblock = 0; |
| 209 | f->f_nlocals = code->co_nlocals; |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 210 | f->f_stacksize = extras - code->co_nlocals; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 211 | |
| 212 | while (--extras >= 0) |
| 213 | f->f_localsplus[extras] = NULL; |
| 214 | |
| 215 | f->f_valuestack = f->f_localsplus + f->f_nlocals; |
| 216 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 217 | return f; |
| 218 | } |
| 219 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 220 | /* Block management */ |
| 221 | |
| 222 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 223 | PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 224 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 225 | PyTryBlock *b; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 226 | if (f->f_iblock >= CO_MAXBLOCKS) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 227 | Py_FatalError("XXX block stack overflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 228 | 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 Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 234 | PyTryBlock * |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 235 | PyFrame_BlockPop(PyFrameObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 236 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 237 | PyTryBlock *b; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 238 | if (f->f_iblock <= 0) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 239 | Py_FatalError("XXX block stack underflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 240 | b = &f->f_blockstack[--f->f_iblock]; |
| 241 | return b; |
| 242 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 243 | |
| 244 | /* Convert between "fast" version of locals and dictionary version */ |
| 245 | |
| 246 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 247 | PyFrame_FastToLocals(PyFrameObject *f) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 248 | { |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 249 | /* Merge fast locals into f->f_locals */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 250 | PyObject *locals, *map; |
| 251 | PyObject **fast; |
| 252 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 253 | int j; |
| 254 | if (f == NULL) |
| 255 | return; |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 256 | locals = f->f_locals; |
| 257 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 258 | locals = f->f_locals = PyDict_New(); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 259 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 260 | PyErr_Clear(); /* Can't report it :-( */ |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 261 | return; |
| 262 | } |
| 263 | } |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 264 | if (f->f_nlocals == 0) |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 265 | return; |
| 266 | map = f->f_code->co_varnames; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 267 | if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 268 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 269 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 270 | fast = f->f_localsplus; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 271 | j = PyTuple_Size(map); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 272 | if (j > f->f_nlocals) |
| 273 | j = f->f_nlocals; |
| 274 | for (; --j >= 0; ) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 275 | PyObject *key = PyTuple_GetItem(map, j); |
| 276 | PyObject *value = fast[j]; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 277 | if (value == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 278 | PyErr_Clear(); |
| 279 | if (PyDict_DelItem(locals, key) != 0) |
| 280 | PyErr_Clear(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 281 | } |
| 282 | else { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 283 | if (PyDict_SetItem(locals, key, value) != 0) |
| 284 | PyErr_Clear(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 287 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 291 | PyFrame_LocalsToFast(PyFrameObject *f, int clear) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 292 | { |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 293 | /* Merge f->f_locals into fast locals */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 294 | PyObject *locals, *map; |
| 295 | PyObject **fast; |
| 296 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 297 | int j; |
| 298 | if (f == NULL) |
| 299 | return; |
| 300 | locals = f->f_locals; |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 301 | map = f->f_code->co_varnames; |
Guido van Rossum | 3bb63a8 | 1997-01-20 04:29:16 +0000 | [diff] [blame] | 302 | if (locals == NULL || f->f_code->co_nlocals == 0) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 303 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 304 | if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 305 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 306 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 307 | fast = f->f_localsplus; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 308 | j = PyTuple_Size(map); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 309 | if (j > f->f_nlocals) |
| 310 | j = f->f_nlocals; |
| 311 | for (; --j >= 0; ) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 312 | PyObject *key = PyTuple_GetItem(map, j); |
| 313 | PyObject *value = PyDict_GetItem(locals, key); |
Guido van Rossum | e23eb57 | 1998-05-14 01:49:48 +0000 | [diff] [blame] | 314 | Py_XINCREF(value); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 315 | if (value != NULL || clear) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 316 | Py_XDECREF(fast[j]); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 317 | fast[j] = value; |
| 318 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 319 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 320 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 321 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 322 | |
| 323 | /* Clear out the free list */ |
| 324 | |
| 325 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 326 | PyFrame_Fini(void) |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 327 | { |
| 328 | while (free_list != NULL) { |
| 329 | PyFrameObject *f = free_list; |
| 330 | free_list = free_list->f_back; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 331 | PyObject_DEL(f); |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 332 | } |
| 333 | } |