2 /* Execute compiled code */
5 XXX speed up searching for keywords by using a dictionary
9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE
15 #include "frameobject.h"
18 #include "structmember.h"
24 #define READ_TIMESTAMP(var)
28 typedef unsigned long long uint64
;
30 #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
31 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
35 #define READ_TIMESTAMP(var) ppc_getcounter(&var)
38 ppc_getcounter(uint64
*v
)
40 register unsigned long tbu
, tb
, tbu2
;
43 asm volatile ("mftbu %0" : "=r" (tbu
) );
44 asm volatile ("mftb %0" : "=r" (tb
) );
45 asm volatile ("mftbu %0" : "=r" (tbu2
));
46 if (__builtin_expect(tbu
!= tbu2
, 0)) goto loop
;
48 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v
))[0] = tbu
;
54 #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
56 #define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
61 void dump_tsc(int opcode
, int ticked
, uint64 inst0
, uint64 inst1
,
62 uint64 loop0
, uint64 loop1
, uint64 intr0
, uint64 intr1
)
64 uint64 intr
, inst
, loop
;
65 PyThreadState
*tstate
= PyThreadState_Get();
66 if (!tstate
->interp
->tscdump
)
69 inst
= inst1
- inst0
- intr
;
70 loop
= loop1
- loop0
- intr
;
71 fprintf(stderr
, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode
, ticked
, inst
, loop
);
77 /* Turn this on if your compiler chokes on the big switch: */
78 /* #define CASE_TOO_BIG 1 */
81 /* For debugging the interpreter: */
82 #define LLTRACE 1 /* Low-level trace feature */
83 #define CHECKEXC 1 /* Double-check exception checking */
86 typedef PyObject
*(*callproc
)(PyObject
*, PyObject
*, PyObject
*);
88 /* Forward declarations */
90 static PyObject
* call_function(PyObject
***, int, uint64
*, uint64
*);
92 static PyObject
* call_function(PyObject
***, int);
94 static PyObject
* fast_function(PyObject
*, PyObject
***, int, int, int);
95 static PyObject
* do_call(PyObject
*, PyObject
***, int, int);
96 static PyObject
* ext_do_call(PyObject
*, PyObject
***, int, int, int);
97 static PyObject
* update_keyword_args(PyObject
*, int, PyObject
***,
99 static PyObject
* update_star_args(int, int, PyObject
*, PyObject
***);
100 static PyObject
* load_args(PyObject
***, int);
101 #define CALL_FLAG_VAR 1
102 #define CALL_FLAG_KW 2
106 static int prtrace(PyObject
*, char *);
108 static int call_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*,
110 static int call_trace_protected(Py_tracefunc
, PyObject
*,
111 PyFrameObject
*, int, PyObject
*);
112 static void call_exc_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*);
113 static int maybe_call_line_trace(Py_tracefunc
, PyObject
*,
114 PyFrameObject
*, int *, int *, int *);
116 static PyObject
* apply_slice(PyObject
*, PyObject
*, PyObject
*);
117 static int assign_slice(PyObject
*, PyObject
*,
118 PyObject
*, PyObject
*);
119 static PyObject
* cmp_outcome(int, PyObject
*, PyObject
*);
120 static PyObject
* import_from(PyObject
*, PyObject
*);
121 static int import_all_from(PyObject
*, PyObject
*);
122 static PyObject
* build_class(PyObject
*, PyObject
*, PyObject
*);
123 static int exec_statement(PyFrameObject
*,
124 PyObject
*, PyObject
*, PyObject
*);
125 static void set_exc_info(PyThreadState
*, PyObject
*, PyObject
*, PyObject
*);
126 static void reset_exc_info(PyThreadState
*);
127 static void format_exc_check_arg(PyObject
*, char *, PyObject
*);
128 static PyObject
* string_concatenate(PyObject
*, PyObject
*,
129 PyFrameObject
*, unsigned char *);
131 #define NAME_ERROR_MSG \
132 "name '%.200s' is not defined"
133 #define GLOBAL_NAME_ERROR_MSG \
134 "global name '%.200s' is not defined"
135 #define UNBOUNDLOCAL_ERROR_MSG \
136 "local variable '%.200s' referenced before assignment"
137 #define UNBOUNDFREE_ERROR_MSG \
138 "free variable '%.200s' referenced before assignment" \
139 " in enclosing scope"
141 /* Dynamic execution profile */
142 #ifdef DYNAMIC_EXECUTION_PROFILE
144 static long dxpairs
[257][256];
145 #define dxp dxpairs[256]
147 static long dxp
[256];
151 /* Function call profile */
154 static int pcall
[PCALL_NUM
];
157 #define PCALL_FUNCTION 1
158 #define PCALL_FAST_FUNCTION 2
159 #define PCALL_FASTER_FUNCTION 3
160 #define PCALL_METHOD 4
161 #define PCALL_BOUND_METHOD 5
162 #define PCALL_CFUNCTION 6
164 #define PCALL_GENERATOR 8
165 #define PCALL_OTHER 9
168 /* Notes about the statistics
172 FAST_FUNCTION means no argument tuple needs to be created.
173 FASTER_FUNCTION means that the fast-path frame setup code is used.
175 If there is a method call where the call can be optimized by changing
176 the argument tuple and calling the function directly, it gets recorded
179 As a result, the relationship among the statistics appears to be
180 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
181 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
182 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
183 PCALL_METHOD > PCALL_BOUND_METHOD
186 #define PCALL(POS) pcall[POS]++
189 PyEval_GetCallStats(PyObject
*self
)
191 return Py_BuildValue("iiiiiiiiiii",
192 pcall
[0], pcall
[1], pcall
[2], pcall
[3],
193 pcall
[4], pcall
[5], pcall
[6], pcall
[7],
194 pcall
[8], pcall
[9], pcall
[10]);
200 PyEval_GetCallStats(PyObject
*self
)
213 #include "pythread.h"
215 static PyThread_type_lock interpreter_lock
= 0; /* This is the GIL */
216 static 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 HAS_ARG 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) ((void)(lltrace && \
669 prtrace((STACK_POINTER)[-1], "ext_pop")), \
672 #define PUSH(v) BASIC_PUSH(v)
673 #define POP() BASIC_POP()
674 #define STACKADJ(n) BASIC_STACKADJ(n)
675 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
678 /* Local variable macros */
680 #define GETLOCAL(i) (fastlocals[i])
682 /* The SETLOCAL() macro must not DECREF the local variable in-place and
683 then store the new value; it must copy the old value to a temporary
684 value, then store the new value, and then DECREF the temporary value.
685 This is because it is possible that during the DECREF the frame is
686 accessed by other code (e.g. a __del__ method or gc.collect()) and the
687 variable would be pointing to already-freed memory. */
688 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
689 GETLOCAL(i) = value; \
690 Py_XDECREF(tmp); } while (0)
698 if (Py_EnterRecursiveCall(""))
703 if (tstate
->use_tracing
) {
704 if (tstate
->c_tracefunc
!= NULL
) {
705 /* tstate->c_tracefunc, if defined, is a
706 function that will be called on *every* entry
707 to a code block. Its return value, if not
708 None, is a function that will be called at
709 the start of each executed line of code.
710 (Actually, the function must return itself
711 in order to continue tracing.) The trace
712 functions are called with three arguments:
713 a pointer to the current frame, a string
714 indicating why the function is called, and
715 an argument which depends on the situation.
716 The global trace function is also called
717 whenever an exception is detected. */
718 if (call_trace_protected(tstate
->c_tracefunc
,
720 f
, PyTrace_CALL
, Py_None
)) {
721 /* Trace function raised an error */
722 goto exit_eval_frame
;
725 if (tstate
->c_profilefunc
!= NULL
) {
726 /* Similar for c_profilefunc, except it needn't
727 return itself and isn't called for "line" events */
728 if (call_trace_protected(tstate
->c_profilefunc
,
729 tstate
->c_profileobj
,
730 f
, PyTrace_CALL
, Py_None
)) {
731 /* Profile function raised an error */
732 goto exit_eval_frame
;
738 names
= co
->co_names
;
739 consts
= co
->co_consts
;
740 fastlocals
= f
->f_localsplus
;
741 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
742 first_instr
= (unsigned char*) PyBytes_AS_STRING(co
->co_code
);
743 /* An explanation is in order for the next line.
745 f->f_lasti now refers to the index of the last instruction
746 executed. You might think this was obvious from the name, but
747 this wasn't always true before 2.3! PyFrame_New now sets
748 f->f_lasti to -1 (i.e. the index *before* the first instruction)
749 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
752 When the PREDICT() macros are enabled, some opcode pairs follow in
753 direct succession without updating f->f_lasti. A successful
754 prediction effectively links the two codes together as if they
755 were a single new opcode; accordingly,f->f_lasti will point to
756 the first code in the pair (for instance, GET_ITER followed by
757 FOR_ITER is effectively a single opcode and f->f_lasti will point
758 at to the beginning of the combined pair.)
760 next_instr
= first_instr
+ f
->f_lasti
+ 1;
761 stack_pointer
= f
->f_stacktop
;
762 assert(stack_pointer
!= NULL
);
763 f
->f_stacktop
= NULL
; /* remains NULL unless yield suspends frame */
766 lltrace
= PyDict_GetItemString(f
->f_globals
, "__lltrace__") != NULL
;
768 #if defined(Py_DEBUG) || defined(LLTRACE)
769 filename
= PyBytes_AsString(co
->co_filename
);
774 x
= Py_None
; /* Not a reference, just anything non-NULL */
777 if (throwflag
) { /* support for generator.throw() */
785 /* Almost surely, the opcode executed a break
786 or a continue, preventing inst1 from being set
787 on the way out of the loop.
789 READ_TIMESTAMP(inst1
);
792 dump_tsc(opcode
, ticked
, inst0
, inst1
, loop0
, loop1
,
798 READ_TIMESTAMP(loop0
);
800 assert(stack_pointer
>= f
->f_valuestack
); /* else underflow */
801 assert(STACK_LEVEL() <= co
->co_stacksize
); /* else overflow */
803 /* Do periodic things. Doing this every time through
804 the loop would add too much overhead, so we do it
805 only every Nth instruction. We also do it if
806 ``things_to_do'' is set, i.e. when an asynchronous
807 event needs attention (e.g. a signal handler or
808 async I/O handler); see Py_AddPendingCall() and
809 Py_MakePendingCalls() above. */
811 if (--_Py_Ticker
< 0) {
812 if (*next_instr
== SETUP_FINALLY
) {
813 /* Make the last opcode before
814 a try: finally: block uninterruptable. */
815 goto fast_next_opcode
;
817 _Py_Ticker
= _Py_CheckInterval
;
818 tstate
->tick_counter
++;
823 if (Py_MakePendingCalls() < 0) {
828 /* MakePendingCalls() didn't succeed.
829 Force early re-execution of this
830 "periodic" code, possibly after
835 if (interpreter_lock
) {
836 /* Give another thread a chance */
838 if (PyThreadState_Swap(NULL
) != tstate
)
839 Py_FatalError("ceval: tstate mix-up");
840 PyThread_release_lock(interpreter_lock
);
842 /* Other threads may run now */
844 PyThread_acquire_lock(interpreter_lock
, 1);
845 if (PyThreadState_Swap(tstate
) != NULL
)
846 Py_FatalError("ceval: orphan tstate");
848 /* Check for thread interrupts */
850 if (tstate
->async_exc
!= NULL
) {
851 x
= tstate
->async_exc
;
852 tstate
->async_exc
= NULL
;
863 f
->f_lasti
= INSTR_OFFSET();
865 /* line-by-line tracing support */
867 if (tstate
->c_tracefunc
!= NULL
&& !tstate
->tracing
) {
868 /* see maybe_call_line_trace
869 for expository comments */
870 f
->f_stacktop
= stack_pointer
;
872 err
= maybe_call_line_trace(tstate
->c_tracefunc
,
874 f
, &instr_lb
, &instr_ub
,
876 /* Reload possibly changed frame fields */
878 if (f
->f_stacktop
!= NULL
) {
879 stack_pointer
= f
->f_stacktop
;
880 f
->f_stacktop
= NULL
;
883 /* trace function raised an exception */
888 /* Extract opcode and argument */
891 oparg
= 0; /* allows oparg to be stored in a register because
892 it doesn't have to be remembered across a full loop */
896 #ifdef DYNAMIC_EXECUTION_PROFILE
898 dxpairs
[lastopcode
][opcode
]++;
905 /* Instruction tracing */
908 if (HAS_ARG(opcode
)) {
909 printf("%d: %d, %d\n",
910 f
->f_lasti
, opcode
, oparg
);
919 /* Main switch on opcode */
920 READ_TIMESTAMP(inst0
);
925 It is essential that any operation that fails sets either
926 x to NULL, err to nonzero, or why to anything but WHY_NOT,
927 and that no operation that succeeds does this! */
929 /* case STOP_CODE: this is an error! */
932 goto fast_next_opcode
;
939 goto fast_next_opcode
;
941 format_exc_check_arg(PyExc_UnboundLocalError
,
942 UNBOUNDLOCAL_ERROR_MSG
,
943 PyTuple_GetItem(co
->co_varnames
, oparg
));
947 x
= GETITEM(consts
, oparg
);
950 goto fast_next_opcode
;
952 PREDICTED_WITH_ARG(STORE_FAST
);
956 goto fast_next_opcode
;
962 goto fast_next_opcode
;
969 goto fast_next_opcode
;
978 goto fast_next_opcode
;
989 goto fast_next_opcode
;
995 goto fast_next_opcode
;
1006 goto fast_next_opcode
;
1007 } else if (oparg
== 3) {
1018 goto fast_next_opcode
;
1020 Py_FatalError("invalid argument to DUP_TOPX"
1021 " (bytecode corruption?)");
1024 case UNARY_POSITIVE
:
1026 x
= PyNumber_Positive(v
);
1029 if (x
!= NULL
) continue;
1032 case UNARY_NEGATIVE
:
1034 x
= PyNumber_Negative(v
);
1037 if (x
!= NULL
) continue;
1042 err
= PyObject_IsTrue(v
);
1050 Py_INCREF(Py_False
);
1060 x
= PyObject_Repr(v
);
1063 if (x
!= NULL
) continue;
1068 x
= PyNumber_Invert(v
);
1071 if (x
!= NULL
) continue;
1077 x
= PyNumber_Power(v
, w
, Py_None
);
1081 if (x
!= NULL
) continue;
1084 case BINARY_MULTIPLY
:
1087 x
= PyNumber_Multiply(v
, w
);
1091 if (x
!= NULL
) continue;
1095 if (!_Py_QnewFlag
) {
1098 x
= PyNumber_Divide(v
, w
);
1102 if (x
!= NULL
) continue;
1105 /* -Qnew is in effect: fall through to
1106 BINARY_TRUE_DIVIDE */
1107 case BINARY_TRUE_DIVIDE
:
1110 x
= PyNumber_TrueDivide(v
, w
);
1114 if (x
!= NULL
) continue;
1117 case BINARY_FLOOR_DIVIDE
:
1120 x
= PyNumber_FloorDivide(v
, w
);
1124 if (x
!= NULL
) continue;
1130 x
= PyNumber_Remainder(v
, w
);
1134 if (x
!= NULL
) continue;
1140 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1141 /* INLINE: int + int */
1142 register long a
, b
, i
;
1143 a
= PyInt_AS_LONG(v
);
1144 b
= PyInt_AS_LONG(w
);
1146 if ((i
^a
) < 0 && (i
^b
) < 0)
1148 x
= PyInt_FromLong(i
);
1150 else if (PyBytes_CheckExact(v
) &&
1151 PyBytes_CheckExact(w
)) {
1152 x
= string_concatenate(v
, w
, f
, next_instr
);
1153 /* string_concatenate consumed the ref to v */
1154 goto skip_decref_vx
;
1158 x
= PyNumber_Add(v
, w
);
1164 if (x
!= NULL
) continue;
1167 case BINARY_SUBTRACT
:
1170 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1171 /* INLINE: int - int */
1172 register long a
, b
, i
;
1173 a
= PyInt_AS_LONG(v
);
1174 b
= PyInt_AS_LONG(w
);
1176 if ((i
^a
) < 0 && (i
^~b
) < 0)
1178 x
= PyInt_FromLong(i
);
1182 x
= PyNumber_Subtract(v
, w
);
1187 if (x
!= NULL
) continue;
1193 if (PyList_CheckExact(v
) && PyInt_CheckExact(w
)) {
1194 /* INLINE: list[int] */
1195 Py_ssize_t i
= PyInt_AsSsize_t(w
);
1197 i
+= PyList_GET_SIZE(v
);
1198 if (i
>= 0 && i
< PyList_GET_SIZE(v
)) {
1199 x
= PyList_GET_ITEM(v
, i
);
1207 x
= PyObject_GetItem(v
, w
);
1211 if (x
!= NULL
) continue;
1217 x
= PyNumber_Lshift(v
, w
);
1221 if (x
!= NULL
) continue;
1227 x
= PyNumber_Rshift(v
, w
);
1231 if (x
!= NULL
) continue;
1237 x
= PyNumber_And(v
, w
);
1241 if (x
!= NULL
) continue;
1247 x
= PyNumber_Xor(v
, w
);
1251 if (x
!= NULL
) continue;
1257 x
= PyNumber_Or(v
, w
);
1261 if (x
!= NULL
) continue;
1267 err
= PyList_Append(v
, w
);
1271 PREDICT(JUMP_ABSOLUTE
);
1279 x
= PyNumber_InPlacePower(v
, w
, Py_None
);
1283 if (x
!= NULL
) continue;
1286 case INPLACE_MULTIPLY
:
1289 x
= PyNumber_InPlaceMultiply(v
, w
);
1293 if (x
!= NULL
) continue;
1296 case INPLACE_DIVIDE
:
1297 if (!_Py_QnewFlag
) {
1300 x
= PyNumber_InPlaceDivide(v
, w
);
1304 if (x
!= NULL
) continue;
1307 /* -Qnew is in effect: fall through to
1308 INPLACE_TRUE_DIVIDE */
1309 case INPLACE_TRUE_DIVIDE
:
1312 x
= PyNumber_InPlaceTrueDivide(v
, w
);
1316 if (x
!= NULL
) continue;
1319 case INPLACE_FLOOR_DIVIDE
:
1322 x
= PyNumber_InPlaceFloorDivide(v
, w
);
1326 if (x
!= NULL
) continue;
1329 case INPLACE_MODULO
:
1332 x
= PyNumber_InPlaceRemainder(v
, w
);
1336 if (x
!= NULL
) continue;
1342 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1343 /* INLINE: int + int */
1344 register long a
, b
, i
;
1345 a
= PyInt_AS_LONG(v
);
1346 b
= PyInt_AS_LONG(w
);
1348 if ((i
^a
) < 0 && (i
^b
) < 0)
1350 x
= PyInt_FromLong(i
);
1352 else if (PyBytes_CheckExact(v
) &&
1353 PyBytes_CheckExact(w
)) {
1354 x
= string_concatenate(v
, w
, f
, next_instr
);
1355 /* string_concatenate consumed the ref to v */
1360 x
= PyNumber_InPlaceAdd(v
, w
);
1366 if (x
!= NULL
) continue;
1369 case INPLACE_SUBTRACT
:
1372 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1373 /* INLINE: int - int */
1374 register long a
, b
, i
;
1375 a
= PyInt_AS_LONG(v
);
1376 b
= PyInt_AS_LONG(w
);
1378 if ((i
^a
) < 0 && (i
^~b
) < 0)
1380 x
= PyInt_FromLong(i
);
1384 x
= PyNumber_InPlaceSubtract(v
, w
);
1389 if (x
!= NULL
) continue;
1392 case INPLACE_LSHIFT
:
1395 x
= PyNumber_InPlaceLshift(v
, w
);
1399 if (x
!= NULL
) continue;
1402 case INPLACE_RSHIFT
:
1405 x
= PyNumber_InPlaceRshift(v
, w
);
1409 if (x
!= NULL
) continue;
1415 x
= PyNumber_InPlaceAnd(v
, w
);
1419 if (x
!= NULL
) continue;
1425 x
= PyNumber_InPlaceXor(v
, w
);
1429 if (x
!= NULL
) continue;
1435 x
= PyNumber_InPlaceOr(v
, w
);
1439 if (x
!= NULL
) continue;
1446 if ((opcode
-SLICE
) & 2)
1450 if ((opcode
-SLICE
) & 1)
1455 x
= apply_slice(u
, v
, w
);
1460 if (x
!= NULL
) continue;
1467 if ((opcode
-STORE_SLICE
) & 2)
1471 if ((opcode
-STORE_SLICE
) & 1)
1477 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
1482 if (err
== 0) continue;
1485 case DELETE_SLICE
+0:
1486 case DELETE_SLICE
+1:
1487 case DELETE_SLICE
+2:
1488 case DELETE_SLICE
+3:
1489 if ((opcode
-DELETE_SLICE
) & 2)
1493 if ((opcode
-DELETE_SLICE
) & 1)
1498 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
1503 if (err
== 0) continue;
1512 err
= PyObject_SetItem(v
, w
, u
);
1516 if (err
== 0) continue;
1524 err
= PyObject_DelItem(v
, w
);
1527 if (err
== 0) continue;
1532 w
= PySys_GetObject("displayhook");
1534 PyErr_SetString(PyExc_RuntimeError
,
1535 "lost sys.displayhook");
1540 x
= PyTuple_Pack(1, v
);
1545 w
= PyEval_CallObject(w
, x
);
1556 /* fall through to PRINT_ITEM */
1560 if (stream
== NULL
|| stream
== Py_None
) {
1561 w
= PySys_GetObject("stdout");
1563 PyErr_SetString(PyExc_RuntimeError
,
1568 /* PyFile_SoftSpace() can exececute arbitrary code
1569 if sys.stdout is an instance with a __getattr__.
1570 If __getattr__ raises an exception, w will
1571 be freed, so we need to prevent that temporarily. */
1573 if (w
!= NULL
&& PyFile_SoftSpace(w
, 0))
1574 err
= PyFile_WriteString(" ", w
);
1576 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1578 /* XXX move into writeobject() ? */
1579 if (PyBytes_Check(v
)) {
1580 char *s
= PyBytes_AS_STRING(v
);
1581 Py_ssize_t len
= PyBytes_GET_SIZE(v
);
1583 !isspace(Py_CHARMASK(s
[len
-1])) ||
1585 PyFile_SoftSpace(w
, 1);
1587 #ifdef Py_USING_UNICODE
1588 else if (PyUnicode_Check(v
)) {
1589 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(v
);
1590 Py_ssize_t len
= PyUnicode_GET_SIZE(v
);
1592 !Py_UNICODE_ISSPACE(s
[len
-1]) ||
1594 PyFile_SoftSpace(w
, 1);
1598 PyFile_SoftSpace(w
, 1);
1608 case PRINT_NEWLINE_TO
:
1610 /* fall through to PRINT_NEWLINE */
1613 if (stream
== NULL
|| stream
== Py_None
) {
1614 w
= PySys_GetObject("stdout");
1616 PyErr_SetString(PyExc_RuntimeError
,
1620 err
= PyFile_WriteString("\n", w
);
1622 PyFile_SoftSpace(w
, 0);
1630 default: switch (opcode
) {
1636 u
= POP(); /* traceback */
1639 v
= POP(); /* value */
1642 w
= POP(); /* exc */
1643 case 0: /* Fallthrough */
1644 why
= do_raise(w
, v
, u
);
1647 PyErr_SetString(PyExc_SystemError
,
1648 "bad RAISE_VARARGS oparg");
1649 why
= WHY_EXCEPTION
;
1655 if ((x
= f
->f_locals
) != NULL
) {
1660 PyErr_SetString(PyExc_SystemError
, "no locals");
1666 goto fast_block_end
;
1670 f
->f_stacktop
= stack_pointer
;
1679 READ_TIMESTAMP(intr0
);
1680 err
= exec_statement(f
, u
, v
, w
);
1681 READ_TIMESTAMP(intr1
);
1689 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1690 while (STACK_LEVEL() > b
->b_level
) {
1697 PREDICTED(END_FINALLY
);
1700 if (PyInt_Check(v
)) {
1701 why
= (enum why_code
) PyInt_AS_LONG(v
);
1702 assert(why
!= WHY_YIELD
);
1703 if (why
== WHY_RETURN
||
1704 why
== WHY_CONTINUE
)
1707 else if (PyExceptionClass_Check(v
) ||
1711 PyErr_Restore(v
, w
, u
);
1715 else if (v
!= Py_None
) {
1716 PyErr_SetString(PyExc_SystemError
,
1717 "'finally' pops bad exception");
1718 why
= WHY_EXCEPTION
;
1728 x
= build_class(u
, v
, w
);
1736 w
= GETITEM(names
, oparg
);
1738 if ((x
= f
->f_locals
) != NULL
) {
1739 if (PyDict_CheckExact(x
))
1740 err
= PyDict_SetItem(x
, w
, v
);
1742 err
= PyObject_SetItem(x
, w
, v
);
1744 if (err
== 0) continue;
1747 PyErr_Format(PyExc_SystemError
,
1748 "no locals found when storing %s",
1753 w
= GETITEM(names
, oparg
);
1754 if ((x
= f
->f_locals
) != NULL
) {
1755 if ((err
= PyObject_DelItem(x
, w
)) != 0)
1756 format_exc_check_arg(PyExc_NameError
,
1761 PyErr_Format(PyExc_SystemError
,
1762 "no locals when deleting %s",
1766 PREDICTED_WITH_ARG(UNPACK_SEQUENCE
);
1767 case UNPACK_SEQUENCE
:
1769 if (PyTuple_CheckExact(v
) &&
1770 PyTuple_GET_SIZE(v
) == oparg
) {
1771 PyObject
**items
= \
1772 ((PyTupleObject
*)v
)->ob_item
;
1780 } else if (PyList_CheckExact(v
) &&
1781 PyList_GET_SIZE(v
) == oparg
) {
1782 PyObject
**items
= \
1783 ((PyListObject
*)v
)->ob_item
;
1789 } else if (unpack_iterable(v
, oparg
,
1790 stack_pointer
+ oparg
)) {
1791 stack_pointer
+= oparg
;
1793 /* unpack_iterable() raised an exception */
1794 why
= WHY_EXCEPTION
;
1800 w
= GETITEM(names
, oparg
);
1804 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1807 if (err
== 0) continue;
1811 w
= GETITEM(names
, oparg
);
1813 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1819 w
= GETITEM(names
, oparg
);
1821 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1823 if (err
== 0) continue;
1827 w
= GETITEM(names
, oparg
);
1828 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1829 format_exc_check_arg(
1830 PyExc_NameError
, GLOBAL_NAME_ERROR_MSG
, w
);
1834 w
= GETITEM(names
, oparg
);
1835 if ((v
= f
->f_locals
) == NULL
) {
1836 PyErr_Format(PyExc_SystemError
,
1837 "no locals when loading %s",
1841 if (PyDict_CheckExact(v
)) {
1842 x
= PyDict_GetItem(v
, w
);
1846 x
= PyObject_GetItem(v
, w
);
1847 if (x
== NULL
&& PyErr_Occurred()) {
1848 if (!PyErr_ExceptionMatches(
1855 x
= PyDict_GetItem(f
->f_globals
, w
);
1857 x
= PyDict_GetItem(f
->f_builtins
, w
);
1859 format_exc_check_arg(
1871 w
= GETITEM(names
, oparg
);
1872 if (PyBytes_CheckExact(w
)) {
1873 /* Inline the PyDict_GetItem() calls.
1874 WARNING: this is an extreme speed hack.
1875 Do not try this at home. */
1876 long hash
= ((PyBytesObject
*)w
)->ob_shash
;
1880 d
= (PyDictObject
*)(f
->f_globals
);
1881 e
= d
->ma_lookup(d
, w
, hash
);
1892 d
= (PyDictObject
*)(f
->f_builtins
);
1893 e
= d
->ma_lookup(d
, w
, hash
);
1904 goto load_global_error
;
1907 /* This is the un-inlined version of the code above */
1908 x
= PyDict_GetItem(f
->f_globals
, w
);
1910 x
= PyDict_GetItem(f
->f_builtins
, w
);
1913 format_exc_check_arg(
1915 GLOBAL_NAME_ERROR_MSG
, w
);
1924 x
= GETLOCAL(oparg
);
1926 SETLOCAL(oparg
, NULL
);
1929 format_exc_check_arg(
1930 PyExc_UnboundLocalError
,
1931 UNBOUNDLOCAL_ERROR_MSG
,
1932 PyTuple_GetItem(co
->co_varnames
, oparg
)
1937 x
= freevars
[oparg
];
1940 if (x
!= NULL
) continue;
1944 x
= freevars
[oparg
];
1951 /* Don't stomp existing exception */
1952 if (PyErr_Occurred())
1954 if (oparg
< PyTuple_GET_SIZE(co
->co_cellvars
)) {
1955 v
= PyTuple_GET_ITEM(co
->co_cellvars
,
1957 format_exc_check_arg(
1958 PyExc_UnboundLocalError
,
1959 UNBOUNDLOCAL_ERROR_MSG
,
1962 v
= PyTuple_GET_ITEM(co
->co_freevars
, oparg
-
1963 PyTuple_GET_SIZE(co
->co_cellvars
));
1964 format_exc_check_arg(PyExc_NameError
,
1965 UNBOUNDFREE_ERROR_MSG
, v
);
1971 x
= freevars
[oparg
];
1977 x
= PyTuple_New(oparg
);
1979 for (; --oparg
>= 0;) {
1981 PyTuple_SET_ITEM(x
, oparg
, w
);
1989 x
= PyList_New(oparg
);
1991 for (; --oparg
>= 0;) {
1993 PyList_SET_ITEM(x
, oparg
, w
);
2001 x
= _PyDict_NewPresized((Py_ssize_t
)oparg
);
2003 if (x
!= NULL
) continue;
2007 w
= TOP(); /* key */
2008 u
= SECOND(); /* value */
2009 v
= THIRD(); /* dict */
2011 assert (PyDict_CheckExact(v
));
2012 err
= PyDict_SetItem(v
, w
, u
); /* v[w] = u */
2015 if (err
== 0) continue;
2019 w
= GETITEM(names
, oparg
);
2021 x
= PyObject_GetAttr(v
, w
);
2024 if (x
!= NULL
) continue;
2030 if (PyInt_CheckExact(w
) && PyInt_CheckExact(v
)) {
2031 /* INLINE: cmp(int, int) */
2034 a
= PyInt_AS_LONG(v
);
2035 b
= PyInt_AS_LONG(w
);
2037 case PyCmp_LT
: res
= a
< b
; break;
2038 case PyCmp_LE
: res
= a
<= b
; break;
2039 case PyCmp_EQ
: res
= a
== b
; break;
2040 case PyCmp_NE
: res
= a
!= b
; break;
2041 case PyCmp_GT
: res
= a
> b
; break;
2042 case PyCmp_GE
: res
= a
>= b
; break;
2043 case PyCmp_IS
: res
= v
== w
; break;
2044 case PyCmp_IS_NOT
: res
= v
!= w
; break;
2045 default: goto slow_compare
;
2047 x
= res
? Py_True
: Py_False
;
2052 x
= cmp_outcome(oparg
, v
, w
);
2057 if (x
== NULL
) break;
2058 PREDICT(JUMP_IF_FALSE
);
2059 PREDICT(JUMP_IF_TRUE
);
2063 w
= GETITEM(names
, oparg
);
2064 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
2066 PyErr_SetString(PyExc_ImportError
,
2067 "__import__ not found");
2073 if (PyInt_AsLong(u
) != -1 || PyErr_Occurred())
2077 f
->f_locals
== NULL
?
2078 Py_None
: f
->f_locals
,
2085 f
->f_locals
== NULL
?
2086 Py_None
: f
->f_locals
,
2096 READ_TIMESTAMP(intr0
);
2098 x
= PyEval_CallObject(v
, w
);
2100 READ_TIMESTAMP(intr1
);
2103 if (x
!= NULL
) continue;
2108 PyFrame_FastToLocals(f
);
2109 if ((x
= f
->f_locals
) == NULL
) {
2110 PyErr_SetString(PyExc_SystemError
,
2111 "no locals found during 'import *'");
2114 READ_TIMESTAMP(intr0
);
2115 err
= import_all_from(x
, v
);
2116 READ_TIMESTAMP(intr1
);
2117 PyFrame_LocalsToFast(f
, 0);
2119 if (err
== 0) continue;
2123 w
= GETITEM(names
, oparg
);
2125 READ_TIMESTAMP(intr0
);
2126 x
= import_from(v
, w
);
2127 READ_TIMESTAMP(intr1
);
2129 if (x
!= NULL
) continue;
2134 goto fast_next_opcode
;
2136 PREDICTED_WITH_ARG(JUMP_IF_FALSE
);
2141 goto fast_next_opcode
;
2143 if (w
== Py_False
) {
2145 goto fast_next_opcode
;
2147 err
= PyObject_IsTrue(w
);
2156 PREDICTED_WITH_ARG(JUMP_IF_TRUE
);
2159 if (w
== Py_False
) {
2161 goto fast_next_opcode
;
2165 goto fast_next_opcode
;
2167 err
= PyObject_IsTrue(w
);
2178 PREDICTED_WITH_ARG(JUMP_ABSOLUTE
);
2182 /* Enabling this path speeds-up all while and for-loops by bypassing
2183 the per-loop checks for signals. By default, this should be turned-off
2184 because it prevents detection of a control-break in tight loops like
2185 "while 1: pass". Compile with this option turned-on when you need
2186 the speed-up and do not need break checking inside tight loops (ones
2187 that contain only instructions ending with goto fast_next_opcode).
2189 goto fast_next_opcode
;
2195 /* before: [obj]; after [getiter(obj)] */
2197 x
= PyObject_GetIter(v
);
2207 PREDICTED_WITH_ARG(FOR_ITER
);
2209 /* before: [iter]; after: [iter, iter()] *or* [] */
2211 x
= (*v
->ob_type
->tp_iternext
)(v
);
2214 PREDICT(STORE_FAST
);
2215 PREDICT(UNPACK_SEQUENCE
);
2218 if (PyErr_Occurred()) {
2219 if (!PyErr_ExceptionMatches(
2220 PyExc_StopIteration
))
2224 /* iterator ended normally */
2232 goto fast_block_end
;
2235 retval
= PyInt_FromLong(oparg
);
2241 goto fast_block_end
;
2246 /* NOTE: If you add any new block-setup opcodes that
2247 are not try/except/finally handlers, you may need
2248 to update the PyGen_NeedsFinalizing() function.
2251 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
2257 /* At the top of the stack are 1-3 values indicating
2258 how/why we entered the finally clause:
2260 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2261 - TOP = WHY_*; no retval below it
2262 - (TOP, SECOND, THIRD) = exc_info()
2263 Below them is EXIT, the context.__exit__ bound method.
2264 In the last case, we must call
2265 EXIT(TOP, SECOND, THIRD)
2266 otherwise we must call
2267 EXIT(None, None, None)
2269 In all cases, we remove EXIT from the stack, leaving
2270 the rest in the same order.
2272 In addition, if the stack represents an exception,
2273 *and* the function call returns a 'true' value, we
2274 "zap" this information, to prevent END_FINALLY from
2275 re-raising the exception. (But non-local gotos
2276 should still be resumed.)
2279 PyObject
*exit_func
;
2287 else if (PyInt_Check(u
)) {
2288 switch(PyInt_AS_LONG(u
)) {
2291 /* Retval in TOP. */
2292 exit_func
= SECOND();
2301 u
= v
= w
= Py_None
;
2306 exit_func
= THIRD();
2311 /* XXX Not the fastest way to call it... */
2312 x
= PyObject_CallFunctionObjArgs(exit_func
, u
, v
, w
,
2315 Py_DECREF(exit_func
);
2316 break; /* Go to error exit */
2318 if (u
!= Py_None
&& PyObject_IsTrue(x
)) {
2319 /* There was an exception and a true return */
2327 /* The stack was rearranged to remove EXIT
2328 above. Let END_FINALLY do its thing */
2331 Py_DECREF(exit_func
);
2332 PREDICT(END_FINALLY
);
2342 x
= call_function(&sp
, oparg
, &intr0
, &intr1
);
2344 x
= call_function(&sp
, oparg
);
2353 case CALL_FUNCTION_VAR
:
2354 case CALL_FUNCTION_KW
:
2355 case CALL_FUNCTION_VAR_KW
:
2357 int na
= oparg
& 0xff;
2358 int nk
= (oparg
>>8) & 0xff;
2359 int flags
= (opcode
- CALL_FUNCTION
) & 3;
2360 int n
= na
+ 2 * nk
;
2361 PyObject
**pfunc
, *func
, **sp
;
2363 if (flags
& CALL_FLAG_VAR
)
2365 if (flags
& CALL_FLAG_KW
)
2367 pfunc
= stack_pointer
- n
- 1;
2370 if (PyMethod_Check(func
)
2371 && PyMethod_GET_SELF(func
) != NULL
) {
2372 PyObject
*self
= PyMethod_GET_SELF(func
);
2374 func
= PyMethod_GET_FUNCTION(func
);
2383 READ_TIMESTAMP(intr0
);
2384 x
= ext_do_call(func
, &sp
, flags
, na
, nk
);
2385 READ_TIMESTAMP(intr1
);
2389 while (stack_pointer
> pfunc
) {
2400 v
= POP(); /* code object */
2401 x
= PyFunction_New(v
, f
->f_globals
);
2403 /* XXX Maybe this should be a separate opcode? */
2404 if (x
!= NULL
&& oparg
> 0) {
2405 v
= PyTuple_New(oparg
);
2411 while (--oparg
>= 0) {
2413 PyTuple_SET_ITEM(v
, oparg
, w
);
2415 err
= PyFunction_SetDefaults(x
, v
);
2423 v
= POP(); /* code object */
2424 x
= PyFunction_New(v
, f
->f_globals
);
2428 err
= PyFunction_SetClosure(x
, v
);
2431 if (x
!= NULL
&& oparg
> 0) {
2432 v
= PyTuple_New(oparg
);
2438 while (--oparg
>= 0) {
2440 PyTuple_SET_ITEM(v
, oparg
, w
);
2442 err
= PyFunction_SetDefaults(x
, v
);
2456 x
= PySlice_New(u
, v
, w
);
2461 if (x
!= NULL
) continue;
2466 oparg
= oparg
<<16 | NEXTARG();
2467 goto dispatch_opcode
;
2471 "XXX lineno: %d, opcode: %d\n",
2472 PyCode_Addr2Line(f
->f_code
, f
->f_lasti
),
2474 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
2475 why
= WHY_EXCEPTION
;
2486 READ_TIMESTAMP(inst1
);
2488 /* Quickly continue if no error occurred */
2490 if (why
== WHY_NOT
) {
2491 if (err
== 0 && x
!= NULL
) {
2493 /* This check is expensive! */
2494 if (PyErr_Occurred())
2496 "XXX undetected error\n");
2499 READ_TIMESTAMP(loop1
);
2500 continue; /* Normal, fast path */
2505 why
= WHY_EXCEPTION
;
2510 /* Double-check exception status */
2512 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
2513 if (!PyErr_Occurred()) {
2514 PyErr_SetString(PyExc_SystemError
,
2515 "error return without exception set");
2516 why
= WHY_EXCEPTION
;
2521 /* This check is expensive! */
2522 if (PyErr_Occurred()) {
2524 sprintf(buf
, "Stack unwind with exception "
2525 "set and why=%d", why
);
2531 /* Log traceback info if this is a real exception */
2533 if (why
== WHY_EXCEPTION
) {
2534 PyTraceBack_Here(f
);
2536 if (tstate
->c_tracefunc
!= NULL
)
2537 call_exc_trace(tstate
->c_tracefunc
,
2538 tstate
->c_traceobj
, f
);
2541 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2543 if (why
== WHY_RERAISE
)
2544 why
= WHY_EXCEPTION
;
2546 /* Unwind stacks if a (pseudo) exception occurred */
2549 while (why
!= WHY_NOT
&& f
->f_iblock
> 0) {
2550 PyTryBlock
*b
= PyFrame_BlockPop(f
);
2552 assert(why
!= WHY_YIELD
);
2553 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_CONTINUE
) {
2554 /* For a continue inside a try block,
2555 don't pop the block for the loop. */
2556 PyFrame_BlockSetup(f
, b
->b_type
, b
->b_handler
,
2559 JUMPTO(PyInt_AS_LONG(retval
));
2564 while (STACK_LEVEL() > b
->b_level
) {
2568 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
2570 JUMPTO(b
->b_handler
);
2573 if (b
->b_type
== SETUP_FINALLY
||
2574 (b
->b_type
== SETUP_EXCEPT
&&
2575 why
== WHY_EXCEPTION
)) {
2576 if (why
== WHY_EXCEPTION
) {
2577 PyObject
*exc
, *val
, *tb
;
2578 PyErr_Fetch(&exc
, &val
, &tb
);
2583 /* Make the raw exception data
2584 available to the handler,
2585 so a program can emulate the
2586 Python main loop. Don't do
2587 this for 'finally'. */
2588 if (b
->b_type
== SETUP_EXCEPT
) {
2589 PyErr_NormalizeException(
2591 set_exc_info(tstate
,
2603 if (why
& (WHY_RETURN
| WHY_CONTINUE
))
2605 v
= PyInt_FromLong((long)why
);
2609 JUMPTO(b
->b_handler
);
2612 } /* unwind stack */
2614 /* End the loop if we still have an error (or return) */
2618 READ_TIMESTAMP(loop1
);
2622 assert(why
!= WHY_YIELD
);
2623 /* Pop remaining stack entries. */
2629 if (why
!= WHY_RETURN
)
2633 if (tstate
->use_tracing
) {
2634 if (tstate
->c_tracefunc
) {
2635 if (why
== WHY_RETURN
|| why
== WHY_YIELD
) {
2636 if (call_trace(tstate
->c_tracefunc
,
2637 tstate
->c_traceobj
, f
,
2638 PyTrace_RETURN
, retval
)) {
2641 why
= WHY_EXCEPTION
;
2644 else if (why
== WHY_EXCEPTION
) {
2645 call_trace_protected(tstate
->c_tracefunc
,
2646 tstate
->c_traceobj
, f
,
2647 PyTrace_RETURN
, NULL
);
2650 if (tstate
->c_profilefunc
) {
2651 if (why
== WHY_EXCEPTION
)
2652 call_trace_protected(tstate
->c_profilefunc
,
2653 tstate
->c_profileobj
, f
,
2654 PyTrace_RETURN
, NULL
);
2655 else if (call_trace(tstate
->c_profilefunc
,
2656 tstate
->c_profileobj
, f
,
2657 PyTrace_RETURN
, retval
)) {
2660 why
= WHY_EXCEPTION
;
2665 if (tstate
->frame
->f_exc_type
!= NULL
)
2666 reset_exc_info(tstate
);
2668 assert(tstate
->frame
->f_exc_value
== NULL
);
2669 assert(tstate
->frame
->f_exc_traceback
== NULL
);
2674 Py_LeaveRecursiveCall();
2675 tstate
->frame
= f
->f_back
;
2680 /* This is gonna seem *real weird*, but if you put some other code between
2681 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2682 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2685 PyEval_EvalCodeEx(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
,
2686 PyObject
**args
, int argcount
, PyObject
**kws
, int kwcount
,
2687 PyObject
**defs
, int defcount
, PyObject
*closure
)
2689 register PyFrameObject
*f
;
2690 register PyObject
*retval
= NULL
;
2691 register PyObject
**fastlocals
, **freevars
;
2692 PyThreadState
*tstate
= PyThreadState_GET();
2695 if (globals
== NULL
) {
2696 PyErr_SetString(PyExc_SystemError
,
2697 "PyEval_EvalCodeEx: NULL globals");
2701 assert(tstate
!= NULL
);
2702 assert(globals
!= NULL
);
2703 f
= PyFrame_New(tstate
, co
, globals
, locals
);
2707 fastlocals
= f
->f_localsplus
;
2708 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
2710 if (co
->co_argcount
> 0 ||
2711 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
2714 PyObject
*kwdict
= NULL
;
2715 if (co
->co_flags
& CO_VARKEYWORDS
) {
2716 kwdict
= PyDict_New();
2719 i
= co
->co_argcount
;
2720 if (co
->co_flags
& CO_VARARGS
)
2722 SETLOCAL(i
, kwdict
);
2724 if (argcount
> co
->co_argcount
) {
2725 if (!(co
->co_flags
& CO_VARARGS
)) {
2726 PyErr_Format(PyExc_TypeError
,
2727 "%.200s() takes %s %d "
2728 "%sargument%s (%d given)",
2729 PyBytes_AsString(co
->co_name
),
2730 defcount
? "at most" : "exactly",
2732 kwcount
? "non-keyword " : "",
2733 co
->co_argcount
== 1 ? "" : "s",
2737 n
= co
->co_argcount
;
2739 for (i
= 0; i
< n
; i
++) {
2744 if (co
->co_flags
& CO_VARARGS
) {
2745 u
= PyTuple_New(argcount
- n
);
2748 SETLOCAL(co
->co_argcount
, u
);
2749 for (i
= n
; i
< argcount
; i
++) {
2752 PyTuple_SET_ITEM(u
, i
-n
, x
);
2755 for (i
= 0; i
< kwcount
; i
++) {
2756 PyObject
*keyword
= kws
[2*i
];
2757 PyObject
*value
= kws
[2*i
+ 1];
2759 if (keyword
== NULL
|| !PyBytes_Check(keyword
)) {
2760 PyErr_Format(PyExc_TypeError
,
2761 "%.200s() keywords must be strings",
2762 PyBytes_AsString(co
->co_name
));
2765 /* XXX slow -- speed up using dictionary? */
2766 for (j
= 0; j
< co
->co_argcount
; j
++) {
2767 PyObject
*nm
= PyTuple_GET_ITEM(
2768 co
->co_varnames
, j
);
2769 int cmp
= PyObject_RichCompareBool(
2770 keyword
, nm
, Py_EQ
);
2776 /* Check errors from Compare */
2777 if (PyErr_Occurred())
2779 if (j
>= co
->co_argcount
) {
2780 if (kwdict
== NULL
) {
2781 PyErr_Format(PyExc_TypeError
,
2782 "%.200s() got an unexpected "
2783 "keyword argument '%.400s'",
2784 PyBytes_AsString(co
->co_name
),
2785 PyBytes_AsString(keyword
));
2788 PyDict_SetItem(kwdict
, keyword
, value
);
2791 if (GETLOCAL(j
) != NULL
) {
2792 PyErr_Format(PyExc_TypeError
,
2793 "%.200s() got multiple "
2794 "values for keyword "
2795 "argument '%.400s'",
2796 PyBytes_AsString(co
->co_name
),
2797 PyBytes_AsString(keyword
));
2804 if (argcount
< co
->co_argcount
) {
2805 int m
= co
->co_argcount
- defcount
;
2806 for (i
= argcount
; i
< m
; i
++) {
2807 if (GETLOCAL(i
) == NULL
) {
2808 PyErr_Format(PyExc_TypeError
,
2809 "%.200s() takes %s %d "
2810 "%sargument%s (%d given)",
2811 PyBytes_AsString(co
->co_name
),
2812 ((co
->co_flags
& CO_VARARGS
) ||
2813 defcount
) ? "at least"
2815 m
, kwcount
? "non-keyword " : "",
2816 m
== 1 ? "" : "s", i
);
2824 for (; i
< defcount
; i
++) {
2825 if (GETLOCAL(m
+i
) == NULL
) {
2826 PyObject
*def
= defs
[i
];
2834 if (argcount
> 0 || kwcount
> 0) {
2835 PyErr_Format(PyExc_TypeError
,
2836 "%.200s() takes no arguments (%d given)",
2837 PyBytes_AsString(co
->co_name
),
2838 argcount
+ kwcount
);
2842 /* Allocate and initialize storage for cell vars, and copy free
2843 vars into frame. This isn't too efficient right now. */
2844 if (PyTuple_GET_SIZE(co
->co_cellvars
)) {
2845 int i
, j
, nargs
, found
;
2846 char *cellname
, *argname
;
2849 nargs
= co
->co_argcount
;
2850 if (co
->co_flags
& CO_VARARGS
)
2852 if (co
->co_flags
& CO_VARKEYWORDS
)
2855 /* Initialize each cell var, taking into account
2856 cell vars that are initialized from arguments.
2858 Should arrange for the compiler to put cellvars
2859 that are arguments at the beginning of the cellvars
2860 list so that we can march over it more efficiently?
2862 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_cellvars
); ++i
) {
2863 cellname
= PyBytes_AS_STRING(
2864 PyTuple_GET_ITEM(co
->co_cellvars
, i
));
2866 for (j
= 0; j
< nargs
; j
++) {
2867 argname
= PyBytes_AS_STRING(
2868 PyTuple_GET_ITEM(co
->co_varnames
, j
));
2869 if (strcmp(cellname
, argname
) == 0) {
2870 c
= PyCell_New(GETLOCAL(j
));
2873 GETLOCAL(co
->co_nlocals
+ i
) = c
;
2879 c
= PyCell_New(NULL
);
2882 SETLOCAL(co
->co_nlocals
+ i
, c
);
2886 if (PyTuple_GET_SIZE(co
->co_freevars
)) {
2888 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_freevars
); ++i
) {
2889 PyObject
*o
= PyTuple_GET_ITEM(closure
, i
);
2891 freevars
[PyTuple_GET_SIZE(co
->co_cellvars
) + i
] = o
;
2895 if (co
->co_flags
& CO_GENERATOR
) {
2896 /* Don't need to keep the reference to f_back, it will be set
2897 * when the generator is resumed. */
2898 Py_XDECREF(f
->f_back
);
2901 PCALL(PCALL_GENERATOR
);
2903 /* Create a new generator that owns the ready to run frame
2904 * and return that as the value. */
2905 return PyGen_New(f
);
2908 retval
= PyEval_EvalFrameEx(f
,0);
2910 fail
: /* Jump here from prelude on failure */
2912 /* decref'ing the frame can cause __del__ methods to get invoked,
2913 which can call back into Python. While we're done with the
2914 current Python frame (f), the associated C stack is still in use,
2915 so recursion_depth must be boosted for the duration.
2917 assert(tstate
!= NULL
);
2918 ++tstate
->recursion_depth
;
2920 --tstate
->recursion_depth
;
2925 /* Implementation notes for set_exc_info() and reset_exc_info():
2927 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2928 'exc_traceback'. These always travel together.
2930 - tstate->curexc_ZZZ is the "hot" exception that is set by
2931 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2933 - Once an exception is caught by an except clause, it is transferred
2934 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2935 can pick it up. This is the primary task of set_exc_info().
2936 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
2938 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
2940 Long ago, when none of this existed, there were just a few globals:
2941 one set corresponding to the "hot" exception, and one set
2942 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2943 globals; they were simply stored as sys.exc_ZZZ. For backwards
2944 compatibility, they still are!) The problem was that in code like
2948 "something that may fail"
2949 except "some exception":
2950 "do something else first"
2951 "print the exception from sys.exc_ZZZ."
2953 if "do something else first" invoked something that raised and caught
2954 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2955 cause of subtle bugs. I fixed this by changing the semantics as
2958 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2961 - But initially, and as long as no exception is caught in a given
2962 frame, sys.exc_ZZZ will hold the last exception caught in the
2963 previous frame (or the frame before that, etc.).
2965 The first bullet fixed the bug in the above example. The second
2966 bullet was for backwards compatibility: it was (and is) common to
2967 have a function that is called when an exception is caught, and to
2968 have that function access the caught exception via sys.exc_ZZZ.
2969 (Example: traceback.print_exc()).
2971 At the same time I fixed the problem that sys.exc_ZZZ weren't
2972 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2973 but that's really a separate improvement.
2975 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2976 variables to what they were before the current frame was called. The
2977 set_exc_info() function saves them on the frame so that
2978 reset_exc_info() can restore them. The invariant is that
2979 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2980 exception (where "catching" an exception applies only to successful
2981 except clauses); and if the current frame ever caught an exception,
2982 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2983 at the start of the current frame.
2988 set_exc_info(PyThreadState
*tstate
,
2989 PyObject
*type
, PyObject
*value
, PyObject
*tb
)
2991 PyFrameObject
*frame
= tstate
->frame
;
2992 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
2994 assert(type
!= NULL
);
2995 assert(frame
!= NULL
);
2996 if (frame
->f_exc_type
== NULL
) {
2997 assert(frame
->f_exc_value
== NULL
);
2998 assert(frame
->f_exc_traceback
== NULL
);
2999 /* This frame didn't catch an exception before. */
3000 /* Save previous exception of this thread in this frame. */
3001 if (tstate
->exc_type
== NULL
) {
3002 /* XXX Why is this set to Py_None? */
3004 tstate
->exc_type
= Py_None
;
3006 Py_INCREF(tstate
->exc_type
);
3007 Py_XINCREF(tstate
->exc_value
);
3008 Py_XINCREF(tstate
->exc_traceback
);
3009 frame
->f_exc_type
= tstate
->exc_type
;
3010 frame
->f_exc_value
= tstate
->exc_value
;
3011 frame
->f_exc_traceback
= tstate
->exc_traceback
;
3013 /* Set new exception for this thread. */
3014 tmp_type
= tstate
->exc_type
;
3015 tmp_value
= tstate
->exc_value
;
3016 tmp_tb
= tstate
->exc_traceback
;
3020 tstate
->exc_type
= type
;
3021 tstate
->exc_value
= value
;
3022 tstate
->exc_traceback
= tb
;
3023 Py_XDECREF(tmp_type
);
3024 Py_XDECREF(tmp_value
);
3026 /* For b/w compatibility */
3027 PySys_SetObject("exc_type", type
);
3028 PySys_SetObject("exc_value", value
);
3029 PySys_SetObject("exc_traceback", tb
);
3033 reset_exc_info(PyThreadState
*tstate
)
3035 PyFrameObject
*frame
;
3036 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
3038 /* It's a precondition that the thread state's frame caught an
3039 * exception -- verify in a debug build.
3041 assert(tstate
!= NULL
);
3042 frame
= tstate
->frame
;
3043 assert(frame
!= NULL
);
3044 assert(frame
->f_exc_type
!= NULL
);
3046 /* Copy the frame's exception info back to the thread state. */
3047 tmp_type
= tstate
->exc_type
;
3048 tmp_value
= tstate
->exc_value
;
3049 tmp_tb
= tstate
->exc_traceback
;
3050 Py_INCREF(frame
->f_exc_type
);
3051 Py_XINCREF(frame
->f_exc_value
);
3052 Py_XINCREF(frame
->f_exc_traceback
);
3053 tstate
->exc_type
= frame
->f_exc_type
;
3054 tstate
->exc_value
= frame
->f_exc_value
;
3055 tstate
->exc_traceback
= frame
->f_exc_traceback
;
3056 Py_XDECREF(tmp_type
);
3057 Py_XDECREF(tmp_value
);
3060 /* For b/w compatibility */
3061 PySys_SetObject("exc_type", frame
->f_exc_type
);
3062 PySys_SetObject("exc_value", frame
->f_exc_value
);
3063 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
3065 /* Clear the frame's exception info. */
3066 tmp_type
= frame
->f_exc_type
;
3067 tmp_value
= frame
->f_exc_value
;
3068 tmp_tb
= frame
->f_exc_traceback
;
3069 frame
->f_exc_type
= NULL
;
3070 frame
->f_exc_value
= NULL
;
3071 frame
->f_exc_traceback
= NULL
;
3072 Py_DECREF(tmp_type
);
3073 Py_XDECREF(tmp_value
);
3077 /* Logic for the raise statement (too complicated for inlining).
3078 This *consumes* a reference count to each of its arguments. */
3079 static enum why_code
3080 do_raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3084 PyThreadState
*tstate
= PyThreadState_GET();
3085 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
3086 value
= tstate
->exc_value
;
3087 tb
= tstate
->exc_traceback
;
3093 /* We support the following forms of raise:
3094 raise <class>, <classinstance>
3095 raise <class>, <argument tuple>
3097 raise <class>, <argument>
3098 raise <classinstance>, None
3099 raise <string>, <object>
3100 raise <string>, None
3102 An omitted second argument is the same as None.
3104 In addition, raise <tuple>, <anything> is the same as
3105 raising the tuple's first item (and it better have one!);
3106 this rule is applied recursively.
3108 Finally, an optional third argument can be supplied, which
3109 gives the traceback to be substituted (useful when
3110 re-raising an exception after examining it). */
3112 /* First, check the traceback argument, replacing None with
3114 if (tb
== Py_None
) {
3118 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
3119 PyErr_SetString(PyExc_TypeError
,
3120 "raise: arg 3 must be a traceback or None");
3124 /* Next, replace a missing value with None */
3125 if (value
== NULL
) {
3130 /* Next, repeatedly, replace a tuple exception with its first item */
3131 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
3132 PyObject
*tmp
= type
;
3133 type
= PyTuple_GET_ITEM(type
, 0);
3138 if (PyExceptionClass_Check(type
))
3139 PyErr_NormalizeException(&type
, &value
, &tb
);
3141 else if (PyExceptionInstance_Check(type
)) {
3142 /* Raising an instance. The value should be a dummy. */
3143 if (value
!= Py_None
) {
3144 PyErr_SetString(PyExc_TypeError
,
3145 "instance exception may not have a separate value");
3149 /* Normalize to raise <class>, <instance> */
3152 type
= PyExceptionInstance_Class(type
);
3157 /* Not something you can raise. You get an exception
3158 anyway, just not what you specified :-) */
3159 PyErr_Format(PyExc_TypeError
,
3160 "exceptions must be classes or instances, not %s",
3161 type
->ob_type
->tp_name
);
3165 assert(PyExceptionClass_Check(type
));
3166 if (Py_Py3kWarningFlag
&& PyClass_Check(type
)) {
3167 if (PyErr_WarnEx(PyExc_DeprecationWarning
,
3168 "exceptions must derive from BaseException "
3173 PyErr_Restore(type
, value
, tb
);
3175 return WHY_EXCEPTION
;
3182 return WHY_EXCEPTION
;
3185 /* Iterate v argcnt times and store the results on the stack (via decreasing
3186 sp). Return 1 for success, 0 if error. */
3189 unpack_iterable(PyObject
*v
, int argcnt
, PyObject
**sp
)
3192 PyObject
*it
; /* iter(v) */
3197 it
= PyObject_GetIter(v
);
3201 for (; i
< argcnt
; i
++) {
3202 w
= PyIter_Next(it
);
3204 /* Iterator done, via error or exhaustion. */
3205 if (!PyErr_Occurred()) {
3206 PyErr_Format(PyExc_ValueError
,
3207 "need more than %d value%s to unpack",
3208 i
, i
== 1 ? "" : "s");
3215 /* We better have exhausted the iterator now. */
3216 w
= PyIter_Next(it
);
3218 if (PyErr_Occurred())
3224 PyErr_SetString(PyExc_ValueError
, "too many values to unpack");
3227 for (; i
> 0; i
--, sp
++)
3236 prtrace(PyObject
*v
, char *str
)
3239 if (PyObject_Print(v
, stdout
, 0) != 0)
3240 PyErr_Clear(); /* Don't know what else to do */
3247 call_exc_trace(Py_tracefunc func
, PyObject
*self
, PyFrameObject
*f
)
3249 PyObject
*type
, *value
, *traceback
, *arg
;
3251 PyErr_Fetch(&type
, &value
, &traceback
);
3252 if (value
== NULL
) {
3256 arg
= PyTuple_Pack(3, type
, value
, traceback
);
3258 PyErr_Restore(type
, value
, traceback
);
3261 err
= call_trace(func
, self
, f
, PyTrace_EXCEPTION
, arg
);
3264 PyErr_Restore(type
, value
, traceback
);
3268 Py_XDECREF(traceback
);
3273 call_trace_protected(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3274 int what
, PyObject
*arg
)
3276 PyObject
*type
, *value
, *traceback
;
3278 PyErr_Fetch(&type
, &value
, &traceback
);
3279 err
= call_trace(func
, obj
, frame
, what
, arg
);
3282 PyErr_Restore(type
, value
, traceback
);
3288 Py_XDECREF(traceback
);
3294 call_trace(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3295 int what
, PyObject
*arg
)
3297 register PyThreadState
*tstate
= frame
->f_tstate
;
3299 if (tstate
->tracing
)
3302 tstate
->use_tracing
= 0;
3303 result
= func(obj
, frame
, what
, arg
);
3304 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3305 || (tstate
->c_profilefunc
!= NULL
));
3311 _PyEval_CallTracing(PyObject
*func
, PyObject
*args
)
3313 PyFrameObject
*frame
= PyEval_GetFrame();
3314 PyThreadState
*tstate
= frame
->f_tstate
;
3315 int save_tracing
= tstate
->tracing
;
3316 int save_use_tracing
= tstate
->use_tracing
;
3319 tstate
->tracing
= 0;
3320 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3321 || (tstate
->c_profilefunc
!= NULL
));
3322 result
= PyObject_Call(func
, args
, NULL
);
3323 tstate
->tracing
= save_tracing
;
3324 tstate
->use_tracing
= save_use_tracing
;
3329 maybe_call_line_trace(Py_tracefunc func
, PyObject
*obj
,
3330 PyFrameObject
*frame
, int *instr_lb
, int *instr_ub
,
3335 /* If the last instruction executed isn't in the current
3336 instruction window, reset the window. If the last
3337 instruction happens to fall at the start of a line or if it
3338 represents a jump backwards, call the trace function.
3340 if ((frame
->f_lasti
< *instr_lb
|| frame
->f_lasti
>= *instr_ub
)) {
3344 line
= PyCode_CheckLineNumber(frame
->f_code
, frame
->f_lasti
,
3347 frame
->f_lineno
= line
;
3348 result
= call_trace(func
, obj
, frame
,
3349 PyTrace_LINE
, Py_None
);
3351 *instr_lb
= bounds
.ap_lower
;
3352 *instr_ub
= bounds
.ap_upper
;
3354 else if (frame
->f_lasti
<= *instr_prev
) {
3355 result
= call_trace(func
, obj
, frame
, PyTrace_LINE
, Py_None
);
3357 *instr_prev
= frame
->f_lasti
;
3362 PyEval_SetProfile(Py_tracefunc func
, PyObject
*arg
)
3364 PyThreadState
*tstate
= PyThreadState_GET();
3365 PyObject
*temp
= tstate
->c_profileobj
;
3367 tstate
->c_profilefunc
= NULL
;
3368 tstate
->c_profileobj
= NULL
;
3369 /* Must make sure that tracing is not ignored if 'temp' is freed */
3370 tstate
->use_tracing
= tstate
->c_tracefunc
!= NULL
;
3372 tstate
->c_profilefunc
= func
;
3373 tstate
->c_profileobj
= arg
;
3374 /* Flag that tracing or profiling is turned on */
3375 tstate
->use_tracing
= (func
!= NULL
) || (tstate
->c_tracefunc
!= NULL
);
3379 PyEval_SetTrace(Py_tracefunc func
, PyObject
*arg
)
3381 PyThreadState
*tstate
= PyThreadState_GET();
3382 PyObject
*temp
= tstate
->c_traceobj
;
3384 tstate
->c_tracefunc
= NULL
;
3385 tstate
->c_traceobj
= NULL
;
3386 /* Must make sure that profiling is not ignored if 'temp' is freed */
3387 tstate
->use_tracing
= tstate
->c_profilefunc
!= NULL
;
3389 tstate
->c_tracefunc
= func
;
3390 tstate
->c_traceobj
= arg
;
3391 /* Flag that tracing or profiling is turned on */
3392 tstate
->use_tracing
= ((func
!= NULL
)
3393 || (tstate
->c_profilefunc
!= NULL
));
3397 PyEval_GetBuiltins(void)
3399 PyFrameObject
*current_frame
= PyEval_GetFrame();
3400 if (current_frame
== NULL
)
3401 return PyThreadState_GET()->interp
->builtins
;
3403 return current_frame
->f_builtins
;
3407 PyEval_GetLocals(void)
3409 PyFrameObject
*current_frame
= PyEval_GetFrame();
3410 if (current_frame
== NULL
)
3412 PyFrame_FastToLocals(current_frame
);
3413 return current_frame
->f_locals
;
3417 PyEval_GetGlobals(void)
3419 PyFrameObject
*current_frame
= PyEval_GetFrame();
3420 if (current_frame
== NULL
)
3423 return current_frame
->f_globals
;
3427 PyEval_GetFrame(void)
3429 PyThreadState
*tstate
= PyThreadState_GET();
3430 return _PyThreadState_GetFrame(tstate
);
3434 PyEval_GetRestricted(void)
3436 PyFrameObject
*current_frame
= PyEval_GetFrame();
3437 return current_frame
== NULL
? 0 : PyFrame_IsRestricted(current_frame
);
3441 PyEval_MergeCompilerFlags(PyCompilerFlags
*cf
)
3443 PyFrameObject
*current_frame
= PyEval_GetFrame();
3444 int result
= cf
->cf_flags
!= 0;
3446 if (current_frame
!= NULL
) {
3447 const int codeflags
= current_frame
->f_code
->co_flags
;
3448 const int compilerflags
= codeflags
& PyCF_MASK
;
3449 if (compilerflags
) {
3451 cf
->cf_flags
|= compilerflags
;
3453 #if 0 /* future keyword */
3454 if (codeflags
& CO_GENERATOR_ALLOWED
) {
3456 cf
->cf_flags
|= CO_GENERATOR_ALLOWED
;
3466 PyObject
*f
= PySys_GetObject("stdout");
3469 if (!PyFile_SoftSpace(f
, 0))
3471 return PyFile_WriteString("\n", f
);
3475 /* External interface to call any callable object.
3476 The arg must be a tuple or NULL. */
3478 #undef PyEval_CallObject
3479 /* for backward compatibility: export this interface */
3482 PyEval_CallObject(PyObject
*func
, PyObject
*arg
)
3484 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
3486 #define PyEval_CallObject(func,arg) \
3487 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3490 PyEval_CallObjectWithKeywords(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
3495 arg
= PyTuple_New(0);
3499 else if (!PyTuple_Check(arg
)) {
3500 PyErr_SetString(PyExc_TypeError
,
3501 "argument list must be a tuple");
3507 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
3508 PyErr_SetString(PyExc_TypeError
,
3509 "keyword list must be a dictionary");
3514 result
= PyObject_Call(func
, arg
, kw
);
3520 PyEval_GetFuncName(PyObject
*func
)
3522 if (PyMethod_Check(func
))
3523 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func
));
3524 else if (PyFunction_Check(func
))
3525 return PyBytes_AsString(((PyFunctionObject
*)func
)->func_name
);
3526 else if (PyCFunction_Check(func
))
3527 return ((PyCFunctionObject
*)func
)->m_ml
->ml_name
;
3528 else if (PyClass_Check(func
))
3529 return PyBytes_AsString(((PyClassObject
*)func
)->cl_name
);
3530 else if (PyInstance_Check(func
)) {
3531 return PyBytes_AsString(
3532 ((PyInstanceObject
*)func
)->in_class
->cl_name
);
3534 return func
->ob_type
->tp_name
;
3539 PyEval_GetFuncDesc(PyObject
*func
)
3541 if (PyMethod_Check(func
))
3543 else if (PyFunction_Check(func
))
3545 else if (PyCFunction_Check(func
))
3547 else if (PyClass_Check(func
))
3548 return " constructor";
3549 else if (PyInstance_Check(func
)) {
3557 err_args(PyObject
*func
, int flags
, int nargs
)
3559 if (flags
& METH_NOARGS
)
3560 PyErr_Format(PyExc_TypeError
,
3561 "%.200s() takes no arguments (%d given)",
3562 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3565 PyErr_Format(PyExc_TypeError
,
3566 "%.200s() takes exactly one argument (%d given)",
3567 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3571 #define C_TRACE(x, call) \
3572 if (tstate->use_tracing && tstate->c_profilefunc) { \
3573 if (call_trace(tstate->c_profilefunc, \
3574 tstate->c_profileobj, \
3575 tstate->frame, PyTrace_C_CALL, \
3581 if (tstate->c_profilefunc != NULL) { \
3583 call_trace_protected(tstate->c_profilefunc, \
3584 tstate->c_profileobj, \
3585 tstate->frame, PyTrace_C_EXCEPTION, \
3587 /* XXX should pass (type, value, tb) */ \
3589 if (call_trace(tstate->c_profilefunc, \
3590 tstate->c_profileobj, \
3591 tstate->frame, PyTrace_C_RETURN, \
3604 call_function(PyObject
***pp_stack
, int oparg
3606 , uint64
* pintr0
, uint64
* pintr1
3610 int na
= oparg
& 0xff;
3611 int nk
= (oparg
>>8) & 0xff;
3612 int n
= na
+ 2 * nk
;
3613 PyObject
**pfunc
= (*pp_stack
) - n
- 1;
3614 PyObject
*func
= *pfunc
;
3617 /* Always dispatch PyCFunction first, because these are
3618 presumed to be the most frequent callable object.
3620 if (PyCFunction_Check(func
) && nk
== 0) {
3621 int flags
= PyCFunction_GET_FLAGS(func
);
3622 PyThreadState
*tstate
= PyThreadState_GET();
3624 PCALL(PCALL_CFUNCTION
);
3625 if (flags
& (METH_NOARGS
| METH_O
)) {
3626 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
3627 PyObject
*self
= PyCFunction_GET_SELF(func
);
3628 if (flags
& METH_NOARGS
&& na
== 0) {
3629 C_TRACE(x
, (*meth
)(self
,NULL
));
3631 else if (flags
& METH_O
&& na
== 1) {
3632 PyObject
*arg
= EXT_POP(*pp_stack
);
3633 C_TRACE(x
, (*meth
)(self
,arg
));
3637 err_args(func
, flags
, na
);
3643 callargs
= load_args(pp_stack
, na
);
3644 READ_TIMESTAMP(*pintr0
);
3645 C_TRACE(x
, PyCFunction_Call(func
,callargs
,NULL
));
3646 READ_TIMESTAMP(*pintr1
);
3647 Py_XDECREF(callargs
);
3650 if (PyMethod_Check(func
) && PyMethod_GET_SELF(func
) != NULL
) {
3651 /* optimize access to bound methods */
3652 PyObject
*self
= PyMethod_GET_SELF(func
);
3653 PCALL(PCALL_METHOD
);
3654 PCALL(PCALL_BOUND_METHOD
);
3656 func
= PyMethod_GET_FUNCTION(func
);
3664 READ_TIMESTAMP(*pintr0
);
3665 if (PyFunction_Check(func
))
3666 x
= fast_function(func
, pp_stack
, n
, na
, nk
);
3668 x
= do_call(func
, pp_stack
, na
, nk
);
3669 READ_TIMESTAMP(*pintr1
);
3673 /* Clear the stack of the function object. Also removes
3674 the arguments in case they weren't consumed already
3675 (fast_function() and err_args() leave them on the stack).
3677 while ((*pp_stack
) > pfunc
) {
3678 w
= EXT_POP(*pp_stack
);
3685 /* The fast_function() function optimize calls for which no argument
3686 tuple is necessary; the objects are passed directly from the stack.
3687 For the simplest case -- a function that takes only positional
3688 arguments and is called with only positional arguments -- it
3689 inlines the most primitive frame setup code from
3690 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3691 done before evaluating the frame.
3695 fast_function(PyObject
*func
, PyObject
***pp_stack
, int n
, int na
, int nk
)
3697 PyCodeObject
*co
= (PyCodeObject
*)PyFunction_GET_CODE(func
);
3698 PyObject
*globals
= PyFunction_GET_GLOBALS(func
);
3699 PyObject
*argdefs
= PyFunction_GET_DEFAULTS(func
);
3700 PyObject
**d
= NULL
;
3703 PCALL(PCALL_FUNCTION
);
3704 PCALL(PCALL_FAST_FUNCTION
);
3705 if (argdefs
== NULL
&& co
->co_argcount
== n
&& nk
==0 &&
3706 co
->co_flags
== (CO_OPTIMIZED
| CO_NEWLOCALS
| CO_NOFREE
)) {
3708 PyObject
*retval
= NULL
;
3709 PyThreadState
*tstate
= PyThreadState_GET();
3710 PyObject
**fastlocals
, **stack
;
3713 PCALL(PCALL_FASTER_FUNCTION
);
3714 assert(globals
!= NULL
);
3715 /* XXX Perhaps we should create a specialized
3716 PyFrame_New() that doesn't take locals, but does
3717 take builtins without sanity checking them.
3719 assert(tstate
!= NULL
);
3720 f
= PyFrame_New(tstate
, co
, globals
, NULL
);
3724 fastlocals
= f
->f_localsplus
;
3725 stack
= (*pp_stack
) - n
;
3727 for (i
= 0; i
< n
; i
++) {
3729 fastlocals
[i
] = *stack
++;
3731 retval
= PyEval_EvalFrameEx(f
,0);
3732 ++tstate
->recursion_depth
;
3734 --tstate
->recursion_depth
;
3737 if (argdefs
!= NULL
) {
3738 d
= &PyTuple_GET_ITEM(argdefs
, 0);
3739 nd
= Py_SIZE(argdefs
);
3741 return PyEval_EvalCodeEx(co
, globals
,
3742 (PyObject
*)NULL
, (*pp_stack
)-n
, na
,
3743 (*pp_stack
)-2*nk
, nk
, d
, nd
,
3744 PyFunction_GET_CLOSURE(func
));
3748 update_keyword_args(PyObject
*orig_kwdict
, int nk
, PyObject
***pp_stack
,
3751 PyObject
*kwdict
= NULL
;
3752 if (orig_kwdict
== NULL
)
3753 kwdict
= PyDict_New();
3755 kwdict
= PyDict_Copy(orig_kwdict
);
3756 Py_DECREF(orig_kwdict
);
3762 PyObject
*value
= EXT_POP(*pp_stack
);
3763 PyObject
*key
= EXT_POP(*pp_stack
);
3764 if (PyDict_GetItem(kwdict
, key
) != NULL
) {
3765 PyErr_Format(PyExc_TypeError
,
3766 "%.200s%s got multiple values "
3767 "for keyword argument '%.200s'",
3768 PyEval_GetFuncName(func
),
3769 PyEval_GetFuncDesc(func
),
3770 PyBytes_AsString(key
));
3776 err
= PyDict_SetItem(kwdict
, key
, value
);
3788 update_star_args(int nstack
, int nstar
, PyObject
*stararg
,
3789 PyObject
***pp_stack
)
3791 PyObject
*callargs
, *w
;
3793 callargs
= PyTuple_New(nstack
+ nstar
);
3794 if (callargs
== NULL
) {
3799 for (i
= 0; i
< nstar
; i
++) {
3800 PyObject
*a
= PyTuple_GET_ITEM(stararg
, i
);
3802 PyTuple_SET_ITEM(callargs
, nstack
+ i
, a
);
3805 while (--nstack
>= 0) {
3806 w
= EXT_POP(*pp_stack
);
3807 PyTuple_SET_ITEM(callargs
, nstack
, w
);
3813 load_args(PyObject
***pp_stack
, int na
)
3815 PyObject
*args
= PyTuple_New(na
);
3821 w
= EXT_POP(*pp_stack
);
3822 PyTuple_SET_ITEM(args
, na
, w
);
3828 do_call(PyObject
*func
, PyObject
***pp_stack
, int na
, int nk
)
3830 PyObject
*callargs
= NULL
;
3831 PyObject
*kwdict
= NULL
;
3832 PyObject
*result
= NULL
;
3835 kwdict
= update_keyword_args(NULL
, nk
, pp_stack
, func
);
3839 callargs
= load_args(pp_stack
, na
);
3840 if (callargs
== NULL
)
3843 /* At this point, we have to look at the type of func to
3844 update the call stats properly. Do it here so as to avoid
3845 exposing the call stats machinery outside ceval.c
3847 if (PyFunction_Check(func
))
3848 PCALL(PCALL_FUNCTION
);
3849 else if (PyMethod_Check(func
))
3850 PCALL(PCALL_METHOD
);
3851 else if (PyType_Check(func
))
3856 result
= PyObject_Call(func
, callargs
, kwdict
);
3858 Py_XDECREF(callargs
);
3864 ext_do_call(PyObject
*func
, PyObject
***pp_stack
, int flags
, int na
, int nk
)
3867 PyObject
*callargs
= NULL
;
3868 PyObject
*stararg
= NULL
;
3869 PyObject
*kwdict
= NULL
;
3870 PyObject
*result
= NULL
;
3872 if (flags
& CALL_FLAG_KW
) {
3873 kwdict
= EXT_POP(*pp_stack
);
3874 if (!PyDict_Check(kwdict
)) {
3879 if (PyDict_Update(d
, kwdict
) != 0) {
3881 /* PyDict_Update raises attribute
3882 * error (percolated from an attempt
3883 * to get 'keys' attribute) instead of
3884 * a type error if its second argument
3887 if (PyErr_ExceptionMatches(PyExc_AttributeError
)) {
3888 PyErr_Format(PyExc_TypeError
,
3889 "%.200s%.200s argument after ** "
3890 "must be a mapping, not %.200s",
3891 PyEval_GetFuncName(func
),
3892 PyEval_GetFuncDesc(func
),
3893 kwdict
->ob_type
->tp_name
);
3901 if (flags
& CALL_FLAG_VAR
) {
3902 stararg
= EXT_POP(*pp_stack
);
3903 if (!PyTuple_Check(stararg
)) {
3905 t
= PySequence_Tuple(stararg
);
3907 if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
3908 PyErr_Format(PyExc_TypeError
,
3909 "%.200s%.200s argument after * "
3910 "must be a sequence, not %200s",
3911 PyEval_GetFuncName(func
),
3912 PyEval_GetFuncDesc(func
),
3913 stararg
->ob_type
->tp_name
);
3920 nstar
= PyTuple_GET_SIZE(stararg
);
3923 kwdict
= update_keyword_args(kwdict
, nk
, pp_stack
, func
);
3927 callargs
= update_star_args(na
, nstar
, stararg
, pp_stack
);
3928 if (callargs
== NULL
)
3931 /* At this point, we have to look at the type of func to
3932 update the call stats properly. Do it here so as to avoid
3933 exposing the call stats machinery outside ceval.c
3935 if (PyFunction_Check(func
))
3936 PCALL(PCALL_FUNCTION
);
3937 else if (PyMethod_Check(func
))
3938 PCALL(PCALL_METHOD
);
3939 else if (PyType_Check(func
))
3944 result
= PyObject_Call(func
, callargs
, kwdict
);
3946 Py_XDECREF(callargs
);
3948 Py_XDECREF(stararg
);
3952 /* Extract a slice index from a PyInt or PyLong or an object with the
3953 nb_index slot defined, and store in *pi.
3954 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3955 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
3956 Return 0 on error, 1 on success.
3958 /* Note: If v is NULL, return success without storing into *pi. This
3959 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3960 called by the SLICE opcode with v and/or w equal to NULL.
3963 _PyEval_SliceIndex(PyObject
*v
, Py_ssize_t
*pi
)
3967 if (PyInt_Check(v
)) {
3968 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3969 however, it looks like it should be AsSsize_t.
3970 There should be a comment here explaining why.
3972 x
= PyInt_AS_LONG(v
);
3974 else if (PyIndex_Check(v
)) {
3975 x
= PyNumber_AsSsize_t(v
, NULL
);
3976 if (x
== -1 && PyErr_Occurred())
3980 PyErr_SetString(PyExc_TypeError
,
3981 "slice indices must be integers or "
3982 "None or have an __index__ method");
3991 #define ISINDEX(x) ((x) == NULL || \
3992 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
3995 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
3997 PyTypeObject
*tp
= u
->ob_type
;
3998 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4000 if (sq
&& sq
->sq_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4001 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4002 if (!_PyEval_SliceIndex(v
, &ilow
))
4004 if (!_PyEval_SliceIndex(w
, &ihigh
))
4006 return PySequence_GetSlice(u
, ilow
, ihigh
);
4009 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4010 if (slice
!= NULL
) {
4011 PyObject
*res
= PyObject_GetItem(u
, slice
);
4021 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
4024 PyTypeObject
*tp
= u
->ob_type
;
4025 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
4027 if (sq
&& sq
->sq_ass_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
4028 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
4029 if (!_PyEval_SliceIndex(v
, &ilow
))
4031 if (!_PyEval_SliceIndex(w
, &ihigh
))
4034 return PySequence_DelSlice(u
, ilow
, ihigh
);
4036 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
4039 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
4040 if (slice
!= NULL
) {
4043 res
= PyObject_SetItem(u
, slice
, x
);
4045 res
= PyObject_DelItem(u
, slice
);
4054 #define Py3kExceptionClass_Check(x) \
4055 (PyType_Check((x)) && \
4056 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4058 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4059 "BaseException is not allowed in 3.x"
4062 cmp_outcome(int op
, register PyObject
*v
, register PyObject
*w
)
4073 res
= PySequence_Contains(w
, v
);
4078 res
= PySequence_Contains(w
, v
);
4083 case PyCmp_EXC_MATCH
:
4084 if (PyTuple_Check(w
)) {
4085 Py_ssize_t i
, length
;
4086 length
= PyTuple_Size(w
);
4087 for (i
= 0; i
< length
; i
+= 1) {
4088 PyObject
*exc
= PyTuple_GET_ITEM(w
, i
);
4089 if (PyBytes_Check(exc
)) {
4091 ret_val
= PyErr_WarnEx(
4092 PyExc_DeprecationWarning
,
4093 "catching of string "
4094 "exceptions is deprecated", 1);
4098 else if (Py_Py3kWarningFlag
&&
4099 !PyTuple_Check(exc
) &&
4100 !Py3kExceptionClass_Check(exc
))
4103 ret_val
= PyErr_WarnEx(
4104 PyExc_DeprecationWarning
,
4105 CANNOT_CATCH_MSG
, 1);
4112 if (PyBytes_Check(w
)) {
4114 ret_val
= PyErr_WarnEx(
4115 PyExc_DeprecationWarning
,
4116 "catching of string "
4117 "exceptions is deprecated", 1);
4121 else if (Py_Py3kWarningFlag
&&
4122 !PyTuple_Check(w
) &&
4123 !Py3kExceptionClass_Check(w
))
4126 ret_val
= PyErr_WarnEx(
4127 PyExc_DeprecationWarning
,
4128 CANNOT_CATCH_MSG
, 1);
4133 res
= PyErr_GivenExceptionMatches(v
, w
);
4136 return PyObject_RichCompare(v
, w
, op
);
4138 v
= res
? Py_True
: Py_False
;
4144 import_from(PyObject
*v
, PyObject
*name
)
4148 x
= PyObject_GetAttr(v
, name
);
4149 if (x
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
4150 PyErr_Format(PyExc_ImportError
,
4151 "cannot import name %.230s",
4152 PyBytes_AsString(name
));
4158 import_all_from(PyObject
*locals
, PyObject
*v
)
4160 PyObject
*all
= PyObject_GetAttrString(v
, "__all__");
4161 PyObject
*dict
, *name
, *value
;
4162 int skip_leading_underscores
= 0;
4166 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4167 return -1; /* Unexpected error */
4169 dict
= PyObject_GetAttrString(v
, "__dict__");
4171 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4173 PyErr_SetString(PyExc_ImportError
,
4174 "from-import-* object has no __dict__ and no __all__");
4177 all
= PyMapping_Keys(dict
);
4181 skip_leading_underscores
= 1;
4184 for (pos
= 0, err
= 0; ; pos
++) {
4185 name
= PySequence_GetItem(all
, pos
);
4187 if (!PyErr_ExceptionMatches(PyExc_IndexError
))
4193 if (skip_leading_underscores
&&
4194 PyBytes_Check(name
) &&
4195 PyBytes_AS_STRING(name
)[0] == '_')
4200 value
= PyObject_GetAttr(v
, name
);
4203 else if (PyDict_CheckExact(locals
))
4204 err
= PyDict_SetItem(locals
, name
, value
);
4206 err
= PyObject_SetItem(locals
, name
, value
);
4217 build_class(PyObject
*methods
, PyObject
*bases
, PyObject
*name
)
4219 PyObject
*metaclass
= NULL
, *result
, *base
;
4221 if (PyDict_Check(methods
))
4222 metaclass
= PyDict_GetItemString(methods
, "__metaclass__");
4223 if (metaclass
!= NULL
)
4224 Py_INCREF(metaclass
);
4225 else if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
4226 base
= PyTuple_GET_ITEM(bases
, 0);
4227 metaclass
= PyObject_GetAttrString(base
, "__class__");
4228 if (metaclass
== NULL
) {
4230 metaclass
= (PyObject
*)base
->ob_type
;
4231 Py_INCREF(metaclass
);
4235 PyObject
*g
= PyEval_GetGlobals();
4236 if (g
!= NULL
&& PyDict_Check(g
))
4237 metaclass
= PyDict_GetItemString(g
, "__metaclass__");
4238 if (metaclass
== NULL
)
4239 metaclass
= (PyObject
*) &PyClass_Type
;
4240 Py_INCREF(metaclass
);
4242 result
= PyObject_CallFunctionObjArgs(metaclass
, name
, bases
, methods
,
4244 Py_DECREF(metaclass
);
4245 if (result
== NULL
&& PyErr_ExceptionMatches(PyExc_TypeError
)) {
4246 /* A type error here likely means that the user passed
4247 in a base that was not a class (such the random module
4248 instead of the random.random type). Help them out with
4249 by augmenting the error message with more information.*/
4251 PyObject
*ptype
, *pvalue
, *ptraceback
;
4253 PyErr_Fetch(&ptype
, &pvalue
, &ptraceback
);
4254 if (PyBytes_Check(pvalue
)) {
4256 newmsg
= PyBytes_FromFormat(
4257 "Error when calling the metaclass bases\n"
4259 PyBytes_AS_STRING(pvalue
));
4260 if (newmsg
!= NULL
) {
4265 PyErr_Restore(ptype
, pvalue
, ptraceback
);
4271 exec_statement(PyFrameObject
*f
, PyObject
*prog
, PyObject
*globals
,
4278 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
4279 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
4280 /* Backward compatibility hack */
4281 globals
= PyTuple_GetItem(prog
, 1);
4283 locals
= PyTuple_GetItem(prog
, 2);
4284 prog
= PyTuple_GetItem(prog
, 0);
4286 if (globals
== Py_None
) {
4287 globals
= PyEval_GetGlobals();
4288 if (locals
== Py_None
) {
4289 locals
= PyEval_GetLocals();
4292 if (!globals
|| !locals
) {
4293 PyErr_SetString(PyExc_SystemError
,
4294 "globals and locals cannot be NULL");
4298 else if (locals
== Py_None
)
4300 if (!PyBytes_Check(prog
) &&
4301 !PyUnicode_Check(prog
) &&
4302 !PyCode_Check(prog
) &&
4303 !PyFile_Check(prog
)) {
4304 PyErr_SetString(PyExc_TypeError
,
4305 "exec: arg 1 must be a string, file, or code object");
4308 if (!PyDict_Check(globals
)) {
4309 PyErr_SetString(PyExc_TypeError
,
4310 "exec: arg 2 must be a dictionary or None");
4313 if (!PyMapping_Check(locals
)) {
4314 PyErr_SetString(PyExc_TypeError
,
4315 "exec: arg 3 must be a mapping or None");
4318 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
4319 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
4320 if (PyCode_Check(prog
)) {
4321 if (PyCode_GetNumFree((PyCodeObject
*)prog
) > 0) {
4322 PyErr_SetString(PyExc_TypeError
,
4323 "code object passed to exec may not contain free variables");
4326 v
= PyEval_EvalCode((PyCodeObject
*) prog
, globals
, locals
);
4328 else if (PyFile_Check(prog
)) {
4329 FILE *fp
= PyFile_AsFile(prog
);
4330 char *name
= PyBytes_AsString(PyFile_Name(prog
));
4335 if (PyEval_MergeCompilerFlags(&cf
))
4336 v
= PyRun_FileFlags(fp
, name
, Py_file_input
, globals
,
4339 v
= PyRun_File(fp
, name
, Py_file_input
, globals
,
4343 PyObject
*tmp
= NULL
;
4347 #ifdef Py_USING_UNICODE
4348 if (PyUnicode_Check(prog
)) {
4349 tmp
= PyUnicode_AsUTF8String(prog
);
4353 cf
.cf_flags
|= PyCF_SOURCE_IS_UTF8
;
4356 if (PyBytes_AsStringAndSize(prog
, &str
, NULL
))
4358 if (PyEval_MergeCompilerFlags(&cf
))
4359 v
= PyRun_StringFlags(str
, Py_file_input
, globals
,
4362 v
= PyRun_String(str
, Py_file_input
, globals
, locals
);
4366 PyFrame_LocalsToFast(f
, 0);
4374 format_exc_check_arg(PyObject
*exc
, char *format_str
, PyObject
*obj
)
4381 obj_str
= PyBytes_AsString(obj
);
4385 PyErr_Format(exc
, format_str
, obj_str
);
4389 string_concatenate(PyObject
*v
, PyObject
*w
,
4390 PyFrameObject
*f
, unsigned char *next_instr
)
4392 /* This function implements 'variable += expr' when both arguments
4394 Py_ssize_t v_len
= PyBytes_GET_SIZE(v
);
4395 Py_ssize_t w_len
= PyBytes_GET_SIZE(w
);
4396 Py_ssize_t new_len
= v_len
+ w_len
;
4398 PyErr_SetString(PyExc_OverflowError
,
4399 "strings are too large to concat");
4403 if (v
->ob_refcnt
== 2) {
4404 /* In the common case, there are 2 references to the value
4405 * stored in 'variable' when the += is performed: one on the
4406 * value stack (in 'v') and one still stored in the
4407 * 'variable'. We try to delete the variable now to reduce
4410 switch (*next_instr
) {
4413 int oparg
= PEEKARG();
4414 PyObject
**fastlocals
= f
->f_localsplus
;
4415 if (GETLOCAL(oparg
) == v
)
4416 SETLOCAL(oparg
, NULL
);
4421 PyObject
**freevars
= (f
->f_localsplus
+
4422 f
->f_code
->co_nlocals
);
4423 PyObject
*c
= freevars
[PEEKARG()];
4424 if (PyCell_GET(c
) == v
)
4425 PyCell_Set(c
, NULL
);
4430 PyObject
*names
= f
->f_code
->co_names
;
4431 PyObject
*name
= GETITEM(names
, PEEKARG());
4432 PyObject
*locals
= f
->f_locals
;
4433 if (PyDict_CheckExact(locals
) &&
4434 PyDict_GetItem(locals
, name
) == v
) {
4435 if (PyDict_DelItem(locals
, name
) != 0) {
4444 if (v
->ob_refcnt
== 1 && !PyBytes_CHECK_INTERNED(v
)) {
4445 /* Now we own the last reference to 'v', so we can resize it
4448 if (_PyBytes_Resize(&v
, new_len
) != 0) {
4449 /* XXX if _PyBytes_Resize() fails, 'v' has been
4450 * deallocated so it cannot be put back into
4451 * 'variable'. The MemoryError is raised when there
4452 * is no value in 'variable', which might (very
4453 * remotely) be a cause of incompatibilities.
4457 /* copy 'w' into the newly allocated area of 'v' */
4458 memcpy(PyBytes_AS_STRING(v
) + v_len
,
4459 PyBytes_AS_STRING(w
), w_len
);
4463 /* When in-place resizing is not an option. */
4464 PyBytes_Concat(&v
, w
);
4469 #ifdef DYNAMIC_EXECUTION_PROFILE
4472 getarray(long a
[256])
4475 PyObject
*l
= PyList_New(256);
4476 if (l
== NULL
) return NULL
;
4477 for (i
= 0; i
< 256; i
++) {
4478 PyObject
*x
= PyInt_FromLong(a
[i
]);
4483 PyList_SetItem(l
, i
, x
);
4485 for (i
= 0; i
< 256; i
++)
4491 _Py_GetDXProfile(PyObject
*self
, PyObject
*args
)
4494 return getarray(dxp
);
4497 PyObject
*l
= PyList_New(257);
4498 if (l
== NULL
) return NULL
;
4499 for (i
= 0; i
< 257; i
++) {
4500 PyObject
*x
= getarray(dxpairs
[i
]);
4505 PyList_SetItem(l
, i
, x
);