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 *);
131 #define NAME_ERROR_MSG \
132 "name '%.200s' is not defined"
133 #define GLOBAL_NAME_ERROR_MSG \
134 "global name '%.200s' is not defined"
135 #define UNBOUNDLOCAL_ERROR_MSG \
136 "local variable '%.200s' referenced before assignment"
137 #define UNBOUNDFREE_ERROR_MSG \
138 "free variable '%.200s' referenced before assignment" \
139 " in enclosing scope"
141 /* Dynamic execution profile */
142 #ifdef DYNAMIC_EXECUTION_PROFILE
144 static long dxpairs
[257][256];
145 #define dxp dxpairs[256]
147 static long dxp
[256];
151 /* Function call profile */
154 static int pcall
[PCALL_NUM
];
157 #define PCALL_FUNCTION 1
158 #define PCALL_FAST_FUNCTION 2
159 #define PCALL_FASTER_FUNCTION 3
160 #define PCALL_METHOD 4
161 #define PCALL_BOUND_METHOD 5
162 #define PCALL_CFUNCTION 6
164 #define PCALL_GENERATOR 8
165 #define PCALL_OTHER 9
168 /* Notes about the statistics
172 FAST_FUNCTION means no argument tuple needs to be created.
173 FASTER_FUNCTION means that the fast-path frame setup code is used.
175 If there is a method call where the call can be optimized by changing
176 the argument tuple and calling the function directly, it gets recorded
179 As a result, the relationship among the statistics appears to be
180 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
181 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
182 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
183 PCALL_METHOD > PCALL_BOUND_METHOD
186 #define PCALL(POS) pcall[POS]++
189 PyEval_GetCallStats(PyObject
*self
)
191 return Py_BuildValue("iiiiiiiiiii",
192 pcall
[0], pcall
[1], pcall
[2], pcall
[3],
193 pcall
[4], pcall
[5], pcall
[6], pcall
[7],
194 pcall
[8], pcall
[9], pcall
[10]);
200 PyEval_GetCallStats(PyObject
*self
)
213 #include "pythread.h"
215 static PyThread_type_lock interpreter_lock
= 0; /* This is the GIL */
216 static PyThread_type_lock pending_lock
= 0; /* for pending calls */
217 static long main_thread
= 0;
220 PyEval_ThreadsInitialized(void)
222 return interpreter_lock
!= 0;
226 PyEval_InitThreads(void)
228 if (interpreter_lock
)
230 interpreter_lock
= PyThread_allocate_lock();
231 PyThread_acquire_lock(interpreter_lock
, 1);
232 main_thread
= PyThread_get_thread_ident();
236 PyEval_AcquireLock(void)
238 PyThread_acquire_lock(interpreter_lock
, 1);
242 PyEval_ReleaseLock(void)
244 PyThread_release_lock(interpreter_lock
);
248 PyEval_AcquireThread(PyThreadState
*tstate
)
251 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
252 /* Check someone has called PyEval_InitThreads() to create the lock */
253 assert(interpreter_lock
);
254 PyThread_acquire_lock(interpreter_lock
, 1);
255 if (PyThreadState_Swap(tstate
) != NULL
)
257 "PyEval_AcquireThread: non-NULL old thread state");
261 PyEval_ReleaseThread(PyThreadState
*tstate
)
264 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
265 if (PyThreadState_Swap(NULL
) != tstate
)
266 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
267 PyThread_release_lock(interpreter_lock
);
270 /* This function is called from PyOS_AfterFork to ensure that newly
271 created child processes don't hold locks referring to threads which
272 are not running in the child process. (This could also be done using
273 pthread_atfork mechanism, at least for the pthreads implementation.) */
276 PyEval_ReInitThreads(void)
278 PyObject
*threading
, *result
;
279 PyThreadState
*tstate
;
281 if (!interpreter_lock
)
283 /*XXX Can't use PyThread_free_lock here because it does too
284 much error-checking. Doing this cleanly would require
285 adding a new function to each thread_*.h. Instead, just
286 create a new lock and waste a little bit of memory */
287 interpreter_lock
= PyThread_allocate_lock();
288 pending_lock
= PyThread_allocate_lock();
289 PyThread_acquire_lock(interpreter_lock
, 1);
290 main_thread
= PyThread_get_thread_ident();
292 /* Update the threading module with the new state.
294 tstate
= PyThreadState_GET();
295 threading
= PyMapping_GetItemString(tstate
->interp
->modules
,
297 if (threading
== NULL
) {
298 /* threading not imported */
302 result
= PyObject_CallMethod(threading
, "_after_fork", NULL
);
304 PyErr_WriteUnraisable(threading
);
307 Py_DECREF(threading
);
311 /* Functions save_thread and restore_thread are always defined so
312 dynamically loaded modules needn't be compiled separately for use
313 with and without threads: */
316 PyEval_SaveThread(void)
318 PyThreadState
*tstate
= PyThreadState_Swap(NULL
);
320 Py_FatalError("PyEval_SaveThread: NULL tstate");
322 if (interpreter_lock
)
323 PyThread_release_lock(interpreter_lock
);
329 PyEval_RestoreThread(PyThreadState
*tstate
)
332 Py_FatalError("PyEval_RestoreThread: NULL tstate");
334 if (interpreter_lock
) {
336 PyThread_acquire_lock(interpreter_lock
, 1);
340 PyThreadState_Swap(tstate
);
344 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
345 signal handlers or Mac I/O completion routines) can schedule calls
346 to a function to be called synchronously.
347 The synchronous function is called with one void* argument.
348 It should return 0 for success or -1 for failure -- failure should
349 be accompanied by an exception.
351 If registry succeeds, the registry function returns 0; if it fails
352 (e.g. due to too many pending calls) it returns -1 (without setting
353 an exception condition).
355 Note that because registry may occur from within signal handlers,
356 or other asynchronous events, calling malloc() is unsafe!
359 Any thread can schedule pending calls, but only the main thread
361 There is no facility to schedule calls to a particular thread, but
362 that should be easy to change, should that ever be required. In
363 that case, the static variables here should go into the python
370 /* The WITH_THREAD implementation is thread-safe. It allows
371 scheduling to be made from any thread, and even from an executing
375 #define NPENDINGCALLS 32
379 } pendingcalls
[NPENDINGCALLS
];
380 static int pendingfirst
= 0;
381 static int pendinglast
= 0;
382 static volatile int pendingcalls_to_do
= 1; /* trigger initialization of lock */
383 static char pendingbusy
= 0;
386 Py_AddPendingCall(int (*func
)(void *), void *arg
)
389 PyThread_type_lock lock
= pending_lock
;
391 /* try a few times for the lock. Since this mechanism is used
392 * for signal handling (on the main thread), there is a (slim)
393 * chance that a signal is delivered on the same thread while we
394 * hold the lock during the Py_MakePendingCalls() function.
395 * This avoids a deadlock in that case.
396 * Note that signals can be delivered on any thread. In particular,
397 * on Windows, a SIGINT is delivered on a system-created worker
399 * We also check for lock being NULL, in the unlikely case that
400 * this function is called before any bytecode evaluation takes place.
403 for (i
= 0; i
<100; i
++) {
404 if (PyThread_acquire_lock(lock
, NOWAIT_LOCK
))
412 j
= (i
+ 1) % NPENDINGCALLS
;
413 if (j
== pendingfirst
) {
414 result
= -1; /* Queue full */
416 pendingcalls
[i
].func
= func
;
417 pendingcalls
[i
].arg
= arg
;
420 /* signal main loop */
422 pendingcalls_to_do
= 1;
424 PyThread_release_lock(lock
);
429 Py_MakePendingCalls(void)
435 /* initial allocation of the lock */
436 pending_lock
= PyThread_allocate_lock();
437 if (pending_lock
== NULL
)
441 /* only service pending calls on main thread */
442 if (main_thread
&& PyThread_get_thread_ident() != main_thread
)
444 /* don't perform recursive pending calls */
448 /* perform a bounded number of calls, in case of recursion */
449 for (i
=0; i
<NPENDINGCALLS
; i
++) {
454 /* pop one item off the queue while holding the lock */
455 PyThread_acquire_lock(pending_lock
, WAIT_LOCK
);
457 if (j
== pendinglast
) {
458 func
= NULL
; /* Queue empty */
460 func
= pendingcalls
[j
].func
;
461 arg
= pendingcalls
[j
].arg
;
462 pendingfirst
= (j
+ 1) % NPENDINGCALLS
;
464 pendingcalls_to_do
= pendingfirst
!= pendinglast
;
465 PyThread_release_lock(pending_lock
);
466 /* having released the lock, perform the callback */
477 #else /* if ! defined WITH_THREAD */
480 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
481 This code is used for signal handling in python that isn't built
483 Don't use this implementation when Py_AddPendingCalls() can happen
484 on a different thread!
486 There are two possible race conditions:
487 (1) nested asynchronous calls to Py_AddPendingCall()
488 (2) AddPendingCall() calls made while pending calls are being processed.
490 (1) is very unlikely because typically signal delivery
491 is blocked during signal handling. So it should be impossible.
492 (2) is a real possibility.
493 The current code is safe against (2), but not against (1).
494 The safety against (2) is derived from the fact that only one
495 thread is present, interrupted by signals, and that the critical
496 section is protected with the "busy" variable. On Windows, which
497 delivers SIGINT on a system thread, this does not hold and therefore
498 Windows really shouldn't use this version.
499 The two threads could theoretically wiggle around the "busy" variable.
502 #define NPENDINGCALLS 32
506 } pendingcalls
[NPENDINGCALLS
];
507 static volatile int pendingfirst
= 0;
508 static volatile int pendinglast
= 0;
509 static volatile int pendingcalls_to_do
= 0;
512 Py_AddPendingCall(int (*func
)(void *), void *arg
)
514 static volatile int busy
= 0;
516 /* XXX Begin critical section */
521 j
= (i
+ 1) % NPENDINGCALLS
;
522 if (j
== pendingfirst
) {
524 return -1; /* Queue full */
526 pendingcalls
[i
].func
= func
;
527 pendingcalls
[i
].arg
= arg
;
531 pendingcalls_to_do
= 1; /* Signal main loop */
533 /* XXX End critical section */
538 Py_MakePendingCalls(void)
544 pendingcalls_to_do
= 0;
550 if (i
== pendinglast
)
551 break; /* Queue empty */
552 func
= pendingcalls
[i
].func
;
553 arg
= pendingcalls
[i
].arg
;
554 pendingfirst
= (i
+ 1) % NPENDINGCALLS
;
557 pendingcalls_to_do
= 1; /* We're not done yet */
565 #endif /* WITH_THREAD */
568 /* The interpreter's recursion limit */
570 #ifndef Py_DEFAULT_RECURSION_LIMIT
571 #define Py_DEFAULT_RECURSION_LIMIT 1000
573 static int recursion_limit
= Py_DEFAULT_RECURSION_LIMIT
;
574 int _Py_CheckRecursionLimit
= Py_DEFAULT_RECURSION_LIMIT
;
577 Py_GetRecursionLimit(void)
579 return recursion_limit
;
583 Py_SetRecursionLimit(int new_limit
)
585 recursion_limit
= new_limit
;
586 _Py_CheckRecursionLimit
= recursion_limit
;
589 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
590 if the recursion_depth reaches _Py_CheckRecursionLimit.
591 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
592 to guarantee that _Py_CheckRecursiveCall() is regularly called.
593 Without USE_STACKCHECK, there is no need for this. */
595 _Py_CheckRecursiveCall(char *where
)
597 PyThreadState
*tstate
= PyThreadState_GET();
599 #ifdef USE_STACKCHECK
600 if (PyOS_CheckStack()) {
601 --tstate
->recursion_depth
;
602 PyErr_SetString(PyExc_MemoryError
, "Stack overflow");
606 if (tstate
->recursion_depth
> recursion_limit
) {
607 --tstate
->recursion_depth
;
608 PyErr_Format(PyExc_RuntimeError
,
609 "maximum recursion depth exceeded%s",
613 _Py_CheckRecursionLimit
= recursion_limit
;
617 /* Status code for main loop (reason for stack unwind) */
619 WHY_NOT
= 0x0001, /* No error */
620 WHY_EXCEPTION
= 0x0002, /* Exception occurred */
621 WHY_RERAISE
= 0x0004, /* Exception re-raised by 'finally' */
622 WHY_RETURN
= 0x0008, /* 'return' statement */
623 WHY_BREAK
= 0x0010, /* 'break' statement */
624 WHY_CONTINUE
= 0x0020, /* 'continue' statement */
625 WHY_YIELD
= 0x0040 /* 'yield' operator */
628 static enum why_code
do_raise(PyObject
*, PyObject
*, PyObject
*);
629 static int unpack_iterable(PyObject
*, int, PyObject
**);
631 /* Records whether tracing is on for any thread. Counts the number of
632 threads for which tstate->c_tracefunc is non-NULL, so if the value
633 is 0, we know we don't have to check this thread's c_tracefunc.
634 This speeds up the if statement in PyEval_EvalFrameEx() after
636 static int _Py_TracingPossible
= 0;
638 /* for manipulating the thread switch and periodic "stuff" - used to be
639 per thread, now just a pair o' globals */
640 int _Py_CheckInterval
= 100;
641 volatile int _Py_Ticker
= 0; /* so that we hit a "tick" first thing */
644 PyEval_EvalCode(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
)
646 return PyEval_EvalCodeEx(co
,
648 (PyObject
**)NULL
, 0,
649 (PyObject
**)NULL
, 0,
650 (PyObject
**)NULL
, 0,
655 /* Interpreter main loop */
658 PyEval_EvalFrame(PyFrameObject
*f
) {
659 /* This is for backward compatibility with extension modules that
660 used this API; core interpreter code should call
661 PyEval_EvalFrameEx() */
662 return PyEval_EvalFrameEx(f
, 0);
666 PyEval_EvalFrameEx(PyFrameObject
*f
, int throwflag
)
671 register PyObject
**stack_pointer
; /* Next free slot in value stack */
672 register unsigned char *next_instr
;
673 register int opcode
; /* Current opcode */
674 register int oparg
; /* Current opcode argument, if any */
675 register enum why_code why
; /* Reason for block stack unwind */
676 register int err
; /* Error status -- nonzero if error */
677 register PyObject
*x
; /* Result object -- NULL if error */
678 register PyObject
*v
; /* Temporary objects popped off stack */
679 register PyObject
*w
;
680 register PyObject
*u
;
681 register PyObject
*t
;
682 register PyObject
*stream
= NULL
; /* for PRINT opcodes */
683 register PyObject
**fastlocals
, **freevars
;
684 PyObject
*retval
= NULL
; /* Return value */
685 PyThreadState
*tstate
= PyThreadState_GET();
688 /* when tracing we set things up so that
690 not (instr_lb <= current_bytecode_offset < instr_ub)
692 is true when the line being executed has changed. The
693 initial values are such as to make this false the first
694 time it is tested. */
695 int instr_ub
= -1, instr_lb
= 0, instr_prev
= -1;
697 unsigned char *first_instr
;
700 #if defined(Py_DEBUG) || defined(LLTRACE)
701 /* Make it easier to find out where we are with a debugger */
705 /* Tuple access macros */
708 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
710 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
714 /* Use Pentium timestamp counter to mark certain events:
715 inst0 -- beginning of switch statement for opcode dispatch
716 inst1 -- end of switch statement (may be skipped)
717 loop0 -- the top of the mainloop
718 loop1 -- place where control returns again to top of mainloop
720 intr1 -- beginning of long interruption
721 intr2 -- end of long interruption
723 Many opcodes call out to helper C functions. In some cases, the
724 time in those functions should be counted towards the time for the
725 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
726 calls another Python function; there's no point in charge all the
727 bytecode executed by the called function to the caller.
729 It's hard to make a useful judgement statically. In the presence
730 of operator overloading, it's impossible to tell if a call will
731 execute new Python code or not.
733 It's a case-by-case judgement. I'll use intr1 for the following
739 CALL_FUNCTION (and friends)
742 uint64 inst0
, inst1
, loop0
, loop1
, intr0
= 0, intr1
= 0;
745 READ_TIMESTAMP(inst0
);
746 READ_TIMESTAMP(inst1
);
747 READ_TIMESTAMP(loop0
);
748 READ_TIMESTAMP(loop1
);
750 /* shut up the compiler */
754 /* Code access macros */
756 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
757 #define NEXTOP() (*next_instr++)
758 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
759 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
760 #define JUMPTO(x) (next_instr = first_instr + (x))
761 #define JUMPBY(x) (next_instr += (x))
763 /* OpCode prediction macros
764 Some opcodes tend to come in pairs thus making it possible to
765 predict the second code when the first is run. For example,
766 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
767 those opcodes are often followed by a POP_TOP.
769 Verifying the prediction costs a single high-speed test of a register
770 variable against a constant. If the pairing was good, then the
771 processor's own internal branch predication has a high likelihood of
772 success, resulting in a nearly zero-overhead transition to the
773 next opcode. A successful prediction saves a trip through the eval-loop
774 including its two unpredictable branches, the HAS_ARG test and the
775 switch-case. Combined with the processor's internal branch prediction,
776 a successful PREDICT has the effect of making the two opcodes run as if
777 they were a single new opcode with the bodies combined.
779 If collecting opcode statistics, your choices are to either keep the
780 predictions turned-on and interpret the results as if some opcodes
781 had been combined or turn-off predictions so that the opcode frequency
782 counter updates for both opcodes.
785 #ifdef DYNAMIC_EXECUTION_PROFILE
786 #define PREDICT(op) if (0) goto PRED_##op
788 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
791 #define PREDICTED(op) PRED_##op: next_instr++
792 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
794 /* Stack manipulation macros */
796 /* The stack can grow at most MAXINT deep, as co_nlocals and
797 co_stacksize are ints. */
798 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
799 #define EMPTY() (STACK_LEVEL() == 0)
800 #define TOP() (stack_pointer[-1])
801 #define SECOND() (stack_pointer[-2])
802 #define THIRD() (stack_pointer[-3])
803 #define FOURTH() (stack_pointer[-4])
804 #define SET_TOP(v) (stack_pointer[-1] = (v))
805 #define SET_SECOND(v) (stack_pointer[-2] = (v))
806 #define SET_THIRD(v) (stack_pointer[-3] = (v))
807 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
808 #define BASIC_STACKADJ(n) (stack_pointer += n)
809 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
810 #define BASIC_POP() (*--stack_pointer)
813 #define PUSH(v) { (void)(BASIC_PUSH(v), \
814 lltrace && prtrace(TOP(), "push")); \
815 assert(STACK_LEVEL() <= co->co_stacksize); }
816 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
818 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
819 lltrace && prtrace(TOP(), "stackadj")); \
820 assert(STACK_LEVEL() <= co->co_stacksize); }
821 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
822 prtrace((STACK_POINTER)[-1], "ext_pop")), \
825 #define PUSH(v) BASIC_PUSH(v)
826 #define POP() BASIC_POP()
827 #define STACKADJ(n) BASIC_STACKADJ(n)
828 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
831 /* Local variable macros */
833 #define GETLOCAL(i) (fastlocals[i])
835 /* The SETLOCAL() macro must not DECREF the local variable in-place and
836 then store the new value; it must copy the old value to a temporary
837 value, then store the new value, and then DECREF the temporary value.
838 This is because it is possible that during the DECREF the frame is
839 accessed by other code (e.g. a __del__ method or gc.collect()) and the
840 variable would be pointing to already-freed memory. */
841 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
842 GETLOCAL(i) = value; \
843 Py_XDECREF(tmp); } while (0)
851 if (Py_EnterRecursiveCall(""))
856 if (tstate
->use_tracing
) {
857 if (tstate
->c_tracefunc
!= NULL
) {
858 /* tstate->c_tracefunc, if defined, is a
859 function that will be called on *every* entry
860 to a code block. Its return value, if not
861 None, is a function that will be called at
862 the start of each executed line of code.
863 (Actually, the function must return itself
864 in order to continue tracing.) The trace
865 functions are called with three arguments:
866 a pointer to the current frame, a string
867 indicating why the function is called, and
868 an argument which depends on the situation.
869 The global trace function is also called
870 whenever an exception is detected. */
871 if (call_trace_protected(tstate
->c_tracefunc
,
873 f
, PyTrace_CALL
, Py_None
)) {
874 /* Trace function raised an error */
875 goto exit_eval_frame
;
878 if (tstate
->c_profilefunc
!= NULL
) {
879 /* Similar for c_profilefunc, except it needn't
880 return itself and isn't called for "line" events */
881 if (call_trace_protected(tstate
->c_profilefunc
,
882 tstate
->c_profileobj
,
883 f
, PyTrace_CALL
, Py_None
)) {
884 /* Profile function raised an error */
885 goto exit_eval_frame
;
891 names
= co
->co_names
;
892 consts
= co
->co_consts
;
893 fastlocals
= f
->f_localsplus
;
894 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
895 first_instr
= (unsigned char*) PyString_AS_STRING(co
->co_code
);
896 /* An explanation is in order for the next line.
898 f->f_lasti now refers to the index of the last instruction
899 executed. You might think this was obvious from the name, but
900 this wasn't always true before 2.3! PyFrame_New now sets
901 f->f_lasti to -1 (i.e. the index *before* the first instruction)
902 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
905 When the PREDICT() macros are enabled, some opcode pairs follow in
906 direct succession without updating f->f_lasti. A successful
907 prediction effectively links the two codes together as if they
908 were a single new opcode; accordingly,f->f_lasti will point to
909 the first code in the pair (for instance, GET_ITER followed by
910 FOR_ITER is effectively a single opcode and f->f_lasti will point
911 at to the beginning of the combined pair.)
913 next_instr
= first_instr
+ f
->f_lasti
+ 1;
914 stack_pointer
= f
->f_stacktop
;
915 assert(stack_pointer
!= NULL
);
916 f
->f_stacktop
= NULL
; /* remains NULL unless yield suspends frame */
919 lltrace
= PyDict_GetItemString(f
->f_globals
, "__lltrace__") != NULL
;
921 #if defined(Py_DEBUG) || defined(LLTRACE)
922 filename
= PyString_AsString(co
->co_filename
);
927 x
= Py_None
; /* Not a reference, just anything non-NULL */
930 if (throwflag
) { /* support for generator.throw() */
938 /* Almost surely, the opcode executed a break
939 or a continue, preventing inst1 from being set
940 on the way out of the loop.
942 READ_TIMESTAMP(inst1
);
945 dump_tsc(opcode
, ticked
, inst0
, inst1
, loop0
, loop1
,
951 READ_TIMESTAMP(loop0
);
953 assert(stack_pointer
>= f
->f_valuestack
); /* else underflow */
954 assert(STACK_LEVEL() <= co
->co_stacksize
); /* else overflow */
956 /* Do periodic things. Doing this every time through
957 the loop would add too much overhead, so we do it
958 only every Nth instruction. We also do it if
959 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
960 event needs attention (e.g. a signal handler or
961 async I/O handler); see Py_AddPendingCall() and
962 Py_MakePendingCalls() above. */
964 if (--_Py_Ticker
< 0) {
965 if (*next_instr
== SETUP_FINALLY
) {
966 /* Make the last opcode before
967 a try: finally: block uninterruptable. */
968 goto fast_next_opcode
;
970 _Py_Ticker
= _Py_CheckInterval
;
971 tstate
->tick_counter
++;
975 if (pendingcalls_to_do
) {
976 if (Py_MakePendingCalls() < 0) {
980 if (pendingcalls_to_do
)
981 /* MakePendingCalls() didn't succeed.
982 Force early re-execution of this
983 "periodic" code, possibly after
988 if (interpreter_lock
) {
989 /* Give another thread a chance */
991 if (PyThreadState_Swap(NULL
) != tstate
)
992 Py_FatalError("ceval: tstate mix-up");
993 PyThread_release_lock(interpreter_lock
);
995 /* Other threads may run now */
997 PyThread_acquire_lock(interpreter_lock
, 1);
998 if (PyThreadState_Swap(tstate
) != NULL
)
999 Py_FatalError("ceval: orphan tstate");
1001 /* Check for thread interrupts */
1003 if (tstate
->async_exc
!= NULL
) {
1004 x
= tstate
->async_exc
;
1005 tstate
->async_exc
= NULL
;
1008 why
= WHY_EXCEPTION
;
1016 f
->f_lasti
= INSTR_OFFSET();
1018 /* line-by-line tracing support */
1020 if (_Py_TracingPossible
&&
1021 tstate
->c_tracefunc
!= NULL
&& !tstate
->tracing
) {
1022 /* see maybe_call_line_trace
1023 for expository comments */
1024 f
->f_stacktop
= stack_pointer
;
1026 err
= maybe_call_line_trace(tstate
->c_tracefunc
,
1028 f
, &instr_lb
, &instr_ub
,
1030 /* Reload possibly changed frame fields */
1032 if (f
->f_stacktop
!= NULL
) {
1033 stack_pointer
= f
->f_stacktop
;
1034 f
->f_stacktop
= NULL
;
1037 /* trace function raised an exception */
1042 /* Extract opcode and argument */
1045 oparg
= 0; /* allows oparg to be stored in a register because
1046 it doesn't have to be remembered across a full loop */
1047 if (HAS_ARG(opcode
))
1050 #ifdef DYNAMIC_EXECUTION_PROFILE
1052 dxpairs
[lastopcode
][opcode
]++;
1053 lastopcode
= opcode
;
1059 /* Instruction tracing */
1062 if (HAS_ARG(opcode
)) {
1063 printf("%d: %d, %d\n",
1064 f
->f_lasti
, opcode
, oparg
);
1068 f
->f_lasti
, opcode
);
1073 /* Main switch on opcode */
1074 READ_TIMESTAMP(inst0
);
1079 It is essential that any operation that fails sets either
1080 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1081 and that no operation that succeeds does this! */
1083 /* case STOP_CODE: this is an error! */
1086 goto fast_next_opcode
;
1089 x
= GETLOCAL(oparg
);
1093 goto fast_next_opcode
;
1095 format_exc_check_arg(PyExc_UnboundLocalError
,
1096 UNBOUNDLOCAL_ERROR_MSG
,
1097 PyTuple_GetItem(co
->co_varnames
, oparg
));
1101 x
= GETITEM(consts
, oparg
);
1104 goto fast_next_opcode
;
1106 PREDICTED_WITH_ARG(STORE_FAST
);
1110 goto fast_next_opcode
;
1116 goto fast_next_opcode
;
1123 goto fast_next_opcode
;
1132 goto fast_next_opcode
;
1143 goto fast_next_opcode
;
1149 goto fast_next_opcode
;
1160 goto fast_next_opcode
;
1161 } else if (oparg
== 3) {
1172 goto fast_next_opcode
;
1174 Py_FatalError("invalid argument to DUP_TOPX"
1175 " (bytecode corruption?)");
1176 /* Never returns, so don't bother to set why. */
1179 case UNARY_POSITIVE
:
1181 x
= PyNumber_Positive(v
);
1184 if (x
!= NULL
) continue;
1187 case UNARY_NEGATIVE
:
1189 x
= PyNumber_Negative(v
);
1192 if (x
!= NULL
) continue;
1197 err
= PyObject_IsTrue(v
);
1205 Py_INCREF(Py_False
);
1215 x
= PyObject_Repr(v
);
1218 if (x
!= NULL
) continue;
1223 x
= PyNumber_Invert(v
);
1226 if (x
!= NULL
) continue;
1232 x
= PyNumber_Power(v
, w
, Py_None
);
1236 if (x
!= NULL
) continue;
1239 case BINARY_MULTIPLY
:
1242 x
= PyNumber_Multiply(v
, w
);
1246 if (x
!= NULL
) continue;
1250 if (!_Py_QnewFlag
) {
1253 x
= PyNumber_Divide(v
, w
);
1257 if (x
!= NULL
) continue;
1260 /* -Qnew is in effect: fall through to
1261 BINARY_TRUE_DIVIDE */
1262 case BINARY_TRUE_DIVIDE
:
1265 x
= PyNumber_TrueDivide(v
, w
);
1269 if (x
!= NULL
) continue;
1272 case BINARY_FLOOR_DIVIDE
:
1275 x
= PyNumber_FloorDivide(v
, w
);
1279 if (x
!= NULL
) continue;
1285 x
= PyNumber_Remainder(v
, w
);
1289 if (x
!= NULL
) continue;
1295 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1296 /* INLINE: int + int */
1297 register long a
, b
, i
;
1298 a
= PyInt_AS_LONG(v
);
1299 b
= PyInt_AS_LONG(w
);
1301 if ((i
^a
) < 0 && (i
^b
) < 0)
1303 x
= PyInt_FromLong(i
);
1305 else if (PyString_CheckExact(v
) &&
1306 PyString_CheckExact(w
)) {
1307 x
= string_concatenate(v
, w
, f
, next_instr
);
1308 /* string_concatenate consumed the ref to v */
1309 goto skip_decref_vx
;
1313 x
= PyNumber_Add(v
, w
);
1319 if (x
!= NULL
) continue;
1322 case BINARY_SUBTRACT
:
1325 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1326 /* INLINE: int - int */
1327 register long a
, b
, i
;
1328 a
= PyInt_AS_LONG(v
);
1329 b
= PyInt_AS_LONG(w
);
1331 if ((i
^a
) < 0 && (i
^~b
) < 0)
1333 x
= PyInt_FromLong(i
);
1337 x
= PyNumber_Subtract(v
, w
);
1342 if (x
!= NULL
) continue;
1348 if (PyList_CheckExact(v
) && PyInt_CheckExact(w
)) {
1349 /* INLINE: list[int] */
1350 Py_ssize_t i
= PyInt_AsSsize_t(w
);
1352 i
+= PyList_GET_SIZE(v
);
1353 if (i
>= 0 && i
< PyList_GET_SIZE(v
)) {
1354 x
= PyList_GET_ITEM(v
, i
);
1362 x
= PyObject_GetItem(v
, w
);
1366 if (x
!= NULL
) continue;
1372 x
= PyNumber_Lshift(v
, w
);
1376 if (x
!= NULL
) continue;
1382 x
= PyNumber_Rshift(v
, w
);
1386 if (x
!= NULL
) continue;
1392 x
= PyNumber_And(v
, w
);
1396 if (x
!= NULL
) continue;
1402 x
= PyNumber_Xor(v
, w
);
1406 if (x
!= NULL
) continue;
1412 x
= PyNumber_Or(v
, w
);
1416 if (x
!= NULL
) continue;
1421 v
= stack_pointer
[-oparg
];
1422 err
= PyList_Append(v
, w
);
1425 PREDICT(JUMP_ABSOLUTE
);
1433 x
= PyNumber_InPlacePower(v
, w
, Py_None
);
1437 if (x
!= NULL
) continue;
1440 case INPLACE_MULTIPLY
:
1443 x
= PyNumber_InPlaceMultiply(v
, w
);
1447 if (x
!= NULL
) continue;
1450 case INPLACE_DIVIDE
:
1451 if (!_Py_QnewFlag
) {
1454 x
= PyNumber_InPlaceDivide(v
, w
);
1458 if (x
!= NULL
) continue;
1461 /* -Qnew is in effect: fall through to
1462 INPLACE_TRUE_DIVIDE */
1463 case INPLACE_TRUE_DIVIDE
:
1466 x
= PyNumber_InPlaceTrueDivide(v
, w
);
1470 if (x
!= NULL
) continue;
1473 case INPLACE_FLOOR_DIVIDE
:
1476 x
= PyNumber_InPlaceFloorDivide(v
, w
);
1480 if (x
!= NULL
) continue;
1483 case INPLACE_MODULO
:
1486 x
= PyNumber_InPlaceRemainder(v
, w
);
1490 if (x
!= NULL
) continue;
1496 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1497 /* INLINE: int + int */
1498 register long a
, b
, i
;
1499 a
= PyInt_AS_LONG(v
);
1500 b
= PyInt_AS_LONG(w
);
1502 if ((i
^a
) < 0 && (i
^b
) < 0)
1504 x
= PyInt_FromLong(i
);
1506 else if (PyString_CheckExact(v
) &&
1507 PyString_CheckExact(w
)) {
1508 x
= string_concatenate(v
, w
, f
, next_instr
);
1509 /* string_concatenate consumed the ref to v */
1514 x
= PyNumber_InPlaceAdd(v
, w
);
1520 if (x
!= NULL
) continue;
1523 case INPLACE_SUBTRACT
:
1526 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1527 /* INLINE: int - int */
1528 register long a
, b
, i
;
1529 a
= PyInt_AS_LONG(v
);
1530 b
= PyInt_AS_LONG(w
);
1532 if ((i
^a
) < 0 && (i
^~b
) < 0)
1534 x
= PyInt_FromLong(i
);
1538 x
= PyNumber_InPlaceSubtract(v
, w
);
1543 if (x
!= NULL
) continue;
1546 case INPLACE_LSHIFT
:
1549 x
= PyNumber_InPlaceLshift(v
, w
);
1553 if (x
!= NULL
) continue;
1556 case INPLACE_RSHIFT
:
1559 x
= PyNumber_InPlaceRshift(v
, w
);
1563 if (x
!= NULL
) continue;
1569 x
= PyNumber_InPlaceAnd(v
, w
);
1573 if (x
!= NULL
) continue;
1579 x
= PyNumber_InPlaceXor(v
, w
);
1583 if (x
!= NULL
) continue;
1589 x
= PyNumber_InPlaceOr(v
, w
);
1593 if (x
!= NULL
) continue;
1600 if ((opcode
-SLICE
) & 2)
1604 if ((opcode
-SLICE
) & 1)
1609 x
= apply_slice(u
, v
, w
);
1614 if (x
!= NULL
) continue;
1621 if ((opcode
-STORE_SLICE
) & 2)
1625 if ((opcode
-STORE_SLICE
) & 1)
1631 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
1636 if (err
== 0) continue;
1639 case DELETE_SLICE
+0:
1640 case DELETE_SLICE
+1:
1641 case DELETE_SLICE
+2:
1642 case DELETE_SLICE
+3:
1643 if ((opcode
-DELETE_SLICE
) & 2)
1647 if ((opcode
-DELETE_SLICE
) & 1)
1652 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
1657 if (err
== 0) continue;
1666 err
= PyObject_SetItem(v
, w
, u
);
1670 if (err
== 0) continue;
1678 err
= PyObject_DelItem(v
, w
);
1681 if (err
== 0) continue;
1686 w
= PySys_GetObject("displayhook");
1688 PyErr_SetString(PyExc_RuntimeError
,
1689 "lost sys.displayhook");
1694 x
= PyTuple_Pack(1, v
);
1699 w
= PyEval_CallObject(w
, x
);
1710 /* fall through to PRINT_ITEM */
1714 if (stream
== NULL
|| stream
== Py_None
) {
1715 w
= PySys_GetObject("stdout");
1717 PyErr_SetString(PyExc_RuntimeError
,
1722 /* PyFile_SoftSpace() can exececute arbitrary code
1723 if sys.stdout is an instance with a __getattr__.
1724 If __getattr__ raises an exception, w will
1725 be freed, so we need to prevent that temporarily. */
1727 if (w
!= NULL
&& PyFile_SoftSpace(w
, 0))
1728 err
= PyFile_WriteString(" ", w
);
1730 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1732 /* XXX move into writeobject() ? */
1733 if (PyString_Check(v
)) {
1734 char *s
= PyString_AS_STRING(v
);
1735 Py_ssize_t len
= PyString_GET_SIZE(v
);
1737 !isspace(Py_CHARMASK(s
[len
-1])) ||
1739 PyFile_SoftSpace(w
, 1);
1741 #ifdef Py_USING_UNICODE
1742 else if (PyUnicode_Check(v
)) {
1743 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(v
);
1744 Py_ssize_t len
= PyUnicode_GET_SIZE(v
);
1746 !Py_UNICODE_ISSPACE(s
[len
-1]) ||
1748 PyFile_SoftSpace(w
, 1);
1752 PyFile_SoftSpace(w
, 1);
1762 case PRINT_NEWLINE_TO
:
1764 /* fall through to PRINT_NEWLINE */
1767 if (stream
== NULL
|| stream
== Py_None
) {
1768 w
= PySys_GetObject("stdout");
1770 PyErr_SetString(PyExc_RuntimeError
,
1772 why
= WHY_EXCEPTION
;
1776 /* w.write() may replace sys.stdout, so we
1777 * have to keep our reference to it */
1779 err
= PyFile_WriteString("\n", w
);
1781 PyFile_SoftSpace(w
, 0);
1790 default: switch (opcode
) {
1796 u
= POP(); /* traceback */
1799 v
= POP(); /* value */
1802 w
= POP(); /* exc */
1803 case 0: /* Fallthrough */
1804 why
= do_raise(w
, v
, u
);
1807 PyErr_SetString(PyExc_SystemError
,
1808 "bad RAISE_VARARGS oparg");
1809 why
= WHY_EXCEPTION
;
1815 if ((x
= f
->f_locals
) != NULL
) {
1820 PyErr_SetString(PyExc_SystemError
, "no locals");
1826 goto fast_block_end
;
1830 f
->f_stacktop
= stack_pointer
;
1839 READ_TIMESTAMP(intr0
);
1840 err
= exec_statement(f
, u
, v
, w
);
1841 READ_TIMESTAMP(intr1
);
1849 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1850 while (STACK_LEVEL() > b
->b_level
) {
1857 PREDICTED(END_FINALLY
);
1860 if (PyInt_Check(v
)) {
1861 why
= (enum why_code
) PyInt_AS_LONG(v
);
1862 assert(why
!= WHY_YIELD
);
1863 if (why
== WHY_RETURN
||
1864 why
== WHY_CONTINUE
)
1867 else if (PyExceptionClass_Check(v
) ||
1868 PyString_Check(v
)) {
1871 PyErr_Restore(v
, w
, u
);
1875 else if (v
!= Py_None
) {
1876 PyErr_SetString(PyExc_SystemError
,
1877 "'finally' pops bad exception");
1878 why
= WHY_EXCEPTION
;
1888 x
= build_class(u
, v
, w
);
1896 w
= GETITEM(names
, oparg
);
1898 if ((x
= f
->f_locals
) != NULL
) {
1899 if (PyDict_CheckExact(x
))
1900 err
= PyDict_SetItem(x
, w
, v
);
1902 err
= PyObject_SetItem(x
, w
, v
);
1904 if (err
== 0) continue;
1907 PyErr_Format(PyExc_SystemError
,
1908 "no locals found when storing %s",
1913 w
= GETITEM(names
, oparg
);
1914 if ((x
= f
->f_locals
) != NULL
) {
1915 if ((err
= PyObject_DelItem(x
, w
)) != 0)
1916 format_exc_check_arg(PyExc_NameError
,
1921 PyErr_Format(PyExc_SystemError
,
1922 "no locals when deleting %s",
1926 PREDICTED_WITH_ARG(UNPACK_SEQUENCE
);
1927 case UNPACK_SEQUENCE
:
1929 if (PyTuple_CheckExact(v
) &&
1930 PyTuple_GET_SIZE(v
) == oparg
) {
1931 PyObject
**items
= \
1932 ((PyTupleObject
*)v
)->ob_item
;
1940 } else if (PyList_CheckExact(v
) &&
1941 PyList_GET_SIZE(v
) == oparg
) {
1942 PyObject
**items
= \
1943 ((PyListObject
*)v
)->ob_item
;
1949 } else if (unpack_iterable(v
, oparg
,
1950 stack_pointer
+ oparg
)) {
1951 stack_pointer
+= oparg
;
1953 /* unpack_iterable() raised an exception */
1954 why
= WHY_EXCEPTION
;
1960 w
= GETITEM(names
, oparg
);
1964 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1967 if (err
== 0) continue;
1971 w
= GETITEM(names
, oparg
);
1973 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1979 w
= GETITEM(names
, oparg
);
1981 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1983 if (err
== 0) continue;
1987 w
= GETITEM(names
, oparg
);
1988 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1989 format_exc_check_arg(
1990 PyExc_NameError
, GLOBAL_NAME_ERROR_MSG
, w
);
1994 w
= GETITEM(names
, oparg
);
1995 if ((v
= f
->f_locals
) == NULL
) {
1996 PyErr_Format(PyExc_SystemError
,
1997 "no locals when loading %s",
1999 why
= WHY_EXCEPTION
;
2002 if (PyDict_CheckExact(v
)) {
2003 x
= PyDict_GetItem(v
, w
);
2007 x
= PyObject_GetItem(v
, w
);
2008 if (x
== NULL
&& PyErr_Occurred()) {
2009 if (!PyErr_ExceptionMatches(
2016 x
= PyDict_GetItem(f
->f_globals
, w
);
2018 x
= PyDict_GetItem(f
->f_builtins
, w
);
2020 format_exc_check_arg(
2032 w
= GETITEM(names
, oparg
);
2033 if (PyString_CheckExact(w
)) {
2034 /* Inline the PyDict_GetItem() calls.
2035 WARNING: this is an extreme speed hack.
2036 Do not try this at home. */
2037 long hash
= ((PyStringObject
*)w
)->ob_shash
;
2041 d
= (PyDictObject
*)(f
->f_globals
);
2042 e
= d
->ma_lookup(d
, w
, hash
);
2053 d
= (PyDictObject
*)(f
->f_builtins
);
2054 e
= d
->ma_lookup(d
, w
, hash
);
2065 goto load_global_error
;
2068 /* This is the un-inlined version of the code above */
2069 x
= PyDict_GetItem(f
->f_globals
, w
);
2071 x
= PyDict_GetItem(f
->f_builtins
, w
);
2074 format_exc_check_arg(
2076 GLOBAL_NAME_ERROR_MSG
, w
);
2085 x
= GETLOCAL(oparg
);
2087 SETLOCAL(oparg
, NULL
);
2090 format_exc_check_arg(
2091 PyExc_UnboundLocalError
,
2092 UNBOUNDLOCAL_ERROR_MSG
,
2093 PyTuple_GetItem(co
->co_varnames
, oparg
)
2098 x
= freevars
[oparg
];
2101 if (x
!= NULL
) continue;
2105 x
= freevars
[oparg
];
2112 /* Don't stomp existing exception */
2113 if (PyErr_Occurred())
2115 if (oparg
< PyTuple_GET_SIZE(co
->co_cellvars
)) {
2116 v
= PyTuple_GET_ITEM(co
->co_cellvars
,
2118 format_exc_check_arg(
2119 PyExc_UnboundLocalError
,
2120 UNBOUNDLOCAL_ERROR_MSG
,
2123 v
= PyTuple_GET_ITEM(co
->co_freevars
, oparg
-
2124 PyTuple_GET_SIZE(co
->co_cellvars
));
2125 format_exc_check_arg(PyExc_NameError
,
2126 UNBOUNDFREE_ERROR_MSG
, v
);
2132 x
= freevars
[oparg
];
2138 x
= PyTuple_New(oparg
);
2140 for (; --oparg
>= 0;) {
2142 PyTuple_SET_ITEM(x
, oparg
, w
);
2150 x
= PyList_New(oparg
);
2152 for (; --oparg
>= 0;) {
2154 PyList_SET_ITEM(x
, oparg
, w
);
2162 x
= _PyDict_NewPresized((Py_ssize_t
)oparg
);
2164 if (x
!= NULL
) continue;
2168 w
= TOP(); /* key */
2169 u
= SECOND(); /* value */
2170 v
= THIRD(); /* dict */
2172 assert (PyDict_CheckExact(v
));
2173 err
= PyDict_SetItem(v
, w
, u
); /* v[w] = u */
2176 if (err
== 0) continue;
2180 w
= GETITEM(names
, oparg
);
2182 x
= PyObject_GetAttr(v
, w
);
2185 if (x
!= NULL
) continue;
2191 if (PyInt_CheckExact(w
) && PyInt_CheckExact(v
)) {
2192 /* INLINE: cmp(int, int) */
2195 a
= PyInt_AS_LONG(v
);
2196 b
= PyInt_AS_LONG(w
);
2198 case PyCmp_LT
: res
= a
< b
; break;
2199 case PyCmp_LE
: res
= a
<= b
; break;
2200 case PyCmp_EQ
: res
= a
== b
; break;
2201 case PyCmp_NE
: res
= a
!= b
; break;
2202 case PyCmp_GT
: res
= a
> b
; break;
2203 case PyCmp_GE
: res
= a
>= b
; break;
2204 case PyCmp_IS
: res
= v
== w
; break;
2205 case PyCmp_IS_NOT
: res
= v
!= w
; break;
2206 default: goto slow_compare
;
2208 x
= res
? Py_True
: Py_False
;
2213 x
= cmp_outcome(oparg
, v
, w
);
2218 if (x
== NULL
) break;
2219 PREDICT(JUMP_IF_FALSE
);
2220 PREDICT(JUMP_IF_TRUE
);
2224 w
= GETITEM(names
, oparg
);
2225 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
2227 PyErr_SetString(PyExc_ImportError
,
2228 "__import__ not found");
2234 if (PyInt_AsLong(u
) != -1 || PyErr_Occurred())
2238 f
->f_locals
== NULL
?
2239 Py_None
: f
->f_locals
,
2246 f
->f_locals
== NULL
?
2247 Py_None
: f
->f_locals
,
2257 READ_TIMESTAMP(intr0
);
2259 x
= PyEval_CallObject(v
, w
);
2261 READ_TIMESTAMP(intr1
);
2264 if (x
!= NULL
) continue;
2269 PyFrame_FastToLocals(f
);
2270 if ((x
= f
->f_locals
) == NULL
) {
2271 PyErr_SetString(PyExc_SystemError
,
2272 "no locals found during 'import *'");
2275 READ_TIMESTAMP(intr0
);
2276 err
= import_all_from(x
, v
);
2277 READ_TIMESTAMP(intr1
);
2278 PyFrame_LocalsToFast(f
, 0);
2280 if (err
== 0) continue;
2284 w
= GETITEM(names
, oparg
);
2286 READ_TIMESTAMP(intr0
);
2287 x
= import_from(v
, w
);
2288 READ_TIMESTAMP(intr1
);
2290 if (x
!= NULL
) continue;
2295 goto fast_next_opcode
;
2297 PREDICTED_WITH_ARG(JUMP_IF_FALSE
);
2302 goto fast_next_opcode
;
2304 if (w
== Py_False
) {
2306 goto fast_next_opcode
;
2308 err
= PyObject_IsTrue(w
);
2317 PREDICTED_WITH_ARG(JUMP_IF_TRUE
);
2320 if (w
== Py_False
) {
2322 goto fast_next_opcode
;
2326 goto fast_next_opcode
;
2328 err
= PyObject_IsTrue(w
);
2339 PREDICTED_WITH_ARG(JUMP_ABSOLUTE
);
2343 /* Enabling this path speeds-up all while and for-loops by bypassing
2344 the per-loop checks for signals. By default, this should be turned-off
2345 because it prevents detection of a control-break in tight loops like
2346 "while 1: pass". Compile with this option turned-on when you need
2347 the speed-up and do not need break checking inside tight loops (ones
2348 that contain only instructions ending with goto fast_next_opcode).
2350 goto fast_next_opcode
;
2356 /* before: [obj]; after [getiter(obj)] */
2358 x
= PyObject_GetIter(v
);
2368 PREDICTED_WITH_ARG(FOR_ITER
);
2370 /* before: [iter]; after: [iter, iter()] *or* [] */
2372 x
= (*v
->ob_type
->tp_iternext
)(v
);
2375 PREDICT(STORE_FAST
);
2376 PREDICT(UNPACK_SEQUENCE
);
2379 if (PyErr_Occurred()) {
2380 if (!PyErr_ExceptionMatches(
2381 PyExc_StopIteration
))
2385 /* iterator ended normally */
2393 goto fast_block_end
;
2396 retval
= PyInt_FromLong(oparg
);
2402 goto fast_block_end
;
2407 /* NOTE: If you add any new block-setup opcodes that
2408 are not try/except/finally handlers, you may need
2409 to update the PyGen_NeedsFinalizing() function.
2412 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
2418 /* At the top of the stack are 1-3 values indicating
2419 how/why we entered the finally clause:
2421 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2422 - TOP = WHY_*; no retval below it
2423 - (TOP, SECOND, THIRD) = exc_info()
2424 Below them is EXIT, the context.__exit__ bound method.
2425 In the last case, we must call
2426 EXIT(TOP, SECOND, THIRD)
2427 otherwise we must call
2428 EXIT(None, None, None)
2430 In all cases, we remove EXIT from the stack, leaving
2431 the rest in the same order.
2433 In addition, if the stack represents an exception,
2434 *and* the function call returns a 'true' value, we
2435 "zap" this information, to prevent END_FINALLY from
2436 re-raising the exception. (But non-local gotos
2437 should still be resumed.)
2440 PyObject
*exit_func
;
2448 else if (PyInt_Check(u
)) {
2449 switch(PyInt_AS_LONG(u
)) {
2452 /* Retval in TOP. */
2453 exit_func
= SECOND();
2462 u
= v
= w
= Py_None
;
2467 exit_func
= THIRD();
2472 /* XXX Not the fastest way to call it... */
2473 x
= PyObject_CallFunctionObjArgs(exit_func
, u
, v
, w
,
2475 Py_DECREF(exit_func
);
2477 break; /* Go to error exit */
2480 err
= PyObject_IsTrue(x
);
2486 break; /* Go to error exit */
2489 /* There was an exception and a true return */
2497 /* The stack was rearranged to remove EXIT
2498 above. Let END_FINALLY do its thing */
2500 PREDICT(END_FINALLY
);
2510 x
= call_function(&sp
, oparg
, &intr0
, &intr1
);
2512 x
= call_function(&sp
, oparg
);
2521 case CALL_FUNCTION_VAR
:
2522 case CALL_FUNCTION_KW
:
2523 case CALL_FUNCTION_VAR_KW
:
2525 int na
= oparg
& 0xff;
2526 int nk
= (oparg
>>8) & 0xff;
2527 int flags
= (opcode
- CALL_FUNCTION
) & 3;
2528 int n
= na
+ 2 * nk
;
2529 PyObject
**pfunc
, *func
, **sp
;
2531 if (flags
& CALL_FLAG_VAR
)
2533 if (flags
& CALL_FLAG_KW
)
2535 pfunc
= stack_pointer
- n
- 1;
2538 if (PyMethod_Check(func
)
2539 && PyMethod_GET_SELF(func
) != NULL
) {
2540 PyObject
*self
= PyMethod_GET_SELF(func
);
2542 func
= PyMethod_GET_FUNCTION(func
);
2551 READ_TIMESTAMP(intr0
);
2552 x
= ext_do_call(func
, &sp
, flags
, na
, nk
);
2553 READ_TIMESTAMP(intr1
);
2557 while (stack_pointer
> pfunc
) {
2568 v
= POP(); /* code object */
2569 x
= PyFunction_New(v
, f
->f_globals
);
2571 /* XXX Maybe this should be a separate opcode? */
2572 if (x
!= NULL
&& oparg
> 0) {
2573 v
= PyTuple_New(oparg
);
2579 while (--oparg
>= 0) {
2581 PyTuple_SET_ITEM(v
, oparg
, w
);
2583 err
= PyFunction_SetDefaults(x
, v
);
2591 v
= POP(); /* code object */
2592 x
= PyFunction_New(v
, f
->f_globals
);
2596 if (PyFunction_SetClosure(x
, v
) != 0) {
2597 /* Can't happen unless bytecode is corrupt. */
2598 why
= WHY_EXCEPTION
;
2602 if (x
!= NULL
&& oparg
> 0) {
2603 v
= PyTuple_New(oparg
);
2609 while (--oparg
>= 0) {
2611 PyTuple_SET_ITEM(v
, oparg
, w
);
2613 if (PyFunction_SetDefaults(x
, v
) != 0) {
2614 /* Can't happen unless
2615 PyFunction_SetDefaults changes. */
2616 why
= WHY_EXCEPTION
;
2631 x
= PySlice_New(u
, v
, w
);
2636 if (x
!= NULL
) continue;
2641 oparg
= oparg
<<16 | NEXTARG();
2642 goto dispatch_opcode
;
2646 "XXX lineno: %d, opcode: %d\n",
2647 PyCode_Addr2Line(f
->f_code
, f
->f_lasti
),
2649 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
2650 why
= WHY_EXCEPTION
;
2661 READ_TIMESTAMP(inst1
);
2663 /* Quickly continue if no error occurred */
2665 if (why
== WHY_NOT
) {
2666 if (err
== 0 && x
!= NULL
) {
2668 /* This check is expensive! */
2669 if (PyErr_Occurred())
2671 "XXX undetected error\n");
2674 READ_TIMESTAMP(loop1
);
2675 continue; /* Normal, fast path */
2680 why
= WHY_EXCEPTION
;
2685 /* Double-check exception status */
2687 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
2688 if (!PyErr_Occurred()) {
2689 PyErr_SetString(PyExc_SystemError
,
2690 "error return without exception set");
2691 why
= WHY_EXCEPTION
;
2696 /* This check is expensive! */
2697 if (PyErr_Occurred()) {
2699 sprintf(buf
, "Stack unwind with exception "
2700 "set and why=%d", why
);
2706 /* Log traceback info if this is a real exception */
2708 if (why
== WHY_EXCEPTION
) {
2709 PyTraceBack_Here(f
);
2711 if (tstate
->c_tracefunc
!= NULL
)
2712 call_exc_trace(tstate
->c_tracefunc
,
2713 tstate
->c_traceobj
, f
);
2716 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2718 if (why
== WHY_RERAISE
)
2719 why
= WHY_EXCEPTION
;
2721 /* Unwind stacks if a (pseudo) exception occurred */
2724 while (why
!= WHY_NOT
&& f
->f_iblock
> 0) {
2725 PyTryBlock
*b
= PyFrame_BlockPop(f
);
2727 assert(why
!= WHY_YIELD
);
2728 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_CONTINUE
) {
2729 /* For a continue inside a try block,
2730 don't pop the block for the loop. */
2731 PyFrame_BlockSetup(f
, b
->b_type
, b
->b_handler
,
2734 JUMPTO(PyInt_AS_LONG(retval
));
2739 while (STACK_LEVEL() > b
->b_level
) {
2743 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
2745 JUMPTO(b
->b_handler
);
2748 if (b
->b_type
== SETUP_FINALLY
||
2749 (b
->b_type
== SETUP_EXCEPT
&&
2750 why
== WHY_EXCEPTION
)) {
2751 if (why
== WHY_EXCEPTION
) {
2752 PyObject
*exc
, *val
, *tb
;
2753 PyErr_Fetch(&exc
, &val
, &tb
);
2758 /* Make the raw exception data
2759 available to the handler,
2760 so a program can emulate the
2761 Python main loop. Don't do
2762 this for 'finally'. */
2763 if (b
->b_type
== SETUP_EXCEPT
) {
2764 PyErr_NormalizeException(
2766 set_exc_info(tstate
,
2778 if (why
& (WHY_RETURN
| WHY_CONTINUE
))
2780 v
= PyInt_FromLong((long)why
);
2784 JUMPTO(b
->b_handler
);
2787 } /* unwind stack */
2789 /* End the loop if we still have an error (or return) */
2793 READ_TIMESTAMP(loop1
);
2797 assert(why
!= WHY_YIELD
);
2798 /* Pop remaining stack entries. */
2804 if (why
!= WHY_RETURN
)
2808 if (tstate
->use_tracing
) {
2809 if (tstate
->c_tracefunc
) {
2810 if (why
== WHY_RETURN
|| why
== WHY_YIELD
) {
2811 if (call_trace(tstate
->c_tracefunc
,
2812 tstate
->c_traceobj
, f
,
2813 PyTrace_RETURN
, retval
)) {
2816 why
= WHY_EXCEPTION
;
2819 else if (why
== WHY_EXCEPTION
) {
2820 call_trace_protected(tstate
->c_tracefunc
,
2821 tstate
->c_traceobj
, f
,
2822 PyTrace_RETURN
, NULL
);
2825 if (tstate
->c_profilefunc
) {
2826 if (why
== WHY_EXCEPTION
)
2827 call_trace_protected(tstate
->c_profilefunc
,
2828 tstate
->c_profileobj
, f
,
2829 PyTrace_RETURN
, NULL
);
2830 else if (call_trace(tstate
->c_profilefunc
,
2831 tstate
->c_profileobj
, f
,
2832 PyTrace_RETURN
, retval
)) {
2835 why
= WHY_EXCEPTION
;
2840 if (tstate
->frame
->f_exc_type
!= NULL
)
2841 reset_exc_info(tstate
);
2843 assert(tstate
->frame
->f_exc_value
== NULL
);
2844 assert(tstate
->frame
->f_exc_traceback
== NULL
);
2849 Py_LeaveRecursiveCall();
2850 tstate
->frame
= f
->f_back
;
2855 /* This is gonna seem *real weird*, but if you put some other code between
2856 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2857 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2860 PyEval_EvalCodeEx(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
,
2861 PyObject
**args
, int argcount
, PyObject
**kws
, int kwcount
,
2862 PyObject
**defs
, int defcount
, PyObject
*closure
)
2864 register PyFrameObject
*f
;
2865 register PyObject
*retval
= NULL
;
2866 register PyObject
**fastlocals
, **freevars
;
2867 PyThreadState
*tstate
= PyThreadState_GET();
2870 if (globals
== NULL
) {
2871 PyErr_SetString(PyExc_SystemError
,
2872 "PyEval_EvalCodeEx: NULL globals");
2876 assert(tstate
!= NULL
);
2877 assert(globals
!= NULL
);
2878 f
= PyFrame_New(tstate
, co
, globals
, locals
);
2882 fastlocals
= f
->f_localsplus
;
2883 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
2885 if (co
->co_argcount
> 0 ||
2886 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
2889 PyObject
*kwdict
= NULL
;
2890 if (co
->co_flags
& CO_VARKEYWORDS
) {
2891 kwdict
= PyDict_New();
2894 i
= co
->co_argcount
;
2895 if (co
->co_flags
& CO_VARARGS
)
2897 SETLOCAL(i
, kwdict
);
2899 if (argcount
> co
->co_argcount
) {
2900 if (!(co
->co_flags
& CO_VARARGS
)) {
2901 PyErr_Format(PyExc_TypeError
,
2902 "%.200s() takes %s %d "
2903 "%sargument%s (%d given)",
2904 PyString_AsString(co
->co_name
),
2905 defcount
? "at most" : "exactly",
2907 kwcount
? "non-keyword " : "",
2908 co
->co_argcount
== 1 ? "" : "s",
2912 n
= co
->co_argcount
;
2914 for (i
= 0; i
< n
; i
++) {
2919 if (co
->co_flags
& CO_VARARGS
) {
2920 u
= PyTuple_New(argcount
- n
);
2923 SETLOCAL(co
->co_argcount
, u
);
2924 for (i
= n
; i
< argcount
; i
++) {
2927 PyTuple_SET_ITEM(u
, i
-n
, x
);
2930 for (i
= 0; i
< kwcount
; i
++) {
2931 PyObject
**co_varnames
;
2932 PyObject
*keyword
= kws
[2*i
];
2933 PyObject
*value
= kws
[2*i
+ 1];
2935 if (keyword
== NULL
|| !PyString_Check(keyword
)) {
2936 PyErr_Format(PyExc_TypeError
,
2937 "%.200s() keywords must be strings",
2938 PyString_AsString(co
->co_name
));
2941 /* Speed hack: do raw pointer compares. As names are
2942 normally interned this should almost always hit. */
2943 co_varnames
= PySequence_Fast_ITEMS(co
->co_varnames
);
2944 for (j
= 0; j
< co
->co_argcount
; j
++) {
2945 PyObject
*nm
= co_varnames
[j
];
2949 /* Slow fallback, just in case */
2950 for (j
= 0; j
< co
->co_argcount
; j
++) {
2951 PyObject
*nm
= co_varnames
[j
];
2952 int cmp
= PyObject_RichCompareBool(
2953 keyword
, nm
, Py_EQ
);
2959 /* Check errors from Compare */
2960 if (PyErr_Occurred())
2962 if (j
>= co
->co_argcount
) {
2963 if (kwdict
== NULL
) {
2964 PyErr_Format(PyExc_TypeError
,
2965 "%.200s() got an unexpected "
2966 "keyword argument '%.400s'",
2967 PyString_AsString(co
->co_name
),
2968 PyString_AsString(keyword
));
2971 PyDict_SetItem(kwdict
, keyword
, value
);
2975 if (GETLOCAL(j
) != NULL
) {
2976 PyErr_Format(PyExc_TypeError
,
2977 "%.200s() got multiple "
2978 "values for keyword "
2979 "argument '%.400s'",
2980 PyString_AsString(co
->co_name
),
2981 PyString_AsString(keyword
));
2987 if (argcount
< co
->co_argcount
) {
2988 int m
= co
->co_argcount
- defcount
;
2989 for (i
= argcount
; i
< m
; i
++) {
2990 if (GETLOCAL(i
) == NULL
) {
2991 PyErr_Format(PyExc_TypeError
,
2992 "%.200s() takes %s %d "
2993 "%sargument%s (%d given)",
2994 PyString_AsString(co
->co_name
),
2995 ((co
->co_flags
& CO_VARARGS
) ||
2996 defcount
) ? "at least"
2998 m
, kwcount
? "non-keyword " : "",
2999 m
== 1 ? "" : "s", i
);
3007 for (; i
< defcount
; i
++) {
3008 if (GETLOCAL(m
+i
) == NULL
) {
3009 PyObject
*def
= defs
[i
];
3017 if (argcount
> 0 || kwcount
> 0) {
3018 PyErr_Format(PyExc_TypeError
,
3019 "%.200s() takes no arguments (%d given)",
3020 PyString_AsString(co
->co_name
),
3021 argcount
+ kwcount
);
3025 /* Allocate and initialize storage for cell vars, and copy free
3026 vars into frame. This isn't too efficient right now. */
3027 if (PyTuple_GET_SIZE(co
->co_cellvars
)) {
3028 int i
, j
, nargs
, found
;
3029 char *cellname
, *argname
;
3032 nargs
= co
->co_argcount
;
3033 if (co
->co_flags
& CO_VARARGS
)
3035 if (co
->co_flags
& CO_VARKEYWORDS
)
3038 /* Initialize each cell var, taking into account
3039 cell vars that are initialized from arguments.
3041 Should arrange for the compiler to put cellvars
3042 that are arguments at the beginning of the cellvars
3043 list so that we can march over it more efficiently?
3045 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_cellvars
); ++i
) {
3046 cellname
= PyString_AS_STRING(
3047 PyTuple_GET_ITEM(co
->co_cellvars
, i
));
3049 for (j
= 0; j
< nargs
; j
++) {
3050 argname
= PyString_AS_STRING(
3051 PyTuple_GET_ITEM(co
->co_varnames
, j
));
3052 if (strcmp(cellname
, argname
) == 0) {
3053 c
= PyCell_New(GETLOCAL(j
));
3056 GETLOCAL(co
->co_nlocals
+ i
) = c
;
3062 c
= PyCell_New(NULL
);
3065 SETLOCAL(co
->co_nlocals
+ i
, c
);
3069 if (PyTuple_GET_SIZE(co
->co_freevars
)) {
3071 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_freevars
); ++i
) {
3072 PyObject
*o
= PyTuple_GET_ITEM(closure
, i
);
3074 freevars
[PyTuple_GET_SIZE(co
->co_cellvars
) + i
] = o
;
3078 if (co
->co_flags
& CO_GENERATOR
) {
3079 /* Don't need to keep the reference to f_back, it will be set
3080 * when the generator is resumed. */
3081 Py_XDECREF(f
->f_back
);
3084 PCALL(PCALL_GENERATOR
);
3086 /* Create a new generator that owns the ready to run frame
3087 * and return that as the value. */
3088 return PyGen_New(f
);
3091 retval
= PyEval_EvalFrameEx(f
,0);
3093 fail
: /* Jump here from prelude on failure */
3095 /* decref'ing the frame can cause __del__ methods to get invoked,
3096 which can call back into Python. While we're done with the
3097 current Python frame (f), the associated C stack is still in use,
3098 so recursion_depth must be boosted for the duration.
3100 assert(tstate
!= NULL
);
3101 ++tstate
->recursion_depth
;
3103 --tstate
->recursion_depth
;
3108 /* Implementation notes for set_exc_info() and reset_exc_info():
3110 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3111 'exc_traceback'. These always travel together.
3113 - tstate->curexc_ZZZ is the "hot" exception that is set by
3114 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3116 - Once an exception is caught by an except clause, it is transferred
3117 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3118 can pick it up. This is the primary task of set_exc_info().
3119 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
3121 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3123 Long ago, when none of this existed, there were just a few globals:
3124 one set corresponding to the "hot" exception, and one set
3125 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3126 globals; they were simply stored as sys.exc_ZZZ. For backwards
3127 compatibility, they still are!) The problem was that in code like
3131 "something that may fail"
3132 except "some exception":
3133 "do something else first"
3134 "print the exception from sys.exc_ZZZ."
3136 if "do something else first" invoked something that raised and caught
3137 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3138 cause of subtle bugs. I fixed this by changing the semantics as
3141 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3144 - But initially, and as long as no exception is caught in a given
3145 frame, sys.exc_ZZZ will hold the last exception caught in the
3146 previous frame (or the frame before that, etc.).
3148 The first bullet fixed the bug in the above example. The second
3149 bullet was for backwards compatibility: it was (and is) common to
3150 have a function that is called when an exception is caught, and to
3151 have that function access the caught exception via sys.exc_ZZZ.
3152 (Example: traceback.print_exc()).
3154 At the same time I fixed the problem that sys.exc_ZZZ weren't
3155 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3156 but that's really a separate improvement.
3158 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3159 variables to what they were before the current frame was called. The
3160 set_exc_info() function saves them on the frame so that
3161 reset_exc_info() can restore them. The invariant is that
3162 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3163 exception (where "catching" an exception applies only to successful
3164 except clauses); and if the current frame ever caught an exception,
3165 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3166 at the start of the current frame.
3171 set_exc_info(PyThreadState
*tstate
,
3172 PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3174 PyFrameObject
*frame
= tstate
->frame
;
3175 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
3177 assert(type
!= NULL
);
3178 assert(frame
!= NULL
);
3179 if (frame
->f_exc_type
== NULL
) {
3180 assert(frame
->f_exc_value
== NULL
);
3181 assert(frame
->f_exc_traceback
== NULL
);
3182 /* This frame didn't catch an exception before. */
3183 /* Save previous exception of this thread in this frame. */
3184 if (tstate
->exc_type
== NULL
) {
3185 /* XXX Why is this set to Py_None? */
3187 tstate
->exc_type
= Py_None
;
3189 Py_INCREF(tstate
->exc_type
);
3190 Py_XINCREF(tstate
->exc_value
);
3191 Py_XINCREF(tstate
->exc_traceback
);
3192 frame
->f_exc_type
= tstate
->exc_type
;
3193 frame
->f_exc_value
= tstate
->exc_value
;
3194 frame
->f_exc_traceback
= tstate
->exc_traceback
;
3196 /* Set new exception for this thread. */
3197 tmp_type
= tstate
->exc_type
;
3198 tmp_value
= tstate
->exc_value
;
3199 tmp_tb
= tstate
->exc_traceback
;
3203 tstate
->exc_type
= type
;
3204 tstate
->exc_value
= value
;
3205 tstate
->exc_traceback
= tb
;
3206 Py_XDECREF(tmp_type
);
3207 Py_XDECREF(tmp_value
);
3209 /* For b/w compatibility */
3210 PySys_SetObject("exc_type", type
);
3211 PySys_SetObject("exc_value", value
);
3212 PySys_SetObject("exc_traceback", tb
);
3216 reset_exc_info(PyThreadState
*tstate
)
3218 PyFrameObject
*frame
;
3219 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
3221 /* It's a precondition that the thread state's frame caught an
3222 * exception -- verify in a debug build.
3224 assert(tstate
!= NULL
);
3225 frame
= tstate
->frame
;
3226 assert(frame
!= NULL
);
3227 assert(frame
->f_exc_type
!= NULL
);
3229 /* Copy the frame's exception info back to the thread state. */
3230 tmp_type
= tstate
->exc_type
;
3231 tmp_value
= tstate
->exc_value
;
3232 tmp_tb
= tstate
->exc_traceback
;
3233 Py_INCREF(frame
->f_exc_type
);
3234 Py_XINCREF(frame
->f_exc_value
);
3235 Py_XINCREF(frame
->f_exc_traceback
);
3236 tstate
->exc_type
= frame
->f_exc_type
;
3237 tstate
->exc_value
= frame
->f_exc_value
;
3238 tstate
->exc_traceback
= frame
->f_exc_traceback
;
3239 Py_XDECREF(tmp_type
);
3240 Py_XDECREF(tmp_value
);
3243 /* For b/w compatibility */
3244 PySys_SetObject("exc_type", frame
->f_exc_type
);
3245 PySys_SetObject("exc_value", frame
->f_exc_value
);
3246 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
3248 /* Clear the frame's exception info. */
3249 tmp_type
= frame
->f_exc_type
;
3250 tmp_value
= frame
->f_exc_value
;
3251 tmp_tb
= frame
->f_exc_traceback
;
3252 frame
->f_exc_type
= NULL
;
3253 frame
->f_exc_value
= NULL
;
3254 frame
->f_exc_traceback
= NULL
;
3255 Py_DECREF(tmp_type
);
3256 Py_XDECREF(tmp_value
);
3260 /* Logic for the raise statement (too complicated for inlining).
3261 This *consumes* a reference count to each of its arguments. */
3262 static enum why_code
3263 do_raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3267 PyThreadState
*tstate
= PyThreadState_GET();
3268 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
3269 value
= tstate
->exc_value
;
3270 tb
= tstate
->exc_traceback
;
3276 /* We support the following forms of raise:
3277 raise <class>, <classinstance>
3278 raise <class>, <argument tuple>
3280 raise <class>, <argument>
3281 raise <classinstance>, None
3282 raise <string>, <object>
3283 raise <string>, None
3285 An omitted second argument is the same as None.
3287 In addition, raise <tuple>, <anything> is the same as
3288 raising the tuple's first item (and it better have one!);
3289 this rule is applied recursively.
3291 Finally, an optional third argument can be supplied, which
3292 gives the traceback to be substituted (useful when
3293 re-raising an exception after examining it). */
3295 /* First, check the traceback argument, replacing None with
3297 if (tb
== Py_None
) {
3301 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
3302 PyErr_SetString(PyExc_TypeError
,
3303 "raise: arg 3 must be a traceback or None");
3307 /* Next, replace a missing value with None */
3308 if (value
== NULL
) {
3313 /* Next, repeatedly, replace a tuple exception with its first item */
3314 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
3315 PyObject
*tmp
= type
;
3316 type
= PyTuple_GET_ITEM(type
, 0);
3321 if (PyExceptionClass_Check(type
))
3322 PyErr_NormalizeException(&type
, &value
, &tb
);
3324 else if (PyExceptionInstance_Check(type
)) {
3325 /* Raising an instance. The value should be a dummy. */
3326 if (value
!= Py_None
) {
3327 PyErr_SetString(PyExc_TypeError
,
3328 "instance exception may not have a separate value");
3332 /* Normalize to raise <class>, <instance> */
3335 type
= PyExceptionInstance_Class(type
);
3340 /* Not something you can raise. You get an exception
3341 anyway, just not what you specified :-) */
3342 PyErr_Format(PyExc_TypeError
,
3343 "exceptions must be classes or instances, not %s",
3344 type
->ob_type
->tp_name
);
3348 assert(PyExceptionClass_Check(type
));
3349 if (Py_Py3kWarningFlag
&& PyClass_Check(type
)) {
3350 if (PyErr_WarnEx(PyExc_DeprecationWarning
,
3351 "exceptions must derive from BaseException "
3356 PyErr_Restore(type
, value
, tb
);
3358 return WHY_EXCEPTION
;
3365 return WHY_EXCEPTION
;
3368 /* Iterate v argcnt times and store the results on the stack (via decreasing
3369 sp). Return 1 for success, 0 if error. */
3372 unpack_iterable(PyObject
*v
, int argcnt
, PyObject
**sp
)
3375 PyObject
*it
; /* iter(v) */
3380 it
= PyObject_GetIter(v
);
3384 for (; i
< argcnt
; i
++) {
3385 w
= PyIter_Next(it
);
3387 /* Iterator done, via error or exhaustion. */
3388 if (!PyErr_Occurred()) {
3389 PyErr_Format(PyExc_ValueError
,
3390 "need more than %d value%s to unpack",
3391 i
, i
== 1 ? "" : "s");
3398 /* We better have exhausted the iterator now. */
3399 w
= PyIter_Next(it
);
3401 if (PyErr_Occurred())
3407 PyErr_SetString(PyExc_ValueError
, "too many values to unpack");
3410 for (; i
> 0; i
--, sp
++)
3419 prtrace(PyObject
*v
, char *str
)
3422 if (PyObject_Print(v
, stdout
, 0) != 0)
3423 PyErr_Clear(); /* Don't know what else to do */
3430 call_exc_trace(Py_tracefunc func
, PyObject
*self
, PyFrameObject
*f
)
3432 PyObject
*type
, *value
, *traceback
, *arg
;
3434 PyErr_Fetch(&type
, &value
, &traceback
);
3435 if (value
== NULL
) {
3439 arg
= PyTuple_Pack(3, type
, value
, traceback
);
3441 PyErr_Restore(type
, value
, traceback
);
3444 err
= call_trace(func
, self
, f
, PyTrace_EXCEPTION
, arg
);
3447 PyErr_Restore(type
, value
, traceback
);
3451 Py_XDECREF(traceback
);
3456 call_trace_protected(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3457 int what
, PyObject
*arg
)
3459 PyObject
*type
, *value
, *traceback
;
3461 PyErr_Fetch(&type
, &value
, &traceback
);
3462 err
= call_trace(func
, obj
, frame
, what
, arg
);
3465 PyErr_Restore(type
, value
, traceback
);
3471 Py_XDECREF(traceback
);
3477 call_trace(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3478 int what
, PyObject
*arg
)
3480 register PyThreadState
*tstate
= frame
->f_tstate
;
3482 if (tstate
->tracing
)
3485 tstate
->use_tracing
= 0;
3486 result
= func(obj
, frame
, what
, arg
);
3487 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3488 || (tstate
->c_profilefunc
!= NULL
));
3494 _PyEval_CallTracing(PyObject
*func
, PyObject
*args
)
3496 PyFrameObject
*frame
= PyEval_GetFrame();
3497 PyThreadState
*tstate
= frame
->f_tstate
;
3498 int save_tracing
= tstate
->tracing
;
3499 int save_use_tracing
= tstate
->use_tracing
;
3502 tstate
->tracing
= 0;
3503 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3504 || (tstate
->c_profilefunc
!= NULL
));
3505 result
= PyObject_Call(func
, args
, NULL
);
3506 tstate
->tracing
= save_tracing
;
3507 tstate
->use_tracing
= save_use_tracing
;
3512 maybe_call_line_trace(Py_tracefunc func
, PyObject
*obj
,
3513 PyFrameObject
*frame
, int *instr_lb
, int *instr_ub
,
3518 /* If the last instruction executed isn't in the current
3519 instruction window, reset the window. If the last
3520 instruction happens to fall at the start of a line or if it
3521 represents a jump backwards, call the trace function.
3523 if ((frame
->f_lasti
< *instr_lb
|| frame
->f_lasti
>= *instr_ub
)) {
3527 line
= PyCode_CheckLineNumber(frame
->f_code
, frame
->f_lasti
,
3530 frame
->f_lineno
= line
;
3531 result
= call_trace(func
, obj
, frame
,
3532 PyTrace_LINE
, Py_None
);
3534 *instr_lb
= bounds
.ap_lower
;
3535 *instr_ub
= bounds
.ap_upper
;
3537 else if (frame
->f_lasti
<= *instr_prev
) {
3538 result
= call_trace(func
, obj
, frame
, PyTrace_LINE
, Py_None
);
3540 *instr_prev
= frame
->f_lasti
;
3545 PyEval_SetProfile(Py_tracefunc func
, PyObject
*arg
)
3547 PyThreadState
*tstate
= PyThreadState_GET();
3548 PyObject
*temp
= tstate
->c_profileobj
;
3550 tstate
->c_profilefunc
= NULL
;
3551 tstate
->c_profileobj
= NULL
;
3552 /* Must make sure that tracing is not ignored if 'temp' is freed */
3553 tstate
->use_tracing
= tstate
->c_tracefunc
!= NULL
;
3555 tstate
->c_profilefunc
= func
;
3556 tstate
->c_profileobj
= arg
;
3557 /* Flag that tracing or profiling is turned on */
3558 tstate
->use_tracing
= (func
!= NULL
) || (tstate
->c_tracefunc
!= NULL
);
3562 PyEval_SetTrace(Py_tracefunc func
, PyObject
*arg
)
3564 PyThreadState
*tstate
= PyThreadState_GET();
3565 PyObject
*temp
= tstate
->c_traceobj
;
3566 _Py_TracingPossible
+= (func
!= NULL
) - (tstate
->c_tracefunc
!= NULL
);
3568 tstate
->c_tracefunc
= NULL
;
3569 tstate
->c_traceobj
= NULL
;
3570 /* Must make sure that profiling is not ignored if 'temp' is freed */
3571 tstate
->use_tracing
= tstate
->c_profilefunc
!= NULL
;
3573 tstate
->c_tracefunc
= func
;
3574 tstate
->c_traceobj
= arg
;
3575 /* Flag that tracing or profiling is turned on */
3576 tstate
->use_tracing
= ((func
!= NULL
)
3577 || (tstate
->c_profilefunc
!= NULL
));
3581 PyEval_GetBuiltins(void)
3583 PyFrameObject
*current_frame
= PyEval_GetFrame();
3584 if (current_frame
== NULL
)
3585 return PyThreadState_GET()->interp
->builtins
;
3587 return current_frame
->f_builtins
;
3591 PyEval_GetLocals(void)
3593 PyFrameObject
*current_frame
= PyEval_GetFrame();
3594 if (current_frame
== NULL
)
3596 PyFrame_FastToLocals(current_frame
);
3597 return current_frame
->f_locals
;
3601 PyEval_GetGlobals(void)
3603 PyFrameObject
*current_frame
= PyEval_GetFrame();
3604 if (current_frame
== NULL
)
3607 return current_frame
->f_globals
;
3611 PyEval_GetFrame(void)
3613 PyThreadState
*tstate
= PyThreadState_GET();
3614 return _PyThreadState_GetFrame(tstate
);
3618 PyEval_GetRestricted(void)
3620 PyFrameObject
*current_frame
= PyEval_GetFrame();
3621 return current_frame
== NULL
? 0 : PyFrame_IsRestricted(current_frame
);
3625 PyEval_MergeCompilerFlags(PyCompilerFlags
*cf
)
3627 PyFrameObject
*current_frame
= PyEval_GetFrame();
3628 int result
= cf
->cf_flags
!= 0;
3630 if (current_frame
!= NULL
) {
3631 const int codeflags
= current_frame
->f_code
->co_flags
;
3632 const int compilerflags
= codeflags
& PyCF_MASK
;
3633 if (compilerflags
) {
3635 cf
->cf_flags
|= compilerflags
;
3637 #if 0 /* future keyword */
3638 if (codeflags
& CO_GENERATOR_ALLOWED
) {
3640 cf
->cf_flags
|= CO_GENERATOR_ALLOWED
;
3650 PyObject
*f
= PySys_GetObject("stdout");
3653 if (!PyFile_SoftSpace(f
, 0))
3655 return PyFile_WriteString("\n", f
);
3659 /* External interface to call any callable object.
3660 The arg must be a tuple or NULL. */
3662 #undef PyEval_CallObject
3663 /* for backward compatibility: export this interface */
3666 PyEval_CallObject(PyObject
*func
, PyObject
*arg
)
3668 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
3670 #define PyEval_CallObject(func,arg) \
3671 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3674 PyEval_CallObjectWithKeywords(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
3679 arg
= PyTuple_New(0);
3683 else if (!PyTuple_Check(arg
)) {
3684 PyErr_SetString(PyExc_TypeError
,
3685 "argument list must be a tuple");
3691 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
3692 PyErr_SetString(PyExc_TypeError
,
3693 "keyword list must be a dictionary");
3698 result
= PyObject_Call(func
, arg
, kw
);
3704 PyEval_GetFuncName(PyObject
*func
)
3706 if (PyMethod_Check(func
))
3707 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func
));
3708 else if (PyFunction_Check(func
))
3709 return PyString_AsString(((PyFunctionObject
*)func
)->func_name
);
3710 else if (PyCFunction_Check(func
))
3711 return ((PyCFunctionObject
*)func
)->m_ml
->ml_name
;
3712 else if (PyClass_Check(func
))
3713 return PyString_AsString(((PyClassObject
*)func
)->cl_name
);
3714 else if (PyInstance_Check(func
)) {
3715 return PyString_AsString(
3716 ((PyInstanceObject
*)func
)->in_class
->cl_name
);
3718 return func
->ob_type
->tp_name
;
3723 PyEval_GetFuncDesc(PyObject
*func
)
3725 if (PyMethod_Check(func
))
3727 else if (PyFunction_Check(func
))
3729 else if (PyCFunction_Check(func
))
3731 else if (PyClass_Check(func
))
3732 return " constructor";
3733 else if (PyInstance_Check(func
)) {
3741 err_args(PyObject
*func
, int flags
, int nargs
)
3743 if (flags
& METH_NOARGS
)
3744 PyErr_Format(PyExc_TypeError
,
3745 "%.200s() takes no arguments (%d given)",
3746 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3749 PyErr_Format(PyExc_TypeError
,
3750 "%.200s() takes exactly one argument (%d given)",
3751 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3755 #define C_TRACE(x, call) \
3756 if (tstate->use_tracing && tstate->c_profilefunc) { \
3757 if (call_trace(tstate->c_profilefunc, \
3758 tstate->c_profileobj, \
3759 tstate->frame, PyTrace_C_CALL, \
3765 if (tstate->c_profilefunc != NULL) { \
3767 call_trace_protected(tstate->c_profilefunc, \
3768 tstate->c_profileobj, \
3769 tstate->frame, PyTrace_C_EXCEPTION, \
3771 /* XXX should pass (type, value, tb) */ \
3773 if (call_trace(tstate->c_profilefunc, \
3774 tstate->c_profileobj, \
3775 tstate->frame, PyTrace_C_RETURN, \
3788 call_function(PyObject
***pp_stack
, int oparg
3790 , uint64
* pintr0
, uint64
* pintr1
3794 int na
= oparg
& 0xff;
3795 int nk
= (oparg
>>8) & 0xff;
3796 int n
= na
+ 2 * nk
;
3797 PyObject
**pfunc
= (*pp_stack
) - n
- 1;
3798 PyObject
*func
= *pfunc
;
3801 /* Always dispatch PyCFunction first, because these are
3802 presumed to be the most frequent callable object.
3804 if (PyCFunction_Check(func
) && nk
== 0) {
3805 int flags
= PyCFunction_GET_FLAGS(func
);
3806 PyThreadState
*tstate
= PyThreadState_GET();
3808 PCALL(PCALL_CFUNCTION
);
3809 if (flags
& (METH_NOARGS
| METH_O
)) {
3810 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
3811 PyObject
*self
= PyCFunction_GET_SELF(func
);
3812 if (flags
& METH_NOARGS
&& na
== 0) {
3813 C_TRACE(x
, (*meth
)(self
,NULL
));
3815 else if (flags
& METH_O
&& na
== 1) {
3816 PyObject
*arg
= EXT_POP(*pp_stack
);
3817 C_TRACE(x
, (*meth
)(self
,arg
));
3821 err_args(func
, flags
, na
);
3827 callargs
= load_args(pp_stack
, na
);
3828 READ_TIMESTAMP(*pintr0
);
3829 C_TRACE(x
, PyCFunction_Call(func
,callargs
,NULL
));
3830 READ_TIMESTAMP(*pintr1
);
3831 Py_XDECREF(callargs
);
3834 if (PyMethod_Check(func
) && PyMethod_GET_SELF(func
) != NULL
) {
3835 /* optimize access to bound methods */
3836 PyObject
*self
= PyMethod_GET_SELF(func
);
3837 PCALL(PCALL_METHOD
);
3838 PCALL(PCALL_BOUND_METHOD
);
3840 func
= PyMethod_GET_FUNCTION(func
);
3848 READ_TIMESTAMP(*pintr0
);
3849 if (PyFunction_Check(func
))
3850 x
= fast_function(func
, pp_stack
, n
, na
, nk
);
3852 x
= do_call(func
, pp_stack
, na
, nk
);
3853 READ_TIMESTAMP(*pintr1
);
3857 /* Clear the stack of the function object. Also removes
3858 the arguments in case they weren't consumed already
3859 (fast_function() and err_args() leave them on the stack).
3861 while ((*pp_stack
) > pfunc
) {
3862 w
= EXT_POP(*pp_stack
);
3869 /* The fast_function() function optimize calls for which no argument
3870 tuple is necessary; the objects are passed directly from the stack.
3871 For the simplest case -- a function that takes only positional
3872 arguments and is called with only positional arguments -- it
3873 inlines the most primitive frame setup code from
3874 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3875 done before evaluating the frame.
3879 fast_function(PyObject
*func
, PyObject
***pp_stack
, int n
, int na
, int nk
)
3881 PyCodeObject
*co
= (PyCodeObject
*)PyFunction_GET_CODE(func
);
3882 PyObject
*globals
= PyFunction_GET_GLOBALS(func
);
3883 PyObject
*argdefs
= PyFunction_GET_DEFAULTS(func
);
3884 PyObject
**d
= NULL
;
3887 PCALL(PCALL_FUNCTION
);
3888 PCALL(PCALL_FAST_FUNCTION
);
3889 if (argdefs
== NULL
&& co
->co_argcount
== n
&& nk
==0 &&
3890 co
->co_flags
== (CO_OPTIMIZED
| CO_NEWLOCALS
| CO_NOFREE
)) {
3892 PyObject
*retval
= NULL
;
3893 PyThreadState
*tstate
= PyThreadState_GET();
3894 PyObject
**fastlocals
, **stack
;
3897 PCALL(PCALL_FASTER_FUNCTION
);
3898 assert(globals
!= NULL
);
3899 /* XXX Perhaps we should create a specialized
3900 PyFrame_New() that doesn't take locals, but does
3901 take builtins without sanity checking them.
3903 assert(tstate
!= NULL
);
3904 f
= PyFrame_New(tstate
, co
, globals
, NULL
);
3908 fastlocals
= f
->f_localsplus
;
3909 stack
= (*pp_stack
) - n
;
3911 for (i
= 0; i
< n
; i
++) {
3913 fastlocals
[i
] = *stack
++;
3915 retval
= PyEval_EvalFrameEx(f
,0);
3916 ++tstate
->recursion_depth
;
3918 --tstate
->recursion_depth
;
3921 if (argdefs
!= NULL
) {
3922 d
= &PyTuple_GET_ITEM(argdefs
, 0);
3923 nd
= Py_SIZE(argdefs
);
3925 return PyEval_EvalCodeEx(co
, globals
,
3926 (PyObject
*)NULL
, (*pp_stack
)-n
, na
,
3927 (*pp_stack
)-2*nk
, nk
, d
, nd
,
3928 PyFunction_GET_CLOSURE(func
));
3932 update_keyword_args(PyObject
*orig_kwdict
, int nk
, PyObject
***pp_stack
,
3935 PyObject
*kwdict
= NULL
;
3936 if (orig_kwdict
== NULL
)
3937 kwdict
= PyDict_New();
3939 kwdict
= PyDict_Copy(orig_kwdict
);
3940 Py_DECREF(orig_kwdict
);
3946 PyObject
*value
= EXT_POP(*pp_stack
);
3947 PyObject
*key
= EXT_POP(*pp_stack
);
3948 if (PyDict_GetItem(kwdict
, key
) != NULL
) {
3949 PyErr_Format(PyExc_TypeError
,
3950 "%.200s%s got multiple values "
3951 "for keyword argument '%.200s'",
3952 PyEval_GetFuncName(func
),
3953 PyEval_GetFuncDesc(func
),
3954 PyString_AsString(key
));
3960 err
= PyDict_SetItem(kwdict
, key
, value
);
3972 update_star_args(int nstack
, int nstar
, PyObject
*stararg
,
3973 PyObject
***pp_stack
)
3975 PyObject
*callargs
, *w
;
3977 callargs
= PyTuple_New(nstack
+ nstar
);
3978 if (callargs
== NULL
) {
3983 for (i
= 0; i
< nstar
; i
++) {
3984 PyObject
*a
= PyTuple_GET_ITEM(stararg
, i
);
3986 PyTuple_SET_ITEM(callargs
, nstack
+ i
, a
);
3989 while (--nstack
>= 0) {
3990 w
= EXT_POP(*pp_stack
);
3991 PyTuple_SET_ITEM(callargs
, nstack
, w
);
3997 load_args(PyObject
***pp_stack
, int na
)
3999 PyObject
*args
= PyTuple_New(na
);
4005 w
= EXT_POP(*pp_stack
);
4006 PyTuple_SET_ITEM(args
, na
, w
);
4012 do_call(PyObject
*func
, PyObject
***pp_stack
, int na
, int nk
)
4014 PyObject
*callargs
= NULL
;
4015 PyObject
*kwdict
= NULL
;
4016 PyObject
*result
= NULL
;
4019 kwdict
= update_keyword_args(NULL
, nk
, pp_stack
, func
);
4023 callargs
= load_args(pp_stack
, na
);
4024 if (callargs
== NULL
)
4027 /* At this point, we have to look at the type of func to
4028 update the call stats properly. Do it here so as to avoid
4029 exposing the call stats machinery outside ceval.c
4031 if (PyFunction_Check(func
))
4032 PCALL(PCALL_FUNCTION
);
4033 else if (PyMethod_Check(func
))
4034 PCALL(PCALL_METHOD
);
4035 else if (PyType_Check(func
))
4040 result
= PyObject_Call(func
, callargs
, kwdict
);
4042 Py_XDECREF(callargs
);
4048 ext_do_call(PyObject
*func
, PyObject
***pp_stack
, int flags
, int na
, int nk
)
4051 PyObject
*callargs
= NULL
;
4052 PyObject
*stararg
= NULL
;
4053 PyObject
*kwdict
= NULL
;
4054 PyObject
*result
= NULL
;
4056 if (flags
& CALL_FLAG_KW
) {
4057 kwdict
= EXT_POP(*pp_stack
);
4058 if (!PyDict_Check(kwdict
)) {
4063 if (PyDict_Update(d
, kwdict
) != 0) {
4065 /* PyDict_Update raises attribute
4066 * error (percolated from an attempt
4067 * to get 'keys' attribute) instead of
4068 * a type error if its second argument
4071 if (PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4072 PyErr_Format(PyExc_TypeError
,
4073 "%.200s%.200s argument after ** "
4074 "must be a mapping, not %.200s",
4075 PyEval_GetFuncName(func
),
4076 PyEval_GetFuncDesc(func
),
4077 kwdict
->ob_type
->tp_name
);
4085 if (flags
& CALL_FLAG_VAR
) {
4086 stararg
= EXT_POP(*pp_stack
);
4087 if (!PyTuple_Check(stararg
)) {
4089 t
= PySequence_Tuple(stararg
);
4091 if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
4092 PyErr_Format(PyExc_TypeError
,
4093 "%.200s%.200s argument after * "
4094 "must be a sequence, not %200s",
4095 PyEval_GetFuncName(func
),
4096 PyEval_GetFuncDesc(func
),
4097 stararg
->ob_type
->tp_name
);
4104 nstar
= PyTuple_GET_SIZE(stararg
);
4107 kwdict
= update_keyword_args(kwdict
, nk
, pp_stack
, func
);
4111 callargs
= update_star_args(na
, nstar
, stararg
, pp_stack
);
4112 if (callargs
== NULL
)
4115 /* At this point, we have to look at the type of func to
4116 update the call stats properly. Do it here so as to avoid
4117 exposing the call stats machinery outside ceval.c
4119 if (PyFunction_Check(func
))
4120 PCALL(PCALL_FUNCTION
);
4121 else if (PyMethod_Check(func
))
4122 PCALL(PCALL_METHOD
);
4123 else if (PyType_Check(func
))
4128 result
= PyObject_Call(func
, callargs
, kwdict
);
4130 Py_XDECREF(callargs
);
4132 Py_XDECREF(stararg
);
4136 /* Extract a slice index from a PyInt or PyLong or an object with the
4137 nb_index slot defined, and store in *pi.
4138 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4139 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
4140 Return 0 on error, 1 on success.
4142 /* Note: If v is NULL, return success without storing into *pi. This
4143 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4144 called by the SLICE opcode with v and/or w equal to NULL.
4147 _PyEval_SliceIndex(PyObject
*v
, Py_ssize_t
*pi
)
4151 if (PyInt_Check(v
)) {
4152 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4153 however, it looks like it should be AsSsize_t.
4154 There should be a comment here explaining why.
4156 x
= PyInt_AS_LONG(v
);
4158 else if (PyIndex_Check(v
)) {
4159 x
= PyNumber_AsSsize_t(v
, NULL
);
4160 if (x
== -1 && PyErr_Occurred())
4164 PyErr_SetString(PyExc_TypeError
,
4165 "slice indices must be integers or "
4166 "None or have an __index__ method");
4175 #define ISINDEX(x) ((x) == NULL || \
4176 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
4179 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
4181 PyTypeObject
*tp
= u
->ob_type
;
4182 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4184 if (sq
&& sq
->sq_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4185 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4186 if (!_PyEval_SliceIndex(v
, &ilow
))
4188 if (!_PyEval_SliceIndex(w
, &ihigh
))
4190 return PySequence_GetSlice(u
, ilow
, ihigh
);
4193 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4194 if (slice
!= NULL
) {
4195 PyObject
*res
= PyObject_GetItem(u
, slice
);
4205 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
4208 PyTypeObject
*tp
= u
->ob_type
;
4209 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4211 if (sq
&& sq
->sq_ass_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4212 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4213 if (!_PyEval_SliceIndex(v
, &ilow
))
4215 if (!_PyEval_SliceIndex(w
, &ihigh
))
4218 return PySequence_DelSlice(u
, ilow
, ihigh
);
4220 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
4223 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4224 if (slice
!= NULL
) {
4227 res
= PyObject_SetItem(u
, slice
, x
);
4229 res
= PyObject_DelItem(u
, slice
);
4238 #define Py3kExceptionClass_Check(x) \
4239 (PyType_Check((x)) && \
4240 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4242 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4243 "BaseException is not allowed in 3.x"
4246 cmp_outcome(int op
, register PyObject
*v
, register PyObject
*w
)
4257 res
= PySequence_Contains(w
, v
);
4262 res
= PySequence_Contains(w
, v
);
4267 case PyCmp_EXC_MATCH
:
4268 if (PyTuple_Check(w
)) {
4269 Py_ssize_t i
, length
;
4270 length
= PyTuple_Size(w
);
4271 for (i
= 0; i
< length
; i
+= 1) {
4272 PyObject
*exc
= PyTuple_GET_ITEM(w
, i
);
4273 if (PyString_Check(exc
)) {
4275 ret_val
= PyErr_WarnEx(
4276 PyExc_DeprecationWarning
,
4277 "catching of string "
4278 "exceptions is deprecated", 1);
4282 else if (Py_Py3kWarningFlag
&&
4283 !PyTuple_Check(exc
) &&
4284 !Py3kExceptionClass_Check(exc
))
4287 ret_val
= PyErr_WarnEx(
4288 PyExc_DeprecationWarning
,
4289 CANNOT_CATCH_MSG
, 1);
4296 if (PyString_Check(w
)) {
4298 ret_val
= PyErr_WarnEx(
4299 PyExc_DeprecationWarning
,
4300 "catching of string "
4301 "exceptions is deprecated", 1);
4305 else if (Py_Py3kWarningFlag
&&
4306 !PyTuple_Check(w
) &&
4307 !Py3kExceptionClass_Check(w
))
4310 ret_val
= PyErr_WarnEx(
4311 PyExc_DeprecationWarning
,
4312 CANNOT_CATCH_MSG
, 1);
4317 res
= PyErr_GivenExceptionMatches(v
, w
);
4320 return PyObject_RichCompare(v
, w
, op
);
4322 v
= res
? Py_True
: Py_False
;
4328 import_from(PyObject
*v
, PyObject
*name
)
4332 x
= PyObject_GetAttr(v
, name
);
4333 if (x
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4334 PyErr_Format(PyExc_ImportError
,
4335 "cannot import name %.230s",
4336 PyString_AsString(name
));
4342 import_all_from(PyObject
*locals
, PyObject
*v
)
4344 PyObject
*all
= PyObject_GetAttrString(v
, "__all__");
4345 PyObject
*dict
, *name
, *value
;
4346 int skip_leading_underscores
= 0;
4350 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4351 return -1; /* Unexpected error */
4353 dict
= PyObject_GetAttrString(v
, "__dict__");
4355 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4357 PyErr_SetString(PyExc_ImportError
,
4358 "from-import-* object has no __dict__ and no __all__");
4361 all
= PyMapping_Keys(dict
);
4365 skip_leading_underscores
= 1;
4368 for (pos
= 0, err
= 0; ; pos
++) {
4369 name
= PySequence_GetItem(all
, pos
);
4371 if (!PyErr_ExceptionMatches(PyExc_IndexError
))
4377 if (skip_leading_underscores
&&
4378 PyString_Check(name
) &&
4379 PyString_AS_STRING(name
)[0] == '_')
4384 value
= PyObject_GetAttr(v
, name
);
4387 else if (PyDict_CheckExact(locals
))
4388 err
= PyDict_SetItem(locals
, name
, value
);
4390 err
= PyObject_SetItem(locals
, name
, value
);
4401 build_class(PyObject
*methods
, PyObject
*bases
, PyObject
*name
)
4403 PyObject
*metaclass
= NULL
, *result
, *base
;
4405 if (PyDict_Check(methods
))
4406 metaclass
= PyDict_GetItemString(methods
, "__metaclass__");
4407 if (metaclass
!= NULL
)
4408 Py_INCREF(metaclass
);
4409 else if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
4410 base
= PyTuple_GET_ITEM(bases
, 0);
4411 metaclass
= PyObject_GetAttrString(base
, "__class__");
4412 if (metaclass
== NULL
) {
4414 metaclass
= (PyObject
*)base
->ob_type
;
4415 Py_INCREF(metaclass
);
4419 PyObject
*g
= PyEval_GetGlobals();
4420 if (g
!= NULL
&& PyDict_Check(g
))
4421 metaclass
= PyDict_GetItemString(g
, "__metaclass__");
4422 if (metaclass
== NULL
)
4423 metaclass
= (PyObject
*) &PyClass_Type
;
4424 Py_INCREF(metaclass
);
4426 result
= PyObject_CallFunctionObjArgs(metaclass
, name
, bases
, methods
,
4428 Py_DECREF(metaclass
);
4429 if (result
== NULL
&& PyErr_ExceptionMatches(PyExc_TypeError
)) {
4430 /* A type error here likely means that the user passed
4431 in a base that was not a class (such the random module
4432 instead of the random.random type). Help them out with
4433 by augmenting the error message with more information.*/
4435 PyObject
*ptype
, *pvalue
, *ptraceback
;
4437 PyErr_Fetch(&ptype
, &pvalue
, &ptraceback
);
4438 if (PyString_Check(pvalue
)) {
4440 newmsg
= PyString_FromFormat(
4441 "Error when calling the metaclass bases\n"
4443 PyString_AS_STRING(pvalue
));
4444 if (newmsg
!= NULL
) {
4449 PyErr_Restore(ptype
, pvalue
, ptraceback
);
4455 exec_statement(PyFrameObject
*f
, PyObject
*prog
, PyObject
*globals
,
4462 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
4463 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
4464 /* Backward compatibility hack */
4465 globals
= PyTuple_GetItem(prog
, 1);
4467 locals
= PyTuple_GetItem(prog
, 2);
4468 prog
= PyTuple_GetItem(prog
, 0);
4470 if (globals
== Py_None
) {
4471 globals
= PyEval_GetGlobals();
4472 if (locals
== Py_None
) {
4473 locals
= PyEval_GetLocals();
4476 if (!globals
|| !locals
) {
4477 PyErr_SetString(PyExc_SystemError
,
4478 "globals and locals cannot be NULL");
4482 else if (locals
== Py_None
)
4484 if (!PyString_Check(prog
) &&
4485 !PyUnicode_Check(prog
) &&
4486 !PyCode_Check(prog
) &&
4487 !PyFile_Check(prog
)) {
4488 PyErr_SetString(PyExc_TypeError
,
4489 "exec: arg 1 must be a string, file, or code object");
4492 if (!PyDict_Check(globals
)) {
4493 PyErr_SetString(PyExc_TypeError
,
4494 "exec: arg 2 must be a dictionary or None");
4497 if (!PyMapping_Check(locals
)) {
4498 PyErr_SetString(PyExc_TypeError
,
4499 "exec: arg 3 must be a mapping or None");
4502 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
4503 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
4504 if (PyCode_Check(prog
)) {
4505 if (PyCode_GetNumFree((PyCodeObject
*)prog
) > 0) {
4506 PyErr_SetString(PyExc_TypeError
,
4507 "code object passed to exec may not contain free variables");
4510 v
= PyEval_EvalCode((PyCodeObject
*) prog
, globals
, locals
);
4512 else if (PyFile_Check(prog
)) {
4513 FILE *fp
= PyFile_AsFile(prog
);
4514 char *name
= PyString_AsString(PyFile_Name(prog
));
4519 if (PyEval_MergeCompilerFlags(&cf
))
4520 v
= PyRun_FileFlags(fp
, name
, Py_file_input
, globals
,
4523 v
= PyRun_File(fp
, name
, Py_file_input
, globals
,
4527 PyObject
*tmp
= NULL
;
4531 #ifdef Py_USING_UNICODE
4532 if (PyUnicode_Check(prog
)) {
4533 tmp
= PyUnicode_AsUTF8String(prog
);
4537 cf
.cf_flags
|= PyCF_SOURCE_IS_UTF8
;
4540 if (PyString_AsStringAndSize(prog
, &str
, NULL
))
4542 if (PyEval_MergeCompilerFlags(&cf
))
4543 v
= PyRun_StringFlags(str
, Py_file_input
, globals
,
4546 v
= PyRun_String(str
, Py_file_input
, globals
, locals
);
4550 PyFrame_LocalsToFast(f
, 0);
4558 format_exc_check_arg(PyObject
*exc
, char *format_str
, PyObject
*obj
)
4565 obj_str
= PyString_AsString(obj
);
4569 PyErr_Format(exc
, format_str
, obj_str
);
4573 string_concatenate(PyObject
*v
, PyObject
*w
,
4574 PyFrameObject
*f
, unsigned char *next_instr
)
4576 /* This function implements 'variable += expr' when both arguments
4578 Py_ssize_t v_len
= PyString_GET_SIZE(v
);
4579 Py_ssize_t w_len
= PyString_GET_SIZE(w
);
4580 Py_ssize_t new_len
= v_len
+ w_len
;
4582 PyErr_SetString(PyExc_OverflowError
,
4583 "strings are too large to concat");
4587 if (v
->ob_refcnt
== 2) {
4588 /* In the common case, there are 2 references to the value
4589 * stored in 'variable' when the += is performed: one on the
4590 * value stack (in 'v') and one still stored in the
4591 * 'variable'. We try to delete the variable now to reduce
4594 switch (*next_instr
) {
4597 int oparg
= PEEKARG();
4598 PyObject
**fastlocals
= f
->f_localsplus
;
4599 if (GETLOCAL(oparg
) == v
)
4600 SETLOCAL(oparg
, NULL
);
4605 PyObject
**freevars
= (f
->f_localsplus
+
4606 f
->f_code
->co_nlocals
);
4607 PyObject
*c
= freevars
[PEEKARG()];
4608 if (PyCell_GET(c
) == v
)
4609 PyCell_Set(c
, NULL
);
4614 PyObject
*names
= f
->f_code
->co_names
;
4615 PyObject
*name
= GETITEM(names
, PEEKARG());
4616 PyObject
*locals
= f
->f_locals
;
4617 if (PyDict_CheckExact(locals
) &&
4618 PyDict_GetItem(locals
, name
) == v
) {
4619 if (PyDict_DelItem(locals
, name
) != 0) {
4628 if (v
->ob_refcnt
== 1 && !PyString_CHECK_INTERNED(v
)) {
4629 /* Now we own the last reference to 'v', so we can resize it
4632 if (_PyString_Resize(&v
, new_len
) != 0) {
4633 /* XXX if _PyString_Resize() fails, 'v' has been
4634 * deallocated so it cannot be put back into
4635 * 'variable'. The MemoryError is raised when there
4636 * is no value in 'variable', which might (very
4637 * remotely) be a cause of incompatibilities.
4641 /* copy 'w' into the newly allocated area of 'v' */
4642 memcpy(PyString_AS_STRING(v
) + v_len
,
4643 PyString_AS_STRING(w
), w_len
);
4647 /* When in-place resizing is not an option. */
4648 PyString_Concat(&v
, w
);
4653 #ifdef DYNAMIC_EXECUTION_PROFILE
4656 getarray(long a
[256])
4659 PyObject
*l
= PyList_New(256);
4660 if (l
== NULL
) return NULL
;
4661 for (i
= 0; i
< 256; i
++) {
4662 PyObject
*x
= PyInt_FromLong(a
[i
]);
4667 PyList_SetItem(l
, i
, x
);
4669 for (i
= 0; i
< 256; i
++)
4675 _Py_GetDXProfile(PyObject
*self
, PyObject
*args
)
4678 return getarray(dxp
);
4681 PyObject
*l
= PyList_New(257);
4682 if (l
== NULL
) return NULL
;
4683 for (i
= 0; i
< 257; i
++) {
4684 PyObject
*x
= getarray(dxpairs
[i
]);
4689 PyList_SetItem(l
, i
, x
);