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