Minor date change.
[python.git] / Python / ceval.c
blob245b08cf23f27684b25179caaa0d8b135e2069e7
2 /* Execute compiled code */
4 /* XXX TO DO:
5 XXX speed up searching for keywords by using a dictionary
6 XXX document it!
7 */
9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE
12 #include "Python.h"
14 #include "code.h"
15 #include "frameobject.h"
16 #include "eval.h"
17 #include "opcode.h"
18 #include "structmember.h"
20 #include <ctype.h>
22 #ifndef WITH_TSC
24 #define READ_TIMESTAMP(var)
26 #else
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)
37 static void
38 ppc_getcounter(uint64 *v)
40 register unsigned long tbu, tb, tbu2;
42 loop:
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;
51 ((long*)(v))[1] = tb;
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))
59 #endif
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)
67 return;
68 intr = intr1 - intr0;
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);
75 #endif
77 /* Turn this on if your compiler chokes on the big switch: */
78 /* #define CASE_TOO_BIG 1 */
80 #ifdef Py_DEBUG
81 /* For debugging the interpreter: */
82 #define LLTRACE 1 /* Low-level trace feature */
83 #define CHECKEXC 1 /* Double-check exception checking */
84 #endif
86 typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
88 /* Forward declarations */
89 #ifdef WITH_TSC
90 static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
91 #else
92 static PyObject * call_function(PyObject ***, int);
93 #endif
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 ***,
98 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
104 #ifdef LLTRACE
105 static int lltrace;
106 static int prtrace(PyObject *, char *);
107 #endif
108 static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
109 int, PyObject *);
110 static void call_trace_protected(Py_tracefunc, PyObject *,
111 PyFrameObject *, int, PyObject *);
112 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
113 static int maybe_call_line_trace(Py_tracefunc, PyObject *,
114 PyFrameObject *, int *, int *, int *);
116 static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
117 static int assign_slice(PyObject *, PyObject *,
118 PyObject *, PyObject *);
119 static PyObject * cmp_outcome(int, PyObject *, PyObject *);
120 static PyObject * import_from(PyObject *, PyObject *);
121 static int import_all_from(PyObject *, PyObject *);
122 static PyObject * build_class(PyObject *, PyObject *, PyObject *);
123 static int exec_statement(PyFrameObject *,
124 PyObject *, PyObject *, PyObject *);
125 static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
126 static void reset_exc_info(PyThreadState *);
127 static void format_exc_check_arg(PyObject *, char *, PyObject *);
128 static PyObject * string_concatenate(PyObject *, PyObject *,
129 PyFrameObject *, unsigned char *);
131 #define NAME_ERROR_MSG \
132 "name '%.200s' is not defined"
133 #define GLOBAL_NAME_ERROR_MSG \
134 "global name '%.200s' is not defined"
135 #define UNBOUNDLOCAL_ERROR_MSG \
136 "local variable '%.200s' referenced before assignment"
137 #define UNBOUNDFREE_ERROR_MSG \
138 "free variable '%.200s' referenced before assignment" \
139 " in enclosing scope"
141 /* Dynamic execution profile */
142 #ifdef DYNAMIC_EXECUTION_PROFILE
143 #ifdef DXPAIRS
144 static long dxpairs[257][256];
145 #define dxp dxpairs[256]
146 #else
147 static long dxp[256];
148 #endif
149 #endif
151 /* Function call profile */
152 #ifdef CALL_PROFILE
153 #define PCALL_NUM 11
154 static int pcall[PCALL_NUM];
156 #define PCALL_ALL 0
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
163 #define PCALL_TYPE 7
164 #define PCALL_GENERATOR 8
165 #define PCALL_OTHER 9
166 #define PCALL_POP 10
168 /* Notes about the statistics
170 PCALL_FAST stats
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
177 twice.
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]++
188 PyObject *
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]);
196 #else
197 #define PCALL(O)
199 PyObject *
200 PyEval_GetCallStats(PyObject *self)
202 Py_INCREF(Py_None);
203 return Py_None;
205 #endif
208 #ifdef WITH_THREAD
210 #ifdef HAVE_ERRNO_H
211 #include <errno.h>
212 #endif
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;
224 void
225 PyEval_InitThreads(void)
227 if (interpreter_lock)
228 return;
229 interpreter_lock = PyThread_allocate_lock();
230 PyThread_acquire_lock(interpreter_lock, 1);
231 main_thread = PyThread_get_thread_ident();
234 void
235 PyEval_AcquireLock(void)
237 PyThread_acquire_lock(interpreter_lock, 1);
240 void
241 PyEval_ReleaseLock(void)
243 PyThread_release_lock(interpreter_lock);
246 void
247 PyEval_AcquireThread(PyThreadState *tstate)
249 if (tstate == NULL)
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)
255 Py_FatalError(
256 "PyEval_AcquireThread: non-NULL old thread state");
259 void
260 PyEval_ReleaseThread(PyThreadState *tstate)
262 if (tstate == NULL)
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.) */
274 void
275 PyEval_ReInitThreads(void)
277 if (!interpreter_lock)
278 return;
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();
287 #endif
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: */
293 PyThreadState *
294 PyEval_SaveThread(void)
296 PyThreadState *tstate = PyThreadState_Swap(NULL);
297 if (tstate == NULL)
298 Py_FatalError("PyEval_SaveThread: NULL tstate");
299 #ifdef WITH_THREAD
300 if (interpreter_lock)
301 PyThread_release_lock(interpreter_lock);
302 #endif
303 return tstate;
306 void
307 PyEval_RestoreThread(PyThreadState *tstate)
309 if (tstate == NULL)
310 Py_FatalError("PyEval_RestoreThread: NULL tstate");
311 #ifdef WITH_THREAD
312 if (interpreter_lock) {
313 int err = errno;
314 PyThread_acquire_lock(interpreter_lock, 1);
315 errno = err;
317 #endif
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!
336 #ifdef WITH_THREAD
337 Any thread can schedule pending calls, but only the main thread
338 will execute them.
339 #endif
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
355 static struct {
356 int (*func)(void *);
357 void *arg;
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;
367 int i, j;
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! */
371 if (busy)
372 return -1;
373 busy = 1;
374 i = pendinglast;
375 j = (i + 1) % NPENDINGCALLS;
376 if (j == pendingfirst) {
377 busy = 0;
378 return -1; /* Queue full */
380 pendingcalls[i].func = func;
381 pendingcalls[i].arg = arg;
382 pendinglast = j;
384 _Py_Ticker = 0;
385 things_to_do = 1; /* Signal main loop */
386 busy = 0;
387 /* XXX End critical section */
388 return 0;
392 Py_MakePendingCalls(void)
394 static int busy = 0;
395 #ifdef WITH_THREAD
396 if (main_thread && PyThread_get_thread_ident() != main_thread)
397 return 0;
398 #endif
399 if (busy)
400 return 0;
401 busy = 1;
402 things_to_do = 0;
403 for (;;) {
404 int i;
405 int (*func)(void *);
406 void *arg;
407 i = pendingfirst;
408 if (i == pendinglast)
409 break; /* Queue empty */
410 func = pendingcalls[i].func;
411 arg = pendingcalls[i].arg;
412 pendingfirst = (i + 1) % NPENDINGCALLS;
413 if (func(arg) < 0) {
414 busy = 0;
415 things_to_do = 1; /* We're not done yet */
416 return -1;
419 busy = 0;
420 return 0;
424 /* The interpreter's recursion limit */
426 #ifndef Py_DEFAULT_RECURSION_LIMIT
427 #define Py_DEFAULT_RECURSION_LIMIT 1000
428 #endif
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;
438 void
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");
459 return -1;
461 #endif
462 if (tstate->recursion_depth > recursion_limit) {
463 --tstate->recursion_depth;
464 PyErr_Format(PyExc_RuntimeError,
465 "maximum recursion depth exceeded%s",
466 where);
467 return -1;
469 _Py_CheckRecursionLimit = recursion_limit;
470 return 0;
473 /* Status code for main loop (reason for stack unwind) */
474 enum why_code {
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;
492 PyObject *
493 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
495 return PyEval_EvalCodeEx(co,
496 globals, locals,
497 (PyObject **)NULL, 0,
498 (PyObject **)NULL, 0,
499 (PyObject **)NULL, 0,
500 NULL);
504 /* Interpreter main loop */
506 PyObject *
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);
514 PyObject *
515 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
517 #ifdef DXPAIRS
518 int lastopcode = 0;
519 #endif
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();
535 PyCodeObject *co;
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;
547 PyObject *names;
548 PyObject *consts;
549 #if defined(Py_DEBUG) || defined(LLTRACE)
550 /* Make it easier to find out where we are with a debugger */
551 char *filename;
552 #endif
554 /* Tuple access macros */
556 #ifndef Py_DEBUG
557 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
558 #else
559 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
560 #endif
562 #ifdef WITH_TSC
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
568 (may be skipped)
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
583 cases:
585 EXEC_STMT
586 IMPORT_STAR
587 IMPORT_FROM
588 CALL_FUNCTION (and friends)
591 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
592 int ticked = 0;
594 READ_TIMESTAMP(inst0);
595 READ_TIMESTAMP(inst1);
596 READ_TIMESTAMP(loop0);
597 READ_TIMESTAMP(loop1);
599 /* shut up the compiler */
600 opcode = 0;
601 #endif
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
622 next opcode.
624 A successful prediction saves a trip through the eval-loop including
625 its two unpredictable branches, the HASARG test and the switch-case.
627 If collecting opcode statistics, turn off prediction so that
628 statistics are accurately maintained (the predictions bypass
629 the opcode frequency counter updates).
632 #ifdef DYNAMIC_EXECUTION_PROFILE
633 #define PREDICT(op) if (0) goto PRED_##op
634 #else
635 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
636 #endif
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)
659 #ifdef LLTRACE
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")), \
664 BASIC_POP())
665 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
666 lltrace && prtrace(TOP(), "stackadj")); \
667 assert(STACK_LEVEL() <= co->co_stacksize); }
668 #define EXT_POP(STACK_POINTER) (lltrace && prtrace((STACK_POINTER)[-1], \
669 "ext_pop"), *--(STACK_POINTER))
670 #else
671 #define PUSH(v) BASIC_PUSH(v)
672 #define POP() BASIC_POP()
673 #define STACKADJ(n) BASIC_STACKADJ(n)
674 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
675 #endif
677 /* Local variable macros */
679 #define GETLOCAL(i) (fastlocals[i])
681 /* The SETLOCAL() macro must not DECREF the local variable in-place and
682 then store the new value; it must copy the old value to a temporary
683 value, then store the new value, and then DECREF the temporary value.
684 This is because it is possible that during the DECREF the frame is
685 accessed by other code (e.g. a __del__ method or gc.collect()) and the
686 variable would be pointing to already-freed memory. */
687 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
688 GETLOCAL(i) = value; \
689 Py_XDECREF(tmp); } while (0)
691 /* Start of code */
693 if (f == NULL)
694 return NULL;
696 /* push frame */
697 if (Py_EnterRecursiveCall(""))
698 return NULL;
700 tstate->frame = f;
702 if (tstate->use_tracing) {
703 if (tstate->c_tracefunc != NULL) {
704 /* tstate->c_tracefunc, if defined, is a
705 function that will be called on *every* entry
706 to a code block. Its return value, if not
707 None, is a function that will be called at
708 the start of each executed line of code.
709 (Actually, the function must return itself
710 in order to continue tracing.) The trace
711 functions are called with three arguments:
712 a pointer to the current frame, a string
713 indicating why the function is called, and
714 an argument which depends on the situation.
715 The global trace function is also called
716 whenever an exception is detected. */
717 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
718 f, PyTrace_CALL, Py_None)) {
719 /* Trace function raised an error */
720 goto exit_eval_frame;
723 if (tstate->c_profilefunc != NULL) {
724 /* Similar for c_profilefunc, except it needn't
725 return itself and isn't called for "line" events */
726 if (call_trace(tstate->c_profilefunc,
727 tstate->c_profileobj,
728 f, PyTrace_CALL, Py_None)) {
729 /* Profile function raised an error */
730 goto exit_eval_frame;
735 co = f->f_code;
736 names = co->co_names;
737 consts = co->co_consts;
738 fastlocals = f->f_localsplus;
739 freevars = f->f_localsplus + co->co_nlocals;
740 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
741 /* An explanation is in order for the next line.
743 f->f_lasti now refers to the index of the last instruction
744 executed. You might think this was obvious from the name, but
745 this wasn't always true before 2.3! PyFrame_New now sets
746 f->f_lasti to -1 (i.e. the index *before* the first instruction)
747 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
748 does work. Promise.
750 When the PREDICT() macros are enabled, some opcode pairs follow in
751 direct succession without updating f->f_lasti. A successful
752 prediction effectively links the two codes together as if they
753 were a single new opcode; accordingly,f->f_lasti will point to
754 the first code in the pair (for instance, GET_ITER followed by
755 FOR_ITER is effectively a single opcode and f->f_lasti will point
756 at to the beginning of the combined pair.)
758 next_instr = first_instr + f->f_lasti + 1;
759 stack_pointer = f->f_stacktop;
760 assert(stack_pointer != NULL);
761 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
763 #ifdef LLTRACE
764 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
765 #endif
766 #if defined(Py_DEBUG) || defined(LLTRACE)
767 filename = PyString_AsString(co->co_filename);
768 #endif
770 why = WHY_NOT;
771 err = 0;
772 x = Py_None; /* Not a reference, just anything non-NULL */
773 w = NULL;
775 if (throwflag) { /* support for generator.throw() */
776 why = WHY_EXCEPTION;
777 goto on_error;
780 for (;;) {
781 #ifdef WITH_TSC
782 if (inst1 == 0) {
783 /* Almost surely, the opcode executed a break
784 or a continue, preventing inst1 from being set
785 on the way out of the loop.
787 READ_TIMESTAMP(inst1);
788 loop1 = inst1;
790 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
791 intr0, intr1);
792 ticked = 0;
793 inst1 = 0;
794 intr0 = 0;
795 intr1 = 0;
796 READ_TIMESTAMP(loop0);
797 #endif
798 assert(stack_pointer >= f->f_valuestack); /* else underflow */
799 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
801 /* Do periodic things. Doing this every time through
802 the loop would add too much overhead, so we do it
803 only every Nth instruction. We also do it if
804 ``things_to_do'' is set, i.e. when an asynchronous
805 event needs attention (e.g. a signal handler or
806 async I/O handler); see Py_AddPendingCall() and
807 Py_MakePendingCalls() above. */
809 if (--_Py_Ticker < 0) {
810 if (*next_instr == SETUP_FINALLY) {
811 /* Make the last opcode before
812 a try: finally: block uninterruptable. */
813 goto fast_next_opcode;
815 _Py_Ticker = _Py_CheckInterval;
816 tstate->tick_counter++;
817 #ifdef WITH_TSC
818 ticked = 1;
819 #endif
820 if (things_to_do) {
821 if (Py_MakePendingCalls() < 0) {
822 why = WHY_EXCEPTION;
823 goto on_error;
825 if (things_to_do)
826 /* MakePendingCalls() didn't succeed.
827 Force early re-execution of this
828 "periodic" code, possibly after
829 a thread switch */
830 _Py_Ticker = 0;
832 #ifdef WITH_THREAD
833 if (interpreter_lock) {
834 /* Give another thread a chance */
836 if (PyThreadState_Swap(NULL) != tstate)
837 Py_FatalError("ceval: tstate mix-up");
838 PyThread_release_lock(interpreter_lock);
840 /* Other threads may run now */
842 PyThread_acquire_lock(interpreter_lock, 1);
843 if (PyThreadState_Swap(tstate) != NULL)
844 Py_FatalError("ceval: orphan tstate");
846 /* Check for thread interrupts */
848 if (tstate->async_exc != NULL) {
849 x = tstate->async_exc;
850 tstate->async_exc = NULL;
851 PyErr_SetNone(x);
852 Py_DECREF(x);
853 why = WHY_EXCEPTION;
854 goto on_error;
857 #endif
860 fast_next_opcode:
861 f->f_lasti = INSTR_OFFSET();
863 /* line-by-line tracing support */
865 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
866 /* see maybe_call_line_trace
867 for expository comments */
868 f->f_stacktop = stack_pointer;
870 err = maybe_call_line_trace(tstate->c_tracefunc,
871 tstate->c_traceobj,
872 f, &instr_lb, &instr_ub,
873 &instr_prev);
874 /* Reload possibly changed frame fields */
875 JUMPTO(f->f_lasti);
876 if (f->f_stacktop != NULL) {
877 stack_pointer = f->f_stacktop;
878 f->f_stacktop = NULL;
880 if (err) {
881 /* trace function raised an exception */
882 goto on_error;
886 /* Extract opcode and argument */
888 opcode = NEXTOP();
889 oparg = 0; /* allows oparg to be stored in a register because
890 it doesn't have to be remembered across a full loop */
891 if (HAS_ARG(opcode))
892 oparg = NEXTARG();
893 dispatch_opcode:
894 #ifdef DYNAMIC_EXECUTION_PROFILE
895 #ifdef DXPAIRS
896 dxpairs[lastopcode][opcode]++;
897 lastopcode = opcode;
898 #endif
899 dxp[opcode]++;
900 #endif
902 #ifdef LLTRACE
903 /* Instruction tracing */
905 if (lltrace) {
906 if (HAS_ARG(opcode)) {
907 printf("%d: %d, %d\n",
908 f->f_lasti, opcode, oparg);
910 else {
911 printf("%d: %d\n",
912 f->f_lasti, opcode);
915 #endif
917 /* Main switch on opcode */
918 READ_TIMESTAMP(inst0);
920 switch (opcode) {
922 /* BEWARE!
923 It is essential that any operation that fails sets either
924 x to NULL, err to nonzero, or why to anything but WHY_NOT,
925 and that no operation that succeeds does this! */
927 /* case STOP_CODE: this is an error! */
929 case NOP:
930 goto fast_next_opcode;
932 case LOAD_FAST:
933 x = GETLOCAL(oparg);
934 if (x != NULL) {
935 Py_INCREF(x);
936 PUSH(x);
937 goto fast_next_opcode;
939 format_exc_check_arg(PyExc_UnboundLocalError,
940 UNBOUNDLOCAL_ERROR_MSG,
941 PyTuple_GetItem(co->co_varnames, oparg));
942 break;
944 case LOAD_CONST:
945 x = GETITEM(consts, oparg);
946 Py_INCREF(x);
947 PUSH(x);
948 goto fast_next_opcode;
950 PREDICTED_WITH_ARG(STORE_FAST);
951 case STORE_FAST:
952 v = POP();
953 SETLOCAL(oparg, v);
954 goto fast_next_opcode;
956 PREDICTED(POP_TOP);
957 case POP_TOP:
958 v = POP();
959 Py_DECREF(v);
960 goto fast_next_opcode;
962 case ROT_TWO:
963 v = TOP();
964 w = SECOND();
965 SET_TOP(w);
966 SET_SECOND(v);
967 goto fast_next_opcode;
969 case ROT_THREE:
970 v = TOP();
971 w = SECOND();
972 x = THIRD();
973 SET_TOP(w);
974 SET_SECOND(x);
975 SET_THIRD(v);
976 goto fast_next_opcode;
978 case ROT_FOUR:
979 u = TOP();
980 v = SECOND();
981 w = THIRD();
982 x = FOURTH();
983 SET_TOP(v);
984 SET_SECOND(w);
985 SET_THIRD(x);
986 SET_FOURTH(u);
987 goto fast_next_opcode;
989 case DUP_TOP:
990 v = TOP();
991 Py_INCREF(v);
992 PUSH(v);
993 goto fast_next_opcode;
995 case DUP_TOPX:
996 if (oparg == 2) {
997 x = TOP();
998 Py_INCREF(x);
999 w = SECOND();
1000 Py_INCREF(w);
1001 STACKADJ(2);
1002 SET_TOP(x);
1003 SET_SECOND(w);
1004 goto fast_next_opcode;
1005 } else if (oparg == 3) {
1006 x = TOP();
1007 Py_INCREF(x);
1008 w = SECOND();
1009 Py_INCREF(w);
1010 v = THIRD();
1011 Py_INCREF(v);
1012 STACKADJ(3);
1013 SET_TOP(x);
1014 SET_SECOND(w);
1015 SET_THIRD(v);
1016 goto fast_next_opcode;
1018 Py_FatalError("invalid argument to DUP_TOPX"
1019 " (bytecode corruption?)");
1020 break;
1022 case UNARY_POSITIVE:
1023 v = TOP();
1024 x = PyNumber_Positive(v);
1025 Py_DECREF(v);
1026 SET_TOP(x);
1027 if (x != NULL) continue;
1028 break;
1030 case UNARY_NEGATIVE:
1031 v = TOP();
1032 x = PyNumber_Negative(v);
1033 Py_DECREF(v);
1034 SET_TOP(x);
1035 if (x != NULL) continue;
1036 break;
1038 case UNARY_NOT:
1039 v = TOP();
1040 err = PyObject_IsTrue(v);
1041 Py_DECREF(v);
1042 if (err == 0) {
1043 Py_INCREF(Py_True);
1044 SET_TOP(Py_True);
1045 continue;
1047 else if (err > 0) {
1048 Py_INCREF(Py_False);
1049 SET_TOP(Py_False);
1050 err = 0;
1051 continue;
1053 STACKADJ(-1);
1054 break;
1056 case UNARY_CONVERT:
1057 v = TOP();
1058 x = PyObject_Repr(v);
1059 Py_DECREF(v);
1060 SET_TOP(x);
1061 if (x != NULL) continue;
1062 break;
1064 case UNARY_INVERT:
1065 v = TOP();
1066 x = PyNumber_Invert(v);
1067 Py_DECREF(v);
1068 SET_TOP(x);
1069 if (x != NULL) continue;
1070 break;
1072 case BINARY_POWER:
1073 w = POP();
1074 v = TOP();
1075 x = PyNumber_Power(v, w, Py_None);
1076 Py_DECREF(v);
1077 Py_DECREF(w);
1078 SET_TOP(x);
1079 if (x != NULL) continue;
1080 break;
1082 case BINARY_MULTIPLY:
1083 w = POP();
1084 v = TOP();
1085 x = PyNumber_Multiply(v, w);
1086 Py_DECREF(v);
1087 Py_DECREF(w);
1088 SET_TOP(x);
1089 if (x != NULL) continue;
1090 break;
1092 case BINARY_DIVIDE:
1093 if (!_Py_QnewFlag) {
1094 w = POP();
1095 v = TOP();
1096 x = PyNumber_Divide(v, w);
1097 Py_DECREF(v);
1098 Py_DECREF(w);
1099 SET_TOP(x);
1100 if (x != NULL) continue;
1101 break;
1103 /* -Qnew is in effect: fall through to
1104 BINARY_TRUE_DIVIDE */
1105 case BINARY_TRUE_DIVIDE:
1106 w = POP();
1107 v = TOP();
1108 x = PyNumber_TrueDivide(v, w);
1109 Py_DECREF(v);
1110 Py_DECREF(w);
1111 SET_TOP(x);
1112 if (x != NULL) continue;
1113 break;
1115 case BINARY_FLOOR_DIVIDE:
1116 w = POP();
1117 v = TOP();
1118 x = PyNumber_FloorDivide(v, w);
1119 Py_DECREF(v);
1120 Py_DECREF(w);
1121 SET_TOP(x);
1122 if (x != NULL) continue;
1123 break;
1125 case BINARY_MODULO:
1126 w = POP();
1127 v = TOP();
1128 x = PyNumber_Remainder(v, w);
1129 Py_DECREF(v);
1130 Py_DECREF(w);
1131 SET_TOP(x);
1132 if (x != NULL) continue;
1133 break;
1135 case BINARY_ADD:
1136 w = POP();
1137 v = TOP();
1138 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1139 /* INLINE: int + int */
1140 register long a, b, i;
1141 a = PyInt_AS_LONG(v);
1142 b = PyInt_AS_LONG(w);
1143 i = a + b;
1144 if ((i^a) < 0 && (i^b) < 0)
1145 goto slow_add;
1146 x = PyInt_FromLong(i);
1148 else if (PyString_CheckExact(v) &&
1149 PyString_CheckExact(w)) {
1150 x = string_concatenate(v, w, f, next_instr);
1151 /* string_concatenate consumed the ref to v */
1152 goto skip_decref_vx;
1154 else {
1155 slow_add:
1156 x = PyNumber_Add(v, w);
1158 Py_DECREF(v);
1159 skip_decref_vx:
1160 Py_DECREF(w);
1161 SET_TOP(x);
1162 if (x != NULL) continue;
1163 break;
1165 case BINARY_SUBTRACT:
1166 w = POP();
1167 v = TOP();
1168 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1169 /* INLINE: int - int */
1170 register long a, b, i;
1171 a = PyInt_AS_LONG(v);
1172 b = PyInt_AS_LONG(w);
1173 i = a - b;
1174 if ((i^a) < 0 && (i^~b) < 0)
1175 goto slow_sub;
1176 x = PyInt_FromLong(i);
1178 else {
1179 slow_sub:
1180 x = PyNumber_Subtract(v, w);
1182 Py_DECREF(v);
1183 Py_DECREF(w);
1184 SET_TOP(x);
1185 if (x != NULL) continue;
1186 break;
1188 case BINARY_SUBSCR:
1189 w = POP();
1190 v = TOP();
1191 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1192 /* INLINE: list[int] */
1193 Py_ssize_t i = PyInt_AsSsize_t(w);
1194 if (i < 0)
1195 i += PyList_GET_SIZE(v);
1196 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1197 x = PyList_GET_ITEM(v, i);
1198 Py_INCREF(x);
1200 else
1201 goto slow_get;
1203 else
1204 slow_get:
1205 x = PyObject_GetItem(v, w);
1206 Py_DECREF(v);
1207 Py_DECREF(w);
1208 SET_TOP(x);
1209 if (x != NULL) continue;
1210 break;
1212 case BINARY_LSHIFT:
1213 w = POP();
1214 v = TOP();
1215 x = PyNumber_Lshift(v, w);
1216 Py_DECREF(v);
1217 Py_DECREF(w);
1218 SET_TOP(x);
1219 if (x != NULL) continue;
1220 break;
1222 case BINARY_RSHIFT:
1223 w = POP();
1224 v = TOP();
1225 x = PyNumber_Rshift(v, w);
1226 Py_DECREF(v);
1227 Py_DECREF(w);
1228 SET_TOP(x);
1229 if (x != NULL) continue;
1230 break;
1232 case BINARY_AND:
1233 w = POP();
1234 v = TOP();
1235 x = PyNumber_And(v, w);
1236 Py_DECREF(v);
1237 Py_DECREF(w);
1238 SET_TOP(x);
1239 if (x != NULL) continue;
1240 break;
1242 case BINARY_XOR:
1243 w = POP();
1244 v = TOP();
1245 x = PyNumber_Xor(v, w);
1246 Py_DECREF(v);
1247 Py_DECREF(w);
1248 SET_TOP(x);
1249 if (x != NULL) continue;
1250 break;
1252 case BINARY_OR:
1253 w = POP();
1254 v = TOP();
1255 x = PyNumber_Or(v, w);
1256 Py_DECREF(v);
1257 Py_DECREF(w);
1258 SET_TOP(x);
1259 if (x != NULL) continue;
1260 break;
1262 case LIST_APPEND:
1263 w = POP();
1264 v = POP();
1265 err = PyList_Append(v, w);
1266 Py_DECREF(v);
1267 Py_DECREF(w);
1268 if (err == 0) {
1269 PREDICT(JUMP_ABSOLUTE);
1270 continue;
1272 break;
1274 case INPLACE_POWER:
1275 w = POP();
1276 v = TOP();
1277 x = PyNumber_InPlacePower(v, w, Py_None);
1278 Py_DECREF(v);
1279 Py_DECREF(w);
1280 SET_TOP(x);
1281 if (x != NULL) continue;
1282 break;
1284 case INPLACE_MULTIPLY:
1285 w = POP();
1286 v = TOP();
1287 x = PyNumber_InPlaceMultiply(v, w);
1288 Py_DECREF(v);
1289 Py_DECREF(w);
1290 SET_TOP(x);
1291 if (x != NULL) continue;
1292 break;
1294 case INPLACE_DIVIDE:
1295 if (!_Py_QnewFlag) {
1296 w = POP();
1297 v = TOP();
1298 x = PyNumber_InPlaceDivide(v, w);
1299 Py_DECREF(v);
1300 Py_DECREF(w);
1301 SET_TOP(x);
1302 if (x != NULL) continue;
1303 break;
1305 /* -Qnew is in effect: fall through to
1306 INPLACE_TRUE_DIVIDE */
1307 case INPLACE_TRUE_DIVIDE:
1308 w = POP();
1309 v = TOP();
1310 x = PyNumber_InPlaceTrueDivide(v, w);
1311 Py_DECREF(v);
1312 Py_DECREF(w);
1313 SET_TOP(x);
1314 if (x != NULL) continue;
1315 break;
1317 case INPLACE_FLOOR_DIVIDE:
1318 w = POP();
1319 v = TOP();
1320 x = PyNumber_InPlaceFloorDivide(v, w);
1321 Py_DECREF(v);
1322 Py_DECREF(w);
1323 SET_TOP(x);
1324 if (x != NULL) continue;
1325 break;
1327 case INPLACE_MODULO:
1328 w = POP();
1329 v = TOP();
1330 x = PyNumber_InPlaceRemainder(v, w);
1331 Py_DECREF(v);
1332 Py_DECREF(w);
1333 SET_TOP(x);
1334 if (x != NULL) continue;
1335 break;
1337 case INPLACE_ADD:
1338 w = POP();
1339 v = TOP();
1340 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1341 /* INLINE: int + int */
1342 register long a, b, i;
1343 a = PyInt_AS_LONG(v);
1344 b = PyInt_AS_LONG(w);
1345 i = a + b;
1346 if ((i^a) < 0 && (i^b) < 0)
1347 goto slow_iadd;
1348 x = PyInt_FromLong(i);
1350 else if (PyString_CheckExact(v) &&
1351 PyString_CheckExact(w)) {
1352 x = string_concatenate(v, w, f, next_instr);
1353 /* string_concatenate consumed the ref to v */
1354 goto skip_decref_v;
1356 else {
1357 slow_iadd:
1358 x = PyNumber_InPlaceAdd(v, w);
1360 Py_DECREF(v);
1361 skip_decref_v:
1362 Py_DECREF(w);
1363 SET_TOP(x);
1364 if (x != NULL) continue;
1365 break;
1367 case INPLACE_SUBTRACT:
1368 w = POP();
1369 v = TOP();
1370 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1371 /* INLINE: int - int */
1372 register long a, b, i;
1373 a = PyInt_AS_LONG(v);
1374 b = PyInt_AS_LONG(w);
1375 i = a - b;
1376 if ((i^a) < 0 && (i^~b) < 0)
1377 goto slow_isub;
1378 x = PyInt_FromLong(i);
1380 else {
1381 slow_isub:
1382 x = PyNumber_InPlaceSubtract(v, w);
1384 Py_DECREF(v);
1385 Py_DECREF(w);
1386 SET_TOP(x);
1387 if (x != NULL) continue;
1388 break;
1390 case INPLACE_LSHIFT:
1391 w = POP();
1392 v = TOP();
1393 x = PyNumber_InPlaceLshift(v, w);
1394 Py_DECREF(v);
1395 Py_DECREF(w);
1396 SET_TOP(x);
1397 if (x != NULL) continue;
1398 break;
1400 case INPLACE_RSHIFT:
1401 w = POP();
1402 v = TOP();
1403 x = PyNumber_InPlaceRshift(v, w);
1404 Py_DECREF(v);
1405 Py_DECREF(w);
1406 SET_TOP(x);
1407 if (x != NULL) continue;
1408 break;
1410 case INPLACE_AND:
1411 w = POP();
1412 v = TOP();
1413 x = PyNumber_InPlaceAnd(v, w);
1414 Py_DECREF(v);
1415 Py_DECREF(w);
1416 SET_TOP(x);
1417 if (x != NULL) continue;
1418 break;
1420 case INPLACE_XOR:
1421 w = POP();
1422 v = TOP();
1423 x = PyNumber_InPlaceXor(v, w);
1424 Py_DECREF(v);
1425 Py_DECREF(w);
1426 SET_TOP(x);
1427 if (x != NULL) continue;
1428 break;
1430 case INPLACE_OR:
1431 w = POP();
1432 v = TOP();
1433 x = PyNumber_InPlaceOr(v, w);
1434 Py_DECREF(v);
1435 Py_DECREF(w);
1436 SET_TOP(x);
1437 if (x != NULL) continue;
1438 break;
1440 case SLICE+0:
1441 case SLICE+1:
1442 case SLICE+2:
1443 case SLICE+3:
1444 if ((opcode-SLICE) & 2)
1445 w = POP();
1446 else
1447 w = NULL;
1448 if ((opcode-SLICE) & 1)
1449 v = POP();
1450 else
1451 v = NULL;
1452 u = TOP();
1453 x = apply_slice(u, v, w);
1454 Py_DECREF(u);
1455 Py_XDECREF(v);
1456 Py_XDECREF(w);
1457 SET_TOP(x);
1458 if (x != NULL) continue;
1459 break;
1461 case STORE_SLICE+0:
1462 case STORE_SLICE+1:
1463 case STORE_SLICE+2:
1464 case STORE_SLICE+3:
1465 if ((opcode-STORE_SLICE) & 2)
1466 w = POP();
1467 else
1468 w = NULL;
1469 if ((opcode-STORE_SLICE) & 1)
1470 v = POP();
1471 else
1472 v = NULL;
1473 u = POP();
1474 t = POP();
1475 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1476 Py_DECREF(t);
1477 Py_DECREF(u);
1478 Py_XDECREF(v);
1479 Py_XDECREF(w);
1480 if (err == 0) continue;
1481 break;
1483 case DELETE_SLICE+0:
1484 case DELETE_SLICE+1:
1485 case DELETE_SLICE+2:
1486 case DELETE_SLICE+3:
1487 if ((opcode-DELETE_SLICE) & 2)
1488 w = POP();
1489 else
1490 w = NULL;
1491 if ((opcode-DELETE_SLICE) & 1)
1492 v = POP();
1493 else
1494 v = NULL;
1495 u = POP();
1496 err = assign_slice(u, v, w, (PyObject *)NULL);
1497 /* del u[v:w] */
1498 Py_DECREF(u);
1499 Py_XDECREF(v);
1500 Py_XDECREF(w);
1501 if (err == 0) continue;
1502 break;
1504 case STORE_SUBSCR:
1505 w = TOP();
1506 v = SECOND();
1507 u = THIRD();
1508 STACKADJ(-3);
1509 /* v[w] = u */
1510 err = PyObject_SetItem(v, w, u);
1511 Py_DECREF(u);
1512 Py_DECREF(v);
1513 Py_DECREF(w);
1514 if (err == 0) continue;
1515 break;
1517 case DELETE_SUBSCR:
1518 w = TOP();
1519 v = SECOND();
1520 STACKADJ(-2);
1521 /* del v[w] */
1522 err = PyObject_DelItem(v, w);
1523 Py_DECREF(v);
1524 Py_DECREF(w);
1525 if (err == 0) continue;
1526 break;
1528 case PRINT_EXPR:
1529 v = POP();
1530 w = PySys_GetObject("displayhook");
1531 if (w == NULL) {
1532 PyErr_SetString(PyExc_RuntimeError,
1533 "lost sys.displayhook");
1534 err = -1;
1535 x = NULL;
1537 if (err == 0) {
1538 x = PyTuple_Pack(1, v);
1539 if (x == NULL)
1540 err = -1;
1542 if (err == 0) {
1543 w = PyEval_CallObject(w, x);
1544 Py_XDECREF(w);
1545 if (w == NULL)
1546 err = -1;
1548 Py_DECREF(v);
1549 Py_XDECREF(x);
1550 break;
1552 case PRINT_ITEM_TO:
1553 w = stream = POP();
1554 /* fall through to PRINT_ITEM */
1556 case PRINT_ITEM:
1557 v = POP();
1558 if (stream == NULL || stream == Py_None) {
1559 w = PySys_GetObject("stdout");
1560 if (w == NULL) {
1561 PyErr_SetString(PyExc_RuntimeError,
1562 "lost sys.stdout");
1563 err = -1;
1566 /* PyFile_SoftSpace() can exececute arbitrary code
1567 if sys.stdout is an instance with a __getattr__.
1568 If __getattr__ raises an exception, w will
1569 be freed, so we need to prevent that temporarily. */
1570 Py_XINCREF(w);
1571 if (w != NULL && PyFile_SoftSpace(w, 0))
1572 err = PyFile_WriteString(" ", w);
1573 if (err == 0)
1574 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1575 if (err == 0) {
1576 /* XXX move into writeobject() ? */
1577 if (PyString_Check(v)) {
1578 char *s = PyString_AS_STRING(v);
1579 Py_ssize_t len = PyString_GET_SIZE(v);
1580 if (len == 0 ||
1581 !isspace(Py_CHARMASK(s[len-1])) ||
1582 s[len-1] == ' ')
1583 PyFile_SoftSpace(w, 1);
1585 #ifdef Py_USING_UNICODE
1586 else if (PyUnicode_Check(v)) {
1587 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1588 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1589 if (len == 0 ||
1590 !Py_UNICODE_ISSPACE(s[len-1]) ||
1591 s[len-1] == ' ')
1592 PyFile_SoftSpace(w, 1);
1594 #endif
1595 else
1596 PyFile_SoftSpace(w, 1);
1598 Py_XDECREF(w);
1599 Py_DECREF(v);
1600 Py_XDECREF(stream);
1601 stream = NULL;
1602 if (err == 0)
1603 continue;
1604 break;
1606 case PRINT_NEWLINE_TO:
1607 w = stream = POP();
1608 /* fall through to PRINT_NEWLINE */
1610 case PRINT_NEWLINE:
1611 if (stream == NULL || stream == Py_None) {
1612 w = PySys_GetObject("stdout");
1613 if (w == NULL)
1614 PyErr_SetString(PyExc_RuntimeError,
1615 "lost sys.stdout");
1617 if (w != NULL) {
1618 err = PyFile_WriteString("\n", w);
1619 if (err == 0)
1620 PyFile_SoftSpace(w, 0);
1622 Py_XDECREF(stream);
1623 stream = NULL;
1624 break;
1627 #ifdef CASE_TOO_BIG
1628 default: switch (opcode) {
1629 #endif
1630 case RAISE_VARARGS:
1631 u = v = w = NULL;
1632 switch (oparg) {
1633 case 3:
1634 u = POP(); /* traceback */
1635 /* Fallthrough */
1636 case 2:
1637 v = POP(); /* value */
1638 /* Fallthrough */
1639 case 1:
1640 w = POP(); /* exc */
1641 case 0: /* Fallthrough */
1642 why = do_raise(w, v, u);
1643 break;
1644 default:
1645 PyErr_SetString(PyExc_SystemError,
1646 "bad RAISE_VARARGS oparg");
1647 why = WHY_EXCEPTION;
1648 break;
1650 break;
1652 case LOAD_LOCALS:
1653 if ((x = f->f_locals) != NULL) {
1654 Py_INCREF(x);
1655 PUSH(x);
1656 continue;
1658 PyErr_SetString(PyExc_SystemError, "no locals");
1659 break;
1661 case RETURN_VALUE:
1662 retval = POP();
1663 why = WHY_RETURN;
1664 goto fast_block_end;
1666 case YIELD_VALUE:
1667 retval = POP();
1668 f->f_stacktop = stack_pointer;
1669 why = WHY_YIELD;
1670 goto fast_yield;
1672 case EXEC_STMT:
1673 w = TOP();
1674 v = SECOND();
1675 u = THIRD();
1676 STACKADJ(-3);
1677 READ_TIMESTAMP(intr0);
1678 err = exec_statement(f, u, v, w);
1679 READ_TIMESTAMP(intr1);
1680 Py_DECREF(u);
1681 Py_DECREF(v);
1682 Py_DECREF(w);
1683 break;
1685 case POP_BLOCK:
1687 PyTryBlock *b = PyFrame_BlockPop(f);
1688 while (STACK_LEVEL() > b->b_level) {
1689 v = POP();
1690 Py_DECREF(v);
1693 continue;
1695 case END_FINALLY:
1696 v = POP();
1697 if (PyInt_Check(v)) {
1698 why = (enum why_code) PyInt_AS_LONG(v);
1699 assert(why != WHY_YIELD);
1700 if (why == WHY_RETURN ||
1701 why == WHY_CONTINUE)
1702 retval = POP();
1704 else if (PyExceptionClass_Check(v) ||
1705 PyString_Check(v)) {
1706 w = POP();
1707 u = POP();
1708 PyErr_Restore(v, w, u);
1709 why = WHY_RERAISE;
1710 break;
1712 else if (v != Py_None) {
1713 PyErr_SetString(PyExc_SystemError,
1714 "'finally' pops bad exception");
1715 why = WHY_EXCEPTION;
1717 Py_DECREF(v);
1718 break;
1720 case BUILD_CLASS:
1721 u = TOP();
1722 v = SECOND();
1723 w = THIRD();
1724 STACKADJ(-2);
1725 x = build_class(u, v, w);
1726 SET_TOP(x);
1727 Py_DECREF(u);
1728 Py_DECREF(v);
1729 Py_DECREF(w);
1730 break;
1732 case STORE_NAME:
1733 w = GETITEM(names, oparg);
1734 v = POP();
1735 if ((x = f->f_locals) != NULL) {
1736 if (PyDict_CheckExact(x))
1737 err = PyDict_SetItem(x, w, v);
1738 else
1739 err = PyObject_SetItem(x, w, v);
1740 Py_DECREF(v);
1741 if (err == 0) continue;
1742 break;
1744 PyErr_Format(PyExc_SystemError,
1745 "no locals found when storing %s",
1746 PyObject_REPR(w));
1747 break;
1749 case DELETE_NAME:
1750 w = GETITEM(names, oparg);
1751 if ((x = f->f_locals) != NULL) {
1752 if ((err = PyObject_DelItem(x, w)) != 0)
1753 format_exc_check_arg(PyExc_NameError,
1754 NAME_ERROR_MSG,
1756 break;
1758 PyErr_Format(PyExc_SystemError,
1759 "no locals when deleting %s",
1760 PyObject_REPR(w));
1761 break;
1763 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1764 case UNPACK_SEQUENCE:
1765 v = POP();
1766 if (PyTuple_CheckExact(v) &&
1767 PyTuple_GET_SIZE(v) == oparg) {
1768 PyObject **items = \
1769 ((PyTupleObject *)v)->ob_item;
1770 while (oparg--) {
1771 w = items[oparg];
1772 Py_INCREF(w);
1773 PUSH(w);
1775 Py_DECREF(v);
1776 continue;
1777 } else if (PyList_CheckExact(v) &&
1778 PyList_GET_SIZE(v) == oparg) {
1779 PyObject **items = \
1780 ((PyListObject *)v)->ob_item;
1781 while (oparg--) {
1782 w = items[oparg];
1783 Py_INCREF(w);
1784 PUSH(w);
1786 } else if (unpack_iterable(v, oparg,
1787 stack_pointer + oparg)) {
1788 stack_pointer += oparg;
1789 } else {
1790 /* unpack_iterable() raised an exception */
1791 why = WHY_EXCEPTION;
1793 Py_DECREF(v);
1794 break;
1796 case STORE_ATTR:
1797 w = GETITEM(names, oparg);
1798 v = TOP();
1799 u = SECOND();
1800 STACKADJ(-2);
1801 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1802 Py_DECREF(v);
1803 Py_DECREF(u);
1804 if (err == 0) continue;
1805 break;
1807 case DELETE_ATTR:
1808 w = GETITEM(names, oparg);
1809 v = POP();
1810 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1811 /* del v.w */
1812 Py_DECREF(v);
1813 break;
1815 case STORE_GLOBAL:
1816 w = GETITEM(names, oparg);
1817 v = POP();
1818 err = PyDict_SetItem(f->f_globals, w, v);
1819 Py_DECREF(v);
1820 if (err == 0) continue;
1821 break;
1823 case DELETE_GLOBAL:
1824 w = GETITEM(names, oparg);
1825 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1826 format_exc_check_arg(
1827 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1828 break;
1830 case LOAD_NAME:
1831 w = GETITEM(names, oparg);
1832 if ((v = f->f_locals) == NULL) {
1833 PyErr_Format(PyExc_SystemError,
1834 "no locals when loading %s",
1835 PyObject_REPR(w));
1836 break;
1838 if (PyDict_CheckExact(v)) {
1839 x = PyDict_GetItem(v, w);
1840 Py_XINCREF(x);
1842 else {
1843 x = PyObject_GetItem(v, w);
1844 if (x == NULL && PyErr_Occurred()) {
1845 if (!PyErr_ExceptionMatches(
1846 PyExc_KeyError))
1847 break;
1848 PyErr_Clear();
1851 if (x == NULL) {
1852 x = PyDict_GetItem(f->f_globals, w);
1853 if (x == NULL) {
1854 x = PyDict_GetItem(f->f_builtins, w);
1855 if (x == NULL) {
1856 format_exc_check_arg(
1857 PyExc_NameError,
1858 NAME_ERROR_MSG, w);
1859 break;
1862 Py_INCREF(x);
1864 PUSH(x);
1865 continue;
1867 case LOAD_GLOBAL:
1868 w = GETITEM(names, oparg);
1869 if (PyString_CheckExact(w)) {
1870 /* Inline the PyDict_GetItem() calls.
1871 WARNING: this is an extreme speed hack.
1872 Do not try this at home. */
1873 long hash = ((PyStringObject *)w)->ob_shash;
1874 if (hash != -1) {
1875 PyDictObject *d;
1876 PyDictEntry *e;
1877 d = (PyDictObject *)(f->f_globals);
1878 e = d->ma_lookup(d, w, hash);
1879 if (e == NULL) {
1880 x = NULL;
1881 break;
1883 x = e->me_value;
1884 if (x != NULL) {
1885 Py_INCREF(x);
1886 PUSH(x);
1887 continue;
1889 d = (PyDictObject *)(f->f_builtins);
1890 e = d->ma_lookup(d, w, hash);
1891 if (e == NULL) {
1892 x = NULL;
1893 break;
1895 x = e->me_value;
1896 if (x != NULL) {
1897 Py_INCREF(x);
1898 PUSH(x);
1899 continue;
1901 goto load_global_error;
1904 /* This is the un-inlined version of the code above */
1905 x = PyDict_GetItem(f->f_globals, w);
1906 if (x == NULL) {
1907 x = PyDict_GetItem(f->f_builtins, w);
1908 if (x == NULL) {
1909 load_global_error:
1910 format_exc_check_arg(
1911 PyExc_NameError,
1912 GLOBAL_NAME_ERROR_MSG, w);
1913 break;
1916 Py_INCREF(x);
1917 PUSH(x);
1918 continue;
1920 case DELETE_FAST:
1921 x = GETLOCAL(oparg);
1922 if (x != NULL) {
1923 SETLOCAL(oparg, NULL);
1924 continue;
1926 format_exc_check_arg(
1927 PyExc_UnboundLocalError,
1928 UNBOUNDLOCAL_ERROR_MSG,
1929 PyTuple_GetItem(co->co_varnames, oparg)
1931 break;
1933 case LOAD_CLOSURE:
1934 x = freevars[oparg];
1935 Py_INCREF(x);
1936 PUSH(x);
1937 if (x != NULL) continue;
1938 break;
1940 case LOAD_DEREF:
1941 x = freevars[oparg];
1942 w = PyCell_Get(x);
1943 if (w != NULL) {
1944 PUSH(w);
1945 continue;
1947 err = -1;
1948 /* Don't stomp existing exception */
1949 if (PyErr_Occurred())
1950 break;
1951 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1952 v = PyTuple_GET_ITEM(co->co_cellvars,
1953 oparg);
1954 format_exc_check_arg(
1955 PyExc_UnboundLocalError,
1956 UNBOUNDLOCAL_ERROR_MSG,
1958 } else {
1959 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
1960 PyTuple_GET_SIZE(co->co_cellvars));
1961 format_exc_check_arg(PyExc_NameError,
1962 UNBOUNDFREE_ERROR_MSG, v);
1964 break;
1966 case STORE_DEREF:
1967 w = POP();
1968 x = freevars[oparg];
1969 PyCell_Set(x, w);
1970 Py_DECREF(w);
1971 continue;
1973 case BUILD_TUPLE:
1974 x = PyTuple_New(oparg);
1975 if (x != NULL) {
1976 for (; --oparg >= 0;) {
1977 w = POP();
1978 PyTuple_SET_ITEM(x, oparg, w);
1980 PUSH(x);
1981 continue;
1983 break;
1985 case BUILD_LIST:
1986 x = PyList_New(oparg);
1987 if (x != NULL) {
1988 for (; --oparg >= 0;) {
1989 w = POP();
1990 PyList_SET_ITEM(x, oparg, w);
1992 PUSH(x);
1993 continue;
1995 break;
1997 case BUILD_MAP:
1998 x = PyDict_New();
1999 PUSH(x);
2000 if (x != NULL) continue;
2001 break;
2003 case LOAD_ATTR:
2004 w = GETITEM(names, oparg);
2005 v = TOP();
2006 x = PyObject_GetAttr(v, w);
2007 Py_DECREF(v);
2008 SET_TOP(x);
2009 if (x != NULL) continue;
2010 break;
2012 case COMPARE_OP:
2013 w = POP();
2014 v = TOP();
2015 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2016 /* INLINE: cmp(int, int) */
2017 register long a, b;
2018 register int res;
2019 a = PyInt_AS_LONG(v);
2020 b = PyInt_AS_LONG(w);
2021 switch (oparg) {
2022 case PyCmp_LT: res = a < b; break;
2023 case PyCmp_LE: res = a <= b; break;
2024 case PyCmp_EQ: res = a == b; break;
2025 case PyCmp_NE: res = a != b; break;
2026 case PyCmp_GT: res = a > b; break;
2027 case PyCmp_GE: res = a >= b; break;
2028 case PyCmp_IS: res = v == w; break;
2029 case PyCmp_IS_NOT: res = v != w; break;
2030 default: goto slow_compare;
2032 x = res ? Py_True : Py_False;
2033 Py_INCREF(x);
2035 else {
2036 slow_compare:
2037 x = cmp_outcome(oparg, v, w);
2039 Py_DECREF(v);
2040 Py_DECREF(w);
2041 SET_TOP(x);
2042 if (x == NULL) break;
2043 PREDICT(JUMP_IF_FALSE);
2044 PREDICT(JUMP_IF_TRUE);
2045 continue;
2047 case IMPORT_NAME:
2048 w = GETITEM(names, oparg);
2049 x = PyDict_GetItemString(f->f_builtins, "__import__");
2050 if (x == NULL) {
2051 PyErr_SetString(PyExc_ImportError,
2052 "__import__ not found");
2053 break;
2055 v = POP();
2056 u = TOP();
2057 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2058 w = PyTuple_Pack(5,
2060 f->f_globals,
2061 f->f_locals == NULL ?
2062 Py_None : f->f_locals,
2065 else
2066 w = PyTuple_Pack(4,
2068 f->f_globals,
2069 f->f_locals == NULL ?
2070 Py_None : f->f_locals,
2072 Py_DECREF(v);
2073 Py_DECREF(u);
2074 if (w == NULL) {
2075 u = POP();
2076 x = NULL;
2077 break;
2079 READ_TIMESTAMP(intr0);
2080 x = PyEval_CallObject(x, w);
2081 READ_TIMESTAMP(intr1);
2082 Py_DECREF(w);
2083 SET_TOP(x);
2084 if (x != NULL) continue;
2085 break;
2087 case IMPORT_STAR:
2088 v = POP();
2089 PyFrame_FastToLocals(f);
2090 if ((x = f->f_locals) == NULL) {
2091 PyErr_SetString(PyExc_SystemError,
2092 "no locals found during 'import *'");
2093 break;
2095 READ_TIMESTAMP(intr0);
2096 err = import_all_from(x, v);
2097 READ_TIMESTAMP(intr1);
2098 PyFrame_LocalsToFast(f, 0);
2099 Py_DECREF(v);
2100 if (err == 0) continue;
2101 break;
2103 case IMPORT_FROM:
2104 w = GETITEM(names, oparg);
2105 v = TOP();
2106 READ_TIMESTAMP(intr0);
2107 x = import_from(v, w);
2108 READ_TIMESTAMP(intr1);
2109 PUSH(x);
2110 if (x != NULL) continue;
2111 break;
2113 case JUMP_FORWARD:
2114 JUMPBY(oparg);
2115 goto fast_next_opcode;
2117 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
2118 case JUMP_IF_FALSE:
2119 w = TOP();
2120 if (w == Py_True) {
2121 PREDICT(POP_TOP);
2122 goto fast_next_opcode;
2124 if (w == Py_False) {
2125 JUMPBY(oparg);
2126 goto fast_next_opcode;
2128 err = PyObject_IsTrue(w);
2129 if (err > 0)
2130 err = 0;
2131 else if (err == 0)
2132 JUMPBY(oparg);
2133 else
2134 break;
2135 continue;
2137 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
2138 case JUMP_IF_TRUE:
2139 w = TOP();
2140 if (w == Py_False) {
2141 PREDICT(POP_TOP);
2142 goto fast_next_opcode;
2144 if (w == Py_True) {
2145 JUMPBY(oparg);
2146 goto fast_next_opcode;
2148 err = PyObject_IsTrue(w);
2149 if (err > 0) {
2150 err = 0;
2151 JUMPBY(oparg);
2153 else if (err == 0)
2155 else
2156 break;
2157 continue;
2159 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2160 case JUMP_ABSOLUTE:
2161 JUMPTO(oparg);
2162 continue;
2164 case GET_ITER:
2165 /* before: [obj]; after [getiter(obj)] */
2166 v = TOP();
2167 x = PyObject_GetIter(v);
2168 Py_DECREF(v);
2169 if (x != NULL) {
2170 SET_TOP(x);
2171 PREDICT(FOR_ITER);
2172 continue;
2174 STACKADJ(-1);
2175 break;
2177 PREDICTED_WITH_ARG(FOR_ITER);
2178 case FOR_ITER:
2179 /* before: [iter]; after: [iter, iter()] *or* [] */
2180 v = TOP();
2181 x = (*v->ob_type->tp_iternext)(v);
2182 if (x != NULL) {
2183 PUSH(x);
2184 PREDICT(STORE_FAST);
2185 PREDICT(UNPACK_SEQUENCE);
2186 continue;
2188 if (PyErr_Occurred()) {
2189 if (!PyErr_ExceptionMatches(
2190 PyExc_StopIteration))
2191 break;
2192 PyErr_Clear();
2194 /* iterator ended normally */
2195 x = v = POP();
2196 Py_DECREF(v);
2197 JUMPBY(oparg);
2198 continue;
2200 case BREAK_LOOP:
2201 why = WHY_BREAK;
2202 goto fast_block_end;
2204 case CONTINUE_LOOP:
2205 retval = PyInt_FromLong(oparg);
2206 if (!retval) {
2207 x = NULL;
2208 break;
2210 why = WHY_CONTINUE;
2211 goto fast_block_end;
2213 case SETUP_LOOP:
2214 case SETUP_EXCEPT:
2215 case SETUP_FINALLY:
2216 /* NOTE: If you add any new block-setup opcodes that
2217 are not try/except/finally handlers, you may need
2218 to update the PyGen_NeedsFinalizing() function.
2221 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2222 STACK_LEVEL());
2223 continue;
2225 case WITH_CLEANUP:
2227 /* TOP is the context.__exit__ bound method.
2228 Below that are 1-3 values indicating how/why
2229 we entered the finally clause:
2230 - SECOND = None
2231 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
2232 - SECOND = WHY_*; no retval below it
2233 - (SECOND, THIRD, FOURTH) = exc_info()
2234 In the last case, we must call
2235 TOP(SECOND, THIRD, FOURTH)
2236 otherwise we must call
2237 TOP(None, None, None)
2239 In addition, if the stack represents an exception,
2240 *and* the function call returns a 'true' value, we
2241 "zap" this information, to prevent END_FINALLY from
2242 re-raising the exception. (But non-local gotos
2243 should still be resumed.)
2246 x = TOP();
2247 u = SECOND();
2248 if (PyInt_Check(u) || u == Py_None) {
2249 u = v = w = Py_None;
2251 else {
2252 v = THIRD();
2253 w = FOURTH();
2255 /* XXX Not the fastest way to call it... */
2256 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2257 if (x == NULL)
2258 break; /* Go to error exit */
2259 if (u != Py_None && PyObject_IsTrue(x)) {
2260 /* There was an exception and a true return */
2261 Py_DECREF(x);
2262 x = TOP(); /* Again */
2263 STACKADJ(-3);
2264 Py_INCREF(Py_None);
2265 SET_TOP(Py_None);
2266 Py_DECREF(x);
2267 Py_DECREF(u);
2268 Py_DECREF(v);
2269 Py_DECREF(w);
2270 } else {
2271 /* Let END_FINALLY do its thing */
2272 Py_DECREF(x);
2273 x = POP();
2274 Py_DECREF(x);
2276 break;
2279 case CALL_FUNCTION:
2281 PyObject **sp;
2282 PCALL(PCALL_ALL);
2283 sp = stack_pointer;
2284 #ifdef WITH_TSC
2285 x = call_function(&sp, oparg, &intr0, &intr1);
2286 #else
2287 x = call_function(&sp, oparg);
2288 #endif
2289 stack_pointer = sp;
2290 PUSH(x);
2291 if (x != NULL)
2292 continue;
2293 break;
2296 case CALL_FUNCTION_VAR:
2297 case CALL_FUNCTION_KW:
2298 case CALL_FUNCTION_VAR_KW:
2300 int na = oparg & 0xff;
2301 int nk = (oparg>>8) & 0xff;
2302 int flags = (opcode - CALL_FUNCTION) & 3;
2303 int n = na + 2 * nk;
2304 PyObject **pfunc, *func, **sp;
2305 PCALL(PCALL_ALL);
2306 if (flags & CALL_FLAG_VAR)
2307 n++;
2308 if (flags & CALL_FLAG_KW)
2309 n++;
2310 pfunc = stack_pointer - n - 1;
2311 func = *pfunc;
2313 if (PyMethod_Check(func)
2314 && PyMethod_GET_SELF(func) != NULL) {
2315 PyObject *self = PyMethod_GET_SELF(func);
2316 Py_INCREF(self);
2317 func = PyMethod_GET_FUNCTION(func);
2318 Py_INCREF(func);
2319 Py_DECREF(*pfunc);
2320 *pfunc = self;
2321 na++;
2322 n++;
2323 } else
2324 Py_INCREF(func);
2325 sp = stack_pointer;
2326 READ_TIMESTAMP(intr0);
2327 x = ext_do_call(func, &sp, flags, na, nk);
2328 READ_TIMESTAMP(intr1);
2329 stack_pointer = sp;
2330 Py_DECREF(func);
2332 while (stack_pointer > pfunc) {
2333 w = POP();
2334 Py_DECREF(w);
2336 PUSH(x);
2337 if (x != NULL)
2338 continue;
2339 break;
2342 case MAKE_FUNCTION:
2343 v = POP(); /* code object */
2344 x = PyFunction_New(v, f->f_globals);
2345 Py_DECREF(v);
2346 /* XXX Maybe this should be a separate opcode? */
2347 if (x != NULL && oparg > 0) {
2348 v = PyTuple_New(oparg);
2349 if (v == NULL) {
2350 Py_DECREF(x);
2351 x = NULL;
2352 break;
2354 while (--oparg >= 0) {
2355 w = POP();
2356 PyTuple_SET_ITEM(v, oparg, w);
2358 err = PyFunction_SetDefaults(x, v);
2359 Py_DECREF(v);
2361 PUSH(x);
2362 break;
2364 case MAKE_CLOSURE:
2366 v = POP(); /* code object */
2367 x = PyFunction_New(v, f->f_globals);
2368 Py_DECREF(v);
2369 if (x != NULL) {
2370 v = POP();
2371 err = PyFunction_SetClosure(x, v);
2372 Py_DECREF(v);
2374 if (x != NULL && oparg > 0) {
2375 v = PyTuple_New(oparg);
2376 if (v == NULL) {
2377 Py_DECREF(x);
2378 x = NULL;
2379 break;
2381 while (--oparg >= 0) {
2382 w = POP();
2383 PyTuple_SET_ITEM(v, oparg, w);
2385 err = PyFunction_SetDefaults(x, v);
2386 Py_DECREF(v);
2388 PUSH(x);
2389 break;
2392 case BUILD_SLICE:
2393 if (oparg == 3)
2394 w = POP();
2395 else
2396 w = NULL;
2397 v = POP();
2398 u = TOP();
2399 x = PySlice_New(u, v, w);
2400 Py_DECREF(u);
2401 Py_DECREF(v);
2402 Py_XDECREF(w);
2403 SET_TOP(x);
2404 if (x != NULL) continue;
2405 break;
2407 case EXTENDED_ARG:
2408 opcode = NEXTOP();
2409 oparg = oparg<<16 | NEXTARG();
2410 goto dispatch_opcode;
2412 default:
2413 fprintf(stderr,
2414 "XXX lineno: %d, opcode: %d\n",
2415 PyCode_Addr2Line(f->f_code, f->f_lasti),
2416 opcode);
2417 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2418 why = WHY_EXCEPTION;
2419 break;
2421 #ifdef CASE_TOO_BIG
2423 #endif
2425 } /* switch */
2427 on_error:
2429 READ_TIMESTAMP(inst1);
2431 /* Quickly continue if no error occurred */
2433 if (why == WHY_NOT) {
2434 if (err == 0 && x != NULL) {
2435 #ifdef CHECKEXC
2436 /* This check is expensive! */
2437 if (PyErr_Occurred())
2438 fprintf(stderr,
2439 "XXX undetected error\n");
2440 else {
2441 #endif
2442 READ_TIMESTAMP(loop1);
2443 continue; /* Normal, fast path */
2444 #ifdef CHECKEXC
2446 #endif
2448 why = WHY_EXCEPTION;
2449 x = Py_None;
2450 err = 0;
2453 /* Double-check exception status */
2455 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2456 if (!PyErr_Occurred()) {
2457 PyErr_SetString(PyExc_SystemError,
2458 "error return without exception set");
2459 why = WHY_EXCEPTION;
2462 #ifdef CHECKEXC
2463 else {
2464 /* This check is expensive! */
2465 if (PyErr_Occurred()) {
2466 char buf[1024];
2467 sprintf(buf, "Stack unwind with exception "
2468 "set and why=%d", why);
2469 Py_FatalError(buf);
2472 #endif
2474 /* Log traceback info if this is a real exception */
2476 if (why == WHY_EXCEPTION) {
2477 PyTraceBack_Here(f);
2479 if (tstate->c_tracefunc != NULL)
2480 call_exc_trace(tstate->c_tracefunc,
2481 tstate->c_traceobj, f);
2484 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2486 if (why == WHY_RERAISE)
2487 why = WHY_EXCEPTION;
2489 /* Unwind stacks if a (pseudo) exception occurred */
2491 fast_block_end:
2492 while (why != WHY_NOT && f->f_iblock > 0) {
2493 PyTryBlock *b = PyFrame_BlockPop(f);
2495 assert(why != WHY_YIELD);
2496 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2497 /* For a continue inside a try block,
2498 don't pop the block for the loop. */
2499 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2500 b->b_level);
2501 why = WHY_NOT;
2502 JUMPTO(PyInt_AS_LONG(retval));
2503 Py_DECREF(retval);
2504 break;
2507 while (STACK_LEVEL() > b->b_level) {
2508 v = POP();
2509 Py_XDECREF(v);
2511 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2512 why = WHY_NOT;
2513 JUMPTO(b->b_handler);
2514 break;
2516 if (b->b_type == SETUP_FINALLY ||
2517 (b->b_type == SETUP_EXCEPT &&
2518 why == WHY_EXCEPTION)) {
2519 if (why == WHY_EXCEPTION) {
2520 PyObject *exc, *val, *tb;
2521 PyErr_Fetch(&exc, &val, &tb);
2522 if (val == NULL) {
2523 val = Py_None;
2524 Py_INCREF(val);
2526 /* Make the raw exception data
2527 available to the handler,
2528 so a program can emulate the
2529 Python main loop. Don't do
2530 this for 'finally'. */
2531 if (b->b_type == SETUP_EXCEPT) {
2532 PyErr_NormalizeException(
2533 &exc, &val, &tb);
2534 set_exc_info(tstate,
2535 exc, val, tb);
2537 if (tb == NULL) {
2538 Py_INCREF(Py_None);
2539 PUSH(Py_None);
2540 } else
2541 PUSH(tb);
2542 PUSH(val);
2543 PUSH(exc);
2545 else {
2546 if (why & (WHY_RETURN | WHY_CONTINUE))
2547 PUSH(retval);
2548 v = PyInt_FromLong((long)why);
2549 PUSH(v);
2551 why = WHY_NOT;
2552 JUMPTO(b->b_handler);
2553 break;
2555 } /* unwind stack */
2557 /* End the loop if we still have an error (or return) */
2559 if (why != WHY_NOT)
2560 break;
2561 READ_TIMESTAMP(loop1);
2563 } /* main loop */
2565 assert(why != WHY_YIELD);
2566 /* Pop remaining stack entries. */
2567 while (!EMPTY()) {
2568 v = POP();
2569 Py_XDECREF(v);
2572 if (why != WHY_RETURN)
2573 retval = NULL;
2575 fast_yield:
2576 if (tstate->use_tracing) {
2577 if (tstate->c_tracefunc) {
2578 if (why == WHY_RETURN || why == WHY_YIELD) {
2579 if (call_trace(tstate->c_tracefunc,
2580 tstate->c_traceobj, f,
2581 PyTrace_RETURN, retval)) {
2582 Py_XDECREF(retval);
2583 retval = NULL;
2584 why = WHY_EXCEPTION;
2587 else if (why == WHY_EXCEPTION) {
2588 call_trace_protected(tstate->c_tracefunc,
2589 tstate->c_traceobj, f,
2590 PyTrace_RETURN, NULL);
2593 if (tstate->c_profilefunc) {
2594 if (why == WHY_EXCEPTION)
2595 call_trace_protected(tstate->c_profilefunc,
2596 tstate->c_profileobj, f,
2597 PyTrace_RETURN, NULL);
2598 else if (call_trace(tstate->c_profilefunc,
2599 tstate->c_profileobj, f,
2600 PyTrace_RETURN, retval)) {
2601 Py_XDECREF(retval);
2602 retval = NULL;
2603 why = WHY_EXCEPTION;
2608 if (tstate->frame->f_exc_type != NULL)
2609 reset_exc_info(tstate);
2610 else {
2611 assert(tstate->frame->f_exc_value == NULL);
2612 assert(tstate->frame->f_exc_traceback == NULL);
2615 /* pop frame */
2616 exit_eval_frame:
2617 Py_LeaveRecursiveCall();
2618 tstate->frame = f->f_back;
2620 return retval;
2623 /* This is gonna seem *real weird*, but if you put some other code between
2624 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2625 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2627 PyObject *
2628 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
2629 PyObject **args, int argcount, PyObject **kws, int kwcount,
2630 PyObject **defs, int defcount, PyObject *closure)
2632 register PyFrameObject *f;
2633 register PyObject *retval = NULL;
2634 register PyObject **fastlocals, **freevars;
2635 PyThreadState *tstate = PyThreadState_GET();
2636 PyObject *x, *u;
2638 if (globals == NULL) {
2639 PyErr_SetString(PyExc_SystemError,
2640 "PyEval_EvalCodeEx: NULL globals");
2641 return NULL;
2644 assert(tstate != NULL);
2645 assert(globals != NULL);
2646 f = PyFrame_New(tstate, co, globals, locals);
2647 if (f == NULL)
2648 return NULL;
2650 fastlocals = f->f_localsplus;
2651 freevars = f->f_localsplus + co->co_nlocals;
2653 if (co->co_argcount > 0 ||
2654 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2655 int i;
2656 int n = argcount;
2657 PyObject *kwdict = NULL;
2658 if (co->co_flags & CO_VARKEYWORDS) {
2659 kwdict = PyDict_New();
2660 if (kwdict == NULL)
2661 goto fail;
2662 i = co->co_argcount;
2663 if (co->co_flags & CO_VARARGS)
2664 i++;
2665 SETLOCAL(i, kwdict);
2667 if (argcount > co->co_argcount) {
2668 if (!(co->co_flags & CO_VARARGS)) {
2669 PyErr_Format(PyExc_TypeError,
2670 "%.200s() takes %s %d "
2671 "%sargument%s (%d given)",
2672 PyString_AsString(co->co_name),
2673 defcount ? "at most" : "exactly",
2674 co->co_argcount,
2675 kwcount ? "non-keyword " : "",
2676 co->co_argcount == 1 ? "" : "s",
2677 argcount);
2678 goto fail;
2680 n = co->co_argcount;
2682 for (i = 0; i < n; i++) {
2683 x = args[i];
2684 Py_INCREF(x);
2685 SETLOCAL(i, x);
2687 if (co->co_flags & CO_VARARGS) {
2688 u = PyTuple_New(argcount - n);
2689 if (u == NULL)
2690 goto fail;
2691 SETLOCAL(co->co_argcount, u);
2692 for (i = n; i < argcount; i++) {
2693 x = args[i];
2694 Py_INCREF(x);
2695 PyTuple_SET_ITEM(u, i-n, x);
2698 for (i = 0; i < kwcount; i++) {
2699 PyObject *keyword = kws[2*i];
2700 PyObject *value = kws[2*i + 1];
2701 int j;
2702 if (keyword == NULL || !PyString_Check(keyword)) {
2703 PyErr_Format(PyExc_TypeError,
2704 "%.200s() keywords must be strings",
2705 PyString_AsString(co->co_name));
2706 goto fail;
2708 /* XXX slow -- speed up using dictionary? */
2709 for (j = 0; j < co->co_argcount; j++) {
2710 PyObject *nm = PyTuple_GET_ITEM(
2711 co->co_varnames, j);
2712 int cmp = PyObject_RichCompareBool(
2713 keyword, nm, Py_EQ);
2714 if (cmp > 0)
2715 break;
2716 else if (cmp < 0)
2717 goto fail;
2719 /* Check errors from Compare */
2720 if (PyErr_Occurred())
2721 goto fail;
2722 if (j >= co->co_argcount) {
2723 if (kwdict == NULL) {
2724 PyErr_Format(PyExc_TypeError,
2725 "%.200s() got an unexpected "
2726 "keyword argument '%.400s'",
2727 PyString_AsString(co->co_name),
2728 PyString_AsString(keyword));
2729 goto fail;
2731 PyDict_SetItem(kwdict, keyword, value);
2733 else {
2734 if (GETLOCAL(j) != NULL) {
2735 PyErr_Format(PyExc_TypeError,
2736 "%.200s() got multiple "
2737 "values for keyword "
2738 "argument '%.400s'",
2739 PyString_AsString(co->co_name),
2740 PyString_AsString(keyword));
2741 goto fail;
2743 Py_INCREF(value);
2744 SETLOCAL(j, value);
2747 if (argcount < co->co_argcount) {
2748 int m = co->co_argcount - defcount;
2749 for (i = argcount; i < m; i++) {
2750 if (GETLOCAL(i) == NULL) {
2751 PyErr_Format(PyExc_TypeError,
2752 "%.200s() takes %s %d "
2753 "%sargument%s (%d given)",
2754 PyString_AsString(co->co_name),
2755 ((co->co_flags & CO_VARARGS) ||
2756 defcount) ? "at least"
2757 : "exactly",
2758 m, kwcount ? "non-keyword " : "",
2759 m == 1 ? "" : "s", i);
2760 goto fail;
2763 if (n > m)
2764 i = n - m;
2765 else
2766 i = 0;
2767 for (; i < defcount; i++) {
2768 if (GETLOCAL(m+i) == NULL) {
2769 PyObject *def = defs[i];
2770 Py_INCREF(def);
2771 SETLOCAL(m+i, def);
2776 else {
2777 if (argcount > 0 || kwcount > 0) {
2778 PyErr_Format(PyExc_TypeError,
2779 "%.200s() takes no arguments (%d given)",
2780 PyString_AsString(co->co_name),
2781 argcount + kwcount);
2782 goto fail;
2785 /* Allocate and initialize storage for cell vars, and copy free
2786 vars into frame. This isn't too efficient right now. */
2787 if (PyTuple_GET_SIZE(co->co_cellvars)) {
2788 int i, j, nargs, found;
2789 char *cellname, *argname;
2790 PyObject *c;
2792 nargs = co->co_argcount;
2793 if (co->co_flags & CO_VARARGS)
2794 nargs++;
2795 if (co->co_flags & CO_VARKEYWORDS)
2796 nargs++;
2798 /* Initialize each cell var, taking into account
2799 cell vars that are initialized from arguments.
2801 Should arrange for the compiler to put cellvars
2802 that are arguments at the beginning of the cellvars
2803 list so that we can march over it more efficiently?
2805 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
2806 cellname = PyString_AS_STRING(
2807 PyTuple_GET_ITEM(co->co_cellvars, i));
2808 found = 0;
2809 for (j = 0; j < nargs; j++) {
2810 argname = PyString_AS_STRING(
2811 PyTuple_GET_ITEM(co->co_varnames, j));
2812 if (strcmp(cellname, argname) == 0) {
2813 c = PyCell_New(GETLOCAL(j));
2814 if (c == NULL)
2815 goto fail;
2816 GETLOCAL(co->co_nlocals + i) = c;
2817 found = 1;
2818 break;
2821 if (found == 0) {
2822 c = PyCell_New(NULL);
2823 if (c == NULL)
2824 goto fail;
2825 SETLOCAL(co->co_nlocals + i, c);
2829 if (PyTuple_GET_SIZE(co->co_freevars)) {
2830 int i;
2831 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
2832 PyObject *o = PyTuple_GET_ITEM(closure, i);
2833 Py_INCREF(o);
2834 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
2838 if (co->co_flags & CO_GENERATOR) {
2839 /* Don't need to keep the reference to f_back, it will be set
2840 * when the generator is resumed. */
2841 Py_XDECREF(f->f_back);
2842 f->f_back = NULL;
2844 PCALL(PCALL_GENERATOR);
2846 /* Create a new generator that owns the ready to run frame
2847 * and return that as the value. */
2848 return PyGen_New(f);
2851 retval = PyEval_EvalFrameEx(f,0);
2853 fail: /* Jump here from prelude on failure */
2855 /* decref'ing the frame can cause __del__ methods to get invoked,
2856 which can call back into Python. While we're done with the
2857 current Python frame (f), the associated C stack is still in use,
2858 so recursion_depth must be boosted for the duration.
2860 assert(tstate != NULL);
2861 ++tstate->recursion_depth;
2862 Py_DECREF(f);
2863 --tstate->recursion_depth;
2864 return retval;
2868 /* Implementation notes for set_exc_info() and reset_exc_info():
2870 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2871 'exc_traceback'. These always travel together.
2873 - tstate->curexc_ZZZ is the "hot" exception that is set by
2874 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2876 - Once an exception is caught by an except clause, it is transferred
2877 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2878 can pick it up. This is the primary task of set_exc_info().
2879 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
2881 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
2883 Long ago, when none of this existed, there were just a few globals:
2884 one set corresponding to the "hot" exception, and one set
2885 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2886 globals; they were simply stored as sys.exc_ZZZ. For backwards
2887 compatibility, they still are!) The problem was that in code like
2888 this:
2890 try:
2891 "something that may fail"
2892 except "some exception":
2893 "do something else first"
2894 "print the exception from sys.exc_ZZZ."
2896 if "do something else first" invoked something that raised and caught
2897 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2898 cause of subtle bugs. I fixed this by changing the semantics as
2899 follows:
2901 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2902 *in that frame*.
2904 - But initially, and as long as no exception is caught in a given
2905 frame, sys.exc_ZZZ will hold the last exception caught in the
2906 previous frame (or the frame before that, etc.).
2908 The first bullet fixed the bug in the above example. The second
2909 bullet was for backwards compatibility: it was (and is) common to
2910 have a function that is called when an exception is caught, and to
2911 have that function access the caught exception via sys.exc_ZZZ.
2912 (Example: traceback.print_exc()).
2914 At the same time I fixed the problem that sys.exc_ZZZ weren't
2915 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2916 but that's really a separate improvement.
2918 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2919 variables to what they were before the current frame was called. The
2920 set_exc_info() function saves them on the frame so that
2921 reset_exc_info() can restore them. The invariant is that
2922 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2923 exception (where "catching" an exception applies only to successful
2924 except clauses); and if the current frame ever caught an exception,
2925 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2926 at the start of the current frame.
2930 static void
2931 set_exc_info(PyThreadState *tstate,
2932 PyObject *type, PyObject *value, PyObject *tb)
2934 PyFrameObject *frame = tstate->frame;
2935 PyObject *tmp_type, *tmp_value, *tmp_tb;
2937 assert(type != NULL);
2938 assert(frame != NULL);
2939 if (frame->f_exc_type == NULL) {
2940 assert(frame->f_exc_value == NULL);
2941 assert(frame->f_exc_traceback == NULL);
2942 /* This frame didn't catch an exception before. */
2943 /* Save previous exception of this thread in this frame. */
2944 if (tstate->exc_type == NULL) {
2945 /* XXX Why is this set to Py_None? */
2946 Py_INCREF(Py_None);
2947 tstate->exc_type = Py_None;
2949 Py_INCREF(tstate->exc_type);
2950 Py_XINCREF(tstate->exc_value);
2951 Py_XINCREF(tstate->exc_traceback);
2952 frame->f_exc_type = tstate->exc_type;
2953 frame->f_exc_value = tstate->exc_value;
2954 frame->f_exc_traceback = tstate->exc_traceback;
2956 /* Set new exception for this thread. */
2957 tmp_type = tstate->exc_type;
2958 tmp_value = tstate->exc_value;
2959 tmp_tb = tstate->exc_traceback;
2960 Py_INCREF(type);
2961 Py_XINCREF(value);
2962 Py_XINCREF(tb);
2963 tstate->exc_type = type;
2964 tstate->exc_value = value;
2965 tstate->exc_traceback = tb;
2966 Py_XDECREF(tmp_type);
2967 Py_XDECREF(tmp_value);
2968 Py_XDECREF(tmp_tb);
2969 /* For b/w compatibility */
2970 PySys_SetObject("exc_type", type);
2971 PySys_SetObject("exc_value", value);
2972 PySys_SetObject("exc_traceback", tb);
2975 static void
2976 reset_exc_info(PyThreadState *tstate)
2978 PyFrameObject *frame;
2979 PyObject *tmp_type, *tmp_value, *tmp_tb;
2981 /* It's a precondition that the thread state's frame caught an
2982 * exception -- verify in a debug build.
2984 assert(tstate != NULL);
2985 frame = tstate->frame;
2986 assert(frame != NULL);
2987 assert(frame->f_exc_type != NULL);
2989 /* Copy the frame's exception info back to the thread state. */
2990 tmp_type = tstate->exc_type;
2991 tmp_value = tstate->exc_value;
2992 tmp_tb = tstate->exc_traceback;
2993 Py_INCREF(frame->f_exc_type);
2994 Py_XINCREF(frame->f_exc_value);
2995 Py_XINCREF(frame->f_exc_traceback);
2996 tstate->exc_type = frame->f_exc_type;
2997 tstate->exc_value = frame->f_exc_value;
2998 tstate->exc_traceback = frame->f_exc_traceback;
2999 Py_XDECREF(tmp_type);
3000 Py_XDECREF(tmp_value);
3001 Py_XDECREF(tmp_tb);
3003 /* For b/w compatibility */
3004 PySys_SetObject("exc_type", frame->f_exc_type);
3005 PySys_SetObject("exc_value", frame->f_exc_value);
3006 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3008 /* Clear the frame's exception info. */
3009 tmp_type = frame->f_exc_type;
3010 tmp_value = frame->f_exc_value;
3011 tmp_tb = frame->f_exc_traceback;
3012 frame->f_exc_type = NULL;
3013 frame->f_exc_value = NULL;
3014 frame->f_exc_traceback = NULL;
3015 Py_DECREF(tmp_type);
3016 Py_XDECREF(tmp_value);
3017 Py_XDECREF(tmp_tb);
3020 /* Logic for the raise statement (too complicated for inlining).
3021 This *consumes* a reference count to each of its arguments. */
3022 static enum why_code
3023 do_raise(PyObject *type, PyObject *value, PyObject *tb)
3025 if (type == NULL) {
3026 /* Reraise */
3027 PyThreadState *tstate = PyThreadState_GET();
3028 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3029 value = tstate->exc_value;
3030 tb = tstate->exc_traceback;
3031 Py_XINCREF(type);
3032 Py_XINCREF(value);
3033 Py_XINCREF(tb);
3036 /* We support the following forms of raise:
3037 raise <class>, <classinstance>
3038 raise <class>, <argument tuple>
3039 raise <class>, None
3040 raise <class>, <argument>
3041 raise <classinstance>, None
3042 raise <string>, <object>
3043 raise <string>, None
3045 An omitted second argument is the same as None.
3047 In addition, raise <tuple>, <anything> is the same as
3048 raising the tuple's first item (and it better have one!);
3049 this rule is applied recursively.
3051 Finally, an optional third argument can be supplied, which
3052 gives the traceback to be substituted (useful when
3053 re-raising an exception after examining it). */
3055 /* First, check the traceback argument, replacing None with
3056 NULL. */
3057 if (tb == Py_None) {
3058 Py_DECREF(tb);
3059 tb = NULL;
3061 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3062 PyErr_SetString(PyExc_TypeError,
3063 "raise: arg 3 must be a traceback or None");
3064 goto raise_error;
3067 /* Next, replace a missing value with None */
3068 if (value == NULL) {
3069 value = Py_None;
3070 Py_INCREF(value);
3073 /* Next, repeatedly, replace a tuple exception with its first item */
3074 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3075 PyObject *tmp = type;
3076 type = PyTuple_GET_ITEM(type, 0);
3077 Py_INCREF(type);
3078 Py_DECREF(tmp);
3081 if (PyExceptionClass_Check(type))
3082 PyErr_NormalizeException(&type, &value, &tb);
3084 else if (PyExceptionInstance_Check(type)) {
3085 /* Raising an instance. The value should be a dummy. */
3086 if (value != Py_None) {
3087 PyErr_SetString(PyExc_TypeError,
3088 "instance exception may not have a separate value");
3089 goto raise_error;
3091 else {
3092 /* Normalize to raise <class>, <instance> */
3093 Py_DECREF(value);
3094 value = type;
3095 type = PyExceptionInstance_Class(type);
3096 Py_INCREF(type);
3099 else {
3100 /* Not something you can raise. You get an exception
3101 anyway, just not what you specified :-) */
3102 PyErr_Format(PyExc_TypeError,
3103 "exceptions must be classes or instances, not %s",
3104 type->ob_type->tp_name);
3105 goto raise_error;
3107 PyErr_Restore(type, value, tb);
3108 if (tb == NULL)
3109 return WHY_EXCEPTION;
3110 else
3111 return WHY_RERAISE;
3112 raise_error:
3113 Py_XDECREF(value);
3114 Py_XDECREF(type);
3115 Py_XDECREF(tb);
3116 return WHY_EXCEPTION;
3119 /* Iterate v argcnt times and store the results on the stack (via decreasing
3120 sp). Return 1 for success, 0 if error. */
3122 static int
3123 unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
3125 int i = 0;
3126 PyObject *it; /* iter(v) */
3127 PyObject *w;
3129 assert(v != NULL);
3131 it = PyObject_GetIter(v);
3132 if (it == NULL)
3133 goto Error;
3135 for (; i < argcnt; i++) {
3136 w = PyIter_Next(it);
3137 if (w == NULL) {
3138 /* Iterator done, via error or exhaustion. */
3139 if (!PyErr_Occurred()) {
3140 PyErr_Format(PyExc_ValueError,
3141 "need more than %d value%s to unpack",
3142 i, i == 1 ? "" : "s");
3144 goto Error;
3146 *--sp = w;
3149 /* We better have exhausted the iterator now. */
3150 w = PyIter_Next(it);
3151 if (w == NULL) {
3152 if (PyErr_Occurred())
3153 goto Error;
3154 Py_DECREF(it);
3155 return 1;
3157 Py_DECREF(w);
3158 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3159 /* fall through */
3160 Error:
3161 for (; i > 0; i--, sp++)
3162 Py_DECREF(*sp);
3163 Py_XDECREF(it);
3164 return 0;
3168 #ifdef LLTRACE
3169 static int
3170 prtrace(PyObject *v, char *str)
3172 printf("%s ", str);
3173 if (PyObject_Print(v, stdout, 0) != 0)
3174 PyErr_Clear(); /* Don't know what else to do */
3175 printf("\n");
3176 return 1;
3178 #endif
3180 static void
3181 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
3183 PyObject *type, *value, *traceback, *arg;
3184 int err;
3185 PyErr_Fetch(&type, &value, &traceback);
3186 if (value == NULL) {
3187 value = Py_None;
3188 Py_INCREF(value);
3190 arg = PyTuple_Pack(3, type, value, traceback);
3191 if (arg == NULL) {
3192 PyErr_Restore(type, value, traceback);
3193 return;
3195 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3196 Py_DECREF(arg);
3197 if (err == 0)
3198 PyErr_Restore(type, value, traceback);
3199 else {
3200 Py_XDECREF(type);
3201 Py_XDECREF(value);
3202 Py_XDECREF(traceback);
3206 static void
3207 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3208 int what, PyObject *arg)
3210 PyObject *type, *value, *traceback;
3211 int err;
3212 PyErr_Fetch(&type, &value, &traceback);
3213 err = call_trace(func, obj, frame, what, arg);
3214 if (err == 0)
3215 PyErr_Restore(type, value, traceback);
3216 else {
3217 Py_XDECREF(type);
3218 Py_XDECREF(value);
3219 Py_XDECREF(traceback);
3223 static int
3224 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3225 int what, PyObject *arg)
3227 register PyThreadState *tstate = frame->f_tstate;
3228 int result;
3229 if (tstate->tracing)
3230 return 0;
3231 tstate->tracing++;
3232 tstate->use_tracing = 0;
3233 result = func(obj, frame, what, arg);
3234 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3235 || (tstate->c_profilefunc != NULL));
3236 tstate->tracing--;
3237 return result;
3240 PyObject *
3241 _PyEval_CallTracing(PyObject *func, PyObject *args)
3243 PyFrameObject *frame = PyEval_GetFrame();
3244 PyThreadState *tstate = frame->f_tstate;
3245 int save_tracing = tstate->tracing;
3246 int save_use_tracing = tstate->use_tracing;
3247 PyObject *result;
3249 tstate->tracing = 0;
3250 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3251 || (tstate->c_profilefunc != NULL));
3252 result = PyObject_Call(func, args, NULL);
3253 tstate->tracing = save_tracing;
3254 tstate->use_tracing = save_use_tracing;
3255 return result;
3258 static int
3259 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
3260 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3261 int *instr_prev)
3263 int result = 0;
3265 /* If the last instruction executed isn't in the current
3266 instruction window, reset the window. If the last
3267 instruction happens to fall at the start of a line or if it
3268 represents a jump backwards, call the trace function.
3270 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
3271 int line;
3272 PyAddrPair bounds;
3274 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3275 &bounds);
3276 if (line >= 0) {
3277 frame->f_lineno = line;
3278 result = call_trace(func, obj, frame,
3279 PyTrace_LINE, Py_None);
3281 *instr_lb = bounds.ap_lower;
3282 *instr_ub = bounds.ap_upper;
3284 else if (frame->f_lasti <= *instr_prev) {
3285 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3287 *instr_prev = frame->f_lasti;
3288 return result;
3291 void
3292 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
3294 PyThreadState *tstate = PyThreadState_GET();
3295 PyObject *temp = tstate->c_profileobj;
3296 Py_XINCREF(arg);
3297 tstate->c_profilefunc = NULL;
3298 tstate->c_profileobj = NULL;
3299 /* Must make sure that tracing is not ignored if 'temp' is freed */
3300 tstate->use_tracing = tstate->c_tracefunc != NULL;
3301 Py_XDECREF(temp);
3302 tstate->c_profilefunc = func;
3303 tstate->c_profileobj = arg;
3304 /* Flag that tracing or profiling is turned on */
3305 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
3308 void
3309 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3311 PyThreadState *tstate = PyThreadState_GET();
3312 PyObject *temp = tstate->c_traceobj;
3313 Py_XINCREF(arg);
3314 tstate->c_tracefunc = NULL;
3315 tstate->c_traceobj = NULL;
3316 /* Must make sure that profiling is not ignored if 'temp' is freed */
3317 tstate->use_tracing = tstate->c_profilefunc != NULL;
3318 Py_XDECREF(temp);
3319 tstate->c_tracefunc = func;
3320 tstate->c_traceobj = arg;
3321 /* Flag that tracing or profiling is turned on */
3322 tstate->use_tracing = ((func != NULL)
3323 || (tstate->c_profilefunc != NULL));
3326 PyObject *
3327 PyEval_GetBuiltins(void)
3329 PyFrameObject *current_frame = PyEval_GetFrame();
3330 if (current_frame == NULL)
3331 return PyThreadState_GET()->interp->builtins;
3332 else
3333 return current_frame->f_builtins;
3336 PyObject *
3337 PyEval_GetLocals(void)
3339 PyFrameObject *current_frame = PyEval_GetFrame();
3340 if (current_frame == NULL)
3341 return NULL;
3342 PyFrame_FastToLocals(current_frame);
3343 return current_frame->f_locals;
3346 PyObject *
3347 PyEval_GetGlobals(void)
3349 PyFrameObject *current_frame = PyEval_GetFrame();
3350 if (current_frame == NULL)
3351 return NULL;
3352 else
3353 return current_frame->f_globals;
3356 PyFrameObject *
3357 PyEval_GetFrame(void)
3359 PyThreadState *tstate = PyThreadState_GET();
3360 return _PyThreadState_GetFrame(tstate);
3364 PyEval_GetRestricted(void)
3366 PyFrameObject *current_frame = PyEval_GetFrame();
3367 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
3371 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
3373 PyFrameObject *current_frame = PyEval_GetFrame();
3374 int result = cf->cf_flags != 0;
3376 if (current_frame != NULL) {
3377 const int codeflags = current_frame->f_code->co_flags;
3378 const int compilerflags = codeflags & PyCF_MASK;
3379 if (compilerflags) {
3380 result = 1;
3381 cf->cf_flags |= compilerflags;
3383 #if 0 /* future keyword */
3384 if (codeflags & CO_GENERATOR_ALLOWED) {
3385 result = 1;
3386 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3388 #endif
3390 return result;
3394 Py_FlushLine(void)
3396 PyObject *f = PySys_GetObject("stdout");
3397 if (f == NULL)
3398 return 0;
3399 if (!PyFile_SoftSpace(f, 0))
3400 return 0;
3401 return PyFile_WriteString("\n", f);
3405 /* External interface to call any callable object.
3406 The arg must be a tuple or NULL. */
3408 #undef PyEval_CallObject
3409 /* for backward compatibility: export this interface */
3411 PyObject *
3412 PyEval_CallObject(PyObject *func, PyObject *arg)
3414 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
3416 #define PyEval_CallObject(func,arg) \
3417 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3419 PyObject *
3420 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
3422 PyObject *result;
3424 if (arg == NULL) {
3425 arg = PyTuple_New(0);
3426 if (arg == NULL)
3427 return NULL;
3429 else if (!PyTuple_Check(arg)) {
3430 PyErr_SetString(PyExc_TypeError,
3431 "argument list must be a tuple");
3432 return NULL;
3434 else
3435 Py_INCREF(arg);
3437 if (kw != NULL && !PyDict_Check(kw)) {
3438 PyErr_SetString(PyExc_TypeError,
3439 "keyword list must be a dictionary");
3440 Py_DECREF(arg);
3441 return NULL;
3444 result = PyObject_Call(func, arg, kw);
3445 Py_DECREF(arg);
3446 return result;
3449 const char *
3450 PyEval_GetFuncName(PyObject *func)
3452 if (PyMethod_Check(func))
3453 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3454 else if (PyFunction_Check(func))
3455 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3456 else if (PyCFunction_Check(func))
3457 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3458 else if (PyClass_Check(func))
3459 return PyString_AsString(((PyClassObject*)func)->cl_name);
3460 else if (PyInstance_Check(func)) {
3461 return PyString_AsString(
3462 ((PyInstanceObject*)func)->in_class->cl_name);
3463 } else {
3464 return func->ob_type->tp_name;
3468 const char *
3469 PyEval_GetFuncDesc(PyObject *func)
3471 if (PyMethod_Check(func))
3472 return "()";
3473 else if (PyFunction_Check(func))
3474 return "()";
3475 else if (PyCFunction_Check(func))
3476 return "()";
3477 else if (PyClass_Check(func))
3478 return " constructor";
3479 else if (PyInstance_Check(func)) {
3480 return " instance";
3481 } else {
3482 return " object";
3486 static void
3487 err_args(PyObject *func, int flags, int nargs)
3489 if (flags & METH_NOARGS)
3490 PyErr_Format(PyExc_TypeError,
3491 "%.200s() takes no arguments (%d given)",
3492 ((PyCFunctionObject *)func)->m_ml->ml_name,
3493 nargs);
3494 else
3495 PyErr_Format(PyExc_TypeError,
3496 "%.200s() takes exactly one argument (%d given)",
3497 ((PyCFunctionObject *)func)->m_ml->ml_name,
3498 nargs);
3501 #define C_TRACE(x, call) \
3502 if (tstate->use_tracing && tstate->c_profilefunc) { \
3503 if (call_trace(tstate->c_profilefunc, \
3504 tstate->c_profileobj, \
3505 tstate->frame, PyTrace_C_CALL, \
3506 func)) { \
3507 x = NULL; \
3509 else { \
3510 x = call; \
3511 if (tstate->c_profilefunc != NULL) { \
3512 if (x == NULL) { \
3513 call_trace_protected(tstate->c_profilefunc, \
3514 tstate->c_profileobj, \
3515 tstate->frame, PyTrace_C_EXCEPTION, \
3516 func); \
3517 /* XXX should pass (type, value, tb) */ \
3518 } else { \
3519 if (call_trace(tstate->c_profilefunc, \
3520 tstate->c_profileobj, \
3521 tstate->frame, PyTrace_C_RETURN, \
3522 func)) { \
3523 Py_DECREF(x); \
3524 x = NULL; \
3529 } else { \
3530 x = call; \
3533 static PyObject *
3534 call_function(PyObject ***pp_stack, int oparg
3535 #ifdef WITH_TSC
3536 , uint64* pintr0, uint64* pintr1
3537 #endif
3540 int na = oparg & 0xff;
3541 int nk = (oparg>>8) & 0xff;
3542 int n = na + 2 * nk;
3543 PyObject **pfunc = (*pp_stack) - n - 1;
3544 PyObject *func = *pfunc;
3545 PyObject *x, *w;
3547 /* Always dispatch PyCFunction first, because these are
3548 presumed to be the most frequent callable object.
3550 if (PyCFunction_Check(func) && nk == 0) {
3551 int flags = PyCFunction_GET_FLAGS(func);
3552 PyThreadState *tstate = PyThreadState_GET();
3554 PCALL(PCALL_CFUNCTION);
3555 if (flags & (METH_NOARGS | METH_O)) {
3556 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3557 PyObject *self = PyCFunction_GET_SELF(func);
3558 if (flags & METH_NOARGS && na == 0) {
3559 C_TRACE(x, (*meth)(self,NULL));
3561 else if (flags & METH_O && na == 1) {
3562 PyObject *arg = EXT_POP(*pp_stack);
3563 C_TRACE(x, (*meth)(self,arg));
3564 Py_DECREF(arg);
3566 else {
3567 err_args(func, flags, na);
3568 x = NULL;
3571 else {
3572 PyObject *callargs;
3573 callargs = load_args(pp_stack, na);
3574 READ_TIMESTAMP(*pintr0);
3575 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3576 READ_TIMESTAMP(*pintr1);
3577 Py_XDECREF(callargs);
3579 } else {
3580 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3581 /* optimize access to bound methods */
3582 PyObject *self = PyMethod_GET_SELF(func);
3583 PCALL(PCALL_METHOD);
3584 PCALL(PCALL_BOUND_METHOD);
3585 Py_INCREF(self);
3586 func = PyMethod_GET_FUNCTION(func);
3587 Py_INCREF(func);
3588 Py_DECREF(*pfunc);
3589 *pfunc = self;
3590 na++;
3591 n++;
3592 } else
3593 Py_INCREF(func);
3594 READ_TIMESTAMP(*pintr0);
3595 if (PyFunction_Check(func))
3596 x = fast_function(func, pp_stack, n, na, nk);
3597 else
3598 x = do_call(func, pp_stack, na, nk);
3599 READ_TIMESTAMP(*pintr1);
3600 Py_DECREF(func);
3603 /* Clear the stack of the function object. Also removes
3604 the arguments in case they weren't consumed already
3605 (fast_function() and err_args() leave them on the stack).
3607 while ((*pp_stack) > pfunc) {
3608 w = EXT_POP(*pp_stack);
3609 Py_DECREF(w);
3610 PCALL(PCALL_POP);
3612 return x;
3615 /* The fast_function() function optimize calls for which no argument
3616 tuple is necessary; the objects are passed directly from the stack.
3617 For the simplest case -- a function that takes only positional
3618 arguments and is called with only positional arguments -- it
3619 inlines the most primitive frame setup code from
3620 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3621 done before evaluating the frame.
3624 static PyObject *
3625 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
3627 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3628 PyObject *globals = PyFunction_GET_GLOBALS(func);
3629 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3630 PyObject **d = NULL;
3631 int nd = 0;
3633 PCALL(PCALL_FUNCTION);
3634 PCALL(PCALL_FAST_FUNCTION);
3635 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
3636 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3637 PyFrameObject *f;
3638 PyObject *retval = NULL;
3639 PyThreadState *tstate = PyThreadState_GET();
3640 PyObject **fastlocals, **stack;
3641 int i;
3643 PCALL(PCALL_FASTER_FUNCTION);
3644 assert(globals != NULL);
3645 /* XXX Perhaps we should create a specialized
3646 PyFrame_New() that doesn't take locals, but does
3647 take builtins without sanity checking them.
3649 assert(tstate != NULL);
3650 f = PyFrame_New(tstate, co, globals, NULL);
3651 if (f == NULL)
3652 return NULL;
3654 fastlocals = f->f_localsplus;
3655 stack = (*pp_stack) - n;
3657 for (i = 0; i < n; i++) {
3658 Py_INCREF(*stack);
3659 fastlocals[i] = *stack++;
3661 retval = PyEval_EvalFrameEx(f,0);
3662 ++tstate->recursion_depth;
3663 Py_DECREF(f);
3664 --tstate->recursion_depth;
3665 return retval;
3667 if (argdefs != NULL) {
3668 d = &PyTuple_GET_ITEM(argdefs, 0);
3669 nd = Py_Size(argdefs);
3671 return PyEval_EvalCodeEx(co, globals,
3672 (PyObject *)NULL, (*pp_stack)-n, na,
3673 (*pp_stack)-2*nk, nk, d, nd,
3674 PyFunction_GET_CLOSURE(func));
3677 static PyObject *
3678 update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3679 PyObject *func)
3681 PyObject *kwdict = NULL;
3682 if (orig_kwdict == NULL)
3683 kwdict = PyDict_New();
3684 else {
3685 kwdict = PyDict_Copy(orig_kwdict);
3686 Py_DECREF(orig_kwdict);
3688 if (kwdict == NULL)
3689 return NULL;
3690 while (--nk >= 0) {
3691 int err;
3692 PyObject *value = EXT_POP(*pp_stack);
3693 PyObject *key = EXT_POP(*pp_stack);
3694 if (PyDict_GetItem(kwdict, key) != NULL) {
3695 PyErr_Format(PyExc_TypeError,
3696 "%.200s%s got multiple values "
3697 "for keyword argument '%.200s'",
3698 PyEval_GetFuncName(func),
3699 PyEval_GetFuncDesc(func),
3700 PyString_AsString(key));
3701 Py_DECREF(key);
3702 Py_DECREF(value);
3703 Py_DECREF(kwdict);
3704 return NULL;
3706 err = PyDict_SetItem(kwdict, key, value);
3707 Py_DECREF(key);
3708 Py_DECREF(value);
3709 if (err) {
3710 Py_DECREF(kwdict);
3711 return NULL;
3714 return kwdict;
3717 static PyObject *
3718 update_star_args(int nstack, int nstar, PyObject *stararg,
3719 PyObject ***pp_stack)
3721 PyObject *callargs, *w;
3723 callargs = PyTuple_New(nstack + nstar);
3724 if (callargs == NULL) {
3725 return NULL;
3727 if (nstar) {
3728 int i;
3729 for (i = 0; i < nstar; i++) {
3730 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3731 Py_INCREF(a);
3732 PyTuple_SET_ITEM(callargs, nstack + i, a);
3735 while (--nstack >= 0) {
3736 w = EXT_POP(*pp_stack);
3737 PyTuple_SET_ITEM(callargs, nstack, w);
3739 return callargs;
3742 static PyObject *
3743 load_args(PyObject ***pp_stack, int na)
3745 PyObject *args = PyTuple_New(na);
3746 PyObject *w;
3748 if (args == NULL)
3749 return NULL;
3750 while (--na >= 0) {
3751 w = EXT_POP(*pp_stack);
3752 PyTuple_SET_ITEM(args, na, w);
3754 return args;
3757 static PyObject *
3758 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3760 PyObject *callargs = NULL;
3761 PyObject *kwdict = NULL;
3762 PyObject *result = NULL;
3764 if (nk > 0) {
3765 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
3766 if (kwdict == NULL)
3767 goto call_fail;
3769 callargs = load_args(pp_stack, na);
3770 if (callargs == NULL)
3771 goto call_fail;
3772 #ifdef CALL_PROFILE
3773 /* At this point, we have to look at the type of func to
3774 update the call stats properly. Do it here so as to avoid
3775 exposing the call stats machinery outside ceval.c
3777 if (PyFunction_Check(func))
3778 PCALL(PCALL_FUNCTION);
3779 else if (PyMethod_Check(func))
3780 PCALL(PCALL_METHOD);
3781 else if (PyType_Check(func))
3782 PCALL(PCALL_TYPE);
3783 else
3784 PCALL(PCALL_OTHER);
3785 #endif
3786 result = PyObject_Call(func, callargs, kwdict);
3787 call_fail:
3788 Py_XDECREF(callargs);
3789 Py_XDECREF(kwdict);
3790 return result;
3793 static PyObject *
3794 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3796 int nstar = 0;
3797 PyObject *callargs = NULL;
3798 PyObject *stararg = NULL;
3799 PyObject *kwdict = NULL;
3800 PyObject *result = NULL;
3802 if (flags & CALL_FLAG_KW) {
3803 kwdict = EXT_POP(*pp_stack);
3804 if (!PyDict_Check(kwdict)) {
3805 PyObject *d;
3806 d = PyDict_New();
3807 if (d == NULL)
3808 goto ext_call_fail;
3809 if (PyDict_Update(d, kwdict) != 0) {
3810 Py_DECREF(d);
3811 /* PyDict_Update raises attribute
3812 * error (percolated from an attempt
3813 * to get 'keys' attribute) instead of
3814 * a type error if its second argument
3815 * is not a mapping.
3817 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3818 PyErr_Format(PyExc_TypeError,
3819 "%.200s%.200s argument after ** "
3820 "must be a mapping, not %.200s",
3821 PyEval_GetFuncName(func),
3822 PyEval_GetFuncDesc(func),
3823 kwdict->ob_type->tp_name);
3825 goto ext_call_fail;
3827 Py_DECREF(kwdict);
3828 kwdict = d;
3831 if (flags & CALL_FLAG_VAR) {
3832 stararg = EXT_POP(*pp_stack);
3833 if (!PyTuple_Check(stararg)) {
3834 PyObject *t = NULL;
3835 t = PySequence_Tuple(stararg);
3836 if (t == NULL) {
3837 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3838 PyErr_Format(PyExc_TypeError,
3839 "%.200s%.200s argument after * "
3840 "must be a sequence, not %200s",
3841 PyEval_GetFuncName(func),
3842 PyEval_GetFuncDesc(func),
3843 stararg->ob_type->tp_name);
3845 goto ext_call_fail;
3847 Py_DECREF(stararg);
3848 stararg = t;
3850 nstar = PyTuple_GET_SIZE(stararg);
3852 if (nk > 0) {
3853 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
3854 if (kwdict == NULL)
3855 goto ext_call_fail;
3857 callargs = update_star_args(na, nstar, stararg, pp_stack);
3858 if (callargs == NULL)
3859 goto ext_call_fail;
3860 #ifdef CALL_PROFILE
3861 /* At this point, we have to look at the type of func to
3862 update the call stats properly. Do it here so as to avoid
3863 exposing the call stats machinery outside ceval.c
3865 if (PyFunction_Check(func))
3866 PCALL(PCALL_FUNCTION);
3867 else if (PyMethod_Check(func))
3868 PCALL(PCALL_METHOD);
3869 else if (PyType_Check(func))
3870 PCALL(PCALL_TYPE);
3871 else
3872 PCALL(PCALL_OTHER);
3873 #endif
3874 result = PyObject_Call(func, callargs, kwdict);
3875 ext_call_fail:
3876 Py_XDECREF(callargs);
3877 Py_XDECREF(kwdict);
3878 Py_XDECREF(stararg);
3879 return result;
3882 /* Extract a slice index from a PyInt or PyLong or an object with the
3883 nb_index slot defined, and store in *pi.
3884 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3885 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
3886 Return 0 on error, 1 on success.
3888 /* Note: If v is NULL, return success without storing into *pi. This
3889 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3890 called by the SLICE opcode with v and/or w equal to NULL.
3893 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
3895 if (v != NULL) {
3896 Py_ssize_t x;
3897 if (PyInt_Check(v)) {
3898 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3899 however, it looks like it should be AsSsize_t.
3900 There should be a comment here explaining why.
3902 x = PyInt_AS_LONG(v);
3904 else if (PyIndex_Check(v)) {
3905 x = PyNumber_AsSsize_t(v, NULL);
3906 if (x == -1 && PyErr_Occurred())
3907 return 0;
3909 else {
3910 PyErr_SetString(PyExc_TypeError,
3911 "slice indices must be integers or "
3912 "None or have an __index__ method");
3913 return 0;
3915 *pi = x;
3917 return 1;
3920 #undef ISINDEX
3921 #define ISINDEX(x) ((x) == NULL || \
3922 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
3924 static PyObject *
3925 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
3927 PyTypeObject *tp = u->ob_type;
3928 PySequenceMethods *sq = tp->tp_as_sequence;
3930 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
3931 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
3932 if (!_PyEval_SliceIndex(v, &ilow))
3933 return NULL;
3934 if (!_PyEval_SliceIndex(w, &ihigh))
3935 return NULL;
3936 return PySequence_GetSlice(u, ilow, ihigh);
3938 else {
3939 PyObject *slice = PySlice_New(v, w, NULL);
3940 if (slice != NULL) {
3941 PyObject *res = PyObject_GetItem(u, slice);
3942 Py_DECREF(slice);
3943 return res;
3945 else
3946 return NULL;
3950 static int
3951 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3952 /* u[v:w] = x */
3954 PyTypeObject *tp = u->ob_type;
3955 PySequenceMethods *sq = tp->tp_as_sequence;
3957 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
3958 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
3959 if (!_PyEval_SliceIndex(v, &ilow))
3960 return -1;
3961 if (!_PyEval_SliceIndex(w, &ihigh))
3962 return -1;
3963 if (x == NULL)
3964 return PySequence_DelSlice(u, ilow, ihigh);
3965 else
3966 return PySequence_SetSlice(u, ilow, ihigh, x);
3968 else {
3969 PyObject *slice = PySlice_New(v, w, NULL);
3970 if (slice != NULL) {
3971 int res;
3972 if (x != NULL)
3973 res = PyObject_SetItem(u, slice, x);
3974 else
3975 res = PyObject_DelItem(u, slice);
3976 Py_DECREF(slice);
3977 return res;
3979 else
3980 return -1;
3984 static PyObject *
3985 cmp_outcome(int op, register PyObject *v, register PyObject *w)
3987 int res = 0;
3988 switch (op) {
3989 case PyCmp_IS:
3990 res = (v == w);
3991 break;
3992 case PyCmp_IS_NOT:
3993 res = (v != w);
3994 break;
3995 case PyCmp_IN:
3996 res = PySequence_Contains(w, v);
3997 if (res < 0)
3998 return NULL;
3999 break;
4000 case PyCmp_NOT_IN:
4001 res = PySequence_Contains(w, v);
4002 if (res < 0)
4003 return NULL;
4004 res = !res;
4005 break;
4006 case PyCmp_EXC_MATCH:
4007 if (PyTuple_Check(w)) {
4008 Py_ssize_t i, length;
4009 length = PyTuple_Size(w);
4010 for (i = 0; i < length; i += 1) {
4011 PyObject *exc = PyTuple_GET_ITEM(w, i);
4012 if (PyString_Check(exc)) {
4013 int ret_val;
4014 ret_val = PyErr_WarnEx(
4015 PyExc_DeprecationWarning,
4016 "catching of string "
4017 "exceptions is deprecated", 1);
4018 if (ret_val == -1)
4019 return NULL;
4023 else {
4024 if (PyString_Check(w)) {
4025 int ret_val;
4026 ret_val = PyErr_WarnEx(
4027 PyExc_DeprecationWarning,
4028 "catching of string "
4029 "exceptions is deprecated", 1);
4030 if (ret_val == -1)
4031 return NULL;
4034 res = PyErr_GivenExceptionMatches(v, w);
4035 break;
4036 default:
4037 return PyObject_RichCompare(v, w, op);
4039 v = res ? Py_True : Py_False;
4040 Py_INCREF(v);
4041 return v;
4044 static PyObject *
4045 import_from(PyObject *v, PyObject *name)
4047 PyObject *x;
4049 x = PyObject_GetAttr(v, name);
4050 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4051 PyErr_Format(PyExc_ImportError,
4052 "cannot import name %.230s",
4053 PyString_AsString(name));
4055 return x;
4058 static int
4059 import_all_from(PyObject *locals, PyObject *v)
4061 PyObject *all = PyObject_GetAttrString(v, "__all__");
4062 PyObject *dict, *name, *value;
4063 int skip_leading_underscores = 0;
4064 int pos, err;
4066 if (all == NULL) {
4067 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4068 return -1; /* Unexpected error */
4069 PyErr_Clear();
4070 dict = PyObject_GetAttrString(v, "__dict__");
4071 if (dict == NULL) {
4072 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4073 return -1;
4074 PyErr_SetString(PyExc_ImportError,
4075 "from-import-* object has no __dict__ and no __all__");
4076 return -1;
4078 all = PyMapping_Keys(dict);
4079 Py_DECREF(dict);
4080 if (all == NULL)
4081 return -1;
4082 skip_leading_underscores = 1;
4085 for (pos = 0, err = 0; ; pos++) {
4086 name = PySequence_GetItem(all, pos);
4087 if (name == NULL) {
4088 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4089 err = -1;
4090 else
4091 PyErr_Clear();
4092 break;
4094 if (skip_leading_underscores &&
4095 PyString_Check(name) &&
4096 PyString_AS_STRING(name)[0] == '_')
4098 Py_DECREF(name);
4099 continue;
4101 value = PyObject_GetAttr(v, name);
4102 if (value == NULL)
4103 err = -1;
4104 else if (PyDict_CheckExact(locals))
4105 err = PyDict_SetItem(locals, name, value);
4106 else
4107 err = PyObject_SetItem(locals, name, value);
4108 Py_DECREF(name);
4109 Py_XDECREF(value);
4110 if (err != 0)
4111 break;
4113 Py_DECREF(all);
4114 return err;
4117 static PyObject *
4118 build_class(PyObject *methods, PyObject *bases, PyObject *name)
4120 PyObject *metaclass = NULL, *result, *base;
4122 if (PyDict_Check(methods))
4123 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4124 if (metaclass != NULL)
4125 Py_INCREF(metaclass);
4126 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4127 base = PyTuple_GET_ITEM(bases, 0);
4128 metaclass = PyObject_GetAttrString(base, "__class__");
4129 if (metaclass == NULL) {
4130 PyErr_Clear();
4131 metaclass = (PyObject *)base->ob_type;
4132 Py_INCREF(metaclass);
4135 else {
4136 PyObject *g = PyEval_GetGlobals();
4137 if (g != NULL && PyDict_Check(g))
4138 metaclass = PyDict_GetItemString(g, "__metaclass__");
4139 if (metaclass == NULL)
4140 metaclass = (PyObject *) &PyClass_Type;
4141 Py_INCREF(metaclass);
4143 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4144 NULL);
4145 Py_DECREF(metaclass);
4146 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4147 /* A type error here likely means that the user passed
4148 in a base that was not a class (such the random module
4149 instead of the random.random type). Help them out with
4150 by augmenting the error message with more information.*/
4152 PyObject *ptype, *pvalue, *ptraceback;
4154 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4155 if (PyString_Check(pvalue)) {
4156 PyObject *newmsg;
4157 newmsg = PyString_FromFormat(
4158 "Error when calling the metaclass bases\n"
4159 " %s",
4160 PyString_AS_STRING(pvalue));
4161 if (newmsg != NULL) {
4162 Py_DECREF(pvalue);
4163 pvalue = newmsg;
4166 PyErr_Restore(ptype, pvalue, ptraceback);
4168 return result;
4171 static int
4172 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4173 PyObject *locals)
4175 int n;
4176 PyObject *v;
4177 int plain = 0;
4179 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4180 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4181 /* Backward compatibility hack */
4182 globals = PyTuple_GetItem(prog, 1);
4183 if (n == 3)
4184 locals = PyTuple_GetItem(prog, 2);
4185 prog = PyTuple_GetItem(prog, 0);
4187 if (globals == Py_None) {
4188 globals = PyEval_GetGlobals();
4189 if (locals == Py_None) {
4190 locals = PyEval_GetLocals();
4191 plain = 1;
4193 if (!globals || !locals) {
4194 PyErr_SetString(PyExc_SystemError,
4195 "globals and locals cannot be NULL");
4196 return -1;
4199 else if (locals == Py_None)
4200 locals = globals;
4201 if (!PyString_Check(prog) &&
4202 !PyUnicode_Check(prog) &&
4203 !PyCode_Check(prog) &&
4204 !PyFile_Check(prog)) {
4205 PyErr_SetString(PyExc_TypeError,
4206 "exec: arg 1 must be a string, file, or code object");
4207 return -1;
4209 if (!PyDict_Check(globals)) {
4210 PyErr_SetString(PyExc_TypeError,
4211 "exec: arg 2 must be a dictionary or None");
4212 return -1;
4214 if (!PyMapping_Check(locals)) {
4215 PyErr_SetString(PyExc_TypeError,
4216 "exec: arg 3 must be a mapping or None");
4217 return -1;
4219 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4220 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4221 if (PyCode_Check(prog)) {
4222 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4223 PyErr_SetString(PyExc_TypeError,
4224 "code object passed to exec may not contain free variables");
4225 return -1;
4227 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4229 else if (PyFile_Check(prog)) {
4230 FILE *fp = PyFile_AsFile(prog);
4231 char *name = PyString_AsString(PyFile_Name(prog));
4232 PyCompilerFlags cf;
4233 if (name == NULL)
4234 return -1;
4235 cf.cf_flags = 0;
4236 if (PyEval_MergeCompilerFlags(&cf))
4237 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4238 locals, &cf);
4239 else
4240 v = PyRun_File(fp, name, Py_file_input, globals,
4241 locals);
4243 else {
4244 PyObject *tmp = NULL;
4245 char *str;
4246 PyCompilerFlags cf;
4247 cf.cf_flags = 0;
4248 #ifdef Py_USING_UNICODE
4249 if (PyUnicode_Check(prog)) {
4250 tmp = PyUnicode_AsUTF8String(prog);
4251 if (tmp == NULL)
4252 return -1;
4253 prog = tmp;
4254 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4256 #endif
4257 if (PyString_AsStringAndSize(prog, &str, NULL))
4258 return -1;
4259 if (PyEval_MergeCompilerFlags(&cf))
4260 v = PyRun_StringFlags(str, Py_file_input, globals,
4261 locals, &cf);
4262 else
4263 v = PyRun_String(str, Py_file_input, globals, locals);
4264 Py_XDECREF(tmp);
4266 if (plain)
4267 PyFrame_LocalsToFast(f, 0);
4268 if (v == NULL)
4269 return -1;
4270 Py_DECREF(v);
4271 return 0;
4274 static void
4275 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4277 char *obj_str;
4279 if (!obj)
4280 return;
4282 obj_str = PyString_AsString(obj);
4283 if (!obj_str)
4284 return;
4286 PyErr_Format(exc, format_str, obj_str);
4289 static PyObject *
4290 string_concatenate(PyObject *v, PyObject *w,
4291 PyFrameObject *f, unsigned char *next_instr)
4293 /* This function implements 'variable += expr' when both arguments
4294 are strings. */
4295 Py_ssize_t v_len = PyString_GET_SIZE(v);
4296 Py_ssize_t w_len = PyString_GET_SIZE(w);
4297 Py_ssize_t new_len = v_len + w_len;
4298 if (new_len < 0) {
4299 PyErr_SetString(PyExc_OverflowError,
4300 "strings are too large to concat");
4301 return NULL;
4304 if (v->ob_refcnt == 2) {
4305 /* In the common case, there are 2 references to the value
4306 * stored in 'variable' when the += is performed: one on the
4307 * value stack (in 'v') and one still stored in the
4308 * 'variable'. We try to delete the variable now to reduce
4309 * the refcnt to 1.
4311 switch (*next_instr) {
4312 case STORE_FAST:
4314 int oparg = PEEKARG();
4315 PyObject **fastlocals = f->f_localsplus;
4316 if (GETLOCAL(oparg) == v)
4317 SETLOCAL(oparg, NULL);
4318 break;
4320 case STORE_DEREF:
4322 PyObject **freevars = (f->f_localsplus +
4323 f->f_code->co_nlocals);
4324 PyObject *c = freevars[PEEKARG()];
4325 if (PyCell_GET(c) == v)
4326 PyCell_Set(c, NULL);
4327 break;
4329 case STORE_NAME:
4331 PyObject *names = f->f_code->co_names;
4332 PyObject *name = GETITEM(names, PEEKARG());
4333 PyObject *locals = f->f_locals;
4334 if (PyDict_CheckExact(locals) &&
4335 PyDict_GetItem(locals, name) == v) {
4336 if (PyDict_DelItem(locals, name) != 0) {
4337 PyErr_Clear();
4340 break;
4345 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4346 /* Now we own the last reference to 'v', so we can resize it
4347 * in-place.
4349 if (_PyString_Resize(&v, new_len) != 0) {
4350 /* XXX if _PyString_Resize() fails, 'v' has been
4351 * deallocated so it cannot be put back into
4352 * 'variable'. The MemoryError is raised when there
4353 * is no value in 'variable', which might (very
4354 * remotely) be a cause of incompatibilities.
4356 return NULL;
4358 /* copy 'w' into the newly allocated area of 'v' */
4359 memcpy(PyString_AS_STRING(v) + v_len,
4360 PyString_AS_STRING(w), w_len);
4361 return v;
4363 else {
4364 /* When in-place resizing is not an option. */
4365 PyString_Concat(&v, w);
4366 return v;
4370 #ifdef DYNAMIC_EXECUTION_PROFILE
4372 static PyObject *
4373 getarray(long a[256])
4375 int i;
4376 PyObject *l = PyList_New(256);
4377 if (l == NULL) return NULL;
4378 for (i = 0; i < 256; i++) {
4379 PyObject *x = PyInt_FromLong(a[i]);
4380 if (x == NULL) {
4381 Py_DECREF(l);
4382 return NULL;
4384 PyList_SetItem(l, i, x);
4386 for (i = 0; i < 256; i++)
4387 a[i] = 0;
4388 return l;
4391 PyObject *
4392 _Py_GetDXProfile(PyObject *self, PyObject *args)
4394 #ifndef DXPAIRS
4395 return getarray(dxp);
4396 #else
4397 int i;
4398 PyObject *l = PyList_New(257);
4399 if (l == NULL) return NULL;
4400 for (i = 0; i < 257; i++) {
4401 PyObject *x = getarray(dxpairs[i]);
4402 if (x == NULL) {
4403 Py_DECREF(l);
4404 return NULL;
4406 PyList_SetItem(l, i, x);
4408 return l;
4409 #endif
4412 #endif