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