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