blob: 425e2a06a6cf634b60d1c92ea134493c36e7a847 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum34679b71993-01-26 13:33:44 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3f5da241990-12-20 15:06:42 +000025/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000026
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000028
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "import.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#include "sysmodule.h"
31#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000032#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000033#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "ceval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000035#include "opcode.h"
Guido van Rossum86cd6e61991-01-21 15:12:35 +000036#include "bltinmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000037#include "traceback.h"
Guido van Rossumdb3165e1993-10-18 17:06:59 +000038#include "graminit.h"
39#include "pythonrun.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000040
Guido van Rossumc6004111993-11-05 10:22:19 +000041#include <ctype.h>
42
Guido van Rossum04691fc1992-08-12 15:35:34 +000043/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossumacbe8da1993-04-15 15:33:52 +000044/* #define CASE_TOO_BIG 1 /**/
Guido van Rossum04691fc1992-08-12 15:35:34 +000045
Guido van Rossumacbe8da1993-04-15 15:33:52 +000046/* 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 Rossum96a42c81992-01-12 02:29:51 +000051/* For debugging the interpreter: */
52#define LLTRACE 1 /* Low-level trace feature */
53#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000054#endif
55
Guido van Rossum9e90a671993-06-24 11:10:19 +000056/* Global option, may be set by main() */
57int killprint;
58
Guido van Rossum5b722181993-03-30 17:46:03 +000059
Guido van Rossum374a9221991-04-04 10:40:29 +000060/* Forward declarations */
Guido van Rossum374a9221991-04-04 10:40:29 +000061
Guido van Rossum0a066c01992-03-27 17:29:15 +000062#ifdef LLTRACE
63static int prtrace PROTO((object *, char *));
64#endif
Guido van Rossum9c8d70d1992-03-23 18:19:28 +000065static void call_exc_trace PROTO((object **, object**, frameobject *));
66static int call_trace
67 PROTO((object **, object **, frameobject *, char *, object *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000068static object *add PROTO((object *, object *));
69static object *sub PROTO((object *, object *));
70static object *mul PROTO((object *, object *));
71static object *divide PROTO((object *, object *));
72static object *rem PROTO((object *, object *));
73static object *neg PROTO((object *));
74static object *pos PROTO((object *));
75static object *not PROTO((object *));
76static object *invert PROTO((object *));
77static object *lshift PROTO((object *, object *));
78static object *rshift PROTO((object *, object *));
79static object *and PROTO((object *, object *));
80static object *xor PROTO((object *, object *));
81static object *or PROTO((object *, object *));
82static object *call_builtin PROTO((object *, object *));
83static object *call_function PROTO((object *, object *));
84static object *apply_subscript PROTO((object *, object *));
85static object *loop_subscript PROTO((object *, object *));
86static int slice_index PROTO((object *, int, int *));
87static object *apply_slice PROTO((object *, object *, object *));
88static int assign_subscript PROTO((object *, object *, object *));
89static int assign_slice PROTO((object *, object *, object *, object *));
90static int cmp_exception PROTO((object *, object *));
91static int cmp_member PROTO((object *, object *));
Guido van Rossum5b7313a1992-04-06 13:24:57 +000092static object *cmp_outcome PROTO((int, object *, object *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000093static int import_from PROTO((object *, object *, object *));
Guido van Rossum25831651993-05-19 14:50:45 +000094static object *build_class PROTO((object *, object *, object *));
Guido van Rossum8b17d6b1993-03-30 13:18:41 +000095static void locals_2_fast PROTO((frameobject *, int));
Guido van Rossum5b722181993-03-30 17:46:03 +000096static void fast_2_locals PROTO((frameobject *));
Guido van Rossumb3f72581993-05-21 19:56:10 +000097static int access_statement PROTO((object *, object *, frameobject *));
Guido van Rossumdb3165e1993-10-18 17:06:59 +000098static int exec_statement PROTO((object *, object *, object *));
Guido van Rossumb73cc041993-11-01 16:28:59 +000099static void mergelocals PROTO(());
Guido van Rossum374a9221991-04-04 10:40:29 +0000100
101
Guido van Rossum0a066c01992-03-27 17:29:15 +0000102/* Pointer to current frame, used to link new frames to */
103
Guido van Rossum374a9221991-04-04 10:40:29 +0000104static frameobject *current_frame;
105
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000106#ifdef USE_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000107
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000108#include <errno.h>
109#include "thread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000110
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000111static type_lock interpreter_lock;
112
113void
114init_save_thread()
115{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000116 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000117 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000118 interpreter_lock = allocate_lock();
119 acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000120}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000121
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000122#endif
123
Guido van Rossumff4949e1992-08-05 19:58:53 +0000124/* 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 Rossum04691fc1992-08-12 15:35:34 +0000128object *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000129save_thread()
130{
131#ifdef USE_THREAD
132 if (interpreter_lock) {
Guido van Rossum04691fc1992-08-12 15:35:34 +0000133 object *res;
134 res = (object *)current_frame;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000135 current_frame = NULL;
136 release_lock(interpreter_lock);
137 return res;
138 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000139#endif
Guido van Rossum9575a441993-04-07 14:06:14 +0000140 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000141}
142
143void
144restore_thread(x)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000145 object *x;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146{
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 Rossum374a9221991-04-04 10:40:29 +0000159/* Status code for main loop (reason for stack unwind) */
160
161enum 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
172object *
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000173eval_code(co, globals, locals, owner, arg)
Guido van Rossum374a9221991-04-04 10:40:29 +0000174 codeobject *co;
175 object *globals;
176 object *locals;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000177 object *owner;
Guido van Rossum374a9221991-04-04 10:40:29 +0000178 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 Rossum9bfef441993-03-29 10:43:31 +0000192 register listobject *fastlocals = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000193 object *trace = NULL; /* Trace function or NULL */
Guido van Rossum374a9221991-04-04 10:40:29 +0000194 object *retval; /* Return value iff why == WHY_RETURN */
195 char *name; /* Name used by some instructions */
Guido van Rossumb3f72581993-05-21 19:56:10 +0000196 int needmerge = 0; /* Set if need to merge locals back at end */
197 int defmode = 0; /* Default access mode for new variables */
Guido van Rossum96a42c81992-01-12 02:29:51 +0000198#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000199 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000200#endif
Guido van Rossum99bec951992-09-03 20:29:45 +0000201#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 Rossum374a9221991-04-04 10:40:29 +0000205
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 Rossuma9e7dc11992-10-18 18:53:57 +0000226#define CHECK_STACK(n) (STACK_LEVEL() + (n) < f->f_nvalues || \
227 (stack_pointer = extend_stack(f, STACK_LEVEL(), n)))
228
Guido van Rossum96a42c81992-01-12 02:29:51 +0000229#ifdef LLTRACE
230#define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
231#define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
Guido van Rossum374a9221991-04-04 10:40:29 +0000232#else
233#define PUSH(v) BASIC_PUSH(v)
234#define POP() BASIC_POP()
235#endif
236
Guido van Rossum5b722181993-03-30 17:46:03 +0000237 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 Rossumacbe8da1993-04-15 15:33:52 +0000249#ifdef LLTRACE
250 lltrace = dictlookup(globals, "__lltrace__") != NULL;
251#endif
252
Guido van Rossum374a9221991-04-04 10:40:29 +0000253 f = newframeobject(
254 current_frame, /*back*/
255 co, /*code*/
256 globals, /*globals*/
257 locals, /*locals*/
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000258 owner, /*owner*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000259 50, /*nvalues*/
260 20); /*nblocks*/
261 if (f == NULL)
262 return NULL;
263
264 current_frame = f;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000265
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000266 if (sys_trace != NULL) {
267 /* sys_trace, if defined, is a function that will
Guido van Rossum96a42c81992-01-12 02:29:51 +0000268 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 Rossum9c8d70d1992-03-23 18:19:28 +0000279 if (call_trace(&sys_trace, &trace, f, "call", arg)) {
Guido van Rossum96a42c81992-01-12 02:29:51 +0000280 /* Trace function raised an error */
Guido van Rossum96a42c81992-01-12 02:29:51 +0000281 current_frame = f->f_back;
282 DECREF(f);
283 return NULL;
284 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000285 }
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 Rossum96a42c81992-01-12 02:29:51 +0000294 }
295 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000296
297 next_instr = GETUSTRINGVALUE(f->f_code->co_code);
Guido van Rossum374a9221991-04-04 10:40:29 +0000298 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 Rossum374a9221991-04-04 10:40:29 +0000308
309 for (;;) {
Guido van Rossum4965bc81991-05-14 11:51:49 +0000310 static int ticker;
Guido van Rossum374a9221991-04-04 10:40:29 +0000311
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000312 /* 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 Rossum374a9221991-04-04 10:40:29 +0000316
317 if (--ticker < 0) {
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318 ticker = 10;
Guido van Rossum374a9221991-04-04 10:40:29 +0000319 if (intrcheck()) {
320 err_set(KeyboardInterrupt);
321 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000322 goto on_error;
323 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000324
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 Rossum374a9221991-04-04 10:40:29 +0000338 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000339
Guido van Rossum374a9221991-04-04 10:40:29 +0000340 /* Extract opcode and argument */
Guido van Rossum99bec951992-09-03 20:29:45 +0000341
342#ifdef DEBUG
343 f->f_lasti = INSTR_OFFSET();
344#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000345
346 opcode = NEXTOP();
347 if (HAS_ARG(opcode))
348 oparg = NEXTARG();
349
Guido van Rossum96a42c81992-01-12 02:29:51 +0000350#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000351 /* Instruction tracing */
352
Guido van Rossum96a42c81992-01-12 02:29:51 +0000353 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000354 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 Rossuma9e7dc11992-10-18 18:53:57 +0000366 if (!CHECK_STACK(3)) {
367 x = NULL;
368 break;
369 }
370
Guido van Rossum374a9221991-04-04 10:40:29 +0000371 /* 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 Rossum96a42c81992-01-12 02:29:51 +0000439 f->f_lasti = INSTR_OFFSET() - 1; /* For tracing */
Guido van Rossum9c7b8611991-12-16 13:04:47 +0000440 x = call_object(v, (object *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +0000441 DECREF(v);
442 PUSH(x);
443 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000444
445 case UNARY_INVERT:
446 v = POP();
447 x = invert(v);
448 DECREF(v);
449 PUSH(x);
450 break;
Guido van Rossum374a9221991-04-04 10:40:29 +0000451
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 Rossum96a42c81992-01-12 02:29:51 +0000509 f->f_lasti = INSTR_OFFSET() - 1; /* For tracing */
Guido van Rossum9c7b8611991-12-16 13:04:47 +0000510 x = call_object(v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000511 DECREF(v);
512 DECREF(w);
513 PUSH(x);
514 break;
515
Guido van Rossum7928cd71991-10-24 14:59:31 +0000516 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 Rossum374a9221991-04-04 10:40:29 +0000561 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 Rossum374a9221991-04-04 10:40:29 +0000644 /* Print value except if procedure result */
645 if (v != None) {
646 flushline();
Guido van Rossum3165fe61992-09-25 21:59:05 +0000647 x = sysget("stdout");
648 softspace(x, 1);
649 err = writeobject(v, x, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +0000650 flushline();
Guido van Rossum9e90a671993-06-24 11:10:19 +0000651 if (killprint) {
652 err_setstr(RuntimeError,
653 "printing expression statement");
654 x = 0;
655 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000656 }
657 DECREF(v);
658 break;
659
660 case PRINT_ITEM:
661 v = POP();
Guido van Rossum3165fe61992-09-25 21:59:05 +0000662 w = sysget("stdout");
663 if (softspace(w, 1))
664 writestring(" ", w);
Guido van Rossumc6004111993-11-05 10:22:19 +0000665 err = writeobject(v, w, PRINT_RAW);
666 if (err == 0 && is_stringobject(v)) {
667 /* XXX move into writeobject() ? */
Guido van Rossum374a9221991-04-04 10:40:29 +0000668 char *s = getstringvalue(v);
669 int len = getstringsize(v);
Guido van Rossumc6004111993-11-05 10:22:19 +0000670 if (len > 0 && isspace(s[len-1]) &&
671 s[len-1] != ' ')
Guido van Rossum3165fe61992-09-25 21:59:05 +0000672 softspace(w, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +0000673 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000674 DECREF(v);
675 break;
676
677 case PRINT_NEWLINE:
Guido van Rossum3165fe61992-09-25 21:59:05 +0000678 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 Rossum374a9221991-04-04 10:40:29 +0000685 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 Rossum32c6cdf1991-12-10 13:52:46 +0000694 /* 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 Rossum374a9221991-04-04 10:40:29 +0000700 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 Rossumdb3165e1993-10-18 17:06:59 +0000720
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 Rossum374a9221991-04-04 10:40:29 +0000736
Guido van Rossum374a9221991-04-04 10:40:29 +0000737 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 Rossum25831651993-05-19 14:50:45 +0000779 u = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +0000780 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +0000781 w = POP();
782 x = build_class(u, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000783 PUSH(x);
Guido van Rossum25831651993-05-19 14:50:45 +0000784 DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +0000785 DECREF(v);
786 DECREF(w);
787 break;
788
789 case STORE_NAME:
790 w = GETNAMEV(oparg);
791 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +0000792 u = dict2lookup(f->f_locals, w);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000793 if (u == NULL) {
794 if (defmode != 0) {
795 if (v != None)
796 u = (object *)v->ob_type;
797 else
798 u = NULL;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000799 x = newaccessobject(v, f->f_locals,
Guido van Rossumb3f72581993-05-21 19:56:10 +0000800 (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 Rossumeb6b33a1993-05-25 09:38:27 +0000809 err = setaccessvalue(u, f->f_locals, v);
Guido van Rossum25831651993-05-19 14:50:45 +0000810 DECREF(v);
811 break;
812 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000813 err = dict2insert(f->f_locals, w, v);
814 DECREF(v);
815 break;
816
817 case DELETE_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +0000818 w = GETNAMEV(oparg);
Guido van Rossum25831651993-05-19 14:50:45 +0000819 u = dict2lookup(f->f_locals, w);
820 if (u != NULL && is_accessobject(u)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000821 err = setaccessvalue(u, f->f_locals,
Guido van Rossum25831651993-05-19 14:50:45 +0000822 (object *)NULL);
823 break;
824 }
Guido van Rossumdf62e441991-08-16 08:56:04 +0000825 if ((err = dict2remove(f->f_locals, w)) != 0)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000826 err_setval(NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000827 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000828
829#ifdef CASE_TOO_BIG
830 default: switch (opcode) {
831#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000832
Guido van Rossum6a3f9a81992-01-14 18:29:20 +0000833 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 Rossuma9e7dc11992-10-18 18:53:57 +0000859 if (!CHECK_STACK(oparg)) {
860 x = NULL;
861 break;
862 }
Guido van Rossum6a3f9a81992-01-14 18:29:20 +0000863 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 Rossum9c7b8611991-12-16 13:04:47 +0000873 case UNPACK_ARG:
Guido van Rossum9c7b8611991-12-16 13:04:47 +0000874 {
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 Rossum6f5afc91993-02-05 09:46:15 +0000890#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 Rossum9c7b8611991-12-16 13:04:47 +0000896 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 Rossum5f59d601992-12-14 16:59:51 +0000937#endif /* Disabled compatibility hacks */
Guido van Rossum9c7b8611991-12-16 13:04:47 +0000938 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 Rossum374a9221991-04-04 10:40:29 +0000948 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 Rossum32c6cdf1991-12-10 13:52:46 +0000955 err_setstr(ValueError,
Guido van Rossum374a9221991-04-04 10:40:29 +0000956 "unpack tuple of wrong size");
957 why = WHY_EXCEPTION;
958 }
959 else {
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000960 if (!CHECK_STACK(oparg)) {
961 x = NULL;
962 break;
963 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000964 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 Rossum32c6cdf1991-12-10 13:52:46 +0000980 err_setstr(ValueError,
Guido van Rossum374a9221991-04-04 10:40:29 +0000981 "unpack list of wrong size");
982 why = WHY_EXCEPTION;
983 }
984 else {
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000985 if (!CHECK_STACK(oparg)) {
986 x = NULL;
987 break;
988 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000989 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 Rossum9bfef441993-03-29 10:43:31 +0000999 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001000 v = POP();
1001 u = POP();
Guido van Rossum9bfef441993-03-29 10:43:31 +00001002 err = setattro(v, w, u); /* v.w = u */
Guido van Rossum374a9221991-04-04 10:40:29 +00001003 DECREF(v);
1004 DECREF(u);
1005 break;
1006
1007 case DELETE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001008 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001009 v = POP();
Guido van Rossum9bfef441993-03-29 10:43:31 +00001010 err = setattro(v, w, (object *)NULL); /* del v.w */
Guido van Rossum374a9221991-04-04 10:40:29 +00001011 DECREF(v);
1012 break;
1013
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001014 case STORE_GLOBAL:
1015 w = GETNAMEV(oparg);
1016 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +00001017 u = dict2lookup(f->f_locals, w);
1018 if (u != NULL && is_accessobject(u)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001019 err = setaccessvalue(u, f->f_globals, v);
Guido van Rossum25831651993-05-19 14:50:45 +00001020 DECREF(v);
1021 break;
1022 }
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001023 err = dict2insert(f->f_globals, w, v);
1024 DECREF(v);
1025 break;
1026
1027 case DELETE_GLOBAL:
1028 w = GETNAMEV(oparg);
Guido van Rossum25831651993-05-19 14:50:45 +00001029 u = dict2lookup(f->f_locals, w);
1030 if (u != NULL && is_accessobject(u)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001031 err = setaccessvalue(u, f->f_globals,
Guido van Rossum25831651993-05-19 14:50:45 +00001032 (object *)NULL);
1033 break;
1034 }
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001035 if ((err = dict2remove(f->f_globals, w)) != 0)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001036 err_setval(NameError, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001037 break;
1038
Guido van Rossum374a9221991-04-04 10:40:29 +00001039 case LOAD_CONST:
1040 x = GETCONST(oparg);
1041 INCREF(x);
1042 PUSH(x);
1043 break;
1044
1045 case LOAD_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001046 w = GETNAMEV(oparg);
1047 x = dict2lookup(f->f_locals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001048 if (x == NULL) {
Guido van Rossumdf62e441991-08-16 08:56:04 +00001049 err_clear();
1050 x = dict2lookup(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001051 if (x == NULL) {
Guido van Rossumdf62e441991-08-16 08:56:04 +00001052 err_clear();
1053 x = getbuiltin(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001054 if (x == NULL) {
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001055 err_setval(NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001056 break;
1057 }
1058 }
1059 }
Guido van Rossum25831651993-05-19 14:50:45 +00001060 if (is_accessobject(x)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001061 x = getaccessvalue(x, f->f_globals /* XXX */);
Guido van Rossum25831651993-05-19 14:50:45 +00001062 if (x == NULL)
1063 break;
1064 }
Guido van Rossum81daa321993-05-20 14:24:46 +00001065 else
1066 INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001067 PUSH(x);
1068 break;
1069
1070 case LOAD_GLOBAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001071 w = GETNAMEV(oparg);
1072 x = dict2lookup(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001073 if (x == NULL) {
Guido van Rossumdf62e441991-08-16 08:56:04 +00001074 err_clear();
1075 x = getbuiltin(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001076 if (x == NULL) {
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001077 err_setval(NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001078 break;
1079 }
1080 }
Guido van Rossum25831651993-05-19 14:50:45 +00001081 if (is_accessobject(x)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001082 x = getaccessvalue(x, f->f_globals);
Guido van Rossum25831651993-05-19 14:50:45 +00001083 if (x == NULL)
1084 break;
1085 }
Guido van Rossum81daa321993-05-20 14:24:46 +00001086 else
1087 INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001088 PUSH(x);
1089 break;
1090
1091 case LOAD_LOCAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001092 w = GETNAMEV(oparg);
1093 x = dict2lookup(f->f_locals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001094 if (x == NULL) {
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001095 err_setval(NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001096 break;
1097 }
Guido van Rossum25831651993-05-19 14:50:45 +00001098 if (is_accessobject(x)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001099 x = getaccessvalue(x, f->f_locals);
Guido van Rossum25831651993-05-19 14:50:45 +00001100 if (x == NULL)
1101 break;
1102 }
Guido van Rossum81daa321993-05-20 14:24:46 +00001103 else
1104 INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 PUSH(x);
1106 break;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001107
1108 case RESERVE_FAST:
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001109 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 Rossum9bfef441993-03-29 10:43:31 +00001117 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001118 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 Rossum9bfef441993-03-29 10:43:31 +00001125 break;
1126
1127 case LOAD_FAST:
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001128 x = GETLISTITEM(fastlocals, oparg);
1129 if (x == NULL) {
1130 err_setstr(NameError,
1131 "undefined local variable");
1132 break;
1133 }
Guido van Rossum25831651993-05-19 14:50:45 +00001134 if (is_accessobject(x)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001135 x = getaccessvalue(x, f->f_locals);
Guido van Rossum25831651993-05-19 14:50:45 +00001136 if (x == NULL)
1137 break;
1138 }
Guido van Rossum81daa321993-05-20 14:24:46 +00001139 else
1140 INCREF(x);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001141 PUSH(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001142 break;
1143
1144 case STORE_FAST:
Guido van Rossum25831651993-05-19 14:50:45 +00001145 v = POP();
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001146 w = GETLISTITEM(fastlocals, oparg);
Guido van Rossum25831651993-05-19 14:50:45 +00001147 if (w != NULL && is_accessobject(w)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001148 err = setaccessvalue(w, f->f_locals, v);
Guido van Rossum25831651993-05-19 14:50:45 +00001149 DECREF(v);
1150 break;
1151 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001152 XDECREF(w);
Guido van Rossum25831651993-05-19 14:50:45 +00001153 GETLISTITEM(fastlocals, oparg) = v;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001154 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 Rossum25831651993-05-19 14:50:45 +00001163 if (w != NULL && is_accessobject(w)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001164 err = setaccessvalue(w, f->f_locals,
1165 (object *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001166 break;
1167 }
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001168 DECREF(x);
1169 GETLISTITEM(fastlocals, oparg) = NULL;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001170 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001171
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 Rossum9bfef441993-03-29 10:43:31 +00001204 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001205 v = POP();
Guido van Rossum9bfef441993-03-29 10:43:31 +00001206 x = getattro(v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001207 DECREF(v);
1208 PUSH(x);
1209 break;
1210
1211 case COMPARE_OP:
1212 w = POP();
1213 v = POP();
Guido van Rossum5b7313a1992-04-06 13:24:57 +00001214 x = cmp_outcome(oparg, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001215 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 Rossumdf62e441991-08-16 08:56:04 +00001228 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001229 v = TOP();
Guido van Rossumdf62e441991-08-16 08:56:04 +00001230 err = import_from(f->f_locals, v, w);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001231 locals_2_fast(f, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +00001232 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001233
1234 case ACCESS_MODE:
1235 v = POP();
1236 w = GETNAMEV(oparg);
Guido van Rossumb3f72581993-05-21 19:56:10 +00001237 if (getstringvalue(w)[0] == '*')
1238 defmode = getintvalue(v);
1239 else
1240 err = access_statement(w, v, f);
Guido van Rossum25831651993-05-19 14:50:45 +00001241 DECREF(v);
1242 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001243
1244 case JUMP_FORWARD:
1245 JUMPBY(oparg);
1246 break;
1247
1248 case JUMP_IF_FALSE:
Guido van Rossum04691fc1992-08-12 15:35:34 +00001249 err = testbool(TOP());
1250 if (err > 0)
1251 err = 0;
1252 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001253 JUMPBY(oparg);
1254 break;
1255
1256 case JUMP_IF_TRUE:
Guido van Rossum04691fc1992-08-12 15:35:34 +00001257 err = testbool(TOP());
1258 if (err > 0) {
1259 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001260 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001261 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001262 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 Rossum96a42c81992-01-12 02:29:51 +00001304#ifdef LLTRACE
1305 if (lltrace)
Guido van Rossum374a9221991-04-04 10:40:29 +00001306 printf("--- Line %d ---\n", oparg);
1307#endif
Guido van Rossum96a42c81992-01-12 02:29:51 +00001308 f->f_lineno = oparg;
1309 if (trace != NULL) {
1310 /* Trace each line of code reached */
1311 f->f_lasti = INSTR_OFFSET();
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001312 err = call_trace(&trace, &trace,
1313 f, "line", None);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001314 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001315 break;
1316
1317 default:
1318 fprintf(stderr,
1319 "XXX lineno: %d, opcode: %d\n",
Guido van Rossum96a42c81992-01-12 02:29:51 +00001320 f->f_lineno, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001321 err_setstr(SystemError, "eval_code: unknown opcode");
1322 why = WHY_EXCEPTION;
1323 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001324
1325#ifdef CASE_TOO_BIG
1326 }
1327#endif
1328
Guido van Rossum374a9221991-04-04 10:40:29 +00001329 } /* 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 Rossum801dcae1992-04-08 11:32:32 +00001343#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00001344 /* 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 Rossum99bec951992-09-03 20:29:45 +00001356 abort();
1357 /* NOTREACHED */
Guido van Rossum374a9221991-04-04 10:40:29 +00001358 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 Rossum96a42c81992-01-12 02:29:51 +00001366 f->f_lasti = INSTR_OFFSET() - 1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001367 if (HAS_ARG(opcode))
Guido van Rossum96a42c81992-01-12 02:29:51 +00001368 f->f_lasti -= 2;
1369 tb_here(f);
1370
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001371 if (trace)
1372 call_exc_trace(&trace, &trace, f);
1373 if (sys_profile)
1374 call_exc_trace(&sys_profile, (object**)0, f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001375 }
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 Rossum374a9221991-04-04 10:40:29 +00001412 sysset("exc_traceback", v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001413 sysset("exc_value", val);
1414 sysset("exc_type", exc);
Guido van Rossum374a9221991-04-04 10:40:29 +00001415 }
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 Rossum96a42c81992-01-12 02:29:51 +00001446 if (why != WHY_RETURN)
1447 retval = NULL;
1448
1449 if (trace) {
1450 if (why == WHY_RETURN) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001451 if (call_trace(&trace, &trace, f, "return", retval)) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001452 XDECREF(retval);
1453 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001454 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001455 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001456 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001457 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 Rossum96a42c81992-01-12 02:29:51 +00001467 }
Guido van Rossum5b722181993-03-30 17:46:03 +00001468
1469 if (fastlocals && (f->ob_refcnt > 1 || f->f_locals->ob_refcnt > 2))
1470 fast_2_locals(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001471
Guido van Rossum374a9221991-04-04 10:40:29 +00001472 /* Restore previous frame and release the current one */
1473
1474 current_frame = f->f_back;
1475 DECREF(f);
Guido van Rossum5b722181993-03-30 17:46:03 +00001476
1477 if (needmerge)
1478 locals_2_fast(current_frame, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001479
Guido van Rossum96a42c81992-01-12 02:29:51 +00001480 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00001481}
1482
Guido van Rossum96a42c81992-01-12 02:29:51 +00001483#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00001484static int
1485prtrace(v, str)
1486 object *v;
1487 char *str;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001488{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001489 printf("%s ", str);
Guido van Rossum90933611991-06-07 16:10:43 +00001490 if (printobject(v, stdout, 0) != 0)
1491 err_clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001492 printf("\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001493}
Guido van Rossum3f5da241990-12-20 15:06:42 +00001494#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001495
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001496static void
1497call_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 Rossumbd9ccca1992-04-09 14:58:08 +00001504 if (value == NULL) {
1505 value = None;
1506 INCREF(value);
1507 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001508 traceback = tb_fetch();
1509 arg = newtupleobject(3);
Guido van Rossumeee3fd41992-04-05 14:18:13 +00001510 if (arg == NULL)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001511 goto cleanup;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001512 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 Rossum9c8d70d1992-03-23 18:19:28 +00001516 if (!err) {
Guido van Rossumeee3fd41992-04-05 14:18:13 +00001517 cleanup:
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001518 /* Restore original exception */
1519 err_setval(type, value);
1520 tb_store(traceback);
1521 }
Guido van Rossumeee3fd41992-04-05 14:18:13 +00001522 XDECREF(arg);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001523}
1524
1525static int
1526call_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 Rossum96a42c81992-01-12 02:29:51 +00001532 frameobject *f;
1533 char *msg;
1534 object *arg;
1535{
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001536 object *arglist, *what;
1537 object *res = NULL;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001538 static int tracing = 0;
1539
1540 if (tracing) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001541 /* Don't do recursive traces */
1542 if (p_newtrace) {
1543 XDECREF(*p_newtrace);
1544 *p_newtrace = NULL;
1545 }
1546 return 0;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001547 }
1548
1549 arglist = newtupleobject(3);
1550 if (arglist == NULL)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001551 goto cleanup;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001552 what = newstringobject(msg);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001553 if (what == NULL)
1554 goto cleanup;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001555 INCREF(f);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001556 settupleitem(arglist, 0, (object *)f);
1557 settupleitem(arglist, 1, what);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001558 if (arg == NULL)
1559 arg = None;
1560 INCREF(arg);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001561 settupleitem(arglist, 2, arg);
1562 tracing++;
Guido van Rossum5b722181993-03-30 17:46:03 +00001563 fast_2_locals(f);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001564 res = call_object(*p_trace, arglist);
Guido van Rossum5b722181993-03-30 17:46:03 +00001565 locals_2_fast(f, 1);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001566 tracing--;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001567 cleanup:
1568 XDECREF(arglist);
1569 if (res == NULL) {
1570 /* The trace proc raised an exception */
Guido van Rossum96a42c81992-01-12 02:29:51 +00001571 tb_here(f);
Guido van Rossumeee3fd41992-04-05 14:18:13 +00001572 DECREF(*p_trace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001573 *p_trace = NULL;
1574 if (p_newtrace) {
1575 XDECREF(*p_newtrace);
1576 *p_newtrace = NULL;
1577 }
Guido van Rossumeee3fd41992-04-05 14:18:13 +00001578 return -1;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001579 }
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 Rossum9c8d70d1992-03-23 18:19:28 +00001591 return 0;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00001592 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001593}
1594
Guido van Rossum5b722181993-03-30 17:46:03 +00001595static void
1596fast_2_locals(f)
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001597 frameobject *f;
Guido van Rossum5b722181993-03-30 17:46:03 +00001598{
1599 /* Merge f->f_fastlocals into f->f_locals */
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001600 object *locals, *fast, *map;
Guido van Rossum5b722181993-03-30 17:46:03 +00001601 object *error_type, *error_value;
Guido van Rossum25831651993-05-19 14:50:45 +00001602 int pos;
1603 object *key, *value;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001604 if (f == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001605 return;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001606 locals = f->f_locals;
1607 fast = f->f_fastlocals;
1608 map = f->f_localmap;
1609 if (locals == NULL || fast == NULL || map == NULL)
Guido van Rossum5b722181993-03-30 17:46:03 +00001610 return;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001611 if (!is_dictobject(locals) || !is_listobject(fast) ||
1612 !is_dictobject(map))
Guido van Rossum5b722181993-03-30 17:46:03 +00001613 return;
1614 err_get(&error_type, &error_value);
Guido van Rossum25831651993-05-19 14:50:45 +00001615 pos = 0;
1616 while (mappinggetnext(map, &pos, &key, &value)) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001617 int j;
Guido van Rossum25831651993-05-19 14:50:45 +00001618 if (!is_intobject(value))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001619 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 Rossum5b722181993-03-30 17:46:03 +00001632 err_setval(error_type, error_value);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001633}
1634
1635static void
1636locals_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 Rossumacbe8da1993-04-15 15:33:52 +00001642 object *error_type, *error_value;
Guido van Rossum25831651993-05-19 14:50:45 +00001643 int pos;
1644 object *key, *value;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001645 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 Rossumacbe8da1993-04-15 15:33:52 +00001655 err_get(&error_type, &error_value);
Guido van Rossum25831651993-05-19 14:50:45 +00001656 pos = 0;
1657 while (mappinggetnext(map, &pos, &key, &value)) {
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001658 int j;
Guido van Rossum25831651993-05-19 14:50:45 +00001659 if (!is_intobject(value))
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001660 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 Rossumacbe8da1993-04-15 15:33:52 +00001671 err_setval(error_type, error_value);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001672}
1673
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001674static void
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001675mergelocals()
1676{
1677 locals_2_fast(current_frame, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001678}
1679
1680object *
Guido van Rossum5b722181993-03-30 17:46:03 +00001681getlocals()
1682{
1683 if (current_frame == NULL)
1684 return NULL;
1685 fast_2_locals(current_frame);
1686 return current_frame->f_locals;
1687}
1688
1689object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001690getglobals()
1691{
1692 if (current_frame == NULL)
1693 return NULL;
1694 else
1695 return current_frame->f_globals;
1696}
1697
Guido van Rossum81daa321993-05-20 14:24:46 +00001698object *
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001699getowner()
Guido van Rossum81daa321993-05-20 14:24:46 +00001700{
1701 if (current_frame == NULL)
1702 return NULL;
1703 else
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001704 return current_frame->f_owner;
Guido van Rossum81daa321993-05-20 14:24:46 +00001705}
1706
Guido van Rossum3f5da241990-12-20 15:06:42 +00001707void
Guido van Rossum3165fe61992-09-25 21:59:05 +00001708printtraceback(f)
1709 object *f;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001710{
1711 object *v = tb_fetch();
1712 if (v != NULL) {
Guido van Rossum3165fe61992-09-25 21:59:05 +00001713 writestring("Stack backtrace (innermost last):\n", f);
1714 tb_print(v, f);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001715 DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001716 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001717}
1718
1719
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001720void
1721flushline()
1722{
Guido van Rossum3165fe61992-09-25 21:59:05 +00001723 object *f = sysget("stdout");
1724 if (softspace(f, 0))
1725 writestring("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001726}
1727
Guido van Rossum3f5da241990-12-20 15:06:42 +00001728
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001729static object *
Guido van Rossum7928cd71991-10-24 14:59:31 +00001730or(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
1749static object *
1750xor(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
1769static object *
1770and(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
1789static object *
1790lshift(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
1809static object *
1810rshift(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
1829static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001830add(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001831 object *v, *w;
1832{
Guido van Rossume6eefc21992-08-14 12:06:52 +00001833 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 Rossum89d55ca1991-07-01 18:43:13 +00001836 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 Rossum34679b71993-01-26 13:33:44 +00001844 err_setstr(TypeError, "bad operand type(s) for +");
1845 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001846}
1847
1848static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001849sub(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001850 object *v, *w;
1851{
Guido van Rossum89d55ca1991-07-01 18:43:13 +00001852 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 Rossum3f5da241990-12-20 15:06:42 +00001861 err_setstr(TypeError, "bad operand type(s) for -");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001862 return NULL;
1863}
1864
1865static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001866mul(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001867 object *v, *w;
1868{
1869 typeobject *tp;
Guido van Rossume6eefc21992-08-14 12:06:52 +00001870 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 Rossum10dc2e81990-11-18 17:27:39 +00001875 object *tmp = v;
1876 v = w;
1877 w = tmp;
Guido van Rossume6eefc21992-08-14 12:06:52 +00001878 tp = v->ob_type;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001879 }
Guido van Rossum89d55ca1991-07-01 18:43:13 +00001880 if (tp->tp_as_number != NULL) {
1881 object *x;
Guido van Rossume6eefc21992-08-14 12:06:52 +00001882 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 Rossum89d55ca1991-07-01 18:43:13 +00001891 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 Rossum10dc2e81990-11-18 17:27:39 +00001897 if (tp->tp_as_sequence != NULL) {
1898 if (!is_intobject(w)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001899 err_setstr(TypeError,
1900 "can't multiply sequence with non-int");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001901 return NULL;
1902 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001903 return (*tp->tp_as_sequence->sq_repeat)
1904 (v, (int)getintvalue(w));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001905 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001906 err_setstr(TypeError, "bad operand type(s) for *");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001907 return NULL;
1908}
1909
1910static object *
Guido van Rossum40d0b7e1990-12-20 23:03:11 +00001911divide(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001912 object *v, *w;
1913{
Guido van Rossum89d55ca1991-07-01 18:43:13 +00001914 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 Rossum3f5da241990-12-20 15:06:42 +00001923 err_setstr(TypeError, "bad operand type(s) for /");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001924 return NULL;
1925}
1926
1927static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001928rem(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001929 object *v, *w;
1930{
Guido van Rossum89d55ca1991-07-01 18:43:13 +00001931 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 Rossume5372401993-03-16 12:15:04 +00001940 if (is_stringobject(v)) {
1941 return formatstring(v, w);
1942 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001943 err_setstr(TypeError, "bad operand type(s) for %");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001944 return NULL;
1945}
1946
1947static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001948neg(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001949 object *v;
1950{
1951 if (v->ob_type->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001952 return (*v->ob_type->tp_as_number->nb_negative)(v);
1953 err_setstr(TypeError, "bad operand type(s) for unary -");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001954 return NULL;
1955}
1956
1957static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001958pos(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001959 object *v;
1960{
1961 if (v->ob_type->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001962 return (*v->ob_type->tp_as_number->nb_positive)(v);
1963 err_setstr(TypeError, "bad operand type(s) for unary +");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001964 return NULL;
1965}
1966
1967static object *
Guido van Rossum7928cd71991-10-24 14:59:31 +00001968invert(v)
1969 object *v;
1970{
Guido van Rossum9b1d33b1992-02-11 15:56:02 +00001971 object * (*f) FPROTO((object *));
Guido van Rossum7928cd71991-10-24 14:59:31 +00001972 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
1979static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001980not(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001981 object *v;
1982{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001983 int outcome = testbool(v);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001984 object *w;
1985 if (outcome < 0)
1986 return NULL;
1987 if (outcome == 0)
1988 w = True;
1989 else
1990 w = False;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001991 INCREF(w);
1992 return w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001993}
Guido van Rossum234f9421993-06-17 12:35:49 +00001994
1995
Guido van Rossum83bf35c1991-07-27 21:32:34 +00001996/* External interface to call any callable object. The arg may be NULL. */
1997
1998object *
1999call_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 Rossum10dc2e81990-11-18 17:27:39 +00002009static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002010call_builtin(func, arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002011 object *func;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002012 object *arg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002013{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002014 if (is_methodobject(func)) {
2015 method meth = getmethod(func);
2016 object *self = getself(func);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002017 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 Rossum3f5da241990-12-20 15:06:42 +00002024 return (*meth)(self, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002025 }
2026 if (is_classobject(func)) {
Guido van Rossum25831651993-05-19 14:50:45 +00002027 return newinstanceobject(func, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002028 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002029 err_setstr(TypeError, "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002030 return NULL;
2031}
2032
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002033static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002034call_function(func, arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002035 object *func;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002036 object *arg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002037{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002038 object *newarg = NULL;
2039 object *newlocals, *newglobals;
Guido van Rossum81daa321993-05-20 14:24:46 +00002040 object *class = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002041 object *co, *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002042
Guido van Rossume8122f11991-05-05 20:03:07 +00002043 if (is_instancemethodobject(func)) {
2044 object *self = instancemethodgetself(func);
Guido van Rossum81daa321993-05-20 14:24:46 +00002045 class = instancemethodgetclass(func);
Guido van Rossume8122f11991-05-05 20:03:07 +00002046 func = instancemethodgetfunc(func);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002047 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 Rossum81daa321993-05-20 14:24:46 +00002069 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 Rossum9c7b8611991-12-16 13:04:47 +00002084 }
Guido van Rossum81daa321993-05-20 14:24:46 +00002085 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 Rossum10dc2e81990-11-18 17:27:39 +00002095 }
2096 }
2097 else {
2098 if (!is_funcobject(func)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002099 err_setstr(TypeError, "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002100 return NULL;
2101 }
2102 }
2103
Guido van Rossum3f5da241990-12-20 15:06:42 +00002104 co = getfunccode(func);
2105 if (co == NULL) {
2106 XDECREF(newarg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002107 return NULL;
2108 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002109 if (!is_codeobject(co)) {
2110 fprintf(stderr, "XXX Bad code\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002111 abort();
2112 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002113 newlocals = newdictobject();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002114 if (newlocals == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002115 XDECREF(newarg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002116 return NULL;
2117 }
2118
Guido van Rossum3f5da241990-12-20 15:06:42 +00002119 newglobals = getfuncglobals(func);
2120 INCREF(newglobals);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002121
Guido van Rossum81daa321993-05-20 14:24:46 +00002122 v = eval_code((codeobject *)co, newglobals, newlocals, class, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002123
Guido van Rossum3f5da241990-12-20 15:06:42 +00002124 DECREF(newlocals);
2125 DECREF(newglobals);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002126
Guido van Rossum3f5da241990-12-20 15:06:42 +00002127 XDECREF(newarg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002128
2129 return v;
2130}
2131
2132static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002133apply_subscript(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002134 object *v, *w;
2135{
2136 typeobject *tp = v->ob_type;
2137 if (tp->tp_as_sequence == NULL && tp->tp_as_mapping == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002138 err_setstr(TypeError, "unsubscriptable object");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002139 return NULL;
2140 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002141 if (tp->tp_as_mapping != NULL) {
2142 return (*tp->tp_as_mapping->mp_subscript)(v, w);
2143 }
2144 else {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002145 int i;
2146 if (!is_intobject(w)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002147 err_setstr(TypeError, "sequence subscript not int");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002148 return NULL;
2149 }
2150 i = getintvalue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002151 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 Rossum3f5da241990-12-20 15:06:42 +00002157 return (*tp->tp_as_sequence->sq_item)(v, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002158 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002159}
2160
2161static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002162loop_subscript(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002163 object *v, *w;
2164{
2165 sequence_methods *sq = v->ob_type->tp_as_sequence;
2166 int i, n;
2167 if (sq == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002168 err_setstr(TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002169 return NULL;
2170 }
2171 i = getintvalue(w);
2172 n = (*sq->sq_length)(v);
Guido van Rossumd014ea61992-11-26 10:30:26 +00002173 if (n < 0)
2174 return NULL; /* Exception */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002175 if (i >= n)
2176 return NULL; /* End of loop */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002177 return (*sq->sq_item)(v, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002178}
2179
2180static int
Guido van Rossum3f5da241990-12-20 15:06:42 +00002181slice_index(v, isize, pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002182 object *v;
2183 int isize;
2184 int *pi;
2185{
2186 if (v != NULL) {
2187 if (!is_intobject(v)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002188 err_setstr(TypeError, "slice index must be int");
2189 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002190 }
2191 *pi = getintvalue(v);
2192 if (*pi < 0)
2193 *pi += isize;
2194 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002195 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002196}
2197
2198static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002199apply_slice(u, v, w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002200 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 Rossum3f5da241990-12-20 15:06:42 +00002205 err_setstr(TypeError, "only sequences can be sliced");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002206 return NULL;
2207 }
2208 ilow = 0;
2209 isize = ihigh = (*tp->tp_as_sequence->sq_length)(u);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002210 if (isize < 0)
2211 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002212 if (slice_index(v, isize, &ilow) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002213 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002214 if (slice_index(w, isize, &ihigh) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002215 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002216 return (*tp->tp_as_sequence->sq_slice)(u, ilow, ihigh);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002217}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002218
2219static int
2220assign_subscript(w, key, v) /* w[key] = v */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002221 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 Rossum04691fc1992-08-12 15:35:34 +00002229 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 Rossum10dc2e81990-11-18 17:27:39 +00002234 (func = sq->sq_ass_item) != NULL) {
2235 if (!is_intobject(key)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002236 err_setstr(TypeError,
Guido van Rossum98256aa1991-12-24 13:25:19 +00002237 "sequence subscript must be integer (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002238 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002239 }
Guido van Rossum98256aa1991-12-24 13:25:19 +00002240 else {
2241 int i = getintvalue(key);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002242 if (i < 0) {
2243 int len = (*sq->sq_length)(w);
2244 if (len < 0)
2245 return -1;
2246 i += len;
2247 }
Guido van Rossum98256aa1991-12-24 13:25:19 +00002248 return (*func)(w, i, v);
2249 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002250 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002251 else {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002252 err_setstr(TypeError,
2253 "can't assign to this subscripted object");
2254 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002255 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002256}
2257
Guido van Rossum3f5da241990-12-20 15:06:42 +00002258static int
2259assign_slice(u, v, w, x) /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002260 object *u, *v, *w, *x;
2261{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002262 sequence_methods *sq = u->ob_type->tp_as_sequence;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002263 int ilow, ihigh, isize;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002264 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 Rossum10dc2e81990-11-18 17:27:39 +00002271 }
2272 ilow = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002273 isize = ihigh = (*sq->sq_length)(u);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002274 if (isize < 0)
2275 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002276 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 Rossum10dc2e81990-11-18 17:27:39 +00002281}
2282
2283static int
2284cmp_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 Rossum32c6cdf1991-12-10 13:52:46 +00002291 /* Test recursively */
2292 if (cmp_exception(err, gettupleitem(v, i)))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002293 return 1;
2294 }
2295 return 0;
2296 }
2297 return err == v;
2298}
2299
Guido van Rossum3f5da241990-12-20 15:06:42 +00002300static int
2301cmp_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 Rossum04691fc1992-08-12 15:35:34 +00002332 if (n < 0)
2333 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002334 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 Rossum10dc2e81990-11-18 17:27:39 +00002344static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002345cmp_outcome(op, v, w)
Guido van Rossum5b7313a1992-04-06 13:24:57 +00002346 int op;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002347 register object *v;
2348 register object *w;
2349{
2350 register int cmp;
2351 register int res = 0;
2352 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002353 case IS:
2354 case IS_NOT:
2355 res = (v == w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002356 if (op == (int) IS_NOT)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002357 res = !res;
2358 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002359 case IN:
2360 case NOT_IN:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002361 res = cmp_member(v, w);
2362 if (res < 0)
2363 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002364 if (op == (int) NOT_IN)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002365 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002366 break;
2367 case EXC_MATCH:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002368 res = cmp_exception(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002369 break;
2370 default:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002371 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 Rossum10dc2e81990-11-18 17:27:39 +00002381 }
2382 v = res ? True : False;
2383 INCREF(v);
2384 return v;
2385}
2386
Guido van Rossum3f5da241990-12-20 15:06:42 +00002387static int
2388import_from(locals, v, name)
2389 object *locals;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002390 object *v;
Guido van Rossumdf62e441991-08-16 08:56:04 +00002391 object *name;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002392{
2393 object *w, *x;
2394 w = getmoduledict(v);
Guido van Rossumdf62e441991-08-16 08:56:04 +00002395 if (getstringvalue(name)[0] == '*') {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002396 int pos, err;
Guido van Rossum25831651993-05-19 14:50:45 +00002397 object *name, *value;
2398 pos = 0;
2399 while (mappinggetnext(w, &pos, &name, &value)) {
2400 if (!is_stringobject(name) ||
2401 getstringvalue(name)[0] == '_')
Guido van Rossume9736fc1990-11-18 17:33:06 +00002402 continue;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002403 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 Rossum3f5da241990-12-20 15:06:42 +00002415 return -1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002416 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002417 return 0;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002418 }
2419 else {
Guido van Rossumdf62e441991-08-16 08:56:04 +00002420 x = dict2lookup(w, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002421 if (x == NULL) {
Guido van Rossum98256aa1991-12-24 13:25:19 +00002422 char buf[250];
Guido van Rossum16dfd291992-02-05 11:17:30 +00002423 sprintf(buf, "cannot import name %.230s",
Guido van Rossum98256aa1991-12-24 13:25:19 +00002424 getstringvalue(name));
2425 err_setstr(ImportError, buf);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002426 return -1;
2427 }
2428 else
Guido van Rossumdf62e441991-08-16 08:56:04 +00002429 return dict2insert(locals, name, x);
Guido van Rossume9736fc1990-11-18 17:33:06 +00002430 }
2431}
2432
2433static object *
Guido van Rossum25831651993-05-19 14:50:45 +00002434build_class(methods, bases, name)
2435 object *methods; /* dictionary */
2436 object *bases; /* tuple containing classes */
2437 object *name; /* string */
Guido van Rossume9736fc1990-11-18 17:33:06 +00002438{
Guido van Rossum25831651993-05-19 14:50:45 +00002439 int i;
2440 if (!is_tupleobject(bases)) {
2441 err_setstr(SystemError, "build_class with non-tuple bases");
2442 return NULL;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002443 }
Guido van Rossum25831651993-05-19 14:50:45 +00002444 if (!is_dictobject(methods)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002445 err_setstr(SystemError, "build_class with non-dictionary");
Guido van Rossume9736fc1990-11-18 17:33:06 +00002446 return NULL;
2447 }
Guido van Rossum25831651993-05-19 14:50:45 +00002448 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
2463static int
Guido van Rossumb3f72581993-05-21 19:56:10 +00002464access_statement(name, vmode, f)
Guido van Rossum25831651993-05-19 14:50:45 +00002465 object *name;
Guido van Rossumb3f72581993-05-21 19:56:10 +00002466 object *vmode;
Guido van Rossum25831651993-05-19 14:50:45 +00002467 frameobject *f;
2468{
Guido van Rossumb3f72581993-05-21 19:56:10 +00002469 int mode = getintvalue(vmode);
Guido van Rossum81daa321993-05-20 14:24:46 +00002470 object *value, *ac;
2471 typeobject *type;
2472 int fastind, ret;
2473 fastind = -1;
Guido van Rossum25831651993-05-19 14:50:45 +00002474 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 Rossum81daa321993-05-20 14:24:46 +00002481 fastind = getintvalue(value);
2482 if (0 <= fastind &&
2483 fastind < getlistsize(f->f_fastlocals))
2484 value = getlistitem(f->f_fastlocals, fastind);
Guido van Rossum25831651993-05-19 14:50:45 +00002485 else {
2486 value = NULL;
Guido van Rossum81daa321993-05-20 14:24:46 +00002487 fastind = -1;
Guido van Rossum25831651993-05-19 14:50:45 +00002488 }
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 Rossum81daa321993-05-20 14:24:46 +00002496 if (value != NULL && value != None)
2497 type = value->ob_type;
2498 else
2499 type = NULL;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002500 ac = newaccessobject(value, f->f_locals, type, mode);
Guido van Rossum25831651993-05-19 14:50:45 +00002501 if (ac == NULL)
2502 return -1;
Guido van Rossum81daa321993-05-20 14:24:46 +00002503 if (fastind >= 0)
2504 ret = setlistitem(f->f_fastlocals, fastind, ac);
Guido van Rossum25831651993-05-19 14:50:45 +00002505 else {
2506 ret = dict2insert(f->f_locals, name, ac);
2507 DECREF(ac);
2508 }
2509 return ret;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002510}
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002511
2512static int
2513exec_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}