2 /* Execute compiled code */
5 XXX speed up searching for keywords by using a dictionary
9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE
15 #include "frameobject.h"
18 #include "structmember.h"
24 #define READ_TIMESTAMP(var)
28 typedef unsigned long long uint64
;
30 #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
31 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
35 #define READ_TIMESTAMP(var) ppc_getcounter(&var)
38 ppc_getcounter(uint64
*v
)
40 register unsigned long tbu
, tb
, tbu2
;
43 asm volatile ("mftbu %0" : "=r" (tbu
) );
44 asm volatile ("mftb %0" : "=r" (tb
) );
45 asm volatile ("mftbu %0" : "=r" (tbu2
));
46 if (__builtin_expect(tbu
!= tbu2
, 0)) goto loop
;
48 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v
))[0] = tbu
;
54 #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
56 #define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
61 void dump_tsc(int opcode
, int ticked
, uint64 inst0
, uint64 inst1
,
62 uint64 loop0
, uint64 loop1
, uint64 intr0
, uint64 intr1
)
64 uint64 intr
, inst
, loop
;
65 PyThreadState
*tstate
= PyThreadState_Get();
66 if (!tstate
->interp
->tscdump
)
69 inst
= inst1
- inst0
- intr
;
70 loop
= loop1
- loop0
- intr
;
71 fprintf(stderr
, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode
, ticked
, inst
, loop
);
77 /* Turn this on if your compiler chokes on the big switch: */
78 /* #define CASE_TOO_BIG 1 */
81 /* For debugging the interpreter: */
82 #define LLTRACE 1 /* Low-level trace feature */
83 #define CHECKEXC 1 /* Double-check exception checking */
86 typedef PyObject
*(*callproc
)(PyObject
*, PyObject
*, PyObject
*);
88 /* Forward declarations */
90 static PyObject
* call_function(PyObject
***, int, uint64
*, uint64
*);
92 static PyObject
* call_function(PyObject
***, int);
94 static PyObject
* fast_function(PyObject
*, PyObject
***, int, int, int);
95 static PyObject
* do_call(PyObject
*, PyObject
***, int, int);
96 static PyObject
* ext_do_call(PyObject
*, PyObject
***, int, int, int);
97 static PyObject
* update_keyword_args(PyObject
*, int, PyObject
***,
99 static PyObject
* update_star_args(int, int, PyObject
*, PyObject
***);
100 static PyObject
* load_args(PyObject
***, int);
101 #define CALL_FLAG_VAR 1
102 #define CALL_FLAG_KW 2
106 static int prtrace(PyObject
*, char *);
108 static int call_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*,
110 static int call_trace_protected(Py_tracefunc
, PyObject
*,
111 PyFrameObject
*, int, PyObject
*);
112 static void call_exc_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*);
113 static int maybe_call_line_trace(Py_tracefunc
, PyObject
*,
114 PyFrameObject
*, int *, int *, int *);
116 static PyObject
* apply_slice(PyObject
*, PyObject
*, PyObject
*);
117 static int assign_slice(PyObject
*, PyObject
*,
118 PyObject
*, PyObject
*);
119 static PyObject
* cmp_outcome(int, PyObject
*, PyObject
*);
120 static PyObject
* import_from(PyObject
*, PyObject
*);
121 static int import_all_from(PyObject
*, PyObject
*);
122 static PyObject
* build_class(PyObject
*, PyObject
*, PyObject
*);
123 static int exec_statement(PyFrameObject
*,
124 PyObject
*, PyObject
*, PyObject
*);
125 static void set_exc_info(PyThreadState
*, PyObject
*, PyObject
*, PyObject
*);
126 static void reset_exc_info(PyThreadState
*);
127 static void format_exc_check_arg(PyObject
*, char *, PyObject
*);
128 static PyObject
* string_concatenate(PyObject
*, PyObject
*,
129 PyFrameObject
*, unsigned char *);
130 static PyObject
* kwd_as_string(PyObject
*);
132 #define NAME_ERROR_MSG \
133 "name '%.200s' is not defined"
134 #define GLOBAL_NAME_ERROR_MSG \
135 "global name '%.200s' is not defined"
136 #define UNBOUNDLOCAL_ERROR_MSG \
137 "local variable '%.200s' referenced before assignment"
138 #define UNBOUNDFREE_ERROR_MSG \
139 "free variable '%.200s' referenced before assignment" \
140 " in enclosing scope"
142 /* Dynamic execution profile */
143 #ifdef DYNAMIC_EXECUTION_PROFILE
145 static long dxpairs
[257][256];
146 #define dxp dxpairs[256]
148 static long dxp
[256];
152 /* Function call profile */
155 static int pcall
[PCALL_NUM
];
158 #define PCALL_FUNCTION 1
159 #define PCALL_FAST_FUNCTION 2
160 #define PCALL_FASTER_FUNCTION 3
161 #define PCALL_METHOD 4
162 #define PCALL_BOUND_METHOD 5
163 #define PCALL_CFUNCTION 6
165 #define PCALL_GENERATOR 8
166 #define PCALL_OTHER 9
169 /* Notes about the statistics
173 FAST_FUNCTION means no argument tuple needs to be created.
174 FASTER_FUNCTION means that the fast-path frame setup code is used.
176 If there is a method call where the call can be optimized by changing
177 the argument tuple and calling the function directly, it gets recorded
180 As a result, the relationship among the statistics appears to be
181 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
182 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
183 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
184 PCALL_METHOD > PCALL_BOUND_METHOD
187 #define PCALL(POS) pcall[POS]++
190 PyEval_GetCallStats(PyObject
*self
)
192 return Py_BuildValue("iiiiiiiiiii",
193 pcall
[0], pcall
[1], pcall
[2], pcall
[3],
194 pcall
[4], pcall
[5], pcall
[6], pcall
[7],
195 pcall
[8], pcall
[9], pcall
[10]);
201 PyEval_GetCallStats(PyObject
*self
)
214 #include "pythread.h"
216 static PyThread_type_lock interpreter_lock
= 0; /* This is the GIL */
217 static PyThread_type_lock pending_lock
= 0; /* for pending calls */
218 static long main_thread
= 0;
221 PyEval_ThreadsInitialized(void)
223 return interpreter_lock
!= 0;
227 PyEval_InitThreads(void)
229 if (interpreter_lock
)
231 interpreter_lock
= PyThread_allocate_lock();
232 PyThread_acquire_lock(interpreter_lock
, 1);
233 main_thread
= PyThread_get_thread_ident();
237 PyEval_AcquireLock(void)
239 PyThread_acquire_lock(interpreter_lock
, 1);
243 PyEval_ReleaseLock(void)
245 PyThread_release_lock(interpreter_lock
);
249 PyEval_AcquireThread(PyThreadState
*tstate
)
252 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
253 /* Check someone has called PyEval_InitThreads() to create the lock */
254 assert(interpreter_lock
);
255 PyThread_acquire_lock(interpreter_lock
, 1);
256 if (PyThreadState_Swap(tstate
) != NULL
)
258 "PyEval_AcquireThread: non-NULL old thread state");
262 PyEval_ReleaseThread(PyThreadState
*tstate
)
265 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
266 if (PyThreadState_Swap(NULL
) != tstate
)
267 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
268 PyThread_release_lock(interpreter_lock
);
271 /* This function is called from PyOS_AfterFork to ensure that newly
272 created child processes don't hold locks referring to threads which
273 are not running in the child process. (This could also be done using
274 pthread_atfork mechanism, at least for the pthreads implementation.) */
277 PyEval_ReInitThreads(void)
279 PyObject
*threading
, *result
;
280 PyThreadState
*tstate
;
282 if (!interpreter_lock
)
284 /*XXX Can't use PyThread_free_lock here because it does too
285 much error-checking. Doing this cleanly would require
286 adding a new function to each thread_*.h. Instead, just
287 create a new lock and waste a little bit of memory */
288 interpreter_lock
= PyThread_allocate_lock();
289 pending_lock
= PyThread_allocate_lock();
290 PyThread_acquire_lock(interpreter_lock
, 1);
291 main_thread
= PyThread_get_thread_ident();
293 /* Update the threading module with the new state.
295 tstate
= PyThreadState_GET();
296 threading
= PyMapping_GetItemString(tstate
->interp
->modules
,
298 if (threading
== NULL
) {
299 /* threading not imported */
303 result
= PyObject_CallMethod(threading
, "_after_fork", NULL
);
305 PyErr_WriteUnraisable(threading
);
308 Py_DECREF(threading
);
312 /* Functions save_thread and restore_thread are always defined so
313 dynamically loaded modules needn't be compiled separately for use
314 with and without threads: */
317 PyEval_SaveThread(void)
319 PyThreadState
*tstate
= PyThreadState_Swap(NULL
);
321 Py_FatalError("PyEval_SaveThread: NULL tstate");
323 if (interpreter_lock
)
324 PyThread_release_lock(interpreter_lock
);
330 PyEval_RestoreThread(PyThreadState
*tstate
)
333 Py_FatalError("PyEval_RestoreThread: NULL tstate");
335 if (interpreter_lock
) {
337 PyThread_acquire_lock(interpreter_lock
, 1);
341 PyThreadState_Swap(tstate
);
345 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
346 signal handlers or Mac I/O completion routines) can schedule calls
347 to a function to be called synchronously.
348 The synchronous function is called with one void* argument.
349 It should return 0 for success or -1 for failure -- failure should
350 be accompanied by an exception.
352 If registry succeeds, the registry function returns 0; if it fails
353 (e.g. due to too many pending calls) it returns -1 (without setting
354 an exception condition).
356 Note that because registry may occur from within signal handlers,
357 or other asynchronous events, calling malloc() is unsafe!
360 Any thread can schedule pending calls, but only the main thread
362 There is no facility to schedule calls to a particular thread, but
363 that should be easy to change, should that ever be required. In
364 that case, the static variables here should go into the python
371 /* The WITH_THREAD implementation is thread-safe. It allows
372 scheduling to be made from any thread, and even from an executing
376 #define NPENDINGCALLS 32
380 } pendingcalls
[NPENDINGCALLS
];
381 static int pendingfirst
= 0;
382 static int pendinglast
= 0;
383 static volatile int pendingcalls_to_do
= 1; /* trigger initialization of lock */
384 static char pendingbusy
= 0;
387 Py_AddPendingCall(int (*func
)(void *), void *arg
)
390 PyThread_type_lock lock
= pending_lock
;
392 /* try a few times for the lock. Since this mechanism is used
393 * for signal handling (on the main thread), there is a (slim)
394 * chance that a signal is delivered on the same thread while we
395 * hold the lock during the Py_MakePendingCalls() function.
396 * This avoids a deadlock in that case.
397 * Note that signals can be delivered on any thread. In particular,
398 * on Windows, a SIGINT is delivered on a system-created worker
400 * We also check for lock being NULL, in the unlikely case that
401 * this function is called before any bytecode evaluation takes place.
404 for (i
= 0; i
<100; i
++) {
405 if (PyThread_acquire_lock(lock
, NOWAIT_LOCK
))
413 j
= (i
+ 1) % NPENDINGCALLS
;
414 if (j
== pendingfirst
) {
415 result
= -1; /* Queue full */
417 pendingcalls
[i
].func
= func
;
418 pendingcalls
[i
].arg
= arg
;
421 /* signal main loop */
423 pendingcalls_to_do
= 1;
425 PyThread_release_lock(lock
);
430 Py_MakePendingCalls(void)
436 /* initial allocation of the lock */
437 pending_lock
= PyThread_allocate_lock();
438 if (pending_lock
== NULL
)
442 /* only service pending calls on main thread */
443 if (main_thread
&& PyThread_get_thread_ident() != main_thread
)
445 /* don't perform recursive pending calls */
449 /* perform a bounded number of calls, in case of recursion */
450 for (i
=0; i
<NPENDINGCALLS
; i
++) {
455 /* pop one item off the queue while holding the lock */
456 PyThread_acquire_lock(pending_lock
, WAIT_LOCK
);
458 if (j
== pendinglast
) {
459 func
= NULL
; /* Queue empty */
461 func
= pendingcalls
[j
].func
;
462 arg
= pendingcalls
[j
].arg
;
463 pendingfirst
= (j
+ 1) % NPENDINGCALLS
;
465 pendingcalls_to_do
= pendingfirst
!= pendinglast
;
466 PyThread_release_lock(pending_lock
);
467 /* having released the lock, perform the callback */
478 #else /* if ! defined WITH_THREAD */
481 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
482 This code is used for signal handling in python that isn't built
484 Don't use this implementation when Py_AddPendingCalls() can happen
485 on a different thread!
487 There are two possible race conditions:
488 (1) nested asynchronous calls to Py_AddPendingCall()
489 (2) AddPendingCall() calls made while pending calls are being processed.
491 (1) is very unlikely because typically signal delivery
492 is blocked during signal handling. So it should be impossible.
493 (2) is a real possibility.
494 The current code is safe against (2), but not against (1).
495 The safety against (2) is derived from the fact that only one
496 thread is present, interrupted by signals, and that the critical
497 section is protected with the "busy" variable. On Windows, which
498 delivers SIGINT on a system thread, this does not hold and therefore
499 Windows really shouldn't use this version.
500 The two threads could theoretically wiggle around the "busy" variable.
503 #define NPENDINGCALLS 32
507 } pendingcalls
[NPENDINGCALLS
];
508 static volatile int pendingfirst
= 0;
509 static volatile int pendinglast
= 0;
510 static volatile int pendingcalls_to_do
= 0;
513 Py_AddPendingCall(int (*func
)(void *), void *arg
)
515 static volatile int busy
= 0;
517 /* XXX Begin critical section */
522 j
= (i
+ 1) % NPENDINGCALLS
;
523 if (j
== pendingfirst
) {
525 return -1; /* Queue full */
527 pendingcalls
[i
].func
= func
;
528 pendingcalls
[i
].arg
= arg
;
532 pendingcalls_to_do
= 1; /* Signal main loop */
534 /* XXX End critical section */
539 Py_MakePendingCalls(void)
545 pendingcalls_to_do
= 0;
551 if (i
== pendinglast
)
552 break; /* Queue empty */
553 func
= pendingcalls
[i
].func
;
554 arg
= pendingcalls
[i
].arg
;
555 pendingfirst
= (i
+ 1) % NPENDINGCALLS
;
558 pendingcalls_to_do
= 1; /* We're not done yet */
566 #endif /* WITH_THREAD */
569 /* The interpreter's recursion limit */
571 #ifndef Py_DEFAULT_RECURSION_LIMIT
572 #define Py_DEFAULT_RECURSION_LIMIT 1000
574 static int recursion_limit
= Py_DEFAULT_RECURSION_LIMIT
;
575 int _Py_CheckRecursionLimit
= Py_DEFAULT_RECURSION_LIMIT
;
578 Py_GetRecursionLimit(void)
580 return recursion_limit
;
584 Py_SetRecursionLimit(int new_limit
)
586 recursion_limit
= new_limit
;
587 _Py_CheckRecursionLimit
= recursion_limit
;
590 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
591 if the recursion_depth reaches _Py_CheckRecursionLimit.
592 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
593 to guarantee that _Py_CheckRecursiveCall() is regularly called.
594 Without USE_STACKCHECK, there is no need for this. */
596 _Py_CheckRecursiveCall(char *where
)
598 PyThreadState
*tstate
= PyThreadState_GET();
600 #ifdef USE_STACKCHECK
601 if (PyOS_CheckStack()) {
602 --tstate
->recursion_depth
;
603 PyErr_SetString(PyExc_MemoryError
, "Stack overflow");
607 if (tstate
->recursion_depth
> recursion_limit
) {
608 --tstate
->recursion_depth
;
609 PyErr_Format(PyExc_RuntimeError
,
610 "maximum recursion depth exceeded%s",
614 _Py_CheckRecursionLimit
= recursion_limit
;
618 /* Status code for main loop (reason for stack unwind) */
620 WHY_NOT
= 0x0001, /* No error */
621 WHY_EXCEPTION
= 0x0002, /* Exception occurred */
622 WHY_RERAISE
= 0x0004, /* Exception re-raised by 'finally' */
623 WHY_RETURN
= 0x0008, /* 'return' statement */
624 WHY_BREAK
= 0x0010, /* 'break' statement */
625 WHY_CONTINUE
= 0x0020, /* 'continue' statement */
626 WHY_YIELD
= 0x0040 /* 'yield' operator */
629 static enum why_code
do_raise(PyObject
*, PyObject
*, PyObject
*);
630 static int unpack_iterable(PyObject
*, int, PyObject
**);
632 /* Records whether tracing is on for any thread. Counts the number of
633 threads for which tstate->c_tracefunc is non-NULL, so if the value
634 is 0, we know we don't have to check this thread's c_tracefunc.
635 This speeds up the if statement in PyEval_EvalFrameEx() after
637 static int _Py_TracingPossible
= 0;
639 /* for manipulating the thread switch and periodic "stuff" - used to be
640 per thread, now just a pair o' globals */
641 int _Py_CheckInterval
= 100;
642 volatile int _Py_Ticker
= 0; /* so that we hit a "tick" first thing */
645 PyEval_EvalCode(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
)
647 return PyEval_EvalCodeEx(co
,
649 (PyObject
**)NULL
, 0,
650 (PyObject
**)NULL
, 0,
651 (PyObject
**)NULL
, 0,
656 /* Interpreter main loop */
659 PyEval_EvalFrame(PyFrameObject
*f
) {
660 /* This is for backward compatibility with extension modules that
661 used this API; core interpreter code should call
662 PyEval_EvalFrameEx() */
663 return PyEval_EvalFrameEx(f
, 0);
667 PyEval_EvalFrameEx(PyFrameObject
*f
, int throwflag
)
672 register PyObject
**stack_pointer
; /* Next free slot in value stack */
673 register unsigned char *next_instr
;
674 register int opcode
; /* Current opcode */
675 register int oparg
; /* Current opcode argument, if any */
676 register enum why_code why
; /* Reason for block stack unwind */
677 register int err
; /* Error status -- nonzero if error */
678 register PyObject
*x
; /* Result object -- NULL if error */
679 register PyObject
*v
; /* Temporary objects popped off stack */
680 register PyObject
*w
;
681 register PyObject
*u
;
682 register PyObject
*t
;
683 register PyObject
*stream
= NULL
; /* for PRINT opcodes */
684 register PyObject
**fastlocals
, **freevars
;
685 PyObject
*retval
= NULL
; /* Return value */
686 PyThreadState
*tstate
= PyThreadState_GET();
689 /* when tracing we set things up so that
691 not (instr_lb <= current_bytecode_offset < instr_ub)
693 is true when the line being executed has changed. The
694 initial values are such as to make this false the first
695 time it is tested. */
696 int instr_ub
= -1, instr_lb
= 0, instr_prev
= -1;
698 unsigned char *first_instr
;
701 #if defined(Py_DEBUG) || defined(LLTRACE)
702 /* Make it easier to find out where we are with a debugger */
706 /* Tuple access macros */
709 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
711 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
715 /* Use Pentium timestamp counter to mark certain events:
716 inst0 -- beginning of switch statement for opcode dispatch
717 inst1 -- end of switch statement (may be skipped)
718 loop0 -- the top of the mainloop
719 loop1 -- place where control returns again to top of mainloop
721 intr1 -- beginning of long interruption
722 intr2 -- end of long interruption
724 Many opcodes call out to helper C functions. In some cases, the
725 time in those functions should be counted towards the time for the
726 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
727 calls another Python function; there's no point in charge all the
728 bytecode executed by the called function to the caller.
730 It's hard to make a useful judgement statically. In the presence
731 of operator overloading, it's impossible to tell if a call will
732 execute new Python code or not.
734 It's a case-by-case judgement. I'll use intr1 for the following
740 CALL_FUNCTION (and friends)
743 uint64 inst0
, inst1
, loop0
, loop1
, intr0
= 0, intr1
= 0;
746 READ_TIMESTAMP(inst0
);
747 READ_TIMESTAMP(inst1
);
748 READ_TIMESTAMP(loop0
);
749 READ_TIMESTAMP(loop1
);
751 /* shut up the compiler */
755 /* Code access macros */
757 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
758 #define NEXTOP() (*next_instr++)
759 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
760 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
761 #define JUMPTO(x) (next_instr = first_instr + (x))
762 #define JUMPBY(x) (next_instr += (x))
764 /* OpCode prediction macros
765 Some opcodes tend to come in pairs thus making it possible to
766 predict the second code when the first is run. For example,
767 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
768 those opcodes are often followed by a POP_TOP.
770 Verifying the prediction costs a single high-speed test of a register
771 variable against a constant. If the pairing was good, then the
772 processor's own internal branch predication has a high likelihood of
773 success, resulting in a nearly zero-overhead transition to the
774 next opcode. A successful prediction saves a trip through the eval-loop
775 including its two unpredictable branches, the HAS_ARG test and the
776 switch-case. Combined with the processor's internal branch prediction,
777 a successful PREDICT has the effect of making the two opcodes run as if
778 they were a single new opcode with the bodies combined.
780 If collecting opcode statistics, your choices are to either keep the
781 predictions turned-on and interpret the results as if some opcodes
782 had been combined or turn-off predictions so that the opcode frequency
783 counter updates for both opcodes.
786 #ifdef DYNAMIC_EXECUTION_PROFILE
787 #define PREDICT(op) if (0) goto PRED_##op
789 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
792 #define PREDICTED(op) PRED_##op: next_instr++
793 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
795 /* Stack manipulation macros */
797 /* The stack can grow at most MAXINT deep, as co_nlocals and
798 co_stacksize are ints. */
799 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
800 #define EMPTY() (STACK_LEVEL() == 0)
801 #define TOP() (stack_pointer[-1])
802 #define SECOND() (stack_pointer[-2])
803 #define THIRD() (stack_pointer[-3])
804 #define FOURTH() (stack_pointer[-4])
805 #define SET_TOP(v) (stack_pointer[-1] = (v))
806 #define SET_SECOND(v) (stack_pointer[-2] = (v))
807 #define SET_THIRD(v) (stack_pointer[-3] = (v))
808 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
809 #define BASIC_STACKADJ(n) (stack_pointer += n)
810 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
811 #define BASIC_POP() (*--stack_pointer)
814 #define PUSH(v) { (void)(BASIC_PUSH(v), \
815 lltrace && prtrace(TOP(), "push")); \
816 assert(STACK_LEVEL() <= co->co_stacksize); }
817 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
819 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
820 lltrace && prtrace(TOP(), "stackadj")); \
821 assert(STACK_LEVEL() <= co->co_stacksize); }
822 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
823 prtrace((STACK_POINTER)[-1], "ext_pop")), \
826 #define PUSH(v) BASIC_PUSH(v)
827 #define POP() BASIC_POP()
828 #define STACKADJ(n) BASIC_STACKADJ(n)
829 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
832 /* Local variable macros */
834 #define GETLOCAL(i) (fastlocals[i])
836 /* The SETLOCAL() macro must not DECREF the local variable in-place and
837 then store the new value; it must copy the old value to a temporary
838 value, then store the new value, and then DECREF the temporary value.
839 This is because it is possible that during the DECREF the frame is
840 accessed by other code (e.g. a __del__ method or gc.collect()) and the
841 variable would be pointing to already-freed memory. */
842 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
843 GETLOCAL(i) = value; \
844 Py_XDECREF(tmp); } while (0)
852 if (Py_EnterRecursiveCall(""))
857 if (tstate
->use_tracing
) {
858 if (tstate
->c_tracefunc
!= NULL
) {
859 /* tstate->c_tracefunc, if defined, is a
860 function that will be called on *every* entry
861 to a code block. Its return value, if not
862 None, is a function that will be called at
863 the start of each executed line of code.
864 (Actually, the function must return itself
865 in order to continue tracing.) The trace
866 functions are called with three arguments:
867 a pointer to the current frame, a string
868 indicating why the function is called, and
869 an argument which depends on the situation.
870 The global trace function is also called
871 whenever an exception is detected. */
872 if (call_trace_protected(tstate
->c_tracefunc
,
874 f
, PyTrace_CALL
, Py_None
)) {
875 /* Trace function raised an error */
876 goto exit_eval_frame
;
879 if (tstate
->c_profilefunc
!= NULL
) {
880 /* Similar for c_profilefunc, except it needn't
881 return itself and isn't called for "line" events */
882 if (call_trace_protected(tstate
->c_profilefunc
,
883 tstate
->c_profileobj
,
884 f
, PyTrace_CALL
, Py_None
)) {
885 /* Profile function raised an error */
886 goto exit_eval_frame
;
892 names
= co
->co_names
;
893 consts
= co
->co_consts
;
894 fastlocals
= f
->f_localsplus
;
895 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
896 first_instr
= (unsigned char*) PyString_AS_STRING(co
->co_code
);
897 /* An explanation is in order for the next line.
899 f->f_lasti now refers to the index of the last instruction
900 executed. You might think this was obvious from the name, but
901 this wasn't always true before 2.3! PyFrame_New now sets
902 f->f_lasti to -1 (i.e. the index *before* the first instruction)
903 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
906 When the PREDICT() macros are enabled, some opcode pairs follow in
907 direct succession without updating f->f_lasti. A successful
908 prediction effectively links the two codes together as if they
909 were a single new opcode; accordingly,f->f_lasti will point to
910 the first code in the pair (for instance, GET_ITER followed by
911 FOR_ITER is effectively a single opcode and f->f_lasti will point
912 at to the beginning of the combined pair.)
914 next_instr
= first_instr
+ f
->f_lasti
+ 1;
915 stack_pointer
= f
->f_stacktop
;
916 assert(stack_pointer
!= NULL
);
917 f
->f_stacktop
= NULL
; /* remains NULL unless yield suspends frame */
920 lltrace
= PyDict_GetItemString(f
->f_globals
, "__lltrace__") != NULL
;
922 #if defined(Py_DEBUG) || defined(LLTRACE)
923 filename
= PyString_AsString(co
->co_filename
);
928 x
= Py_None
; /* Not a reference, just anything non-NULL */
931 if (throwflag
) { /* support for generator.throw() */
939 /* Almost surely, the opcode executed a break
940 or a continue, preventing inst1 from being set
941 on the way out of the loop.
943 READ_TIMESTAMP(inst1
);
946 dump_tsc(opcode
, ticked
, inst0
, inst1
, loop0
, loop1
,
952 READ_TIMESTAMP(loop0
);
954 assert(stack_pointer
>= f
->f_valuestack
); /* else underflow */
955 assert(STACK_LEVEL() <= co
->co_stacksize
); /* else overflow */
957 /* Do periodic things. Doing this every time through
958 the loop would add too much overhead, so we do it
959 only every Nth instruction. We also do it if
960 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
961 event needs attention (e.g. a signal handler or
962 async I/O handler); see Py_AddPendingCall() and
963 Py_MakePendingCalls() above. */
965 if (--_Py_Ticker
< 0) {
966 if (*next_instr
== SETUP_FINALLY
) {
967 /* Make the last opcode before
968 a try: finally: block uninterruptable. */
969 goto fast_next_opcode
;
971 _Py_Ticker
= _Py_CheckInterval
;
972 tstate
->tick_counter
++;
976 if (pendingcalls_to_do
) {
977 if (Py_MakePendingCalls() < 0) {
981 if (pendingcalls_to_do
)
982 /* MakePendingCalls() didn't succeed.
983 Force early re-execution of this
984 "periodic" code, possibly after
989 if (interpreter_lock
) {
990 /* Give another thread a chance */
992 if (PyThreadState_Swap(NULL
) != tstate
)
993 Py_FatalError("ceval: tstate mix-up");
994 PyThread_release_lock(interpreter_lock
);
996 /* Other threads may run now */
998 PyThread_acquire_lock(interpreter_lock
, 1);
999 if (PyThreadState_Swap(tstate
) != NULL
)
1000 Py_FatalError("ceval: orphan tstate");
1002 /* Check for thread interrupts */
1004 if (tstate
->async_exc
!= NULL
) {
1005 x
= tstate
->async_exc
;
1006 tstate
->async_exc
= NULL
;
1009 why
= WHY_EXCEPTION
;
1017 f
->f_lasti
= INSTR_OFFSET();
1019 /* line-by-line tracing support */
1021 if (_Py_TracingPossible
&&
1022 tstate
->c_tracefunc
!= NULL
&& !tstate
->tracing
) {
1023 /* see maybe_call_line_trace
1024 for expository comments */
1025 f
->f_stacktop
= stack_pointer
;
1027 err
= maybe_call_line_trace(tstate
->c_tracefunc
,
1029 f
, &instr_lb
, &instr_ub
,
1031 /* Reload possibly changed frame fields */
1033 if (f
->f_stacktop
!= NULL
) {
1034 stack_pointer
= f
->f_stacktop
;
1035 f
->f_stacktop
= NULL
;
1038 /* trace function raised an exception */
1043 /* Extract opcode and argument */
1046 oparg
= 0; /* allows oparg to be stored in a register because
1047 it doesn't have to be remembered across a full loop */
1048 if (HAS_ARG(opcode
))
1051 #ifdef DYNAMIC_EXECUTION_PROFILE
1053 dxpairs
[lastopcode
][opcode
]++;
1054 lastopcode
= opcode
;
1060 /* Instruction tracing */
1063 if (HAS_ARG(opcode
)) {
1064 printf("%d: %d, %d\n",
1065 f
->f_lasti
, opcode
, oparg
);
1069 f
->f_lasti
, opcode
);
1074 /* Main switch on opcode */
1075 READ_TIMESTAMP(inst0
);
1080 It is essential that any operation that fails sets either
1081 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1082 and that no operation that succeeds does this! */
1084 /* case STOP_CODE: this is an error! */
1087 goto fast_next_opcode
;
1090 x
= GETLOCAL(oparg
);
1094 goto fast_next_opcode
;
1096 format_exc_check_arg(PyExc_UnboundLocalError
,
1097 UNBOUNDLOCAL_ERROR_MSG
,
1098 PyTuple_GetItem(co
->co_varnames
, oparg
));
1102 x
= GETITEM(consts
, oparg
);
1105 goto fast_next_opcode
;
1107 PREDICTED_WITH_ARG(STORE_FAST
);
1111 goto fast_next_opcode
;
1117 goto fast_next_opcode
;
1124 goto fast_next_opcode
;
1133 goto fast_next_opcode
;
1144 goto fast_next_opcode
;
1150 goto fast_next_opcode
;
1161 goto fast_next_opcode
;
1162 } else if (oparg
== 3) {
1173 goto fast_next_opcode
;
1175 Py_FatalError("invalid argument to DUP_TOPX"
1176 " (bytecode corruption?)");
1177 /* Never returns, so don't bother to set why. */
1180 case UNARY_POSITIVE
:
1182 x
= PyNumber_Positive(v
);
1185 if (x
!= NULL
) continue;
1188 case UNARY_NEGATIVE
:
1190 x
= PyNumber_Negative(v
);
1193 if (x
!= NULL
) continue;
1198 err
= PyObject_IsTrue(v
);
1206 Py_INCREF(Py_False
);
1216 x
= PyObject_Repr(v
);
1219 if (x
!= NULL
) continue;
1224 x
= PyNumber_Invert(v
);
1227 if (x
!= NULL
) continue;
1233 x
= PyNumber_Power(v
, w
, Py_None
);
1237 if (x
!= NULL
) continue;
1240 case BINARY_MULTIPLY
:
1243 x
= PyNumber_Multiply(v
, w
);
1247 if (x
!= NULL
) continue;
1251 if (!_Py_QnewFlag
) {
1254 x
= PyNumber_Divide(v
, w
);
1258 if (x
!= NULL
) continue;
1261 /* -Qnew is in effect: fall through to
1262 BINARY_TRUE_DIVIDE */
1263 case BINARY_TRUE_DIVIDE
:
1266 x
= PyNumber_TrueDivide(v
, w
);
1270 if (x
!= NULL
) continue;
1273 case BINARY_FLOOR_DIVIDE
:
1276 x
= PyNumber_FloorDivide(v
, w
);
1280 if (x
!= NULL
) continue;
1286 if (PyString_CheckExact(v
))
1287 x
= PyString_Format(v
, w
);
1289 x
= PyNumber_Remainder(v
, w
);
1293 if (x
!= NULL
) continue;
1299 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1300 /* INLINE: int + int */
1301 register long a
, b
, i
;
1302 a
= PyInt_AS_LONG(v
);
1303 b
= PyInt_AS_LONG(w
);
1305 if ((i
^a
) < 0 && (i
^b
) < 0)
1307 x
= PyInt_FromLong(i
);
1309 else if (PyString_CheckExact(v
) &&
1310 PyString_CheckExact(w
)) {
1311 x
= string_concatenate(v
, w
, f
, next_instr
);
1312 /* string_concatenate consumed the ref to v */
1313 goto skip_decref_vx
;
1317 x
= PyNumber_Add(v
, w
);
1323 if (x
!= NULL
) continue;
1326 case BINARY_SUBTRACT
:
1329 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1330 /* INLINE: int - int */
1331 register long a
, b
, i
;
1332 a
= PyInt_AS_LONG(v
);
1333 b
= PyInt_AS_LONG(w
);
1335 if ((i
^a
) < 0 && (i
^~b
) < 0)
1337 x
= PyInt_FromLong(i
);
1341 x
= PyNumber_Subtract(v
, w
);
1346 if (x
!= NULL
) continue;
1352 if (PyList_CheckExact(v
) && PyInt_CheckExact(w
)) {
1353 /* INLINE: list[int] */
1354 Py_ssize_t i
= PyInt_AsSsize_t(w
);
1356 i
+= PyList_GET_SIZE(v
);
1357 if (i
>= 0 && i
< PyList_GET_SIZE(v
)) {
1358 x
= PyList_GET_ITEM(v
, i
);
1366 x
= PyObject_GetItem(v
, w
);
1370 if (x
!= NULL
) continue;
1376 x
= PyNumber_Lshift(v
, w
);
1380 if (x
!= NULL
) continue;
1386 x
= PyNumber_Rshift(v
, w
);
1390 if (x
!= NULL
) continue;
1396 x
= PyNumber_And(v
, w
);
1400 if (x
!= NULL
) continue;
1406 x
= PyNumber_Xor(v
, w
);
1410 if (x
!= NULL
) continue;
1416 x
= PyNumber_Or(v
, w
);
1420 if (x
!= NULL
) continue;
1425 v
= stack_pointer
[-oparg
];
1426 err
= PyList_Append(v
, w
);
1429 PREDICT(JUMP_ABSOLUTE
);
1437 x
= PyNumber_InPlacePower(v
, w
, Py_None
);
1441 if (x
!= NULL
) continue;
1444 case INPLACE_MULTIPLY
:
1447 x
= PyNumber_InPlaceMultiply(v
, w
);
1451 if (x
!= NULL
) continue;
1454 case INPLACE_DIVIDE
:
1455 if (!_Py_QnewFlag
) {
1458 x
= PyNumber_InPlaceDivide(v
, w
);
1462 if (x
!= NULL
) continue;
1465 /* -Qnew is in effect: fall through to
1466 INPLACE_TRUE_DIVIDE */
1467 case INPLACE_TRUE_DIVIDE
:
1470 x
= PyNumber_InPlaceTrueDivide(v
, w
);
1474 if (x
!= NULL
) continue;
1477 case INPLACE_FLOOR_DIVIDE
:
1480 x
= PyNumber_InPlaceFloorDivide(v
, w
);
1484 if (x
!= NULL
) continue;
1487 case INPLACE_MODULO
:
1490 x
= PyNumber_InPlaceRemainder(v
, w
);
1494 if (x
!= NULL
) continue;
1500 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1501 /* INLINE: int + int */
1502 register long a
, b
, i
;
1503 a
= PyInt_AS_LONG(v
);
1504 b
= PyInt_AS_LONG(w
);
1506 if ((i
^a
) < 0 && (i
^b
) < 0)
1508 x
= PyInt_FromLong(i
);
1510 else if (PyString_CheckExact(v
) &&
1511 PyString_CheckExact(w
)) {
1512 x
= string_concatenate(v
, w
, f
, next_instr
);
1513 /* string_concatenate consumed the ref to v */
1518 x
= PyNumber_InPlaceAdd(v
, w
);
1524 if (x
!= NULL
) continue;
1527 case INPLACE_SUBTRACT
:
1530 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1531 /* INLINE: int - int */
1532 register long a
, b
, i
;
1533 a
= PyInt_AS_LONG(v
);
1534 b
= PyInt_AS_LONG(w
);
1536 if ((i
^a
) < 0 && (i
^~b
) < 0)
1538 x
= PyInt_FromLong(i
);
1542 x
= PyNumber_InPlaceSubtract(v
, w
);
1547 if (x
!= NULL
) continue;
1550 case INPLACE_LSHIFT
:
1553 x
= PyNumber_InPlaceLshift(v
, w
);
1557 if (x
!= NULL
) continue;
1560 case INPLACE_RSHIFT
:
1563 x
= PyNumber_InPlaceRshift(v
, w
);
1567 if (x
!= NULL
) continue;
1573 x
= PyNumber_InPlaceAnd(v
, w
);
1577 if (x
!= NULL
) continue;
1583 x
= PyNumber_InPlaceXor(v
, w
);
1587 if (x
!= NULL
) continue;
1593 x
= PyNumber_InPlaceOr(v
, w
);
1597 if (x
!= NULL
) continue;
1604 if ((opcode
-SLICE
) & 2)
1608 if ((opcode
-SLICE
) & 1)
1613 x
= apply_slice(u
, v
, w
);
1618 if (x
!= NULL
) continue;
1625 if ((opcode
-STORE_SLICE
) & 2)
1629 if ((opcode
-STORE_SLICE
) & 1)
1635 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
1640 if (err
== 0) continue;
1643 case DELETE_SLICE
+0:
1644 case DELETE_SLICE
+1:
1645 case DELETE_SLICE
+2:
1646 case DELETE_SLICE
+3:
1647 if ((opcode
-DELETE_SLICE
) & 2)
1651 if ((opcode
-DELETE_SLICE
) & 1)
1656 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
1661 if (err
== 0) continue;
1670 err
= PyObject_SetItem(v
, w
, u
);
1674 if (err
== 0) continue;
1682 err
= PyObject_DelItem(v
, w
);
1685 if (err
== 0) continue;
1690 w
= PySys_GetObject("displayhook");
1692 PyErr_SetString(PyExc_RuntimeError
,
1693 "lost sys.displayhook");
1698 x
= PyTuple_Pack(1, v
);
1703 w
= PyEval_CallObject(w
, x
);
1714 /* fall through to PRINT_ITEM */
1718 if (stream
== NULL
|| stream
== Py_None
) {
1719 w
= PySys_GetObject("stdout");
1721 PyErr_SetString(PyExc_RuntimeError
,
1726 /* PyFile_SoftSpace() can exececute arbitrary code
1727 if sys.stdout is an instance with a __getattr__.
1728 If __getattr__ raises an exception, w will
1729 be freed, so we need to prevent that temporarily. */
1731 if (w
!= NULL
&& PyFile_SoftSpace(w
, 0))
1732 err
= PyFile_WriteString(" ", w
);
1734 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1736 /* XXX move into writeobject() ? */
1737 if (PyString_Check(v
)) {
1738 char *s
= PyString_AS_STRING(v
);
1739 Py_ssize_t len
= PyString_GET_SIZE(v
);
1741 !isspace(Py_CHARMASK(s
[len
-1])) ||
1743 PyFile_SoftSpace(w
, 1);
1745 #ifdef Py_USING_UNICODE
1746 else if (PyUnicode_Check(v
)) {
1747 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(v
);
1748 Py_ssize_t len
= PyUnicode_GET_SIZE(v
);
1750 !Py_UNICODE_ISSPACE(s
[len
-1]) ||
1752 PyFile_SoftSpace(w
, 1);
1756 PyFile_SoftSpace(w
, 1);
1766 case PRINT_NEWLINE_TO
:
1768 /* fall through to PRINT_NEWLINE */
1771 if (stream
== NULL
|| stream
== Py_None
) {
1772 w
= PySys_GetObject("stdout");
1774 PyErr_SetString(PyExc_RuntimeError
,
1776 why
= WHY_EXCEPTION
;
1780 /* w.write() may replace sys.stdout, so we
1781 * have to keep our reference to it */
1783 err
= PyFile_WriteString("\n", w
);
1785 PyFile_SoftSpace(w
, 0);
1794 default: switch (opcode
) {
1800 u
= POP(); /* traceback */
1803 v
= POP(); /* value */
1806 w
= POP(); /* exc */
1807 case 0: /* Fallthrough */
1808 why
= do_raise(w
, v
, u
);
1811 PyErr_SetString(PyExc_SystemError
,
1812 "bad RAISE_VARARGS oparg");
1813 why
= WHY_EXCEPTION
;
1819 if ((x
= f
->f_locals
) != NULL
) {
1824 PyErr_SetString(PyExc_SystemError
, "no locals");
1830 goto fast_block_end
;
1834 f
->f_stacktop
= stack_pointer
;
1843 READ_TIMESTAMP(intr0
);
1844 err
= exec_statement(f
, u
, v
, w
);
1845 READ_TIMESTAMP(intr1
);
1853 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1854 while (STACK_LEVEL() > b
->b_level
) {
1861 PREDICTED(END_FINALLY
);
1864 if (PyInt_Check(v
)) {
1865 why
= (enum why_code
) PyInt_AS_LONG(v
);
1866 assert(why
!= WHY_YIELD
);
1867 if (why
== WHY_RETURN
||
1868 why
== WHY_CONTINUE
)
1871 else if (PyExceptionClass_Check(v
) ||
1872 PyString_Check(v
)) {
1875 PyErr_Restore(v
, w
, u
);
1879 else if (v
!= Py_None
) {
1880 PyErr_SetString(PyExc_SystemError
,
1881 "'finally' pops bad exception");
1882 why
= WHY_EXCEPTION
;
1892 x
= build_class(u
, v
, w
);
1900 w
= GETITEM(names
, oparg
);
1902 if ((x
= f
->f_locals
) != NULL
) {
1903 if (PyDict_CheckExact(x
))
1904 err
= PyDict_SetItem(x
, w
, v
);
1906 err
= PyObject_SetItem(x
, w
, v
);
1908 if (err
== 0) continue;
1911 PyErr_Format(PyExc_SystemError
,
1912 "no locals found when storing %s",
1917 w
= GETITEM(names
, oparg
);
1918 if ((x
= f
->f_locals
) != NULL
) {
1919 if ((err
= PyObject_DelItem(x
, w
)) != 0)
1920 format_exc_check_arg(PyExc_NameError
,
1925 PyErr_Format(PyExc_SystemError
,
1926 "no locals when deleting %s",
1930 PREDICTED_WITH_ARG(UNPACK_SEQUENCE
);
1931 case UNPACK_SEQUENCE
:
1933 if (PyTuple_CheckExact(v
) &&
1934 PyTuple_GET_SIZE(v
) == oparg
) {
1935 PyObject
**items
= \
1936 ((PyTupleObject
*)v
)->ob_item
;
1944 } else if (PyList_CheckExact(v
) &&
1945 PyList_GET_SIZE(v
) == oparg
) {
1946 PyObject
**items
= \
1947 ((PyListObject
*)v
)->ob_item
;
1953 } else if (unpack_iterable(v
, oparg
,
1954 stack_pointer
+ oparg
)) {
1955 stack_pointer
+= oparg
;
1957 /* unpack_iterable() raised an exception */
1958 why
= WHY_EXCEPTION
;
1964 w
= GETITEM(names
, oparg
);
1968 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1971 if (err
== 0) continue;
1975 w
= GETITEM(names
, oparg
);
1977 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1983 w
= GETITEM(names
, oparg
);
1985 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1987 if (err
== 0) continue;
1991 w
= GETITEM(names
, oparg
);
1992 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1993 format_exc_check_arg(
1994 PyExc_NameError
, GLOBAL_NAME_ERROR_MSG
, w
);
1998 w
= GETITEM(names
, oparg
);
1999 if ((v
= f
->f_locals
) == NULL
) {
2000 PyErr_Format(PyExc_SystemError
,
2001 "no locals when loading %s",
2003 why
= WHY_EXCEPTION
;
2006 if (PyDict_CheckExact(v
)) {
2007 x
= PyDict_GetItem(v
, w
);
2011 x
= PyObject_GetItem(v
, w
);
2012 if (x
== NULL
&& PyErr_Occurred()) {
2013 if (!PyErr_ExceptionMatches(
2020 x
= PyDict_GetItem(f
->f_globals
, w
);
2022 x
= PyDict_GetItem(f
->f_builtins
, w
);
2024 format_exc_check_arg(
2036 w
= GETITEM(names
, oparg
);
2037 if (PyString_CheckExact(w
)) {
2038 /* Inline the PyDict_GetItem() calls.
2039 WARNING: this is an extreme speed hack.
2040 Do not try this at home. */
2041 long hash
= ((PyStringObject
*)w
)->ob_shash
;
2045 d
= (PyDictObject
*)(f
->f_globals
);
2046 e
= d
->ma_lookup(d
, w
, hash
);
2057 d
= (PyDictObject
*)(f
->f_builtins
);
2058 e
= d
->ma_lookup(d
, w
, hash
);
2069 goto load_global_error
;
2072 /* This is the un-inlined version of the code above */
2073 x
= PyDict_GetItem(f
->f_globals
, w
);
2075 x
= PyDict_GetItem(f
->f_builtins
, w
);
2078 format_exc_check_arg(
2080 GLOBAL_NAME_ERROR_MSG
, w
);
2089 x
= GETLOCAL(oparg
);
2091 SETLOCAL(oparg
, NULL
);
2094 format_exc_check_arg(
2095 PyExc_UnboundLocalError
,
2096 UNBOUNDLOCAL_ERROR_MSG
,
2097 PyTuple_GetItem(co
->co_varnames
, oparg
)
2102 x
= freevars
[oparg
];
2105 if (x
!= NULL
) continue;
2109 x
= freevars
[oparg
];
2116 /* Don't stomp existing exception */
2117 if (PyErr_Occurred())
2119 if (oparg
< PyTuple_GET_SIZE(co
->co_cellvars
)) {
2120 v
= PyTuple_GET_ITEM(co
->co_cellvars
,
2122 format_exc_check_arg(
2123 PyExc_UnboundLocalError
,
2124 UNBOUNDLOCAL_ERROR_MSG
,
2127 v
= PyTuple_GET_ITEM(co
->co_freevars
, oparg
-
2128 PyTuple_GET_SIZE(co
->co_cellvars
));
2129 format_exc_check_arg(PyExc_NameError
,
2130 UNBOUNDFREE_ERROR_MSG
, v
);
2136 x
= freevars
[oparg
];
2142 x
= PyTuple_New(oparg
);
2144 for (; --oparg
>= 0;) {
2146 PyTuple_SET_ITEM(x
, oparg
, w
);
2154 x
= PyList_New(oparg
);
2156 for (; --oparg
>= 0;) {
2158 PyList_SET_ITEM(x
, oparg
, w
);
2166 x
= _PyDict_NewPresized((Py_ssize_t
)oparg
);
2168 if (x
!= NULL
) continue;
2172 w
= TOP(); /* key */
2173 u
= SECOND(); /* value */
2174 v
= THIRD(); /* dict */
2176 assert (PyDict_CheckExact(v
));
2177 err
= PyDict_SetItem(v
, w
, u
); /* v[w] = u */
2180 if (err
== 0) continue;
2184 w
= GETITEM(names
, oparg
);
2186 x
= PyObject_GetAttr(v
, w
);
2189 if (x
!= NULL
) continue;
2195 if (PyInt_CheckExact(w
) && PyInt_CheckExact(v
)) {
2196 /* INLINE: cmp(int, int) */
2199 a
= PyInt_AS_LONG(v
);
2200 b
= PyInt_AS_LONG(w
);
2202 case PyCmp_LT
: res
= a
< b
; break;
2203 case PyCmp_LE
: res
= a
<= b
; break;
2204 case PyCmp_EQ
: res
= a
== b
; break;
2205 case PyCmp_NE
: res
= a
!= b
; break;
2206 case PyCmp_GT
: res
= a
> b
; break;
2207 case PyCmp_GE
: res
= a
>= b
; break;
2208 case PyCmp_IS
: res
= v
== w
; break;
2209 case PyCmp_IS_NOT
: res
= v
!= w
; break;
2210 default: goto slow_compare
;
2212 x
= res
? Py_True
: Py_False
;
2217 x
= cmp_outcome(oparg
, v
, w
);
2222 if (x
== NULL
) break;
2223 PREDICT(JUMP_IF_FALSE
);
2224 PREDICT(JUMP_IF_TRUE
);
2228 w
= GETITEM(names
, oparg
);
2229 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
2231 PyErr_SetString(PyExc_ImportError
,
2232 "__import__ not found");
2238 if (PyInt_AsLong(u
) != -1 || PyErr_Occurred())
2242 f
->f_locals
== NULL
?
2243 Py_None
: f
->f_locals
,
2250 f
->f_locals
== NULL
?
2251 Py_None
: f
->f_locals
,
2261 READ_TIMESTAMP(intr0
);
2263 x
= PyEval_CallObject(v
, w
);
2265 READ_TIMESTAMP(intr1
);
2268 if (x
!= NULL
) continue;
2273 PyFrame_FastToLocals(f
);
2274 if ((x
= f
->f_locals
) == NULL
) {
2275 PyErr_SetString(PyExc_SystemError
,
2276 "no locals found during 'import *'");
2279 READ_TIMESTAMP(intr0
);
2280 err
= import_all_from(x
, v
);
2281 READ_TIMESTAMP(intr1
);
2282 PyFrame_LocalsToFast(f
, 0);
2284 if (err
== 0) continue;
2288 w
= GETITEM(names
, oparg
);
2290 READ_TIMESTAMP(intr0
);
2291 x
= import_from(v
, w
);
2292 READ_TIMESTAMP(intr1
);
2294 if (x
!= NULL
) continue;
2299 goto fast_next_opcode
;
2301 PREDICTED_WITH_ARG(JUMP_IF_FALSE
);
2306 goto fast_next_opcode
;
2308 if (w
== Py_False
) {
2310 goto fast_next_opcode
;
2312 err
= PyObject_IsTrue(w
);
2321 PREDICTED_WITH_ARG(JUMP_IF_TRUE
);
2324 if (w
== Py_False
) {
2326 goto fast_next_opcode
;
2330 goto fast_next_opcode
;
2332 err
= PyObject_IsTrue(w
);
2343 PREDICTED_WITH_ARG(JUMP_ABSOLUTE
);
2347 /* Enabling this path speeds-up all while and for-loops by bypassing
2348 the per-loop checks for signals. By default, this should be turned-off
2349 because it prevents detection of a control-break in tight loops like
2350 "while 1: pass". Compile with this option turned-on when you need
2351 the speed-up and do not need break checking inside tight loops (ones
2352 that contain only instructions ending with goto fast_next_opcode).
2354 goto fast_next_opcode
;
2360 /* before: [obj]; after [getiter(obj)] */
2362 x
= PyObject_GetIter(v
);
2372 PREDICTED_WITH_ARG(FOR_ITER
);
2374 /* before: [iter]; after: [iter, iter()] *or* [] */
2376 x
= (*v
->ob_type
->tp_iternext
)(v
);
2379 PREDICT(STORE_FAST
);
2380 PREDICT(UNPACK_SEQUENCE
);
2383 if (PyErr_Occurred()) {
2384 if (!PyErr_ExceptionMatches(
2385 PyExc_StopIteration
))
2389 /* iterator ended normally */
2397 goto fast_block_end
;
2400 retval
= PyInt_FromLong(oparg
);
2406 goto fast_block_end
;
2411 /* NOTE: If you add any new block-setup opcodes that
2412 are not try/except/finally handlers, you may need
2413 to update the PyGen_NeedsFinalizing() function.
2416 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
2422 /* At the top of the stack are 1-3 values indicating
2423 how/why we entered the finally clause:
2425 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2426 - TOP = WHY_*; no retval below it
2427 - (TOP, SECOND, THIRD) = exc_info()
2428 Below them is EXIT, the context.__exit__ bound method.
2429 In the last case, we must call
2430 EXIT(TOP, SECOND, THIRD)
2431 otherwise we must call
2432 EXIT(None, None, None)
2434 In all cases, we remove EXIT from the stack, leaving
2435 the rest in the same order.
2437 In addition, if the stack represents an exception,
2438 *and* the function call returns a 'true' value, we
2439 "zap" this information, to prevent END_FINALLY from
2440 re-raising the exception. (But non-local gotos
2441 should still be resumed.)
2444 PyObject
*exit_func
;
2452 else if (PyInt_Check(u
)) {
2453 switch(PyInt_AS_LONG(u
)) {
2456 /* Retval in TOP. */
2457 exit_func
= SECOND();
2466 u
= v
= w
= Py_None
;
2471 exit_func
= THIRD();
2476 /* XXX Not the fastest way to call it... */
2477 x
= PyObject_CallFunctionObjArgs(exit_func
, u
, v
, w
,
2479 Py_DECREF(exit_func
);
2481 break; /* Go to error exit */
2484 err
= PyObject_IsTrue(x
);
2490 break; /* Go to error exit */
2493 /* There was an exception and a true return */
2501 /* The stack was rearranged to remove EXIT
2502 above. Let END_FINALLY do its thing */
2504 PREDICT(END_FINALLY
);
2514 x
= call_function(&sp
, oparg
, &intr0
, &intr1
);
2516 x
= call_function(&sp
, oparg
);
2525 case CALL_FUNCTION_VAR
:
2526 case CALL_FUNCTION_KW
:
2527 case CALL_FUNCTION_VAR_KW
:
2529 int na
= oparg
& 0xff;
2530 int nk
= (oparg
>>8) & 0xff;
2531 int flags
= (opcode
- CALL_FUNCTION
) & 3;
2532 int n
= na
+ 2 * nk
;
2533 PyObject
**pfunc
, *func
, **sp
;
2535 if (flags
& CALL_FLAG_VAR
)
2537 if (flags
& CALL_FLAG_KW
)
2539 pfunc
= stack_pointer
- n
- 1;
2542 if (PyMethod_Check(func
)
2543 && PyMethod_GET_SELF(func
) != NULL
) {
2544 PyObject
*self
= PyMethod_GET_SELF(func
);
2546 func
= PyMethod_GET_FUNCTION(func
);
2555 READ_TIMESTAMP(intr0
);
2556 x
= ext_do_call(func
, &sp
, flags
, na
, nk
);
2557 READ_TIMESTAMP(intr1
);
2561 while (stack_pointer
> pfunc
) {
2572 v
= POP(); /* code object */
2573 x
= PyFunction_New(v
, f
->f_globals
);
2575 /* XXX Maybe this should be a separate opcode? */
2576 if (x
!= NULL
&& oparg
> 0) {
2577 v
= PyTuple_New(oparg
);
2583 while (--oparg
>= 0) {
2585 PyTuple_SET_ITEM(v
, oparg
, w
);
2587 err
= PyFunction_SetDefaults(x
, v
);
2595 v
= POP(); /* code object */
2596 x
= PyFunction_New(v
, f
->f_globals
);
2600 if (PyFunction_SetClosure(x
, v
) != 0) {
2601 /* Can't happen unless bytecode is corrupt. */
2602 why
= WHY_EXCEPTION
;
2606 if (x
!= NULL
&& oparg
> 0) {
2607 v
= PyTuple_New(oparg
);
2613 while (--oparg
>= 0) {
2615 PyTuple_SET_ITEM(v
, oparg
, w
);
2617 if (PyFunction_SetDefaults(x
, v
) != 0) {
2618 /* Can't happen unless
2619 PyFunction_SetDefaults changes. */
2620 why
= WHY_EXCEPTION
;
2635 x
= PySlice_New(u
, v
, w
);
2640 if (x
!= NULL
) continue;
2645 oparg
= oparg
<<16 | NEXTARG();
2646 goto dispatch_opcode
;
2650 "XXX lineno: %d, opcode: %d\n",
2651 PyCode_Addr2Line(f
->f_code
, f
->f_lasti
),
2653 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
2654 why
= WHY_EXCEPTION
;
2665 READ_TIMESTAMP(inst1
);
2667 /* Quickly continue if no error occurred */
2669 if (why
== WHY_NOT
) {
2670 if (err
== 0 && x
!= NULL
) {
2672 /* This check is expensive! */
2673 if (PyErr_Occurred())
2675 "XXX undetected error\n");
2678 READ_TIMESTAMP(loop1
);
2679 continue; /* Normal, fast path */
2684 why
= WHY_EXCEPTION
;
2689 /* Double-check exception status */
2691 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
2692 if (!PyErr_Occurred()) {
2693 PyErr_SetString(PyExc_SystemError
,
2694 "error return without exception set");
2695 why
= WHY_EXCEPTION
;
2700 /* This check is expensive! */
2701 if (PyErr_Occurred()) {
2703 sprintf(buf
, "Stack unwind with exception "
2704 "set and why=%d", why
);
2710 /* Log traceback info if this is a real exception */
2712 if (why
== WHY_EXCEPTION
) {
2713 PyTraceBack_Here(f
);
2715 if (tstate
->c_tracefunc
!= NULL
)
2716 call_exc_trace(tstate
->c_tracefunc
,
2717 tstate
->c_traceobj
, f
);
2720 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2722 if (why
== WHY_RERAISE
)
2723 why
= WHY_EXCEPTION
;
2725 /* Unwind stacks if a (pseudo) exception occurred */
2728 while (why
!= WHY_NOT
&& f
->f_iblock
> 0) {
2729 PyTryBlock
*b
= PyFrame_BlockPop(f
);
2731 assert(why
!= WHY_YIELD
);
2732 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_CONTINUE
) {
2733 /* For a continue inside a try block,
2734 don't pop the block for the loop. */
2735 PyFrame_BlockSetup(f
, b
->b_type
, b
->b_handler
,
2738 JUMPTO(PyInt_AS_LONG(retval
));
2743 while (STACK_LEVEL() > b
->b_level
) {
2747 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
2749 JUMPTO(b
->b_handler
);
2752 if (b
->b_type
== SETUP_FINALLY
||
2753 (b
->b_type
== SETUP_EXCEPT
&&
2754 why
== WHY_EXCEPTION
)) {
2755 if (why
== WHY_EXCEPTION
) {
2756 PyObject
*exc
, *val
, *tb
;
2757 PyErr_Fetch(&exc
, &val
, &tb
);
2762 /* Make the raw exception data
2763 available to the handler,
2764 so a program can emulate the
2765 Python main loop. Don't do
2766 this for 'finally'. */
2767 if (b
->b_type
== SETUP_EXCEPT
) {
2768 PyErr_NormalizeException(
2770 set_exc_info(tstate
,
2782 if (why
& (WHY_RETURN
| WHY_CONTINUE
))
2784 v
= PyInt_FromLong((long)why
);
2788 JUMPTO(b
->b_handler
);
2791 } /* unwind stack */
2793 /* End the loop if we still have an error (or return) */
2797 READ_TIMESTAMP(loop1
);
2801 assert(why
!= WHY_YIELD
);
2802 /* Pop remaining stack entries. */
2808 if (why
!= WHY_RETURN
)
2812 if (tstate
->use_tracing
) {
2813 if (tstate
->c_tracefunc
) {
2814 if (why
== WHY_RETURN
|| why
== WHY_YIELD
) {
2815 if (call_trace(tstate
->c_tracefunc
,
2816 tstate
->c_traceobj
, f
,
2817 PyTrace_RETURN
, retval
)) {
2820 why
= WHY_EXCEPTION
;
2823 else if (why
== WHY_EXCEPTION
) {
2824 call_trace_protected(tstate
->c_tracefunc
,
2825 tstate
->c_traceobj
, f
,
2826 PyTrace_RETURN
, NULL
);
2829 if (tstate
->c_profilefunc
) {
2830 if (why
== WHY_EXCEPTION
)
2831 call_trace_protected(tstate
->c_profilefunc
,
2832 tstate
->c_profileobj
, f
,
2833 PyTrace_RETURN
, NULL
);
2834 else if (call_trace(tstate
->c_profilefunc
,
2835 tstate
->c_profileobj
, f
,
2836 PyTrace_RETURN
, retval
)) {
2839 why
= WHY_EXCEPTION
;
2844 if (tstate
->frame
->f_exc_type
!= NULL
)
2845 reset_exc_info(tstate
);
2847 assert(tstate
->frame
->f_exc_value
== NULL
);
2848 assert(tstate
->frame
->f_exc_traceback
== NULL
);
2853 Py_LeaveRecursiveCall();
2854 tstate
->frame
= f
->f_back
;
2859 /* This is gonna seem *real weird*, but if you put some other code between
2860 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2861 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2864 PyEval_EvalCodeEx(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
,
2865 PyObject
**args
, int argcount
, PyObject
**kws
, int kwcount
,
2866 PyObject
**defs
, int defcount
, PyObject
*closure
)
2868 register PyFrameObject
*f
;
2869 register PyObject
*retval
= NULL
;
2870 register PyObject
**fastlocals
, **freevars
;
2871 PyThreadState
*tstate
= PyThreadState_GET();
2874 if (globals
== NULL
) {
2875 PyErr_SetString(PyExc_SystemError
,
2876 "PyEval_EvalCodeEx: NULL globals");
2880 assert(tstate
!= NULL
);
2881 assert(globals
!= NULL
);
2882 f
= PyFrame_New(tstate
, co
, globals
, locals
);
2886 fastlocals
= f
->f_localsplus
;
2887 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
2889 if (co
->co_argcount
> 0 ||
2890 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
2893 PyObject
*kwdict
= NULL
;
2894 if (co
->co_flags
& CO_VARKEYWORDS
) {
2895 kwdict
= PyDict_New();
2898 i
= co
->co_argcount
;
2899 if (co
->co_flags
& CO_VARARGS
)
2901 SETLOCAL(i
, kwdict
);
2903 if (argcount
> co
->co_argcount
) {
2904 if (!(co
->co_flags
& CO_VARARGS
)) {
2905 PyErr_Format(PyExc_TypeError
,
2906 "%.200s() takes %s %d "
2907 "%sargument%s (%d given)",
2908 PyString_AsString(co
->co_name
),
2909 defcount
? "at most" : "exactly",
2911 kwcount
? "non-keyword " : "",
2912 co
->co_argcount
== 1 ? "" : "s",
2916 n
= co
->co_argcount
;
2918 for (i
= 0; i
< n
; i
++) {
2923 if (co
->co_flags
& CO_VARARGS
) {
2924 u
= PyTuple_New(argcount
- n
);
2927 SETLOCAL(co
->co_argcount
, u
);
2928 for (i
= n
; i
< argcount
; i
++) {
2931 PyTuple_SET_ITEM(u
, i
-n
, x
);
2934 for (i
= 0; i
< kwcount
; i
++) {
2935 PyObject
**co_varnames
;
2936 PyObject
*keyword
= kws
[2*i
];
2937 PyObject
*value
= kws
[2*i
+ 1];
2939 if (keyword
== NULL
|| !(PyString_Check(keyword
)
2940 #ifdef Py_USING_UNICODE
2941 || PyUnicode_Check(keyword
)
2944 PyErr_Format(PyExc_TypeError
,
2945 "%.200s() keywords must be strings",
2946 PyString_AsString(co
->co_name
));
2949 /* Speed hack: do raw pointer compares. As names are
2950 normally interned this should almost always hit. */
2951 co_varnames
= PySequence_Fast_ITEMS(co
->co_varnames
);
2952 for (j
= 0; j
< co
->co_argcount
; j
++) {
2953 PyObject
*nm
= co_varnames
[j
];
2957 /* Slow fallback, just in case */
2958 for (j
= 0; j
< co
->co_argcount
; j
++) {
2959 PyObject
*nm
= co_varnames
[j
];
2960 int cmp
= PyObject_RichCompareBool(
2961 keyword
, nm
, Py_EQ
);
2967 /* Check errors from Compare */
2968 if (PyErr_Occurred())
2970 if (j
>= co
->co_argcount
) {
2971 if (kwdict
== NULL
) {
2972 PyObject
*kwd_str
= kwd_as_string(keyword
);
2974 PyErr_Format(PyExc_TypeError
,
2975 "%.200s() got an unexpected "
2976 "keyword argument '%.400s'",
2977 PyString_AsString(co
->co_name
),
2978 PyString_AsString(kwd_str
));
2983 PyDict_SetItem(kwdict
, keyword
, value
);
2987 if (GETLOCAL(j
) != NULL
) {
2988 PyObject
*kwd_str
= kwd_as_string(keyword
);
2990 PyErr_Format(PyExc_TypeError
,
2991 "%.200s() got multiple "
2992 "values for keyword "
2993 "argument '%.400s'",
2994 PyString_AsString(co
->co_name
),
2995 PyString_AsString(kwd_str
));
3003 if (argcount
< co
->co_argcount
) {
3004 int m
= co
->co_argcount
- defcount
;
3005 for (i
= argcount
; i
< m
; i
++) {
3006 if (GETLOCAL(i
) == NULL
) {
3007 PyErr_Format(PyExc_TypeError
,
3008 "%.200s() takes %s %d "
3009 "%sargument%s (%d given)",
3010 PyString_AsString(co
->co_name
),
3011 ((co
->co_flags
& CO_VARARGS
) ||
3012 defcount
) ? "at least"
3014 m
, kwcount
? "non-keyword " : "",
3015 m
== 1 ? "" : "s", i
);
3023 for (; i
< defcount
; i
++) {
3024 if (GETLOCAL(m
+i
) == NULL
) {
3025 PyObject
*def
= defs
[i
];
3033 if (argcount
> 0 || kwcount
> 0) {
3034 PyErr_Format(PyExc_TypeError
,
3035 "%.200s() takes no arguments (%d given)",
3036 PyString_AsString(co
->co_name
),
3037 argcount
+ kwcount
);
3041 /* Allocate and initialize storage for cell vars, and copy free
3042 vars into frame. This isn't too efficient right now. */
3043 if (PyTuple_GET_SIZE(co
->co_cellvars
)) {
3044 int i
, j
, nargs
, found
;
3045 char *cellname
, *argname
;
3048 nargs
= co
->co_argcount
;
3049 if (co
->co_flags
& CO_VARARGS
)
3051 if (co
->co_flags
& CO_VARKEYWORDS
)
3054 /* Initialize each cell var, taking into account
3055 cell vars that are initialized from arguments.
3057 Should arrange for the compiler to put cellvars
3058 that are arguments at the beginning of the cellvars
3059 list so that we can march over it more efficiently?
3061 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_cellvars
); ++i
) {
3062 cellname
= PyString_AS_STRING(
3063 PyTuple_GET_ITEM(co
->co_cellvars
, i
));
3065 for (j
= 0; j
< nargs
; j
++) {
3066 argname
= PyString_AS_STRING(
3067 PyTuple_GET_ITEM(co
->co_varnames
, j
));
3068 if (strcmp(cellname
, argname
) == 0) {
3069 c
= PyCell_New(GETLOCAL(j
));
3072 GETLOCAL(co
->co_nlocals
+ i
) = c
;
3078 c
= PyCell_New(NULL
);
3081 SETLOCAL(co
->co_nlocals
+ i
, c
);
3085 if (PyTuple_GET_SIZE(co
->co_freevars
)) {
3087 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_freevars
); ++i
) {
3088 PyObject
*o
= PyTuple_GET_ITEM(closure
, i
);
3090 freevars
[PyTuple_GET_SIZE(co
->co_cellvars
) + i
] = o
;
3094 if (co
->co_flags
& CO_GENERATOR
) {
3095 /* Don't need to keep the reference to f_back, it will be set
3096 * when the generator is resumed. */
3097 Py_XDECREF(f
->f_back
);
3100 PCALL(PCALL_GENERATOR
);
3102 /* Create a new generator that owns the ready to run frame
3103 * and return that as the value. */
3104 return PyGen_New(f
);
3107 retval
= PyEval_EvalFrameEx(f
,0);
3109 fail
: /* Jump here from prelude on failure */
3111 /* decref'ing the frame can cause __del__ methods to get invoked,
3112 which can call back into Python. While we're done with the
3113 current Python frame (f), the associated C stack is still in use,
3114 so recursion_depth must be boosted for the duration.
3116 assert(tstate
!= NULL
);
3117 ++tstate
->recursion_depth
;
3119 --tstate
->recursion_depth
;
3126 kwd_as_string(PyObject
*kwd
) {
3127 #ifdef Py_USING_UNICODE
3128 if (PyString_Check(kwd
)) {
3130 assert(PyString_Check(kwd
));
3134 #ifdef Py_USING_UNICODE
3136 return _PyUnicode_AsDefaultEncodedString(kwd
, "replace");
3141 /* Implementation notes for set_exc_info() and reset_exc_info():
3143 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3144 'exc_traceback'. These always travel together.
3146 - tstate->curexc_ZZZ is the "hot" exception that is set by
3147 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3149 - Once an exception is caught by an except clause, it is transferred
3150 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3151 can pick it up. This is the primary task of set_exc_info().
3152 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
3154 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3156 Long ago, when none of this existed, there were just a few globals:
3157 one set corresponding to the "hot" exception, and one set
3158 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3159 globals; they were simply stored as sys.exc_ZZZ. For backwards
3160 compatibility, they still are!) The problem was that in code like
3164 "something that may fail"
3165 except "some exception":
3166 "do something else first"
3167 "print the exception from sys.exc_ZZZ."
3169 if "do something else first" invoked something that raised and caught
3170 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3171 cause of subtle bugs. I fixed this by changing the semantics as
3174 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3177 - But initially, and as long as no exception is caught in a given
3178 frame, sys.exc_ZZZ will hold the last exception caught in the
3179 previous frame (or the frame before that, etc.).
3181 The first bullet fixed the bug in the above example. The second
3182 bullet was for backwards compatibility: it was (and is) common to
3183 have a function that is called when an exception is caught, and to
3184 have that function access the caught exception via sys.exc_ZZZ.
3185 (Example: traceback.print_exc()).
3187 At the same time I fixed the problem that sys.exc_ZZZ weren't
3188 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3189 but that's really a separate improvement.
3191 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3192 variables to what they were before the current frame was called. The
3193 set_exc_info() function saves them on the frame so that
3194 reset_exc_info() can restore them. The invariant is that
3195 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3196 exception (where "catching" an exception applies only to successful
3197 except clauses); and if the current frame ever caught an exception,
3198 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3199 at the start of the current frame.
3204 set_exc_info(PyThreadState
*tstate
,
3205 PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3207 PyFrameObject
*frame
= tstate
->frame
;
3208 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
3210 assert(type
!= NULL
);
3211 assert(frame
!= NULL
);
3212 if (frame
->f_exc_type
== NULL
) {
3213 assert(frame
->f_exc_value
== NULL
);
3214 assert(frame
->f_exc_traceback
== NULL
);
3215 /* This frame didn't catch an exception before. */
3216 /* Save previous exception of this thread in this frame. */
3217 if (tstate
->exc_type
== NULL
) {
3218 /* XXX Why is this set to Py_None? */
3220 tstate
->exc_type
= Py_None
;
3222 Py_INCREF(tstate
->exc_type
);
3223 Py_XINCREF(tstate
->exc_value
);
3224 Py_XINCREF(tstate
->exc_traceback
);
3225 frame
->f_exc_type
= tstate
->exc_type
;
3226 frame
->f_exc_value
= tstate
->exc_value
;
3227 frame
->f_exc_traceback
= tstate
->exc_traceback
;
3229 /* Set new exception for this thread. */
3230 tmp_type
= tstate
->exc_type
;
3231 tmp_value
= tstate
->exc_value
;
3232 tmp_tb
= tstate
->exc_traceback
;
3236 tstate
->exc_type
= type
;
3237 tstate
->exc_value
= value
;
3238 tstate
->exc_traceback
= tb
;
3239 Py_XDECREF(tmp_type
);
3240 Py_XDECREF(tmp_value
);
3242 /* For b/w compatibility */
3243 PySys_SetObject("exc_type", type
);
3244 PySys_SetObject("exc_value", value
);
3245 PySys_SetObject("exc_traceback", tb
);
3249 reset_exc_info(PyThreadState
*tstate
)
3251 PyFrameObject
*frame
;
3252 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
3254 /* It's a precondition that the thread state's frame caught an
3255 * exception -- verify in a debug build.
3257 assert(tstate
!= NULL
);
3258 frame
= tstate
->frame
;
3259 assert(frame
!= NULL
);
3260 assert(frame
->f_exc_type
!= NULL
);
3262 /* Copy the frame's exception info back to the thread state. */
3263 tmp_type
= tstate
->exc_type
;
3264 tmp_value
= tstate
->exc_value
;
3265 tmp_tb
= tstate
->exc_traceback
;
3266 Py_INCREF(frame
->f_exc_type
);
3267 Py_XINCREF(frame
->f_exc_value
);
3268 Py_XINCREF(frame
->f_exc_traceback
);
3269 tstate
->exc_type
= frame
->f_exc_type
;
3270 tstate
->exc_value
= frame
->f_exc_value
;
3271 tstate
->exc_traceback
= frame
->f_exc_traceback
;
3272 Py_XDECREF(tmp_type
);
3273 Py_XDECREF(tmp_value
);
3276 /* For b/w compatibility */
3277 PySys_SetObject("exc_type", frame
->f_exc_type
);
3278 PySys_SetObject("exc_value", frame
->f_exc_value
);
3279 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
3281 /* Clear the frame's exception info. */
3282 tmp_type
= frame
->f_exc_type
;
3283 tmp_value
= frame
->f_exc_value
;
3284 tmp_tb
= frame
->f_exc_traceback
;
3285 frame
->f_exc_type
= NULL
;
3286 frame
->f_exc_value
= NULL
;
3287 frame
->f_exc_traceback
= NULL
;
3288 Py_DECREF(tmp_type
);
3289 Py_XDECREF(tmp_value
);
3293 /* Logic for the raise statement (too complicated for inlining).
3294 This *consumes* a reference count to each of its arguments. */
3295 static enum why_code
3296 do_raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3300 PyThreadState
*tstate
= PyThreadState_GET();
3301 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
3302 value
= tstate
->exc_value
;
3303 tb
= tstate
->exc_traceback
;
3309 /* We support the following forms of raise:
3310 raise <class>, <classinstance>
3311 raise <class>, <argument tuple>
3313 raise <class>, <argument>
3314 raise <classinstance>, None
3315 raise <string>, <object>
3316 raise <string>, None
3318 An omitted second argument is the same as None.
3320 In addition, raise <tuple>, <anything> is the same as
3321 raising the tuple's first item (and it better have one!);
3322 this rule is applied recursively.
3324 Finally, an optional third argument can be supplied, which
3325 gives the traceback to be substituted (useful when
3326 re-raising an exception after examining it). */
3328 /* First, check the traceback argument, replacing None with
3330 if (tb
== Py_None
) {
3334 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
3335 PyErr_SetString(PyExc_TypeError
,
3336 "raise: arg 3 must be a traceback or None");
3340 /* Next, replace a missing value with None */
3341 if (value
== NULL
) {
3346 /* Next, repeatedly, replace a tuple exception with its first item */
3347 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
3348 PyObject
*tmp
= type
;
3349 type
= PyTuple_GET_ITEM(type
, 0);
3354 if (PyExceptionClass_Check(type
))
3355 PyErr_NormalizeException(&type
, &value
, &tb
);
3357 else if (PyExceptionInstance_Check(type
)) {
3358 /* Raising an instance. The value should be a dummy. */
3359 if (value
!= Py_None
) {
3360 PyErr_SetString(PyExc_TypeError
,
3361 "instance exception may not have a separate value");
3365 /* Normalize to raise <class>, <instance> */
3368 type
= PyExceptionInstance_Class(type
);
3373 /* Not something you can raise. You get an exception
3374 anyway, just not what you specified :-) */
3375 PyErr_Format(PyExc_TypeError
,
3376 "exceptions must be classes or instances, not %s",
3377 type
->ob_type
->tp_name
);
3381 assert(PyExceptionClass_Check(type
));
3382 if (Py_Py3kWarningFlag
&& PyClass_Check(type
)) {
3383 if (PyErr_WarnEx(PyExc_DeprecationWarning
,
3384 "exceptions must derive from BaseException "
3389 PyErr_Restore(type
, value
, tb
);
3391 return WHY_EXCEPTION
;
3398 return WHY_EXCEPTION
;
3401 /* Iterate v argcnt times and store the results on the stack (via decreasing
3402 sp). Return 1 for success, 0 if error. */
3405 unpack_iterable(PyObject
*v
, int argcnt
, PyObject
**sp
)
3408 PyObject
*it
; /* iter(v) */
3413 it
= PyObject_GetIter(v
);
3417 for (; i
< argcnt
; i
++) {
3418 w
= PyIter_Next(it
);
3420 /* Iterator done, via error or exhaustion. */
3421 if (!PyErr_Occurred()) {
3422 PyErr_Format(PyExc_ValueError
,
3423 "need more than %d value%s to unpack",
3424 i
, i
== 1 ? "" : "s");
3431 /* We better have exhausted the iterator now. */
3432 w
= PyIter_Next(it
);
3434 if (PyErr_Occurred())
3440 PyErr_SetString(PyExc_ValueError
, "too many values to unpack");
3443 for (; i
> 0; i
--, sp
++)
3452 prtrace(PyObject
*v
, char *str
)
3455 if (PyObject_Print(v
, stdout
, 0) != 0)
3456 PyErr_Clear(); /* Don't know what else to do */
3463 call_exc_trace(Py_tracefunc func
, PyObject
*self
, PyFrameObject
*f
)
3465 PyObject
*type
, *value
, *traceback
, *arg
;
3467 PyErr_Fetch(&type
, &value
, &traceback
);
3468 if (value
== NULL
) {
3472 arg
= PyTuple_Pack(3, type
, value
, traceback
);
3474 PyErr_Restore(type
, value
, traceback
);
3477 err
= call_trace(func
, self
, f
, PyTrace_EXCEPTION
, arg
);
3480 PyErr_Restore(type
, value
, traceback
);
3484 Py_XDECREF(traceback
);
3489 call_trace_protected(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3490 int what
, PyObject
*arg
)
3492 PyObject
*type
, *value
, *traceback
;
3494 PyErr_Fetch(&type
, &value
, &traceback
);
3495 err
= call_trace(func
, obj
, frame
, what
, arg
);
3498 PyErr_Restore(type
, value
, traceback
);
3504 Py_XDECREF(traceback
);
3510 call_trace(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3511 int what
, PyObject
*arg
)
3513 register PyThreadState
*tstate
= frame
->f_tstate
;
3515 if (tstate
->tracing
)
3518 tstate
->use_tracing
= 0;
3519 result
= func(obj
, frame
, what
, arg
);
3520 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3521 || (tstate
->c_profilefunc
!= NULL
));
3527 _PyEval_CallTracing(PyObject
*func
, PyObject
*args
)
3529 PyFrameObject
*frame
= PyEval_GetFrame();
3530 PyThreadState
*tstate
= frame
->f_tstate
;
3531 int save_tracing
= tstate
->tracing
;
3532 int save_use_tracing
= tstate
->use_tracing
;
3535 tstate
->tracing
= 0;
3536 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3537 || (tstate
->c_profilefunc
!= NULL
));
3538 result
= PyObject_Call(func
, args
, NULL
);
3539 tstate
->tracing
= save_tracing
;
3540 tstate
->use_tracing
= save_use_tracing
;
3545 maybe_call_line_trace(Py_tracefunc func
, PyObject
*obj
,
3546 PyFrameObject
*frame
, int *instr_lb
, int *instr_ub
,
3551 /* If the last instruction executed isn't in the current
3552 instruction window, reset the window. If the last
3553 instruction happens to fall at the start of a line or if it
3554 represents a jump backwards, call the trace function.
3556 if ((frame
->f_lasti
< *instr_lb
|| frame
->f_lasti
>= *instr_ub
)) {
3560 line
= PyCode_CheckLineNumber(frame
->f_code
, frame
->f_lasti
,
3563 frame
->f_lineno
= line
;
3564 result
= call_trace(func
, obj
, frame
,
3565 PyTrace_LINE
, Py_None
);
3567 *instr_lb
= bounds
.ap_lower
;
3568 *instr_ub
= bounds
.ap_upper
;
3570 else if (frame
->f_lasti
<= *instr_prev
) {
3571 result
= call_trace(func
, obj
, frame
, PyTrace_LINE
, Py_None
);
3573 *instr_prev
= frame
->f_lasti
;
3578 PyEval_SetProfile(Py_tracefunc func
, PyObject
*arg
)
3580 PyThreadState
*tstate
= PyThreadState_GET();
3581 PyObject
*temp
= tstate
->c_profileobj
;
3583 tstate
->c_profilefunc
= NULL
;
3584 tstate
->c_profileobj
= NULL
;
3585 /* Must make sure that tracing is not ignored if 'temp' is freed */
3586 tstate
->use_tracing
= tstate
->c_tracefunc
!= NULL
;
3588 tstate
->c_profilefunc
= func
;
3589 tstate
->c_profileobj
= arg
;
3590 /* Flag that tracing or profiling is turned on */
3591 tstate
->use_tracing
= (func
!= NULL
) || (tstate
->c_tracefunc
!= NULL
);
3595 PyEval_SetTrace(Py_tracefunc func
, PyObject
*arg
)
3597 PyThreadState
*tstate
= PyThreadState_GET();
3598 PyObject
*temp
= tstate
->c_traceobj
;
3599 _Py_TracingPossible
+= (func
!= NULL
) - (tstate
->c_tracefunc
!= NULL
);
3601 tstate
->c_tracefunc
= NULL
;
3602 tstate
->c_traceobj
= NULL
;
3603 /* Must make sure that profiling is not ignored if 'temp' is freed */
3604 tstate
->use_tracing
= tstate
->c_profilefunc
!= NULL
;
3606 tstate
->c_tracefunc
= func
;
3607 tstate
->c_traceobj
= arg
;
3608 /* Flag that tracing or profiling is turned on */
3609 tstate
->use_tracing
= ((func
!= NULL
)
3610 || (tstate
->c_profilefunc
!= NULL
));
3614 PyEval_GetBuiltins(void)
3616 PyFrameObject
*current_frame
= PyEval_GetFrame();
3617 if (current_frame
== NULL
)
3618 return PyThreadState_GET()->interp
->builtins
;
3620 return current_frame
->f_builtins
;
3624 PyEval_GetLocals(void)
3626 PyFrameObject
*current_frame
= PyEval_GetFrame();
3627 if (current_frame
== NULL
)
3629 PyFrame_FastToLocals(current_frame
);
3630 return current_frame
->f_locals
;
3634 PyEval_GetGlobals(void)
3636 PyFrameObject
*current_frame
= PyEval_GetFrame();
3637 if (current_frame
== NULL
)
3640 return current_frame
->f_globals
;
3644 PyEval_GetFrame(void)
3646 PyThreadState
*tstate
= PyThreadState_GET();
3647 return _PyThreadState_GetFrame(tstate
);
3651 PyEval_GetRestricted(void)
3653 PyFrameObject
*current_frame
= PyEval_GetFrame();
3654 return current_frame
== NULL
? 0 : PyFrame_IsRestricted(current_frame
);
3658 PyEval_MergeCompilerFlags(PyCompilerFlags
*cf
)
3660 PyFrameObject
*current_frame
= PyEval_GetFrame();
3661 int result
= cf
->cf_flags
!= 0;
3663 if (current_frame
!= NULL
) {
3664 const int codeflags
= current_frame
->f_code
->co_flags
;
3665 const int compilerflags
= codeflags
& PyCF_MASK
;
3666 if (compilerflags
) {
3668 cf
->cf_flags
|= compilerflags
;
3670 #if 0 /* future keyword */
3671 if (codeflags
& CO_GENERATOR_ALLOWED
) {
3673 cf
->cf_flags
|= CO_GENERATOR_ALLOWED
;
3683 PyObject
*f
= PySys_GetObject("stdout");
3686 if (!PyFile_SoftSpace(f
, 0))
3688 return PyFile_WriteString("\n", f
);
3692 /* External interface to call any callable object.
3693 The arg must be a tuple or NULL. */
3695 #undef PyEval_CallObject
3696 /* for backward compatibility: export this interface */
3699 PyEval_CallObject(PyObject
*func
, PyObject
*arg
)
3701 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
3703 #define PyEval_CallObject(func,arg) \
3704 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3707 PyEval_CallObjectWithKeywords(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
3712 arg
= PyTuple_New(0);
3716 else if (!PyTuple_Check(arg
)) {
3717 PyErr_SetString(PyExc_TypeError
,
3718 "argument list must be a tuple");
3724 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
3725 PyErr_SetString(PyExc_TypeError
,
3726 "keyword list must be a dictionary");
3731 result
= PyObject_Call(func
, arg
, kw
);
3737 PyEval_GetFuncName(PyObject
*func
)
3739 if (PyMethod_Check(func
))
3740 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func
));
3741 else if (PyFunction_Check(func
))
3742 return PyString_AsString(((PyFunctionObject
*)func
)->func_name
);
3743 else if (PyCFunction_Check(func
))
3744 return ((PyCFunctionObject
*)func
)->m_ml
->ml_name
;
3745 else if (PyClass_Check(func
))
3746 return PyString_AsString(((PyClassObject
*)func
)->cl_name
);
3747 else if (PyInstance_Check(func
)) {
3748 return PyString_AsString(
3749 ((PyInstanceObject
*)func
)->in_class
->cl_name
);
3751 return func
->ob_type
->tp_name
;
3756 PyEval_GetFuncDesc(PyObject
*func
)
3758 if (PyMethod_Check(func
))
3760 else if (PyFunction_Check(func
))
3762 else if (PyCFunction_Check(func
))
3764 else if (PyClass_Check(func
))
3765 return " constructor";
3766 else if (PyInstance_Check(func
)) {
3774 err_args(PyObject
*func
, int flags
, int nargs
)
3776 if (flags
& METH_NOARGS
)
3777 PyErr_Format(PyExc_TypeError
,
3778 "%.200s() takes no arguments (%d given)",
3779 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3782 PyErr_Format(PyExc_TypeError
,
3783 "%.200s() takes exactly one argument (%d given)",
3784 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3788 #define C_TRACE(x, call) \
3789 if (tstate->use_tracing && tstate->c_profilefunc) { \
3790 if (call_trace(tstate->c_profilefunc, \
3791 tstate->c_profileobj, \
3792 tstate->frame, PyTrace_C_CALL, \
3798 if (tstate->c_profilefunc != NULL) { \
3800 call_trace_protected(tstate->c_profilefunc, \
3801 tstate->c_profileobj, \
3802 tstate->frame, PyTrace_C_EXCEPTION, \
3804 /* XXX should pass (type, value, tb) */ \
3806 if (call_trace(tstate->c_profilefunc, \
3807 tstate->c_profileobj, \
3808 tstate->frame, PyTrace_C_RETURN, \
3821 call_function(PyObject
***pp_stack
, int oparg
3823 , uint64
* pintr0
, uint64
* pintr1
3827 int na
= oparg
& 0xff;
3828 int nk
= (oparg
>>8) & 0xff;
3829 int n
= na
+ 2 * nk
;
3830 PyObject
**pfunc
= (*pp_stack
) - n
- 1;
3831 PyObject
*func
= *pfunc
;
3834 /* Always dispatch PyCFunction first, because these are
3835 presumed to be the most frequent callable object.
3837 if (PyCFunction_Check(func
) && nk
== 0) {
3838 int flags
= PyCFunction_GET_FLAGS(func
);
3839 PyThreadState
*tstate
= PyThreadState_GET();
3841 PCALL(PCALL_CFUNCTION
);
3842 if (flags
& (METH_NOARGS
| METH_O
)) {
3843 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
3844 PyObject
*self
= PyCFunction_GET_SELF(func
);
3845 if (flags
& METH_NOARGS
&& na
== 0) {
3846 C_TRACE(x
, (*meth
)(self
,NULL
));
3848 else if (flags
& METH_O
&& na
== 1) {
3849 PyObject
*arg
= EXT_POP(*pp_stack
);
3850 C_TRACE(x
, (*meth
)(self
,arg
));
3854 err_args(func
, flags
, na
);
3860 callargs
= load_args(pp_stack
, na
);
3861 READ_TIMESTAMP(*pintr0
);
3862 C_TRACE(x
, PyCFunction_Call(func
,callargs
,NULL
));
3863 READ_TIMESTAMP(*pintr1
);
3864 Py_XDECREF(callargs
);
3867 if (PyMethod_Check(func
) && PyMethod_GET_SELF(func
) != NULL
) {
3868 /* optimize access to bound methods */
3869 PyObject
*self
= PyMethod_GET_SELF(func
);
3870 PCALL(PCALL_METHOD
);
3871 PCALL(PCALL_BOUND_METHOD
);
3873 func
= PyMethod_GET_FUNCTION(func
);
3881 READ_TIMESTAMP(*pintr0
);
3882 if (PyFunction_Check(func
))
3883 x
= fast_function(func
, pp_stack
, n
, na
, nk
);
3885 x
= do_call(func
, pp_stack
, na
, nk
);
3886 READ_TIMESTAMP(*pintr1
);
3890 /* Clear the stack of the function object. Also removes
3891 the arguments in case they weren't consumed already
3892 (fast_function() and err_args() leave them on the stack).
3894 while ((*pp_stack
) > pfunc
) {
3895 w
= EXT_POP(*pp_stack
);
3902 /* The fast_function() function optimize calls for which no argument
3903 tuple is necessary; the objects are passed directly from the stack.
3904 For the simplest case -- a function that takes only positional
3905 arguments and is called with only positional arguments -- it
3906 inlines the most primitive frame setup code from
3907 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3908 done before evaluating the frame.
3912 fast_function(PyObject
*func
, PyObject
***pp_stack
, int n
, int na
, int nk
)
3914 PyCodeObject
*co
= (PyCodeObject
*)PyFunction_GET_CODE(func
);
3915 PyObject
*globals
= PyFunction_GET_GLOBALS(func
);
3916 PyObject
*argdefs
= PyFunction_GET_DEFAULTS(func
);
3917 PyObject
**d
= NULL
;
3920 PCALL(PCALL_FUNCTION
);
3921 PCALL(PCALL_FAST_FUNCTION
);
3922 if (argdefs
== NULL
&& co
->co_argcount
== n
&& nk
==0 &&
3923 co
->co_flags
== (CO_OPTIMIZED
| CO_NEWLOCALS
| CO_NOFREE
)) {
3925 PyObject
*retval
= NULL
;
3926 PyThreadState
*tstate
= PyThreadState_GET();
3927 PyObject
**fastlocals
, **stack
;
3930 PCALL(PCALL_FASTER_FUNCTION
);
3931 assert(globals
!= NULL
);
3932 /* XXX Perhaps we should create a specialized
3933 PyFrame_New() that doesn't take locals, but does
3934 take builtins without sanity checking them.
3936 assert(tstate
!= NULL
);
3937 f
= PyFrame_New(tstate
, co
, globals
, NULL
);
3941 fastlocals
= f
->f_localsplus
;
3942 stack
= (*pp_stack
) - n
;
3944 for (i
= 0; i
< n
; i
++) {
3946 fastlocals
[i
] = *stack
++;
3948 retval
= PyEval_EvalFrameEx(f
,0);
3949 ++tstate
->recursion_depth
;
3951 --tstate
->recursion_depth
;
3954 if (argdefs
!= NULL
) {
3955 d
= &PyTuple_GET_ITEM(argdefs
, 0);
3956 nd
= Py_SIZE(argdefs
);
3958 return PyEval_EvalCodeEx(co
, globals
,
3959 (PyObject
*)NULL
, (*pp_stack
)-n
, na
,
3960 (*pp_stack
)-2*nk
, nk
, d
, nd
,
3961 PyFunction_GET_CLOSURE(func
));
3965 update_keyword_args(PyObject
*orig_kwdict
, int nk
, PyObject
***pp_stack
,
3968 PyObject
*kwdict
= NULL
;
3969 if (orig_kwdict
== NULL
)
3970 kwdict
= PyDict_New();
3972 kwdict
= PyDict_Copy(orig_kwdict
);
3973 Py_DECREF(orig_kwdict
);
3979 PyObject
*value
= EXT_POP(*pp_stack
);
3980 PyObject
*key
= EXT_POP(*pp_stack
);
3981 if (PyDict_GetItem(kwdict
, key
) != NULL
) {
3982 PyErr_Format(PyExc_TypeError
,
3983 "%.200s%s got multiple values "
3984 "for keyword argument '%.200s'",
3985 PyEval_GetFuncName(func
),
3986 PyEval_GetFuncDesc(func
),
3987 PyString_AsString(key
));
3993 err
= PyDict_SetItem(kwdict
, key
, value
);
4005 update_star_args(int nstack
, int nstar
, PyObject
*stararg
,
4006 PyObject
***pp_stack
)
4008 PyObject
*callargs
, *w
;
4010 callargs
= PyTuple_New(nstack
+ nstar
);
4011 if (callargs
== NULL
) {
4016 for (i
= 0; i
< nstar
; i
++) {
4017 PyObject
*a
= PyTuple_GET_ITEM(stararg
, i
);
4019 PyTuple_SET_ITEM(callargs
, nstack
+ i
, a
);
4022 while (--nstack
>= 0) {
4023 w
= EXT_POP(*pp_stack
);
4024 PyTuple_SET_ITEM(callargs
, nstack
, w
);
4030 load_args(PyObject
***pp_stack
, int na
)
4032 PyObject
*args
= PyTuple_New(na
);
4038 w
= EXT_POP(*pp_stack
);
4039 PyTuple_SET_ITEM(args
, na
, w
);
4045 do_call(PyObject
*func
, PyObject
***pp_stack
, int na
, int nk
)
4047 PyObject
*callargs
= NULL
;
4048 PyObject
*kwdict
= NULL
;
4049 PyObject
*result
= NULL
;
4052 kwdict
= update_keyword_args(NULL
, nk
, pp_stack
, func
);
4056 callargs
= load_args(pp_stack
, na
);
4057 if (callargs
== NULL
)
4060 /* At this point, we have to look at the type of func to
4061 update the call stats properly. Do it here so as to avoid
4062 exposing the call stats machinery outside ceval.c
4064 if (PyFunction_Check(func
))
4065 PCALL(PCALL_FUNCTION
);
4066 else if (PyMethod_Check(func
))
4067 PCALL(PCALL_METHOD
);
4068 else if (PyType_Check(func
))
4073 result
= PyObject_Call(func
, callargs
, kwdict
);
4075 Py_XDECREF(callargs
);
4081 ext_do_call(PyObject
*func
, PyObject
***pp_stack
, int flags
, int na
, int nk
)
4084 PyObject
*callargs
= NULL
;
4085 PyObject
*stararg
= NULL
;
4086 PyObject
*kwdict
= NULL
;
4087 PyObject
*result
= NULL
;
4089 if (flags
& CALL_FLAG_KW
) {
4090 kwdict
= EXT_POP(*pp_stack
);
4091 if (!PyDict_Check(kwdict
)) {
4096 if (PyDict_Update(d
, kwdict
) != 0) {
4098 /* PyDict_Update raises attribute
4099 * error (percolated from an attempt
4100 * to get 'keys' attribute) instead of
4101 * a type error if its second argument
4104 if (PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4105 PyErr_Format(PyExc_TypeError
,
4106 "%.200s%.200s argument after ** "
4107 "must be a mapping, not %.200s",
4108 PyEval_GetFuncName(func
),
4109 PyEval_GetFuncDesc(func
),
4110 kwdict
->ob_type
->tp_name
);
4118 if (flags
& CALL_FLAG_VAR
) {
4119 stararg
= EXT_POP(*pp_stack
);
4120 if (!PyTuple_Check(stararg
)) {
4122 t
= PySequence_Tuple(stararg
);
4124 if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
4125 PyErr_Format(PyExc_TypeError
,
4126 "%.200s%.200s argument after * "
4127 "must be a sequence, not %200s",
4128 PyEval_GetFuncName(func
),
4129 PyEval_GetFuncDesc(func
),
4130 stararg
->ob_type
->tp_name
);
4137 nstar
= PyTuple_GET_SIZE(stararg
);
4140 kwdict
= update_keyword_args(kwdict
, nk
, pp_stack
, func
);
4144 callargs
= update_star_args(na
, nstar
, stararg
, pp_stack
);
4145 if (callargs
== NULL
)
4148 /* At this point, we have to look at the type of func to
4149 update the call stats properly. Do it here so as to avoid
4150 exposing the call stats machinery outside ceval.c
4152 if (PyFunction_Check(func
))
4153 PCALL(PCALL_FUNCTION
);
4154 else if (PyMethod_Check(func
))
4155 PCALL(PCALL_METHOD
);
4156 else if (PyType_Check(func
))
4161 result
= PyObject_Call(func
, callargs
, kwdict
);
4163 Py_XDECREF(callargs
);
4165 Py_XDECREF(stararg
);
4169 /* Extract a slice index from a PyInt or PyLong or an object with the
4170 nb_index slot defined, and store in *pi.
4171 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4172 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
4173 Return 0 on error, 1 on success.
4175 /* Note: If v is NULL, return success without storing into *pi. This
4176 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4177 called by the SLICE opcode with v and/or w equal to NULL.
4180 _PyEval_SliceIndex(PyObject
*v
, Py_ssize_t
*pi
)
4184 if (PyInt_Check(v
)) {
4185 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4186 however, it looks like it should be AsSsize_t.
4187 There should be a comment here explaining why.
4189 x
= PyInt_AS_LONG(v
);
4191 else if (PyIndex_Check(v
)) {
4192 x
= PyNumber_AsSsize_t(v
, NULL
);
4193 if (x
== -1 && PyErr_Occurred())
4197 PyErr_SetString(PyExc_TypeError
,
4198 "slice indices must be integers or "
4199 "None or have an __index__ method");
4208 #define ISINDEX(x) ((x) == NULL || \
4209 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
4212 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
4214 PyTypeObject
*tp
= u
->ob_type
;
4215 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4217 if (sq
&& sq
->sq_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4218 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4219 if (!_PyEval_SliceIndex(v
, &ilow
))
4221 if (!_PyEval_SliceIndex(w
, &ihigh
))
4223 return PySequence_GetSlice(u
, ilow
, ihigh
);
4226 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4227 if (slice
!= NULL
) {
4228 PyObject
*res
= PyObject_GetItem(u
, slice
);
4238 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
4241 PyTypeObject
*tp
= u
->ob_type
;
4242 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4244 if (sq
&& sq
->sq_ass_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4245 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4246 if (!_PyEval_SliceIndex(v
, &ilow
))
4248 if (!_PyEval_SliceIndex(w
, &ihigh
))
4251 return PySequence_DelSlice(u
, ilow
, ihigh
);
4253 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
4256 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4257 if (slice
!= NULL
) {
4260 res
= PyObject_SetItem(u
, slice
, x
);
4262 res
= PyObject_DelItem(u
, slice
);
4271 #define Py3kExceptionClass_Check(x) \
4272 (PyType_Check((x)) && \
4273 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4275 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4276 "BaseException is not allowed in 3.x"
4279 cmp_outcome(int op
, register PyObject
*v
, register PyObject
*w
)
4290 res
= PySequence_Contains(w
, v
);
4295 res
= PySequence_Contains(w
, v
);
4300 case PyCmp_EXC_MATCH
:
4301 if (PyTuple_Check(w
)) {
4302 Py_ssize_t i
, length
;
4303 length
= PyTuple_Size(w
);
4304 for (i
= 0; i
< length
; i
+= 1) {
4305 PyObject
*exc
= PyTuple_GET_ITEM(w
, i
);
4306 if (PyString_Check(exc
)) {
4308 ret_val
= PyErr_WarnEx(
4309 PyExc_DeprecationWarning
,
4310 "catching of string "
4311 "exceptions is deprecated", 1);
4315 else if (Py_Py3kWarningFlag
&&
4316 !PyTuple_Check(exc
) &&
4317 !Py3kExceptionClass_Check(exc
))
4320 ret_val
= PyErr_WarnEx(
4321 PyExc_DeprecationWarning
,
4322 CANNOT_CATCH_MSG
, 1);
4329 if (PyString_Check(w
)) {
4331 ret_val
= PyErr_WarnEx(
4332 PyExc_DeprecationWarning
,
4333 "catching of string "
4334 "exceptions is deprecated", 1);
4338 else if (Py_Py3kWarningFlag
&&
4339 !PyTuple_Check(w
) &&
4340 !Py3kExceptionClass_Check(w
))
4343 ret_val
= PyErr_WarnEx(
4344 PyExc_DeprecationWarning
,
4345 CANNOT_CATCH_MSG
, 1);
4350 res
= PyErr_GivenExceptionMatches(v
, w
);
4353 return PyObject_RichCompare(v
, w
, op
);
4355 v
= res
? Py_True
: Py_False
;
4361 import_from(PyObject
*v
, PyObject
*name
)
4365 x
= PyObject_GetAttr(v
, name
);
4366 if (x
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4367 PyErr_Format(PyExc_ImportError
,
4368 "cannot import name %.230s",
4369 PyString_AsString(name
));
4375 import_all_from(PyObject
*locals
, PyObject
*v
)
4377 PyObject
*all
= PyObject_GetAttrString(v
, "__all__");
4378 PyObject
*dict
, *name
, *value
;
4379 int skip_leading_underscores
= 0;
4383 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4384 return -1; /* Unexpected error */
4386 dict
= PyObject_GetAttrString(v
, "__dict__");
4388 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4390 PyErr_SetString(PyExc_ImportError
,
4391 "from-import-* object has no __dict__ and no __all__");
4394 all
= PyMapping_Keys(dict
);
4398 skip_leading_underscores
= 1;
4401 for (pos
= 0, err
= 0; ; pos
++) {
4402 name
= PySequence_GetItem(all
, pos
);
4404 if (!PyErr_ExceptionMatches(PyExc_IndexError
))
4410 if (skip_leading_underscores
&&
4411 PyString_Check(name
) &&
4412 PyString_AS_STRING(name
)[0] == '_')
4417 value
= PyObject_GetAttr(v
, name
);
4420 else if (PyDict_CheckExact(locals
))
4421 err
= PyDict_SetItem(locals
, name
, value
);
4423 err
= PyObject_SetItem(locals
, name
, value
);
4434 build_class(PyObject
*methods
, PyObject
*bases
, PyObject
*name
)
4436 PyObject
*metaclass
= NULL
, *result
, *base
;
4438 if (PyDict_Check(methods
))
4439 metaclass
= PyDict_GetItemString(methods
, "__metaclass__");
4440 if (metaclass
!= NULL
)
4441 Py_INCREF(metaclass
);
4442 else if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
4443 base
= PyTuple_GET_ITEM(bases
, 0);
4444 metaclass
= PyObject_GetAttrString(base
, "__class__");
4445 if (metaclass
== NULL
) {
4447 metaclass
= (PyObject
*)base
->ob_type
;
4448 Py_INCREF(metaclass
);
4452 PyObject
*g
= PyEval_GetGlobals();
4453 if (g
!= NULL
&& PyDict_Check(g
))
4454 metaclass
= PyDict_GetItemString(g
, "__metaclass__");
4455 if (metaclass
== NULL
)
4456 metaclass
= (PyObject
*) &PyClass_Type
;
4457 Py_INCREF(metaclass
);
4459 result
= PyObject_CallFunctionObjArgs(metaclass
, name
, bases
, methods
,
4461 Py_DECREF(metaclass
);
4462 if (result
== NULL
&& PyErr_ExceptionMatches(PyExc_TypeError
)) {
4463 /* A type error here likely means that the user passed
4464 in a base that was not a class (such the random module
4465 instead of the random.random type). Help them out with
4466 by augmenting the error message with more information.*/
4468 PyObject
*ptype
, *pvalue
, *ptraceback
;
4470 PyErr_Fetch(&ptype
, &pvalue
, &ptraceback
);
4471 if (PyString_Check(pvalue
)) {
4473 newmsg
= PyString_FromFormat(
4474 "Error when calling the metaclass bases\n"
4476 PyString_AS_STRING(pvalue
));
4477 if (newmsg
!= NULL
) {
4482 PyErr_Restore(ptype
, pvalue
, ptraceback
);
4488 exec_statement(PyFrameObject
*f
, PyObject
*prog
, PyObject
*globals
,
4495 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
4496 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
4497 /* Backward compatibility hack */
4498 globals
= PyTuple_GetItem(prog
, 1);
4500 locals
= PyTuple_GetItem(prog
, 2);
4501 prog
= PyTuple_GetItem(prog
, 0);
4503 if (globals
== Py_None
) {
4504 globals
= PyEval_GetGlobals();
4505 if (locals
== Py_None
) {
4506 locals
= PyEval_GetLocals();
4509 if (!globals
|| !locals
) {
4510 PyErr_SetString(PyExc_SystemError
,
4511 "globals and locals cannot be NULL");
4515 else if (locals
== Py_None
)
4517 if (!PyString_Check(prog
) &&
4518 #ifdef Py_USING_UNICODE
4519 !PyUnicode_Check(prog
) &&
4521 !PyCode_Check(prog
) &&
4522 !PyFile_Check(prog
)) {
4523 PyErr_SetString(PyExc_TypeError
,
4524 "exec: arg 1 must be a string, file, or code object");
4527 if (!PyDict_Check(globals
)) {
4528 PyErr_SetString(PyExc_TypeError
,
4529 "exec: arg 2 must be a dictionary or None");
4532 if (!PyMapping_Check(locals
)) {
4533 PyErr_SetString(PyExc_TypeError
,
4534 "exec: arg 3 must be a mapping or None");
4537 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
4538 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
4539 if (PyCode_Check(prog
)) {
4540 if (PyCode_GetNumFree((PyCodeObject
*)prog
) > 0) {
4541 PyErr_SetString(PyExc_TypeError
,
4542 "code object passed to exec may not contain free variables");
4545 v
= PyEval_EvalCode((PyCodeObject
*) prog
, globals
, locals
);
4547 else if (PyFile_Check(prog
)) {
4548 FILE *fp
= PyFile_AsFile(prog
);
4549 char *name
= PyString_AsString(PyFile_Name(prog
));
4554 if (PyEval_MergeCompilerFlags(&cf
))
4555 v
= PyRun_FileFlags(fp
, name
, Py_file_input
, globals
,
4558 v
= PyRun_File(fp
, name
, Py_file_input
, globals
,
4562 PyObject
*tmp
= NULL
;
4566 #ifdef Py_USING_UNICODE
4567 if (PyUnicode_Check(prog
)) {
4568 tmp
= PyUnicode_AsUTF8String(prog
);
4572 cf
.cf_flags
|= PyCF_SOURCE_IS_UTF8
;
4575 if (PyString_AsStringAndSize(prog
, &str
, NULL
))
4577 if (PyEval_MergeCompilerFlags(&cf
))
4578 v
= PyRun_StringFlags(str
, Py_file_input
, globals
,
4581 v
= PyRun_String(str
, Py_file_input
, globals
, locals
);
4585 PyFrame_LocalsToFast(f
, 0);
4593 format_exc_check_arg(PyObject
*exc
, char *format_str
, PyObject
*obj
)
4600 obj_str
= PyString_AsString(obj
);
4604 PyErr_Format(exc
, format_str
, obj_str
);
4608 string_concatenate(PyObject
*v
, PyObject
*w
,
4609 PyFrameObject
*f
, unsigned char *next_instr
)
4611 /* This function implements 'variable += expr' when both arguments
4613 Py_ssize_t v_len
= PyString_GET_SIZE(v
);
4614 Py_ssize_t w_len
= PyString_GET_SIZE(w
);
4615 Py_ssize_t new_len
= v_len
+ w_len
;
4617 PyErr_SetString(PyExc_OverflowError
,
4618 "strings are too large to concat");
4622 if (v
->ob_refcnt
== 2) {
4623 /* In the common case, there are 2 references to the value
4624 * stored in 'variable' when the += is performed: one on the
4625 * value stack (in 'v') and one still stored in the
4626 * 'variable'. We try to delete the variable now to reduce
4629 switch (*next_instr
) {
4632 int oparg
= PEEKARG();
4633 PyObject
**fastlocals
= f
->f_localsplus
;
4634 if (GETLOCAL(oparg
) == v
)
4635 SETLOCAL(oparg
, NULL
);
4640 PyObject
**freevars
= (f
->f_localsplus
+
4641 f
->f_code
->co_nlocals
);
4642 PyObject
*c
= freevars
[PEEKARG()];
4643 if (PyCell_GET(c
) == v
)
4644 PyCell_Set(c
, NULL
);
4649 PyObject
*names
= f
->f_code
->co_names
;
4650 PyObject
*name
= GETITEM(names
, PEEKARG());
4651 PyObject
*locals
= f
->f_locals
;
4652 if (PyDict_CheckExact(locals
) &&
4653 PyDict_GetItem(locals
, name
) == v
) {
4654 if (PyDict_DelItem(locals
, name
) != 0) {
4663 if (v
->ob_refcnt
== 1 && !PyString_CHECK_INTERNED(v
)) {
4664 /* Now we own the last reference to 'v', so we can resize it
4667 if (_PyString_Resize(&v
, new_len
) != 0) {
4668 /* XXX if _PyString_Resize() fails, 'v' has been
4669 * deallocated so it cannot be put back into
4670 * 'variable'. The MemoryError is raised when there
4671 * is no value in 'variable', which might (very
4672 * remotely) be a cause of incompatibilities.
4676 /* copy 'w' into the newly allocated area of 'v' */
4677 memcpy(PyString_AS_STRING(v
) + v_len
,
4678 PyString_AS_STRING(w
), w_len
);
4682 /* When in-place resizing is not an option. */
4683 PyString_Concat(&v
, w
);
4688 #ifdef DYNAMIC_EXECUTION_PROFILE
4691 getarray(long a
[256])
4694 PyObject
*l
= PyList_New(256);
4695 if (l
== NULL
) return NULL
;
4696 for (i
= 0; i
< 256; i
++) {
4697 PyObject
*x
= PyInt_FromLong(a
[i
]);
4702 PyList_SetItem(l
, i
, x
);
4704 for (i
= 0; i
< 256; i
++)
4710 _Py_GetDXProfile(PyObject
*self
, PyObject
*args
)
4713 return getarray(dxp
);
4716 PyObject
*l
= PyList_New(257);
4717 if (l
== NULL
) return NULL
;
4718 for (i
= 0; i
< 257; i
++) {
4719 PyObject
*x
= getarray(dxpairs
[i
]);
4724 PyList_SetItem(l
, i
, x
);