Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 11 | /* Frame object implementation */ |
| 12 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 14 | |
| 15 | #include "compile.h" |
| 16 | #include "frameobject.h" |
| 17 | #include "opcode.h" |
| 18 | #include "structmember.h" |
| 19 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 20 | #define OFF(x) offsetof(PyFrameObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 21 | |
| 22 | static struct memberlist frame_memberlist[] = { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 23 | {"f_back", T_OBJECT, OFF(f_back), RO}, |
| 24 | {"f_code", T_OBJECT, OFF(f_code), RO}, |
Guido van Rossum | c113482 | 1995-01-10 10:39:16 +0000 | [diff] [blame] | 25 | {"f_builtins", T_OBJECT, OFF(f_builtins),RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 26 | {"f_globals", T_OBJECT, OFF(f_globals), RO}, |
| 27 | {"f_locals", T_OBJECT, OFF(f_locals), RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 28 | {"f_lasti", T_INT, OFF(f_lasti), RO}, |
| 29 | {"f_lineno", T_INT, OFF(f_lineno), RO}, |
Guido van Rossum | c113482 | 1995-01-10 10:39:16 +0000 | [diff] [blame] | 30 | {"f_restricted",T_INT, OFF(f_restricted),RO}, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 31 | {"f_trace", T_OBJECT, OFF(f_trace)}, |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 32 | {"f_exc_type", T_OBJECT, OFF(f_exc_type)}, |
| 33 | {"f_exc_value", T_OBJECT, OFF(f_exc_value)}, |
| 34 | {"f_exc_traceback", T_OBJECT, OFF(f_exc_traceback)}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | {NULL} /* Sentinel */ |
| 36 | }; |
| 37 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 38 | static PyObject * |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 39 | frame_getattr(PyFrameObject *f, char *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 41 | if (strcmp(name, "f_locals") == 0) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 42 | PyFrame_FastToLocals(f); |
| 43 | return PyMember_Get((char *)f, frame_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 46 | static int |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 47 | frame_setattr(PyFrameObject *f, char *name, PyObject *value) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 48 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 49 | return PyMember_Set((char *)f, frame_memberlist, name, value); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 52 | /* Stack frames are allocated and deallocated at a considerable rate. |
| 53 | In an attempt to improve the speed of function calls, we maintain a |
| 54 | separate free list of stack frames (just like integers are |
| 55 | allocated in a special way -- see intobject.c). When a stack frame |
| 56 | is on the free list, only the following members have a meaning: |
| 57 | ob_type == &Frametype |
| 58 | f_back next item on free list, or NULL |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 59 | f_nlocals number of locals |
| 60 | f_stacksize size of value stack |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 61 | Note that the value and block stacks are preserved -- this can save |
| 62 | another malloc() call or two (and two free() calls as well!). |
| 63 | Also note that, unlike for integers, each frame object is a |
| 64 | malloc'ed object in its own right -- it is only the actual calls to |
| 65 | malloc() that we are trying to save here, not the administration. |
| 66 | After all, while a typical program may make millions of calls, a |
| 67 | call depth of more than 20 or 30 is probably already exceptional |
| 68 | unless the program contains run-away recursion. I hope. |
| 69 | */ |
| 70 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 71 | static PyFrameObject *free_list = NULL; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 73 | static void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 74 | frame_dealloc(PyFrameObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | { |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 76 | int i; |
| 77 | PyObject **fastlocals; |
| 78 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 79 | Py_TRASHCAN_SAFE_BEGIN(f) |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 80 | /* Kill all local variables */ |
| 81 | fastlocals = f->f_localsplus; |
| 82 | for (i = f->f_nlocals; --i >= 0; ++fastlocals) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 83 | Py_XDECREF(*fastlocals); |
Guido van Rossum | 7582bfb | 1997-02-14 16:27:29 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 86 | Py_XDECREF(f->f_back); |
| 87 | Py_XDECREF(f->f_code); |
| 88 | Py_XDECREF(f->f_builtins); |
| 89 | Py_XDECREF(f->f_globals); |
| 90 | Py_XDECREF(f->f_locals); |
| 91 | Py_XDECREF(f->f_trace); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 92 | Py_XDECREF(f->f_exc_type); |
| 93 | Py_XDECREF(f->f_exc_value); |
| 94 | Py_XDECREF(f->f_exc_traceback); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 95 | f->f_back = free_list; |
| 96 | free_list = f; |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 97 | Py_TRASHCAN_SAFE_END(f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 100 | PyTypeObject PyFrame_Type = { |
| 101 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 102 | 0, |
| 103 | "frame", |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 104 | sizeof(PyFrameObject), |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 105 | 0, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 106 | (destructor)frame_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 107 | 0, /*tp_print*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 108 | (getattrfunc)frame_getattr, /*tp_getattr*/ |
| 109 | (setattrfunc)frame_setattr, /*tp_setattr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 110 | 0, /*tp_compare*/ |
| 111 | 0, /*tp_repr*/ |
| 112 | 0, /*tp_as_number*/ |
| 113 | 0, /*tp_as_sequence*/ |
| 114 | 0, /*tp_as_mapping*/ |
| 115 | }; |
| 116 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 117 | PyFrameObject * |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 118 | PyFrame_New(PyThreadState *tstate, PyCodeObject *code, |
| 119 | PyObject *globals, PyObject *locals) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 120 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 121 | PyFrameObject *back = tstate->frame; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 122 | static PyObject *builtin_object; |
| 123 | PyFrameObject *f; |
| 124 | PyObject *builtins; |
Guido van Rossum | 67906af | 1998-09-25 14:11:46 +0000 | [diff] [blame] | 125 | int extras; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 126 | |
Sjoerd Mullender | 5b7f3cd | 1995-04-04 11:47:41 +0000 | [diff] [blame] | 127 | if (builtin_object == NULL) { |
Guido van Rossum | b56933e | 1997-01-18 07:58:41 +0000 | [diff] [blame] | 128 | builtin_object = PyString_InternFromString("__builtins__"); |
Sjoerd Mullender | 5b7f3cd | 1995-04-04 11:47:41 +0000 | [diff] [blame] | 129 | if (builtin_object == NULL) |
| 130 | return NULL; |
| 131 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 132 | if ((back != NULL && !PyFrame_Check(back)) || |
| 133 | code == NULL || !PyCode_Check(code) || |
| 134 | globals == NULL || !PyDict_Check(globals) || |
| 135 | (locals != NULL && !PyDict_Check(locals))) { |
| 136 | PyErr_BadInternalCall(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
Guido van Rossum | 67906af | 1998-09-25 14:11:46 +0000 | [diff] [blame] | 139 | extras = code->co_stacksize + code->co_nlocals; |
Guido van Rossum | bde6ff7 | 1998-02-19 20:48:26 +0000 | [diff] [blame] | 140 | if (back == NULL || back->f_globals != globals) { |
| 141 | builtins = PyDict_GetItem(globals, builtin_object); |
| 142 | if (builtins != NULL && PyModule_Check(builtins)) |
| 143 | builtins = PyModule_GetDict(builtins); |
| 144 | } |
| 145 | else { |
| 146 | /* If we share the globals, we share the builtins. |
| 147 | Save a lookup and a call. */ |
| 148 | builtins = back->f_builtins; |
| 149 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 150 | if (builtins != NULL && !PyDict_Check(builtins)) |
| 151 | builtins = NULL; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 152 | if (free_list == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 153 | /* PyObject_New is inlined */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 154 | f = (PyFrameObject *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 155 | PyObject_MALLOC(sizeof(PyFrameObject) + |
| 156 | extras*sizeof(PyObject *)); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 157 | if (f == NULL) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 158 | return (PyFrameObject *)PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 159 | PyObject_INIT(f, &PyFrame_Type); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 160 | } |
| 161 | else { |
| 162 | f = free_list; |
| 163 | free_list = free_list->f_back; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 164 | if (f->f_nlocals + f->f_stacksize < extras) { |
Guido van Rossum | 6345ac6 | 1997-10-31 20:32:13 +0000 | [diff] [blame] | 165 | f = (PyFrameObject *) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 166 | PyObject_REALLOC(f, sizeof(PyFrameObject) + |
| 167 | extras*sizeof(PyObject *)); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 168 | if (f == NULL) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 169 | return (PyFrameObject *)PyErr_NoMemory(); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 170 | } |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 171 | else |
| 172 | extras = f->f_nlocals + f->f_stacksize; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 173 | PyObject_INIT(f, &PyFrame_Type); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 174 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 175 | if (builtins == NULL) { |
Guido van Rossum | bde6ff7 | 1998-02-19 20:48:26 +0000 | [diff] [blame] | 176 | /* No builtins! Make up a minimal one. */ |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 177 | builtins = PyDict_New(); |
Guido van Rossum | f61618c | 1998-10-19 14:20:20 +0000 | [diff] [blame] | 178 | if (builtins == NULL || /* Give them 'None', at least. */ |
| 179 | PyDict_SetItemString(builtins, "None", Py_None) < 0) { |
| 180 | Py_DECREF(f); |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 181 | return NULL; |
Guido van Rossum | f61618c | 1998-10-19 14:20:20 +0000 | [diff] [blame] | 182 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | Py_XINCREF(builtins); |
| 186 | f->f_builtins = builtins; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 187 | Py_XINCREF(back); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 188 | f->f_back = back; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 189 | Py_INCREF(code); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 190 | f->f_code = code; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 191 | Py_INCREF(globals); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 192 | f->f_globals = globals; |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 193 | if (code->co_flags & CO_NEWLOCALS) { |
| 194 | if (code->co_flags & CO_OPTIMIZED) |
| 195 | locals = NULL; /* Let fast_2_locals handle it */ |
| 196 | else { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 197 | locals = PyDict_New(); |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 198 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 199 | Py_DECREF(f); |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 200 | return NULL; |
| 201 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 202 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 203 | } |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 204 | else { |
| 205 | if (locals == NULL) |
| 206 | locals = globals; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 207 | Py_INCREF(locals); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 208 | } |
| 209 | f->f_locals = locals; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 210 | f->f_trace = NULL; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 211 | 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] | 212 | f->f_tstate = tstate; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 214 | f->f_lasti = 0; |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 215 | f->f_lineno = code->co_firstlineno; |
Guido van Rossum | eb46d67 | 1997-08-02 02:59:08 +0000 | [diff] [blame] | 216 | f->f_restricted = (builtins != tstate->interp->builtins); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 217 | f->f_iblock = 0; |
| 218 | f->f_nlocals = code->co_nlocals; |
Guido van Rossum | 747596a | 1997-01-24 04:00:21 +0000 | [diff] [blame] | 219 | f->f_stacksize = extras - code->co_nlocals; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 220 | |
| 221 | while (--extras >= 0) |
| 222 | f->f_localsplus[extras] = NULL; |
| 223 | |
| 224 | f->f_valuestack = f->f_localsplus + f->f_nlocals; |
| 225 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 226 | return f; |
| 227 | } |
| 228 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 229 | /* Block management */ |
| 230 | |
| 231 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 232 | PyFrame_BlockSetup(PyFrameObject *f, int type, int handler, int level) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 233 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 234 | PyTryBlock *b; |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 235 | if (f->f_iblock >= CO_MAXBLOCKS) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 236 | Py_FatalError("XXX block stack overflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 237 | b = &f->f_blockstack[f->f_iblock++]; |
| 238 | b->b_type = type; |
| 239 | b->b_level = level; |
| 240 | b->b_handler = handler; |
| 241 | } |
| 242 | |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 243 | PyTryBlock * |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 244 | PyFrame_BlockPop(PyFrameObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 245 | { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 246 | PyTryBlock *b; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 247 | if (f->f_iblock <= 0) |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 248 | Py_FatalError("XXX block stack underflow"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 249 | b = &f->f_blockstack[--f->f_iblock]; |
| 250 | return b; |
| 251 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 252 | |
| 253 | /* Convert between "fast" version of locals and dictionary version */ |
| 254 | |
| 255 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 256 | PyFrame_FastToLocals(PyFrameObject *f) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 257 | { |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 258 | /* Merge fast locals into f->f_locals */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 259 | PyObject *locals, *map; |
| 260 | PyObject **fast; |
| 261 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 262 | int j; |
| 263 | if (f == NULL) |
| 264 | return; |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 265 | locals = f->f_locals; |
| 266 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 267 | locals = f->f_locals = PyDict_New(); |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 268 | if (locals == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 269 | PyErr_Clear(); /* Can't report it :-( */ |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | } |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 273 | if (f->f_nlocals == 0) |
Guido van Rossum | bdd207a | 1995-07-26 16:14:30 +0000 | [diff] [blame] | 274 | return; |
| 275 | map = f->f_code->co_varnames; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 276 | if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 277 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 278 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 279 | fast = f->f_localsplus; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 280 | j = PyTuple_Size(map); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 281 | if (j > f->f_nlocals) |
| 282 | j = f->f_nlocals; |
| 283 | for (; --j >= 0; ) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 284 | PyObject *key = PyTuple_GetItem(map, j); |
| 285 | PyObject *value = fast[j]; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 286 | if (value == NULL) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 287 | PyErr_Clear(); |
| 288 | if (PyDict_DelItem(locals, key) != 0) |
| 289 | PyErr_Clear(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 290 | } |
| 291 | else { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 292 | if (PyDict_SetItem(locals, key, value) != 0) |
| 293 | PyErr_Clear(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 296 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 300 | PyFrame_LocalsToFast(PyFrameObject *f, int clear) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 301 | { |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 302 | /* Merge f->f_locals into fast locals */ |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 303 | PyObject *locals, *map; |
| 304 | PyObject **fast; |
| 305 | PyObject *error_type, *error_value, *error_traceback; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 306 | int j; |
| 307 | if (f == NULL) |
| 308 | return; |
| 309 | locals = f->f_locals; |
Guido van Rossum | 2271bf7 | 1995-07-18 14:30:34 +0000 | [diff] [blame] | 310 | map = f->f_code->co_varnames; |
Guido van Rossum | 3bb63a8 | 1997-01-20 04:29:16 +0000 | [diff] [blame] | 311 | if (locals == NULL || f->f_code->co_nlocals == 0) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 312 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 313 | if (!PyDict_Check(locals) || !PyTuple_Check(map)) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 314 | return; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 315 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 316 | fast = f->f_localsplus; |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 317 | j = PyTuple_Size(map); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 318 | if (j > f->f_nlocals) |
| 319 | j = f->f_nlocals; |
| 320 | for (; --j >= 0; ) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 321 | PyObject *key = PyTuple_GetItem(map, j); |
| 322 | PyObject *value = PyDict_GetItem(locals, key); |
Guido van Rossum | e23eb57 | 1998-05-14 01:49:48 +0000 | [diff] [blame] | 323 | Py_XINCREF(value); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 324 | if (value != NULL || clear) { |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 325 | Py_XDECREF(fast[j]); |
Guido van Rossum | f3e85a0 | 1997-01-20 04:20:52 +0000 | [diff] [blame] | 326 | fast[j] = value; |
| 327 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 328 | } |
Guido van Rossum | 1875247 | 1997-04-29 14:49:28 +0000 | [diff] [blame] | 329 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 330 | } |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 331 | |
| 332 | /* Clear out the free list */ |
| 333 | |
| 334 | void |
Fred Drake | 1b190b4 | 2000-07-09 05:40:56 +0000 | [diff] [blame] | 335 | PyFrame_Fini(void) |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 336 | { |
| 337 | while (free_list != NULL) { |
| 338 | PyFrameObject *f = free_list; |
| 339 | free_list = free_list->f_back; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 340 | PyObject_DEL(f); |
Guido van Rossum | 404b95d | 1997-08-05 02:09:46 +0000 | [diff] [blame] | 341 | } |
| 342 | } |