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