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 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4 | /* XXX TO DO: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 5 | XXX speed up searching for keywords by using a dictionary |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 6 | XXX document it! |
| 7 | */ |
| 8 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 9 | #include "Python.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 11 | #include "compile.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 12 | #include "frameobject.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 13 | #include "eval.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 14 | #include "opcode.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 15 | #include "structmember.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 16 | |
Jack Jansen | cbf630f | 2000-07-11 21:59:16 +0000 | [diff] [blame] | 17 | #ifdef macintosh |
| 18 | #include "macglue.h" |
| 19 | #endif |
| 20 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 23 | /* Turn this on if your compiler chokes on the big switch: */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 24 | /* #define CASE_TOO_BIG 1 */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 26 | #ifdef Py_DEBUG |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 27 | /* For debugging the interpreter: */ |
| 28 | #define LLTRACE 1 /* Low-level trace feature */ |
| 29 | #define CHECKEXC 1 /* Double-check exception checking */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 32 | typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 34 | /* Forward declarations */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 35 | static PyObject *eval_frame(PyFrameObject *); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 36 | static PyObject *fast_function(PyObject *, PyObject ***, int, int, int); |
| 37 | static PyObject *fast_cfunction(PyObject *, PyObject ***, int); |
| 38 | static PyObject *do_call(PyObject *, PyObject ***, int, int); |
| 39 | static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 40 | static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *); |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 41 | static PyObject *update_star_args(int, int, PyObject *, PyObject ***); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 42 | static PyObject *load_args(PyObject ***, int); |
| 43 | #define CALL_FLAG_VAR 1 |
| 44 | #define CALL_FLAG_KW 2 |
| 45 | |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 46 | #ifdef LLTRACE |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 47 | static int prtrace(PyObject *, char *); |
Guido van Rossum | 0a066c0 | 1992-03-27 17:29:15 +0000 | [diff] [blame] | 48 | #endif |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 49 | static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *, |
| 50 | int, PyObject *); |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 51 | static void call_trace_protected(Py_tracefunc, PyObject *, |
| 52 | PyFrameObject *, int); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 53 | static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 54 | static PyObject *loop_subscript(PyObject *, PyObject *); |
| 55 | static PyObject *apply_slice(PyObject *, PyObject *, PyObject *); |
| 56 | static int assign_slice(PyObject *, PyObject *, |
| 57 | PyObject *, PyObject *); |
| 58 | static PyObject *cmp_outcome(int, PyObject *, PyObject *); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 59 | static PyObject *import_from(PyObject *, PyObject *); |
| 60 | static int import_all_from(PyObject *, PyObject *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 61 | static PyObject *build_class(PyObject *, PyObject *, PyObject *); |
| 62 | static int exec_statement(PyFrameObject *, |
| 63 | PyObject *, PyObject *, PyObject *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 64 | static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *); |
| 65 | static void reset_exc_info(PyThreadState *); |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 66 | static void format_exc_check_arg(PyObject *, char *, PyObject *); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 67 | |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 68 | #define NAME_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 69 | "name '%.200s' is not defined" |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 70 | #define GLOBAL_NAME_ERROR_MSG \ |
| 71 | "global name '%.200s' is not defined" |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 72 | #define UNBOUNDLOCAL_ERROR_MSG \ |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 73 | "local variable '%.200s' referenced before assignment" |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 74 | #define UNBOUNDFREE_ERROR_MSG \ |
| 75 | "free variable '%.200s' referenced before assignment" \ |
| 76 | " in enclosing scope" |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 78 | /* Dynamic execution profile */ |
| 79 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 80 | #ifdef DXPAIRS |
| 81 | static long dxpairs[257][256]; |
| 82 | #define dxp dxpairs[256] |
| 83 | #else |
| 84 | static long dxp[256]; |
| 85 | #endif |
| 86 | #endif |
| 87 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 88 | staticforward PyTypeObject gentype; |
| 89 | |
| 90 | typedef struct { |
| 91 | PyObject_HEAD |
Tim Peters | d8e1c9e | 2001-06-26 20:58:58 +0000 | [diff] [blame] | 92 | /* The gi_ prefix is intended to remind of generator-iterator. */ |
| 93 | |
| 94 | PyFrameObject *gi_frame; |
| 95 | |
Tim Peters | e77f2e2 | 2001-06-26 22:24:51 +0000 | [diff] [blame] | 96 | /* True if generator is being executed. */ |
| 97 | int gi_running; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 98 | } genobject; |
| 99 | |
| 100 | static PyObject * |
| 101 | gen_new(PyFrameObject *f) |
| 102 | { |
| 103 | genobject *gen = PyObject_New(genobject, &gentype); |
| 104 | if (gen == NULL) { |
| 105 | Py_DECREF(f); |
| 106 | return NULL; |
| 107 | } |
Tim Peters | d8e1c9e | 2001-06-26 20:58:58 +0000 | [diff] [blame] | 108 | gen->gi_frame = f; |
| 109 | gen->gi_running = 0; |
Neil Schemenauer | f8c7c20 | 2001-07-12 13:27:49 +0000 | [diff] [blame] | 110 | PyObject_GC_Init(gen); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 111 | return (PyObject *)gen; |
| 112 | } |
| 113 | |
Neil Schemenauer | f8c7c20 | 2001-07-12 13:27:49 +0000 | [diff] [blame] | 114 | static int |
| 115 | gen_traverse(genobject *gen, visitproc visit, void *arg) |
| 116 | { |
| 117 | return visit((PyObject *)gen->gi_frame, arg); |
| 118 | } |
| 119 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 120 | static void |
| 121 | gen_dealloc(genobject *gen) |
| 122 | { |
Neil Schemenauer | f8c7c20 | 2001-07-12 13:27:49 +0000 | [diff] [blame] | 123 | PyObject_GC_Fini(gen); |
Tim Peters | d8e1c9e | 2001-06-26 20:58:58 +0000 | [diff] [blame] | 124 | Py_DECREF(gen->gi_frame); |
Neil Schemenauer | f8c7c20 | 2001-07-12 13:27:49 +0000 | [diff] [blame] | 125 | PyObject_Del(gen); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static PyObject * |
| 129 | gen_iternext(genobject *gen) |
| 130 | { |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 131 | PyThreadState *tstate = PyThreadState_GET(); |
Tim Peters | d8e1c9e | 2001-06-26 20:58:58 +0000 | [diff] [blame] | 132 | PyFrameObject *f = gen->gi_frame; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 133 | PyObject *result; |
| 134 | |
Tim Peters | d8e1c9e | 2001-06-26 20:58:58 +0000 | [diff] [blame] | 135 | if (gen->gi_running) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 136 | PyErr_SetString(PyExc_ValueError, |
| 137 | "generator already executing"); |
| 138 | return NULL; |
| 139 | } |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 140 | if (f->f_stacktop == NULL) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 141 | return NULL; |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 142 | |
| 143 | /* Generators always return to their most recent caller, not |
| 144 | * necessarily their creator. */ |
Tim Peters | 5eb4b87 | 2001-06-23 05:47:56 +0000 | [diff] [blame] | 145 | Py_XINCREF(tstate->frame); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 146 | assert(f->f_back == NULL); |
| 147 | f->f_back = tstate->frame; |
| 148 | |
Tim Peters | d8e1c9e | 2001-06-26 20:58:58 +0000 | [diff] [blame] | 149 | gen->gi_running = 1; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 150 | result = eval_frame(f); |
Tim Peters | d8e1c9e | 2001-06-26 20:58:58 +0000 | [diff] [blame] | 151 | gen->gi_running = 0; |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 152 | |
| 153 | /* Don't keep the reference to f_back any longer than necessary. It |
| 154 | * may keep a chain of frames alive or it could create a reference |
| 155 | * cycle. */ |
Tim Peters | 5eb4b87 | 2001-06-23 05:47:56 +0000 | [diff] [blame] | 156 | Py_XDECREF(f->f_back); |
Tim Peters | 6302ec6 | 2001-06-20 06:57:32 +0000 | [diff] [blame] | 157 | f->f_back = NULL; |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 158 | |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 159 | /* If the generator just returned (as opposed to yielding), signal |
| 160 | * that the generator is exhausted. */ |
| 161 | if (result == Py_None && f->f_stacktop == NULL) { |
| 162 | Py_DECREF(result); |
| 163 | result = NULL; |
| 164 | } |
| 165 | |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 166 | return result; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 170 | gen_next(genobject *gen) |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 171 | { |
| 172 | PyObject *result; |
| 173 | |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 174 | result = gen_iternext(gen); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 175 | |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 176 | if (result == NULL && !PyErr_Occurred()) { |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 177 | PyErr_SetObject(PyExc_StopIteration, Py_None); |
| 178 | return NULL; |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 179 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 180 | |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | static PyObject * |
| 185 | gen_getiter(PyObject *gen) |
| 186 | { |
| 187 | Py_INCREF(gen); |
| 188 | return gen; |
| 189 | } |
| 190 | |
| 191 | static struct PyMethodDef gen_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 192 | {"next", (PyCFunction)gen_next, METH_NOARGS, |
Tim Peters | e77f2e2 | 2001-06-26 22:24:51 +0000 | [diff] [blame] | 193 | "next() -- get the next value, or raise StopIteration"}, |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 194 | {NULL, NULL} /* Sentinel */ |
| 195 | }; |
| 196 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 197 | static PyMemberDef gen_memberlist[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 198 | {"gi_frame", T_OBJECT, offsetof(genobject, gi_frame), RO}, |
| 199 | {"gi_running", T_INT, offsetof(genobject, gi_running), RO}, |
| 200 | {NULL} /* Sentinel */ |
| 201 | }; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 202 | |
| 203 | statichere PyTypeObject gentype = { |
| 204 | PyObject_HEAD_INIT(&PyType_Type) |
| 205 | 0, /* ob_size */ |
| 206 | "generator", /* tp_name */ |
Neil Schemenauer | f8c7c20 | 2001-07-12 13:27:49 +0000 | [diff] [blame] | 207 | sizeof(genobject) + PyGC_HEAD_SIZE, /* tp_basicsize */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 208 | 0, /* tp_itemsize */ |
| 209 | /* methods */ |
| 210 | (destructor)gen_dealloc, /* tp_dealloc */ |
| 211 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 212 | 0, /* tp_getattr */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 213 | 0, /* tp_setattr */ |
| 214 | 0, /* tp_compare */ |
| 215 | 0, /* tp_repr */ |
| 216 | 0, /* tp_as_number */ |
| 217 | 0, /* tp_as_sequence */ |
| 218 | 0, /* tp_as_mapping */ |
| 219 | 0, /* tp_hash */ |
| 220 | 0, /* tp_call */ |
| 221 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 222 | PyObject_GenericGetAttr, /* tp_getattro */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 223 | 0, /* tp_setattro */ |
| 224 | 0, /* tp_as_buffer */ |
Neil Schemenauer | f8c7c20 | 2001-07-12 13:27:49 +0000 | [diff] [blame] | 225 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 226 | 0, /* tp_doc */ |
Neil Schemenauer | f8c7c20 | 2001-07-12 13:27:49 +0000 | [diff] [blame] | 227 | (traverseproc)gen_traverse, /* tp_traverse */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 228 | 0, /* tp_clear */ |
| 229 | 0, /* tp_richcompare */ |
| 230 | 0, /* tp_weaklistoffset */ |
| 231 | (getiterfunc)gen_getiter, /* tp_iter */ |
| 232 | (iternextfunc)gen_iternext, /* tp_iternext */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 233 | gen_methods, /* tp_methods */ |
| 234 | gen_memberlist, /* tp_members */ |
| 235 | 0, /* tp_getset */ |
| 236 | 0, /* tp_base */ |
| 237 | 0, /* tp_dict */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 241 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 243 | #ifndef DONT_HAVE_ERRNO_H |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 244 | #include <errno.h> |
Guido van Rossum | 2571cc8 | 1999-04-07 16:07:23 +0000 | [diff] [blame] | 245 | #endif |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 246 | #include "pythread.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 248 | extern int _PyThread_Started; /* Flag for Py_Exit */ |
| 249 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 250 | static PyThread_type_lock interpreter_lock = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 251 | static long main_thread = 0; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 252 | |
| 253 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 254 | PyEval_InitThreads(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 255 | { |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 256 | if (interpreter_lock) |
Sjoerd Mullender | ed59d20 | 1993-01-06 13:36:38 +0000 | [diff] [blame] | 257 | return; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 258 | _PyThread_Started = 1; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 259 | interpreter_lock = PyThread_allocate_lock(); |
| 260 | PyThread_acquire_lock(interpreter_lock, 1); |
| 261 | main_thread = PyThread_get_thread_ident(); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 262 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 263 | |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 264 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 265 | PyEval_AcquireLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 266 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 267 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 271 | PyEval_ReleaseLock(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 272 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 273 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 277 | PyEval_AcquireThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 278 | { |
| 279 | if (tstate == NULL) |
| 280 | Py_FatalError("PyEval_AcquireThread: NULL new thread state"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 281 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 282 | if (PyThreadState_Swap(tstate) != NULL) |
| 283 | Py_FatalError( |
| 284 | "PyEval_AcquireThread: non-NULL old thread state"); |
| 285 | } |
| 286 | |
| 287 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 288 | PyEval_ReleaseThread(PyThreadState *tstate) |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 289 | { |
| 290 | if (tstate == NULL) |
| 291 | Py_FatalError("PyEval_ReleaseThread: NULL thread state"); |
| 292 | if (PyThreadState_Swap(NULL) != tstate) |
| 293 | Py_FatalError("PyEval_ReleaseThread: wrong thread state"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 294 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 9cc8a20 | 1997-07-19 19:55:50 +0000 | [diff] [blame] | 295 | } |
Guido van Rossum | fee3a2d | 2000-08-27 17:34:07 +0000 | [diff] [blame] | 296 | |
| 297 | /* This function is called from PyOS_AfterFork to ensure that newly |
| 298 | created child processes don't hold locks referring to threads which |
| 299 | are not running in the child process. (This could also be done using |
| 300 | pthread_atfork mechanism, at least for the pthreads implementation.) */ |
| 301 | |
| 302 | void |
| 303 | PyEval_ReInitThreads(void) |
| 304 | { |
| 305 | if (!interpreter_lock) |
| 306 | return; |
| 307 | /*XXX Can't use PyThread_free_lock here because it does too |
| 308 | much error-checking. Doing this cleanly would require |
| 309 | adding a new function to each thread_*.h. Instead, just |
| 310 | create a new lock and waste a little bit of memory */ |
| 311 | interpreter_lock = PyThread_allocate_lock(); |
| 312 | PyThread_acquire_lock(interpreter_lock, 1); |
| 313 | main_thread = PyThread_get_thread_ident(); |
| 314 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 315 | #endif |
| 316 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 317 | /* Functions save_thread and restore_thread are always defined so |
| 318 | dynamically loaded modules needn't be compiled separately for use |
| 319 | with and without threads: */ |
| 320 | |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 321 | PyThreadState * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 322 | PyEval_SaveThread(void) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 323 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 324 | PyThreadState *tstate = PyThreadState_Swap(NULL); |
| 325 | if (tstate == NULL) |
| 326 | Py_FatalError("PyEval_SaveThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 327 | #ifdef WITH_THREAD |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 328 | if (interpreter_lock) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 329 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 330 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 331 | return tstate; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 335 | PyEval_RestoreThread(PyThreadState *tstate) |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 336 | { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 337 | if (tstate == NULL) |
| 338 | Py_FatalError("PyEval_RestoreThread: NULL tstate"); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 339 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 340 | if (interpreter_lock) { |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 341 | int err = errno; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 342 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 343 | errno = err; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 344 | } |
| 345 | #endif |
Guido van Rossum | b74eca9 | 1997-09-30 22:03:16 +0000 | [diff] [blame] | 346 | PyThreadState_Swap(tstate); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 350 | /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX |
| 351 | signal handlers or Mac I/O completion routines) can schedule calls |
| 352 | to a function to be called synchronously. |
| 353 | The synchronous function is called with one void* argument. |
| 354 | It should return 0 for success or -1 for failure -- failure should |
| 355 | be accompanied by an exception. |
| 356 | |
| 357 | If registry succeeds, the registry function returns 0; if it fails |
| 358 | (e.g. due to too many pending calls) it returns -1 (without setting |
| 359 | an exception condition). |
| 360 | |
| 361 | Note that because registry may occur from within signal handlers, |
| 362 | or other asynchronous events, calling malloc() is unsafe! |
| 363 | |
| 364 | #ifdef WITH_THREAD |
| 365 | Any thread can schedule pending calls, but only the main thread |
| 366 | will execute them. |
| 367 | #endif |
| 368 | |
| 369 | XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE! |
| 370 | There are two possible race conditions: |
| 371 | (1) nested asynchronous registry calls; |
| 372 | (2) registry calls made while pending calls are being processed. |
| 373 | While (1) is very unlikely, (2) is a real possibility. |
| 374 | The current code is safe against (2), but not against (1). |
| 375 | The safety against (2) is derived from the fact that only one |
| 376 | thread (the main thread) ever takes things out of the queue. |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 378 | XXX Darn! With the advent of thread state, we should have an array |
| 379 | of pending calls per thread in the thread state! Later... |
| 380 | */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 381 | |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 382 | #define NPENDINGCALLS 32 |
| 383 | static struct { |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 384 | int (*func)(void *); |
| 385 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 386 | } pendingcalls[NPENDINGCALLS]; |
| 387 | static volatile int pendingfirst = 0; |
| 388 | static volatile int pendinglast = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 389 | static volatile int things_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 390 | |
| 391 | int |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 392 | Py_AddPendingCall(int (*func)(void *), void *arg) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 393 | { |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 394 | static int busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 395 | int i, j; |
| 396 | /* XXX Begin critical section */ |
| 397 | /* XXX If you want this to be safe against nested |
| 398 | XXX asynchronous calls, you'll have to work harder! */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 399 | if (busy) |
| 400 | return -1; |
| 401 | busy = 1; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 402 | i = pendinglast; |
| 403 | j = (i + 1) % NPENDINGCALLS; |
| 404 | if (j == pendingfirst) |
| 405 | return -1; /* Queue full */ |
| 406 | pendingcalls[i].func = func; |
| 407 | pendingcalls[i].arg = arg; |
| 408 | pendinglast = j; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 409 | things_to_do = 1; /* Signal main loop */ |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 410 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 411 | /* XXX End critical section */ |
| 412 | return 0; |
| 413 | } |
| 414 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 415 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 416 | Py_MakePendingCalls(void) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 417 | { |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 418 | static int busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 419 | #ifdef WITH_THREAD |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 420 | if (main_thread && PyThread_get_thread_ident() != main_thread) |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 421 | return 0; |
| 422 | #endif |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 423 | if (busy) |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 424 | return 0; |
| 425 | busy = 1; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 426 | things_to_do = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 427 | for (;;) { |
| 428 | int i; |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 429 | int (*func)(void *); |
| 430 | void *arg; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 431 | i = pendingfirst; |
| 432 | if (i == pendinglast) |
| 433 | break; /* Queue empty */ |
| 434 | func = pendingcalls[i].func; |
| 435 | arg = pendingcalls[i].arg; |
| 436 | pendingfirst = (i + 1) % NPENDINGCALLS; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 437 | if (func(arg) < 0) { |
| 438 | busy = 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 439 | things_to_do = 1; /* We're not done yet */ |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 440 | return -1; |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 441 | } |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 442 | } |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 443 | busy = 0; |
Guido van Rossum | a967209 | 1994-09-14 13:31:22 +0000 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 448 | /* The interpreter's recursion limit */ |
| 449 | |
Guido van Rossum | 349ff6f | 2000-09-01 01:52:08 +0000 | [diff] [blame] | 450 | static int recursion_limit = 1000; |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 451 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 452 | int |
| 453 | Py_GetRecursionLimit(void) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 454 | { |
| 455 | return recursion_limit; |
| 456 | } |
| 457 | |
Vladimir Marangozov | 7bd25be | 2000-09-01 11:07:19 +0000 | [diff] [blame] | 458 | void |
| 459 | Py_SetRecursionLimit(int new_limit) |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 460 | { |
| 461 | recursion_limit = new_limit; |
| 462 | } |
| 463 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 464 | /* Status code for main loop (reason for stack unwind) */ |
| 465 | |
| 466 | enum why_code { |
| 467 | WHY_NOT, /* No error */ |
| 468 | WHY_EXCEPTION, /* Exception occurred */ |
| 469 | WHY_RERAISE, /* Exception re-raised by 'finally' */ |
| 470 | WHY_RETURN, /* 'return' statement */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 471 | WHY_BREAK, /* 'break' statement */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 472 | WHY_CONTINUE, /* 'continue' statement */ |
| 473 | WHY_YIELD, /* 'yield' operator */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 474 | }; |
| 475 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 476 | static enum why_code do_raise(PyObject *, PyObject *, PyObject *); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 477 | static int unpack_iterable(PyObject *, int, PyObject **); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 479 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 480 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 481 | PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 482 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 483 | return PyEval_EvalCodeEx(co, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 484 | globals, locals, |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 485 | (PyObject **)NULL, 0, |
| 486 | (PyObject **)NULL, 0, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 487 | (PyObject **)NULL, 0, |
| 488 | NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
| 492 | /* Interpreter main loop */ |
| 493 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 494 | static PyObject * |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 495 | eval_frame(PyFrameObject *f) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 496 | { |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 497 | #ifdef DXPAIRS |
| 498 | int lastopcode = 0; |
| 499 | #endif |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 500 | PyObject **stack_pointer; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 501 | register unsigned char *next_instr; |
Moshe Zadka | aa39a7e | 2000-08-07 06:34:45 +0000 | [diff] [blame] | 502 | register int opcode=0; /* Current opcode */ |
| 503 | register int oparg=0; /* Current opcode argument, if any */ |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 504 | register enum why_code why; /* Reason for block stack unwind */ |
| 505 | register int err; /* Error status -- nonzero if error */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 506 | register PyObject *x; /* Result object -- NULL if error */ |
| 507 | register PyObject *v; /* Temporary objects popped off stack */ |
| 508 | register PyObject *w; |
| 509 | register PyObject *u; |
| 510 | register PyObject *t; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 511 | register PyObject *stream = NULL; /* for PRINT opcodes */ |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 512 | register PyObject **fastlocals, **freevars; |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 513 | PyObject *retval = NULL; /* Return value */ |
Guido van Rossum | 885553e | 1998-12-21 18:33:30 +0000 | [diff] [blame] | 514 | PyThreadState *tstate = PyThreadState_GET(); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 515 | PyCodeObject *co; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 516 | unsigned char *first_instr; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 517 | #ifdef LLTRACE |
Guido van Rossum | acbe8da | 1993-04-15 15:33:52 +0000 | [diff] [blame] | 518 | int lltrace; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 519 | #endif |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 520 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 521 | /* Make it easier to find out where we are with a debugger */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 522 | char *filename; |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 523 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 524 | |
| 525 | /* Code access macros */ |
| 526 | |
| 527 | #define GETCONST(i) Getconst(f, i) |
| 528 | #define GETNAME(i) Getname(f, i) |
| 529 | #define GETNAMEV(i) Getnamev(f, i) |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 530 | #define INSTR_OFFSET() (next_instr - first_instr) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 531 | #define NEXTOP() (*next_instr++) |
| 532 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 533 | #define JUMPTO(x) (next_instr = first_instr + (x)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 534 | #define JUMPBY(x) (next_instr += (x)) |
| 535 | |
| 536 | /* Stack manipulation macros */ |
| 537 | |
| 538 | #define STACK_LEVEL() (stack_pointer - f->f_valuestack) |
| 539 | #define EMPTY() (STACK_LEVEL() == 0) |
| 540 | #define TOP() (stack_pointer[-1]) |
| 541 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 542 | #define BASIC_POP() (*--stack_pointer) |
| 543 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 544 | #ifdef LLTRACE |
Fred Drake | de26cfc | 2001-10-13 06:11:28 +0000 | [diff] [blame] | 545 | #define PUSH(v) (void)(BASIC_PUSH(v), lltrace && prtrace(TOP(), "push")) |
| 546 | #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP()) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 547 | #else |
| 548 | #define PUSH(v) BASIC_PUSH(v) |
| 549 | #define POP() BASIC_POP() |
| 550 | #endif |
| 551 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 552 | /* Local variable macros */ |
| 553 | |
| 554 | #define GETLOCAL(i) (fastlocals[i]) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 555 | #define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 556 | GETLOCAL(i) = value; } while (0) |
| 557 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 558 | /* Start of code */ |
| 559 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 560 | if (f == NULL) |
| 561 | return NULL; |
| 562 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 563 | #ifdef USE_STACKCHECK |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 564 | if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 565 | PyErr_SetString(PyExc_MemoryError, "Stack overflow"); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 566 | return NULL; |
| 567 | } |
| 568 | #endif |
| 569 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 570 | /* push frame */ |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 571 | if (++tstate->recursion_depth > recursion_limit) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 572 | --tstate->recursion_depth; |
| 573 | PyErr_SetString(PyExc_RuntimeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 574 | "maximum recursion depth exceeded"); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 575 | tstate->frame = f->f_back; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 576 | return NULL; |
| 577 | } |
| 578 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 579 | tstate->frame = f; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 580 | co = f->f_code; |
| 581 | fastlocals = f->f_localsplus; |
| 582 | freevars = f->f_localsplus + f->f_nlocals; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 583 | _PyCode_GETCODEPTR(co, &first_instr); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 584 | next_instr = first_instr + f->f_lasti; |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 585 | stack_pointer = f->f_stacktop; |
| 586 | assert(stack_pointer != NULL); |
| 587 | f->f_stacktop = NULL; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 588 | |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 589 | if (tstate->use_tracing) { |
| 590 | if (tstate->c_tracefunc != NULL) { |
| 591 | /* tstate->c_tracefunc, if defined, is a |
| 592 | function that will be called on *every* entry |
| 593 | to a code block. Its return value, if not |
| 594 | None, is a function that will be called at |
| 595 | the start of each executed line of code. |
| 596 | (Actually, the function must return itself |
| 597 | in order to continue tracing.) The trace |
| 598 | functions are called with three arguments: |
| 599 | a pointer to the current frame, a string |
| 600 | indicating why the function is called, and |
| 601 | an argument which depends on the situation. |
| 602 | The global trace function is also called |
| 603 | whenever an exception is detected. */ |
| 604 | if (call_trace(tstate->c_tracefunc, tstate->c_traceobj, |
| 605 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 606 | /* Trace function raised an error */ |
| 607 | return NULL; |
| 608 | } |
| 609 | } |
| 610 | if (tstate->c_profilefunc != NULL) { |
| 611 | /* Similar for c_profilefunc, except it needn't |
| 612 | return itself and isn't called for "line" events */ |
| 613 | if (call_trace(tstate->c_profilefunc, |
| 614 | tstate->c_profileobj, |
| 615 | f, PyTrace_CALL, Py_None)) { |
Neil Schemenauer | 6c0f200 | 2001-09-04 19:03:35 +0000 | [diff] [blame] | 616 | /* Profile function raised an error */ |
| 617 | return NULL; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 622 | #ifdef LLTRACE |
| 623 | lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL; |
| 624 | #endif |
| 625 | #if defined(Py_DEBUG) || defined(LLTRACE) |
| 626 | filename = PyString_AsString(co->co_filename); |
| 627 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 628 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 629 | why = WHY_NOT; |
| 630 | err = 0; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 631 | x = Py_None; /* Not a reference, just anything non-NULL */ |
Fred Drake | 48fba73 | 2000-10-11 13:54:07 +0000 | [diff] [blame] | 632 | w = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 633 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 634 | for (;;) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 635 | /* Do periodic things. Doing this every time through |
| 636 | the loop would add too much overhead, so we do it |
| 637 | only every Nth instruction. We also do it if |
| 638 | ``things_to_do'' is set, i.e. when an asynchronous |
| 639 | event needs attention (e.g. a signal handler or |
| 640 | async I/O handler); see Py_AddPendingCall() and |
| 641 | Py_MakePendingCalls() above. */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 642 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 643 | if (things_to_do || --tstate->ticker < 0) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 644 | tstate->ticker = tstate->interp->checkinterval; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 645 | if (things_to_do) { |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 646 | if (Py_MakePendingCalls() < 0) { |
| 647 | why = WHY_EXCEPTION; |
| 648 | goto on_error; |
| 649 | } |
| 650 | } |
Guido van Rossum | df0d00e | 1997-05-20 15:57:49 +0000 | [diff] [blame] | 651 | #if !defined(HAVE_SIGNAL_H) || defined(macintosh) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 652 | /* If we have true signals, the signal handler |
| 653 | will call Py_AddPendingCall() so we don't |
| 654 | have to call sigcheck(). On the Mac and |
| 655 | DOS, alas, we have to call it. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 656 | if (PyErr_CheckSignals()) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 657 | why = WHY_EXCEPTION; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 658 | goto on_error; |
| 659 | } |
Guido van Rossum | 70d4478 | 1997-01-21 06:15:24 +0000 | [diff] [blame] | 660 | #endif |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 661 | |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 662 | #ifdef WITH_THREAD |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 663 | if (interpreter_lock) { |
| 664 | /* Give another thread a chance */ |
| 665 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 666 | if (PyThreadState_Swap(NULL) != tstate) |
| 667 | Py_FatalError("ceval: tstate mix-up"); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 668 | PyThread_release_lock(interpreter_lock); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 669 | |
| 670 | /* Other threads may run now */ |
| 671 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 672 | PyThread_acquire_lock(interpreter_lock, 1); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 673 | if (PyThreadState_Swap(tstate) != NULL) |
| 674 | Py_FatalError("ceval: orphan tstate"); |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 675 | } |
| 676 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 677 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 678 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 679 | /* Extract opcode and argument */ |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 680 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 681 | #if defined(Py_DEBUG) || defined(LLTRACE) |
Guido van Rossum | 99bec95 | 1992-09-03 20:29:45 +0000 | [diff] [blame] | 682 | f->f_lasti = INSTR_OFFSET(); |
| 683 | #endif |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 684 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 685 | opcode = NEXTOP(); |
| 686 | if (HAS_ARG(opcode)) |
| 687 | oparg = NEXTARG(); |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 688 | dispatch_opcode: |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 689 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 690 | #ifdef DXPAIRS |
| 691 | dxpairs[lastopcode][opcode]++; |
| 692 | lastopcode = opcode; |
| 693 | #endif |
| 694 | dxp[opcode]++; |
| 695 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 696 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 697 | #ifdef LLTRACE |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 698 | /* Instruction tracing */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 699 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 700 | if (lltrace) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 701 | if (HAS_ARG(opcode)) { |
| 702 | printf("%d: %d, %d\n", |
| 703 | (int) (INSTR_OFFSET() - 3), |
| 704 | opcode, oparg); |
| 705 | } |
| 706 | else { |
| 707 | printf("%d: %d\n", |
| 708 | (int) (INSTR_OFFSET() - 1), opcode); |
| 709 | } |
| 710 | } |
| 711 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 712 | /* Main switch on opcode */ |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 713 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 714 | switch (opcode) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 715 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 716 | /* BEWARE! |
| 717 | It is essential that any operation that fails sets either |
| 718 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 719 | and that no operation that succeeds does this! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 720 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 721 | /* case STOP_CODE: this is an error! */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 722 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 723 | case POP_TOP: |
| 724 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 725 | Py_DECREF(v); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 726 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 727 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 728 | case ROT_TWO: |
| 729 | v = POP(); |
| 730 | w = POP(); |
| 731 | PUSH(v); |
| 732 | PUSH(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 733 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 734 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 735 | case ROT_THREE: |
| 736 | v = POP(); |
| 737 | w = POP(); |
| 738 | x = POP(); |
| 739 | PUSH(v); |
| 740 | PUSH(x); |
| 741 | PUSH(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 742 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 743 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 744 | case ROT_FOUR: |
| 745 | u = POP(); |
| 746 | v = POP(); |
| 747 | w = POP(); |
| 748 | x = POP(); |
| 749 | PUSH(u); |
| 750 | PUSH(x); |
| 751 | PUSH(w); |
| 752 | PUSH(v); |
| 753 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 754 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 755 | case DUP_TOP: |
| 756 | v = TOP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 757 | Py_INCREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 758 | PUSH(v); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 759 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 760 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 761 | case DUP_TOPX: |
| 762 | switch (oparg) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 763 | case 1: |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 764 | x = TOP(); |
| 765 | Py_INCREF(x); |
| 766 | PUSH(x); |
| 767 | continue; |
| 768 | case 2: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 769 | x = POP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 770 | Py_INCREF(x); |
| 771 | w = TOP(); |
| 772 | Py_INCREF(w); |
| 773 | PUSH(x); |
| 774 | PUSH(w); |
| 775 | PUSH(x); |
| 776 | continue; |
| 777 | case 3: |
| 778 | x = POP(); |
| 779 | Py_INCREF(x); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 780 | w = POP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 781 | Py_INCREF(w); |
| 782 | v = TOP(); |
| 783 | Py_INCREF(v); |
| 784 | PUSH(w); |
| 785 | PUSH(x); |
| 786 | PUSH(v); |
| 787 | PUSH(w); |
| 788 | PUSH(x); |
| 789 | continue; |
| 790 | case 4: |
| 791 | x = POP(); |
| 792 | Py_INCREF(x); |
| 793 | w = POP(); |
| 794 | Py_INCREF(w); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 795 | v = POP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 796 | Py_INCREF(v); |
| 797 | u = TOP(); |
| 798 | Py_INCREF(u); |
| 799 | PUSH(v); |
| 800 | PUSH(w); |
| 801 | PUSH(x); |
| 802 | PUSH(u); |
| 803 | PUSH(v); |
| 804 | PUSH(w); |
| 805 | PUSH(x); |
| 806 | continue; |
| 807 | case 5: |
| 808 | x = POP(); |
| 809 | Py_INCREF(x); |
| 810 | w = POP(); |
| 811 | Py_INCREF(w); |
| 812 | v = POP(); |
| 813 | Py_INCREF(v); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 814 | u = POP(); |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 815 | Py_INCREF(u); |
| 816 | t = TOP(); |
| 817 | Py_INCREF(t); |
| 818 | PUSH(u); |
| 819 | PUSH(v); |
| 820 | PUSH(w); |
| 821 | PUSH(x); |
| 822 | PUSH(t); |
| 823 | PUSH(u); |
| 824 | PUSH(v); |
| 825 | PUSH(w); |
| 826 | PUSH(x); |
| 827 | continue; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 828 | default: |
Thomas Wouters | 0be483f | 2000-10-11 23:26:11 +0000 | [diff] [blame] | 829 | Py_FatalError("invalid argument to DUP_TOPX" |
| 830 | " (bytecode corruption?)"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 831 | } |
Tim Peters | 35ba689 | 2000-10-11 07:04:49 +0000 | [diff] [blame] | 832 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 833 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 834 | case UNARY_POSITIVE: |
| 835 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 836 | x = PyNumber_Positive(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 837 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 838 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 839 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 840 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 841 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 842 | case UNARY_NEGATIVE: |
| 843 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 844 | x = PyNumber_Negative(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 845 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 846 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 847 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 848 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 849 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 850 | case UNARY_NOT: |
| 851 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 852 | err = PyObject_IsTrue(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 853 | Py_DECREF(v); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 854 | if (err == 0) { |
| 855 | Py_INCREF(Py_True); |
| 856 | PUSH(Py_True); |
| 857 | continue; |
| 858 | } |
| 859 | else if (err > 0) { |
| 860 | Py_INCREF(Py_False); |
| 861 | PUSH(Py_False); |
| 862 | err = 0; |
| 863 | continue; |
| 864 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 865 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 866 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 867 | case UNARY_CONVERT: |
| 868 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 869 | x = PyObject_Repr(v); |
| 870 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 871 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 872 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 873 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 874 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 875 | case UNARY_INVERT: |
| 876 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 877 | x = PyNumber_Invert(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 878 | Py_DECREF(v); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 879 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 880 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 881 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 882 | |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 883 | case BINARY_POWER: |
| 884 | w = POP(); |
| 885 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 886 | x = PyNumber_Power(v, w, Py_None); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 887 | Py_DECREF(v); |
| 888 | Py_DECREF(w); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 889 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 890 | if (x != NULL) continue; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 891 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 892 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 893 | case BINARY_MULTIPLY: |
| 894 | w = POP(); |
| 895 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 896 | x = PyNumber_Multiply(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 897 | Py_DECREF(v); |
| 898 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 899 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 900 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 901 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 902 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 903 | case BINARY_DIVIDE: |
| 904 | w = POP(); |
| 905 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 906 | x = PyNumber_Divide(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 907 | Py_DECREF(v); |
| 908 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 909 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 910 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 911 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 912 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 913 | case BINARY_FLOOR_DIVIDE: |
| 914 | w = POP(); |
| 915 | v = POP(); |
| 916 | x = PyNumber_FloorDivide(v, w); |
| 917 | Py_DECREF(v); |
| 918 | Py_DECREF(w); |
| 919 | PUSH(x); |
| 920 | if (x != NULL) continue; |
| 921 | break; |
| 922 | |
| 923 | case BINARY_TRUE_DIVIDE: |
| 924 | w = POP(); |
| 925 | v = POP(); |
| 926 | x = PyNumber_TrueDivide(v, w); |
| 927 | Py_DECREF(v); |
| 928 | Py_DECREF(w); |
| 929 | PUSH(x); |
| 930 | if (x != NULL) continue; |
| 931 | break; |
| 932 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 933 | case BINARY_MODULO: |
| 934 | w = POP(); |
| 935 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 936 | x = PyNumber_Remainder(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 937 | Py_DECREF(v); |
| 938 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 939 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 940 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 941 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 942 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 943 | case BINARY_ADD: |
| 944 | w = POP(); |
| 945 | v = POP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 946 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 947 | /* INLINE: int + int */ |
| 948 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 949 | a = PyInt_AS_LONG(v); |
| 950 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 951 | i = a + b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 952 | if ((i^a) < 0 && (i^b) < 0) |
| 953 | goto slow_add; |
| 954 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 955 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 956 | else { |
| 957 | slow_add: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 958 | x = PyNumber_Add(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 959 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 960 | Py_DECREF(v); |
| 961 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 962 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 963 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 964 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 965 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 966 | case BINARY_SUBTRACT: |
| 967 | w = POP(); |
| 968 | v = POP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 969 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 970 | /* INLINE: int - int */ |
| 971 | register long a, b, i; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 972 | a = PyInt_AS_LONG(v); |
| 973 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 974 | i = a - b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 975 | if ((i^a) < 0 && (i^~b) < 0) |
| 976 | goto slow_sub; |
| 977 | x = PyInt_FromLong(i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 978 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 979 | else { |
| 980 | slow_sub: |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 981 | x = PyNumber_Subtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 982 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 983 | Py_DECREF(v); |
| 984 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 985 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 986 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 987 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 988 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 989 | case BINARY_SUBSCR: |
| 990 | w = POP(); |
| 991 | v = POP(); |
Tim Peters | b1c4698 | 2001-10-05 20:41:38 +0000 | [diff] [blame] | 992 | if (PyList_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 993 | /* INLINE: list[int] */ |
| 994 | long i = PyInt_AsLong(w); |
| 995 | if (i < 0) |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 996 | i += PyList_GET_SIZE(v); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 997 | if (i < 0 || |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 998 | i >= PyList_GET_SIZE(v)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 999 | PyErr_SetString(PyExc_IndexError, |
| 1000 | "list index out of range"); |
| 1001 | x = NULL; |
| 1002 | } |
| 1003 | else { |
Guido van Rossum | fa00e95 | 1998-07-08 15:02:37 +0000 | [diff] [blame] | 1004 | x = PyList_GET_ITEM(v, i); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1005 | Py_INCREF(x); |
| 1006 | } |
| 1007 | } |
| 1008 | else |
| 1009 | x = PyObject_GetItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1010 | Py_DECREF(v); |
| 1011 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1012 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1013 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1014 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1015 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1016 | case BINARY_LSHIFT: |
| 1017 | w = POP(); |
| 1018 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1019 | x = PyNumber_Lshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1020 | Py_DECREF(v); |
| 1021 | Py_DECREF(w); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1022 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1023 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1024 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1025 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1026 | case BINARY_RSHIFT: |
| 1027 | w = POP(); |
| 1028 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1029 | x = PyNumber_Rshift(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1030 | Py_DECREF(v); |
| 1031 | Py_DECREF(w); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1032 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1033 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1034 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1035 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1036 | case BINARY_AND: |
| 1037 | w = POP(); |
| 1038 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1039 | x = PyNumber_And(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1040 | Py_DECREF(v); |
| 1041 | Py_DECREF(w); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1042 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1043 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1044 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1045 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1046 | case BINARY_XOR: |
| 1047 | w = POP(); |
| 1048 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1049 | x = PyNumber_Xor(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1050 | Py_DECREF(v); |
| 1051 | Py_DECREF(w); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1052 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1053 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1054 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1055 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1056 | case BINARY_OR: |
| 1057 | w = POP(); |
| 1058 | v = POP(); |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1059 | x = PyNumber_Or(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1060 | Py_DECREF(v); |
| 1061 | Py_DECREF(w); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1062 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1063 | if (x != NULL) continue; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 1064 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1065 | |
| 1066 | case INPLACE_POWER: |
| 1067 | w = POP(); |
| 1068 | v = POP(); |
| 1069 | x = PyNumber_InPlacePower(v, w, Py_None); |
| 1070 | Py_DECREF(v); |
| 1071 | Py_DECREF(w); |
| 1072 | PUSH(x); |
| 1073 | if (x != NULL) continue; |
| 1074 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1075 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1076 | case INPLACE_MULTIPLY: |
| 1077 | w = POP(); |
| 1078 | v = POP(); |
| 1079 | x = PyNumber_InPlaceMultiply(v, w); |
| 1080 | Py_DECREF(v); |
| 1081 | Py_DECREF(w); |
| 1082 | PUSH(x); |
| 1083 | if (x != NULL) continue; |
| 1084 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1085 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1086 | case INPLACE_DIVIDE: |
| 1087 | w = POP(); |
| 1088 | v = POP(); |
| 1089 | x = PyNumber_InPlaceDivide(v, w); |
| 1090 | Py_DECREF(v); |
| 1091 | Py_DECREF(w); |
| 1092 | PUSH(x); |
| 1093 | if (x != NULL) continue; |
| 1094 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1095 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1096 | case INPLACE_FLOOR_DIVIDE: |
| 1097 | w = POP(); |
| 1098 | v = POP(); |
| 1099 | x = PyNumber_InPlaceFloorDivide(v, w); |
| 1100 | Py_DECREF(v); |
| 1101 | Py_DECREF(w); |
| 1102 | PUSH(x); |
| 1103 | if (x != NULL) continue; |
| 1104 | break; |
| 1105 | |
| 1106 | case INPLACE_TRUE_DIVIDE: |
| 1107 | w = POP(); |
| 1108 | v = POP(); |
| 1109 | x = PyNumber_InPlaceTrueDivide(v, w); |
| 1110 | Py_DECREF(v); |
| 1111 | Py_DECREF(w); |
| 1112 | PUSH(x); |
| 1113 | if (x != NULL) continue; |
| 1114 | break; |
| 1115 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1116 | case INPLACE_MODULO: |
| 1117 | w = POP(); |
| 1118 | v = POP(); |
| 1119 | x = PyNumber_InPlaceRemainder(v, w); |
| 1120 | Py_DECREF(v); |
| 1121 | Py_DECREF(w); |
| 1122 | PUSH(x); |
| 1123 | if (x != NULL) continue; |
| 1124 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1125 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1126 | case INPLACE_ADD: |
| 1127 | w = POP(); |
| 1128 | v = POP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1129 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1130 | /* INLINE: int + int */ |
| 1131 | register long a, b, i; |
| 1132 | a = PyInt_AS_LONG(v); |
| 1133 | b = PyInt_AS_LONG(w); |
| 1134 | i = a + b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1135 | if ((i^a) < 0 && (i^b) < 0) |
| 1136 | goto slow_iadd; |
| 1137 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1138 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1139 | else { |
| 1140 | slow_iadd: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1141 | x = PyNumber_InPlaceAdd(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1142 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1143 | Py_DECREF(v); |
| 1144 | Py_DECREF(w); |
| 1145 | PUSH(x); |
| 1146 | if (x != NULL) continue; |
| 1147 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1148 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1149 | case INPLACE_SUBTRACT: |
| 1150 | w = POP(); |
| 1151 | v = POP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1152 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1153 | /* INLINE: int - int */ |
| 1154 | register long a, b, i; |
| 1155 | a = PyInt_AS_LONG(v); |
| 1156 | b = PyInt_AS_LONG(w); |
| 1157 | i = a - b; |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1158 | if ((i^a) < 0 && (i^~b) < 0) |
| 1159 | goto slow_isub; |
| 1160 | x = PyInt_FromLong(i); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1161 | } |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1162 | else { |
| 1163 | slow_isub: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1164 | x = PyNumber_InPlaceSubtract(v, w); |
Guido van Rossum | 87780df | 2001-08-23 02:58:07 +0000 | [diff] [blame] | 1165 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1166 | Py_DECREF(v); |
| 1167 | Py_DECREF(w); |
| 1168 | PUSH(x); |
| 1169 | if (x != NULL) continue; |
| 1170 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1171 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1172 | case INPLACE_LSHIFT: |
| 1173 | w = POP(); |
| 1174 | v = POP(); |
| 1175 | x = PyNumber_InPlaceLshift(v, w); |
| 1176 | Py_DECREF(v); |
| 1177 | Py_DECREF(w); |
| 1178 | PUSH(x); |
| 1179 | if (x != NULL) continue; |
| 1180 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1181 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1182 | case INPLACE_RSHIFT: |
| 1183 | w = POP(); |
| 1184 | v = POP(); |
| 1185 | x = PyNumber_InPlaceRshift(v, w); |
| 1186 | Py_DECREF(v); |
| 1187 | Py_DECREF(w); |
| 1188 | PUSH(x); |
| 1189 | if (x != NULL) continue; |
| 1190 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1191 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1192 | case INPLACE_AND: |
| 1193 | w = POP(); |
| 1194 | v = POP(); |
| 1195 | x = PyNumber_InPlaceAnd(v, w); |
| 1196 | Py_DECREF(v); |
| 1197 | Py_DECREF(w); |
| 1198 | PUSH(x); |
| 1199 | if (x != NULL) continue; |
| 1200 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1201 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1202 | case INPLACE_XOR: |
| 1203 | w = POP(); |
| 1204 | v = POP(); |
| 1205 | x = PyNumber_InPlaceXor(v, w); |
| 1206 | Py_DECREF(v); |
| 1207 | Py_DECREF(w); |
| 1208 | PUSH(x); |
| 1209 | if (x != NULL) continue; |
| 1210 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1211 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1212 | case INPLACE_OR: |
| 1213 | w = POP(); |
| 1214 | v = POP(); |
| 1215 | x = PyNumber_InPlaceOr(v, w); |
| 1216 | Py_DECREF(v); |
| 1217 | Py_DECREF(w); |
| 1218 | PUSH(x); |
| 1219 | if (x != NULL) continue; |
| 1220 | break; |
| 1221 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1222 | case SLICE+0: |
| 1223 | case SLICE+1: |
| 1224 | case SLICE+2: |
| 1225 | case SLICE+3: |
| 1226 | if ((opcode-SLICE) & 2) |
| 1227 | w = POP(); |
| 1228 | else |
| 1229 | w = NULL; |
| 1230 | if ((opcode-SLICE) & 1) |
| 1231 | v = POP(); |
| 1232 | else |
| 1233 | v = NULL; |
| 1234 | u = POP(); |
| 1235 | x = apply_slice(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1236 | Py_DECREF(u); |
| 1237 | Py_XDECREF(v); |
| 1238 | Py_XDECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1239 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1240 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1241 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1242 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1243 | case STORE_SLICE+0: |
| 1244 | case STORE_SLICE+1: |
| 1245 | case STORE_SLICE+2: |
| 1246 | case STORE_SLICE+3: |
| 1247 | if ((opcode-STORE_SLICE) & 2) |
| 1248 | w = POP(); |
| 1249 | else |
| 1250 | w = NULL; |
| 1251 | if ((opcode-STORE_SLICE) & 1) |
| 1252 | v = POP(); |
| 1253 | else |
| 1254 | v = NULL; |
| 1255 | u = POP(); |
| 1256 | t = POP(); |
| 1257 | err = assign_slice(u, v, w, t); /* u[v:w] = t */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1258 | Py_DECREF(t); |
| 1259 | Py_DECREF(u); |
| 1260 | Py_XDECREF(v); |
| 1261 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1262 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1263 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1264 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1265 | case DELETE_SLICE+0: |
| 1266 | case DELETE_SLICE+1: |
| 1267 | case DELETE_SLICE+2: |
| 1268 | case DELETE_SLICE+3: |
| 1269 | if ((opcode-DELETE_SLICE) & 2) |
| 1270 | w = POP(); |
| 1271 | else |
| 1272 | w = NULL; |
| 1273 | if ((opcode-DELETE_SLICE) & 1) |
| 1274 | v = POP(); |
| 1275 | else |
| 1276 | v = NULL; |
| 1277 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1278 | err = assign_slice(u, v, w, (PyObject *)NULL); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1279 | /* del u[v:w] */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1280 | Py_DECREF(u); |
| 1281 | Py_XDECREF(v); |
| 1282 | Py_XDECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1283 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1284 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1285 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1286 | case STORE_SUBSCR: |
| 1287 | w = POP(); |
| 1288 | v = POP(); |
| 1289 | u = POP(); |
| 1290 | /* v[w] = u */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1291 | err = PyObject_SetItem(v, w, u); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1292 | Py_DECREF(u); |
| 1293 | Py_DECREF(v); |
| 1294 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1295 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1296 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1297 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1298 | case DELETE_SUBSCR: |
| 1299 | w = POP(); |
| 1300 | v = POP(); |
| 1301 | /* del v[w] */ |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1302 | err = PyObject_DelItem(v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1303 | Py_DECREF(v); |
| 1304 | Py_DECREF(w); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1305 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1306 | break; |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1307 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1308 | case PRINT_EXPR: |
| 1309 | v = POP(); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1310 | w = PySys_GetObject("displayhook"); |
| 1311 | if (w == NULL) { |
| 1312 | PyErr_SetString(PyExc_RuntimeError, |
| 1313 | "lost sys.displayhook"); |
| 1314 | err = -1; |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1315 | x = NULL; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1316 | } |
| 1317 | if (err == 0) { |
| 1318 | x = Py_BuildValue("(O)", v); |
| 1319 | if (x == NULL) |
| 1320 | err = -1; |
| 1321 | } |
| 1322 | if (err == 0) { |
| 1323 | w = PyEval_CallObject(w, x); |
Moshe Zadka | f5df383 | 2001-01-11 11:55:37 +0000 | [diff] [blame] | 1324 | Py_XDECREF(w); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1325 | if (w == NULL) |
| 1326 | err = -1; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1327 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1328 | Py_DECREF(v); |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1329 | Py_XDECREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1330 | break; |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 1331 | |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1332 | case PRINT_ITEM_TO: |
| 1333 | w = stream = POP(); |
| 1334 | /* fall through to PRINT_ITEM */ |
| 1335 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1336 | case PRINT_ITEM: |
| 1337 | v = POP(); |
Barry Warsaw | 093abe0 | 2000-08-29 04:56:13 +0000 | [diff] [blame] | 1338 | if (stream == NULL || stream == Py_None) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1339 | w = PySys_GetObject("stdout"); |
| 1340 | if (w == NULL) { |
| 1341 | PyErr_SetString(PyExc_RuntimeError, |
| 1342 | "lost sys.stdout"); |
| 1343 | err = -1; |
| 1344 | } |
Guido van Rossum | 8f18320 | 1997-12-31 05:53:15 +0000 | [diff] [blame] | 1345 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1346 | if (w != NULL && PyFile_SoftSpace(w, 1)) |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 1347 | err = PyFile_WriteString(" ", w); |
| 1348 | if (err == 0) |
| 1349 | err = PyFile_WriteObject(v, w, Py_PRINT_RAW); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1350 | if (err == 0 && PyString_Check(v)) { |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1351 | /* XXX move into writeobject() ? */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1352 | char *s = PyString_AsString(v); |
| 1353 | int len = PyString_Size(v); |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 1354 | if (len > 0 && |
| 1355 | isspace(Py_CHARMASK(s[len-1])) && |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1356 | s[len-1] != ' ') |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1357 | PyFile_SoftSpace(w, 0); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1358 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1359 | Py_DECREF(v); |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1360 | Py_XDECREF(stream); |
| 1361 | stream = NULL; |
| 1362 | if (err == 0) |
| 1363 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1364 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1365 | |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1366 | case PRINT_NEWLINE_TO: |
| 1367 | w = stream = POP(); |
| 1368 | /* fall through to PRINT_NEWLINE */ |
| 1369 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1370 | case PRINT_NEWLINE: |
Barry Warsaw | 093abe0 | 2000-08-29 04:56:13 +0000 | [diff] [blame] | 1371 | if (stream == NULL || stream == Py_None) { |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1372 | w = PySys_GetObject("stdout"); |
| 1373 | if (w == NULL) |
| 1374 | PyErr_SetString(PyExc_RuntimeError, |
| 1375 | "lost sys.stdout"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1376 | } |
Barry Warsaw | 23c9ec8 | 2000-08-21 15:44:01 +0000 | [diff] [blame] | 1377 | if (w != NULL) { |
| 1378 | err = PyFile_WriteString("\n", w); |
| 1379 | if (err == 0) |
| 1380 | PyFile_SoftSpace(w, 0); |
| 1381 | } |
| 1382 | Py_XDECREF(stream); |
| 1383 | stream = NULL; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1384 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1385 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1386 | |
| 1387 | #ifdef CASE_TOO_BIG |
| 1388 | default: switch (opcode) { |
| 1389 | #endif |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1390 | case BREAK_LOOP: |
| 1391 | why = WHY_BREAK; |
| 1392 | break; |
Guido van Rossum | 66b0e9c | 2001-03-21 19:17:22 +0000 | [diff] [blame] | 1393 | |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 1394 | case CONTINUE_LOOP: |
| 1395 | retval = PyInt_FromLong(oparg); |
| 1396 | why = WHY_CONTINUE; |
| 1397 | break; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1398 | |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1399 | case RAISE_VARARGS: |
| 1400 | u = v = w = NULL; |
| 1401 | switch (oparg) { |
| 1402 | case 3: |
| 1403 | u = POP(); /* traceback */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1404 | /* Fallthrough */ |
| 1405 | case 2: |
| 1406 | v = POP(); /* value */ |
| 1407 | /* Fallthrough */ |
| 1408 | case 1: |
| 1409 | w = POP(); /* exc */ |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 1410 | case 0: /* Fallthrough */ |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1411 | why = do_raise(w, v, u); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1412 | break; |
| 1413 | default: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1414 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1415 | "bad RAISE_VARARGS oparg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1416 | why = WHY_EXCEPTION; |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 1417 | break; |
| 1418 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1419 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1420 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1421 | case LOAD_LOCALS: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1422 | if ((x = f->f_locals) == NULL) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1423 | PyErr_SetString(PyExc_SystemError, |
| 1424 | "no locals"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1427 | Py_INCREF(x); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1428 | PUSH(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1429 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1430 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1431 | case RETURN_VALUE: |
| 1432 | retval = POP(); |
| 1433 | why = WHY_RETURN; |
| 1434 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1435 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1436 | case YIELD_VALUE: |
| 1437 | retval = POP(); |
Tim Peters | 8c96369 | 2001-06-23 05:26:56 +0000 | [diff] [blame] | 1438 | f->f_stacktop = stack_pointer; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1439 | f->f_lasti = INSTR_OFFSET(); |
| 1440 | why = WHY_YIELD; |
| 1441 | break; |
| 1442 | |
| 1443 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1444 | case EXEC_STMT: |
| 1445 | w = POP(); |
| 1446 | v = POP(); |
| 1447 | u = POP(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1448 | err = exec_statement(f, u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1449 | Py_DECREF(u); |
| 1450 | Py_DECREF(v); |
| 1451 | Py_DECREF(w); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 1452 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1453 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1454 | case POP_BLOCK: |
| 1455 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1456 | PyTryBlock *b = PyFrame_BlockPop(f); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1457 | while (STACK_LEVEL() > b->b_level) { |
| 1458 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1459 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1463 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1464 | case END_FINALLY: |
| 1465 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1466 | if (PyInt_Check(v)) { |
| 1467 | why = (enum why_code) PyInt_AsLong(v); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 1468 | if (why == WHY_RETURN || |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 1469 | why == WHY_YIELD || |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 1470 | why == CONTINUE_LOOP) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1471 | retval = POP(); |
| 1472 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1473 | else if (PyString_Check(v) || PyClass_Check(v)) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1474 | w = POP(); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1475 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1476 | PyErr_Restore(v, w, u); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1477 | why = WHY_RERAISE; |
Guido van Rossum | 0db1ef9 | 1995-07-28 23:06:00 +0000 | [diff] [blame] | 1478 | break; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1479 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1480 | else if (v != Py_None) { |
| 1481 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1482 | "'finally' pops bad exception"); |
| 1483 | why = WHY_EXCEPTION; |
| 1484 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1485 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1486 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1487 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1488 | case BUILD_CLASS: |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1489 | u = POP(); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1490 | v = POP(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1491 | w = POP(); |
| 1492 | x = build_class(u, v, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1493 | PUSH(x); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1494 | Py_DECREF(u); |
| 1495 | Py_DECREF(v); |
| 1496 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1497 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1498 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1499 | case STORE_NAME: |
| 1500 | w = GETNAMEV(oparg); |
| 1501 | v = POP(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1502 | if ((x = f->f_locals) == NULL) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 1503 | PyErr_Format(PyExc_SystemError, |
| 1504 | "no locals found when storing %s", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 1505 | PyObject_REPR(w)); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1506 | break; |
| 1507 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1508 | err = PyDict_SetItem(x, w, v); |
| 1509 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1510 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1511 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1512 | case DELETE_NAME: |
Guido van Rossum | df62e44 | 1991-08-16 08:56:04 +0000 | [diff] [blame] | 1513 | w = GETNAMEV(oparg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1514 | if ((x = f->f_locals) == NULL) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 1515 | PyErr_Format(PyExc_SystemError, |
| 1516 | "no locals when deleting %s", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 1517 | PyObject_REPR(w)); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1518 | break; |
| 1519 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1520 | if ((err = PyDict_DelItem(x, w)) != 0) |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1521 | format_exc_check_arg(PyExc_NameError, |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1522 | NAME_ERROR_MSG ,w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1523 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1524 | |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 1525 | case UNPACK_SEQUENCE: |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1526 | v = POP(); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1527 | if (PyTuple_Check(v)) { |
| 1528 | if (PyTuple_Size(v) != oparg) { |
| 1529 | PyErr_SetString(PyExc_ValueError, |
| 1530 | "unpack tuple of wrong size"); |
| 1531 | why = WHY_EXCEPTION; |
| 1532 | } |
| 1533 | else { |
| 1534 | for (; --oparg >= 0; ) { |
| 1535 | w = PyTuple_GET_ITEM(v, oparg); |
| 1536 | Py_INCREF(w); |
| 1537 | PUSH(w); |
| 1538 | } |
| 1539 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1540 | } |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1541 | else if (PyList_Check(v)) { |
| 1542 | if (PyList_Size(v) != oparg) { |
| 1543 | PyErr_SetString(PyExc_ValueError, |
| 1544 | "unpack list of wrong size"); |
| 1545 | why = WHY_EXCEPTION; |
| 1546 | } |
| 1547 | else { |
| 1548 | for (; --oparg >= 0; ) { |
| 1549 | w = PyList_GET_ITEM(v, oparg); |
| 1550 | Py_INCREF(w); |
| 1551 | PUSH(w); |
| 1552 | } |
| 1553 | } |
| 1554 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 1555 | else if (unpack_iterable(v, oparg, |
| 1556 | stack_pointer + oparg)) |
| 1557 | stack_pointer += oparg; |
Tim Peters | 8b13b3e | 2001-09-30 05:58:42 +0000 | [diff] [blame] | 1558 | else { |
| 1559 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1560 | PyErr_SetString(PyExc_TypeError, |
| 1561 | "unpack non-sequence"); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 1562 | why = WHY_EXCEPTION; |
Tim Peters | 8b13b3e | 2001-09-30 05:58:42 +0000 | [diff] [blame] | 1563 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1564 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1565 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1566 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1567 | case STORE_ATTR: |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1568 | w = GETNAMEV(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1569 | v = POP(); |
| 1570 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1571 | err = PyObject_SetAttr(v, w, u); /* v.w = u */ |
| 1572 | Py_DECREF(v); |
| 1573 | Py_DECREF(u); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1574 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1575 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1576 | case DELETE_ATTR: |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1577 | w = GETNAMEV(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1578 | v = POP(); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1579 | err = PyObject_SetAttr(v, w, (PyObject *)NULL); |
| 1580 | /* del v.w */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1581 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1582 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1583 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1584 | case STORE_GLOBAL: |
| 1585 | w = GETNAMEV(oparg); |
| 1586 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1587 | err = PyDict_SetItem(f->f_globals, w, v); |
| 1588 | Py_DECREF(v); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1589 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1590 | |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1591 | case DELETE_GLOBAL: |
| 1592 | w = GETNAMEV(oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1593 | if ((err = PyDict_DelItem(f->f_globals, w)) != 0) |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1594 | format_exc_check_arg( |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1595 | PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w); |
Guido van Rossum | 32c6cdf | 1991-12-10 13:52:46 +0000 | [diff] [blame] | 1596 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1597 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1598 | case LOAD_CONST: |
| 1599 | x = GETCONST(oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1600 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1601 | PUSH(x); |
| 1602 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1603 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1604 | case LOAD_NAME: |
Guido van Rossum | df62e44 | 1991-08-16 08:56:04 +0000 | [diff] [blame] | 1605 | w = GETNAMEV(oparg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1606 | if ((x = f->f_locals) == NULL) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 1607 | PyErr_Format(PyExc_SystemError, |
| 1608 | "no locals when loading %s", |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 1609 | PyObject_REPR(w)); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1610 | break; |
| 1611 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1612 | x = PyDict_GetItem(x, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1613 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1614 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1615 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1616 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1617 | if (x == NULL) { |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1618 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1619 | PyExc_NameError, |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1620 | NAME_ERROR_MSG ,w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1621 | break; |
| 1622 | } |
| 1623 | } |
| 1624 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1625 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1626 | PUSH(x); |
| 1627 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1628 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1629 | case LOAD_GLOBAL: |
Guido van Rossum | df62e44 | 1991-08-16 08:56:04 +0000 | [diff] [blame] | 1630 | w = GETNAMEV(oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1631 | x = PyDict_GetItem(f->f_globals, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1632 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1633 | x = PyDict_GetItem(f->f_builtins, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1634 | if (x == NULL) { |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1635 | format_exc_check_arg( |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1636 | PyExc_NameError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1637 | GLOBAL_NAME_ERROR_MSG ,w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1638 | break; |
| 1639 | } |
| 1640 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1641 | Py_INCREF(x); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1642 | PUSH(x); |
| 1643 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1644 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1645 | case LOAD_FAST: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1646 | x = GETLOCAL(oparg); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 1647 | if (x == NULL) { |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1648 | format_exc_check_arg( |
| 1649 | PyExc_UnboundLocalError, |
| 1650 | UNBOUNDLOCAL_ERROR_MSG, |
| 1651 | PyTuple_GetItem(co->co_varnames, oparg) |
| 1652 | ); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 1653 | break; |
| 1654 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1655 | Py_INCREF(x); |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 1656 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1657 | if (x != NULL) continue; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1658 | break; |
| 1659 | |
| 1660 | case STORE_FAST: |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1661 | v = POP(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1662 | SETLOCAL(oparg, v); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1663 | continue; |
Guido van Rossum | 8b17d6b | 1993-03-30 13:18:41 +0000 | [diff] [blame] | 1664 | |
| 1665 | case DELETE_FAST: |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 1666 | x = GETLOCAL(oparg); |
| 1667 | if (x == NULL) { |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 1668 | format_exc_check_arg( |
| 1669 | PyExc_UnboundLocalError, |
| 1670 | UNBOUNDLOCAL_ERROR_MSG, |
| 1671 | PyTuple_GetItem(co->co_varnames, oparg) |
| 1672 | ); |
Guido van Rossum | 2e4c899 | 1998-05-12 20:27:36 +0000 | [diff] [blame] | 1673 | break; |
| 1674 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1675 | SETLOCAL(oparg, NULL); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1676 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1677 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1678 | case LOAD_CLOSURE: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 1679 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1680 | Py_INCREF(x); |
| 1681 | PUSH(x); |
| 1682 | break; |
| 1683 | |
| 1684 | case LOAD_DEREF: |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 1685 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1686 | w = PyCell_Get(x); |
Jeremy Hylton | 2524d69 | 2001-02-05 17:23:16 +0000 | [diff] [blame] | 1687 | if (w == NULL) { |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 1688 | if (oparg < f->f_ncells) { |
Jeremy Hylton | 2524d69 | 2001-02-05 17:23:16 +0000 | [diff] [blame] | 1689 | v = PyTuple_GetItem(co->co_cellvars, |
| 1690 | oparg); |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 1691 | format_exc_check_arg( |
| 1692 | PyExc_UnboundLocalError, |
| 1693 | UNBOUNDLOCAL_ERROR_MSG, |
| 1694 | v); |
| 1695 | } else { |
Jeremy Hylton | 2524d69 | 2001-02-05 17:23:16 +0000 | [diff] [blame] | 1696 | v = PyTuple_GetItem( |
| 1697 | co->co_freevars, |
| 1698 | oparg - f->f_ncells); |
Jeremy Hylton | c76770c | 2001-04-13 16:51:46 +0000 | [diff] [blame] | 1699 | format_exc_check_arg( |
| 1700 | PyExc_NameError, |
| 1701 | UNBOUNDFREE_ERROR_MSG, |
| 1702 | v); |
| 1703 | } |
Jeremy Hylton | 2524d69 | 2001-02-05 17:23:16 +0000 | [diff] [blame] | 1704 | err = -1; |
| 1705 | break; |
| 1706 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1707 | PUSH(w); |
| 1708 | break; |
| 1709 | |
| 1710 | case STORE_DEREF: |
| 1711 | w = POP(); |
Jeremy Hylton | 2b724da | 2001-01-29 22:51:52 +0000 | [diff] [blame] | 1712 | x = freevars[oparg]; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1713 | PyCell_Set(x, w); |
Jeremy Hylton | 30c9f39 | 2001-03-13 01:58:22 +0000 | [diff] [blame] | 1714 | Py_DECREF(w); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1715 | continue; |
| 1716 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1717 | case BUILD_TUPLE: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1718 | x = PyTuple_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1719 | if (x != NULL) { |
| 1720 | for (; --oparg >= 0;) { |
| 1721 | w = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1722 | PyTuple_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1723 | } |
| 1724 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1725 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1726 | } |
| 1727 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1728 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1729 | case BUILD_LIST: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1730 | x = PyList_New(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1731 | if (x != NULL) { |
| 1732 | for (; --oparg >= 0;) { |
| 1733 | w = POP(); |
Guido van Rossum | 5053efc | 1998-08-04 15:27:50 +0000 | [diff] [blame] | 1734 | PyList_SET_ITEM(x, oparg, w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1735 | } |
| 1736 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1737 | continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1738 | } |
| 1739 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1740 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1741 | case BUILD_MAP: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1742 | x = PyDict_New(); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1743 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1744 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1745 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1746 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1747 | case LOAD_ATTR: |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1748 | w = GETNAMEV(oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1749 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1750 | x = PyObject_GetAttr(v, w); |
| 1751 | Py_DECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1752 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1753 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1754 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1755 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1756 | case COMPARE_OP: |
| 1757 | w = POP(); |
| 1758 | v = POP(); |
Tim Peters | c1e6d96 | 2001-10-05 20:21:03 +0000 | [diff] [blame] | 1759 | if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1760 | /* INLINE: cmp(int, int) */ |
| 1761 | register long a, b; |
| 1762 | register int res; |
Guido van Rossum | cf183ac | 1998-12-04 18:51:36 +0000 | [diff] [blame] | 1763 | a = PyInt_AS_LONG(v); |
| 1764 | b = PyInt_AS_LONG(w); |
Guido van Rossum | c12da69 | 1997-07-17 23:12:42 +0000 | [diff] [blame] | 1765 | switch (oparg) { |
| 1766 | case LT: res = a < b; break; |
| 1767 | case LE: res = a <= b; break; |
| 1768 | case EQ: res = a == b; break; |
| 1769 | case NE: res = a != b; break; |
| 1770 | case GT: res = a > b; break; |
| 1771 | case GE: res = a >= b; break; |
| 1772 | case IS: res = v == w; break; |
| 1773 | case IS_NOT: res = v != w; break; |
| 1774 | default: goto slow_compare; |
| 1775 | } |
| 1776 | x = res ? Py_True : Py_False; |
| 1777 | Py_INCREF(x); |
| 1778 | } |
| 1779 | else { |
| 1780 | slow_compare: |
| 1781 | x = cmp_outcome(oparg, v, w); |
| 1782 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1783 | Py_DECREF(v); |
| 1784 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1785 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1786 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1787 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1788 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1789 | case IMPORT_NAME: |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1790 | w = GETNAMEV(oparg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1791 | x = PyDict_GetItemString(f->f_builtins, "__import__"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1792 | if (x == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1793 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | fc49073 | 1997-05-06 15:06:49 +0000 | [diff] [blame] | 1794 | "__import__ not found"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1795 | break; |
| 1796 | } |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 1797 | u = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1798 | w = Py_BuildValue("(OOOO)", |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1799 | w, |
| 1800 | f->f_globals, |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1801 | f->f_locals == NULL ? |
| 1802 | Py_None : f->f_locals, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1803 | u); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1804 | Py_DECREF(u); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1805 | if (w == NULL) { |
| 1806 | x = NULL; |
| 1807 | break; |
| 1808 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1809 | x = PyEval_CallObject(x, w); |
| 1810 | Py_DECREF(w); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1811 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1812 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1813 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1814 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 1815 | case IMPORT_STAR: |
| 1816 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1817 | PyFrame_FastToLocals(f); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1818 | if ((x = f->f_locals) == NULL) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 1819 | PyErr_SetString(PyExc_SystemError, |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 1820 | "no locals found during 'import *'"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 1821 | break; |
| 1822 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 1823 | err = import_all_from(x, v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1824 | PyFrame_LocalsToFast(f, 0); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 1825 | Py_DECREF(v); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1826 | if (err == 0) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1827 | break; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1828 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 1829 | case IMPORT_FROM: |
| 1830 | w = GETNAMEV(oparg); |
| 1831 | v = TOP(); |
| 1832 | x = import_from(v, w); |
| 1833 | PUSH(x); |
| 1834 | if (x != NULL) continue; |
| 1835 | break; |
| 1836 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1837 | case JUMP_FORWARD: |
| 1838 | JUMPBY(oparg); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1839 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1840 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1841 | case JUMP_IF_FALSE: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1842 | err = PyObject_IsTrue(TOP()); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1843 | if (err > 0) |
| 1844 | err = 0; |
| 1845 | else if (err == 0) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1846 | JUMPBY(oparg); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1847 | else |
| 1848 | break; |
| 1849 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1850 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1851 | case JUMP_IF_TRUE: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1852 | err = PyObject_IsTrue(TOP()); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1853 | if (err > 0) { |
| 1854 | err = 0; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1855 | JUMPBY(oparg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1856 | } |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1857 | else if (err == 0) |
| 1858 | ; |
| 1859 | else |
| 1860 | break; |
| 1861 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1862 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1863 | case JUMP_ABSOLUTE: |
| 1864 | JUMPTO(oparg); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1865 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1866 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1867 | case GET_ITER: |
| 1868 | /* before: [obj]; after [getiter(obj)] */ |
| 1869 | v = POP(); |
| 1870 | x = PyObject_GetIter(v); |
| 1871 | Py_DECREF(v); |
| 1872 | if (x != NULL) { |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1873 | PUSH(x); |
| 1874 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1875 | } |
| 1876 | break; |
| 1877 | |
| 1878 | case FOR_ITER: |
| 1879 | /* before: [iter]; after: [iter, iter()] *or* [] */ |
| 1880 | v = TOP(); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1881 | x = PyIter_Next(v); |
| 1882 | if (x != NULL) { |
| 1883 | PUSH(x); |
| 1884 | continue; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1885 | } |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 1886 | if (!PyErr_Occurred()) { |
| 1887 | /* iterator ended normally */ |
| 1888 | x = v = POP(); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1889 | Py_DECREF(v); |
| 1890 | JUMPBY(oparg); |
| 1891 | continue; |
| 1892 | } |
| 1893 | break; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1894 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1895 | case FOR_LOOP: |
| 1896 | /* for v in s: ... |
| 1897 | On entry: stack contains s, i. |
| 1898 | On exit: stack contains s, i+1, s[i]; |
| 1899 | but if loop exhausted: |
| 1900 | s, i are popped, and we jump */ |
| 1901 | w = POP(); /* Loop index */ |
| 1902 | v = POP(); /* Sequence object */ |
| 1903 | u = loop_subscript(v, w); |
| 1904 | if (u != NULL) { |
| 1905 | PUSH(v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1906 | x = PyInt_FromLong(PyInt_AsLong(w)+1); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1907 | PUSH(x); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1908 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1909 | PUSH(u); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1910 | if (x != NULL) continue; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1911 | } |
| 1912 | else { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1913 | Py_DECREF(v); |
| 1914 | Py_DECREF(w); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1915 | /* A NULL can mean "s exhausted" |
| 1916 | but also an error: */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1917 | if (PyErr_Occurred()) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1918 | why = WHY_EXCEPTION; |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1919 | else { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1920 | JUMPBY(oparg); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1921 | continue; |
| 1922 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1923 | } |
| 1924 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1925 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1926 | case SETUP_LOOP: |
| 1927 | case SETUP_EXCEPT: |
| 1928 | case SETUP_FINALLY: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 1929 | PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg, |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 1930 | STACK_LEVEL()); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1931 | continue; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1932 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1933 | case SET_LINENO: |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1934 | #ifdef LLTRACE |
| 1935 | if (lltrace) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1936 | printf("--- %s:%d \n", filename, oparg); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1937 | #endif |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 1938 | f->f_lineno = oparg; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 1939 | if (tstate->c_tracefunc == NULL || tstate->tracing) |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 1940 | continue; |
| 1941 | /* Trace each line of code reached */ |
| 1942 | f->f_lasti = INSTR_OFFSET(); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 1943 | /* Inline call_trace() for performance: */ |
| 1944 | tstate->tracing++; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 1945 | tstate->use_tracing = 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 1946 | err = (tstate->c_tracefunc)(tstate->c_traceobj, f, |
| 1947 | PyTrace_LINE, Py_None); |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 1948 | tstate->use_tracing = (tstate->c_tracefunc |
| 1949 | || tstate->c_profilefunc); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 1950 | tstate->tracing--; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1951 | break; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1952 | |
| 1953 | case CALL_FUNCTION: |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1954 | { |
| 1955 | int na = oparg & 0xff; |
| 1956 | int nk = (oparg>>8) & 0xff; |
| 1957 | int n = na + 2 * nk; |
| 1958 | PyObject **pfunc = stack_pointer - n - 1; |
| 1959 | PyObject *func = *pfunc; |
| 1960 | f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */ |
| 1961 | |
| 1962 | /* Always dispatch PyCFunction first, because |
| 1963 | these are presumed to be the most frequent |
| 1964 | callable object. |
| 1965 | */ |
| 1966 | if (PyCFunction_Check(func)) { |
Jeremy Hylton | da20fce | 2001-05-18 20:53:14 +0000 | [diff] [blame] | 1967 | int flags = PyCFunction_GET_FLAGS(func); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1968 | if (nk != 0 || (flags & METH_KEYWORDS)) |
Jeremy Hylton | 4c9dace | 2001-05-29 16:23:26 +0000 | [diff] [blame] | 1969 | x = do_call(func, &stack_pointer, |
| 1970 | na, nk); |
| 1971 | else if (flags == METH_VARARGS) { |
Jeremy Hylton | da20fce | 2001-05-18 20:53:14 +0000 | [diff] [blame] | 1972 | PyObject *callargs; |
| 1973 | callargs = load_args(&stack_pointer, na); |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 1974 | x = PyCFunction_Call(func, callargs, NULL); |
Jeremy Hylton | da20fce | 2001-05-18 20:53:14 +0000 | [diff] [blame] | 1975 | Py_XDECREF(callargs); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1976 | } else |
| 1977 | x = fast_cfunction(func, |
| 1978 | &stack_pointer, na); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1979 | } else { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1980 | if (PyMethod_Check(func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1981 | && PyMethod_GET_SELF(func) != NULL) { |
| 1982 | /* optimize access to bound methods */ |
| 1983 | PyObject *self = PyMethod_GET_SELF(func); |
| 1984 | Py_INCREF(self); |
| 1985 | func = PyMethod_GET_FUNCTION(func); |
| 1986 | Py_INCREF(func); |
| 1987 | Py_DECREF(*pfunc); |
| 1988 | *pfunc = self; |
| 1989 | na++; |
| 1990 | n++; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1991 | } else |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1992 | Py_INCREF(func); |
| 1993 | if (PyFunction_Check(func)) { |
| 1994 | x = fast_function(func, &stack_pointer, |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 1995 | n, na, nk); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 1996 | } else { |
| 1997 | x = do_call(func, &stack_pointer, |
| 1998 | na, nk); |
| 1999 | } |
| 2000 | Py_DECREF(func); |
| 2001 | } |
| 2002 | |
| 2003 | while (stack_pointer > pfunc) { |
| 2004 | w = POP(); |
| 2005 | Py_DECREF(w); |
| 2006 | } |
| 2007 | PUSH(x); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2008 | if (x != NULL) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2009 | continue; |
| 2010 | break; |
| 2011 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2012 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2013 | case CALL_FUNCTION_VAR: |
| 2014 | case CALL_FUNCTION_KW: |
| 2015 | case CALL_FUNCTION_VAR_KW: |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2016 | { |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2017 | int na = oparg & 0xff; |
| 2018 | int nk = (oparg>>8) & 0xff; |
| 2019 | int flags = (opcode - CALL_FUNCTION) & 3; |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2020 | int n = na + 2 * nk; |
| 2021 | PyObject **pfunc, *func; |
| 2022 | if (flags & CALL_FLAG_VAR) |
| 2023 | n++; |
| 2024 | if (flags & CALL_FLAG_KW) |
| 2025 | n++; |
| 2026 | pfunc = stack_pointer - n - 1; |
| 2027 | func = *pfunc; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2028 | f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */ |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2029 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2030 | if (PyMethod_Check(func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2031 | && PyMethod_GET_SELF(func) != NULL) { |
| 2032 | PyObject *self = PyMethod_GET_SELF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2033 | Py_INCREF(self); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2034 | func = PyMethod_GET_FUNCTION(func); |
| 2035 | Py_INCREF(func); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2036 | Py_DECREF(*pfunc); |
| 2037 | *pfunc = self; |
| 2038 | na++; |
| 2039 | n++; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2040 | } else |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2041 | Py_INCREF(func); |
| 2042 | x = ext_do_call(func, &stack_pointer, flags, na, nk); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2043 | Py_DECREF(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2044 | |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2045 | while (stack_pointer > pfunc) { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2046 | w = POP(); |
| 2047 | Py_DECREF(w); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2048 | } |
| 2049 | PUSH(x); |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2050 | if (x != NULL) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2051 | continue; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2052 | break; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2053 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2054 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2055 | case MAKE_FUNCTION: |
| 2056 | v = POP(); /* code object */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2057 | x = PyFunction_New(v, f->f_globals); |
| 2058 | Py_DECREF(v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2059 | /* XXX Maybe this should be a separate opcode? */ |
| 2060 | if (x != NULL && oparg > 0) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2061 | v = PyTuple_New(oparg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2062 | if (v == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2063 | Py_DECREF(x); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2064 | x = NULL; |
| 2065 | break; |
| 2066 | } |
| 2067 | while (--oparg >= 0) { |
| 2068 | w = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2069 | PyTuple_SET_ITEM(v, oparg, w); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2070 | } |
| 2071 | err = PyFunction_SetDefaults(x, v); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2072 | Py_DECREF(v); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2073 | } |
| 2074 | PUSH(x); |
| 2075 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2076 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2077 | case MAKE_CLOSURE: |
| 2078 | { |
| 2079 | int nfree; |
| 2080 | v = POP(); /* code object */ |
| 2081 | x = PyFunction_New(v, f->f_globals); |
| 2082 | nfree = PyTuple_GET_SIZE(((PyCodeObject *)v)->co_freevars); |
| 2083 | Py_DECREF(v); |
| 2084 | /* XXX Maybe this should be a separate opcode? */ |
| 2085 | if (x != NULL && nfree > 0) { |
| 2086 | v = PyTuple_New(nfree); |
| 2087 | if (v == NULL) { |
| 2088 | Py_DECREF(x); |
| 2089 | x = NULL; |
| 2090 | break; |
| 2091 | } |
| 2092 | while (--nfree >= 0) { |
| 2093 | w = POP(); |
| 2094 | PyTuple_SET_ITEM(v, nfree, w); |
| 2095 | } |
| 2096 | err = PyFunction_SetClosure(x, v); |
| 2097 | Py_DECREF(v); |
| 2098 | } |
| 2099 | if (x != NULL && oparg > 0) { |
| 2100 | v = PyTuple_New(oparg); |
| 2101 | if (v == NULL) { |
| 2102 | Py_DECREF(x); |
| 2103 | x = NULL; |
| 2104 | break; |
| 2105 | } |
| 2106 | while (--oparg >= 0) { |
| 2107 | w = POP(); |
| 2108 | PyTuple_SET_ITEM(v, oparg, w); |
| 2109 | } |
| 2110 | err = PyFunction_SetDefaults(x, v); |
| 2111 | Py_DECREF(v); |
| 2112 | } |
| 2113 | PUSH(x); |
| 2114 | break; |
| 2115 | } |
| 2116 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2117 | case BUILD_SLICE: |
| 2118 | if (oparg == 3) |
| 2119 | w = POP(); |
| 2120 | else |
| 2121 | w = NULL; |
| 2122 | v = POP(); |
| 2123 | u = POP(); |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 2124 | x = PySlice_New(u, v, w); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2125 | Py_DECREF(u); |
| 2126 | Py_DECREF(v); |
| 2127 | Py_XDECREF(w); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2128 | PUSH(x); |
Guido van Rossum | 3dfd53b | 1997-01-18 02:46:13 +0000 | [diff] [blame] | 2129 | if (x != NULL) continue; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2130 | break; |
| 2131 | |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 2132 | case EXTENDED_ARG: |
| 2133 | opcode = NEXTOP(); |
| 2134 | oparg = oparg<<16 | NEXTARG(); |
| 2135 | goto dispatch_opcode; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2136 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2137 | default: |
| 2138 | fprintf(stderr, |
| 2139 | "XXX lineno: %d, opcode: %d\n", |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2140 | f->f_lineno, opcode); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2141 | PyErr_SetString(PyExc_SystemError, "unknown opcode"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2142 | why = WHY_EXCEPTION; |
| 2143 | break; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2144 | |
| 2145 | #ifdef CASE_TOO_BIG |
| 2146 | } |
| 2147 | #endif |
| 2148 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2149 | } /* switch */ |
| 2150 | |
| 2151 | on_error: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2152 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2153 | /* Quickly continue if no error occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2154 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2155 | if (why == WHY_NOT) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2156 | if (err == 0 && x != NULL) { |
| 2157 | #ifdef CHECKEXC |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2158 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2159 | if (PyErr_Occurred()) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2160 | fprintf(stderr, |
| 2161 | "XXX undetected error\n"); |
| 2162 | else |
| 2163 | #endif |
| 2164 | continue; /* Normal, fast path */ |
| 2165 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2166 | why = WHY_EXCEPTION; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2167 | x = Py_None; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2168 | err = 0; |
| 2169 | } |
| 2170 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2171 | /* Double-check exception status */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2172 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2173 | if (why == WHY_EXCEPTION || why == WHY_RERAISE) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2174 | if (!PyErr_Occurred()) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2175 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2176 | "error return without exception set"); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2177 | why = WHY_EXCEPTION; |
| 2178 | } |
| 2179 | } |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2180 | #ifdef CHECKEXC |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2181 | else { |
Guido van Rossum | eb894eb | 1999-03-09 16:16:45 +0000 | [diff] [blame] | 2182 | /* This check is expensive! */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2183 | if (PyErr_Occurred()) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2184 | fprintf(stderr, |
| 2185 | "XXX undetected error (why=%d)\n", |
| 2186 | why); |
| 2187 | why = WHY_EXCEPTION; |
| 2188 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2189 | } |
| 2190 | #endif |
| 2191 | |
| 2192 | /* Log traceback info if this is a real exception */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2193 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2194 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2195 | f->f_lasti = INSTR_OFFSET() - 1; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2196 | if (HAS_ARG(opcode)) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2197 | f->f_lasti -= 2; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2198 | PyTraceBack_Here(f); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2199 | |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2200 | if (tstate->c_tracefunc != NULL) |
| 2201 | call_exc_trace(tstate->c_tracefunc, |
| 2202 | tstate->c_traceobj, f); |
Guido van Rossum | 014518f | 1998-11-23 21:09:51 +0000 | [diff] [blame] | 2203 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2204 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2205 | /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2206 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2207 | if (why == WHY_RERAISE) |
| 2208 | why = WHY_EXCEPTION; |
| 2209 | |
| 2210 | /* Unwind stacks if a (pseudo) exception occurred */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2211 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2212 | while (why != WHY_NOT && why != WHY_YIELD && f->f_iblock > 0) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2213 | PyTryBlock *b = PyFrame_BlockPop(f); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2214 | |
| 2215 | if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) { |
| 2216 | /* For a continue inside a try block, |
| 2217 | don't pop the block for the loop. */ |
Thomas Wouters | 1ee6422 | 2001-09-24 19:32:01 +0000 | [diff] [blame] | 2218 | PyFrame_BlockSetup(f, b->b_type, b->b_handler, |
| 2219 | b->b_level); |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2220 | why = WHY_NOT; |
| 2221 | JUMPTO(PyInt_AS_LONG(retval)); |
| 2222 | Py_DECREF(retval); |
| 2223 | break; |
| 2224 | } |
| 2225 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2226 | while (STACK_LEVEL() > b->b_level) { |
| 2227 | v = POP(); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2228 | Py_XDECREF(v); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2229 | } |
| 2230 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 2231 | why = WHY_NOT; |
| 2232 | JUMPTO(b->b_handler); |
| 2233 | break; |
| 2234 | } |
| 2235 | if (b->b_type == SETUP_FINALLY || |
Guido van Rossum | 150b2df | 1996-12-05 23:17:11 +0000 | [diff] [blame] | 2236 | (b->b_type == SETUP_EXCEPT && |
| 2237 | why == WHY_EXCEPTION)) { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2238 | if (why == WHY_EXCEPTION) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2239 | PyObject *exc, *val, *tb; |
| 2240 | PyErr_Fetch(&exc, &val, &tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2241 | if (val == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2242 | val = Py_None; |
| 2243 | Py_INCREF(val); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2244 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2245 | /* Make the raw exception data |
| 2246 | available to the handler, |
| 2247 | so a program can emulate the |
| 2248 | Python main loop. Don't do |
| 2249 | this for 'finally'. */ |
| 2250 | if (b->b_type == SETUP_EXCEPT) { |
Barry Warsaw | eaedc7c | 1997-08-28 22:36:40 +0000 | [diff] [blame] | 2251 | PyErr_NormalizeException( |
| 2252 | &exc, &val, &tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2253 | set_exc_info(tstate, |
| 2254 | exc, val, tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2255 | } |
Jeremy Hylton | c631489 | 2001-09-26 19:24:45 +0000 | [diff] [blame] | 2256 | if (tb == NULL) { |
| 2257 | Py_INCREF(Py_None); |
| 2258 | PUSH(Py_None); |
| 2259 | } else |
| 2260 | PUSH(tb); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2261 | PUSH(val); |
| 2262 | PUSH(exc); |
| 2263 | } |
| 2264 | else { |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 2265 | if (why == WHY_RETURN || |
| 2266 | why == CONTINUE_LOOP) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2267 | PUSH(retval); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2268 | v = PyInt_FromLong((long)why); |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2269 | PUSH(v); |
| 2270 | } |
| 2271 | why = WHY_NOT; |
| 2272 | JUMPTO(b->b_handler); |
| 2273 | break; |
| 2274 | } |
| 2275 | } /* unwind stack */ |
| 2276 | |
| 2277 | /* End the loop if we still have an error (or return) */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2278 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2279 | if (why != WHY_NOT) |
| 2280 | break; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2281 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2282 | } /* main loop */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2283 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2284 | if (why != WHY_RETURN && why != WHY_YIELD) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2285 | retval = NULL; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2286 | |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2287 | if (tstate->use_tracing) { |
| 2288 | if (tstate->c_tracefunc |
| 2289 | && (why == WHY_RETURN || why == WHY_YIELD)) { |
| 2290 | if (call_trace(tstate->c_tracefunc, |
| 2291 | tstate->c_traceobj, f, |
| 2292 | PyTrace_RETURN, retval)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2293 | Py_XDECREF(retval); |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2294 | retval = NULL; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2295 | why = WHY_EXCEPTION; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2296 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2297 | } |
Fred Drake | 8f51f54 | 2001-10-04 14:48:42 +0000 | [diff] [blame] | 2298 | if (tstate->c_profilefunc) { |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2299 | if (why == WHY_EXCEPTION) |
| 2300 | call_trace_protected(tstate->c_profilefunc, |
| 2301 | tstate->c_profileobj, f, |
| 2302 | PyTrace_RETURN); |
| 2303 | else if (call_trace(tstate->c_profilefunc, |
| 2304 | tstate->c_profileobj, f, |
| 2305 | PyTrace_RETURN, retval)) { |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2306 | Py_XDECREF(retval); |
| 2307 | retval = NULL; |
| 2308 | why = WHY_EXCEPTION; |
| 2309 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2310 | } |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2311 | } |
Guido van Rossum | a424013 | 1997-01-21 21:18:36 +0000 | [diff] [blame] | 2312 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2313 | reset_exc_info(tstate); |
| 2314 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2315 | /* pop frame */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2316 | --tstate->recursion_depth; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2317 | tstate->frame = f->f_back; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2318 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2319 | return retval; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2322 | PyObject * |
| 2323 | PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2324 | PyObject **args, int argcount, PyObject **kws, int kwcount, |
| 2325 | PyObject **defs, int defcount, PyObject *closure) |
| 2326 | { |
| 2327 | register PyFrameObject *f; |
| 2328 | register PyObject *retval = NULL; |
| 2329 | register PyObject **fastlocals, **freevars; |
| 2330 | PyThreadState *tstate = PyThreadState_GET(); |
| 2331 | PyObject *x, *u; |
| 2332 | |
| 2333 | if (globals == NULL) { |
Jeremy Hylton | 910d7d4 | 2001-08-12 21:52:24 +0000 | [diff] [blame] | 2334 | PyErr_SetString(PyExc_SystemError, |
| 2335 | "PyEval_EvalCodeEx: NULL globals"); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2336 | return NULL; |
| 2337 | } |
| 2338 | |
| 2339 | f = PyFrame_New(tstate, /*back*/ |
| 2340 | co, /*code*/ |
| 2341 | globals, locals); |
| 2342 | if (f == NULL) |
| 2343 | return NULL; |
| 2344 | |
| 2345 | fastlocals = f->f_localsplus; |
| 2346 | freevars = f->f_localsplus + f->f_nlocals; |
| 2347 | |
| 2348 | if (co->co_argcount > 0 || |
| 2349 | co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) { |
| 2350 | int i; |
| 2351 | int n = argcount; |
| 2352 | PyObject *kwdict = NULL; |
| 2353 | if (co->co_flags & CO_VARKEYWORDS) { |
| 2354 | kwdict = PyDict_New(); |
| 2355 | if (kwdict == NULL) |
| 2356 | goto fail; |
| 2357 | i = co->co_argcount; |
| 2358 | if (co->co_flags & CO_VARARGS) |
| 2359 | i++; |
| 2360 | SETLOCAL(i, kwdict); |
| 2361 | } |
| 2362 | if (argcount > co->co_argcount) { |
| 2363 | if (!(co->co_flags & CO_VARARGS)) { |
| 2364 | PyErr_Format(PyExc_TypeError, |
| 2365 | "%.200s() takes %s %d " |
| 2366 | "%sargument%s (%d given)", |
| 2367 | PyString_AsString(co->co_name), |
| 2368 | defcount ? "at most" : "exactly", |
| 2369 | co->co_argcount, |
| 2370 | kwcount ? "non-keyword " : "", |
| 2371 | co->co_argcount == 1 ? "" : "s", |
| 2372 | argcount); |
| 2373 | goto fail; |
| 2374 | } |
| 2375 | n = co->co_argcount; |
| 2376 | } |
| 2377 | for (i = 0; i < n; i++) { |
| 2378 | x = args[i]; |
| 2379 | Py_INCREF(x); |
| 2380 | SETLOCAL(i, x); |
| 2381 | } |
| 2382 | if (co->co_flags & CO_VARARGS) { |
| 2383 | u = PyTuple_New(argcount - n); |
| 2384 | if (u == NULL) |
| 2385 | goto fail; |
| 2386 | SETLOCAL(co->co_argcount, u); |
| 2387 | for (i = n; i < argcount; i++) { |
| 2388 | x = args[i]; |
| 2389 | Py_INCREF(x); |
| 2390 | PyTuple_SET_ITEM(u, i-n, x); |
| 2391 | } |
| 2392 | } |
| 2393 | for (i = 0; i < kwcount; i++) { |
| 2394 | PyObject *keyword = kws[2*i]; |
| 2395 | PyObject *value = kws[2*i + 1]; |
| 2396 | int j; |
| 2397 | if (keyword == NULL || !PyString_Check(keyword)) { |
| 2398 | PyErr_Format(PyExc_TypeError, |
| 2399 | "%.200s() keywords must be strings", |
| 2400 | PyString_AsString(co->co_name)); |
| 2401 | goto fail; |
| 2402 | } |
| 2403 | /* XXX slow -- speed up using dictionary? */ |
| 2404 | for (j = 0; j < co->co_argcount; j++) { |
| 2405 | PyObject *nm = PyTuple_GET_ITEM( |
| 2406 | co->co_varnames, j); |
| 2407 | int cmp = PyObject_RichCompareBool( |
| 2408 | keyword, nm, Py_EQ); |
| 2409 | if (cmp > 0) |
| 2410 | break; |
| 2411 | else if (cmp < 0) |
| 2412 | goto fail; |
| 2413 | } |
| 2414 | /* Check errors from Compare */ |
| 2415 | if (PyErr_Occurred()) |
| 2416 | goto fail; |
| 2417 | if (j >= co->co_argcount) { |
| 2418 | if (kwdict == NULL) { |
| 2419 | PyErr_Format(PyExc_TypeError, |
| 2420 | "%.200s() got an unexpected " |
| 2421 | "keyword argument '%.400s'", |
| 2422 | PyString_AsString(co->co_name), |
| 2423 | PyString_AsString(keyword)); |
| 2424 | goto fail; |
| 2425 | } |
| 2426 | PyDict_SetItem(kwdict, keyword, value); |
| 2427 | } |
| 2428 | else { |
| 2429 | if (GETLOCAL(j) != NULL) { |
| 2430 | PyErr_Format(PyExc_TypeError, |
| 2431 | "%.200s() got multiple " |
| 2432 | "values for keyword " |
| 2433 | "argument '%.400s'", |
| 2434 | PyString_AsString(co->co_name), |
| 2435 | PyString_AsString(keyword)); |
| 2436 | goto fail; |
| 2437 | } |
| 2438 | Py_INCREF(value); |
| 2439 | SETLOCAL(j, value); |
| 2440 | } |
| 2441 | } |
| 2442 | if (argcount < co->co_argcount) { |
| 2443 | int m = co->co_argcount - defcount; |
| 2444 | for (i = argcount; i < m; i++) { |
| 2445 | if (GETLOCAL(i) == NULL) { |
| 2446 | PyErr_Format(PyExc_TypeError, |
| 2447 | "%.200s() takes %s %d " |
| 2448 | "%sargument%s (%d given)", |
| 2449 | PyString_AsString(co->co_name), |
| 2450 | ((co->co_flags & CO_VARARGS) || |
| 2451 | defcount) ? "at least" |
| 2452 | : "exactly", |
| 2453 | m, kwcount ? "non-keyword " : "", |
| 2454 | m == 1 ? "" : "s", i); |
| 2455 | goto fail; |
| 2456 | } |
| 2457 | } |
| 2458 | if (n > m) |
| 2459 | i = n - m; |
| 2460 | else |
| 2461 | i = 0; |
| 2462 | for (; i < defcount; i++) { |
| 2463 | if (GETLOCAL(m+i) == NULL) { |
| 2464 | PyObject *def = defs[i]; |
| 2465 | Py_INCREF(def); |
| 2466 | SETLOCAL(m+i, def); |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | } |
| 2471 | else { |
| 2472 | if (argcount > 0 || kwcount > 0) { |
| 2473 | PyErr_Format(PyExc_TypeError, |
| 2474 | "%.200s() takes no arguments (%d given)", |
| 2475 | PyString_AsString(co->co_name), |
| 2476 | argcount + kwcount); |
| 2477 | goto fail; |
| 2478 | } |
| 2479 | } |
| 2480 | /* Allocate and initialize storage for cell vars, and copy free |
| 2481 | vars into frame. This isn't too efficient right now. */ |
| 2482 | if (f->f_ncells) { |
| 2483 | int i = 0, j = 0, nargs, found; |
| 2484 | char *cellname, *argname; |
| 2485 | PyObject *c; |
| 2486 | |
| 2487 | nargs = co->co_argcount; |
| 2488 | if (co->co_flags & CO_VARARGS) |
| 2489 | nargs++; |
| 2490 | if (co->co_flags & CO_VARKEYWORDS) |
| 2491 | nargs++; |
| 2492 | |
| 2493 | /* Check for cells that shadow args */ |
| 2494 | for (i = 0; i < f->f_ncells && j < nargs; ++i) { |
| 2495 | cellname = PyString_AS_STRING( |
| 2496 | PyTuple_GET_ITEM(co->co_cellvars, i)); |
| 2497 | found = 0; |
| 2498 | while (j < nargs) { |
| 2499 | argname = PyString_AS_STRING( |
| 2500 | PyTuple_GET_ITEM(co->co_varnames, j)); |
| 2501 | if (strcmp(cellname, argname) == 0) { |
| 2502 | c = PyCell_New(GETLOCAL(j)); |
| 2503 | if (c == NULL) |
| 2504 | goto fail; |
| 2505 | GETLOCAL(f->f_nlocals + i) = c; |
| 2506 | found = 1; |
| 2507 | break; |
| 2508 | } |
| 2509 | j++; |
| 2510 | } |
| 2511 | if (found == 0) { |
| 2512 | c = PyCell_New(NULL); |
| 2513 | if (c == NULL) |
| 2514 | goto fail; |
| 2515 | SETLOCAL(f->f_nlocals + i, c); |
| 2516 | } |
| 2517 | } |
| 2518 | /* Initialize any that are left */ |
| 2519 | while (i < f->f_ncells) { |
| 2520 | c = PyCell_New(NULL); |
| 2521 | if (c == NULL) |
| 2522 | goto fail; |
| 2523 | SETLOCAL(f->f_nlocals + i, c); |
| 2524 | i++; |
| 2525 | } |
| 2526 | } |
| 2527 | if (f->f_nfreevars) { |
| 2528 | int i; |
| 2529 | for (i = 0; i < f->f_nfreevars; ++i) { |
| 2530 | PyObject *o = PyTuple_GET_ITEM(closure, i); |
| 2531 | Py_INCREF(o); |
| 2532 | freevars[f->f_ncells + i] = o; |
| 2533 | } |
| 2534 | } |
| 2535 | |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2536 | if (co->co_flags & CO_GENERATOR) { |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 2537 | /* Don't need to keep the reference to f_back, it will be set |
| 2538 | * when the generator is resumed. */ |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 2539 | Py_XDECREF(f->f_back); |
Neil Schemenauer | 2b13ce8 | 2001-06-21 02:41:10 +0000 | [diff] [blame] | 2540 | f->f_back = NULL; |
| 2541 | |
| 2542 | /* Create a new generator that owns the ready to run frame |
| 2543 | * and return that as the value. */ |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2544 | return gen_new(f); |
| 2545 | } |
| 2546 | |
| 2547 | retval = eval_frame(f); |
| 2548 | |
| 2549 | fail: /* Jump here from prelude on failure */ |
| 2550 | |
| 2551 | Py_DECREF(f); |
| 2552 | return retval; |
| 2553 | } |
| 2554 | |
| 2555 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2556 | static void |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2557 | set_exc_info(PyThreadState *tstate, |
| 2558 | PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2559 | { |
| 2560 | PyFrameObject *frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2561 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 2562 | |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2563 | frame = tstate->frame; |
| 2564 | if (frame->f_exc_type == NULL) { |
| 2565 | /* This frame didn't catch an exception before */ |
| 2566 | /* Save previous exception of this thread in this frame */ |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2567 | if (tstate->exc_type == NULL) { |
| 2568 | Py_INCREF(Py_None); |
| 2569 | tstate->exc_type = Py_None; |
| 2570 | } |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2571 | tmp_type = frame->f_exc_type; |
| 2572 | tmp_value = frame->f_exc_value; |
| 2573 | tmp_tb = frame->f_exc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2574 | Py_XINCREF(tstate->exc_type); |
| 2575 | Py_XINCREF(tstate->exc_value); |
| 2576 | Py_XINCREF(tstate->exc_traceback); |
| 2577 | frame->f_exc_type = tstate->exc_type; |
| 2578 | frame->f_exc_value = tstate->exc_value; |
| 2579 | frame->f_exc_traceback = tstate->exc_traceback; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2580 | Py_XDECREF(tmp_type); |
| 2581 | Py_XDECREF(tmp_value); |
| 2582 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2583 | } |
| 2584 | /* Set new exception for this thread */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2585 | tmp_type = tstate->exc_type; |
| 2586 | tmp_value = tstate->exc_value; |
| 2587 | tmp_tb = tstate->exc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2588 | Py_XINCREF(type); |
| 2589 | Py_XINCREF(value); |
| 2590 | Py_XINCREF(tb); |
| 2591 | tstate->exc_type = type; |
| 2592 | tstate->exc_value = value; |
| 2593 | tstate->exc_traceback = tb; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2594 | Py_XDECREF(tmp_type); |
| 2595 | Py_XDECREF(tmp_value); |
| 2596 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2597 | /* For b/w compatibility */ |
| 2598 | PySys_SetObject("exc_type", type); |
| 2599 | PySys_SetObject("exc_value", value); |
| 2600 | PySys_SetObject("exc_traceback", tb); |
| 2601 | } |
| 2602 | |
| 2603 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2604 | reset_exc_info(PyThreadState *tstate) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2605 | { |
| 2606 | PyFrameObject *frame; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2607 | PyObject *tmp_type, *tmp_value, *tmp_tb; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2608 | frame = tstate->frame; |
| 2609 | if (frame->f_exc_type != NULL) { |
| 2610 | /* This frame caught an exception */ |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2611 | tmp_type = tstate->exc_type; |
| 2612 | tmp_value = tstate->exc_value; |
| 2613 | tmp_tb = tstate->exc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2614 | Py_XINCREF(frame->f_exc_type); |
| 2615 | Py_XINCREF(frame->f_exc_value); |
| 2616 | Py_XINCREF(frame->f_exc_traceback); |
| 2617 | tstate->exc_type = frame->f_exc_type; |
| 2618 | tstate->exc_value = frame->f_exc_value; |
| 2619 | tstate->exc_traceback = frame->f_exc_traceback; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2620 | Py_XDECREF(tmp_type); |
| 2621 | Py_XDECREF(tmp_value); |
| 2622 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2623 | /* For b/w compatibility */ |
| 2624 | PySys_SetObject("exc_type", frame->f_exc_type); |
| 2625 | PySys_SetObject("exc_value", frame->f_exc_value); |
| 2626 | PySys_SetObject("exc_traceback", frame->f_exc_traceback); |
| 2627 | } |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2628 | tmp_type = frame->f_exc_type; |
| 2629 | tmp_value = frame->f_exc_value; |
| 2630 | tmp_tb = frame->f_exc_traceback; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2631 | frame->f_exc_type = NULL; |
| 2632 | frame->f_exc_value = NULL; |
| 2633 | frame->f_exc_traceback = NULL; |
Guido van Rossum | df4c308 | 1997-05-20 17:06:11 +0000 | [diff] [blame] | 2634 | Py_XDECREF(tmp_type); |
| 2635 | Py_XDECREF(tmp_value); |
| 2636 | Py_XDECREF(tmp_tb); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2639 | /* Logic for the raise statement (too complicated for inlining). |
| 2640 | This *consumes* a reference count to each of its arguments. */ |
Guido van Rossum | 1aa1483 | 1997-01-21 05:34:20 +0000 | [diff] [blame] | 2641 | static enum why_code |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2642 | do_raise(PyObject *type, PyObject *value, PyObject *tb) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2643 | { |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2644 | if (type == NULL) { |
| 2645 | /* Reraise */ |
| 2646 | PyThreadState *tstate = PyThreadState_Get(); |
| 2647 | type = tstate->exc_type == NULL ? Py_None : tstate->exc_type; |
| 2648 | value = tstate->exc_value; |
| 2649 | tb = tstate->exc_traceback; |
| 2650 | Py_XINCREF(type); |
| 2651 | Py_XINCREF(value); |
| 2652 | Py_XINCREF(tb); |
| 2653 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2654 | |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2655 | /* We support the following forms of raise: |
| 2656 | raise <class>, <classinstance> |
| 2657 | raise <class>, <argument tuple> |
| 2658 | raise <class>, None |
| 2659 | raise <class>, <argument> |
| 2660 | raise <classinstance>, None |
| 2661 | raise <string>, <object> |
| 2662 | raise <string>, None |
| 2663 | |
| 2664 | An omitted second argument is the same as None. |
| 2665 | |
| 2666 | In addition, raise <tuple>, <anything> is the same as |
| 2667 | raising the tuple's first item (and it better have one!); |
| 2668 | this rule is applied recursively. |
| 2669 | |
| 2670 | Finally, an optional third argument can be supplied, which |
| 2671 | gives the traceback to be substituted (useful when |
| 2672 | re-raising an exception after examining it). */ |
| 2673 | |
| 2674 | /* First, check the traceback argument, replacing None with |
| 2675 | NULL. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2676 | if (tb == Py_None) { |
| 2677 | Py_DECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2678 | tb = NULL; |
| 2679 | } |
| 2680 | else if (tb != NULL && !PyTraceBack_Check(tb)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2681 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2682 | "raise: arg 3 must be a traceback or None"); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2683 | goto raise_error; |
| 2684 | } |
| 2685 | |
| 2686 | /* Next, replace a missing value with None */ |
| 2687 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2688 | value = Py_None; |
| 2689 | Py_INCREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | /* Next, repeatedly, replace a tuple exception with its first item */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2693 | while (PyTuple_Check(type) && PyTuple_Size(type) > 0) { |
| 2694 | PyObject *tmp = type; |
| 2695 | type = PyTuple_GET_ITEM(type, 0); |
| 2696 | Py_INCREF(type); |
| 2697 | Py_DECREF(tmp); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 2700 | if (PyString_Check(type)) |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2701 | ; |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 2702 | |
| 2703 | else if (PyClass_Check(type)) |
| 2704 | PyErr_NormalizeException(&type, &value, &tb); |
| 2705 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2706 | else if (PyInstance_Check(type)) { |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2707 | /* Raising an instance. The value should be a dummy. */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2708 | if (value != Py_None) { |
| 2709 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2710 | "instance exception may not have a separate value"); |
| 2711 | goto raise_error; |
| 2712 | } |
| 2713 | else { |
| 2714 | /* Normalize to raise <class>, <instance> */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2715 | Py_DECREF(value); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2716 | value = type; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2717 | type = (PyObject*) ((PyInstanceObject*)type)->in_class; |
| 2718 | Py_INCREF(type); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | else { |
| 2722 | /* Not something you can raise. You get an exception |
| 2723 | anyway, just not what you specified :-) */ |
Jeremy Hylton | 960d948 | 2001-04-27 02:25:33 +0000 | [diff] [blame] | 2724 | PyErr_Format(PyExc_TypeError, |
| 2725 | "exceptions must be strings, classes, or " |
| 2726 | "instances, not %s", type->ob_type->tp_name); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2727 | goto raise_error; |
| 2728 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2729 | PyErr_Restore(type, value, tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2730 | if (tb == NULL) |
| 2731 | return WHY_EXCEPTION; |
| 2732 | else |
| 2733 | return WHY_RERAISE; |
| 2734 | raise_error: |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2735 | Py_XDECREF(value); |
| 2736 | Py_XDECREF(type); |
| 2737 | Py_XDECREF(tb); |
Guido van Rossum | 0aa9ee6 | 1996-12-10 18:07:35 +0000 | [diff] [blame] | 2738 | return WHY_EXCEPTION; |
| 2739 | } |
| 2740 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 2741 | /* Iterate v argcnt times and store the results on the stack (via decreasing |
| 2742 | sp). Return 1 for success, 0 if error. */ |
| 2743 | |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 2744 | static int |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 2745 | unpack_iterable(PyObject *v, int argcnt, PyObject **sp) |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 2746 | { |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 2747 | int i = 0; |
| 2748 | PyObject *it; /* iter(v) */ |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 2749 | PyObject *w; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 2750 | |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 2751 | assert(v != NULL); |
| 2752 | |
| 2753 | it = PyObject_GetIter(v); |
| 2754 | if (it == NULL) |
| 2755 | goto Error; |
| 2756 | |
| 2757 | for (; i < argcnt; i++) { |
| 2758 | w = PyIter_Next(it); |
| 2759 | if (w == NULL) { |
| 2760 | /* Iterator done, via error or exhaustion. */ |
| 2761 | if (!PyErr_Occurred()) { |
| 2762 | PyErr_Format(PyExc_ValueError, |
| 2763 | "need more than %d value%s to unpack", |
| 2764 | i, i == 1 ? "" : "s"); |
| 2765 | } |
| 2766 | goto Error; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 2767 | } |
| 2768 | *--sp = w; |
| 2769 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 2770 | |
| 2771 | /* We better have exhausted the iterator now. */ |
| 2772 | w = PyIter_Next(it); |
| 2773 | if (w == NULL) { |
| 2774 | if (PyErr_Occurred()) |
| 2775 | goto Error; |
| 2776 | Py_DECREF(it); |
| 2777 | return 1; |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 2778 | } |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 2779 | PyErr_SetString(PyExc_ValueError, "too many values to unpack"); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 2780 | /* fall through */ |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 2781 | Error: |
Barry Warsaw | 9101055 | 1997-08-25 22:30:51 +0000 | [diff] [blame] | 2782 | for (; i > 0; i--, sp++) |
| 2783 | Py_DECREF(*sp); |
Tim Peters | d6d010b | 2001-06-21 02:49:55 +0000 | [diff] [blame] | 2784 | Py_XDECREF(it); |
Barry Warsaw | e42b18f | 1997-08-25 22:13:04 +0000 | [diff] [blame] | 2785 | return 0; |
| 2786 | } |
| 2787 | |
| 2788 | |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2789 | #ifdef LLTRACE |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2790 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2791 | prtrace(PyObject *v, char *str) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2792 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2793 | printf("%s ", str); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2794 | if (PyObject_Print(v, stdout, 0) != 0) |
| 2795 | PyErr_Clear(); /* Don't know what else to do */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2796 | printf("\n"); |
Guido van Rossum | cc229ea | 2000-05-04 00:55:17 +0000 | [diff] [blame] | 2797 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2798 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2799 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2800 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2801 | static void |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2802 | call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2803 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2804 | PyObject *type, *value, *traceback, *arg; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2805 | int err; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2806 | PyErr_Fetch(&type, &value, &traceback); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 2807 | if (value == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2808 | value = Py_None; |
| 2809 | Py_INCREF(value); |
Guido van Rossum | bd9ccca | 1992-04-09 14:58:08 +0000 | [diff] [blame] | 2810 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2811 | arg = Py_BuildValue("(OOO)", type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2812 | if (arg == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2813 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2814 | return; |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2815 | } |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2816 | err = call_trace(func, self, f, PyTrace_EXCEPTION, arg); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2817 | Py_DECREF(arg); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2818 | if (err == 0) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2819 | PyErr_Restore(type, value, traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2820 | else { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2821 | Py_XDECREF(type); |
| 2822 | Py_XDECREF(value); |
| 2823 | Py_XDECREF(traceback); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2824 | } |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Fred Drake | 4ec5d56 | 2001-10-04 19:26:43 +0000 | [diff] [blame] | 2827 | static void |
| 2828 | call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
| 2829 | int what) |
| 2830 | { |
| 2831 | PyObject *type, *value, *traceback; |
| 2832 | int err; |
| 2833 | PyErr_Fetch(&type, &value, &traceback); |
| 2834 | err = call_trace(func, obj, frame, what, NULL); |
| 2835 | if (err == 0) |
| 2836 | PyErr_Restore(type, value, traceback); |
| 2837 | else { |
| 2838 | Py_XDECREF(type); |
| 2839 | Py_XDECREF(value); |
| 2840 | Py_XDECREF(traceback); |
| 2841 | } |
| 2842 | } |
| 2843 | |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2844 | static int |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2845 | call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame, |
| 2846 | int what, PyObject *arg) |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2847 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2848 | register PyThreadState *tstate = frame->f_tstate; |
| 2849 | int result; |
| 2850 | if (tstate->tracing) |
Guido van Rossum | 9c8d70d | 1992-03-23 18:19:28 +0000 | [diff] [blame] | 2851 | return 0; |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2852 | tstate->tracing++; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2853 | tstate->use_tracing = 0; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2854 | result = func(obj, frame, what, arg); |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2855 | tstate->use_tracing = ((tstate->c_tracefunc != NULL) |
| 2856 | || (tstate->c_profilefunc != NULL)); |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2857 | tstate->tracing--; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2858 | return result; |
Guido van Rossum | 96a42c8 | 1992-01-12 02:29:51 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2861 | void |
| 2862 | PyEval_SetProfile(Py_tracefunc func, PyObject *arg) |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 2863 | { |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2864 | PyThreadState *tstate = PyThreadState_Get(); |
| 2865 | PyObject *temp = tstate->c_profileobj; |
| 2866 | Py_XINCREF(arg); |
| 2867 | tstate->c_profilefunc = NULL; |
| 2868 | tstate->c_profileobj = NULL; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2869 | tstate->use_tracing = tstate->c_tracefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2870 | Py_XDECREF(temp); |
| 2871 | tstate->c_profilefunc = func; |
| 2872 | tstate->c_profileobj = arg; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2873 | tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL); |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | void |
| 2877 | PyEval_SetTrace(Py_tracefunc func, PyObject *arg) |
| 2878 | { |
| 2879 | PyThreadState *tstate = PyThreadState_Get(); |
| 2880 | PyObject *temp = tstate->c_traceobj; |
| 2881 | Py_XINCREF(arg); |
| 2882 | tstate->c_tracefunc = NULL; |
| 2883 | tstate->c_traceobj = NULL; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2884 | tstate->use_tracing = tstate->c_profilefunc != NULL; |
Fred Drake | 5755ce6 | 2001-06-27 19:19:46 +0000 | [diff] [blame] | 2885 | Py_XDECREF(temp); |
| 2886 | tstate->c_tracefunc = func; |
| 2887 | tstate->c_traceobj = arg; |
Fred Drake | 9e3ad78 | 2001-07-03 23:39:52 +0000 | [diff] [blame] | 2888 | tstate->use_tracing = ((func != NULL) |
| 2889 | || (tstate->c_profilefunc != NULL)); |
Fred Drake | d083839 | 2001-06-16 21:02:31 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2892 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2893 | PyEval_GetBuiltins(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 2894 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2895 | PyThreadState *tstate = PyThreadState_Get(); |
| 2896 | PyFrameObject *current_frame = tstate->frame; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 2897 | if (current_frame == NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2898 | return tstate->interp->builtins; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 2899 | else |
| 2900 | return current_frame->f_builtins; |
| 2901 | } |
| 2902 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2903 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2904 | PyEval_GetLocals(void) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 2905 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2906 | PyFrameObject *current_frame = PyThreadState_Get()->frame; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 2907 | if (current_frame == NULL) |
| 2908 | return NULL; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2909 | PyFrame_FastToLocals(current_frame); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 2910 | return current_frame->f_locals; |
| 2911 | } |
| 2912 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2913 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2914 | PyEval_GetGlobals(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2915 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2916 | PyFrameObject *current_frame = PyThreadState_Get()->frame; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2917 | if (current_frame == NULL) |
| 2918 | return NULL; |
| 2919 | else |
| 2920 | return current_frame->f_globals; |
| 2921 | } |
| 2922 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2923 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2924 | PyEval_GetFrame(void) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 2925 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2926 | PyFrameObject *current_frame = PyThreadState_Get()->frame; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2927 | return (PyObject *)current_frame; |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 2930 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2931 | PyEval_GetRestricted(void) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 2932 | { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2933 | PyFrameObject *current_frame = PyThreadState_Get()->frame; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 2934 | return current_frame == NULL ? 0 : current_frame->f_restricted; |
| 2935 | } |
| 2936 | |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 2937 | int |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 2938 | PyEval_MergeCompilerFlags(PyCompilerFlags *cf) |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 2939 | { |
| 2940 | PyFrameObject *current_frame = PyThreadState_Get()->frame; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 2941 | int result = 0; |
| 2942 | |
| 2943 | if (current_frame != NULL) { |
| 2944 | const int codeflags = current_frame->f_code->co_flags; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 2945 | const int compilerflags = codeflags & PyCF_MASK; |
| 2946 | if (compilerflags) { |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 2947 | result = 1; |
Tim Peters | e2c18e9 | 2001-08-17 20:47:47 +0000 | [diff] [blame] | 2948 | cf->cf_flags |= compilerflags; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 2949 | } |
| 2950 | } |
| 2951 | return result; |
Jeremy Hylton | 061d106 | 2001-03-22 02:32:48 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2955 | Py_FlushLine(void) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2956 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2957 | PyObject *f = PySys_GetObject("stdout"); |
Guido van Rossum | be27026 | 1997-05-22 22:26:18 +0000 | [diff] [blame] | 2958 | if (f == NULL) |
| 2959 | return 0; |
| 2960 | if (!PyFile_SoftSpace(f, 0)) |
| 2961 | return 0; |
| 2962 | return PyFile_WriteString("\n", f); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2965 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2966 | /* External interface to call any callable object. |
| 2967 | The arg must be a tuple or NULL. */ |
Guido van Rossum | 83bf35c | 1991-07-27 21:32:34 +0000 | [diff] [blame] | 2968 | |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 2969 | #undef PyEval_CallObject |
| 2970 | /* for backward compatibility: export this interface */ |
| 2971 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2972 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2973 | PyEval_CallObject(PyObject *func, PyObject *arg) |
Guido van Rossum | 83bf35c | 1991-07-27 21:32:34 +0000 | [diff] [blame] | 2974 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2975 | return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2976 | } |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 2977 | #define PyEval_CallObject(func,arg) \ |
| 2978 | PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL) |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 2979 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2980 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2981 | PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2982 | { |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 2983 | PyObject *result; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2984 | |
| 2985 | if (arg == NULL) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2986 | arg = PyTuple_New(0); |
| 2987 | else if (!PyTuple_Check(arg)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2988 | PyErr_SetString(PyExc_TypeError, |
| 2989 | "argument list must be a tuple"); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2990 | return NULL; |
| 2991 | } |
| 2992 | else |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2993 | Py_INCREF(arg); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2994 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 2995 | if (kw != NULL && !PyDict_Check(kw)) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 2996 | PyErr_SetString(PyExc_TypeError, |
| 2997 | "keyword list must be a dictionary"); |
Guido van Rossum | 25826c9 | 2000-04-21 21:17:39 +0000 | [diff] [blame] | 2998 | Py_DECREF(arg); |
Guido van Rossum | e3e61c1 | 1995-08-04 04:14:47 +0000 | [diff] [blame] | 2999 | return NULL; |
| 3000 | } |
| 3001 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3002 | result = PyObject_Call(func, arg, kw); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3003 | Py_DECREF(arg); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3004 | return result; |
| 3005 | } |
| 3006 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3007 | char * |
| 3008 | PyEval_GetFuncName(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3009 | { |
| 3010 | if (PyMethod_Check(func)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3011 | return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func)); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3012 | else if (PyFunction_Check(func)) |
| 3013 | return PyString_AsString(((PyFunctionObject*)func)->func_name); |
| 3014 | else if (PyCFunction_Check(func)) |
| 3015 | return ((PyCFunctionObject*)func)->m_ml->ml_name; |
| 3016 | else if (PyClass_Check(func)) |
| 3017 | return PyString_AsString(((PyClassObject*)func)->cl_name); |
| 3018 | else if (PyInstance_Check(func)) { |
| 3019 | return PyString_AsString( |
| 3020 | ((PyInstanceObject*)func)->in_class->cl_name); |
| 3021 | } else { |
| 3022 | return func->ob_type->tp_name; |
| 3023 | } |
| 3024 | } |
| 3025 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3026 | char * |
| 3027 | PyEval_GetFuncDesc(PyObject *func) |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3028 | { |
| 3029 | if (PyMethod_Check(func)) |
| 3030 | return "()"; |
| 3031 | else if (PyFunction_Check(func)) |
| 3032 | return "()"; |
| 3033 | else if (PyCFunction_Check(func)) |
| 3034 | return "()"; |
| 3035 | else if (PyClass_Check(func)) |
| 3036 | return " constructor"; |
| 3037 | else if (PyInstance_Check(func)) { |
| 3038 | return " instance"; |
| 3039 | } else { |
| 3040 | return " object"; |
| 3041 | } |
| 3042 | } |
| 3043 | |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3044 | #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER)) |
| 3045 | |
| 3046 | /* The two fast_xxx() functions optimize calls for which no argument |
| 3047 | tuple is necessary; the objects are passed directly from the stack. |
| 3048 | fast_cfunction() is called for METH_OLDARGS functions. |
| 3049 | fast_function() is for functions with no special argument handling. |
| 3050 | */ |
| 3051 | |
| 3052 | static PyObject * |
| 3053 | fast_cfunction(PyObject *func, PyObject ***pp_stack, int na) |
| 3054 | { |
| 3055 | PyCFunction meth = PyCFunction_GET_FUNCTION(func); |
| 3056 | PyObject *self = PyCFunction_GET_SELF(func); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3057 | int flags = PyCFunction_GET_FLAGS(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3058 | |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3059 | switch (flags) { |
| 3060 | case METH_OLDARGS: |
| 3061 | if (na == 0) |
| 3062 | return (*meth)(self, NULL); |
| 3063 | else if (na == 1) { |
| 3064 | PyObject *arg = EXT_POP(*pp_stack); |
| 3065 | PyObject *result = (*meth)(self, arg); |
| 3066 | Py_DECREF(arg); |
| 3067 | return result; |
| 3068 | } else { |
| 3069 | PyObject *args = load_args(pp_stack, na); |
| 3070 | PyObject *result = (*meth)(self, args); |
| 3071 | Py_DECREF(args); |
| 3072 | return result; |
| 3073 | } |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3074 | case METH_NOARGS: |
| 3075 | if (na == 0) |
| 3076 | return (*meth)(self, NULL); |
| 3077 | PyErr_Format(PyExc_TypeError, |
| 3078 | "%.200s() takes no arguments (%d given)", |
| 3079 | ((PyCFunctionObject*)func)->m_ml->ml_name, na); |
| 3080 | return NULL; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3081 | case METH_O: |
| 3082 | if (na == 1) { |
| 3083 | PyObject *arg = EXT_POP(*pp_stack); |
| 3084 | PyObject *result = (*meth)(self, arg); |
| 3085 | Py_DECREF(arg); |
| 3086 | return result; |
| 3087 | } |
| 3088 | PyErr_Format(PyExc_TypeError, |
| 3089 | "%.200s() takes exactly one argument (%d given)", |
| 3090 | ((PyCFunctionObject*)func)->m_ml->ml_name, na); |
| 3091 | return NULL; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3092 | default: |
| 3093 | fprintf(stderr, "%.200s() flags = %d\n", |
| 3094 | ((PyCFunctionObject*)func)->m_ml->ml_name, flags); |
| 3095 | PyErr_BadInternalCall(); |
| 3096 | return NULL; |
| 3097 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | static PyObject * |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3101 | fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3102 | { |
| 3103 | PyObject *co = PyFunction_GET_CODE(func); |
| 3104 | PyObject *globals = PyFunction_GET_GLOBALS(func); |
| 3105 | PyObject *argdefs = PyFunction_GET_DEFAULTS(func); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3106 | PyObject *closure = PyFunction_GET_CLOSURE(func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3107 | PyObject **d = NULL; |
| 3108 | int nd = 0; |
| 3109 | |
| 3110 | if (argdefs != NULL) { |
| 3111 | d = &PyTuple_GET_ITEM(argdefs, 0); |
| 3112 | nd = ((PyTupleObject *)argdefs)->ob_size; |
| 3113 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3114 | return PyEval_EvalCodeEx((PyCodeObject *)co, globals, |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3115 | (PyObject *)NULL, (*pp_stack)-n, na, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3116 | (*pp_stack)-2*nk, nk, d, nd, |
| 3117 | closure); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | static PyObject * |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3121 | update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack, |
| 3122 | PyObject *func) |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3123 | { |
| 3124 | PyObject *kwdict = NULL; |
| 3125 | if (orig_kwdict == NULL) |
| 3126 | kwdict = PyDict_New(); |
| 3127 | else { |
| 3128 | kwdict = PyDict_Copy(orig_kwdict); |
| 3129 | Py_DECREF(orig_kwdict); |
| 3130 | } |
| 3131 | if (kwdict == NULL) |
| 3132 | return NULL; |
| 3133 | while (--nk >= 0) { |
| 3134 | int err; |
| 3135 | PyObject *value = EXT_POP(*pp_stack); |
| 3136 | PyObject *key = EXT_POP(*pp_stack); |
| 3137 | if (PyDict_GetItem(kwdict, key) != NULL) { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3138 | PyErr_Format(PyExc_TypeError, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3139 | "%.200s%s got multiple values " |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3140 | "for keyword argument '%.200s'", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3141 | PyEval_GetFuncName(func), |
| 3142 | PyEval_GetFuncDesc(func), |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3143 | PyString_AsString(key)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3144 | Py_DECREF(key); |
| 3145 | Py_DECREF(value); |
| 3146 | Py_DECREF(kwdict); |
| 3147 | return NULL; |
| 3148 | } |
| 3149 | err = PyDict_SetItem(kwdict, key, value); |
| 3150 | Py_DECREF(key); |
| 3151 | Py_DECREF(value); |
| 3152 | if (err) { |
| 3153 | Py_DECREF(kwdict); |
| 3154 | return NULL; |
| 3155 | } |
| 3156 | } |
| 3157 | return kwdict; |
| 3158 | } |
| 3159 | |
| 3160 | static PyObject * |
| 3161 | update_star_args(int nstack, int nstar, PyObject *stararg, |
| 3162 | PyObject ***pp_stack) |
| 3163 | { |
| 3164 | PyObject *callargs, *w; |
| 3165 | |
| 3166 | callargs = PyTuple_New(nstack + nstar); |
| 3167 | if (callargs == NULL) { |
| 3168 | return NULL; |
| 3169 | } |
| 3170 | if (nstar) { |
| 3171 | int i; |
| 3172 | for (i = 0; i < nstar; i++) { |
| 3173 | PyObject *a = PyTuple_GET_ITEM(stararg, i); |
| 3174 | Py_INCREF(a); |
| 3175 | PyTuple_SET_ITEM(callargs, nstack + i, a); |
| 3176 | } |
| 3177 | } |
| 3178 | while (--nstack >= 0) { |
| 3179 | w = EXT_POP(*pp_stack); |
| 3180 | PyTuple_SET_ITEM(callargs, nstack, w); |
| 3181 | } |
| 3182 | return callargs; |
| 3183 | } |
| 3184 | |
| 3185 | static PyObject * |
| 3186 | load_args(PyObject ***pp_stack, int na) |
| 3187 | { |
| 3188 | PyObject *args = PyTuple_New(na); |
| 3189 | PyObject *w; |
| 3190 | |
| 3191 | if (args == NULL) |
| 3192 | return NULL; |
| 3193 | while (--na >= 0) { |
| 3194 | w = EXT_POP(*pp_stack); |
| 3195 | PyTuple_SET_ITEM(args, na, w); |
| 3196 | } |
| 3197 | return args; |
| 3198 | } |
| 3199 | |
| 3200 | static PyObject * |
| 3201 | do_call(PyObject *func, PyObject ***pp_stack, int na, int nk) |
| 3202 | { |
| 3203 | PyObject *callargs = NULL; |
| 3204 | PyObject *kwdict = NULL; |
| 3205 | PyObject *result = NULL; |
| 3206 | |
| 3207 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3208 | kwdict = update_keyword_args(NULL, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3209 | if (kwdict == NULL) |
| 3210 | goto call_fail; |
| 3211 | } |
| 3212 | callargs = load_args(pp_stack, na); |
| 3213 | if (callargs == NULL) |
| 3214 | goto call_fail; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3215 | result = PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3216 | call_fail: |
| 3217 | Py_XDECREF(callargs); |
| 3218 | Py_XDECREF(kwdict); |
| 3219 | return result; |
| 3220 | } |
| 3221 | |
| 3222 | static PyObject * |
| 3223 | ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk) |
| 3224 | { |
| 3225 | int nstar = 0; |
| 3226 | PyObject *callargs = NULL; |
| 3227 | PyObject *stararg = NULL; |
| 3228 | PyObject *kwdict = NULL; |
| 3229 | PyObject *result = NULL; |
| 3230 | |
| 3231 | if (flags & CALL_FLAG_KW) { |
| 3232 | kwdict = EXT_POP(*pp_stack); |
| 3233 | if (!(kwdict && PyDict_Check(kwdict))) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3234 | PyErr_Format(PyExc_TypeError, |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3235 | "%s%s argument after ** " |
| 3236 | "must be a dictionary", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3237 | PyEval_GetFuncName(func), |
| 3238 | PyEval_GetFuncDesc(func)); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3239 | goto ext_call_fail; |
| 3240 | } |
| 3241 | } |
| 3242 | if (flags & CALL_FLAG_VAR) { |
| 3243 | stararg = EXT_POP(*pp_stack); |
| 3244 | if (!PyTuple_Check(stararg)) { |
| 3245 | PyObject *t = NULL; |
| 3246 | t = PySequence_Tuple(stararg); |
| 3247 | if (t == NULL) { |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3248 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
| 3249 | PyErr_Format(PyExc_TypeError, |
| 3250 | "%s%s argument after * " |
| 3251 | "must be a sequence", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3252 | PyEval_GetFuncName(func), |
| 3253 | PyEval_GetFuncDesc(func)); |
Jeremy Hylton | 512a237 | 2001-04-11 13:52:29 +0000 | [diff] [blame] | 3254 | } |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3255 | goto ext_call_fail; |
| 3256 | } |
| 3257 | Py_DECREF(stararg); |
| 3258 | stararg = t; |
| 3259 | } |
| 3260 | nstar = PyTuple_GET_SIZE(stararg); |
| 3261 | } |
| 3262 | if (nk > 0) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 3263 | kwdict = update_keyword_args(kwdict, nk, pp_stack, func); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3264 | if (kwdict == NULL) |
| 3265 | goto ext_call_fail; |
| 3266 | } |
| 3267 | callargs = update_star_args(na, nstar, stararg, pp_stack); |
| 3268 | if (callargs == NULL) |
| 3269 | goto ext_call_fail; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3270 | result = PyObject_Call(func, callargs, kwdict); |
Jeremy Hylton | 5282044 | 2001-01-03 23:52:36 +0000 | [diff] [blame] | 3271 | ext_call_fail: |
| 3272 | Py_XDECREF(callargs); |
| 3273 | Py_XDECREF(kwdict); |
| 3274 | Py_XDECREF(stararg); |
| 3275 | return result; |
| 3276 | } |
| 3277 | |
Guido van Rossum | 3b9c667 | 1996-07-30 18:40:29 +0000 | [diff] [blame] | 3278 | #define SLICE_ERROR_MSG \ |
| 3279 | "standard sequence type does not support step size other than one" |
| 3280 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3281 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3282 | loop_subscript(PyObject *v, PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3283 | { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3284 | PySequenceMethods *sq = v->ob_type->tp_as_sequence; |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3285 | int i; |
Guido van Rossum | 7e33c6e | 1998-05-22 00:52:29 +0000 | [diff] [blame] | 3286 | if (sq == NULL || sq->sq_item == NULL) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3287 | PyErr_SetString(PyExc_TypeError, "loop over non-sequence"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3288 | return NULL; |
| 3289 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3290 | i = PyInt_AsLong(w); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3291 | v = (*sq->sq_item)(v, i); |
| 3292 | if (v) |
| 3293 | return v; |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3294 | if (PyErr_ExceptionMatches(PyExc_IndexError)) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3295 | PyErr_Clear(); |
Guido van Rossum | e59214e | 1994-08-30 08:01:59 +0000 | [diff] [blame] | 3296 | return NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 3299 | /* Extract a slice index from a PyInt or PyLong, the index is bound to |
| 3300 | the range [-INT_MAX+1, INTMAX]. Returns 0 and an exception if there is |
| 3301 | and error. Returns 1 on success.*/ |
| 3302 | |
| 3303 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3304 | _PyEval_SliceIndex(PyObject *v, int *pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3305 | { |
| 3306 | if (v != NULL) { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3307 | long x; |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 3308 | if (PyInt_Check(v)) { |
| 3309 | x = PyInt_AsLong(v); |
| 3310 | } else if (PyLong_Check(v)) { |
| 3311 | x = PyLong_AsLong(v); |
| 3312 | if (x==-1 && PyErr_Occurred()) { |
| 3313 | PyObject *long_zero; |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3314 | int cmp; |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 3315 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3316 | if (!PyErr_ExceptionMatches( |
| 3317 | PyExc_OverflowError)) { |
| 3318 | /* It's not an overflow error, so just |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 3319 | signal an error */ |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 3320 | return 0; |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 3321 | } |
| 3322 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3323 | /* Clear the OverflowError */ |
| 3324 | PyErr_Clear(); |
| 3325 | |
| 3326 | /* It's an overflow error, so we need to |
| 3327 | check the sign of the long integer, |
| 3328 | set the value to INT_MAX or 0, and clear |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 3329 | the error. */ |
| 3330 | |
| 3331 | /* Create a long integer with a value of 0 */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3332 | long_zero = PyLong_FromLong(0L); |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 3333 | if (long_zero == NULL) return 0; |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 3334 | |
| 3335 | /* Check sign */ |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3336 | cmp = PyObject_RichCompareBool(v, long_zero, |
| 3337 | Py_GT); |
| 3338 | Py_DECREF(long_zero); |
| 3339 | if (cmp < 0) |
| 3340 | return 0; |
| 3341 | else if (cmp > 0) |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 3342 | x = INT_MAX; |
| 3343 | else |
| 3344 | x = 0; |
Andrew M. Kuchling | 2194b16 | 2000-02-23 22:18:48 +0000 | [diff] [blame] | 3345 | } |
| 3346 | } else { |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3347 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3348 | "slice indices must be integers"); |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 3349 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3350 | } |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3351 | /* Truncate -- very long indices are truncated anyway */ |
| 3352 | if (x > INT_MAX) |
| 3353 | x = INT_MAX; |
| 3354 | else if (x < -INT_MAX) |
| 3355 | x = 0; |
| 3356 | *pi = x; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3357 | } |
Guido van Rossum | 20c6add | 2000-05-08 14:06:50 +0000 | [diff] [blame] | 3358 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3361 | #undef ISINT |
| 3362 | #define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x)) |
| 3363 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3364 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3365 | apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3366 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3367 | PyTypeObject *tp = u->ob_type; |
| 3368 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 3369 | |
| 3370 | if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) { |
| 3371 | int ilow = 0, ihigh = INT_MAX; |
| 3372 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 3373 | return NULL; |
| 3374 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 3375 | return NULL; |
| 3376 | return PySequence_GetSlice(u, ilow, ihigh); |
| 3377 | } |
| 3378 | else { |
| 3379 | PyObject *slice = PySlice_New(v, w, NULL); |
| 3380 | if (slice != NULL) |
| 3381 | return PyObject_GetItem(u, slice); |
| 3382 | else |
| 3383 | return NULL; |
| 3384 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3385 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3386 | |
| 3387 | static int |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3388 | assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) |
| 3389 | /* u[v:w] = x */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3390 | { |
Guido van Rossum | 50d756e | 2001-08-18 17:43:36 +0000 | [diff] [blame] | 3391 | PyTypeObject *tp = u->ob_type; |
| 3392 | PySequenceMethods *sq = tp->tp_as_sequence; |
| 3393 | |
| 3394 | if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) { |
| 3395 | int ilow = 0, ihigh = INT_MAX; |
| 3396 | if (!_PyEval_SliceIndex(v, &ilow)) |
| 3397 | return -1; |
| 3398 | if (!_PyEval_SliceIndex(w, &ihigh)) |
| 3399 | return -1; |
| 3400 | if (x == NULL) |
| 3401 | return PySequence_DelSlice(u, ilow, ihigh); |
| 3402 | else |
| 3403 | return PySequence_SetSlice(u, ilow, ihigh, x); |
| 3404 | } |
| 3405 | else { |
| 3406 | PyObject *slice = PySlice_New(v, w, NULL); |
| 3407 | if (slice != NULL) { |
| 3408 | if (x != NULL) |
| 3409 | return PyObject_SetItem(u, slice, x); |
| 3410 | else |
| 3411 | return PyObject_DelItem(u, slice); |
| 3412 | } |
| 3413 | else |
| 3414 | return -1; |
| 3415 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3418 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3419 | cmp_outcome(int op, register PyObject *v, register PyObject *w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3420 | { |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3421 | int res = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3422 | switch (op) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3423 | case IS: |
| 3424 | case IS_NOT: |
| 3425 | res = (v == w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3426 | if (op == (int) IS_NOT) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3427 | res = !res; |
| 3428 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3429 | case IN: |
| 3430 | case NOT_IN: |
Guido van Rossum | 7e33c6e | 1998-05-22 00:52:29 +0000 | [diff] [blame] | 3431 | res = PySequence_Contains(w, v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3432 | if (res < 0) |
| 3433 | return NULL; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 3434 | if (op == (int) NOT_IN) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3435 | res = !res; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3436 | break; |
| 3437 | case EXC_MATCH: |
Barry Warsaw | 4249f54 | 1997-08-22 21:26:19 +0000 | [diff] [blame] | 3438 | res = PyErr_GivenExceptionMatches(v, w); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3439 | break; |
| 3440 | default: |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3441 | return PyObject_RichCompare(v, w, op); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3442 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3443 | v = res ? Py_True : Py_False; |
| 3444 | Py_INCREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3445 | return v; |
| 3446 | } |
| 3447 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3448 | static PyObject * |
| 3449 | import_from(PyObject *v, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 3450 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3451 | PyObject *x; |
| 3452 | |
| 3453 | x = PyObject_GetAttr(v, name); |
| 3454 | if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3455 | PyErr_Format(PyExc_ImportError, |
| 3456 | "cannot import name %.230s", |
| 3457 | PyString_AsString(name)); |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3458 | } |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3459 | return x; |
| 3460 | } |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3461 | |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3462 | static int |
| 3463 | import_all_from(PyObject *locals, PyObject *v) |
| 3464 | { |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3465 | PyObject *all = PyObject_GetAttrString(v, "__all__"); |
| 3466 | PyObject *dict, *name, *value; |
| 3467 | int skip_leading_underscores = 0; |
| 3468 | int pos, err; |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3469 | |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3470 | if (all == NULL) { |
| 3471 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3472 | return -1; /* Unexpected error */ |
| 3473 | PyErr_Clear(); |
| 3474 | dict = PyObject_GetAttrString(v, "__dict__"); |
| 3475 | if (dict == NULL) { |
| 3476 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3477 | return -1; |
| 3478 | PyErr_SetString(PyExc_ImportError, |
| 3479 | "from-import-* object has no __dict__ and no __all__"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3480 | return -1; |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3481 | } |
| 3482 | all = PyMapping_Keys(dict); |
| 3483 | Py_DECREF(dict); |
| 3484 | if (all == NULL) |
| 3485 | return -1; |
| 3486 | skip_leading_underscores = 1; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 3487 | } |
Guido van Rossum | 18d4d8f | 2001-01-12 16:24:03 +0000 | [diff] [blame] | 3488 | |
| 3489 | for (pos = 0, err = 0; ; pos++) { |
| 3490 | name = PySequence_GetItem(all, pos); |
| 3491 | if (name == NULL) { |
| 3492 | if (!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 3493 | err = -1; |
| 3494 | else |
| 3495 | PyErr_Clear(); |
| 3496 | break; |
| 3497 | } |
| 3498 | if (skip_leading_underscores && |
| 3499 | PyString_Check(name) && |
| 3500 | PyString_AS_STRING(name)[0] == '_') |
| 3501 | { |
| 3502 | Py_DECREF(name); |
| 3503 | continue; |
| 3504 | } |
| 3505 | value = PyObject_GetAttr(v, name); |
| 3506 | if (value == NULL) |
| 3507 | err = -1; |
| 3508 | else |
| 3509 | err = PyDict_SetItem(locals, name, value); |
| 3510 | Py_DECREF(name); |
| 3511 | Py_XDECREF(value); |
| 3512 | if (err != 0) |
| 3513 | break; |
| 3514 | } |
| 3515 | Py_DECREF(all); |
| 3516 | return err; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3519 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3520 | build_class(PyObject *methods, PyObject *bases, PyObject *name) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 3521 | { |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 3522 | PyObject *metaclass = NULL, *result, *base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3523 | |
| 3524 | if (PyDict_Check(methods)) |
| 3525 | metaclass = PyDict_GetItemString(methods, "__metaclass__"); |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 3526 | if (metaclass != NULL) |
| 3527 | Py_INCREF(methods); |
| 3528 | else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) { |
| 3529 | base = PyTuple_GET_ITEM(bases, 0); |
| 3530 | metaclass = PyObject_GetAttrString(base, "__class__"); |
| 3531 | if (metaclass == NULL) { |
| 3532 | PyErr_Clear(); |
| 3533 | metaclass = (PyObject *)base->ob_type; |
| 3534 | Py_INCREF(metaclass); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3535 | } |
| 3536 | } |
Guido van Rossum | 7851eea | 2001-09-12 19:19:18 +0000 | [diff] [blame] | 3537 | else { |
| 3538 | PyObject *g = PyEval_GetGlobals(); |
| 3539 | if (g != NULL && PyDict_Check(g)) |
| 3540 | metaclass = PyDict_GetItemString(g, "__metaclass__"); |
| 3541 | if (metaclass == NULL) |
| 3542 | metaclass = (PyObject *) &PyClass_Type; |
| 3543 | Py_INCREF(metaclass); |
| 3544 | } |
| 3545 | result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods); |
| 3546 | Py_DECREF(metaclass); |
| 3547 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3550 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3551 | exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, |
| 3552 | PyObject *locals) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3553 | { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3554 | int n; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3555 | PyObject *v; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3556 | int plain = 0; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3557 | |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3558 | if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None && |
| 3559 | ((n = PyTuple_Size(prog)) == 2 || n == 3)) { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3560 | /* Backward compatibility hack */ |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3561 | globals = PyTuple_GetItem(prog, 1); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3562 | if (n == 3) |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3563 | locals = PyTuple_GetItem(prog, 2); |
| 3564 | prog = PyTuple_GetItem(prog, 0); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3565 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3566 | if (globals == Py_None) { |
| 3567 | globals = PyEval_GetGlobals(); |
| 3568 | if (locals == Py_None) { |
| 3569 | locals = PyEval_GetLocals(); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3570 | plain = 1; |
| 3571 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3572 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3573 | else if (locals == Py_None) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3574 | locals = globals; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3575 | if (!PyString_Check(prog) && |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 3576 | !PyUnicode_Check(prog) && |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3577 | !PyCode_Check(prog) && |
| 3578 | !PyFile_Check(prog)) { |
| 3579 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3580 | "exec: arg 1 must be a string, file, or code object"); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3581 | return -1; |
| 3582 | } |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3583 | if (!PyDict_Check(globals)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3584 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3585 | "exec: arg 2 must be a dictionary or None"); |
| 3586 | return -1; |
| 3587 | } |
| 3588 | if (!PyDict_Check(locals)) { |
| 3589 | PyErr_SetString(PyExc_TypeError, |
| 3590 | "exec: arg 3 must be a dictionary or None"); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3591 | return -1; |
| 3592 | } |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3593 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) |
Guido van Rossum | a027efa | 1997-05-05 20:56:21 +0000 | [diff] [blame] | 3594 | PyDict_SetItemString(globals, "__builtins__", f->f_builtins); |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3595 | if (PyCode_Check(prog)) { |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 3596 | v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3597 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 3598 | else if (PyFile_Check(prog)) { |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3599 | FILE *fp = PyFile_AsFile(prog); |
| 3600 | char *name = PyString_AsString(PyFile_Name(prog)); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3601 | PyCompilerFlags cf; |
| 3602 | cf.cf_flags = 0; |
| 3603 | if (PyEval_MergeCompilerFlags(&cf)) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 3604 | v = PyRun_FileFlags(fp, name, Py_file_input, globals, |
| 3605 | locals, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3606 | else |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 3607 | v = PyRun_File(fp, name, Py_file_input, globals, |
| 3608 | locals); |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 3609 | } |
| 3610 | else { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 3611 | char *str; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3612 | PyCompilerFlags cf; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 3613 | if (PyString_AsStringAndSize(prog, &str, NULL)) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3614 | return -1; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3615 | cf.cf_flags = 0; |
| 3616 | if (PyEval_MergeCompilerFlags(&cf)) |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 3617 | v = PyRun_StringFlags(str, Py_file_input, globals, |
| 3618 | locals, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 3619 | else |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 3620 | v = PyRun_String(str, Py_file_input, globals, locals); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3621 | } |
Guido van Rossum | a400d8a | 2000-01-12 22:45:54 +0000 | [diff] [blame] | 3622 | if (plain) |
| 3623 | PyFrame_LocalsToFast(f, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3624 | if (v == NULL) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3625 | return -1; |
Guido van Rossum | b209a11 | 1997-04-29 18:18:01 +0000 | [diff] [blame] | 3626 | Py_DECREF(v); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3627 | return 0; |
| 3628 | } |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 3629 | |
Guido van Rossum | ac7be68 | 2001-01-17 15:42:30 +0000 | [diff] [blame] | 3630 | static void |
Paul Prescod | e68140d | 2000-08-30 20:25:01 +0000 | [diff] [blame] | 3631 | format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj) |
| 3632 | { |
| 3633 | char *obj_str; |
| 3634 | |
| 3635 | if (!obj) |
| 3636 | return; |
| 3637 | |
| 3638 | obj_str = PyString_AsString(obj); |
| 3639 | if (!obj_str) |
| 3640 | return; |
| 3641 | |
| 3642 | PyErr_Format(exc, format_str, obj_str); |
| 3643 | } |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 3644 | |
| 3645 | #ifdef DYNAMIC_EXECUTION_PROFILE |
| 3646 | |
Skip Montanaro | f118cb1 | 2001-10-15 20:51:38 +0000 | [diff] [blame] | 3647 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3648 | getarray(long a[256]) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 3649 | { |
| 3650 | int i; |
| 3651 | PyObject *l = PyList_New(256); |
| 3652 | if (l == NULL) return NULL; |
| 3653 | for (i = 0; i < 256; i++) { |
| 3654 | PyObject *x = PyInt_FromLong(a[i]); |
| 3655 | if (x == NULL) { |
| 3656 | Py_DECREF(l); |
| 3657 | return NULL; |
| 3658 | } |
| 3659 | PyList_SetItem(l, i, x); |
| 3660 | } |
| 3661 | for (i = 0; i < 256; i++) |
| 3662 | a[i] = 0; |
| 3663 | return l; |
| 3664 | } |
| 3665 | |
| 3666 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3667 | _Py_GetDXProfile(PyObject *self, PyObject *args) |
Guido van Rossum | 950361c | 1997-01-24 13:49:28 +0000 | [diff] [blame] | 3668 | { |
| 3669 | #ifndef DXPAIRS |
| 3670 | return getarray(dxp); |
| 3671 | #else |
| 3672 | int i; |
| 3673 | PyObject *l = PyList_New(257); |
| 3674 | if (l == NULL) return NULL; |
| 3675 | for (i = 0; i < 257; i++) { |
| 3676 | PyObject *x = getarray(dxpairs[i]); |
| 3677 | if (x == NULL) { |
| 3678 | Py_DECREF(l); |
| 3679 | return NULL; |
| 3680 | } |
| 3681 | PyList_SetItem(l, i, x); |
| 3682 | } |
| 3683 | return l; |
| 3684 | #endif |
| 3685 | } |
| 3686 | |
| 3687 | #endif |