Updated with fix for #3136.
[python.git] / Python / ceval.c
blob0cff157662fe78ffefe785a84c4c2a4b4a6de537
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 int call_trace_protected(Py_tracefunc, PyObject *,
111 PyFrameObject *, int, PyObject *);
112 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
113 static int maybe_call_line_trace(Py_tracefunc, PyObject *,
114 PyFrameObject *, int *, int *, int *);
116 static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
117 static int assign_slice(PyObject *, PyObject *,
118 PyObject *, PyObject *);
119 static PyObject * cmp_outcome(int, PyObject *, PyObject *);
120 static PyObject * import_from(PyObject *, PyObject *);
121 static int import_all_from(PyObject *, PyObject *);
122 static PyObject * build_class(PyObject *, PyObject *, PyObject *);
123 static int exec_statement(PyFrameObject *,
124 PyObject *, PyObject *, PyObject *);
125 static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
126 static void reset_exc_info(PyThreadState *);
127 static void format_exc_check_arg(PyObject *, char *, PyObject *);
128 static PyObject * string_concatenate(PyObject *, PyObject *,
129 PyFrameObject *, unsigned char *);
131 #define NAME_ERROR_MSG \
132 "name '%.200s' is not defined"
133 #define GLOBAL_NAME_ERROR_MSG \
134 "global name '%.200s' is not defined"
135 #define UNBOUNDLOCAL_ERROR_MSG \
136 "local variable '%.200s' referenced before assignment"
137 #define UNBOUNDFREE_ERROR_MSG \
138 "free variable '%.200s' referenced before assignment" \
139 " in enclosing scope"
141 /* Dynamic execution profile */
142 #ifdef DYNAMIC_EXECUTION_PROFILE
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 HAS_ARG test and the switch-case.
627 If collecting opcode statistics, turn off prediction so that
628 statistics are accurately maintained (the predictions bypass
629 the opcode frequency counter updates).
632 #ifdef DYNAMIC_EXECUTION_PROFILE
633 #define PREDICT(op) if (0) goto PRED_##op
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) ((void)(lltrace && \
669 prtrace((STACK_POINTER)[-1], "ext_pop")), \
670 *--(STACK_POINTER))
671 #else
672 #define PUSH(v) BASIC_PUSH(v)
673 #define POP() BASIC_POP()
674 #define STACKADJ(n) BASIC_STACKADJ(n)
675 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
676 #endif
678 /* Local variable macros */
680 #define GETLOCAL(i) (fastlocals[i])
682 /* The SETLOCAL() macro must not DECREF the local variable in-place and
683 then store the new value; it must copy the old value to a temporary
684 value, then store the new value, and then DECREF the temporary value.
685 This is because it is possible that during the DECREF the frame is
686 accessed by other code (e.g. a __del__ method or gc.collect()) and the
687 variable would be pointing to already-freed memory. */
688 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
689 GETLOCAL(i) = value; \
690 Py_XDECREF(tmp); } while (0)
692 /* Start of code */
694 if (f == NULL)
695 return NULL;
697 /* push frame */
698 if (Py_EnterRecursiveCall(""))
699 return NULL;
701 tstate->frame = f;
703 if (tstate->use_tracing) {
704 if (tstate->c_tracefunc != NULL) {
705 /* tstate->c_tracefunc, if defined, is a
706 function that will be called on *every* entry
707 to a code block. Its return value, if not
708 None, is a function that will be called at
709 the start of each executed line of code.
710 (Actually, the function must return itself
711 in order to continue tracing.) The trace
712 functions are called with three arguments:
713 a pointer to the current frame, a string
714 indicating why the function is called, and
715 an argument which depends on the situation.
716 The global trace function is also called
717 whenever an exception is detected. */
718 if (call_trace_protected(tstate->c_tracefunc,
719 tstate->c_traceobj,
720 f, PyTrace_CALL, Py_None)) {
721 /* Trace function raised an error */
722 goto exit_eval_frame;
725 if (tstate->c_profilefunc != NULL) {
726 /* Similar for c_profilefunc, except it needn't
727 return itself and isn't called for "line" events */
728 if (call_trace_protected(tstate->c_profilefunc,
729 tstate->c_profileobj,
730 f, PyTrace_CALL, Py_None)) {
731 /* Profile function raised an error */
732 goto exit_eval_frame;
737 co = f->f_code;
738 names = co->co_names;
739 consts = co->co_consts;
740 fastlocals = f->f_localsplus;
741 freevars = f->f_localsplus + co->co_nlocals;
742 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
743 /* An explanation is in order for the next line.
745 f->f_lasti now refers to the index of the last instruction
746 executed. You might think this was obvious from the name, but
747 this wasn't always true before 2.3! PyFrame_New now sets
748 f->f_lasti to -1 (i.e. the index *before* the first instruction)
749 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
750 does work. Promise.
752 When the PREDICT() macros are enabled, some opcode pairs follow in
753 direct succession without updating f->f_lasti. A successful
754 prediction effectively links the two codes together as if they
755 were a single new opcode; accordingly,f->f_lasti will point to
756 the first code in the pair (for instance, GET_ITER followed by
757 FOR_ITER is effectively a single opcode and f->f_lasti will point
758 at to the beginning of the combined pair.)
760 next_instr = first_instr + f->f_lasti + 1;
761 stack_pointer = f->f_stacktop;
762 assert(stack_pointer != NULL);
763 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
765 #ifdef LLTRACE
766 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
767 #endif
768 #if defined(Py_DEBUG) || defined(LLTRACE)
769 filename = PyString_AsString(co->co_filename);
770 #endif
772 why = WHY_NOT;
773 err = 0;
774 x = Py_None; /* Not a reference, just anything non-NULL */
775 w = NULL;
777 if (throwflag) { /* support for generator.throw() */
778 why = WHY_EXCEPTION;
779 goto on_error;
782 for (;;) {
783 #ifdef WITH_TSC
784 if (inst1 == 0) {
785 /* Almost surely, the opcode executed a break
786 or a continue, preventing inst1 from being set
787 on the way out of the loop.
789 READ_TIMESTAMP(inst1);
790 loop1 = inst1;
792 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
793 intr0, intr1);
794 ticked = 0;
795 inst1 = 0;
796 intr0 = 0;
797 intr1 = 0;
798 READ_TIMESTAMP(loop0);
799 #endif
800 assert(stack_pointer >= f->f_valuestack); /* else underflow */
801 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
803 /* Do periodic things. Doing this every time through
804 the loop would add too much overhead, so we do it
805 only every Nth instruction. We also do it if
806 ``things_to_do'' is set, i.e. when an asynchronous
807 event needs attention (e.g. a signal handler or
808 async I/O handler); see Py_AddPendingCall() and
809 Py_MakePendingCalls() above. */
811 if (--_Py_Ticker < 0) {
812 if (*next_instr == SETUP_FINALLY) {
813 /* Make the last opcode before
814 a try: finally: block uninterruptable. */
815 goto fast_next_opcode;
817 _Py_Ticker = _Py_CheckInterval;
818 tstate->tick_counter++;
819 #ifdef WITH_TSC
820 ticked = 1;
821 #endif
822 if (things_to_do) {
823 if (Py_MakePendingCalls() < 0) {
824 why = WHY_EXCEPTION;
825 goto on_error;
827 if (things_to_do)
828 /* MakePendingCalls() didn't succeed.
829 Force early re-execution of this
830 "periodic" code, possibly after
831 a thread switch */
832 _Py_Ticker = 0;
834 #ifdef WITH_THREAD
835 if (interpreter_lock) {
836 /* Give another thread a chance */
838 if (PyThreadState_Swap(NULL) != tstate)
839 Py_FatalError("ceval: tstate mix-up");
840 PyThread_release_lock(interpreter_lock);
842 /* Other threads may run now */
844 PyThread_acquire_lock(interpreter_lock, 1);
845 if (PyThreadState_Swap(tstate) != NULL)
846 Py_FatalError("ceval: orphan tstate");
848 /* Check for thread interrupts */
850 if (tstate->async_exc != NULL) {
851 x = tstate->async_exc;
852 tstate->async_exc = NULL;
853 PyErr_SetNone(x);
854 Py_DECREF(x);
855 why = WHY_EXCEPTION;
856 goto on_error;
859 #endif
862 fast_next_opcode:
863 f->f_lasti = INSTR_OFFSET();
865 /* line-by-line tracing support */
867 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
868 /* see maybe_call_line_trace
869 for expository comments */
870 f->f_stacktop = stack_pointer;
872 err = maybe_call_line_trace(tstate->c_tracefunc,
873 tstate->c_traceobj,
874 f, &instr_lb, &instr_ub,
875 &instr_prev);
876 /* Reload possibly changed frame fields */
877 JUMPTO(f->f_lasti);
878 if (f->f_stacktop != NULL) {
879 stack_pointer = f->f_stacktop;
880 f->f_stacktop = NULL;
882 if (err) {
883 /* trace function raised an exception */
884 goto on_error;
888 /* Extract opcode and argument */
890 opcode = NEXTOP();
891 oparg = 0; /* allows oparg to be stored in a register because
892 it doesn't have to be remembered across a full loop */
893 if (HAS_ARG(opcode))
894 oparg = NEXTARG();
895 dispatch_opcode:
896 #ifdef DYNAMIC_EXECUTION_PROFILE
897 #ifdef DXPAIRS
898 dxpairs[lastopcode][opcode]++;
899 lastopcode = opcode;
900 #endif
901 dxp[opcode]++;
902 #endif
904 #ifdef LLTRACE
905 /* Instruction tracing */
907 if (lltrace) {
908 if (HAS_ARG(opcode)) {
909 printf("%d: %d, %d\n",
910 f->f_lasti, opcode, oparg);
912 else {
913 printf("%d: %d\n",
914 f->f_lasti, opcode);
917 #endif
919 /* Main switch on opcode */
920 READ_TIMESTAMP(inst0);
922 switch (opcode) {
924 /* BEWARE!
925 It is essential that any operation that fails sets either
926 x to NULL, err to nonzero, or why to anything but WHY_NOT,
927 and that no operation that succeeds does this! */
929 /* case STOP_CODE: this is an error! */
931 case NOP:
932 goto fast_next_opcode;
934 case LOAD_FAST:
935 x = GETLOCAL(oparg);
936 if (x != NULL) {
937 Py_INCREF(x);
938 PUSH(x);
939 goto fast_next_opcode;
941 format_exc_check_arg(PyExc_UnboundLocalError,
942 UNBOUNDLOCAL_ERROR_MSG,
943 PyTuple_GetItem(co->co_varnames, oparg));
944 break;
946 case LOAD_CONST:
947 x = GETITEM(consts, oparg);
948 Py_INCREF(x);
949 PUSH(x);
950 goto fast_next_opcode;
952 PREDICTED_WITH_ARG(STORE_FAST);
953 case STORE_FAST:
954 v = POP();
955 SETLOCAL(oparg, v);
956 goto fast_next_opcode;
958 PREDICTED(POP_TOP);
959 case POP_TOP:
960 v = POP();
961 Py_DECREF(v);
962 goto fast_next_opcode;
964 case ROT_TWO:
965 v = TOP();
966 w = SECOND();
967 SET_TOP(w);
968 SET_SECOND(v);
969 goto fast_next_opcode;
971 case ROT_THREE:
972 v = TOP();
973 w = SECOND();
974 x = THIRD();
975 SET_TOP(w);
976 SET_SECOND(x);
977 SET_THIRD(v);
978 goto fast_next_opcode;
980 case ROT_FOUR:
981 u = TOP();
982 v = SECOND();
983 w = THIRD();
984 x = FOURTH();
985 SET_TOP(v);
986 SET_SECOND(w);
987 SET_THIRD(x);
988 SET_FOURTH(u);
989 goto fast_next_opcode;
991 case DUP_TOP:
992 v = TOP();
993 Py_INCREF(v);
994 PUSH(v);
995 goto fast_next_opcode;
997 case DUP_TOPX:
998 if (oparg == 2) {
999 x = TOP();
1000 Py_INCREF(x);
1001 w = SECOND();
1002 Py_INCREF(w);
1003 STACKADJ(2);
1004 SET_TOP(x);
1005 SET_SECOND(w);
1006 goto fast_next_opcode;
1007 } else if (oparg == 3) {
1008 x = TOP();
1009 Py_INCREF(x);
1010 w = SECOND();
1011 Py_INCREF(w);
1012 v = THIRD();
1013 Py_INCREF(v);
1014 STACKADJ(3);
1015 SET_TOP(x);
1016 SET_SECOND(w);
1017 SET_THIRD(v);
1018 goto fast_next_opcode;
1020 Py_FatalError("invalid argument to DUP_TOPX"
1021 " (bytecode corruption?)");
1022 break;
1024 case UNARY_POSITIVE:
1025 v = TOP();
1026 x = PyNumber_Positive(v);
1027 Py_DECREF(v);
1028 SET_TOP(x);
1029 if (x != NULL) continue;
1030 break;
1032 case UNARY_NEGATIVE:
1033 v = TOP();
1034 x = PyNumber_Negative(v);
1035 Py_DECREF(v);
1036 SET_TOP(x);
1037 if (x != NULL) continue;
1038 break;
1040 case UNARY_NOT:
1041 v = TOP();
1042 err = PyObject_IsTrue(v);
1043 Py_DECREF(v);
1044 if (err == 0) {
1045 Py_INCREF(Py_True);
1046 SET_TOP(Py_True);
1047 continue;
1049 else if (err > 0) {
1050 Py_INCREF(Py_False);
1051 SET_TOP(Py_False);
1052 err = 0;
1053 continue;
1055 STACKADJ(-1);
1056 break;
1058 case UNARY_CONVERT:
1059 v = TOP();
1060 x = PyObject_Repr(v);
1061 Py_DECREF(v);
1062 SET_TOP(x);
1063 if (x != NULL) continue;
1064 break;
1066 case UNARY_INVERT:
1067 v = TOP();
1068 x = PyNumber_Invert(v);
1069 Py_DECREF(v);
1070 SET_TOP(x);
1071 if (x != NULL) continue;
1072 break;
1074 case BINARY_POWER:
1075 w = POP();
1076 v = TOP();
1077 x = PyNumber_Power(v, w, Py_None);
1078 Py_DECREF(v);
1079 Py_DECREF(w);
1080 SET_TOP(x);
1081 if (x != NULL) continue;
1082 break;
1084 case BINARY_MULTIPLY:
1085 w = POP();
1086 v = TOP();
1087 x = PyNumber_Multiply(v, w);
1088 Py_DECREF(v);
1089 Py_DECREF(w);
1090 SET_TOP(x);
1091 if (x != NULL) continue;
1092 break;
1094 case BINARY_DIVIDE:
1095 if (!_Py_QnewFlag) {
1096 w = POP();
1097 v = TOP();
1098 x = PyNumber_Divide(v, w);
1099 Py_DECREF(v);
1100 Py_DECREF(w);
1101 SET_TOP(x);
1102 if (x != NULL) continue;
1103 break;
1105 /* -Qnew is in effect: fall through to
1106 BINARY_TRUE_DIVIDE */
1107 case BINARY_TRUE_DIVIDE:
1108 w = POP();
1109 v = TOP();
1110 x = PyNumber_TrueDivide(v, w);
1111 Py_DECREF(v);
1112 Py_DECREF(w);
1113 SET_TOP(x);
1114 if (x != NULL) continue;
1115 break;
1117 case BINARY_FLOOR_DIVIDE:
1118 w = POP();
1119 v = TOP();
1120 x = PyNumber_FloorDivide(v, w);
1121 Py_DECREF(v);
1122 Py_DECREF(w);
1123 SET_TOP(x);
1124 if (x != NULL) continue;
1125 break;
1127 case BINARY_MODULO:
1128 w = POP();
1129 v = TOP();
1130 x = PyNumber_Remainder(v, w);
1131 Py_DECREF(v);
1132 Py_DECREF(w);
1133 SET_TOP(x);
1134 if (x != NULL) continue;
1135 break;
1137 case BINARY_ADD:
1138 w = POP();
1139 v = TOP();
1140 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1141 /* INLINE: int + int */
1142 register long a, b, i;
1143 a = PyInt_AS_LONG(v);
1144 b = PyInt_AS_LONG(w);
1145 i = a + b;
1146 if ((i^a) < 0 && (i^b) < 0)
1147 goto slow_add;
1148 x = PyInt_FromLong(i);
1150 else if (PyString_CheckExact(v) &&
1151 PyString_CheckExact(w)) {
1152 x = string_concatenate(v, w, f, next_instr);
1153 /* string_concatenate consumed the ref to v */
1154 goto skip_decref_vx;
1156 else {
1157 slow_add:
1158 x = PyNumber_Add(v, w);
1160 Py_DECREF(v);
1161 skip_decref_vx:
1162 Py_DECREF(w);
1163 SET_TOP(x);
1164 if (x != NULL) continue;
1165 break;
1167 case BINARY_SUBTRACT:
1168 w = POP();
1169 v = TOP();
1170 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1171 /* INLINE: int - int */
1172 register long a, b, i;
1173 a = PyInt_AS_LONG(v);
1174 b = PyInt_AS_LONG(w);
1175 i = a - b;
1176 if ((i^a) < 0 && (i^~b) < 0)
1177 goto slow_sub;
1178 x = PyInt_FromLong(i);
1180 else {
1181 slow_sub:
1182 x = PyNumber_Subtract(v, w);
1184 Py_DECREF(v);
1185 Py_DECREF(w);
1186 SET_TOP(x);
1187 if (x != NULL) continue;
1188 break;
1190 case BINARY_SUBSCR:
1191 w = POP();
1192 v = TOP();
1193 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1194 /* INLINE: list[int] */
1195 Py_ssize_t i = PyInt_AsSsize_t(w);
1196 if (i < 0)
1197 i += PyList_GET_SIZE(v);
1198 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1199 x = PyList_GET_ITEM(v, i);
1200 Py_INCREF(x);
1202 else
1203 goto slow_get;
1205 else
1206 slow_get:
1207 x = PyObject_GetItem(v, w);
1208 Py_DECREF(v);
1209 Py_DECREF(w);
1210 SET_TOP(x);
1211 if (x != NULL) continue;
1212 break;
1214 case BINARY_LSHIFT:
1215 w = POP();
1216 v = TOP();
1217 x = PyNumber_Lshift(v, w);
1218 Py_DECREF(v);
1219 Py_DECREF(w);
1220 SET_TOP(x);
1221 if (x != NULL) continue;
1222 break;
1224 case BINARY_RSHIFT:
1225 w = POP();
1226 v = TOP();
1227 x = PyNumber_Rshift(v, w);
1228 Py_DECREF(v);
1229 Py_DECREF(w);
1230 SET_TOP(x);
1231 if (x != NULL) continue;
1232 break;
1234 case BINARY_AND:
1235 w = POP();
1236 v = TOP();
1237 x = PyNumber_And(v, w);
1238 Py_DECREF(v);
1239 Py_DECREF(w);
1240 SET_TOP(x);
1241 if (x != NULL) continue;
1242 break;
1244 case BINARY_XOR:
1245 w = POP();
1246 v = TOP();
1247 x = PyNumber_Xor(v, w);
1248 Py_DECREF(v);
1249 Py_DECREF(w);
1250 SET_TOP(x);
1251 if (x != NULL) continue;
1252 break;
1254 case BINARY_OR:
1255 w = POP();
1256 v = TOP();
1257 x = PyNumber_Or(v, w);
1258 Py_DECREF(v);
1259 Py_DECREF(w);
1260 SET_TOP(x);
1261 if (x != NULL) continue;
1262 break;
1264 case LIST_APPEND:
1265 w = POP();
1266 v = POP();
1267 err = PyList_Append(v, w);
1268 Py_DECREF(v);
1269 Py_DECREF(w);
1270 if (err == 0) {
1271 PREDICT(JUMP_ABSOLUTE);
1272 continue;
1274 break;
1276 case INPLACE_POWER:
1277 w = POP();
1278 v = TOP();
1279 x = PyNumber_InPlacePower(v, w, Py_None);
1280 Py_DECREF(v);
1281 Py_DECREF(w);
1282 SET_TOP(x);
1283 if (x != NULL) continue;
1284 break;
1286 case INPLACE_MULTIPLY:
1287 w = POP();
1288 v = TOP();
1289 x = PyNumber_InPlaceMultiply(v, w);
1290 Py_DECREF(v);
1291 Py_DECREF(w);
1292 SET_TOP(x);
1293 if (x != NULL) continue;
1294 break;
1296 case INPLACE_DIVIDE:
1297 if (!_Py_QnewFlag) {
1298 w = POP();
1299 v = TOP();
1300 x = PyNumber_InPlaceDivide(v, w);
1301 Py_DECREF(v);
1302 Py_DECREF(w);
1303 SET_TOP(x);
1304 if (x != NULL) continue;
1305 break;
1307 /* -Qnew is in effect: fall through to
1308 INPLACE_TRUE_DIVIDE */
1309 case INPLACE_TRUE_DIVIDE:
1310 w = POP();
1311 v = TOP();
1312 x = PyNumber_InPlaceTrueDivide(v, w);
1313 Py_DECREF(v);
1314 Py_DECREF(w);
1315 SET_TOP(x);
1316 if (x != NULL) continue;
1317 break;
1319 case INPLACE_FLOOR_DIVIDE:
1320 w = POP();
1321 v = TOP();
1322 x = PyNumber_InPlaceFloorDivide(v, w);
1323 Py_DECREF(v);
1324 Py_DECREF(w);
1325 SET_TOP(x);
1326 if (x != NULL) continue;
1327 break;
1329 case INPLACE_MODULO:
1330 w = POP();
1331 v = TOP();
1332 x = PyNumber_InPlaceRemainder(v, w);
1333 Py_DECREF(v);
1334 Py_DECREF(w);
1335 SET_TOP(x);
1336 if (x != NULL) continue;
1337 break;
1339 case INPLACE_ADD:
1340 w = POP();
1341 v = TOP();
1342 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1343 /* INLINE: int + int */
1344 register long a, b, i;
1345 a = PyInt_AS_LONG(v);
1346 b = PyInt_AS_LONG(w);
1347 i = a + b;
1348 if ((i^a) < 0 && (i^b) < 0)
1349 goto slow_iadd;
1350 x = PyInt_FromLong(i);
1352 else if (PyString_CheckExact(v) &&
1353 PyString_CheckExact(w)) {
1354 x = string_concatenate(v, w, f, next_instr);
1355 /* string_concatenate consumed the ref to v */
1356 goto skip_decref_v;
1358 else {
1359 slow_iadd:
1360 x = PyNumber_InPlaceAdd(v, w);
1362 Py_DECREF(v);
1363 skip_decref_v:
1364 Py_DECREF(w);
1365 SET_TOP(x);
1366 if (x != NULL) continue;
1367 break;
1369 case INPLACE_SUBTRACT:
1370 w = POP();
1371 v = TOP();
1372 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1373 /* INLINE: int - int */
1374 register long a, b, i;
1375 a = PyInt_AS_LONG(v);
1376 b = PyInt_AS_LONG(w);
1377 i = a - b;
1378 if ((i^a) < 0 && (i^~b) < 0)
1379 goto slow_isub;
1380 x = PyInt_FromLong(i);
1382 else {
1383 slow_isub:
1384 x = PyNumber_InPlaceSubtract(v, w);
1386 Py_DECREF(v);
1387 Py_DECREF(w);
1388 SET_TOP(x);
1389 if (x != NULL) continue;
1390 break;
1392 case INPLACE_LSHIFT:
1393 w = POP();
1394 v = TOP();
1395 x = PyNumber_InPlaceLshift(v, w);
1396 Py_DECREF(v);
1397 Py_DECREF(w);
1398 SET_TOP(x);
1399 if (x != NULL) continue;
1400 break;
1402 case INPLACE_RSHIFT:
1403 w = POP();
1404 v = TOP();
1405 x = PyNumber_InPlaceRshift(v, w);
1406 Py_DECREF(v);
1407 Py_DECREF(w);
1408 SET_TOP(x);
1409 if (x != NULL) continue;
1410 break;
1412 case INPLACE_AND:
1413 w = POP();
1414 v = TOP();
1415 x = PyNumber_InPlaceAnd(v, w);
1416 Py_DECREF(v);
1417 Py_DECREF(w);
1418 SET_TOP(x);
1419 if (x != NULL) continue;
1420 break;
1422 case INPLACE_XOR:
1423 w = POP();
1424 v = TOP();
1425 x = PyNumber_InPlaceXor(v, w);
1426 Py_DECREF(v);
1427 Py_DECREF(w);
1428 SET_TOP(x);
1429 if (x != NULL) continue;
1430 break;
1432 case INPLACE_OR:
1433 w = POP();
1434 v = TOP();
1435 x = PyNumber_InPlaceOr(v, w);
1436 Py_DECREF(v);
1437 Py_DECREF(w);
1438 SET_TOP(x);
1439 if (x != NULL) continue;
1440 break;
1442 case SLICE+0:
1443 case SLICE+1:
1444 case SLICE+2:
1445 case SLICE+3:
1446 if ((opcode-SLICE) & 2)
1447 w = POP();
1448 else
1449 w = NULL;
1450 if ((opcode-SLICE) & 1)
1451 v = POP();
1452 else
1453 v = NULL;
1454 u = TOP();
1455 x = apply_slice(u, v, w);
1456 Py_DECREF(u);
1457 Py_XDECREF(v);
1458 Py_XDECREF(w);
1459 SET_TOP(x);
1460 if (x != NULL) continue;
1461 break;
1463 case STORE_SLICE+0:
1464 case STORE_SLICE+1:
1465 case STORE_SLICE+2:
1466 case STORE_SLICE+3:
1467 if ((opcode-STORE_SLICE) & 2)
1468 w = POP();
1469 else
1470 w = NULL;
1471 if ((opcode-STORE_SLICE) & 1)
1472 v = POP();
1473 else
1474 v = NULL;
1475 u = POP();
1476 t = POP();
1477 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1478 Py_DECREF(t);
1479 Py_DECREF(u);
1480 Py_XDECREF(v);
1481 Py_XDECREF(w);
1482 if (err == 0) continue;
1483 break;
1485 case DELETE_SLICE+0:
1486 case DELETE_SLICE+1:
1487 case DELETE_SLICE+2:
1488 case DELETE_SLICE+3:
1489 if ((opcode-DELETE_SLICE) & 2)
1490 w = POP();
1491 else
1492 w = NULL;
1493 if ((opcode-DELETE_SLICE) & 1)
1494 v = POP();
1495 else
1496 v = NULL;
1497 u = POP();
1498 err = assign_slice(u, v, w, (PyObject *)NULL);
1499 /* del u[v:w] */
1500 Py_DECREF(u);
1501 Py_XDECREF(v);
1502 Py_XDECREF(w);
1503 if (err == 0) continue;
1504 break;
1506 case STORE_SUBSCR:
1507 w = TOP();
1508 v = SECOND();
1509 u = THIRD();
1510 STACKADJ(-3);
1511 /* v[w] = u */
1512 err = PyObject_SetItem(v, w, u);
1513 Py_DECREF(u);
1514 Py_DECREF(v);
1515 Py_DECREF(w);
1516 if (err == 0) continue;
1517 break;
1519 case DELETE_SUBSCR:
1520 w = TOP();
1521 v = SECOND();
1522 STACKADJ(-2);
1523 /* del v[w] */
1524 err = PyObject_DelItem(v, w);
1525 Py_DECREF(v);
1526 Py_DECREF(w);
1527 if (err == 0) continue;
1528 break;
1530 case PRINT_EXPR:
1531 v = POP();
1532 w = PySys_GetObject("displayhook");
1533 if (w == NULL) {
1534 PyErr_SetString(PyExc_RuntimeError,
1535 "lost sys.displayhook");
1536 err = -1;
1537 x = NULL;
1539 if (err == 0) {
1540 x = PyTuple_Pack(1, v);
1541 if (x == NULL)
1542 err = -1;
1544 if (err == 0) {
1545 w = PyEval_CallObject(w, x);
1546 Py_XDECREF(w);
1547 if (w == NULL)
1548 err = -1;
1550 Py_DECREF(v);
1551 Py_XDECREF(x);
1552 break;
1554 case PRINT_ITEM_TO:
1555 w = stream = POP();
1556 /* fall through to PRINT_ITEM */
1558 case PRINT_ITEM:
1559 v = POP();
1560 if (stream == NULL || stream == Py_None) {
1561 w = PySys_GetObject("stdout");
1562 if (w == NULL) {
1563 PyErr_SetString(PyExc_RuntimeError,
1564 "lost sys.stdout");
1565 err = -1;
1568 /* PyFile_SoftSpace() can exececute arbitrary code
1569 if sys.stdout is an instance with a __getattr__.
1570 If __getattr__ raises an exception, w will
1571 be freed, so we need to prevent that temporarily. */
1572 Py_XINCREF(w);
1573 if (w != NULL && PyFile_SoftSpace(w, 0))
1574 err = PyFile_WriteString(" ", w);
1575 if (err == 0)
1576 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1577 if (err == 0) {
1578 /* XXX move into writeobject() ? */
1579 if (PyString_Check(v)) {
1580 char *s = PyString_AS_STRING(v);
1581 Py_ssize_t len = PyString_GET_SIZE(v);
1582 if (len == 0 ||
1583 !isspace(Py_CHARMASK(s[len-1])) ||
1584 s[len-1] == ' ')
1585 PyFile_SoftSpace(w, 1);
1587 #ifdef Py_USING_UNICODE
1588 else if (PyUnicode_Check(v)) {
1589 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1590 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1591 if (len == 0 ||
1592 !Py_UNICODE_ISSPACE(s[len-1]) ||
1593 s[len-1] == ' ')
1594 PyFile_SoftSpace(w, 1);
1596 #endif
1597 else
1598 PyFile_SoftSpace(w, 1);
1600 Py_XDECREF(w);
1601 Py_DECREF(v);
1602 Py_XDECREF(stream);
1603 stream = NULL;
1604 if (err == 0)
1605 continue;
1606 break;
1608 case PRINT_NEWLINE_TO:
1609 w = stream = POP();
1610 /* fall through to PRINT_NEWLINE */
1612 case PRINT_NEWLINE:
1613 if (stream == NULL || stream == Py_None) {
1614 w = PySys_GetObject("stdout");
1615 if (w == NULL)
1616 PyErr_SetString(PyExc_RuntimeError,
1617 "lost sys.stdout");
1619 if (w != NULL) {
1620 err = PyFile_WriteString("\n", w);
1621 if (err == 0)
1622 PyFile_SoftSpace(w, 0);
1624 Py_XDECREF(stream);
1625 stream = NULL;
1626 break;
1629 #ifdef CASE_TOO_BIG
1630 default: switch (opcode) {
1631 #endif
1632 case RAISE_VARARGS:
1633 u = v = w = NULL;
1634 switch (oparg) {
1635 case 3:
1636 u = POP(); /* traceback */
1637 /* Fallthrough */
1638 case 2:
1639 v = POP(); /* value */
1640 /* Fallthrough */
1641 case 1:
1642 w = POP(); /* exc */
1643 case 0: /* Fallthrough */
1644 why = do_raise(w, v, u);
1645 break;
1646 default:
1647 PyErr_SetString(PyExc_SystemError,
1648 "bad RAISE_VARARGS oparg");
1649 why = WHY_EXCEPTION;
1650 break;
1652 break;
1654 case LOAD_LOCALS:
1655 if ((x = f->f_locals) != NULL) {
1656 Py_INCREF(x);
1657 PUSH(x);
1658 continue;
1660 PyErr_SetString(PyExc_SystemError, "no locals");
1661 break;
1663 case RETURN_VALUE:
1664 retval = POP();
1665 why = WHY_RETURN;
1666 goto fast_block_end;
1668 case YIELD_VALUE:
1669 retval = POP();
1670 f->f_stacktop = stack_pointer;
1671 why = WHY_YIELD;
1672 goto fast_yield;
1674 case EXEC_STMT:
1675 w = TOP();
1676 v = SECOND();
1677 u = THIRD();
1678 STACKADJ(-3);
1679 READ_TIMESTAMP(intr0);
1680 err = exec_statement(f, u, v, w);
1681 READ_TIMESTAMP(intr1);
1682 Py_DECREF(u);
1683 Py_DECREF(v);
1684 Py_DECREF(w);
1685 break;
1687 case POP_BLOCK:
1689 PyTryBlock *b = PyFrame_BlockPop(f);
1690 while (STACK_LEVEL() > b->b_level) {
1691 v = POP();
1692 Py_DECREF(v);
1695 continue;
1697 PREDICTED(END_FINALLY);
1698 case END_FINALLY:
1699 v = POP();
1700 if (PyInt_Check(v)) {
1701 why = (enum why_code) PyInt_AS_LONG(v);
1702 assert(why != WHY_YIELD);
1703 if (why == WHY_RETURN ||
1704 why == WHY_CONTINUE)
1705 retval = POP();
1707 else if (PyExceptionClass_Check(v) ||
1708 PyString_Check(v)) {
1709 w = POP();
1710 u = POP();
1711 PyErr_Restore(v, w, u);
1712 why = WHY_RERAISE;
1713 break;
1715 else if (v != Py_None) {
1716 PyErr_SetString(PyExc_SystemError,
1717 "'finally' pops bad exception");
1718 why = WHY_EXCEPTION;
1720 Py_DECREF(v);
1721 break;
1723 case BUILD_CLASS:
1724 u = TOP();
1725 v = SECOND();
1726 w = THIRD();
1727 STACKADJ(-2);
1728 x = build_class(u, v, w);
1729 SET_TOP(x);
1730 Py_DECREF(u);
1731 Py_DECREF(v);
1732 Py_DECREF(w);
1733 break;
1735 case STORE_NAME:
1736 w = GETITEM(names, oparg);
1737 v = POP();
1738 if ((x = f->f_locals) != NULL) {
1739 if (PyDict_CheckExact(x))
1740 err = PyDict_SetItem(x, w, v);
1741 else
1742 err = PyObject_SetItem(x, w, v);
1743 Py_DECREF(v);
1744 if (err == 0) continue;
1745 break;
1747 PyErr_Format(PyExc_SystemError,
1748 "no locals found when storing %s",
1749 PyObject_REPR(w));
1750 break;
1752 case DELETE_NAME:
1753 w = GETITEM(names, oparg);
1754 if ((x = f->f_locals) != NULL) {
1755 if ((err = PyObject_DelItem(x, w)) != 0)
1756 format_exc_check_arg(PyExc_NameError,
1757 NAME_ERROR_MSG,
1759 break;
1761 PyErr_Format(PyExc_SystemError,
1762 "no locals when deleting %s",
1763 PyObject_REPR(w));
1764 break;
1766 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1767 case UNPACK_SEQUENCE:
1768 v = POP();
1769 if (PyTuple_CheckExact(v) &&
1770 PyTuple_GET_SIZE(v) == oparg) {
1771 PyObject **items = \
1772 ((PyTupleObject *)v)->ob_item;
1773 while (oparg--) {
1774 w = items[oparg];
1775 Py_INCREF(w);
1776 PUSH(w);
1778 Py_DECREF(v);
1779 continue;
1780 } else if (PyList_CheckExact(v) &&
1781 PyList_GET_SIZE(v) == oparg) {
1782 PyObject **items = \
1783 ((PyListObject *)v)->ob_item;
1784 while (oparg--) {
1785 w = items[oparg];
1786 Py_INCREF(w);
1787 PUSH(w);
1789 } else if (unpack_iterable(v, oparg,
1790 stack_pointer + oparg)) {
1791 stack_pointer += oparg;
1792 } else {
1793 /* unpack_iterable() raised an exception */
1794 why = WHY_EXCEPTION;
1796 Py_DECREF(v);
1797 break;
1799 case STORE_ATTR:
1800 w = GETITEM(names, oparg);
1801 v = TOP();
1802 u = SECOND();
1803 STACKADJ(-2);
1804 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1805 Py_DECREF(v);
1806 Py_DECREF(u);
1807 if (err == 0) continue;
1808 break;
1810 case DELETE_ATTR:
1811 w = GETITEM(names, oparg);
1812 v = POP();
1813 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1814 /* del v.w */
1815 Py_DECREF(v);
1816 break;
1818 case STORE_GLOBAL:
1819 w = GETITEM(names, oparg);
1820 v = POP();
1821 err = PyDict_SetItem(f->f_globals, w, v);
1822 Py_DECREF(v);
1823 if (err == 0) continue;
1824 break;
1826 case DELETE_GLOBAL:
1827 w = GETITEM(names, oparg);
1828 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1829 format_exc_check_arg(
1830 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1831 break;
1833 case LOAD_NAME:
1834 w = GETITEM(names, oparg);
1835 if ((v = f->f_locals) == NULL) {
1836 PyErr_Format(PyExc_SystemError,
1837 "no locals when loading %s",
1838 PyObject_REPR(w));
1839 break;
1841 if (PyDict_CheckExact(v)) {
1842 x = PyDict_GetItem(v, w);
1843 Py_XINCREF(x);
1845 else {
1846 x = PyObject_GetItem(v, w);
1847 if (x == NULL && PyErr_Occurred()) {
1848 if (!PyErr_ExceptionMatches(
1849 PyExc_KeyError))
1850 break;
1851 PyErr_Clear();
1854 if (x == NULL) {
1855 x = PyDict_GetItem(f->f_globals, w);
1856 if (x == NULL) {
1857 x = PyDict_GetItem(f->f_builtins, w);
1858 if (x == NULL) {
1859 format_exc_check_arg(
1860 PyExc_NameError,
1861 NAME_ERROR_MSG, w);
1862 break;
1865 Py_INCREF(x);
1867 PUSH(x);
1868 continue;
1870 case LOAD_GLOBAL:
1871 w = GETITEM(names, oparg);
1872 if (PyString_CheckExact(w)) {
1873 /* Inline the PyDict_GetItem() calls.
1874 WARNING: this is an extreme speed hack.
1875 Do not try this at home. */
1876 long hash = ((PyStringObject *)w)->ob_shash;
1877 if (hash != -1) {
1878 PyDictObject *d;
1879 PyDictEntry *e;
1880 d = (PyDictObject *)(f->f_globals);
1881 e = d->ma_lookup(d, w, hash);
1882 if (e == NULL) {
1883 x = NULL;
1884 break;
1886 x = e->me_value;
1887 if (x != NULL) {
1888 Py_INCREF(x);
1889 PUSH(x);
1890 continue;
1892 d = (PyDictObject *)(f->f_builtins);
1893 e = d->ma_lookup(d, w, hash);
1894 if (e == NULL) {
1895 x = NULL;
1896 break;
1898 x = e->me_value;
1899 if (x != NULL) {
1900 Py_INCREF(x);
1901 PUSH(x);
1902 continue;
1904 goto load_global_error;
1907 /* This is the un-inlined version of the code above */
1908 x = PyDict_GetItem(f->f_globals, w);
1909 if (x == NULL) {
1910 x = PyDict_GetItem(f->f_builtins, w);
1911 if (x == NULL) {
1912 load_global_error:
1913 format_exc_check_arg(
1914 PyExc_NameError,
1915 GLOBAL_NAME_ERROR_MSG, w);
1916 break;
1919 Py_INCREF(x);
1920 PUSH(x);
1921 continue;
1923 case DELETE_FAST:
1924 x = GETLOCAL(oparg);
1925 if (x != NULL) {
1926 SETLOCAL(oparg, NULL);
1927 continue;
1929 format_exc_check_arg(
1930 PyExc_UnboundLocalError,
1931 UNBOUNDLOCAL_ERROR_MSG,
1932 PyTuple_GetItem(co->co_varnames, oparg)
1934 break;
1936 case LOAD_CLOSURE:
1937 x = freevars[oparg];
1938 Py_INCREF(x);
1939 PUSH(x);
1940 if (x != NULL) continue;
1941 break;
1943 case LOAD_DEREF:
1944 x = freevars[oparg];
1945 w = PyCell_Get(x);
1946 if (w != NULL) {
1947 PUSH(w);
1948 continue;
1950 err = -1;
1951 /* Don't stomp existing exception */
1952 if (PyErr_Occurred())
1953 break;
1954 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1955 v = PyTuple_GET_ITEM(co->co_cellvars,
1956 oparg);
1957 format_exc_check_arg(
1958 PyExc_UnboundLocalError,
1959 UNBOUNDLOCAL_ERROR_MSG,
1961 } else {
1962 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
1963 PyTuple_GET_SIZE(co->co_cellvars));
1964 format_exc_check_arg(PyExc_NameError,
1965 UNBOUNDFREE_ERROR_MSG, v);
1967 break;
1969 case STORE_DEREF:
1970 w = POP();
1971 x = freevars[oparg];
1972 PyCell_Set(x, w);
1973 Py_DECREF(w);
1974 continue;
1976 case BUILD_TUPLE:
1977 x = PyTuple_New(oparg);
1978 if (x != NULL) {
1979 for (; --oparg >= 0;) {
1980 w = POP();
1981 PyTuple_SET_ITEM(x, oparg, w);
1983 PUSH(x);
1984 continue;
1986 break;
1988 case BUILD_LIST:
1989 x = PyList_New(oparg);
1990 if (x != NULL) {
1991 for (; --oparg >= 0;) {
1992 w = POP();
1993 PyList_SET_ITEM(x, oparg, w);
1995 PUSH(x);
1996 continue;
1998 break;
2000 case BUILD_MAP:
2001 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2002 PUSH(x);
2003 if (x != NULL) continue;
2004 break;
2006 case STORE_MAP:
2007 w = TOP(); /* key */
2008 u = SECOND(); /* value */
2009 v = THIRD(); /* dict */
2010 STACKADJ(-2);
2011 assert (PyDict_CheckExact(v));
2012 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2013 Py_DECREF(u);
2014 Py_DECREF(w);
2015 if (err == 0) continue;
2016 break;
2018 case LOAD_ATTR:
2019 w = GETITEM(names, oparg);
2020 v = TOP();
2021 x = PyObject_GetAttr(v, w);
2022 Py_DECREF(v);
2023 SET_TOP(x);
2024 if (x != NULL) continue;
2025 break;
2027 case COMPARE_OP:
2028 w = POP();
2029 v = TOP();
2030 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2031 /* INLINE: cmp(int, int) */
2032 register long a, b;
2033 register int res;
2034 a = PyInt_AS_LONG(v);
2035 b = PyInt_AS_LONG(w);
2036 switch (oparg) {
2037 case PyCmp_LT: res = a < b; break;
2038 case PyCmp_LE: res = a <= b; break;
2039 case PyCmp_EQ: res = a == b; break;
2040 case PyCmp_NE: res = a != b; break;
2041 case PyCmp_GT: res = a > b; break;
2042 case PyCmp_GE: res = a >= b; break;
2043 case PyCmp_IS: res = v == w; break;
2044 case PyCmp_IS_NOT: res = v != w; break;
2045 default: goto slow_compare;
2047 x = res ? Py_True : Py_False;
2048 Py_INCREF(x);
2050 else {
2051 slow_compare:
2052 x = cmp_outcome(oparg, v, w);
2054 Py_DECREF(v);
2055 Py_DECREF(w);
2056 SET_TOP(x);
2057 if (x == NULL) break;
2058 PREDICT(JUMP_IF_FALSE);
2059 PREDICT(JUMP_IF_TRUE);
2060 continue;
2062 case IMPORT_NAME:
2063 w = GETITEM(names, oparg);
2064 x = PyDict_GetItemString(f->f_builtins, "__import__");
2065 if (x == NULL) {
2066 PyErr_SetString(PyExc_ImportError,
2067 "__import__ not found");
2068 break;
2070 Py_INCREF(x);
2071 v = POP();
2072 u = TOP();
2073 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2074 w = PyTuple_Pack(5,
2076 f->f_globals,
2077 f->f_locals == NULL ?
2078 Py_None : f->f_locals,
2081 else
2082 w = PyTuple_Pack(4,
2084 f->f_globals,
2085 f->f_locals == NULL ?
2086 Py_None : f->f_locals,
2088 Py_DECREF(v);
2089 Py_DECREF(u);
2090 if (w == NULL) {
2091 u = POP();
2092 Py_DECREF(x);
2093 x = NULL;
2094 break;
2096 READ_TIMESTAMP(intr0);
2097 v = x;
2098 x = PyEval_CallObject(v, w);
2099 Py_DECREF(v);
2100 READ_TIMESTAMP(intr1);
2101 Py_DECREF(w);
2102 SET_TOP(x);
2103 if (x != NULL) continue;
2104 break;
2106 case IMPORT_STAR:
2107 v = POP();
2108 PyFrame_FastToLocals(f);
2109 if ((x = f->f_locals) == NULL) {
2110 PyErr_SetString(PyExc_SystemError,
2111 "no locals found during 'import *'");
2112 break;
2114 READ_TIMESTAMP(intr0);
2115 err = import_all_from(x, v);
2116 READ_TIMESTAMP(intr1);
2117 PyFrame_LocalsToFast(f, 0);
2118 Py_DECREF(v);
2119 if (err == 0) continue;
2120 break;
2122 case IMPORT_FROM:
2123 w = GETITEM(names, oparg);
2124 v = TOP();
2125 READ_TIMESTAMP(intr0);
2126 x = import_from(v, w);
2127 READ_TIMESTAMP(intr1);
2128 PUSH(x);
2129 if (x != NULL) continue;
2130 break;
2132 case JUMP_FORWARD:
2133 JUMPBY(oparg);
2134 goto fast_next_opcode;
2136 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
2137 case JUMP_IF_FALSE:
2138 w = TOP();
2139 if (w == Py_True) {
2140 PREDICT(POP_TOP);
2141 goto fast_next_opcode;
2143 if (w == Py_False) {
2144 JUMPBY(oparg);
2145 goto fast_next_opcode;
2147 err = PyObject_IsTrue(w);
2148 if (err > 0)
2149 err = 0;
2150 else if (err == 0)
2151 JUMPBY(oparg);
2152 else
2153 break;
2154 continue;
2156 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
2157 case JUMP_IF_TRUE:
2158 w = TOP();
2159 if (w == Py_False) {
2160 PREDICT(POP_TOP);
2161 goto fast_next_opcode;
2163 if (w == Py_True) {
2164 JUMPBY(oparg);
2165 goto fast_next_opcode;
2167 err = PyObject_IsTrue(w);
2168 if (err > 0) {
2169 err = 0;
2170 JUMPBY(oparg);
2172 else if (err == 0)
2174 else
2175 break;
2176 continue;
2178 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2179 case JUMP_ABSOLUTE:
2180 JUMPTO(oparg);
2181 #if FAST_LOOPS
2182 /* Enabling this path speeds-up all while and for-loops by bypassing
2183 the per-loop checks for signals. By default, this should be turned-off
2184 because it prevents detection of a control-break in tight loops like
2185 "while 1: pass". Compile with this option turned-on when you need
2186 the speed-up and do not need break checking inside tight loops (ones
2187 that contain only instructions ending with goto fast_next_opcode).
2189 goto fast_next_opcode;
2190 #else
2191 continue;
2192 #endif
2194 case GET_ITER:
2195 /* before: [obj]; after [getiter(obj)] */
2196 v = TOP();
2197 x = PyObject_GetIter(v);
2198 Py_DECREF(v);
2199 if (x != NULL) {
2200 SET_TOP(x);
2201 PREDICT(FOR_ITER);
2202 continue;
2204 STACKADJ(-1);
2205 break;
2207 PREDICTED_WITH_ARG(FOR_ITER);
2208 case FOR_ITER:
2209 /* before: [iter]; after: [iter, iter()] *or* [] */
2210 v = TOP();
2211 x = (*v->ob_type->tp_iternext)(v);
2212 if (x != NULL) {
2213 PUSH(x);
2214 PREDICT(STORE_FAST);
2215 PREDICT(UNPACK_SEQUENCE);
2216 continue;
2218 if (PyErr_Occurred()) {
2219 if (!PyErr_ExceptionMatches(
2220 PyExc_StopIteration))
2221 break;
2222 PyErr_Clear();
2224 /* iterator ended normally */
2225 x = v = POP();
2226 Py_DECREF(v);
2227 JUMPBY(oparg);
2228 continue;
2230 case BREAK_LOOP:
2231 why = WHY_BREAK;
2232 goto fast_block_end;
2234 case CONTINUE_LOOP:
2235 retval = PyInt_FromLong(oparg);
2236 if (!retval) {
2237 x = NULL;
2238 break;
2240 why = WHY_CONTINUE;
2241 goto fast_block_end;
2243 case SETUP_LOOP:
2244 case SETUP_EXCEPT:
2245 case SETUP_FINALLY:
2246 /* NOTE: If you add any new block-setup opcodes that
2247 are not try/except/finally handlers, you may need
2248 to update the PyGen_NeedsFinalizing() function.
2251 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2252 STACK_LEVEL());
2253 continue;
2255 case WITH_CLEANUP:
2257 /* At the top of the stack are 1-3 values indicating
2258 how/why we entered the finally clause:
2259 - TOP = None
2260 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2261 - TOP = WHY_*; no retval below it
2262 - (TOP, SECOND, THIRD) = exc_info()
2263 Below them is EXIT, the context.__exit__ bound method.
2264 In the last case, we must call
2265 EXIT(TOP, SECOND, THIRD)
2266 otherwise we must call
2267 EXIT(None, None, None)
2269 In all cases, we remove EXIT from the stack, leaving
2270 the rest in the same order.
2272 In addition, if the stack represents an exception,
2273 *and* the function call returns a 'true' value, we
2274 "zap" this information, to prevent END_FINALLY from
2275 re-raising the exception. (But non-local gotos
2276 should still be resumed.)
2279 PyObject *exit_func;
2281 u = POP();
2282 if (u == Py_None) {
2283 exit_func = TOP();
2284 SET_TOP(u);
2285 v = w = Py_None;
2287 else if (PyInt_Check(u)) {
2288 switch(PyInt_AS_LONG(u)) {
2289 case WHY_RETURN:
2290 case WHY_CONTINUE:
2291 /* Retval in TOP. */
2292 exit_func = SECOND();
2293 SET_SECOND(TOP());
2294 SET_TOP(u);
2295 break;
2296 default:
2297 exit_func = TOP();
2298 SET_TOP(u);
2299 break;
2301 u = v = w = Py_None;
2303 else {
2304 v = TOP();
2305 w = SECOND();
2306 exit_func = THIRD();
2307 SET_TOP(u);
2308 SET_SECOND(v);
2309 SET_THIRD(w);
2311 /* XXX Not the fastest way to call it... */
2312 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2313 NULL);
2314 if (x == NULL) {
2315 Py_DECREF(exit_func);
2316 break; /* Go to error exit */
2318 if (u != Py_None && PyObject_IsTrue(x)) {
2319 /* There was an exception and a true return */
2320 STACKADJ(-2);
2321 Py_INCREF(Py_None);
2322 SET_TOP(Py_None);
2323 Py_DECREF(u);
2324 Py_DECREF(v);
2325 Py_DECREF(w);
2326 } else {
2327 /* The stack was rearranged to remove EXIT
2328 above. Let END_FINALLY do its thing */
2330 Py_DECREF(x);
2331 Py_DECREF(exit_func);
2332 PREDICT(END_FINALLY);
2333 break;
2336 case CALL_FUNCTION:
2338 PyObject **sp;
2339 PCALL(PCALL_ALL);
2340 sp = stack_pointer;
2341 #ifdef WITH_TSC
2342 x = call_function(&sp, oparg, &intr0, &intr1);
2343 #else
2344 x = call_function(&sp, oparg);
2345 #endif
2346 stack_pointer = sp;
2347 PUSH(x);
2348 if (x != NULL)
2349 continue;
2350 break;
2353 case CALL_FUNCTION_VAR:
2354 case CALL_FUNCTION_KW:
2355 case CALL_FUNCTION_VAR_KW:
2357 int na = oparg & 0xff;
2358 int nk = (oparg>>8) & 0xff;
2359 int flags = (opcode - CALL_FUNCTION) & 3;
2360 int n = na + 2 * nk;
2361 PyObject **pfunc, *func, **sp;
2362 PCALL(PCALL_ALL);
2363 if (flags & CALL_FLAG_VAR)
2364 n++;
2365 if (flags & CALL_FLAG_KW)
2366 n++;
2367 pfunc = stack_pointer - n - 1;
2368 func = *pfunc;
2370 if (PyMethod_Check(func)
2371 && PyMethod_GET_SELF(func) != NULL) {
2372 PyObject *self = PyMethod_GET_SELF(func);
2373 Py_INCREF(self);
2374 func = PyMethod_GET_FUNCTION(func);
2375 Py_INCREF(func);
2376 Py_DECREF(*pfunc);
2377 *pfunc = self;
2378 na++;
2379 n++;
2380 } else
2381 Py_INCREF(func);
2382 sp = stack_pointer;
2383 READ_TIMESTAMP(intr0);
2384 x = ext_do_call(func, &sp, flags, na, nk);
2385 READ_TIMESTAMP(intr1);
2386 stack_pointer = sp;
2387 Py_DECREF(func);
2389 while (stack_pointer > pfunc) {
2390 w = POP();
2391 Py_DECREF(w);
2393 PUSH(x);
2394 if (x != NULL)
2395 continue;
2396 break;
2399 case MAKE_FUNCTION:
2400 v = POP(); /* code object */
2401 x = PyFunction_New(v, f->f_globals);
2402 Py_DECREF(v);
2403 /* XXX Maybe this should be a separate opcode? */
2404 if (x != NULL && oparg > 0) {
2405 v = PyTuple_New(oparg);
2406 if (v == NULL) {
2407 Py_DECREF(x);
2408 x = NULL;
2409 break;
2411 while (--oparg >= 0) {
2412 w = POP();
2413 PyTuple_SET_ITEM(v, oparg, w);
2415 err = PyFunction_SetDefaults(x, v);
2416 Py_DECREF(v);
2418 PUSH(x);
2419 break;
2421 case MAKE_CLOSURE:
2423 v = POP(); /* code object */
2424 x = PyFunction_New(v, f->f_globals);
2425 Py_DECREF(v);
2426 if (x != NULL) {
2427 v = POP();
2428 err = PyFunction_SetClosure(x, v);
2429 Py_DECREF(v);
2431 if (x != NULL && oparg > 0) {
2432 v = PyTuple_New(oparg);
2433 if (v == NULL) {
2434 Py_DECREF(x);
2435 x = NULL;
2436 break;
2438 while (--oparg >= 0) {
2439 w = POP();
2440 PyTuple_SET_ITEM(v, oparg, w);
2442 err = PyFunction_SetDefaults(x, v);
2443 Py_DECREF(v);
2445 PUSH(x);
2446 break;
2449 case BUILD_SLICE:
2450 if (oparg == 3)
2451 w = POP();
2452 else
2453 w = NULL;
2454 v = POP();
2455 u = TOP();
2456 x = PySlice_New(u, v, w);
2457 Py_DECREF(u);
2458 Py_DECREF(v);
2459 Py_XDECREF(w);
2460 SET_TOP(x);
2461 if (x != NULL) continue;
2462 break;
2464 case EXTENDED_ARG:
2465 opcode = NEXTOP();
2466 oparg = oparg<<16 | NEXTARG();
2467 goto dispatch_opcode;
2469 default:
2470 fprintf(stderr,
2471 "XXX lineno: %d, opcode: %d\n",
2472 PyCode_Addr2Line(f->f_code, f->f_lasti),
2473 opcode);
2474 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2475 why = WHY_EXCEPTION;
2476 break;
2478 #ifdef CASE_TOO_BIG
2480 #endif
2482 } /* switch */
2484 on_error:
2486 READ_TIMESTAMP(inst1);
2488 /* Quickly continue if no error occurred */
2490 if (why == WHY_NOT) {
2491 if (err == 0 && x != NULL) {
2492 #ifdef CHECKEXC
2493 /* This check is expensive! */
2494 if (PyErr_Occurred())
2495 fprintf(stderr,
2496 "XXX undetected error\n");
2497 else {
2498 #endif
2499 READ_TIMESTAMP(loop1);
2500 continue; /* Normal, fast path */
2501 #ifdef CHECKEXC
2503 #endif
2505 why = WHY_EXCEPTION;
2506 x = Py_None;
2507 err = 0;
2510 /* Double-check exception status */
2512 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2513 if (!PyErr_Occurred()) {
2514 PyErr_SetString(PyExc_SystemError,
2515 "error return without exception set");
2516 why = WHY_EXCEPTION;
2519 #ifdef CHECKEXC
2520 else {
2521 /* This check is expensive! */
2522 if (PyErr_Occurred()) {
2523 char buf[128];
2524 sprintf(buf, "Stack unwind with exception "
2525 "set and why=%d", why);
2526 Py_FatalError(buf);
2529 #endif
2531 /* Log traceback info if this is a real exception */
2533 if (why == WHY_EXCEPTION) {
2534 PyTraceBack_Here(f);
2536 if (tstate->c_tracefunc != NULL)
2537 call_exc_trace(tstate->c_tracefunc,
2538 tstate->c_traceobj, f);
2541 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2543 if (why == WHY_RERAISE)
2544 why = WHY_EXCEPTION;
2546 /* Unwind stacks if a (pseudo) exception occurred */
2548 fast_block_end:
2549 while (why != WHY_NOT && f->f_iblock > 0) {
2550 PyTryBlock *b = PyFrame_BlockPop(f);
2552 assert(why != WHY_YIELD);
2553 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2554 /* For a continue inside a try block,
2555 don't pop the block for the loop. */
2556 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2557 b->b_level);
2558 why = WHY_NOT;
2559 JUMPTO(PyInt_AS_LONG(retval));
2560 Py_DECREF(retval);
2561 break;
2564 while (STACK_LEVEL() > b->b_level) {
2565 v = POP();
2566 Py_XDECREF(v);
2568 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2569 why = WHY_NOT;
2570 JUMPTO(b->b_handler);
2571 break;
2573 if (b->b_type == SETUP_FINALLY ||
2574 (b->b_type == SETUP_EXCEPT &&
2575 why == WHY_EXCEPTION)) {
2576 if (why == WHY_EXCEPTION) {
2577 PyObject *exc, *val, *tb;
2578 PyErr_Fetch(&exc, &val, &tb);
2579 if (val == NULL) {
2580 val = Py_None;
2581 Py_INCREF(val);
2583 /* Make the raw exception data
2584 available to the handler,
2585 so a program can emulate the
2586 Python main loop. Don't do
2587 this for 'finally'. */
2588 if (b->b_type == SETUP_EXCEPT) {
2589 PyErr_NormalizeException(
2590 &exc, &val, &tb);
2591 set_exc_info(tstate,
2592 exc, val, tb);
2594 if (tb == NULL) {
2595 Py_INCREF(Py_None);
2596 PUSH(Py_None);
2597 } else
2598 PUSH(tb);
2599 PUSH(val);
2600 PUSH(exc);
2602 else {
2603 if (why & (WHY_RETURN | WHY_CONTINUE))
2604 PUSH(retval);
2605 v = PyInt_FromLong((long)why);
2606 PUSH(v);
2608 why = WHY_NOT;
2609 JUMPTO(b->b_handler);
2610 break;
2612 } /* unwind stack */
2614 /* End the loop if we still have an error (or return) */
2616 if (why != WHY_NOT)
2617 break;
2618 READ_TIMESTAMP(loop1);
2620 } /* main loop */
2622 assert(why != WHY_YIELD);
2623 /* Pop remaining stack entries. */
2624 while (!EMPTY()) {
2625 v = POP();
2626 Py_XDECREF(v);
2629 if (why != WHY_RETURN)
2630 retval = NULL;
2632 fast_yield:
2633 if (tstate->use_tracing) {
2634 if (tstate->c_tracefunc) {
2635 if (why == WHY_RETURN || why == WHY_YIELD) {
2636 if (call_trace(tstate->c_tracefunc,
2637 tstate->c_traceobj, f,
2638 PyTrace_RETURN, retval)) {
2639 Py_XDECREF(retval);
2640 retval = NULL;
2641 why = WHY_EXCEPTION;
2644 else if (why == WHY_EXCEPTION) {
2645 call_trace_protected(tstate->c_tracefunc,
2646 tstate->c_traceobj, f,
2647 PyTrace_RETURN, NULL);
2650 if (tstate->c_profilefunc) {
2651 if (why == WHY_EXCEPTION)
2652 call_trace_protected(tstate->c_profilefunc,
2653 tstate->c_profileobj, f,
2654 PyTrace_RETURN, NULL);
2655 else if (call_trace(tstate->c_profilefunc,
2656 tstate->c_profileobj, f,
2657 PyTrace_RETURN, retval)) {
2658 Py_XDECREF(retval);
2659 retval = NULL;
2660 why = WHY_EXCEPTION;
2665 if (tstate->frame->f_exc_type != NULL)
2666 reset_exc_info(tstate);
2667 else {
2668 assert(tstate->frame->f_exc_value == NULL);
2669 assert(tstate->frame->f_exc_traceback == NULL);
2672 /* pop frame */
2673 exit_eval_frame:
2674 Py_LeaveRecursiveCall();
2675 tstate->frame = f->f_back;
2677 return retval;
2680 /* This is gonna seem *real weird*, but if you put some other code between
2681 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2682 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2684 PyObject *
2685 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
2686 PyObject **args, int argcount, PyObject **kws, int kwcount,
2687 PyObject **defs, int defcount, PyObject *closure)
2689 register PyFrameObject *f;
2690 register PyObject *retval = NULL;
2691 register PyObject **fastlocals, **freevars;
2692 PyThreadState *tstate = PyThreadState_GET();
2693 PyObject *x, *u;
2695 if (globals == NULL) {
2696 PyErr_SetString(PyExc_SystemError,
2697 "PyEval_EvalCodeEx: NULL globals");
2698 return NULL;
2701 assert(tstate != NULL);
2702 assert(globals != NULL);
2703 f = PyFrame_New(tstate, co, globals, locals);
2704 if (f == NULL)
2705 return NULL;
2707 fastlocals = f->f_localsplus;
2708 freevars = f->f_localsplus + co->co_nlocals;
2710 if (co->co_argcount > 0 ||
2711 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2712 int i;
2713 int n = argcount;
2714 PyObject *kwdict = NULL;
2715 if (co->co_flags & CO_VARKEYWORDS) {
2716 kwdict = PyDict_New();
2717 if (kwdict == NULL)
2718 goto fail;
2719 i = co->co_argcount;
2720 if (co->co_flags & CO_VARARGS)
2721 i++;
2722 SETLOCAL(i, kwdict);
2724 if (argcount > co->co_argcount) {
2725 if (!(co->co_flags & CO_VARARGS)) {
2726 PyErr_Format(PyExc_TypeError,
2727 "%.200s() takes %s %d "
2728 "%sargument%s (%d given)",
2729 PyString_AsString(co->co_name),
2730 defcount ? "at most" : "exactly",
2731 co->co_argcount,
2732 kwcount ? "non-keyword " : "",
2733 co->co_argcount == 1 ? "" : "s",
2734 argcount);
2735 goto fail;
2737 n = co->co_argcount;
2739 for (i = 0; i < n; i++) {
2740 x = args[i];
2741 Py_INCREF(x);
2742 SETLOCAL(i, x);
2744 if (co->co_flags & CO_VARARGS) {
2745 u = PyTuple_New(argcount - n);
2746 if (u == NULL)
2747 goto fail;
2748 SETLOCAL(co->co_argcount, u);
2749 for (i = n; i < argcount; i++) {
2750 x = args[i];
2751 Py_INCREF(x);
2752 PyTuple_SET_ITEM(u, i-n, x);
2755 for (i = 0; i < kwcount; i++) {
2756 PyObject *keyword = kws[2*i];
2757 PyObject *value = kws[2*i + 1];
2758 int j;
2759 if (keyword == NULL || !PyString_Check(keyword)) {
2760 PyErr_Format(PyExc_TypeError,
2761 "%.200s() keywords must be strings",
2762 PyString_AsString(co->co_name));
2763 goto fail;
2765 /* XXX slow -- speed up using dictionary? */
2766 for (j = 0; j < co->co_argcount; j++) {
2767 PyObject *nm = PyTuple_GET_ITEM(
2768 co->co_varnames, j);
2769 int cmp = PyObject_RichCompareBool(
2770 keyword, nm, Py_EQ);
2771 if (cmp > 0)
2772 break;
2773 else if (cmp < 0)
2774 goto fail;
2776 /* Check errors from Compare */
2777 if (PyErr_Occurred())
2778 goto fail;
2779 if (j >= co->co_argcount) {
2780 if (kwdict == NULL) {
2781 PyErr_Format(PyExc_TypeError,
2782 "%.200s() got an unexpected "
2783 "keyword argument '%.400s'",
2784 PyString_AsString(co->co_name),
2785 PyString_AsString(keyword));
2786 goto fail;
2788 PyDict_SetItem(kwdict, keyword, value);
2790 else {
2791 if (GETLOCAL(j) != NULL) {
2792 PyErr_Format(PyExc_TypeError,
2793 "%.200s() got multiple "
2794 "values for keyword "
2795 "argument '%.400s'",
2796 PyString_AsString(co->co_name),
2797 PyString_AsString(keyword));
2798 goto fail;
2800 Py_INCREF(value);
2801 SETLOCAL(j, value);
2804 if (argcount < co->co_argcount) {
2805 int m = co->co_argcount - defcount;
2806 for (i = argcount; i < m; i++) {
2807 if (GETLOCAL(i) == NULL) {
2808 PyErr_Format(PyExc_TypeError,
2809 "%.200s() takes %s %d "
2810 "%sargument%s (%d given)",
2811 PyString_AsString(co->co_name),
2812 ((co->co_flags & CO_VARARGS) ||
2813 defcount) ? "at least"
2814 : "exactly",
2815 m, kwcount ? "non-keyword " : "",
2816 m == 1 ? "" : "s", i);
2817 goto fail;
2820 if (n > m)
2821 i = n - m;
2822 else
2823 i = 0;
2824 for (; i < defcount; i++) {
2825 if (GETLOCAL(m+i) == NULL) {
2826 PyObject *def = defs[i];
2827 Py_INCREF(def);
2828 SETLOCAL(m+i, def);
2833 else {
2834 if (argcount > 0 || kwcount > 0) {
2835 PyErr_Format(PyExc_TypeError,
2836 "%.200s() takes no arguments (%d given)",
2837 PyString_AsString(co->co_name),
2838 argcount + kwcount);
2839 goto fail;
2842 /* Allocate and initialize storage for cell vars, and copy free
2843 vars into frame. This isn't too efficient right now. */
2844 if (PyTuple_GET_SIZE(co->co_cellvars)) {
2845 int i, j, nargs, found;
2846 char *cellname, *argname;
2847 PyObject *c;
2849 nargs = co->co_argcount;
2850 if (co->co_flags & CO_VARARGS)
2851 nargs++;
2852 if (co->co_flags & CO_VARKEYWORDS)
2853 nargs++;
2855 /* Initialize each cell var, taking into account
2856 cell vars that are initialized from arguments.
2858 Should arrange for the compiler to put cellvars
2859 that are arguments at the beginning of the cellvars
2860 list so that we can march over it more efficiently?
2862 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
2863 cellname = PyString_AS_STRING(
2864 PyTuple_GET_ITEM(co->co_cellvars, i));
2865 found = 0;
2866 for (j = 0; j < nargs; j++) {
2867 argname = PyString_AS_STRING(
2868 PyTuple_GET_ITEM(co->co_varnames, j));
2869 if (strcmp(cellname, argname) == 0) {
2870 c = PyCell_New(GETLOCAL(j));
2871 if (c == NULL)
2872 goto fail;
2873 GETLOCAL(co->co_nlocals + i) = c;
2874 found = 1;
2875 break;
2878 if (found == 0) {
2879 c = PyCell_New(NULL);
2880 if (c == NULL)
2881 goto fail;
2882 SETLOCAL(co->co_nlocals + i, c);
2886 if (PyTuple_GET_SIZE(co->co_freevars)) {
2887 int i;
2888 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
2889 PyObject *o = PyTuple_GET_ITEM(closure, i);
2890 Py_INCREF(o);
2891 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
2895 if (co->co_flags & CO_GENERATOR) {
2896 /* Don't need to keep the reference to f_back, it will be set
2897 * when the generator is resumed. */
2898 Py_XDECREF(f->f_back);
2899 f->f_back = NULL;
2901 PCALL(PCALL_GENERATOR);
2903 /* Create a new generator that owns the ready to run frame
2904 * and return that as the value. */
2905 return PyGen_New(f);
2908 retval = PyEval_EvalFrameEx(f,0);
2910 fail: /* Jump here from prelude on failure */
2912 /* decref'ing the frame can cause __del__ methods to get invoked,
2913 which can call back into Python. While we're done with the
2914 current Python frame (f), the associated C stack is still in use,
2915 so recursion_depth must be boosted for the duration.
2917 assert(tstate != NULL);
2918 ++tstate->recursion_depth;
2919 Py_DECREF(f);
2920 --tstate->recursion_depth;
2921 return retval;
2925 /* Implementation notes for set_exc_info() and reset_exc_info():
2927 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2928 'exc_traceback'. These always travel together.
2930 - tstate->curexc_ZZZ is the "hot" exception that is set by
2931 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2933 - Once an exception is caught by an except clause, it is transferred
2934 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2935 can pick it up. This is the primary task of set_exc_info().
2936 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
2938 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
2940 Long ago, when none of this existed, there were just a few globals:
2941 one set corresponding to the "hot" exception, and one set
2942 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2943 globals; they were simply stored as sys.exc_ZZZ. For backwards
2944 compatibility, they still are!) The problem was that in code like
2945 this:
2947 try:
2948 "something that may fail"
2949 except "some exception":
2950 "do something else first"
2951 "print the exception from sys.exc_ZZZ."
2953 if "do something else first" invoked something that raised and caught
2954 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2955 cause of subtle bugs. I fixed this by changing the semantics as
2956 follows:
2958 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2959 *in that frame*.
2961 - But initially, and as long as no exception is caught in a given
2962 frame, sys.exc_ZZZ will hold the last exception caught in the
2963 previous frame (or the frame before that, etc.).
2965 The first bullet fixed the bug in the above example. The second
2966 bullet was for backwards compatibility: it was (and is) common to
2967 have a function that is called when an exception is caught, and to
2968 have that function access the caught exception via sys.exc_ZZZ.
2969 (Example: traceback.print_exc()).
2971 At the same time I fixed the problem that sys.exc_ZZZ weren't
2972 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2973 but that's really a separate improvement.
2975 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2976 variables to what they were before the current frame was called. The
2977 set_exc_info() function saves them on the frame so that
2978 reset_exc_info() can restore them. The invariant is that
2979 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2980 exception (where "catching" an exception applies only to successful
2981 except clauses); and if the current frame ever caught an exception,
2982 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2983 at the start of the current frame.
2987 static void
2988 set_exc_info(PyThreadState *tstate,
2989 PyObject *type, PyObject *value, PyObject *tb)
2991 PyFrameObject *frame = tstate->frame;
2992 PyObject *tmp_type, *tmp_value, *tmp_tb;
2994 assert(type != NULL);
2995 assert(frame != NULL);
2996 if (frame->f_exc_type == NULL) {
2997 assert(frame->f_exc_value == NULL);
2998 assert(frame->f_exc_traceback == NULL);
2999 /* This frame didn't catch an exception before. */
3000 /* Save previous exception of this thread in this frame. */
3001 if (tstate->exc_type == NULL) {
3002 /* XXX Why is this set to Py_None? */
3003 Py_INCREF(Py_None);
3004 tstate->exc_type = Py_None;
3006 Py_INCREF(tstate->exc_type);
3007 Py_XINCREF(tstate->exc_value);
3008 Py_XINCREF(tstate->exc_traceback);
3009 frame->f_exc_type = tstate->exc_type;
3010 frame->f_exc_value = tstate->exc_value;
3011 frame->f_exc_traceback = tstate->exc_traceback;
3013 /* Set new exception for this thread. */
3014 tmp_type = tstate->exc_type;
3015 tmp_value = tstate->exc_value;
3016 tmp_tb = tstate->exc_traceback;
3017 Py_INCREF(type);
3018 Py_XINCREF(value);
3019 Py_XINCREF(tb);
3020 tstate->exc_type = type;
3021 tstate->exc_value = value;
3022 tstate->exc_traceback = tb;
3023 Py_XDECREF(tmp_type);
3024 Py_XDECREF(tmp_value);
3025 Py_XDECREF(tmp_tb);
3026 /* For b/w compatibility */
3027 PySys_SetObject("exc_type", type);
3028 PySys_SetObject("exc_value", value);
3029 PySys_SetObject("exc_traceback", tb);
3032 static void
3033 reset_exc_info(PyThreadState *tstate)
3035 PyFrameObject *frame;
3036 PyObject *tmp_type, *tmp_value, *tmp_tb;
3038 /* It's a precondition that the thread state's frame caught an
3039 * exception -- verify in a debug build.
3041 assert(tstate != NULL);
3042 frame = tstate->frame;
3043 assert(frame != NULL);
3044 assert(frame->f_exc_type != NULL);
3046 /* Copy the frame's exception info back to the thread state. */
3047 tmp_type = tstate->exc_type;
3048 tmp_value = tstate->exc_value;
3049 tmp_tb = tstate->exc_traceback;
3050 Py_INCREF(frame->f_exc_type);
3051 Py_XINCREF(frame->f_exc_value);
3052 Py_XINCREF(frame->f_exc_traceback);
3053 tstate->exc_type = frame->f_exc_type;
3054 tstate->exc_value = frame->f_exc_value;
3055 tstate->exc_traceback = frame->f_exc_traceback;
3056 Py_XDECREF(tmp_type);
3057 Py_XDECREF(tmp_value);
3058 Py_XDECREF(tmp_tb);
3060 /* For b/w compatibility */
3061 PySys_SetObject("exc_type", frame->f_exc_type);
3062 PySys_SetObject("exc_value", frame->f_exc_value);
3063 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3065 /* Clear the frame's exception info. */
3066 tmp_type = frame->f_exc_type;
3067 tmp_value = frame->f_exc_value;
3068 tmp_tb = frame->f_exc_traceback;
3069 frame->f_exc_type = NULL;
3070 frame->f_exc_value = NULL;
3071 frame->f_exc_traceback = NULL;
3072 Py_DECREF(tmp_type);
3073 Py_XDECREF(tmp_value);
3074 Py_XDECREF(tmp_tb);
3077 /* Logic for the raise statement (too complicated for inlining).
3078 This *consumes* a reference count to each of its arguments. */
3079 static enum why_code
3080 do_raise(PyObject *type, PyObject *value, PyObject *tb)
3082 if (type == NULL) {
3083 /* Reraise */
3084 PyThreadState *tstate = PyThreadState_GET();
3085 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3086 value = tstate->exc_value;
3087 tb = tstate->exc_traceback;
3088 Py_XINCREF(type);
3089 Py_XINCREF(value);
3090 Py_XINCREF(tb);
3093 /* We support the following forms of raise:
3094 raise <class>, <classinstance>
3095 raise <class>, <argument tuple>
3096 raise <class>, None
3097 raise <class>, <argument>
3098 raise <classinstance>, None
3099 raise <string>, <object>
3100 raise <string>, None
3102 An omitted second argument is the same as None.
3104 In addition, raise <tuple>, <anything> is the same as
3105 raising the tuple's first item (and it better have one!);
3106 this rule is applied recursively.
3108 Finally, an optional third argument can be supplied, which
3109 gives the traceback to be substituted (useful when
3110 re-raising an exception after examining it). */
3112 /* First, check the traceback argument, replacing None with
3113 NULL. */
3114 if (tb == Py_None) {
3115 Py_DECREF(tb);
3116 tb = NULL;
3118 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3119 PyErr_SetString(PyExc_TypeError,
3120 "raise: arg 3 must be a traceback or None");
3121 goto raise_error;
3124 /* Next, replace a missing value with None */
3125 if (value == NULL) {
3126 value = Py_None;
3127 Py_INCREF(value);
3130 /* Next, repeatedly, replace a tuple exception with its first item */
3131 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3132 PyObject *tmp = type;
3133 type = PyTuple_GET_ITEM(type, 0);
3134 Py_INCREF(type);
3135 Py_DECREF(tmp);
3138 if (PyExceptionClass_Check(type))
3139 PyErr_NormalizeException(&type, &value, &tb);
3141 else if (PyExceptionInstance_Check(type)) {
3142 /* Raising an instance. The value should be a dummy. */
3143 if (value != Py_None) {
3144 PyErr_SetString(PyExc_TypeError,
3145 "instance exception may not have a separate value");
3146 goto raise_error;
3148 else {
3149 /* Normalize to raise <class>, <instance> */
3150 Py_DECREF(value);
3151 value = type;
3152 type = PyExceptionInstance_Class(type);
3153 Py_INCREF(type);
3156 else {
3157 /* Not something you can raise. You get an exception
3158 anyway, just not what you specified :-) */
3159 PyErr_Format(PyExc_TypeError,
3160 "exceptions must be classes or instances, not %s",
3161 type->ob_type->tp_name);
3162 goto raise_error;
3165 assert(PyExceptionClass_Check(type));
3166 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3167 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3168 "exceptions must derive from BaseException "
3169 "in 3.x", 1) < 0)
3170 goto raise_error;
3173 PyErr_Restore(type, value, tb);
3174 if (tb == NULL)
3175 return WHY_EXCEPTION;
3176 else
3177 return WHY_RERAISE;
3178 raise_error:
3179 Py_XDECREF(value);
3180 Py_XDECREF(type);
3181 Py_XDECREF(tb);
3182 return WHY_EXCEPTION;
3185 /* Iterate v argcnt times and store the results on the stack (via decreasing
3186 sp). Return 1 for success, 0 if error. */
3188 static int
3189 unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
3191 int i = 0;
3192 PyObject *it; /* iter(v) */
3193 PyObject *w;
3195 assert(v != NULL);
3197 it = PyObject_GetIter(v);
3198 if (it == NULL)
3199 goto Error;
3201 for (; i < argcnt; i++) {
3202 w = PyIter_Next(it);
3203 if (w == NULL) {
3204 /* Iterator done, via error or exhaustion. */
3205 if (!PyErr_Occurred()) {
3206 PyErr_Format(PyExc_ValueError,
3207 "need more than %d value%s to unpack",
3208 i, i == 1 ? "" : "s");
3210 goto Error;
3212 *--sp = w;
3215 /* We better have exhausted the iterator now. */
3216 w = PyIter_Next(it);
3217 if (w == NULL) {
3218 if (PyErr_Occurred())
3219 goto Error;
3220 Py_DECREF(it);
3221 return 1;
3223 Py_DECREF(w);
3224 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3225 /* fall through */
3226 Error:
3227 for (; i > 0; i--, sp++)
3228 Py_DECREF(*sp);
3229 Py_XDECREF(it);
3230 return 0;
3234 #ifdef LLTRACE
3235 static int
3236 prtrace(PyObject *v, char *str)
3238 printf("%s ", str);
3239 if (PyObject_Print(v, stdout, 0) != 0)
3240 PyErr_Clear(); /* Don't know what else to do */
3241 printf("\n");
3242 return 1;
3244 #endif
3246 static void
3247 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
3249 PyObject *type, *value, *traceback, *arg;
3250 int err;
3251 PyErr_Fetch(&type, &value, &traceback);
3252 if (value == NULL) {
3253 value = Py_None;
3254 Py_INCREF(value);
3256 arg = PyTuple_Pack(3, type, value, traceback);
3257 if (arg == NULL) {
3258 PyErr_Restore(type, value, traceback);
3259 return;
3261 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3262 Py_DECREF(arg);
3263 if (err == 0)
3264 PyErr_Restore(type, value, traceback);
3265 else {
3266 Py_XDECREF(type);
3267 Py_XDECREF(value);
3268 Py_XDECREF(traceback);
3272 static int
3273 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3274 int what, PyObject *arg)
3276 PyObject *type, *value, *traceback;
3277 int err;
3278 PyErr_Fetch(&type, &value, &traceback);
3279 err = call_trace(func, obj, frame, what, arg);
3280 if (err == 0)
3282 PyErr_Restore(type, value, traceback);
3283 return 0;
3285 else {
3286 Py_XDECREF(type);
3287 Py_XDECREF(value);
3288 Py_XDECREF(traceback);
3289 return -1;
3293 static int
3294 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3295 int what, PyObject *arg)
3297 register PyThreadState *tstate = frame->f_tstate;
3298 int result;
3299 if (tstate->tracing)
3300 return 0;
3301 tstate->tracing++;
3302 tstate->use_tracing = 0;
3303 result = func(obj, frame, what, arg);
3304 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3305 || (tstate->c_profilefunc != NULL));
3306 tstate->tracing--;
3307 return result;
3310 PyObject *
3311 _PyEval_CallTracing(PyObject *func, PyObject *args)
3313 PyFrameObject *frame = PyEval_GetFrame();
3314 PyThreadState *tstate = frame->f_tstate;
3315 int save_tracing = tstate->tracing;
3316 int save_use_tracing = tstate->use_tracing;
3317 PyObject *result;
3319 tstate->tracing = 0;
3320 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3321 || (tstate->c_profilefunc != NULL));
3322 result = PyObject_Call(func, args, NULL);
3323 tstate->tracing = save_tracing;
3324 tstate->use_tracing = save_use_tracing;
3325 return result;
3328 static int
3329 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
3330 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3331 int *instr_prev)
3333 int result = 0;
3335 /* If the last instruction executed isn't in the current
3336 instruction window, reset the window. If the last
3337 instruction happens to fall at the start of a line or if it
3338 represents a jump backwards, call the trace function.
3340 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
3341 int line;
3342 PyAddrPair bounds;
3344 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3345 &bounds);
3346 if (line >= 0) {
3347 frame->f_lineno = line;
3348 result = call_trace(func, obj, frame,
3349 PyTrace_LINE, Py_None);
3351 *instr_lb = bounds.ap_lower;
3352 *instr_ub = bounds.ap_upper;
3354 else if (frame->f_lasti <= *instr_prev) {
3355 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3357 *instr_prev = frame->f_lasti;
3358 return result;
3361 void
3362 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
3364 PyThreadState *tstate = PyThreadState_GET();
3365 PyObject *temp = tstate->c_profileobj;
3366 Py_XINCREF(arg);
3367 tstate->c_profilefunc = NULL;
3368 tstate->c_profileobj = NULL;
3369 /* Must make sure that tracing is not ignored if 'temp' is freed */
3370 tstate->use_tracing = tstate->c_tracefunc != NULL;
3371 Py_XDECREF(temp);
3372 tstate->c_profilefunc = func;
3373 tstate->c_profileobj = arg;
3374 /* Flag that tracing or profiling is turned on */
3375 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
3378 void
3379 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3381 PyThreadState *tstate = PyThreadState_GET();
3382 PyObject *temp = tstate->c_traceobj;
3383 Py_XINCREF(arg);
3384 tstate->c_tracefunc = NULL;
3385 tstate->c_traceobj = NULL;
3386 /* Must make sure that profiling is not ignored if 'temp' is freed */
3387 tstate->use_tracing = tstate->c_profilefunc != NULL;
3388 Py_XDECREF(temp);
3389 tstate->c_tracefunc = func;
3390 tstate->c_traceobj = arg;
3391 /* Flag that tracing or profiling is turned on */
3392 tstate->use_tracing = ((func != NULL)
3393 || (tstate->c_profilefunc != NULL));
3396 PyObject *
3397 PyEval_GetBuiltins(void)
3399 PyFrameObject *current_frame = PyEval_GetFrame();
3400 if (current_frame == NULL)
3401 return PyThreadState_GET()->interp->builtins;
3402 else
3403 return current_frame->f_builtins;
3406 PyObject *
3407 PyEval_GetLocals(void)
3409 PyFrameObject *current_frame = PyEval_GetFrame();
3410 if (current_frame == NULL)
3411 return NULL;
3412 PyFrame_FastToLocals(current_frame);
3413 return current_frame->f_locals;
3416 PyObject *
3417 PyEval_GetGlobals(void)
3419 PyFrameObject *current_frame = PyEval_GetFrame();
3420 if (current_frame == NULL)
3421 return NULL;
3422 else
3423 return current_frame->f_globals;
3426 PyFrameObject *
3427 PyEval_GetFrame(void)
3429 PyThreadState *tstate = PyThreadState_GET();
3430 return _PyThreadState_GetFrame(tstate);
3434 PyEval_GetRestricted(void)
3436 PyFrameObject *current_frame = PyEval_GetFrame();
3437 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
3441 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
3443 PyFrameObject *current_frame = PyEval_GetFrame();
3444 int result = cf->cf_flags != 0;
3446 if (current_frame != NULL) {
3447 const int codeflags = current_frame->f_code->co_flags;
3448 const int compilerflags = codeflags & PyCF_MASK;
3449 if (compilerflags) {
3450 result = 1;
3451 cf->cf_flags |= compilerflags;
3453 #if 0 /* future keyword */
3454 if (codeflags & CO_GENERATOR_ALLOWED) {
3455 result = 1;
3456 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3458 #endif
3460 return result;
3464 Py_FlushLine(void)
3466 PyObject *f = PySys_GetObject("stdout");
3467 if (f == NULL)
3468 return 0;
3469 if (!PyFile_SoftSpace(f, 0))
3470 return 0;
3471 return PyFile_WriteString("\n", f);
3475 /* External interface to call any callable object.
3476 The arg must be a tuple or NULL. */
3478 #undef PyEval_CallObject
3479 /* for backward compatibility: export this interface */
3481 PyObject *
3482 PyEval_CallObject(PyObject *func, PyObject *arg)
3484 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
3486 #define PyEval_CallObject(func,arg) \
3487 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3489 PyObject *
3490 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
3492 PyObject *result;
3494 if (arg == NULL) {
3495 arg = PyTuple_New(0);
3496 if (arg == NULL)
3497 return NULL;
3499 else if (!PyTuple_Check(arg)) {
3500 PyErr_SetString(PyExc_TypeError,
3501 "argument list must be a tuple");
3502 return NULL;
3504 else
3505 Py_INCREF(arg);
3507 if (kw != NULL && !PyDict_Check(kw)) {
3508 PyErr_SetString(PyExc_TypeError,
3509 "keyword list must be a dictionary");
3510 Py_DECREF(arg);
3511 return NULL;
3514 result = PyObject_Call(func, arg, kw);
3515 Py_DECREF(arg);
3516 return result;
3519 const char *
3520 PyEval_GetFuncName(PyObject *func)
3522 if (PyMethod_Check(func))
3523 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3524 else if (PyFunction_Check(func))
3525 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3526 else if (PyCFunction_Check(func))
3527 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3528 else if (PyClass_Check(func))
3529 return PyString_AsString(((PyClassObject*)func)->cl_name);
3530 else if (PyInstance_Check(func)) {
3531 return PyString_AsString(
3532 ((PyInstanceObject*)func)->in_class->cl_name);
3533 } else {
3534 return func->ob_type->tp_name;
3538 const char *
3539 PyEval_GetFuncDesc(PyObject *func)
3541 if (PyMethod_Check(func))
3542 return "()";
3543 else if (PyFunction_Check(func))
3544 return "()";
3545 else if (PyCFunction_Check(func))
3546 return "()";
3547 else if (PyClass_Check(func))
3548 return " constructor";
3549 else if (PyInstance_Check(func)) {
3550 return " instance";
3551 } else {
3552 return " object";
3556 static void
3557 err_args(PyObject *func, int flags, int nargs)
3559 if (flags & METH_NOARGS)
3560 PyErr_Format(PyExc_TypeError,
3561 "%.200s() takes no arguments (%d given)",
3562 ((PyCFunctionObject *)func)->m_ml->ml_name,
3563 nargs);
3564 else
3565 PyErr_Format(PyExc_TypeError,
3566 "%.200s() takes exactly one argument (%d given)",
3567 ((PyCFunctionObject *)func)->m_ml->ml_name,
3568 nargs);
3571 #define C_TRACE(x, call) \
3572 if (tstate->use_tracing && tstate->c_profilefunc) { \
3573 if (call_trace(tstate->c_profilefunc, \
3574 tstate->c_profileobj, \
3575 tstate->frame, PyTrace_C_CALL, \
3576 func)) { \
3577 x = NULL; \
3579 else { \
3580 x = call; \
3581 if (tstate->c_profilefunc != NULL) { \
3582 if (x == NULL) { \
3583 call_trace_protected(tstate->c_profilefunc, \
3584 tstate->c_profileobj, \
3585 tstate->frame, PyTrace_C_EXCEPTION, \
3586 func); \
3587 /* XXX should pass (type, value, tb) */ \
3588 } else { \
3589 if (call_trace(tstate->c_profilefunc, \
3590 tstate->c_profileobj, \
3591 tstate->frame, PyTrace_C_RETURN, \
3592 func)) { \
3593 Py_DECREF(x); \
3594 x = NULL; \
3599 } else { \
3600 x = call; \
3603 static PyObject *
3604 call_function(PyObject ***pp_stack, int oparg
3605 #ifdef WITH_TSC
3606 , uint64* pintr0, uint64* pintr1
3607 #endif
3610 int na = oparg & 0xff;
3611 int nk = (oparg>>8) & 0xff;
3612 int n = na + 2 * nk;
3613 PyObject **pfunc = (*pp_stack) - n - 1;
3614 PyObject *func = *pfunc;
3615 PyObject *x, *w;
3617 /* Always dispatch PyCFunction first, because these are
3618 presumed to be the most frequent callable object.
3620 if (PyCFunction_Check(func) && nk == 0) {
3621 int flags = PyCFunction_GET_FLAGS(func);
3622 PyThreadState *tstate = PyThreadState_GET();
3624 PCALL(PCALL_CFUNCTION);
3625 if (flags & (METH_NOARGS | METH_O)) {
3626 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3627 PyObject *self = PyCFunction_GET_SELF(func);
3628 if (flags & METH_NOARGS && na == 0) {
3629 C_TRACE(x, (*meth)(self,NULL));
3631 else if (flags & METH_O && na == 1) {
3632 PyObject *arg = EXT_POP(*pp_stack);
3633 C_TRACE(x, (*meth)(self,arg));
3634 Py_DECREF(arg);
3636 else {
3637 err_args(func, flags, na);
3638 x = NULL;
3641 else {
3642 PyObject *callargs;
3643 callargs = load_args(pp_stack, na);
3644 READ_TIMESTAMP(*pintr0);
3645 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3646 READ_TIMESTAMP(*pintr1);
3647 Py_XDECREF(callargs);
3649 } else {
3650 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3651 /* optimize access to bound methods */
3652 PyObject *self = PyMethod_GET_SELF(func);
3653 PCALL(PCALL_METHOD);
3654 PCALL(PCALL_BOUND_METHOD);
3655 Py_INCREF(self);
3656 func = PyMethod_GET_FUNCTION(func);
3657 Py_INCREF(func);
3658 Py_DECREF(*pfunc);
3659 *pfunc = self;
3660 na++;
3661 n++;
3662 } else
3663 Py_INCREF(func);
3664 READ_TIMESTAMP(*pintr0);
3665 if (PyFunction_Check(func))
3666 x = fast_function(func, pp_stack, n, na, nk);
3667 else
3668 x = do_call(func, pp_stack, na, nk);
3669 READ_TIMESTAMP(*pintr1);
3670 Py_DECREF(func);
3673 /* Clear the stack of the function object. Also removes
3674 the arguments in case they weren't consumed already
3675 (fast_function() and err_args() leave them on the stack).
3677 while ((*pp_stack) > pfunc) {
3678 w = EXT_POP(*pp_stack);
3679 Py_DECREF(w);
3680 PCALL(PCALL_POP);
3682 return x;
3685 /* The fast_function() function optimize calls for which no argument
3686 tuple is necessary; the objects are passed directly from the stack.
3687 For the simplest case -- a function that takes only positional
3688 arguments and is called with only positional arguments -- it
3689 inlines the most primitive frame setup code from
3690 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3691 done before evaluating the frame.
3694 static PyObject *
3695 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
3697 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3698 PyObject *globals = PyFunction_GET_GLOBALS(func);
3699 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3700 PyObject **d = NULL;
3701 int nd = 0;
3703 PCALL(PCALL_FUNCTION);
3704 PCALL(PCALL_FAST_FUNCTION);
3705 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
3706 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3707 PyFrameObject *f;
3708 PyObject *retval = NULL;
3709 PyThreadState *tstate = PyThreadState_GET();
3710 PyObject **fastlocals, **stack;
3711 int i;
3713 PCALL(PCALL_FASTER_FUNCTION);
3714 assert(globals != NULL);
3715 /* XXX Perhaps we should create a specialized
3716 PyFrame_New() that doesn't take locals, but does
3717 take builtins without sanity checking them.
3719 assert(tstate != NULL);
3720 f = PyFrame_New(tstate, co, globals, NULL);
3721 if (f == NULL)
3722 return NULL;
3724 fastlocals = f->f_localsplus;
3725 stack = (*pp_stack) - n;
3727 for (i = 0; i < n; i++) {
3728 Py_INCREF(*stack);
3729 fastlocals[i] = *stack++;
3731 retval = PyEval_EvalFrameEx(f,0);
3732 ++tstate->recursion_depth;
3733 Py_DECREF(f);
3734 --tstate->recursion_depth;
3735 return retval;
3737 if (argdefs != NULL) {
3738 d = &PyTuple_GET_ITEM(argdefs, 0);
3739 nd = Py_SIZE(argdefs);
3741 return PyEval_EvalCodeEx(co, globals,
3742 (PyObject *)NULL, (*pp_stack)-n, na,
3743 (*pp_stack)-2*nk, nk, d, nd,
3744 PyFunction_GET_CLOSURE(func));
3747 static PyObject *
3748 update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3749 PyObject *func)
3751 PyObject *kwdict = NULL;
3752 if (orig_kwdict == NULL)
3753 kwdict = PyDict_New();
3754 else {
3755 kwdict = PyDict_Copy(orig_kwdict);
3756 Py_DECREF(orig_kwdict);
3758 if (kwdict == NULL)
3759 return NULL;
3760 while (--nk >= 0) {
3761 int err;
3762 PyObject *value = EXT_POP(*pp_stack);
3763 PyObject *key = EXT_POP(*pp_stack);
3764 if (PyDict_GetItem(kwdict, key) != NULL) {
3765 PyErr_Format(PyExc_TypeError,
3766 "%.200s%s got multiple values "
3767 "for keyword argument '%.200s'",
3768 PyEval_GetFuncName(func),
3769 PyEval_GetFuncDesc(func),
3770 PyString_AsString(key));
3771 Py_DECREF(key);
3772 Py_DECREF(value);
3773 Py_DECREF(kwdict);
3774 return NULL;
3776 err = PyDict_SetItem(kwdict, key, value);
3777 Py_DECREF(key);
3778 Py_DECREF(value);
3779 if (err) {
3780 Py_DECREF(kwdict);
3781 return NULL;
3784 return kwdict;
3787 static PyObject *
3788 update_star_args(int nstack, int nstar, PyObject *stararg,
3789 PyObject ***pp_stack)
3791 PyObject *callargs, *w;
3793 callargs = PyTuple_New(nstack + nstar);
3794 if (callargs == NULL) {
3795 return NULL;
3797 if (nstar) {
3798 int i;
3799 for (i = 0; i < nstar; i++) {
3800 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3801 Py_INCREF(a);
3802 PyTuple_SET_ITEM(callargs, nstack + i, a);
3805 while (--nstack >= 0) {
3806 w = EXT_POP(*pp_stack);
3807 PyTuple_SET_ITEM(callargs, nstack, w);
3809 return callargs;
3812 static PyObject *
3813 load_args(PyObject ***pp_stack, int na)
3815 PyObject *args = PyTuple_New(na);
3816 PyObject *w;
3818 if (args == NULL)
3819 return NULL;
3820 while (--na >= 0) {
3821 w = EXT_POP(*pp_stack);
3822 PyTuple_SET_ITEM(args, na, w);
3824 return args;
3827 static PyObject *
3828 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3830 PyObject *callargs = NULL;
3831 PyObject *kwdict = NULL;
3832 PyObject *result = NULL;
3834 if (nk > 0) {
3835 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
3836 if (kwdict == NULL)
3837 goto call_fail;
3839 callargs = load_args(pp_stack, na);
3840 if (callargs == NULL)
3841 goto call_fail;
3842 #ifdef CALL_PROFILE
3843 /* At this point, we have to look at the type of func to
3844 update the call stats properly. Do it here so as to avoid
3845 exposing the call stats machinery outside ceval.c
3847 if (PyFunction_Check(func))
3848 PCALL(PCALL_FUNCTION);
3849 else if (PyMethod_Check(func))
3850 PCALL(PCALL_METHOD);
3851 else if (PyType_Check(func))
3852 PCALL(PCALL_TYPE);
3853 else
3854 PCALL(PCALL_OTHER);
3855 #endif
3856 result = PyObject_Call(func, callargs, kwdict);
3857 call_fail:
3858 Py_XDECREF(callargs);
3859 Py_XDECREF(kwdict);
3860 return result;
3863 static PyObject *
3864 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3866 int nstar = 0;
3867 PyObject *callargs = NULL;
3868 PyObject *stararg = NULL;
3869 PyObject *kwdict = NULL;
3870 PyObject *result = NULL;
3872 if (flags & CALL_FLAG_KW) {
3873 kwdict = EXT_POP(*pp_stack);
3874 if (!PyDict_Check(kwdict)) {
3875 PyObject *d;
3876 d = PyDict_New();
3877 if (d == NULL)
3878 goto ext_call_fail;
3879 if (PyDict_Update(d, kwdict) != 0) {
3880 Py_DECREF(d);
3881 /* PyDict_Update raises attribute
3882 * error (percolated from an attempt
3883 * to get 'keys' attribute) instead of
3884 * a type error if its second argument
3885 * is not a mapping.
3887 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3888 PyErr_Format(PyExc_TypeError,
3889 "%.200s%.200s argument after ** "
3890 "must be a mapping, not %.200s",
3891 PyEval_GetFuncName(func),
3892 PyEval_GetFuncDesc(func),
3893 kwdict->ob_type->tp_name);
3895 goto ext_call_fail;
3897 Py_DECREF(kwdict);
3898 kwdict = d;
3901 if (flags & CALL_FLAG_VAR) {
3902 stararg = EXT_POP(*pp_stack);
3903 if (!PyTuple_Check(stararg)) {
3904 PyObject *t = NULL;
3905 t = PySequence_Tuple(stararg);
3906 if (t == NULL) {
3907 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3908 PyErr_Format(PyExc_TypeError,
3909 "%.200s%.200s argument after * "
3910 "must be a sequence, not %200s",
3911 PyEval_GetFuncName(func),
3912 PyEval_GetFuncDesc(func),
3913 stararg->ob_type->tp_name);
3915 goto ext_call_fail;
3917 Py_DECREF(stararg);
3918 stararg = t;
3920 nstar = PyTuple_GET_SIZE(stararg);
3922 if (nk > 0) {
3923 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
3924 if (kwdict == NULL)
3925 goto ext_call_fail;
3927 callargs = update_star_args(na, nstar, stararg, pp_stack);
3928 if (callargs == NULL)
3929 goto ext_call_fail;
3930 #ifdef CALL_PROFILE
3931 /* At this point, we have to look at the type of func to
3932 update the call stats properly. Do it here so as to avoid
3933 exposing the call stats machinery outside ceval.c
3935 if (PyFunction_Check(func))
3936 PCALL(PCALL_FUNCTION);
3937 else if (PyMethod_Check(func))
3938 PCALL(PCALL_METHOD);
3939 else if (PyType_Check(func))
3940 PCALL(PCALL_TYPE);
3941 else
3942 PCALL(PCALL_OTHER);
3943 #endif
3944 result = PyObject_Call(func, callargs, kwdict);
3945 ext_call_fail:
3946 Py_XDECREF(callargs);
3947 Py_XDECREF(kwdict);
3948 Py_XDECREF(stararg);
3949 return result;
3952 /* Extract a slice index from a PyInt or PyLong or an object with the
3953 nb_index slot defined, and store in *pi.
3954 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3955 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
3956 Return 0 on error, 1 on success.
3958 /* Note: If v is NULL, return success without storing into *pi. This
3959 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3960 called by the SLICE opcode with v and/or w equal to NULL.
3963 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
3965 if (v != NULL) {
3966 Py_ssize_t x;
3967 if (PyInt_Check(v)) {
3968 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3969 however, it looks like it should be AsSsize_t.
3970 There should be a comment here explaining why.
3972 x = PyInt_AS_LONG(v);
3974 else if (PyIndex_Check(v)) {
3975 x = PyNumber_AsSsize_t(v, NULL);
3976 if (x == -1 && PyErr_Occurred())
3977 return 0;
3979 else {
3980 PyErr_SetString(PyExc_TypeError,
3981 "slice indices must be integers or "
3982 "None or have an __index__ method");
3983 return 0;
3985 *pi = x;
3987 return 1;
3990 #undef ISINDEX
3991 #define ISINDEX(x) ((x) == NULL || \
3992 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
3994 static PyObject *
3995 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
3997 PyTypeObject *tp = u->ob_type;
3998 PySequenceMethods *sq = tp->tp_as_sequence;
4000 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4001 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4002 if (!_PyEval_SliceIndex(v, &ilow))
4003 return NULL;
4004 if (!_PyEval_SliceIndex(w, &ihigh))
4005 return NULL;
4006 return PySequence_GetSlice(u, ilow, ihigh);
4008 else {
4009 PyObject *slice = PySlice_New(v, w, NULL);
4010 if (slice != NULL) {
4011 PyObject *res = PyObject_GetItem(u, slice);
4012 Py_DECREF(slice);
4013 return res;
4015 else
4016 return NULL;
4020 static int
4021 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4022 /* u[v:w] = x */
4024 PyTypeObject *tp = u->ob_type;
4025 PySequenceMethods *sq = tp->tp_as_sequence;
4027 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4028 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4029 if (!_PyEval_SliceIndex(v, &ilow))
4030 return -1;
4031 if (!_PyEval_SliceIndex(w, &ihigh))
4032 return -1;
4033 if (x == NULL)
4034 return PySequence_DelSlice(u, ilow, ihigh);
4035 else
4036 return PySequence_SetSlice(u, ilow, ihigh, x);
4038 else {
4039 PyObject *slice = PySlice_New(v, w, NULL);
4040 if (slice != NULL) {
4041 int res;
4042 if (x != NULL)
4043 res = PyObject_SetItem(u, slice, x);
4044 else
4045 res = PyObject_DelItem(u, slice);
4046 Py_DECREF(slice);
4047 return res;
4049 else
4050 return -1;
4054 #define Py3kExceptionClass_Check(x) \
4055 (PyType_Check((x)) && \
4056 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4058 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4059 "BaseException is not allowed in 3.x"
4061 static PyObject *
4062 cmp_outcome(int op, register PyObject *v, register PyObject *w)
4064 int res = 0;
4065 switch (op) {
4066 case PyCmp_IS:
4067 res = (v == w);
4068 break;
4069 case PyCmp_IS_NOT:
4070 res = (v != w);
4071 break;
4072 case PyCmp_IN:
4073 res = PySequence_Contains(w, v);
4074 if (res < 0)
4075 return NULL;
4076 break;
4077 case PyCmp_NOT_IN:
4078 res = PySequence_Contains(w, v);
4079 if (res < 0)
4080 return NULL;
4081 res = !res;
4082 break;
4083 case PyCmp_EXC_MATCH:
4084 if (PyTuple_Check(w)) {
4085 Py_ssize_t i, length;
4086 length = PyTuple_Size(w);
4087 for (i = 0; i < length; i += 1) {
4088 PyObject *exc = PyTuple_GET_ITEM(w, i);
4089 if (PyString_Check(exc)) {
4090 int ret_val;
4091 ret_val = PyErr_WarnEx(
4092 PyExc_DeprecationWarning,
4093 "catching of string "
4094 "exceptions is deprecated", 1);
4095 if (ret_val < 0)
4096 return NULL;
4098 else if (Py_Py3kWarningFlag &&
4099 !PyTuple_Check(exc) &&
4100 !Py3kExceptionClass_Check(exc))
4102 int ret_val;
4103 ret_val = PyErr_WarnEx(
4104 PyExc_DeprecationWarning,
4105 CANNOT_CATCH_MSG, 1);
4106 if (ret_val < 0)
4107 return NULL;
4111 else {
4112 if (PyString_Check(w)) {
4113 int ret_val;
4114 ret_val = PyErr_WarnEx(
4115 PyExc_DeprecationWarning,
4116 "catching of string "
4117 "exceptions is deprecated", 1);
4118 if (ret_val < 0)
4119 return NULL;
4121 else if (Py_Py3kWarningFlag &&
4122 !PyTuple_Check(w) &&
4123 !Py3kExceptionClass_Check(w))
4125 int ret_val;
4126 ret_val = PyErr_WarnEx(
4127 PyExc_DeprecationWarning,
4128 CANNOT_CATCH_MSG, 1);
4129 if (ret_val < 0)
4130 return NULL;
4133 res = PyErr_GivenExceptionMatches(v, w);
4134 break;
4135 default:
4136 return PyObject_RichCompare(v, w, op);
4138 v = res ? Py_True : Py_False;
4139 Py_INCREF(v);
4140 return v;
4143 static PyObject *
4144 import_from(PyObject *v, PyObject *name)
4146 PyObject *x;
4148 x = PyObject_GetAttr(v, name);
4149 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4150 PyErr_Format(PyExc_ImportError,
4151 "cannot import name %.230s",
4152 PyString_AsString(name));
4154 return x;
4157 static int
4158 import_all_from(PyObject *locals, PyObject *v)
4160 PyObject *all = PyObject_GetAttrString(v, "__all__");
4161 PyObject *dict, *name, *value;
4162 int skip_leading_underscores = 0;
4163 int pos, err;
4165 if (all == NULL) {
4166 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4167 return -1; /* Unexpected error */
4168 PyErr_Clear();
4169 dict = PyObject_GetAttrString(v, "__dict__");
4170 if (dict == NULL) {
4171 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4172 return -1;
4173 PyErr_SetString(PyExc_ImportError,
4174 "from-import-* object has no __dict__ and no __all__");
4175 return -1;
4177 all = PyMapping_Keys(dict);
4178 Py_DECREF(dict);
4179 if (all == NULL)
4180 return -1;
4181 skip_leading_underscores = 1;
4184 for (pos = 0, err = 0; ; pos++) {
4185 name = PySequence_GetItem(all, pos);
4186 if (name == NULL) {
4187 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4188 err = -1;
4189 else
4190 PyErr_Clear();
4191 break;
4193 if (skip_leading_underscores &&
4194 PyString_Check(name) &&
4195 PyString_AS_STRING(name)[0] == '_')
4197 Py_DECREF(name);
4198 continue;
4200 value = PyObject_GetAttr(v, name);
4201 if (value == NULL)
4202 err = -1;
4203 else if (PyDict_CheckExact(locals))
4204 err = PyDict_SetItem(locals, name, value);
4205 else
4206 err = PyObject_SetItem(locals, name, value);
4207 Py_DECREF(name);
4208 Py_XDECREF(value);
4209 if (err != 0)
4210 break;
4212 Py_DECREF(all);
4213 return err;
4216 static PyObject *
4217 build_class(PyObject *methods, PyObject *bases, PyObject *name)
4219 PyObject *metaclass = NULL, *result, *base;
4221 if (PyDict_Check(methods))
4222 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4223 if (metaclass != NULL)
4224 Py_INCREF(metaclass);
4225 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4226 base = PyTuple_GET_ITEM(bases, 0);
4227 metaclass = PyObject_GetAttrString(base, "__class__");
4228 if (metaclass == NULL) {
4229 PyErr_Clear();
4230 metaclass = (PyObject *)base->ob_type;
4231 Py_INCREF(metaclass);
4234 else {
4235 PyObject *g = PyEval_GetGlobals();
4236 if (g != NULL && PyDict_Check(g))
4237 metaclass = PyDict_GetItemString(g, "__metaclass__");
4238 if (metaclass == NULL)
4239 metaclass = (PyObject *) &PyClass_Type;
4240 Py_INCREF(metaclass);
4242 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4243 NULL);
4244 Py_DECREF(metaclass);
4245 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4246 /* A type error here likely means that the user passed
4247 in a base that was not a class (such the random module
4248 instead of the random.random type). Help them out with
4249 by augmenting the error message with more information.*/
4251 PyObject *ptype, *pvalue, *ptraceback;
4253 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4254 if (PyString_Check(pvalue)) {
4255 PyObject *newmsg;
4256 newmsg = PyString_FromFormat(
4257 "Error when calling the metaclass bases\n"
4258 " %s",
4259 PyString_AS_STRING(pvalue));
4260 if (newmsg != NULL) {
4261 Py_DECREF(pvalue);
4262 pvalue = newmsg;
4265 PyErr_Restore(ptype, pvalue, ptraceback);
4267 return result;
4270 static int
4271 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4272 PyObject *locals)
4274 int n;
4275 PyObject *v;
4276 int plain = 0;
4278 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4279 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4280 /* Backward compatibility hack */
4281 globals = PyTuple_GetItem(prog, 1);
4282 if (n == 3)
4283 locals = PyTuple_GetItem(prog, 2);
4284 prog = PyTuple_GetItem(prog, 0);
4286 if (globals == Py_None) {
4287 globals = PyEval_GetGlobals();
4288 if (locals == Py_None) {
4289 locals = PyEval_GetLocals();
4290 plain = 1;
4292 if (!globals || !locals) {
4293 PyErr_SetString(PyExc_SystemError,
4294 "globals and locals cannot be NULL");
4295 return -1;
4298 else if (locals == Py_None)
4299 locals = globals;
4300 if (!PyString_Check(prog) &&
4301 !PyUnicode_Check(prog) &&
4302 !PyCode_Check(prog) &&
4303 !PyFile_Check(prog)) {
4304 PyErr_SetString(PyExc_TypeError,
4305 "exec: arg 1 must be a string, file, or code object");
4306 return -1;
4308 if (!PyDict_Check(globals)) {
4309 PyErr_SetString(PyExc_TypeError,
4310 "exec: arg 2 must be a dictionary or None");
4311 return -1;
4313 if (!PyMapping_Check(locals)) {
4314 PyErr_SetString(PyExc_TypeError,
4315 "exec: arg 3 must be a mapping or None");
4316 return -1;
4318 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4319 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4320 if (PyCode_Check(prog)) {
4321 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4322 PyErr_SetString(PyExc_TypeError,
4323 "code object passed to exec may not contain free variables");
4324 return -1;
4326 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4328 else if (PyFile_Check(prog)) {
4329 FILE *fp = PyFile_AsFile(prog);
4330 char *name = PyString_AsString(PyFile_Name(prog));
4331 PyCompilerFlags cf;
4332 if (name == NULL)
4333 return -1;
4334 cf.cf_flags = 0;
4335 if (PyEval_MergeCompilerFlags(&cf))
4336 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4337 locals, &cf);
4338 else
4339 v = PyRun_File(fp, name, Py_file_input, globals,
4340 locals);
4342 else {
4343 PyObject *tmp = NULL;
4344 char *str;
4345 PyCompilerFlags cf;
4346 cf.cf_flags = 0;
4347 #ifdef Py_USING_UNICODE
4348 if (PyUnicode_Check(prog)) {
4349 tmp = PyUnicode_AsUTF8String(prog);
4350 if (tmp == NULL)
4351 return -1;
4352 prog = tmp;
4353 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4355 #endif
4356 if (PyString_AsStringAndSize(prog, &str, NULL))
4357 return -1;
4358 if (PyEval_MergeCompilerFlags(&cf))
4359 v = PyRun_StringFlags(str, Py_file_input, globals,
4360 locals, &cf);
4361 else
4362 v = PyRun_String(str, Py_file_input, globals, locals);
4363 Py_XDECREF(tmp);
4365 if (plain)
4366 PyFrame_LocalsToFast(f, 0);
4367 if (v == NULL)
4368 return -1;
4369 Py_DECREF(v);
4370 return 0;
4373 static void
4374 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4376 char *obj_str;
4378 if (!obj)
4379 return;
4381 obj_str = PyString_AsString(obj);
4382 if (!obj_str)
4383 return;
4385 PyErr_Format(exc, format_str, obj_str);
4388 static PyObject *
4389 string_concatenate(PyObject *v, PyObject *w,
4390 PyFrameObject *f, unsigned char *next_instr)
4392 /* This function implements 'variable += expr' when both arguments
4393 are strings. */
4394 Py_ssize_t v_len = PyString_GET_SIZE(v);
4395 Py_ssize_t w_len = PyString_GET_SIZE(w);
4396 Py_ssize_t new_len = v_len + w_len;
4397 if (new_len < 0) {
4398 PyErr_SetString(PyExc_OverflowError,
4399 "strings are too large to concat");
4400 return NULL;
4403 if (v->ob_refcnt == 2) {
4404 /* In the common case, there are 2 references to the value
4405 * stored in 'variable' when the += is performed: one on the
4406 * value stack (in 'v') and one still stored in the
4407 * 'variable'. We try to delete the variable now to reduce
4408 * the refcnt to 1.
4410 switch (*next_instr) {
4411 case STORE_FAST:
4413 int oparg = PEEKARG();
4414 PyObject **fastlocals = f->f_localsplus;
4415 if (GETLOCAL(oparg) == v)
4416 SETLOCAL(oparg, NULL);
4417 break;
4419 case STORE_DEREF:
4421 PyObject **freevars = (f->f_localsplus +
4422 f->f_code->co_nlocals);
4423 PyObject *c = freevars[PEEKARG()];
4424 if (PyCell_GET(c) == v)
4425 PyCell_Set(c, NULL);
4426 break;
4428 case STORE_NAME:
4430 PyObject *names = f->f_code->co_names;
4431 PyObject *name = GETITEM(names, PEEKARG());
4432 PyObject *locals = f->f_locals;
4433 if (PyDict_CheckExact(locals) &&
4434 PyDict_GetItem(locals, name) == v) {
4435 if (PyDict_DelItem(locals, name) != 0) {
4436 PyErr_Clear();
4439 break;
4444 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4445 /* Now we own the last reference to 'v', so we can resize it
4446 * in-place.
4448 if (_PyString_Resize(&v, new_len) != 0) {
4449 /* XXX if _PyString_Resize() fails, 'v' has been
4450 * deallocated so it cannot be put back into
4451 * 'variable'. The MemoryError is raised when there
4452 * is no value in 'variable', which might (very
4453 * remotely) be a cause of incompatibilities.
4455 return NULL;
4457 /* copy 'w' into the newly allocated area of 'v' */
4458 memcpy(PyString_AS_STRING(v) + v_len,
4459 PyString_AS_STRING(w), w_len);
4460 return v;
4462 else {
4463 /* When in-place resizing is not an option. */
4464 PyString_Concat(&v, w);
4465 return v;
4469 #ifdef DYNAMIC_EXECUTION_PROFILE
4471 static PyObject *
4472 getarray(long a[256])
4474 int i;
4475 PyObject *l = PyList_New(256);
4476 if (l == NULL) return NULL;
4477 for (i = 0; i < 256; i++) {
4478 PyObject *x = PyInt_FromLong(a[i]);
4479 if (x == NULL) {
4480 Py_DECREF(l);
4481 return NULL;
4483 PyList_SetItem(l, i, x);
4485 for (i = 0; i < 256; i++)
4486 a[i] = 0;
4487 return l;
4490 PyObject *
4491 _Py_GetDXProfile(PyObject *self, PyObject *args)
4493 #ifndef DXPAIRS
4494 return getarray(dxp);
4495 #else
4496 int i;
4497 PyObject *l = PyList_New(257);
4498 if (l == NULL) return NULL;
4499 for (i = 0; i < 257; i++) {
4500 PyObject *x = getarray(dxpairs[i]);
4501 if (x == NULL) {
4502 Py_DECREF(l);
4503 return NULL;
4505 PyList_SetItem(l, i, x);
4507 return l;
4508 #endif
4511 #endif