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