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