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