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