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 platform,
32 irrespective of OS. POWER? Who knows :-) */
34 #define READ_TIMESTAMP(var) ppc_getcounter(&var)
37 ppc_getcounter(uint64
*v
)
39 register unsigned long tbu
, tb
, tbu2
;
42 asm volatile ("mftbu %0" : "=r" (tbu
) );
43 asm volatile ("mftb %0" : "=r" (tb
) );
44 asm volatile ("mftbu %0" : "=r" (tbu2
));
45 if (__builtin_expect(tbu
!= tbu2
, 0)) goto loop
;
47 /* The slightly peculiar way of writing the next lines is
48 compiled better by GCC than any other way I tried. */
49 ((long*)(v
))[0] = tbu
;
53 #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
55 #define READ_TIMESTAMP(val) \
56 __asm__ __volatile__("rdtsc" : "=A" (val))
60 void dump_tsc(int opcode
, int ticked
, uint64 inst0
, uint64 inst1
,
61 uint64 loop0
, uint64 loop1
, uint64 intr0
, uint64 intr1
)
63 uint64 intr
, inst
, loop
;
64 PyThreadState
*tstate
= PyThreadState_Get();
65 if (!tstate
->interp
->tscdump
)
68 inst
= inst1
- inst0
- intr
;
69 loop
= loop1
- loop0
- intr
;
70 fprintf(stderr
, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
71 opcode
, ticked
, inst
, loop
);
76 /* Turn this on if your compiler chokes on the big switch: */
77 /* #define CASE_TOO_BIG 1 */
80 /* For debugging the interpreter: */
81 #define LLTRACE 1 /* Low-level trace feature */
82 #define CHECKEXC 1 /* Double-check exception checking */
85 typedef PyObject
*(*callproc
)(PyObject
*, PyObject
*, PyObject
*);
87 /* Forward declarations */
89 static PyObject
* call_function(PyObject
***, int, uint64
*, uint64
*);
91 static PyObject
* call_function(PyObject
***, int);
93 static PyObject
* fast_function(PyObject
*, PyObject
***, int, int, int);
94 static PyObject
* do_call(PyObject
*, PyObject
***, int, int);
95 static PyObject
* ext_do_call(PyObject
*, PyObject
***, int, int, int);
96 static PyObject
* update_keyword_args(PyObject
*, int, PyObject
***,PyObject
*);
97 static PyObject
* update_star_args(int, int, PyObject
*, PyObject
***);
98 static PyObject
* load_args(PyObject
***, int);
99 #define CALL_FLAG_VAR 1
100 #define CALL_FLAG_KW 2
104 static int prtrace(PyObject
*, char *);
106 static int call_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*,
108 static void call_trace_protected(Py_tracefunc
, PyObject
*,
109 PyFrameObject
*, int, PyObject
*);
110 static void call_exc_trace(Py_tracefunc
, PyObject
*, PyFrameObject
*);
111 static int maybe_call_line_trace(Py_tracefunc
, PyObject
*,
112 PyFrameObject
*, int *, int *, int *);
114 static PyObject
* apply_slice(PyObject
*, PyObject
*, PyObject
*);
115 static int assign_slice(PyObject
*, PyObject
*,
116 PyObject
*, PyObject
*);
117 static PyObject
* cmp_outcome(int, PyObject
*, PyObject
*);
118 static PyObject
* import_from(PyObject
*, PyObject
*);
119 static int import_all_from(PyObject
*, PyObject
*);
120 static PyObject
* build_class(PyObject
*, PyObject
*, PyObject
*);
121 static int exec_statement(PyFrameObject
*,
122 PyObject
*, PyObject
*, PyObject
*);
123 static void set_exc_info(PyThreadState
*, PyObject
*, PyObject
*, PyObject
*);
124 static void reset_exc_info(PyThreadState
*);
125 static void format_exc_check_arg(PyObject
*, char *, PyObject
*);
126 static PyObject
* string_concatenate(PyObject
*, PyObject
*,
127 PyFrameObject
*, unsigned char *);
129 #define NAME_ERROR_MSG \
130 "name '%.200s' is not defined"
131 #define GLOBAL_NAME_ERROR_MSG \
132 "global name '%.200s' is not defined"
133 #define UNBOUNDLOCAL_ERROR_MSG \
134 "local variable '%.200s' referenced before assignment"
135 #define UNBOUNDFREE_ERROR_MSG \
136 "free variable '%.200s' referenced before assignment" \
137 " in enclosing scope"
139 /* Dynamic execution profile */
140 #ifdef DYNAMIC_EXECUTION_PROFILE
142 static long dxpairs
[257][256];
143 #define dxp dxpairs[256]
145 static long dxp
[256];
149 /* Function call profile */
152 static int pcall
[PCALL_NUM
];
155 #define PCALL_FUNCTION 1
156 #define PCALL_FAST_FUNCTION 2
157 #define PCALL_FASTER_FUNCTION 3
158 #define PCALL_METHOD 4
159 #define PCALL_BOUND_METHOD 5
160 #define PCALL_CFUNCTION 6
162 #define PCALL_GENERATOR 8
163 #define PCALL_OTHER 9
166 /* Notes about the statistics
170 FAST_FUNCTION means no argument tuple needs to be created.
171 FASTER_FUNCTION means that the fast-path frame setup code is used.
173 If there is a method call where the call can be optimized by changing
174 the argument tuple and calling the function directly, it gets recorded
177 As a result, the relationship among the statistics appears to be
178 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
179 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
180 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
181 PCALL_METHOD > PCALL_BOUND_METHOD
184 #define PCALL(POS) pcall[POS]++
187 PyEval_GetCallStats(PyObject
*self
)
189 return Py_BuildValue("iiiiiiiiii",
190 pcall
[0], pcall
[1], pcall
[2], pcall
[3],
191 pcall
[4], pcall
[5], pcall
[6], pcall
[7],
198 PyEval_GetCallStats(PyObject
*self
)
211 #include "pythread.h"
213 static PyThread_type_lock interpreter_lock
= 0; /* This is the GIL */
214 static long main_thread
= 0;
217 PyEval_ThreadsInitialized(void)
219 return interpreter_lock
!= 0;
223 PyEval_InitThreads(void)
225 if (interpreter_lock
)
227 interpreter_lock
= PyThread_allocate_lock();
228 PyThread_acquire_lock(interpreter_lock
, 1);
229 main_thread
= PyThread_get_thread_ident();
233 PyEval_AcquireLock(void)
235 PyThread_acquire_lock(interpreter_lock
, 1);
239 PyEval_ReleaseLock(void)
241 PyThread_release_lock(interpreter_lock
);
245 PyEval_AcquireThread(PyThreadState
*tstate
)
248 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
249 /* Check someone has called PyEval_InitThreads() to create the lock */
250 assert(interpreter_lock
);
251 PyThread_acquire_lock(interpreter_lock
, 1);
252 if (PyThreadState_Swap(tstate
) != NULL
)
254 "PyEval_AcquireThread: non-NULL old thread state");
258 PyEval_ReleaseThread(PyThreadState
*tstate
)
261 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
262 if (PyThreadState_Swap(NULL
) != tstate
)
263 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
264 PyThread_release_lock(interpreter_lock
);
267 /* This function is called from PyOS_AfterFork to ensure that newly
268 created child processes don't hold locks referring to threads which
269 are not running in the child process. (This could also be done using
270 pthread_atfork mechanism, at least for the pthreads implementation.) */
273 PyEval_ReInitThreads(void)
275 if (!interpreter_lock
)
277 /*XXX Can't use PyThread_free_lock here because it does too
278 much error-checking. Doing this cleanly would require
279 adding a new function to each thread_*.h. Instead, just
280 create a new lock and waste a little bit of memory */
281 interpreter_lock
= PyThread_allocate_lock();
282 PyThread_acquire_lock(interpreter_lock
, 1);
283 main_thread
= PyThread_get_thread_ident();
287 /* Functions save_thread and restore_thread are always defined so
288 dynamically loaded modules needn't be compiled separately for use
289 with and without threads: */
292 PyEval_SaveThread(void)
294 PyThreadState
*tstate
= PyThreadState_Swap(NULL
);
296 Py_FatalError("PyEval_SaveThread: NULL tstate");
298 if (interpreter_lock
)
299 PyThread_release_lock(interpreter_lock
);
305 PyEval_RestoreThread(PyThreadState
*tstate
)
308 Py_FatalError("PyEval_RestoreThread: NULL tstate");
310 if (interpreter_lock
) {
312 PyThread_acquire_lock(interpreter_lock
, 1);
316 PyThreadState_Swap(tstate
);
320 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
321 signal handlers or Mac I/O completion routines) can schedule calls
322 to a function to be called synchronously.
323 The synchronous function is called with one void* argument.
324 It should return 0 for success or -1 for failure -- failure should
325 be accompanied by an exception.
327 If registry succeeds, the registry function returns 0; if it fails
328 (e.g. due to too many pending calls) it returns -1 (without setting
329 an exception condition).
331 Note that because registry may occur from within signal handlers,
332 or other asynchronous events, calling malloc() is unsafe!
335 Any thread can schedule pending calls, but only the main thread
339 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
340 There are two possible race conditions:
341 (1) nested asynchronous registry calls;
342 (2) registry calls made while pending calls are being processed.
343 While (1) is very unlikely, (2) is a real possibility.
344 The current code is safe against (2), but not against (1).
345 The safety against (2) is derived from the fact that only one
346 thread (the main thread) ever takes things out of the queue.
348 XXX Darn! With the advent of thread state, we should have an array
349 of pending calls per thread in the thread state! Later...
352 #define NPENDINGCALLS 32
356 } pendingcalls
[NPENDINGCALLS
];
357 static volatile int pendingfirst
= 0;
358 static volatile int pendinglast
= 0;
359 static volatile int things_to_do
= 0;
362 Py_AddPendingCall(int (*func
)(void *), void *arg
)
364 static volatile int busy
= 0;
366 /* XXX Begin critical section */
367 /* XXX If you want this to be safe against nested
368 XXX asynchronous calls, you'll have to work harder! */
373 j
= (i
+ 1) % NPENDINGCALLS
;
374 if (j
== pendingfirst
) {
376 return -1; /* Queue full */
378 pendingcalls
[i
].func
= func
;
379 pendingcalls
[i
].arg
= arg
;
383 things_to_do
= 1; /* Signal main loop */
385 /* XXX End critical section */
390 Py_MakePendingCalls(void)
394 if (main_thread
&& PyThread_get_thread_ident() != main_thread
)
406 if (i
== pendinglast
)
407 break; /* Queue empty */
408 func
= pendingcalls
[i
].func
;
409 arg
= pendingcalls
[i
].arg
;
410 pendingfirst
= (i
+ 1) % NPENDINGCALLS
;
413 things_to_do
= 1; /* We're not done yet */
422 /* The interpreter's recursion limit */
424 #ifndef Py_DEFAULT_RECURSION_LIMIT
425 #define Py_DEFAULT_RECURSION_LIMIT 1000
427 static int recursion_limit
= Py_DEFAULT_RECURSION_LIMIT
;
428 int _Py_CheckRecursionLimit
= Py_DEFAULT_RECURSION_LIMIT
;
431 Py_GetRecursionLimit(void)
433 return recursion_limit
;
437 Py_SetRecursionLimit(int new_limit
)
439 recursion_limit
= new_limit
;
440 _Py_CheckRecursionLimit
= recursion_limit
;
443 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
444 if the recursion_depth reaches _Py_CheckRecursionLimit.
445 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
446 to guarantee that _Py_CheckRecursiveCall() is regularly called.
447 Without USE_STACKCHECK, there is no need for this. */
449 _Py_CheckRecursiveCall(char *where
)
451 PyThreadState
*tstate
= PyThreadState_GET();
453 #ifdef USE_STACKCHECK
454 if (PyOS_CheckStack()) {
455 --tstate
->recursion_depth
;
456 PyErr_SetString(PyExc_MemoryError
, "Stack overflow");
460 if (tstate
->recursion_depth
> recursion_limit
) {
461 --tstate
->recursion_depth
;
462 PyErr_Format(PyExc_RuntimeError
,
463 "maximum recursion depth exceeded%s",
467 _Py_CheckRecursionLimit
= recursion_limit
;
471 /* Status code for main loop (reason for stack unwind) */
473 WHY_NOT
= 0x0001, /* No error */
474 WHY_EXCEPTION
= 0x0002, /* Exception occurred */
475 WHY_RERAISE
= 0x0004, /* Exception re-raised by 'finally' */
476 WHY_RETURN
= 0x0008, /* 'return' statement */
477 WHY_BREAK
= 0x0010, /* 'break' statement */
478 WHY_CONTINUE
= 0x0020, /* 'continue' statement */
479 WHY_YIELD
= 0x0040 /* 'yield' operator */
482 static enum why_code
do_raise(PyObject
*, PyObject
*, PyObject
*);
483 static int unpack_iterable(PyObject
*, int, PyObject
**);
485 /* for manipulating the thread switch and periodic "stuff" - used to be
486 per thread, now just a pair o' globals */
487 int _Py_CheckInterval
= 100;
488 volatile int _Py_Ticker
= 100;
491 PyEval_EvalCode(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
)
493 /* XXX raise SystemError if globals is NULL */
494 return PyEval_EvalCodeEx(co
,
496 (PyObject
**)NULL
, 0,
497 (PyObject
**)NULL
, 0,
498 (PyObject
**)NULL
, 0,
503 /* Interpreter main loop */
506 PyEval_EvalFrame(PyFrameObject
*f
) {
507 /* This is for backward compatibility with extension modules that
508 used this API; core interpreter code should call PyEval_EvalFrameEx() */
509 return PyEval_EvalFrameEx(f
, 0);
513 PyEval_EvalFrameEx(PyFrameObject
*f
, int throwflag
)
518 register PyObject
**stack_pointer
; /* Next free slot in value stack */
519 register unsigned char *next_instr
;
520 register int opcode
; /* Current opcode */
521 register int oparg
; /* Current opcode argument, if any */
522 register enum why_code why
; /* Reason for block stack unwind */
523 register int err
; /* Error status -- nonzero if error */
524 register PyObject
*x
; /* Result object -- NULL if error */
525 register PyObject
*v
; /* Temporary objects popped off stack */
526 register PyObject
*w
;
527 register PyObject
*u
;
528 register PyObject
*t
;
529 register PyObject
*stream
= NULL
; /* for PRINT opcodes */
530 register PyObject
**fastlocals
, **freevars
;
531 PyObject
*retval
= NULL
; /* Return value */
532 PyThreadState
*tstate
= PyThreadState_GET();
535 /* when tracing we set things up so that
537 not (instr_lb <= current_bytecode_offset < instr_ub)
539 is true when the line being executed has changed. The
540 initial values are such as to make this false the first
541 time it is tested. */
542 int instr_ub
= -1, instr_lb
= 0, instr_prev
= -1;
544 unsigned char *first_instr
;
547 #if defined(Py_DEBUG) || defined(LLTRACE)
548 /* Make it easier to find out where we are with a debugger */
552 /* Tuple access macros */
555 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
557 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
561 /* Use Pentium timestamp counter to mark certain events:
562 inst0 -- beginning of switch statement for opcode dispatch
563 inst1 -- end of switch statement (may be skipped)
564 loop0 -- the top of the mainloop
565 loop1 -- place where control returns again to top of mainloop
567 intr1 -- beginning of long interruption
568 intr2 -- end of long interruption
570 Many opcodes call out to helper C functions. In some cases, the
571 time in those functions should be counted towards the time for the
572 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
573 calls another Python function; there's no point in charge all the
574 bytecode executed by the called function to the caller.
576 It's hard to make a useful judgement statically. In the presence
577 of operator overloading, it's impossible to tell if a call will
578 execute new Python code or not.
580 It's a case-by-case judgement. I'll use intr1 for the following
586 CALL_FUNCTION (and friends)
589 uint64 inst0
, inst1
, loop0
, loop1
, intr0
= 0, intr1
= 0;
592 READ_TIMESTAMP(inst0
);
593 READ_TIMESTAMP(inst1
);
594 READ_TIMESTAMP(loop0
);
595 READ_TIMESTAMP(loop1
);
597 /* shut up the compiler */
601 /* Code access macros */
603 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
604 #define NEXTOP() (*next_instr++)
605 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
606 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
607 #define JUMPTO(x) (next_instr = first_instr + (x))
608 #define JUMPBY(x) (next_instr += (x))
610 /* OpCode prediction macros
611 Some opcodes tend to come in pairs thus making it possible to predict
612 the second code when the first is run. For example, COMPARE_OP is often
613 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
614 followed by a POP_TOP.
616 Verifying the prediction costs a single high-speed test of register
617 variable against a constant. If the pairing was good, then the
618 processor has a high likelihood of making its own successful branch
619 prediction which results in a nearly zero overhead transition to the
622 A successful prediction saves a trip through the eval-loop including
623 its two unpredictable branches, the HASARG test and the switch-case.
625 If collecting opcode statistics, turn off prediction so that
626 statistics are accurately maintained (the predictions bypass
627 the opcode frequency counter updates).
630 #ifdef DYNAMIC_EXECUTION_PROFILE
631 #define PREDICT(op) if (0) goto PRED_##op
633 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
636 #define PREDICTED(op) PRED_##op: next_instr++
637 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
639 /* Stack manipulation macros */
641 /* The stack can grow at most MAXINT deep, as co_nlocals and
642 co_stacksize are ints. */
643 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
644 #define EMPTY() (STACK_LEVEL() == 0)
645 #define TOP() (stack_pointer[-1])
646 #define SECOND() (stack_pointer[-2])
647 #define THIRD() (stack_pointer[-3])
648 #define FOURTH() (stack_pointer[-4])
649 #define SET_TOP(v) (stack_pointer[-1] = (v))
650 #define SET_SECOND(v) (stack_pointer[-2] = (v))
651 #define SET_THIRD(v) (stack_pointer[-3] = (v))
652 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
653 #define BASIC_STACKADJ(n) (stack_pointer += n)
654 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
655 #define BASIC_POP() (*--stack_pointer)
658 #define PUSH(v) { (void)(BASIC_PUSH(v), \
659 lltrace && prtrace(TOP(), "push")); \
660 assert(STACK_LEVEL() <= co->co_stacksize); }
661 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
662 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
663 lltrace && prtrace(TOP(), "stackadj")); \
664 assert(STACK_LEVEL() <= co->co_stacksize); }
665 #define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER))
667 #define PUSH(v) BASIC_PUSH(v)
668 #define POP() BASIC_POP()
669 #define STACKADJ(n) BASIC_STACKADJ(n)
670 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
673 /* Local variable macros */
675 #define GETLOCAL(i) (fastlocals[i])
677 /* The SETLOCAL() macro must not DECREF the local variable in-place and
678 then store the new value; it must copy the old value to a temporary
679 value, then store the new value, and then DECREF the temporary value.
680 This is because it is possible that during the DECREF the frame is
681 accessed by other code (e.g. a __del__ method or gc.collect()) and the
682 variable would be pointing to already-freed memory. */
683 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
684 GETLOCAL(i) = value; \
685 Py_XDECREF(tmp); } while (0)
693 if (Py_EnterRecursiveCall(""))
698 if (tstate
->use_tracing
) {
699 if (tstate
->c_tracefunc
!= NULL
) {
700 /* tstate->c_tracefunc, if defined, is a
701 function that will be called on *every* entry
702 to a code block. Its return value, if not
703 None, is a function that will be called at
704 the start of each executed line of code.
705 (Actually, the function must return itself
706 in order to continue tracing.) The trace
707 functions are called with three arguments:
708 a pointer to the current frame, a string
709 indicating why the function is called, and
710 an argument which depends on the situation.
711 The global trace function is also called
712 whenever an exception is detected. */
713 if (call_trace(tstate
->c_tracefunc
, tstate
->c_traceobj
,
714 f
, PyTrace_CALL
, Py_None
)) {
715 /* Trace function raised an error */
716 goto exit_eval_frame
;
719 if (tstate
->c_profilefunc
!= NULL
) {
720 /* Similar for c_profilefunc, except it needn't
721 return itself and isn't called for "line" events */
722 if (call_trace(tstate
->c_profilefunc
,
723 tstate
->c_profileobj
,
724 f
, PyTrace_CALL
, Py_None
)) {
725 /* Profile function raised an error */
726 goto exit_eval_frame
;
732 names
= co
->co_names
;
733 consts
= co
->co_consts
;
734 fastlocals
= f
->f_localsplus
;
735 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
736 first_instr
= (unsigned char*) PyString_AS_STRING(co
->co_code
);
737 /* An explanation is in order for the next line.
739 f->f_lasti now refers to the index of the last instruction
740 executed. You might think this was obvious from the name, but
741 this wasn't always true before 2.3! PyFrame_New now sets
742 f->f_lasti to -1 (i.e. the index *before* the first instruction)
743 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
744 does work. Promise. */
745 next_instr
= first_instr
+ f
->f_lasti
+ 1;
746 stack_pointer
= f
->f_stacktop
;
747 assert(stack_pointer
!= NULL
);
748 f
->f_stacktop
= NULL
; /* remains NULL unless yield suspends frame */
751 lltrace
= PyDict_GetItemString(f
->f_globals
, "__lltrace__") != NULL
;
753 #if defined(Py_DEBUG) || defined(LLTRACE)
754 filename
= PyString_AsString(co
->co_filename
);
759 x
= Py_None
; /* Not a reference, just anything non-NULL */
762 if (throwflag
) { /* support for generator.throw() */
770 /* Almost surely, the opcode executed a break
771 or a continue, preventing inst1 from being set
772 on the way out of the loop.
774 READ_TIMESTAMP(inst1
);
777 dump_tsc(opcode
, ticked
, inst0
, inst1
, loop0
, loop1
,
783 READ_TIMESTAMP(loop0
);
785 assert(stack_pointer
>= f
->f_valuestack
); /* else underflow */
786 assert(STACK_LEVEL() <= co
->co_stacksize
); /* else overflow */
788 /* Do periodic things. Doing this every time through
789 the loop would add too much overhead, so we do it
790 only every Nth instruction. We also do it if
791 ``things_to_do'' is set, i.e. when an asynchronous
792 event needs attention (e.g. a signal handler or
793 async I/O handler); see Py_AddPendingCall() and
794 Py_MakePendingCalls() above. */
796 if (--_Py_Ticker
< 0) {
797 if (*next_instr
== SETUP_FINALLY
) {
798 /* Make the last opcode before
799 a try: finally: block uninterruptable. */
800 goto fast_next_opcode
;
802 _Py_Ticker
= _Py_CheckInterval
;
803 tstate
->tick_counter
++;
808 if (Py_MakePendingCalls() < 0) {
813 /* MakePendingCalls() didn't succeed.
814 Force early re-execution of this
815 "periodic" code, possibly after
820 if (interpreter_lock
) {
821 /* Give another thread a chance */
823 if (PyThreadState_Swap(NULL
) != tstate
)
824 Py_FatalError("ceval: tstate mix-up");
825 PyThread_release_lock(interpreter_lock
);
827 /* Other threads may run now */
829 PyThread_acquire_lock(interpreter_lock
, 1);
830 if (PyThreadState_Swap(tstate
) != NULL
)
831 Py_FatalError("ceval: orphan tstate");
833 /* Check for thread interrupts */
835 if (tstate
->async_exc
!= NULL
) {
836 x
= tstate
->async_exc
;
837 tstate
->async_exc
= NULL
;
848 f
->f_lasti
= INSTR_OFFSET();
850 /* line-by-line tracing support */
852 if (tstate
->c_tracefunc
!= NULL
&& !tstate
->tracing
) {
853 /* see maybe_call_line_trace
854 for expository comments */
855 f
->f_stacktop
= stack_pointer
;
857 err
= maybe_call_line_trace(tstate
->c_tracefunc
,
859 f
, &instr_lb
, &instr_ub
,
861 /* Reload possibly changed frame fields */
863 if (f
->f_stacktop
!= NULL
) {
864 stack_pointer
= f
->f_stacktop
;
865 f
->f_stacktop
= NULL
;
868 /* trace function raised an exception */
873 /* Extract opcode and argument */
876 oparg
= 0; /* allows oparg to be stored in a register because
877 it doesn't have to be remembered across a full loop */
881 #ifdef DYNAMIC_EXECUTION_PROFILE
883 dxpairs
[lastopcode
][opcode
]++;
890 /* Instruction tracing */
893 if (HAS_ARG(opcode
)) {
894 printf("%d: %d, %d\n",
895 f
->f_lasti
, opcode
, oparg
);
904 /* Main switch on opcode */
905 READ_TIMESTAMP(inst0
);
910 It is essential that any operation that fails sets either
911 x to NULL, err to nonzero, or why to anything but WHY_NOT,
912 and that no operation that succeeds does this! */
914 /* case STOP_CODE: this is an error! */
917 goto fast_next_opcode
;
924 goto fast_next_opcode
;
926 format_exc_check_arg(PyExc_UnboundLocalError
,
927 UNBOUNDLOCAL_ERROR_MSG
,
928 PyTuple_GetItem(co
->co_varnames
, oparg
));
932 x
= GETITEM(consts
, oparg
);
935 goto fast_next_opcode
;
937 PREDICTED_WITH_ARG(STORE_FAST
);
941 goto fast_next_opcode
;
947 goto fast_next_opcode
;
954 goto fast_next_opcode
;
963 goto fast_next_opcode
;
974 goto fast_next_opcode
;
980 goto fast_next_opcode
;
991 goto fast_next_opcode
;
992 } else if (oparg
== 3) {
1003 goto fast_next_opcode
;
1005 Py_FatalError("invalid argument to DUP_TOPX"
1006 " (bytecode corruption?)");
1009 case UNARY_POSITIVE
:
1011 x
= PyNumber_Positive(v
);
1014 if (x
!= NULL
) continue;
1017 case UNARY_NEGATIVE
:
1019 x
= PyNumber_Negative(v
);
1022 if (x
!= NULL
) continue;
1027 err
= PyObject_IsTrue(v
);
1035 Py_INCREF(Py_False
);
1045 x
= PyObject_Repr(v
);
1048 if (x
!= NULL
) continue;
1053 x
= PyNumber_Invert(v
);
1056 if (x
!= NULL
) continue;
1062 x
= PyNumber_Power(v
, w
, Py_None
);
1066 if (x
!= NULL
) continue;
1069 case BINARY_MULTIPLY
:
1072 x
= PyNumber_Multiply(v
, w
);
1076 if (x
!= NULL
) continue;
1080 if (!_Py_QnewFlag
) {
1083 x
= PyNumber_Divide(v
, w
);
1087 if (x
!= NULL
) continue;
1090 /* -Qnew is in effect: fall through to
1091 BINARY_TRUE_DIVIDE */
1092 case BINARY_TRUE_DIVIDE
:
1095 x
= PyNumber_TrueDivide(v
, w
);
1099 if (x
!= NULL
) continue;
1102 case BINARY_FLOOR_DIVIDE
:
1105 x
= PyNumber_FloorDivide(v
, w
);
1109 if (x
!= NULL
) continue;
1115 x
= PyNumber_Remainder(v
, w
);
1119 if (x
!= NULL
) continue;
1125 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1126 /* INLINE: int + int */
1127 register long a
, b
, i
;
1128 a
= PyInt_AS_LONG(v
);
1129 b
= PyInt_AS_LONG(w
);
1131 if ((i
^a
) < 0 && (i
^b
) < 0)
1133 x
= PyInt_FromLong(i
);
1135 else if (PyString_CheckExact(v
) &&
1136 PyString_CheckExact(w
)) {
1137 x
= string_concatenate(v
, w
, f
, next_instr
);
1138 /* string_concatenate consumed the ref to v */
1139 goto skip_decref_vx
;
1143 x
= PyNumber_Add(v
, w
);
1149 if (x
!= NULL
) continue;
1152 case BINARY_SUBTRACT
:
1155 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1156 /* INLINE: int - int */
1157 register long a
, b
, i
;
1158 a
= PyInt_AS_LONG(v
);
1159 b
= PyInt_AS_LONG(w
);
1161 if ((i
^a
) < 0 && (i
^~b
) < 0)
1163 x
= PyInt_FromLong(i
);
1167 x
= PyNumber_Subtract(v
, w
);
1172 if (x
!= NULL
) continue;
1178 if (PyList_CheckExact(v
) && PyInt_CheckExact(w
)) {
1179 /* INLINE: list[int] */
1180 Py_ssize_t i
= PyInt_AsSsize_t(w
);
1182 i
+= PyList_GET_SIZE(v
);
1183 if (i
>= 0 && i
< PyList_GET_SIZE(v
)) {
1184 x
= PyList_GET_ITEM(v
, i
);
1192 x
= PyObject_GetItem(v
, w
);
1196 if (x
!= NULL
) continue;
1202 x
= PyNumber_Lshift(v
, w
);
1206 if (x
!= NULL
) continue;
1212 x
= PyNumber_Rshift(v
, w
);
1216 if (x
!= NULL
) continue;
1222 x
= PyNumber_And(v
, w
);
1226 if (x
!= NULL
) continue;
1232 x
= PyNumber_Xor(v
, w
);
1236 if (x
!= NULL
) continue;
1242 x
= PyNumber_Or(v
, w
);
1246 if (x
!= NULL
) continue;
1252 err
= PyList_Append(v
, w
);
1256 PREDICT(JUMP_ABSOLUTE
);
1264 x
= PyNumber_InPlacePower(v
, w
, Py_None
);
1268 if (x
!= NULL
) continue;
1271 case INPLACE_MULTIPLY
:
1274 x
= PyNumber_InPlaceMultiply(v
, w
);
1278 if (x
!= NULL
) continue;
1281 case INPLACE_DIVIDE
:
1282 if (!_Py_QnewFlag
) {
1285 x
= PyNumber_InPlaceDivide(v
, w
);
1289 if (x
!= NULL
) continue;
1292 /* -Qnew is in effect: fall through to
1293 INPLACE_TRUE_DIVIDE */
1294 case INPLACE_TRUE_DIVIDE
:
1297 x
= PyNumber_InPlaceTrueDivide(v
, w
);
1301 if (x
!= NULL
) continue;
1304 case INPLACE_FLOOR_DIVIDE
:
1307 x
= PyNumber_InPlaceFloorDivide(v
, w
);
1311 if (x
!= NULL
) continue;
1314 case INPLACE_MODULO
:
1317 x
= PyNumber_InPlaceRemainder(v
, w
);
1321 if (x
!= NULL
) continue;
1327 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1328 /* INLINE: int + int */
1329 register long a
, b
, i
;
1330 a
= PyInt_AS_LONG(v
);
1331 b
= PyInt_AS_LONG(w
);
1333 if ((i
^a
) < 0 && (i
^b
) < 0)
1335 x
= PyInt_FromLong(i
);
1337 else if (PyString_CheckExact(v
) &&
1338 PyString_CheckExact(w
)) {
1339 x
= string_concatenate(v
, w
, f
, next_instr
);
1340 /* string_concatenate consumed the ref to v */
1345 x
= PyNumber_InPlaceAdd(v
, w
);
1351 if (x
!= NULL
) continue;
1354 case INPLACE_SUBTRACT
:
1357 if (PyInt_CheckExact(v
) && PyInt_CheckExact(w
)) {
1358 /* INLINE: int - int */
1359 register long a
, b
, i
;
1360 a
= PyInt_AS_LONG(v
);
1361 b
= PyInt_AS_LONG(w
);
1363 if ((i
^a
) < 0 && (i
^~b
) < 0)
1365 x
= PyInt_FromLong(i
);
1369 x
= PyNumber_InPlaceSubtract(v
, w
);
1374 if (x
!= NULL
) continue;
1377 case INPLACE_LSHIFT
:
1380 x
= PyNumber_InPlaceLshift(v
, w
);
1384 if (x
!= NULL
) continue;
1387 case INPLACE_RSHIFT
:
1390 x
= PyNumber_InPlaceRshift(v
, w
);
1394 if (x
!= NULL
) continue;
1400 x
= PyNumber_InPlaceAnd(v
, w
);
1404 if (x
!= NULL
) continue;
1410 x
= PyNumber_InPlaceXor(v
, w
);
1414 if (x
!= NULL
) continue;
1420 x
= PyNumber_InPlaceOr(v
, w
);
1424 if (x
!= NULL
) continue;
1431 if ((opcode
-SLICE
) & 2)
1435 if ((opcode
-SLICE
) & 1)
1440 x
= apply_slice(u
, v
, w
);
1445 if (x
!= NULL
) continue;
1452 if ((opcode
-STORE_SLICE
) & 2)
1456 if ((opcode
-STORE_SLICE
) & 1)
1462 err
= assign_slice(u
, v
, w
, t
); /* u[v:w] = t */
1467 if (err
== 0) continue;
1470 case DELETE_SLICE
+0:
1471 case DELETE_SLICE
+1:
1472 case DELETE_SLICE
+2:
1473 case DELETE_SLICE
+3:
1474 if ((opcode
-DELETE_SLICE
) & 2)
1478 if ((opcode
-DELETE_SLICE
) & 1)
1483 err
= assign_slice(u
, v
, w
, (PyObject
*)NULL
);
1488 if (err
== 0) continue;
1497 err
= PyObject_SetItem(v
, w
, u
);
1501 if (err
== 0) continue;
1509 err
= PyObject_DelItem(v
, w
);
1512 if (err
== 0) continue;
1517 w
= PySys_GetObject("displayhook");
1519 PyErr_SetString(PyExc_RuntimeError
,
1520 "lost sys.displayhook");
1525 x
= PyTuple_Pack(1, v
);
1530 w
= PyEval_CallObject(w
, x
);
1541 /* fall through to PRINT_ITEM */
1545 if (stream
== NULL
|| stream
== Py_None
) {
1546 w
= PySys_GetObject("stdout");
1548 PyErr_SetString(PyExc_RuntimeError
,
1553 /* PyFile_SoftSpace() can exececute arbitrary code
1554 if sys.stdout is an instance with a __getattr__.
1555 If __getattr__ raises an exception, w will
1556 be freed, so we need to prevent that temporarily. */
1558 if (w
!= NULL
&& PyFile_SoftSpace(w
, 0))
1559 err
= PyFile_WriteString(" ", w
);
1561 err
= PyFile_WriteObject(v
, w
, Py_PRINT_RAW
);
1563 /* XXX move into writeobject() ? */
1564 if (PyString_Check(v
)) {
1565 char *s
= PyString_AS_STRING(v
);
1566 Py_ssize_t len
= PyString_GET_SIZE(v
);
1568 !isspace(Py_CHARMASK(s
[len
-1])) ||
1570 PyFile_SoftSpace(w
, 1);
1572 #ifdef Py_USING_UNICODE
1573 else if (PyUnicode_Check(v
)) {
1574 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(v
);
1575 Py_ssize_t len
= PyUnicode_GET_SIZE(v
);
1577 !Py_UNICODE_ISSPACE(s
[len
-1]) ||
1579 PyFile_SoftSpace(w
, 1);
1583 PyFile_SoftSpace(w
, 1);
1593 case PRINT_NEWLINE_TO
:
1595 /* fall through to PRINT_NEWLINE */
1598 if (stream
== NULL
|| stream
== Py_None
) {
1599 w
= PySys_GetObject("stdout");
1601 PyErr_SetString(PyExc_RuntimeError
,
1605 err
= PyFile_WriteString("\n", w
);
1607 PyFile_SoftSpace(w
, 0);
1615 default: switch (opcode
) {
1621 u
= POP(); /* traceback */
1624 v
= POP(); /* value */
1627 w
= POP(); /* exc */
1628 case 0: /* Fallthrough */
1629 why
= do_raise(w
, v
, u
);
1632 PyErr_SetString(PyExc_SystemError
,
1633 "bad RAISE_VARARGS oparg");
1634 why
= WHY_EXCEPTION
;
1640 if ((x
= f
->f_locals
) != NULL
) {
1645 PyErr_SetString(PyExc_SystemError
, "no locals");
1651 goto fast_block_end
;
1655 f
->f_stacktop
= stack_pointer
;
1664 READ_TIMESTAMP(intr0
);
1665 err
= exec_statement(f
, u
, v
, w
);
1666 READ_TIMESTAMP(intr1
);
1674 PyTryBlock
*b
= PyFrame_BlockPop(f
);
1675 while (STACK_LEVEL() > b
->b_level
) {
1684 if (PyInt_Check(v
)) {
1685 why
= (enum why_code
) PyInt_AS_LONG(v
);
1686 assert(why
!= WHY_YIELD
);
1687 if (why
== WHY_RETURN
||
1688 why
== WHY_CONTINUE
)
1691 else if (PyExceptionClass_Check(v
) || PyString_Check(v
)) {
1694 PyErr_Restore(v
, w
, u
);
1698 else if (v
!= Py_None
) {
1699 PyErr_SetString(PyExc_SystemError
,
1700 "'finally' pops bad exception");
1701 why
= WHY_EXCEPTION
;
1711 x
= build_class(u
, v
, w
);
1719 w
= GETITEM(names
, oparg
);
1721 if ((x
= f
->f_locals
) != NULL
) {
1722 if (PyDict_CheckExact(x
))
1723 err
= PyDict_SetItem(x
, w
, v
);
1725 err
= PyObject_SetItem(x
, w
, v
);
1727 if (err
== 0) continue;
1730 PyErr_Format(PyExc_SystemError
,
1731 "no locals found when storing %s",
1736 w
= GETITEM(names
, oparg
);
1737 if ((x
= f
->f_locals
) != NULL
) {
1738 if ((err
= PyObject_DelItem(x
, w
)) != 0)
1739 format_exc_check_arg(PyExc_NameError
,
1743 PyErr_Format(PyExc_SystemError
,
1744 "no locals when deleting %s",
1748 PREDICTED_WITH_ARG(UNPACK_SEQUENCE
);
1749 case UNPACK_SEQUENCE
:
1751 if (PyTuple_CheckExact(v
) && PyTuple_GET_SIZE(v
) == oparg
) {
1752 PyObject
**items
= ((PyTupleObject
*)v
)->ob_item
;
1760 } else if (PyList_CheckExact(v
) && PyList_GET_SIZE(v
) == oparg
) {
1761 PyObject
**items
= ((PyListObject
*)v
)->ob_item
;
1767 } else if (unpack_iterable(v
, oparg
,
1768 stack_pointer
+ oparg
))
1769 stack_pointer
+= oparg
;
1771 if (PyErr_ExceptionMatches(PyExc_TypeError
))
1772 PyErr_SetString(PyExc_TypeError
,
1773 "unpack non-sequence");
1774 why
= WHY_EXCEPTION
;
1780 w
= GETITEM(names
, oparg
);
1784 err
= PyObject_SetAttr(v
, w
, u
); /* v.w = u */
1787 if (err
== 0) continue;
1791 w
= GETITEM(names
, oparg
);
1793 err
= PyObject_SetAttr(v
, w
, (PyObject
*)NULL
);
1799 w
= GETITEM(names
, oparg
);
1801 err
= PyDict_SetItem(f
->f_globals
, w
, v
);
1803 if (err
== 0) continue;
1807 w
= GETITEM(names
, oparg
);
1808 if ((err
= PyDict_DelItem(f
->f_globals
, w
)) != 0)
1809 format_exc_check_arg(
1810 PyExc_NameError
, GLOBAL_NAME_ERROR_MSG
, w
);
1814 w
= GETITEM(names
, oparg
);
1815 if ((v
= f
->f_locals
) == NULL
) {
1816 PyErr_Format(PyExc_SystemError
,
1817 "no locals when loading %s",
1821 if (PyDict_CheckExact(v
)) {
1822 x
= PyDict_GetItem(v
, w
);
1826 x
= PyObject_GetItem(v
, w
);
1827 if (x
== NULL
&& PyErr_Occurred()) {
1828 if (!PyErr_ExceptionMatches(PyExc_KeyError
))
1834 x
= PyDict_GetItem(f
->f_globals
, w
);
1836 x
= PyDict_GetItem(f
->f_builtins
, w
);
1838 format_exc_check_arg(
1850 w
= GETITEM(names
, oparg
);
1851 if (PyString_CheckExact(w
)) {
1852 /* Inline the PyDict_GetItem() calls.
1853 WARNING: this is an extreme speed hack.
1854 Do not try this at home. */
1855 long hash
= ((PyStringObject
*)w
)->ob_shash
;
1859 d
= (PyDictObject
*)(f
->f_globals
);
1860 e
= d
->ma_lookup(d
, w
, hash
);
1871 d
= (PyDictObject
*)(f
->f_builtins
);
1872 e
= d
->ma_lookup(d
, w
, hash
);
1883 goto load_global_error
;
1886 /* This is the un-inlined version of the code above */
1887 x
= PyDict_GetItem(f
->f_globals
, w
);
1889 x
= PyDict_GetItem(f
->f_builtins
, w
);
1892 format_exc_check_arg(
1894 GLOBAL_NAME_ERROR_MSG
, w
);
1903 x
= GETLOCAL(oparg
);
1905 SETLOCAL(oparg
, NULL
);
1908 format_exc_check_arg(
1909 PyExc_UnboundLocalError
,
1910 UNBOUNDLOCAL_ERROR_MSG
,
1911 PyTuple_GetItem(co
->co_varnames
, oparg
)
1916 x
= freevars
[oparg
];
1919 if (x
!= NULL
) continue;
1923 x
= freevars
[oparg
];
1930 /* Don't stomp existing exception */
1931 if (PyErr_Occurred())
1933 if (oparg
< PyTuple_GET_SIZE(co
->co_cellvars
)) {
1934 v
= PyTuple_GET_ITEM(co
->co_cellvars
,
1936 format_exc_check_arg(
1937 PyExc_UnboundLocalError
,
1938 UNBOUNDLOCAL_ERROR_MSG
,
1941 v
= PyTuple_GET_ITEM(
1943 oparg
- PyTuple_GET_SIZE(co
->co_cellvars
));
1944 format_exc_check_arg(
1946 UNBOUNDFREE_ERROR_MSG
,
1953 x
= freevars
[oparg
];
1959 x
= PyTuple_New(oparg
);
1961 for (; --oparg
>= 0;) {
1963 PyTuple_SET_ITEM(x
, oparg
, w
);
1971 x
= PyList_New(oparg
);
1973 for (; --oparg
>= 0;) {
1975 PyList_SET_ITEM(x
, oparg
, w
);
1985 if (x
!= NULL
) continue;
1989 w
= GETITEM(names
, oparg
);
1991 x
= PyObject_GetAttr(v
, w
);
1994 if (x
!= NULL
) continue;
2000 if (PyInt_CheckExact(w
) && PyInt_CheckExact(v
)) {
2001 /* INLINE: cmp(int, int) */
2004 a
= PyInt_AS_LONG(v
);
2005 b
= PyInt_AS_LONG(w
);
2007 case PyCmp_LT
: res
= a
< b
; break;
2008 case PyCmp_LE
: res
= a
<= b
; break;
2009 case PyCmp_EQ
: res
= a
== b
; break;
2010 case PyCmp_NE
: res
= a
!= b
; break;
2011 case PyCmp_GT
: res
= a
> b
; break;
2012 case PyCmp_GE
: res
= a
>= b
; break;
2013 case PyCmp_IS
: res
= v
== w
; break;
2014 case PyCmp_IS_NOT
: res
= v
!= w
; break;
2015 default: goto slow_compare
;
2017 x
= res
? Py_True
: Py_False
;
2022 x
= cmp_outcome(oparg
, v
, w
);
2027 if (x
== NULL
) break;
2028 PREDICT(JUMP_IF_FALSE
);
2029 PREDICT(JUMP_IF_TRUE
);
2033 w
= GETITEM(names
, oparg
);
2034 x
= PyDict_GetItemString(f
->f_builtins
, "__import__");
2036 PyErr_SetString(PyExc_ImportError
,
2037 "__import__ not found");
2042 if (PyInt_AsLong(u
) != -1 || PyErr_Occurred())
2046 f
->f_locals
== NULL
?
2047 Py_None
: f
->f_locals
,
2054 f
->f_locals
== NULL
?
2055 Py_None
: f
->f_locals
,
2064 READ_TIMESTAMP(intr0
);
2065 x
= PyEval_CallObject(x
, w
);
2066 READ_TIMESTAMP(intr1
);
2069 if (x
!= NULL
) continue;
2074 PyFrame_FastToLocals(f
);
2075 if ((x
= f
->f_locals
) == NULL
) {
2076 PyErr_SetString(PyExc_SystemError
,
2077 "no locals found during 'import *'");
2080 READ_TIMESTAMP(intr0
);
2081 err
= import_all_from(x
, v
);
2082 READ_TIMESTAMP(intr1
);
2083 PyFrame_LocalsToFast(f
, 0);
2085 if (err
== 0) continue;
2089 w
= GETITEM(names
, oparg
);
2091 READ_TIMESTAMP(intr0
);
2092 x
= import_from(v
, w
);
2093 READ_TIMESTAMP(intr1
);
2095 if (x
!= NULL
) continue;
2100 goto fast_next_opcode
;
2102 PREDICTED_WITH_ARG(JUMP_IF_FALSE
);
2107 goto fast_next_opcode
;
2109 if (w
== Py_False
) {
2111 goto fast_next_opcode
;
2113 err
= PyObject_IsTrue(w
);
2122 PREDICTED_WITH_ARG(JUMP_IF_TRUE
);
2125 if (w
== Py_False
) {
2127 goto fast_next_opcode
;
2131 goto fast_next_opcode
;
2133 err
= PyObject_IsTrue(w
);
2144 PREDICTED_WITH_ARG(JUMP_ABSOLUTE
);
2150 /* before: [obj]; after [getiter(obj)] */
2152 x
= PyObject_GetIter(v
);
2162 PREDICTED_WITH_ARG(FOR_ITER
);
2164 /* before: [iter]; after: [iter, iter()] *or* [] */
2166 x
= (*v
->ob_type
->tp_iternext
)(v
);
2169 PREDICT(STORE_FAST
);
2170 PREDICT(UNPACK_SEQUENCE
);
2173 if (PyErr_Occurred()) {
2174 if (!PyErr_ExceptionMatches(PyExc_StopIteration
))
2178 /* iterator ended normally */
2186 goto fast_block_end
;
2189 retval
= PyInt_FromLong(oparg
);
2195 goto fast_block_end
;
2200 /* NOTE: If you add any new block-setup opcodes that are not try/except/finally
2201 handlers, you may need to update the PyGen_NeedsFinalizing() function. */
2203 PyFrame_BlockSetup(f
, opcode
, INSTR_OFFSET() + oparg
,
2209 /* TOP is the context.__exit__ bound method.
2210 Below that are 1-3 values indicating how/why
2211 we entered the finally clause:
2213 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
2214 - SECOND = WHY_*; no retval below it
2215 - (SECOND, THIRD, FOURTH) = exc_info()
2216 In the last case, we must call
2217 TOP(SECOND, THIRD, FOURTH)
2218 otherwise we must call
2219 TOP(None, None, None)
2221 In addition, if the stack represents an exception,
2222 *and* the function call returns a 'true' value, we
2223 "zap" this information, to prevent END_FINALLY from
2224 re-raising the exception. (But non-local gotos
2225 should still be resumed.)
2230 if (PyInt_Check(u
) || u
== Py_None
) {
2231 u
= v
= w
= Py_None
;
2237 /* XXX Not the fastest way to call it... */
2238 x
= PyObject_CallFunctionObjArgs(x
, u
, v
, w
, NULL
);
2240 break; /* Go to error exit */
2241 if (u
!= Py_None
&& PyObject_IsTrue(x
)) {
2242 /* There was an exception and a true return */
2244 x
= TOP(); /* Again */
2253 /* Let END_FINALLY do its thing */
2267 x
= call_function(&sp
, oparg
, &intr0
, &intr1
);
2269 x
= call_function(&sp
, oparg
);
2278 case CALL_FUNCTION_VAR
:
2279 case CALL_FUNCTION_KW
:
2280 case CALL_FUNCTION_VAR_KW
:
2282 int na
= oparg
& 0xff;
2283 int nk
= (oparg
>>8) & 0xff;
2284 int flags
= (opcode
- CALL_FUNCTION
) & 3;
2285 int n
= na
+ 2 * nk
;
2286 PyObject
**pfunc
, *func
, **sp
;
2288 if (flags
& CALL_FLAG_VAR
)
2290 if (flags
& CALL_FLAG_KW
)
2292 pfunc
= stack_pointer
- n
- 1;
2295 if (PyMethod_Check(func
)
2296 && PyMethod_GET_SELF(func
) != NULL
) {
2297 PyObject
*self
= PyMethod_GET_SELF(func
);
2299 func
= PyMethod_GET_FUNCTION(func
);
2308 READ_TIMESTAMP(intr0
);
2309 x
= ext_do_call(func
, &sp
, flags
, na
, nk
);
2310 READ_TIMESTAMP(intr1
);
2314 while (stack_pointer
> pfunc
) {
2325 v
= POP(); /* code object */
2326 x
= PyFunction_New(v
, f
->f_globals
);
2328 /* XXX Maybe this should be a separate opcode? */
2329 if (x
!= NULL
&& oparg
> 0) {
2330 v
= PyTuple_New(oparg
);
2336 while (--oparg
>= 0) {
2338 PyTuple_SET_ITEM(v
, oparg
, w
);
2340 err
= PyFunction_SetDefaults(x
, v
);
2348 v
= POP(); /* code object */
2349 x
= PyFunction_New(v
, f
->f_globals
);
2353 err
= PyFunction_SetClosure(x
, v
);
2356 if (x
!= NULL
&& oparg
> 0) {
2357 v
= PyTuple_New(oparg
);
2363 while (--oparg
>= 0) {
2365 PyTuple_SET_ITEM(v
, oparg
, w
);
2367 err
= PyFunction_SetDefaults(x
, v
);
2381 x
= PySlice_New(u
, v
, w
);
2386 if (x
!= NULL
) continue;
2391 oparg
= oparg
<<16 | NEXTARG();
2392 goto dispatch_opcode
;
2396 "XXX lineno: %d, opcode: %d\n",
2397 PyCode_Addr2Line(f
->f_code
, f
->f_lasti
),
2399 PyErr_SetString(PyExc_SystemError
, "unknown opcode");
2400 why
= WHY_EXCEPTION
;
2411 READ_TIMESTAMP(inst1
);
2413 /* Quickly continue if no error occurred */
2415 if (why
== WHY_NOT
) {
2416 if (err
== 0 && x
!= NULL
) {
2418 /* This check is expensive! */
2419 if (PyErr_Occurred())
2421 "XXX undetected error\n");
2424 READ_TIMESTAMP(loop1
);
2425 continue; /* Normal, fast path */
2430 why
= WHY_EXCEPTION
;
2435 /* Double-check exception status */
2437 if (why
== WHY_EXCEPTION
|| why
== WHY_RERAISE
) {
2438 if (!PyErr_Occurred()) {
2439 PyErr_SetString(PyExc_SystemError
,
2440 "error return without exception set");
2441 why
= WHY_EXCEPTION
;
2446 /* This check is expensive! */
2447 if (PyErr_Occurred()) {
2449 sprintf(buf
, "Stack unwind with exception "
2450 "set and why=%d", why
);
2456 /* Log traceback info if this is a real exception */
2458 if (why
== WHY_EXCEPTION
) {
2459 PyTraceBack_Here(f
);
2461 if (tstate
->c_tracefunc
!= NULL
)
2462 call_exc_trace(tstate
->c_tracefunc
,
2463 tstate
->c_traceobj
, f
);
2466 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2468 if (why
== WHY_RERAISE
)
2469 why
= WHY_EXCEPTION
;
2471 /* Unwind stacks if a (pseudo) exception occurred */
2474 while (why
!= WHY_NOT
&& f
->f_iblock
> 0) {
2475 PyTryBlock
*b
= PyFrame_BlockPop(f
);
2477 assert(why
!= WHY_YIELD
);
2478 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_CONTINUE
) {
2479 /* For a continue inside a try block,
2480 don't pop the block for the loop. */
2481 PyFrame_BlockSetup(f
, b
->b_type
, b
->b_handler
,
2484 JUMPTO(PyInt_AS_LONG(retval
));
2489 while (STACK_LEVEL() > b
->b_level
) {
2493 if (b
->b_type
== SETUP_LOOP
&& why
== WHY_BREAK
) {
2495 JUMPTO(b
->b_handler
);
2498 if (b
->b_type
== SETUP_FINALLY
||
2499 (b
->b_type
== SETUP_EXCEPT
&&
2500 why
== WHY_EXCEPTION
)) {
2501 if (why
== WHY_EXCEPTION
) {
2502 PyObject
*exc
, *val
, *tb
;
2503 PyErr_Fetch(&exc
, &val
, &tb
);
2508 /* Make the raw exception data
2509 available to the handler,
2510 so a program can emulate the
2511 Python main loop. Don't do
2512 this for 'finally'. */
2513 if (b
->b_type
== SETUP_EXCEPT
) {
2514 PyErr_NormalizeException(
2516 set_exc_info(tstate
,
2528 if (why
& (WHY_RETURN
| WHY_CONTINUE
))
2530 v
= PyInt_FromLong((long)why
);
2534 JUMPTO(b
->b_handler
);
2537 } /* unwind stack */
2539 /* End the loop if we still have an error (or return) */
2543 READ_TIMESTAMP(loop1
);
2547 assert(why
!= WHY_YIELD
);
2548 /* Pop remaining stack entries. */
2554 if (why
!= WHY_RETURN
)
2558 if (tstate
->use_tracing
) {
2559 if (tstate
->c_tracefunc
) {
2560 if (why
== WHY_RETURN
|| why
== WHY_YIELD
) {
2561 if (call_trace(tstate
->c_tracefunc
,
2562 tstate
->c_traceobj
, f
,
2563 PyTrace_RETURN
, retval
)) {
2566 why
= WHY_EXCEPTION
;
2569 else if (why
== WHY_EXCEPTION
) {
2570 call_trace_protected(tstate
->c_tracefunc
,
2571 tstate
->c_traceobj
, f
,
2572 PyTrace_RETURN
, NULL
);
2575 if (tstate
->c_profilefunc
) {
2576 if (why
== WHY_EXCEPTION
)
2577 call_trace_protected(tstate
->c_profilefunc
,
2578 tstate
->c_profileobj
, f
,
2579 PyTrace_RETURN
, NULL
);
2580 else if (call_trace(tstate
->c_profilefunc
,
2581 tstate
->c_profileobj
, f
,
2582 PyTrace_RETURN
, retval
)) {
2585 why
= WHY_EXCEPTION
;
2590 if (tstate
->frame
->f_exc_type
!= NULL
)
2591 reset_exc_info(tstate
);
2593 assert(tstate
->frame
->f_exc_value
== NULL
);
2594 assert(tstate
->frame
->f_exc_traceback
== NULL
);
2599 Py_LeaveRecursiveCall();
2600 tstate
->frame
= f
->f_back
;
2605 /* This is gonna seem *real weird*, but if you put some other code between
2606 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2607 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2610 PyEval_EvalCodeEx(PyCodeObject
*co
, PyObject
*globals
, PyObject
*locals
,
2611 PyObject
**args
, int argcount
, PyObject
**kws
, int kwcount
,
2612 PyObject
**defs
, int defcount
, PyObject
*closure
)
2614 register PyFrameObject
*f
;
2615 register PyObject
*retval
= NULL
;
2616 register PyObject
**fastlocals
, **freevars
;
2617 PyThreadState
*tstate
= PyThreadState_GET();
2620 if (globals
== NULL
) {
2621 PyErr_SetString(PyExc_SystemError
,
2622 "PyEval_EvalCodeEx: NULL globals");
2626 assert(globals
!= NULL
);
2627 f
= PyFrame_New(tstate
, co
, globals
, locals
);
2631 fastlocals
= f
->f_localsplus
;
2632 freevars
= f
->f_localsplus
+ co
->co_nlocals
;
2634 if (co
->co_argcount
> 0 ||
2635 co
->co_flags
& (CO_VARARGS
| CO_VARKEYWORDS
)) {
2638 PyObject
*kwdict
= NULL
;
2639 if (co
->co_flags
& CO_VARKEYWORDS
) {
2640 kwdict
= PyDict_New();
2643 i
= co
->co_argcount
;
2644 if (co
->co_flags
& CO_VARARGS
)
2646 SETLOCAL(i
, kwdict
);
2648 if (argcount
> co
->co_argcount
) {
2649 if (!(co
->co_flags
& CO_VARARGS
)) {
2650 PyErr_Format(PyExc_TypeError
,
2651 "%.200s() takes %s %d "
2652 "%sargument%s (%d given)",
2653 PyString_AsString(co
->co_name
),
2654 defcount
? "at most" : "exactly",
2656 kwcount
? "non-keyword " : "",
2657 co
->co_argcount
== 1 ? "" : "s",
2661 n
= co
->co_argcount
;
2663 for (i
= 0; i
< n
; i
++) {
2668 if (co
->co_flags
& CO_VARARGS
) {
2669 u
= PyTuple_New(argcount
- n
);
2672 SETLOCAL(co
->co_argcount
, u
);
2673 for (i
= n
; i
< argcount
; i
++) {
2676 PyTuple_SET_ITEM(u
, i
-n
, x
);
2679 for (i
= 0; i
< kwcount
; i
++) {
2680 PyObject
*keyword
= kws
[2*i
];
2681 PyObject
*value
= kws
[2*i
+ 1];
2683 if (keyword
== NULL
|| !PyString_Check(keyword
)) {
2684 PyErr_Format(PyExc_TypeError
,
2685 "%.200s() keywords must be strings",
2686 PyString_AsString(co
->co_name
));
2689 /* XXX slow -- speed up using dictionary? */
2690 for (j
= 0; j
< co
->co_argcount
; j
++) {
2691 PyObject
*nm
= PyTuple_GET_ITEM(
2692 co
->co_varnames
, j
);
2693 int cmp
= PyObject_RichCompareBool(
2694 keyword
, nm
, Py_EQ
);
2700 /* Check errors from Compare */
2701 if (PyErr_Occurred())
2703 if (j
>= co
->co_argcount
) {
2704 if (kwdict
== NULL
) {
2705 PyErr_Format(PyExc_TypeError
,
2706 "%.200s() got an unexpected "
2707 "keyword argument '%.400s'",
2708 PyString_AsString(co
->co_name
),
2709 PyString_AsString(keyword
));
2712 PyDict_SetItem(kwdict
, keyword
, value
);
2715 if (GETLOCAL(j
) != NULL
) {
2716 PyErr_Format(PyExc_TypeError
,
2717 "%.200s() got multiple "
2718 "values for keyword "
2719 "argument '%.400s'",
2720 PyString_AsString(co
->co_name
),
2721 PyString_AsString(keyword
));
2728 if (argcount
< co
->co_argcount
) {
2729 int m
= co
->co_argcount
- defcount
;
2730 for (i
= argcount
; i
< m
; i
++) {
2731 if (GETLOCAL(i
) == NULL
) {
2732 PyErr_Format(PyExc_TypeError
,
2733 "%.200s() takes %s %d "
2734 "%sargument%s (%d given)",
2735 PyString_AsString(co
->co_name
),
2736 ((co
->co_flags
& CO_VARARGS
) ||
2737 defcount
) ? "at least"
2739 m
, kwcount
? "non-keyword " : "",
2740 m
== 1 ? "" : "s", i
);
2748 for (; i
< defcount
; i
++) {
2749 if (GETLOCAL(m
+i
) == NULL
) {
2750 PyObject
*def
= defs
[i
];
2758 if (argcount
> 0 || kwcount
> 0) {
2759 PyErr_Format(PyExc_TypeError
,
2760 "%.200s() takes no arguments (%d given)",
2761 PyString_AsString(co
->co_name
),
2762 argcount
+ kwcount
);
2766 /* Allocate and initialize storage for cell vars, and copy free
2767 vars into frame. This isn't too efficient right now. */
2768 if (PyTuple_GET_SIZE(co
->co_cellvars
)) {
2769 int i
, j
, nargs
, found
;
2770 char *cellname
, *argname
;
2773 nargs
= co
->co_argcount
;
2774 if (co
->co_flags
& CO_VARARGS
)
2776 if (co
->co_flags
& CO_VARKEYWORDS
)
2779 /* Initialize each cell var, taking into account
2780 cell vars that are initialized from arguments.
2782 Should arrange for the compiler to put cellvars
2783 that are arguments at the beginning of the cellvars
2784 list so that we can march over it more efficiently?
2786 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_cellvars
); ++i
) {
2787 cellname
= PyString_AS_STRING(
2788 PyTuple_GET_ITEM(co
->co_cellvars
, i
));
2790 for (j
= 0; j
< nargs
; j
++) {
2791 argname
= PyString_AS_STRING(
2792 PyTuple_GET_ITEM(co
->co_varnames
, j
));
2793 if (strcmp(cellname
, argname
) == 0) {
2794 c
= PyCell_New(GETLOCAL(j
));
2797 GETLOCAL(co
->co_nlocals
+ i
) = c
;
2803 c
= PyCell_New(NULL
);
2806 SETLOCAL(co
->co_nlocals
+ i
, c
);
2810 if (PyTuple_GET_SIZE(co
->co_freevars
)) {
2812 for (i
= 0; i
< PyTuple_GET_SIZE(co
->co_freevars
); ++i
) {
2813 PyObject
*o
= PyTuple_GET_ITEM(closure
, i
);
2815 freevars
[PyTuple_GET_SIZE(co
->co_cellvars
) + i
] = o
;
2819 if (co
->co_flags
& CO_GENERATOR
) {
2820 /* Don't need to keep the reference to f_back, it will be set
2821 * when the generator is resumed. */
2822 Py_XDECREF(f
->f_back
);
2825 PCALL(PCALL_GENERATOR
);
2827 /* Create a new generator that owns the ready to run frame
2828 * and return that as the value. */
2829 return PyGen_New(f
);
2832 retval
= PyEval_EvalFrameEx(f
,0);
2834 fail
: /* Jump here from prelude on failure */
2836 /* decref'ing the frame can cause __del__ methods to get invoked,
2837 which can call back into Python. While we're done with the
2838 current Python frame (f), the associated C stack is still in use,
2839 so recursion_depth must be boosted for the duration.
2841 assert(tstate
!= NULL
);
2842 ++tstate
->recursion_depth
;
2844 --tstate
->recursion_depth
;
2849 /* Implementation notes for set_exc_info() and reset_exc_info():
2851 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2852 'exc_traceback'. These always travel together.
2854 - tstate->curexc_ZZZ is the "hot" exception that is set by
2855 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2857 - Once an exception is caught by an except clause, it is transferred
2858 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2859 can pick it up. This is the primary task of set_exc_info().
2860 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
2862 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
2864 Long ago, when none of this existed, there were just a few globals:
2865 one set corresponding to the "hot" exception, and one set
2866 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2867 globals; they were simply stored as sys.exc_ZZZ. For backwards
2868 compatibility, they still are!) The problem was that in code like
2872 "something that may fail"
2873 except "some exception":
2874 "do something else first"
2875 "print the exception from sys.exc_ZZZ."
2877 if "do something else first" invoked something that raised and caught
2878 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2879 cause of subtle bugs. I fixed this by changing the semantics as
2882 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2885 - But initially, and as long as no exception is caught in a given
2886 frame, sys.exc_ZZZ will hold the last exception caught in the
2887 previous frame (or the frame before that, etc.).
2889 The first bullet fixed the bug in the above example. The second
2890 bullet was for backwards compatibility: it was (and is) common to
2891 have a function that is called when an exception is caught, and to
2892 have that function access the caught exception via sys.exc_ZZZ.
2893 (Example: traceback.print_exc()).
2895 At the same time I fixed the problem that sys.exc_ZZZ weren't
2896 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2897 but that's really a separate improvement.
2899 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2900 variables to what they were before the current frame was called. The
2901 set_exc_info() function saves them on the frame so that
2902 reset_exc_info() can restore them. The invariant is that
2903 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2904 exception (where "catching" an exception applies only to successful
2905 except clauses); and if the current frame ever caught an exception,
2906 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2907 at the start of the current frame.
2912 set_exc_info(PyThreadState
*tstate
,
2913 PyObject
*type
, PyObject
*value
, PyObject
*tb
)
2915 PyFrameObject
*frame
= tstate
->frame
;
2916 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
2918 assert(type
!= NULL
);
2919 assert(frame
!= NULL
);
2920 if (frame
->f_exc_type
== NULL
) {
2921 assert(frame
->f_exc_value
== NULL
);
2922 assert(frame
->f_exc_traceback
== NULL
);
2923 /* This frame didn't catch an exception before. */
2924 /* Save previous exception of this thread in this frame. */
2925 if (tstate
->exc_type
== NULL
) {
2926 /* XXX Why is this set to Py_None? */
2928 tstate
->exc_type
= Py_None
;
2930 Py_INCREF(tstate
->exc_type
);
2931 Py_XINCREF(tstate
->exc_value
);
2932 Py_XINCREF(tstate
->exc_traceback
);
2933 frame
->f_exc_type
= tstate
->exc_type
;
2934 frame
->f_exc_value
= tstate
->exc_value
;
2935 frame
->f_exc_traceback
= tstate
->exc_traceback
;
2937 /* Set new exception for this thread. */
2938 tmp_type
= tstate
->exc_type
;
2939 tmp_value
= tstate
->exc_value
;
2940 tmp_tb
= tstate
->exc_traceback
;
2944 tstate
->exc_type
= type
;
2945 tstate
->exc_value
= value
;
2946 tstate
->exc_traceback
= tb
;
2947 Py_XDECREF(tmp_type
);
2948 Py_XDECREF(tmp_value
);
2950 /* For b/w compatibility */
2951 PySys_SetObject("exc_type", type
);
2952 PySys_SetObject("exc_value", value
);
2953 PySys_SetObject("exc_traceback", tb
);
2957 reset_exc_info(PyThreadState
*tstate
)
2959 PyFrameObject
*frame
;
2960 PyObject
*tmp_type
, *tmp_value
, *tmp_tb
;
2962 /* It's a precondition that the thread state's frame caught an
2963 * exception -- verify in a debug build.
2965 assert(tstate
!= NULL
);
2966 frame
= tstate
->frame
;
2967 assert(frame
!= NULL
);
2968 assert(frame
->f_exc_type
!= NULL
);
2970 /* Copy the frame's exception info back to the thread state. */
2971 tmp_type
= tstate
->exc_type
;
2972 tmp_value
= tstate
->exc_value
;
2973 tmp_tb
= tstate
->exc_traceback
;
2974 Py_INCREF(frame
->f_exc_type
);
2975 Py_XINCREF(frame
->f_exc_value
);
2976 Py_XINCREF(frame
->f_exc_traceback
);
2977 tstate
->exc_type
= frame
->f_exc_type
;
2978 tstate
->exc_value
= frame
->f_exc_value
;
2979 tstate
->exc_traceback
= frame
->f_exc_traceback
;
2980 Py_XDECREF(tmp_type
);
2981 Py_XDECREF(tmp_value
);
2984 /* For b/w compatibility */
2985 PySys_SetObject("exc_type", frame
->f_exc_type
);
2986 PySys_SetObject("exc_value", frame
->f_exc_value
);
2987 PySys_SetObject("exc_traceback", frame
->f_exc_traceback
);
2989 /* Clear the frame's exception info. */
2990 tmp_type
= frame
->f_exc_type
;
2991 tmp_value
= frame
->f_exc_value
;
2992 tmp_tb
= frame
->f_exc_traceback
;
2993 frame
->f_exc_type
= NULL
;
2994 frame
->f_exc_value
= NULL
;
2995 frame
->f_exc_traceback
= NULL
;
2996 Py_DECREF(tmp_type
);
2997 Py_XDECREF(tmp_value
);
3001 /* Logic for the raise statement (too complicated for inlining).
3002 This *consumes* a reference count to each of its arguments. */
3003 static enum why_code
3004 do_raise(PyObject
*type
, PyObject
*value
, PyObject
*tb
)
3008 PyThreadState
*tstate
= PyThreadState_GET();
3009 type
= tstate
->exc_type
== NULL
? Py_None
: tstate
->exc_type
;
3010 value
= tstate
->exc_value
;
3011 tb
= tstate
->exc_traceback
;
3017 /* We support the following forms of raise:
3018 raise <class>, <classinstance>
3019 raise <class>, <argument tuple>
3021 raise <class>, <argument>
3022 raise <classinstance>, None
3023 raise <string>, <object>
3024 raise <string>, None
3026 An omitted second argument is the same as None.
3028 In addition, raise <tuple>, <anything> is the same as
3029 raising the tuple's first item (and it better have one!);
3030 this rule is applied recursively.
3032 Finally, an optional third argument can be supplied, which
3033 gives the traceback to be substituted (useful when
3034 re-raising an exception after examining it). */
3036 /* First, check the traceback argument, replacing None with
3038 if (tb
== Py_None
) {
3042 else if (tb
!= NULL
&& !PyTraceBack_Check(tb
)) {
3043 PyErr_SetString(PyExc_TypeError
,
3044 "raise: arg 3 must be a traceback or None");
3048 /* Next, replace a missing value with None */
3049 if (value
== NULL
) {
3054 /* Next, repeatedly, replace a tuple exception with its first item */
3055 while (PyTuple_Check(type
) && PyTuple_Size(type
) > 0) {
3056 PyObject
*tmp
= type
;
3057 type
= PyTuple_GET_ITEM(type
, 0);
3062 if (PyString_CheckExact(type
)) {
3063 /* Raising builtin string is deprecated but still allowed --
3064 * do nothing. Raising an instance of a new-style str
3065 * subclass is right out. */
3066 if (PyErr_Warn(PyExc_DeprecationWarning
,
3067 "raising a string exception is deprecated"))
3070 else if (PyExceptionClass_Check(type
))
3071 PyErr_NormalizeException(&type
, &value
, &tb
);
3073 else if (PyExceptionInstance_Check(type
)) {
3074 /* Raising an instance. The value should be a dummy. */
3075 if (value
!= Py_None
) {
3076 PyErr_SetString(PyExc_TypeError
,
3077 "instance exception may not have a separate value");
3081 /* Normalize to raise <class>, <instance> */
3084 type
= PyExceptionInstance_Class(type
);
3089 /* Not something you can raise. You get an exception
3090 anyway, just not what you specified :-) */
3091 PyErr_Format(PyExc_TypeError
,
3092 "exceptions must be classes, instances, or "
3093 "strings (deprecated), not %s",
3094 type
->ob_type
->tp_name
);
3097 PyErr_Restore(type
, value
, tb
);
3099 return WHY_EXCEPTION
;
3106 return WHY_EXCEPTION
;
3109 /* Iterate v argcnt times and store the results on the stack (via decreasing
3110 sp). Return 1 for success, 0 if error. */
3113 unpack_iterable(PyObject
*v
, int argcnt
, PyObject
**sp
)
3116 PyObject
*it
; /* iter(v) */
3121 it
= PyObject_GetIter(v
);
3125 for (; i
< argcnt
; i
++) {
3126 w
= PyIter_Next(it
);
3128 /* Iterator done, via error or exhaustion. */
3129 if (!PyErr_Occurred()) {
3130 PyErr_Format(PyExc_ValueError
,
3131 "need more than %d value%s to unpack",
3132 i
, i
== 1 ? "" : "s");
3139 /* We better have exhausted the iterator now. */
3140 w
= PyIter_Next(it
);
3142 if (PyErr_Occurred())
3148 PyErr_SetString(PyExc_ValueError
, "too many values to unpack");
3151 for (; i
> 0; i
--, sp
++)
3160 prtrace(PyObject
*v
, char *str
)
3163 if (PyObject_Print(v
, stdout
, 0) != 0)
3164 PyErr_Clear(); /* Don't know what else to do */
3171 call_exc_trace(Py_tracefunc func
, PyObject
*self
, PyFrameObject
*f
)
3173 PyObject
*type
, *value
, *traceback
, *arg
;
3175 PyErr_Fetch(&type
, &value
, &traceback
);
3176 if (value
== NULL
) {
3180 arg
= PyTuple_Pack(3, type
, value
, traceback
);
3182 PyErr_Restore(type
, value
, traceback
);
3185 err
= call_trace(func
, self
, f
, PyTrace_EXCEPTION
, arg
);
3188 PyErr_Restore(type
, value
, traceback
);
3192 Py_XDECREF(traceback
);
3197 call_trace_protected(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3198 int what
, PyObject
*arg
)
3200 PyObject
*type
, *value
, *traceback
;
3202 PyErr_Fetch(&type
, &value
, &traceback
);
3203 err
= call_trace(func
, obj
, frame
, what
, arg
);
3205 PyErr_Restore(type
, value
, traceback
);
3209 Py_XDECREF(traceback
);
3214 call_trace(Py_tracefunc func
, PyObject
*obj
, PyFrameObject
*frame
,
3215 int what
, PyObject
*arg
)
3217 register PyThreadState
*tstate
= frame
->f_tstate
;
3219 if (tstate
->tracing
)
3222 tstate
->use_tracing
= 0;
3223 result
= func(obj
, frame
, what
, arg
);
3224 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3225 || (tstate
->c_profilefunc
!= NULL
));
3231 _PyEval_CallTracing(PyObject
*func
, PyObject
*args
)
3233 PyFrameObject
*frame
= PyEval_GetFrame();
3234 PyThreadState
*tstate
= frame
->f_tstate
;
3235 int save_tracing
= tstate
->tracing
;
3236 int save_use_tracing
= tstate
->use_tracing
;
3239 tstate
->tracing
= 0;
3240 tstate
->use_tracing
= ((tstate
->c_tracefunc
!= NULL
)
3241 || (tstate
->c_profilefunc
!= NULL
));
3242 result
= PyObject_Call(func
, args
, NULL
);
3243 tstate
->tracing
= save_tracing
;
3244 tstate
->use_tracing
= save_use_tracing
;
3249 maybe_call_line_trace(Py_tracefunc func
, PyObject
*obj
,
3250 PyFrameObject
*frame
, int *instr_lb
, int *instr_ub
,
3255 /* If the last instruction executed isn't in the current
3256 instruction window, reset the window. If the last
3257 instruction happens to fall at the start of a line or if it
3258 represents a jump backwards, call the trace function.
3260 if ((frame
->f_lasti
< *instr_lb
|| frame
->f_lasti
>= *instr_ub
)) {
3264 line
= PyCode_CheckLineNumber(frame
->f_code
, frame
->f_lasti
,
3267 frame
->f_lineno
= line
;
3268 result
= call_trace(func
, obj
, frame
,
3269 PyTrace_LINE
, Py_None
);
3271 *instr_lb
= bounds
.ap_lower
;
3272 *instr_ub
= bounds
.ap_upper
;
3274 else if (frame
->f_lasti
<= *instr_prev
) {
3275 result
= call_trace(func
, obj
, frame
, PyTrace_LINE
, Py_None
);
3277 *instr_prev
= frame
->f_lasti
;
3282 PyEval_SetProfile(Py_tracefunc func
, PyObject
*arg
)
3284 PyThreadState
*tstate
= PyThreadState_GET();
3285 PyObject
*temp
= tstate
->c_profileobj
;
3287 tstate
->c_profilefunc
= NULL
;
3288 tstate
->c_profileobj
= NULL
;
3289 /* Must make sure that tracing is not ignored if 'temp' is freed */
3290 tstate
->use_tracing
= tstate
->c_tracefunc
!= NULL
;
3292 tstate
->c_profilefunc
= func
;
3293 tstate
->c_profileobj
= arg
;
3294 /* Flag that tracing or profiling is turned on */
3295 tstate
->use_tracing
= (func
!= NULL
) || (tstate
->c_tracefunc
!= NULL
);
3299 PyEval_SetTrace(Py_tracefunc func
, PyObject
*arg
)
3301 PyThreadState
*tstate
= PyThreadState_GET();
3302 PyObject
*temp
= tstate
->c_traceobj
;
3304 tstate
->c_tracefunc
= NULL
;
3305 tstate
->c_traceobj
= NULL
;
3306 /* Must make sure that profiling is not ignored if 'temp' is freed */
3307 tstate
->use_tracing
= tstate
->c_profilefunc
!= NULL
;
3309 tstate
->c_tracefunc
= func
;
3310 tstate
->c_traceobj
= arg
;
3311 /* Flag that tracing or profiling is turned on */
3312 tstate
->use_tracing
= ((func
!= NULL
)
3313 || (tstate
->c_profilefunc
!= NULL
));
3317 PyEval_GetBuiltins(void)
3319 PyFrameObject
*current_frame
= PyEval_GetFrame();
3320 if (current_frame
== NULL
)
3321 return PyThreadState_GET()->interp
->builtins
;
3323 return current_frame
->f_builtins
;
3327 PyEval_GetLocals(void)
3329 PyFrameObject
*current_frame
= PyEval_GetFrame();
3330 if (current_frame
== NULL
)
3332 PyFrame_FastToLocals(current_frame
);
3333 return current_frame
->f_locals
;
3337 PyEval_GetGlobals(void)
3339 PyFrameObject
*current_frame
= PyEval_GetFrame();
3340 if (current_frame
== NULL
)
3343 return current_frame
->f_globals
;
3347 PyEval_GetFrame(void)
3349 PyThreadState
*tstate
= PyThreadState_GET();
3350 return _PyThreadState_GetFrame(tstate
);
3354 PyEval_GetRestricted(void)
3356 PyFrameObject
*current_frame
= PyEval_GetFrame();
3357 return current_frame
== NULL
? 0 : PyFrame_IsRestricted(current_frame
);
3361 PyEval_MergeCompilerFlags(PyCompilerFlags
*cf
)
3363 PyFrameObject
*current_frame
= PyEval_GetFrame();
3364 int result
= cf
->cf_flags
!= 0;
3366 if (current_frame
!= NULL
) {
3367 const int codeflags
= current_frame
->f_code
->co_flags
;
3368 const int compilerflags
= codeflags
& PyCF_MASK
;
3369 if (compilerflags
) {
3371 cf
->cf_flags
|= compilerflags
;
3373 #if 0 /* future keyword */
3374 if (codeflags
& CO_GENERATOR_ALLOWED
) {
3376 cf
->cf_flags
|= CO_GENERATOR_ALLOWED
;
3386 PyObject
*f
= PySys_GetObject("stdout");
3389 if (!PyFile_SoftSpace(f
, 0))
3391 return PyFile_WriteString("\n", f
);
3395 /* External interface to call any callable object.
3396 The arg must be a tuple or NULL. */
3398 #undef PyEval_CallObject
3399 /* for backward compatibility: export this interface */
3402 PyEval_CallObject(PyObject
*func
, PyObject
*arg
)
3404 return PyEval_CallObjectWithKeywords(func
, arg
, (PyObject
*)NULL
);
3406 #define PyEval_CallObject(func,arg) \
3407 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3410 PyEval_CallObjectWithKeywords(PyObject
*func
, PyObject
*arg
, PyObject
*kw
)
3415 arg
= PyTuple_New(0);
3419 else if (!PyTuple_Check(arg
)) {
3420 PyErr_SetString(PyExc_TypeError
,
3421 "argument list must be a tuple");
3427 if (kw
!= NULL
&& !PyDict_Check(kw
)) {
3428 PyErr_SetString(PyExc_TypeError
,
3429 "keyword list must be a dictionary");
3434 result
= PyObject_Call(func
, arg
, kw
);
3440 PyEval_GetFuncName(PyObject
*func
)
3442 if (PyMethod_Check(func
))
3443 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func
));
3444 else if (PyFunction_Check(func
))
3445 return PyString_AsString(((PyFunctionObject
*)func
)->func_name
);
3446 else if (PyCFunction_Check(func
))
3447 return ((PyCFunctionObject
*)func
)->m_ml
->ml_name
;
3448 else if (PyClass_Check(func
))
3449 return PyString_AsString(((PyClassObject
*)func
)->cl_name
);
3450 else if (PyInstance_Check(func
)) {
3451 return PyString_AsString(
3452 ((PyInstanceObject
*)func
)->in_class
->cl_name
);
3454 return func
->ob_type
->tp_name
;
3459 PyEval_GetFuncDesc(PyObject
*func
)
3461 if (PyMethod_Check(func
))
3463 else if (PyFunction_Check(func
))
3465 else if (PyCFunction_Check(func
))
3467 else if (PyClass_Check(func
))
3468 return " constructor";
3469 else if (PyInstance_Check(func
)) {
3477 err_args(PyObject
*func
, int flags
, int nargs
)
3479 if (flags
& METH_NOARGS
)
3480 PyErr_Format(PyExc_TypeError
,
3481 "%.200s() takes no arguments (%d given)",
3482 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3485 PyErr_Format(PyExc_TypeError
,
3486 "%.200s() takes exactly one argument (%d given)",
3487 ((PyCFunctionObject
*)func
)->m_ml
->ml_name
,
3491 #define C_TRACE(x, call) \
3492 if (tstate->use_tracing && tstate->c_profilefunc) { \
3493 if (call_trace(tstate->c_profilefunc, \
3494 tstate->c_profileobj, \
3495 tstate->frame, PyTrace_C_CALL, \
3501 if (tstate->c_profilefunc != NULL) { \
3503 call_trace_protected(tstate->c_profilefunc, \
3504 tstate->c_profileobj, \
3505 tstate->frame, PyTrace_C_EXCEPTION, \
3507 /* XXX should pass (type, value, tb) */ \
3509 if (call_trace(tstate->c_profilefunc, \
3510 tstate->c_profileobj, \
3511 tstate->frame, PyTrace_C_RETURN, \
3524 call_function(PyObject
***pp_stack
, int oparg
3526 , uint64
* pintr0
, uint64
* pintr1
3530 int na
= oparg
& 0xff;
3531 int nk
= (oparg
>>8) & 0xff;
3532 int n
= na
+ 2 * nk
;
3533 PyObject
**pfunc
= (*pp_stack
) - n
- 1;
3534 PyObject
*func
= *pfunc
;
3537 /* Always dispatch PyCFunction first, because these are
3538 presumed to be the most frequent callable object.
3540 if (PyCFunction_Check(func
) && nk
== 0) {
3541 int flags
= PyCFunction_GET_FLAGS(func
);
3542 PyThreadState
*tstate
= PyThreadState_GET();
3544 PCALL(PCALL_CFUNCTION
);
3545 if (flags
& (METH_NOARGS
| METH_O
)) {
3546 PyCFunction meth
= PyCFunction_GET_FUNCTION(func
);
3547 PyObject
*self
= PyCFunction_GET_SELF(func
);
3548 if (flags
& METH_NOARGS
&& na
== 0) {
3549 C_TRACE(x
, (*meth
)(self
,NULL
));
3551 else if (flags
& METH_O
&& na
== 1) {
3552 PyObject
*arg
= EXT_POP(*pp_stack
);
3553 C_TRACE(x
, (*meth
)(self
,arg
));
3557 err_args(func
, flags
, na
);
3563 callargs
= load_args(pp_stack
, na
);
3564 READ_TIMESTAMP(*pintr0
);
3565 C_TRACE(x
, PyCFunction_Call(func
,callargs
,NULL
));
3566 READ_TIMESTAMP(*pintr1
);
3567 Py_XDECREF(callargs
);
3570 if (PyMethod_Check(func
) && PyMethod_GET_SELF(func
) != NULL
) {
3571 /* optimize access to bound methods */
3572 PyObject
*self
= PyMethod_GET_SELF(func
);
3573 PCALL(PCALL_METHOD
);
3574 PCALL(PCALL_BOUND_METHOD
);
3576 func
= PyMethod_GET_FUNCTION(func
);
3584 READ_TIMESTAMP(*pintr0
);
3585 if (PyFunction_Check(func
))
3586 x
= fast_function(func
, pp_stack
, n
, na
, nk
);
3588 x
= do_call(func
, pp_stack
, na
, nk
);
3589 READ_TIMESTAMP(*pintr1
);
3593 /* Clear the stack of the function object. Also removes
3594 the arguments in case they weren't consumed already
3595 (fast_function() and err_args() leave them on the stack).
3597 while ((*pp_stack
) > pfunc
) {
3598 w
= EXT_POP(*pp_stack
);
3605 /* The fast_function() function optimize calls for which no argument
3606 tuple is necessary; the objects are passed directly from the stack.
3607 For the simplest case -- a function that takes only positional
3608 arguments and is called with only positional arguments -- it
3609 inlines the most primitive frame setup code from
3610 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3611 done before evaluating the frame.
3615 fast_function(PyObject
*func
, PyObject
***pp_stack
, int n
, int na
, int nk
)
3617 PyCodeObject
*co
= (PyCodeObject
*)PyFunction_GET_CODE(func
);
3618 PyObject
*globals
= PyFunction_GET_GLOBALS(func
);
3619 PyObject
*argdefs
= PyFunction_GET_DEFAULTS(func
);
3620 PyObject
**d
= NULL
;
3623 PCALL(PCALL_FUNCTION
);
3624 PCALL(PCALL_FAST_FUNCTION
);
3625 if (argdefs
== NULL
&& co
->co_argcount
== n
&& nk
==0 &&
3626 co
->co_flags
== (CO_OPTIMIZED
| CO_NEWLOCALS
| CO_NOFREE
)) {
3628 PyObject
*retval
= NULL
;
3629 PyThreadState
*tstate
= PyThreadState_GET();
3630 PyObject
**fastlocals
, **stack
;
3633 PCALL(PCALL_FASTER_FUNCTION
);
3634 assert(globals
!= NULL
);
3635 /* XXX Perhaps we should create a specialized
3636 PyFrame_New() that doesn't take locals, but does
3637 take builtins without sanity checking them.
3639 f
= PyFrame_New(tstate
, co
, globals
, NULL
);
3643 fastlocals
= f
->f_localsplus
;
3644 stack
= (*pp_stack
) - n
;
3646 for (i
= 0; i
< n
; i
++) {
3648 fastlocals
[i
] = *stack
++;
3650 retval
= PyEval_EvalFrameEx(f
,0);
3651 assert(tstate
!= NULL
);
3652 ++tstate
->recursion_depth
;
3654 --tstate
->recursion_depth
;
3657 if (argdefs
!= NULL
) {
3658 d
= &PyTuple_GET_ITEM(argdefs
, 0);
3659 nd
= ((PyTupleObject
*)argdefs
)->ob_size
;
3661 return PyEval_EvalCodeEx(co
, globals
,
3662 (PyObject
*)NULL
, (*pp_stack
)-n
, na
,
3663 (*pp_stack
)-2*nk
, nk
, d
, nd
,
3664 PyFunction_GET_CLOSURE(func
));
3668 update_keyword_args(PyObject
*orig_kwdict
, int nk
, PyObject
***pp_stack
,
3671 PyObject
*kwdict
= NULL
;
3672 if (orig_kwdict
== NULL
)
3673 kwdict
= PyDict_New();
3675 kwdict
= PyDict_Copy(orig_kwdict
);
3676 Py_DECREF(orig_kwdict
);
3682 PyObject
*value
= EXT_POP(*pp_stack
);
3683 PyObject
*key
= EXT_POP(*pp_stack
);
3684 if (PyDict_GetItem(kwdict
, key
) != NULL
) {
3685 PyErr_Format(PyExc_TypeError
,
3686 "%.200s%s got multiple values "
3687 "for keyword argument '%.200s'",
3688 PyEval_GetFuncName(func
),
3689 PyEval_GetFuncDesc(func
),
3690 PyString_AsString(key
));
3696 err
= PyDict_SetItem(kwdict
, key
, value
);
3708 update_star_args(int nstack
, int nstar
, PyObject
*stararg
,
3709 PyObject
***pp_stack
)
3711 PyObject
*callargs
, *w
;
3713 callargs
= PyTuple_New(nstack
+ nstar
);
3714 if (callargs
== NULL
) {
3719 for (i
= 0; i
< nstar
; i
++) {
3720 PyObject
*a
= PyTuple_GET_ITEM(stararg
, i
);
3722 PyTuple_SET_ITEM(callargs
, nstack
+ i
, a
);
3725 while (--nstack
>= 0) {
3726 w
= EXT_POP(*pp_stack
);
3727 PyTuple_SET_ITEM(callargs
, nstack
, w
);
3733 load_args(PyObject
***pp_stack
, int na
)
3735 PyObject
*args
= PyTuple_New(na
);
3741 w
= EXT_POP(*pp_stack
);
3742 PyTuple_SET_ITEM(args
, na
, w
);
3748 do_call(PyObject
*func
, PyObject
***pp_stack
, int na
, int nk
)
3750 PyObject
*callargs
= NULL
;
3751 PyObject
*kwdict
= NULL
;
3752 PyObject
*result
= NULL
;
3755 kwdict
= update_keyword_args(NULL
, nk
, pp_stack
, func
);
3759 callargs
= load_args(pp_stack
, na
);
3760 if (callargs
== NULL
)
3763 /* At this point, we have to look at the type of func to
3764 update the call stats properly. Do it here so as to avoid
3765 exposing the call stats machinery outside ceval.c
3767 if (PyFunction_Check(func
))
3768 PCALL(PCALL_FUNCTION
);
3769 else if (PyMethod_Check(func
))
3770 PCALL(PCALL_METHOD
);
3771 else if (PyType_Check(func
))
3776 result
= PyObject_Call(func
, callargs
, kwdict
);
3778 Py_XDECREF(callargs
);
3784 ext_do_call(PyObject
*func
, PyObject
***pp_stack
, int flags
, int na
, int nk
)
3787 PyObject
*callargs
= NULL
;
3788 PyObject
*stararg
= NULL
;
3789 PyObject
*kwdict
= NULL
;
3790 PyObject
*result
= NULL
;
3792 if (flags
& CALL_FLAG_KW
) {
3793 kwdict
= EXT_POP(*pp_stack
);
3794 if (!(kwdict
&& PyDict_Check(kwdict
))) {
3795 PyErr_Format(PyExc_TypeError
,
3796 "%s%s argument after ** "
3797 "must be a dictionary",
3798 PyEval_GetFuncName(func
),
3799 PyEval_GetFuncDesc(func
));
3803 if (flags
& CALL_FLAG_VAR
) {
3804 stararg
= EXT_POP(*pp_stack
);
3805 if (!PyTuple_Check(stararg
)) {
3807 t
= PySequence_Tuple(stararg
);
3809 if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
3810 PyErr_Format(PyExc_TypeError
,
3811 "%s%s argument after * "
3812 "must be a sequence",
3813 PyEval_GetFuncName(func
),
3814 PyEval_GetFuncDesc(func
));
3821 nstar
= PyTuple_GET_SIZE(stararg
);
3824 kwdict
= update_keyword_args(kwdict
, nk
, pp_stack
, func
);
3828 callargs
= update_star_args(na
, nstar
, stararg
, pp_stack
);
3829 if (callargs
== NULL
)
3832 /* At this point, we have to look at the type of func to
3833 update the call stats properly. Do it here so as to avoid
3834 exposing the call stats machinery outside ceval.c
3836 if (PyFunction_Check(func
))
3837 PCALL(PCALL_FUNCTION
);
3838 else if (PyMethod_Check(func
))
3839 PCALL(PCALL_METHOD
);
3840 else if (PyType_Check(func
))
3845 result
= PyObject_Call(func
, callargs
, kwdict
);
3847 Py_XDECREF(callargs
);
3849 Py_XDECREF(stararg
);
3853 /* Extract a slice index from a PyInt or PyLong or an object with the
3854 nb_index slot defined, and store in *pi.
3855 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3856 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
3857 Return 0 on error, 1 on success.
3859 /* Note: If v is NULL, return success without storing into *pi. This
3860 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3861 called by the SLICE opcode with v and/or w equal to NULL.
3864 _PyEval_SliceIndex(PyObject
*v
, Py_ssize_t
*pi
)
3868 if (PyInt_Check(v
)) {
3869 x
= PyInt_AsSsize_t(v
);
3871 else if (v
->ob_type
->tp_as_number
&&
3872 PyType_HasFeature(v
->ob_type
, Py_TPFLAGS_HAVE_INDEX
)
3873 && v
->ob_type
->tp_as_number
->nb_index
) {
3874 x
= v
->ob_type
->tp_as_number
->nb_index(v
);
3875 if (x
== -1 && PyErr_Occurred())
3879 PyErr_SetString(PyExc_TypeError
,
3880 "slice indices must be integers or "
3881 "None or have an __index__ method");
3890 #define ISINDEX(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x) || \
3891 ((x)->ob_type->tp_as_number && \
3892 PyType_HasFeature((x)->ob_type, Py_TPFLAGS_HAVE_INDEX) \
3893 && (x)->ob_type->tp_as_number->nb_index))
3896 apply_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
) /* return u[v:w] */
3898 PyTypeObject
*tp
= u
->ob_type
;
3899 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
3901 if (sq
&& sq
->sq_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
3902 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
3903 if (!_PyEval_SliceIndex(v
, &ilow
))
3905 if (!_PyEval_SliceIndex(w
, &ihigh
))
3907 return PySequence_GetSlice(u
, ilow
, ihigh
);
3910 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
3911 if (slice
!= NULL
) {
3912 PyObject
*res
= PyObject_GetItem(u
, slice
);
3922 assign_slice(PyObject
*u
, PyObject
*v
, PyObject
*w
, PyObject
*x
)
3925 PyTypeObject
*tp
= u
->ob_type
;
3926 PySequenceMethods
*sq
= tp
->tp_as_sequence
;
3928 if (sq
&& sq
->sq_slice
&& ISINDEX(v
) && ISINDEX(w
)) {
3929 Py_ssize_t ilow
= 0, ihigh
= PY_SSIZE_T_MAX
;
3930 if (!_PyEval_SliceIndex(v
, &ilow
))
3932 if (!_PyEval_SliceIndex(w
, &ihigh
))
3935 return PySequence_DelSlice(u
, ilow
, ihigh
);
3937 return PySequence_SetSlice(u
, ilow
, ihigh
, x
);
3940 PyObject
*slice
= PySlice_New(v
, w
, NULL
);
3941 if (slice
!= NULL
) {
3944 res
= PyObject_SetItem(u
, slice
, x
);
3946 res
= PyObject_DelItem(u
, slice
);
3956 cmp_outcome(int op
, register PyObject
*v
, register PyObject
*w
)
3967 res
= PySequence_Contains(w
, v
);
3972 res
= PySequence_Contains(w
, v
);
3977 case PyCmp_EXC_MATCH
:
3978 res
= PyErr_GivenExceptionMatches(v
, w
);
3981 return PyObject_RichCompare(v
, w
, op
);
3983 v
= res
? Py_True
: Py_False
;
3989 import_from(PyObject
*v
, PyObject
*name
)
3993 x
= PyObject_GetAttr(v
, name
);
3994 if (x
== NULL
&& PyErr_ExceptionMatches(PyExc_AttributeError
)) {
3995 PyErr_Format(PyExc_ImportError
,
3996 "cannot import name %.230s",
3997 PyString_AsString(name
));
4003 import_all_from(PyObject
*locals
, PyObject
*v
)
4005 PyObject
*all
= PyObject_GetAttrString(v
, "__all__");
4006 PyObject
*dict
, *name
, *value
;
4007 int skip_leading_underscores
= 0;
4011 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4012 return -1; /* Unexpected error */
4014 dict
= PyObject_GetAttrString(v
, "__dict__");
4016 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
4018 PyErr_SetString(PyExc_ImportError
,
4019 "from-import-* object has no __dict__ and no __all__");
4022 all
= PyMapping_Keys(dict
);
4026 skip_leading_underscores
= 1;
4029 for (pos
= 0, err
= 0; ; pos
++) {
4030 name
= PySequence_GetItem(all
, pos
);
4032 if (!PyErr_ExceptionMatches(PyExc_IndexError
))
4038 if (skip_leading_underscores
&&
4039 PyString_Check(name
) &&
4040 PyString_AS_STRING(name
)[0] == '_')
4045 value
= PyObject_GetAttr(v
, name
);
4049 err
= PyDict_SetItem(locals
, name
, value
);
4060 build_class(PyObject
*methods
, PyObject
*bases
, PyObject
*name
)
4062 PyObject
*metaclass
= NULL
, *result
, *base
;
4064 if (PyDict_Check(methods
))
4065 metaclass
= PyDict_GetItemString(methods
, "__metaclass__");
4066 if (metaclass
!= NULL
)
4067 Py_INCREF(metaclass
);
4068 else if (PyTuple_Check(bases
) && PyTuple_GET_SIZE(bases
) > 0) {
4069 base
= PyTuple_GET_ITEM(bases
, 0);
4070 metaclass
= PyObject_GetAttrString(base
, "__class__");
4071 if (metaclass
== NULL
) {
4073 metaclass
= (PyObject
*)base
->ob_type
;
4074 Py_INCREF(metaclass
);
4078 PyObject
*g
= PyEval_GetGlobals();
4079 if (g
!= NULL
&& PyDict_Check(g
))
4080 metaclass
= PyDict_GetItemString(g
, "__metaclass__");
4081 if (metaclass
== NULL
)
4082 metaclass
= (PyObject
*) &PyClass_Type
;
4083 Py_INCREF(metaclass
);
4085 result
= PyObject_CallFunctionObjArgs(metaclass
, name
, bases
, methods
, NULL
);
4086 Py_DECREF(metaclass
);
4087 if (result
== NULL
&& PyErr_ExceptionMatches(PyExc_TypeError
)) {
4088 /* A type error here likely means that the user passed
4089 in a base that was not a class (such the random module
4090 instead of the random.random type). Help them out with
4091 by augmenting the error message with more information.*/
4093 PyObject
*ptype
, *pvalue
, *ptraceback
;
4095 PyErr_Fetch(&ptype
, &pvalue
, &ptraceback
);
4096 if (PyString_Check(pvalue
)) {
4098 newmsg
= PyString_FromFormat(
4099 "Error when calling the metaclass bases\n %s",
4100 PyString_AS_STRING(pvalue
));
4101 if (newmsg
!= NULL
) {
4106 PyErr_Restore(ptype
, pvalue
, ptraceback
);
4112 exec_statement(PyFrameObject
*f
, PyObject
*prog
, PyObject
*globals
,
4119 if (PyTuple_Check(prog
) && globals
== Py_None
&& locals
== Py_None
&&
4120 ((n
= PyTuple_Size(prog
)) == 2 || n
== 3)) {
4121 /* Backward compatibility hack */
4122 globals
= PyTuple_GetItem(prog
, 1);
4124 locals
= PyTuple_GetItem(prog
, 2);
4125 prog
= PyTuple_GetItem(prog
, 0);
4127 if (globals
== Py_None
) {
4128 globals
= PyEval_GetGlobals();
4129 if (locals
== Py_None
) {
4130 locals
= PyEval_GetLocals();
4134 else if (locals
== Py_None
)
4136 if (!PyString_Check(prog
) &&
4137 !PyUnicode_Check(prog
) &&
4138 !PyCode_Check(prog
) &&
4139 !PyFile_Check(prog
)) {
4140 PyErr_SetString(PyExc_TypeError
,
4141 "exec: arg 1 must be a string, file, or code object");
4144 if (!PyDict_Check(globals
)) {
4145 PyErr_SetString(PyExc_TypeError
,
4146 "exec: arg 2 must be a dictionary or None");
4149 if (!PyMapping_Check(locals
)) {
4150 PyErr_SetString(PyExc_TypeError
,
4151 "exec: arg 3 must be a mapping or None");
4154 if (PyDict_GetItemString(globals
, "__builtins__") == NULL
)
4155 PyDict_SetItemString(globals
, "__builtins__", f
->f_builtins
);
4156 if (PyCode_Check(prog
)) {
4157 if (PyCode_GetNumFree((PyCodeObject
*)prog
) > 0) {
4158 PyErr_SetString(PyExc_TypeError
,
4159 "code object passed to exec may not contain free variables");
4162 v
= PyEval_EvalCode((PyCodeObject
*) prog
, globals
, locals
);
4164 else if (PyFile_Check(prog
)) {
4165 FILE *fp
= PyFile_AsFile(prog
);
4166 char *name
= PyString_AsString(PyFile_Name(prog
));
4169 if (PyEval_MergeCompilerFlags(&cf
))
4170 v
= PyRun_FileFlags(fp
, name
, Py_file_input
, globals
,
4173 v
= PyRun_File(fp
, name
, Py_file_input
, globals
,
4177 PyObject
*tmp
= NULL
;
4181 #ifdef Py_USING_UNICODE
4182 if (PyUnicode_Check(prog
)) {
4183 tmp
= PyUnicode_AsUTF8String(prog
);
4187 cf
.cf_flags
|= PyCF_SOURCE_IS_UTF8
;
4190 if (PyString_AsStringAndSize(prog
, &str
, NULL
))
4192 if (PyEval_MergeCompilerFlags(&cf
))
4193 v
= PyRun_StringFlags(str
, Py_file_input
, globals
,
4196 v
= PyRun_String(str
, Py_file_input
, globals
, locals
);
4200 PyFrame_LocalsToFast(f
, 0);
4208 format_exc_check_arg(PyObject
*exc
, char *format_str
, PyObject
*obj
)
4215 obj_str
= PyString_AsString(obj
);
4219 PyErr_Format(exc
, format_str
, obj_str
);
4223 string_concatenate(PyObject
*v
, PyObject
*w
,
4224 PyFrameObject
*f
, unsigned char *next_instr
)
4226 /* This function implements 'variable += expr' when both arguments
4229 if (v
->ob_refcnt
== 2) {
4230 /* In the common case, there are 2 references to the value
4231 * stored in 'variable' when the += is performed: one on the
4232 * value stack (in 'v') and one still stored in the 'variable'.
4233 * We try to delete the variable now to reduce the refcnt to 1.
4235 switch (*next_instr
) {
4238 int oparg
= PEEKARG();
4239 PyObject
**fastlocals
= f
->f_localsplus
;
4240 if (GETLOCAL(oparg
) == v
)
4241 SETLOCAL(oparg
, NULL
);
4246 PyObject
**freevars
= f
->f_localsplus
+ f
->f_code
->co_nlocals
;
4247 PyObject
*c
= freevars
[PEEKARG()];
4248 if (PyCell_GET(c
) == v
)
4249 PyCell_Set(c
, NULL
);
4254 PyObject
*names
= f
->f_code
->co_names
;
4255 PyObject
*name
= GETITEM(names
, PEEKARG());
4256 PyObject
*locals
= f
->f_locals
;
4257 if (PyDict_CheckExact(locals
) &&
4258 PyDict_GetItem(locals
, name
) == v
) {
4259 if (PyDict_DelItem(locals
, name
) != 0) {
4268 if (v
->ob_refcnt
== 1 && !PyString_CHECK_INTERNED(v
)) {
4269 /* Now we own the last reference to 'v', so we can resize it
4272 Py_ssize_t v_len
= PyString_GET_SIZE(v
);
4273 Py_ssize_t w_len
= PyString_GET_SIZE(w
);
4274 if (_PyString_Resize(&v
, v_len
+ w_len
) != 0) {
4275 /* XXX if _PyString_Resize() fails, 'v' has been
4276 * deallocated so it cannot be put back into 'variable'.
4277 * The MemoryError is raised when there is no value in
4278 * 'variable', which might (very remotely) be a cause
4279 * of incompatibilities.
4283 /* copy 'w' into the newly allocated area of 'v' */
4284 memcpy(PyString_AS_STRING(v
) + v_len
,
4285 PyString_AS_STRING(w
), w_len
);
4289 /* When in-place resizing is not an option. */
4290 PyString_Concat(&v
, w
);
4295 #ifdef DYNAMIC_EXECUTION_PROFILE
4298 getarray(long a
[256])
4301 PyObject
*l
= PyList_New(256);
4302 if (l
== NULL
) return NULL
;
4303 for (i
= 0; i
< 256; i
++) {
4304 PyObject
*x
= PyInt_FromLong(a
[i
]);
4309 PyList_SetItem(l
, i
, x
);
4311 for (i
= 0; i
< 256; i
++)
4317 _Py_GetDXProfile(PyObject
*self
, PyObject
*args
)
4320 return getarray(dxp
);
4323 PyObject
*l
= PyList_New(257);
4324 if (l
== NULL
) return NULL
;
4325 for (i
= 0; i
< 257; i
++) {
4326 PyObject
*x
= getarray(dxpairs
[i
]);
4331 PyList_SetItem(l
, i
, x
);