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