2 /* Execute compiled code */
5 XXX speed up searching for keywords by using a dictionary
12 #include "frameobject.h"
15 #include "structmember.h"
23 /* Turn this on if your compiler chokes on the big switch: */
24 /* #define CASE_TOO_BIG 1 */
27 /* For debugging the interpreter: */
28 #define LLTRACE 1 /* Low-level trace feature */
29 #define CHECKEXC 1 /* Double-check exception checking */
32 typedef PyObject
*(*callproc
)(PyObject
*, PyObject
*, PyObject
*);
34 /* Forward declarations */
35 static PyObject
*eval_frame(PyFrameObject
*);
36 static PyObject
*fast_function(PyObject
*, PyObject
***, int, int, int);
37 static PyObject
*fast_cfunction(PyObject
*, PyObject
***, int);
38 static PyObject
*do_call(PyObject
*, PyObject
***, int, int);
39 static PyObject
*ext_do_call(PyObject
*, PyObject
***, int, int, int);
40 static PyObject
*update_keyword_args(PyObject
*, int, PyObject
***,PyObject
*);
41 static PyObject
*update_star_args(int, int, PyObject
*, PyObject
***);
42 static PyObject
*load_args(PyObject
***, int);
43 #define CALL_FLAG_VAR 1
44 #define CALL_FLAG_KW 2
47 static int prtrace(PyObject
*, char *);
49 static int call_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*,
51 static void call_trace_protected(Py_tracefunc
, PyObject
*,
52 PyFrameObject
*, int);
53 static void call_exc_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*);
54 static PyObject
*loop_subscript(PyObject
*, PyObject
*);
55 static PyObject
*apply_slice(PyObject
*, PyObject
*, PyObject
*);
56 static int assign_slice(PyObject
*, PyObject
*,
57 PyObject
*, PyObject
*);
58 static PyObject
*cmp_outcome(int, PyObject
*, PyObject
*);
59 static PyObject
*import_from(PyObject
*, PyObject
*);
60 static int import_all_from(PyObject
*, PyObject
*);
61 static PyObject
*build_class(PyObject
*, PyObject
*, PyObject
*);
62 static int exec_statement(PyFrameObject
*,
63 PyObject
*, PyObject
*, PyObject
*);
64 static void set_exc_info(PyThreadState
*, PyObject
*, PyObject
*, PyObject
*);
65 static void reset_exc_info(PyThreadState
*);
66 static void format_exc_check_arg(PyObject
*, char *, PyObject
*);
68 #define NAME_ERROR_MSG \
69 "name '%.200s' is not defined"
70 #define GLOBAL_NAME_ERROR_MSG \
71 "global name '%.200s' is not defined"
72 #define UNBOUNDLOCAL_ERROR_MSG \
73 "local variable '%.200s' referenced before assignment"
74 #define UNBOUNDFREE_ERROR_MSG \
75 "free variable '%.200s' referenced before assignment" \
78 /* Dynamic execution profile */
79 #ifdef DYNAMIC_EXECUTION_PROFILE
81 static long dxpairs
[257][256];
82 #define dxp dxpairs[256]
88 staticforward PyTypeObject gentype
;
92 /* The gi_ prefix is intended to remind of generator-iterator. */
94 PyFrameObject
*gi_frame
;
96 /* True if generator is being executed. */
101 gen_new(PyFrameObject
*f
)
103 genobject
*gen
= PyObject_New(genobject
, &gentype
);
110 PyObject_GC_Init(gen
);
111 return (PyObject
*)gen
;
115 gen_traverse(genobject
*gen
, visitproc visit
, void *arg
)
117 return visit((PyObject
*)gen
->gi_frame
, arg
);
121 gen_dealloc(genobject
*gen
)
123 PyObject_GC_Fini(gen
);
124 Py_DECREF(gen
->gi_frame
);
129 gen_iternext(genobject
*gen
)
131 PyThreadState
*tstate
= PyThreadState_GET();
132 PyFrameObject
*f
= gen
->gi_frame
;
135 if (gen
->gi_running
) {
136 PyErr_SetString(PyExc_ValueError
,
137 "generator already executing");
140 if (f
->f_stacktop
== NULL
)
143 /* Generators always return to their most recent caller, not
144 * necessarily their creator. */
145 Py_XINCREF(tstate
->frame
);
146 assert(f
->f_back
== NULL
);
147 f
->f_back
= tstate
->frame
;
150 result
= eval_frame(f
);
153 /* Don't keep the reference to f_back any longer than necessary. It
154 * may keep a chain of frames alive or it could create a reference
156 Py_XDECREF(f
->f_back
);
159 /* If the generator just returned (as opposed to yielding), signal
160 * that the generator is exhausted. */
161 if (result
== Py_None
&& f
->f_stacktop
== NULL
) {
170 gen_next(genobject
*gen
)
174 result
= gen_iternext(gen
);
176 if (result
== NULL
&& !PyErr_Occurred()) {
177 PyErr_SetObject(PyExc_StopIteration
, Py_None
);
185 gen_getiter(PyObject
*gen
)
191 static struct PyMethodDef gen_methods
[] = {
192 {"next", (PyCFunction
)gen_next
, METH_NOARGS
,
193 "next() -- get the next value, or raise StopIteration"},
194 {NULL
, NULL
} /* Sentinel */
197 static PyMemberDef gen_memberlist
[] = {
198 {"gi_frame", T_OBJECT
, offsetof(genobject
, gi_frame
), RO
},
199 {"gi_running", T_INT
, offsetof(genobject
, gi_running
), RO
},
200 {NULL
} /* Sentinel */
203 statichere PyTypeObject gentype
= {
204 PyObject_HEAD_INIT(&PyType_Type
)
206 "generator", /* tp_name */
207 sizeof(genobject
) + PyGC_HEAD_SIZE
, /* tp_basicsize */
210 (destructor
)gen_dealloc
, /* tp_dealloc */
216 0, /* tp_as_number */
217 0, /* tp_as_sequence */
218 0, /* tp_as_mapping */
222 PyObject_GenericGetAttr
, /* tp_getattro */
224 0, /* tp_as_buffer */
225 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /* tp_flags */
227 (traverseproc
)gen_traverse
, /* tp_traverse */
229 0, /* tp_richcompare */
230 0, /* tp_weaklistoffset */
231 (getiterfunc
)gen_getiter
, /* tp_iter */
232 (iternextfunc
)gen_iternext
, /* tp_iternext */
233 gen_methods
, /* tp_methods */
234 gen_memberlist
, /* tp_members */
243 #ifndef DONT_HAVE_ERRNO_H
246 #include "pythread.h"
248 extern int _PyThread_Started
; /* Flag for Py_Exit */
250 static PyThread_type_lock interpreter_lock
= 0;
251 static long main_thread
= 0;
254 PyEval_InitThreads(void)
256 if (interpreter_lock
)
258 _PyThread_Started
= 1;
259 interpreter_lock
= PyThread_allocate_lock();
260 PyThread_acquire_lock(interpreter_lock
, 1);
261 main_thread
= PyThread_get_thread_ident();
265 PyEval_AcquireLock(void)
267 PyThread_acquire_lock(interpreter_lock
, 1);
271 PyEval_ReleaseLock(void)
273 PyThread_release_lock(interpreter_lock
);
277 PyEval_AcquireThread(PyThreadState
*tstate
)
280 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
281 PyThread_acquire_lock(interpreter_lock
, 1);
282 if (PyThreadState_Swap(tstate
) != NULL
)
284 "PyEval_AcquireThread: non-NULL old thread state");
288 PyEval_ReleaseThread(PyThreadState
*tstate
)
291 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
292 if (PyThreadState_Swap(NULL
) != tstate
)
293 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
294 PyThread_release_lock(interpreter_lock
);
297 /* This function is called from PyOS_AfterFork to ensure that newly
298 created child processes don't hold locks referring to threads which
299 are not running in the child process. (This could also be done using
300 pthread_atfork mechanism, at least for the pthreads implementation.) */
303 PyEval_ReInitThreads(void)
305 if (!interpreter_lock
)
307 /*XXX Can't use PyThread_free_lock here because it does too
308 much error-checking. Doing this cleanly would require
309 adding a new function to each thread_*.h. Instead, just
310 create a new lock and waste a little bit of memory */
311 interpreter_lock
= PyThread_allocate_lock();
312 PyThread_acquire_lock(interpreter_lock
, 1);
313 main_thread
= PyThread_get_thread_ident();
317 /* Functions save_thread and restore_thread are always defined so
318 dynamically loaded modules needn't be compiled separately for use
319 with and without threads: */
322 PyEval_SaveThread(void)
324 PyThreadState
*tstate
= PyThreadState_Swap(NULL
);
326 Py_FatalError("PyEval_SaveThread: NULL tstate");
328 if (interpreter_lock
)
329 PyThread_release_lock(interpreter_lock
);
335 PyEval_RestoreThread(PyThreadState
*tstate
)
338 Py_FatalError("PyEval_RestoreThread: NULL tstate");
340 if (interpreter_lock
) {
342 PyThread_acquire_lock(interpreter_lock
, 1);
346 PyThreadState_Swap(tstate
);
350 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
351 signal handlers or Mac I/O completion routines) can schedule calls
352 to a function to be called synchronously.
353 The synchronous function is called with one void* argument.
354 It should return 0 for success or -1 for failure -- failure should
355 be accompanied by an exception.
357 If registry succeeds, the registry function returns 0; if it fails
358 (e.g. due to too many pending calls) it returns -1 (without setting
359 an exception condition).
361 Note that because registry may occur from within signal handlers,
362 or other asynchronous events, calling malloc() is unsafe!
365 Any thread can schedule pending calls, but only the main thread
369 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
370 There are two possible race conditions:
371 (1) nested asynchronous registry calls;
372 (2) registry calls made while pending calls are being processed.
373 While (1) is very unlikely, (2) is a real possibility.
374 The current code is safe against (2), but not against (1).
375 The safety against (2) is derived from the fact that only one
376 thread (the main thread) ever takes things out of the queue.
378 XXX Darn! With the advent of thread state, we should have an array
379 of pending calls per thread in the thread state! Later...
382 #define NPENDINGCALLS 32
386 } pendingcalls
[NPENDINGCALLS
];
387 static volatile int pendingfirst
= 0;
388 static volatile int pendinglast
= 0;
389 static volatile int things_to_do
= 0;
392 Py_AddPendingCall(int (*func
)(void *), void *arg
)
396 /* XXX Begin critical section */
397 /* XXX If you want this to be safe against nested
398 XXX asynchronous calls, you'll have to work harder! */
403 j
= (i
+ 1) % NPENDINGCALLS
;
404 if (j
== pendingfirst
)
405 return -1; /* Queue full */
406 pendingcalls
[i
].func
= func
;
407 pendingcalls
[i
].arg
= arg
;
409 things_to_do
= 1; /* Signal main loop */
411 /* XXX End critical section */
416 Py_MakePendingCalls(void)
420 if (main_thread
&& PyThread_get_thread_ident() != main_thread
)
432 if (i
== pendinglast
)
433 break; /* Queue empty */
434 func
= pendingcalls
[i
].func
;
435 arg
= pendingcalls
[i
].arg
;
436 pendingfirst
= (i
+ 1) % NPENDINGCALLS
;
439 things_to_do
= 1; /* We're not done yet */
448 /* The interpreter's recursion limit */
450 static int recursion_limit
= 1000;
453 Py_GetRecursionLimit(void)
455 return recursion_limit
;
459 Py_SetRecursionLimit(int new_limit
)
461 recursion_limit
= new_limit
;
464 /* Status code for main loop (reason for stack unwind) */
467 WHY_NOT
, /* No error */
468 WHY_EXCEPTION
, /* Exception occurred */
469 WHY_RERAISE
, /* Exception re-raised by 'finally' */
470 WHY_RETURN
, /* 'return' statement */
471 WHY_BREAK
, /* 'break' statement */
472 WHY_CONTINUE
, /* 'continue' statement */
473 WHY_YIELD
/* 'yield' operator */
476 static enum why_code
do_raise(PyObject
*, PyObject
*, PyObject
*);
477 static int unpack_iterable(PyObject
*, int, PyObject
**);
481 PyEval_EvalCode(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
)
483 return PyEval_EvalCodeEx(co
,
485 (PyObject
**)NULL
, 0,
486 (PyObject
**)NULL
, 0,
487 (PyObject
**)NULL
, 0,
492 /* Interpreter main loop */
495 eval_frame(PyFrameObject
*f
)
500 PyObject
**stack_pointer
; /* Next free slot in value stack */
501 register unsigned char *next_instr
;
502 register int opcode
=0; /* Current opcode */
503 register int oparg
=0; /* Current opcode argument, if any */
504 register enum why_code why
; /* Reason for block stack unwind */
505 register int err
; /* Error status -- nonzero if error */
506 register PyObject
*x
; /* Result object -- NULL if error */
507 register PyObject
*v
; /* Temporary objects popped off stack */
508 register PyObject
*w
;
509 register PyObject
*u
;
510 register PyObject
*t
;
511 register PyObject
*stream
= NULL
; /* for PRINT opcodes */
512 register PyObject
**fastlocals
, **freevars
;
513 PyObject
*retval
= NULL
; /* Return value */
514 PyThreadState
*tstate
= PyThreadState_GET();
516 unsigned char *first_instr
;
520 #if defined(Py_DEBUG) || defined(LLTRACE)
521 /* Make it easier to find out where we are with a debugger */
525 /* Code access macros */
527 #define GETCONST(i) Getconst(f, i)
528 #define GETNAME(i) Getname(f, i)
529 #define GETNAMEV(i) Getnamev(f, i)
530 #define INSTR_OFFSET() (next_instr - first_instr)
531 #define NEXTOP() (*next_instr++)
532 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
533 #define JUMPTO(x) (next_instr = first_instr + (x))
534 #define JUMPBY(x) (next_instr += (x))
536 /* Stack manipulation macros */
538 #define STACK_LEVEL() (stack_pointer - f->f_valuestack)
539 #define EMPTY() (STACK_LEVEL() == 0)
540 #define TOP() (stack_pointer[-1])
541 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
542 #define BASIC_POP() (*--stack_pointer)
545 #define PUSH(v) { (void)(BASIC_PUSH(v), \
546 lltrace && prtrace(TOP(), "push")); \
547 assert(STACK_LEVEL() <= f->f_stacksize); }
548 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
550 #define PUSH(v) BASIC_PUSH(v)
551 #define POP() BASIC_POP()
554 /* Local variable macros */
556 #define GETLOCAL(i) (fastlocals[i])
558 /* The SETLOCAL() macro must not DECREF the local variable in-place and
559 then store the new value; it must copy the old value to a temporary
560 value, then store the new value, and then DECREF the temporary value.
561 This is because it is possible that during the DECREF the frame is
562 accessed by other code (e.g. a __del__ method or gc.collect()) and the
563 variable would be pointing to already-freed memory. */
564 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
565 GETLOCAL(i) = value; \
566 Py_XDECREF(tmp); } while (0)
573 #ifdef USE_STACKCHECK
574 if (tstate
->recursion_depth
%10 == 0 && PyOS_CheckStack()) {
575 PyErr_SetString(PyExc_MemoryError
, "Stack overflow");
581 if (++tstate
->recursion_depth
> recursion_limit
) {
582 --tstate
->recursion_depth
;
583 PyErr_SetString(PyExc_RuntimeError
,
584 "maximum recursion depth exceeded");
585 tstate
->frame
= f
->f_back
;
591 fastlocals
= f
->f_localsplus
;
592 freevars
= f
->f_localsplus
+ f
->f_nlocals
;
593 _PyCode_GETCODEPTR(co
, &first_instr
);
594 next_instr
= first_instr
+ f
->f_lasti
;
595 stack_pointer
= f
->f_stacktop
;
596 assert(stack_pointer
!= NULL
);
597 f
->f_stacktop
= NULL
; /* remains NULL unless yield suspends frame */
599 if (tstate
->use_tracing
) {
600 if (tstate
->c_tracefunc
!= NULL
) {
601 /* tstate->c_tracefunc, if defined, is a
602 function that will be called on *every* entry
603 to a code block. Its return value, if not
604 None, is a function that will be called at
605 the start of each executed line of code.
606 (Actually, the function must return itself
607 in order to continue tracing.) The trace
608 functions are called with three arguments:
609 a pointer to the current frame, a string
610 indicating why the function is called, and
611 an argument which depends on the situation.
612 The global trace function is also called
613 whenever an exception is detected. */
614 if (call_trace(tstate
->c_tracefunc
, tstate
->c_traceobj
,
615 f
, PyTrace_CALL
, Py_None
)) {
616 /* Trace function raised an error */
620 if (tstate
->c_profilefunc
!= NULL
) {
621 /* Similar for c_profilefunc, except it needn't
622 return itself and isn't called for "line" events */
623 if (call_trace(tstate
->c_profilefunc
,
624 tstate
->c_profileobj
,
625 f
, PyTrace_CALL
, Py_None
)) {
626 /* Profile function raised an error */
633 lltrace
= PyDict_GetItemString(f
->f_globals
,"__lltrace__") != NULL
;
635 #if defined(Py_DEBUG) || defined(LLTRACE)
636 filename
= PyString_AsString(co
->co_filename
);
641 x
= Py_None
; /* Not a reference, just anything non-NULL */
645 assert(stack_pointer
>= f
->f_valuestack
); /* else underflow */
646 assert(STACK_LEVEL() <= f
->f_stacksize
); /* else overflow */
647 /* Do periodic things. Doing this every time through
648 the loop would add too much overhead, so we do it
649 only every Nth instruction. We also do it if
650 ``things_to_do'' is set, i.e. when an asynchronous
651 event needs attention (e.g. a signal handler or
652 async I/O handler); see Py_AddPendingCall() and
653 Py_MakePendingCalls() above. */
655 if (things_to_do
|| --tstate
->ticker
< 0) {
656 tstate
->ticker
= tstate
->interp
->checkinterval
;
658 if (Py_MakePendingCalls() < 0) {
663 #if !defined(HAVE_SIGNAL_H) || defined(macintosh)
664 /* If we have true signals, the signal handler
665 will call Py_AddPendingCall() so we don't
666 have to call sigcheck(). On the Mac and
667 DOS, alas, we have to call it. */
668 if (PyErr_CheckSignals()) {
675 if (interpreter_lock
) {
676 /* Give another thread a chance */
678 if (PyThreadState_Swap(NULL
) != tstate
)
679 Py_FatalError("ceval: tstate mix-up");
680 PyThread_release_lock(interpreter_lock
);
682 /* Other threads may run now */
684 PyThread_acquire_lock(interpreter_lock
, 1);
685 if (PyThreadState_Swap(tstate
) != NULL
)
686 Py_FatalError("ceval: orphan tstate");
691 /* Extract opcode and argument */
693 #if defined(Py_DEBUG) || defined(LLTRACE)
694 f
->f_lasti
= INSTR_OFFSET();
701 #ifdef DYNAMIC_EXECUTION_PROFILE
703 dxpairs
[lastopcode
][opcode
]++;
710 /* Instruction tracing */
713 if (HAS_ARG(opcode
)) {
714 printf("%d: %d, %d\n",
715 (int) (INSTR_OFFSET() - 3),
720 (int) (INSTR_OFFSET() - 1), opcode
);
724 /* Main switch on opcode */
729 It is essential that any operation that fails sets either
730 x to NULL, err to nonzero, or why to anything but WHY_NOT,
731 and that no operation that succeeds does this! */
733 /* case STOP_CODE: this is an error! */
841 Py_FatalError("invalid argument to DUP_TOPX"
842 " (bytecode corruption?)");
848 x
= PyNumber_Positive(v
);
851 if (x
!= NULL
) continue;
856 x
= PyNumber_Negative(v
);
859 if (x
!= NULL
) continue;
864 err
= PyObject_IsTrue(v
);
881 x
= PyObject_Repr(v
);
884 if (x
!= NULL
) continue;
889 x
= PyNumber_Invert(v
);
892 if (x
!= NULL
) continue;
898 x
= PyNumber_Power(v
, w
, Py_None
);
902 if (x
!= NULL
) continue;
905 case BINARY_MULTIPLY
:
908 x
= PyNumber_Multiply(v
, w
);
912 if (x
!= NULL
) continue;
919 x
= PyNumber_Divide(v
, w
);
923 if (x
!= NULL
) continue;
926 /* -Qnew is in effect: fall through to
927 BINARY_TRUE_DIVIDE */
928 case BINARY_TRUE_DIVIDE
:
931 x
= PyNumber_TrueDivide(v
, w
);
935 if (x
!= NULL
) continue;
938 case BINARY_FLOOR_DIVIDE
:
941 x
= PyNumber_FloorDivide(v
, w
);
945 if (x
!= NULL
) continue;
951 x
= PyNumber_Remainder(v
, w
);
955 if (x
!= NULL
) continue;
961 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
962 /* INLINE: int + int */
963 register long a
, b
, i
;
964 a
= PyInt_AS_LONG(v
);
965 b
= PyInt_AS_LONG(w
);
967 if ((i
^a
) < 0 && (i
^b
) < 0)
969 x
= PyInt_FromLong(i
);
973 x
= PyNumber_Add(v
, w
);
978 if (x
!= NULL
) continue;
981 case BINARY_SUBTRACT
:
984 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
985 /* INLINE: int - int */
986 register long a
, b
, i
;
987 a
= PyInt_AS_LONG(v
);
988 b
= PyInt_AS_LONG(w
);
990 if ((i
^a
) < 0 && (i
^~b
) < 0)
992 x
= PyInt_FromLong(i
);
996 x
= PyNumber_Subtract(v
, w
);
1001 if (x
!= NULL
) continue;
1007 if (PyList_CheckExact(v
) && PyInt_CheckExact(w
)) {
1008 /* INLINE: list[int] */
1009 long i
= PyInt_AsLong(w
);
1011 i
+= PyList_GET_SIZE(v
);
1013 i
>= PyList_GET_SIZE(v
)) {
1014 PyErr_SetString(PyExc_IndexError
,
1015 "list index out of range");
1019 x
= PyList_GET_ITEM(v
, i
);
1024 x
= PyObject_GetItem(v
, w
);
1028 if (x
!= NULL
) continue;
1034 x
= PyNumber_Lshift(v
, w
);
1038 if (x
!= NULL
) continue;
1044 x
= PyNumber_Rshift(v
, w
);
1048 if (x
!= NULL
) continue;
1054 x
= PyNumber_And(v
, w
);
1058 if (x
!= NULL
) continue;
1064 x
= PyNumber_Xor(v
, w
);
1068 if (x
!= NULL
) continue;
1074 x
= PyNumber_Or(v
, w
);
1078 if (x
!= NULL
) continue;
1084 x
= PyNumber_InPlacePower(v
, w
, Py_None
);
1088 if (x
!= NULL
) continue;
1091 case INPLACE_MULTIPLY
:
1094 x
= PyNumber_InPlaceMultiply(v
, w
);
1098 if (x
!= NULL
) continue;
1101 case INPLACE_DIVIDE
:
1102 if (!_Py_QnewFlag
) {
1105 x
= PyNumber_InPlaceDivide(v
, w
);
1109 if (x
!= NULL
) continue;
1112 /* -Qnew is in effect: fall through to
1113 INPLACE_TRUE_DIVIDE */
1114 case INPLACE_TRUE_DIVIDE
:
1117 x
= PyNumber_InPlaceTrueDivide(v
, w
);
1121 if (x
!= NULL
) continue;
1124 case INPLACE_FLOOR_DIVIDE
:
1127 x
= PyNumber_InPlaceFloorDivide(v
, w
);
1131 if (x
!= NULL
) continue;
1134 case INPLACE_MODULO
:
1137 x
= PyNumber_InPlaceRemainder(v
, w
);
1141 if (x
!= NULL
) continue;
1147 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1148 /* INLINE: int + int */
1149 register long a
, b
, i
;
1150 a
= PyInt_AS_LONG(v
);
1151 b
= PyInt_AS_LONG(w
);
1153 if ((i
^a
) < 0 && (i
^b
) < 0)
1155 x
= PyInt_FromLong(i
);
1159 x
= PyNumber_InPlaceAdd(v
, w
);
1164 if (x
!= NULL
) continue;
1167 case INPLACE_SUBTRACT
:
1170 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1171 /* INLINE: int - int */
1172 register long a
, b
, i
;
1173 a
= PyInt_AS_LONG(v
);
1174 b
= PyInt_AS_LONG(w
);
1176 if ((i
^a
) < 0 && (i
^~b
) < 0)
1178 x
= PyInt_FromLong(i
);
1182 x
= PyNumber_InPlaceSubtract(v
, w
);
1187 if (x
!= NULL
) continue;
1190 case INPLACE_LSHIFT
:
1193 x
= PyNumber_InPlaceLshift(v
, w
);
1197 if (x
!= NULL
) continue;
1200 case INPLACE_RSHIFT
:
1203 x
= PyNumber_InPlaceRshift(v
, w
);
1207 if (x
!= NULL
) continue;
1213 x
= PyNumber_InPlaceAnd(v
, w
);
1217 if (x
!= NULL
) continue;
1223 x
= PyNumber_InPlaceXor(v
, w
);
1227 if (x
!= NULL
) continue;
1233 x
= PyNumber_InPlaceOr(v
, w
);
1237 if (x
!= NULL
) continue;
1244 if ((opcode
-SLICE
) & 2)
1248 if ((opcode
-SLICE
) & 1)
1253 x
= apply_slice(u
, v
, w
);
1258 if (x
!= NULL
) continue;
1265 if ((opcode
-STORE_SLICE
) & 2)
1269 if ((opcode
-STORE_SLICE
) & 1)
1275 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
1280 if (err
== 0) continue;
1283 case DELETE_SLICE
+0:
1284 case DELETE_SLICE
+1:
1285 case DELETE_SLICE
+2:
1286 case DELETE_SLICE
+3:
1287 if ((opcode
-DELETE_SLICE
) & 2)
1291 if ((opcode
-DELETE_SLICE
) & 1)
1296 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
1301 if (err
== 0) continue;
1309 err
= PyObject_SetItem(v
, w
, u
);
1313 if (err
== 0) continue;
1320 err
= PyObject_DelItem(v
, w
);
1323 if (err
== 0) continue;
1328 w
= PySys_GetObject("displayhook");
1330 PyErr_SetString(PyExc_RuntimeError
,
1331 "lost sys.displayhook");
1336 x
= Py_BuildValue("(O)", v
);
1341 w
= PyEval_CallObject(w
, x
);
1352 /* fall through to PRINT_ITEM */
1356 if (stream
== NULL
|| stream
== Py_None
) {
1357 w
= PySys_GetObject("stdout");
1359 PyErr_SetString(PyExc_RuntimeError
,
1364 if (w
!= NULL
&& PyFile_SoftSpace(w
, 1))
1365 err
= PyFile_WriteString(" ", w
);
1367 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1369 /* XXX move into writeobject() ? */
1370 if (PyString_Check(v
)) {
1371 char *s
= PyString_AS_STRING(v
);
1372 int len
= PyString_GET_SIZE(v
);
1374 isspace(Py_CHARMASK(s
[len
-1])) &&
1376 PyFile_SoftSpace(w
, 0);
1378 #ifdef Py_USING_UNICODE
1379 else if (PyUnicode_Check(v
)) {
1380 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(v
);
1381 int len
= PyUnicode_GET_SIZE(v
);
1383 Py_UNICODE_ISSPACE(s
[len
-1]) &&
1385 PyFile_SoftSpace(w
, 0);
1396 case PRINT_NEWLINE_TO
:
1398 /* fall through to PRINT_NEWLINE */
1401 if (stream
== NULL
|| stream
== Py_None
) {
1402 w
= PySys_GetObject("stdout");
1404 PyErr_SetString(PyExc_RuntimeError
,
1408 err
= PyFile_WriteString("\n", w
);
1410 PyFile_SoftSpace(w
, 0);
1418 default: switch (opcode
) {
1425 retval
= PyInt_FromLong(oparg
);
1433 u
= POP(); /* traceback */
1436 v
= POP(); /* value */
1439 w
= POP(); /* exc */
1440 case 0: /* Fallthrough */
1441 why
= do_raise(w
, v
, u
);
1444 PyErr_SetString(PyExc_SystemError
,
1445 "bad RAISE_VARARGS oparg");
1446 why
= WHY_EXCEPTION
;
1452 if ((x
= f
->f_locals
) == NULL
) {
1453 PyErr_SetString(PyExc_SystemError
,
1468 f
->f_stacktop
= stack_pointer
;
1469 f
->f_lasti
= INSTR_OFFSET();
1478 err
= exec_statement(f
, u
, v
, w
);
1486 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1487 while (STACK_LEVEL() > b
->b_level
) {
1496 if (PyInt_Check(v
)) {
1497 why
= (enum why_code
) PyInt_AsLong(v
);
1498 if (why
== WHY_RETURN
||
1500 why
== CONTINUE_LOOP
)
1503 else if (PyString_Check(v
) || PyClass_Check(v
)) {
1506 PyErr_Restore(v
, w
, u
);
1510 else if (v
!= Py_None
) {
1511 PyErr_SetString(PyExc_SystemError
,
1512 "'finally' pops bad exception");
1513 why
= WHY_EXCEPTION
;
1522 x
= build_class(u
, v
, w
);
1530 w
= GETNAMEV(oparg
);
1532 if ((x
= f
->f_locals
) == NULL
) {
1533 PyErr_Format(PyExc_SystemError
,
1534 "no locals found when storing %s",
1538 err
= PyDict_SetItem(x
, w
, v
);
1543 w
= GETNAMEV(oparg
);
1544 if ((x
= f
->f_locals
) == NULL
) {
1545 PyErr_Format(PyExc_SystemError
,
1546 "no locals when deleting %s",
1550 if ((err
= PyDict_DelItem(x
, w
)) != 0)
1551 format_exc_check_arg(PyExc_NameError
,
1555 case UNPACK_SEQUENCE
:
1557 if (PyTuple_Check(v
)) {
1558 if (PyTuple_Size(v
) != oparg
) {
1559 PyErr_SetString(PyExc_ValueError
,
1560 "unpack tuple of wrong size");
1561 why
= WHY_EXCEPTION
;
1564 for (; --oparg
>= 0; ) {
1565 w
= PyTuple_GET_ITEM(v
, oparg
);
1571 else if (PyList_Check(v
)) {
1572 if (PyList_Size(v
) != oparg
) {
1573 PyErr_SetString(PyExc_ValueError
,
1574 "unpack list of wrong size");
1575 why
= WHY_EXCEPTION
;
1578 for (; --oparg
>= 0; ) {
1579 w
= PyList_GET_ITEM(v
, oparg
);
1585 else if (unpack_iterable(v
, oparg
,
1586 stack_pointer
+ oparg
))
1587 stack_pointer
+= oparg
;
1589 if (PyErr_ExceptionMatches(PyExc_TypeError
))
1590 PyErr_SetString(PyExc_TypeError
,
1591 "unpack non-sequence");
1592 why
= WHY_EXCEPTION
;
1598 w
= GETNAMEV(oparg
);
1601 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1607 w
= GETNAMEV(oparg
);
1609 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1615 w
= GETNAMEV(oparg
);
1617 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1622 w
= GETNAMEV(oparg
);
1623 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1624 format_exc_check_arg(
1625 PyExc_NameError
, GLOBAL_NAME_ERROR_MSG
, w
);
1629 x
= GETCONST(oparg
);
1635 w
= GETNAMEV(oparg
);
1636 if ((x
= f
->f_locals
) == NULL
) {
1637 PyErr_Format(PyExc_SystemError
,
1638 "no locals when loading %s",
1642 x
= PyDict_GetItem(x
, w
);
1644 x
= PyDict_GetItem(f
->f_globals
, w
);
1646 x
= PyDict_GetItem(f
->f_builtins
, w
);
1648 format_exc_check_arg(
1660 w
= GETNAMEV(oparg
);
1661 x
= PyDict_GetItem(f
->f_globals
, w
);
1663 x
= PyDict_GetItem(f
->f_builtins
, w
);
1665 format_exc_check_arg(
1667 GLOBAL_NAME_ERROR_MSG
,w
);
1676 x
= GETLOCAL(oparg
);
1678 format_exc_check_arg(
1679 PyExc_UnboundLocalError
,
1680 UNBOUNDLOCAL_ERROR_MSG
,
1681 PyTuple_GetItem(co
->co_varnames
, oparg
)
1687 if (x
!= NULL
) continue;
1696 x
= GETLOCAL(oparg
);
1698 format_exc_check_arg(
1699 PyExc_UnboundLocalError
,
1700 UNBOUNDLOCAL_ERROR_MSG
,
1701 PyTuple_GetItem(co
->co_varnames
, oparg
)
1705 SETLOCAL(oparg
, NULL
);
1709 x
= freevars
[oparg
];
1715 x
= freevars
[oparg
];
1718 if (oparg
< f
->f_ncells
) {
1719 v
= PyTuple_GetItem(co
->co_cellvars
,
1721 format_exc_check_arg(
1722 PyExc_UnboundLocalError
,
1723 UNBOUNDLOCAL_ERROR_MSG
,
1726 v
= PyTuple_GetItem(
1728 oparg
- f
->f_ncells
);
1729 format_exc_check_arg(
1731 UNBOUNDFREE_ERROR_MSG
,
1742 x
= freevars
[oparg
];
1748 x
= PyTuple_New(oparg
);
1750 for (; --oparg
>= 0;) {
1752 PyTuple_SET_ITEM(x
, oparg
, w
);
1760 x
= PyList_New(oparg
);
1762 for (; --oparg
>= 0;) {
1764 PyList_SET_ITEM(x
, oparg
, w
);
1774 if (x
!= NULL
) continue;
1778 w
= GETNAMEV(oparg
);
1780 x
= PyObject_GetAttr(v
, w
);
1783 if (x
!= NULL
) continue;
1789 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1790 /* INLINE: cmp(int, int) */
1793 a
= PyInt_AS_LONG(v
);
1794 b
= PyInt_AS_LONG(w
);
1796 case LT
: res
= a
< b
; break;
1797 case LE
: res
= a
<= b
; break;
1798 case EQ
: res
= a
== b
; break;
1799 case NE
: res
= a
!= b
; break;
1800 case GT
: res
= a
> b
; break;
1801 case GE
: res
= a
>= b
; break;
1802 case IS
: res
= v
== w
; break;
1803 case IS_NOT
: res
= v
!= w
; break;
1804 default: goto slow_compare
;
1806 x
= res
? Py_True
: Py_False
;
1811 x
= cmp_outcome(oparg
, v
, w
);
1816 if (x
!= NULL
) continue;
1820 w
= GETNAMEV(oparg
);
1821 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
1823 PyErr_SetString(PyExc_ImportError
,
1824 "__import__ not found");
1828 w
= Py_BuildValue("(OOOO)",
1831 f
->f_locals
== NULL
?
1832 Py_None
: f
->f_locals
,
1839 x
= PyEval_CallObject(x
, w
);
1842 if (x
!= NULL
) continue;
1847 PyFrame_FastToLocals(f
);
1848 if ((x
= f
->f_locals
) == NULL
) {
1849 PyErr_SetString(PyExc_SystemError
,
1850 "no locals found during 'import *'");
1853 err
= import_all_from(x
, v
);
1854 PyFrame_LocalsToFast(f
, 0);
1856 if (err
== 0) continue;
1860 w
= GETNAMEV(oparg
);
1862 x
= import_from(v
, w
);
1864 if (x
!= NULL
) continue;
1872 err
= PyObject_IsTrue(TOP());
1882 err
= PyObject_IsTrue(TOP());
1898 /* before: [obj]; after [getiter(obj)] */
1900 x
= PyObject_GetIter(v
);
1909 /* before: [iter]; after: [iter, iter()] *or* [] */
1916 if (!PyErr_Occurred()) {
1917 /* iterator ended normally */
1927 On entry: stack contains s, i.
1928 On exit: stack contains s, i+1, s[i];
1929 but if loop exhausted:
1930 s, i are popped, and we jump */
1931 w
= POP(); /* Loop index */
1932 v
= POP(); /* Sequence object */
1933 u
= loop_subscript(v
, w
);
1936 x
= PyInt_FromLong(PyInt_AsLong(w
)+1);
1940 if (x
!= NULL
) continue;
1945 /* A NULL can mean "s exhausted"
1946 but also an error: */
1947 if (PyErr_Occurred())
1948 why
= WHY_EXCEPTION
;
1959 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
1966 printf("--- %s:%d \n", filename
, oparg
);
1968 f
->f_lineno
= oparg
;
1969 if (tstate
->c_tracefunc
== NULL
|| tstate
->tracing
)
1971 /* Trace each line of code reached */
1972 f
->f_lasti
= INSTR_OFFSET();
1973 /* Inline call_trace() for performance: */
1975 tstate
->use_tracing
= 0;
1976 err
= (tstate
->c_tracefunc
)(tstate
->c_traceobj
, f
,
1977 PyTrace_LINE
, Py_None
);
1978 tstate
->use_tracing
= (tstate
->c_tracefunc
1979 || tstate
->c_profilefunc
);
1985 int na
= oparg
& 0xff;
1986 int nk
= (oparg
>>8) & 0xff;
1987 int n
= na
+ 2 * nk
;
1988 PyObject
**pfunc
= stack_pointer
- n
- 1;
1989 PyObject
*func
= *pfunc
;
1990 f
->f_lasti
= INSTR_OFFSET() - 3; /* For tracing */
1992 /* Always dispatch PyCFunction first, because
1993 these are presumed to be the most frequent
1996 if (PyCFunction_Check(func
)) {
1997 int flags
= PyCFunction_GET_FLAGS(func
);
1998 if (nk
!= 0 || (flags
& METH_KEYWORDS
))
1999 x
= do_call(func
, &stack_pointer
,
2001 else if (flags
== METH_VARARGS
) {
2003 callargs
= load_args(&stack_pointer
, na
);
2004 x
= PyCFunction_Call(func
, callargs
, NULL
);
2005 Py_XDECREF(callargs
);
2007 x
= fast_cfunction(func
,
2008 &stack_pointer
, na
);
2010 if (PyMethod_Check(func
)
2011 && PyMethod_GET_SELF(func
) != NULL
) {
2012 /* optimize access to bound methods */
2013 PyObject
*self
= PyMethod_GET_SELF(func
);
2015 func
= PyMethod_GET_FUNCTION(func
);
2023 if (PyFunction_Check(func
)) {
2024 x
= fast_function(func
, &stack_pointer
,
2027 x
= do_call(func
, &stack_pointer
,
2033 while (stack_pointer
> pfunc
) {
2043 case CALL_FUNCTION_VAR
:
2044 case CALL_FUNCTION_KW
:
2045 case CALL_FUNCTION_VAR_KW
:
2047 int na
= oparg
& 0xff;
2048 int nk
= (oparg
>>8) & 0xff;
2049 int flags
= (opcode
- CALL_FUNCTION
) & 3;
2050 int n
= na
+ 2 * nk
;
2051 PyObject
**pfunc
, *func
;
2052 if (flags
& CALL_FLAG_VAR
)
2054 if (flags
& CALL_FLAG_KW
)
2056 pfunc
= stack_pointer
- n
- 1;
2058 f
->f_lasti
= INSTR_OFFSET() - 3; /* For tracing */
2060 if (PyMethod_Check(func
)
2061 && PyMethod_GET_SELF(func
) != NULL
) {
2062 PyObject
*self
= PyMethod_GET_SELF(func
);
2064 func
= PyMethod_GET_FUNCTION(func
);
2072 x
= ext_do_call(func
, &stack_pointer
, flags
, na
, nk
);
2075 while (stack_pointer
> pfunc
) {
2086 v
= POP(); /* code object */
2087 x
= PyFunction_New(v
, f
->f_globals
);
2089 /* XXX Maybe this should be a separate opcode? */
2090 if (x
!= NULL
&& oparg
> 0) {
2091 v
= PyTuple_New(oparg
);
2097 while (--oparg
>= 0) {
2099 PyTuple_SET_ITEM(v
, oparg
, w
);
2101 err
= PyFunction_SetDefaults(x
, v
);
2110 v
= POP(); /* code object */
2111 x
= PyFunction_New(v
, f
->f_globals
);
2112 nfree
= PyCode_GetNumFree((PyCodeObject
*)v
);
2114 /* XXX Maybe this should be a separate opcode? */
2115 if (x
!= NULL
&& nfree
> 0) {
2116 v
= PyTuple_New(nfree
);
2122 while (--nfree
>= 0) {
2124 PyTuple_SET_ITEM(v
, nfree
, w
);
2126 err
= PyFunction_SetClosure(x
, v
);
2129 if (x
!= NULL
&& oparg
> 0) {
2130 v
= PyTuple_New(oparg
);
2136 while (--oparg
>= 0) {
2138 PyTuple_SET_ITEM(v
, oparg
, w
);
2140 err
= PyFunction_SetDefaults(x
, v
);
2154 x
= PySlice_New(u
, v
, w
);
2159 if (x
!= NULL
) continue;
2164 oparg
= oparg
<<16 | NEXTARG();
2165 goto dispatch_opcode
;
2169 "XXX lineno: %d, opcode: %d\n",
2170 f
->f_lineno
, opcode
);
2171 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
2172 why
= WHY_EXCEPTION
;
2183 /* Quickly continue if no error occurred */
2185 if (why
== WHY_NOT
) {
2186 if (err
== 0 && x
!= NULL
) {
2188 /* This check is expensive! */
2189 if (PyErr_Occurred())
2191 "XXX undetected error\n");
2194 continue; /* Normal, fast path */
2196 why
= WHY_EXCEPTION
;
2201 /* Double-check exception status */
2203 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
2204 if (!PyErr_Occurred()) {
2205 PyErr_SetString(PyExc_SystemError
,
2206 "error return without exception set");
2207 why
= WHY_EXCEPTION
;
2212 /* This check is expensive! */
2213 if (PyErr_Occurred()) {
2215 "XXX undetected error (why=%d)\n",
2217 why
= WHY_EXCEPTION
;
2222 /* Log traceback info if this is a real exception */
2224 if (why
== WHY_EXCEPTION
) {
2225 f
->f_lasti
= INSTR_OFFSET() - 1;
2226 if (HAS_ARG(opcode
))
2228 PyTraceBack_Here(f
);
2230 if (tstate
->c_tracefunc
!= NULL
)
2231 call_exc_trace(tstate
->c_tracefunc
,
2232 tstate
->c_traceobj
, f
);
2235 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2237 if (why
== WHY_RERAISE
)
2238 why
= WHY_EXCEPTION
;
2240 /* Unwind stacks if a (pseudo) exception occurred */
2242 while (why
!= WHY_NOT
&& why
!= WHY_YIELD
&& f
->f_iblock
> 0) {
2243 PyTryBlock
*b
= PyFrame_BlockPop(f
);
2245 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_CONTINUE
) {
2246 /* For a continue inside a try block,
2247 don't pop the block for the loop. */
2248 PyFrame_BlockSetup(f
, b
->b_type
, b
->b_handler
,
2251 JUMPTO(PyInt_AS_LONG(retval
));
2256 while (STACK_LEVEL() > b
->b_level
) {
2260 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
2262 JUMPTO(b
->b_handler
);
2265 if (b
->b_type
== SETUP_FINALLY
||
2266 (b
->b_type
== SETUP_EXCEPT
&&
2267 why
== WHY_EXCEPTION
)) {
2268 if (why
== WHY_EXCEPTION
) {
2269 PyObject
*exc
, *val
, *tb
;
2270 PyErr_Fetch(&exc
, &val
, &tb
);
2275 /* Make the raw exception data
2276 available to the handler,
2277 so a program can emulate the
2278 Python main loop. Don't do
2279 this for 'finally'. */
2280 if (b
->b_type
== SETUP_EXCEPT
) {
2281 PyErr_NormalizeException(
2283 set_exc_info(tstate
,
2295 if (why
== WHY_RETURN
||
2296 why
== CONTINUE_LOOP
)
2298 v
= PyInt_FromLong((long)why
);
2302 JUMPTO(b
->b_handler
);
2305 } /* unwind stack */
2307 /* End the loop if we still have an error (or return) */
2314 if (why
!= WHY_YIELD
) {
2315 /* Pop remaining stack entries -- but when yielding */
2322 if (why
!= WHY_RETURN
&& why
!= WHY_YIELD
)
2325 if (tstate
->use_tracing
) {
2326 if (tstate
->c_tracefunc
2327 && (why
== WHY_RETURN
|| why
== WHY_YIELD
)) {
2328 if (call_trace(tstate
->c_tracefunc
,
2329 tstate
->c_traceobj
, f
,
2330 PyTrace_RETURN
, retval
)) {
2333 why
= WHY_EXCEPTION
;
2336 if (tstate
->c_profilefunc
) {
2337 if (why
== WHY_EXCEPTION
)
2338 call_trace_protected(tstate
->c_profilefunc
,
2339 tstate
->c_profileobj
, f
,
2341 else if (call_trace(tstate
->c_profilefunc
,
2342 tstate
->c_profileobj
, f
,
2343 PyTrace_RETURN
, retval
)) {
2346 why
= WHY_EXCEPTION
;
2351 reset_exc_info(tstate
);
2354 --tstate
->recursion_depth
;
2355 tstate
->frame
= f
->f_back
;
2361 PyEval_EvalCodeEx(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
,
2362 PyObject
**args
, int argcount
, PyObject
**kws
, int kwcount
,
2363 PyObject
**defs
, int defcount
, PyObject
*closure
)
2365 register PyFrameObject
*f
;
2366 register PyObject
*retval
= NULL
;
2367 register PyObject
**fastlocals
, **freevars
;
2368 PyThreadState
*tstate
= PyThreadState_GET();
2371 if (globals
== NULL
) {
2372 PyErr_SetString(PyExc_SystemError
,
2373 "PyEval_EvalCodeEx: NULL globals");
2377 f
= PyFrame_New(tstate
, /*back*/
2383 fastlocals
= f
->f_localsplus
;
2384 freevars
= f
->f_localsplus
+ f
->f_nlocals
;
2386 if (co
->co_argcount
> 0 ||
2387 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
2390 PyObject
*kwdict
= NULL
;
2391 if (co
->co_flags
& CO_VARKEYWORDS
) {
2392 kwdict
= PyDict_New();
2395 i
= co
->co_argcount
;
2396 if (co
->co_flags
& CO_VARARGS
)
2398 SETLOCAL(i
, kwdict
);
2400 if (argcount
> co
->co_argcount
) {
2401 if (!(co
->co_flags
& CO_VARARGS
)) {
2402 PyErr_Format(PyExc_TypeError
,
2403 "%.200s() takes %s %d "
2404 "%sargument%s (%d given)",
2405 PyString_AsString(co
->co_name
),
2406 defcount
? "at most" : "exactly",
2408 kwcount
? "non-keyword " : "",
2409 co
->co_argcount
== 1 ? "" : "s",
2413 n
= co
->co_argcount
;
2415 for (i
= 0; i
< n
; i
++) {
2420 if (co
->co_flags
& CO_VARARGS
) {
2421 u
= PyTuple_New(argcount
- n
);
2424 SETLOCAL(co
->co_argcount
, u
);
2425 for (i
= n
; i
< argcount
; i
++) {
2428 PyTuple_SET_ITEM(u
, i
-n
, x
);
2431 for (i
= 0; i
< kwcount
; i
++) {
2432 PyObject
*keyword
= kws
[2*i
];
2433 PyObject
*value
= kws
[2*i
+ 1];
2435 if (keyword
== NULL
|| !PyString_Check(keyword
)) {
2436 PyErr_Format(PyExc_TypeError
,
2437 "%.200s() keywords must be strings",
2438 PyString_AsString(co
->co_name
));
2441 /* XXX slow -- speed up using dictionary? */
2442 for (j
= 0; j
< co
->co_argcount
; j
++) {
2443 PyObject
*nm
= PyTuple_GET_ITEM(
2444 co
->co_varnames
, j
);
2445 int cmp
= PyObject_RichCompareBool(
2446 keyword
, nm
, Py_EQ
);
2452 /* Check errors from Compare */
2453 if (PyErr_Occurred())
2455 if (j
>= co
->co_argcount
) {
2456 if (kwdict
== NULL
) {
2457 PyErr_Format(PyExc_TypeError
,
2458 "%.200s() got an unexpected "
2459 "keyword argument '%.400s'",
2460 PyString_AsString(co
->co_name
),
2461 PyString_AsString(keyword
));
2464 PyDict_SetItem(kwdict
, keyword
, value
);
2467 if (GETLOCAL(j
) != NULL
) {
2468 PyErr_Format(PyExc_TypeError
,
2469 "%.200s() got multiple "
2470 "values for keyword "
2471 "argument '%.400s'",
2472 PyString_AsString(co
->co_name
),
2473 PyString_AsString(keyword
));
2480 if (argcount
< co
->co_argcount
) {
2481 int m
= co
->co_argcount
- defcount
;
2482 for (i
= argcount
; i
< m
; i
++) {
2483 if (GETLOCAL(i
) == NULL
) {
2484 PyErr_Format(PyExc_TypeError
,
2485 "%.200s() takes %s %d "
2486 "%sargument%s (%d given)",
2487 PyString_AsString(co
->co_name
),
2488 ((co
->co_flags
& CO_VARARGS
) ||
2489 defcount
) ? "at least"
2491 m
, kwcount
? "non-keyword " : "",
2492 m
== 1 ? "" : "s", i
);
2500 for (; i
< defcount
; i
++) {
2501 if (GETLOCAL(m
+i
) == NULL
) {
2502 PyObject
*def
= defs
[i
];
2510 if (argcount
> 0 || kwcount
> 0) {
2511 PyErr_Format(PyExc_TypeError
,
2512 "%.200s() takes no arguments (%d given)",
2513 PyString_AsString(co
->co_name
),
2514 argcount
+ kwcount
);
2518 /* Allocate and initialize storage for cell vars, and copy free
2519 vars into frame. This isn't too efficient right now. */
2521 int i
= 0, j
= 0, nargs
, found
;
2522 char *cellname
, *argname
;
2525 nargs
= co
->co_argcount
;
2526 if (co
->co_flags
& CO_VARARGS
)
2528 if (co
->co_flags
& CO_VARKEYWORDS
)
2531 /* Check for cells that shadow args */
2532 for (i
= 0; i
< f
->f_ncells
&& j
< nargs
; ++i
) {
2533 cellname
= PyString_AS_STRING(
2534 PyTuple_GET_ITEM(co
->co_cellvars
, i
));
2537 argname
= PyString_AS_STRING(
2538 PyTuple_GET_ITEM(co
->co_varnames
, j
));
2539 if (strcmp(cellname
, argname
) == 0) {
2540 c
= PyCell_New(GETLOCAL(j
));
2543 GETLOCAL(f
->f_nlocals
+ i
) = c
;
2550 c
= PyCell_New(NULL
);
2553 SETLOCAL(f
->f_nlocals
+ i
, c
);
2556 /* Initialize any that are left */
2557 while (i
< f
->f_ncells
) {
2558 c
= PyCell_New(NULL
);
2561 SETLOCAL(f
->f_nlocals
+ i
, c
);
2565 if (f
->f_nfreevars
) {
2567 for (i
= 0; i
< f
->f_nfreevars
; ++i
) {
2568 PyObject
*o
= PyTuple_GET_ITEM(closure
, i
);
2570 freevars
[f
->f_ncells
+ i
] = o
;
2574 if (co
->co_flags
& CO_GENERATOR
) {
2575 /* Don't need to keep the reference to f_back, it will be set
2576 * when the generator is resumed. */
2577 Py_XDECREF(f
->f_back
);
2580 /* Create a new generator that owns the ready to run frame
2581 * and return that as the value. */
2585 retval
= eval_frame(f
);
2587 fail
: /* Jump here from prelude on failure */
2589 /* decref'ing the frame can cause __del__ methods to get invoked,
2590 which can call back into Python. While we're done with the
2591 current Python frame (f), the associated C stack is still in use,
2592 so recursion_depth must be boosted for the duration.
2594 assert(tstate
!= NULL
);
2595 ++tstate
->recursion_depth
;
2597 --tstate
->recursion_depth
;
2603 set_exc_info(PyThreadState
*tstate
,
2604 PyObject
*type
, PyObject
*value
, PyObject
*tb
)
2606 PyFrameObject
*frame
;
2607 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
2609 frame
= tstate
->frame
;
2610 if (frame
->f_exc_type
== NULL
) {
2611 /* This frame didn't catch an exception before */
2612 /* Save previous exception of this thread in this frame */
2613 if (tstate
->exc_type
== NULL
) {
2615 tstate
->exc_type
= Py_None
;
2617 tmp_type
= frame
->f_exc_type
;
2618 tmp_value
= frame
->f_exc_value
;
2619 tmp_tb
= frame
->f_exc_traceback
;
2620 Py_XINCREF(tstate
->exc_type
);
2621 Py_XINCREF(tstate
->exc_value
);
2622 Py_XINCREF(tstate
->exc_traceback
);
2623 frame
->f_exc_type
= tstate
->exc_type
;
2624 frame
->f_exc_value
= tstate
->exc_value
;
2625 frame
->f_exc_traceback
= tstate
->exc_traceback
;
2626 Py_XDECREF(tmp_type
);
2627 Py_XDECREF(tmp_value
);
2630 /* Set new exception for this thread */
2631 tmp_type
= tstate
->exc_type
;
2632 tmp_value
= tstate
->exc_value
;
2633 tmp_tb
= tstate
->exc_traceback
;
2637 tstate
->exc_type
= type
;
2638 tstate
->exc_value
= value
;
2639 tstate
->exc_traceback
= tb
;
2640 Py_XDECREF(tmp_type
);
2641 Py_XDECREF(tmp_value
);
2643 /* For b/w compatibility */
2644 PySys_SetObject("exc_type", type
);
2645 PySys_SetObject("exc_value", value
);
2646 PySys_SetObject("exc_traceback", tb
);
2650 reset_exc_info(PyThreadState
*tstate
)
2652 PyFrameObject
*frame
;
2653 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
2654 frame
= tstate
->frame
;
2655 if (frame
->f_exc_type
!= NULL
) {
2656 /* This frame caught an exception */
2657 tmp_type
= tstate
->exc_type
;
2658 tmp_value
= tstate
->exc_value
;
2659 tmp_tb
= tstate
->exc_traceback
;
2660 Py_XINCREF(frame
->f_exc_type
);
2661 Py_XINCREF(frame
->f_exc_value
);
2662 Py_XINCREF(frame
->f_exc_traceback
);
2663 tstate
->exc_type
= frame
->f_exc_type
;
2664 tstate
->exc_value
= frame
->f_exc_value
;
2665 tstate
->exc_traceback
= frame
->f_exc_traceback
;
2666 Py_XDECREF(tmp_type
);
2667 Py_XDECREF(tmp_value
);
2669 /* For b/w compatibility */
2670 PySys_SetObject("exc_type", frame
->f_exc_type
);
2671 PySys_SetObject("exc_value", frame
->f_exc_value
);
2672 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
2674 tmp_type
= frame
->f_exc_type
;
2675 tmp_value
= frame
->f_exc_value
;
2676 tmp_tb
= frame
->f_exc_traceback
;
2677 frame
->f_exc_type
= NULL
;
2678 frame
->f_exc_value
= NULL
;
2679 frame
->f_exc_traceback
= NULL
;
2680 Py_XDECREF(tmp_type
);
2681 Py_XDECREF(tmp_value
);
2685 /* Logic for the raise statement (too complicated for inlining).
2686 This *consumes* a reference count to each of its arguments. */
2687 static enum why_code
2688 do_raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
)
2692 PyThreadState
*tstate
= PyThreadState_Get();
2693 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
2694 value
= tstate
->exc_value
;
2695 tb
= tstate
->exc_traceback
;
2701 /* We support the following forms of raise:
2702 raise <class>, <classinstance>
2703 raise <class>, <argument tuple>
2705 raise <class>, <argument>
2706 raise <classinstance>, None
2707 raise <string>, <object>
2708 raise <string>, None
2710 An omitted second argument is the same as None.
2712 In addition, raise <tuple>, <anything> is the same as
2713 raising the tuple's first item (and it better have one!);
2714 this rule is applied recursively.
2716 Finally, an optional third argument can be supplied, which
2717 gives the traceback to be substituted (useful when
2718 re-raising an exception after examining it). */
2720 /* First, check the traceback argument, replacing None with
2722 if (tb
== Py_None
) {
2726 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
2727 PyErr_SetString(PyExc_TypeError
,
2728 "raise: arg 3 must be a traceback or None");
2732 /* Next, replace a missing value with None */
2733 if (value
== NULL
) {
2738 /* Next, repeatedly, replace a tuple exception with its first item */
2739 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
2740 PyObject
*tmp
= type
;
2741 type
= PyTuple_GET_ITEM(type
, 0);
2746 if (PyString_Check(type
))
2749 else if (PyClass_Check(type
))
2750 PyErr_NormalizeException(&type
, &value
, &tb
);
2752 else if (PyInstance_Check(type
)) {
2753 /* Raising an instance. The value should be a dummy. */
2754 if (value
!= Py_None
) {
2755 PyErr_SetString(PyExc_TypeError
,
2756 "instance exception may not have a separate value");
2760 /* Normalize to raise <class>, <instance> */
2763 type
= (PyObject
*) ((PyInstanceObject
*)type
)->in_class
;
2768 /* Not something you can raise. You get an exception
2769 anyway, just not what you specified :-) */
2770 PyErr_Format(PyExc_TypeError
,
2771 "exceptions must be strings, classes, or "
2772 "instances, not %s", type
->ob_type
->tp_name
);
2775 PyErr_Restore(type
, value
, tb
);
2777 return WHY_EXCEPTION
;
2784 return WHY_EXCEPTION
;
2787 /* Iterate v argcnt times and store the results on the stack (via decreasing
2788 sp). Return 1 for success, 0 if error. */
2791 unpack_iterable(PyObject
*v
, int argcnt
, PyObject
**sp
)
2794 PyObject
*it
; /* iter(v) */
2799 it
= PyObject_GetIter(v
);
2803 for (; i
< argcnt
; i
++) {
2804 w
= PyIter_Next(it
);
2806 /* Iterator done, via error or exhaustion. */
2807 if (!PyErr_Occurred()) {
2808 PyErr_Format(PyExc_ValueError
,
2809 "need more than %d value%s to unpack",
2810 i
, i
== 1 ? "" : "s");
2817 /* We better have exhausted the iterator now. */
2818 w
= PyIter_Next(it
);
2820 if (PyErr_Occurred())
2826 PyErr_SetString(PyExc_ValueError
, "too many values to unpack");
2829 for (; i
> 0; i
--, sp
++)
2838 prtrace(PyObject
*v
, char *str
)
2841 if (PyObject_Print(v
, stdout
, 0) != 0)
2842 PyErr_Clear(); /* Don't know what else to do */
2849 call_exc_trace(Py_tracefunc func
, PyObject
*self
, PyFrameObject
*f
)
2851 PyObject
*type
, *value
, *traceback
, *arg
;
2853 PyErr_Fetch(&type
, &value
, &traceback
);
2854 if (value
== NULL
) {
2858 arg
= Py_BuildValue("(OOO)", type
, value
, traceback
);
2860 PyErr_Restore(type
, value
, traceback
);
2863 err
= call_trace(func
, self
, f
, PyTrace_EXCEPTION
, arg
);
2866 PyErr_Restore(type
, value
, traceback
);
2870 Py_XDECREF(traceback
);
2875 call_trace_protected(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
2878 PyObject
*type
, *value
, *traceback
;
2880 PyErr_Fetch(&type
, &value
, &traceback
);
2881 err
= call_trace(func
, obj
, frame
, what
, NULL
);
2883 PyErr_Restore(type
, value
, traceback
);
2887 Py_XDECREF(traceback
);
2892 call_trace(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
2893 int what
, PyObject
*arg
)
2895 register PyThreadState
*tstate
= frame
->f_tstate
;
2897 if (tstate
->tracing
)
2900 tstate
->use_tracing
= 0;
2901 result
= func(obj
, frame
, what
, arg
);
2902 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
2903 || (tstate
->c_profilefunc
!= NULL
));
2909 PyEval_SetProfile(Py_tracefunc func
, PyObject
*arg
)
2911 PyThreadState
*tstate
= PyThreadState_Get();
2912 PyObject
*temp
= tstate
->c_profileobj
;
2914 tstate
->c_profilefunc
= NULL
;
2915 tstate
->c_profileobj
= NULL
;
2916 tstate
->use_tracing
= tstate
->c_tracefunc
!= NULL
;
2918 tstate
->c_profilefunc
= func
;
2919 tstate
->c_profileobj
= arg
;
2920 tstate
->use_tracing
= (func
!= NULL
) || (tstate
->c_tracefunc
!= NULL
);
2924 PyEval_SetTrace(Py_tracefunc func
, PyObject
*arg
)
2926 PyThreadState
*tstate
= PyThreadState_Get();
2927 PyObject
*temp
= tstate
->c_traceobj
;
2929 tstate
->c_tracefunc
= NULL
;
2930 tstate
->c_traceobj
= NULL
;
2931 tstate
->use_tracing
= tstate
->c_profilefunc
!= NULL
;
2933 tstate
->c_tracefunc
= func
;
2934 tstate
->c_traceobj
= arg
;
2935 tstate
->use_tracing
= ((func
!= NULL
)
2936 || (tstate
->c_profilefunc
!= NULL
));
2940 PyEval_GetBuiltins(void)
2942 PyThreadState
*tstate
= PyThreadState_Get();
2943 PyFrameObject
*current_frame
= tstate
->frame
;
2944 if (current_frame
== NULL
)
2945 return tstate
->interp
->builtins
;
2947 return current_frame
->f_builtins
;
2951 PyEval_GetLocals(void)
2953 PyFrameObject
*current_frame
= PyThreadState_Get()->frame
;
2954 if (current_frame
== NULL
)
2956 PyFrame_FastToLocals(current_frame
);
2957 return current_frame
->f_locals
;
2961 PyEval_GetGlobals(void)
2963 PyFrameObject
*current_frame
= PyThreadState_Get()->frame
;
2964 if (current_frame
== NULL
)
2967 return current_frame
->f_globals
;
2971 PyEval_GetFrame(void)
2973 PyFrameObject
*current_frame
= PyThreadState_Get()->frame
;
2974 return (PyObject
*)current_frame
;
2978 PyEval_GetRestricted(void)
2980 PyFrameObject
*current_frame
= PyThreadState_Get()->frame
;
2981 return current_frame
== NULL
? 0 : current_frame
->f_restricted
;
2985 PyEval_MergeCompilerFlags(PyCompilerFlags
*cf
)
2987 PyFrameObject
*current_frame
= PyThreadState_Get()->frame
;
2990 if (current_frame
!= NULL
) {
2991 const int codeflags
= current_frame
->f_code
->co_flags
;
2992 const int compilerflags
= codeflags
& PyCF_MASK
;
2993 if (compilerflags
) {
2995 cf
->cf_flags
|= compilerflags
;
3004 PyObject
*f
= PySys_GetObject("stdout");
3007 if (!PyFile_SoftSpace(f
, 0))
3009 return PyFile_WriteString("\n", f
);
3013 /* External interface to call any callable object.
3014 The arg must be a tuple or NULL. */
3016 #undef PyEval_CallObject
3017 /* for backward compatibility: export this interface */
3020 PyEval_CallObject(PyObject
*func
, PyObject
*arg
)
3022 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
3024 #define PyEval_CallObject(func,arg) \
3025 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3028 PyEval_CallObjectWithKeywords(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
3033 arg
= PyTuple_New(0);
3034 else if (!PyTuple_Check(arg
)) {
3035 PyErr_SetString(PyExc_TypeError
,
3036 "argument list must be a tuple");
3042 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
3043 PyErr_SetString(PyExc_TypeError
,
3044 "keyword list must be a dictionary");
3049 result
= PyObject_Call(func
, arg
, kw
);
3055 PyEval_GetFuncName(PyObject
*func
)
3057 if (PyMethod_Check(func
))
3058 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func
));
3059 else if (PyFunction_Check(func
))
3060 return PyString_AsString(((PyFunctionObject
*)func
)->func_name
);
3061 else if (PyCFunction_Check(func
))
3062 return ((PyCFunctionObject
*)func
)->m_ml
->ml_name
;
3063 else if (PyClass_Check(func
))
3064 return PyString_AsString(((PyClassObject
*)func
)->cl_name
);
3065 else if (PyInstance_Check(func
)) {
3066 return PyString_AsString(
3067 ((PyInstanceObject
*)func
)->in_class
->cl_name
);
3069 return func
->ob_type
->tp_name
;
3074 PyEval_GetFuncDesc(PyObject
*func
)
3076 if (PyMethod_Check(func
))
3078 else if (PyFunction_Check(func
))
3080 else if (PyCFunction_Check(func
))
3082 else if (PyClass_Check(func
))
3083 return " constructor";
3084 else if (PyInstance_Check(func
)) {
3091 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3093 /* The two fast_xxx() functions optimize calls for which no argument
3094 tuple is necessary; the objects are passed directly from the stack.
3095 fast_cfunction() is called for METH_OLDARGS functions.
3096 fast_function() is for functions with no special argument handling.
3100 fast_cfunction(PyObject
*func
, PyObject
***pp_stack
, int na
)
3102 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
3103 PyObject
*self
= PyCFunction_GET_SELF(func
);
3104 int flags
= PyCFunction_GET_FLAGS(func
);
3109 return (*meth
)(self
, NULL
);
3111 PyObject
*arg
= EXT_POP(*pp_stack
);
3112 PyObject
*result
= (*meth
)(self
, arg
);
3116 PyObject
*args
= load_args(pp_stack
, na
);
3117 PyObject
*result
= (*meth
)(self
, args
);
3123 return (*meth
)(self
, NULL
);
3124 PyErr_Format(PyExc_TypeError
,
3125 "%.200s() takes no arguments (%d given)",
3126 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
, na
);
3130 PyObject
*arg
= EXT_POP(*pp_stack
);
3131 PyObject
*result
= (*meth
)(self
, arg
);
3135 PyErr_Format(PyExc_TypeError
,
3136 "%.200s() takes exactly one argument (%d given)",
3137 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
, na
);
3140 fprintf(stderr
, "%.200s() flags = %d\n",
3141 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
, flags
);
3142 PyErr_BadInternalCall();
3148 fast_function(PyObject
*func
, PyObject
***pp_stack
, int n
, int na
, int nk
)
3150 PyObject
*co
= PyFunction_GET_CODE(func
);
3151 PyObject
*globals
= PyFunction_GET_GLOBALS(func
);
3152 PyObject
*argdefs
= PyFunction_GET_DEFAULTS(func
);
3153 PyObject
*closure
= PyFunction_GET_CLOSURE(func
);
3154 PyObject
**d
= NULL
;
3157 if (argdefs
!= NULL
) {
3158 d
= &PyTuple_GET_ITEM(argdefs
, 0);
3159 nd
= ((PyTupleObject
*)argdefs
)->ob_size
;
3161 return PyEval_EvalCodeEx((PyCodeObject
*)co
, globals
,
3162 (PyObject
*)NULL
, (*pp_stack
)-n
, na
,
3163 (*pp_stack
)-2*nk
, nk
, d
, nd
,
3168 update_keyword_args(PyObject
*orig_kwdict
, int nk
, PyObject
***pp_stack
,
3171 PyObject
*kwdict
= NULL
;
3172 if (orig_kwdict
== NULL
)
3173 kwdict
= PyDict_New();
3175 kwdict
= PyDict_Copy(orig_kwdict
);
3176 Py_DECREF(orig_kwdict
);
3182 PyObject
*value
= EXT_POP(*pp_stack
);
3183 PyObject
*key
= EXT_POP(*pp_stack
);
3184 if (PyDict_GetItem(kwdict
, key
) != NULL
) {
3185 PyErr_Format(PyExc_TypeError
,
3186 "%.200s%s got multiple values "
3187 "for keyword argument '%.200s'",
3188 PyEval_GetFuncName(func
),
3189 PyEval_GetFuncDesc(func
),
3190 PyString_AsString(key
));
3196 err
= PyDict_SetItem(kwdict
, key
, value
);
3208 update_star_args(int nstack
, int nstar
, PyObject
*stararg
,
3209 PyObject
***pp_stack
)
3211 PyObject
*callargs
, *w
;
3213 callargs
= PyTuple_New(nstack
+ nstar
);
3214 if (callargs
== NULL
) {
3219 for (i
= 0; i
< nstar
; i
++) {
3220 PyObject
*a
= PyTuple_GET_ITEM(stararg
, i
);
3222 PyTuple_SET_ITEM(callargs
, nstack
+ i
, a
);
3225 while (--nstack
>= 0) {
3226 w
= EXT_POP(*pp_stack
);
3227 PyTuple_SET_ITEM(callargs
, nstack
, w
);
3233 load_args(PyObject
***pp_stack
, int na
)
3235 PyObject
*args
= PyTuple_New(na
);
3241 w
= EXT_POP(*pp_stack
);
3242 PyTuple_SET_ITEM(args
, na
, w
);
3248 do_call(PyObject
*func
, PyObject
***pp_stack
, int na
, int nk
)
3250 PyObject
*callargs
= NULL
;
3251 PyObject
*kwdict
= NULL
;
3252 PyObject
*result
= NULL
;
3255 kwdict
= update_keyword_args(NULL
, nk
, pp_stack
, func
);
3259 callargs
= load_args(pp_stack
, na
);
3260 if (callargs
== NULL
)
3262 result
= PyObject_Call(func
, callargs
, kwdict
);
3264 Py_XDECREF(callargs
);
3270 ext_do_call(PyObject
*func
, PyObject
***pp_stack
, int flags
, int na
, int nk
)
3273 PyObject
*callargs
= NULL
;
3274 PyObject
*stararg
= NULL
;
3275 PyObject
*kwdict
= NULL
;
3276 PyObject
*result
= NULL
;
3278 if (flags
& CALL_FLAG_KW
) {
3279 kwdict
= EXT_POP(*pp_stack
);
3280 if (!(kwdict
&& PyDict_Check(kwdict
))) {
3281 PyErr_Format(PyExc_TypeError
,
3282 "%s%s argument after ** "
3283 "must be a dictionary",
3284 PyEval_GetFuncName(func
),
3285 PyEval_GetFuncDesc(func
));
3289 if (flags
& CALL_FLAG_VAR
) {
3290 stararg
= EXT_POP(*pp_stack
);
3291 if (!PyTuple_Check(stararg
)) {
3293 t
= PySequence_Tuple(stararg
);
3295 if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
3296 PyErr_Format(PyExc_TypeError
,
3297 "%s%s argument after * "
3298 "must be a sequence",
3299 PyEval_GetFuncName(func
),
3300 PyEval_GetFuncDesc(func
));
3307 nstar
= PyTuple_GET_SIZE(stararg
);
3310 kwdict
= update_keyword_args(kwdict
, nk
, pp_stack
, func
);
3314 callargs
= update_star_args(na
, nstar
, stararg
, pp_stack
);
3315 if (callargs
== NULL
)
3317 result
= PyObject_Call(func
, callargs
, kwdict
);
3319 Py_XDECREF(callargs
);
3321 Py_XDECREF(stararg
);
3325 #define SLICE_ERROR_MSG \
3326 "standard sequence type does not support step size other than one"
3329 loop_subscript(PyObject
*v
, PyObject
*w
)
3331 PySequenceMethods
*sq
= v
->ob_type
->tp_as_sequence
;
3333 if (sq
== NULL
|| sq
->sq_item
== NULL
) {
3334 PyErr_SetString(PyExc_TypeError
, "loop over non-sequence");
3337 i
= PyInt_AsLong(w
);
3338 v
= (*sq
->sq_item
)(v
, i
);
3341 if (PyErr_ExceptionMatches(PyExc_IndexError
))
3346 /* Extract a slice index from a PyInt or PyLong, and store in *pi.
3347 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3348 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3350 /* Note: If v is NULL, return success without storing into *pi. This
3351 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3352 called by the SLICE opcode with v and/or w equal to NULL.
3355 _PyEval_SliceIndex(PyObject
*v
, int *pi
)
3359 if (PyInt_Check(v
)) {
3360 x
= PyInt_AsLong(v
);
3361 } else if (PyLong_Check(v
)) {
3362 x
= PyLong_AsLong(v
);
3363 if (x
==-1 && PyErr_Occurred()) {
3364 PyObject
*long_zero
;
3367 if (!PyErr_ExceptionMatches(
3368 PyExc_OverflowError
)) {
3369 /* It's not an overflow error, so just
3374 /* Clear the OverflowError */
3377 /* It's an overflow error, so we need to
3378 check the sign of the long integer,
3379 set the value to INT_MAX or 0, and clear
3382 /* Create a long integer with a value of 0 */
3383 long_zero
= PyLong_FromLong(0L);
3384 if (long_zero
== NULL
)
3388 cmp
= PyObject_RichCompareBool(v
, long_zero
,
3390 Py_DECREF(long_zero
);
3399 PyErr_SetString(PyExc_TypeError
,
3400 "slice indices must be integers");
3403 /* Truncate -- very long indices are truncated anyway */
3406 else if (x
< -INT_MAX
)
3414 #define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3417 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
3419 PyTypeObject
*tp
= u
->ob_type
;
3420 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
3422 if (sq
&& sq
->sq_slice
&& ISINT(v
) && ISINT(w
)) {
3423 int ilow
= 0, ihigh
= INT_MAX
;
3424 if (!_PyEval_SliceIndex(v
, &ilow
))
3426 if (!_PyEval_SliceIndex(w
, &ihigh
))
3428 return PySequence_GetSlice(u
, ilow
, ihigh
);
3431 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
3432 if (slice
!= NULL
) {
3433 PyObject
*res
= PyObject_GetItem(u
, slice
);
3443 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
3446 PyTypeObject
*tp
= u
->ob_type
;
3447 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
3449 if (sq
&& sq
->sq_slice
&& ISINT(v
) && ISINT(w
)) {
3450 int ilow
= 0, ihigh
= INT_MAX
;
3451 if (!_PyEval_SliceIndex(v
, &ilow
))
3453 if (!_PyEval_SliceIndex(w
, &ihigh
))
3456 return PySequence_DelSlice(u
, ilow
, ihigh
);
3458 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
3461 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
3462 if (slice
!= NULL
) {
3465 res
= PyObject_SetItem(u
, slice
, x
);
3467 res
= PyObject_DelItem(u
, slice
);
3477 cmp_outcome(int op
, register PyObject
*v
, register PyObject
*w
)
3484 if (op
== (int) IS_NOT
)
3489 res
= PySequence_Contains(w
, v
);
3492 if (op
== (int) NOT_IN
)
3496 res
= PyErr_GivenExceptionMatches(v
, w
);
3499 return PyObject_RichCompare(v
, w
, op
);
3501 v
= res
? Py_True
: Py_False
;
3507 import_from(PyObject
*v
, PyObject
*name
)
3511 x
= PyObject_GetAttr(v
, name
);
3512 if (x
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
3513 PyErr_Format(PyExc_ImportError
,
3514 "cannot import name %.230s",
3515 PyString_AsString(name
));
3521 import_all_from(PyObject
*locals
, PyObject
*v
)
3523 PyObject
*all
= PyObject_GetAttrString(v
, "__all__");
3524 PyObject
*dict
, *name
, *value
;
3525 int skip_leading_underscores
= 0;
3529 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
3530 return -1; /* Unexpected error */
3532 dict
= PyObject_GetAttrString(v
, "__dict__");
3534 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
3536 PyErr_SetString(PyExc_ImportError
,
3537 "from-import-* object has no __dict__ and no __all__");
3540 all
= PyMapping_Keys(dict
);
3544 skip_leading_underscores
= 1;
3547 for (pos
= 0, err
= 0; ; pos
++) {
3548 name
= PySequence_GetItem(all
, pos
);
3550 if (!PyErr_ExceptionMatches(PyExc_IndexError
))
3556 if (skip_leading_underscores
&&
3557 PyString_Check(name
) &&
3558 PyString_AS_STRING(name
)[0] == '_')
3563 value
= PyObject_GetAttr(v
, name
);
3567 err
= PyDict_SetItem(locals
, name
, value
);
3578 build_class(PyObject
*methods
, PyObject
*bases
, PyObject
*name
)
3580 PyObject
*metaclass
= NULL
, *result
, *base
;
3582 if (PyDict_Check(methods
))
3583 metaclass
= PyDict_GetItemString(methods
, "__metaclass__");
3584 if (metaclass
!= NULL
)
3585 Py_INCREF(metaclass
);
3586 else if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
3587 base
= PyTuple_GET_ITEM(bases
, 0);
3588 metaclass
= PyObject_GetAttrString(base
, "__class__");
3589 if (metaclass
== NULL
) {
3591 metaclass
= (PyObject
*)base
->ob_type
;
3592 Py_INCREF(metaclass
);
3596 PyObject
*g
= PyEval_GetGlobals();
3597 if (g
!= NULL
&& PyDict_Check(g
))
3598 metaclass
= PyDict_GetItemString(g
, "__metaclass__");
3599 if (metaclass
== NULL
)
3600 metaclass
= (PyObject
*) &PyClass_Type
;
3601 Py_INCREF(metaclass
);
3603 result
= PyObject_CallFunction(metaclass
, "OOO", name
, bases
, methods
);
3604 Py_DECREF(metaclass
);
3609 exec_statement(PyFrameObject
*f
, PyObject
*prog
, PyObject
*globals
,
3616 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
3617 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
3618 /* Backward compatibility hack */
3619 globals
= PyTuple_GetItem(prog
, 1);
3621 locals
= PyTuple_GetItem(prog
, 2);
3622 prog
= PyTuple_GetItem(prog
, 0);
3624 if (globals
== Py_None
) {
3625 globals
= PyEval_GetGlobals();
3626 if (locals
== Py_None
) {
3627 locals
= PyEval_GetLocals();
3631 else if (locals
== Py_None
)
3633 if (!PyString_Check(prog
) &&
3634 !PyUnicode_Check(prog
) &&
3635 !PyCode_Check(prog
) &&
3636 !PyFile_Check(prog
)) {
3637 PyErr_SetString(PyExc_TypeError
,
3638 "exec: arg 1 must be a string, file, or code object");
3641 if (!PyDict_Check(globals
)) {
3642 PyErr_SetString(PyExc_TypeError
,
3643 "exec: arg 2 must be a dictionary or None");
3646 if (!PyDict_Check(locals
)) {
3647 PyErr_SetString(PyExc_TypeError
,
3648 "exec: arg 3 must be a dictionary or None");
3651 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
3652 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
3653 if (PyCode_Check(prog
)) {
3654 if (PyCode_GetNumFree((PyCodeObject
*)prog
) > 0) {
3655 PyErr_SetString(PyExc_TypeError
,
3656 "code object passed to exec may not contain free variables");
3659 v
= PyEval_EvalCode((PyCodeObject
*) prog
, globals
, locals
);
3661 else if (PyFile_Check(prog
)) {
3662 FILE *fp
= PyFile_AsFile(prog
);
3663 char *name
= PyString_AsString(PyFile_Name(prog
));
3666 if (PyEval_MergeCompilerFlags(&cf
))
3667 v
= PyRun_FileFlags(fp
, name
, Py_file_input
, globals
,
3670 v
= PyRun_File(fp
, name
, Py_file_input
, globals
,
3676 if (PyString_AsStringAndSize(prog
, &str
, NULL
))
3679 if (PyEval_MergeCompilerFlags(&cf
))
3680 v
= PyRun_StringFlags(str
, Py_file_input
, globals
,
3683 v
= PyRun_String(str
, Py_file_input
, globals
, locals
);
3686 PyFrame_LocalsToFast(f
, 0);
3694 format_exc_check_arg(PyObject
*exc
, char *format_str
, PyObject
*obj
)
3701 obj_str
= PyString_AsString(obj
);
3705 PyErr_Format(exc
, format_str
, obj_str
);
3708 #ifdef DYNAMIC_EXECUTION_PROFILE
3711 getarray(long a
[256])
3714 PyObject
*l
= PyList_New(256);
3715 if (l
== NULL
) return NULL
;
3716 for (i
= 0; i
< 256; i
++) {
3717 PyObject
*x
= PyInt_FromLong(a
[i
]);
3722 PyList_SetItem(l
, i
, x
);
3724 for (i
= 0; i
< 256; i
++)
3730 _Py_GetDXProfile(PyObject
*self
, PyObject
*args
)
3733 return getarray(dxp
);
3736 PyObject
*l
= PyList_New(257);
3737 if (l
== NULL
) return NULL
;
3738 for (i
= 0; i
< 257; i
++) {
3739 PyObject
*x
= getarray(dxpairs
[i
]);
3744 PyList_SetItem(l
, i
, x
);