2 /* Execute compiled code */
5 XXX speed up searching for keywords by using a dictionary
9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE
15 #include "frameobject.h"
18 #include "structmember.h"
24 #define READ_TIMESTAMP(var)
28 typedef unsigned long long uint64
;
30 #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
31 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
35 #define READ_TIMESTAMP(var) ppc_getcounter(&var)
38 ppc_getcounter(uint64
*v
)
40 register unsigned long tbu
, tb
, tbu2
;
43 asm volatile ("mftbu %0" : "=r" (tbu
) );
44 asm volatile ("mftb %0" : "=r" (tb
) );
45 asm volatile ("mftbu %0" : "=r" (tbu2
));
46 if (__builtin_expect(tbu
!= tbu2
, 0)) goto loop
;
48 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v
))[0] = tbu
;
54 #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
56 #define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
61 void dump_tsc(int opcode
, int ticked
, uint64 inst0
, uint64 inst1
,
62 uint64 loop0
, uint64 loop1
, uint64 intr0
, uint64 intr1
)
64 uint64 intr
, inst
, loop
;
65 PyThreadState
*tstate
= PyThreadState_Get();
66 if (!tstate
->interp
->tscdump
)
69 inst
= inst1
- inst0
- intr
;
70 loop
= loop1
- loop0
- intr
;
71 fprintf(stderr
, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode
, ticked
, inst
, loop
);
77 /* Turn this on if your compiler chokes on the big switch: */
78 /* #define CASE_TOO_BIG 1 */
81 /* For debugging the interpreter: */
82 #define LLTRACE 1 /* Low-level trace feature */
83 #define CHECKEXC 1 /* Double-check exception checking */
86 typedef PyObject
*(*callproc
)(PyObject
*, PyObject
*, PyObject
*);
88 /* Forward declarations */
90 static PyObject
* call_function(PyObject
***, int, uint64
*, uint64
*);
92 static PyObject
* call_function(PyObject
***, int);
94 static PyObject
* fast_function(PyObject
*, PyObject
***, int, int, int);
95 static PyObject
* do_call(PyObject
*, PyObject
***, int, int);
96 static PyObject
* ext_do_call(PyObject
*, PyObject
***, int, int, int);
97 static PyObject
* update_keyword_args(PyObject
*, int, PyObject
***,
99 static PyObject
* update_star_args(int, int, PyObject
*, PyObject
***);
100 static PyObject
* load_args(PyObject
***, int);
101 #define CALL_FLAG_VAR 1
102 #define CALL_FLAG_KW 2
106 static int prtrace(PyObject
*, char *);
108 static int call_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*,
110 static int call_trace_protected(Py_tracefunc
, PyObject
*,
111 PyFrameObject
*, int, PyObject
*);
112 static void call_exc_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*);
113 static int maybe_call_line_trace(Py_tracefunc
, PyObject
*,
114 PyFrameObject
*, int *, int *, int *);
116 static PyObject
* apply_slice(PyObject
*, PyObject
*, PyObject
*);
117 static int assign_slice(PyObject
*, PyObject
*,
118 PyObject
*, PyObject
*);
119 static PyObject
* cmp_outcome(int, PyObject
*, PyObject
*);
120 static PyObject
* import_from(PyObject
*, PyObject
*);
121 static int import_all_from(PyObject
*, PyObject
*);
122 static PyObject
* build_class(PyObject
*, PyObject
*, PyObject
*);
123 static int exec_statement(PyFrameObject
*,
124 PyObject
*, PyObject
*, PyObject
*);
125 static void set_exc_info(PyThreadState
*, PyObject
*, PyObject
*, PyObject
*);
126 static void reset_exc_info(PyThreadState
*);
127 static void format_exc_check_arg(PyObject
*, char *, PyObject
*);
128 static PyObject
* string_concatenate(PyObject
*, PyObject
*,
129 PyFrameObject
*, unsigned char *);
130 static PyObject
* kwd_as_string(PyObject
*);
131 static PyObject
* special_lookup(PyObject
*, char *, PyObject
**);
133 #define NAME_ERROR_MSG \
134 "name '%.200s' is not defined"
135 #define GLOBAL_NAME_ERROR_MSG \
136 "global name '%.200s' is not defined"
137 #define UNBOUNDLOCAL_ERROR_MSG \
138 "local variable '%.200s' referenced before assignment"
139 #define UNBOUNDFREE_ERROR_MSG \
140 "free variable '%.200s' referenced before assignment" \
141 " in enclosing scope"
143 /* Dynamic execution profile */
144 #ifdef DYNAMIC_EXECUTION_PROFILE
146 static long dxpairs
[257][256];
147 #define dxp dxpairs[256]
149 static long dxp
[256];
153 /* Function call profile */
156 static int pcall
[PCALL_NUM
];
159 #define PCALL_FUNCTION 1
160 #define PCALL_FAST_FUNCTION 2
161 #define PCALL_FASTER_FUNCTION 3
162 #define PCALL_METHOD 4
163 #define PCALL_BOUND_METHOD 5
164 #define PCALL_CFUNCTION 6
166 #define PCALL_GENERATOR 8
167 #define PCALL_OTHER 9
170 /* Notes about the statistics
174 FAST_FUNCTION means no argument tuple needs to be created.
175 FASTER_FUNCTION means that the fast-path frame setup code is used.
177 If there is a method call where the call can be optimized by changing
178 the argument tuple and calling the function directly, it gets recorded
181 As a result, the relationship among the statistics appears to be
182 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
183 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
184 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
185 PCALL_METHOD > PCALL_BOUND_METHOD
188 #define PCALL(POS) pcall[POS]++
191 PyEval_GetCallStats(PyObject
*self
)
193 return Py_BuildValue("iiiiiiiiiii",
194 pcall
[0], pcall
[1], pcall
[2], pcall
[3],
195 pcall
[4], pcall
[5], pcall
[6], pcall
[7],
196 pcall
[8], pcall
[9], pcall
[10]);
202 PyEval_GetCallStats(PyObject
*self
)
215 #include "pythread.h"
217 static PyThread_type_lock interpreter_lock
= 0; /* This is the GIL */
218 static PyThread_type_lock pending_lock
= 0; /* for pending calls */
219 static long main_thread
= 0;
222 PyEval_ThreadsInitialized(void)
224 return interpreter_lock
!= 0;
228 PyEval_InitThreads(void)
230 if (interpreter_lock
)
232 interpreter_lock
= PyThread_allocate_lock();
233 PyThread_acquire_lock(interpreter_lock
, 1);
234 main_thread
= PyThread_get_thread_ident();
238 PyEval_AcquireLock(void)
240 PyThread_acquire_lock(interpreter_lock
, 1);
244 PyEval_ReleaseLock(void)
246 PyThread_release_lock(interpreter_lock
);
250 PyEval_AcquireThread(PyThreadState
*tstate
)
253 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
254 /* Check someone has called PyEval_InitThreads() to create the lock */
255 assert(interpreter_lock
);
256 PyThread_acquire_lock(interpreter_lock
, 1);
257 if (PyThreadState_Swap(tstate
) != NULL
)
259 "PyEval_AcquireThread: non-NULL old thread state");
263 PyEval_ReleaseThread(PyThreadState
*tstate
)
266 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
267 if (PyThreadState_Swap(NULL
) != tstate
)
268 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
269 PyThread_release_lock(interpreter_lock
);
272 /* This function is called from PyOS_AfterFork to ensure that newly
273 created child processes don't hold locks referring to threads which
274 are not running in the child process. (This could also be done using
275 pthread_atfork mechanism, at least for the pthreads implementation.) */
278 PyEval_ReInitThreads(void)
280 PyObject
*threading
, *result
;
281 PyThreadState
*tstate
;
283 if (!interpreter_lock
)
285 /*XXX Can't use PyThread_free_lock here because it does too
286 much error-checking. Doing this cleanly would require
287 adding a new function to each thread_*.h. Instead, just
288 create a new lock and waste a little bit of memory */
289 interpreter_lock
= PyThread_allocate_lock();
290 pending_lock
= PyThread_allocate_lock();
291 PyThread_acquire_lock(interpreter_lock
, 1);
292 main_thread
= PyThread_get_thread_ident();
294 /* Update the threading module with the new state.
296 tstate
= PyThreadState_GET();
297 threading
= PyMapping_GetItemString(tstate
->interp
->modules
,
299 if (threading
== NULL
) {
300 /* threading not imported */
304 result
= PyObject_CallMethod(threading
, "_after_fork", NULL
);
306 PyErr_WriteUnraisable(threading
);
309 Py_DECREF(threading
);
313 /* Functions save_thread and restore_thread are always defined so
314 dynamically loaded modules needn't be compiled separately for use
315 with and without threads: */
318 PyEval_SaveThread(void)
320 PyThreadState
*tstate
= PyThreadState_Swap(NULL
);
322 Py_FatalError("PyEval_SaveThread: NULL tstate");
324 if (interpreter_lock
)
325 PyThread_release_lock(interpreter_lock
);
331 PyEval_RestoreThread(PyThreadState
*tstate
)
334 Py_FatalError("PyEval_RestoreThread: NULL tstate");
336 if (interpreter_lock
) {
338 PyThread_acquire_lock(interpreter_lock
, 1);
342 PyThreadState_Swap(tstate
);
346 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
347 signal handlers or Mac I/O completion routines) can schedule calls
348 to a function to be called synchronously.
349 The synchronous function is called with one void* argument.
350 It should return 0 for success or -1 for failure -- failure should
351 be accompanied by an exception.
353 If registry succeeds, the registry function returns 0; if it fails
354 (e.g. due to too many pending calls) it returns -1 (without setting
355 an exception condition).
357 Note that because registry may occur from within signal handlers,
358 or other asynchronous events, calling malloc() is unsafe!
361 Any thread can schedule pending calls, but only the main thread
363 There is no facility to schedule calls to a particular thread, but
364 that should be easy to change, should that ever be required. In
365 that case, the static variables here should go into the python
372 /* The WITH_THREAD implementation is thread-safe. It allows
373 scheduling to be made from any thread, and even from an executing
377 #define NPENDINGCALLS 32
381 } pendingcalls
[NPENDINGCALLS
];
382 static int pendingfirst
= 0;
383 static int pendinglast
= 0;
384 static volatile int pendingcalls_to_do
= 1; /* trigger initialization of lock */
385 static char pendingbusy
= 0;
388 Py_AddPendingCall(int (*func
)(void *), void *arg
)
391 PyThread_type_lock lock
= pending_lock
;
393 /* try a few times for the lock. Since this mechanism is used
394 * for signal handling (on the main thread), there is a (slim)
395 * chance that a signal is delivered on the same thread while we
396 * hold the lock during the Py_MakePendingCalls() function.
397 * This avoids a deadlock in that case.
398 * Note that signals can be delivered on any thread. In particular,
399 * on Windows, a SIGINT is delivered on a system-created worker
401 * We also check for lock being NULL, in the unlikely case that
402 * this function is called before any bytecode evaluation takes place.
405 for (i
= 0; i
<100; i
++) {
406 if (PyThread_acquire_lock(lock
, NOWAIT_LOCK
))
414 j
= (i
+ 1) % NPENDINGCALLS
;
415 if (j
== pendingfirst
) {
416 result
= -1; /* Queue full */
418 pendingcalls
[i
].func
= func
;
419 pendingcalls
[i
].arg
= arg
;
422 /* signal main loop */
424 pendingcalls_to_do
= 1;
426 PyThread_release_lock(lock
);
431 Py_MakePendingCalls(void)
437 /* initial allocation of the lock */
438 pending_lock
= PyThread_allocate_lock();
439 if (pending_lock
== NULL
)
443 /* only service pending calls on main thread */
444 if (main_thread
&& PyThread_get_thread_ident() != main_thread
)
446 /* don't perform recursive pending calls */
450 /* perform a bounded number of calls, in case of recursion */
451 for (i
=0; i
<NPENDINGCALLS
; i
++) {
456 /* pop one item off the queue while holding the lock */
457 PyThread_acquire_lock(pending_lock
, WAIT_LOCK
);
459 if (j
== pendinglast
) {
460 func
= NULL
; /* Queue empty */
462 func
= pendingcalls
[j
].func
;
463 arg
= pendingcalls
[j
].arg
;
464 pendingfirst
= (j
+ 1) % NPENDINGCALLS
;
466 pendingcalls_to_do
= pendingfirst
!= pendinglast
;
467 PyThread_release_lock(pending_lock
);
468 /* having released the lock, perform the callback */
479 #else /* if ! defined WITH_THREAD */
482 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
483 This code is used for signal handling in python that isn't built
485 Don't use this implementation when Py_AddPendingCalls() can happen
486 on a different thread!
488 There are two possible race conditions:
489 (1) nested asynchronous calls to Py_AddPendingCall()
490 (2) AddPendingCall() calls made while pending calls are being processed.
492 (1) is very unlikely because typically signal delivery
493 is blocked during signal handling. So it should be impossible.
494 (2) is a real possibility.
495 The current code is safe against (2), but not against (1).
496 The safety against (2) is derived from the fact that only one
497 thread is present, interrupted by signals, and that the critical
498 section is protected with the "busy" variable. On Windows, which
499 delivers SIGINT on a system thread, this does not hold and therefore
500 Windows really shouldn't use this version.
501 The two threads could theoretically wiggle around the "busy" variable.
504 #define NPENDINGCALLS 32
508 } pendingcalls
[NPENDINGCALLS
];
509 static volatile int pendingfirst
= 0;
510 static volatile int pendinglast
= 0;
511 static volatile int pendingcalls_to_do
= 0;
514 Py_AddPendingCall(int (*func
)(void *), void *arg
)
516 static volatile int busy
= 0;
518 /* XXX Begin critical section */
523 j
= (i
+ 1) % NPENDINGCALLS
;
524 if (j
== pendingfirst
) {
526 return -1; /* Queue full */
528 pendingcalls
[i
].func
= func
;
529 pendingcalls
[i
].arg
= arg
;
533 pendingcalls_to_do
= 1; /* Signal main loop */
535 /* XXX End critical section */
540 Py_MakePendingCalls(void)
546 pendingcalls_to_do
= 0;
552 if (i
== pendinglast
)
553 break; /* Queue empty */
554 func
= pendingcalls
[i
].func
;
555 arg
= pendingcalls
[i
].arg
;
556 pendingfirst
= (i
+ 1) % NPENDINGCALLS
;
559 pendingcalls_to_do
= 1; /* We're not done yet */
567 #endif /* WITH_THREAD */
570 /* The interpreter's recursion limit */
572 #ifndef Py_DEFAULT_RECURSION_LIMIT
573 #define Py_DEFAULT_RECURSION_LIMIT 1000
575 static int recursion_limit
= Py_DEFAULT_RECURSION_LIMIT
;
576 int _Py_CheckRecursionLimit
= Py_DEFAULT_RECURSION_LIMIT
;
579 Py_GetRecursionLimit(void)
581 return recursion_limit
;
585 Py_SetRecursionLimit(int new_limit
)
587 recursion_limit
= new_limit
;
588 _Py_CheckRecursionLimit
= recursion_limit
;
591 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
592 if the recursion_depth reaches _Py_CheckRecursionLimit.
593 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
594 to guarantee that _Py_CheckRecursiveCall() is regularly called.
595 Without USE_STACKCHECK, there is no need for this. */
597 _Py_CheckRecursiveCall(char *where
)
599 PyThreadState
*tstate
= PyThreadState_GET();
601 #ifdef USE_STACKCHECK
602 if (PyOS_CheckStack()) {
603 --tstate
->recursion_depth
;
604 PyErr_SetString(PyExc_MemoryError
, "Stack overflow");
608 if (tstate
->recursion_depth
> recursion_limit
) {
609 --tstate
->recursion_depth
;
610 PyErr_Format(PyExc_RuntimeError
,
611 "maximum recursion depth exceeded%s",
615 _Py_CheckRecursionLimit
= recursion_limit
;
619 /* Status code for main loop (reason for stack unwind) */
621 WHY_NOT
= 0x0001, /* No error */
622 WHY_EXCEPTION
= 0x0002, /* Exception occurred */
623 WHY_RERAISE
= 0x0004, /* Exception re-raised by 'finally' */
624 WHY_RETURN
= 0x0008, /* 'return' statement */
625 WHY_BREAK
= 0x0010, /* 'break' statement */
626 WHY_CONTINUE
= 0x0020, /* 'continue' statement */
627 WHY_YIELD
= 0x0040 /* 'yield' operator */
630 static enum why_code
do_raise(PyObject
*, PyObject
*, PyObject
*);
631 static int unpack_iterable(PyObject
*, int, PyObject
**);
633 /* Records whether tracing is on for any thread. Counts the number of
634 threads for which tstate->c_tracefunc is non-NULL, so if the value
635 is 0, we know we don't have to check this thread's c_tracefunc.
636 This speeds up the if statement in PyEval_EvalFrameEx() after
638 static int _Py_TracingPossible
= 0;
640 /* for manipulating the thread switch and periodic "stuff" - used to be
641 per thread, now just a pair o' globals */
642 int _Py_CheckInterval
= 100;
643 volatile int _Py_Ticker
= 0; /* so that we hit a "tick" first thing */
646 PyEval_EvalCode(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
)
648 return PyEval_EvalCodeEx(co
,
650 (PyObject
**)NULL
, 0,
651 (PyObject
**)NULL
, 0,
652 (PyObject
**)NULL
, 0,
657 /* Interpreter main loop */
660 PyEval_EvalFrame(PyFrameObject
*f
) {
661 /* This is for backward compatibility with extension modules that
662 used this API; core interpreter code should call
663 PyEval_EvalFrameEx() */
664 return PyEval_EvalFrameEx(f
, 0);
668 PyEval_EvalFrameEx(PyFrameObject
*f
, int throwflag
)
673 register PyObject
**stack_pointer
; /* Next free slot in value stack */
674 register unsigned char *next_instr
;
675 register int opcode
; /* Current opcode */
676 register int oparg
; /* Current opcode argument, if any */
677 register enum why_code why
; /* Reason for block stack unwind */
678 register int err
; /* Error status -- nonzero if error */
679 register PyObject
*x
; /* Result object -- NULL if error */
680 register PyObject
*v
; /* Temporary objects popped off stack */
681 register PyObject
*w
;
682 register PyObject
*u
;
683 register PyObject
*t
;
684 register PyObject
*stream
= NULL
; /* for PRINT opcodes */
685 register PyObject
**fastlocals
, **freevars
;
686 PyObject
*retval
= NULL
; /* Return value */
687 PyThreadState
*tstate
= PyThreadState_GET();
690 /* when tracing we set things up so that
692 not (instr_lb <= current_bytecode_offset < instr_ub)
694 is true when the line being executed has changed. The
695 initial values are such as to make this false the first
696 time it is tested. */
697 int instr_ub
= -1, instr_lb
= 0, instr_prev
= -1;
699 unsigned char *first_instr
;
702 #if defined(Py_DEBUG) || defined(LLTRACE)
703 /* Make it easier to find out where we are with a debugger */
707 /* Tuple access macros */
710 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
712 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
716 /* Use Pentium timestamp counter to mark certain events:
717 inst0 -- beginning of switch statement for opcode dispatch
718 inst1 -- end of switch statement (may be skipped)
719 loop0 -- the top of the mainloop
720 loop1 -- place where control returns again to top of mainloop
722 intr1 -- beginning of long interruption
723 intr2 -- end of long interruption
725 Many opcodes call out to helper C functions. In some cases, the
726 time in those functions should be counted towards the time for the
727 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
728 calls another Python function; there's no point in charge all the
729 bytecode executed by the called function to the caller.
731 It's hard to make a useful judgement statically. In the presence
732 of operator overloading, it's impossible to tell if a call will
733 execute new Python code or not.
735 It's a case-by-case judgement. I'll use intr1 for the following
741 CALL_FUNCTION (and friends)
744 uint64 inst0
, inst1
, loop0
, loop1
, intr0
= 0, intr1
= 0;
747 READ_TIMESTAMP(inst0
);
748 READ_TIMESTAMP(inst1
);
749 READ_TIMESTAMP(loop0
);
750 READ_TIMESTAMP(loop1
);
752 /* shut up the compiler */
756 /* Code access macros */
758 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
759 #define NEXTOP() (*next_instr++)
760 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
761 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
762 #define JUMPTO(x) (next_instr = first_instr + (x))
763 #define JUMPBY(x) (next_instr += (x))
765 /* OpCode prediction macros
766 Some opcodes tend to come in pairs thus making it possible to
767 predict the second code when the first is run. For example,
768 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
769 followed by STORE_FAST or UNPACK_SEQUENCE.
771 Verifying the prediction costs a single high-speed test of a register
772 variable against a constant. If the pairing was good, then the
773 processor's own internal branch predication has a high likelihood of
774 success, resulting in a nearly zero-overhead transition to the
775 next opcode. A successful prediction saves a trip through the eval-loop
776 including its two unpredictable branches, the HAS_ARG test and the
777 switch-case. Combined with the processor's internal branch prediction,
778 a successful PREDICT has the effect of making the two opcodes run as if
779 they were a single new opcode with the bodies combined.
781 If collecting opcode statistics, your choices are to either keep the
782 predictions turned-on and interpret the results as if some opcodes
783 had been combined or turn-off predictions so that the opcode frequency
784 counter updates for both opcodes.
787 #ifdef DYNAMIC_EXECUTION_PROFILE
788 #define PREDICT(op) if (0) goto PRED_##op
790 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
793 #define PREDICTED(op) PRED_##op: next_instr++
794 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
796 /* Stack manipulation macros */
798 /* The stack can grow at most MAXINT deep, as co_nlocals and
799 co_stacksize are ints. */
800 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
801 #define EMPTY() (STACK_LEVEL() == 0)
802 #define TOP() (stack_pointer[-1])
803 #define SECOND() (stack_pointer[-2])
804 #define THIRD() (stack_pointer[-3])
805 #define FOURTH() (stack_pointer[-4])
806 #define PEEK(n) (stack_pointer[-(n)])
807 #define SET_TOP(v) (stack_pointer[-1] = (v))
808 #define SET_SECOND(v) (stack_pointer[-2] = (v))
809 #define SET_THIRD(v) (stack_pointer[-3] = (v))
810 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
811 #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
812 #define BASIC_STACKADJ(n) (stack_pointer += n)
813 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
814 #define BASIC_POP() (*--stack_pointer)
817 #define PUSH(v) { (void)(BASIC_PUSH(v), \
818 lltrace && prtrace(TOP(), "push")); \
819 assert(STACK_LEVEL() <= co->co_stacksize); }
820 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
822 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
823 lltrace && prtrace(TOP(), "stackadj")); \
824 assert(STACK_LEVEL() <= co->co_stacksize); }
825 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
826 prtrace((STACK_POINTER)[-1], "ext_pop")), \
829 #define PUSH(v) BASIC_PUSH(v)
830 #define POP() BASIC_POP()
831 #define STACKADJ(n) BASIC_STACKADJ(n)
832 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
835 /* Local variable macros */
837 #define GETLOCAL(i) (fastlocals[i])
839 /* The SETLOCAL() macro must not DECREF the local variable in-place and
840 then store the new value; it must copy the old value to a temporary
841 value, then store the new value, and then DECREF the temporary value.
842 This is because it is possible that during the DECREF the frame is
843 accessed by other code (e.g. a __del__ method or gc.collect()) and the
844 variable would be pointing to already-freed memory. */
845 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
846 GETLOCAL(i) = value; \
847 Py_XDECREF(tmp); } while (0)
855 if (Py_EnterRecursiveCall(""))
860 if (tstate
->use_tracing
) {
861 if (tstate
->c_tracefunc
!= NULL
) {
862 /* tstate->c_tracefunc, if defined, is a
863 function that will be called on *every* entry
864 to a code block. Its return value, if not
865 None, is a function that will be called at
866 the start of each executed line of code.
867 (Actually, the function must return itself
868 in order to continue tracing.) The trace
869 functions are called with three arguments:
870 a pointer to the current frame, a string
871 indicating why the function is called, and
872 an argument which depends on the situation.
873 The global trace function is also called
874 whenever an exception is detected. */
875 if (call_trace_protected(tstate
->c_tracefunc
,
877 f
, PyTrace_CALL
, Py_None
)) {
878 /* Trace function raised an error */
879 goto exit_eval_frame
;
882 if (tstate
->c_profilefunc
!= NULL
) {
883 /* Similar for c_profilefunc, except it needn't
884 return itself and isn't called for "line" events */
885 if (call_trace_protected(tstate
->c_profilefunc
,
886 tstate
->c_profileobj
,
887 f
, PyTrace_CALL
, Py_None
)) {
888 /* Profile function raised an error */
889 goto exit_eval_frame
;
895 names
= co
->co_names
;
896 consts
= co
->co_consts
;
897 fastlocals
= f
->f_localsplus
;
898 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
899 first_instr
= (unsigned char*) PyString_AS_STRING(co
->co_code
);
900 /* An explanation is in order for the next line.
902 f->f_lasti now refers to the index of the last instruction
903 executed. You might think this was obvious from the name, but
904 this wasn't always true before 2.3! PyFrame_New now sets
905 f->f_lasti to -1 (i.e. the index *before* the first instruction)
906 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
909 When the PREDICT() macros are enabled, some opcode pairs follow in
910 direct succession without updating f->f_lasti. A successful
911 prediction effectively links the two codes together as if they
912 were a single new opcode; accordingly,f->f_lasti will point to
913 the first code in the pair (for instance, GET_ITER followed by
914 FOR_ITER is effectively a single opcode and f->f_lasti will point
915 at to the beginning of the combined pair.)
917 next_instr
= first_instr
+ f
->f_lasti
+ 1;
918 stack_pointer
= f
->f_stacktop
;
919 assert(stack_pointer
!= NULL
);
920 f
->f_stacktop
= NULL
; /* remains NULL unless yield suspends frame */
923 lltrace
= PyDict_GetItemString(f
->f_globals
, "__lltrace__") != NULL
;
925 #if defined(Py_DEBUG) || defined(LLTRACE)
926 filename
= PyString_AsString(co
->co_filename
);
931 x
= Py_None
; /* Not a reference, just anything non-NULL */
934 if (throwflag
) { /* support for generator.throw() */
942 /* Almost surely, the opcode executed a break
943 or a continue, preventing inst1 from being set
944 on the way out of the loop.
946 READ_TIMESTAMP(inst1
);
949 dump_tsc(opcode
, ticked
, inst0
, inst1
, loop0
, loop1
,
955 READ_TIMESTAMP(loop0
);
957 assert(stack_pointer
>= f
->f_valuestack
); /* else underflow */
958 assert(STACK_LEVEL() <= co
->co_stacksize
); /* else overflow */
960 /* Do periodic things. Doing this every time through
961 the loop would add too much overhead, so we do it
962 only every Nth instruction. We also do it if
963 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
964 event needs attention (e.g. a signal handler or
965 async I/O handler); see Py_AddPendingCall() and
966 Py_MakePendingCalls() above. */
968 if (--_Py_Ticker
< 0) {
969 if (*next_instr
== SETUP_FINALLY
) {
970 /* Make the last opcode before
971 a try: finally: block uninterruptable. */
972 goto fast_next_opcode
;
974 _Py_Ticker
= _Py_CheckInterval
;
975 tstate
->tick_counter
++;
979 if (pendingcalls_to_do
) {
980 if (Py_MakePendingCalls() < 0) {
984 if (pendingcalls_to_do
)
985 /* MakePendingCalls() didn't succeed.
986 Force early re-execution of this
987 "periodic" code, possibly after
992 if (interpreter_lock
) {
993 /* Give another thread a chance */
995 if (PyThreadState_Swap(NULL
) != tstate
)
996 Py_FatalError("ceval: tstate mix-up");
997 PyThread_release_lock(interpreter_lock
);
999 /* Other threads may run now */
1001 PyThread_acquire_lock(interpreter_lock
, 1);
1002 if (PyThreadState_Swap(tstate
) != NULL
)
1003 Py_FatalError("ceval: orphan tstate");
1005 /* Check for thread interrupts */
1007 if (tstate
->async_exc
!= NULL
) {
1008 x
= tstate
->async_exc
;
1009 tstate
->async_exc
= NULL
;
1012 why
= WHY_EXCEPTION
;
1020 f
->f_lasti
= INSTR_OFFSET();
1022 /* line-by-line tracing support */
1024 if (_Py_TracingPossible
&&
1025 tstate
->c_tracefunc
!= NULL
&& !tstate
->tracing
) {
1026 /* see maybe_call_line_trace
1027 for expository comments */
1028 f
->f_stacktop
= stack_pointer
;
1030 err
= maybe_call_line_trace(tstate
->c_tracefunc
,
1032 f
, &instr_lb
, &instr_ub
,
1034 /* Reload possibly changed frame fields */
1036 if (f
->f_stacktop
!= NULL
) {
1037 stack_pointer
= f
->f_stacktop
;
1038 f
->f_stacktop
= NULL
;
1041 /* trace function raised an exception */
1046 /* Extract opcode and argument */
1049 oparg
= 0; /* allows oparg to be stored in a register because
1050 it doesn't have to be remembered across a full loop */
1051 if (HAS_ARG(opcode
))
1054 #ifdef DYNAMIC_EXECUTION_PROFILE
1056 dxpairs
[lastopcode
][opcode
]++;
1057 lastopcode
= opcode
;
1063 /* Instruction tracing */
1066 if (HAS_ARG(opcode
)) {
1067 printf("%d: %d, %d\n",
1068 f
->f_lasti
, opcode
, oparg
);
1072 f
->f_lasti
, opcode
);
1077 /* Main switch on opcode */
1078 READ_TIMESTAMP(inst0
);
1083 It is essential that any operation that fails sets either
1084 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1085 and that no operation that succeeds does this! */
1087 /* case STOP_CODE: this is an error! */
1090 goto fast_next_opcode
;
1093 x
= GETLOCAL(oparg
);
1097 goto fast_next_opcode
;
1099 format_exc_check_arg(PyExc_UnboundLocalError
,
1100 UNBOUNDLOCAL_ERROR_MSG
,
1101 PyTuple_GetItem(co
->co_varnames
, oparg
));
1105 x
= GETITEM(consts
, oparg
);
1108 goto fast_next_opcode
;
1110 PREDICTED_WITH_ARG(STORE_FAST
);
1114 goto fast_next_opcode
;
1119 goto fast_next_opcode
;
1126 goto fast_next_opcode
;
1135 goto fast_next_opcode
;
1146 goto fast_next_opcode
;
1152 goto fast_next_opcode
;
1163 goto fast_next_opcode
;
1164 } else if (oparg
== 3) {
1175 goto fast_next_opcode
;
1177 Py_FatalError("invalid argument to DUP_TOPX"
1178 " (bytecode corruption?)");
1179 /* Never returns, so don't bother to set why. */
1182 case UNARY_POSITIVE
:
1184 x
= PyNumber_Positive(v
);
1187 if (x
!= NULL
) continue;
1190 case UNARY_NEGATIVE
:
1192 x
= PyNumber_Negative(v
);
1195 if (x
!= NULL
) continue;
1200 err
= PyObject_IsTrue(v
);
1208 Py_INCREF(Py_False
);
1218 x
= PyObject_Repr(v
);
1221 if (x
!= NULL
) continue;
1226 x
= PyNumber_Invert(v
);
1229 if (x
!= NULL
) continue;
1235 x
= PyNumber_Power(v
, w
, Py_None
);
1239 if (x
!= NULL
) continue;
1242 case BINARY_MULTIPLY
:
1245 x
= PyNumber_Multiply(v
, w
);
1249 if (x
!= NULL
) continue;
1253 if (!_Py_QnewFlag
) {
1256 x
= PyNumber_Divide(v
, w
);
1260 if (x
!= NULL
) continue;
1263 /* -Qnew is in effect: fall through to
1264 BINARY_TRUE_DIVIDE */
1265 case BINARY_TRUE_DIVIDE
:
1268 x
= PyNumber_TrueDivide(v
, w
);
1272 if (x
!= NULL
) continue;
1275 case BINARY_FLOOR_DIVIDE
:
1278 x
= PyNumber_FloorDivide(v
, w
);
1282 if (x
!= NULL
) continue;
1288 if (PyString_CheckExact(v
))
1289 x
= PyString_Format(v
, w
);
1291 x
= PyNumber_Remainder(v
, w
);
1295 if (x
!= NULL
) continue;
1301 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1302 /* INLINE: int + int */
1303 register long a
, b
, i
;
1304 a
= PyInt_AS_LONG(v
);
1305 b
= PyInt_AS_LONG(w
);
1307 if ((i
^a
) < 0 && (i
^b
) < 0)
1309 x
= PyInt_FromLong(i
);
1311 else if (PyString_CheckExact(v
) &&
1312 PyString_CheckExact(w
)) {
1313 x
= string_concatenate(v
, w
, f
, next_instr
);
1314 /* string_concatenate consumed the ref to v */
1315 goto skip_decref_vx
;
1319 x
= PyNumber_Add(v
, w
);
1325 if (x
!= NULL
) continue;
1328 case BINARY_SUBTRACT
:
1331 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1332 /* INLINE: int - int */
1333 register long a
, b
, i
;
1334 a
= PyInt_AS_LONG(v
);
1335 b
= PyInt_AS_LONG(w
);
1337 if ((i
^a
) < 0 && (i
^~b
) < 0)
1339 x
= PyInt_FromLong(i
);
1343 x
= PyNumber_Subtract(v
, w
);
1348 if (x
!= NULL
) continue;
1354 if (PyList_CheckExact(v
) && PyInt_CheckExact(w
)) {
1355 /* INLINE: list[int] */
1356 Py_ssize_t i
= PyInt_AsSsize_t(w
);
1358 i
+= PyList_GET_SIZE(v
);
1359 if (i
>= 0 && i
< PyList_GET_SIZE(v
)) {
1360 x
= PyList_GET_ITEM(v
, i
);
1368 x
= PyObject_GetItem(v
, w
);
1372 if (x
!= NULL
) continue;
1378 x
= PyNumber_Lshift(v
, w
);
1382 if (x
!= NULL
) continue;
1388 x
= PyNumber_Rshift(v
, w
);
1392 if (x
!= NULL
) continue;
1398 x
= PyNumber_And(v
, w
);
1402 if (x
!= NULL
) continue;
1408 x
= PyNumber_Xor(v
, w
);
1412 if (x
!= NULL
) continue;
1418 x
= PyNumber_Or(v
, w
);
1422 if (x
!= NULL
) continue;
1428 err
= PyList_Append(v
, w
);
1431 PREDICT(JUMP_ABSOLUTE
);
1439 x
= PyNumber_InPlacePower(v
, w
, Py_None
);
1443 if (x
!= NULL
) continue;
1446 case INPLACE_MULTIPLY
:
1449 x
= PyNumber_InPlaceMultiply(v
, w
);
1453 if (x
!= NULL
) continue;
1456 case INPLACE_DIVIDE
:
1457 if (!_Py_QnewFlag
) {
1460 x
= PyNumber_InPlaceDivide(v
, w
);
1464 if (x
!= NULL
) continue;
1467 /* -Qnew is in effect: fall through to
1468 INPLACE_TRUE_DIVIDE */
1469 case INPLACE_TRUE_DIVIDE
:
1472 x
= PyNumber_InPlaceTrueDivide(v
, w
);
1476 if (x
!= NULL
) continue;
1479 case INPLACE_FLOOR_DIVIDE
:
1482 x
= PyNumber_InPlaceFloorDivide(v
, w
);
1486 if (x
!= NULL
) continue;
1489 case INPLACE_MODULO
:
1492 x
= PyNumber_InPlaceRemainder(v
, w
);
1496 if (x
!= NULL
) continue;
1502 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1503 /* INLINE: int + int */
1504 register long a
, b
, i
;
1505 a
= PyInt_AS_LONG(v
);
1506 b
= PyInt_AS_LONG(w
);
1508 if ((i
^a
) < 0 && (i
^b
) < 0)
1510 x
= PyInt_FromLong(i
);
1512 else if (PyString_CheckExact(v
) &&
1513 PyString_CheckExact(w
)) {
1514 x
= string_concatenate(v
, w
, f
, next_instr
);
1515 /* string_concatenate consumed the ref to v */
1520 x
= PyNumber_InPlaceAdd(v
, w
);
1526 if (x
!= NULL
) continue;
1529 case INPLACE_SUBTRACT
:
1532 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1533 /* INLINE: int - int */
1534 register long a
, b
, i
;
1535 a
= PyInt_AS_LONG(v
);
1536 b
= PyInt_AS_LONG(w
);
1538 if ((i
^a
) < 0 && (i
^~b
) < 0)
1540 x
= PyInt_FromLong(i
);
1544 x
= PyNumber_InPlaceSubtract(v
, w
);
1549 if (x
!= NULL
) continue;
1552 case INPLACE_LSHIFT
:
1555 x
= PyNumber_InPlaceLshift(v
, w
);
1559 if (x
!= NULL
) continue;
1562 case INPLACE_RSHIFT
:
1565 x
= PyNumber_InPlaceRshift(v
, w
);
1569 if (x
!= NULL
) continue;
1575 x
= PyNumber_InPlaceAnd(v
, w
);
1579 if (x
!= NULL
) continue;
1585 x
= PyNumber_InPlaceXor(v
, w
);
1589 if (x
!= NULL
) continue;
1595 x
= PyNumber_InPlaceOr(v
, w
);
1599 if (x
!= NULL
) continue;
1606 if ((opcode
-SLICE
) & 2)
1610 if ((opcode
-SLICE
) & 1)
1615 x
= apply_slice(u
, v
, w
);
1620 if (x
!= NULL
) continue;
1627 if ((opcode
-STORE_SLICE
) & 2)
1631 if ((opcode
-STORE_SLICE
) & 1)
1637 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
1642 if (err
== 0) continue;
1645 case DELETE_SLICE
+0:
1646 case DELETE_SLICE
+1:
1647 case DELETE_SLICE
+2:
1648 case DELETE_SLICE
+3:
1649 if ((opcode
-DELETE_SLICE
) & 2)
1653 if ((opcode
-DELETE_SLICE
) & 1)
1658 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
1663 if (err
== 0) continue;
1672 err
= PyObject_SetItem(v
, w
, u
);
1676 if (err
== 0) continue;
1684 err
= PyObject_DelItem(v
, w
);
1687 if (err
== 0) continue;
1692 w
= PySys_GetObject("displayhook");
1694 PyErr_SetString(PyExc_RuntimeError
,
1695 "lost sys.displayhook");
1700 x
= PyTuple_Pack(1, v
);
1705 w
= PyEval_CallObject(w
, x
);
1716 /* fall through to PRINT_ITEM */
1720 if (stream
== NULL
|| stream
== Py_None
) {
1721 w
= PySys_GetObject("stdout");
1723 PyErr_SetString(PyExc_RuntimeError
,
1728 /* PyFile_SoftSpace() can exececute arbitrary code
1729 if sys.stdout is an instance with a __getattr__.
1730 If __getattr__ raises an exception, w will
1731 be freed, so we need to prevent that temporarily. */
1733 if (w
!= NULL
&& PyFile_SoftSpace(w
, 0))
1734 err
= PyFile_WriteString(" ", w
);
1736 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1738 /* XXX move into writeobject() ? */
1739 if (PyString_Check(v
)) {
1740 char *s
= PyString_AS_STRING(v
);
1741 Py_ssize_t len
= PyString_GET_SIZE(v
);
1743 !isspace(Py_CHARMASK(s
[len
-1])) ||
1745 PyFile_SoftSpace(w
, 1);
1747 #ifdef Py_USING_UNICODE
1748 else if (PyUnicode_Check(v
)) {
1749 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(v
);
1750 Py_ssize_t len
= PyUnicode_GET_SIZE(v
);
1752 !Py_UNICODE_ISSPACE(s
[len
-1]) ||
1754 PyFile_SoftSpace(w
, 1);
1758 PyFile_SoftSpace(w
, 1);
1768 case PRINT_NEWLINE_TO
:
1770 /* fall through to PRINT_NEWLINE */
1773 if (stream
== NULL
|| stream
== Py_None
) {
1774 w
= PySys_GetObject("stdout");
1776 PyErr_SetString(PyExc_RuntimeError
,
1778 why
= WHY_EXCEPTION
;
1782 /* w.write() may replace sys.stdout, so we
1783 * have to keep our reference to it */
1785 err
= PyFile_WriteString("\n", w
);
1787 PyFile_SoftSpace(w
, 0);
1796 default: switch (opcode
) {
1802 u
= POP(); /* traceback */
1805 v
= POP(); /* value */
1808 w
= POP(); /* exc */
1809 case 0: /* Fallthrough */
1810 why
= do_raise(w
, v
, u
);
1813 PyErr_SetString(PyExc_SystemError
,
1814 "bad RAISE_VARARGS oparg");
1815 why
= WHY_EXCEPTION
;
1821 if ((x
= f
->f_locals
) != NULL
) {
1826 PyErr_SetString(PyExc_SystemError
, "no locals");
1832 goto fast_block_end
;
1836 f
->f_stacktop
= stack_pointer
;
1845 READ_TIMESTAMP(intr0
);
1846 err
= exec_statement(f
, u
, v
, w
);
1847 READ_TIMESTAMP(intr1
);
1855 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1856 while (STACK_LEVEL() > b
->b_level
) {
1863 PREDICTED(END_FINALLY
);
1866 if (PyInt_Check(v
)) {
1867 why
= (enum why_code
) PyInt_AS_LONG(v
);
1868 assert(why
!= WHY_YIELD
);
1869 if (why
== WHY_RETURN
||
1870 why
== WHY_CONTINUE
)
1873 else if (PyExceptionClass_Check(v
) ||
1874 PyString_Check(v
)) {
1877 PyErr_Restore(v
, w
, u
);
1881 else if (v
!= Py_None
) {
1882 PyErr_SetString(PyExc_SystemError
,
1883 "'finally' pops bad exception");
1884 why
= WHY_EXCEPTION
;
1894 x
= build_class(u
, v
, w
);
1902 w
= GETITEM(names
, oparg
);
1904 if ((x
= f
->f_locals
) != NULL
) {
1905 if (PyDict_CheckExact(x
))
1906 err
= PyDict_SetItem(x
, w
, v
);
1908 err
= PyObject_SetItem(x
, w
, v
);
1910 if (err
== 0) continue;
1913 PyErr_Format(PyExc_SystemError
,
1914 "no locals found when storing %s",
1919 w
= GETITEM(names
, oparg
);
1920 if ((x
= f
->f_locals
) != NULL
) {
1921 if ((err
= PyObject_DelItem(x
, w
)) != 0)
1922 format_exc_check_arg(PyExc_NameError
,
1927 PyErr_Format(PyExc_SystemError
,
1928 "no locals when deleting %s",
1932 PREDICTED_WITH_ARG(UNPACK_SEQUENCE
);
1933 case UNPACK_SEQUENCE
:
1935 if (PyTuple_CheckExact(v
) &&
1936 PyTuple_GET_SIZE(v
) == oparg
) {
1937 PyObject
**items
= \
1938 ((PyTupleObject
*)v
)->ob_item
;
1946 } else if (PyList_CheckExact(v
) &&
1947 PyList_GET_SIZE(v
) == oparg
) {
1948 PyObject
**items
= \
1949 ((PyListObject
*)v
)->ob_item
;
1955 } else if (unpack_iterable(v
, oparg
,
1956 stack_pointer
+ oparg
)) {
1959 /* unpack_iterable() raised an exception */
1960 why
= WHY_EXCEPTION
;
1966 w
= GETITEM(names
, oparg
);
1970 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1973 if (err
== 0) continue;
1977 w
= GETITEM(names
, oparg
);
1979 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1985 w
= GETITEM(names
, oparg
);
1987 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1989 if (err
== 0) continue;
1993 w
= GETITEM(names
, oparg
);
1994 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1995 format_exc_check_arg(
1996 PyExc_NameError
, GLOBAL_NAME_ERROR_MSG
, w
);
2000 w
= GETITEM(names
, oparg
);
2001 if ((v
= f
->f_locals
) == NULL
) {
2002 PyErr_Format(PyExc_SystemError
,
2003 "no locals when loading %s",
2005 why
= WHY_EXCEPTION
;
2008 if (PyDict_CheckExact(v
)) {
2009 x
= PyDict_GetItem(v
, w
);
2013 x
= PyObject_GetItem(v
, w
);
2014 if (x
== NULL
&& PyErr_Occurred()) {
2015 if (!PyErr_ExceptionMatches(
2022 x
= PyDict_GetItem(f
->f_globals
, w
);
2024 x
= PyDict_GetItem(f
->f_builtins
, w
);
2026 format_exc_check_arg(
2038 w
= GETITEM(names
, oparg
);
2039 if (PyString_CheckExact(w
)) {
2040 /* Inline the PyDict_GetItem() calls.
2041 WARNING: this is an extreme speed hack.
2042 Do not try this at home. */
2043 long hash
= ((PyStringObject
*)w
)->ob_shash
;
2047 d
= (PyDictObject
*)(f
->f_globals
);
2048 e
= d
->ma_lookup(d
, w
, hash
);
2059 d
= (PyDictObject
*)(f
->f_builtins
);
2060 e
= d
->ma_lookup(d
, w
, hash
);
2071 goto load_global_error
;
2074 /* This is the un-inlined version of the code above */
2075 x
= PyDict_GetItem(f
->f_globals
, w
);
2077 x
= PyDict_GetItem(f
->f_builtins
, w
);
2080 format_exc_check_arg(
2082 GLOBAL_NAME_ERROR_MSG
, w
);
2091 x
= GETLOCAL(oparg
);
2093 SETLOCAL(oparg
, NULL
);
2096 format_exc_check_arg(
2097 PyExc_UnboundLocalError
,
2098 UNBOUNDLOCAL_ERROR_MSG
,
2099 PyTuple_GetItem(co
->co_varnames
, oparg
)
2104 x
= freevars
[oparg
];
2107 if (x
!= NULL
) continue;
2111 x
= freevars
[oparg
];
2118 /* Don't stomp existing exception */
2119 if (PyErr_Occurred())
2121 if (oparg
< PyTuple_GET_SIZE(co
->co_cellvars
)) {
2122 v
= PyTuple_GET_ITEM(co
->co_cellvars
,
2124 format_exc_check_arg(
2125 PyExc_UnboundLocalError
,
2126 UNBOUNDLOCAL_ERROR_MSG
,
2129 v
= PyTuple_GET_ITEM(co
->co_freevars
, oparg
-
2130 PyTuple_GET_SIZE(co
->co_cellvars
));
2131 format_exc_check_arg(PyExc_NameError
,
2132 UNBOUNDFREE_ERROR_MSG
, v
);
2138 x
= freevars
[oparg
];
2144 x
= PyTuple_New(oparg
);
2146 for (; --oparg
>= 0;) {
2148 PyTuple_SET_ITEM(x
, oparg
, w
);
2156 x
= PyList_New(oparg
);
2158 for (; --oparg
>= 0;) {
2160 PyList_SET_ITEM(x
, oparg
, w
);
2168 x
= _PyDict_NewPresized((Py_ssize_t
)oparg
);
2170 if (x
!= NULL
) continue;
2174 w
= TOP(); /* key */
2175 u
= SECOND(); /* value */
2176 v
= THIRD(); /* dict */
2178 assert (PyDict_CheckExact(v
));
2179 err
= PyDict_SetItem(v
, w
, u
); /* v[w] = u */
2182 if (err
== 0) continue;
2186 w
= GETITEM(names
, oparg
);
2188 x
= PyObject_GetAttr(v
, w
);
2191 if (x
!= NULL
) continue;
2197 if (PyInt_CheckExact(w
) && PyInt_CheckExact(v
)) {
2198 /* INLINE: cmp(int, int) */
2201 a
= PyInt_AS_LONG(v
);
2202 b
= PyInt_AS_LONG(w
);
2204 case PyCmp_LT
: res
= a
< b
; break;
2205 case PyCmp_LE
: res
= a
<= b
; break;
2206 case PyCmp_EQ
: res
= a
== b
; break;
2207 case PyCmp_NE
: res
= a
!= b
; break;
2208 case PyCmp_GT
: res
= a
> b
; break;
2209 case PyCmp_GE
: res
= a
>= b
; break;
2210 case PyCmp_IS
: res
= v
== w
; break;
2211 case PyCmp_IS_NOT
: res
= v
!= w
; break;
2212 default: goto slow_compare
;
2214 x
= res
? Py_True
: Py_False
;
2219 x
= cmp_outcome(oparg
, v
, w
);
2224 if (x
== NULL
) break;
2225 PREDICT(POP_JUMP_IF_FALSE
);
2226 PREDICT(POP_JUMP_IF_TRUE
);
2230 w
= GETITEM(names
, oparg
);
2231 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
2233 PyErr_SetString(PyExc_ImportError
,
2234 "__import__ not found");
2240 if (PyInt_AsLong(u
) != -1 || PyErr_Occurred())
2244 f
->f_locals
== NULL
?
2245 Py_None
: f
->f_locals
,
2252 f
->f_locals
== NULL
?
2253 Py_None
: f
->f_locals
,
2263 READ_TIMESTAMP(intr0
);
2265 x
= PyEval_CallObject(v
, w
);
2267 READ_TIMESTAMP(intr1
);
2270 if (x
!= NULL
) continue;
2275 PyFrame_FastToLocals(f
);
2276 if ((x
= f
->f_locals
) == NULL
) {
2277 PyErr_SetString(PyExc_SystemError
,
2278 "no locals found during 'import *'");
2281 READ_TIMESTAMP(intr0
);
2282 err
= import_all_from(x
, v
);
2283 READ_TIMESTAMP(intr1
);
2284 PyFrame_LocalsToFast(f
, 0);
2286 if (err
== 0) continue;
2290 w
= GETITEM(names
, oparg
);
2292 READ_TIMESTAMP(intr0
);
2293 x
= import_from(v
, w
);
2294 READ_TIMESTAMP(intr1
);
2296 if (x
!= NULL
) continue;
2301 goto fast_next_opcode
;
2303 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE
);
2304 case POP_JUMP_IF_FALSE
:
2308 goto fast_next_opcode
;
2310 if (w
== Py_False
) {
2313 goto fast_next_opcode
;
2315 err
= PyObject_IsTrue(w
);
2325 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE
);
2326 case POP_JUMP_IF_TRUE
:
2328 if (w
== Py_False
) {
2330 goto fast_next_opcode
;
2335 goto fast_next_opcode
;
2337 err
= PyObject_IsTrue(w
);
2349 case JUMP_IF_FALSE_OR_POP
:
2354 goto fast_next_opcode
;
2356 if (w
== Py_False
) {
2358 goto fast_next_opcode
;
2360 err
= PyObject_IsTrue(w
);
2372 case JUMP_IF_TRUE_OR_POP
:
2374 if (w
== Py_False
) {
2377 goto fast_next_opcode
;
2381 goto fast_next_opcode
;
2383 err
= PyObject_IsTrue(w
);
2388 else if (err
== 0) {
2396 PREDICTED_WITH_ARG(JUMP_ABSOLUTE
);
2400 /* Enabling this path speeds-up all while and for-loops by bypassing
2401 the per-loop checks for signals. By default, this should be turned-off
2402 because it prevents detection of a control-break in tight loops like
2403 "while 1: pass". Compile with this option turned-on when you need
2404 the speed-up and do not need break checking inside tight loops (ones
2405 that contain only instructions ending with goto fast_next_opcode).
2407 goto fast_next_opcode
;
2413 /* before: [obj]; after [getiter(obj)] */
2415 x
= PyObject_GetIter(v
);
2425 PREDICTED_WITH_ARG(FOR_ITER
);
2427 /* before: [iter]; after: [iter, iter()] *or* [] */
2429 x
= (*v
->ob_type
->tp_iternext
)(v
);
2432 PREDICT(STORE_FAST
);
2433 PREDICT(UNPACK_SEQUENCE
);
2436 if (PyErr_Occurred()) {
2437 if (!PyErr_ExceptionMatches(
2438 PyExc_StopIteration
))
2442 /* iterator ended normally */
2450 goto fast_block_end
;
2453 retval
= PyInt_FromLong(oparg
);
2459 goto fast_block_end
;
2464 /* NOTE: If you add any new block-setup opcodes that
2465 are not try/except/finally handlers, you may need
2466 to update the PyGen_NeedsFinalizing() function.
2469 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
2475 static PyObject
*exit
, *enter
;
2477 x
= special_lookup(w
, "__exit__", &exit
);
2481 u
= special_lookup(w
, "__enter__", &enter
);
2487 x
= PyObject_CallFunctionObjArgs(u
, NULL
);
2491 /* Setup the finally block before pushing the result
2492 of __enter__ on the stack. */
2493 PyFrame_BlockSetup(f
, SETUP_FINALLY
, INSTR_OFFSET() + oparg
,
2502 /* At the top of the stack are 1-3 values indicating
2503 how/why we entered the finally clause:
2505 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2506 - TOP = WHY_*; no retval below it
2507 - (TOP, SECOND, THIRD) = exc_info()
2508 Below them is EXIT, the context.__exit__ bound method.
2509 In the last case, we must call
2510 EXIT(TOP, SECOND, THIRD)
2511 otherwise we must call
2512 EXIT(None, None, None)
2514 In all cases, we remove EXIT from the stack, leaving
2515 the rest in the same order.
2517 In addition, if the stack represents an exception,
2518 *and* the function call returns a 'true' value, we
2519 "zap" this information, to prevent END_FINALLY from
2520 re-raising the exception. (But non-local gotos
2521 should still be resumed.)
2524 PyObject
*exit_func
;
2532 else if (PyInt_Check(u
)) {
2533 switch(PyInt_AS_LONG(u
)) {
2536 /* Retval in TOP. */
2537 exit_func
= SECOND();
2546 u
= v
= w
= Py_None
;
2551 exit_func
= THIRD();
2556 /* XXX Not the fastest way to call it... */
2557 x
= PyObject_CallFunctionObjArgs(exit_func
, u
, v
, w
,
2559 Py_DECREF(exit_func
);
2561 break; /* Go to error exit */
2564 err
= PyObject_IsTrue(x
);
2570 break; /* Go to error exit */
2573 /* There was an exception and a true return */
2581 /* The stack was rearranged to remove EXIT
2582 above. Let END_FINALLY do its thing */
2584 PREDICT(END_FINALLY
);
2594 x
= call_function(&sp
, oparg
, &intr0
, &intr1
);
2596 x
= call_function(&sp
, oparg
);
2605 case CALL_FUNCTION_VAR
:
2606 case CALL_FUNCTION_KW
:
2607 case CALL_FUNCTION_VAR_KW
:
2609 int na
= oparg
& 0xff;
2610 int nk
= (oparg
>>8) & 0xff;
2611 int flags
= (opcode
- CALL_FUNCTION
) & 3;
2612 int n
= na
+ 2 * nk
;
2613 PyObject
**pfunc
, *func
, **sp
;
2615 if (flags
& CALL_FLAG_VAR
)
2617 if (flags
& CALL_FLAG_KW
)
2619 pfunc
= stack_pointer
- n
- 1;
2622 if (PyMethod_Check(func
)
2623 && PyMethod_GET_SELF(func
) != NULL
) {
2624 PyObject
*self
= PyMethod_GET_SELF(func
);
2626 func
= PyMethod_GET_FUNCTION(func
);
2635 READ_TIMESTAMP(intr0
);
2636 x
= ext_do_call(func
, &sp
, flags
, na
, nk
);
2637 READ_TIMESTAMP(intr1
);
2641 while (stack_pointer
> pfunc
) {
2652 v
= POP(); /* code object */
2653 x
= PyFunction_New(v
, f
->f_globals
);
2655 /* XXX Maybe this should be a separate opcode? */
2656 if (x
!= NULL
&& oparg
> 0) {
2657 v
= PyTuple_New(oparg
);
2663 while (--oparg
>= 0) {
2665 PyTuple_SET_ITEM(v
, oparg
, w
);
2667 err
= PyFunction_SetDefaults(x
, v
);
2675 v
= POP(); /* code object */
2676 x
= PyFunction_New(v
, f
->f_globals
);
2680 if (PyFunction_SetClosure(x
, v
) != 0) {
2681 /* Can't happen unless bytecode is corrupt. */
2682 why
= WHY_EXCEPTION
;
2686 if (x
!= NULL
&& oparg
> 0) {
2687 v
= PyTuple_New(oparg
);
2693 while (--oparg
>= 0) {
2695 PyTuple_SET_ITEM(v
, oparg
, w
);
2697 if (PyFunction_SetDefaults(x
, v
) != 0) {
2698 /* Can't happen unless
2699 PyFunction_SetDefaults changes. */
2700 why
= WHY_EXCEPTION
;
2715 x
= PySlice_New(u
, v
, w
);
2720 if (x
!= NULL
) continue;
2725 oparg
= oparg
<<16 | NEXTARG();
2726 goto dispatch_opcode
;
2730 "XXX lineno: %d, opcode: %d\n",
2731 PyFrame_GetLineNumber(f
),
2733 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
2734 why
= WHY_EXCEPTION
;
2745 READ_TIMESTAMP(inst1
);
2747 /* Quickly continue if no error occurred */
2749 if (why
== WHY_NOT
) {
2750 if (err
== 0 && x
!= NULL
) {
2752 /* This check is expensive! */
2753 if (PyErr_Occurred())
2755 "XXX undetected error\n");
2758 READ_TIMESTAMP(loop1
);
2759 continue; /* Normal, fast path */
2764 why
= WHY_EXCEPTION
;
2769 /* Double-check exception status */
2771 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
2772 if (!PyErr_Occurred()) {
2773 PyErr_SetString(PyExc_SystemError
,
2774 "error return without exception set");
2775 why
= WHY_EXCEPTION
;
2780 /* This check is expensive! */
2781 if (PyErr_Occurred()) {
2783 sprintf(buf
, "Stack unwind with exception "
2784 "set and why=%d", why
);
2790 /* Log traceback info if this is a real exception */
2792 if (why
== WHY_EXCEPTION
) {
2793 PyTraceBack_Here(f
);
2795 if (tstate
->c_tracefunc
!= NULL
)
2796 call_exc_trace(tstate
->c_tracefunc
,
2797 tstate
->c_traceobj
, f
);
2800 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2802 if (why
== WHY_RERAISE
)
2803 why
= WHY_EXCEPTION
;
2805 /* Unwind stacks if a (pseudo) exception occurred */
2808 while (why
!= WHY_NOT
&& f
->f_iblock
> 0) {
2809 /* Peek at the current block. */
2810 PyTryBlock
*b
= &f
->f_blockstack
[f
->f_iblock
- 1];
2812 assert(why
!= WHY_YIELD
);
2813 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_CONTINUE
) {
2815 JUMPTO(PyInt_AS_LONG(retval
));
2820 /* Now we have to pop the block. */
2823 while (STACK_LEVEL() > b
->b_level
) {
2827 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
2829 JUMPTO(b
->b_handler
);
2832 if (b
->b_type
== SETUP_FINALLY
||
2833 (b
->b_type
== SETUP_EXCEPT
&&
2834 why
== WHY_EXCEPTION
)) {
2835 if (why
== WHY_EXCEPTION
) {
2836 PyObject
*exc
, *val
, *tb
;
2837 PyErr_Fetch(&exc
, &val
, &tb
);
2842 /* Make the raw exception data
2843 available to the handler,
2844 so a program can emulate the
2845 Python main loop. Don't do
2846 this for 'finally'. */
2847 if (b
->b_type
== SETUP_EXCEPT
) {
2848 PyErr_NormalizeException(
2850 set_exc_info(tstate
,
2862 if (why
& (WHY_RETURN
| WHY_CONTINUE
))
2864 v
= PyInt_FromLong((long)why
);
2868 JUMPTO(b
->b_handler
);
2871 } /* unwind stack */
2873 /* End the loop if we still have an error (or return) */
2877 READ_TIMESTAMP(loop1
);
2881 assert(why
!= WHY_YIELD
);
2882 /* Pop remaining stack entries. */
2888 if (why
!= WHY_RETURN
)
2892 if (tstate
->use_tracing
) {
2893 if (tstate
->c_tracefunc
) {
2894 if (why
== WHY_RETURN
|| why
== WHY_YIELD
) {
2895 if (call_trace(tstate
->c_tracefunc
,
2896 tstate
->c_traceobj
, f
,
2897 PyTrace_RETURN
, retval
)) {
2900 why
= WHY_EXCEPTION
;
2903 else if (why
== WHY_EXCEPTION
) {
2904 call_trace_protected(tstate
->c_tracefunc
,
2905 tstate
->c_traceobj
, f
,
2906 PyTrace_RETURN
, NULL
);
2909 if (tstate
->c_profilefunc
) {
2910 if (why
== WHY_EXCEPTION
)
2911 call_trace_protected(tstate
->c_profilefunc
,
2912 tstate
->c_profileobj
, f
,
2913 PyTrace_RETURN
, NULL
);
2914 else if (call_trace(tstate
->c_profilefunc
,
2915 tstate
->c_profileobj
, f
,
2916 PyTrace_RETURN
, retval
)) {
2919 why
= WHY_EXCEPTION
;
2924 if (tstate
->frame
->f_exc_type
!= NULL
)
2925 reset_exc_info(tstate
);
2927 assert(tstate
->frame
->f_exc_value
== NULL
);
2928 assert(tstate
->frame
->f_exc_traceback
== NULL
);
2933 Py_LeaveRecursiveCall();
2934 tstate
->frame
= f
->f_back
;
2939 /* This is gonna seem *real weird*, but if you put some other code between
2940 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2941 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2944 PyEval_EvalCodeEx(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
,
2945 PyObject
**args
, int argcount
, PyObject
**kws
, int kwcount
,
2946 PyObject
**defs
, int defcount
, PyObject
*closure
)
2948 register PyFrameObject
*f
;
2949 register PyObject
*retval
= NULL
;
2950 register PyObject
**fastlocals
, **freevars
;
2951 PyThreadState
*tstate
= PyThreadState_GET();
2954 if (globals
== NULL
) {
2955 PyErr_SetString(PyExc_SystemError
,
2956 "PyEval_EvalCodeEx: NULL globals");
2960 assert(tstate
!= NULL
);
2961 assert(globals
!= NULL
);
2962 f
= PyFrame_New(tstate
, co
, globals
, locals
);
2966 fastlocals
= f
->f_localsplus
;
2967 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
2969 if (co
->co_argcount
> 0 ||
2970 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
2973 PyObject
*kwdict
= NULL
;
2974 if (co
->co_flags
& CO_VARKEYWORDS
) {
2975 kwdict
= PyDict_New();
2978 i
= co
->co_argcount
;
2979 if (co
->co_flags
& CO_VARARGS
)
2981 SETLOCAL(i
, kwdict
);
2983 if (argcount
> co
->co_argcount
) {
2984 if (!(co
->co_flags
& CO_VARARGS
)) {
2985 PyErr_Format(PyExc_TypeError
,
2986 "%.200s() takes %s %d "
2987 "%sargument%s (%d given)",
2988 PyString_AsString(co
->co_name
),
2989 defcount
? "at most" : "exactly",
2991 kwcount
? "non-keyword " : "",
2992 co
->co_argcount
== 1 ? "" : "s",
2996 n
= co
->co_argcount
;
2998 for (i
= 0; i
< n
; i
++) {
3003 if (co
->co_flags
& CO_VARARGS
) {
3004 u
= PyTuple_New(argcount
- n
);
3007 SETLOCAL(co
->co_argcount
, u
);
3008 for (i
= n
; i
< argcount
; i
++) {
3011 PyTuple_SET_ITEM(u
, i
-n
, x
);
3014 for (i
= 0; i
< kwcount
; i
++) {
3015 PyObject
**co_varnames
;
3016 PyObject
*keyword
= kws
[2*i
];
3017 PyObject
*value
= kws
[2*i
+ 1];
3019 if (keyword
== NULL
|| !(PyString_Check(keyword
)
3020 #ifdef Py_USING_UNICODE
3021 || PyUnicode_Check(keyword
)
3024 PyErr_Format(PyExc_TypeError
,
3025 "%.200s() keywords must be strings",
3026 PyString_AsString(co
->co_name
));
3029 /* Speed hack: do raw pointer compares. As names are
3030 normally interned this should almost always hit. */
3031 co_varnames
= PySequence_Fast_ITEMS(co
->co_varnames
);
3032 for (j
= 0; j
< co
->co_argcount
; j
++) {
3033 PyObject
*nm
= co_varnames
[j
];
3037 /* Slow fallback, just in case */
3038 for (j
= 0; j
< co
->co_argcount
; j
++) {
3039 PyObject
*nm
= co_varnames
[j
];
3040 int cmp
= PyObject_RichCompareBool(
3041 keyword
, nm
, Py_EQ
);
3047 /* Check errors from Compare */
3048 if (PyErr_Occurred())
3050 if (j
>= co
->co_argcount
) {
3051 if (kwdict
== NULL
) {
3052 PyObject
*kwd_str
= kwd_as_string(keyword
);
3054 PyErr_Format(PyExc_TypeError
,
3055 "%.200s() got an unexpected "
3056 "keyword argument '%.400s'",
3057 PyString_AsString(co
->co_name
),
3058 PyString_AsString(kwd_str
));
3063 PyDict_SetItem(kwdict
, keyword
, value
);
3067 if (GETLOCAL(j
) != NULL
) {
3068 PyObject
*kwd_str
= kwd_as_string(keyword
);
3070 PyErr_Format(PyExc_TypeError
,
3071 "%.200s() got multiple "
3072 "values for keyword "
3073 "argument '%.400s'",
3074 PyString_AsString(co
->co_name
),
3075 PyString_AsString(kwd_str
));
3083 if (argcount
< co
->co_argcount
) {
3084 int m
= co
->co_argcount
- defcount
;
3085 for (i
= argcount
; i
< m
; i
++) {
3086 if (GETLOCAL(i
) == NULL
) {
3087 PyErr_Format(PyExc_TypeError
,
3088 "%.200s() takes %s %d "
3089 "%sargument%s (%d given)",
3090 PyString_AsString(co
->co_name
),
3091 ((co
->co_flags
& CO_VARARGS
) ||
3092 defcount
) ? "at least"
3094 m
, kwcount
? "non-keyword " : "",
3095 m
== 1 ? "" : "s", i
);
3103 for (; i
< defcount
; i
++) {
3104 if (GETLOCAL(m
+i
) == NULL
) {
3105 PyObject
*def
= defs
[i
];
3113 if (argcount
> 0 || kwcount
> 0) {
3114 PyErr_Format(PyExc_TypeError
,
3115 "%.200s() takes no arguments (%d given)",
3116 PyString_AsString(co
->co_name
),
3117 argcount
+ kwcount
);
3121 /* Allocate and initialize storage for cell vars, and copy free
3122 vars into frame. This isn't too efficient right now. */
3123 if (PyTuple_GET_SIZE(co
->co_cellvars
)) {
3124 int i
, j
, nargs
, found
;
3125 char *cellname
, *argname
;
3128 nargs
= co
->co_argcount
;
3129 if (co
->co_flags
& CO_VARARGS
)
3131 if (co
->co_flags
& CO_VARKEYWORDS
)
3134 /* Initialize each cell var, taking into account
3135 cell vars that are initialized from arguments.
3137 Should arrange for the compiler to put cellvars
3138 that are arguments at the beginning of the cellvars
3139 list so that we can march over it more efficiently?
3141 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_cellvars
); ++i
) {
3142 cellname
= PyString_AS_STRING(
3143 PyTuple_GET_ITEM(co
->co_cellvars
, i
));
3145 for (j
= 0; j
< nargs
; j
++) {
3146 argname
= PyString_AS_STRING(
3147 PyTuple_GET_ITEM(co
->co_varnames
, j
));
3148 if (strcmp(cellname
, argname
) == 0) {
3149 c
= PyCell_New(GETLOCAL(j
));
3152 GETLOCAL(co
->co_nlocals
+ i
) = c
;
3158 c
= PyCell_New(NULL
);
3161 SETLOCAL(co
->co_nlocals
+ i
, c
);
3165 if (PyTuple_GET_SIZE(co
->co_freevars
)) {
3167 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_freevars
); ++i
) {
3168 PyObject
*o
= PyTuple_GET_ITEM(closure
, i
);
3170 freevars
[PyTuple_GET_SIZE(co
->co_cellvars
) + i
] = o
;
3174 if (co
->co_flags
& CO_GENERATOR
) {
3175 /* Don't need to keep the reference to f_back, it will be set
3176 * when the generator is resumed. */
3177 Py_XDECREF(f
->f_back
);
3180 PCALL(PCALL_GENERATOR
);
3182 /* Create a new generator that owns the ready to run frame
3183 * and return that as the value. */
3184 return PyGen_New(f
);
3187 retval
= PyEval_EvalFrameEx(f
,0);
3189 fail
: /* Jump here from prelude on failure */
3191 /* decref'ing the frame can cause __del__ methods to get invoked,
3192 which can call back into Python. While we're done with the
3193 current Python frame (f), the associated C stack is still in use,
3194 so recursion_depth must be boosted for the duration.
3196 assert(tstate
!= NULL
);
3197 ++tstate
->recursion_depth
;
3199 --tstate
->recursion_depth
;
3205 special_lookup(PyObject
*o
, char *meth
, PyObject
**cache
)
3208 if (PyInstance_Check(o
)) {
3210 return PyObject_GetAttrString(o
, meth
);
3212 return PyObject_GetAttr(o
, *cache
);
3214 res
= _PyObject_LookupSpecial(o
, meth
, cache
);
3215 if (res
== NULL
&& !PyErr_Occurred()) {
3216 PyErr_SetObject(PyExc_AttributeError
, *cache
);
3224 kwd_as_string(PyObject
*kwd
) {
3225 #ifdef Py_USING_UNICODE
3226 if (PyString_Check(kwd
)) {
3228 assert(PyString_Check(kwd
));
3232 #ifdef Py_USING_UNICODE
3234 return _PyUnicode_AsDefaultEncodedString(kwd
, "replace");
3239 /* Implementation notes for set_exc_info() and reset_exc_info():
3241 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3242 'exc_traceback'. These always travel together.
3244 - tstate->curexc_ZZZ is the "hot" exception that is set by
3245 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3247 - Once an exception is caught by an except clause, it is transferred
3248 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3249 can pick it up. This is the primary task of set_exc_info().
3250 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
3252 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3254 Long ago, when none of this existed, there were just a few globals:
3255 one set corresponding to the "hot" exception, and one set
3256 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3257 globals; they were simply stored as sys.exc_ZZZ. For backwards
3258 compatibility, they still are!) The problem was that in code like
3262 "something that may fail"
3263 except "some exception":
3264 "do something else first"
3265 "print the exception from sys.exc_ZZZ."
3267 if "do something else first" invoked something that raised and caught
3268 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3269 cause of subtle bugs. I fixed this by changing the semantics as
3272 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3275 - But initially, and as long as no exception is caught in a given
3276 frame, sys.exc_ZZZ will hold the last exception caught in the
3277 previous frame (or the frame before that, etc.).
3279 The first bullet fixed the bug in the above example. The second
3280 bullet was for backwards compatibility: it was (and is) common to
3281 have a function that is called when an exception is caught, and to
3282 have that function access the caught exception via sys.exc_ZZZ.
3283 (Example: traceback.print_exc()).
3285 At the same time I fixed the problem that sys.exc_ZZZ weren't
3286 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3287 but that's really a separate improvement.
3289 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3290 variables to what they were before the current frame was called. The
3291 set_exc_info() function saves them on the frame so that
3292 reset_exc_info() can restore them. The invariant is that
3293 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3294 exception (where "catching" an exception applies only to successful
3295 except clauses); and if the current frame ever caught an exception,
3296 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3297 at the start of the current frame.
3302 set_exc_info(PyThreadState
*tstate
,
3303 PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3305 PyFrameObject
*frame
= tstate
->frame
;
3306 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
3308 assert(type
!= NULL
);
3309 assert(frame
!= NULL
);
3310 if (frame
->f_exc_type
== NULL
) {
3311 assert(frame
->f_exc_value
== NULL
);
3312 assert(frame
->f_exc_traceback
== NULL
);
3313 /* This frame didn't catch an exception before. */
3314 /* Save previous exception of this thread in this frame. */
3315 if (tstate
->exc_type
== NULL
) {
3316 /* XXX Why is this set to Py_None? */
3318 tstate
->exc_type
= Py_None
;
3320 Py_INCREF(tstate
->exc_type
);
3321 Py_XINCREF(tstate
->exc_value
);
3322 Py_XINCREF(tstate
->exc_traceback
);
3323 frame
->f_exc_type
= tstate
->exc_type
;
3324 frame
->f_exc_value
= tstate
->exc_value
;
3325 frame
->f_exc_traceback
= tstate
->exc_traceback
;
3327 /* Set new exception for this thread. */
3328 tmp_type
= tstate
->exc_type
;
3329 tmp_value
= tstate
->exc_value
;
3330 tmp_tb
= tstate
->exc_traceback
;
3334 tstate
->exc_type
= type
;
3335 tstate
->exc_value
= value
;
3336 tstate
->exc_traceback
= tb
;
3337 Py_XDECREF(tmp_type
);
3338 Py_XDECREF(tmp_value
);
3340 /* For b/w compatibility */
3341 PySys_SetObject("exc_type", type
);
3342 PySys_SetObject("exc_value", value
);
3343 PySys_SetObject("exc_traceback", tb
);
3347 reset_exc_info(PyThreadState
*tstate
)
3349 PyFrameObject
*frame
;
3350 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
3352 /* It's a precondition that the thread state's frame caught an
3353 * exception -- verify in a debug build.
3355 assert(tstate
!= NULL
);
3356 frame
= tstate
->frame
;
3357 assert(frame
!= NULL
);
3358 assert(frame
->f_exc_type
!= NULL
);
3360 /* Copy the frame's exception info back to the thread state. */
3361 tmp_type
= tstate
->exc_type
;
3362 tmp_value
= tstate
->exc_value
;
3363 tmp_tb
= tstate
->exc_traceback
;
3364 Py_INCREF(frame
->f_exc_type
);
3365 Py_XINCREF(frame
->f_exc_value
);
3366 Py_XINCREF(frame
->f_exc_traceback
);
3367 tstate
->exc_type
= frame
->f_exc_type
;
3368 tstate
->exc_value
= frame
->f_exc_value
;
3369 tstate
->exc_traceback
= frame
->f_exc_traceback
;
3370 Py_XDECREF(tmp_type
);
3371 Py_XDECREF(tmp_value
);
3374 /* For b/w compatibility */
3375 PySys_SetObject("exc_type", frame
->f_exc_type
);
3376 PySys_SetObject("exc_value", frame
->f_exc_value
);
3377 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
3379 /* Clear the frame's exception info. */
3380 tmp_type
= frame
->f_exc_type
;
3381 tmp_value
= frame
->f_exc_value
;
3382 tmp_tb
= frame
->f_exc_traceback
;
3383 frame
->f_exc_type
= NULL
;
3384 frame
->f_exc_value
= NULL
;
3385 frame
->f_exc_traceback
= NULL
;
3386 Py_DECREF(tmp_type
);
3387 Py_XDECREF(tmp_value
);
3391 /* Logic for the raise statement (too complicated for inlining).
3392 This *consumes* a reference count to each of its arguments. */
3393 static enum why_code
3394 do_raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3398 PyThreadState
*tstate
= PyThreadState_GET();
3399 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
3400 value
= tstate
->exc_value
;
3401 tb
= tstate
->exc_traceback
;
3407 /* We support the following forms of raise:
3408 raise <class>, <classinstance>
3409 raise <class>, <argument tuple>
3411 raise <class>, <argument>
3412 raise <classinstance>, None
3413 raise <string>, <object>
3414 raise <string>, None
3416 An omitted second argument is the same as None.
3418 In addition, raise <tuple>, <anything> is the same as
3419 raising the tuple's first item (and it better have one!);
3420 this rule is applied recursively.
3422 Finally, an optional third argument can be supplied, which
3423 gives the traceback to be substituted (useful when
3424 re-raising an exception after examining it). */
3426 /* First, check the traceback argument, replacing None with
3428 if (tb
== Py_None
) {
3432 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
3433 PyErr_SetString(PyExc_TypeError
,
3434 "raise: arg 3 must be a traceback or None");
3438 /* Next, replace a missing value with None */
3439 if (value
== NULL
) {
3444 /* Next, repeatedly, replace a tuple exception with its first item */
3445 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
3446 PyObject
*tmp
= type
;
3447 type
= PyTuple_GET_ITEM(type
, 0);
3452 if (PyExceptionClass_Check(type
))
3453 PyErr_NormalizeException(&type
, &value
, &tb
);
3455 else if (PyExceptionInstance_Check(type
)) {
3456 /* Raising an instance. The value should be a dummy. */
3457 if (value
!= Py_None
) {
3458 PyErr_SetString(PyExc_TypeError
,
3459 "instance exception may not have a separate value");
3463 /* Normalize to raise <class>, <instance> */
3466 type
= PyExceptionInstance_Class(type
);
3471 /* Not something you can raise. You get an exception
3472 anyway, just not what you specified :-) */
3473 PyErr_Format(PyExc_TypeError
,
3474 "exceptions must be classes or instances, not %s",
3475 type
->ob_type
->tp_name
);
3479 assert(PyExceptionClass_Check(type
));
3480 if (Py_Py3kWarningFlag
&& PyClass_Check(type
)) {
3481 if (PyErr_WarnEx(PyExc_DeprecationWarning
,
3482 "exceptions must derive from BaseException "
3487 PyErr_Restore(type
, value
, tb
);
3489 return WHY_EXCEPTION
;
3496 return WHY_EXCEPTION
;
3499 /* Iterate v argcnt times and store the results on the stack (via decreasing
3500 sp). Return 1 for success, 0 if error. */
3503 unpack_iterable(PyObject
*v
, int argcnt
, PyObject
**sp
)
3506 PyObject
*it
; /* iter(v) */
3511 it
= PyObject_GetIter(v
);
3515 for (; i
< argcnt
; i
++) {
3516 w
= PyIter_Next(it
);
3518 /* Iterator done, via error or exhaustion. */
3519 if (!PyErr_Occurred()) {
3520 PyErr_Format(PyExc_ValueError
,
3521 "need more than %d value%s to unpack",
3522 i
, i
== 1 ? "" : "s");
3529 /* We better have exhausted the iterator now. */
3530 w
= PyIter_Next(it
);
3532 if (PyErr_Occurred())
3538 PyErr_SetString(PyExc_ValueError
, "too many values to unpack");
3541 for (; i
> 0; i
--, sp
++)
3550 prtrace(PyObject
*v
, char *str
)
3553 if (PyObject_Print(v
, stdout
, 0) != 0)
3554 PyErr_Clear(); /* Don't know what else to do */
3561 call_exc_trace(Py_tracefunc func
, PyObject
*self
, PyFrameObject
*f
)
3563 PyObject
*type
, *value
, *traceback
, *arg
;
3565 PyErr_Fetch(&type
, &value
, &traceback
);
3566 if (value
== NULL
) {
3570 arg
= PyTuple_Pack(3, type
, value
, traceback
);
3572 PyErr_Restore(type
, value
, traceback
);
3575 err
= call_trace(func
, self
, f
, PyTrace_EXCEPTION
, arg
);
3578 PyErr_Restore(type
, value
, traceback
);
3582 Py_XDECREF(traceback
);
3587 call_trace_protected(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3588 int what
, PyObject
*arg
)
3590 PyObject
*type
, *value
, *traceback
;
3592 PyErr_Fetch(&type
, &value
, &traceback
);
3593 err
= call_trace(func
, obj
, frame
, what
, arg
);
3596 PyErr_Restore(type
, value
, traceback
);
3602 Py_XDECREF(traceback
);
3608 call_trace(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3609 int what
, PyObject
*arg
)
3611 register PyThreadState
*tstate
= frame
->f_tstate
;
3613 if (tstate
->tracing
)
3616 tstate
->use_tracing
= 0;
3617 result
= func(obj
, frame
, what
, arg
);
3618 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3619 || (tstate
->c_profilefunc
!= NULL
));
3625 _PyEval_CallTracing(PyObject
*func
, PyObject
*args
)
3627 PyFrameObject
*frame
= PyEval_GetFrame();
3628 PyThreadState
*tstate
= frame
->f_tstate
;
3629 int save_tracing
= tstate
->tracing
;
3630 int save_use_tracing
= tstate
->use_tracing
;
3633 tstate
->tracing
= 0;
3634 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3635 || (tstate
->c_profilefunc
!= NULL
));
3636 result
= PyObject_Call(func
, args
, NULL
);
3637 tstate
->tracing
= save_tracing
;
3638 tstate
->use_tracing
= save_use_tracing
;
3642 /* See Objects/lnotab_notes.txt for a description of how tracing works. */
3644 maybe_call_line_trace(Py_tracefunc func
, PyObject
*obj
,
3645 PyFrameObject
*frame
, int *instr_lb
, int *instr_ub
,
3649 int line
= frame
->f_lineno
;
3651 /* If the last instruction executed isn't in the current
3652 instruction window, reset the window.
3654 if (frame
->f_lasti
< *instr_lb
|| frame
->f_lasti
>= *instr_ub
) {
3656 line
= _PyCode_CheckLineNumber(frame
->f_code
, frame
->f_lasti
,
3658 *instr_lb
= bounds
.ap_lower
;
3659 *instr_ub
= bounds
.ap_upper
;
3661 /* If the last instruction falls at the start of a line or if
3662 it represents a jump backwards, update the frame's line
3663 number and call the trace function. */
3664 if (frame
->f_lasti
== *instr_lb
|| frame
->f_lasti
< *instr_prev
) {
3665 frame
->f_lineno
= line
;
3666 result
= call_trace(func
, obj
, frame
, PyTrace_LINE
, Py_None
);
3668 *instr_prev
= frame
->f_lasti
;
3673 PyEval_SetProfile(Py_tracefunc func
, PyObject
*arg
)
3675 PyThreadState
*tstate
= PyThreadState_GET();
3676 PyObject
*temp
= tstate
->c_profileobj
;
3678 tstate
->c_profilefunc
= NULL
;
3679 tstate
->c_profileobj
= NULL
;
3680 /* Must make sure that tracing is not ignored if 'temp' is freed */
3681 tstate
->use_tracing
= tstate
->c_tracefunc
!= NULL
;
3683 tstate
->c_profilefunc
= func
;
3684 tstate
->c_profileobj
= arg
;
3685 /* Flag that tracing or profiling is turned on */
3686 tstate
->use_tracing
= (func
!= NULL
) || (tstate
->c_tracefunc
!= NULL
);
3690 PyEval_SetTrace(Py_tracefunc func
, PyObject
*arg
)
3692 PyThreadState
*tstate
= PyThreadState_GET();
3693 PyObject
*temp
= tstate
->c_traceobj
;
3694 _Py_TracingPossible
+= (func
!= NULL
) - (tstate
->c_tracefunc
!= NULL
);
3696 tstate
->c_tracefunc
= NULL
;
3697 tstate
->c_traceobj
= NULL
;
3698 /* Must make sure that profiling is not ignored if 'temp' is freed */
3699 tstate
->use_tracing
= tstate
->c_profilefunc
!= NULL
;
3701 tstate
->c_tracefunc
= func
;
3702 tstate
->c_traceobj
= arg
;
3703 /* Flag that tracing or profiling is turned on */
3704 tstate
->use_tracing
= ((func
!= NULL
)
3705 || (tstate
->c_profilefunc
!= NULL
));
3709 PyEval_GetBuiltins(void)
3711 PyFrameObject
*current_frame
= PyEval_GetFrame();
3712 if (current_frame
== NULL
)
3713 return PyThreadState_GET()->interp
->builtins
;
3715 return current_frame
->f_builtins
;
3719 PyEval_GetLocals(void)
3721 PyFrameObject
*current_frame
= PyEval_GetFrame();
3722 if (current_frame
== NULL
)
3724 PyFrame_FastToLocals(current_frame
);
3725 return current_frame
->f_locals
;
3729 PyEval_GetGlobals(void)
3731 PyFrameObject
*current_frame
= PyEval_GetFrame();
3732 if (current_frame
== NULL
)
3735 return current_frame
->f_globals
;
3739 PyEval_GetFrame(void)
3741 PyThreadState
*tstate
= PyThreadState_GET();
3742 return _PyThreadState_GetFrame(tstate
);
3746 PyEval_GetRestricted(void)
3748 PyFrameObject
*current_frame
= PyEval_GetFrame();
3749 return current_frame
== NULL
? 0 : PyFrame_IsRestricted(current_frame
);
3753 PyEval_MergeCompilerFlags(PyCompilerFlags
*cf
)
3755 PyFrameObject
*current_frame
= PyEval_GetFrame();
3756 int result
= cf
->cf_flags
!= 0;
3758 if (current_frame
!= NULL
) {
3759 const int codeflags
= current_frame
->f_code
->co_flags
;
3760 const int compilerflags
= codeflags
& PyCF_MASK
;
3761 if (compilerflags
) {
3763 cf
->cf_flags
|= compilerflags
;
3765 #if 0 /* future keyword */
3766 if (codeflags
& CO_GENERATOR_ALLOWED
) {
3768 cf
->cf_flags
|= CO_GENERATOR_ALLOWED
;
3778 PyObject
*f
= PySys_GetObject("stdout");
3781 if (!PyFile_SoftSpace(f
, 0))
3783 return PyFile_WriteString("\n", f
);
3787 /* External interface to call any callable object.
3788 The arg must be a tuple or NULL. */
3790 #undef PyEval_CallObject
3791 /* for backward compatibility: export this interface */
3794 PyEval_CallObject(PyObject
*func
, PyObject
*arg
)
3796 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
3798 #define PyEval_CallObject(func,arg) \
3799 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3802 PyEval_CallObjectWithKeywords(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
3807 arg
= PyTuple_New(0);
3811 else if (!PyTuple_Check(arg
)) {
3812 PyErr_SetString(PyExc_TypeError
,
3813 "argument list must be a tuple");
3819 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
3820 PyErr_SetString(PyExc_TypeError
,
3821 "keyword list must be a dictionary");
3826 result
= PyObject_Call(func
, arg
, kw
);
3832 PyEval_GetFuncName(PyObject
*func
)
3834 if (PyMethod_Check(func
))
3835 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func
));
3836 else if (PyFunction_Check(func
))
3837 return PyString_AsString(((PyFunctionObject
*)func
)->func_name
);
3838 else if (PyCFunction_Check(func
))
3839 return ((PyCFunctionObject
*)func
)->m_ml
->ml_name
;
3840 else if (PyClass_Check(func
))
3841 return PyString_AsString(((PyClassObject
*)func
)->cl_name
);
3842 else if (PyInstance_Check(func
)) {
3843 return PyString_AsString(
3844 ((PyInstanceObject
*)func
)->in_class
->cl_name
);
3846 return func
->ob_type
->tp_name
;
3851 PyEval_GetFuncDesc(PyObject
*func
)
3853 if (PyMethod_Check(func
))
3855 else if (PyFunction_Check(func
))
3857 else if (PyCFunction_Check(func
))
3859 else if (PyClass_Check(func
))
3860 return " constructor";
3861 else if (PyInstance_Check(func
)) {
3869 err_args(PyObject
*func
, int flags
, int nargs
)
3871 if (flags
& METH_NOARGS
)
3872 PyErr_Format(PyExc_TypeError
,
3873 "%.200s() takes no arguments (%d given)",
3874 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3877 PyErr_Format(PyExc_TypeError
,
3878 "%.200s() takes exactly one argument (%d given)",
3879 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3883 #define C_TRACE(x, call) \
3884 if (tstate->use_tracing && tstate->c_profilefunc) { \
3885 if (call_trace(tstate->c_profilefunc, \
3886 tstate->c_profileobj, \
3887 tstate->frame, PyTrace_C_CALL, \
3893 if (tstate->c_profilefunc != NULL) { \
3895 call_trace_protected(tstate->c_profilefunc, \
3896 tstate->c_profileobj, \
3897 tstate->frame, PyTrace_C_EXCEPTION, \
3899 /* XXX should pass (type, value, tb) */ \
3901 if (call_trace(tstate->c_profilefunc, \
3902 tstate->c_profileobj, \
3903 tstate->frame, PyTrace_C_RETURN, \
3916 call_function(PyObject
***pp_stack
, int oparg
3918 , uint64
* pintr0
, uint64
* pintr1
3922 int na
= oparg
& 0xff;
3923 int nk
= (oparg
>>8) & 0xff;
3924 int n
= na
+ 2 * nk
;
3925 PyObject
**pfunc
= (*pp_stack
) - n
- 1;
3926 PyObject
*func
= *pfunc
;
3929 /* Always dispatch PyCFunction first, because these are
3930 presumed to be the most frequent callable object.
3932 if (PyCFunction_Check(func
) && nk
== 0) {
3933 int flags
= PyCFunction_GET_FLAGS(func
);
3934 PyThreadState
*tstate
= PyThreadState_GET();
3936 PCALL(PCALL_CFUNCTION
);
3937 if (flags
& (METH_NOARGS
| METH_O
)) {
3938 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
3939 PyObject
*self
= PyCFunction_GET_SELF(func
);
3940 if (flags
& METH_NOARGS
&& na
== 0) {
3941 C_TRACE(x
, (*meth
)(self
,NULL
));
3943 else if (flags
& METH_O
&& na
== 1) {
3944 PyObject
*arg
= EXT_POP(*pp_stack
);
3945 C_TRACE(x
, (*meth
)(self
,arg
));
3949 err_args(func
, flags
, na
);
3955 callargs
= load_args(pp_stack
, na
);
3956 READ_TIMESTAMP(*pintr0
);
3957 C_TRACE(x
, PyCFunction_Call(func
,callargs
,NULL
));
3958 READ_TIMESTAMP(*pintr1
);
3959 Py_XDECREF(callargs
);
3962 if (PyMethod_Check(func
) && PyMethod_GET_SELF(func
) != NULL
) {
3963 /* optimize access to bound methods */
3964 PyObject
*self
= PyMethod_GET_SELF(func
);
3965 PCALL(PCALL_METHOD
);
3966 PCALL(PCALL_BOUND_METHOD
);
3968 func
= PyMethod_GET_FUNCTION(func
);
3976 READ_TIMESTAMP(*pintr0
);
3977 if (PyFunction_Check(func
))
3978 x
= fast_function(func
, pp_stack
, n
, na
, nk
);
3980 x
= do_call(func
, pp_stack
, na
, nk
);
3981 READ_TIMESTAMP(*pintr1
);
3985 /* Clear the stack of the function object. Also removes
3986 the arguments in case they weren't consumed already
3987 (fast_function() and err_args() leave them on the stack).
3989 while ((*pp_stack
) > pfunc
) {
3990 w
= EXT_POP(*pp_stack
);
3997 /* The fast_function() function optimize calls for which no argument
3998 tuple is necessary; the objects are passed directly from the stack.
3999 For the simplest case -- a function that takes only positional
4000 arguments and is called with only positional arguments -- it
4001 inlines the most primitive frame setup code from
4002 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4003 done before evaluating the frame.
4007 fast_function(PyObject
*func
, PyObject
***pp_stack
, int n
, int na
, int nk
)
4009 PyCodeObject
*co
= (PyCodeObject
*)PyFunction_GET_CODE(func
);
4010 PyObject
*globals
= PyFunction_GET_GLOBALS(func
);
4011 PyObject
*argdefs
= PyFunction_GET_DEFAULTS(func
);
4012 PyObject
**d
= NULL
;
4015 PCALL(PCALL_FUNCTION
);
4016 PCALL(PCALL_FAST_FUNCTION
);
4017 if (argdefs
== NULL
&& co
->co_argcount
== n
&& nk
==0 &&
4018 co
->co_flags
== (CO_OPTIMIZED
| CO_NEWLOCALS
| CO_NOFREE
)) {
4020 PyObject
*retval
= NULL
;
4021 PyThreadState
*tstate
= PyThreadState_GET();
4022 PyObject
**fastlocals
, **stack
;
4025 PCALL(PCALL_FASTER_FUNCTION
);
4026 assert(globals
!= NULL
);
4027 /* XXX Perhaps we should create a specialized
4028 PyFrame_New() that doesn't take locals, but does
4029 take builtins without sanity checking them.
4031 assert(tstate
!= NULL
);
4032 f
= PyFrame_New(tstate
, co
, globals
, NULL
);
4036 fastlocals
= f
->f_localsplus
;
4037 stack
= (*pp_stack
) - n
;
4039 for (i
= 0; i
< n
; i
++) {
4041 fastlocals
[i
] = *stack
++;
4043 retval
= PyEval_EvalFrameEx(f
,0);
4044 ++tstate
->recursion_depth
;
4046 --tstate
->recursion_depth
;
4049 if (argdefs
!= NULL
) {
4050 d
= &PyTuple_GET_ITEM(argdefs
, 0);
4051 nd
= Py_SIZE(argdefs
);
4053 return PyEval_EvalCodeEx(co
, globals
,
4054 (PyObject
*)NULL
, (*pp_stack
)-n
, na
,
4055 (*pp_stack
)-2*nk
, nk
, d
, nd
,
4056 PyFunction_GET_CLOSURE(func
));
4060 update_keyword_args(PyObject
*orig_kwdict
, int nk
, PyObject
***pp_stack
,
4063 PyObject
*kwdict
= NULL
;
4064 if (orig_kwdict
== NULL
)
4065 kwdict
= PyDict_New();
4067 kwdict
= PyDict_Copy(orig_kwdict
);
4068 Py_DECREF(orig_kwdict
);
4074 PyObject
*value
= EXT_POP(*pp_stack
);
4075 PyObject
*key
= EXT_POP(*pp_stack
);
4076 if (PyDict_GetItem(kwdict
, key
) != NULL
) {
4077 PyErr_Format(PyExc_TypeError
,
4078 "%.200s%s got multiple values "
4079 "for keyword argument '%.200s'",
4080 PyEval_GetFuncName(func
),
4081 PyEval_GetFuncDesc(func
),
4082 PyString_AsString(key
));
4088 err
= PyDict_SetItem(kwdict
, key
, value
);
4100 update_star_args(int nstack
, int nstar
, PyObject
*stararg
,
4101 PyObject
***pp_stack
)
4103 PyObject
*callargs
, *w
;
4105 callargs
= PyTuple_New(nstack
+ nstar
);
4106 if (callargs
== NULL
) {
4111 for (i
= 0; i
< nstar
; i
++) {
4112 PyObject
*a
= PyTuple_GET_ITEM(stararg
, i
);
4114 PyTuple_SET_ITEM(callargs
, nstack
+ i
, a
);
4117 while (--nstack
>= 0) {
4118 w
= EXT_POP(*pp_stack
);
4119 PyTuple_SET_ITEM(callargs
, nstack
, w
);
4125 load_args(PyObject
***pp_stack
, int na
)
4127 PyObject
*args
= PyTuple_New(na
);
4133 w
= EXT_POP(*pp_stack
);
4134 PyTuple_SET_ITEM(args
, na
, w
);
4140 do_call(PyObject
*func
, PyObject
***pp_stack
, int na
, int nk
)
4142 PyObject
*callargs
= NULL
;
4143 PyObject
*kwdict
= NULL
;
4144 PyObject
*result
= NULL
;
4147 kwdict
= update_keyword_args(NULL
, nk
, pp_stack
, func
);
4151 callargs
= load_args(pp_stack
, na
);
4152 if (callargs
== NULL
)
4155 /* At this point, we have to look at the type of func to
4156 update the call stats properly. Do it here so as to avoid
4157 exposing the call stats machinery outside ceval.c
4159 if (PyFunction_Check(func
))
4160 PCALL(PCALL_FUNCTION
);
4161 else if (PyMethod_Check(func
))
4162 PCALL(PCALL_METHOD
);
4163 else if (PyType_Check(func
))
4165 else if (PyCFunction_Check(func
))
4166 PCALL(PCALL_CFUNCTION
);
4170 if (PyCFunction_Check(func
)) {
4171 PyThreadState
*tstate
= PyThreadState_GET();
4172 C_TRACE(result
, PyCFunction_Call(func
, callargs
, kwdict
));
4175 result
= PyObject_Call(func
, callargs
, kwdict
);
4177 Py_XDECREF(callargs
);
4183 ext_do_call(PyObject
*func
, PyObject
***pp_stack
, int flags
, int na
, int nk
)
4186 PyObject
*callargs
= NULL
;
4187 PyObject
*stararg
= NULL
;
4188 PyObject
*kwdict
= NULL
;
4189 PyObject
*result
= NULL
;
4191 if (flags
& CALL_FLAG_KW
) {
4192 kwdict
= EXT_POP(*pp_stack
);
4193 if (!PyDict_Check(kwdict
)) {
4198 if (PyDict_Update(d
, kwdict
) != 0) {
4200 /* PyDict_Update raises attribute
4201 * error (percolated from an attempt
4202 * to get 'keys' attribute) instead of
4203 * a type error if its second argument
4206 if (PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4207 PyErr_Format(PyExc_TypeError
,
4208 "%.200s%.200s argument after ** "
4209 "must be a mapping, not %.200s",
4210 PyEval_GetFuncName(func
),
4211 PyEval_GetFuncDesc(func
),
4212 kwdict
->ob_type
->tp_name
);
4220 if (flags
& CALL_FLAG_VAR
) {
4221 stararg
= EXT_POP(*pp_stack
);
4222 if (!PyTuple_Check(stararg
)) {
4224 t
= PySequence_Tuple(stararg
);
4226 if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
4227 PyErr_Format(PyExc_TypeError
,
4228 "%.200s%.200s argument after * "
4229 "must be a sequence, not %200s",
4230 PyEval_GetFuncName(func
),
4231 PyEval_GetFuncDesc(func
),
4232 stararg
->ob_type
->tp_name
);
4239 nstar
= PyTuple_GET_SIZE(stararg
);
4242 kwdict
= update_keyword_args(kwdict
, nk
, pp_stack
, func
);
4246 callargs
= update_star_args(na
, nstar
, stararg
, pp_stack
);
4247 if (callargs
== NULL
)
4250 /* At this point, we have to look at the type of func to
4251 update the call stats properly. Do it here so as to avoid
4252 exposing the call stats machinery outside ceval.c
4254 if (PyFunction_Check(func
))
4255 PCALL(PCALL_FUNCTION
);
4256 else if (PyMethod_Check(func
))
4257 PCALL(PCALL_METHOD
);
4258 else if (PyType_Check(func
))
4260 else if (PyCFunction_Check(func
))
4261 PCALL(PCALL_CFUNCTION
);
4265 if (PyCFunction_Check(func
)) {
4266 PyThreadState
*tstate
= PyThreadState_GET();
4267 C_TRACE(result
, PyCFunction_Call(func
, callargs
, kwdict
));
4270 result
= PyObject_Call(func
, callargs
, kwdict
);
4272 Py_XDECREF(callargs
);
4274 Py_XDECREF(stararg
);
4278 /* Extract a slice index from a PyInt or PyLong or an object with the
4279 nb_index slot defined, and store in *pi.
4280 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4281 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
4282 Return 0 on error, 1 on success.
4284 /* Note: If v is NULL, return success without storing into *pi. This
4285 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4286 called by the SLICE opcode with v and/or w equal to NULL.
4289 _PyEval_SliceIndex(PyObject
*v
, Py_ssize_t
*pi
)
4293 if (PyInt_Check(v
)) {
4294 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4295 however, it looks like it should be AsSsize_t.
4296 There should be a comment here explaining why.
4298 x
= PyInt_AS_LONG(v
);
4300 else if (PyIndex_Check(v
)) {
4301 x
= PyNumber_AsSsize_t(v
, NULL
);
4302 if (x
== -1 && PyErr_Occurred())
4306 PyErr_SetString(PyExc_TypeError
,
4307 "slice indices must be integers or "
4308 "None or have an __index__ method");
4317 #define ISINDEX(x) ((x) == NULL || \
4318 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
4321 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
4323 PyTypeObject
*tp
= u
->ob_type
;
4324 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4326 if (sq
&& sq
->sq_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4327 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4328 if (!_PyEval_SliceIndex(v
, &ilow
))
4330 if (!_PyEval_SliceIndex(w
, &ihigh
))
4332 return PySequence_GetSlice(u
, ilow
, ihigh
);
4335 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4336 if (slice
!= NULL
) {
4337 PyObject
*res
= PyObject_GetItem(u
, slice
);
4347 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
4350 PyTypeObject
*tp
= u
->ob_type
;
4351 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4353 if (sq
&& sq
->sq_ass_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4354 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4355 if (!_PyEval_SliceIndex(v
, &ilow
))
4357 if (!_PyEval_SliceIndex(w
, &ihigh
))
4360 return PySequence_DelSlice(u
, ilow
, ihigh
);
4362 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
4365 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4366 if (slice
!= NULL
) {
4369 res
= PyObject_SetItem(u
, slice
, x
);
4371 res
= PyObject_DelItem(u
, slice
);
4380 #define Py3kExceptionClass_Check(x) \
4381 (PyType_Check((x)) && \
4382 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4384 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4385 "BaseException is not allowed in 3.x"
4388 cmp_outcome(int op
, register PyObject
*v
, register PyObject
*w
)
4399 res
= PySequence_Contains(w
, v
);
4404 res
= PySequence_Contains(w
, v
);
4409 case PyCmp_EXC_MATCH
:
4410 if (PyTuple_Check(w
)) {
4411 Py_ssize_t i
, length
;
4412 length
= PyTuple_Size(w
);
4413 for (i
= 0; i
< length
; i
+= 1) {
4414 PyObject
*exc
= PyTuple_GET_ITEM(w
, i
);
4415 if (PyString_Check(exc
)) {
4417 ret_val
= PyErr_WarnEx(
4418 PyExc_DeprecationWarning
,
4419 "catching of string "
4420 "exceptions is deprecated", 1);
4424 else if (Py_Py3kWarningFlag
&&
4425 !PyTuple_Check(exc
) &&
4426 !Py3kExceptionClass_Check(exc
))
4429 ret_val
= PyErr_WarnEx(
4430 PyExc_DeprecationWarning
,
4431 CANNOT_CATCH_MSG
, 1);
4438 if (PyString_Check(w
)) {
4440 ret_val
= PyErr_WarnEx(
4441 PyExc_DeprecationWarning
,
4442 "catching of string "
4443 "exceptions is deprecated", 1);
4447 else if (Py_Py3kWarningFlag
&&
4448 !PyTuple_Check(w
) &&
4449 !Py3kExceptionClass_Check(w
))
4452 ret_val
= PyErr_WarnEx(
4453 PyExc_DeprecationWarning
,
4454 CANNOT_CATCH_MSG
, 1);
4459 res
= PyErr_GivenExceptionMatches(v
, w
);
4462 return PyObject_RichCompare(v
, w
, op
);
4464 v
= res
? Py_True
: Py_False
;
4470 import_from(PyObject
*v
, PyObject
*name
)
4474 x
= PyObject_GetAttr(v
, name
);
4475 if (x
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4476 PyErr_Format(PyExc_ImportError
,
4477 "cannot import name %.230s",
4478 PyString_AsString(name
));
4484 import_all_from(PyObject
*locals
, PyObject
*v
)
4486 PyObject
*all
= PyObject_GetAttrString(v
, "__all__");
4487 PyObject
*dict
, *name
, *value
;
4488 int skip_leading_underscores
= 0;
4492 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4493 return -1; /* Unexpected error */
4495 dict
= PyObject_GetAttrString(v
, "__dict__");
4497 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4499 PyErr_SetString(PyExc_ImportError
,
4500 "from-import-* object has no __dict__ and no __all__");
4503 all
= PyMapping_Keys(dict
);
4507 skip_leading_underscores
= 1;
4510 for (pos
= 0, err
= 0; ; pos
++) {
4511 name
= PySequence_GetItem(all
, pos
);
4513 if (!PyErr_ExceptionMatches(PyExc_IndexError
))
4519 if (skip_leading_underscores
&&
4520 PyString_Check(name
) &&
4521 PyString_AS_STRING(name
)[0] == '_')
4526 value
= PyObject_GetAttr(v
, name
);
4529 else if (PyDict_CheckExact(locals
))
4530 err
= PyDict_SetItem(locals
, name
, value
);
4532 err
= PyObject_SetItem(locals
, name
, value
);
4543 build_class(PyObject
*methods
, PyObject
*bases
, PyObject
*name
)
4545 PyObject
*metaclass
= NULL
, *result
, *base
;
4547 if (PyDict_Check(methods
))
4548 metaclass
= PyDict_GetItemString(methods
, "__metaclass__");
4549 if (metaclass
!= NULL
)
4550 Py_INCREF(metaclass
);
4551 else if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
4552 base
= PyTuple_GET_ITEM(bases
, 0);
4553 metaclass
= PyObject_GetAttrString(base
, "__class__");
4554 if (metaclass
== NULL
) {
4556 metaclass
= (PyObject
*)base
->ob_type
;
4557 Py_INCREF(metaclass
);
4561 PyObject
*g
= PyEval_GetGlobals();
4562 if (g
!= NULL
&& PyDict_Check(g
))
4563 metaclass
= PyDict_GetItemString(g
, "__metaclass__");
4564 if (metaclass
== NULL
)
4565 metaclass
= (PyObject
*) &PyClass_Type
;
4566 Py_INCREF(metaclass
);
4568 result
= PyObject_CallFunctionObjArgs(metaclass
, name
, bases
, methods
,
4570 Py_DECREF(metaclass
);
4571 if (result
== NULL
&& PyErr_ExceptionMatches(PyExc_TypeError
)) {
4572 /* A type error here likely means that the user passed
4573 in a base that was not a class (such the random module
4574 instead of the random.random type). Help them out with
4575 by augmenting the error message with more information.*/
4577 PyObject
*ptype
, *pvalue
, *ptraceback
;
4579 PyErr_Fetch(&ptype
, &pvalue
, &ptraceback
);
4580 if (PyString_Check(pvalue
)) {
4582 newmsg
= PyString_FromFormat(
4583 "Error when calling the metaclass bases\n"
4585 PyString_AS_STRING(pvalue
));
4586 if (newmsg
!= NULL
) {
4591 PyErr_Restore(ptype
, pvalue
, ptraceback
);
4597 exec_statement(PyFrameObject
*f
, PyObject
*prog
, PyObject
*globals
,
4604 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
4605 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
4606 /* Backward compatibility hack */
4607 globals
= PyTuple_GetItem(prog
, 1);
4609 locals
= PyTuple_GetItem(prog
, 2);
4610 prog
= PyTuple_GetItem(prog
, 0);
4612 if (globals
== Py_None
) {
4613 globals
= PyEval_GetGlobals();
4614 if (locals
== Py_None
) {
4615 locals
= PyEval_GetLocals();
4618 if (!globals
|| !locals
) {
4619 PyErr_SetString(PyExc_SystemError
,
4620 "globals and locals cannot be NULL");
4624 else if (locals
== Py_None
)
4626 if (!PyString_Check(prog
) &&
4627 #ifdef Py_USING_UNICODE
4628 !PyUnicode_Check(prog
) &&
4630 !PyCode_Check(prog
) &&
4631 !PyFile_Check(prog
)) {
4632 PyErr_SetString(PyExc_TypeError
,
4633 "exec: arg 1 must be a string, file, or code object");
4636 if (!PyDict_Check(globals
)) {
4637 PyErr_SetString(PyExc_TypeError
,
4638 "exec: arg 2 must be a dictionary or None");
4641 if (!PyMapping_Check(locals
)) {
4642 PyErr_SetString(PyExc_TypeError
,
4643 "exec: arg 3 must be a mapping or None");
4646 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
4647 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
4648 if (PyCode_Check(prog
)) {
4649 if (PyCode_GetNumFree((PyCodeObject
*)prog
) > 0) {
4650 PyErr_SetString(PyExc_TypeError
,
4651 "code object passed to exec may not contain free variables");
4654 v
= PyEval_EvalCode((PyCodeObject
*) prog
, globals
, locals
);
4656 else if (PyFile_Check(prog
)) {
4657 FILE *fp
= PyFile_AsFile(prog
);
4658 char *name
= PyString_AsString(PyFile_Name(prog
));
4663 if (PyEval_MergeCompilerFlags(&cf
))
4664 v
= PyRun_FileFlags(fp
, name
, Py_file_input
, globals
,
4667 v
= PyRun_File(fp
, name
, Py_file_input
, globals
,
4671 PyObject
*tmp
= NULL
;
4675 #ifdef Py_USING_UNICODE
4676 if (PyUnicode_Check(prog
)) {
4677 tmp
= PyUnicode_AsUTF8String(prog
);
4681 cf
.cf_flags
|= PyCF_SOURCE_IS_UTF8
;
4684 if (PyString_AsStringAndSize(prog
, &str
, NULL
))
4686 if (PyEval_MergeCompilerFlags(&cf
))
4687 v
= PyRun_StringFlags(str
, Py_file_input
, globals
,
4690 v
= PyRun_String(str
, Py_file_input
, globals
, locals
);
4694 PyFrame_LocalsToFast(f
, 0);
4702 format_exc_check_arg(PyObject
*exc
, char *format_str
, PyObject
*obj
)
4709 obj_str
= PyString_AsString(obj
);
4713 PyErr_Format(exc
, format_str
, obj_str
);
4717 string_concatenate(PyObject
*v
, PyObject
*w
,
4718 PyFrameObject
*f
, unsigned char *next_instr
)
4720 /* This function implements 'variable += expr' when both arguments
4722 Py_ssize_t v_len
= PyString_GET_SIZE(v
);
4723 Py_ssize_t w_len
= PyString_GET_SIZE(w
);
4724 Py_ssize_t new_len
= v_len
+ w_len
;
4726 PyErr_SetString(PyExc_OverflowError
,
4727 "strings are too large to concat");
4731 if (v
->ob_refcnt
== 2) {
4732 /* In the common case, there are 2 references to the value
4733 * stored in 'variable' when the += is performed: one on the
4734 * value stack (in 'v') and one still stored in the
4735 * 'variable'. We try to delete the variable now to reduce
4738 switch (*next_instr
) {
4741 int oparg
= PEEKARG();
4742 PyObject
**fastlocals
= f
->f_localsplus
;
4743 if (GETLOCAL(oparg
) == v
)
4744 SETLOCAL(oparg
, NULL
);
4749 PyObject
**freevars
= (f
->f_localsplus
+
4750 f
->f_code
->co_nlocals
);
4751 PyObject
*c
= freevars
[PEEKARG()];
4752 if (PyCell_GET(c
) == v
)
4753 PyCell_Set(c
, NULL
);
4758 PyObject
*names
= f
->f_code
->co_names
;
4759 PyObject
*name
= GETITEM(names
, PEEKARG());
4760 PyObject
*locals
= f
->f_locals
;
4761 if (PyDict_CheckExact(locals
) &&
4762 PyDict_GetItem(locals
, name
) == v
) {
4763 if (PyDict_DelItem(locals
, name
) != 0) {
4772 if (v
->ob_refcnt
== 1 && !PyString_CHECK_INTERNED(v
)) {
4773 /* Now we own the last reference to 'v', so we can resize it
4776 if (_PyString_Resize(&v
, new_len
) != 0) {
4777 /* XXX if _PyString_Resize() fails, 'v' has been
4778 * deallocated so it cannot be put back into
4779 * 'variable'. The MemoryError is raised when there
4780 * is no value in 'variable', which might (very
4781 * remotely) be a cause of incompatibilities.
4785 /* copy 'w' into the newly allocated area of 'v' */
4786 memcpy(PyString_AS_STRING(v
) + v_len
,
4787 PyString_AS_STRING(w
), w_len
);
4791 /* When in-place resizing is not an option. */
4792 PyString_Concat(&v
, w
);
4797 #ifdef DYNAMIC_EXECUTION_PROFILE
4800 getarray(long a
[256])
4803 PyObject
*l
= PyList_New(256);
4804 if (l
== NULL
) return NULL
;
4805 for (i
= 0; i
< 256; i
++) {
4806 PyObject
*x
= PyInt_FromLong(a
[i
]);
4811 PyList_SetItem(l
, i
, x
);
4813 for (i
= 0; i
< 256; i
++)
4819 _Py_GetDXProfile(PyObject
*self
, PyObject
*args
)
4822 return getarray(dxp
);
4825 PyObject
*l
= PyList_New(257);
4826 if (l
== NULL
) return NULL
;
4827 for (i
= 0; i
< 257; i
++) {
4828 PyObject
*x
= getarray(dxpairs
[i
]);
4833 PyList_SetItem(l
, i
, x
);