1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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 or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Execute compiled code */
35 XXX how to pass arguments to call_trace?
36 XXX speed up searching for keywords by using a dictionary
43 #include "frameobject.h"
52 #define INT_MAX 2147483647
55 /* Turn this on if your compiler chokes on the big switch: */
56 /* #define CASE_TOO_BIG 1 */
59 /* For debugging the interpreter: */
60 #define LLTRACE 1 /* Low-level trace feature */
61 #define CHECKEXC 1 /* Double-check exception checking */
65 /* Forward declarations */
67 static PyObject
*eval_code2
Py_PROTO((PyCodeObject
*,
68 PyObject
*, PyObject
*,
74 static int prtrace
Py_PROTO((PyObject
*, char *));
76 static void call_exc_trace
Py_PROTO((PyObject
**, PyObject
**,
78 static int call_trace
Py_PROTO((PyObject
**, PyObject
**,
79 PyFrameObject
*, char *, PyObject
*));
80 static PyObject
*call_builtin
Py_PROTO((PyObject
*, PyObject
*, PyObject
*));
81 static PyObject
*call_function
Py_PROTO((PyObject
*, PyObject
*, PyObject
*));
82 static PyObject
*loop_subscript
Py_PROTO((PyObject
*, PyObject
*));
83 static int slice_index
Py_PROTO((PyObject
*, int *));
84 static PyObject
*apply_slice
Py_PROTO((PyObject
*, PyObject
*, PyObject
*));
85 static int assign_slice
Py_PROTO((PyObject
*, PyObject
*,
86 PyObject
*, PyObject
*));
87 static PyObject
*cmp_outcome
Py_PROTO((int, PyObject
*, PyObject
*));
88 static int import_from
Py_PROTO((PyObject
*, PyObject
*, PyObject
*));
89 static PyObject
*build_class
Py_PROTO((PyObject
*, PyObject
*, PyObject
*));
90 static int exec_statement
Py_PROTO((PyFrameObject
*,
91 PyObject
*, PyObject
*, PyObject
*));
92 static PyObject
*find_from_args
Py_PROTO((PyFrameObject
*, int));
93 static void set_exc_info
Py_PROTO((PyThreadState
*,
94 PyObject
*, PyObject
*, PyObject
*));
95 static void reset_exc_info
Py_PROTO((PyThreadState
*));
98 /* Dynamic execution profile */
99 #ifdef DYNAMIC_EXECUTION_PROFILE
101 static long dxpairs
[257][256];
102 #define dxp dxpairs[256]
104 static long dxp
[256];
112 #include "pythread.h"
114 extern int _PyThread_Started
; /* Flag for Py_Exit */
116 static PyThread_type_lock interpreter_lock
= 0;
117 static long main_thread
= 0;
122 if (interpreter_lock
)
124 _PyThread_Started
= 1;
125 interpreter_lock
= PyThread_allocate_lock();
126 PyThread_acquire_lock(interpreter_lock
, 1);
127 main_thread
= PyThread_get_thread_ident();
133 PyThread_acquire_lock(interpreter_lock
, 1);
139 PyThread_release_lock(interpreter_lock
);
143 PyEval_AcquireThread(tstate
)
144 PyThreadState
*tstate
;
147 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
148 PyThread_acquire_lock(interpreter_lock
, 1);
149 if (PyThreadState_Swap(tstate
) != NULL
)
151 "PyEval_AcquireThread: non-NULL old thread state");
155 PyEval_ReleaseThread(tstate
)
156 PyThreadState
*tstate
;
159 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
160 if (PyThreadState_Swap(NULL
) != tstate
)
161 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
162 PyThread_release_lock(interpreter_lock
);
166 /* Functions save_thread and restore_thread are always defined so
167 dynamically loaded modules needn't be compiled separately for use
168 with and without threads: */
173 PyThreadState
*tstate
= PyThreadState_Swap(NULL
);
175 Py_FatalError("PyEval_SaveThread: NULL tstate");
177 if (interpreter_lock
)
178 PyThread_release_lock(interpreter_lock
);
184 PyEval_RestoreThread(tstate
)
185 PyThreadState
*tstate
;
188 Py_FatalError("PyEval_RestoreThread: NULL tstate");
190 if (interpreter_lock
) {
192 PyThread_acquire_lock(interpreter_lock
, 1);
196 PyThreadState_Swap(tstate
);
200 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
201 signal handlers or Mac I/O completion routines) can schedule calls
202 to a function to be called synchronously.
203 The synchronous function is called with one void* argument.
204 It should return 0 for success or -1 for failure -- failure should
205 be accompanied by an exception.
207 If registry succeeds, the registry function returns 0; if it fails
208 (e.g. due to too many pending calls) it returns -1 (without setting
209 an exception condition).
211 Note that because registry may occur from within signal handlers,
212 or other asynchronous events, calling malloc() is unsafe!
215 Any thread can schedule pending calls, but only the main thread
219 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
220 There are two possible race conditions:
221 (1) nested asynchronous registry calls;
222 (2) registry calls made while pending calls are being processed.
223 While (1) is very unlikely, (2) is a real possibility.
224 The current code is safe against (2), but not against (1).
225 The safety against (2) is derived from the fact that only one
226 thread (the main thread) ever takes things out of the queue.
228 XXX Darn! With the advent of thread state, we should have an array
229 of pending calls per thread in the thread state! Later...
232 #define NPENDINGCALLS 32
234 int (*func
) Py_PROTO((ANY
*));
236 } pendingcalls
[NPENDINGCALLS
];
237 static volatile int pendingfirst
= 0;
238 static volatile int pendinglast
= 0;
239 static volatile int things_to_do
= 0;
242 Py_AddPendingCall(func
, arg
)
243 int (*func
) Py_PROTO((ANY
*));
248 /* XXX Begin critical section */
249 /* XXX If you want this to be safe against nested
250 XXX asynchronous calls, you'll have to work harder! */
255 j
= (i
+ 1) % NPENDINGCALLS
;
256 if (j
== pendingfirst
)
257 return -1; /* Queue full */
258 pendingcalls
[i
].func
= func
;
259 pendingcalls
[i
].arg
= arg
;
261 things_to_do
= 1; /* Signal main loop */
263 /* XXX End critical section */
268 Py_MakePendingCalls()
272 if (main_thread
&& PyThread_get_thread_ident() != main_thread
)
281 int (*func
) Py_PROTO((ANY
*));
284 if (i
== pendinglast
)
285 break; /* Queue empty */
286 func
= pendingcalls
[i
].func
;
287 arg
= pendingcalls
[i
].arg
;
288 pendingfirst
= (i
+ 1) % NPENDINGCALLS
;
291 things_to_do
= 1; /* We're not done yet */
300 /* Status code for main loop (reason for stack unwind) */
303 WHY_NOT
, /* No error */
304 WHY_EXCEPTION
, /* Exception occurred */
305 WHY_RERAISE
, /* Exception re-raised by 'finally' */
306 WHY_RETURN
, /* 'return' statement */
307 WHY_BREAK
/* 'break' statement */
310 static enum why_code do_raise
Py_PROTO((PyObject
*, PyObject
*, PyObject
*));
311 static int unpack_sequence
Py_PROTO((PyObject
*, int, PyObject
**));
314 /* Backward compatible interface */
317 PyEval_EvalCode(co
, globals
, locals
)
322 return eval_code2(co
,
324 (PyObject
**)NULL
, 0,
325 (PyObject
**)NULL
, 0,
326 (PyObject
**)NULL
, 0,
331 /* Interpreter main loop */
333 #ifndef MAX_RECURSION_DEPTH
334 #define MAX_RECURSION_DEPTH 10000
338 eval_code2(co
, globals
, locals
,
339 args
, argcount
, kws
, kwcount
, defs
, defcount
, owner
)
345 PyObject
**kws
; /* length: 2*kwcount */
354 register unsigned char *next_instr
;
355 register int opcode
; /* Current opcode */
356 register int oparg
; /* Current opcode argument, if any */
357 register PyObject
**stack_pointer
;
358 register enum why_code why
; /* Reason for block stack unwind */
359 register int err
; /* Error status -- nonzero if error */
360 register PyObject
*x
; /* Result object -- NULL if error */
361 register PyObject
*v
; /* Temporary objects popped off stack */
362 register PyObject
*w
;
363 register PyObject
*u
;
364 register PyObject
*t
;
365 register PyFrameObject
*f
; /* Current frame */
366 register PyObject
**fastlocals
;
367 PyObject
*retval
= NULL
; /* Return value */
368 PyThreadState
*tstate
= PyThreadState_GET();
369 unsigned char *first_instr
;
373 #if defined(Py_DEBUG) || defined(LLTRACE)
374 /* Make it easier to find out where we are with a debugger */
375 char *filename
= PyString_AsString(co
->co_filename
);
378 /* Code access macros */
380 #define GETCONST(i) Getconst(f, i)
381 #define GETNAME(i) Getname(f, i)
382 #define GETNAMEV(i) Getnamev(f, i)
383 #define INSTR_OFFSET() (next_instr - first_instr)
384 #define NEXTOP() (*next_instr++)
385 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
386 #define JUMPTO(x) (next_instr = first_instr + (x))
387 #define JUMPBY(x) (next_instr += (x))
389 /* Stack manipulation macros */
391 #define STACK_LEVEL() (stack_pointer - f->f_valuestack)
392 #define EMPTY() (STACK_LEVEL() == 0)
393 #define TOP() (stack_pointer[-1])
394 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
395 #define BASIC_POP() (*--stack_pointer)
398 #define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
399 #define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
401 #define PUSH(v) BASIC_PUSH(v)
402 #define POP() BASIC_POP()
405 /* Local variable macros */
407 #define GETLOCAL(i) (fastlocals[i])
408 #define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \
409 GETLOCAL(i) = value; } while (0)
413 #ifdef USE_STACKCHECK
414 if (tstate
->recursion_depth
%10 == 0 && PyOS_CheckStack()) {
415 PyErr_SetString(PyExc_MemoryError
, "Stack overflow");
420 if (globals
== NULL
) {
421 PyErr_SetString(PyExc_SystemError
, "eval_code2: NULL globals");
426 lltrace
= PyDict_GetItemString(globals
, "__lltrace__") != NULL
;
438 fastlocals
= f
->f_localsplus
;
440 if (co
->co_argcount
> 0 ||
441 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
444 PyObject
*kwdict
= NULL
;
445 if (co
->co_flags
& CO_VARKEYWORDS
) {
446 kwdict
= PyDict_New();
450 if (co
->co_flags
& CO_VARARGS
)
454 if (argcount
> co
->co_argcount
) {
455 if (!(co
->co_flags
& CO_VARARGS
)) {
456 PyErr_Format(PyExc_TypeError
,
457 "too many arguments; expected %d, got %d",
458 co
->co_argcount
, argcount
);
463 for (i
= 0; i
< n
; i
++) {
468 if (co
->co_flags
& CO_VARARGS
) {
469 u
= PyTuple_New(argcount
- n
);
472 SETLOCAL(co
->co_argcount
, u
);
473 for (i
= n
; i
< argcount
; i
++) {
476 PyTuple_SET_ITEM(u
, i
-n
, x
);
479 for (i
= 0; i
< kwcount
; i
++) {
480 PyObject
*keyword
= kws
[2*i
];
481 PyObject
*value
= kws
[2*i
+ 1];
483 /* XXX slow -- speed up using dictionary? */
484 for (j
= 0; j
< co
->co_argcount
; j
++) {
485 PyObject
*nm
= PyTuple_GET_ITEM(
487 if (PyObject_Compare(keyword
, nm
) == 0)
490 /* Check errors from Compare */
491 if (PyErr_Occurred())
493 if (j
>= co
->co_argcount
) {
494 if (kwdict
== NULL
) {
495 PyErr_Format(PyExc_TypeError
,
496 "unexpected keyword argument: %.400s",
497 PyString_AsString(keyword
));
500 PyDict_SetItem(kwdict
, keyword
, value
);
503 if (GETLOCAL(j
) != NULL
) {
504 PyErr_SetString(PyExc_TypeError
,
505 "keyword parameter redefined");
512 if (argcount
< co
->co_argcount
) {
513 int m
= co
->co_argcount
- defcount
;
514 for (i
= argcount
; i
< m
; i
++) {
515 if (GETLOCAL(i
) == NULL
) {
516 PyErr_Format(PyExc_TypeError
,
517 "not enough arguments; expected %d, got %d",
526 for (; i
< defcount
; i
++) {
527 if (GETLOCAL(m
+i
) == NULL
) {
528 PyObject
*def
= defs
[i
];
536 if (argcount
> 0 || kwcount
> 0) {
537 PyErr_SetString(PyExc_TypeError
,
538 "no arguments expected");
543 if (tstate
->sys_tracefunc
!= NULL
) {
544 /* tstate->sys_tracefunc, if defined, is a function that
545 will be called on *every* entry to a code block.
546 Its return value, if not None, is a function that
547 will be called at the start of each executed line
548 of code. (Actually, the function must return
549 itself in order to continue tracing.)
550 The trace functions are called with three arguments:
551 a pointer to the current frame, a string indicating
552 why the function is called, and an argument which
553 depends on the situation. The global trace function
554 (sys.trace) is also called whenever an exception
556 if (call_trace(&tstate
->sys_tracefunc
,
557 &f
->f_trace
, f
, "call",
558 Py_None
/*XXX how to compute arguments now?*/)) {
559 /* Trace function raised an error */
564 if (tstate
->sys_profilefunc
!= NULL
) {
565 /* Similar for sys_profilefunc, except it needn't return
566 itself and isn't called for "line" events */
567 if (call_trace(&tstate
->sys_profilefunc
,
568 (PyObject
**)0, f
, "call",
574 if (++tstate
->recursion_depth
> MAX_RECURSION_DEPTH
) {
575 --tstate
->recursion_depth
;
576 PyErr_SetString(PyExc_RuntimeError
,
577 "Maximum recursion depth exceeded");
578 tstate
->frame
= f
->f_back
;
583 _PyCode_GETCODEPTR(co
, &first_instr
);
584 next_instr
= first_instr
;
585 stack_pointer
= f
->f_valuestack
;
589 x
= Py_None
; /* Not a reference, just anything non-NULL */
592 /* Do periodic things. Doing this every time through
593 the loop would add too much overhead, so we do it
594 only every Nth instruction. We also do it if
595 ``things_to_do'' is set, i.e. when an asynchronous
596 event needs attention (e.g. a signal handler or
597 async I/O handler); see Py_AddPendingCall() and
598 Py_MakePendingCalls() above. */
600 if (things_to_do
|| --tstate
->ticker
< 0) {
601 tstate
->ticker
= tstate
->interp
->checkinterval
;
603 if (Py_MakePendingCalls() < 0) {
608 #if !defined(HAVE_SIGNAL_H) || defined(macintosh)
609 /* If we have true signals, the signal handler
610 will call Py_AddPendingCall() so we don't
611 have to call sigcheck(). On the Mac and
612 DOS, alas, we have to call it. */
613 if (PyErr_CheckSignals()) {
620 if (interpreter_lock
) {
621 /* Give another thread a chance */
623 if (PyThreadState_Swap(NULL
) != tstate
)
624 Py_FatalError("ceval: tstate mix-up");
625 PyThread_release_lock(interpreter_lock
);
627 /* Other threads may run now */
629 PyThread_acquire_lock(interpreter_lock
, 1);
630 if (PyThreadState_Swap(tstate
) != NULL
)
631 Py_FatalError("ceval: orphan tstate");
636 /* Extract opcode and argument */
638 #if defined(Py_DEBUG) || defined(LLTRACE)
639 f
->f_lasti
= INSTR_OFFSET();
645 #ifdef DYNAMIC_EXECUTION_PROFILE
647 dxpairs
[lastopcode
][opcode
]++;
654 /* Instruction tracing */
657 if (HAS_ARG(opcode
)) {
658 printf("%d: %d, %d\n",
659 (int) (INSTR_OFFSET() - 3),
664 (int) (INSTR_OFFSET() - 1), opcode
);
669 /* Main switch on opcode */
674 It is essential that any operation that fails sets either
675 x to NULL, err to nonzero, or why to anything but WHY_NOT,
676 and that no operation that succeeds does this! */
678 /* case STOP_CODE: this is an error! */
709 x
= PyNumber_Positive(v
);
712 if (x
!= NULL
) continue;
717 x
= PyNumber_Negative(v
);
720 if (x
!= NULL
) continue;
725 err
= PyObject_IsTrue(v
);
742 x
= PyObject_Repr(v
);
745 if (x
!= NULL
) continue;
750 x
= PyNumber_Invert(v
);
753 if (x
!= NULL
) continue;
759 x
= PyNumber_Power(v
, w
, Py_None
);
763 if (x
!= NULL
) continue;
766 case BINARY_MULTIPLY
:
769 x
= PyNumber_Multiply(v
, w
);
773 if (x
!= NULL
) continue;
779 x
= PyNumber_Divide(v
, w
);
783 if (x
!= NULL
) continue;
789 x
= PyNumber_Remainder(v
, w
);
793 if (x
!= NULL
) continue;
799 if (PyInt_Check(v
) && PyInt_Check(w
)) {
800 /* INLINE: int + int */
801 register long a
, b
, i
;
802 a
= PyInt_AS_LONG(v
);
803 b
= PyInt_AS_LONG(w
);
805 if ((i
^a
) < 0 && (i
^b
) < 0) {
806 PyErr_SetString(PyExc_OverflowError
,
811 x
= PyInt_FromLong(i
);
814 x
= PyNumber_Add(v
, w
);
818 if (x
!= NULL
) continue;
821 case BINARY_SUBTRACT
:
824 if (PyInt_Check(v
) && PyInt_Check(w
)) {
825 /* INLINE: int - int */
826 register long a
, b
, i
;
827 a
= PyInt_AS_LONG(v
);
828 b
= PyInt_AS_LONG(w
);
830 if ((i
^a
) < 0 && (i
^~b
) < 0) {
831 PyErr_SetString(PyExc_OverflowError
,
832 "integer subtraction");
836 x
= PyInt_FromLong(i
);
839 x
= PyNumber_Subtract(v
, w
);
843 if (x
!= NULL
) continue;
849 if (PyList_Check(v
) && PyInt_Check(w
)) {
850 /* INLINE: list[int] */
851 long i
= PyInt_AsLong(w
);
853 i
+= PyList_GET_SIZE(v
);
855 i
>= PyList_GET_SIZE(v
)) {
856 PyErr_SetString(PyExc_IndexError
,
857 "list index out of range");
861 x
= PyList_GET_ITEM(v
, i
);
866 x
= PyObject_GetItem(v
, w
);
870 if (x
!= NULL
) continue;
876 x
= PyNumber_Lshift(v
, w
);
880 if (x
!= NULL
) continue;
886 x
= PyNumber_Rshift(v
, w
);
890 if (x
!= NULL
) continue;
896 x
= PyNumber_And(v
, w
);
900 if (x
!= NULL
) continue;
906 x
= PyNumber_Xor(v
, w
);
910 if (x
!= NULL
) continue;
916 x
= PyNumber_Or(v
, w
);
920 if (x
!= NULL
) continue;
927 if ((opcode
-SLICE
) & 2)
931 if ((opcode
-SLICE
) & 1)
936 x
= apply_slice(u
, v
, w
);
941 if (x
!= NULL
) continue;
948 if ((opcode
-STORE_SLICE
) & 2)
952 if ((opcode
-STORE_SLICE
) & 1)
958 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
963 if (err
== 0) continue;
970 if ((opcode
-DELETE_SLICE
) & 2)
974 if ((opcode
-DELETE_SLICE
) & 1)
979 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
984 if (err
== 0) continue;
992 err
= PyObject_SetItem(v
, w
, u
);
996 if (err
== 0) continue;
1003 err
= PyObject_DelItem(v
, w
);
1006 if (err
== 0) continue;
1011 /* Print value except if None */
1012 /* After printing, also assign to '_' */
1013 /* Before, set '_' to None to avoid recursion */
1015 (err
= PyDict_SetItemString(
1016 f
->f_builtins
, "_", Py_None
)) == 0) {
1017 err
= Py_FlushLine();
1019 x
= PySys_GetObject("stdout");
1028 err
= PyFile_WriteObject(v
, x
, 0);
1030 PyFile_SoftSpace(x
, 1);
1031 err
= Py_FlushLine();
1034 err
= PyDict_SetItemString(
1035 f
->f_builtins
, "_", v
);
1043 w
= PySys_GetObject("stdout");
1045 PyErr_SetString(PyExc_RuntimeError
,
1049 else if (PyFile_SoftSpace(w
, 1))
1050 err
= PyFile_WriteString(" ", w
);
1052 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1053 if (err
== 0 && PyString_Check(v
)) {
1054 /* XXX move into writeobject() ? */
1055 char *s
= PyString_AsString(v
);
1056 int len
= PyString_Size(v
);
1058 isspace(Py_CHARMASK(s
[len
-1])) &&
1060 PyFile_SoftSpace(w
, 0);
1063 if (err
== 0) continue;
1067 x
= PySys_GetObject("stdout");
1069 PyErr_SetString(PyExc_RuntimeError
,
1072 err
= PyFile_WriteString("\n", x
);
1074 PyFile_SoftSpace(x
, 0);
1086 u
= POP(); /* traceback */
1089 v
= POP(); /* value */
1092 w
= POP(); /* exc */
1093 case 0: /* Fallthrough */
1094 why
= do_raise(w
, v
, u
);
1097 PyErr_SetString(PyExc_SystemError
,
1098 "bad RAISE_VARARGS oparg");
1099 why
= WHY_EXCEPTION
;
1105 if ((x
= f
->f_locals
) == NULL
) {
1106 PyErr_SetString(PyExc_SystemError
,
1123 err
= exec_statement(f
, u
, v
, w
);
1131 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1132 while (STACK_LEVEL() > b
->b_level
) {
1141 if (PyInt_Check(v
)) {
1142 why
= (enum why_code
) PyInt_AsLong(v
);
1143 if (why
== WHY_RETURN
)
1146 else if (PyString_Check(v
) || PyClass_Check(v
)) {
1149 PyErr_Restore(v
, w
, u
);
1153 else if (v
!= Py_None
) {
1154 PyErr_SetString(PyExc_SystemError
,
1155 "'finally' pops bad exception");
1156 why
= WHY_EXCEPTION
;
1165 x
= build_class(u
, v
, w
);
1173 w
= GETNAMEV(oparg
);
1175 if ((x
= f
->f_locals
) == NULL
) {
1176 PyErr_SetString(PyExc_SystemError
,
1180 err
= PyDict_SetItem(x
, w
, v
);
1185 w
= GETNAMEV(oparg
);
1186 if ((x
= f
->f_locals
) == NULL
) {
1187 PyErr_SetString(PyExc_SystemError
,
1191 if ((err
= PyDict_DelItem(x
, w
)) != 0)
1192 PyErr_SetObject(PyExc_NameError
, w
);
1196 default: switch (opcode
) {
1202 if (PyTuple_Check(v
)) {
1203 if (PyTuple_Size(v
) != oparg
) {
1204 PyErr_SetString(PyExc_ValueError
,
1205 "unpack tuple of wrong size");
1206 why
= WHY_EXCEPTION
;
1209 for (; --oparg
>= 0; ) {
1210 w
= PyTuple_GET_ITEM(v
, oparg
);
1216 else if (PyList_Check(v
)) {
1217 if (PyList_Size(v
) != oparg
) {
1218 PyErr_SetString(PyExc_ValueError
,
1219 "unpack list of wrong size");
1220 why
= WHY_EXCEPTION
;
1223 for (; --oparg
>= 0; ) {
1224 w
= PyList_GET_ITEM(v
, oparg
);
1230 else if (PySequence_Check(v
)) {
1231 if (unpack_sequence(v
, oparg
,
1232 stack_pointer
+ oparg
))
1233 stack_pointer
+= oparg
;
1235 why
= WHY_EXCEPTION
;
1238 PyErr_SetString(PyExc_TypeError
,
1239 "unpack non-sequence");
1240 why
= WHY_EXCEPTION
;
1246 w
= GETNAMEV(oparg
);
1249 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1255 w
= GETNAMEV(oparg
);
1257 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1263 w
= GETNAMEV(oparg
);
1265 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1270 w
= GETNAMEV(oparg
);
1271 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1272 PyErr_SetObject(PyExc_NameError
, w
);
1276 x
= GETCONST(oparg
);
1282 w
= GETNAMEV(oparg
);
1283 if ((x
= f
->f_locals
) == NULL
) {
1284 PyErr_SetString(PyExc_SystemError
,
1288 x
= PyDict_GetItem(x
, w
);
1290 x
= PyDict_GetItem(f
->f_globals
, w
);
1292 x
= PyDict_GetItem(f
->f_builtins
, w
);
1295 PyExc_NameError
, w
);
1305 w
= GETNAMEV(oparg
);
1306 x
= PyDict_GetItem(f
->f_globals
, w
);
1308 x
= PyDict_GetItem(f
->f_builtins
, w
);
1310 PyErr_SetObject(PyExc_NameError
, w
);
1319 x
= GETLOCAL(oparg
);
1321 PyErr_SetObject(PyExc_NameError
,
1322 PyTuple_GetItem(co
->co_varnames
,
1328 if (x
!= NULL
) continue;
1337 x
= GETLOCAL(oparg
);
1339 PyErr_SetObject(PyExc_NameError
,
1340 PyTuple_GetItem(co
->co_varnames
,
1344 SETLOCAL(oparg
, NULL
);
1348 x
= PyTuple_New(oparg
);
1350 for (; --oparg
>= 0;) {
1352 PyTuple_SET_ITEM(x
, oparg
, w
);
1360 x
= PyList_New(oparg
);
1362 for (; --oparg
>= 0;) {
1364 PyList_SET_ITEM(x
, oparg
, w
);
1374 if (x
!= NULL
) continue;
1378 w
= GETNAMEV(oparg
);
1380 x
= PyObject_GetAttr(v
, w
);
1383 if (x
!= NULL
) continue;
1389 if (PyInt_Check(v
) && PyInt_Check(w
)) {
1390 /* INLINE: cmp(int, int) */
1393 a
= PyInt_AS_LONG(v
);
1394 b
= PyInt_AS_LONG(w
);
1396 case LT
: res
= a
< b
; break;
1397 case LE
: res
= a
<= b
; break;
1398 case EQ
: res
= a
== b
; break;
1399 case NE
: res
= a
!= b
; break;
1400 case GT
: res
= a
> b
; break;
1401 case GE
: res
= a
>= b
; break;
1402 case IS
: res
= v
== w
; break;
1403 case IS_NOT
: res
= v
!= w
; break;
1404 default: goto slow_compare
;
1406 x
= res
? Py_True
: Py_False
;
1411 x
= cmp_outcome(oparg
, v
, w
);
1416 if (x
!= NULL
) continue;
1420 w
= GETNAMEV(oparg
);
1421 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
1423 PyErr_SetString(PyExc_ImportError
,
1424 "__import__ not found");
1427 u
= find_from_args(f
, INSTR_OFFSET());
1432 w
= Py_BuildValue("(OOOO)",
1435 f
->f_locals
== NULL
?
1436 Py_None
: f
->f_locals
,
1443 x
= PyEval_CallObject(x
, w
);
1446 if (x
!= NULL
) continue;
1450 w
= GETNAMEV(oparg
);
1452 PyFrame_FastToLocals(f
);
1453 if ((x
= f
->f_locals
) == NULL
) {
1454 PyErr_SetString(PyExc_SystemError
,
1458 err
= import_from(x
, v
, w
);
1459 PyFrame_LocalsToFast(f
, 0);
1460 if (err
== 0) continue;
1468 err
= PyObject_IsTrue(TOP());
1478 err
= PyObject_IsTrue(TOP());
1495 On entry: stack contains s, i.
1496 On exit: stack contains s, i+1, s[i];
1497 but if loop exhausted:
1498 s, i are popped, and we jump */
1499 w
= POP(); /* Loop index */
1500 v
= POP(); /* Sequence object */
1501 u
= loop_subscript(v
, w
);
1504 x
= PyInt_FromLong(PyInt_AsLong(w
)+1);
1508 if (x
!= NULL
) continue;
1513 /* A NULL can mean "s exhausted"
1514 but also an error: */
1515 if (PyErr_Occurred())
1516 why
= WHY_EXCEPTION
;
1527 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
1534 printf("--- %s:%d \n", filename
, oparg
);
1536 f
->f_lineno
= oparg
;
1537 if (f
->f_trace
== NULL
)
1539 /* Trace each line of code reached */
1540 f
->f_lasti
= INSTR_OFFSET();
1541 err
= call_trace(&f
->f_trace
, &f
->f_trace
,
1542 f
, "line", Py_None
);
1547 int na
= oparg
& 0xff;
1548 int nk
= (oparg
>>8) & 0xff;
1550 PyObject
**pfunc
= stack_pointer
- n
- 1;
1551 PyObject
*func
= *pfunc
;
1552 PyObject
*self
= NULL
;
1553 PyObject
*class = NULL
;
1554 f
->f_lasti
= INSTR_OFFSET() - 3; /* For tracing */
1555 if (PyMethod_Check(func
)) {
1556 self
= PyMethod_Self(func
);
1557 class = PyMethod_Class(func
);
1558 func
= PyMethod_Function(func
);
1568 /* Unbound methods must be
1569 called with an instance of
1570 the class (or a derived
1571 class) as first argument */
1573 (self
= stack_pointer
[-n
])
1575 PyInstance_Check(self
) &&
1578 (((PyInstanceObject
*)self
)
1585 "unbound method must be called with class instance 1st argument");
1593 if (PyFunction_Check(func
)) {
1594 PyObject
*co
= PyFunction_GetCode(func
);
1596 PyFunction_GetGlobals(func
);
1598 PyFunction_GetDefaults(func
);
1601 if (argdefs
!= NULL
) {
1602 d
= &PyTuple_GET_ITEM(argdefs
, 0);
1603 nd
= ((PyTupleObject
*)argdefs
) ->
1612 globals
, (PyObject
*)NULL
,
1613 stack_pointer
-n
, na
,
1614 stack_pointer
-2*nk
, nk
,
1619 PyObject
*args
= PyTuple_New(na
);
1620 PyObject
*kwdict
= NULL
;
1626 kwdict
= PyDict_New();
1627 if (kwdict
== NULL
) {
1633 PyObject
*value
= POP();
1634 PyObject
*key
= POP();
1635 err
= PyDict_SetItem(
1636 kwdict
, key
, value
);
1650 PyTuple_SET_ITEM(args
, na
, w
);
1652 x
= PyEval_CallObjectWithKeywords(
1653 func
, args
, kwdict
);
1658 while (stack_pointer
> pfunc
) {
1663 if (x
!= NULL
) continue;
1668 v
= POP(); /* code object */
1669 x
= PyFunction_New(v
, f
->f_globals
);
1671 /* XXX Maybe this should be a separate opcode? */
1672 if (x
!= NULL
&& oparg
> 0) {
1673 v
= PyTuple_New(oparg
);
1679 while (--oparg
>= 0) {
1681 PyTuple_SET_ITEM(v
, oparg
, w
);
1683 err
= PyFunction_SetDefaults(x
, v
);
1696 x
= PySlice_New(u
, v
, w
);
1701 if (x
!= NULL
) continue;
1707 "XXX lineno: %d, opcode: %d\n",
1708 f
->f_lineno
, opcode
);
1709 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
1710 why
= WHY_EXCEPTION
;
1721 /* Quickly continue if no error occurred */
1723 if (why
== WHY_NOT
) {
1724 if (err
== 0 && x
!= NULL
) {
1726 if (PyErr_Occurred())
1728 "XXX undetected error\n");
1731 continue; /* Normal, fast path */
1733 why
= WHY_EXCEPTION
;
1739 /* Double-check exception status */
1741 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
1742 if (!PyErr_Occurred()) {
1743 fprintf(stderr
, "XXX ghost error\n");
1744 PyErr_SetString(PyExc_SystemError
,
1746 why
= WHY_EXCEPTION
;
1750 if (PyErr_Occurred()) {
1752 "XXX undetected error (why=%d)\n",
1754 why
= WHY_EXCEPTION
;
1759 /* Log traceback info if this is a real exception */
1761 if (why
== WHY_EXCEPTION
) {
1762 f
->f_lasti
= INSTR_OFFSET() - 1;
1763 if (HAS_ARG(opcode
))
1765 PyTraceBack_Here(f
);
1768 call_exc_trace(&f
->f_trace
, &f
->f_trace
, f
);
1769 if (tstate
->sys_profilefunc
)
1770 call_exc_trace(&tstate
->sys_profilefunc
,
1774 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
1776 if (why
== WHY_RERAISE
)
1777 why
= WHY_EXCEPTION
;
1779 /* Unwind stacks if a (pseudo) exception occurred */
1781 while (why
!= WHY_NOT
&& f
->f_iblock
> 0) {
1782 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1783 while (STACK_LEVEL() > b
->b_level
) {
1787 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
1789 JUMPTO(b
->b_handler
);
1792 if (b
->b_type
== SETUP_FINALLY
||
1793 (b
->b_type
== SETUP_EXCEPT
&&
1794 why
== WHY_EXCEPTION
)) {
1795 if (why
== WHY_EXCEPTION
) {
1796 PyObject
*exc
, *val
, *tb
;
1797 PyErr_Fetch(&exc
, &val
, &tb
);
1802 /* Make the raw exception data
1803 available to the handler,
1804 so a program can emulate the
1805 Python main loop. Don't do
1806 this for 'finally'. */
1807 if (b
->b_type
== SETUP_EXCEPT
) {
1808 PyErr_NormalizeException(
1810 set_exc_info(tstate
,
1818 if (why
== WHY_RETURN
)
1820 v
= PyInt_FromLong((long)why
);
1824 JUMPTO(b
->b_handler
);
1827 } /* unwind stack */
1829 /* End the loop if we still have an error (or return) */
1836 /* Pop remaining stack entries */
1843 if (why
!= WHY_RETURN
)
1847 if (why
== WHY_RETURN
) {
1848 if (call_trace(&f
->f_trace
, &f
->f_trace
, f
,
1849 "return", retval
)) {
1852 why
= WHY_EXCEPTION
;
1857 if (tstate
->sys_profilefunc
&& why
== WHY_RETURN
) {
1858 if (call_trace(&tstate
->sys_profilefunc
, (PyObject
**)0,
1859 f
, "return", retval
)) {
1862 why
= WHY_EXCEPTION
;
1866 reset_exc_info(tstate
);
1868 --tstate
->recursion_depth
;
1870 fail
: /* Jump here from prelude on failure */
1872 /* Restore previous frame and release the current one */
1874 tstate
->frame
= f
->f_back
;
1881 set_exc_info(tstate
, type
, value
, tb
)
1882 PyThreadState
*tstate
;
1887 PyFrameObject
*frame
;
1888 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
1890 frame
= tstate
->frame
;
1891 if (frame
->f_exc_type
== NULL
) {
1892 /* This frame didn't catch an exception before */
1893 /* Save previous exception of this thread in this frame */
1894 if (tstate
->exc_type
== NULL
) {
1896 tstate
->exc_type
= Py_None
;
1898 tmp_type
= frame
->f_exc_type
;
1899 tmp_value
= frame
->f_exc_value
;
1900 tmp_tb
= frame
->f_exc_traceback
;
1901 Py_XINCREF(tstate
->exc_type
);
1902 Py_XINCREF(tstate
->exc_value
);
1903 Py_XINCREF(tstate
->exc_traceback
);
1904 frame
->f_exc_type
= tstate
->exc_type
;
1905 frame
->f_exc_value
= tstate
->exc_value
;
1906 frame
->f_exc_traceback
= tstate
->exc_traceback
;
1907 Py_XDECREF(tmp_type
);
1908 Py_XDECREF(tmp_value
);
1911 /* Set new exception for this thread */
1912 tmp_type
= tstate
->exc_type
;
1913 tmp_value
= tstate
->exc_value
;
1914 tmp_tb
= tstate
->exc_traceback
;
1918 tstate
->exc_type
= type
;
1919 tstate
->exc_value
= value
;
1920 tstate
->exc_traceback
= tb
;
1921 Py_XDECREF(tmp_type
);
1922 Py_XDECREF(tmp_value
);
1924 /* For b/w compatibility */
1925 PySys_SetObject("exc_type", type
);
1926 PySys_SetObject("exc_value", value
);
1927 PySys_SetObject("exc_traceback", tb
);
1931 reset_exc_info(tstate
)
1932 PyThreadState
*tstate
;
1934 PyFrameObject
*frame
;
1935 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
1936 frame
= tstate
->frame
;
1937 if (frame
->f_exc_type
!= NULL
) {
1938 /* This frame caught an exception */
1939 tmp_type
= tstate
->exc_type
;
1940 tmp_value
= tstate
->exc_value
;
1941 tmp_tb
= tstate
->exc_traceback
;
1942 Py_XINCREF(frame
->f_exc_type
);
1943 Py_XINCREF(frame
->f_exc_value
);
1944 Py_XINCREF(frame
->f_exc_traceback
);
1945 tstate
->exc_type
= frame
->f_exc_type
;
1946 tstate
->exc_value
= frame
->f_exc_value
;
1947 tstate
->exc_traceback
= frame
->f_exc_traceback
;
1948 Py_XDECREF(tmp_type
);
1949 Py_XDECREF(tmp_value
);
1951 /* For b/w compatibility */
1952 PySys_SetObject("exc_type", frame
->f_exc_type
);
1953 PySys_SetObject("exc_value", frame
->f_exc_value
);
1954 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
1956 tmp_type
= frame
->f_exc_type
;
1957 tmp_value
= frame
->f_exc_value
;
1958 tmp_tb
= frame
->f_exc_traceback
;
1959 frame
->f_exc_type
= NULL
;
1960 frame
->f_exc_value
= NULL
;
1961 frame
->f_exc_traceback
= NULL
;
1962 Py_XDECREF(tmp_type
);
1963 Py_XDECREF(tmp_value
);
1967 /* Logic for the raise statement (too complicated for inlining).
1968 This *consumes* a reference count to each of its arguments. */
1969 static enum why_code
1970 do_raise(type
, value
, tb
)
1971 PyObject
*type
, *value
, *tb
;
1975 PyThreadState
*tstate
= PyThreadState_Get();
1976 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
1977 value
= tstate
->exc_value
;
1978 tb
= tstate
->exc_traceback
;
1984 /* We support the following forms of raise:
1985 raise <class>, <classinstance>
1986 raise <class>, <argument tuple>
1988 raise <class>, <argument>
1989 raise <classinstance>, None
1990 raise <string>, <object>
1991 raise <string>, None
1993 An omitted second argument is the same as None.
1995 In addition, raise <tuple>, <anything> is the same as
1996 raising the tuple's first item (and it better have one!);
1997 this rule is applied recursively.
1999 Finally, an optional third argument can be supplied, which
2000 gives the traceback to be substituted (useful when
2001 re-raising an exception after examining it). */
2003 /* First, check the traceback argument, replacing None with
2005 if (tb
== Py_None
) {
2009 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
2010 PyErr_SetString(PyExc_TypeError
,
2011 "raise 3rd arg must be traceback or None");
2015 /* Next, replace a missing value with None */
2016 if (value
== NULL
) {
2021 /* Next, repeatedly, replace a tuple exception with its first item */
2022 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
2023 PyObject
*tmp
= type
;
2024 type
= PyTuple_GET_ITEM(type
, 0);
2029 if (PyString_Check(type
))
2032 else if (PyClass_Check(type
))
2033 PyErr_NormalizeException(&type
, &value
, &tb
);
2035 else if (PyInstance_Check(type
)) {
2036 /* Raising an instance. The value should be a dummy. */
2037 if (value
!= Py_None
) {
2038 PyErr_SetString(PyExc_TypeError
,
2039 "instance exception may not have a separate value");
2043 /* Normalize to raise <class>, <instance> */
2046 type
= (PyObject
*) ((PyInstanceObject
*)type
)->in_class
;
2051 /* Not something you can raise. You get an exception
2052 anyway, just not what you specified :-) */
2053 PyErr_SetString(PyExc_TypeError
,
2054 "exceptions must be strings, classes, or instances");
2057 PyErr_Restore(type
, value
, tb
);
2059 return WHY_EXCEPTION
;
2066 return WHY_EXCEPTION
;
2070 unpack_sequence(v
, argcnt
, sp
)
2078 for (i
= 0; i
< argcnt
; i
++) {
2079 if (! (w
= PySequence_GetItem(v
, i
))) {
2080 if (PyErr_ExceptionMatches(PyExc_IndexError
))
2081 PyErr_SetString(PyExc_ValueError
,
2082 "unpack sequence of wrong size");
2087 /* we better get an IndexError now */
2088 if (PySequence_GetItem(v
, i
) == NULL
) {
2089 if (PyErr_ExceptionMatches(PyExc_IndexError
)) {
2093 /* some other exception occurred. fall through to finally */
2096 PyErr_SetString(PyExc_ValueError
,
2097 "unpack sequence of wrong size");
2100 for (; i
> 0; i
--, sp
++)
2114 if (PyObject_Print(v
, stdout
, 0) != 0)
2115 PyErr_Clear(); /* Don't know what else to do */
2121 call_exc_trace(p_trace
, p_newtrace
, f
)
2122 PyObject
**p_trace
, **p_newtrace
;
2125 PyObject
*type
, *value
, *traceback
, *arg
;
2127 PyErr_Fetch(&type
, &value
, &traceback
);
2128 if (value
== NULL
) {
2132 arg
= Py_BuildValue("(OOO)", type
, value
, traceback
);
2134 PyErr_Restore(type
, value
, traceback
);
2137 err
= call_trace(p_trace
, p_newtrace
, f
, "exception", arg
);
2140 PyErr_Restore(type
, value
, traceback
);
2144 Py_XDECREF(traceback
);
2149 call_trace(p_trace
, p_newtrace
, f
, msg
, arg
)
2150 PyObject
**p_trace
; /* in/out; may not be NULL;
2151 may not point to NULL variable initially */
2152 PyObject
**p_newtrace
; /* in/out; may be NULL;
2153 may point to NULL variable;
2154 may be same variable as p_newtrace */
2159 PyThreadState
*tstate
= f
->f_tstate
;
2160 PyObject
*args
, *what
;
2161 PyObject
*res
= NULL
;
2163 if (tstate
->tracing
) {
2164 /* Don't do recursive traces */
2166 Py_XDECREF(*p_newtrace
);
2172 args
= PyTuple_New(3);
2175 what
= PyString_FromString(msg
);
2179 PyTuple_SET_ITEM(args
, 0, (PyObject
*)f
);
2180 PyTuple_SET_ITEM(args
, 1, what
);
2184 PyTuple_SET_ITEM(args
, 2, arg
);
2186 PyFrame_FastToLocals(f
);
2187 res
= PyEval_CallObject(*p_trace
, args
); /* May clear *p_trace! */
2188 PyFrame_LocalsToFast(f
, 1);
2193 /* The trace proc raised an exception */
2194 PyTraceBack_Here(f
);
2195 Py_XDECREF(*p_trace
);
2198 Py_XDECREF(*p_newtrace
);
2205 Py_XDECREF(*p_newtrace
);
2219 PyEval_GetBuiltins()
2221 PyThreadState
*tstate
= PyThreadState_Get();
2222 PyFrameObject
*current_frame
= tstate
->frame
;
2223 if (current_frame
== NULL
)
2224 return tstate
->interp
->builtins
;
2226 return current_frame
->f_builtins
;
2232 PyFrameObject
*current_frame
= PyThreadState_Get()->frame
;
2233 if (current_frame
== NULL
)
2235 PyFrame_FastToLocals(current_frame
);
2236 return current_frame
->f_locals
;
2242 PyFrameObject
*current_frame
= PyThreadState_Get()->frame
;
2243 if (current_frame
== NULL
)
2246 return current_frame
->f_globals
;
2252 PyFrameObject
*current_frame
= PyThreadState_Get()->frame
;
2253 return (PyObject
*)current_frame
;
2257 PyEval_GetRestricted()
2259 PyFrameObject
*current_frame
= PyThreadState_Get()->frame
;
2260 return current_frame
== NULL
? 0 : current_frame
->f_restricted
;
2266 PyObject
*f
= PySys_GetObject("stdout");
2269 if (!PyFile_SoftSpace(f
, 0))
2271 return PyFile_WriteString("\n", f
);
2275 /* External interface to call any callable object.
2276 The arg must be a tuple or NULL. */
2278 #undef PyEval_CallObject
2279 /* for backward compatibility: export this interface */
2282 PyEval_CallObject(func
, arg
)
2286 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
2288 #define PyEval_CallObject(func,arg) \
2289 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
2292 PyEval_CallObjectWithKeywords(func
, arg
, kw
)
2301 arg
= PyTuple_New(0);
2302 else if (!PyTuple_Check(arg
)) {
2303 PyErr_SetString(PyExc_TypeError
,
2304 "argument list must be a tuple");
2310 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
2311 PyErr_SetString(PyExc_TypeError
,
2312 "keyword list must be a dictionary");
2316 if ((call
= func
->ob_type
->tp_call
) != NULL
)
2317 result
= (*call
)(func
, arg
, kw
);
2318 else if (PyMethod_Check(func
) || PyFunction_Check(func
))
2319 result
= call_function(func
, arg
, kw
);
2321 result
= call_builtin(func
, arg
, kw
);
2325 if (result
== NULL
&& !PyErr_Occurred())
2326 PyErr_SetString(PyExc_SystemError
,
2327 "NULL result without error in call_object");
2333 call_builtin(func
, arg
, kw
)
2338 if (PyCFunction_Check(func
)) {
2339 PyCFunction meth
= PyCFunction_GetFunction(func
);
2340 PyObject
*self
= PyCFunction_GetSelf(func
);
2341 int flags
= PyCFunction_GetFlags(func
);
2342 if (!(flags
& METH_VARARGS
)) {
2343 int size
= PyTuple_Size(arg
);
2345 arg
= PyTuple_GET_ITEM(arg
, 0);
2349 if (flags
& METH_KEYWORDS
)
2350 return (*(PyCFunctionWithKeywords
)meth
)(self
, arg
, kw
);
2351 if (kw
!= NULL
&& PyDict_Size(kw
) != 0) {
2352 PyErr_SetString(PyExc_TypeError
,
2353 "this function takes no keyword arguments");
2356 return (*meth
)(self
, arg
);
2358 if (PyClass_Check(func
)) {
2359 return PyInstance_New(func
, arg
, kw
);
2361 if (PyInstance_Check(func
)) {
2362 PyObject
*res
, *call
= PyObject_GetAttrString(func
,"__call__");
2365 PyErr_SetString(PyExc_AttributeError
,
2366 "no __call__ method defined");
2369 res
= PyEval_CallObjectWithKeywords(call
, arg
, kw
);
2373 PyErr_Format(PyExc_TypeError
, "call of non-function (type %s)",
2374 func
->ob_type
->tp_name
);
2379 call_function(func
, arg
, kw
)
2384 PyObject
*class = NULL
; /* == owner */
2390 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
2391 PyErr_BadInternalCall();
2395 if (PyMethod_Check(func
)) {
2396 PyObject
*self
= PyMethod_Self(func
);
2397 class = PyMethod_Class(func
);
2398 func
= PyMethod_Function(func
);
2400 /* Unbound methods must be called with an instance of
2401 the class (or a derived class) as first argument */
2402 if (PyTuple_Size(arg
) >= 1) {
2403 self
= PyTuple_GET_ITEM(arg
, 0);
2405 PyInstance_Check(self
) &&
2406 PyClass_IsSubclass((PyObject
*)
2407 (((PyInstanceObject
*)self
)->in_class
),
2414 PyErr_SetString(PyExc_TypeError
,
2415 "unbound method must be called with class instance 1st argument");
2421 int argcount
= PyTuple_Size(arg
);
2422 PyObject
*newarg
= PyTuple_New(argcount
+ 1);
2427 PyTuple_SET_ITEM(newarg
, 0, self
);
2428 for (i
= 0; i
< argcount
; i
++) {
2429 PyObject
*v
= PyTuple_GET_ITEM(arg
, i
);
2431 PyTuple_SET_ITEM(newarg
, i
+1, v
);
2435 if (!PyFunction_Check(func
)) {
2436 result
= PyEval_CallObjectWithKeywords(func
, arg
, kw
);
2442 if (!PyFunction_Check(func
)) {
2443 PyErr_Format(PyExc_TypeError
,
2444 "call of non-function (type %s)",
2445 func
->ob_type
->tp_name
);
2451 argdefs
= PyFunction_GetDefaults(func
);
2452 if (argdefs
!= NULL
&& PyTuple_Check(argdefs
)) {
2453 d
= &PyTuple_GET_ITEM((PyTupleObject
*)argdefs
, 0);
2454 nd
= PyTuple_Size(argdefs
);
2463 nk
= PyDict_Size(kw
);
2464 k
= PyMem_NEW(PyObject
*, 2*nk
);
2471 while (PyDict_Next(kw
, &pos
, &k
[i
], &k
[i
+1]))
2474 /* XXX This is broken if the caller deletes dict items! */
2481 result
= eval_code2(
2482 (PyCodeObject
*)PyFunction_GetCode(func
),
2483 PyFunction_GetGlobals(func
), (PyObject
*)NULL
,
2484 &PyTuple_GET_ITEM(arg
, 0), PyTuple_Size(arg
),
2495 #define SLICE_ERROR_MSG \
2496 "standard sequence type does not support step size other than one"
2499 loop_subscript(v
, w
)
2502 PySequenceMethods
*sq
= v
->ob_type
->tp_as_sequence
;
2504 if (sq
== NULL
|| sq
->sq_item
== NULL
) {
2505 PyErr_SetString(PyExc_TypeError
, "loop over non-sequence");
2508 i
= PyInt_AsLong(w
);
2509 v
= (*sq
->sq_item
)(v
, i
);
2512 if (PyErr_ExceptionMatches(PyExc_IndexError
))
2524 if (!PyInt_Check(v
)) {
2525 PyErr_SetString(PyExc_TypeError
,
2526 "slice index must be int");
2529 x
= PyInt_AsLong(v
);
2530 /* Truncate -- very long indices are truncated anyway */
2533 else if (x
< -INT_MAX
)
2541 apply_slice(u
, v
, w
) /* return u[v:w] */
2542 PyObject
*u
, *v
, *w
;
2544 int ilow
= 0, ihigh
= INT_MAX
;
2545 if (slice_index(v
, &ilow
) != 0)
2547 if (slice_index(w
, &ihigh
) != 0)
2549 return PySequence_GetSlice(u
, ilow
, ihigh
);
2553 assign_slice(u
, v
, w
, x
) /* u[v:w] = x */
2554 PyObject
*u
, *v
, *w
, *x
;
2556 int ilow
= 0, ihigh
= INT_MAX
;
2557 if (slice_index(v
, &ilow
) != 0)
2559 if (slice_index(w
, &ihigh
) != 0)
2562 return PySequence_DelSlice(u
, ilow
, ihigh
);
2564 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
2568 cmp_outcome(op
, v
, w
)
2570 register PyObject
*v
;
2571 register PyObject
*w
;
2574 register int res
= 0;
2579 if (op
== (int) IS_NOT
)
2584 res
= PySequence_Contains(w
, v
);
2587 if (op
== (int) NOT_IN
)
2591 res
= PyErr_GivenExceptionMatches(v
, w
);
2594 cmp
= PyObject_Compare(v
, w
);
2595 if (cmp
&& PyErr_Occurred())
2598 case LT
: res
= cmp
< 0; break;
2599 case LE
: res
= cmp
<= 0; break;
2600 case EQ
: res
= cmp
== 0; break;
2601 case NE
: res
= cmp
!= 0; break;
2602 case GT
: res
= cmp
> 0; break;
2603 case GE
: res
= cmp
>= 0; break;
2604 /* XXX no default? (res is initialized to 0 though) */
2607 v
= res
? Py_True
: Py_False
;
2613 import_from(locals
, v
, name
)
2619 if (!PyModule_Check(v
)) {
2620 PyErr_SetString(PyExc_TypeError
,
2621 "import-from requires module object");
2624 w
= PyModule_GetDict(v
);
2625 if (PyString_AsString(name
)[0] == '*') {
2627 PyObject
*name
, *value
;
2629 while (PyDict_Next(w
, &pos
, &name
, &value
)) {
2630 if (!PyString_Check(name
) ||
2631 PyString_AsString(name
)[0] == '_')
2634 err
= PyDict_SetItem(locals
, name
, value
);
2642 x
= PyDict_GetItem(w
, name
);
2645 sprintf(buf
, "cannot import name %.230s",
2646 PyString_AsString(name
));
2647 PyErr_SetString(PyExc_ImportError
, buf
);
2651 return PyDict_SetItem(locals
, name
, x
);
2656 build_class(methods
, bases
, name
)
2657 PyObject
*methods
; /* dictionary */
2658 PyObject
*bases
; /* tuple containing classes */
2659 PyObject
*name
; /* string */
2662 if (!PyTuple_Check(bases
)) {
2663 PyErr_SetString(PyExc_SystemError
,
2664 "build_class with non-tuple bases");
2667 if (!PyDict_Check(methods
)) {
2668 PyErr_SetString(PyExc_SystemError
,
2669 "build_class with non-dictionary");
2672 if (!PyString_Check(name
)) {
2673 PyErr_SetString(PyExc_SystemError
,
2674 "build_class witn non-string name");
2677 n
= PyTuple_Size(bases
);
2678 for (i
= 0; i
< n
; i
++) {
2679 PyObject
*base
= PyTuple_GET_ITEM(bases
, i
);
2680 if (!PyClass_Check(base
)) {
2681 /* Call the base's *type*, if it is callable.
2682 This code is a hook for Donald Beaudry's
2683 and Jim Fulton's type extensions. In
2684 unexended Python it will never be triggered
2685 since its types are not callable.
2686 Ditto: call the bases's *class*, if it has
2687 one. This makes the same thing possible
2688 without writing C code. A true meta-object
2690 PyObject
*basetype
= (PyObject
*)base
->ob_type
;
2691 PyObject
*callable
= NULL
;
2692 if (PyCallable_Check(basetype
))
2693 callable
= basetype
;
2695 callable
= PyObject_GetAttrString(
2699 PyObject
*newclass
= NULL
;
2700 args
= Py_BuildValue(
2701 "(OOO)", name
, bases
, methods
);
2703 newclass
= PyEval_CallObject(
2707 if (callable
!= basetype
) {
2708 Py_DECREF(callable
);
2712 PyErr_SetString(PyExc_TypeError
,
2713 "base is not a class object");
2717 return PyClass_New(bases
, methods
, name
);
2721 exec_statement(f
, prog
, globals
, locals
)
2732 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
2733 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
2734 /* Backward compatibility hack */
2735 globals
= PyTuple_GetItem(prog
, 1);
2737 locals
= PyTuple_GetItem(prog
, 2);
2738 prog
= PyTuple_GetItem(prog
, 0);
2740 if (globals
== Py_None
) {
2741 globals
= PyEval_GetGlobals();
2742 if (locals
== Py_None
) {
2743 locals
= PyEval_GetLocals();
2747 else if (locals
== Py_None
)
2749 if (!PyString_Check(prog
) &&
2750 !PyCode_Check(prog
) &&
2751 !PyFile_Check(prog
)) {
2752 PyErr_SetString(PyExc_TypeError
,
2753 "exec 1st arg must be string, code or file object");
2756 if (!PyDict_Check(globals
) || !PyDict_Check(locals
)) {
2757 PyErr_SetString(PyExc_TypeError
,
2758 "exec 2nd/3rd args must be dict or None");
2761 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
2762 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
2763 if (PyCode_Check(prog
)) {
2764 v
= PyEval_EvalCode((PyCodeObject
*) prog
,
2771 if (PyFile_Check(prog
)) {
2772 FILE *fp
= PyFile_AsFile(prog
);
2773 char *name
= PyString_AsString(PyFile_Name(prog
));
2774 if (PyRun_File(fp
, name
, Py_file_input
,
2775 globals
, locals
) == NULL
)
2779 s
= PyString_AsString(prog
);
2780 if ((int)strlen(s
) != PyString_Size(prog
)) {
2781 PyErr_SetString(PyExc_ValueError
,
2782 "embedded '\\0' in exec string");
2785 v
= PyRun_String(s
, Py_file_input
, globals
, locals
);
2790 PyFrame_LocalsToFast(f
, 0);
2794 /* Hack for ni.py */
2796 find_from_args(f
, nexti
)
2802 PyObject
*list
, *name
;
2803 unsigned char *next_instr
;
2805 _PyCode_GETCODEPTR(f
->f_code
, &next_instr
);
2806 next_instr
+= nexti
;
2808 opcode
= (*next_instr
++);
2809 if (opcode
!= IMPORT_FROM
) {
2814 list
= PyList_New(0);
2819 oparg
= (next_instr
[1]<<8) + next_instr
[0];
2821 name
= Getnamev(f
, oparg
);
2822 if (PyList_Append(list
, name
) < 0) {
2826 opcode
= (*next_instr
++);
2827 } while (opcode
== IMPORT_FROM
);
2833 #ifdef DYNAMIC_EXECUTION_PROFILE
2840 PyObject
*l
= PyList_New(256);
2841 if (l
== NULL
) return NULL
;
2842 for (i
= 0; i
< 256; i
++) {
2843 PyObject
*x
= PyInt_FromLong(a
[i
]);
2848 PyList_SetItem(l
, i
, x
);
2850 for (i
= 0; i
< 256; i
++)
2856 _Py_GetDXProfile(self
, args
)
2857 PyObject
*self
, *args
;
2860 return getarray(dxp
);
2863 PyObject
*l
= PyList_New(257);
2864 if (l
== NULL
) return NULL
;
2865 for (i
= 0; i
< 257; i
++) {
2866 PyObject
*x
= getarray(dxpairs
[i
]);
2871 PyList_SetItem(l
, i
, x
);