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 void call_trace_protected(Py_tracefunc
, PyObject
*,
111 PyFrameObject
*, int, PyObject
*);
112 static void call_exc_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*);
113 static int maybe_call_line_trace(Py_tracefunc
, PyObject
*,
114 PyFrameObject
*, int *, int *, int *);
116 static PyObject
* apply_slice(PyObject
*, PyObject
*, PyObject
*);
117 static int assign_slice(PyObject
*, PyObject
*,
118 PyObject
*, PyObject
*);
119 static PyObject
* cmp_outcome(int, PyObject
*, PyObject
*);
120 static PyObject
* import_from(PyObject
*, PyObject
*);
121 static int import_all_from(PyObject
*, PyObject
*);
122 static PyObject
* build_class(PyObject
*, PyObject
*, PyObject
*);
123 static int exec_statement(PyFrameObject
*,
124 PyObject
*, PyObject
*, PyObject
*);
125 static void set_exc_info(PyThreadState
*, PyObject
*, PyObject
*, PyObject
*);
126 static void reset_exc_info(PyThreadState
*);
127 static void format_exc_check_arg(PyObject
*, char *, PyObject
*);
128 static PyObject
* string_concatenate(PyObject
*, PyObject
*,
129 PyFrameObject
*, unsigned char *);
131 #define NAME_ERROR_MSG \
132 "name '%.200s' is not defined"
133 #define GLOBAL_NAME_ERROR_MSG \
134 "global name '%.200s' is not defined"
135 #define UNBOUNDLOCAL_ERROR_MSG \
136 "local variable '%.200s' referenced before assignment"
137 #define UNBOUNDFREE_ERROR_MSG \
138 "free variable '%.200s' referenced before assignment" \
139 " in enclosing scope"
141 /* Dynamic execution profile */
142 #ifdef DYNAMIC_EXECUTION_PROFILE
144 static long dxpairs
[257][256];
145 #define dxp dxpairs[256]
147 static long dxp
[256];
151 /* Function call profile */
154 static int pcall
[PCALL_NUM
];
157 #define PCALL_FUNCTION 1
158 #define PCALL_FAST_FUNCTION 2
159 #define PCALL_FASTER_FUNCTION 3
160 #define PCALL_METHOD 4
161 #define PCALL_BOUND_METHOD 5
162 #define PCALL_CFUNCTION 6
164 #define PCALL_GENERATOR 8
165 #define PCALL_OTHER 9
168 /* Notes about the statistics
172 FAST_FUNCTION means no argument tuple needs to be created.
173 FASTER_FUNCTION means that the fast-path frame setup code is used.
175 If there is a method call where the call can be optimized by changing
176 the argument tuple and calling the function directly, it gets recorded
179 As a result, the relationship among the statistics appears to be
180 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
181 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
182 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
183 PCALL_METHOD > PCALL_BOUND_METHOD
186 #define PCALL(POS) pcall[POS]++
189 PyEval_GetCallStats(PyObject
*self
)
191 return Py_BuildValue("iiiiiiiiiii",
192 pcall
[0], pcall
[1], pcall
[2], pcall
[3],
193 pcall
[4], pcall
[5], pcall
[6], pcall
[7],
194 pcall
[8], pcall
[9], pcall
[10]);
200 PyEval_GetCallStats(PyObject
*self
)
213 #include "pythread.h"
215 static PyThread_type_lock interpreter_lock
= 0; /* This is the GIL */
216 static long main_thread
= 0;
219 PyEval_ThreadsInitialized(void)
221 return interpreter_lock
!= 0;
225 PyEval_InitThreads(void)
227 if (interpreter_lock
)
229 interpreter_lock
= PyThread_allocate_lock();
230 PyThread_acquire_lock(interpreter_lock
, 1);
231 main_thread
= PyThread_get_thread_ident();
235 PyEval_AcquireLock(void)
237 PyThread_acquire_lock(interpreter_lock
, 1);
241 PyEval_ReleaseLock(void)
243 PyThread_release_lock(interpreter_lock
);
247 PyEval_AcquireThread(PyThreadState
*tstate
)
250 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
251 /* Check someone has called PyEval_InitThreads() to create the lock */
252 assert(interpreter_lock
);
253 PyThread_acquire_lock(interpreter_lock
, 1);
254 if (PyThreadState_Swap(tstate
) != NULL
)
256 "PyEval_AcquireThread: non-NULL old thread state");
260 PyEval_ReleaseThread(PyThreadState
*tstate
)
263 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
264 if (PyThreadState_Swap(NULL
) != tstate
)
265 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
266 PyThread_release_lock(interpreter_lock
);
269 /* This function is called from PyOS_AfterFork to ensure that newly
270 created child processes don't hold locks referring to threads which
271 are not running in the child process. (This could also be done using
272 pthread_atfork mechanism, at least for the pthreads implementation.) */
275 PyEval_ReInitThreads(void)
277 if (!interpreter_lock
)
279 /*XXX Can't use PyThread_free_lock here because it does too
280 much error-checking. Doing this cleanly would require
281 adding a new function to each thread_*.h. Instead, just
282 create a new lock and waste a little bit of memory */
283 interpreter_lock
= PyThread_allocate_lock();
284 PyThread_acquire_lock(interpreter_lock
, 1);
285 main_thread
= PyThread_get_thread_ident();
289 /* Functions save_thread and restore_thread are always defined so
290 dynamically loaded modules needn't be compiled separately for use
291 with and without threads: */
294 PyEval_SaveThread(void)
296 PyThreadState
*tstate
= PyThreadState_Swap(NULL
);
298 Py_FatalError("PyEval_SaveThread: NULL tstate");
300 if (interpreter_lock
)
301 PyThread_release_lock(interpreter_lock
);
307 PyEval_RestoreThread(PyThreadState
*tstate
)
310 Py_FatalError("PyEval_RestoreThread: NULL tstate");
312 if (interpreter_lock
) {
314 PyThread_acquire_lock(interpreter_lock
, 1);
318 PyThreadState_Swap(tstate
);
322 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
323 signal handlers or Mac I/O completion routines) can schedule calls
324 to a function to be called synchronously.
325 The synchronous function is called with one void* argument.
326 It should return 0 for success or -1 for failure -- failure should
327 be accompanied by an exception.
329 If registry succeeds, the registry function returns 0; if it fails
330 (e.g. due to too many pending calls) it returns -1 (without setting
331 an exception condition).
333 Note that because registry may occur from within signal handlers,
334 or other asynchronous events, calling malloc() is unsafe!
337 Any thread can schedule pending calls, but only the main thread
341 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
342 There are two possible race conditions:
343 (1) nested asynchronous registry calls;
344 (2) registry calls made while pending calls are being processed.
345 While (1) is very unlikely, (2) is a real possibility.
346 The current code is safe against (2), but not against (1).
347 The safety against (2) is derived from the fact that only one
348 thread (the main thread) ever takes things out of the queue.
350 XXX Darn! With the advent of thread state, we should have an array
351 of pending calls per thread in the thread state! Later...
354 #define NPENDINGCALLS 32
358 } pendingcalls
[NPENDINGCALLS
];
359 static volatile int pendingfirst
= 0;
360 static volatile int pendinglast
= 0;
361 static volatile int things_to_do
= 0;
364 Py_AddPendingCall(int (*func
)(void *), void *arg
)
366 static volatile int busy
= 0;
368 /* XXX Begin critical section */
369 /* XXX If you want this to be safe against nested
370 XXX asynchronous calls, you'll have to work harder! */
375 j
= (i
+ 1) % NPENDINGCALLS
;
376 if (j
== pendingfirst
) {
378 return -1; /* Queue full */
380 pendingcalls
[i
].func
= func
;
381 pendingcalls
[i
].arg
= arg
;
385 things_to_do
= 1; /* Signal main loop */
387 /* XXX End critical section */
392 Py_MakePendingCalls(void)
396 if (main_thread
&& PyThread_get_thread_ident() != main_thread
)
408 if (i
== pendinglast
)
409 break; /* Queue empty */
410 func
= pendingcalls
[i
].func
;
411 arg
= pendingcalls
[i
].arg
;
412 pendingfirst
= (i
+ 1) % NPENDINGCALLS
;
415 things_to_do
= 1; /* We're not done yet */
424 /* The interpreter's recursion limit */
426 #ifndef Py_DEFAULT_RECURSION_LIMIT
427 #define Py_DEFAULT_RECURSION_LIMIT 1000
429 static int recursion_limit
= Py_DEFAULT_RECURSION_LIMIT
;
430 int _Py_CheckRecursionLimit
= Py_DEFAULT_RECURSION_LIMIT
;
433 Py_GetRecursionLimit(void)
435 return recursion_limit
;
439 Py_SetRecursionLimit(int new_limit
)
441 recursion_limit
= new_limit
;
442 _Py_CheckRecursionLimit
= recursion_limit
;
445 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
446 if the recursion_depth reaches _Py_CheckRecursionLimit.
447 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
448 to guarantee that _Py_CheckRecursiveCall() is regularly called.
449 Without USE_STACKCHECK, there is no need for this. */
451 _Py_CheckRecursiveCall(char *where
)
453 PyThreadState
*tstate
= PyThreadState_GET();
455 #ifdef USE_STACKCHECK
456 if (PyOS_CheckStack()) {
457 --tstate
->recursion_depth
;
458 PyErr_SetString(PyExc_MemoryError
, "Stack overflow");
462 if (tstate
->recursion_depth
> recursion_limit
) {
463 --tstate
->recursion_depth
;
464 PyErr_Format(PyExc_RuntimeError
,
465 "maximum recursion depth exceeded%s",
469 _Py_CheckRecursionLimit
= recursion_limit
;
473 /* Status code for main loop (reason for stack unwind) */
475 WHY_NOT
= 0x0001, /* No error */
476 WHY_EXCEPTION
= 0x0002, /* Exception occurred */
477 WHY_RERAISE
= 0x0004, /* Exception re-raised by 'finally' */
478 WHY_RETURN
= 0x0008, /* 'return' statement */
479 WHY_BREAK
= 0x0010, /* 'break' statement */
480 WHY_CONTINUE
= 0x0020, /* 'continue' statement */
481 WHY_YIELD
= 0x0040 /* 'yield' operator */
484 static enum why_code
do_raise(PyObject
*, PyObject
*, PyObject
*);
485 static int unpack_iterable(PyObject
*, int, PyObject
**);
487 /* for manipulating the thread switch and periodic "stuff" - used to be
488 per thread, now just a pair o' globals */
489 int _Py_CheckInterval
= 100;
490 volatile int _Py_Ticker
= 100;
493 PyEval_EvalCode(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
)
495 return PyEval_EvalCodeEx(co
,
497 (PyObject
**)NULL
, 0,
498 (PyObject
**)NULL
, 0,
499 (PyObject
**)NULL
, 0,
504 /* Interpreter main loop */
507 PyEval_EvalFrame(PyFrameObject
*f
) {
508 /* This is for backward compatibility with extension modules that
509 used this API; core interpreter code should call
510 PyEval_EvalFrameEx() */
511 return PyEval_EvalFrameEx(f
, 0);
515 PyEval_EvalFrameEx(PyFrameObject
*f
, int throwflag
)
520 register PyObject
**stack_pointer
; /* Next free slot in value stack */
521 register unsigned char *next_instr
;
522 register int opcode
; /* Current opcode */
523 register int oparg
; /* Current opcode argument, if any */
524 register enum why_code why
; /* Reason for block stack unwind */
525 register int err
; /* Error status -- nonzero if error */
526 register PyObject
*x
; /* Result object -- NULL if error */
527 register PyObject
*v
; /* Temporary objects popped off stack */
528 register PyObject
*w
;
529 register PyObject
*u
;
530 register PyObject
*t
;
531 register PyObject
*stream
= NULL
; /* for PRINT opcodes */
532 register PyObject
**fastlocals
, **freevars
;
533 PyObject
*retval
= NULL
; /* Return value */
534 PyThreadState
*tstate
= PyThreadState_GET();
537 /* when tracing we set things up so that
539 not (instr_lb <= current_bytecode_offset < instr_ub)
541 is true when the line being executed has changed. The
542 initial values are such as to make this false the first
543 time it is tested. */
544 int instr_ub
= -1, instr_lb
= 0, instr_prev
= -1;
546 unsigned char *first_instr
;
549 #if defined(Py_DEBUG) || defined(LLTRACE)
550 /* Make it easier to find out where we are with a debugger */
554 /* Tuple access macros */
557 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
559 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
563 /* Use Pentium timestamp counter to mark certain events:
564 inst0 -- beginning of switch statement for opcode dispatch
565 inst1 -- end of switch statement (may be skipped)
566 loop0 -- the top of the mainloop
567 loop1 -- place where control returns again to top of mainloop
569 intr1 -- beginning of long interruption
570 intr2 -- end of long interruption
572 Many opcodes call out to helper C functions. In some cases, the
573 time in those functions should be counted towards the time for the
574 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
575 calls another Python function; there's no point in charge all the
576 bytecode executed by the called function to the caller.
578 It's hard to make a useful judgement statically. In the presence
579 of operator overloading, it's impossible to tell if a call will
580 execute new Python code or not.
582 It's a case-by-case judgement. I'll use intr1 for the following
588 CALL_FUNCTION (and friends)
591 uint64 inst0
, inst1
, loop0
, loop1
, intr0
= 0, intr1
= 0;
594 READ_TIMESTAMP(inst0
);
595 READ_TIMESTAMP(inst1
);
596 READ_TIMESTAMP(loop0
);
597 READ_TIMESTAMP(loop1
);
599 /* shut up the compiler */
603 /* Code access macros */
605 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
606 #define NEXTOP() (*next_instr++)
607 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
608 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
609 #define JUMPTO(x) (next_instr = first_instr + (x))
610 #define JUMPBY(x) (next_instr += (x))
612 /* OpCode prediction macros
613 Some opcodes tend to come in pairs thus making it possible to
614 predict the second code when the first is run. For example,
615 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
616 those opcodes are often followed by a POP_TOP.
618 Verifying the prediction costs a single high-speed test of register
619 variable against a constant. If the pairing was good, then the
620 processor has a high likelihood of making its own successful branch
621 prediction which results in a nearly zero overhead transition to the
624 A successful prediction saves a trip through the eval-loop including
625 its two unpredictable branches, the HASARG test and the switch-case.
627 If collecting opcode statistics, turn off prediction so that
628 statistics are accurately maintained (the predictions bypass
629 the opcode frequency counter updates).
632 #ifdef DYNAMIC_EXECUTION_PROFILE
633 #define PREDICT(op) if (0) goto PRED_##op
635 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
638 #define PREDICTED(op) PRED_##op: next_instr++
639 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
641 /* Stack manipulation macros */
643 /* The stack can grow at most MAXINT deep, as co_nlocals and
644 co_stacksize are ints. */
645 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
646 #define EMPTY() (STACK_LEVEL() == 0)
647 #define TOP() (stack_pointer[-1])
648 #define SECOND() (stack_pointer[-2])
649 #define THIRD() (stack_pointer[-3])
650 #define FOURTH() (stack_pointer[-4])
651 #define SET_TOP(v) (stack_pointer[-1] = (v))
652 #define SET_SECOND(v) (stack_pointer[-2] = (v))
653 #define SET_THIRD(v) (stack_pointer[-3] = (v))
654 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
655 #define BASIC_STACKADJ(n) (stack_pointer += n)
656 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
657 #define BASIC_POP() (*--stack_pointer)
660 #define PUSH(v) { (void)(BASIC_PUSH(v), \
661 lltrace && prtrace(TOP(), "push")); \
662 assert(STACK_LEVEL() <= co->co_stacksize); }
663 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
665 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
666 lltrace && prtrace(TOP(), "stackadj")); \
667 assert(STACK_LEVEL() <= co->co_stacksize); }
668 #define EXT_POP(STACK_POINTER) (lltrace && prtrace((STACK_POINTER)[-1], \
669 "ext_pop"), *--(STACK_POINTER))
671 #define PUSH(v) BASIC_PUSH(v)
672 #define POP() BASIC_POP()
673 #define STACKADJ(n) BASIC_STACKADJ(n)
674 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
677 /* Local variable macros */
679 #define GETLOCAL(i) (fastlocals[i])
681 /* The SETLOCAL() macro must not DECREF the local variable in-place and
682 then store the new value; it must copy the old value to a temporary
683 value, then store the new value, and then DECREF the temporary value.
684 This is because it is possible that during the DECREF the frame is
685 accessed by other code (e.g. a __del__ method or gc.collect()) and the
686 variable would be pointing to already-freed memory. */
687 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
688 GETLOCAL(i) = value; \
689 Py_XDECREF(tmp); } while (0)
697 if (Py_EnterRecursiveCall(""))
702 if (tstate
->use_tracing
) {
703 if (tstate
->c_tracefunc
!= NULL
) {
704 /* tstate->c_tracefunc, if defined, is a
705 function that will be called on *every* entry
706 to a code block. Its return value, if not
707 None, is a function that will be called at
708 the start of each executed line of code.
709 (Actually, the function must return itself
710 in order to continue tracing.) The trace
711 functions are called with three arguments:
712 a pointer to the current frame, a string
713 indicating why the function is called, and
714 an argument which depends on the situation.
715 The global trace function is also called
716 whenever an exception is detected. */
717 if (call_trace(tstate
->c_tracefunc
, tstate
->c_traceobj
,
718 f
, PyTrace_CALL
, Py_None
)) {
719 /* Trace function raised an error */
720 goto exit_eval_frame
;
723 if (tstate
->c_profilefunc
!= NULL
) {
724 /* Similar for c_profilefunc, except it needn't
725 return itself and isn't called for "line" events */
726 if (call_trace(tstate
->c_profilefunc
,
727 tstate
->c_profileobj
,
728 f
, PyTrace_CALL
, Py_None
)) {
729 /* Profile function raised an error */
730 goto exit_eval_frame
;
736 names
= co
->co_names
;
737 consts
= co
->co_consts
;
738 fastlocals
= f
->f_localsplus
;
739 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
740 first_instr
= (unsigned char*) PyString_AS_STRING(co
->co_code
);
741 /* An explanation is in order for the next line.
743 f->f_lasti now refers to the index of the last instruction
744 executed. You might think this was obvious from the name, but
745 this wasn't always true before 2.3! PyFrame_New now sets
746 f->f_lasti to -1 (i.e. the index *before* the first instruction)
747 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
750 When the PREDICT() macros are enabled, some opcode pairs follow in
751 direct succession without updating f->f_lasti. A successful
752 prediction effectively links the two codes together as if they
753 were a single new opcode; accordingly,f->f_lasti will point to
754 the first code in the pair (for instance, GET_ITER followed by
755 FOR_ITER is effectively a single opcode and f->f_lasti will point
756 at to the beginning of the combined pair.)
758 next_instr
= first_instr
+ f
->f_lasti
+ 1;
759 stack_pointer
= f
->f_stacktop
;
760 assert(stack_pointer
!= NULL
);
761 f
->f_stacktop
= NULL
; /* remains NULL unless yield suspends frame */
764 lltrace
= PyDict_GetItemString(f
->f_globals
, "__lltrace__") != NULL
;
766 #if defined(Py_DEBUG) || defined(LLTRACE)
767 filename
= PyString_AsString(co
->co_filename
);
772 x
= Py_None
; /* Not a reference, just anything non-NULL */
775 if (throwflag
) { /* support for generator.throw() */
783 /* Almost surely, the opcode executed a break
784 or a continue, preventing inst1 from being set
785 on the way out of the loop.
787 READ_TIMESTAMP(inst1
);
790 dump_tsc(opcode
, ticked
, inst0
, inst1
, loop0
, loop1
,
796 READ_TIMESTAMP(loop0
);
798 assert(stack_pointer
>= f
->f_valuestack
); /* else underflow */
799 assert(STACK_LEVEL() <= co
->co_stacksize
); /* else overflow */
801 /* Do periodic things. Doing this every time through
802 the loop would add too much overhead, so we do it
803 only every Nth instruction. We also do it if
804 ``things_to_do'' is set, i.e. when an asynchronous
805 event needs attention (e.g. a signal handler or
806 async I/O handler); see Py_AddPendingCall() and
807 Py_MakePendingCalls() above. */
809 if (--_Py_Ticker
< 0) {
810 if (*next_instr
== SETUP_FINALLY
) {
811 /* Make the last opcode before
812 a try: finally: block uninterruptable. */
813 goto fast_next_opcode
;
815 _Py_Ticker
= _Py_CheckInterval
;
816 tstate
->tick_counter
++;
821 if (Py_MakePendingCalls() < 0) {
826 /* MakePendingCalls() didn't succeed.
827 Force early re-execution of this
828 "periodic" code, possibly after
833 if (interpreter_lock
) {
834 /* Give another thread a chance */
836 if (PyThreadState_Swap(NULL
) != tstate
)
837 Py_FatalError("ceval: tstate mix-up");
838 PyThread_release_lock(interpreter_lock
);
840 /* Other threads may run now */
842 PyThread_acquire_lock(interpreter_lock
, 1);
843 if (PyThreadState_Swap(tstate
) != NULL
)
844 Py_FatalError("ceval: orphan tstate");
846 /* Check for thread interrupts */
848 if (tstate
->async_exc
!= NULL
) {
849 x
= tstate
->async_exc
;
850 tstate
->async_exc
= NULL
;
861 f
->f_lasti
= INSTR_OFFSET();
863 /* line-by-line tracing support */
865 if (tstate
->c_tracefunc
!= NULL
&& !tstate
->tracing
) {
866 /* see maybe_call_line_trace
867 for expository comments */
868 f
->f_stacktop
= stack_pointer
;
870 err
= maybe_call_line_trace(tstate
->c_tracefunc
,
872 f
, &instr_lb
, &instr_ub
,
874 /* Reload possibly changed frame fields */
876 if (f
->f_stacktop
!= NULL
) {
877 stack_pointer
= f
->f_stacktop
;
878 f
->f_stacktop
= NULL
;
881 /* trace function raised an exception */
886 /* Extract opcode and argument */
889 oparg
= 0; /* allows oparg to be stored in a register because
890 it doesn't have to be remembered across a full loop */
894 #ifdef DYNAMIC_EXECUTION_PROFILE
896 dxpairs
[lastopcode
][opcode
]++;
903 /* Instruction tracing */
906 if (HAS_ARG(opcode
)) {
907 printf("%d: %d, %d\n",
908 f
->f_lasti
, opcode
, oparg
);
917 /* Main switch on opcode */
918 READ_TIMESTAMP(inst0
);
923 It is essential that any operation that fails sets either
924 x to NULL, err to nonzero, or why to anything but WHY_NOT,
925 and that no operation that succeeds does this! */
927 /* case STOP_CODE: this is an error! */
930 goto fast_next_opcode
;
937 goto fast_next_opcode
;
939 format_exc_check_arg(PyExc_UnboundLocalError
,
940 UNBOUNDLOCAL_ERROR_MSG
,
941 PyTuple_GetItem(co
->co_varnames
, oparg
));
945 x
= GETITEM(consts
, oparg
);
948 goto fast_next_opcode
;
950 PREDICTED_WITH_ARG(STORE_FAST
);
954 goto fast_next_opcode
;
960 goto fast_next_opcode
;
967 goto fast_next_opcode
;
976 goto fast_next_opcode
;
987 goto fast_next_opcode
;
993 goto fast_next_opcode
;
1004 goto fast_next_opcode
;
1005 } else if (oparg
== 3) {
1016 goto fast_next_opcode
;
1018 Py_FatalError("invalid argument to DUP_TOPX"
1019 " (bytecode corruption?)");
1022 case UNARY_POSITIVE
:
1024 x
= PyNumber_Positive(v
);
1027 if (x
!= NULL
) continue;
1030 case UNARY_NEGATIVE
:
1032 x
= PyNumber_Negative(v
);
1035 if (x
!= NULL
) continue;
1040 err
= PyObject_IsTrue(v
);
1048 Py_INCREF(Py_False
);
1058 x
= PyObject_Repr(v
);
1061 if (x
!= NULL
) continue;
1066 x
= PyNumber_Invert(v
);
1069 if (x
!= NULL
) continue;
1075 x
= PyNumber_Power(v
, w
, Py_None
);
1079 if (x
!= NULL
) continue;
1082 case BINARY_MULTIPLY
:
1085 x
= PyNumber_Multiply(v
, w
);
1089 if (x
!= NULL
) continue;
1093 if (!_Py_QnewFlag
) {
1096 x
= PyNumber_Divide(v
, w
);
1100 if (x
!= NULL
) continue;
1103 /* -Qnew is in effect: fall through to
1104 BINARY_TRUE_DIVIDE */
1105 case BINARY_TRUE_DIVIDE
:
1108 x
= PyNumber_TrueDivide(v
, w
);
1112 if (x
!= NULL
) continue;
1115 case BINARY_FLOOR_DIVIDE
:
1118 x
= PyNumber_FloorDivide(v
, w
);
1122 if (x
!= NULL
) continue;
1128 x
= PyNumber_Remainder(v
, w
);
1132 if (x
!= NULL
) continue;
1138 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1139 /* INLINE: int + int */
1140 register long a
, b
, i
;
1141 a
= PyInt_AS_LONG(v
);
1142 b
= PyInt_AS_LONG(w
);
1144 if ((i
^a
) < 0 && (i
^b
) < 0)
1146 x
= PyInt_FromLong(i
);
1148 else if (PyString_CheckExact(v
) &&
1149 PyString_CheckExact(w
)) {
1150 x
= string_concatenate(v
, w
, f
, next_instr
);
1151 /* string_concatenate consumed the ref to v */
1152 goto skip_decref_vx
;
1156 x
= PyNumber_Add(v
, w
);
1162 if (x
!= NULL
) continue;
1165 case BINARY_SUBTRACT
:
1168 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1169 /* INLINE: int - int */
1170 register long a
, b
, i
;
1171 a
= PyInt_AS_LONG(v
);
1172 b
= PyInt_AS_LONG(w
);
1174 if ((i
^a
) < 0 && (i
^~b
) < 0)
1176 x
= PyInt_FromLong(i
);
1180 x
= PyNumber_Subtract(v
, w
);
1185 if (x
!= NULL
) continue;
1191 if (PyList_CheckExact(v
) && PyInt_CheckExact(w
)) {
1192 /* INLINE: list[int] */
1193 Py_ssize_t i
= PyInt_AsSsize_t(w
);
1195 i
+= PyList_GET_SIZE(v
);
1196 if (i
>= 0 && i
< PyList_GET_SIZE(v
)) {
1197 x
= PyList_GET_ITEM(v
, i
);
1205 x
= PyObject_GetItem(v
, w
);
1209 if (x
!= NULL
) continue;
1215 x
= PyNumber_Lshift(v
, w
);
1219 if (x
!= NULL
) continue;
1225 x
= PyNumber_Rshift(v
, w
);
1229 if (x
!= NULL
) continue;
1235 x
= PyNumber_And(v
, w
);
1239 if (x
!= NULL
) continue;
1245 x
= PyNumber_Xor(v
, w
);
1249 if (x
!= NULL
) continue;
1255 x
= PyNumber_Or(v
, w
);
1259 if (x
!= NULL
) continue;
1265 err
= PyList_Append(v
, w
);
1269 PREDICT(JUMP_ABSOLUTE
);
1277 x
= PyNumber_InPlacePower(v
, w
, Py_None
);
1281 if (x
!= NULL
) continue;
1284 case INPLACE_MULTIPLY
:
1287 x
= PyNumber_InPlaceMultiply(v
, w
);
1291 if (x
!= NULL
) continue;
1294 case INPLACE_DIVIDE
:
1295 if (!_Py_QnewFlag
) {
1298 x
= PyNumber_InPlaceDivide(v
, w
);
1302 if (x
!= NULL
) continue;
1305 /* -Qnew is in effect: fall through to
1306 INPLACE_TRUE_DIVIDE */
1307 case INPLACE_TRUE_DIVIDE
:
1310 x
= PyNumber_InPlaceTrueDivide(v
, w
);
1314 if (x
!= NULL
) continue;
1317 case INPLACE_FLOOR_DIVIDE
:
1320 x
= PyNumber_InPlaceFloorDivide(v
, w
);
1324 if (x
!= NULL
) continue;
1327 case INPLACE_MODULO
:
1330 x
= PyNumber_InPlaceRemainder(v
, w
);
1334 if (x
!= NULL
) continue;
1340 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1341 /* INLINE: int + int */
1342 register long a
, b
, i
;
1343 a
= PyInt_AS_LONG(v
);
1344 b
= PyInt_AS_LONG(w
);
1346 if ((i
^a
) < 0 && (i
^b
) < 0)
1348 x
= PyInt_FromLong(i
);
1350 else if (PyString_CheckExact(v
) &&
1351 PyString_CheckExact(w
)) {
1352 x
= string_concatenate(v
, w
, f
, next_instr
);
1353 /* string_concatenate consumed the ref to v */
1358 x
= PyNumber_InPlaceAdd(v
, w
);
1364 if (x
!= NULL
) continue;
1367 case INPLACE_SUBTRACT
:
1370 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1371 /* INLINE: int - int */
1372 register long a
, b
, i
;
1373 a
= PyInt_AS_LONG(v
);
1374 b
= PyInt_AS_LONG(w
);
1376 if ((i
^a
) < 0 && (i
^~b
) < 0)
1378 x
= PyInt_FromLong(i
);
1382 x
= PyNumber_InPlaceSubtract(v
, w
);
1387 if (x
!= NULL
) continue;
1390 case INPLACE_LSHIFT
:
1393 x
= PyNumber_InPlaceLshift(v
, w
);
1397 if (x
!= NULL
) continue;
1400 case INPLACE_RSHIFT
:
1403 x
= PyNumber_InPlaceRshift(v
, w
);
1407 if (x
!= NULL
) continue;
1413 x
= PyNumber_InPlaceAnd(v
, w
);
1417 if (x
!= NULL
) continue;
1423 x
= PyNumber_InPlaceXor(v
, w
);
1427 if (x
!= NULL
) continue;
1433 x
= PyNumber_InPlaceOr(v
, w
);
1437 if (x
!= NULL
) continue;
1444 if ((opcode
-SLICE
) & 2)
1448 if ((opcode
-SLICE
) & 1)
1453 x
= apply_slice(u
, v
, w
);
1458 if (x
!= NULL
) continue;
1465 if ((opcode
-STORE_SLICE
) & 2)
1469 if ((opcode
-STORE_SLICE
) & 1)
1475 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
1480 if (err
== 0) continue;
1483 case DELETE_SLICE
+0:
1484 case DELETE_SLICE
+1:
1485 case DELETE_SLICE
+2:
1486 case DELETE_SLICE
+3:
1487 if ((opcode
-DELETE_SLICE
) & 2)
1491 if ((opcode
-DELETE_SLICE
) & 1)
1496 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
1501 if (err
== 0) continue;
1510 err
= PyObject_SetItem(v
, w
, u
);
1514 if (err
== 0) continue;
1522 err
= PyObject_DelItem(v
, w
);
1525 if (err
== 0) continue;
1530 w
= PySys_GetObject("displayhook");
1532 PyErr_SetString(PyExc_RuntimeError
,
1533 "lost sys.displayhook");
1538 x
= PyTuple_Pack(1, v
);
1543 w
= PyEval_CallObject(w
, x
);
1554 /* fall through to PRINT_ITEM */
1558 if (stream
== NULL
|| stream
== Py_None
) {
1559 w
= PySys_GetObject("stdout");
1561 PyErr_SetString(PyExc_RuntimeError
,
1566 /* PyFile_SoftSpace() can exececute arbitrary code
1567 if sys.stdout is an instance with a __getattr__.
1568 If __getattr__ raises an exception, w will
1569 be freed, so we need to prevent that temporarily. */
1571 if (w
!= NULL
&& PyFile_SoftSpace(w
, 0))
1572 err
= PyFile_WriteString(" ", w
);
1574 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1576 /* XXX move into writeobject() ? */
1577 if (PyString_Check(v
)) {
1578 char *s
= PyString_AS_STRING(v
);
1579 Py_ssize_t len
= PyString_GET_SIZE(v
);
1581 !isspace(Py_CHARMASK(s
[len
-1])) ||
1583 PyFile_SoftSpace(w
, 1);
1585 #ifdef Py_USING_UNICODE
1586 else if (PyUnicode_Check(v
)) {
1587 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(v
);
1588 Py_ssize_t len
= PyUnicode_GET_SIZE(v
);
1590 !Py_UNICODE_ISSPACE(s
[len
-1]) ||
1592 PyFile_SoftSpace(w
, 1);
1596 PyFile_SoftSpace(w
, 1);
1606 case PRINT_NEWLINE_TO
:
1608 /* fall through to PRINT_NEWLINE */
1611 if (stream
== NULL
|| stream
== Py_None
) {
1612 w
= PySys_GetObject("stdout");
1614 PyErr_SetString(PyExc_RuntimeError
,
1618 err
= PyFile_WriteString("\n", w
);
1620 PyFile_SoftSpace(w
, 0);
1628 default: switch (opcode
) {
1634 u
= POP(); /* traceback */
1637 v
= POP(); /* value */
1640 w
= POP(); /* exc */
1641 case 0: /* Fallthrough */
1642 why
= do_raise(w
, v
, u
);
1645 PyErr_SetString(PyExc_SystemError
,
1646 "bad RAISE_VARARGS oparg");
1647 why
= WHY_EXCEPTION
;
1653 if ((x
= f
->f_locals
) != NULL
) {
1658 PyErr_SetString(PyExc_SystemError
, "no locals");
1664 goto fast_block_end
;
1668 f
->f_stacktop
= stack_pointer
;
1677 READ_TIMESTAMP(intr0
);
1678 err
= exec_statement(f
, u
, v
, w
);
1679 READ_TIMESTAMP(intr1
);
1687 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1688 while (STACK_LEVEL() > b
->b_level
) {
1697 if (PyInt_Check(v
)) {
1698 why
= (enum why_code
) PyInt_AS_LONG(v
);
1699 assert(why
!= WHY_YIELD
);
1700 if (why
== WHY_RETURN
||
1701 why
== WHY_CONTINUE
)
1704 else if (PyExceptionClass_Check(v
) ||
1705 PyString_Check(v
)) {
1708 PyErr_Restore(v
, w
, u
);
1712 else if (v
!= Py_None
) {
1713 PyErr_SetString(PyExc_SystemError
,
1714 "'finally' pops bad exception");
1715 why
= WHY_EXCEPTION
;
1725 x
= build_class(u
, v
, w
);
1733 w
= GETITEM(names
, oparg
);
1735 if ((x
= f
->f_locals
) != NULL
) {
1736 if (PyDict_CheckExact(x
))
1737 err
= PyDict_SetItem(x
, w
, v
);
1739 err
= PyObject_SetItem(x
, w
, v
);
1741 if (err
== 0) continue;
1744 PyErr_Format(PyExc_SystemError
,
1745 "no locals found when storing %s",
1750 w
= GETITEM(names
, oparg
);
1751 if ((x
= f
->f_locals
) != NULL
) {
1752 if ((err
= PyObject_DelItem(x
, w
)) != 0)
1753 format_exc_check_arg(PyExc_NameError
,
1758 PyErr_Format(PyExc_SystemError
,
1759 "no locals when deleting %s",
1763 PREDICTED_WITH_ARG(UNPACK_SEQUENCE
);
1764 case UNPACK_SEQUENCE
:
1766 if (PyTuple_CheckExact(v
) &&
1767 PyTuple_GET_SIZE(v
) == oparg
) {
1768 PyObject
**items
= \
1769 ((PyTupleObject
*)v
)->ob_item
;
1777 } else if (PyList_CheckExact(v
) &&
1778 PyList_GET_SIZE(v
) == oparg
) {
1779 PyObject
**items
= \
1780 ((PyListObject
*)v
)->ob_item
;
1786 } else if (unpack_iterable(v
, oparg
,
1787 stack_pointer
+ oparg
)) {
1788 stack_pointer
+= oparg
;
1790 /* unpack_iterable() raised an exception */
1791 why
= WHY_EXCEPTION
;
1797 w
= GETITEM(names
, oparg
);
1801 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1804 if (err
== 0) continue;
1808 w
= GETITEM(names
, oparg
);
1810 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1816 w
= GETITEM(names
, oparg
);
1818 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1820 if (err
== 0) continue;
1824 w
= GETITEM(names
, oparg
);
1825 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1826 format_exc_check_arg(
1827 PyExc_NameError
, GLOBAL_NAME_ERROR_MSG
, w
);
1831 w
= GETITEM(names
, oparg
);
1832 if ((v
= f
->f_locals
) == NULL
) {
1833 PyErr_Format(PyExc_SystemError
,
1834 "no locals when loading %s",
1838 if (PyDict_CheckExact(v
)) {
1839 x
= PyDict_GetItem(v
, w
);
1843 x
= PyObject_GetItem(v
, w
);
1844 if (x
== NULL
&& PyErr_Occurred()) {
1845 if (!PyErr_ExceptionMatches(
1852 x
= PyDict_GetItem(f
->f_globals
, w
);
1854 x
= PyDict_GetItem(f
->f_builtins
, w
);
1856 format_exc_check_arg(
1868 w
= GETITEM(names
, oparg
);
1869 if (PyString_CheckExact(w
)) {
1870 /* Inline the PyDict_GetItem() calls.
1871 WARNING: this is an extreme speed hack.
1872 Do not try this at home. */
1873 long hash
= ((PyStringObject
*)w
)->ob_shash
;
1877 d
= (PyDictObject
*)(f
->f_globals
);
1878 e
= d
->ma_lookup(d
, w
, hash
);
1889 d
= (PyDictObject
*)(f
->f_builtins
);
1890 e
= d
->ma_lookup(d
, w
, hash
);
1901 goto load_global_error
;
1904 /* This is the un-inlined version of the code above */
1905 x
= PyDict_GetItem(f
->f_globals
, w
);
1907 x
= PyDict_GetItem(f
->f_builtins
, w
);
1910 format_exc_check_arg(
1912 GLOBAL_NAME_ERROR_MSG
, w
);
1921 x
= GETLOCAL(oparg
);
1923 SETLOCAL(oparg
, NULL
);
1926 format_exc_check_arg(
1927 PyExc_UnboundLocalError
,
1928 UNBOUNDLOCAL_ERROR_MSG
,
1929 PyTuple_GetItem(co
->co_varnames
, oparg
)
1934 x
= freevars
[oparg
];
1937 if (x
!= NULL
) continue;
1941 x
= freevars
[oparg
];
1948 /* Don't stomp existing exception */
1949 if (PyErr_Occurred())
1951 if (oparg
< PyTuple_GET_SIZE(co
->co_cellvars
)) {
1952 v
= PyTuple_GET_ITEM(co
->co_cellvars
,
1954 format_exc_check_arg(
1955 PyExc_UnboundLocalError
,
1956 UNBOUNDLOCAL_ERROR_MSG
,
1959 v
= PyTuple_GET_ITEM(co
->co_freevars
, oparg
-
1960 PyTuple_GET_SIZE(co
->co_cellvars
));
1961 format_exc_check_arg(PyExc_NameError
,
1962 UNBOUNDFREE_ERROR_MSG
, v
);
1968 x
= freevars
[oparg
];
1974 x
= PyTuple_New(oparg
);
1976 for (; --oparg
>= 0;) {
1978 PyTuple_SET_ITEM(x
, oparg
, w
);
1986 x
= PyList_New(oparg
);
1988 for (; --oparg
>= 0;) {
1990 PyList_SET_ITEM(x
, oparg
, w
);
2000 if (x
!= NULL
) continue;
2004 w
= GETITEM(names
, oparg
);
2006 x
= PyObject_GetAttr(v
, w
);
2009 if (x
!= NULL
) continue;
2015 if (PyInt_CheckExact(w
) && PyInt_CheckExact(v
)) {
2016 /* INLINE: cmp(int, int) */
2019 a
= PyInt_AS_LONG(v
);
2020 b
= PyInt_AS_LONG(w
);
2022 case PyCmp_LT
: res
= a
< b
; break;
2023 case PyCmp_LE
: res
= a
<= b
; break;
2024 case PyCmp_EQ
: res
= a
== b
; break;
2025 case PyCmp_NE
: res
= a
!= b
; break;
2026 case PyCmp_GT
: res
= a
> b
; break;
2027 case PyCmp_GE
: res
= a
>= b
; break;
2028 case PyCmp_IS
: res
= v
== w
; break;
2029 case PyCmp_IS_NOT
: res
= v
!= w
; break;
2030 default: goto slow_compare
;
2032 x
= res
? Py_True
: Py_False
;
2037 x
= cmp_outcome(oparg
, v
, w
);
2042 if (x
== NULL
) break;
2043 PREDICT(JUMP_IF_FALSE
);
2044 PREDICT(JUMP_IF_TRUE
);
2048 w
= GETITEM(names
, oparg
);
2049 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
2051 PyErr_SetString(PyExc_ImportError
,
2052 "__import__ not found");
2057 if (PyInt_AsLong(u
) != -1 || PyErr_Occurred())
2061 f
->f_locals
== NULL
?
2062 Py_None
: f
->f_locals
,
2069 f
->f_locals
== NULL
?
2070 Py_None
: f
->f_locals
,
2079 READ_TIMESTAMP(intr0
);
2080 x
= PyEval_CallObject(x
, w
);
2081 READ_TIMESTAMP(intr1
);
2084 if (x
!= NULL
) continue;
2089 PyFrame_FastToLocals(f
);
2090 if ((x
= f
->f_locals
) == NULL
) {
2091 PyErr_SetString(PyExc_SystemError
,
2092 "no locals found during 'import *'");
2095 READ_TIMESTAMP(intr0
);
2096 err
= import_all_from(x
, v
);
2097 READ_TIMESTAMP(intr1
);
2098 PyFrame_LocalsToFast(f
, 0);
2100 if (err
== 0) continue;
2104 w
= GETITEM(names
, oparg
);
2106 READ_TIMESTAMP(intr0
);
2107 x
= import_from(v
, w
);
2108 READ_TIMESTAMP(intr1
);
2110 if (x
!= NULL
) continue;
2115 goto fast_next_opcode
;
2117 PREDICTED_WITH_ARG(JUMP_IF_FALSE
);
2122 goto fast_next_opcode
;
2124 if (w
== Py_False
) {
2126 goto fast_next_opcode
;
2128 err
= PyObject_IsTrue(w
);
2137 PREDICTED_WITH_ARG(JUMP_IF_TRUE
);
2140 if (w
== Py_False
) {
2142 goto fast_next_opcode
;
2146 goto fast_next_opcode
;
2148 err
= PyObject_IsTrue(w
);
2159 PREDICTED_WITH_ARG(JUMP_ABSOLUTE
);
2165 /* before: [obj]; after [getiter(obj)] */
2167 x
= PyObject_GetIter(v
);
2177 PREDICTED_WITH_ARG(FOR_ITER
);
2179 /* before: [iter]; after: [iter, iter()] *or* [] */
2181 x
= (*v
->ob_type
->tp_iternext
)(v
);
2184 PREDICT(STORE_FAST
);
2185 PREDICT(UNPACK_SEQUENCE
);
2188 if (PyErr_Occurred()) {
2189 if (!PyErr_ExceptionMatches(
2190 PyExc_StopIteration
))
2194 /* iterator ended normally */
2202 goto fast_block_end
;
2205 retval
= PyInt_FromLong(oparg
);
2211 goto fast_block_end
;
2216 /* NOTE: If you add any new block-setup opcodes that
2217 are not try/except/finally handlers, you may need
2218 to update the PyGen_NeedsFinalizing() function.
2221 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
2227 /* TOP is the context.__exit__ bound method.
2228 Below that are 1-3 values indicating how/why
2229 we entered the finally clause:
2231 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
2232 - SECOND = WHY_*; no retval below it
2233 - (SECOND, THIRD, FOURTH) = exc_info()
2234 In the last case, we must call
2235 TOP(SECOND, THIRD, FOURTH)
2236 otherwise we must call
2237 TOP(None, None, None)
2239 In addition, if the stack represents an exception,
2240 *and* the function call returns a 'true' value, we
2241 "zap" this information, to prevent END_FINALLY from
2242 re-raising the exception. (But non-local gotos
2243 should still be resumed.)
2248 if (PyInt_Check(u
) || u
== Py_None
) {
2249 u
= v
= w
= Py_None
;
2255 /* XXX Not the fastest way to call it... */
2256 x
= PyObject_CallFunctionObjArgs(x
, u
, v
, w
, NULL
);
2258 break; /* Go to error exit */
2259 if (u
!= Py_None
&& PyObject_IsTrue(x
)) {
2260 /* There was an exception and a true return */
2262 x
= TOP(); /* Again */
2271 /* Let END_FINALLY do its thing */
2285 x
= call_function(&sp
, oparg
, &intr0
, &intr1
);
2287 x
= call_function(&sp
, oparg
);
2296 case CALL_FUNCTION_VAR
:
2297 case CALL_FUNCTION_KW
:
2298 case CALL_FUNCTION_VAR_KW
:
2300 int na
= oparg
& 0xff;
2301 int nk
= (oparg
>>8) & 0xff;
2302 int flags
= (opcode
- CALL_FUNCTION
) & 3;
2303 int n
= na
+ 2 * nk
;
2304 PyObject
**pfunc
, *func
, **sp
;
2306 if (flags
& CALL_FLAG_VAR
)
2308 if (flags
& CALL_FLAG_KW
)
2310 pfunc
= stack_pointer
- n
- 1;
2313 if (PyMethod_Check(func
)
2314 && PyMethod_GET_SELF(func
) != NULL
) {
2315 PyObject
*self
= PyMethod_GET_SELF(func
);
2317 func
= PyMethod_GET_FUNCTION(func
);
2326 READ_TIMESTAMP(intr0
);
2327 x
= ext_do_call(func
, &sp
, flags
, na
, nk
);
2328 READ_TIMESTAMP(intr1
);
2332 while (stack_pointer
> pfunc
) {
2343 v
= POP(); /* code object */
2344 x
= PyFunction_New(v
, f
->f_globals
);
2346 /* XXX Maybe this should be a separate opcode? */
2347 if (x
!= NULL
&& oparg
> 0) {
2348 v
= PyTuple_New(oparg
);
2354 while (--oparg
>= 0) {
2356 PyTuple_SET_ITEM(v
, oparg
, w
);
2358 err
= PyFunction_SetDefaults(x
, v
);
2366 v
= POP(); /* code object */
2367 x
= PyFunction_New(v
, f
->f_globals
);
2371 err
= PyFunction_SetClosure(x
, v
);
2374 if (x
!= NULL
&& oparg
> 0) {
2375 v
= PyTuple_New(oparg
);
2381 while (--oparg
>= 0) {
2383 PyTuple_SET_ITEM(v
, oparg
, w
);
2385 err
= PyFunction_SetDefaults(x
, v
);
2399 x
= PySlice_New(u
, v
, w
);
2404 if (x
!= NULL
) continue;
2409 oparg
= oparg
<<16 | NEXTARG();
2410 goto dispatch_opcode
;
2414 "XXX lineno: %d, opcode: %d\n",
2415 PyCode_Addr2Line(f
->f_code
, f
->f_lasti
),
2417 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
2418 why
= WHY_EXCEPTION
;
2429 READ_TIMESTAMP(inst1
);
2431 /* Quickly continue if no error occurred */
2433 if (why
== WHY_NOT
) {
2434 if (err
== 0 && x
!= NULL
) {
2436 /* This check is expensive! */
2437 if (PyErr_Occurred())
2439 "XXX undetected error\n");
2442 READ_TIMESTAMP(loop1
);
2443 continue; /* Normal, fast path */
2448 why
= WHY_EXCEPTION
;
2453 /* Double-check exception status */
2455 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
2456 if (!PyErr_Occurred()) {
2457 PyErr_SetString(PyExc_SystemError
,
2458 "error return without exception set");
2459 why
= WHY_EXCEPTION
;
2464 /* This check is expensive! */
2465 if (PyErr_Occurred()) {
2467 sprintf(buf
, "Stack unwind with exception "
2468 "set and why=%d", why
);
2474 /* Log traceback info if this is a real exception */
2476 if (why
== WHY_EXCEPTION
) {
2477 PyTraceBack_Here(f
);
2479 if (tstate
->c_tracefunc
!= NULL
)
2480 call_exc_trace(tstate
->c_tracefunc
,
2481 tstate
->c_traceobj
, f
);
2484 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2486 if (why
== WHY_RERAISE
)
2487 why
= WHY_EXCEPTION
;
2489 /* Unwind stacks if a (pseudo) exception occurred */
2492 while (why
!= WHY_NOT
&& f
->f_iblock
> 0) {
2493 PyTryBlock
*b
= PyFrame_BlockPop(f
);
2495 assert(why
!= WHY_YIELD
);
2496 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_CONTINUE
) {
2497 /* For a continue inside a try block,
2498 don't pop the block for the loop. */
2499 PyFrame_BlockSetup(f
, b
->b_type
, b
->b_handler
,
2502 JUMPTO(PyInt_AS_LONG(retval
));
2507 while (STACK_LEVEL() > b
->b_level
) {
2511 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
2513 JUMPTO(b
->b_handler
);
2516 if (b
->b_type
== SETUP_FINALLY
||
2517 (b
->b_type
== SETUP_EXCEPT
&&
2518 why
== WHY_EXCEPTION
)) {
2519 if (why
== WHY_EXCEPTION
) {
2520 PyObject
*exc
, *val
, *tb
;
2521 PyErr_Fetch(&exc
, &val
, &tb
);
2526 /* Make the raw exception data
2527 available to the handler,
2528 so a program can emulate the
2529 Python main loop. Don't do
2530 this for 'finally'. */
2531 if (b
->b_type
== SETUP_EXCEPT
) {
2532 PyErr_NormalizeException(
2534 set_exc_info(tstate
,
2546 if (why
& (WHY_RETURN
| WHY_CONTINUE
))
2548 v
= PyInt_FromLong((long)why
);
2552 JUMPTO(b
->b_handler
);
2555 } /* unwind stack */
2557 /* End the loop if we still have an error (or return) */
2561 READ_TIMESTAMP(loop1
);
2565 assert(why
!= WHY_YIELD
);
2566 /* Pop remaining stack entries. */
2572 if (why
!= WHY_RETURN
)
2576 if (tstate
->use_tracing
) {
2577 if (tstate
->c_tracefunc
) {
2578 if (why
== WHY_RETURN
|| why
== WHY_YIELD
) {
2579 if (call_trace(tstate
->c_tracefunc
,
2580 tstate
->c_traceobj
, f
,
2581 PyTrace_RETURN
, retval
)) {
2584 why
= WHY_EXCEPTION
;
2587 else if (why
== WHY_EXCEPTION
) {
2588 call_trace_protected(tstate
->c_tracefunc
,
2589 tstate
->c_traceobj
, f
,
2590 PyTrace_RETURN
, NULL
);
2593 if (tstate
->c_profilefunc
) {
2594 if (why
== WHY_EXCEPTION
)
2595 call_trace_protected(tstate
->c_profilefunc
,
2596 tstate
->c_profileobj
, f
,
2597 PyTrace_RETURN
, NULL
);
2598 else if (call_trace(tstate
->c_profilefunc
,
2599 tstate
->c_profileobj
, f
,
2600 PyTrace_RETURN
, retval
)) {
2603 why
= WHY_EXCEPTION
;
2608 if (tstate
->frame
->f_exc_type
!= NULL
)
2609 reset_exc_info(tstate
);
2611 assert(tstate
->frame
->f_exc_value
== NULL
);
2612 assert(tstate
->frame
->f_exc_traceback
== NULL
);
2617 Py_LeaveRecursiveCall();
2618 tstate
->frame
= f
->f_back
;
2623 /* This is gonna seem *real weird*, but if you put some other code between
2624 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2625 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2628 PyEval_EvalCodeEx(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
,
2629 PyObject
**args
, int argcount
, PyObject
**kws
, int kwcount
,
2630 PyObject
**defs
, int defcount
, PyObject
*closure
)
2632 register PyFrameObject
*f
;
2633 register PyObject
*retval
= NULL
;
2634 register PyObject
**fastlocals
, **freevars
;
2635 PyThreadState
*tstate
= PyThreadState_GET();
2638 if (globals
== NULL
) {
2639 PyErr_SetString(PyExc_SystemError
,
2640 "PyEval_EvalCodeEx: NULL globals");
2644 assert(tstate
!= NULL
);
2645 assert(globals
!= NULL
);
2646 f
= PyFrame_New(tstate
, co
, globals
, locals
);
2650 fastlocals
= f
->f_localsplus
;
2651 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
2653 if (co
->co_argcount
> 0 ||
2654 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
2657 PyObject
*kwdict
= NULL
;
2658 if (co
->co_flags
& CO_VARKEYWORDS
) {
2659 kwdict
= PyDict_New();
2662 i
= co
->co_argcount
;
2663 if (co
->co_flags
& CO_VARARGS
)
2665 SETLOCAL(i
, kwdict
);
2667 if (argcount
> co
->co_argcount
) {
2668 if (!(co
->co_flags
& CO_VARARGS
)) {
2669 PyErr_Format(PyExc_TypeError
,
2670 "%.200s() takes %s %d "
2671 "%sargument%s (%d given)",
2672 PyString_AsString(co
->co_name
),
2673 defcount
? "at most" : "exactly",
2675 kwcount
? "non-keyword " : "",
2676 co
->co_argcount
== 1 ? "" : "s",
2680 n
= co
->co_argcount
;
2682 for (i
= 0; i
< n
; i
++) {
2687 if (co
->co_flags
& CO_VARARGS
) {
2688 u
= PyTuple_New(argcount
- n
);
2691 SETLOCAL(co
->co_argcount
, u
);
2692 for (i
= n
; i
< argcount
; i
++) {
2695 PyTuple_SET_ITEM(u
, i
-n
, x
);
2698 for (i
= 0; i
< kwcount
; i
++) {
2699 PyObject
*keyword
= kws
[2*i
];
2700 PyObject
*value
= kws
[2*i
+ 1];
2702 if (keyword
== NULL
|| !PyString_Check(keyword
)) {
2703 PyErr_Format(PyExc_TypeError
,
2704 "%.200s() keywords must be strings",
2705 PyString_AsString(co
->co_name
));
2708 /* XXX slow -- speed up using dictionary? */
2709 for (j
= 0; j
< co
->co_argcount
; j
++) {
2710 PyObject
*nm
= PyTuple_GET_ITEM(
2711 co
->co_varnames
, j
);
2712 int cmp
= PyObject_RichCompareBool(
2713 keyword
, nm
, Py_EQ
);
2719 /* Check errors from Compare */
2720 if (PyErr_Occurred())
2722 if (j
>= co
->co_argcount
) {
2723 if (kwdict
== NULL
) {
2724 PyErr_Format(PyExc_TypeError
,
2725 "%.200s() got an unexpected "
2726 "keyword argument '%.400s'",
2727 PyString_AsString(co
->co_name
),
2728 PyString_AsString(keyword
));
2731 PyDict_SetItem(kwdict
, keyword
, value
);
2734 if (GETLOCAL(j
) != NULL
) {
2735 PyErr_Format(PyExc_TypeError
,
2736 "%.200s() got multiple "
2737 "values for keyword "
2738 "argument '%.400s'",
2739 PyString_AsString(co
->co_name
),
2740 PyString_AsString(keyword
));
2747 if (argcount
< co
->co_argcount
) {
2748 int m
= co
->co_argcount
- defcount
;
2749 for (i
= argcount
; i
< m
; i
++) {
2750 if (GETLOCAL(i
) == NULL
) {
2751 PyErr_Format(PyExc_TypeError
,
2752 "%.200s() takes %s %d "
2753 "%sargument%s (%d given)",
2754 PyString_AsString(co
->co_name
),
2755 ((co
->co_flags
& CO_VARARGS
) ||
2756 defcount
) ? "at least"
2758 m
, kwcount
? "non-keyword " : "",
2759 m
== 1 ? "" : "s", i
);
2767 for (; i
< defcount
; i
++) {
2768 if (GETLOCAL(m
+i
) == NULL
) {
2769 PyObject
*def
= defs
[i
];
2777 if (argcount
> 0 || kwcount
> 0) {
2778 PyErr_Format(PyExc_TypeError
,
2779 "%.200s() takes no arguments (%d given)",
2780 PyString_AsString(co
->co_name
),
2781 argcount
+ kwcount
);
2785 /* Allocate and initialize storage for cell vars, and copy free
2786 vars into frame. This isn't too efficient right now. */
2787 if (PyTuple_GET_SIZE(co
->co_cellvars
)) {
2788 int i
, j
, nargs
, found
;
2789 char *cellname
, *argname
;
2792 nargs
= co
->co_argcount
;
2793 if (co
->co_flags
& CO_VARARGS
)
2795 if (co
->co_flags
& CO_VARKEYWORDS
)
2798 /* Initialize each cell var, taking into account
2799 cell vars that are initialized from arguments.
2801 Should arrange for the compiler to put cellvars
2802 that are arguments at the beginning of the cellvars
2803 list so that we can march over it more efficiently?
2805 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_cellvars
); ++i
) {
2806 cellname
= PyString_AS_STRING(
2807 PyTuple_GET_ITEM(co
->co_cellvars
, i
));
2809 for (j
= 0; j
< nargs
; j
++) {
2810 argname
= PyString_AS_STRING(
2811 PyTuple_GET_ITEM(co
->co_varnames
, j
));
2812 if (strcmp(cellname
, argname
) == 0) {
2813 c
= PyCell_New(GETLOCAL(j
));
2816 GETLOCAL(co
->co_nlocals
+ i
) = c
;
2822 c
= PyCell_New(NULL
);
2825 SETLOCAL(co
->co_nlocals
+ i
, c
);
2829 if (PyTuple_GET_SIZE(co
->co_freevars
)) {
2831 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_freevars
); ++i
) {
2832 PyObject
*o
= PyTuple_GET_ITEM(closure
, i
);
2834 freevars
[PyTuple_GET_SIZE(co
->co_cellvars
) + i
] = o
;
2838 if (co
->co_flags
& CO_GENERATOR
) {
2839 /* Don't need to keep the reference to f_back, it will be set
2840 * when the generator is resumed. */
2841 Py_XDECREF(f
->f_back
);
2844 PCALL(PCALL_GENERATOR
);
2846 /* Create a new generator that owns the ready to run frame
2847 * and return that as the value. */
2848 return PyGen_New(f
);
2851 retval
= PyEval_EvalFrameEx(f
,0);
2853 fail
: /* Jump here from prelude on failure */
2855 /* decref'ing the frame can cause __del__ methods to get invoked,
2856 which can call back into Python. While we're done with the
2857 current Python frame (f), the associated C stack is still in use,
2858 so recursion_depth must be boosted for the duration.
2860 assert(tstate
!= NULL
);
2861 ++tstate
->recursion_depth
;
2863 --tstate
->recursion_depth
;
2868 /* Implementation notes for set_exc_info() and reset_exc_info():
2870 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2871 'exc_traceback'. These always travel together.
2873 - tstate->curexc_ZZZ is the "hot" exception that is set by
2874 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2876 - Once an exception is caught by an except clause, it is transferred
2877 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2878 can pick it up. This is the primary task of set_exc_info().
2879 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
2881 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
2883 Long ago, when none of this existed, there were just a few globals:
2884 one set corresponding to the "hot" exception, and one set
2885 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2886 globals; they were simply stored as sys.exc_ZZZ. For backwards
2887 compatibility, they still are!) The problem was that in code like
2891 "something that may fail"
2892 except "some exception":
2893 "do something else first"
2894 "print the exception from sys.exc_ZZZ."
2896 if "do something else first" invoked something that raised and caught
2897 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2898 cause of subtle bugs. I fixed this by changing the semantics as
2901 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2904 - But initially, and as long as no exception is caught in a given
2905 frame, sys.exc_ZZZ will hold the last exception caught in the
2906 previous frame (or the frame before that, etc.).
2908 The first bullet fixed the bug in the above example. The second
2909 bullet was for backwards compatibility: it was (and is) common to
2910 have a function that is called when an exception is caught, and to
2911 have that function access the caught exception via sys.exc_ZZZ.
2912 (Example: traceback.print_exc()).
2914 At the same time I fixed the problem that sys.exc_ZZZ weren't
2915 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2916 but that's really a separate improvement.
2918 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2919 variables to what they were before the current frame was called. The
2920 set_exc_info() function saves them on the frame so that
2921 reset_exc_info() can restore them. The invariant is that
2922 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2923 exception (where "catching" an exception applies only to successful
2924 except clauses); and if the current frame ever caught an exception,
2925 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2926 at the start of the current frame.
2931 set_exc_info(PyThreadState
*tstate
,
2932 PyObject
*type
, PyObject
*value
, PyObject
*tb
)
2934 PyFrameObject
*frame
= tstate
->frame
;
2935 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
2937 assert(type
!= NULL
);
2938 assert(frame
!= NULL
);
2939 if (frame
->f_exc_type
== NULL
) {
2940 assert(frame
->f_exc_value
== NULL
);
2941 assert(frame
->f_exc_traceback
== NULL
);
2942 /* This frame didn't catch an exception before. */
2943 /* Save previous exception of this thread in this frame. */
2944 if (tstate
->exc_type
== NULL
) {
2945 /* XXX Why is this set to Py_None? */
2947 tstate
->exc_type
= Py_None
;
2949 Py_INCREF(tstate
->exc_type
);
2950 Py_XINCREF(tstate
->exc_value
);
2951 Py_XINCREF(tstate
->exc_traceback
);
2952 frame
->f_exc_type
= tstate
->exc_type
;
2953 frame
->f_exc_value
= tstate
->exc_value
;
2954 frame
->f_exc_traceback
= tstate
->exc_traceback
;
2956 /* Set new exception for this thread. */
2957 tmp_type
= tstate
->exc_type
;
2958 tmp_value
= tstate
->exc_value
;
2959 tmp_tb
= tstate
->exc_traceback
;
2963 tstate
->exc_type
= type
;
2964 tstate
->exc_value
= value
;
2965 tstate
->exc_traceback
= tb
;
2966 Py_XDECREF(tmp_type
);
2967 Py_XDECREF(tmp_value
);
2969 /* For b/w compatibility */
2970 PySys_SetObject("exc_type", type
);
2971 PySys_SetObject("exc_value", value
);
2972 PySys_SetObject("exc_traceback", tb
);
2976 reset_exc_info(PyThreadState
*tstate
)
2978 PyFrameObject
*frame
;
2979 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
2981 /* It's a precondition that the thread state's frame caught an
2982 * exception -- verify in a debug build.
2984 assert(tstate
!= NULL
);
2985 frame
= tstate
->frame
;
2986 assert(frame
!= NULL
);
2987 assert(frame
->f_exc_type
!= NULL
);
2989 /* Copy the frame's exception info back to the thread state. */
2990 tmp_type
= tstate
->exc_type
;
2991 tmp_value
= tstate
->exc_value
;
2992 tmp_tb
= tstate
->exc_traceback
;
2993 Py_INCREF(frame
->f_exc_type
);
2994 Py_XINCREF(frame
->f_exc_value
);
2995 Py_XINCREF(frame
->f_exc_traceback
);
2996 tstate
->exc_type
= frame
->f_exc_type
;
2997 tstate
->exc_value
= frame
->f_exc_value
;
2998 tstate
->exc_traceback
= frame
->f_exc_traceback
;
2999 Py_XDECREF(tmp_type
);
3000 Py_XDECREF(tmp_value
);
3003 /* For b/w compatibility */
3004 PySys_SetObject("exc_type", frame
->f_exc_type
);
3005 PySys_SetObject("exc_value", frame
->f_exc_value
);
3006 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
3008 /* Clear the frame's exception info. */
3009 tmp_type
= frame
->f_exc_type
;
3010 tmp_value
= frame
->f_exc_value
;
3011 tmp_tb
= frame
->f_exc_traceback
;
3012 frame
->f_exc_type
= NULL
;
3013 frame
->f_exc_value
= NULL
;
3014 frame
->f_exc_traceback
= NULL
;
3015 Py_DECREF(tmp_type
);
3016 Py_XDECREF(tmp_value
);
3020 /* Logic for the raise statement (too complicated for inlining).
3021 This *consumes* a reference count to each of its arguments. */
3022 static enum why_code
3023 do_raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3027 PyThreadState
*tstate
= PyThreadState_GET();
3028 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
3029 value
= tstate
->exc_value
;
3030 tb
= tstate
->exc_traceback
;
3036 /* We support the following forms of raise:
3037 raise <class>, <classinstance>
3038 raise <class>, <argument tuple>
3040 raise <class>, <argument>
3041 raise <classinstance>, None
3042 raise <string>, <object>
3043 raise <string>, None
3045 An omitted second argument is the same as None.
3047 In addition, raise <tuple>, <anything> is the same as
3048 raising the tuple's first item (and it better have one!);
3049 this rule is applied recursively.
3051 Finally, an optional third argument can be supplied, which
3052 gives the traceback to be substituted (useful when
3053 re-raising an exception after examining it). */
3055 /* First, check the traceback argument, replacing None with
3057 if (tb
== Py_None
) {
3061 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
3062 PyErr_SetString(PyExc_TypeError
,
3063 "raise: arg 3 must be a traceback or None");
3067 /* Next, replace a missing value with None */
3068 if (value
== NULL
) {
3073 /* Next, repeatedly, replace a tuple exception with its first item */
3074 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
3075 PyObject
*tmp
= type
;
3076 type
= PyTuple_GET_ITEM(type
, 0);
3081 if (PyExceptionClass_Check(type
))
3082 PyErr_NormalizeException(&type
, &value
, &tb
);
3084 else if (PyExceptionInstance_Check(type
)) {
3085 /* Raising an instance. The value should be a dummy. */
3086 if (value
!= Py_None
) {
3087 PyErr_SetString(PyExc_TypeError
,
3088 "instance exception may not have a separate value");
3092 /* Normalize to raise <class>, <instance> */
3095 type
= PyExceptionInstance_Class(type
);
3100 /* Not something you can raise. You get an exception
3101 anyway, just not what you specified :-) */
3102 PyErr_Format(PyExc_TypeError
,
3103 "exceptions must be classes or instances, not %s",
3104 type
->ob_type
->tp_name
);
3107 PyErr_Restore(type
, value
, tb
);
3109 return WHY_EXCEPTION
;
3116 return WHY_EXCEPTION
;
3119 /* Iterate v argcnt times and store the results on the stack (via decreasing
3120 sp). Return 1 for success, 0 if error. */
3123 unpack_iterable(PyObject
*v
, int argcnt
, PyObject
**sp
)
3126 PyObject
*it
; /* iter(v) */
3131 it
= PyObject_GetIter(v
);
3135 for (; i
< argcnt
; i
++) {
3136 w
= PyIter_Next(it
);
3138 /* Iterator done, via error or exhaustion. */
3139 if (!PyErr_Occurred()) {
3140 PyErr_Format(PyExc_ValueError
,
3141 "need more than %d value%s to unpack",
3142 i
, i
== 1 ? "" : "s");
3149 /* We better have exhausted the iterator now. */
3150 w
= PyIter_Next(it
);
3152 if (PyErr_Occurred())
3158 PyErr_SetString(PyExc_ValueError
, "too many values to unpack");
3161 for (; i
> 0; i
--, sp
++)
3170 prtrace(PyObject
*v
, char *str
)
3173 if (PyObject_Print(v
, stdout
, 0) != 0)
3174 PyErr_Clear(); /* Don't know what else to do */
3181 call_exc_trace(Py_tracefunc func
, PyObject
*self
, PyFrameObject
*f
)
3183 PyObject
*type
, *value
, *traceback
, *arg
;
3185 PyErr_Fetch(&type
, &value
, &traceback
);
3186 if (value
== NULL
) {
3190 arg
= PyTuple_Pack(3, type
, value
, traceback
);
3192 PyErr_Restore(type
, value
, traceback
);
3195 err
= call_trace(func
, self
, f
, PyTrace_EXCEPTION
, arg
);
3198 PyErr_Restore(type
, value
, traceback
);
3202 Py_XDECREF(traceback
);
3207 call_trace_protected(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3208 int what
, PyObject
*arg
)
3210 PyObject
*type
, *value
, *traceback
;
3212 PyErr_Fetch(&type
, &value
, &traceback
);
3213 err
= call_trace(func
, obj
, frame
, what
, arg
);
3215 PyErr_Restore(type
, value
, traceback
);
3219 Py_XDECREF(traceback
);
3224 call_trace(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3225 int what
, PyObject
*arg
)
3227 register PyThreadState
*tstate
= frame
->f_tstate
;
3229 if (tstate
->tracing
)
3232 tstate
->use_tracing
= 0;
3233 result
= func(obj
, frame
, what
, arg
);
3234 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3235 || (tstate
->c_profilefunc
!= NULL
));
3241 _PyEval_CallTracing(PyObject
*func
, PyObject
*args
)
3243 PyFrameObject
*frame
= PyEval_GetFrame();
3244 PyThreadState
*tstate
= frame
->f_tstate
;
3245 int save_tracing
= tstate
->tracing
;
3246 int save_use_tracing
= tstate
->use_tracing
;
3249 tstate
->tracing
= 0;
3250 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3251 || (tstate
->c_profilefunc
!= NULL
));
3252 result
= PyObject_Call(func
, args
, NULL
);
3253 tstate
->tracing
= save_tracing
;
3254 tstate
->use_tracing
= save_use_tracing
;
3259 maybe_call_line_trace(Py_tracefunc func
, PyObject
*obj
,
3260 PyFrameObject
*frame
, int *instr_lb
, int *instr_ub
,
3265 /* If the last instruction executed isn't in the current
3266 instruction window, reset the window. If the last
3267 instruction happens to fall at the start of a line or if it
3268 represents a jump backwards, call the trace function.
3270 if ((frame
->f_lasti
< *instr_lb
|| frame
->f_lasti
>= *instr_ub
)) {
3274 line
= PyCode_CheckLineNumber(frame
->f_code
, frame
->f_lasti
,
3277 frame
->f_lineno
= line
;
3278 result
= call_trace(func
, obj
, frame
,
3279 PyTrace_LINE
, Py_None
);
3281 *instr_lb
= bounds
.ap_lower
;
3282 *instr_ub
= bounds
.ap_upper
;
3284 else if (frame
->f_lasti
<= *instr_prev
) {
3285 result
= call_trace(func
, obj
, frame
, PyTrace_LINE
, Py_None
);
3287 *instr_prev
= frame
->f_lasti
;
3292 PyEval_SetProfile(Py_tracefunc func
, PyObject
*arg
)
3294 PyThreadState
*tstate
= PyThreadState_GET();
3295 PyObject
*temp
= tstate
->c_profileobj
;
3297 tstate
->c_profilefunc
= NULL
;
3298 tstate
->c_profileobj
= NULL
;
3299 /* Must make sure that tracing is not ignored if 'temp' is freed */
3300 tstate
->use_tracing
= tstate
->c_tracefunc
!= NULL
;
3302 tstate
->c_profilefunc
= func
;
3303 tstate
->c_profileobj
= arg
;
3304 /* Flag that tracing or profiling is turned on */
3305 tstate
->use_tracing
= (func
!= NULL
) || (tstate
->c_tracefunc
!= NULL
);
3309 PyEval_SetTrace(Py_tracefunc func
, PyObject
*arg
)
3311 PyThreadState
*tstate
= PyThreadState_GET();
3312 PyObject
*temp
= tstate
->c_traceobj
;
3314 tstate
->c_tracefunc
= NULL
;
3315 tstate
->c_traceobj
= NULL
;
3316 /* Must make sure that profiling is not ignored if 'temp' is freed */
3317 tstate
->use_tracing
= tstate
->c_profilefunc
!= NULL
;
3319 tstate
->c_tracefunc
= func
;
3320 tstate
->c_traceobj
= arg
;
3321 /* Flag that tracing or profiling is turned on */
3322 tstate
->use_tracing
= ((func
!= NULL
)
3323 || (tstate
->c_profilefunc
!= NULL
));
3327 PyEval_GetBuiltins(void)
3329 PyFrameObject
*current_frame
= PyEval_GetFrame();
3330 if (current_frame
== NULL
)
3331 return PyThreadState_GET()->interp
->builtins
;
3333 return current_frame
->f_builtins
;
3337 PyEval_GetLocals(void)
3339 PyFrameObject
*current_frame
= PyEval_GetFrame();
3340 if (current_frame
== NULL
)
3342 PyFrame_FastToLocals(current_frame
);
3343 return current_frame
->f_locals
;
3347 PyEval_GetGlobals(void)
3349 PyFrameObject
*current_frame
= PyEval_GetFrame();
3350 if (current_frame
== NULL
)
3353 return current_frame
->f_globals
;
3357 PyEval_GetFrame(void)
3359 PyThreadState
*tstate
= PyThreadState_GET();
3360 return _PyThreadState_GetFrame(tstate
);
3364 PyEval_GetRestricted(void)
3366 PyFrameObject
*current_frame
= PyEval_GetFrame();
3367 return current_frame
== NULL
? 0 : PyFrame_IsRestricted(current_frame
);
3371 PyEval_MergeCompilerFlags(PyCompilerFlags
*cf
)
3373 PyFrameObject
*current_frame
= PyEval_GetFrame();
3374 int result
= cf
->cf_flags
!= 0;
3376 if (current_frame
!= NULL
) {
3377 const int codeflags
= current_frame
->f_code
->co_flags
;
3378 const int compilerflags
= codeflags
& PyCF_MASK
;
3379 if (compilerflags
) {
3381 cf
->cf_flags
|= compilerflags
;
3383 #if 0 /* future keyword */
3384 if (codeflags
& CO_GENERATOR_ALLOWED
) {
3386 cf
->cf_flags
|= CO_GENERATOR_ALLOWED
;
3396 PyObject
*f
= PySys_GetObject("stdout");
3399 if (!PyFile_SoftSpace(f
, 0))
3401 return PyFile_WriteString("\n", f
);
3405 /* External interface to call any callable object.
3406 The arg must be a tuple or NULL. */
3408 #undef PyEval_CallObject
3409 /* for backward compatibility: export this interface */
3412 PyEval_CallObject(PyObject
*func
, PyObject
*arg
)
3414 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
3416 #define PyEval_CallObject(func,arg) \
3417 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3420 PyEval_CallObjectWithKeywords(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
3425 arg
= PyTuple_New(0);
3429 else if (!PyTuple_Check(arg
)) {
3430 PyErr_SetString(PyExc_TypeError
,
3431 "argument list must be a tuple");
3437 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
3438 PyErr_SetString(PyExc_TypeError
,
3439 "keyword list must be a dictionary");
3444 result
= PyObject_Call(func
, arg
, kw
);
3450 PyEval_GetFuncName(PyObject
*func
)
3452 if (PyMethod_Check(func
))
3453 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func
));
3454 else if (PyFunction_Check(func
))
3455 return PyString_AsString(((PyFunctionObject
*)func
)->func_name
);
3456 else if (PyCFunction_Check(func
))
3457 return ((PyCFunctionObject
*)func
)->m_ml
->ml_name
;
3458 else if (PyClass_Check(func
))
3459 return PyString_AsString(((PyClassObject
*)func
)->cl_name
);
3460 else if (PyInstance_Check(func
)) {
3461 return PyString_AsString(
3462 ((PyInstanceObject
*)func
)->in_class
->cl_name
);
3464 return func
->ob_type
->tp_name
;
3469 PyEval_GetFuncDesc(PyObject
*func
)
3471 if (PyMethod_Check(func
))
3473 else if (PyFunction_Check(func
))
3475 else if (PyCFunction_Check(func
))
3477 else if (PyClass_Check(func
))
3478 return " constructor";
3479 else if (PyInstance_Check(func
)) {
3487 err_args(PyObject
*func
, int flags
, int nargs
)
3489 if (flags
& METH_NOARGS
)
3490 PyErr_Format(PyExc_TypeError
,
3491 "%.200s() takes no arguments (%d given)",
3492 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3495 PyErr_Format(PyExc_TypeError
,
3496 "%.200s() takes exactly one argument (%d given)",
3497 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3501 #define C_TRACE(x, call) \
3502 if (tstate->use_tracing && tstate->c_profilefunc) { \
3503 if (call_trace(tstate->c_profilefunc, \
3504 tstate->c_profileobj, \
3505 tstate->frame, PyTrace_C_CALL, \
3511 if (tstate->c_profilefunc != NULL) { \
3513 call_trace_protected(tstate->c_profilefunc, \
3514 tstate->c_profileobj, \
3515 tstate->frame, PyTrace_C_EXCEPTION, \
3517 /* XXX should pass (type, value, tb) */ \
3519 if (call_trace(tstate->c_profilefunc, \
3520 tstate->c_profileobj, \
3521 tstate->frame, PyTrace_C_RETURN, \
3534 call_function(PyObject
***pp_stack
, int oparg
3536 , uint64
* pintr0
, uint64
* pintr1
3540 int na
= oparg
& 0xff;
3541 int nk
= (oparg
>>8) & 0xff;
3542 int n
= na
+ 2 * nk
;
3543 PyObject
**pfunc
= (*pp_stack
) - n
- 1;
3544 PyObject
*func
= *pfunc
;
3547 /* Always dispatch PyCFunction first, because these are
3548 presumed to be the most frequent callable object.
3550 if (PyCFunction_Check(func
) && nk
== 0) {
3551 int flags
= PyCFunction_GET_FLAGS(func
);
3552 PyThreadState
*tstate
= PyThreadState_GET();
3554 PCALL(PCALL_CFUNCTION
);
3555 if (flags
& (METH_NOARGS
| METH_O
)) {
3556 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
3557 PyObject
*self
= PyCFunction_GET_SELF(func
);
3558 if (flags
& METH_NOARGS
&& na
== 0) {
3559 C_TRACE(x
, (*meth
)(self
,NULL
));
3561 else if (flags
& METH_O
&& na
== 1) {
3562 PyObject
*arg
= EXT_POP(*pp_stack
);
3563 C_TRACE(x
, (*meth
)(self
,arg
));
3567 err_args(func
, flags
, na
);
3573 callargs
= load_args(pp_stack
, na
);
3574 READ_TIMESTAMP(*pintr0
);
3575 C_TRACE(x
, PyCFunction_Call(func
,callargs
,NULL
));
3576 READ_TIMESTAMP(*pintr1
);
3577 Py_XDECREF(callargs
);
3580 if (PyMethod_Check(func
) && PyMethod_GET_SELF(func
) != NULL
) {
3581 /* optimize access to bound methods */
3582 PyObject
*self
= PyMethod_GET_SELF(func
);
3583 PCALL(PCALL_METHOD
);
3584 PCALL(PCALL_BOUND_METHOD
);
3586 func
= PyMethod_GET_FUNCTION(func
);
3594 READ_TIMESTAMP(*pintr0
);
3595 if (PyFunction_Check(func
))
3596 x
= fast_function(func
, pp_stack
, n
, na
, nk
);
3598 x
= do_call(func
, pp_stack
, na
, nk
);
3599 READ_TIMESTAMP(*pintr1
);
3603 /* Clear the stack of the function object. Also removes
3604 the arguments in case they weren't consumed already
3605 (fast_function() and err_args() leave them on the stack).
3607 while ((*pp_stack
) > pfunc
) {
3608 w
= EXT_POP(*pp_stack
);
3615 /* The fast_function() function optimize calls for which no argument
3616 tuple is necessary; the objects are passed directly from the stack.
3617 For the simplest case -- a function that takes only positional
3618 arguments and is called with only positional arguments -- it
3619 inlines the most primitive frame setup code from
3620 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3621 done before evaluating the frame.
3625 fast_function(PyObject
*func
, PyObject
***pp_stack
, int n
, int na
, int nk
)
3627 PyCodeObject
*co
= (PyCodeObject
*)PyFunction_GET_CODE(func
);
3628 PyObject
*globals
= PyFunction_GET_GLOBALS(func
);
3629 PyObject
*argdefs
= PyFunction_GET_DEFAULTS(func
);
3630 PyObject
**d
= NULL
;
3633 PCALL(PCALL_FUNCTION
);
3634 PCALL(PCALL_FAST_FUNCTION
);
3635 if (argdefs
== NULL
&& co
->co_argcount
== n
&& nk
==0 &&
3636 co
->co_flags
== (CO_OPTIMIZED
| CO_NEWLOCALS
| CO_NOFREE
)) {
3638 PyObject
*retval
= NULL
;
3639 PyThreadState
*tstate
= PyThreadState_GET();
3640 PyObject
**fastlocals
, **stack
;
3643 PCALL(PCALL_FASTER_FUNCTION
);
3644 assert(globals
!= NULL
);
3645 /* XXX Perhaps we should create a specialized
3646 PyFrame_New() that doesn't take locals, but does
3647 take builtins without sanity checking them.
3649 assert(tstate
!= NULL
);
3650 f
= PyFrame_New(tstate
, co
, globals
, NULL
);
3654 fastlocals
= f
->f_localsplus
;
3655 stack
= (*pp_stack
) - n
;
3657 for (i
= 0; i
< n
; i
++) {
3659 fastlocals
[i
] = *stack
++;
3661 retval
= PyEval_EvalFrameEx(f
,0);
3662 ++tstate
->recursion_depth
;
3664 --tstate
->recursion_depth
;
3667 if (argdefs
!= NULL
) {
3668 d
= &PyTuple_GET_ITEM(argdefs
, 0);
3669 nd
= Py_Size(argdefs
);
3671 return PyEval_EvalCodeEx(co
, globals
,
3672 (PyObject
*)NULL
, (*pp_stack
)-n
, na
,
3673 (*pp_stack
)-2*nk
, nk
, d
, nd
,
3674 PyFunction_GET_CLOSURE(func
));
3678 update_keyword_args(PyObject
*orig_kwdict
, int nk
, PyObject
***pp_stack
,
3681 PyObject
*kwdict
= NULL
;
3682 if (orig_kwdict
== NULL
)
3683 kwdict
= PyDict_New();
3685 kwdict
= PyDict_Copy(orig_kwdict
);
3686 Py_DECREF(orig_kwdict
);
3692 PyObject
*value
= EXT_POP(*pp_stack
);
3693 PyObject
*key
= EXT_POP(*pp_stack
);
3694 if (PyDict_GetItem(kwdict
, key
) != NULL
) {
3695 PyErr_Format(PyExc_TypeError
,
3696 "%.200s%s got multiple values "
3697 "for keyword argument '%.200s'",
3698 PyEval_GetFuncName(func
),
3699 PyEval_GetFuncDesc(func
),
3700 PyString_AsString(key
));
3706 err
= PyDict_SetItem(kwdict
, key
, value
);
3718 update_star_args(int nstack
, int nstar
, PyObject
*stararg
,
3719 PyObject
***pp_stack
)
3721 PyObject
*callargs
, *w
;
3723 callargs
= PyTuple_New(nstack
+ nstar
);
3724 if (callargs
== NULL
) {
3729 for (i
= 0; i
< nstar
; i
++) {
3730 PyObject
*a
= PyTuple_GET_ITEM(stararg
, i
);
3732 PyTuple_SET_ITEM(callargs
, nstack
+ i
, a
);
3735 while (--nstack
>= 0) {
3736 w
= EXT_POP(*pp_stack
);
3737 PyTuple_SET_ITEM(callargs
, nstack
, w
);
3743 load_args(PyObject
***pp_stack
, int na
)
3745 PyObject
*args
= PyTuple_New(na
);
3751 w
= EXT_POP(*pp_stack
);
3752 PyTuple_SET_ITEM(args
, na
, w
);
3758 do_call(PyObject
*func
, PyObject
***pp_stack
, int na
, int nk
)
3760 PyObject
*callargs
= NULL
;
3761 PyObject
*kwdict
= NULL
;
3762 PyObject
*result
= NULL
;
3765 kwdict
= update_keyword_args(NULL
, nk
, pp_stack
, func
);
3769 callargs
= load_args(pp_stack
, na
);
3770 if (callargs
== NULL
)
3773 /* At this point, we have to look at the type of func to
3774 update the call stats properly. Do it here so as to avoid
3775 exposing the call stats machinery outside ceval.c
3777 if (PyFunction_Check(func
))
3778 PCALL(PCALL_FUNCTION
);
3779 else if (PyMethod_Check(func
))
3780 PCALL(PCALL_METHOD
);
3781 else if (PyType_Check(func
))
3786 result
= PyObject_Call(func
, callargs
, kwdict
);
3788 Py_XDECREF(callargs
);
3794 ext_do_call(PyObject
*func
, PyObject
***pp_stack
, int flags
, int na
, int nk
)
3797 PyObject
*callargs
= NULL
;
3798 PyObject
*stararg
= NULL
;
3799 PyObject
*kwdict
= NULL
;
3800 PyObject
*result
= NULL
;
3802 if (flags
& CALL_FLAG_KW
) {
3803 kwdict
= EXT_POP(*pp_stack
);
3804 if (!PyDict_Check(kwdict
)) {
3809 if (PyDict_Update(d
, kwdict
) != 0) {
3811 /* PyDict_Update raises attribute
3812 * error (percolated from an attempt
3813 * to get 'keys' attribute) instead of
3814 * a type error if its second argument
3817 if (PyErr_ExceptionMatches(PyExc_AttributeError
)) {
3818 PyErr_Format(PyExc_TypeError
,
3819 "%.200s%.200s argument after ** "
3820 "must be a mapping, not %.200s",
3821 PyEval_GetFuncName(func
),
3822 PyEval_GetFuncDesc(func
),
3823 kwdict
->ob_type
->tp_name
);
3831 if (flags
& CALL_FLAG_VAR
) {
3832 stararg
= EXT_POP(*pp_stack
);
3833 if (!PyTuple_Check(stararg
)) {
3835 t
= PySequence_Tuple(stararg
);
3837 if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
3838 PyErr_Format(PyExc_TypeError
,
3839 "%.200s%.200s argument after * "
3840 "must be a sequence, not %200s",
3841 PyEval_GetFuncName(func
),
3842 PyEval_GetFuncDesc(func
),
3843 stararg
->ob_type
->tp_name
);
3850 nstar
= PyTuple_GET_SIZE(stararg
);
3853 kwdict
= update_keyword_args(kwdict
, nk
, pp_stack
, func
);
3857 callargs
= update_star_args(na
, nstar
, stararg
, pp_stack
);
3858 if (callargs
== NULL
)
3861 /* At this point, we have to look at the type of func to
3862 update the call stats properly. Do it here so as to avoid
3863 exposing the call stats machinery outside ceval.c
3865 if (PyFunction_Check(func
))
3866 PCALL(PCALL_FUNCTION
);
3867 else if (PyMethod_Check(func
))
3868 PCALL(PCALL_METHOD
);
3869 else if (PyType_Check(func
))
3874 result
= PyObject_Call(func
, callargs
, kwdict
);
3876 Py_XDECREF(callargs
);
3878 Py_XDECREF(stararg
);
3882 /* Extract a slice index from a PyInt or PyLong or an object with the
3883 nb_index slot defined, and store in *pi.
3884 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3885 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
3886 Return 0 on error, 1 on success.
3888 /* Note: If v is NULL, return success without storing into *pi. This
3889 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3890 called by the SLICE opcode with v and/or w equal to NULL.
3893 _PyEval_SliceIndex(PyObject
*v
, Py_ssize_t
*pi
)
3897 if (PyInt_Check(v
)) {
3898 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3899 however, it looks like it should be AsSsize_t.
3900 There should be a comment here explaining why.
3902 x
= PyInt_AS_LONG(v
);
3904 else if (PyIndex_Check(v
)) {
3905 x
= PyNumber_AsSsize_t(v
, NULL
);
3906 if (x
== -1 && PyErr_Occurred())
3910 PyErr_SetString(PyExc_TypeError
,
3911 "slice indices must be integers or "
3912 "None or have an __index__ method");
3921 #define ISINDEX(x) ((x) == NULL || \
3922 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
3925 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
3927 PyTypeObject
*tp
= u
->ob_type
;
3928 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
3930 if (sq
&& sq
->sq_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
3931 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
3932 if (!_PyEval_SliceIndex(v
, &ilow
))
3934 if (!_PyEval_SliceIndex(w
, &ihigh
))
3936 return PySequence_GetSlice(u
, ilow
, ihigh
);
3939 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
3940 if (slice
!= NULL
) {
3941 PyObject
*res
= PyObject_GetItem(u
, slice
);
3951 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
3954 PyTypeObject
*tp
= u
->ob_type
;
3955 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
3957 if (sq
&& sq
->sq_ass_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
3958 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
3959 if (!_PyEval_SliceIndex(v
, &ilow
))
3961 if (!_PyEval_SliceIndex(w
, &ihigh
))
3964 return PySequence_DelSlice(u
, ilow
, ihigh
);
3966 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
3969 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
3970 if (slice
!= NULL
) {
3973 res
= PyObject_SetItem(u
, slice
, x
);
3975 res
= PyObject_DelItem(u
, slice
);
3985 cmp_outcome(int op
, register PyObject
*v
, register PyObject
*w
)
3996 res
= PySequence_Contains(w
, v
);
4001 res
= PySequence_Contains(w
, v
);
4006 case PyCmp_EXC_MATCH
:
4007 if (PyTuple_Check(w
)) {
4008 Py_ssize_t i
, length
;
4009 length
= PyTuple_Size(w
);
4010 for (i
= 0; i
< length
; i
+= 1) {
4011 PyObject
*exc
= PyTuple_GET_ITEM(w
, i
);
4012 if (PyString_Check(exc
)) {
4014 ret_val
= PyErr_WarnEx(
4015 PyExc_DeprecationWarning
,
4016 "catching of string "
4017 "exceptions is deprecated", 1);
4024 if (PyString_Check(w
)) {
4026 ret_val
= PyErr_WarnEx(
4027 PyExc_DeprecationWarning
,
4028 "catching of string "
4029 "exceptions is deprecated", 1);
4034 res
= PyErr_GivenExceptionMatches(v
, w
);
4037 return PyObject_RichCompare(v
, w
, op
);
4039 v
= res
? Py_True
: Py_False
;
4045 import_from(PyObject
*v
, PyObject
*name
)
4049 x
= PyObject_GetAttr(v
, name
);
4050 if (x
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4051 PyErr_Format(PyExc_ImportError
,
4052 "cannot import name %.230s",
4053 PyString_AsString(name
));
4059 import_all_from(PyObject
*locals
, PyObject
*v
)
4061 PyObject
*all
= PyObject_GetAttrString(v
, "__all__");
4062 PyObject
*dict
, *name
, *value
;
4063 int skip_leading_underscores
= 0;
4067 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4068 return -1; /* Unexpected error */
4070 dict
= PyObject_GetAttrString(v
, "__dict__");
4072 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4074 PyErr_SetString(PyExc_ImportError
,
4075 "from-import-* object has no __dict__ and no __all__");
4078 all
= PyMapping_Keys(dict
);
4082 skip_leading_underscores
= 1;
4085 for (pos
= 0, err
= 0; ; pos
++) {
4086 name
= PySequence_GetItem(all
, pos
);
4088 if (!PyErr_ExceptionMatches(PyExc_IndexError
))
4094 if (skip_leading_underscores
&&
4095 PyString_Check(name
) &&
4096 PyString_AS_STRING(name
)[0] == '_')
4101 value
= PyObject_GetAttr(v
, name
);
4104 else if (PyDict_CheckExact(locals
))
4105 err
= PyDict_SetItem(locals
, name
, value
);
4107 err
= PyObject_SetItem(locals
, name
, value
);
4118 build_class(PyObject
*methods
, PyObject
*bases
, PyObject
*name
)
4120 PyObject
*metaclass
= NULL
, *result
, *base
;
4122 if (PyDict_Check(methods
))
4123 metaclass
= PyDict_GetItemString(methods
, "__metaclass__");
4124 if (metaclass
!= NULL
)
4125 Py_INCREF(metaclass
);
4126 else if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
4127 base
= PyTuple_GET_ITEM(bases
, 0);
4128 metaclass
= PyObject_GetAttrString(base
, "__class__");
4129 if (metaclass
== NULL
) {
4131 metaclass
= (PyObject
*)base
->ob_type
;
4132 Py_INCREF(metaclass
);
4136 PyObject
*g
= PyEval_GetGlobals();
4137 if (g
!= NULL
&& PyDict_Check(g
))
4138 metaclass
= PyDict_GetItemString(g
, "__metaclass__");
4139 if (metaclass
== NULL
)
4140 metaclass
= (PyObject
*) &PyClass_Type
;
4141 Py_INCREF(metaclass
);
4143 result
= PyObject_CallFunctionObjArgs(metaclass
, name
, bases
, methods
,
4145 Py_DECREF(metaclass
);
4146 if (result
== NULL
&& PyErr_ExceptionMatches(PyExc_TypeError
)) {
4147 /* A type error here likely means that the user passed
4148 in a base that was not a class (such the random module
4149 instead of the random.random type). Help them out with
4150 by augmenting the error message with more information.*/
4152 PyObject
*ptype
, *pvalue
, *ptraceback
;
4154 PyErr_Fetch(&ptype
, &pvalue
, &ptraceback
);
4155 if (PyString_Check(pvalue
)) {
4157 newmsg
= PyString_FromFormat(
4158 "Error when calling the metaclass bases\n"
4160 PyString_AS_STRING(pvalue
));
4161 if (newmsg
!= NULL
) {
4166 PyErr_Restore(ptype
, pvalue
, ptraceback
);
4172 exec_statement(PyFrameObject
*f
, PyObject
*prog
, PyObject
*globals
,
4179 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
4180 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
4181 /* Backward compatibility hack */
4182 globals
= PyTuple_GetItem(prog
, 1);
4184 locals
= PyTuple_GetItem(prog
, 2);
4185 prog
= PyTuple_GetItem(prog
, 0);
4187 if (globals
== Py_None
) {
4188 globals
= PyEval_GetGlobals();
4189 if (locals
== Py_None
) {
4190 locals
= PyEval_GetLocals();
4193 if (!globals
|| !locals
) {
4194 PyErr_SetString(PyExc_SystemError
,
4195 "globals and locals cannot be NULL");
4199 else if (locals
== Py_None
)
4201 if (!PyString_Check(prog
) &&
4202 !PyUnicode_Check(prog
) &&
4203 !PyCode_Check(prog
) &&
4204 !PyFile_Check(prog
)) {
4205 PyErr_SetString(PyExc_TypeError
,
4206 "exec: arg 1 must be a string, file, or code object");
4209 if (!PyDict_Check(globals
)) {
4210 PyErr_SetString(PyExc_TypeError
,
4211 "exec: arg 2 must be a dictionary or None");
4214 if (!PyMapping_Check(locals
)) {
4215 PyErr_SetString(PyExc_TypeError
,
4216 "exec: arg 3 must be a mapping or None");
4219 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
4220 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
4221 if (PyCode_Check(prog
)) {
4222 if (PyCode_GetNumFree((PyCodeObject
*)prog
) > 0) {
4223 PyErr_SetString(PyExc_TypeError
,
4224 "code object passed to exec may not contain free variables");
4227 v
= PyEval_EvalCode((PyCodeObject
*) prog
, globals
, locals
);
4229 else if (PyFile_Check(prog
)) {
4230 FILE *fp
= PyFile_AsFile(prog
);
4231 char *name
= PyString_AsString(PyFile_Name(prog
));
4236 if (PyEval_MergeCompilerFlags(&cf
))
4237 v
= PyRun_FileFlags(fp
, name
, Py_file_input
, globals
,
4240 v
= PyRun_File(fp
, name
, Py_file_input
, globals
,
4244 PyObject
*tmp
= NULL
;
4248 #ifdef Py_USING_UNICODE
4249 if (PyUnicode_Check(prog
)) {
4250 tmp
= PyUnicode_AsUTF8String(prog
);
4254 cf
.cf_flags
|= PyCF_SOURCE_IS_UTF8
;
4257 if (PyString_AsStringAndSize(prog
, &str
, NULL
))
4259 if (PyEval_MergeCompilerFlags(&cf
))
4260 v
= PyRun_StringFlags(str
, Py_file_input
, globals
,
4263 v
= PyRun_String(str
, Py_file_input
, globals
, locals
);
4267 PyFrame_LocalsToFast(f
, 0);
4275 format_exc_check_arg(PyObject
*exc
, char *format_str
, PyObject
*obj
)
4282 obj_str
= PyString_AsString(obj
);
4286 PyErr_Format(exc
, format_str
, obj_str
);
4290 string_concatenate(PyObject
*v
, PyObject
*w
,
4291 PyFrameObject
*f
, unsigned char *next_instr
)
4293 /* This function implements 'variable += expr' when both arguments
4295 Py_ssize_t v_len
= PyString_GET_SIZE(v
);
4296 Py_ssize_t w_len
= PyString_GET_SIZE(w
);
4297 Py_ssize_t new_len
= v_len
+ w_len
;
4299 PyErr_SetString(PyExc_OverflowError
,
4300 "strings are too large to concat");
4304 if (v
->ob_refcnt
== 2) {
4305 /* In the common case, there are 2 references to the value
4306 * stored in 'variable' when the += is performed: one on the
4307 * value stack (in 'v') and one still stored in the
4308 * 'variable'. We try to delete the variable now to reduce
4311 switch (*next_instr
) {
4314 int oparg
= PEEKARG();
4315 PyObject
**fastlocals
= f
->f_localsplus
;
4316 if (GETLOCAL(oparg
) == v
)
4317 SETLOCAL(oparg
, NULL
);
4322 PyObject
**freevars
= (f
->f_localsplus
+
4323 f
->f_code
->co_nlocals
);
4324 PyObject
*c
= freevars
[PEEKARG()];
4325 if (PyCell_GET(c
) == v
)
4326 PyCell_Set(c
, NULL
);
4331 PyObject
*names
= f
->f_code
->co_names
;
4332 PyObject
*name
= GETITEM(names
, PEEKARG());
4333 PyObject
*locals
= f
->f_locals
;
4334 if (PyDict_CheckExact(locals
) &&
4335 PyDict_GetItem(locals
, name
) == v
) {
4336 if (PyDict_DelItem(locals
, name
) != 0) {
4345 if (v
->ob_refcnt
== 1 && !PyString_CHECK_INTERNED(v
)) {
4346 /* Now we own the last reference to 'v', so we can resize it
4349 if (_PyString_Resize(&v
, new_len
) != 0) {
4350 /* XXX if _PyString_Resize() fails, 'v' has been
4351 * deallocated so it cannot be put back into
4352 * 'variable'. The MemoryError is raised when there
4353 * is no value in 'variable', which might (very
4354 * remotely) be a cause of incompatibilities.
4358 /* copy 'w' into the newly allocated area of 'v' */
4359 memcpy(PyString_AS_STRING(v
) + v_len
,
4360 PyString_AS_STRING(w
), w_len
);
4364 /* When in-place resizing is not an option. */
4365 PyString_Concat(&v
, w
);
4370 #ifdef DYNAMIC_EXECUTION_PROFILE
4373 getarray(long a
[256])
4376 PyObject
*l
= PyList_New(256);
4377 if (l
== NULL
) return NULL
;
4378 for (i
= 0; i
< 256; i
++) {
4379 PyObject
*x
= PyInt_FromLong(a
[i
]);
4384 PyList_SetItem(l
, i
, x
);
4386 for (i
= 0; i
< 256; i
++)
4392 _Py_GetDXProfile(PyObject
*self
, PyObject
*args
)
4395 return getarray(dxp
);
4398 PyObject
*l
= PyList_New(257);
4399 if (l
== NULL
) return NULL
;
4400 for (i
= 0; i
< 257; i
++) {
4401 PyObject
*x
= getarray(dxpairs
[i
]);
4406 PyList_SetItem(l
, i
, x
);