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