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 SET_TOP(v) (stack_pointer[-1] = (v))
807 #define SET_SECOND(v) (stack_pointer[-2] = (v))
808 #define SET_THIRD(v) (stack_pointer[-3] = (v))
809 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
810 #define BASIC_STACKADJ(n) (stack_pointer += n)
811 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
812 #define BASIC_POP() (*--stack_pointer)
815 #define PUSH(v) { (void)(BASIC_PUSH(v), \
816 lltrace && prtrace(TOP(), "push")); \
817 assert(STACK_LEVEL() <= co->co_stacksize); }
818 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
820 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
821 lltrace && prtrace(TOP(), "stackadj")); \
822 assert(STACK_LEVEL() <= co->co_stacksize); }
823 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
824 prtrace((STACK_POINTER)[-1], "ext_pop")), \
827 #define PUSH(v) BASIC_PUSH(v)
828 #define POP() BASIC_POP()
829 #define STACKADJ(n) BASIC_STACKADJ(n)
830 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
833 /* Local variable macros */
835 #define GETLOCAL(i) (fastlocals[i])
837 /* The SETLOCAL() macro must not DECREF the local variable in-place and
838 then store the new value; it must copy the old value to a temporary
839 value, then store the new value, and then DECREF the temporary value.
840 This is because it is possible that during the DECREF the frame is
841 accessed by other code (e.g. a __del__ method or gc.collect()) and the
842 variable would be pointing to already-freed memory. */
843 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
844 GETLOCAL(i) = value; \
845 Py_XDECREF(tmp); } while (0)
853 if (Py_EnterRecursiveCall(""))
858 if (tstate
->use_tracing
) {
859 if (tstate
->c_tracefunc
!= NULL
) {
860 /* tstate->c_tracefunc, if defined, is a
861 function that will be called on *every* entry
862 to a code block. Its return value, if not
863 None, is a function that will be called at
864 the start of each executed line of code.
865 (Actually, the function must return itself
866 in order to continue tracing.) The trace
867 functions are called with three arguments:
868 a pointer to the current frame, a string
869 indicating why the function is called, and
870 an argument which depends on the situation.
871 The global trace function is also called
872 whenever an exception is detected. */
873 if (call_trace_protected(tstate
->c_tracefunc
,
875 f
, PyTrace_CALL
, Py_None
)) {
876 /* Trace function raised an error */
877 goto exit_eval_frame
;
880 if (tstate
->c_profilefunc
!= NULL
) {
881 /* Similar for c_profilefunc, except it needn't
882 return itself and isn't called for "line" events */
883 if (call_trace_protected(tstate
->c_profilefunc
,
884 tstate
->c_profileobj
,
885 f
, PyTrace_CALL
, Py_None
)) {
886 /* Profile function raised an error */
887 goto exit_eval_frame
;
893 names
= co
->co_names
;
894 consts
= co
->co_consts
;
895 fastlocals
= f
->f_localsplus
;
896 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
897 first_instr
= (unsigned char*) PyString_AS_STRING(co
->co_code
);
898 /* An explanation is in order for the next line.
900 f->f_lasti now refers to the index of the last instruction
901 executed. You might think this was obvious from the name, but
902 this wasn't always true before 2.3! PyFrame_New now sets
903 f->f_lasti to -1 (i.e. the index *before* the first instruction)
904 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
907 When the PREDICT() macros are enabled, some opcode pairs follow in
908 direct succession without updating f->f_lasti. A successful
909 prediction effectively links the two codes together as if they
910 were a single new opcode; accordingly,f->f_lasti will point to
911 the first code in the pair (for instance, GET_ITER followed by
912 FOR_ITER is effectively a single opcode and f->f_lasti will point
913 at to the beginning of the combined pair.)
915 next_instr
= first_instr
+ f
->f_lasti
+ 1;
916 stack_pointer
= f
->f_stacktop
;
917 assert(stack_pointer
!= NULL
);
918 f
->f_stacktop
= NULL
; /* remains NULL unless yield suspends frame */
921 lltrace
= PyDict_GetItemString(f
->f_globals
, "__lltrace__") != NULL
;
923 #if defined(Py_DEBUG) || defined(LLTRACE)
924 filename
= PyString_AsString(co
->co_filename
);
929 x
= Py_None
; /* Not a reference, just anything non-NULL */
932 if (throwflag
) { /* support for generator.throw() */
940 /* Almost surely, the opcode executed a break
941 or a continue, preventing inst1 from being set
942 on the way out of the loop.
944 READ_TIMESTAMP(inst1
);
947 dump_tsc(opcode
, ticked
, inst0
, inst1
, loop0
, loop1
,
953 READ_TIMESTAMP(loop0
);
955 assert(stack_pointer
>= f
->f_valuestack
); /* else underflow */
956 assert(STACK_LEVEL() <= co
->co_stacksize
); /* else overflow */
958 /* Do periodic things. Doing this every time through
959 the loop would add too much overhead, so we do it
960 only every Nth instruction. We also do it if
961 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
962 event needs attention (e.g. a signal handler or
963 async I/O handler); see Py_AddPendingCall() and
964 Py_MakePendingCalls() above. */
966 if (--_Py_Ticker
< 0) {
967 if (*next_instr
== SETUP_FINALLY
) {
968 /* Make the last opcode before
969 a try: finally: block uninterruptable. */
970 goto fast_next_opcode
;
972 _Py_Ticker
= _Py_CheckInterval
;
973 tstate
->tick_counter
++;
977 if (pendingcalls_to_do
) {
978 if (Py_MakePendingCalls() < 0) {
982 if (pendingcalls_to_do
)
983 /* MakePendingCalls() didn't succeed.
984 Force early re-execution of this
985 "periodic" code, possibly after
990 if (interpreter_lock
) {
991 /* Give another thread a chance */
993 if (PyThreadState_Swap(NULL
) != tstate
)
994 Py_FatalError("ceval: tstate mix-up");
995 PyThread_release_lock(interpreter_lock
);
997 /* Other threads may run now */
999 PyThread_acquire_lock(interpreter_lock
, 1);
1000 if (PyThreadState_Swap(tstate
) != NULL
)
1001 Py_FatalError("ceval: orphan tstate");
1003 /* Check for thread interrupts */
1005 if (tstate
->async_exc
!= NULL
) {
1006 x
= tstate
->async_exc
;
1007 tstate
->async_exc
= NULL
;
1010 why
= WHY_EXCEPTION
;
1018 f
->f_lasti
= INSTR_OFFSET();
1020 /* line-by-line tracing support */
1022 if (_Py_TracingPossible
&&
1023 tstate
->c_tracefunc
!= NULL
&& !tstate
->tracing
) {
1024 /* see maybe_call_line_trace
1025 for expository comments */
1026 f
->f_stacktop
= stack_pointer
;
1028 err
= maybe_call_line_trace(tstate
->c_tracefunc
,
1030 f
, &instr_lb
, &instr_ub
,
1032 /* Reload possibly changed frame fields */
1034 if (f
->f_stacktop
!= NULL
) {
1035 stack_pointer
= f
->f_stacktop
;
1036 f
->f_stacktop
= NULL
;
1039 /* trace function raised an exception */
1044 /* Extract opcode and argument */
1047 oparg
= 0; /* allows oparg to be stored in a register because
1048 it doesn't have to be remembered across a full loop */
1049 if (HAS_ARG(opcode
))
1052 #ifdef DYNAMIC_EXECUTION_PROFILE
1054 dxpairs
[lastopcode
][opcode
]++;
1055 lastopcode
= opcode
;
1061 /* Instruction tracing */
1064 if (HAS_ARG(opcode
)) {
1065 printf("%d: %d, %d\n",
1066 f
->f_lasti
, opcode
, oparg
);
1070 f
->f_lasti
, opcode
);
1075 /* Main switch on opcode */
1076 READ_TIMESTAMP(inst0
);
1081 It is essential that any operation that fails sets either
1082 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1083 and that no operation that succeeds does this! */
1085 /* case STOP_CODE: this is an error! */
1088 goto fast_next_opcode
;
1091 x
= GETLOCAL(oparg
);
1095 goto fast_next_opcode
;
1097 format_exc_check_arg(PyExc_UnboundLocalError
,
1098 UNBOUNDLOCAL_ERROR_MSG
,
1099 PyTuple_GetItem(co
->co_varnames
, oparg
));
1103 x
= GETITEM(consts
, oparg
);
1106 goto fast_next_opcode
;
1108 PREDICTED_WITH_ARG(STORE_FAST
);
1112 goto fast_next_opcode
;
1117 goto fast_next_opcode
;
1124 goto fast_next_opcode
;
1133 goto fast_next_opcode
;
1144 goto fast_next_opcode
;
1150 goto fast_next_opcode
;
1161 goto fast_next_opcode
;
1162 } else if (oparg
== 3) {
1173 goto fast_next_opcode
;
1175 Py_FatalError("invalid argument to DUP_TOPX"
1176 " (bytecode corruption?)");
1177 /* Never returns, so don't bother to set why. */
1180 case UNARY_POSITIVE
:
1182 x
= PyNumber_Positive(v
);
1185 if (x
!= NULL
) continue;
1188 case UNARY_NEGATIVE
:
1190 x
= PyNumber_Negative(v
);
1193 if (x
!= NULL
) continue;
1198 err
= PyObject_IsTrue(v
);
1206 Py_INCREF(Py_False
);
1216 x
= PyObject_Repr(v
);
1219 if (x
!= NULL
) continue;
1224 x
= PyNumber_Invert(v
);
1227 if (x
!= NULL
) continue;
1233 x
= PyNumber_Power(v
, w
, Py_None
);
1237 if (x
!= NULL
) continue;
1240 case BINARY_MULTIPLY
:
1243 x
= PyNumber_Multiply(v
, w
);
1247 if (x
!= NULL
) continue;
1251 if (!_Py_QnewFlag
) {
1254 x
= PyNumber_Divide(v
, w
);
1258 if (x
!= NULL
) continue;
1261 /* -Qnew is in effect: fall through to
1262 BINARY_TRUE_DIVIDE */
1263 case BINARY_TRUE_DIVIDE
:
1266 x
= PyNumber_TrueDivide(v
, w
);
1270 if (x
!= NULL
) continue;
1273 case BINARY_FLOOR_DIVIDE
:
1276 x
= PyNumber_FloorDivide(v
, w
);
1280 if (x
!= NULL
) continue;
1286 if (PyString_CheckExact(v
))
1287 x
= PyString_Format(v
, w
);
1289 x
= PyNumber_Remainder(v
, w
);
1293 if (x
!= NULL
) continue;
1299 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1300 /* INLINE: int + int */
1301 register long a
, b
, i
;
1302 a
= PyInt_AS_LONG(v
);
1303 b
= PyInt_AS_LONG(w
);
1305 if ((i
^a
) < 0 && (i
^b
) < 0)
1307 x
= PyInt_FromLong(i
);
1309 else if (PyString_CheckExact(v
) &&
1310 PyString_CheckExact(w
)) {
1311 x
= string_concatenate(v
, w
, f
, next_instr
);
1312 /* string_concatenate consumed the ref to v */
1313 goto skip_decref_vx
;
1317 x
= PyNumber_Add(v
, w
);
1323 if (x
!= NULL
) continue;
1326 case BINARY_SUBTRACT
:
1329 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1330 /* INLINE: int - int */
1331 register long a
, b
, i
;
1332 a
= PyInt_AS_LONG(v
);
1333 b
= PyInt_AS_LONG(w
);
1335 if ((i
^a
) < 0 && (i
^~b
) < 0)
1337 x
= PyInt_FromLong(i
);
1341 x
= PyNumber_Subtract(v
, w
);
1346 if (x
!= NULL
) continue;
1352 if (PyList_CheckExact(v
) && PyInt_CheckExact(w
)) {
1353 /* INLINE: list[int] */
1354 Py_ssize_t i
= PyInt_AsSsize_t(w
);
1356 i
+= PyList_GET_SIZE(v
);
1357 if (i
>= 0 && i
< PyList_GET_SIZE(v
)) {
1358 x
= PyList_GET_ITEM(v
, i
);
1366 x
= PyObject_GetItem(v
, w
);
1370 if (x
!= NULL
) continue;
1376 x
= PyNumber_Lshift(v
, w
);
1380 if (x
!= NULL
) continue;
1386 x
= PyNumber_Rshift(v
, w
);
1390 if (x
!= NULL
) continue;
1396 x
= PyNumber_And(v
, w
);
1400 if (x
!= NULL
) continue;
1406 x
= PyNumber_Xor(v
, w
);
1410 if (x
!= NULL
) continue;
1416 x
= PyNumber_Or(v
, w
);
1420 if (x
!= NULL
) continue;
1425 v
= stack_pointer
[-oparg
];
1426 err
= PyList_Append(v
, w
);
1429 PREDICT(JUMP_ABSOLUTE
);
1437 x
= PyNumber_InPlacePower(v
, w
, Py_None
);
1441 if (x
!= NULL
) continue;
1444 case INPLACE_MULTIPLY
:
1447 x
= PyNumber_InPlaceMultiply(v
, w
);
1451 if (x
!= NULL
) continue;
1454 case INPLACE_DIVIDE
:
1455 if (!_Py_QnewFlag
) {
1458 x
= PyNumber_InPlaceDivide(v
, w
);
1462 if (x
!= NULL
) continue;
1465 /* -Qnew is in effect: fall through to
1466 INPLACE_TRUE_DIVIDE */
1467 case INPLACE_TRUE_DIVIDE
:
1470 x
= PyNumber_InPlaceTrueDivide(v
, w
);
1474 if (x
!= NULL
) continue;
1477 case INPLACE_FLOOR_DIVIDE
:
1480 x
= PyNumber_InPlaceFloorDivide(v
, w
);
1484 if (x
!= NULL
) continue;
1487 case INPLACE_MODULO
:
1490 x
= PyNumber_InPlaceRemainder(v
, w
);
1494 if (x
!= NULL
) continue;
1500 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1501 /* INLINE: int + int */
1502 register long a
, b
, i
;
1503 a
= PyInt_AS_LONG(v
);
1504 b
= PyInt_AS_LONG(w
);
1506 if ((i
^a
) < 0 && (i
^b
) < 0)
1508 x
= PyInt_FromLong(i
);
1510 else if (PyString_CheckExact(v
) &&
1511 PyString_CheckExact(w
)) {
1512 x
= string_concatenate(v
, w
, f
, next_instr
);
1513 /* string_concatenate consumed the ref to v */
1518 x
= PyNumber_InPlaceAdd(v
, w
);
1524 if (x
!= NULL
) continue;
1527 case INPLACE_SUBTRACT
:
1530 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1531 /* INLINE: int - int */
1532 register long a
, b
, i
;
1533 a
= PyInt_AS_LONG(v
);
1534 b
= PyInt_AS_LONG(w
);
1536 if ((i
^a
) < 0 && (i
^~b
) < 0)
1538 x
= PyInt_FromLong(i
);
1542 x
= PyNumber_InPlaceSubtract(v
, w
);
1547 if (x
!= NULL
) continue;
1550 case INPLACE_LSHIFT
:
1553 x
= PyNumber_InPlaceLshift(v
, w
);
1557 if (x
!= NULL
) continue;
1560 case INPLACE_RSHIFT
:
1563 x
= PyNumber_InPlaceRshift(v
, w
);
1567 if (x
!= NULL
) continue;
1573 x
= PyNumber_InPlaceAnd(v
, w
);
1577 if (x
!= NULL
) continue;
1583 x
= PyNumber_InPlaceXor(v
, w
);
1587 if (x
!= NULL
) continue;
1593 x
= PyNumber_InPlaceOr(v
, w
);
1597 if (x
!= NULL
) continue;
1604 if ((opcode
-SLICE
) & 2)
1608 if ((opcode
-SLICE
) & 1)
1613 x
= apply_slice(u
, v
, w
);
1618 if (x
!= NULL
) continue;
1625 if ((opcode
-STORE_SLICE
) & 2)
1629 if ((opcode
-STORE_SLICE
) & 1)
1635 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
1640 if (err
== 0) continue;
1643 case DELETE_SLICE
+0:
1644 case DELETE_SLICE
+1:
1645 case DELETE_SLICE
+2:
1646 case DELETE_SLICE
+3:
1647 if ((opcode
-DELETE_SLICE
) & 2)
1651 if ((opcode
-DELETE_SLICE
) & 1)
1656 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
1661 if (err
== 0) continue;
1670 err
= PyObject_SetItem(v
, w
, u
);
1674 if (err
== 0) continue;
1682 err
= PyObject_DelItem(v
, w
);
1685 if (err
== 0) continue;
1690 w
= PySys_GetObject("displayhook");
1692 PyErr_SetString(PyExc_RuntimeError
,
1693 "lost sys.displayhook");
1698 x
= PyTuple_Pack(1, v
);
1703 w
= PyEval_CallObject(w
, x
);
1714 /* fall through to PRINT_ITEM */
1718 if (stream
== NULL
|| stream
== Py_None
) {
1719 w
= PySys_GetObject("stdout");
1721 PyErr_SetString(PyExc_RuntimeError
,
1726 /* PyFile_SoftSpace() can exececute arbitrary code
1727 if sys.stdout is an instance with a __getattr__.
1728 If __getattr__ raises an exception, w will
1729 be freed, so we need to prevent that temporarily. */
1731 if (w
!= NULL
&& PyFile_SoftSpace(w
, 0))
1732 err
= PyFile_WriteString(" ", w
);
1734 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1736 /* XXX move into writeobject() ? */
1737 if (PyString_Check(v
)) {
1738 char *s
= PyString_AS_STRING(v
);
1739 Py_ssize_t len
= PyString_GET_SIZE(v
);
1741 !isspace(Py_CHARMASK(s
[len
-1])) ||
1743 PyFile_SoftSpace(w
, 1);
1745 #ifdef Py_USING_UNICODE
1746 else if (PyUnicode_Check(v
)) {
1747 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(v
);
1748 Py_ssize_t len
= PyUnicode_GET_SIZE(v
);
1750 !Py_UNICODE_ISSPACE(s
[len
-1]) ||
1752 PyFile_SoftSpace(w
, 1);
1756 PyFile_SoftSpace(w
, 1);
1766 case PRINT_NEWLINE_TO
:
1768 /* fall through to PRINT_NEWLINE */
1771 if (stream
== NULL
|| stream
== Py_None
) {
1772 w
= PySys_GetObject("stdout");
1774 PyErr_SetString(PyExc_RuntimeError
,
1776 why
= WHY_EXCEPTION
;
1780 /* w.write() may replace sys.stdout, so we
1781 * have to keep our reference to it */
1783 err
= PyFile_WriteString("\n", w
);
1785 PyFile_SoftSpace(w
, 0);
1794 default: switch (opcode
) {
1800 u
= POP(); /* traceback */
1803 v
= POP(); /* value */
1806 w
= POP(); /* exc */
1807 case 0: /* Fallthrough */
1808 why
= do_raise(w
, v
, u
);
1811 PyErr_SetString(PyExc_SystemError
,
1812 "bad RAISE_VARARGS oparg");
1813 why
= WHY_EXCEPTION
;
1819 if ((x
= f
->f_locals
) != NULL
) {
1824 PyErr_SetString(PyExc_SystemError
, "no locals");
1830 goto fast_block_end
;
1834 f
->f_stacktop
= stack_pointer
;
1843 READ_TIMESTAMP(intr0
);
1844 err
= exec_statement(f
, u
, v
, w
);
1845 READ_TIMESTAMP(intr1
);
1853 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1854 while (STACK_LEVEL() > b
->b_level
) {
1861 PREDICTED(END_FINALLY
);
1864 if (PyInt_Check(v
)) {
1865 why
= (enum why_code
) PyInt_AS_LONG(v
);
1866 assert(why
!= WHY_YIELD
);
1867 if (why
== WHY_RETURN
||
1868 why
== WHY_CONTINUE
)
1871 else if (PyExceptionClass_Check(v
) ||
1872 PyString_Check(v
)) {
1875 PyErr_Restore(v
, w
, u
);
1879 else if (v
!= Py_None
) {
1880 PyErr_SetString(PyExc_SystemError
,
1881 "'finally' pops bad exception");
1882 why
= WHY_EXCEPTION
;
1892 x
= build_class(u
, v
, w
);
1900 w
= GETITEM(names
, oparg
);
1902 if ((x
= f
->f_locals
) != NULL
) {
1903 if (PyDict_CheckExact(x
))
1904 err
= PyDict_SetItem(x
, w
, v
);
1906 err
= PyObject_SetItem(x
, w
, v
);
1908 if (err
== 0) continue;
1911 PyErr_Format(PyExc_SystemError
,
1912 "no locals found when storing %s",
1917 w
= GETITEM(names
, oparg
);
1918 if ((x
= f
->f_locals
) != NULL
) {
1919 if ((err
= PyObject_DelItem(x
, w
)) != 0)
1920 format_exc_check_arg(PyExc_NameError
,
1925 PyErr_Format(PyExc_SystemError
,
1926 "no locals when deleting %s",
1930 PREDICTED_WITH_ARG(UNPACK_SEQUENCE
);
1931 case UNPACK_SEQUENCE
:
1933 if (PyTuple_CheckExact(v
) &&
1934 PyTuple_GET_SIZE(v
) == oparg
) {
1935 PyObject
**items
= \
1936 ((PyTupleObject
*)v
)->ob_item
;
1944 } else if (PyList_CheckExact(v
) &&
1945 PyList_GET_SIZE(v
) == oparg
) {
1946 PyObject
**items
= \
1947 ((PyListObject
*)v
)->ob_item
;
1953 } else if (unpack_iterable(v
, oparg
,
1954 stack_pointer
+ oparg
)) {
1955 stack_pointer
+= oparg
;
1957 /* unpack_iterable() raised an exception */
1958 why
= WHY_EXCEPTION
;
1964 w
= GETITEM(names
, oparg
);
1968 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1971 if (err
== 0) continue;
1975 w
= GETITEM(names
, oparg
);
1977 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1983 w
= GETITEM(names
, oparg
);
1985 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1987 if (err
== 0) continue;
1991 w
= GETITEM(names
, oparg
);
1992 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1993 format_exc_check_arg(
1994 PyExc_NameError
, GLOBAL_NAME_ERROR_MSG
, w
);
1998 w
= GETITEM(names
, oparg
);
1999 if ((v
= f
->f_locals
) == NULL
) {
2000 PyErr_Format(PyExc_SystemError
,
2001 "no locals when loading %s",
2003 why
= WHY_EXCEPTION
;
2006 if (PyDict_CheckExact(v
)) {
2007 x
= PyDict_GetItem(v
, w
);
2011 x
= PyObject_GetItem(v
, w
);
2012 if (x
== NULL
&& PyErr_Occurred()) {
2013 if (!PyErr_ExceptionMatches(
2020 x
= PyDict_GetItem(f
->f_globals
, w
);
2022 x
= PyDict_GetItem(f
->f_builtins
, w
);
2024 format_exc_check_arg(
2036 w
= GETITEM(names
, oparg
);
2037 if (PyString_CheckExact(w
)) {
2038 /* Inline the PyDict_GetItem() calls.
2039 WARNING: this is an extreme speed hack.
2040 Do not try this at home. */
2041 long hash
= ((PyStringObject
*)w
)->ob_shash
;
2045 d
= (PyDictObject
*)(f
->f_globals
);
2046 e
= d
->ma_lookup(d
, w
, hash
);
2057 d
= (PyDictObject
*)(f
->f_builtins
);
2058 e
= d
->ma_lookup(d
, w
, hash
);
2069 goto load_global_error
;
2072 /* This is the un-inlined version of the code above */
2073 x
= PyDict_GetItem(f
->f_globals
, w
);
2075 x
= PyDict_GetItem(f
->f_builtins
, w
);
2078 format_exc_check_arg(
2080 GLOBAL_NAME_ERROR_MSG
, w
);
2089 x
= GETLOCAL(oparg
);
2091 SETLOCAL(oparg
, NULL
);
2094 format_exc_check_arg(
2095 PyExc_UnboundLocalError
,
2096 UNBOUNDLOCAL_ERROR_MSG
,
2097 PyTuple_GetItem(co
->co_varnames
, oparg
)
2102 x
= freevars
[oparg
];
2105 if (x
!= NULL
) continue;
2109 x
= freevars
[oparg
];
2116 /* Don't stomp existing exception */
2117 if (PyErr_Occurred())
2119 if (oparg
< PyTuple_GET_SIZE(co
->co_cellvars
)) {
2120 v
= PyTuple_GET_ITEM(co
->co_cellvars
,
2122 format_exc_check_arg(
2123 PyExc_UnboundLocalError
,
2124 UNBOUNDLOCAL_ERROR_MSG
,
2127 v
= PyTuple_GET_ITEM(co
->co_freevars
, oparg
-
2128 PyTuple_GET_SIZE(co
->co_cellvars
));
2129 format_exc_check_arg(PyExc_NameError
,
2130 UNBOUNDFREE_ERROR_MSG
, v
);
2136 x
= freevars
[oparg
];
2142 x
= PyTuple_New(oparg
);
2144 for (; --oparg
>= 0;) {
2146 PyTuple_SET_ITEM(x
, oparg
, w
);
2154 x
= PyList_New(oparg
);
2156 for (; --oparg
>= 0;) {
2158 PyList_SET_ITEM(x
, oparg
, w
);
2166 x
= _PyDict_NewPresized((Py_ssize_t
)oparg
);
2168 if (x
!= NULL
) continue;
2172 w
= TOP(); /* key */
2173 u
= SECOND(); /* value */
2174 v
= THIRD(); /* dict */
2176 assert (PyDict_CheckExact(v
));
2177 err
= PyDict_SetItem(v
, w
, u
); /* v[w] = u */
2180 if (err
== 0) continue;
2184 w
= GETITEM(names
, oparg
);
2186 x
= PyObject_GetAttr(v
, w
);
2189 if (x
!= NULL
) continue;
2195 if (PyInt_CheckExact(w
) && PyInt_CheckExact(v
)) {
2196 /* INLINE: cmp(int, int) */
2199 a
= PyInt_AS_LONG(v
);
2200 b
= PyInt_AS_LONG(w
);
2202 case PyCmp_LT
: res
= a
< b
; break;
2203 case PyCmp_LE
: res
= a
<= b
; break;
2204 case PyCmp_EQ
: res
= a
== b
; break;
2205 case PyCmp_NE
: res
= a
!= b
; break;
2206 case PyCmp_GT
: res
= a
> b
; break;
2207 case PyCmp_GE
: res
= a
>= b
; break;
2208 case PyCmp_IS
: res
= v
== w
; break;
2209 case PyCmp_IS_NOT
: res
= v
!= w
; break;
2210 default: goto slow_compare
;
2212 x
= res
? Py_True
: Py_False
;
2217 x
= cmp_outcome(oparg
, v
, w
);
2222 if (x
== NULL
) break;
2223 PREDICT(POP_JUMP_IF_FALSE
);
2224 PREDICT(POP_JUMP_IF_TRUE
);
2228 w
= GETITEM(names
, oparg
);
2229 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
2231 PyErr_SetString(PyExc_ImportError
,
2232 "__import__ not found");
2238 if (PyInt_AsLong(u
) != -1 || PyErr_Occurred())
2242 f
->f_locals
== NULL
?
2243 Py_None
: f
->f_locals
,
2250 f
->f_locals
== NULL
?
2251 Py_None
: f
->f_locals
,
2261 READ_TIMESTAMP(intr0
);
2263 x
= PyEval_CallObject(v
, w
);
2265 READ_TIMESTAMP(intr1
);
2268 if (x
!= NULL
) continue;
2273 PyFrame_FastToLocals(f
);
2274 if ((x
= f
->f_locals
) == NULL
) {
2275 PyErr_SetString(PyExc_SystemError
,
2276 "no locals found during 'import *'");
2279 READ_TIMESTAMP(intr0
);
2280 err
= import_all_from(x
, v
);
2281 READ_TIMESTAMP(intr1
);
2282 PyFrame_LocalsToFast(f
, 0);
2284 if (err
== 0) continue;
2288 w
= GETITEM(names
, oparg
);
2290 READ_TIMESTAMP(intr0
);
2291 x
= import_from(v
, w
);
2292 READ_TIMESTAMP(intr1
);
2294 if (x
!= NULL
) continue;
2299 goto fast_next_opcode
;
2301 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE
);
2302 case POP_JUMP_IF_FALSE
:
2306 goto fast_next_opcode
;
2308 if (w
== Py_False
) {
2311 goto fast_next_opcode
;
2313 err
= PyObject_IsTrue(w
);
2323 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE
);
2324 case POP_JUMP_IF_TRUE
:
2326 if (w
== Py_False
) {
2328 goto fast_next_opcode
;
2333 goto fast_next_opcode
;
2335 err
= PyObject_IsTrue(w
);
2347 case JUMP_IF_FALSE_OR_POP
:
2352 goto fast_next_opcode
;
2354 if (w
== Py_False
) {
2356 goto fast_next_opcode
;
2358 err
= PyObject_IsTrue(w
);
2370 case JUMP_IF_TRUE_OR_POP
:
2372 if (w
== Py_False
) {
2375 goto fast_next_opcode
;
2379 goto fast_next_opcode
;
2381 err
= PyObject_IsTrue(w
);
2386 else if (err
== 0) {
2394 PREDICTED_WITH_ARG(JUMP_ABSOLUTE
);
2398 /* Enabling this path speeds-up all while and for-loops by bypassing
2399 the per-loop checks for signals. By default, this should be turned-off
2400 because it prevents detection of a control-break in tight loops like
2401 "while 1: pass". Compile with this option turned-on when you need
2402 the speed-up and do not need break checking inside tight loops (ones
2403 that contain only instructions ending with goto fast_next_opcode).
2405 goto fast_next_opcode
;
2411 /* before: [obj]; after [getiter(obj)] */
2413 x
= PyObject_GetIter(v
);
2423 PREDICTED_WITH_ARG(FOR_ITER
);
2425 /* before: [iter]; after: [iter, iter()] *or* [] */
2427 x
= (*v
->ob_type
->tp_iternext
)(v
);
2430 PREDICT(STORE_FAST
);
2431 PREDICT(UNPACK_SEQUENCE
);
2434 if (PyErr_Occurred()) {
2435 if (!PyErr_ExceptionMatches(
2436 PyExc_StopIteration
))
2440 /* iterator ended normally */
2448 goto fast_block_end
;
2451 retval
= PyInt_FromLong(oparg
);
2457 goto fast_block_end
;
2462 /* NOTE: If you add any new block-setup opcodes that
2463 are not try/except/finally handlers, you may need
2464 to update the PyGen_NeedsFinalizing() function.
2467 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
2473 static PyObject
*exit
, *enter
;
2475 x
= special_lookup(w
, "__exit__", &exit
);
2479 u
= special_lookup(w
, "__enter__", &enter
);
2485 x
= PyObject_CallFunctionObjArgs(u
, NULL
);
2489 /* Setup the finally block before pushing the result
2490 of __enter__ on the stack. */
2491 PyFrame_BlockSetup(f
, SETUP_FINALLY
, INSTR_OFFSET() + oparg
,
2500 /* At the top of the stack are 1-3 values indicating
2501 how/why we entered the finally clause:
2503 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2504 - TOP = WHY_*; no retval below it
2505 - (TOP, SECOND, THIRD) = exc_info()
2506 Below them is EXIT, the context.__exit__ bound method.
2507 In the last case, we must call
2508 EXIT(TOP, SECOND, THIRD)
2509 otherwise we must call
2510 EXIT(None, None, None)
2512 In all cases, we remove EXIT from the stack, leaving
2513 the rest in the same order.
2515 In addition, if the stack represents an exception,
2516 *and* the function call returns a 'true' value, we
2517 "zap" this information, to prevent END_FINALLY from
2518 re-raising the exception. (But non-local gotos
2519 should still be resumed.)
2522 PyObject
*exit_func
;
2530 else if (PyInt_Check(u
)) {
2531 switch(PyInt_AS_LONG(u
)) {
2534 /* Retval in TOP. */
2535 exit_func
= SECOND();
2544 u
= v
= w
= Py_None
;
2549 exit_func
= THIRD();
2554 /* XXX Not the fastest way to call it... */
2555 x
= PyObject_CallFunctionObjArgs(exit_func
, u
, v
, w
,
2557 Py_DECREF(exit_func
);
2559 break; /* Go to error exit */
2562 err
= PyObject_IsTrue(x
);
2568 break; /* Go to error exit */
2571 /* There was an exception and a true return */
2579 /* The stack was rearranged to remove EXIT
2580 above. Let END_FINALLY do its thing */
2582 PREDICT(END_FINALLY
);
2592 x
= call_function(&sp
, oparg
, &intr0
, &intr1
);
2594 x
= call_function(&sp
, oparg
);
2603 case CALL_FUNCTION_VAR
:
2604 case CALL_FUNCTION_KW
:
2605 case CALL_FUNCTION_VAR_KW
:
2607 int na
= oparg
& 0xff;
2608 int nk
= (oparg
>>8) & 0xff;
2609 int flags
= (opcode
- CALL_FUNCTION
) & 3;
2610 int n
= na
+ 2 * nk
;
2611 PyObject
**pfunc
, *func
, **sp
;
2613 if (flags
& CALL_FLAG_VAR
)
2615 if (flags
& CALL_FLAG_KW
)
2617 pfunc
= stack_pointer
- n
- 1;
2620 if (PyMethod_Check(func
)
2621 && PyMethod_GET_SELF(func
) != NULL
) {
2622 PyObject
*self
= PyMethod_GET_SELF(func
);
2624 func
= PyMethod_GET_FUNCTION(func
);
2633 READ_TIMESTAMP(intr0
);
2634 x
= ext_do_call(func
, &sp
, flags
, na
, nk
);
2635 READ_TIMESTAMP(intr1
);
2639 while (stack_pointer
> pfunc
) {
2650 v
= POP(); /* code object */
2651 x
= PyFunction_New(v
, f
->f_globals
);
2653 /* XXX Maybe this should be a separate opcode? */
2654 if (x
!= NULL
&& oparg
> 0) {
2655 v
= PyTuple_New(oparg
);
2661 while (--oparg
>= 0) {
2663 PyTuple_SET_ITEM(v
, oparg
, w
);
2665 err
= PyFunction_SetDefaults(x
, v
);
2673 v
= POP(); /* code object */
2674 x
= PyFunction_New(v
, f
->f_globals
);
2678 if (PyFunction_SetClosure(x
, v
) != 0) {
2679 /* Can't happen unless bytecode is corrupt. */
2680 why
= WHY_EXCEPTION
;
2684 if (x
!= NULL
&& oparg
> 0) {
2685 v
= PyTuple_New(oparg
);
2691 while (--oparg
>= 0) {
2693 PyTuple_SET_ITEM(v
, oparg
, w
);
2695 if (PyFunction_SetDefaults(x
, v
) != 0) {
2696 /* Can't happen unless
2697 PyFunction_SetDefaults changes. */
2698 why
= WHY_EXCEPTION
;
2713 x
= PySlice_New(u
, v
, w
);
2718 if (x
!= NULL
) continue;
2723 oparg
= oparg
<<16 | NEXTARG();
2724 goto dispatch_opcode
;
2728 "XXX lineno: %d, opcode: %d\n",
2729 PyFrame_GetLineNumber(f
),
2731 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
2732 why
= WHY_EXCEPTION
;
2743 READ_TIMESTAMP(inst1
);
2745 /* Quickly continue if no error occurred */
2747 if (why
== WHY_NOT
) {
2748 if (err
== 0 && x
!= NULL
) {
2750 /* This check is expensive! */
2751 if (PyErr_Occurred())
2753 "XXX undetected error\n");
2756 READ_TIMESTAMP(loop1
);
2757 continue; /* Normal, fast path */
2762 why
= WHY_EXCEPTION
;
2767 /* Double-check exception status */
2769 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
2770 if (!PyErr_Occurred()) {
2771 PyErr_SetString(PyExc_SystemError
,
2772 "error return without exception set");
2773 why
= WHY_EXCEPTION
;
2778 /* This check is expensive! */
2779 if (PyErr_Occurred()) {
2781 sprintf(buf
, "Stack unwind with exception "
2782 "set and why=%d", why
);
2788 /* Log traceback info if this is a real exception */
2790 if (why
== WHY_EXCEPTION
) {
2791 PyTraceBack_Here(f
);
2793 if (tstate
->c_tracefunc
!= NULL
)
2794 call_exc_trace(tstate
->c_tracefunc
,
2795 tstate
->c_traceobj
, f
);
2798 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2800 if (why
== WHY_RERAISE
)
2801 why
= WHY_EXCEPTION
;
2803 /* Unwind stacks if a (pseudo) exception occurred */
2806 while (why
!= WHY_NOT
&& f
->f_iblock
> 0) {
2807 PyTryBlock
*b
= PyFrame_BlockPop(f
);
2809 assert(why
!= WHY_YIELD
);
2810 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_CONTINUE
) {
2811 /* For a continue inside a try block,
2812 don't pop the block for the loop. */
2813 PyFrame_BlockSetup(f
, b
->b_type
, b
->b_handler
,
2816 JUMPTO(PyInt_AS_LONG(retval
));
2821 while (STACK_LEVEL() > b
->b_level
) {
2825 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
2827 JUMPTO(b
->b_handler
);
2830 if (b
->b_type
== SETUP_FINALLY
||
2831 (b
->b_type
== SETUP_EXCEPT
&&
2832 why
== WHY_EXCEPTION
)) {
2833 if (why
== WHY_EXCEPTION
) {
2834 PyObject
*exc
, *val
, *tb
;
2835 PyErr_Fetch(&exc
, &val
, &tb
);
2840 /* Make the raw exception data
2841 available to the handler,
2842 so a program can emulate the
2843 Python main loop. Don't do
2844 this for 'finally'. */
2845 if (b
->b_type
== SETUP_EXCEPT
) {
2846 PyErr_NormalizeException(
2848 set_exc_info(tstate
,
2860 if (why
& (WHY_RETURN
| WHY_CONTINUE
))
2862 v
= PyInt_FromLong((long)why
);
2866 JUMPTO(b
->b_handler
);
2869 } /* unwind stack */
2871 /* End the loop if we still have an error (or return) */
2875 READ_TIMESTAMP(loop1
);
2879 assert(why
!= WHY_YIELD
);
2880 /* Pop remaining stack entries. */
2886 if (why
!= WHY_RETURN
)
2890 if (tstate
->use_tracing
) {
2891 if (tstate
->c_tracefunc
) {
2892 if (why
== WHY_RETURN
|| why
== WHY_YIELD
) {
2893 if (call_trace(tstate
->c_tracefunc
,
2894 tstate
->c_traceobj
, f
,
2895 PyTrace_RETURN
, retval
)) {
2898 why
= WHY_EXCEPTION
;
2901 else if (why
== WHY_EXCEPTION
) {
2902 call_trace_protected(tstate
->c_tracefunc
,
2903 tstate
->c_traceobj
, f
,
2904 PyTrace_RETURN
, NULL
);
2907 if (tstate
->c_profilefunc
) {
2908 if (why
== WHY_EXCEPTION
)
2909 call_trace_protected(tstate
->c_profilefunc
,
2910 tstate
->c_profileobj
, f
,
2911 PyTrace_RETURN
, NULL
);
2912 else if (call_trace(tstate
->c_profilefunc
,
2913 tstate
->c_profileobj
, f
,
2914 PyTrace_RETURN
, retval
)) {
2917 why
= WHY_EXCEPTION
;
2922 if (tstate
->frame
->f_exc_type
!= NULL
)
2923 reset_exc_info(tstate
);
2925 assert(tstate
->frame
->f_exc_value
== NULL
);
2926 assert(tstate
->frame
->f_exc_traceback
== NULL
);
2931 Py_LeaveRecursiveCall();
2932 tstate
->frame
= f
->f_back
;
2937 /* This is gonna seem *real weird*, but if you put some other code between
2938 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2939 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2942 PyEval_EvalCodeEx(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
,
2943 PyObject
**args
, int argcount
, PyObject
**kws
, int kwcount
,
2944 PyObject
**defs
, int defcount
, PyObject
*closure
)
2946 register PyFrameObject
*f
;
2947 register PyObject
*retval
= NULL
;
2948 register PyObject
**fastlocals
, **freevars
;
2949 PyThreadState
*tstate
= PyThreadState_GET();
2952 if (globals
== NULL
) {
2953 PyErr_SetString(PyExc_SystemError
,
2954 "PyEval_EvalCodeEx: NULL globals");
2958 assert(tstate
!= NULL
);
2959 assert(globals
!= NULL
);
2960 f
= PyFrame_New(tstate
, co
, globals
, locals
);
2964 fastlocals
= f
->f_localsplus
;
2965 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
2967 if (co
->co_argcount
> 0 ||
2968 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
2971 PyObject
*kwdict
= NULL
;
2972 if (co
->co_flags
& CO_VARKEYWORDS
) {
2973 kwdict
= PyDict_New();
2976 i
= co
->co_argcount
;
2977 if (co
->co_flags
& CO_VARARGS
)
2979 SETLOCAL(i
, kwdict
);
2981 if (argcount
> co
->co_argcount
) {
2982 if (!(co
->co_flags
& CO_VARARGS
)) {
2983 PyErr_Format(PyExc_TypeError
,
2984 "%.200s() takes %s %d "
2985 "%sargument%s (%d given)",
2986 PyString_AsString(co
->co_name
),
2987 defcount
? "at most" : "exactly",
2989 kwcount
? "non-keyword " : "",
2990 co
->co_argcount
== 1 ? "" : "s",
2994 n
= co
->co_argcount
;
2996 for (i
= 0; i
< n
; i
++) {
3001 if (co
->co_flags
& CO_VARARGS
) {
3002 u
= PyTuple_New(argcount
- n
);
3005 SETLOCAL(co
->co_argcount
, u
);
3006 for (i
= n
; i
< argcount
; i
++) {
3009 PyTuple_SET_ITEM(u
, i
-n
, x
);
3012 for (i
= 0; i
< kwcount
; i
++) {
3013 PyObject
**co_varnames
;
3014 PyObject
*keyword
= kws
[2*i
];
3015 PyObject
*value
= kws
[2*i
+ 1];
3017 if (keyword
== NULL
|| !(PyString_Check(keyword
)
3018 #ifdef Py_USING_UNICODE
3019 || PyUnicode_Check(keyword
)
3022 PyErr_Format(PyExc_TypeError
,
3023 "%.200s() keywords must be strings",
3024 PyString_AsString(co
->co_name
));
3027 /* Speed hack: do raw pointer compares. As names are
3028 normally interned this should almost always hit. */
3029 co_varnames
= PySequence_Fast_ITEMS(co
->co_varnames
);
3030 for (j
= 0; j
< co
->co_argcount
; j
++) {
3031 PyObject
*nm
= co_varnames
[j
];
3035 /* Slow fallback, just in case */
3036 for (j
= 0; j
< co
->co_argcount
; j
++) {
3037 PyObject
*nm
= co_varnames
[j
];
3038 int cmp
= PyObject_RichCompareBool(
3039 keyword
, nm
, Py_EQ
);
3045 /* Check errors from Compare */
3046 if (PyErr_Occurred())
3048 if (j
>= co
->co_argcount
) {
3049 if (kwdict
== NULL
) {
3050 PyObject
*kwd_str
= kwd_as_string(keyword
);
3052 PyErr_Format(PyExc_TypeError
,
3053 "%.200s() got an unexpected "
3054 "keyword argument '%.400s'",
3055 PyString_AsString(co
->co_name
),
3056 PyString_AsString(kwd_str
));
3061 PyDict_SetItem(kwdict
, keyword
, value
);
3065 if (GETLOCAL(j
) != NULL
) {
3066 PyObject
*kwd_str
= kwd_as_string(keyword
);
3068 PyErr_Format(PyExc_TypeError
,
3069 "%.200s() got multiple "
3070 "values for keyword "
3071 "argument '%.400s'",
3072 PyString_AsString(co
->co_name
),
3073 PyString_AsString(kwd_str
));
3081 if (argcount
< co
->co_argcount
) {
3082 int m
= co
->co_argcount
- defcount
;
3083 for (i
= argcount
; i
< m
; i
++) {
3084 if (GETLOCAL(i
) == NULL
) {
3085 PyErr_Format(PyExc_TypeError
,
3086 "%.200s() takes %s %d "
3087 "%sargument%s (%d given)",
3088 PyString_AsString(co
->co_name
),
3089 ((co
->co_flags
& CO_VARARGS
) ||
3090 defcount
) ? "at least"
3092 m
, kwcount
? "non-keyword " : "",
3093 m
== 1 ? "" : "s", i
);
3101 for (; i
< defcount
; i
++) {
3102 if (GETLOCAL(m
+i
) == NULL
) {
3103 PyObject
*def
= defs
[i
];
3111 if (argcount
> 0 || kwcount
> 0) {
3112 PyErr_Format(PyExc_TypeError
,
3113 "%.200s() takes no arguments (%d given)",
3114 PyString_AsString(co
->co_name
),
3115 argcount
+ kwcount
);
3119 /* Allocate and initialize storage for cell vars, and copy free
3120 vars into frame. This isn't too efficient right now. */
3121 if (PyTuple_GET_SIZE(co
->co_cellvars
)) {
3122 int i
, j
, nargs
, found
;
3123 char *cellname
, *argname
;
3126 nargs
= co
->co_argcount
;
3127 if (co
->co_flags
& CO_VARARGS
)
3129 if (co
->co_flags
& CO_VARKEYWORDS
)
3132 /* Initialize each cell var, taking into account
3133 cell vars that are initialized from arguments.
3135 Should arrange for the compiler to put cellvars
3136 that are arguments at the beginning of the cellvars
3137 list so that we can march over it more efficiently?
3139 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_cellvars
); ++i
) {
3140 cellname
= PyString_AS_STRING(
3141 PyTuple_GET_ITEM(co
->co_cellvars
, i
));
3143 for (j
= 0; j
< nargs
; j
++) {
3144 argname
= PyString_AS_STRING(
3145 PyTuple_GET_ITEM(co
->co_varnames
, j
));
3146 if (strcmp(cellname
, argname
) == 0) {
3147 c
= PyCell_New(GETLOCAL(j
));
3150 GETLOCAL(co
->co_nlocals
+ i
) = c
;
3156 c
= PyCell_New(NULL
);
3159 SETLOCAL(co
->co_nlocals
+ i
, c
);
3163 if (PyTuple_GET_SIZE(co
->co_freevars
)) {
3165 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_freevars
); ++i
) {
3166 PyObject
*o
= PyTuple_GET_ITEM(closure
, i
);
3168 freevars
[PyTuple_GET_SIZE(co
->co_cellvars
) + i
] = o
;
3172 if (co
->co_flags
& CO_GENERATOR
) {
3173 /* Don't need to keep the reference to f_back, it will be set
3174 * when the generator is resumed. */
3175 Py_XDECREF(f
->f_back
);
3178 PCALL(PCALL_GENERATOR
);
3180 /* Create a new generator that owns the ready to run frame
3181 * and return that as the value. */
3182 return PyGen_New(f
);
3185 retval
= PyEval_EvalFrameEx(f
,0);
3187 fail
: /* Jump here from prelude on failure */
3189 /* decref'ing the frame can cause __del__ methods to get invoked,
3190 which can call back into Python. While we're done with the
3191 current Python frame (f), the associated C stack is still in use,
3192 so recursion_depth must be boosted for the duration.
3194 assert(tstate
!= NULL
);
3195 ++tstate
->recursion_depth
;
3197 --tstate
->recursion_depth
;
3203 special_lookup(PyObject
*o
, char *meth
, PyObject
**cache
)
3206 if (PyInstance_Check(o
)) {
3208 return PyObject_GetAttrString(o
, meth
);
3210 return PyObject_GetAttr(o
, *cache
);
3212 res
= _PyObject_LookupSpecial(o
, meth
, cache
);
3213 if (res
== NULL
&& !PyErr_Occurred()) {
3214 PyErr_SetObject(PyExc_AttributeError
, *cache
);
3222 kwd_as_string(PyObject
*kwd
) {
3223 #ifdef Py_USING_UNICODE
3224 if (PyString_Check(kwd
)) {
3226 assert(PyString_Check(kwd
));
3230 #ifdef Py_USING_UNICODE
3232 return _PyUnicode_AsDefaultEncodedString(kwd
, "replace");
3237 /* Implementation notes for set_exc_info() and reset_exc_info():
3239 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3240 'exc_traceback'. These always travel together.
3242 - tstate->curexc_ZZZ is the "hot" exception that is set by
3243 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3245 - Once an exception is caught by an except clause, it is transferred
3246 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3247 can pick it up. This is the primary task of set_exc_info().
3248 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
3250 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3252 Long ago, when none of this existed, there were just a few globals:
3253 one set corresponding to the "hot" exception, and one set
3254 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3255 globals; they were simply stored as sys.exc_ZZZ. For backwards
3256 compatibility, they still are!) The problem was that in code like
3260 "something that may fail"
3261 except "some exception":
3262 "do something else first"
3263 "print the exception from sys.exc_ZZZ."
3265 if "do something else first" invoked something that raised and caught
3266 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3267 cause of subtle bugs. I fixed this by changing the semantics as
3270 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3273 - But initially, and as long as no exception is caught in a given
3274 frame, sys.exc_ZZZ will hold the last exception caught in the
3275 previous frame (or the frame before that, etc.).
3277 The first bullet fixed the bug in the above example. The second
3278 bullet was for backwards compatibility: it was (and is) common to
3279 have a function that is called when an exception is caught, and to
3280 have that function access the caught exception via sys.exc_ZZZ.
3281 (Example: traceback.print_exc()).
3283 At the same time I fixed the problem that sys.exc_ZZZ weren't
3284 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3285 but that's really a separate improvement.
3287 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3288 variables to what they were before the current frame was called. The
3289 set_exc_info() function saves them on the frame so that
3290 reset_exc_info() can restore them. The invariant is that
3291 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3292 exception (where "catching" an exception applies only to successful
3293 except clauses); and if the current frame ever caught an exception,
3294 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3295 at the start of the current frame.
3300 set_exc_info(PyThreadState
*tstate
,
3301 PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3303 PyFrameObject
*frame
= tstate
->frame
;
3304 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
3306 assert(type
!= NULL
);
3307 assert(frame
!= NULL
);
3308 if (frame
->f_exc_type
== NULL
) {
3309 assert(frame
->f_exc_value
== NULL
);
3310 assert(frame
->f_exc_traceback
== NULL
);
3311 /* This frame didn't catch an exception before. */
3312 /* Save previous exception of this thread in this frame. */
3313 if (tstate
->exc_type
== NULL
) {
3314 /* XXX Why is this set to Py_None? */
3316 tstate
->exc_type
= Py_None
;
3318 Py_INCREF(tstate
->exc_type
);
3319 Py_XINCREF(tstate
->exc_value
);
3320 Py_XINCREF(tstate
->exc_traceback
);
3321 frame
->f_exc_type
= tstate
->exc_type
;
3322 frame
->f_exc_value
= tstate
->exc_value
;
3323 frame
->f_exc_traceback
= tstate
->exc_traceback
;
3325 /* Set new exception for this thread. */
3326 tmp_type
= tstate
->exc_type
;
3327 tmp_value
= tstate
->exc_value
;
3328 tmp_tb
= tstate
->exc_traceback
;
3332 tstate
->exc_type
= type
;
3333 tstate
->exc_value
= value
;
3334 tstate
->exc_traceback
= tb
;
3335 Py_XDECREF(tmp_type
);
3336 Py_XDECREF(tmp_value
);
3338 /* For b/w compatibility */
3339 PySys_SetObject("exc_type", type
);
3340 PySys_SetObject("exc_value", value
);
3341 PySys_SetObject("exc_traceback", tb
);
3345 reset_exc_info(PyThreadState
*tstate
)
3347 PyFrameObject
*frame
;
3348 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
3350 /* It's a precondition that the thread state's frame caught an
3351 * exception -- verify in a debug build.
3353 assert(tstate
!= NULL
);
3354 frame
= tstate
->frame
;
3355 assert(frame
!= NULL
);
3356 assert(frame
->f_exc_type
!= NULL
);
3358 /* Copy the frame's exception info back to the thread state. */
3359 tmp_type
= tstate
->exc_type
;
3360 tmp_value
= tstate
->exc_value
;
3361 tmp_tb
= tstate
->exc_traceback
;
3362 Py_INCREF(frame
->f_exc_type
);
3363 Py_XINCREF(frame
->f_exc_value
);
3364 Py_XINCREF(frame
->f_exc_traceback
);
3365 tstate
->exc_type
= frame
->f_exc_type
;
3366 tstate
->exc_value
= frame
->f_exc_value
;
3367 tstate
->exc_traceback
= frame
->f_exc_traceback
;
3368 Py_XDECREF(tmp_type
);
3369 Py_XDECREF(tmp_value
);
3372 /* For b/w compatibility */
3373 PySys_SetObject("exc_type", frame
->f_exc_type
);
3374 PySys_SetObject("exc_value", frame
->f_exc_value
);
3375 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
3377 /* Clear the frame's exception info. */
3378 tmp_type
= frame
->f_exc_type
;
3379 tmp_value
= frame
->f_exc_value
;
3380 tmp_tb
= frame
->f_exc_traceback
;
3381 frame
->f_exc_type
= NULL
;
3382 frame
->f_exc_value
= NULL
;
3383 frame
->f_exc_traceback
= NULL
;
3384 Py_DECREF(tmp_type
);
3385 Py_XDECREF(tmp_value
);
3389 /* Logic for the raise statement (too complicated for inlining).
3390 This *consumes* a reference count to each of its arguments. */
3391 static enum why_code
3392 do_raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3396 PyThreadState
*tstate
= PyThreadState_GET();
3397 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
3398 value
= tstate
->exc_value
;
3399 tb
= tstate
->exc_traceback
;
3405 /* We support the following forms of raise:
3406 raise <class>, <classinstance>
3407 raise <class>, <argument tuple>
3409 raise <class>, <argument>
3410 raise <classinstance>, None
3411 raise <string>, <object>
3412 raise <string>, None
3414 An omitted second argument is the same as None.
3416 In addition, raise <tuple>, <anything> is the same as
3417 raising the tuple's first item (and it better have one!);
3418 this rule is applied recursively.
3420 Finally, an optional third argument can be supplied, which
3421 gives the traceback to be substituted (useful when
3422 re-raising an exception after examining it). */
3424 /* First, check the traceback argument, replacing None with
3426 if (tb
== Py_None
) {
3430 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
3431 PyErr_SetString(PyExc_TypeError
,
3432 "raise: arg 3 must be a traceback or None");
3436 /* Next, replace a missing value with None */
3437 if (value
== NULL
) {
3442 /* Next, repeatedly, replace a tuple exception with its first item */
3443 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
3444 PyObject
*tmp
= type
;
3445 type
= PyTuple_GET_ITEM(type
, 0);
3450 if (PyExceptionClass_Check(type
))
3451 PyErr_NormalizeException(&type
, &value
, &tb
);
3453 else if (PyExceptionInstance_Check(type
)) {
3454 /* Raising an instance. The value should be a dummy. */
3455 if (value
!= Py_None
) {
3456 PyErr_SetString(PyExc_TypeError
,
3457 "instance exception may not have a separate value");
3461 /* Normalize to raise <class>, <instance> */
3464 type
= PyExceptionInstance_Class(type
);
3469 /* Not something you can raise. You get an exception
3470 anyway, just not what you specified :-) */
3471 PyErr_Format(PyExc_TypeError
,
3472 "exceptions must be classes or instances, not %s",
3473 type
->ob_type
->tp_name
);
3477 assert(PyExceptionClass_Check(type
));
3478 if (Py_Py3kWarningFlag
&& PyClass_Check(type
)) {
3479 if (PyErr_WarnEx(PyExc_DeprecationWarning
,
3480 "exceptions must derive from BaseException "
3485 PyErr_Restore(type
, value
, tb
);
3487 return WHY_EXCEPTION
;
3494 return WHY_EXCEPTION
;
3497 /* Iterate v argcnt times and store the results on the stack (via decreasing
3498 sp). Return 1 for success, 0 if error. */
3501 unpack_iterable(PyObject
*v
, int argcnt
, PyObject
**sp
)
3504 PyObject
*it
; /* iter(v) */
3509 it
= PyObject_GetIter(v
);
3513 for (; i
< argcnt
; i
++) {
3514 w
= PyIter_Next(it
);
3516 /* Iterator done, via error or exhaustion. */
3517 if (!PyErr_Occurred()) {
3518 PyErr_Format(PyExc_ValueError
,
3519 "need more than %d value%s to unpack",
3520 i
, i
== 1 ? "" : "s");
3527 /* We better have exhausted the iterator now. */
3528 w
= PyIter_Next(it
);
3530 if (PyErr_Occurred())
3536 PyErr_SetString(PyExc_ValueError
, "too many values to unpack");
3539 for (; i
> 0; i
--, sp
++)
3548 prtrace(PyObject
*v
, char *str
)
3551 if (PyObject_Print(v
, stdout
, 0) != 0)
3552 PyErr_Clear(); /* Don't know what else to do */
3559 call_exc_trace(Py_tracefunc func
, PyObject
*self
, PyFrameObject
*f
)
3561 PyObject
*type
, *value
, *traceback
, *arg
;
3563 PyErr_Fetch(&type
, &value
, &traceback
);
3564 if (value
== NULL
) {
3568 arg
= PyTuple_Pack(3, type
, value
, traceback
);
3570 PyErr_Restore(type
, value
, traceback
);
3573 err
= call_trace(func
, self
, f
, PyTrace_EXCEPTION
, arg
);
3576 PyErr_Restore(type
, value
, traceback
);
3580 Py_XDECREF(traceback
);
3585 call_trace_protected(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3586 int what
, PyObject
*arg
)
3588 PyObject
*type
, *value
, *traceback
;
3590 PyErr_Fetch(&type
, &value
, &traceback
);
3591 err
= call_trace(func
, obj
, frame
, what
, arg
);
3594 PyErr_Restore(type
, value
, traceback
);
3600 Py_XDECREF(traceback
);
3606 call_trace(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3607 int what
, PyObject
*arg
)
3609 register PyThreadState
*tstate
= frame
->f_tstate
;
3611 if (tstate
->tracing
)
3614 tstate
->use_tracing
= 0;
3615 result
= func(obj
, frame
, what
, arg
);
3616 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3617 || (tstate
->c_profilefunc
!= NULL
));
3623 _PyEval_CallTracing(PyObject
*func
, PyObject
*args
)
3625 PyFrameObject
*frame
= PyEval_GetFrame();
3626 PyThreadState
*tstate
= frame
->f_tstate
;
3627 int save_tracing
= tstate
->tracing
;
3628 int save_use_tracing
= tstate
->use_tracing
;
3631 tstate
->tracing
= 0;
3632 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3633 || (tstate
->c_profilefunc
!= NULL
));
3634 result
= PyObject_Call(func
, args
, NULL
);
3635 tstate
->tracing
= save_tracing
;
3636 tstate
->use_tracing
= save_use_tracing
;
3640 /* See Objects/lnotab_notes.txt for a description of how tracing works. */
3642 maybe_call_line_trace(Py_tracefunc func
, PyObject
*obj
,
3643 PyFrameObject
*frame
, int *instr_lb
, int *instr_ub
,
3647 int line
= frame
->f_lineno
;
3649 /* If the last instruction executed isn't in the current
3650 instruction window, reset the window.
3652 if (frame
->f_lasti
< *instr_lb
|| frame
->f_lasti
>= *instr_ub
) {
3654 line
= _PyCode_CheckLineNumber(frame
->f_code
, frame
->f_lasti
,
3656 *instr_lb
= bounds
.ap_lower
;
3657 *instr_ub
= bounds
.ap_upper
;
3659 /* If the last instruction falls at the start of a line or if
3660 it represents a jump backwards, update the frame's line
3661 number and call the trace function. */
3662 if (frame
->f_lasti
== *instr_lb
|| frame
->f_lasti
< *instr_prev
) {
3663 frame
->f_lineno
= line
;
3664 result
= call_trace(func
, obj
, frame
, PyTrace_LINE
, Py_None
);
3666 *instr_prev
= frame
->f_lasti
;
3671 PyEval_SetProfile(Py_tracefunc func
, PyObject
*arg
)
3673 PyThreadState
*tstate
= PyThreadState_GET();
3674 PyObject
*temp
= tstate
->c_profileobj
;
3676 tstate
->c_profilefunc
= NULL
;
3677 tstate
->c_profileobj
= NULL
;
3678 /* Must make sure that tracing is not ignored if 'temp' is freed */
3679 tstate
->use_tracing
= tstate
->c_tracefunc
!= NULL
;
3681 tstate
->c_profilefunc
= func
;
3682 tstate
->c_profileobj
= arg
;
3683 /* Flag that tracing or profiling is turned on */
3684 tstate
->use_tracing
= (func
!= NULL
) || (tstate
->c_tracefunc
!= NULL
);
3688 PyEval_SetTrace(Py_tracefunc func
, PyObject
*arg
)
3690 PyThreadState
*tstate
= PyThreadState_GET();
3691 PyObject
*temp
= tstate
->c_traceobj
;
3692 _Py_TracingPossible
+= (func
!= NULL
) - (tstate
->c_tracefunc
!= NULL
);
3694 tstate
->c_tracefunc
= NULL
;
3695 tstate
->c_traceobj
= NULL
;
3696 /* Must make sure that profiling is not ignored if 'temp' is freed */
3697 tstate
->use_tracing
= tstate
->c_profilefunc
!= NULL
;
3699 tstate
->c_tracefunc
= func
;
3700 tstate
->c_traceobj
= arg
;
3701 /* Flag that tracing or profiling is turned on */
3702 tstate
->use_tracing
= ((func
!= NULL
)
3703 || (tstate
->c_profilefunc
!= NULL
));
3707 PyEval_GetBuiltins(void)
3709 PyFrameObject
*current_frame
= PyEval_GetFrame();
3710 if (current_frame
== NULL
)
3711 return PyThreadState_GET()->interp
->builtins
;
3713 return current_frame
->f_builtins
;
3717 PyEval_GetLocals(void)
3719 PyFrameObject
*current_frame
= PyEval_GetFrame();
3720 if (current_frame
== NULL
)
3722 PyFrame_FastToLocals(current_frame
);
3723 return current_frame
->f_locals
;
3727 PyEval_GetGlobals(void)
3729 PyFrameObject
*current_frame
= PyEval_GetFrame();
3730 if (current_frame
== NULL
)
3733 return current_frame
->f_globals
;
3737 PyEval_GetFrame(void)
3739 PyThreadState
*tstate
= PyThreadState_GET();
3740 return _PyThreadState_GetFrame(tstate
);
3744 PyEval_GetRestricted(void)
3746 PyFrameObject
*current_frame
= PyEval_GetFrame();
3747 return current_frame
== NULL
? 0 : PyFrame_IsRestricted(current_frame
);
3751 PyEval_MergeCompilerFlags(PyCompilerFlags
*cf
)
3753 PyFrameObject
*current_frame
= PyEval_GetFrame();
3754 int result
= cf
->cf_flags
!= 0;
3756 if (current_frame
!= NULL
) {
3757 const int codeflags
= current_frame
->f_code
->co_flags
;
3758 const int compilerflags
= codeflags
& PyCF_MASK
;
3759 if (compilerflags
) {
3761 cf
->cf_flags
|= compilerflags
;
3763 #if 0 /* future keyword */
3764 if (codeflags
& CO_GENERATOR_ALLOWED
) {
3766 cf
->cf_flags
|= CO_GENERATOR_ALLOWED
;
3776 PyObject
*f
= PySys_GetObject("stdout");
3779 if (!PyFile_SoftSpace(f
, 0))
3781 return PyFile_WriteString("\n", f
);
3785 /* External interface to call any callable object.
3786 The arg must be a tuple or NULL. */
3788 #undef PyEval_CallObject
3789 /* for backward compatibility: export this interface */
3792 PyEval_CallObject(PyObject
*func
, PyObject
*arg
)
3794 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
3796 #define PyEval_CallObject(func,arg) \
3797 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3800 PyEval_CallObjectWithKeywords(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
3805 arg
= PyTuple_New(0);
3809 else if (!PyTuple_Check(arg
)) {
3810 PyErr_SetString(PyExc_TypeError
,
3811 "argument list must be a tuple");
3817 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
3818 PyErr_SetString(PyExc_TypeError
,
3819 "keyword list must be a dictionary");
3824 result
= PyObject_Call(func
, arg
, kw
);
3830 PyEval_GetFuncName(PyObject
*func
)
3832 if (PyMethod_Check(func
))
3833 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func
));
3834 else if (PyFunction_Check(func
))
3835 return PyString_AsString(((PyFunctionObject
*)func
)->func_name
);
3836 else if (PyCFunction_Check(func
))
3837 return ((PyCFunctionObject
*)func
)->m_ml
->ml_name
;
3838 else if (PyClass_Check(func
))
3839 return PyString_AsString(((PyClassObject
*)func
)->cl_name
);
3840 else if (PyInstance_Check(func
)) {
3841 return PyString_AsString(
3842 ((PyInstanceObject
*)func
)->in_class
->cl_name
);
3844 return func
->ob_type
->tp_name
;
3849 PyEval_GetFuncDesc(PyObject
*func
)
3851 if (PyMethod_Check(func
))
3853 else if (PyFunction_Check(func
))
3855 else if (PyCFunction_Check(func
))
3857 else if (PyClass_Check(func
))
3858 return " constructor";
3859 else if (PyInstance_Check(func
)) {
3867 err_args(PyObject
*func
, int flags
, int nargs
)
3869 if (flags
& METH_NOARGS
)
3870 PyErr_Format(PyExc_TypeError
,
3871 "%.200s() takes no arguments (%d given)",
3872 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3875 PyErr_Format(PyExc_TypeError
,
3876 "%.200s() takes exactly one argument (%d given)",
3877 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3881 #define C_TRACE(x, call) \
3882 if (tstate->use_tracing && tstate->c_profilefunc) { \
3883 if (call_trace(tstate->c_profilefunc, \
3884 tstate->c_profileobj, \
3885 tstate->frame, PyTrace_C_CALL, \
3891 if (tstate->c_profilefunc != NULL) { \
3893 call_trace_protected(tstate->c_profilefunc, \
3894 tstate->c_profileobj, \
3895 tstate->frame, PyTrace_C_EXCEPTION, \
3897 /* XXX should pass (type, value, tb) */ \
3899 if (call_trace(tstate->c_profilefunc, \
3900 tstate->c_profileobj, \
3901 tstate->frame, PyTrace_C_RETURN, \
3914 call_function(PyObject
***pp_stack
, int oparg
3916 , uint64
* pintr0
, uint64
* pintr1
3920 int na
= oparg
& 0xff;
3921 int nk
= (oparg
>>8) & 0xff;
3922 int n
= na
+ 2 * nk
;
3923 PyObject
**pfunc
= (*pp_stack
) - n
- 1;
3924 PyObject
*func
= *pfunc
;
3927 /* Always dispatch PyCFunction first, because these are
3928 presumed to be the most frequent callable object.
3930 if (PyCFunction_Check(func
) && nk
== 0) {
3931 int flags
= PyCFunction_GET_FLAGS(func
);
3932 PyThreadState
*tstate
= PyThreadState_GET();
3934 PCALL(PCALL_CFUNCTION
);
3935 if (flags
& (METH_NOARGS
| METH_O
)) {
3936 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
3937 PyObject
*self
= PyCFunction_GET_SELF(func
);
3938 if (flags
& METH_NOARGS
&& na
== 0) {
3939 C_TRACE(x
, (*meth
)(self
,NULL
));
3941 else if (flags
& METH_O
&& na
== 1) {
3942 PyObject
*arg
= EXT_POP(*pp_stack
);
3943 C_TRACE(x
, (*meth
)(self
,arg
));
3947 err_args(func
, flags
, na
);
3953 callargs
= load_args(pp_stack
, na
);
3954 READ_TIMESTAMP(*pintr0
);
3955 C_TRACE(x
, PyCFunction_Call(func
,callargs
,NULL
));
3956 READ_TIMESTAMP(*pintr1
);
3957 Py_XDECREF(callargs
);
3960 if (PyMethod_Check(func
) && PyMethod_GET_SELF(func
) != NULL
) {
3961 /* optimize access to bound methods */
3962 PyObject
*self
= PyMethod_GET_SELF(func
);
3963 PCALL(PCALL_METHOD
);
3964 PCALL(PCALL_BOUND_METHOD
);
3966 func
= PyMethod_GET_FUNCTION(func
);
3974 READ_TIMESTAMP(*pintr0
);
3975 if (PyFunction_Check(func
))
3976 x
= fast_function(func
, pp_stack
, n
, na
, nk
);
3978 x
= do_call(func
, pp_stack
, na
, nk
);
3979 READ_TIMESTAMP(*pintr1
);
3983 /* Clear the stack of the function object. Also removes
3984 the arguments in case they weren't consumed already
3985 (fast_function() and err_args() leave them on the stack).
3987 while ((*pp_stack
) > pfunc
) {
3988 w
= EXT_POP(*pp_stack
);
3995 /* The fast_function() function optimize calls for which no argument
3996 tuple is necessary; the objects are passed directly from the stack.
3997 For the simplest case -- a function that takes only positional
3998 arguments and is called with only positional arguments -- it
3999 inlines the most primitive frame setup code from
4000 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4001 done before evaluating the frame.
4005 fast_function(PyObject
*func
, PyObject
***pp_stack
, int n
, int na
, int nk
)
4007 PyCodeObject
*co
= (PyCodeObject
*)PyFunction_GET_CODE(func
);
4008 PyObject
*globals
= PyFunction_GET_GLOBALS(func
);
4009 PyObject
*argdefs
= PyFunction_GET_DEFAULTS(func
);
4010 PyObject
**d
= NULL
;
4013 PCALL(PCALL_FUNCTION
);
4014 PCALL(PCALL_FAST_FUNCTION
);
4015 if (argdefs
== NULL
&& co
->co_argcount
== n
&& nk
==0 &&
4016 co
->co_flags
== (CO_OPTIMIZED
| CO_NEWLOCALS
| CO_NOFREE
)) {
4018 PyObject
*retval
= NULL
;
4019 PyThreadState
*tstate
= PyThreadState_GET();
4020 PyObject
**fastlocals
, **stack
;
4023 PCALL(PCALL_FASTER_FUNCTION
);
4024 assert(globals
!= NULL
);
4025 /* XXX Perhaps we should create a specialized
4026 PyFrame_New() that doesn't take locals, but does
4027 take builtins without sanity checking them.
4029 assert(tstate
!= NULL
);
4030 f
= PyFrame_New(tstate
, co
, globals
, NULL
);
4034 fastlocals
= f
->f_localsplus
;
4035 stack
= (*pp_stack
) - n
;
4037 for (i
= 0; i
< n
; i
++) {
4039 fastlocals
[i
] = *stack
++;
4041 retval
= PyEval_EvalFrameEx(f
,0);
4042 ++tstate
->recursion_depth
;
4044 --tstate
->recursion_depth
;
4047 if (argdefs
!= NULL
) {
4048 d
= &PyTuple_GET_ITEM(argdefs
, 0);
4049 nd
= Py_SIZE(argdefs
);
4051 return PyEval_EvalCodeEx(co
, globals
,
4052 (PyObject
*)NULL
, (*pp_stack
)-n
, na
,
4053 (*pp_stack
)-2*nk
, nk
, d
, nd
,
4054 PyFunction_GET_CLOSURE(func
));
4058 update_keyword_args(PyObject
*orig_kwdict
, int nk
, PyObject
***pp_stack
,
4061 PyObject
*kwdict
= NULL
;
4062 if (orig_kwdict
== NULL
)
4063 kwdict
= PyDict_New();
4065 kwdict
= PyDict_Copy(orig_kwdict
);
4066 Py_DECREF(orig_kwdict
);
4072 PyObject
*value
= EXT_POP(*pp_stack
);
4073 PyObject
*key
= EXT_POP(*pp_stack
);
4074 if (PyDict_GetItem(kwdict
, key
) != NULL
) {
4075 PyErr_Format(PyExc_TypeError
,
4076 "%.200s%s got multiple values "
4077 "for keyword argument '%.200s'",
4078 PyEval_GetFuncName(func
),
4079 PyEval_GetFuncDesc(func
),
4080 PyString_AsString(key
));
4086 err
= PyDict_SetItem(kwdict
, key
, value
);
4098 update_star_args(int nstack
, int nstar
, PyObject
*stararg
,
4099 PyObject
***pp_stack
)
4101 PyObject
*callargs
, *w
;
4103 callargs
= PyTuple_New(nstack
+ nstar
);
4104 if (callargs
== NULL
) {
4109 for (i
= 0; i
< nstar
; i
++) {
4110 PyObject
*a
= PyTuple_GET_ITEM(stararg
, i
);
4112 PyTuple_SET_ITEM(callargs
, nstack
+ i
, a
);
4115 while (--nstack
>= 0) {
4116 w
= EXT_POP(*pp_stack
);
4117 PyTuple_SET_ITEM(callargs
, nstack
, w
);
4123 load_args(PyObject
***pp_stack
, int na
)
4125 PyObject
*args
= PyTuple_New(na
);
4131 w
= EXT_POP(*pp_stack
);
4132 PyTuple_SET_ITEM(args
, na
, w
);
4138 do_call(PyObject
*func
, PyObject
***pp_stack
, int na
, int nk
)
4140 PyObject
*callargs
= NULL
;
4141 PyObject
*kwdict
= NULL
;
4142 PyObject
*result
= NULL
;
4145 kwdict
= update_keyword_args(NULL
, nk
, pp_stack
, func
);
4149 callargs
= load_args(pp_stack
, na
);
4150 if (callargs
== NULL
)
4153 /* At this point, we have to look at the type of func to
4154 update the call stats properly. Do it here so as to avoid
4155 exposing the call stats machinery outside ceval.c
4157 if (PyFunction_Check(func
))
4158 PCALL(PCALL_FUNCTION
);
4159 else if (PyMethod_Check(func
))
4160 PCALL(PCALL_METHOD
);
4161 else if (PyType_Check(func
))
4163 else if (PyCFunction_Check(func
))
4164 PCALL(PCALL_CFUNCTION
);
4168 if (PyCFunction_Check(func
)) {
4169 PyThreadState
*tstate
= PyThreadState_GET();
4170 C_TRACE(result
, PyCFunction_Call(func
, callargs
, kwdict
));
4173 result
= PyObject_Call(func
, callargs
, kwdict
);
4175 Py_XDECREF(callargs
);
4181 ext_do_call(PyObject
*func
, PyObject
***pp_stack
, int flags
, int na
, int nk
)
4184 PyObject
*callargs
= NULL
;
4185 PyObject
*stararg
= NULL
;
4186 PyObject
*kwdict
= NULL
;
4187 PyObject
*result
= NULL
;
4189 if (flags
& CALL_FLAG_KW
) {
4190 kwdict
= EXT_POP(*pp_stack
);
4191 if (!PyDict_Check(kwdict
)) {
4196 if (PyDict_Update(d
, kwdict
) != 0) {
4198 /* PyDict_Update raises attribute
4199 * error (percolated from an attempt
4200 * to get 'keys' attribute) instead of
4201 * a type error if its second argument
4204 if (PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4205 PyErr_Format(PyExc_TypeError
,
4206 "%.200s%.200s argument after ** "
4207 "must be a mapping, not %.200s",
4208 PyEval_GetFuncName(func
),
4209 PyEval_GetFuncDesc(func
),
4210 kwdict
->ob_type
->tp_name
);
4218 if (flags
& CALL_FLAG_VAR
) {
4219 stararg
= EXT_POP(*pp_stack
);
4220 if (!PyTuple_Check(stararg
)) {
4222 t
= PySequence_Tuple(stararg
);
4224 if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
4225 PyErr_Format(PyExc_TypeError
,
4226 "%.200s%.200s argument after * "
4227 "must be a sequence, not %200s",
4228 PyEval_GetFuncName(func
),
4229 PyEval_GetFuncDesc(func
),
4230 stararg
->ob_type
->tp_name
);
4237 nstar
= PyTuple_GET_SIZE(stararg
);
4240 kwdict
= update_keyword_args(kwdict
, nk
, pp_stack
, func
);
4244 callargs
= update_star_args(na
, nstar
, stararg
, pp_stack
);
4245 if (callargs
== NULL
)
4248 /* At this point, we have to look at the type of func to
4249 update the call stats properly. Do it here so as to avoid
4250 exposing the call stats machinery outside ceval.c
4252 if (PyFunction_Check(func
))
4253 PCALL(PCALL_FUNCTION
);
4254 else if (PyMethod_Check(func
))
4255 PCALL(PCALL_METHOD
);
4256 else if (PyType_Check(func
))
4258 else if (PyCFunction_Check(func
))
4259 PCALL(PCALL_CFUNCTION
);
4263 if (PyCFunction_Check(func
)) {
4264 PyThreadState
*tstate
= PyThreadState_GET();
4265 C_TRACE(result
, PyCFunction_Call(func
, callargs
, kwdict
));
4268 result
= PyObject_Call(func
, callargs
, kwdict
);
4270 Py_XDECREF(callargs
);
4272 Py_XDECREF(stararg
);
4276 /* Extract a slice index from a PyInt or PyLong or an object with the
4277 nb_index slot defined, and store in *pi.
4278 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4279 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
4280 Return 0 on error, 1 on success.
4282 /* Note: If v is NULL, return success without storing into *pi. This
4283 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4284 called by the SLICE opcode with v and/or w equal to NULL.
4287 _PyEval_SliceIndex(PyObject
*v
, Py_ssize_t
*pi
)
4291 if (PyInt_Check(v
)) {
4292 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4293 however, it looks like it should be AsSsize_t.
4294 There should be a comment here explaining why.
4296 x
= PyInt_AS_LONG(v
);
4298 else if (PyIndex_Check(v
)) {
4299 x
= PyNumber_AsSsize_t(v
, NULL
);
4300 if (x
== -1 && PyErr_Occurred())
4304 PyErr_SetString(PyExc_TypeError
,
4305 "slice indices must be integers or "
4306 "None or have an __index__ method");
4315 #define ISINDEX(x) ((x) == NULL || \
4316 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
4319 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
4321 PyTypeObject
*tp
= u
->ob_type
;
4322 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4324 if (sq
&& sq
->sq_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4325 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4326 if (!_PyEval_SliceIndex(v
, &ilow
))
4328 if (!_PyEval_SliceIndex(w
, &ihigh
))
4330 return PySequence_GetSlice(u
, ilow
, ihigh
);
4333 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4334 if (slice
!= NULL
) {
4335 PyObject
*res
= PyObject_GetItem(u
, slice
);
4345 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
4348 PyTypeObject
*tp
= u
->ob_type
;
4349 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4351 if (sq
&& sq
->sq_ass_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4352 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4353 if (!_PyEval_SliceIndex(v
, &ilow
))
4355 if (!_PyEval_SliceIndex(w
, &ihigh
))
4358 return PySequence_DelSlice(u
, ilow
, ihigh
);
4360 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
4363 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4364 if (slice
!= NULL
) {
4367 res
= PyObject_SetItem(u
, slice
, x
);
4369 res
= PyObject_DelItem(u
, slice
);
4378 #define Py3kExceptionClass_Check(x) \
4379 (PyType_Check((x)) && \
4380 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4382 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4383 "BaseException is not allowed in 3.x"
4386 cmp_outcome(int op
, register PyObject
*v
, register PyObject
*w
)
4397 res
= PySequence_Contains(w
, v
);
4402 res
= PySequence_Contains(w
, v
);
4407 case PyCmp_EXC_MATCH
:
4408 if (PyTuple_Check(w
)) {
4409 Py_ssize_t i
, length
;
4410 length
= PyTuple_Size(w
);
4411 for (i
= 0; i
< length
; i
+= 1) {
4412 PyObject
*exc
= PyTuple_GET_ITEM(w
, i
);
4413 if (PyString_Check(exc
)) {
4415 ret_val
= PyErr_WarnEx(
4416 PyExc_DeprecationWarning
,
4417 "catching of string "
4418 "exceptions is deprecated", 1);
4422 else if (Py_Py3kWarningFlag
&&
4423 !PyTuple_Check(exc
) &&
4424 !Py3kExceptionClass_Check(exc
))
4427 ret_val
= PyErr_WarnEx(
4428 PyExc_DeprecationWarning
,
4429 CANNOT_CATCH_MSG
, 1);
4436 if (PyString_Check(w
)) {
4438 ret_val
= PyErr_WarnEx(
4439 PyExc_DeprecationWarning
,
4440 "catching of string "
4441 "exceptions is deprecated", 1);
4445 else if (Py_Py3kWarningFlag
&&
4446 !PyTuple_Check(w
) &&
4447 !Py3kExceptionClass_Check(w
))
4450 ret_val
= PyErr_WarnEx(
4451 PyExc_DeprecationWarning
,
4452 CANNOT_CATCH_MSG
, 1);
4457 res
= PyErr_GivenExceptionMatches(v
, w
);
4460 return PyObject_RichCompare(v
, w
, op
);
4462 v
= res
? Py_True
: Py_False
;
4468 import_from(PyObject
*v
, PyObject
*name
)
4472 x
= PyObject_GetAttr(v
, name
);
4473 if (x
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4474 PyErr_Format(PyExc_ImportError
,
4475 "cannot import name %.230s",
4476 PyString_AsString(name
));
4482 import_all_from(PyObject
*locals
, PyObject
*v
)
4484 PyObject
*all
= PyObject_GetAttrString(v
, "__all__");
4485 PyObject
*dict
, *name
, *value
;
4486 int skip_leading_underscores
= 0;
4490 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4491 return -1; /* Unexpected error */
4493 dict
= PyObject_GetAttrString(v
, "__dict__");
4495 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4497 PyErr_SetString(PyExc_ImportError
,
4498 "from-import-* object has no __dict__ and no __all__");
4501 all
= PyMapping_Keys(dict
);
4505 skip_leading_underscores
= 1;
4508 for (pos
= 0, err
= 0; ; pos
++) {
4509 name
= PySequence_GetItem(all
, pos
);
4511 if (!PyErr_ExceptionMatches(PyExc_IndexError
))
4517 if (skip_leading_underscores
&&
4518 PyString_Check(name
) &&
4519 PyString_AS_STRING(name
)[0] == '_')
4524 value
= PyObject_GetAttr(v
, name
);
4527 else if (PyDict_CheckExact(locals
))
4528 err
= PyDict_SetItem(locals
, name
, value
);
4530 err
= PyObject_SetItem(locals
, name
, value
);
4541 build_class(PyObject
*methods
, PyObject
*bases
, PyObject
*name
)
4543 PyObject
*metaclass
= NULL
, *result
, *base
;
4545 if (PyDict_Check(methods
))
4546 metaclass
= PyDict_GetItemString(methods
, "__metaclass__");
4547 if (metaclass
!= NULL
)
4548 Py_INCREF(metaclass
);
4549 else if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
4550 base
= PyTuple_GET_ITEM(bases
, 0);
4551 metaclass
= PyObject_GetAttrString(base
, "__class__");
4552 if (metaclass
== NULL
) {
4554 metaclass
= (PyObject
*)base
->ob_type
;
4555 Py_INCREF(metaclass
);
4559 PyObject
*g
= PyEval_GetGlobals();
4560 if (g
!= NULL
&& PyDict_Check(g
))
4561 metaclass
= PyDict_GetItemString(g
, "__metaclass__");
4562 if (metaclass
== NULL
)
4563 metaclass
= (PyObject
*) &PyClass_Type
;
4564 Py_INCREF(metaclass
);
4566 result
= PyObject_CallFunctionObjArgs(metaclass
, name
, bases
, methods
,
4568 Py_DECREF(metaclass
);
4569 if (result
== NULL
&& PyErr_ExceptionMatches(PyExc_TypeError
)) {
4570 /* A type error here likely means that the user passed
4571 in a base that was not a class (such the random module
4572 instead of the random.random type). Help them out with
4573 by augmenting the error message with more information.*/
4575 PyObject
*ptype
, *pvalue
, *ptraceback
;
4577 PyErr_Fetch(&ptype
, &pvalue
, &ptraceback
);
4578 if (PyString_Check(pvalue
)) {
4580 newmsg
= PyString_FromFormat(
4581 "Error when calling the metaclass bases\n"
4583 PyString_AS_STRING(pvalue
));
4584 if (newmsg
!= NULL
) {
4589 PyErr_Restore(ptype
, pvalue
, ptraceback
);
4595 exec_statement(PyFrameObject
*f
, PyObject
*prog
, PyObject
*globals
,
4602 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
4603 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
4604 /* Backward compatibility hack */
4605 globals
= PyTuple_GetItem(prog
, 1);
4607 locals
= PyTuple_GetItem(prog
, 2);
4608 prog
= PyTuple_GetItem(prog
, 0);
4610 if (globals
== Py_None
) {
4611 globals
= PyEval_GetGlobals();
4612 if (locals
== Py_None
) {
4613 locals
= PyEval_GetLocals();
4616 if (!globals
|| !locals
) {
4617 PyErr_SetString(PyExc_SystemError
,
4618 "globals and locals cannot be NULL");
4622 else if (locals
== Py_None
)
4624 if (!PyString_Check(prog
) &&
4625 #ifdef Py_USING_UNICODE
4626 !PyUnicode_Check(prog
) &&
4628 !PyCode_Check(prog
) &&
4629 !PyFile_Check(prog
)) {
4630 PyErr_SetString(PyExc_TypeError
,
4631 "exec: arg 1 must be a string, file, or code object");
4634 if (!PyDict_Check(globals
)) {
4635 PyErr_SetString(PyExc_TypeError
,
4636 "exec: arg 2 must be a dictionary or None");
4639 if (!PyMapping_Check(locals
)) {
4640 PyErr_SetString(PyExc_TypeError
,
4641 "exec: arg 3 must be a mapping or None");
4644 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
4645 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
4646 if (PyCode_Check(prog
)) {
4647 if (PyCode_GetNumFree((PyCodeObject
*)prog
) > 0) {
4648 PyErr_SetString(PyExc_TypeError
,
4649 "code object passed to exec may not contain free variables");
4652 v
= PyEval_EvalCode((PyCodeObject
*) prog
, globals
, locals
);
4654 else if (PyFile_Check(prog
)) {
4655 FILE *fp
= PyFile_AsFile(prog
);
4656 char *name
= PyString_AsString(PyFile_Name(prog
));
4661 if (PyEval_MergeCompilerFlags(&cf
))
4662 v
= PyRun_FileFlags(fp
, name
, Py_file_input
, globals
,
4665 v
= PyRun_File(fp
, name
, Py_file_input
, globals
,
4669 PyObject
*tmp
= NULL
;
4673 #ifdef Py_USING_UNICODE
4674 if (PyUnicode_Check(prog
)) {
4675 tmp
= PyUnicode_AsUTF8String(prog
);
4679 cf
.cf_flags
|= PyCF_SOURCE_IS_UTF8
;
4682 if (PyString_AsStringAndSize(prog
, &str
, NULL
))
4684 if (PyEval_MergeCompilerFlags(&cf
))
4685 v
= PyRun_StringFlags(str
, Py_file_input
, globals
,
4688 v
= PyRun_String(str
, Py_file_input
, globals
, locals
);
4692 PyFrame_LocalsToFast(f
, 0);
4700 format_exc_check_arg(PyObject
*exc
, char *format_str
, PyObject
*obj
)
4707 obj_str
= PyString_AsString(obj
);
4711 PyErr_Format(exc
, format_str
, obj_str
);
4715 string_concatenate(PyObject
*v
, PyObject
*w
,
4716 PyFrameObject
*f
, unsigned char *next_instr
)
4718 /* This function implements 'variable += expr' when both arguments
4720 Py_ssize_t v_len
= PyString_GET_SIZE(v
);
4721 Py_ssize_t w_len
= PyString_GET_SIZE(w
);
4722 Py_ssize_t new_len
= v_len
+ w_len
;
4724 PyErr_SetString(PyExc_OverflowError
,
4725 "strings are too large to concat");
4729 if (v
->ob_refcnt
== 2) {
4730 /* In the common case, there are 2 references to the value
4731 * stored in 'variable' when the += is performed: one on the
4732 * value stack (in 'v') and one still stored in the
4733 * 'variable'. We try to delete the variable now to reduce
4736 switch (*next_instr
) {
4739 int oparg
= PEEKARG();
4740 PyObject
**fastlocals
= f
->f_localsplus
;
4741 if (GETLOCAL(oparg
) == v
)
4742 SETLOCAL(oparg
, NULL
);
4747 PyObject
**freevars
= (f
->f_localsplus
+
4748 f
->f_code
->co_nlocals
);
4749 PyObject
*c
= freevars
[PEEKARG()];
4750 if (PyCell_GET(c
) == v
)
4751 PyCell_Set(c
, NULL
);
4756 PyObject
*names
= f
->f_code
->co_names
;
4757 PyObject
*name
= GETITEM(names
, PEEKARG());
4758 PyObject
*locals
= f
->f_locals
;
4759 if (PyDict_CheckExact(locals
) &&
4760 PyDict_GetItem(locals
, name
) == v
) {
4761 if (PyDict_DelItem(locals
, name
) != 0) {
4770 if (v
->ob_refcnt
== 1 && !PyString_CHECK_INTERNED(v
)) {
4771 /* Now we own the last reference to 'v', so we can resize it
4774 if (_PyString_Resize(&v
, new_len
) != 0) {
4775 /* XXX if _PyString_Resize() fails, 'v' has been
4776 * deallocated so it cannot be put back into
4777 * 'variable'. The MemoryError is raised when there
4778 * is no value in 'variable', which might (very
4779 * remotely) be a cause of incompatibilities.
4783 /* copy 'w' into the newly allocated area of 'v' */
4784 memcpy(PyString_AS_STRING(v
) + v_len
,
4785 PyString_AS_STRING(w
), w_len
);
4789 /* When in-place resizing is not an option. */
4790 PyString_Concat(&v
, w
);
4795 #ifdef DYNAMIC_EXECUTION_PROFILE
4798 getarray(long a
[256])
4801 PyObject
*l
= PyList_New(256);
4802 if (l
== NULL
) return NULL
;
4803 for (i
= 0; i
< 256; i
++) {
4804 PyObject
*x
= PyInt_FromLong(a
[i
]);
4809 PyList_SetItem(l
, i
, x
);
4811 for (i
= 0; i
< 256; i
++)
4817 _Py_GetDXProfile(PyObject
*self
, PyObject
*args
)
4820 return getarray(dxp
);
4823 PyObject
*l
= PyList_New(257);
4824 if (l
== NULL
) return NULL
;
4825 for (i
= 0; i
< 257; i
++) {
4826 PyObject
*x
= getarray(dxpairs
[i
]);
4831 PyList_SetItem(l
, i
, x
);