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