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