1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Execute compiled code */
27 #include "allobjects.h"
30 #include "sysmodule.h"
32 #include "frameobject.h"
36 #include "bltinmodule.h"
37 #include "traceback.h"
39 #include "pythonrun.h"
43 /* Turn this on if your compiler chokes on the big switch: */
44 /* #define CASE_TOO_BIG 1 /**/
46 /* Turn this on if you want to debug the interpreter: */
47 /* (This can be on even if NDEBUG is defined) */
48 /* #define DEBUG 1 /**/
50 #if defined(DEBUG) || !defined(NDEBUG)
51 /* For debugging the interpreter: */
52 #define LLTRACE 1 /* Low-level trace feature */
53 #define CHECKEXC 1 /* Double-check exception checking */
56 /* Global option, may be set by main() */
60 /* Forward declarations */
63 static int prtrace
PROTO((object
*, char *));
65 static void call_exc_trace
PROTO((object
**, object
**, frameobject
*));
67 PROTO((object
**, object
**, frameobject
*, char *, object
*));
68 static object
*add
PROTO((object
*, object
*));
69 static object
*sub
PROTO((object
*, object
*));
70 static object
*mul
PROTO((object
*, object
*));
71 static object
*divide
PROTO((object
*, object
*));
72 static object
*rem
PROTO((object
*, object
*));
73 static object
*neg
PROTO((object
*));
74 static object
*pos
PROTO((object
*));
75 static object
*not PROTO((object
*));
76 static object
*invert
PROTO((object
*));
77 static object
*lshift
PROTO((object
*, object
*));
78 static object
*rshift
PROTO((object
*, object
*));
79 static object
*and PROTO((object
*, object
*));
80 static object
*xor PROTO((object
*, object
*));
81 static object
*or PROTO((object
*, object
*));
82 static object
*call_builtin
PROTO((object
*, object
*));
83 static object
*call_function
PROTO((object
*, object
*));
84 static object
*apply_subscript
PROTO((object
*, object
*));
85 static object
*loop_subscript
PROTO((object
*, object
*));
86 static int slice_index
PROTO((object
*, int, int *));
87 static object
*apply_slice
PROTO((object
*, object
*, object
*));
88 static int assign_subscript
PROTO((object
*, object
*, object
*));
89 static int assign_slice
PROTO((object
*, object
*, object
*, object
*));
90 static int cmp_exception
PROTO((object
*, object
*));
91 static int cmp_member
PROTO((object
*, object
*));
92 static object
*cmp_outcome
PROTO((int, object
*, object
*));
93 static int import_from
PROTO((object
*, object
*, object
*));
94 static object
*build_class
PROTO((object
*, object
*, object
*));
95 static void locals_2_fast
PROTO((frameobject
*, int));
96 static void fast_2_locals
PROTO((frameobject
*));
97 static int access_statement
PROTO((object
*, object
*, frameobject
*));
98 static int exec_statement
PROTO((object
*, object
*, object
*));
99 static void mergelocals
PROTO(());
102 /* Pointer to current frame, used to link new frames to */
104 static frameobject
*current_frame
;
111 static type_lock interpreter_lock
;
116 if (interpreter_lock
)
118 interpreter_lock
= allocate_lock();
119 acquire_lock(interpreter_lock
, 1);
124 /* Functions save_thread and restore_thread are always defined so
125 dynamically loaded modules needn't be compiled separately for use
126 with and without threads: */
132 if (interpreter_lock
) {
134 res
= (object
*)current_frame
;
135 current_frame
= NULL
;
136 release_lock(interpreter_lock
);
148 if (interpreter_lock
) {
151 acquire_lock(interpreter_lock
, 1);
153 current_frame
= (frameobject
*)x
;
159 /* Status code for main loop (reason for stack unwind) */
162 WHY_NOT
, /* No error */
163 WHY_EXCEPTION
, /* Exception occurred */
164 WHY_RERAISE
, /* Exception re-raised by 'finally' */
165 WHY_RETURN
, /* 'return' statement */
166 WHY_BREAK
/* 'break' statement */
170 /* Interpreter main loop */
173 eval_code(co
, globals
, locals
, owner
, arg
)
180 register unsigned char *next_instr
;
181 register int opcode
; /* Current opcode */
182 register int oparg
; /* Current opcode argument, if any */
183 register object
**stack_pointer
;
184 register enum why_code why
; /* Reason for block stack unwind */
185 register int err
; /* Error status -- nonzero if error */
186 register object
*x
; /* Result object -- NULL if error */
187 register object
*v
; /* Temporary objects popped off stack */
191 register frameobject
*f
; /* Current frame */
192 register listobject
*fastlocals
= NULL
;
193 object
*trace
= NULL
; /* Trace function or NULL */
194 object
*retval
; /* Return value iff why == WHY_RETURN */
195 char *name
; /* Name used by some instructions */
196 int needmerge
= 0; /* Set if need to merge locals back at end */
197 int defmode
= 0; /* Default access mode for new variables */
202 /* Make it easier to find out where we are with dbx */
203 char *filename
= getstringvalue(co
->co_filename
);
206 /* Code access macros */
208 #define GETCONST(i) Getconst(f, i)
209 #define GETNAME(i) Getname(f, i)
210 #define GETNAMEV(i) Getnamev(f, i)
211 #define FIRST_INSTR() (GETUSTRINGVALUE(f->f_code->co_code))
212 #define INSTR_OFFSET() (next_instr - FIRST_INSTR())
213 #define NEXTOP() (*next_instr++)
214 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
215 #define JUMPTO(x) (next_instr = FIRST_INSTR() + (x))
216 #define JUMPBY(x) (next_instr += (x))
218 /* Stack manipulation macros */
220 #define STACK_LEVEL() (stack_pointer - f->f_valuestack)
221 #define EMPTY() (STACK_LEVEL() == 0)
222 #define TOP() (stack_pointer[-1])
223 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
224 #define BASIC_POP() (*--stack_pointer)
226 #define CHECK_STACK(n) (STACK_LEVEL() + (n) < f->f_nvalues || \
227 (stack_pointer = extend_stack(f, STACK_LEVEL(), n)))
230 #define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
231 #define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
233 #define PUSH(v) BASIC_PUSH(v)
234 #define POP() BASIC_POP()
237 if (globals
== NULL
) {
238 globals
= getglobals();
239 if (locals
== NULL
) {
240 locals
= getlocals();
250 lltrace
= dictlookup(globals
, "__lltrace__") != NULL
;
254 current_frame
, /*back*/
266 if (sys_trace
!= NULL
) {
267 /* sys_trace, if defined, is a function that will
268 be called on *every* entry to a code block.
269 Its return value, if not None, is a function that
270 will be called at the start of each executed line
271 of code. (Actually, the function must return
272 itself in order to continue tracing.)
273 The trace functions are called with three arguments:
274 a pointer to the current frame, a string indicating
275 why the function is called, and an argument which
276 depends on the situation. The global trace function
277 (sys.trace) is also called whenever an exception
279 if (call_trace(&sys_trace
, &trace
, f
, "call", arg
)) {
280 /* Trace function raised an error */
281 current_frame
= f
->f_back
;
287 if (sys_profile
!= NULL
) {
288 /* Similar for sys_profile, except it needn't return
289 itself and isn't called for "line" events */
290 if (call_trace(&sys_profile
, (object
**)0, f
, "call", arg
)) {
291 current_frame
= f
->f_back
;
297 next_instr
= GETUSTRINGVALUE(f
->f_code
->co_code
);
298 stack_pointer
= f
->f_valuestack
;
307 x
= None
; /* Not a reference, just anything non-NULL */
312 /* Do periodic things.
313 Doing this every time through the loop would add
314 too much overhead (a function call per instruction).
315 So we do it only every tenth instruction. */
320 err_set(KeyboardInterrupt
);
326 if (interpreter_lock
) {
327 /* Give another thread a chance */
329 current_frame
= NULL
;
330 release_lock(interpreter_lock
);
332 /* Other threads may run now */
334 acquire_lock(interpreter_lock
, 1);
340 /* Extract opcode and argument */
343 f
->f_lasti
= INSTR_OFFSET();
351 /* Instruction tracing */
354 if (HAS_ARG(opcode
)) {
355 printf("%d: %d, %d\n",
356 (int) (INSTR_OFFSET() - 3),
361 (int) (INSTR_OFFSET() - 1), opcode
);
366 if (!CHECK_STACK(3)) {
371 /* Main switch on opcode */
376 It is essential that any operation that fails sets either
377 x to NULL, err to nonzero, or why to anything but WHY_NOT,
378 and that no operation that succeeds does this! */
380 /* case STOP_CODE: this is an error! */
439 f
->f_lasti
= INSTR_OFFSET() - 1; /* For tracing */
440 x
= call_object(v
, (object
*)NULL
);
452 case BINARY_MULTIPLY
:
488 case BINARY_SUBTRACT
:
500 x
= apply_subscript(v
, w
);
509 f
->f_lasti
= INSTR_OFFSET() - 1; /* For tracing */
510 x
= call_object(v
, w
);
565 if ((opcode
-SLICE
) & 2)
569 if ((opcode
-SLICE
) & 1)
574 x
= apply_slice(u
, v
, w
);
585 if ((opcode
-STORE_SLICE
) & 2)
589 if ((opcode
-STORE_SLICE
) & 1)
595 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
606 if ((opcode
-DELETE_SLICE
) & 2)
610 if ((opcode
-DELETE_SLICE
) & 1)
615 err
= assign_slice(u
, v
, w
, (object
*)NULL
);
627 err
= assign_subscript(v
, w
, u
);
637 err
= assign_subscript(v
, w
, (object
*)NULL
);
644 /* Print value except if procedure result */
647 x
= sysget("stdout");
649 err
= writeobject(v
, x
, 0);
652 err_setstr(RuntimeError
,
653 "printing expression statement");
662 w
= sysget("stdout");
665 err
= writeobject(v
, w
, PRINT_RAW
);
666 if (err
== 0 && is_stringobject(v
)) {
667 /* XXX move into writeobject() ? */
668 char *s
= getstringvalue(v
);
669 int len
= getstringsize(v
);
670 if (len
> 0 && isspace(s
[len
-1]) &&
678 x
= sysget("stdout");
680 err_setstr(RuntimeError
, "lost sys.stdout");
682 writestring("\n", x
);
691 case RAISE_EXCEPTION
:
694 /* A tuple is equivalent to its first element here */
695 while (is_tupleobject(w
)) {
697 w
= gettupleitem(u
, 0);
700 if (!is_stringobject(w
))
701 err_setstr(TypeError
,
702 "exceptions must be strings");
731 err
= exec_statement(u
, v
, w
);
739 x
= newfuncobject(v
, f
->f_globals
);
746 block
*b
= pop_block(f
);
747 while (STACK_LEVEL() > b
->b_level
) {
756 if (is_intobject(v
)) {
757 why
= (enum why_code
) getintvalue(v
);
758 if (why
== WHY_RETURN
)
761 else if (is_stringobject(v
)) {
770 else if (v
!= None
) {
771 err_setstr(SystemError
,
772 "'finally' pops bad exception");
782 x
= build_class(u
, v
, w
);
792 u
= dict2lookup(f
->f_locals
, w
);
796 u
= (object
*)v
->ob_type
;
799 x
= newaccessobject(v
, f
->f_locals
,
808 else if (is_accessobject(u
)) {
809 err
= setaccessvalue(u
, f
->f_locals
, v
);
813 err
= dict2insert(f
->f_locals
, w
, v
);
819 u
= dict2lookup(f
->f_locals
, w
);
820 if (u
!= NULL
&& is_accessobject(u
)) {
821 err
= setaccessvalue(u
, f
->f_locals
,
825 if ((err
= dict2remove(f
->f_locals
, w
)) != 0)
826 err_setval(NameError
, w
);
830 default: switch (opcode
) {
835 err_setstr(TypeError
,
841 if (!is_tupleobject(v
)) {
842 err_setstr(TypeError
,
843 "bad argument list");
846 else if (gettuplesize(v
) < oparg
) {
847 err_setstr(TypeError
,
848 "not enough arguments");
851 else if (oparg
== 0) {
856 x
= gettupleslice(v
, oparg
, gettuplesize(v
));
859 if (!CHECK_STACK(oparg
)) {
863 for (; --oparg
>= 0; ) {
864 w
= gettupleitem(v
, oparg
);
877 err_setstr(TypeError
,
883 if (!is_tupleobject(v
)) {
884 err_setstr(TypeError
,
885 "bad argument list");
891 /* Implement various compatibility hacks (for 0.9.4 or earlier):
892 (a) f(a,b,...) accepts f((1,2,...))
893 (b) f((a,b,...)) accepts f(1,2,...)
894 (c) f(self,(a,b,...)) accepts f(x,1,2,...)
896 if (n
== 1 && oparg
!= 1) {
898 w
= gettupleitem(v
, 0);
899 if (is_tupleobject(w
)) {
906 else if (n
!= 1 && oparg
== 1) {
910 /* Don't fall through */
912 else if (n
> 2 && oparg
== 2) {
915 w
= newtupleobject(n
-1);
916 u
= newtupleobject(2);
917 if (u
== NULL
|| w
== NULL
) {
924 t
= gettupleitem(v
, 0);
926 settupleitem(u
, 0, t
);
927 for (i
= 1; i
< n
; i
++) {
928 t
= gettupleitem(v
, i
);
930 settupleitem(w
, i
-1, t
);
932 settupleitem(u
, 1, w
);
937 #endif /* Disabled compatibility hacks */
939 err_setstr(TypeError
,
940 "arg count mismatch");
950 if (!is_tupleobject(v
)) {
951 err_setstr(TypeError
, "unpack non-tuple");
954 else if (gettuplesize(v
) != oparg
) {
955 err_setstr(ValueError
,
956 "unpack tuple of wrong size");
960 if (!CHECK_STACK(oparg
)) {
964 for (; --oparg
>= 0; ) {
965 w
= gettupleitem(v
, oparg
);
975 if (!is_listobject(v
)) {
976 err_setstr(TypeError
, "unpack non-list");
979 else if (getlistsize(v
) != oparg
) {
980 err_setstr(ValueError
,
981 "unpack list of wrong size");
985 if (!CHECK_STACK(oparg
)) {
989 for (; --oparg
>= 0; ) {
990 w
= getlistitem(v
, oparg
);
1002 err
= setattro(v
, w
, u
); /* v.w = u */
1008 w
= GETNAMEV(oparg
);
1010 err
= setattro(v
, w
, (object
*)NULL
); /* del v.w */
1015 w
= GETNAMEV(oparg
);
1017 u
= dict2lookup(f
->f_locals
, w
);
1018 if (u
!= NULL
&& is_accessobject(u
)) {
1019 err
= setaccessvalue(u
, f
->f_globals
, v
);
1023 err
= dict2insert(f
->f_globals
, w
, v
);
1028 w
= GETNAMEV(oparg
);
1029 u
= dict2lookup(f
->f_locals
, w
);
1030 if (u
!= NULL
&& is_accessobject(u
)) {
1031 err
= setaccessvalue(u
, f
->f_globals
,
1035 if ((err
= dict2remove(f
->f_globals
, w
)) != 0)
1036 err_setval(NameError
, w
);
1040 x
= GETCONST(oparg
);
1046 w
= GETNAMEV(oparg
);
1047 x
= dict2lookup(f
->f_locals
, w
);
1050 x
= dict2lookup(f
->f_globals
, w
);
1055 err_setval(NameError
, w
);
1060 if (is_accessobject(x
)) {
1061 x
= getaccessvalue(x
, f
->f_globals
/* XXX */);
1071 w
= GETNAMEV(oparg
);
1072 x
= dict2lookup(f
->f_globals
, w
);
1077 err_setval(NameError
, w
);
1081 if (is_accessobject(x
)) {
1082 x
= getaccessvalue(x
, f
->f_globals
);
1092 w
= GETNAMEV(oparg
);
1093 x
= dict2lookup(f
->f_locals
, w
);
1095 err_setval(NameError
, w
);
1098 if (is_accessobject(x
)) {
1099 x
= getaccessvalue(x
, f
->f_locals
);
1109 x
= GETCONST(oparg
);
1112 if (x
== NULL
|| !is_dictobject(x
)) {
1113 fatal("bad RESERVE_FAST");
1114 err_setstr(SystemError
, "bad RESERVE_FAST");
1118 XDECREF(f
->f_fastlocals
);
1119 XDECREF(f
->f_localmap
);
1122 f
->f_fastlocals
= x
= newlistobject(
1123 x
->ob_type
->tp_as_mapping
->mp_length(x
));
1124 fastlocals
= (listobject
*) x
;
1128 x
= GETLISTITEM(fastlocals
, oparg
);
1130 err_setstr(NameError
,
1131 "undefined local variable");
1134 if (is_accessobject(x
)) {
1135 x
= getaccessvalue(x
, f
->f_locals
);
1146 w
= GETLISTITEM(fastlocals
, oparg
);
1147 if (w
!= NULL
&& is_accessobject(w
)) {
1148 err
= setaccessvalue(w
, f
->f_locals
, v
);
1153 GETLISTITEM(fastlocals
, oparg
) = v
;
1157 x
= GETLISTITEM(fastlocals
, oparg
);
1159 err_setstr(NameError
,
1160 "undefined local variable");
1163 if (w
!= NULL
&& is_accessobject(w
)) {
1164 err
= setaccessvalue(w
, f
->f_locals
,
1169 GETLISTITEM(fastlocals
, oparg
) = NULL
;
1173 x
= newtupleobject(oparg
);
1175 for (; --oparg
>= 0;) {
1177 err
= settupleitem(x
, oparg
, w
);
1186 x
= newlistobject(oparg
);
1188 for (; --oparg
>= 0;) {
1190 err
= setlistitem(x
, oparg
, w
);
1199 x
= newdictobject();
1204 w
= GETNAMEV(oparg
);
1214 x
= cmp_outcome(oparg
, v
, w
);
1221 name
= GETNAME(oparg
);
1222 x
= import_module(name
);
1228 w
= GETNAMEV(oparg
);
1230 err
= import_from(f
->f_locals
, v
, w
);
1231 locals_2_fast(f
, 0);
1236 w
= GETNAMEV(oparg
);
1237 if (getstringvalue(w
)[0] == '*')
1238 defmode
= getintvalue(v
);
1240 err
= access_statement(w
, v
, f
);
1249 err
= testbool(TOP());
1257 err
= testbool(TOP());
1270 On entry: stack contains s, i.
1271 On exit: stack contains s, i+1, s[i];
1272 but if loop exhausted:
1273 s, i are popped, and we jump */
1274 w
= POP(); /* Loop index */
1275 v
= POP(); /* Sequence object */
1276 u
= loop_subscript(v
, w
);
1279 x
= newintobject(getintvalue(w
)+1);
1287 /* A NULL can mean "s exhausted"
1288 but also an error: */
1290 why
= WHY_EXCEPTION
;
1299 setup_block(f
, opcode
, INSTR_OFFSET() + oparg
,
1306 printf("--- Line %d ---\n", oparg
);
1308 f
->f_lineno
= oparg
;
1309 if (trace
!= NULL
) {
1310 /* Trace each line of code reached */
1311 f
->f_lasti
= INSTR_OFFSET();
1312 err
= call_trace(&trace
, &trace
,
1319 "XXX lineno: %d, opcode: %d\n",
1320 f
->f_lineno
, opcode
);
1321 err_setstr(SystemError
, "eval_code: unknown opcode");
1322 why
= WHY_EXCEPTION
;
1333 /* Quickly continue if no error occurred */
1335 if (why
== WHY_NOT
) {
1336 if (err
== 0 && x
!= NULL
)
1337 continue; /* Normal, fast path */
1338 why
= WHY_EXCEPTION
;
1344 /* Double-check exception status */
1346 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
1347 if (!err_occurred()) {
1348 fprintf(stderr
, "XXX ghost error\n");
1349 err_setstr(SystemError
, "ghost error");
1350 why
= WHY_EXCEPTION
;
1354 if (err_occurred()) {
1355 fprintf(stderr
, "XXX undetected error\n");
1358 why
= WHY_EXCEPTION
;
1363 /* Log traceback info if this is a real exception */
1365 if (why
== WHY_EXCEPTION
) {
1366 f
->f_lasti
= INSTR_OFFSET() - 1;
1367 if (HAS_ARG(opcode
))
1372 call_exc_trace(&trace
, &trace
, f
);
1374 call_exc_trace(&sys_profile
, (object
**)0, f
);
1377 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
1379 if (why
== WHY_RERAISE
)
1380 why
= WHY_EXCEPTION
;
1382 /* Unwind stacks if a (pseudo) exception occurred */
1384 while (why
!= WHY_NOT
&& f
->f_iblock
> 0) {
1385 block
*b
= pop_block(f
);
1386 while (STACK_LEVEL() > b
->b_level
) {
1390 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
1392 JUMPTO(b
->b_handler
);
1395 if (b
->b_type
== SETUP_FINALLY
||
1396 b
->b_type
== SETUP_EXCEPT
&&
1397 why
== WHY_EXCEPTION
) {
1398 if (why
== WHY_EXCEPTION
) {
1400 err_get(&exc
, &val
);
1406 /* Make the raw exception data
1407 available to the handler,
1408 so a program can emulate the
1409 Python main loop. Don't do
1410 this for 'finally'. */
1411 if (b
->b_type
== SETUP_EXCEPT
) {
1412 sysset("exc_traceback", v
);
1413 sysset("exc_value", val
);
1414 sysset("exc_type", exc
);
1421 if (why
== WHY_RETURN
)
1423 v
= newintobject((long)why
);
1427 JUMPTO(b
->b_handler
);
1430 } /* unwind stack */
1432 /* End the loop if we still have an error (or return) */
1439 /* Pop remaining stack entries */
1446 if (why
!= WHY_RETURN
)
1450 if (why
== WHY_RETURN
) {
1451 if (call_trace(&trace
, &trace
, f
, "return", retval
)) {
1454 why
= WHY_EXCEPTION
;
1460 if (sys_profile
&& why
== WHY_RETURN
) {
1461 if (call_trace(&sys_profile
, (object
**)0,
1462 f
, "return", retval
)) {
1465 why
= WHY_EXCEPTION
;
1469 if (fastlocals
&& (f
->ob_refcnt
> 1 || f
->f_locals
->ob_refcnt
> 2))
1472 /* Restore previous frame and release the current one */
1474 current_frame
= f
->f_back
;
1478 locals_2_fast(current_frame
, 1);
1490 if (printobject(v
, stdout
, 0) != 0)
1491 err_clear(); /* Don't know what else to do */
1497 call_exc_trace(p_trace
, p_newtrace
, f
)
1498 object
**p_trace
, **p_newtrace
;
1501 object
*type
, *value
, *traceback
, *arg
;
1503 err_get(&type
, &value
);
1504 if (value
== NULL
) {
1508 traceback
= tb_fetch();
1509 arg
= newtupleobject(3);
1512 settupleitem(arg
, 0, type
);
1513 settupleitem(arg
, 1, value
);
1514 settupleitem(arg
, 2, traceback
);
1515 err
= call_trace(p_trace
, p_newtrace
, f
, "exception", arg
);
1518 /* Restore original exception */
1519 err_setval(type
, value
);
1520 tb_store(traceback
);
1526 call_trace(p_trace
, p_newtrace
, f
, msg
, arg
)
1527 object
**p_trace
; /* in/out; may not be NULL;
1528 may not point to NULL variable initially */
1529 object
**p_newtrace
; /* in/out; may be NULL;
1530 may point to NULL variable;
1531 may be same variable as p_newtrace */
1536 object
*arglist
, *what
;
1538 static int tracing
= 0;
1541 /* Don't do recursive traces */
1543 XDECREF(*p_newtrace
);
1549 arglist
= newtupleobject(3);
1550 if (arglist
== NULL
)
1552 what
= newstringobject(msg
);
1556 settupleitem(arglist
, 0, (object
*)f
);
1557 settupleitem(arglist
, 1, what
);
1561 settupleitem(arglist
, 2, arg
);
1564 res
= call_object(*p_trace
, arglist
);
1565 locals_2_fast(f
, 1);
1570 /* The trace proc raised an exception */
1575 XDECREF(*p_newtrace
);
1582 XDECREF(*p_newtrace
);
1599 /* Merge f->f_fastlocals into f->f_locals */
1600 object
*locals
, *fast
, *map
;
1601 object
*error_type
, *error_value
;
1603 object
*key
, *value
;
1606 locals
= f
->f_locals
;
1607 fast
= f
->f_fastlocals
;
1608 map
= f
->f_localmap
;
1609 if (locals
== NULL
|| fast
== NULL
|| map
== NULL
)
1611 if (!is_dictobject(locals
) || !is_listobject(fast
) ||
1612 !is_dictobject(map
))
1614 err_get(&error_type
, &error_value
);
1616 while (mappinggetnext(map
, &pos
, &key
, &value
)) {
1618 if (!is_intobject(value
))
1620 j
= getintvalue(value
);
1621 value
= getlistitem(fast
, j
);
1622 if (value
== NULL
) {
1624 if (dict2remove(locals
, key
) != 0)
1628 if (dict2insert(locals
, key
, value
) != 0)
1632 err_setval(error_type
, error_value
);
1636 locals_2_fast(f
, clear
)
1640 /* Merge f->f_locals into f->f_fastlocals */
1641 object
*locals
, *fast
, *map
;
1642 object
*error_type
, *error_value
;
1644 object
*key
, *value
;
1647 locals
= f
->f_locals
;
1648 fast
= f
->f_fastlocals
;
1649 map
= f
->f_localmap
;
1650 if (locals
== NULL
|| fast
== NULL
|| map
== NULL
)
1652 if (!is_dictobject(locals
) || !is_listobject(fast
) ||
1653 !is_dictobject(map
))
1655 err_get(&error_type
, &error_value
);
1657 while (mappinggetnext(map
, &pos
, &key
, &value
)) {
1659 if (!is_intobject(value
))
1661 j
= getintvalue(value
);
1662 value
= dict2lookup(locals
, key
);
1667 if (value
!= NULL
|| clear
)
1668 if (setlistitem(fast
, j
, value
) != 0)
1671 err_setval(error_type
, error_value
);
1677 locals_2_fast(current_frame
, 1);
1683 if (current_frame
== NULL
)
1685 fast_2_locals(current_frame
);
1686 return current_frame
->f_locals
;
1692 if (current_frame
== NULL
)
1695 return current_frame
->f_globals
;
1701 if (current_frame
== NULL
)
1704 return current_frame
->f_owner
;
1711 object
*v
= tb_fetch();
1722 object
*f
= sysget("stdout");
1723 if (softspace(f
, 0))
1724 writestring("\n", f
);
1732 if (v
->ob_type
->tp_as_number
!= NULL
) {
1734 object
* (*f
) FPROTO((object
*, object
*));
1735 if (coerce(&v
, &w
) != 0)
1737 if ((f
= v
->ob_type
->tp_as_number
->nb_or
) != NULL
)
1744 err_setstr(TypeError
, "bad operand type(s) for |");
1752 if (v
->ob_type
->tp_as_number
!= NULL
) {
1754 object
* (*f
) FPROTO((object
*, object
*));
1755 if (coerce(&v
, &w
) != 0)
1757 if ((f
= v
->ob_type
->tp_as_number
->nb_xor
) != NULL
)
1764 err_setstr(TypeError
, "bad operand type(s) for ^");
1772 if (v
->ob_type
->tp_as_number
!= NULL
) {
1774 object
* (*f
) FPROTO((object
*, object
*));
1775 if (coerce(&v
, &w
) != 0)
1777 if ((f
= v
->ob_type
->tp_as_number
->nb_and
) != NULL
)
1784 err_setstr(TypeError
, "bad operand type(s) for &");
1792 if (v
->ob_type
->tp_as_number
!= NULL
) {
1794 object
* (*f
) FPROTO((object
*, object
*));
1795 if (coerce(&v
, &w
) != 0)
1797 if ((f
= v
->ob_type
->tp_as_number
->nb_lshift
) != NULL
)
1804 err_setstr(TypeError
, "bad operand type(s) for <<");
1812 if (v
->ob_type
->tp_as_number
!= NULL
) {
1814 object
* (*f
) FPROTO((object
*, object
*));
1815 if (coerce(&v
, &w
) != 0)
1817 if ((f
= v
->ob_type
->tp_as_number
->nb_rshift
) != NULL
)
1824 err_setstr(TypeError
, "bad operand type(s) for >>");
1832 if (v
->ob_type
->tp_as_sequence
!= NULL
)
1833 return (*v
->ob_type
->tp_as_sequence
->sq_concat
)(v
, w
);
1834 else if (v
->ob_type
->tp_as_number
!= NULL
) {
1836 if (coerce(&v
, &w
) != 0)
1838 x
= (*v
->ob_type
->tp_as_number
->nb_add
)(v
, w
);
1843 err_setstr(TypeError
, "bad operand type(s) for +");
1851 if (v
->ob_type
->tp_as_number
!= NULL
) {
1853 if (coerce(&v
, &w
) != 0)
1855 x
= (*v
->ob_type
->tp_as_number
->nb_subtract
)(v
, w
);
1860 err_setstr(TypeError
, "bad operand type(s) for -");
1870 if (tp
->tp_as_number
!= NULL
&&
1871 w
->ob_type
->tp_as_sequence
!= NULL
&&
1872 !is_instanceobject(v
)) {
1873 /* number*sequence -- swap v and w */
1879 if (tp
->tp_as_number
!= NULL
) {
1881 if (is_instanceobject(v
)) {
1882 /* Instances of user-defined classes get their
1883 other argument uncoerced, so they may
1884 implement sequence*number as well as
1889 else if (coerce(&v
, &w
) != 0)
1891 x
= (*v
->ob_type
->tp_as_number
->nb_multiply
)(v
, w
);
1896 if (tp
->tp_as_sequence
!= NULL
) {
1897 if (!is_intobject(w
)) {
1898 err_setstr(TypeError
,
1899 "can't multiply sequence with non-int");
1902 return (*tp
->tp_as_sequence
->sq_repeat
)
1903 (v
, (int)getintvalue(w
));
1905 err_setstr(TypeError
, "bad operand type(s) for *");
1913 if (v
->ob_type
->tp_as_number
!= NULL
) {
1915 if (coerce(&v
, &w
) != 0)
1917 x
= (*v
->ob_type
->tp_as_number
->nb_divide
)(v
, w
);
1922 err_setstr(TypeError
, "bad operand type(s) for /");
1930 if (v
->ob_type
->tp_as_number
!= NULL
) {
1932 if (coerce(&v
, &w
) != 0)
1934 x
= (*v
->ob_type
->tp_as_number
->nb_remainder
)(v
, w
);
1939 if (is_stringobject(v
)) {
1940 return formatstring(v
, w
);
1942 err_setstr(TypeError
, "bad operand type(s) for %");
1950 if (v
->ob_type
->tp_as_number
!= NULL
)
1951 return (*v
->ob_type
->tp_as_number
->nb_negative
)(v
);
1952 err_setstr(TypeError
, "bad operand type(s) for unary -");
1960 if (v
->ob_type
->tp_as_number
!= NULL
)
1961 return (*v
->ob_type
->tp_as_number
->nb_positive
)(v
);
1962 err_setstr(TypeError
, "bad operand type(s) for unary +");
1970 object
* (*f
) FPROTO((object
*));
1971 if (v
->ob_type
->tp_as_number
!= NULL
&&
1972 (f
= v
->ob_type
->tp_as_number
->nb_invert
) != NULL
)
1974 err_setstr(TypeError
, "bad operand type(s) for unary ~");
1982 int outcome
= testbool(v
);
1995 /* External interface to call any callable object. The arg may be NULL. */
1998 call_object(func
, arg
)
2002 if (is_instancemethodobject(func
) || is_funcobject(func
))
2003 return call_function(func
, arg
);
2005 return call_builtin(func
, arg
);
2009 call_builtin(func
, arg
)
2013 if (is_methodobject(func
)) {
2014 method meth
= getmethod(func
);
2015 object
*self
= getself(func
);
2016 if (!getvarargs(func
) && arg
!= NULL
&& is_tupleobject(arg
)) {
2017 int size
= gettuplesize(arg
);
2019 arg
= gettupleitem(arg
, 0);
2023 return (*meth
)(self
, arg
);
2025 if (is_classobject(func
)) {
2026 return newinstanceobject(func
, arg
);
2028 err_setstr(TypeError
, "call of non-function");
2033 call_function(func
, arg
)
2037 object
*newarg
= NULL
;
2038 object
*newlocals
, *newglobals
;
2039 object
*class = NULL
;
2042 if (is_instancemethodobject(func
)) {
2043 object
*self
= instancemethodgetself(func
);
2044 class = instancemethodgetclass(func
);
2045 func
= instancemethodgetfunc(func
);
2047 /* Unbound methods must be called with an instance of
2048 the class (or a derived class) as first argument */
2049 if (arg
!= NULL
&& is_tupleobject(arg
) &&
2050 gettuplesize(arg
) >= 1) {
2051 self
= gettupleitem(arg
, 0);
2053 is_instanceobject(self
) &&
2054 issubclass((object
*)
2055 (((instanceobject
*)self
)->in_class
),
2062 err_setstr(TypeError
,
2063 "unbound method must be called with class instance argument");
2071 else if (is_tupleobject(arg
))
2072 argcount
= gettuplesize(arg
);
2075 newarg
= newtupleobject(argcount
+ 1);
2079 settupleitem(newarg
, 0, self
);
2080 if (arg
!= NULL
&& !is_tupleobject(arg
)) {
2082 settupleitem(newarg
, 1, arg
);
2087 for (i
= 0; i
< argcount
; i
++) {
2088 v
= gettupleitem(arg
, i
);
2090 settupleitem(newarg
, i
+1, v
);
2097 if (!is_funcobject(func
)) {
2098 err_setstr(TypeError
, "call of non-function");
2103 co
= getfunccode(func
);
2108 if (!is_codeobject(co
)) {
2109 fprintf(stderr
, "XXX Bad code\n");
2112 newlocals
= newdictobject();
2113 if (newlocals
== NULL
) {
2118 newglobals
= getfuncglobals(func
);
2121 v
= eval_code((codeobject
*)co
, newglobals
, newlocals
, class, arg
);
2132 apply_subscript(v
, w
)
2135 typeobject
*tp
= v
->ob_type
;
2136 if (tp
->tp_as_sequence
== NULL
&& tp
->tp_as_mapping
== NULL
) {
2137 err_setstr(TypeError
, "unsubscriptable object");
2140 if (tp
->tp_as_mapping
!= NULL
) {
2141 return (*tp
->tp_as_mapping
->mp_subscript
)(v
, w
);
2145 if (!is_intobject(w
)) {
2146 err_setstr(TypeError
, "sequence subscript not int");
2151 int len
= (*tp
->tp_as_sequence
->sq_length
)(v
);
2156 return (*tp
->tp_as_sequence
->sq_item
)(v
, i
);
2161 loop_subscript(v
, w
)
2164 sequence_methods
*sq
= v
->ob_type
->tp_as_sequence
;
2167 err_setstr(TypeError
, "loop over non-sequence");
2171 n
= (*sq
->sq_length
)(v
);
2173 return NULL
; /* Exception */
2175 return NULL
; /* End of loop */
2176 return (*sq
->sq_item
)(v
, i
);
2180 slice_index(v
, isize
, pi
)
2186 if (!is_intobject(v
)) {
2187 err_setstr(TypeError
, "slice index must be int");
2190 *pi
= getintvalue(v
);
2198 apply_slice(u
, v
, w
) /* return u[v:w] */
2201 typeobject
*tp
= u
->ob_type
;
2202 int ilow
, ihigh
, isize
;
2203 if (tp
->tp_as_sequence
== NULL
) {
2204 err_setstr(TypeError
, "only sequences can be sliced");
2208 isize
= ihigh
= (*tp
->tp_as_sequence
->sq_length
)(u
);
2211 if (slice_index(v
, isize
, &ilow
) != 0)
2213 if (slice_index(w
, isize
, &ihigh
) != 0)
2215 return (*tp
->tp_as_sequence
->sq_slice
)(u
, ilow
, ihigh
);
2219 assign_subscript(w
, key
, v
) /* w[key] = v */
2224 typeobject
*tp
= w
->ob_type
;
2225 sequence_methods
*sq
;
2226 mapping_methods
*mp
;
2228 if ((mp
= tp
->tp_as_mapping
) != NULL
&&
2229 (func
= mp
->mp_ass_subscript
) != NULL
) {
2230 return (*func
)(w
, key
, v
);
2232 else if ((sq
= tp
->tp_as_sequence
) != NULL
&&
2233 (func
= sq
->sq_ass_item
) != NULL
) {
2234 if (!is_intobject(key
)) {
2235 err_setstr(TypeError
,
2236 "sequence subscript must be integer (assign or del)");
2240 int i
= getintvalue(key
);
2242 int len
= (*sq
->sq_length
)(w
);
2247 return (*func
)(w
, i
, v
);
2251 err_setstr(TypeError
,
2252 "can't assign to this subscripted object");
2258 assign_slice(u
, v
, w
, x
) /* u[v:w] = x */
2259 object
*u
, *v
, *w
, *x
;
2261 sequence_methods
*sq
= u
->ob_type
->tp_as_sequence
;
2262 int ilow
, ihigh
, isize
;
2264 err_setstr(TypeError
, "assign to slice of non-sequence");
2267 if (sq
== NULL
|| sq
->sq_ass_slice
== NULL
) {
2268 err_setstr(TypeError
, "unassignable slice");
2272 isize
= ihigh
= (*sq
->sq_length
)(u
);
2275 if (slice_index(v
, isize
, &ilow
) != 0)
2277 if (slice_index(w
, isize
, &ihigh
) != 0)
2279 return (*sq
->sq_ass_slice
)(u
, ilow
, ihigh
, x
);
2283 cmp_exception(err
, v
)
2286 if (is_tupleobject(v
)) {
2288 n
= gettuplesize(v
);
2289 for (i
= 0; i
< n
; i
++) {
2290 /* Test recursively */
2291 if (cmp_exception(err
, gettupleitem(v
, i
)))
2305 sequence_methods
*sq
;
2306 /* Special case for char in string */
2307 if (is_stringobject(w
)) {
2308 register char *s
, *end
;
2310 if (!is_stringobject(v
) || getstringsize(v
) != 1) {
2311 err_setstr(TypeError
,
2312 "string member test needs char left operand");
2315 c
= getstringvalue(v
)[0];
2316 s
= getstringvalue(w
);
2317 end
= s
+ getstringsize(w
);
2324 sq
= w
->ob_type
->tp_as_sequence
;
2326 err_setstr(TypeError
,
2327 "'in' or 'not in' needs sequence right argument");
2330 n
= (*sq
->sq_length
)(w
);
2333 for (i
= 0; i
< n
; i
++) {
2334 x
= (*sq
->sq_item
)(w
, i
);
2335 cmp
= cmpobject(v
, x
);
2344 cmp_outcome(op
, v
, w
)
2350 register int res
= 0;
2355 if (op
== (int) IS_NOT
)
2360 res
= cmp_member(v
, w
);
2363 if (op
== (int) NOT_IN
)
2367 res
= cmp_exception(v
, w
);
2370 cmp
= cmpobject(v
, w
);
2372 case LT
: res
= cmp
< 0; break;
2373 case LE
: res
= cmp
<= 0; break;
2374 case EQ
: res
= cmp
== 0; break;
2375 case NE
: res
= cmp
!= 0; break;
2376 case GT
: res
= cmp
> 0; break;
2377 case GE
: res
= cmp
>= 0; break;
2378 /* XXX no default? (res is initialized to 0 though) */
2381 v
= res
? True
: False
;
2387 import_from(locals
, v
, name
)
2393 w
= getmoduledict(v
);
2394 if (getstringvalue(name
)[0] == '*') {
2396 object
*name
, *value
;
2398 while (mappinggetnext(w
, &pos
, &name
, &value
)) {
2399 if (!is_stringobject(name
) ||
2400 getstringvalue(name
)[0] == '_')
2402 if (is_accessobject(value
)) {
2403 value
= getaccessvalue(value
, (object
*)NULL
);
2404 if (value
== NULL
) {
2411 err
= dict2insert(locals
, name
, value
);
2419 x
= dict2lookup(w
, name
);
2422 sprintf(buf
, "cannot import name %.230s",
2423 getstringvalue(name
));
2424 err_setstr(ImportError
, buf
);
2428 return dict2insert(locals
, name
, x
);
2433 build_class(methods
, bases
, name
)
2434 object
*methods
; /* dictionary */
2435 object
*bases
; /* tuple containing classes */
2436 object
*name
; /* string */
2439 if (!is_tupleobject(bases
)) {
2440 err_setstr(SystemError
, "build_class with non-tuple bases");
2443 if (!is_dictobject(methods
)) {
2444 err_setstr(SystemError
, "build_class with non-dictionary");
2447 if (!is_stringobject(name
)) {
2448 err_setstr(SystemError
, "build_class witn non-string name");
2451 for (i
= gettuplesize(bases
); --i
>= 0; ) {
2452 object
*base
= gettupleitem(bases
, i
);
2453 if (!is_classobject(base
)) {
2454 err_setstr(TypeError
,
2455 "base is not a class object");
2459 return newclassobject(bases
, methods
, name
);
2463 access_statement(name
, vmode
, f
)
2468 int mode
= getintvalue(vmode
);
2473 if (f
->f_localmap
== NULL
)
2474 value
= dict2lookup(f
->f_locals
, name
);
2476 value
= dict2lookup(f
->f_localmap
, name
);
2477 if (value
== NULL
|| !is_intobject(value
))
2480 fastind
= getintvalue(value
);
2482 fastind
< getlistsize(f
->f_fastlocals
))
2483 value
= getlistitem(f
->f_fastlocals
, fastind
);
2490 if (value
&& is_accessobject(value
)) {
2491 err_setstr(AccessError
, "can't override access");
2495 if (value
!= NULL
&& value
!= None
)
2496 type
= value
->ob_type
;
2499 ac
= newaccessobject(value
, f
->f_locals
, type
, mode
);
2503 ret
= setlistitem(f
->f_fastlocals
, fastind
, ac
);
2505 ret
= dict2insert(f
->f_locals
, name
, ac
);
2512 exec_statement(prog
, globals
, locals
)
2520 if (is_tupleobject(prog
) && globals
== None
&& locals
== None
&&
2521 ((n
= gettuplesize(prog
)) == 2 || n
== 3)) {
2522 /* Backward compatibility hack */
2523 globals
= gettupleitem(prog
, 1);
2525 locals
= gettupleitem(prog
, 2);
2526 prog
= gettupleitem(prog
, 0);
2528 if (globals
== None
) {
2529 globals
= getglobals();
2531 locals
= getlocals();
2533 else if (locals
== None
)
2535 if (!is_stringobject(prog
) &&
2536 !is_codeobject(prog
) &&
2537 !is_fileobject(prog
)) {
2538 err_setstr(TypeError
,
2539 "exec 1st arg must be string, code or file object");
2542 if (!is_dictobject(globals
) || !is_dictobject(locals
)) {
2543 err_setstr(TypeError
,
2544 "exec 2nd/3rd args must be dict or None");
2547 if (is_codeobject(prog
)) {
2548 if (eval_code((codeobject
*) prog
, globals
, locals
,
2549 (object
*)NULL
, (object
*)NULL
) == NULL
)
2553 if (is_fileobject(prog
)) {
2554 FILE *fp
= getfilefile(prog
);
2555 char *name
= getstringvalue(getfilename(prog
));
2556 if (run_file(fp
, name
, file_input
, globals
, locals
) == NULL
)
2560 s
= getstringvalue(prog
);
2561 if (strlen(s
) != getstringsize(prog
)) {
2562 err_setstr(ValueError
, "embedded '\\0' in exec string");
2565 if (run_string(s
, file_input
, globals
, locals
) == NULL
)