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