blob: be1e1745d85b4498fe6d058872ed8aeb402cd5b4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The 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 Rossum681d79a1995-07-18 14:51:37 +000027/* XXX TO DO:
28 XXX how to pass arguments to call_trace?
29 XXX access stuff can probably dereference NULL locals?
30 XXX need to extend apply() to be able to pass keyword args
31 XXX need to be able to call built-in functions with keyword args
32 XXX speed up searching for keywords by using a dictionary
33 XXX unknown keyword shouldn't raise KeyError?
34 XXX document it!
35 */
36
Guido van Rossum3f5da241990-12-20 15:06:42 +000037#include "allobjects.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000038
Guido van Rossum10dc2e81990-11-18 17:27:39 +000039#include "import.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000040#include "sysmodule.h"
Guido van Rossum94390ec1995-01-12 11:37:57 +000041#include "bltinmodule.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000042#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000043#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000044#include "eval.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000045#include "ceval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000046#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000047#include "traceback.h"
Guido van Rossumdb3165e1993-10-18 17:06:59 +000048#include "graminit.h"
49#include "pythonrun.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000050
Guido van Rossumc6004111993-11-05 10:22:19 +000051#include <ctype.h>
52
Guido van Rossume59214e1994-08-30 08:01:59 +000053extern int suppress_print; /* Declared in pythonrun.c, set in pythonmain.c */
54
Guido van Rossum04691fc1992-08-12 15:35:34 +000055/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000056/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000057
Guido van Rossumacbe8da1993-04-15 15:33:52 +000058/* Turn this on if you want to debug the interpreter: */
59/* (This can be on even if NDEBUG is defined) */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000060/* #define DEBUG 1 */
Guido van Rossumacbe8da1993-04-15 15:33:52 +000061
62#if defined(DEBUG) || !defined(NDEBUG)
Guido van Rossum96a42c81992-01-12 02:29:51 +000063/* For debugging the interpreter: */
64#define LLTRACE 1 /* Low-level trace feature */
65#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000066#endif
67
Guido van Rossum5b722181993-03-30 17:46:03 +000068
Guido van Rossum374a9221991-04-04 10:40:29 +000069/* Forward declarations */
Guido van Rossum374a9221991-04-04 10:40:29 +000070
Guido van Rossum681d79a1995-07-18 14:51:37 +000071static object *eval_code2 PROTO((codeobject *,
72 object *, object *,
73 object **, int,
74 object **, int,
75 object **, int,
76 object *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000077#ifdef LLTRACE
78static int prtrace PROTO((object *, char *));
79#endif
Guido van Rossum9c8d70d1992-03-23 18:19:28 +000080static void call_exc_trace PROTO((object **, object**, frameobject *));
81static int call_trace
82 PROTO((object **, object **, frameobject *, char *, object *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000083static object *add PROTO((object *, object *));
84static object *sub PROTO((object *, object *));
85static object *mul PROTO((object *, object *));
86static object *divide PROTO((object *, object *));
Guido van Rossum06186511995-01-07 12:40:10 +000087static object *mod PROTO((object *, object *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000088static object *neg PROTO((object *));
89static object *pos PROTO((object *));
90static object *not PROTO((object *));
91static object *invert PROTO((object *));
92static object *lshift PROTO((object *, object *));
93static object *rshift PROTO((object *, object *));
94static object *and PROTO((object *, object *));
95static object *xor PROTO((object *, object *));
96static object *or PROTO((object *, object *));
Guido van Rossum681d79a1995-07-18 14:51:37 +000097static object *call_builtin PROTO((object *, object *, object *));
98static object *call_function PROTO((object *, object *, object *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000099static object *apply_subscript PROTO((object *, object *));
100static object *loop_subscript PROTO((object *, object *));
101static int slice_index PROTO((object *, int, int *));
102static object *apply_slice PROTO((object *, object *, object *));
103static int assign_subscript PROTO((object *, object *, object *));
104static int assign_slice PROTO((object *, object *, object *, object *));
105static int cmp_exception PROTO((object *, object *));
106static int cmp_member PROTO((object *, object *));
Guido van Rossum5b7313a1992-04-06 13:24:57 +0000107static object *cmp_outcome PROTO((int, object *, object *));
Guido van Rossum0a066c01992-03-27 17:29:15 +0000108static int import_from PROTO((object *, object *, object *));
Guido van Rossum25831651993-05-19 14:50:45 +0000109static object *build_class PROTO((object *, object *, object *));
Guido van Rossumb3f72581993-05-21 19:56:10 +0000110static int access_statement PROTO((object *, object *, frameobject *));
Guido van Rossumdb3165e1993-10-18 17:06:59 +0000111static int exec_statement PROTO((object *, object *, object *));
Guido van Rossum24c13741995-02-14 09:42:43 +0000112static object *find_from_args PROTO((frameobject *, int));
Guido van Rossum374a9221991-04-04 10:40:29 +0000113
114
Guido van Rossum0a066c01992-03-27 17:29:15 +0000115/* Pointer to current frame, used to link new frames to */
116
Guido van Rossum374a9221991-04-04 10:40:29 +0000117static frameobject *current_frame;
118
Guido van Rossume59214e1994-08-30 08:01:59 +0000119#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000120
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000121#include <errno.h>
122#include "thread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000123
Guido van Rossuma9672091994-09-14 13:31:22 +0000124static type_lock interpreter_lock = 0;
125static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000126
127void
128init_save_thread()
129{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000130 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000131 return;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000132 interpreter_lock = allocate_lock();
133 acquire_lock(interpreter_lock, 1);
Guido van Rossuma9672091994-09-14 13:31:22 +0000134 main_thread = get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000135}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000136
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000137#endif
138
Guido van Rossumff4949e1992-08-05 19:58:53 +0000139/* Functions save_thread and restore_thread are always defined so
140 dynamically loaded modules needn't be compiled separately for use
141 with and without threads: */
142
Guido van Rossum04691fc1992-08-12 15:35:34 +0000143object *
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000144save_thread()
145{
Guido van Rossume59214e1994-08-30 08:01:59 +0000146#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000147 if (interpreter_lock) {
Guido van Rossum04691fc1992-08-12 15:35:34 +0000148 object *res;
149 res = (object *)current_frame;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000150 current_frame = NULL;
151 release_lock(interpreter_lock);
152 return res;
153 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000154#endif
Guido van Rossum9575a441993-04-07 14:06:14 +0000155 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000156}
157
158void
159restore_thread(x)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000160 object *x;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000161{
Guido van Rossume59214e1994-08-30 08:01:59 +0000162#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000163 if (interpreter_lock) {
164 int err;
165 err = errno;
166 acquire_lock(interpreter_lock, 1);
167 errno = err;
168 current_frame = (frameobject *)x;
169 }
170#endif
171}
172
173
Guido van Rossuma9672091994-09-14 13:31:22 +0000174/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
175 signal handlers or Mac I/O completion routines) can schedule calls
176 to a function to be called synchronously.
177 The synchronous function is called with one void* argument.
178 It should return 0 for success or -1 for failure -- failure should
179 be accompanied by an exception.
180
181 If registry succeeds, the registry function returns 0; if it fails
182 (e.g. due to too many pending calls) it returns -1 (without setting
183 an exception condition).
184
185 Note that because registry may occur from within signal handlers,
186 or other asynchronous events, calling malloc() is unsafe!
187
188#ifdef WITH_THREAD
189 Any thread can schedule pending calls, but only the main thread
190 will execute them.
191#endif
192
193 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
194 There are two possible race conditions:
195 (1) nested asynchronous registry calls;
196 (2) registry calls made while pending calls are being processed.
197 While (1) is very unlikely, (2) is a real possibility.
198 The current code is safe against (2), but not against (1).
199 The safety against (2) is derived from the fact that only one
200 thread (the main thread) ever takes things out of the queue.
201*/
202
203#define NPENDINGCALLS 32
204static struct {
205 int (*func) PROTO((ANY *));
206 ANY *arg;
207} pendingcalls[NPENDINGCALLS];
208static volatile int pendingfirst = 0;
209static volatile int pendinglast = 0;
210
211int
212Py_AddPendingCall(func, arg)
213 int (*func) PROTO((ANY *));
214 ANY *arg;
215{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000216 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000217 int i, j;
218 /* XXX Begin critical section */
219 /* XXX If you want this to be safe against nested
220 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000221 if (busy)
222 return -1;
223 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000224 i = pendinglast;
225 j = (i + 1) % NPENDINGCALLS;
226 if (j == pendingfirst)
227 return -1; /* Queue full */
228 pendingcalls[i].func = func;
229 pendingcalls[i].arg = arg;
230 pendinglast = j;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000231 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000232 /* XXX End critical section */
233 return 0;
234}
235
Guido van Rossum180d7b41994-09-29 09:45:57 +0000236int
237Py_MakePendingCalls()
Guido van Rossuma9672091994-09-14 13:31:22 +0000238{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000239 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000240#ifdef WITH_THREAD
241 if (get_thread_ident() != main_thread)
242 return 0;
243#endif
Guido van Rossum180d7b41994-09-29 09:45:57 +0000244 if (busy)
245 return 0;
246 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000247 for (;;) {
248 int i;
249 int (*func) PROTO((ANY *));
250 ANY *arg;
251 i = pendingfirst;
252 if (i == pendinglast)
253 break; /* Queue empty */
254 func = pendingcalls[i].func;
255 arg = pendingcalls[i].arg;
256 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000257 if (func(arg) < 0) {
258 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000259 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000260 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000261 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000262 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000263 return 0;
264}
265
266
Guido van Rossum374a9221991-04-04 10:40:29 +0000267/* Status code for main loop (reason for stack unwind) */
268
269enum why_code {
270 WHY_NOT, /* No error */
271 WHY_EXCEPTION, /* Exception occurred */
272 WHY_RERAISE, /* Exception re-raised by 'finally' */
273 WHY_RETURN, /* 'return' statement */
274 WHY_BREAK /* 'break' statement */
275};
276
277
Guido van Rossum681d79a1995-07-18 14:51:37 +0000278/* Backward compatible interface */
Guido van Rossum374a9221991-04-04 10:40:29 +0000279
280object *
Guido van Rossum681d79a1995-07-18 14:51:37 +0000281eval_code(co, globals, locals)
Guido van Rossum374a9221991-04-04 10:40:29 +0000282 codeobject *co;
283 object *globals;
284 object *locals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000285{
286 return eval_code2(co,
287 globals, locals,
288 (object **)NULL, 0,
289 (object **)NULL, 0,
290 (object **)NULL, 0,
291 (object *)NULL);
292}
293
294
295/* Interpreter main loop */
296
297static object *
298eval_code2(co, globals, locals,
299 args, argcount, kws, kwcount, defs, defcount, owner)
300 codeobject *co;
301 object *globals;
302 object *locals;
303 object **args;
304 int argcount;
305 object **kws; /* length: 2*kwcount */
306 int kwcount;
307 object **defs;
308 int defcount;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000309 object *owner;
Guido van Rossum374a9221991-04-04 10:40:29 +0000310{
311 register unsigned char *next_instr;
312 register int opcode; /* Current opcode */
313 register int oparg; /* Current opcode argument, if any */
314 register object **stack_pointer;
315 register enum why_code why; /* Reason for block stack unwind */
316 register int err; /* Error status -- nonzero if error */
317 register object *x; /* Result object -- NULL if error */
318 register object *v; /* Temporary objects popped off stack */
319 register object *w;
320 register object *u;
321 register object *t;
322 register frameobject *f; /* Current frame */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000323 register object **fastlocals;
324 object *retval; /* Return value */
Guido van Rossumb3f72581993-05-21 19:56:10 +0000325 int defmode = 0; /* Default access mode for new variables */
Guido van Rossum96a42c81992-01-12 02:29:51 +0000326#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000327 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000328#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +0000329#if defined(DEBUG) || defined(LLTRACE)
330 /* Make it easier to find out where we are with a debugger */
Guido van Rossum99bec951992-09-03 20:29:45 +0000331 char *filename = getstringvalue(co->co_filename);
332#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000333
334/* Code access macros */
335
336#define GETCONST(i) Getconst(f, i)
337#define GETNAME(i) Getname(f, i)
338#define GETNAMEV(i) Getnamev(f, i)
339#define FIRST_INSTR() (GETUSTRINGVALUE(f->f_code->co_code))
340#define INSTR_OFFSET() (next_instr - FIRST_INSTR())
341#define NEXTOP() (*next_instr++)
342#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
343#define JUMPTO(x) (next_instr = FIRST_INSTR() + (x))
344#define JUMPBY(x) (next_instr += (x))
345
346/* Stack manipulation macros */
347
348#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
349#define EMPTY() (STACK_LEVEL() == 0)
350#define TOP() (stack_pointer[-1])
351#define BASIC_PUSH(v) (*stack_pointer++ = (v))
352#define BASIC_POP() (*--stack_pointer)
353
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000354#define CHECK_STACK(n) (STACK_LEVEL() + (n) < f->f_nvalues || \
355 (stack_pointer = extend_stack(f, STACK_LEVEL(), n)))
356
Guido van Rossum96a42c81992-01-12 02:29:51 +0000357#ifdef LLTRACE
358#define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
359#define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
Guido van Rossum374a9221991-04-04 10:40:29 +0000360#else
361#define PUSH(v) BASIC_PUSH(v)
362#define POP() BASIC_POP()
363#endif
364
Guido van Rossum681d79a1995-07-18 14:51:37 +0000365/* Local variable macros */
366
367#define GETLOCAL(i) (fastlocals[i])
368#define SETLOCAL(i, value) do { XDECREF(GETLOCAL(i)); \
369 GETLOCAL(i) = value; } while (0)
370
371 if (globals == NULL) {
372 err_setstr(SystemError, "eval_code2: NULL globals");
Guido van Rossum6135a871995-01-09 17:53:26 +0000373 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000374 }
375
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000376#ifdef LLTRACE
377 lltrace = dictlookup(globals, "__lltrace__") != NULL;
378#endif
379
Guido van Rossum374a9221991-04-04 10:40:29 +0000380 f = newframeobject(
381 current_frame, /*back*/
382 co, /*code*/
383 globals, /*globals*/
384 locals, /*locals*/
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000385 owner, /*owner*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000386 50, /*nvalues*/
387 20); /*nblocks*/
388 if (f == NULL)
389 return NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000390
Guido van Rossum374a9221991-04-04 10:40:29 +0000391 current_frame = f;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000392
Guido van Rossum681d79a1995-07-18 14:51:37 +0000393 if (co->co_nlocals > 0)
394 fastlocals = ((listobject *)f->f_fastlocals)->ob_item;
395
396 if (co->co_argcount > 0 ||
397 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
398 int i;
399 int n = argcount;
400 object *kwdict = NULL;
401 if (co->co_flags & CO_VARKEYWORDS) {
402 kwdict = newmappingobject();
403 if (kwdict == NULL)
404 goto fail;
405 }
406 if (argcount > co->co_argcount) {
407 if (!(co->co_flags & CO_VARARGS)) {
408 err_setstr(TypeError, "too many arguments");
409 goto fail;
410 }
411 n = co->co_argcount;
412 }
413 for (i = 0; i < n; i++) {
414 x = args[i];
415 INCREF(x);
416 SETLOCAL(i, x);
417 }
418 if (co->co_flags & CO_VARARGS) {
419 u = newtupleobject(argcount - n);
420 for (i = n; i < argcount; i++) {
421 x = args[i];
422 INCREF(x);
423 SETTUPLEITEM(u, i-n, x);
424 }
425 SETLOCAL(co->co_argcount, u);
426 }
427 for (i = 0; i < kwcount; i++) {
428 object *keyword = kws[2*i];
429 object *value = kws[2*i + 1];
430 int j;
431 /* XXX slow -- speed up using dictionary? */
432 for (j = 0; j < co->co_argcount; j++) {
433 object *nm = GETTUPLEITEM(co->co_varnames, j);
434 if (cmpobject(keyword, nm) == 0)
435 break;
436 }
437 if (j >= co->co_argcount) {
438 if (kwdict == NULL) {
439 err_setval(KeyError/*XXX*/, keyword);
440 goto fail;
441 }
442 mappinginsert(kwdict, keyword, value);
443 }
444 else {
445 if (GETLOCAL(j) != NULL) {
446 err_setstr(TypeError,
447 "keyword parameter redefined");
448 goto fail;
449 }
450 INCREF(value);
451 SETLOCAL(j, value);
452 }
453 }
454 if (argcount < co->co_argcount) {
455 int m = co->co_argcount - defcount;
456 for (i = argcount; i < m; i++) {
457 if (GETLOCAL(i) == NULL) {
458 err_setstr(TypeError,
459 "not enough arguments");
460 goto fail;
461 }
462 }
463 if (n > m)
464 i = n - m;
465 else
466 i = 0;
467 for (; i < defcount; i++) {
468 if (GETLOCAL(m+i) == NULL) {
469 object *def = defs[i];
470 INCREF(def);
471 SETLOCAL(m+i, def);
472 }
473 }
474 }
475 if (kwdict != NULL) {
476 i = co->co_argcount;
477 if (co->co_flags & CO_VARARGS)
478 i++;
479 SETLOCAL(i, kwdict);
480 }
481 if (0) {
482 fail:
483 XDECREF(kwdict);
484 goto fail2;
485 }
486 }
487 else {
488 if (argcount > 0 || kwcount > 0) {
489 err_setstr(TypeError, "no arguments expected");
490 fail2:
491 current_frame = f->f_back;
492 DECREF(f);
493 return NULL;
494 }
495 }
496
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000497 if (sys_trace != NULL) {
498 /* sys_trace, if defined, is a function that will
Guido van Rossum96a42c81992-01-12 02:29:51 +0000499 be called on *every* entry to a code block.
500 Its return value, if not None, is a function that
501 will be called at the start of each executed line
502 of code. (Actually, the function must return
503 itself in order to continue tracing.)
504 The trace functions are called with three arguments:
505 a pointer to the current frame, a string indicating
506 why the function is called, and an argument which
507 depends on the situation. The global trace function
508 (sys.trace) is also called whenever an exception
509 is detected. */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000510 if (call_trace(&sys_trace, &f->f_trace, f, "call",
511 None/*XXX how to compute arguments now?*/)) {
Guido van Rossum96a42c81992-01-12 02:29:51 +0000512 /* Trace function raised an error */
Guido van Rossum96a42c81992-01-12 02:29:51 +0000513 current_frame = f->f_back;
514 DECREF(f);
515 return NULL;
516 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000517 }
518
519 if (sys_profile != NULL) {
520 /* Similar for sys_profile, except it needn't return
521 itself and isn't called for "line" events */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000522 if (call_trace(&sys_profile, (object**)0, f, "call",
523 None/*XXX*/)) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000524 current_frame = f->f_back;
525 DECREF(f);
526 return NULL;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000527 }
528 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000529
Guido van Rossum374a9221991-04-04 10:40:29 +0000530 next_instr = GETUSTRINGVALUE(f->f_code->co_code);
Guido van Rossum374a9221991-04-04 10:40:29 +0000531 stack_pointer = f->f_valuestack;
532
Guido van Rossum374a9221991-04-04 10:40:29 +0000533 why = WHY_NOT;
534 err = 0;
535 x = None; /* Not a reference, just anything non-NULL */
Guido van Rossum374a9221991-04-04 10:40:29 +0000536
537 for (;;) {
Guido van Rossum4965bc81991-05-14 11:51:49 +0000538 static int ticker;
Guido van Rossum374a9221991-04-04 10:40:29 +0000539
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000540 /* Do periodic things.
541 Doing this every time through the loop would add
542 too much overhead (a function call per instruction).
Guido van Rossume59214e1994-08-30 08:01:59 +0000543 So we do it only every Nth instruction. */
Guido van Rossum374a9221991-04-04 10:40:29 +0000544
Guido van Rossuma9672091994-09-14 13:31:22 +0000545 if (pendingfirst != pendinglast) {
Guido van Rossum180d7b41994-09-29 09:45:57 +0000546 if (Py_MakePendingCalls() < 0) {
Guido van Rossuma9672091994-09-14 13:31:22 +0000547 why = WHY_EXCEPTION;
548 goto on_error;
549 }
550 }
551
Guido van Rossum374a9221991-04-04 10:40:29 +0000552 if (--ticker < 0) {
Guido van Rossum6135a871995-01-09 17:53:26 +0000553 ticker = sys_checkinterval;
Guido van Rossume59214e1994-08-30 08:01:59 +0000554 if (sigcheck()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000555 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000556 goto on_error;
557 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000558
Guido van Rossume59214e1994-08-30 08:01:59 +0000559#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000560 if (interpreter_lock) {
561 /* Give another thread a chance */
562
563 current_frame = NULL;
564 release_lock(interpreter_lock);
565
566 /* Other threads may run now */
567
568 acquire_lock(interpreter_lock, 1);
569 current_frame = f;
570 }
571#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000572 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000573
Guido van Rossum374a9221991-04-04 10:40:29 +0000574 /* Extract opcode and argument */
Guido van Rossum99bec951992-09-03 20:29:45 +0000575
576#ifdef DEBUG
577 f->f_lasti = INSTR_OFFSET();
578#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000579
580 opcode = NEXTOP();
581 if (HAS_ARG(opcode))
582 oparg = NEXTARG();
583
Guido van Rossum96a42c81992-01-12 02:29:51 +0000584#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000585 /* Instruction tracing */
586
Guido van Rossum96a42c81992-01-12 02:29:51 +0000587 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000588 if (HAS_ARG(opcode)) {
589 printf("%d: %d, %d\n",
590 (int) (INSTR_OFFSET() - 3),
591 opcode, oparg);
592 }
593 else {
594 printf("%d: %d\n",
595 (int) (INSTR_OFFSET() - 1), opcode);
596 }
597 }
598#endif
599
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000600 if (!CHECK_STACK(3)) {
601 x = NULL;
602 break;
603 }
604
Guido van Rossum374a9221991-04-04 10:40:29 +0000605 /* Main switch on opcode */
606
607 switch (opcode) {
608
609 /* BEWARE!
610 It is essential that any operation that fails sets either
611 x to NULL, err to nonzero, or why to anything but WHY_NOT,
612 and that no operation that succeeds does this! */
613
614 /* case STOP_CODE: this is an error! */
615
616 case POP_TOP:
617 v = POP();
618 DECREF(v);
619 break;
620
621 case ROT_TWO:
622 v = POP();
623 w = POP();
624 PUSH(v);
625 PUSH(w);
626 break;
627
628 case ROT_THREE:
629 v = POP();
630 w = POP();
631 x = POP();
632 PUSH(v);
633 PUSH(x);
634 PUSH(w);
635 break;
636
637 case DUP_TOP:
638 v = TOP();
639 INCREF(v);
640 PUSH(v);
641 break;
642
643 case UNARY_POSITIVE:
644 v = POP();
645 x = pos(v);
646 DECREF(v);
647 PUSH(x);
648 break;
649
650 case UNARY_NEGATIVE:
651 v = POP();
652 x = neg(v);
653 DECREF(v);
654 PUSH(x);
655 break;
656
657 case UNARY_NOT:
658 v = POP();
659 x = not(v);
660 DECREF(v);
661 PUSH(x);
662 break;
663
664 case UNARY_CONVERT:
665 v = POP();
666 x = reprobject(v);
667 DECREF(v);
668 PUSH(x);
669 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000670
671 case UNARY_INVERT:
672 v = POP();
673 x = invert(v);
674 DECREF(v);
675 PUSH(x);
676 break;
Guido van Rossum374a9221991-04-04 10:40:29 +0000677
678 case BINARY_MULTIPLY:
679 w = POP();
680 v = POP();
681 x = mul(v, w);
682 DECREF(v);
683 DECREF(w);
684 PUSH(x);
685 break;
686
687 case BINARY_DIVIDE:
688 w = POP();
689 v = POP();
690 x = divide(v, w);
691 DECREF(v);
692 DECREF(w);
693 PUSH(x);
694 break;
695
696 case BINARY_MODULO:
697 w = POP();
698 v = POP();
Guido van Rossum06186511995-01-07 12:40:10 +0000699 x = mod(v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000700 DECREF(v);
701 DECREF(w);
702 PUSH(x);
703 break;
704
705 case BINARY_ADD:
706 w = POP();
707 v = POP();
708 x = add(v, w);
709 DECREF(v);
710 DECREF(w);
711 PUSH(x);
712 break;
713
714 case BINARY_SUBTRACT:
715 w = POP();
716 v = POP();
717 x = sub(v, w);
718 DECREF(v);
719 DECREF(w);
720 PUSH(x);
721 break;
722
723 case BINARY_SUBSCR:
724 w = POP();
725 v = POP();
726 x = apply_subscript(v, w);
727 DECREF(v);
728 DECREF(w);
729 PUSH(x);
730 break;
731
Guido van Rossum7928cd71991-10-24 14:59:31 +0000732 case BINARY_LSHIFT:
733 w = POP();
734 v = POP();
735 x = lshift(v, w);
736 DECREF(v);
737 DECREF(w);
738 PUSH(x);
739 break;
740
741 case BINARY_RSHIFT:
742 w = POP();
743 v = POP();
744 x = rshift(v, w);
745 DECREF(v);
746 DECREF(w);
747 PUSH(x);
748 break;
749
750 case BINARY_AND:
751 w = POP();
752 v = POP();
753 x = and(v, w);
754 DECREF(v);
755 DECREF(w);
756 PUSH(x);
757 break;
758
759 case BINARY_XOR:
760 w = POP();
761 v = POP();
762 x = xor(v, w);
763 DECREF(v);
764 DECREF(w);
765 PUSH(x);
766 break;
767
768 case BINARY_OR:
769 w = POP();
770 v = POP();
771 x = or(v, w);
772 DECREF(v);
773 DECREF(w);
774 PUSH(x);
775 break;
776
Guido van Rossum374a9221991-04-04 10:40:29 +0000777 case SLICE+0:
778 case SLICE+1:
779 case SLICE+2:
780 case SLICE+3:
781 if ((opcode-SLICE) & 2)
782 w = POP();
783 else
784 w = NULL;
785 if ((opcode-SLICE) & 1)
786 v = POP();
787 else
788 v = NULL;
789 u = POP();
790 x = apply_slice(u, v, w);
791 DECREF(u);
792 XDECREF(v);
793 XDECREF(w);
794 PUSH(x);
795 break;
796
797 case STORE_SLICE+0:
798 case STORE_SLICE+1:
799 case STORE_SLICE+2:
800 case STORE_SLICE+3:
801 if ((opcode-STORE_SLICE) & 2)
802 w = POP();
803 else
804 w = NULL;
805 if ((opcode-STORE_SLICE) & 1)
806 v = POP();
807 else
808 v = NULL;
809 u = POP();
810 t = POP();
811 err = assign_slice(u, v, w, t); /* u[v:w] = t */
812 DECREF(t);
813 DECREF(u);
814 XDECREF(v);
815 XDECREF(w);
816 break;
817
818 case DELETE_SLICE+0:
819 case DELETE_SLICE+1:
820 case DELETE_SLICE+2:
821 case DELETE_SLICE+3:
822 if ((opcode-DELETE_SLICE) & 2)
823 w = POP();
824 else
825 w = NULL;
826 if ((opcode-DELETE_SLICE) & 1)
827 v = POP();
828 else
829 v = NULL;
830 u = POP();
831 err = assign_slice(u, v, w, (object *)NULL);
832 /* del u[v:w] */
833 DECREF(u);
834 XDECREF(v);
835 XDECREF(w);
836 break;
837
838 case STORE_SUBSCR:
839 w = POP();
840 v = POP();
841 u = POP();
842 /* v[w] = u */
843 err = assign_subscript(v, w, u);
844 DECREF(u);
845 DECREF(v);
846 DECREF(w);
847 break;
848
849 case DELETE_SUBSCR:
850 w = POP();
851 v = POP();
852 /* del v[w] */
853 err = assign_subscript(v, w, (object *)NULL);
854 DECREF(v);
855 DECREF(w);
856 break;
857
858 case PRINT_EXPR:
859 v = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +0000860 /* Print value except if procedure result */
Guido van Rossume59214e1994-08-30 08:01:59 +0000861 /* Before printing, also assign to '_' */
862 if (v != None &&
Guido van Rossum6135a871995-01-09 17:53:26 +0000863 (err = dictinsert(f->f_builtins, "_", v)) == 0 &&
Guido van Rossume59214e1994-08-30 08:01:59 +0000864 !suppress_print) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000865 flushline();
Guido van Rossum3165fe61992-09-25 21:59:05 +0000866 x = sysget("stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +0000867 err = writeobject(v, x, 0);
Guido van Rossumb4e7e251995-01-17 16:27:25 +0000868 softspace(x, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000869 flushline();
870 }
871 DECREF(v);
872 break;
873
874 case PRINT_ITEM:
875 v = POP();
Guido van Rossum3165fe61992-09-25 21:59:05 +0000876 w = sysget("stdout");
877 if (softspace(w, 1))
878 writestring(" ", w);
Guido van Rossumc6004111993-11-05 10:22:19 +0000879 err = writeobject(v, w, PRINT_RAW);
880 if (err == 0 && is_stringobject(v)) {
881 /* XXX move into writeobject() ? */
Guido van Rossum374a9221991-04-04 10:40:29 +0000882 char *s = getstringvalue(v);
883 int len = getstringsize(v);
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000884 if (len > 0 &&
885 isspace(Py_CHARMASK(s[len-1])) &&
Guido van Rossumc6004111993-11-05 10:22:19 +0000886 s[len-1] != ' ')
Guido van Rossum3165fe61992-09-25 21:59:05 +0000887 softspace(w, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +0000888 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000889 DECREF(v);
890 break;
891
892 case PRINT_NEWLINE:
Guido van Rossum3165fe61992-09-25 21:59:05 +0000893 x = sysget("stdout");
894 if (x == NULL)
895 err_setstr(RuntimeError, "lost sys.stdout");
896 else {
897 writestring("\n", x);
898 softspace(x, 0);
899 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000900 break;
901
902 case BREAK_LOOP:
903 why = WHY_BREAK;
904 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +0000905
Guido van Rossumf10570b1995-07-07 22:53:21 +0000906 case RAISE_VARARGS:
907 u = v = w = NULL;
908 switch (oparg) {
909 case 3:
910 u = POP(); /* traceback */
911 if (u == None) {
912 DECREF(u);
913 u = NULL;
914 }
Guido van Rossum9d78d8d1995-09-18 21:29:36 +0000915 else if (!PyTraceBack_Check(u)) {
Guido van Rossumf10570b1995-07-07 22:53:21 +0000916 err_setstr(TypeError,
917 "raise 3rd arg must be traceback or None");
918 goto raise_error;
919 }
920 /* Fallthrough */
921 case 2:
922 v = POP(); /* value */
923 /* Fallthrough */
924 case 1:
925 w = POP(); /* exc */
926 break;
927 default:
928 err_setstr(SystemError,
929 "bad RAISE_VARARGS oparg");
930 goto raise_error;
931 }
932 if (v == NULL) {
933 v = None;
934 INCREF(v);
935 }
Guido van Rossum32c6cdf1991-12-10 13:52:46 +0000936 /* A tuple is equivalent to its first element here */
Guido van Rossume59214e1994-08-30 08:01:59 +0000937 while (is_tupleobject(w) && gettuplesize(w) > 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000938 t = w;
939 w = GETTUPLEITEM(w, 0);
Guido van Rossum1919ca71995-01-20 16:55:14 +0000940 INCREF(w);
Guido van Rossumf10570b1995-07-07 22:53:21 +0000941 DECREF(t);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +0000942 }
Guido van Rossum8bf7c481995-01-26 00:41:04 +0000943 if (is_stringobject(w)) {
Guido van Rossumf10570b1995-07-07 22:53:21 +0000944 ;
Guido van Rossum8bf7c481995-01-26 00:41:04 +0000945 } else if (is_classobject(w)) {
946 if (!is_instanceobject(v)
947 || !issubclass((object*)((instanceobject*)v)->in_class,
Guido van Rossumf10570b1995-07-07 22:53:21 +0000948 w)) {
Guido van Rossum8bf7c481995-01-26 00:41:04 +0000949 err_setstr(TypeError,
950 "a class exception must have a value that is an instance of the class");
Guido van Rossumf10570b1995-07-07 22:53:21 +0000951 goto raise_error;
952 }
Guido van Rossum8bf7c481995-01-26 00:41:04 +0000953 } else if (is_instanceobject(w)) {
Guido van Rossumf10570b1995-07-07 22:53:21 +0000954 if (v != None) {
Guido van Rossum8bf7c481995-01-26 00:41:04 +0000955 err_setstr(TypeError,
956 "an instance exception may not have a separate value");
Guido van Rossumf10570b1995-07-07 22:53:21 +0000957 goto raise_error;
958 }
Guido van Rossum8bf7c481995-01-26 00:41:04 +0000959 else {
960 DECREF(v);
961 v = w;
962 w = (object*) ((instanceobject*)w)->in_class;
963 INCREF(w);
Guido van Rossum8bf7c481995-01-26 00:41:04 +0000964 }
Guido van Rossumf10570b1995-07-07 22:53:21 +0000965 }
966 else {
Guido van Rossum8bf7c481995-01-26 00:41:04 +0000967 err_setstr(TypeError,
968 "exceptions must be strings, classes, or instances");
Guido van Rossumf10570b1995-07-07 22:53:21 +0000969 goto raise_error;
970 }
971 err_restore(w, v, u);
972 if (u == NULL)
973 why = WHY_EXCEPTION;
974 else
975 why = WHY_RERAISE;
976 break;
977 raise_error:
978 XDECREF(v);
979 XDECREF(w);
980 XDECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +0000981 why = WHY_EXCEPTION;
982 break;
983
984 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +0000985 if ((x = f->f_locals) == NULL) {
986 err_setstr(SystemError, "no locals");
987 break;
988 }
989 INCREF(x);
990 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +0000991 break;
992
993 case RETURN_VALUE:
994 retval = POP();
995 why = WHY_RETURN;
996 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +0000997
Guido van Rossumdb3165e1993-10-18 17:06:59 +0000998 case EXEC_STMT:
999 w = POP();
1000 v = POP();
1001 u = POP();
1002 err = exec_statement(u, v, w);
1003 DECREF(u);
1004 DECREF(v);
1005 DECREF(w);
1006 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001007
Guido van Rossum374a9221991-04-04 10:40:29 +00001008 case POP_BLOCK:
1009 {
1010 block *b = pop_block(f);
1011 while (STACK_LEVEL() > b->b_level) {
1012 v = POP();
1013 DECREF(v);
1014 }
1015 }
1016 break;
1017
1018 case END_FINALLY:
1019 v = POP();
1020 if (is_intobject(v)) {
1021 why = (enum why_code) getintvalue(v);
1022 if (why == WHY_RETURN)
1023 retval = POP();
1024 }
Guido van Rossum1d339e81995-02-17 15:04:21 +00001025 else if (is_stringobject(v) || is_classobject(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001026 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001027 u = POP();
1028 err_restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001029 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001030 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001031 }
1032 else if (v != None) {
1033 err_setstr(SystemError,
1034 "'finally' pops bad exception");
1035 why = WHY_EXCEPTION;
1036 }
1037 DECREF(v);
1038 break;
1039
1040 case BUILD_CLASS:
Guido van Rossum25831651993-05-19 14:50:45 +00001041 u = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001042 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +00001043 w = POP();
1044 x = build_class(u, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001045 PUSH(x);
Guido van Rossum25831651993-05-19 14:50:45 +00001046 DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001047 DECREF(v);
1048 DECREF(w);
1049 break;
1050
1051 case STORE_NAME:
1052 w = GETNAMEV(oparg);
1053 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001054 if ((x = f->f_locals) == NULL) {
1055 err_setstr(SystemError, "no locals");
1056 break;
1057 }
1058 u = dict2lookup(x, w);
Guido van Rossumb3f72581993-05-21 19:56:10 +00001059 if (u == NULL) {
1060 if (defmode != 0) {
1061 if (v != None)
1062 u = (object *)v->ob_type;
1063 else
1064 u = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001065 x = newaccessobject(v, x,
Guido van Rossumb3f72581993-05-21 19:56:10 +00001066 (typeobject *)u,
1067 defmode);
1068 DECREF(v);
1069 if (x == NULL)
1070 break;
1071 v = x;
1072 }
1073 }
1074 else if (is_accessobject(u)) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001075 err = setaccessvalue(u, x, v);
Guido van Rossum25831651993-05-19 14:50:45 +00001076 DECREF(v);
1077 break;
1078 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001079 err = dict2insert(x, w, v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001080 DECREF(v);
1081 break;
1082
1083 case DELETE_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001084 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001085 if ((x = f->f_locals) == NULL) {
1086 err_setstr(SystemError, "no locals");
1087 break;
1088 }
1089 u = dict2lookup(x, w);
Guido van Rossum25831651993-05-19 14:50:45 +00001090 if (u != NULL && is_accessobject(u)) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001091 err = setaccessvalue(u, x,
Guido van Rossum25831651993-05-19 14:50:45 +00001092 (object *)NULL);
1093 break;
1094 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001095 if ((err = dict2remove(x, w)) != 0)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001096 err_setval(NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001097 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001098
1099#ifdef CASE_TOO_BIG
1100 default: switch (opcode) {
1101#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001102
1103 case UNPACK_TUPLE:
1104 v = POP();
1105 if (!is_tupleobject(v)) {
1106 err_setstr(TypeError, "unpack non-tuple");
1107 why = WHY_EXCEPTION;
1108 }
1109 else if (gettuplesize(v) != oparg) {
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001110 err_setstr(ValueError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001111 "unpack tuple of wrong size");
1112 why = WHY_EXCEPTION;
1113 }
1114 else {
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001115 if (!CHECK_STACK(oparg)) {
1116 x = NULL;
1117 break;
1118 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001119 for (; --oparg >= 0; ) {
Guido van Rossum8d617a61995-03-09 12:12:11 +00001120 w = GETTUPLEITEM(v, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001121 INCREF(w);
1122 PUSH(w);
1123 }
1124 }
1125 DECREF(v);
1126 break;
1127
1128 case UNPACK_LIST:
1129 v = POP();
1130 if (!is_listobject(v)) {
1131 err_setstr(TypeError, "unpack non-list");
1132 why = WHY_EXCEPTION;
1133 }
1134 else if (getlistsize(v) != oparg) {
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001135 err_setstr(ValueError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001136 "unpack list of wrong size");
1137 why = WHY_EXCEPTION;
1138 }
1139 else {
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001140 if (!CHECK_STACK(oparg)) {
1141 x = NULL;
1142 break;
1143 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001144 for (; --oparg >= 0; ) {
1145 w = getlistitem(v, oparg);
1146 INCREF(w);
1147 PUSH(w);
1148 }
1149 }
1150 DECREF(v);
1151 break;
1152
1153 case STORE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001154 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001155 v = POP();
1156 u = POP();
Guido van Rossum9bfef441993-03-29 10:43:31 +00001157 err = setattro(v, w, u); /* v.w = u */
Guido van Rossum374a9221991-04-04 10:40:29 +00001158 DECREF(v);
1159 DECREF(u);
1160 break;
1161
1162 case DELETE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001163 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001164 v = POP();
Guido van Rossum9bfef441993-03-29 10:43:31 +00001165 err = setattro(v, w, (object *)NULL); /* del v.w */
Guido van Rossum374a9221991-04-04 10:40:29 +00001166 DECREF(v);
1167 break;
1168
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001169 case STORE_GLOBAL:
1170 w = GETNAMEV(oparg);
1171 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001172 if (f->f_locals != NULL) {
1173 u = dict2lookup(f->f_locals, w);
1174 if (u != NULL && is_accessobject(u)) {
1175 err = setaccessvalue(u, f->f_globals,
1176 v);
1177 DECREF(v);
1178 break;
1179 }
Guido van Rossum25831651993-05-19 14:50:45 +00001180 }
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001181 err = dict2insert(f->f_globals, w, v);
1182 DECREF(v);
1183 break;
1184
1185 case DELETE_GLOBAL:
1186 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001187 if (f->f_locals != NULL) {
1188 u = dict2lookup(f->f_locals, w);
1189 if (u != NULL && is_accessobject(u)) {
1190 err = setaccessvalue(u, f->f_globals,
1191 (object *)NULL);
1192 break;
1193 }
Guido van Rossum25831651993-05-19 14:50:45 +00001194 }
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001195 if ((err = dict2remove(f->f_globals, w)) != 0)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001196 err_setval(NameError, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001197 break;
1198
Guido van Rossum374a9221991-04-04 10:40:29 +00001199 case LOAD_CONST:
1200 x = GETCONST(oparg);
1201 INCREF(x);
1202 PUSH(x);
1203 break;
1204
1205 case LOAD_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001206 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001207 if ((x = f->f_locals) == NULL) {
1208 err_setstr(SystemError, "no locals");
1209 break;
1210 }
1211 x = dict2lookup(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001212 if (x == NULL) {
Guido van Rossumdf62e441991-08-16 08:56:04 +00001213 err_clear();
1214 x = dict2lookup(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001215 if (x == NULL) {
Guido van Rossumdf62e441991-08-16 08:56:04 +00001216 err_clear();
Guido van Rossum6135a871995-01-09 17:53:26 +00001217 x = dict2lookup(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001218 if (x == NULL) {
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001219 err_setval(NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001220 break;
1221 }
1222 }
1223 }
Guido van Rossum25831651993-05-19 14:50:45 +00001224 if (is_accessobject(x)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001225 x = getaccessvalue(x, f->f_globals /* XXX */);
Guido van Rossum25831651993-05-19 14:50:45 +00001226 if (x == NULL)
1227 break;
1228 }
Guido van Rossum81daa321993-05-20 14:24:46 +00001229 else
1230 INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001231 PUSH(x);
1232 break;
1233
1234 case LOAD_GLOBAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001235 w = GETNAMEV(oparg);
1236 x = dict2lookup(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001237 if (x == NULL) {
Guido van Rossumdf62e441991-08-16 08:56:04 +00001238 err_clear();
Guido van Rossum6135a871995-01-09 17:53:26 +00001239 x = dict2lookup(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001240 if (x == NULL) {
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001241 err_setval(NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001242 break;
1243 }
1244 }
Guido van Rossum25831651993-05-19 14:50:45 +00001245 if (is_accessobject(x)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001246 x = getaccessvalue(x, f->f_globals);
Guido van Rossum25831651993-05-19 14:50:45 +00001247 if (x == NULL)
1248 break;
1249 }
Guido van Rossum81daa321993-05-20 14:24:46 +00001250 else
1251 INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001252 PUSH(x);
1253 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001254
1255#if 0
Guido van Rossum374a9221991-04-04 10:40:29 +00001256 case LOAD_LOCAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001257 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001258 if ((x = f->f_locals) == NULL) {
1259 err_setstr(SystemError, "no locals");
1260 break;
1261 }
1262 x = dict2lookup(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001263 if (x == NULL) {
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001264 err_setval(NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001265 break;
1266 }
Guido van Rossum25831651993-05-19 14:50:45 +00001267 if (is_accessobject(x)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001268 x = getaccessvalue(x, f->f_locals);
Guido van Rossum25831651993-05-19 14:50:45 +00001269 if (x == NULL)
1270 break;
1271 }
Guido van Rossum81daa321993-05-20 14:24:46 +00001272 else
1273 INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001274 PUSH(x);
1275 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001276#endif
Guido van Rossum9bfef441993-03-29 10:43:31 +00001277
1278 case LOAD_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001279 x = GETLOCAL(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001280 if (x == NULL) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001281 err_setval(NameError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001282 gettupleitem(co->co_varnames,
1283 oparg));
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001284 break;
1285 }
Guido van Rossum25831651993-05-19 14:50:45 +00001286 if (is_accessobject(x)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001287 x = getaccessvalue(x, f->f_locals);
Guido van Rossum25831651993-05-19 14:50:45 +00001288 if (x == NULL)
1289 break;
1290 }
Guido van Rossum81daa321993-05-20 14:24:46 +00001291 else
1292 INCREF(x);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001293 PUSH(x);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001294 break;
1295
1296 case STORE_FAST:
Guido van Rossum25831651993-05-19 14:50:45 +00001297 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001298 w = GETLOCAL(oparg);
Guido van Rossum25831651993-05-19 14:50:45 +00001299 if (w != NULL && is_accessobject(w)) {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001300 err = setaccessvalue(w, f->f_locals, v);
Guido van Rossum25831651993-05-19 14:50:45 +00001301 DECREF(v);
1302 break;
1303 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001304 SETLOCAL(oparg, v);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001305 break;
1306
1307 case DELETE_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001308 x = GETLOCAL(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001309 if (x == NULL) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001310 err_setval(NameError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001311 gettupleitem(co->co_varnames,
1312 oparg));
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001313 break;
1314 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001315 if (is_accessobject(x)) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001316 err = setaccessvalue(x, f->f_locals,
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001317 (object *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001318 break;
1319 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001320 SETLOCAL(oparg, NULL);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001321 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001322
1323 case BUILD_TUPLE:
1324 x = newtupleobject(oparg);
1325 if (x != NULL) {
1326 for (; --oparg >= 0;) {
1327 w = POP();
Guido van Rossum8d617a61995-03-09 12:12:11 +00001328 SETTUPLEITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001329 }
1330 PUSH(x);
1331 }
1332 break;
1333
1334 case BUILD_LIST:
1335 x = newlistobject(oparg);
1336 if (x != NULL) {
1337 for (; --oparg >= 0;) {
1338 w = POP();
1339 err = setlistitem(x, oparg, w);
1340 if (err != 0)
1341 break;
1342 }
1343 PUSH(x);
1344 }
1345 break;
1346
1347 case BUILD_MAP:
1348 x = newdictobject();
1349 PUSH(x);
1350 break;
1351
1352 case LOAD_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001353 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001354 v = POP();
Guido van Rossum9bfef441993-03-29 10:43:31 +00001355 x = getattro(v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001356 DECREF(v);
1357 PUSH(x);
1358 break;
1359
1360 case COMPARE_OP:
1361 w = POP();
1362 v = POP();
Guido van Rossum5b7313a1992-04-06 13:24:57 +00001363 x = cmp_outcome(oparg, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001364 DECREF(v);
1365 DECREF(w);
1366 PUSH(x);
1367 break;
1368
1369 case IMPORT_NAME:
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001370 w = GETNAMEV(oparg);
Guido van Rossum6135a871995-01-09 17:53:26 +00001371 x = dictlookup(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001372 if (x == NULL) {
1373 err_setstr(ImportError,
1374 "__import__ not found");
1375 break;
1376 }
Guido van Rossum24c13741995-02-14 09:42:43 +00001377 if (is_methodobject(x)) {
1378 u = None;
1379 INCREF(u);
1380 }
1381 else {
1382 u = find_from_args(f, INSTR_OFFSET());
1383 if (u == NULL) {
1384 x = u;
1385 break;
1386 }
1387 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001388 w = mkvalue("(OOOO)",
1389 w,
1390 f->f_globals,
1391 f->f_locals == NULL ? None : f->f_locals,
1392 u);
Guido van Rossum24c13741995-02-14 09:42:43 +00001393 DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001394 if (w == NULL) {
1395 x = NULL;
1396 break;
1397 }
1398 x = call_object(x, w);
1399 DECREF(w);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001400 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001401 break;
1402
1403 case IMPORT_FROM:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001404 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001405 v = TOP();
Guido van Rossum24c13741995-02-14 09:42:43 +00001406 fast_2_locals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001407 if ((x = f->f_locals) == NULL) {
1408 err_setstr(SystemError, "no locals");
1409 break;
1410 }
1411 err = import_from(x, v, w);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001412 locals_2_fast(f, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +00001413 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001414
1415 case ACCESS_MODE:
1416 v = POP();
1417 w = GETNAMEV(oparg);
Guido van Rossumb3f72581993-05-21 19:56:10 +00001418 if (getstringvalue(w)[0] == '*')
1419 defmode = getintvalue(v);
1420 else
1421 err = access_statement(w, v, f);
Guido van Rossum25831651993-05-19 14:50:45 +00001422 DECREF(v);
1423 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001424
1425 case JUMP_FORWARD:
1426 JUMPBY(oparg);
1427 break;
1428
1429 case JUMP_IF_FALSE:
Guido van Rossum04691fc1992-08-12 15:35:34 +00001430 err = testbool(TOP());
1431 if (err > 0)
1432 err = 0;
1433 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001434 JUMPBY(oparg);
1435 break;
1436
1437 case JUMP_IF_TRUE:
Guido van Rossum04691fc1992-08-12 15:35:34 +00001438 err = testbool(TOP());
1439 if (err > 0) {
1440 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001442 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001443 break;
1444
1445 case JUMP_ABSOLUTE:
1446 JUMPTO(oparg);
1447 break;
1448
1449 case FOR_LOOP:
1450 /* for v in s: ...
1451 On entry: stack contains s, i.
1452 On exit: stack contains s, i+1, s[i];
1453 but if loop exhausted:
1454 s, i are popped, and we jump */
1455 w = POP(); /* Loop index */
1456 v = POP(); /* Sequence object */
1457 u = loop_subscript(v, w);
1458 if (u != NULL) {
1459 PUSH(v);
1460 x = newintobject(getintvalue(w)+1);
1461 PUSH(x);
1462 DECREF(w);
1463 PUSH(u);
1464 }
1465 else {
1466 DECREF(v);
1467 DECREF(w);
1468 /* A NULL can mean "s exhausted"
1469 but also an error: */
1470 if (err_occurred())
1471 why = WHY_EXCEPTION;
1472 else
1473 JUMPBY(oparg);
1474 }
1475 break;
1476
1477 case SETUP_LOOP:
1478 case SETUP_EXCEPT:
1479 case SETUP_FINALLY:
1480 setup_block(f, opcode, INSTR_OFFSET() + oparg,
1481 STACK_LEVEL());
1482 break;
1483
1484 case SET_LINENO:
Guido van Rossum96a42c81992-01-12 02:29:51 +00001485#ifdef LLTRACE
1486 if (lltrace)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001487 printf("--- %s:%d \n", filename, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001488#endif
Guido van Rossum96a42c81992-01-12 02:29:51 +00001489 f->f_lineno = oparg;
Guido van Rossume59214e1994-08-30 08:01:59 +00001490 if (f->f_trace != NULL) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001491 /* Trace each line of code reached */
1492 f->f_lasti = INSTR_OFFSET();
Guido van Rossume59214e1994-08-30 08:01:59 +00001493 err = call_trace(&f->f_trace, &f->f_trace,
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001494 f, "line", None);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001495 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001496 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001497
1498 case CALL_FUNCTION:
1499 {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001500 int na = oparg & 0xff;
1501 int nk = (oparg>>8) & 0xff;
1502 int n = na + 2*nk;
1503 object **pfunc = stack_pointer - n - 1;
1504 object *func = *pfunc;
1505 object *self = NULL;
1506 object *class = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001507 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001508 if (is_instancemethodobject(func)) {
1509 self = instancemethodgetself(func);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001510 class = instancemethodgetclass(func);
1511 func = instancemethodgetfunc(func);
1512 INCREF(func);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001513 if (self != NULL) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001514 INCREF(self);
1515 DECREF(*pfunc);
1516 *pfunc = self;
1517 na++;
1518 n++;
1519 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001520 else {
1521 /* Unbound methods must be
1522 called with an instance of
1523 the class (or a derived
1524 class) as first argument */
1525 if (na > 0 &&
1526 (self = stack_pointer[-n])
1527 != NULL &&
1528 is_instanceobject(self) &&
1529 issubclass(
1530 (object *)
1531 (((instanceobject *)self)
1532 ->in_class),
1533 class))
1534 /* Handy-dandy */ ;
1535 else {
1536 err_setstr(TypeError,
1537 "unbound method must be called with class instance 1st argument");
1538 return NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001539 }
1540 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001541 }
1542 else
1543 INCREF(func);
1544 if (is_funcobject(func)) {
1545 object *co = getfunccode(func);
1546 object *globals = getfuncglobals(func);
1547 object *argdefs = PyFunction_GetDefaults(func);
1548 object **d;
1549 int nd;
1550 if (argdefs != NULL) {
1551 d = &GETTUPLEITEM(argdefs, 0);
1552 nd = ((tupleobject *)argdefs)->ob_size;
1553 }
1554 else {
1555 d = NULL;
1556 nd = 0;
1557 }
1558 x = eval_code2(
1559 (codeobject *)co,
1560 globals, (object *)NULL,
1561 stack_pointer-n, na,
1562 stack_pointer-2*nk, nk,
1563 d, nd,
1564 class);
1565 }
1566 else {
1567 object *args = newtupleobject(na);
1568 object *kwdict = NULL;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001569 if (args == NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001570 x = NULL;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001571 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001572 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001573 if (nk > 0) {
1574 kwdict = newdictobject();
1575 if (kwdict == NULL) {
1576 x = NULL;
1577 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001578 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001579 err = 0;
1580 while (--nk >= 0) {
1581 object *value = POP();
1582 object *key = POP();
1583 err = mappinginsert(
1584 kwdict, key, value);
1585 if (err) {
1586 DECREF(key);
1587 DECREF(value);
1588 break;
1589 }
1590 }
1591 if (err) {
1592 DECREF(args);
1593 DECREF(kwdict);
1594 break;
1595 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001596 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001597 while (--na >= 0) {
1598 w = POP();
1599 SETTUPLEITEM(args, na, w);
1600 }
1601 x = PyEval_CallObjectWithKeywords(
1602 func, args, kwdict);
1603 DECREF(args);
1604 XDECREF(kwdict);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001605 }
1606 DECREF(func);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001607 while (stack_pointer > pfunc) {
1608 w = POP();
1609 DECREF(w);
1610 }
1611 PUSH(x);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001612 break;
1613 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001614
Guido van Rossum681d79a1995-07-18 14:51:37 +00001615 case MAKE_FUNCTION:
1616 v = POP(); /* code object */
1617 x = newfuncobject(v, f->f_globals);
1618 DECREF(v);
1619 /* XXX Maybe this should be a separate opcode? */
1620 if (x != NULL && oparg > 0) {
1621 v = newtupleobject(oparg);
1622 if (v == NULL) {
1623 DECREF(x);
1624 x = NULL;
1625 break;
1626 }
1627 while (--oparg >= 0) {
1628 w = POP();
1629 SETTUPLEITEM(v, oparg, w);
1630 }
1631 err = PyFunction_SetDefaults(x, v);
1632 DECREF(v);
1633 }
1634 PUSH(x);
1635 break;
1636
Guido van Rossum374a9221991-04-04 10:40:29 +00001637 default:
1638 fprintf(stderr,
1639 "XXX lineno: %d, opcode: %d\n",
Guido van Rossum96a42c81992-01-12 02:29:51 +00001640 f->f_lineno, opcode);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001641 err_setstr(SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 why = WHY_EXCEPTION;
1643 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001644
1645#ifdef CASE_TOO_BIG
1646 }
1647#endif
1648
Guido van Rossum374a9221991-04-04 10:40:29 +00001649 } /* switch */
1650
1651 on_error:
1652
1653 /* Quickly continue if no error occurred */
1654
1655 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001656 if (err == 0 && x != NULL) {
1657#ifdef CHECKEXC
1658 if (err_occurred())
1659 fprintf(stderr,
1660 "XXX undetected error\n");
1661 else
1662#endif
1663 continue; /* Normal, fast path */
1664 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 why = WHY_EXCEPTION;
1666 x = None;
1667 err = 0;
1668 }
1669
Guido van Rossum801dcae1992-04-08 11:32:32 +00001670#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 /* Double-check exception status */
1672
1673 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
1674 if (!err_occurred()) {
1675 fprintf(stderr, "XXX ghost error\n");
1676 err_setstr(SystemError, "ghost error");
1677 why = WHY_EXCEPTION;
1678 }
1679 }
1680 else {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001681 if (err_occurred()) {
1682 fprintf(stderr,
1683 "XXX undetected error (why=%d)\n",
1684 why);
1685 why = WHY_EXCEPTION;
1686 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001687 }
1688#endif
1689
1690 /* Log traceback info if this is a real exception */
1691
1692 if (why == WHY_EXCEPTION) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001693 f->f_lasti = INSTR_OFFSET() - 1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001694 if (HAS_ARG(opcode))
Guido van Rossum96a42c81992-01-12 02:29:51 +00001695 f->f_lasti -= 2;
1696 tb_here(f);
1697
Guido van Rossume59214e1994-08-30 08:01:59 +00001698 if (f->f_trace)
1699 call_exc_trace(&f->f_trace, &f->f_trace, f);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001700 if (sys_profile)
1701 call_exc_trace(&sys_profile, (object**)0, f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001702 }
1703
1704 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
1705
1706 if (why == WHY_RERAISE)
1707 why = WHY_EXCEPTION;
1708
1709 /* Unwind stacks if a (pseudo) exception occurred */
1710
1711 while (why != WHY_NOT && f->f_iblock > 0) {
1712 block *b = pop_block(f);
1713 while (STACK_LEVEL() > b->b_level) {
1714 v = POP();
1715 XDECREF(v);
1716 }
1717 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
1718 why = WHY_NOT;
1719 JUMPTO(b->b_handler);
1720 break;
1721 }
1722 if (b->b_type == SETUP_FINALLY ||
1723 b->b_type == SETUP_EXCEPT &&
1724 why == WHY_EXCEPTION) {
1725 if (why == WHY_EXCEPTION) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001726 object *exc, *val, *tb;
1727 err_fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001728 if (val == NULL) {
1729 val = None;
1730 INCREF(val);
1731 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001732 /* Make the raw exception data
1733 available to the handler,
1734 so a program can emulate the
1735 Python main loop. Don't do
1736 this for 'finally'. */
1737 if (b->b_type == SETUP_EXCEPT) {
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001738 sysset("exc_traceback", tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 sysset("exc_value", val);
1740 sysset("exc_type", exc);
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001742 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 PUSH(val);
1744 PUSH(exc);
1745 }
1746 else {
1747 if (why == WHY_RETURN)
1748 PUSH(retval);
1749 v = newintobject((long)why);
1750 PUSH(v);
1751 }
1752 why = WHY_NOT;
1753 JUMPTO(b->b_handler);
1754 break;
1755 }
1756 } /* unwind stack */
1757
1758 /* End the loop if we still have an error (or return) */
1759
1760 if (why != WHY_NOT)
1761 break;
1762
1763 } /* main loop */
1764
1765 /* Pop remaining stack entries */
1766
1767 while (!EMPTY()) {
1768 v = POP();
1769 XDECREF(v);
1770 }
1771
Guido van Rossum96a42c81992-01-12 02:29:51 +00001772 if (why != WHY_RETURN)
1773 retval = NULL;
1774
Guido van Rossume59214e1994-08-30 08:01:59 +00001775 if (f->f_trace) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001776 if (why == WHY_RETURN) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001777 if (call_trace(&f->f_trace, &f->f_trace, f,
1778 "return", retval)) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001779 XDECREF(retval);
1780 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001781 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001782 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001783 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001784 }
1785
1786 if (sys_profile && why == WHY_RETURN) {
1787 if (call_trace(&sys_profile, (object**)0,
1788 f, "return", retval)) {
1789 XDECREF(retval);
1790 retval = NULL;
1791 why = WHY_EXCEPTION;
1792 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001793 }
1794
Guido van Rossum374a9221991-04-04 10:40:29 +00001795 /* Restore previous frame and release the current one */
Guido van Rossum681d79a1995-07-18 14:51:37 +00001796
Guido van Rossum374a9221991-04-04 10:40:29 +00001797 current_frame = f->f_back;
1798 DECREF(f);
1799
Guido van Rossum96a42c81992-01-12 02:29:51 +00001800 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00001801}
1802
Guido van Rossum96a42c81992-01-12 02:29:51 +00001803#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00001804static int
1805prtrace(v, str)
1806 object *v;
1807 char *str;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001808{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001809 printf("%s ", str);
Guido van Rossum90933611991-06-07 16:10:43 +00001810 if (printobject(v, stdout, 0) != 0)
1811 err_clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001812 printf("\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001813}
Guido van Rossum3f5da241990-12-20 15:06:42 +00001814#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001815
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001816static void
1817call_exc_trace(p_trace, p_newtrace, f)
1818 object **p_trace, **p_newtrace;
1819 frameobject *f;
1820{
1821 object *type, *value, *traceback, *arg;
1822 int err;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001823 err_fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00001824 if (value == NULL) {
1825 value = None;
1826 INCREF(value);
1827 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001828 arg = mkvalue("(OOO)", type, value, traceback);
1829 if (arg == NULL) {
1830 err_restore(type, value, traceback);
1831 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001832 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001833 err = call_trace(p_trace, p_newtrace, f, "exception", arg);
1834 DECREF(arg);
1835 if (err == 0)
1836 err_restore(type, value, traceback);
1837 else {
1838 XDECREF(type);
1839 XDECREF(value);
1840 XDECREF(traceback);
1841 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001842}
1843
1844static int
1845call_trace(p_trace, p_newtrace, f, msg, arg)
1846 object **p_trace; /* in/out; may not be NULL;
1847 may not point to NULL variable initially */
1848 object **p_newtrace; /* in/out; may be NULL;
1849 may point to NULL variable;
1850 may be same variable as p_newtrace */
Guido van Rossum96a42c81992-01-12 02:29:51 +00001851 frameobject *f;
1852 char *msg;
1853 object *arg;
1854{
Guido van Rossumf10570b1995-07-07 22:53:21 +00001855 object *args, *what;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001856 object *res = NULL;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001857 static int tracing = 0;
1858
1859 if (tracing) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001860 /* Don't do recursive traces */
1861 if (p_newtrace) {
1862 XDECREF(*p_newtrace);
1863 *p_newtrace = NULL;
1864 }
1865 return 0;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001866 }
1867
Guido van Rossumf10570b1995-07-07 22:53:21 +00001868 args = newtupleobject(3);
1869 if (args == NULL)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001870 goto cleanup;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001871 what = newstringobject(msg);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001872 if (what == NULL)
1873 goto cleanup;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001874 INCREF(f);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001875 SETTUPLEITEM(args, 0, (object *)f);
1876 SETTUPLEITEM(args, 1, what);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001877 if (arg == NULL)
1878 arg = None;
1879 INCREF(arg);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001880 SETTUPLEITEM(args, 2, arg);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001881 tracing++;
Guido van Rossum5b722181993-03-30 17:46:03 +00001882 fast_2_locals(f);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001883 res = call_object(*p_trace, args); /* May clear *p_trace! */
Guido van Rossum5b722181993-03-30 17:46:03 +00001884 locals_2_fast(f, 1);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001885 tracing--;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001886 cleanup:
Guido van Rossumf10570b1995-07-07 22:53:21 +00001887 XDECREF(args);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001888 if (res == NULL) {
1889 /* The trace proc raised an exception */
Guido van Rossum96a42c81992-01-12 02:29:51 +00001890 tb_here(f);
Guido van Rossume59214e1994-08-30 08:01:59 +00001891 XDECREF(*p_trace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001892 *p_trace = NULL;
1893 if (p_newtrace) {
1894 XDECREF(*p_newtrace);
1895 *p_newtrace = NULL;
1896 }
Guido van Rossumeee3fd41992-04-05 14:18:13 +00001897 return -1;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001898 }
1899 else {
1900 if (p_newtrace) {
1901 XDECREF(*p_newtrace);
1902 if (res == None)
1903 *p_newtrace = NULL;
1904 else {
1905 INCREF(res);
1906 *p_newtrace = res;
1907 }
1908 }
1909 DECREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001910 return 0;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00001911 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001912}
1913
Guido van Rossum3f5da241990-12-20 15:06:42 +00001914object *
Guido van Rossum6135a871995-01-09 17:53:26 +00001915getbuiltins()
1916{
1917 if (current_frame == NULL)
Guido van Rossumb4e7e251995-01-17 16:27:25 +00001918 return getbuiltinmod();
Guido van Rossum6135a871995-01-09 17:53:26 +00001919 else
1920 return current_frame->f_builtins;
1921}
1922
1923object *
Guido van Rossum5b722181993-03-30 17:46:03 +00001924getlocals()
1925{
1926 if (current_frame == NULL)
1927 return NULL;
1928 fast_2_locals(current_frame);
1929 return current_frame->f_locals;
1930}
1931
1932object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001933getglobals()
1934{
1935 if (current_frame == NULL)
1936 return NULL;
1937 else
1938 return current_frame->f_globals;
1939}
1940
Guido van Rossum81daa321993-05-20 14:24:46 +00001941object *
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001942getowner()
Guido van Rossum81daa321993-05-20 14:24:46 +00001943{
1944 if (current_frame == NULL)
1945 return NULL;
1946 else
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00001947 return current_frame->f_owner;
Guido van Rossum81daa321993-05-20 14:24:46 +00001948}
1949
Guido van Rossume59214e1994-08-30 08:01:59 +00001950object *
1951getframe()
1952{
1953 return (object *)current_frame;
1954}
1955
Guido van Rossum6135a871995-01-09 17:53:26 +00001956int
1957getrestricted()
1958{
1959 return current_frame == NULL ? 0 : current_frame->f_restricted;
1960}
1961
Guido van Rossum3f5da241990-12-20 15:06:42 +00001962void
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001963flushline()
1964{
Guido van Rossum3165fe61992-09-25 21:59:05 +00001965 object *f = sysget("stdout");
1966 if (softspace(f, 0))
1967 writestring("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001968}
1969
Guido van Rossum3f5da241990-12-20 15:06:42 +00001970
Guido van Rossum06186511995-01-07 12:40:10 +00001971#define BINOP(opname, ropname, thisfunc) \
1972 if (!is_instanceobject(v) && !is_instanceobject(w)) \
1973 ; \
1974 else \
1975 return instancebinop(v, w, opname, ropname, thisfunc)
Guido van Rossum180d7b41994-09-29 09:45:57 +00001976
1977
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001978static object *
Guido van Rossum7928cd71991-10-24 14:59:31 +00001979or(v, w)
1980 object *v, *w;
1981{
Guido van Rossum06186511995-01-07 12:40:10 +00001982 BINOP("__or__", "__ror__", or);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001983 if (v->ob_type->tp_as_number != NULL) {
1984 object *x;
1985 object * (*f) FPROTO((object *, object *));
1986 if (coerce(&v, &w) != 0)
1987 return NULL;
1988 if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
1989 x = (*f)(v, w);
1990 DECREF(v);
1991 DECREF(w);
1992 if (f != NULL)
1993 return x;
1994 }
1995 err_setstr(TypeError, "bad operand type(s) for |");
1996 return NULL;
1997}
1998
1999static object *
2000xor(v, w)
2001 object *v, *w;
2002{
Guido van Rossum06186511995-01-07 12:40:10 +00002003 BINOP("__xor__", "__rxor__", xor);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002004 if (v->ob_type->tp_as_number != NULL) {
2005 object *x;
2006 object * (*f) FPROTO((object *, object *));
2007 if (coerce(&v, &w) != 0)
2008 return NULL;
2009 if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
2010 x = (*f)(v, w);
2011 DECREF(v);
2012 DECREF(w);
2013 if (f != NULL)
2014 return x;
2015 }
2016 err_setstr(TypeError, "bad operand type(s) for ^");
2017 return NULL;
2018}
2019
2020static object *
2021and(v, w)
2022 object *v, *w;
2023{
Guido van Rossum06186511995-01-07 12:40:10 +00002024 BINOP("__and__", "__rand__", and);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002025 if (v->ob_type->tp_as_number != NULL) {
2026 object *x;
2027 object * (*f) FPROTO((object *, object *));
2028 if (coerce(&v, &w) != 0)
2029 return NULL;
2030 if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
2031 x = (*f)(v, w);
2032 DECREF(v);
2033 DECREF(w);
2034 if (f != NULL)
2035 return x;
2036 }
2037 err_setstr(TypeError, "bad operand type(s) for &");
2038 return NULL;
2039}
2040
2041static object *
2042lshift(v, w)
2043 object *v, *w;
2044{
Guido van Rossum06186511995-01-07 12:40:10 +00002045 BINOP("__lshift__", "__rlshift__", lshift);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002046 if (v->ob_type->tp_as_number != NULL) {
2047 object *x;
2048 object * (*f) FPROTO((object *, object *));
2049 if (coerce(&v, &w) != 0)
2050 return NULL;
2051 if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
2052 x = (*f)(v, w);
2053 DECREF(v);
2054 DECREF(w);
2055 if (f != NULL)
2056 return x;
2057 }
2058 err_setstr(TypeError, "bad operand type(s) for <<");
2059 return NULL;
2060}
2061
2062static object *
2063rshift(v, w)
2064 object *v, *w;
2065{
Guido van Rossum06186511995-01-07 12:40:10 +00002066 BINOP("__rshift__", "__rrshift__", rshift);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002067 if (v->ob_type->tp_as_number != NULL) {
2068 object *x;
2069 object * (*f) FPROTO((object *, object *));
2070 if (coerce(&v, &w) != 0)
2071 return NULL;
2072 if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
2073 x = (*f)(v, w);
2074 DECREF(v);
2075 DECREF(w);
2076 if (f != NULL)
2077 return x;
2078 }
2079 err_setstr(TypeError, "bad operand type(s) for >>");
2080 return NULL;
2081}
2082
2083static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002084add(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002085 object *v, *w;
2086{
Guido van Rossum06186511995-01-07 12:40:10 +00002087 BINOP("__add__", "__radd__", add);
Guido van Rossume6eefc21992-08-14 12:06:52 +00002088 if (v->ob_type->tp_as_sequence != NULL)
2089 return (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
2090 else if (v->ob_type->tp_as_number != NULL) {
Guido van Rossum89d55ca1991-07-01 18:43:13 +00002091 object *x;
2092 if (coerce(&v, &w) != 0)
2093 return NULL;
2094 x = (*v->ob_type->tp_as_number->nb_add)(v, w);
2095 DECREF(v);
2096 DECREF(w);
2097 return x;
2098 }
Guido van Rossum34679b71993-01-26 13:33:44 +00002099 err_setstr(TypeError, "bad operand type(s) for +");
2100 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002101}
2102
2103static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002104sub(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002105 object *v, *w;
2106{
Guido van Rossum06186511995-01-07 12:40:10 +00002107 BINOP("__sub__", "__rsub__", sub);
Guido van Rossum89d55ca1991-07-01 18:43:13 +00002108 if (v->ob_type->tp_as_number != NULL) {
2109 object *x;
2110 if (coerce(&v, &w) != 0)
2111 return NULL;
2112 x = (*v->ob_type->tp_as_number->nb_subtract)(v, w);
2113 DECREF(v);
2114 DECREF(w);
2115 return x;
2116 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002117 err_setstr(TypeError, "bad operand type(s) for -");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002118 return NULL;
2119}
2120
2121static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002122mul(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002123 object *v, *w;
2124{
2125 typeobject *tp;
Guido van Rossume6eefc21992-08-14 12:06:52 +00002126 tp = v->ob_type;
Guido van Rossum06186511995-01-07 12:40:10 +00002127 BINOP("__mul__", "__rmul__", mul);
Guido van Rossume6eefc21992-08-14 12:06:52 +00002128 if (tp->tp_as_number != NULL &&
2129 w->ob_type->tp_as_sequence != NULL &&
2130 !is_instanceobject(v)) {
2131 /* number*sequence -- swap v and w */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002132 object *tmp = v;
2133 v = w;
2134 w = tmp;
Guido van Rossume6eefc21992-08-14 12:06:52 +00002135 tp = v->ob_type;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002136 }
Guido van Rossum89d55ca1991-07-01 18:43:13 +00002137 if (tp->tp_as_number != NULL) {
2138 object *x;
Guido van Rossume6eefc21992-08-14 12:06:52 +00002139 if (is_instanceobject(v)) {
2140 /* Instances of user-defined classes get their
2141 other argument uncoerced, so they may
2142 implement sequence*number as well as
2143 number*number. */
2144 INCREF(v);
2145 INCREF(w);
2146 }
2147 else if (coerce(&v, &w) != 0)
Guido van Rossum89d55ca1991-07-01 18:43:13 +00002148 return NULL;
2149 x = (*v->ob_type->tp_as_number->nb_multiply)(v, w);
2150 DECREF(v);
2151 DECREF(w);
2152 return x;
2153 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002154 if (tp->tp_as_sequence != NULL) {
2155 if (!is_intobject(w)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002156 err_setstr(TypeError,
2157 "can't multiply sequence with non-int");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002158 return NULL;
2159 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002160 return (*tp->tp_as_sequence->sq_repeat)
2161 (v, (int)getintvalue(w));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002162 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002163 err_setstr(TypeError, "bad operand type(s) for *");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002164 return NULL;
2165}
2166
2167static object *
Guido van Rossum40d0b7e1990-12-20 23:03:11 +00002168divide(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002169 object *v, *w;
2170{
Guido van Rossum06186511995-01-07 12:40:10 +00002171 BINOP("__div__", "__rdiv__", divide);
Guido van Rossum89d55ca1991-07-01 18:43:13 +00002172 if (v->ob_type->tp_as_number != NULL) {
2173 object *x;
2174 if (coerce(&v, &w) != 0)
2175 return NULL;
2176 x = (*v->ob_type->tp_as_number->nb_divide)(v, w);
2177 DECREF(v);
2178 DECREF(w);
2179 return x;
2180 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002181 err_setstr(TypeError, "bad operand type(s) for /");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002182 return NULL;
2183}
2184
2185static object *
Guido van Rossum06186511995-01-07 12:40:10 +00002186mod(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002187 object *v, *w;
2188{
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002189 if (is_stringobject(v)) {
2190 return formatstring(v, w);
2191 }
Guido van Rossum06186511995-01-07 12:40:10 +00002192 BINOP("__mod__", "__rmod__", mod);
Guido van Rossum89d55ca1991-07-01 18:43:13 +00002193 if (v->ob_type->tp_as_number != NULL) {
2194 object *x;
2195 if (coerce(&v, &w) != 0)
2196 return NULL;
2197 x = (*v->ob_type->tp_as_number->nb_remainder)(v, w);
2198 DECREF(v);
2199 DECREF(w);
2200 return x;
2201 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002202 err_setstr(TypeError, "bad operand type(s) for %");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002203 return NULL;
2204}
2205
2206static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002207neg(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002208 object *v;
2209{
2210 if (v->ob_type->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002211 return (*v->ob_type->tp_as_number->nb_negative)(v);
2212 err_setstr(TypeError, "bad operand type(s) for unary -");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002213 return NULL;
2214}
2215
2216static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002217pos(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002218 object *v;
2219{
2220 if (v->ob_type->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002221 return (*v->ob_type->tp_as_number->nb_positive)(v);
2222 err_setstr(TypeError, "bad operand type(s) for unary +");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002223 return NULL;
2224}
2225
2226static object *
Guido van Rossum7928cd71991-10-24 14:59:31 +00002227invert(v)
2228 object *v;
2229{
Guido van Rossum9b1d33b1992-02-11 15:56:02 +00002230 object * (*f) FPROTO((object *));
Guido van Rossum7928cd71991-10-24 14:59:31 +00002231 if (v->ob_type->tp_as_number != NULL &&
2232 (f = v->ob_type->tp_as_number->nb_invert) != NULL)
2233 return (*f)(v);
2234 err_setstr(TypeError, "bad operand type(s) for unary ~");
2235 return NULL;
2236}
2237
2238static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002239not(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002240 object *v;
2241{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002242 int outcome = testbool(v);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002243 object *w;
2244 if (outcome < 0)
2245 return NULL;
2246 if (outcome == 0)
2247 w = True;
2248 else
2249 w = False;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002250 INCREF(w);
2251 return w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002252}
Guido van Rossum234f9421993-06-17 12:35:49 +00002253
2254
Guido van Rossum681d79a1995-07-18 14:51:37 +00002255/* External interface to call any callable object.
2256 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002257
2258object *
2259call_object(func, arg)
2260 object *func;
2261 object *arg;
2262{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002263 return PyEval_CallObjectWithKeywords(func, arg, (object *)NULL);
2264}
Guido van Rossume59214e1994-08-30 08:01:59 +00002265
Guido van Rossum681d79a1995-07-18 14:51:37 +00002266object *
2267PyEval_CallObjectWithKeywords(func, arg, kw)
2268 object *func;
2269 object *arg;
2270 object *kw;
2271{
2272 ternaryfunc call;
2273 object *result;
2274
2275 if (arg == NULL)
2276 arg = newtupleobject(0);
2277 else if (!is_tupleobject(arg)) {
2278 err_setstr(TypeError, "argument list must be a tuple");
2279 return NULL;
2280 }
2281 else
2282 INCREF(arg);
2283
Guido van Rossume3e61c11995-08-04 04:14:47 +00002284 if (kw != NULL && !is_dictobject(kw)) {
2285 err_setstr(TypeError, "keyword list must be a dictionary");
2286 return NULL;
2287 }
2288
Guido van Rossum681d79a1995-07-18 14:51:37 +00002289 if (call = func->ob_type->tp_call)
2290 result = (*call)(func, arg, kw);
2291 else if (is_instancemethodobject(func) || is_funcobject(func))
2292 result = call_function(func, arg, kw);
2293 else
2294 result = call_builtin(func, arg, kw);
2295
2296 DECREF(arg);
2297
Guido van Rossum69d9eb91994-11-10 22:41:15 +00002298 if (result == NULL && !err_occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002299 err_setstr(SystemError,
2300 "NULL result without error in call_object");
Guido van Rossume59214e1994-08-30 08:01:59 +00002301
2302 return result;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002303}
2304
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002305static object *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002306call_builtin(func, arg, kw)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002307 object *func;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002308 object *arg;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002309 object *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002310{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002311 if (is_methodobject(func)) {
2312 method meth = getmethod(func);
2313 object *self = getself(func);
Guido van Rossumff8b4941995-07-26 18:16:42 +00002314 int flags = getflags(func);
2315 if (!(flags & METH_VARARGS)) {
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002316 int size = gettuplesize(arg);
2317 if (size == 1)
Guido van Rossum8d617a61995-03-09 12:12:11 +00002318 arg = GETTUPLEITEM(arg, 0);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002319 else if (size == 0)
2320 arg = NULL;
2321 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002322 if (flags & METH_KEYWORDS)
2323 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
Guido van Rossume3e61c11995-08-04 04:14:47 +00002324 if (kw != NULL && getmappingsize(kw) != 0) {
Guido van Rossumff8b4941995-07-26 18:16:42 +00002325 err_setstr(TypeError,
2326 "this function takes no keyword arguments");
2327 return NULL;
2328 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002329 return (*meth)(self, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002330 }
2331 if (is_classobject(func)) {
Guido van Rossumff8b4941995-07-26 18:16:42 +00002332 return newinstanceobject(func, arg, kw);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002333 }
Guido van Rossume59214e1994-08-30 08:01:59 +00002334 if (is_instanceobject(func)) {
2335 object *res, *call = getattr(func,"__call__");
2336 if (call == NULL) {
2337 err_clear();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002338 err_setstr(AttributeError,
2339 "no __call__ method defined");
Guido van Rossume59214e1994-08-30 08:01:59 +00002340 return NULL;
2341 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002342 res = PyEval_CallObjectWithKeywords(call, arg, kw);
Guido van Rossume59214e1994-08-30 08:01:59 +00002343 DECREF(call);
2344 return res;
2345 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002346 err_setstr(TypeError, "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002347 return NULL;
2348}
2349
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002350static object *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002351call_function(func, arg, kw)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002352 object *func;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002353 object *arg;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002354 object *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002355{
Guido van Rossum681d79a1995-07-18 14:51:37 +00002356 object *class = NULL; /* == owner */
Guido van Rossume59214e1994-08-30 08:01:59 +00002357 object *argdefs;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002358 object **d, **k;
2359 int nk, nd;
2360 object *result;
2361
2362 if (kw != NULL && !is_dictobject(kw)) {
2363 err_badcall();
2364 return NULL;
2365 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002366
Guido van Rossume8122f11991-05-05 20:03:07 +00002367 if (is_instancemethodobject(func)) {
2368 object *self = instancemethodgetself(func);
Guido van Rossum81daa321993-05-20 14:24:46 +00002369 class = instancemethodgetclass(func);
Guido van Rossume8122f11991-05-05 20:03:07 +00002370 func = instancemethodgetfunc(func);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002371 if (self == NULL) {
2372 /* Unbound methods must be called with an instance of
2373 the class (or a derived class) as first argument */
Guido van Rossum681d79a1995-07-18 14:51:37 +00002374 if (gettuplesize(arg) >= 1) {
Guido van Rossum8d617a61995-03-09 12:12:11 +00002375 self = GETTUPLEITEM(arg, 0);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002376 if (self != NULL &&
2377 is_instanceobject(self) &&
2378 issubclass((object *)
2379 (((instanceobject *)self)->in_class),
2380 class))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002381 /* Handy-dandy */ ;
Guido van Rossumb3f72581993-05-21 19:56:10 +00002382 else
2383 self = NULL;
2384 }
2385 if (self == NULL) {
2386 err_setstr(TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002387 "unbound method must be called with class instance 1st argument");
Guido van Rossumb3f72581993-05-21 19:56:10 +00002388 return NULL;
2389 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002390 INCREF(arg);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002391 }
2392 else {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002393 int argcount = gettuplesize(arg);
2394 object *newarg = newtupleobject(argcount + 1);
2395 int i;
Guido van Rossum81daa321993-05-20 14:24:46 +00002396 if (newarg == NULL)
2397 return NULL;
2398 INCREF(self);
Guido van Rossum8d617a61995-03-09 12:12:11 +00002399 SETTUPLEITEM(newarg, 0, self);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002400 for (i = 0; i < argcount; i++) {
2401 object *v = GETTUPLEITEM(arg, i);
2402 XINCREF(v);
2403 SETTUPLEITEM(newarg, i+1, v);
Guido van Rossum81daa321993-05-20 14:24:46 +00002404 }
2405 arg = newarg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002406 }
2407 }
2408 else {
2409 if (!is_funcobject(func)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002410 err_setstr(TypeError, "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002411 return NULL;
2412 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002413 INCREF(arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002414 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002415
2416 argdefs = PyFunction_GetDefaults(func);
2417 if (argdefs != NULL && is_tupleobject(argdefs)) {
2418 d = &GETTUPLEITEM((tupleobject *)argdefs, 0);
2419 nd = gettuplesize(argdefs);
2420 }
2421 else {
2422 d = NULL;
2423 nd = 0;
2424 }
2425
2426 if (kw != NULL) {
2427 int pos, i;
2428 nk = getmappingsize(kw);
2429 k = NEW(object *, 2*nk);
2430 if (k == NULL) {
2431 err_nomem();
Guido van Rossume59214e1994-08-30 08:01:59 +00002432 DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002433 return NULL;
Guido van Rossume59214e1994-08-30 08:01:59 +00002434 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002435 pos = i = 0;
2436 while (mappinggetnext(kw, &pos, &k[i], &k[i+1]))
2437 i += 2;
2438 nk = i/2;
2439 /* XXX This is broken if the caller deletes dict items! */
2440 }
2441 else {
2442 k = NULL;
2443 nk = 0;
Guido van Rossume59214e1994-08-30 08:01:59 +00002444 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002445
Guido van Rossum681d79a1995-07-18 14:51:37 +00002446 result = eval_code2(
2447 (codeobject *)getfunccode(func),
2448 getfuncglobals(func), (object *)NULL,
2449 &GETTUPLEITEM(arg, 0), gettuplesize(arg),
2450 k, nk,
2451 d, nd,
2452 class);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002453
Guido van Rossum681d79a1995-07-18 14:51:37 +00002454 DECREF(arg);
2455 XDEL(k);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002456
Guido van Rossum681d79a1995-07-18 14:51:37 +00002457 return result;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002458}
2459
2460static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002461apply_subscript(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002462 object *v, *w;
2463{
2464 typeobject *tp = v->ob_type;
2465 if (tp->tp_as_sequence == NULL && tp->tp_as_mapping == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002466 err_setstr(TypeError, "unsubscriptable object");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002467 return NULL;
2468 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002469 if (tp->tp_as_mapping != NULL) {
2470 return (*tp->tp_as_mapping->mp_subscript)(v, w);
2471 }
2472 else {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002473 int i;
2474 if (!is_intobject(w)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002475 err_setstr(TypeError, "sequence subscript not int");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002476 return NULL;
2477 }
2478 i = getintvalue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002479 if (i < 0) {
2480 int len = (*tp->tp_as_sequence->sq_length)(v);
2481 if (len < 0)
2482 return NULL;
2483 i += len;
2484 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002485 return (*tp->tp_as_sequence->sq_item)(v, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002486 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002487}
2488
2489static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002490loop_subscript(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002491 object *v, *w;
2492{
2493 sequence_methods *sq = v->ob_type->tp_as_sequence;
Guido van Rossume59214e1994-08-30 08:01:59 +00002494 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002495 if (sq == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002496 err_setstr(TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002497 return NULL;
2498 }
2499 i = getintvalue(w);
Guido van Rossume59214e1994-08-30 08:01:59 +00002500 v = (*sq->sq_item)(v, i);
2501 if (v)
2502 return v;
2503 if (err_occurred() == IndexError)
2504 err_clear();
2505 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002506}
2507
2508static int
Guido van Rossum3f5da241990-12-20 15:06:42 +00002509slice_index(v, isize, pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002510 object *v;
2511 int isize;
2512 int *pi;
2513{
2514 if (v != NULL) {
2515 if (!is_intobject(v)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002516 err_setstr(TypeError, "slice index must be int");
2517 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002518 }
2519 *pi = getintvalue(v);
2520 if (*pi < 0)
2521 *pi += isize;
2522 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002523 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002524}
2525
2526static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002527apply_slice(u, v, w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002528 object *u, *v, *w;
2529{
2530 typeobject *tp = u->ob_type;
2531 int ilow, ihigh, isize;
2532 if (tp->tp_as_sequence == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002533 err_setstr(TypeError, "only sequences can be sliced");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002534 return NULL;
2535 }
2536 ilow = 0;
2537 isize = ihigh = (*tp->tp_as_sequence->sq_length)(u);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002538 if (isize < 0)
2539 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002540 if (slice_index(v, isize, &ilow) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002541 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002542 if (slice_index(w, isize, &ihigh) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002543 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002544 return (*tp->tp_as_sequence->sq_slice)(u, ilow, ihigh);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002545}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002546
2547static int
2548assign_subscript(w, key, v) /* w[key] = v */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002549 object *w;
2550 object *key;
2551 object *v;
2552{
2553 typeobject *tp = w->ob_type;
2554 sequence_methods *sq;
2555 mapping_methods *mp;
Guido van Rossum1d339e81995-02-17 15:04:21 +00002556 int (*func1)();
2557 int (*func2)();
Guido van Rossum04691fc1992-08-12 15:35:34 +00002558 if ((mp = tp->tp_as_mapping) != NULL &&
Guido van Rossum1d339e81995-02-17 15:04:21 +00002559 (func1 = mp->mp_ass_subscript) != NULL) {
2560 return (*func1)(w, key, v);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002561 }
2562 else if ((sq = tp->tp_as_sequence) != NULL &&
Guido van Rossum1d339e81995-02-17 15:04:21 +00002563 (func2 = sq->sq_ass_item) != NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002564 if (!is_intobject(key)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002565 err_setstr(TypeError,
Guido van Rossum98256aa1991-12-24 13:25:19 +00002566 "sequence subscript must be integer (assign or del)");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002567 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002568 }
Guido van Rossum98256aa1991-12-24 13:25:19 +00002569 else {
2570 int i = getintvalue(key);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002571 if (i < 0) {
2572 int len = (*sq->sq_length)(w);
2573 if (len < 0)
2574 return -1;
2575 i += len;
2576 }
Guido van Rossum1d339e81995-02-17 15:04:21 +00002577 return (*func2)(w, i, v);
Guido van Rossum98256aa1991-12-24 13:25:19 +00002578 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002579 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002580 else {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002581 err_setstr(TypeError,
2582 "can't assign to this subscripted object");
2583 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002584 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002585}
2586
Guido van Rossum3f5da241990-12-20 15:06:42 +00002587static int
2588assign_slice(u, v, w, x) /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002589 object *u, *v, *w, *x;
2590{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002591 sequence_methods *sq = u->ob_type->tp_as_sequence;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002592 int ilow, ihigh, isize;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002593 if (sq == NULL) {
2594 err_setstr(TypeError, "assign to slice of non-sequence");
2595 return -1;
2596 }
2597 if (sq == NULL || sq->sq_ass_slice == NULL) {
2598 err_setstr(TypeError, "unassignable slice");
2599 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002600 }
2601 ilow = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002602 isize = ihigh = (*sq->sq_length)(u);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002603 if (isize < 0)
2604 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002605 if (slice_index(v, isize, &ilow) != 0)
2606 return -1;
2607 if (slice_index(w, isize, &ihigh) != 0)
2608 return -1;
2609 return (*sq->sq_ass_slice)(u, ilow, ihigh, x);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002610}
2611
2612static int
2613cmp_exception(err, v)
2614 object *err, *v;
2615{
2616 if (is_tupleobject(v)) {
2617 int i, n;
2618 n = gettuplesize(v);
2619 for (i = 0; i < n; i++) {
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002620 /* Test recursively */
Guido van Rossum8d617a61995-03-09 12:12:11 +00002621 if (cmp_exception(err, GETTUPLEITEM(v, i)))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002622 return 1;
2623 }
2624 return 0;
2625 }
Guido van Rossum8bf7c481995-01-26 00:41:04 +00002626 if (is_classobject(v) && is_classobject(err))
2627 return issubclass(err, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002628 return err == v;
2629}
2630
Guido van Rossum3f5da241990-12-20 15:06:42 +00002631static int
2632cmp_member(v, w)
2633 object *v, *w;
2634{
Guido van Rossume59214e1994-08-30 08:01:59 +00002635 int i, cmp;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002636 object *x;
2637 sequence_methods *sq;
2638 /* Special case for char in string */
2639 if (is_stringobject(w)) {
2640 register char *s, *end;
2641 register char c;
2642 if (!is_stringobject(v) || getstringsize(v) != 1) {
2643 err_setstr(TypeError,
2644 "string member test needs char left operand");
2645 return -1;
2646 }
2647 c = getstringvalue(v)[0];
2648 s = getstringvalue(w);
2649 end = s + getstringsize(w);
2650 while (s < end) {
2651 if (c == *s++)
2652 return 1;
2653 }
2654 return 0;
2655 }
2656 sq = w->ob_type->tp_as_sequence;
2657 if (sq == NULL) {
2658 err_setstr(TypeError,
2659 "'in' or 'not in' needs sequence right argument");
2660 return -1;
2661 }
Guido van Rossume59214e1994-08-30 08:01:59 +00002662 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002663 x = (*sq->sq_item)(w, i);
Guido van Rossume59214e1994-08-30 08:01:59 +00002664 if (x == NULL) {
2665 if (err_occurred() == IndexError) {
2666 err_clear();
2667 break;
2668 }
2669 return -1;
2670 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002671 cmp = cmpobject(v, x);
2672 XDECREF(x);
2673 if (cmp == 0)
2674 return 1;
2675 }
2676 return 0;
2677}
2678
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002679static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002680cmp_outcome(op, v, w)
Guido van Rossum5b7313a1992-04-06 13:24:57 +00002681 int op;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002682 register object *v;
2683 register object *w;
2684{
2685 register int cmp;
2686 register int res = 0;
2687 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002688 case IS:
2689 case IS_NOT:
2690 res = (v == w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002691 if (op == (int) IS_NOT)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002692 res = !res;
2693 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002694 case IN:
2695 case NOT_IN:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002696 res = cmp_member(v, w);
2697 if (res < 0)
2698 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002699 if (op == (int) NOT_IN)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002700 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002701 break;
2702 case EXC_MATCH:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002703 res = cmp_exception(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002704 break;
2705 default:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002706 cmp = cmpobject(v, w);
2707 switch (op) {
2708 case LT: res = cmp < 0; break;
2709 case LE: res = cmp <= 0; break;
2710 case EQ: res = cmp == 0; break;
2711 case NE: res = cmp != 0; break;
2712 case GT: res = cmp > 0; break;
2713 case GE: res = cmp >= 0; break;
2714 /* XXX no default? (res is initialized to 0 though) */
2715 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002716 }
2717 v = res ? True : False;
2718 INCREF(v);
2719 return v;
2720}
2721
Guido van Rossum3f5da241990-12-20 15:06:42 +00002722static int
2723import_from(locals, v, name)
2724 object *locals;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002725 object *v;
Guido van Rossumdf62e441991-08-16 08:56:04 +00002726 object *name;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002727{
2728 object *w, *x;
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002729 if (!is_moduleobject(v)) {
Guido van Rossum1919ca71995-01-20 16:55:14 +00002730 err_setstr(TypeError, "import-from requires module object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002731 return -1;
2732 }
Guido van Rossume9736fc1990-11-18 17:33:06 +00002733 w = getmoduledict(v);
Guido van Rossumdf62e441991-08-16 08:56:04 +00002734 if (getstringvalue(name)[0] == '*') {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002735 int pos, err;
Guido van Rossum25831651993-05-19 14:50:45 +00002736 object *name, *value;
2737 pos = 0;
2738 while (mappinggetnext(w, &pos, &name, &value)) {
2739 if (!is_stringobject(name) ||
2740 getstringvalue(name)[0] == '_')
Guido van Rossume9736fc1990-11-18 17:33:06 +00002741 continue;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002742 if (is_accessobject(value)) {
2743 value = getaccessvalue(value, (object *)NULL);
2744 if (value == NULL) {
2745 err_clear();
2746 continue;
2747 }
2748 }
2749 else
2750 INCREF(value);
2751 err = dict2insert(locals, name, value);
2752 DECREF(value);
2753 if (err != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002754 return -1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002755 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002756 return 0;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002757 }
2758 else {
Guido van Rossumdf62e441991-08-16 08:56:04 +00002759 x = dict2lookup(w, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002760 if (x == NULL) {
Guido van Rossum98256aa1991-12-24 13:25:19 +00002761 char buf[250];
Guido van Rossum16dfd291992-02-05 11:17:30 +00002762 sprintf(buf, "cannot import name %.230s",
Guido van Rossum98256aa1991-12-24 13:25:19 +00002763 getstringvalue(name));
2764 err_setstr(ImportError, buf);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002765 return -1;
2766 }
2767 else
Guido van Rossumdf62e441991-08-16 08:56:04 +00002768 return dict2insert(locals, name, x);
Guido van Rossume9736fc1990-11-18 17:33:06 +00002769 }
2770}
2771
2772static object *
Guido van Rossum25831651993-05-19 14:50:45 +00002773build_class(methods, bases, name)
2774 object *methods; /* dictionary */
2775 object *bases; /* tuple containing classes */
2776 object *name; /* string */
Guido van Rossume9736fc1990-11-18 17:33:06 +00002777{
Guido van Rossum25831651993-05-19 14:50:45 +00002778 int i;
2779 if (!is_tupleobject(bases)) {
2780 err_setstr(SystemError, "build_class with non-tuple bases");
2781 return NULL;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002782 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002783 if (gettuplesize(bases) > 0) {
2784 object *base;
Guido van Rossum8d617a61995-03-09 12:12:11 +00002785 base = GETTUPLEITEM(bases, 0);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002786 /* Call the base's *type*, if it is callable.
2787 This code is a hook for Donald Beaudry's type extensions.
2788 In unexended Python it will never be triggered since its
2789 types are not callable. */
2790 if (base->ob_type->ob_type->tp_call) {
2791 object *args;
2792 object *class;
2793 args = mkvalue("(OOO)", name, bases, methods);
2794 class = call_object((object *)base->ob_type, args);
2795 DECREF(args);
2796 return class;
2797 }
2798 }
Guido van Rossum25831651993-05-19 14:50:45 +00002799 if (!is_dictobject(methods)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002800 err_setstr(SystemError, "build_class with non-dictionary");
Guido van Rossume9736fc1990-11-18 17:33:06 +00002801 return NULL;
2802 }
Guido van Rossum25831651993-05-19 14:50:45 +00002803 if (!is_stringobject(name)) {
2804 err_setstr(SystemError, "build_class witn non-string name");
2805 return NULL;
2806 }
2807 for (i = gettuplesize(bases); --i >= 0; ) {
Guido van Rossum8d617a61995-03-09 12:12:11 +00002808 object *base = GETTUPLEITEM(bases, i);
Guido van Rossum25831651993-05-19 14:50:45 +00002809 if (!is_classobject(base)) {
2810 err_setstr(TypeError,
2811 "base is not a class object");
2812 return NULL;
2813 }
2814 }
2815 return newclassobject(bases, methods, name);
2816}
2817
2818static int
Guido van Rossumb3f72581993-05-21 19:56:10 +00002819access_statement(name, vmode, f)
Guido van Rossum25831651993-05-19 14:50:45 +00002820 object *name;
Guido van Rossumb3f72581993-05-21 19:56:10 +00002821 object *vmode;
Guido van Rossum25831651993-05-19 14:50:45 +00002822 frameobject *f;
2823{
Guido van Rossumb3f72581993-05-21 19:56:10 +00002824 int mode = getintvalue(vmode);
Guido van Rossum81daa321993-05-20 14:24:46 +00002825 object *value, *ac;
2826 typeobject *type;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002827 int ret;
2828 fast_2_locals(f);
2829 value = dict2lookup(f->f_locals, name);
Guido van Rossum25831651993-05-19 14:50:45 +00002830 if (value && is_accessobject(value)) {
2831 err_setstr(AccessError, "can't override access");
2832 return -1;
2833 }
2834 err_clear();
Guido van Rossum81daa321993-05-20 14:24:46 +00002835 if (value != NULL && value != None)
2836 type = value->ob_type;
2837 else
2838 type = NULL;
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002839 ac = newaccessobject(value, f->f_locals, type, mode);
Guido van Rossum25831651993-05-19 14:50:45 +00002840 if (ac == NULL)
2841 return -1;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002842 ret = mappinginsert(f->f_locals, name, ac);
2843 DECREF(ac);
2844 locals_2_fast(f, 0);
Guido van Rossum25831651993-05-19 14:50:45 +00002845 return ret;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002846}
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002847
2848static int
2849exec_statement(prog, globals, locals)
2850 object *prog;
2851 object *globals;
2852 object *locals;
2853{
2854 char *s;
2855 int n;
Guido van Rossum6b6e0aa1995-02-07 15:36:56 +00002856 object *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002857 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002858
2859 if (is_tupleobject(prog) && globals == None && locals == None &&
2860 ((n = gettuplesize(prog)) == 2 || n == 3)) {
2861 /* Backward compatibility hack */
2862 globals = gettupleitem(prog, 1);
2863 if (n == 3)
2864 locals = gettupleitem(prog, 2);
2865 prog = gettupleitem(prog, 0);
2866 }
2867 if (globals == None) {
2868 globals = getglobals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002869 if (locals == None) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002870 locals = getlocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002871 plain = 1;
2872 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002873 }
2874 else if (locals == None)
2875 locals = globals;
2876 if (!is_stringobject(prog) &&
2877 !is_codeobject(prog) &&
2878 !is_fileobject(prog)) {
2879 err_setstr(TypeError,
2880 "exec 1st arg must be string, code or file object");
2881 return -1;
2882 }
2883 if (!is_dictobject(globals) || !is_dictobject(locals)) {
2884 err_setstr(TypeError,
2885 "exec 2nd/3rd args must be dict or None");
2886 return -1;
2887 }
Guido van Rossum6135a871995-01-09 17:53:26 +00002888 if (dictlookup(globals, "__builtins__") == NULL)
2889 dictinsert(globals, "__builtins__", current_frame->f_builtins);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002890 if (is_codeobject(prog)) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002891 if (eval_code((codeobject *) prog, globals, locals) == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002892 return -1;
2893 return 0;
2894 }
2895 if (is_fileobject(prog)) {
2896 FILE *fp = getfilefile(prog);
2897 char *name = getstringvalue(getfilename(prog));
2898 if (run_file(fp, name, file_input, globals, locals) == NULL)
2899 return -1;
2900 return 0;
2901 }
2902 s = getstringvalue(prog);
2903 if (strlen(s) != getstringsize(prog)) {
2904 err_setstr(ValueError, "embedded '\\0' in exec string");
2905 return -1;
2906 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002907 v = run_string(s, file_input, globals, locals);
2908 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002909 return -1;
Guido van Rossum6b6e0aa1995-02-07 15:36:56 +00002910 DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002911 if (plain)
2912 locals_2_fast(current_frame, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002913 return 0;
2914}
Guido van Rossum24c13741995-02-14 09:42:43 +00002915
Guido van Rossum681d79a1995-07-18 14:51:37 +00002916/* Hack for newimp.py */
Guido van Rossum24c13741995-02-14 09:42:43 +00002917static object *
2918find_from_args(f, nexti)
2919 frameobject *f;
2920 int nexti;
2921{
2922 int opcode;
2923 int oparg;
2924 object *list, *name;
2925 unsigned char *next_instr;
2926
2927 next_instr = GETUSTRINGVALUE(f->f_code->co_code) + nexti;
2928 opcode = (*next_instr++);
2929 if (opcode != IMPORT_FROM) {
Guido van Rossum24c13741995-02-14 09:42:43 +00002930 INCREF(None);
2931 return None;
2932 }
2933
2934 list = newlistobject(0);
2935 if (list == NULL)
2936 return NULL;
2937
2938 do {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002939 oparg = (next_instr[1]<<8) + next_instr[0];
2940 next_instr += 2;
Guido van Rossum24c13741995-02-14 09:42:43 +00002941 name = Getnamev(f, oparg);
2942 if (addlistitem(list, name) < 0) {
2943 DECREF(list);
2944 break;
2945 }
2946 opcode = (*next_instr++);
2947 } while (opcode == IMPORT_FROM);
2948
2949 return list;
2950}