Added more cross-reference targets and tidied up list of useful handlers.
[python.git] / Python / ceval.c
blob88483a5f36b72126e902b23fc386a2ce21c43fd6
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 PyThread_type_lock pending_lock = 0; /* for pending calls */
217 static long main_thread = 0;
220 PyEval_ThreadsInitialized(void)
222 return interpreter_lock != 0;
225 void
226 PyEval_InitThreads(void)
228 if (interpreter_lock)
229 return;
230 interpreter_lock = PyThread_allocate_lock();
231 PyThread_acquire_lock(interpreter_lock, 1);
232 main_thread = PyThread_get_thread_ident();
235 void
236 PyEval_AcquireLock(void)
238 PyThread_acquire_lock(interpreter_lock, 1);
241 void
242 PyEval_ReleaseLock(void)
244 PyThread_release_lock(interpreter_lock);
247 void
248 PyEval_AcquireThread(PyThreadState *tstate)
250 if (tstate == NULL)
251 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
252 /* Check someone has called PyEval_InitThreads() to create the lock */
253 assert(interpreter_lock);
254 PyThread_acquire_lock(interpreter_lock, 1);
255 if (PyThreadState_Swap(tstate) != NULL)
256 Py_FatalError(
257 "PyEval_AcquireThread: non-NULL old thread state");
260 void
261 PyEval_ReleaseThread(PyThreadState *tstate)
263 if (tstate == NULL)
264 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
265 if (PyThreadState_Swap(NULL) != tstate)
266 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
267 PyThread_release_lock(interpreter_lock);
270 /* This function is called from PyOS_AfterFork to ensure that newly
271 created child processes don't hold locks referring to threads which
272 are not running in the child process. (This could also be done using
273 pthread_atfork mechanism, at least for the pthreads implementation.) */
275 void
276 PyEval_ReInitThreads(void)
278 PyObject *threading, *result;
279 PyThreadState *tstate;
281 if (!interpreter_lock)
282 return;
283 /*XXX Can't use PyThread_free_lock here because it does too
284 much error-checking. Doing this cleanly would require
285 adding a new function to each thread_*.h. Instead, just
286 create a new lock and waste a little bit of memory */
287 interpreter_lock = PyThread_allocate_lock();
288 pending_lock = PyThread_allocate_lock();
289 PyThread_acquire_lock(interpreter_lock, 1);
290 main_thread = PyThread_get_thread_ident();
292 /* Update the threading module with the new state.
294 tstate = PyThreadState_GET();
295 threading = PyMapping_GetItemString(tstate->interp->modules,
296 "threading");
297 if (threading == NULL) {
298 /* threading not imported */
299 PyErr_Clear();
300 return;
302 result = PyObject_CallMethod(threading, "_after_fork", NULL);
303 if (result == NULL)
304 PyErr_WriteUnraisable(threading);
305 else
306 Py_DECREF(result);
307 Py_DECREF(threading);
309 #endif
311 /* Functions save_thread and restore_thread are always defined so
312 dynamically loaded modules needn't be compiled separately for use
313 with and without threads: */
315 PyThreadState *
316 PyEval_SaveThread(void)
318 PyThreadState *tstate = PyThreadState_Swap(NULL);
319 if (tstate == NULL)
320 Py_FatalError("PyEval_SaveThread: NULL tstate");
321 #ifdef WITH_THREAD
322 if (interpreter_lock)
323 PyThread_release_lock(interpreter_lock);
324 #endif
325 return tstate;
328 void
329 PyEval_RestoreThread(PyThreadState *tstate)
331 if (tstate == NULL)
332 Py_FatalError("PyEval_RestoreThread: NULL tstate");
333 #ifdef WITH_THREAD
334 if (interpreter_lock) {
335 int err = errno;
336 PyThread_acquire_lock(interpreter_lock, 1);
337 errno = err;
339 #endif
340 PyThreadState_Swap(tstate);
344 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
345 signal handlers or Mac I/O completion routines) can schedule calls
346 to a function to be called synchronously.
347 The synchronous function is called with one void* argument.
348 It should return 0 for success or -1 for failure -- failure should
349 be accompanied by an exception.
351 If registry succeeds, the registry function returns 0; if it fails
352 (e.g. due to too many pending calls) it returns -1 (without setting
353 an exception condition).
355 Note that because registry may occur from within signal handlers,
356 or other asynchronous events, calling malloc() is unsafe!
358 #ifdef WITH_THREAD
359 Any thread can schedule pending calls, but only the main thread
360 will execute them.
361 There is no facility to schedule calls to a particular thread, but
362 that should be easy to change, should that ever be required. In
363 that case, the static variables here should go into the python
364 threadstate.
365 #endif
368 #ifdef WITH_THREAD
370 /* The WITH_THREAD implementation is thread-safe. It allows
371 scheduling to be made from any thread, and even from an executing
372 callback.
375 #define NPENDINGCALLS 32
376 static struct {
377 int (*func)(void *);
378 void *arg;
379 } pendingcalls[NPENDINGCALLS];
380 static int pendingfirst = 0;
381 static int pendinglast = 0;
382 static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
383 static char pendingbusy = 0;
386 Py_AddPendingCall(int (*func)(void *), void *arg)
388 int i, j, result=0;
389 PyThread_type_lock lock = pending_lock;
391 /* try a few times for the lock. Since this mechanism is used
392 * for signal handling (on the main thread), there is a (slim)
393 * chance that a signal is delivered on the same thread while we
394 * hold the lock during the Py_MakePendingCalls() function.
395 * This avoids a deadlock in that case.
396 * Note that signals can be delivered on any thread. In particular,
397 * on Windows, a SIGINT is delivered on a system-created worker
398 * thread.
399 * We also check for lock being NULL, in the unlikely case that
400 * this function is called before any bytecode evaluation takes place.
402 if (lock != NULL) {
403 for (i = 0; i<100; i++) {
404 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
405 break;
407 if (i == 100)
408 return -1;
411 i = pendinglast;
412 j = (i + 1) % NPENDINGCALLS;
413 if (j == pendingfirst) {
414 result = -1; /* Queue full */
415 } else {
416 pendingcalls[i].func = func;
417 pendingcalls[i].arg = arg;
418 pendinglast = j;
420 /* signal main loop */
421 _Py_Ticker = 0;
422 pendingcalls_to_do = 1;
423 if (lock != NULL)
424 PyThread_release_lock(lock);
425 return result;
429 Py_MakePendingCalls(void)
431 int i;
432 int r = 0;
434 if (!pending_lock) {
435 /* initial allocation of the lock */
436 pending_lock = PyThread_allocate_lock();
437 if (pending_lock == NULL)
438 return -1;
441 /* only service pending calls on main thread */
442 if (main_thread && PyThread_get_thread_ident() != main_thread)
443 return 0;
444 /* don't perform recursive pending calls */
445 if (pendingbusy)
446 return 0;
447 pendingbusy = 1;
448 /* perform a bounded number of calls, in case of recursion */
449 for (i=0; i<NPENDINGCALLS; i++) {
450 int j;
451 int (*func)(void *);
452 void *arg;
454 /* pop one item off the queue while holding the lock */
455 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
456 j = pendingfirst;
457 if (j == pendinglast) {
458 func = NULL; /* Queue empty */
459 } else {
460 func = pendingcalls[j].func;
461 arg = pendingcalls[j].arg;
462 pendingfirst = (j + 1) % NPENDINGCALLS;
464 pendingcalls_to_do = pendingfirst != pendinglast;
465 PyThread_release_lock(pending_lock);
466 /* having released the lock, perform the callback */
467 if (func == NULL)
468 break;
469 r = func(arg);
470 if (r)
471 break;
473 pendingbusy = 0;
474 return r;
477 #else /* if ! defined WITH_THREAD */
480 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
481 This code is used for signal handling in python that isn't built
482 with WITH_THREAD.
483 Don't use this implementation when Py_AddPendingCalls() can happen
484 on a different thread!
486 There are two possible race conditions:
487 (1) nested asynchronous calls to Py_AddPendingCall()
488 (2) AddPendingCall() calls made while pending calls are being processed.
490 (1) is very unlikely because typically signal delivery
491 is blocked during signal handling. So it should be impossible.
492 (2) is a real possibility.
493 The current code is safe against (2), but not against (1).
494 The safety against (2) is derived from the fact that only one
495 thread is present, interrupted by signals, and that the critical
496 section is protected with the "busy" variable. On Windows, which
497 delivers SIGINT on a system thread, this does not hold and therefore
498 Windows really shouldn't use this version.
499 The two threads could theoretically wiggle around the "busy" variable.
502 #define NPENDINGCALLS 32
503 static struct {
504 int (*func)(void *);
505 void *arg;
506 } pendingcalls[NPENDINGCALLS];
507 static volatile int pendingfirst = 0;
508 static volatile int pendinglast = 0;
509 static volatile int pendingcalls_to_do = 0;
512 Py_AddPendingCall(int (*func)(void *), void *arg)
514 static volatile int busy = 0;
515 int i, j;
516 /* XXX Begin critical section */
517 if (busy)
518 return -1;
519 busy = 1;
520 i = pendinglast;
521 j = (i + 1) % NPENDINGCALLS;
522 if (j == pendingfirst) {
523 busy = 0;
524 return -1; /* Queue full */
526 pendingcalls[i].func = func;
527 pendingcalls[i].arg = arg;
528 pendinglast = j;
530 _Py_Ticker = 0;
531 pendingcalls_to_do = 1; /* Signal main loop */
532 busy = 0;
533 /* XXX End critical section */
534 return 0;
538 Py_MakePendingCalls(void)
540 static int busy = 0;
541 if (busy)
542 return 0;
543 busy = 1;
544 pendingcalls_to_do = 0;
545 for (;;) {
546 int i;
547 int (*func)(void *);
548 void *arg;
549 i = pendingfirst;
550 if (i == pendinglast)
551 break; /* Queue empty */
552 func = pendingcalls[i].func;
553 arg = pendingcalls[i].arg;
554 pendingfirst = (i + 1) % NPENDINGCALLS;
555 if (func(arg) < 0) {
556 busy = 0;
557 pendingcalls_to_do = 1; /* We're not done yet */
558 return -1;
561 busy = 0;
562 return 0;
565 #endif /* WITH_THREAD */
568 /* The interpreter's recursion limit */
570 #ifndef Py_DEFAULT_RECURSION_LIMIT
571 #define Py_DEFAULT_RECURSION_LIMIT 1000
572 #endif
573 static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
574 int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
577 Py_GetRecursionLimit(void)
579 return recursion_limit;
582 void
583 Py_SetRecursionLimit(int new_limit)
585 recursion_limit = new_limit;
586 _Py_CheckRecursionLimit = recursion_limit;
589 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
590 if the recursion_depth reaches _Py_CheckRecursionLimit.
591 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
592 to guarantee that _Py_CheckRecursiveCall() is regularly called.
593 Without USE_STACKCHECK, there is no need for this. */
595 _Py_CheckRecursiveCall(char *where)
597 PyThreadState *tstate = PyThreadState_GET();
599 #ifdef USE_STACKCHECK
600 if (PyOS_CheckStack()) {
601 --tstate->recursion_depth;
602 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
603 return -1;
605 #endif
606 if (tstate->recursion_depth > recursion_limit) {
607 --tstate->recursion_depth;
608 PyErr_Format(PyExc_RuntimeError,
609 "maximum recursion depth exceeded%s",
610 where);
611 return -1;
613 _Py_CheckRecursionLimit = recursion_limit;
614 return 0;
617 /* Status code for main loop (reason for stack unwind) */
618 enum why_code {
619 WHY_NOT = 0x0001, /* No error */
620 WHY_EXCEPTION = 0x0002, /* Exception occurred */
621 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
622 WHY_RETURN = 0x0008, /* 'return' statement */
623 WHY_BREAK = 0x0010, /* 'break' statement */
624 WHY_CONTINUE = 0x0020, /* 'continue' statement */
625 WHY_YIELD = 0x0040 /* 'yield' operator */
628 static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
629 static int unpack_iterable(PyObject *, int, PyObject **);
631 /* Records whether tracing is on for any thread. Counts the number of
632 threads for which tstate->c_tracefunc is non-NULL, so if the value
633 is 0, we know we don't have to check this thread's c_tracefunc.
634 This speeds up the if statement in PyEval_EvalFrameEx() after
635 fast_next_opcode*/
636 static int _Py_TracingPossible = 0;
638 /* for manipulating the thread switch and periodic "stuff" - used to be
639 per thread, now just a pair o' globals */
640 int _Py_CheckInterval = 100;
641 volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
643 PyObject *
644 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
646 return PyEval_EvalCodeEx(co,
647 globals, locals,
648 (PyObject **)NULL, 0,
649 (PyObject **)NULL, 0,
650 (PyObject **)NULL, 0,
651 NULL);
655 /* Interpreter main loop */
657 PyObject *
658 PyEval_EvalFrame(PyFrameObject *f) {
659 /* This is for backward compatibility with extension modules that
660 used this API; core interpreter code should call
661 PyEval_EvalFrameEx() */
662 return PyEval_EvalFrameEx(f, 0);
665 PyObject *
666 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
668 #ifdef DXPAIRS
669 int lastopcode = 0;
670 #endif
671 register PyObject **stack_pointer; /* Next free slot in value stack */
672 register unsigned char *next_instr;
673 register int opcode; /* Current opcode */
674 register int oparg; /* Current opcode argument, if any */
675 register enum why_code why; /* Reason for block stack unwind */
676 register int err; /* Error status -- nonzero if error */
677 register PyObject *x; /* Result object -- NULL if error */
678 register PyObject *v; /* Temporary objects popped off stack */
679 register PyObject *w;
680 register PyObject *u;
681 register PyObject *t;
682 register PyObject *stream = NULL; /* for PRINT opcodes */
683 register PyObject **fastlocals, **freevars;
684 PyObject *retval = NULL; /* Return value */
685 PyThreadState *tstate = PyThreadState_GET();
686 PyCodeObject *co;
688 /* when tracing we set things up so that
690 not (instr_lb <= current_bytecode_offset < instr_ub)
692 is true when the line being executed has changed. The
693 initial values are such as to make this false the first
694 time it is tested. */
695 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
697 unsigned char *first_instr;
698 PyObject *names;
699 PyObject *consts;
700 #if defined(Py_DEBUG) || defined(LLTRACE)
701 /* Make it easier to find out where we are with a debugger */
702 char *filename;
703 #endif
705 /* Tuple access macros */
707 #ifndef Py_DEBUG
708 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
709 #else
710 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
711 #endif
713 #ifdef WITH_TSC
714 /* Use Pentium timestamp counter to mark certain events:
715 inst0 -- beginning of switch statement for opcode dispatch
716 inst1 -- end of switch statement (may be skipped)
717 loop0 -- the top of the mainloop
718 loop1 -- place where control returns again to top of mainloop
719 (may be skipped)
720 intr1 -- beginning of long interruption
721 intr2 -- end of long interruption
723 Many opcodes call out to helper C functions. In some cases, the
724 time in those functions should be counted towards the time for the
725 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
726 calls another Python function; there's no point in charge all the
727 bytecode executed by the called function to the caller.
729 It's hard to make a useful judgement statically. In the presence
730 of operator overloading, it's impossible to tell if a call will
731 execute new Python code or not.
733 It's a case-by-case judgement. I'll use intr1 for the following
734 cases:
736 EXEC_STMT
737 IMPORT_STAR
738 IMPORT_FROM
739 CALL_FUNCTION (and friends)
742 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
743 int ticked = 0;
745 READ_TIMESTAMP(inst0);
746 READ_TIMESTAMP(inst1);
747 READ_TIMESTAMP(loop0);
748 READ_TIMESTAMP(loop1);
750 /* shut up the compiler */
751 opcode = 0;
752 #endif
754 /* Code access macros */
756 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
757 #define NEXTOP() (*next_instr++)
758 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
759 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
760 #define JUMPTO(x) (next_instr = first_instr + (x))
761 #define JUMPBY(x) (next_instr += (x))
763 /* OpCode prediction macros
764 Some opcodes tend to come in pairs thus making it possible to
765 predict the second code when the first is run. For example,
766 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
767 those opcodes are often followed by a POP_TOP.
769 Verifying the prediction costs a single high-speed test of a register
770 variable against a constant. If the pairing was good, then the
771 processor's own internal branch predication has a high likelihood of
772 success, resulting in a nearly zero-overhead transition to the
773 next opcode. A successful prediction saves a trip through the eval-loop
774 including its two unpredictable branches, the HAS_ARG test and the
775 switch-case. Combined with the processor's internal branch prediction,
776 a successful PREDICT has the effect of making the two opcodes run as if
777 they were a single new opcode with the bodies combined.
779 If collecting opcode statistics, your choices are to either keep the
780 predictions turned-on and interpret the results as if some opcodes
781 had been combined or turn-off predictions so that the opcode frequency
782 counter updates for both opcodes.
785 #ifdef DYNAMIC_EXECUTION_PROFILE
786 #define PREDICT(op) if (0) goto PRED_##op
787 #else
788 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
789 #endif
791 #define PREDICTED(op) PRED_##op: next_instr++
792 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
794 /* Stack manipulation macros */
796 /* The stack can grow at most MAXINT deep, as co_nlocals and
797 co_stacksize are ints. */
798 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
799 #define EMPTY() (STACK_LEVEL() == 0)
800 #define TOP() (stack_pointer[-1])
801 #define SECOND() (stack_pointer[-2])
802 #define THIRD() (stack_pointer[-3])
803 #define FOURTH() (stack_pointer[-4])
804 #define SET_TOP(v) (stack_pointer[-1] = (v))
805 #define SET_SECOND(v) (stack_pointer[-2] = (v))
806 #define SET_THIRD(v) (stack_pointer[-3] = (v))
807 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
808 #define BASIC_STACKADJ(n) (stack_pointer += n)
809 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
810 #define BASIC_POP() (*--stack_pointer)
812 #ifdef LLTRACE
813 #define PUSH(v) { (void)(BASIC_PUSH(v), \
814 lltrace && prtrace(TOP(), "push")); \
815 assert(STACK_LEVEL() <= co->co_stacksize); }
816 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
817 BASIC_POP())
818 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
819 lltrace && prtrace(TOP(), "stackadj")); \
820 assert(STACK_LEVEL() <= co->co_stacksize); }
821 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
822 prtrace((STACK_POINTER)[-1], "ext_pop")), \
823 *--(STACK_POINTER))
824 #else
825 #define PUSH(v) BASIC_PUSH(v)
826 #define POP() BASIC_POP()
827 #define STACKADJ(n) BASIC_STACKADJ(n)
828 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
829 #endif
831 /* Local variable macros */
833 #define GETLOCAL(i) (fastlocals[i])
835 /* The SETLOCAL() macro must not DECREF the local variable in-place and
836 then store the new value; it must copy the old value to a temporary
837 value, then store the new value, and then DECREF the temporary value.
838 This is because it is possible that during the DECREF the frame is
839 accessed by other code (e.g. a __del__ method or gc.collect()) and the
840 variable would be pointing to already-freed memory. */
841 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
842 GETLOCAL(i) = value; \
843 Py_XDECREF(tmp); } while (0)
845 /* Start of code */
847 if (f == NULL)
848 return NULL;
850 /* push frame */
851 if (Py_EnterRecursiveCall(""))
852 return NULL;
854 tstate->frame = f;
856 if (tstate->use_tracing) {
857 if (tstate->c_tracefunc != NULL) {
858 /* tstate->c_tracefunc, if defined, is a
859 function that will be called on *every* entry
860 to a code block. Its return value, if not
861 None, is a function that will be called at
862 the start of each executed line of code.
863 (Actually, the function must return itself
864 in order to continue tracing.) The trace
865 functions are called with three arguments:
866 a pointer to the current frame, a string
867 indicating why the function is called, and
868 an argument which depends on the situation.
869 The global trace function is also called
870 whenever an exception is detected. */
871 if (call_trace_protected(tstate->c_tracefunc,
872 tstate->c_traceobj,
873 f, PyTrace_CALL, Py_None)) {
874 /* Trace function raised an error */
875 goto exit_eval_frame;
878 if (tstate->c_profilefunc != NULL) {
879 /* Similar for c_profilefunc, except it needn't
880 return itself and isn't called for "line" events */
881 if (call_trace_protected(tstate->c_profilefunc,
882 tstate->c_profileobj,
883 f, PyTrace_CALL, Py_None)) {
884 /* Profile function raised an error */
885 goto exit_eval_frame;
890 co = f->f_code;
891 names = co->co_names;
892 consts = co->co_consts;
893 fastlocals = f->f_localsplus;
894 freevars = f->f_localsplus + co->co_nlocals;
895 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
896 /* An explanation is in order for the next line.
898 f->f_lasti now refers to the index of the last instruction
899 executed. You might think this was obvious from the name, but
900 this wasn't always true before 2.3! PyFrame_New now sets
901 f->f_lasti to -1 (i.e. the index *before* the first instruction)
902 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
903 does work. Promise.
905 When the PREDICT() macros are enabled, some opcode pairs follow in
906 direct succession without updating f->f_lasti. A successful
907 prediction effectively links the two codes together as if they
908 were a single new opcode; accordingly,f->f_lasti will point to
909 the first code in the pair (for instance, GET_ITER followed by
910 FOR_ITER is effectively a single opcode and f->f_lasti will point
911 at to the beginning of the combined pair.)
913 next_instr = first_instr + f->f_lasti + 1;
914 stack_pointer = f->f_stacktop;
915 assert(stack_pointer != NULL);
916 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
918 #ifdef LLTRACE
919 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
920 #endif
921 #if defined(Py_DEBUG) || defined(LLTRACE)
922 filename = PyString_AsString(co->co_filename);
923 #endif
925 why = WHY_NOT;
926 err = 0;
927 x = Py_None; /* Not a reference, just anything non-NULL */
928 w = NULL;
930 if (throwflag) { /* support for generator.throw() */
931 why = WHY_EXCEPTION;
932 goto on_error;
935 for (;;) {
936 #ifdef WITH_TSC
937 if (inst1 == 0) {
938 /* Almost surely, the opcode executed a break
939 or a continue, preventing inst1 from being set
940 on the way out of the loop.
942 READ_TIMESTAMP(inst1);
943 loop1 = inst1;
945 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
946 intr0, intr1);
947 ticked = 0;
948 inst1 = 0;
949 intr0 = 0;
950 intr1 = 0;
951 READ_TIMESTAMP(loop0);
952 #endif
953 assert(stack_pointer >= f->f_valuestack); /* else underflow */
954 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
956 /* Do periodic things. Doing this every time through
957 the loop would add too much overhead, so we do it
958 only every Nth instruction. We also do it if
959 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
960 event needs attention (e.g. a signal handler or
961 async I/O handler); see Py_AddPendingCall() and
962 Py_MakePendingCalls() above. */
964 if (--_Py_Ticker < 0) {
965 if (*next_instr == SETUP_FINALLY) {
966 /* Make the last opcode before
967 a try: finally: block uninterruptable. */
968 goto fast_next_opcode;
970 _Py_Ticker = _Py_CheckInterval;
971 tstate->tick_counter++;
972 #ifdef WITH_TSC
973 ticked = 1;
974 #endif
975 if (pendingcalls_to_do) {
976 if (Py_MakePendingCalls() < 0) {
977 why = WHY_EXCEPTION;
978 goto on_error;
980 if (pendingcalls_to_do)
981 /* MakePendingCalls() didn't succeed.
982 Force early re-execution of this
983 "periodic" code, possibly after
984 a thread switch */
985 _Py_Ticker = 0;
987 #ifdef WITH_THREAD
988 if (interpreter_lock) {
989 /* Give another thread a chance */
991 if (PyThreadState_Swap(NULL) != tstate)
992 Py_FatalError("ceval: tstate mix-up");
993 PyThread_release_lock(interpreter_lock);
995 /* Other threads may run now */
997 PyThread_acquire_lock(interpreter_lock, 1);
998 if (PyThreadState_Swap(tstate) != NULL)
999 Py_FatalError("ceval: orphan tstate");
1001 /* Check for thread interrupts */
1003 if (tstate->async_exc != NULL) {
1004 x = tstate->async_exc;
1005 tstate->async_exc = NULL;
1006 PyErr_SetNone(x);
1007 Py_DECREF(x);
1008 why = WHY_EXCEPTION;
1009 goto on_error;
1012 #endif
1015 fast_next_opcode:
1016 f->f_lasti = INSTR_OFFSET();
1018 /* line-by-line tracing support */
1020 if (_Py_TracingPossible &&
1021 tstate->c_tracefunc != NULL && !tstate->tracing) {
1022 /* see maybe_call_line_trace
1023 for expository comments */
1024 f->f_stacktop = stack_pointer;
1026 err = maybe_call_line_trace(tstate->c_tracefunc,
1027 tstate->c_traceobj,
1028 f, &instr_lb, &instr_ub,
1029 &instr_prev);
1030 /* Reload possibly changed frame fields */
1031 JUMPTO(f->f_lasti);
1032 if (f->f_stacktop != NULL) {
1033 stack_pointer = f->f_stacktop;
1034 f->f_stacktop = NULL;
1036 if (err) {
1037 /* trace function raised an exception */
1038 goto on_error;
1042 /* Extract opcode and argument */
1044 opcode = NEXTOP();
1045 oparg = 0; /* allows oparg to be stored in a register because
1046 it doesn't have to be remembered across a full loop */
1047 if (HAS_ARG(opcode))
1048 oparg = NEXTARG();
1049 dispatch_opcode:
1050 #ifdef DYNAMIC_EXECUTION_PROFILE
1051 #ifdef DXPAIRS
1052 dxpairs[lastopcode][opcode]++;
1053 lastopcode = opcode;
1054 #endif
1055 dxp[opcode]++;
1056 #endif
1058 #ifdef LLTRACE
1059 /* Instruction tracing */
1061 if (lltrace) {
1062 if (HAS_ARG(opcode)) {
1063 printf("%d: %d, %d\n",
1064 f->f_lasti, opcode, oparg);
1066 else {
1067 printf("%d: %d\n",
1068 f->f_lasti, opcode);
1071 #endif
1073 /* Main switch on opcode */
1074 READ_TIMESTAMP(inst0);
1076 switch (opcode) {
1078 /* BEWARE!
1079 It is essential that any operation that fails sets either
1080 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1081 and that no operation that succeeds does this! */
1083 /* case STOP_CODE: this is an error! */
1085 case NOP:
1086 goto fast_next_opcode;
1088 case LOAD_FAST:
1089 x = GETLOCAL(oparg);
1090 if (x != NULL) {
1091 Py_INCREF(x);
1092 PUSH(x);
1093 goto fast_next_opcode;
1095 format_exc_check_arg(PyExc_UnboundLocalError,
1096 UNBOUNDLOCAL_ERROR_MSG,
1097 PyTuple_GetItem(co->co_varnames, oparg));
1098 break;
1100 case LOAD_CONST:
1101 x = GETITEM(consts, oparg);
1102 Py_INCREF(x);
1103 PUSH(x);
1104 goto fast_next_opcode;
1106 PREDICTED_WITH_ARG(STORE_FAST);
1107 case STORE_FAST:
1108 v = POP();
1109 SETLOCAL(oparg, v);
1110 goto fast_next_opcode;
1112 PREDICTED(POP_TOP);
1113 case POP_TOP:
1114 v = POP();
1115 Py_DECREF(v);
1116 goto fast_next_opcode;
1118 case ROT_TWO:
1119 v = TOP();
1120 w = SECOND();
1121 SET_TOP(w);
1122 SET_SECOND(v);
1123 goto fast_next_opcode;
1125 case ROT_THREE:
1126 v = TOP();
1127 w = SECOND();
1128 x = THIRD();
1129 SET_TOP(w);
1130 SET_SECOND(x);
1131 SET_THIRD(v);
1132 goto fast_next_opcode;
1134 case ROT_FOUR:
1135 u = TOP();
1136 v = SECOND();
1137 w = THIRD();
1138 x = FOURTH();
1139 SET_TOP(v);
1140 SET_SECOND(w);
1141 SET_THIRD(x);
1142 SET_FOURTH(u);
1143 goto fast_next_opcode;
1145 case DUP_TOP:
1146 v = TOP();
1147 Py_INCREF(v);
1148 PUSH(v);
1149 goto fast_next_opcode;
1151 case DUP_TOPX:
1152 if (oparg == 2) {
1153 x = TOP();
1154 Py_INCREF(x);
1155 w = SECOND();
1156 Py_INCREF(w);
1157 STACKADJ(2);
1158 SET_TOP(x);
1159 SET_SECOND(w);
1160 goto fast_next_opcode;
1161 } else if (oparg == 3) {
1162 x = TOP();
1163 Py_INCREF(x);
1164 w = SECOND();
1165 Py_INCREF(w);
1166 v = THIRD();
1167 Py_INCREF(v);
1168 STACKADJ(3);
1169 SET_TOP(x);
1170 SET_SECOND(w);
1171 SET_THIRD(v);
1172 goto fast_next_opcode;
1174 Py_FatalError("invalid argument to DUP_TOPX"
1175 " (bytecode corruption?)");
1176 /* Never returns, so don't bother to set why. */
1177 break;
1179 case UNARY_POSITIVE:
1180 v = TOP();
1181 x = PyNumber_Positive(v);
1182 Py_DECREF(v);
1183 SET_TOP(x);
1184 if (x != NULL) continue;
1185 break;
1187 case UNARY_NEGATIVE:
1188 v = TOP();
1189 x = PyNumber_Negative(v);
1190 Py_DECREF(v);
1191 SET_TOP(x);
1192 if (x != NULL) continue;
1193 break;
1195 case UNARY_NOT:
1196 v = TOP();
1197 err = PyObject_IsTrue(v);
1198 Py_DECREF(v);
1199 if (err == 0) {
1200 Py_INCREF(Py_True);
1201 SET_TOP(Py_True);
1202 continue;
1204 else if (err > 0) {
1205 Py_INCREF(Py_False);
1206 SET_TOP(Py_False);
1207 err = 0;
1208 continue;
1210 STACKADJ(-1);
1211 break;
1213 case UNARY_CONVERT:
1214 v = TOP();
1215 x = PyObject_Repr(v);
1216 Py_DECREF(v);
1217 SET_TOP(x);
1218 if (x != NULL) continue;
1219 break;
1221 case UNARY_INVERT:
1222 v = TOP();
1223 x = PyNumber_Invert(v);
1224 Py_DECREF(v);
1225 SET_TOP(x);
1226 if (x != NULL) continue;
1227 break;
1229 case BINARY_POWER:
1230 w = POP();
1231 v = TOP();
1232 x = PyNumber_Power(v, w, Py_None);
1233 Py_DECREF(v);
1234 Py_DECREF(w);
1235 SET_TOP(x);
1236 if (x != NULL) continue;
1237 break;
1239 case BINARY_MULTIPLY:
1240 w = POP();
1241 v = TOP();
1242 x = PyNumber_Multiply(v, w);
1243 Py_DECREF(v);
1244 Py_DECREF(w);
1245 SET_TOP(x);
1246 if (x != NULL) continue;
1247 break;
1249 case BINARY_DIVIDE:
1250 if (!_Py_QnewFlag) {
1251 w = POP();
1252 v = TOP();
1253 x = PyNumber_Divide(v, w);
1254 Py_DECREF(v);
1255 Py_DECREF(w);
1256 SET_TOP(x);
1257 if (x != NULL) continue;
1258 break;
1260 /* -Qnew is in effect: fall through to
1261 BINARY_TRUE_DIVIDE */
1262 case BINARY_TRUE_DIVIDE:
1263 w = POP();
1264 v = TOP();
1265 x = PyNumber_TrueDivide(v, w);
1266 Py_DECREF(v);
1267 Py_DECREF(w);
1268 SET_TOP(x);
1269 if (x != NULL) continue;
1270 break;
1272 case BINARY_FLOOR_DIVIDE:
1273 w = POP();
1274 v = TOP();
1275 x = PyNumber_FloorDivide(v, w);
1276 Py_DECREF(v);
1277 Py_DECREF(w);
1278 SET_TOP(x);
1279 if (x != NULL) continue;
1280 break;
1282 case BINARY_MODULO:
1283 w = POP();
1284 v = TOP();
1285 x = PyNumber_Remainder(v, w);
1286 Py_DECREF(v);
1287 Py_DECREF(w);
1288 SET_TOP(x);
1289 if (x != NULL) continue;
1290 break;
1292 case BINARY_ADD:
1293 w = POP();
1294 v = TOP();
1295 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1296 /* INLINE: int + int */
1297 register long a, b, i;
1298 a = PyInt_AS_LONG(v);
1299 b = PyInt_AS_LONG(w);
1300 i = a + b;
1301 if ((i^a) < 0 && (i^b) < 0)
1302 goto slow_add;
1303 x = PyInt_FromLong(i);
1305 else if (PyString_CheckExact(v) &&
1306 PyString_CheckExact(w)) {
1307 x = string_concatenate(v, w, f, next_instr);
1308 /* string_concatenate consumed the ref to v */
1309 goto skip_decref_vx;
1311 else {
1312 slow_add:
1313 x = PyNumber_Add(v, w);
1315 Py_DECREF(v);
1316 skip_decref_vx:
1317 Py_DECREF(w);
1318 SET_TOP(x);
1319 if (x != NULL) continue;
1320 break;
1322 case BINARY_SUBTRACT:
1323 w = POP();
1324 v = TOP();
1325 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1326 /* INLINE: int - int */
1327 register long a, b, i;
1328 a = PyInt_AS_LONG(v);
1329 b = PyInt_AS_LONG(w);
1330 i = a - b;
1331 if ((i^a) < 0 && (i^~b) < 0)
1332 goto slow_sub;
1333 x = PyInt_FromLong(i);
1335 else {
1336 slow_sub:
1337 x = PyNumber_Subtract(v, w);
1339 Py_DECREF(v);
1340 Py_DECREF(w);
1341 SET_TOP(x);
1342 if (x != NULL) continue;
1343 break;
1345 case BINARY_SUBSCR:
1346 w = POP();
1347 v = TOP();
1348 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1349 /* INLINE: list[int] */
1350 Py_ssize_t i = PyInt_AsSsize_t(w);
1351 if (i < 0)
1352 i += PyList_GET_SIZE(v);
1353 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1354 x = PyList_GET_ITEM(v, i);
1355 Py_INCREF(x);
1357 else
1358 goto slow_get;
1360 else
1361 slow_get:
1362 x = PyObject_GetItem(v, w);
1363 Py_DECREF(v);
1364 Py_DECREF(w);
1365 SET_TOP(x);
1366 if (x != NULL) continue;
1367 break;
1369 case BINARY_LSHIFT:
1370 w = POP();
1371 v = TOP();
1372 x = PyNumber_Lshift(v, w);
1373 Py_DECREF(v);
1374 Py_DECREF(w);
1375 SET_TOP(x);
1376 if (x != NULL) continue;
1377 break;
1379 case BINARY_RSHIFT:
1380 w = POP();
1381 v = TOP();
1382 x = PyNumber_Rshift(v, w);
1383 Py_DECREF(v);
1384 Py_DECREF(w);
1385 SET_TOP(x);
1386 if (x != NULL) continue;
1387 break;
1389 case BINARY_AND:
1390 w = POP();
1391 v = TOP();
1392 x = PyNumber_And(v, w);
1393 Py_DECREF(v);
1394 Py_DECREF(w);
1395 SET_TOP(x);
1396 if (x != NULL) continue;
1397 break;
1399 case BINARY_XOR:
1400 w = POP();
1401 v = TOP();
1402 x = PyNumber_Xor(v, w);
1403 Py_DECREF(v);
1404 Py_DECREF(w);
1405 SET_TOP(x);
1406 if (x != NULL) continue;
1407 break;
1409 case BINARY_OR:
1410 w = POP();
1411 v = TOP();
1412 x = PyNumber_Or(v, w);
1413 Py_DECREF(v);
1414 Py_DECREF(w);
1415 SET_TOP(x);
1416 if (x != NULL) continue;
1417 break;
1419 case LIST_APPEND:
1420 w = POP();
1421 v = stack_pointer[-oparg];
1422 err = PyList_Append(v, w);
1423 Py_DECREF(w);
1424 if (err == 0) {
1425 PREDICT(JUMP_ABSOLUTE);
1426 continue;
1428 break;
1430 case INPLACE_POWER:
1431 w = POP();
1432 v = TOP();
1433 x = PyNumber_InPlacePower(v, w, Py_None);
1434 Py_DECREF(v);
1435 Py_DECREF(w);
1436 SET_TOP(x);
1437 if (x != NULL) continue;
1438 break;
1440 case INPLACE_MULTIPLY:
1441 w = POP();
1442 v = TOP();
1443 x = PyNumber_InPlaceMultiply(v, w);
1444 Py_DECREF(v);
1445 Py_DECREF(w);
1446 SET_TOP(x);
1447 if (x != NULL) continue;
1448 break;
1450 case INPLACE_DIVIDE:
1451 if (!_Py_QnewFlag) {
1452 w = POP();
1453 v = TOP();
1454 x = PyNumber_InPlaceDivide(v, w);
1455 Py_DECREF(v);
1456 Py_DECREF(w);
1457 SET_TOP(x);
1458 if (x != NULL) continue;
1459 break;
1461 /* -Qnew is in effect: fall through to
1462 INPLACE_TRUE_DIVIDE */
1463 case INPLACE_TRUE_DIVIDE:
1464 w = POP();
1465 v = TOP();
1466 x = PyNumber_InPlaceTrueDivide(v, w);
1467 Py_DECREF(v);
1468 Py_DECREF(w);
1469 SET_TOP(x);
1470 if (x != NULL) continue;
1471 break;
1473 case INPLACE_FLOOR_DIVIDE:
1474 w = POP();
1475 v = TOP();
1476 x = PyNumber_InPlaceFloorDivide(v, w);
1477 Py_DECREF(v);
1478 Py_DECREF(w);
1479 SET_TOP(x);
1480 if (x != NULL) continue;
1481 break;
1483 case INPLACE_MODULO:
1484 w = POP();
1485 v = TOP();
1486 x = PyNumber_InPlaceRemainder(v, w);
1487 Py_DECREF(v);
1488 Py_DECREF(w);
1489 SET_TOP(x);
1490 if (x != NULL) continue;
1491 break;
1493 case INPLACE_ADD:
1494 w = POP();
1495 v = TOP();
1496 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1497 /* INLINE: int + int */
1498 register long a, b, i;
1499 a = PyInt_AS_LONG(v);
1500 b = PyInt_AS_LONG(w);
1501 i = a + b;
1502 if ((i^a) < 0 && (i^b) < 0)
1503 goto slow_iadd;
1504 x = PyInt_FromLong(i);
1506 else if (PyString_CheckExact(v) &&
1507 PyString_CheckExact(w)) {
1508 x = string_concatenate(v, w, f, next_instr);
1509 /* string_concatenate consumed the ref to v */
1510 goto skip_decref_v;
1512 else {
1513 slow_iadd:
1514 x = PyNumber_InPlaceAdd(v, w);
1516 Py_DECREF(v);
1517 skip_decref_v:
1518 Py_DECREF(w);
1519 SET_TOP(x);
1520 if (x != NULL) continue;
1521 break;
1523 case INPLACE_SUBTRACT:
1524 w = POP();
1525 v = TOP();
1526 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1527 /* INLINE: int - int */
1528 register long a, b, i;
1529 a = PyInt_AS_LONG(v);
1530 b = PyInt_AS_LONG(w);
1531 i = a - b;
1532 if ((i^a) < 0 && (i^~b) < 0)
1533 goto slow_isub;
1534 x = PyInt_FromLong(i);
1536 else {
1537 slow_isub:
1538 x = PyNumber_InPlaceSubtract(v, w);
1540 Py_DECREF(v);
1541 Py_DECREF(w);
1542 SET_TOP(x);
1543 if (x != NULL) continue;
1544 break;
1546 case INPLACE_LSHIFT:
1547 w = POP();
1548 v = TOP();
1549 x = PyNumber_InPlaceLshift(v, w);
1550 Py_DECREF(v);
1551 Py_DECREF(w);
1552 SET_TOP(x);
1553 if (x != NULL) continue;
1554 break;
1556 case INPLACE_RSHIFT:
1557 w = POP();
1558 v = TOP();
1559 x = PyNumber_InPlaceRshift(v, w);
1560 Py_DECREF(v);
1561 Py_DECREF(w);
1562 SET_TOP(x);
1563 if (x != NULL) continue;
1564 break;
1566 case INPLACE_AND:
1567 w = POP();
1568 v = TOP();
1569 x = PyNumber_InPlaceAnd(v, w);
1570 Py_DECREF(v);
1571 Py_DECREF(w);
1572 SET_TOP(x);
1573 if (x != NULL) continue;
1574 break;
1576 case INPLACE_XOR:
1577 w = POP();
1578 v = TOP();
1579 x = PyNumber_InPlaceXor(v, w);
1580 Py_DECREF(v);
1581 Py_DECREF(w);
1582 SET_TOP(x);
1583 if (x != NULL) continue;
1584 break;
1586 case INPLACE_OR:
1587 w = POP();
1588 v = TOP();
1589 x = PyNumber_InPlaceOr(v, w);
1590 Py_DECREF(v);
1591 Py_DECREF(w);
1592 SET_TOP(x);
1593 if (x != NULL) continue;
1594 break;
1596 case SLICE+0:
1597 case SLICE+1:
1598 case SLICE+2:
1599 case SLICE+3:
1600 if ((opcode-SLICE) & 2)
1601 w = POP();
1602 else
1603 w = NULL;
1604 if ((opcode-SLICE) & 1)
1605 v = POP();
1606 else
1607 v = NULL;
1608 u = TOP();
1609 x = apply_slice(u, v, w);
1610 Py_DECREF(u);
1611 Py_XDECREF(v);
1612 Py_XDECREF(w);
1613 SET_TOP(x);
1614 if (x != NULL) continue;
1615 break;
1617 case STORE_SLICE+0:
1618 case STORE_SLICE+1:
1619 case STORE_SLICE+2:
1620 case STORE_SLICE+3:
1621 if ((opcode-STORE_SLICE) & 2)
1622 w = POP();
1623 else
1624 w = NULL;
1625 if ((opcode-STORE_SLICE) & 1)
1626 v = POP();
1627 else
1628 v = NULL;
1629 u = POP();
1630 t = POP();
1631 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1632 Py_DECREF(t);
1633 Py_DECREF(u);
1634 Py_XDECREF(v);
1635 Py_XDECREF(w);
1636 if (err == 0) continue;
1637 break;
1639 case DELETE_SLICE+0:
1640 case DELETE_SLICE+1:
1641 case DELETE_SLICE+2:
1642 case DELETE_SLICE+3:
1643 if ((opcode-DELETE_SLICE) & 2)
1644 w = POP();
1645 else
1646 w = NULL;
1647 if ((opcode-DELETE_SLICE) & 1)
1648 v = POP();
1649 else
1650 v = NULL;
1651 u = POP();
1652 err = assign_slice(u, v, w, (PyObject *)NULL);
1653 /* del u[v:w] */
1654 Py_DECREF(u);
1655 Py_XDECREF(v);
1656 Py_XDECREF(w);
1657 if (err == 0) continue;
1658 break;
1660 case STORE_SUBSCR:
1661 w = TOP();
1662 v = SECOND();
1663 u = THIRD();
1664 STACKADJ(-3);
1665 /* v[w] = u */
1666 err = PyObject_SetItem(v, w, u);
1667 Py_DECREF(u);
1668 Py_DECREF(v);
1669 Py_DECREF(w);
1670 if (err == 0) continue;
1671 break;
1673 case DELETE_SUBSCR:
1674 w = TOP();
1675 v = SECOND();
1676 STACKADJ(-2);
1677 /* del v[w] */
1678 err = PyObject_DelItem(v, w);
1679 Py_DECREF(v);
1680 Py_DECREF(w);
1681 if (err == 0) continue;
1682 break;
1684 case PRINT_EXPR:
1685 v = POP();
1686 w = PySys_GetObject("displayhook");
1687 if (w == NULL) {
1688 PyErr_SetString(PyExc_RuntimeError,
1689 "lost sys.displayhook");
1690 err = -1;
1691 x = NULL;
1693 if (err == 0) {
1694 x = PyTuple_Pack(1, v);
1695 if (x == NULL)
1696 err = -1;
1698 if (err == 0) {
1699 w = PyEval_CallObject(w, x);
1700 Py_XDECREF(w);
1701 if (w == NULL)
1702 err = -1;
1704 Py_DECREF(v);
1705 Py_XDECREF(x);
1706 break;
1708 case PRINT_ITEM_TO:
1709 w = stream = POP();
1710 /* fall through to PRINT_ITEM */
1712 case PRINT_ITEM:
1713 v = POP();
1714 if (stream == NULL || stream == Py_None) {
1715 w = PySys_GetObject("stdout");
1716 if (w == NULL) {
1717 PyErr_SetString(PyExc_RuntimeError,
1718 "lost sys.stdout");
1719 err = -1;
1722 /* PyFile_SoftSpace() can exececute arbitrary code
1723 if sys.stdout is an instance with a __getattr__.
1724 If __getattr__ raises an exception, w will
1725 be freed, so we need to prevent that temporarily. */
1726 Py_XINCREF(w);
1727 if (w != NULL && PyFile_SoftSpace(w, 0))
1728 err = PyFile_WriteString(" ", w);
1729 if (err == 0)
1730 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1731 if (err == 0) {
1732 /* XXX move into writeobject() ? */
1733 if (PyString_Check(v)) {
1734 char *s = PyString_AS_STRING(v);
1735 Py_ssize_t len = PyString_GET_SIZE(v);
1736 if (len == 0 ||
1737 !isspace(Py_CHARMASK(s[len-1])) ||
1738 s[len-1] == ' ')
1739 PyFile_SoftSpace(w, 1);
1741 #ifdef Py_USING_UNICODE
1742 else if (PyUnicode_Check(v)) {
1743 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1744 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1745 if (len == 0 ||
1746 !Py_UNICODE_ISSPACE(s[len-1]) ||
1747 s[len-1] == ' ')
1748 PyFile_SoftSpace(w, 1);
1750 #endif
1751 else
1752 PyFile_SoftSpace(w, 1);
1754 Py_XDECREF(w);
1755 Py_DECREF(v);
1756 Py_XDECREF(stream);
1757 stream = NULL;
1758 if (err == 0)
1759 continue;
1760 break;
1762 case PRINT_NEWLINE_TO:
1763 w = stream = POP();
1764 /* fall through to PRINT_NEWLINE */
1766 case PRINT_NEWLINE:
1767 if (stream == NULL || stream == Py_None) {
1768 w = PySys_GetObject("stdout");
1769 if (w == NULL) {
1770 PyErr_SetString(PyExc_RuntimeError,
1771 "lost sys.stdout");
1772 why = WHY_EXCEPTION;
1775 if (w != NULL) {
1776 /* w.write() may replace sys.stdout, so we
1777 * have to keep our reference to it */
1778 Py_INCREF(w);
1779 err = PyFile_WriteString("\n", w);
1780 if (err == 0)
1781 PyFile_SoftSpace(w, 0);
1782 Py_DECREF(w);
1784 Py_XDECREF(stream);
1785 stream = NULL;
1786 break;
1789 #ifdef CASE_TOO_BIG
1790 default: switch (opcode) {
1791 #endif
1792 case RAISE_VARARGS:
1793 u = v = w = NULL;
1794 switch (oparg) {
1795 case 3:
1796 u = POP(); /* traceback */
1797 /* Fallthrough */
1798 case 2:
1799 v = POP(); /* value */
1800 /* Fallthrough */
1801 case 1:
1802 w = POP(); /* exc */
1803 case 0: /* Fallthrough */
1804 why = do_raise(w, v, u);
1805 break;
1806 default:
1807 PyErr_SetString(PyExc_SystemError,
1808 "bad RAISE_VARARGS oparg");
1809 why = WHY_EXCEPTION;
1810 break;
1812 break;
1814 case LOAD_LOCALS:
1815 if ((x = f->f_locals) != NULL) {
1816 Py_INCREF(x);
1817 PUSH(x);
1818 continue;
1820 PyErr_SetString(PyExc_SystemError, "no locals");
1821 break;
1823 case RETURN_VALUE:
1824 retval = POP();
1825 why = WHY_RETURN;
1826 goto fast_block_end;
1828 case YIELD_VALUE:
1829 retval = POP();
1830 f->f_stacktop = stack_pointer;
1831 why = WHY_YIELD;
1832 goto fast_yield;
1834 case EXEC_STMT:
1835 w = TOP();
1836 v = SECOND();
1837 u = THIRD();
1838 STACKADJ(-3);
1839 READ_TIMESTAMP(intr0);
1840 err = exec_statement(f, u, v, w);
1841 READ_TIMESTAMP(intr1);
1842 Py_DECREF(u);
1843 Py_DECREF(v);
1844 Py_DECREF(w);
1845 break;
1847 case POP_BLOCK:
1849 PyTryBlock *b = PyFrame_BlockPop(f);
1850 while (STACK_LEVEL() > b->b_level) {
1851 v = POP();
1852 Py_DECREF(v);
1855 continue;
1857 PREDICTED(END_FINALLY);
1858 case END_FINALLY:
1859 v = POP();
1860 if (PyInt_Check(v)) {
1861 why = (enum why_code) PyInt_AS_LONG(v);
1862 assert(why != WHY_YIELD);
1863 if (why == WHY_RETURN ||
1864 why == WHY_CONTINUE)
1865 retval = POP();
1867 else if (PyExceptionClass_Check(v) ||
1868 PyString_Check(v)) {
1869 w = POP();
1870 u = POP();
1871 PyErr_Restore(v, w, u);
1872 why = WHY_RERAISE;
1873 break;
1875 else if (v != Py_None) {
1876 PyErr_SetString(PyExc_SystemError,
1877 "'finally' pops bad exception");
1878 why = WHY_EXCEPTION;
1880 Py_DECREF(v);
1881 break;
1883 case BUILD_CLASS:
1884 u = TOP();
1885 v = SECOND();
1886 w = THIRD();
1887 STACKADJ(-2);
1888 x = build_class(u, v, w);
1889 SET_TOP(x);
1890 Py_DECREF(u);
1891 Py_DECREF(v);
1892 Py_DECREF(w);
1893 break;
1895 case STORE_NAME:
1896 w = GETITEM(names, oparg);
1897 v = POP();
1898 if ((x = f->f_locals) != NULL) {
1899 if (PyDict_CheckExact(x))
1900 err = PyDict_SetItem(x, w, v);
1901 else
1902 err = PyObject_SetItem(x, w, v);
1903 Py_DECREF(v);
1904 if (err == 0) continue;
1905 break;
1907 PyErr_Format(PyExc_SystemError,
1908 "no locals found when storing %s",
1909 PyObject_REPR(w));
1910 break;
1912 case DELETE_NAME:
1913 w = GETITEM(names, oparg);
1914 if ((x = f->f_locals) != NULL) {
1915 if ((err = PyObject_DelItem(x, w)) != 0)
1916 format_exc_check_arg(PyExc_NameError,
1917 NAME_ERROR_MSG,
1919 break;
1921 PyErr_Format(PyExc_SystemError,
1922 "no locals when deleting %s",
1923 PyObject_REPR(w));
1924 break;
1926 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1927 case UNPACK_SEQUENCE:
1928 v = POP();
1929 if (PyTuple_CheckExact(v) &&
1930 PyTuple_GET_SIZE(v) == oparg) {
1931 PyObject **items = \
1932 ((PyTupleObject *)v)->ob_item;
1933 while (oparg--) {
1934 w = items[oparg];
1935 Py_INCREF(w);
1936 PUSH(w);
1938 Py_DECREF(v);
1939 continue;
1940 } else if (PyList_CheckExact(v) &&
1941 PyList_GET_SIZE(v) == oparg) {
1942 PyObject **items = \
1943 ((PyListObject *)v)->ob_item;
1944 while (oparg--) {
1945 w = items[oparg];
1946 Py_INCREF(w);
1947 PUSH(w);
1949 } else if (unpack_iterable(v, oparg,
1950 stack_pointer + oparg)) {
1951 stack_pointer += oparg;
1952 } else {
1953 /* unpack_iterable() raised an exception */
1954 why = WHY_EXCEPTION;
1956 Py_DECREF(v);
1957 break;
1959 case STORE_ATTR:
1960 w = GETITEM(names, oparg);
1961 v = TOP();
1962 u = SECOND();
1963 STACKADJ(-2);
1964 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1965 Py_DECREF(v);
1966 Py_DECREF(u);
1967 if (err == 0) continue;
1968 break;
1970 case DELETE_ATTR:
1971 w = GETITEM(names, oparg);
1972 v = POP();
1973 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1974 /* del v.w */
1975 Py_DECREF(v);
1976 break;
1978 case STORE_GLOBAL:
1979 w = GETITEM(names, oparg);
1980 v = POP();
1981 err = PyDict_SetItem(f->f_globals, w, v);
1982 Py_DECREF(v);
1983 if (err == 0) continue;
1984 break;
1986 case DELETE_GLOBAL:
1987 w = GETITEM(names, oparg);
1988 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1989 format_exc_check_arg(
1990 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1991 break;
1993 case LOAD_NAME:
1994 w = GETITEM(names, oparg);
1995 if ((v = f->f_locals) == NULL) {
1996 PyErr_Format(PyExc_SystemError,
1997 "no locals when loading %s",
1998 PyObject_REPR(w));
1999 why = WHY_EXCEPTION;
2000 break;
2002 if (PyDict_CheckExact(v)) {
2003 x = PyDict_GetItem(v, w);
2004 Py_XINCREF(x);
2006 else {
2007 x = PyObject_GetItem(v, w);
2008 if (x == NULL && PyErr_Occurred()) {
2009 if (!PyErr_ExceptionMatches(
2010 PyExc_KeyError))
2011 break;
2012 PyErr_Clear();
2015 if (x == NULL) {
2016 x = PyDict_GetItem(f->f_globals, w);
2017 if (x == NULL) {
2018 x = PyDict_GetItem(f->f_builtins, w);
2019 if (x == NULL) {
2020 format_exc_check_arg(
2021 PyExc_NameError,
2022 NAME_ERROR_MSG, w);
2023 break;
2026 Py_INCREF(x);
2028 PUSH(x);
2029 continue;
2031 case LOAD_GLOBAL:
2032 w = GETITEM(names, oparg);
2033 if (PyString_CheckExact(w)) {
2034 /* Inline the PyDict_GetItem() calls.
2035 WARNING: this is an extreme speed hack.
2036 Do not try this at home. */
2037 long hash = ((PyStringObject *)w)->ob_shash;
2038 if (hash != -1) {
2039 PyDictObject *d;
2040 PyDictEntry *e;
2041 d = (PyDictObject *)(f->f_globals);
2042 e = d->ma_lookup(d, w, hash);
2043 if (e == NULL) {
2044 x = NULL;
2045 break;
2047 x = e->me_value;
2048 if (x != NULL) {
2049 Py_INCREF(x);
2050 PUSH(x);
2051 continue;
2053 d = (PyDictObject *)(f->f_builtins);
2054 e = d->ma_lookup(d, w, hash);
2055 if (e == NULL) {
2056 x = NULL;
2057 break;
2059 x = e->me_value;
2060 if (x != NULL) {
2061 Py_INCREF(x);
2062 PUSH(x);
2063 continue;
2065 goto load_global_error;
2068 /* This is the un-inlined version of the code above */
2069 x = PyDict_GetItem(f->f_globals, w);
2070 if (x == NULL) {
2071 x = PyDict_GetItem(f->f_builtins, w);
2072 if (x == NULL) {
2073 load_global_error:
2074 format_exc_check_arg(
2075 PyExc_NameError,
2076 GLOBAL_NAME_ERROR_MSG, w);
2077 break;
2080 Py_INCREF(x);
2081 PUSH(x);
2082 continue;
2084 case DELETE_FAST:
2085 x = GETLOCAL(oparg);
2086 if (x != NULL) {
2087 SETLOCAL(oparg, NULL);
2088 continue;
2090 format_exc_check_arg(
2091 PyExc_UnboundLocalError,
2092 UNBOUNDLOCAL_ERROR_MSG,
2093 PyTuple_GetItem(co->co_varnames, oparg)
2095 break;
2097 case LOAD_CLOSURE:
2098 x = freevars[oparg];
2099 Py_INCREF(x);
2100 PUSH(x);
2101 if (x != NULL) continue;
2102 break;
2104 case LOAD_DEREF:
2105 x = freevars[oparg];
2106 w = PyCell_Get(x);
2107 if (w != NULL) {
2108 PUSH(w);
2109 continue;
2111 err = -1;
2112 /* Don't stomp existing exception */
2113 if (PyErr_Occurred())
2114 break;
2115 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2116 v = PyTuple_GET_ITEM(co->co_cellvars,
2117 oparg);
2118 format_exc_check_arg(
2119 PyExc_UnboundLocalError,
2120 UNBOUNDLOCAL_ERROR_MSG,
2122 } else {
2123 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2124 PyTuple_GET_SIZE(co->co_cellvars));
2125 format_exc_check_arg(PyExc_NameError,
2126 UNBOUNDFREE_ERROR_MSG, v);
2128 break;
2130 case STORE_DEREF:
2131 w = POP();
2132 x = freevars[oparg];
2133 PyCell_Set(x, w);
2134 Py_DECREF(w);
2135 continue;
2137 case BUILD_TUPLE:
2138 x = PyTuple_New(oparg);
2139 if (x != NULL) {
2140 for (; --oparg >= 0;) {
2141 w = POP();
2142 PyTuple_SET_ITEM(x, oparg, w);
2144 PUSH(x);
2145 continue;
2147 break;
2149 case BUILD_LIST:
2150 x = PyList_New(oparg);
2151 if (x != NULL) {
2152 for (; --oparg >= 0;) {
2153 w = POP();
2154 PyList_SET_ITEM(x, oparg, w);
2156 PUSH(x);
2157 continue;
2159 break;
2161 case BUILD_MAP:
2162 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2163 PUSH(x);
2164 if (x != NULL) continue;
2165 break;
2167 case STORE_MAP:
2168 w = TOP(); /* key */
2169 u = SECOND(); /* value */
2170 v = THIRD(); /* dict */
2171 STACKADJ(-2);
2172 assert (PyDict_CheckExact(v));
2173 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2174 Py_DECREF(u);
2175 Py_DECREF(w);
2176 if (err == 0) continue;
2177 break;
2179 case LOAD_ATTR:
2180 w = GETITEM(names, oparg);
2181 v = TOP();
2182 x = PyObject_GetAttr(v, w);
2183 Py_DECREF(v);
2184 SET_TOP(x);
2185 if (x != NULL) continue;
2186 break;
2188 case COMPARE_OP:
2189 w = POP();
2190 v = TOP();
2191 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2192 /* INLINE: cmp(int, int) */
2193 register long a, b;
2194 register int res;
2195 a = PyInt_AS_LONG(v);
2196 b = PyInt_AS_LONG(w);
2197 switch (oparg) {
2198 case PyCmp_LT: res = a < b; break;
2199 case PyCmp_LE: res = a <= b; break;
2200 case PyCmp_EQ: res = a == b; break;
2201 case PyCmp_NE: res = a != b; break;
2202 case PyCmp_GT: res = a > b; break;
2203 case PyCmp_GE: res = a >= b; break;
2204 case PyCmp_IS: res = v == w; break;
2205 case PyCmp_IS_NOT: res = v != w; break;
2206 default: goto slow_compare;
2208 x = res ? Py_True : Py_False;
2209 Py_INCREF(x);
2211 else {
2212 slow_compare:
2213 x = cmp_outcome(oparg, v, w);
2215 Py_DECREF(v);
2216 Py_DECREF(w);
2217 SET_TOP(x);
2218 if (x == NULL) break;
2219 PREDICT(JUMP_IF_FALSE);
2220 PREDICT(JUMP_IF_TRUE);
2221 continue;
2223 case IMPORT_NAME:
2224 w = GETITEM(names, oparg);
2225 x = PyDict_GetItemString(f->f_builtins, "__import__");
2226 if (x == NULL) {
2227 PyErr_SetString(PyExc_ImportError,
2228 "__import__ not found");
2229 break;
2231 Py_INCREF(x);
2232 v = POP();
2233 u = TOP();
2234 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2235 w = PyTuple_Pack(5,
2237 f->f_globals,
2238 f->f_locals == NULL ?
2239 Py_None : f->f_locals,
2242 else
2243 w = PyTuple_Pack(4,
2245 f->f_globals,
2246 f->f_locals == NULL ?
2247 Py_None : f->f_locals,
2249 Py_DECREF(v);
2250 Py_DECREF(u);
2251 if (w == NULL) {
2252 u = POP();
2253 Py_DECREF(x);
2254 x = NULL;
2255 break;
2257 READ_TIMESTAMP(intr0);
2258 v = x;
2259 x = PyEval_CallObject(v, w);
2260 Py_DECREF(v);
2261 READ_TIMESTAMP(intr1);
2262 Py_DECREF(w);
2263 SET_TOP(x);
2264 if (x != NULL) continue;
2265 break;
2267 case IMPORT_STAR:
2268 v = POP();
2269 PyFrame_FastToLocals(f);
2270 if ((x = f->f_locals) == NULL) {
2271 PyErr_SetString(PyExc_SystemError,
2272 "no locals found during 'import *'");
2273 break;
2275 READ_TIMESTAMP(intr0);
2276 err = import_all_from(x, v);
2277 READ_TIMESTAMP(intr1);
2278 PyFrame_LocalsToFast(f, 0);
2279 Py_DECREF(v);
2280 if (err == 0) continue;
2281 break;
2283 case IMPORT_FROM:
2284 w = GETITEM(names, oparg);
2285 v = TOP();
2286 READ_TIMESTAMP(intr0);
2287 x = import_from(v, w);
2288 READ_TIMESTAMP(intr1);
2289 PUSH(x);
2290 if (x != NULL) continue;
2291 break;
2293 case JUMP_FORWARD:
2294 JUMPBY(oparg);
2295 goto fast_next_opcode;
2297 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
2298 case JUMP_IF_FALSE:
2299 w = TOP();
2300 if (w == Py_True) {
2301 PREDICT(POP_TOP);
2302 goto fast_next_opcode;
2304 if (w == Py_False) {
2305 JUMPBY(oparg);
2306 goto fast_next_opcode;
2308 err = PyObject_IsTrue(w);
2309 if (err > 0)
2310 err = 0;
2311 else if (err == 0)
2312 JUMPBY(oparg);
2313 else
2314 break;
2315 continue;
2317 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
2318 case JUMP_IF_TRUE:
2319 w = TOP();
2320 if (w == Py_False) {
2321 PREDICT(POP_TOP);
2322 goto fast_next_opcode;
2324 if (w == Py_True) {
2325 JUMPBY(oparg);
2326 goto fast_next_opcode;
2328 err = PyObject_IsTrue(w);
2329 if (err > 0) {
2330 err = 0;
2331 JUMPBY(oparg);
2333 else if (err == 0)
2335 else
2336 break;
2337 continue;
2339 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2340 case JUMP_ABSOLUTE:
2341 JUMPTO(oparg);
2342 #if FAST_LOOPS
2343 /* Enabling this path speeds-up all while and for-loops by bypassing
2344 the per-loop checks for signals. By default, this should be turned-off
2345 because it prevents detection of a control-break in tight loops like
2346 "while 1: pass". Compile with this option turned-on when you need
2347 the speed-up and do not need break checking inside tight loops (ones
2348 that contain only instructions ending with goto fast_next_opcode).
2350 goto fast_next_opcode;
2351 #else
2352 continue;
2353 #endif
2355 case GET_ITER:
2356 /* before: [obj]; after [getiter(obj)] */
2357 v = TOP();
2358 x = PyObject_GetIter(v);
2359 Py_DECREF(v);
2360 if (x != NULL) {
2361 SET_TOP(x);
2362 PREDICT(FOR_ITER);
2363 continue;
2365 STACKADJ(-1);
2366 break;
2368 PREDICTED_WITH_ARG(FOR_ITER);
2369 case FOR_ITER:
2370 /* before: [iter]; after: [iter, iter()] *or* [] */
2371 v = TOP();
2372 x = (*v->ob_type->tp_iternext)(v);
2373 if (x != NULL) {
2374 PUSH(x);
2375 PREDICT(STORE_FAST);
2376 PREDICT(UNPACK_SEQUENCE);
2377 continue;
2379 if (PyErr_Occurred()) {
2380 if (!PyErr_ExceptionMatches(
2381 PyExc_StopIteration))
2382 break;
2383 PyErr_Clear();
2385 /* iterator ended normally */
2386 x = v = POP();
2387 Py_DECREF(v);
2388 JUMPBY(oparg);
2389 continue;
2391 case BREAK_LOOP:
2392 why = WHY_BREAK;
2393 goto fast_block_end;
2395 case CONTINUE_LOOP:
2396 retval = PyInt_FromLong(oparg);
2397 if (!retval) {
2398 x = NULL;
2399 break;
2401 why = WHY_CONTINUE;
2402 goto fast_block_end;
2404 case SETUP_LOOP:
2405 case SETUP_EXCEPT:
2406 case SETUP_FINALLY:
2407 /* NOTE: If you add any new block-setup opcodes that
2408 are not try/except/finally handlers, you may need
2409 to update the PyGen_NeedsFinalizing() function.
2412 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2413 STACK_LEVEL());
2414 continue;
2416 case WITH_CLEANUP:
2418 /* At the top of the stack are 1-3 values indicating
2419 how/why we entered the finally clause:
2420 - TOP = None
2421 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2422 - TOP = WHY_*; no retval below it
2423 - (TOP, SECOND, THIRD) = exc_info()
2424 Below them is EXIT, the context.__exit__ bound method.
2425 In the last case, we must call
2426 EXIT(TOP, SECOND, THIRD)
2427 otherwise we must call
2428 EXIT(None, None, None)
2430 In all cases, we remove EXIT from the stack, leaving
2431 the rest in the same order.
2433 In addition, if the stack represents an exception,
2434 *and* the function call returns a 'true' value, we
2435 "zap" this information, to prevent END_FINALLY from
2436 re-raising the exception. (But non-local gotos
2437 should still be resumed.)
2440 PyObject *exit_func;
2442 u = POP();
2443 if (u == Py_None) {
2444 exit_func = TOP();
2445 SET_TOP(u);
2446 v = w = Py_None;
2448 else if (PyInt_Check(u)) {
2449 switch(PyInt_AS_LONG(u)) {
2450 case WHY_RETURN:
2451 case WHY_CONTINUE:
2452 /* Retval in TOP. */
2453 exit_func = SECOND();
2454 SET_SECOND(TOP());
2455 SET_TOP(u);
2456 break;
2457 default:
2458 exit_func = TOP();
2459 SET_TOP(u);
2460 break;
2462 u = v = w = Py_None;
2464 else {
2465 v = TOP();
2466 w = SECOND();
2467 exit_func = THIRD();
2468 SET_TOP(u);
2469 SET_SECOND(v);
2470 SET_THIRD(w);
2472 /* XXX Not the fastest way to call it... */
2473 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2474 NULL);
2475 Py_DECREF(exit_func);
2476 if (x == NULL)
2477 break; /* Go to error exit */
2479 if (u != Py_None)
2480 err = PyObject_IsTrue(x);
2481 else
2482 err = 0;
2483 Py_DECREF(x);
2485 if (err < 0)
2486 break; /* Go to error exit */
2487 else if (err > 0) {
2488 err = 0;
2489 /* There was an exception and a true return */
2490 STACKADJ(-2);
2491 Py_INCREF(Py_None);
2492 SET_TOP(Py_None);
2493 Py_DECREF(u);
2494 Py_DECREF(v);
2495 Py_DECREF(w);
2496 } else {
2497 /* The stack was rearranged to remove EXIT
2498 above. Let END_FINALLY do its thing */
2500 PREDICT(END_FINALLY);
2501 break;
2504 case CALL_FUNCTION:
2506 PyObject **sp;
2507 PCALL(PCALL_ALL);
2508 sp = stack_pointer;
2509 #ifdef WITH_TSC
2510 x = call_function(&sp, oparg, &intr0, &intr1);
2511 #else
2512 x = call_function(&sp, oparg);
2513 #endif
2514 stack_pointer = sp;
2515 PUSH(x);
2516 if (x != NULL)
2517 continue;
2518 break;
2521 case CALL_FUNCTION_VAR:
2522 case CALL_FUNCTION_KW:
2523 case CALL_FUNCTION_VAR_KW:
2525 int na = oparg & 0xff;
2526 int nk = (oparg>>8) & 0xff;
2527 int flags = (opcode - CALL_FUNCTION) & 3;
2528 int n = na + 2 * nk;
2529 PyObject **pfunc, *func, **sp;
2530 PCALL(PCALL_ALL);
2531 if (flags & CALL_FLAG_VAR)
2532 n++;
2533 if (flags & CALL_FLAG_KW)
2534 n++;
2535 pfunc = stack_pointer - n - 1;
2536 func = *pfunc;
2538 if (PyMethod_Check(func)
2539 && PyMethod_GET_SELF(func) != NULL) {
2540 PyObject *self = PyMethod_GET_SELF(func);
2541 Py_INCREF(self);
2542 func = PyMethod_GET_FUNCTION(func);
2543 Py_INCREF(func);
2544 Py_DECREF(*pfunc);
2545 *pfunc = self;
2546 na++;
2547 n++;
2548 } else
2549 Py_INCREF(func);
2550 sp = stack_pointer;
2551 READ_TIMESTAMP(intr0);
2552 x = ext_do_call(func, &sp, flags, na, nk);
2553 READ_TIMESTAMP(intr1);
2554 stack_pointer = sp;
2555 Py_DECREF(func);
2557 while (stack_pointer > pfunc) {
2558 w = POP();
2559 Py_DECREF(w);
2561 PUSH(x);
2562 if (x != NULL)
2563 continue;
2564 break;
2567 case MAKE_FUNCTION:
2568 v = POP(); /* code object */
2569 x = PyFunction_New(v, f->f_globals);
2570 Py_DECREF(v);
2571 /* XXX Maybe this should be a separate opcode? */
2572 if (x != NULL && oparg > 0) {
2573 v = PyTuple_New(oparg);
2574 if (v == NULL) {
2575 Py_DECREF(x);
2576 x = NULL;
2577 break;
2579 while (--oparg >= 0) {
2580 w = POP();
2581 PyTuple_SET_ITEM(v, oparg, w);
2583 err = PyFunction_SetDefaults(x, v);
2584 Py_DECREF(v);
2586 PUSH(x);
2587 break;
2589 case MAKE_CLOSURE:
2591 v = POP(); /* code object */
2592 x = PyFunction_New(v, f->f_globals);
2593 Py_DECREF(v);
2594 if (x != NULL) {
2595 v = POP();
2596 if (PyFunction_SetClosure(x, v) != 0) {
2597 /* Can't happen unless bytecode is corrupt. */
2598 why = WHY_EXCEPTION;
2600 Py_DECREF(v);
2602 if (x != NULL && oparg > 0) {
2603 v = PyTuple_New(oparg);
2604 if (v == NULL) {
2605 Py_DECREF(x);
2606 x = NULL;
2607 break;
2609 while (--oparg >= 0) {
2610 w = POP();
2611 PyTuple_SET_ITEM(v, oparg, w);
2613 if (PyFunction_SetDefaults(x, v) != 0) {
2614 /* Can't happen unless
2615 PyFunction_SetDefaults changes. */
2616 why = WHY_EXCEPTION;
2618 Py_DECREF(v);
2620 PUSH(x);
2621 break;
2624 case BUILD_SLICE:
2625 if (oparg == 3)
2626 w = POP();
2627 else
2628 w = NULL;
2629 v = POP();
2630 u = TOP();
2631 x = PySlice_New(u, v, w);
2632 Py_DECREF(u);
2633 Py_DECREF(v);
2634 Py_XDECREF(w);
2635 SET_TOP(x);
2636 if (x != NULL) continue;
2637 break;
2639 case EXTENDED_ARG:
2640 opcode = NEXTOP();
2641 oparg = oparg<<16 | NEXTARG();
2642 goto dispatch_opcode;
2644 default:
2645 fprintf(stderr,
2646 "XXX lineno: %d, opcode: %d\n",
2647 PyCode_Addr2Line(f->f_code, f->f_lasti),
2648 opcode);
2649 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2650 why = WHY_EXCEPTION;
2651 break;
2653 #ifdef CASE_TOO_BIG
2655 #endif
2657 } /* switch */
2659 on_error:
2661 READ_TIMESTAMP(inst1);
2663 /* Quickly continue if no error occurred */
2665 if (why == WHY_NOT) {
2666 if (err == 0 && x != NULL) {
2667 #ifdef CHECKEXC
2668 /* This check is expensive! */
2669 if (PyErr_Occurred())
2670 fprintf(stderr,
2671 "XXX undetected error\n");
2672 else {
2673 #endif
2674 READ_TIMESTAMP(loop1);
2675 continue; /* Normal, fast path */
2676 #ifdef CHECKEXC
2678 #endif
2680 why = WHY_EXCEPTION;
2681 x = Py_None;
2682 err = 0;
2685 /* Double-check exception status */
2687 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2688 if (!PyErr_Occurred()) {
2689 PyErr_SetString(PyExc_SystemError,
2690 "error return without exception set");
2691 why = WHY_EXCEPTION;
2694 #ifdef CHECKEXC
2695 else {
2696 /* This check is expensive! */
2697 if (PyErr_Occurred()) {
2698 char buf[128];
2699 sprintf(buf, "Stack unwind with exception "
2700 "set and why=%d", why);
2701 Py_FatalError(buf);
2704 #endif
2706 /* Log traceback info if this is a real exception */
2708 if (why == WHY_EXCEPTION) {
2709 PyTraceBack_Here(f);
2711 if (tstate->c_tracefunc != NULL)
2712 call_exc_trace(tstate->c_tracefunc,
2713 tstate->c_traceobj, f);
2716 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2718 if (why == WHY_RERAISE)
2719 why = WHY_EXCEPTION;
2721 /* Unwind stacks if a (pseudo) exception occurred */
2723 fast_block_end:
2724 while (why != WHY_NOT && f->f_iblock > 0) {
2725 PyTryBlock *b = PyFrame_BlockPop(f);
2727 assert(why != WHY_YIELD);
2728 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2729 /* For a continue inside a try block,
2730 don't pop the block for the loop. */
2731 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2732 b->b_level);
2733 why = WHY_NOT;
2734 JUMPTO(PyInt_AS_LONG(retval));
2735 Py_DECREF(retval);
2736 break;
2739 while (STACK_LEVEL() > b->b_level) {
2740 v = POP();
2741 Py_XDECREF(v);
2743 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2744 why = WHY_NOT;
2745 JUMPTO(b->b_handler);
2746 break;
2748 if (b->b_type == SETUP_FINALLY ||
2749 (b->b_type == SETUP_EXCEPT &&
2750 why == WHY_EXCEPTION)) {
2751 if (why == WHY_EXCEPTION) {
2752 PyObject *exc, *val, *tb;
2753 PyErr_Fetch(&exc, &val, &tb);
2754 if (val == NULL) {
2755 val = Py_None;
2756 Py_INCREF(val);
2758 /* Make the raw exception data
2759 available to the handler,
2760 so a program can emulate the
2761 Python main loop. Don't do
2762 this for 'finally'. */
2763 if (b->b_type == SETUP_EXCEPT) {
2764 PyErr_NormalizeException(
2765 &exc, &val, &tb);
2766 set_exc_info(tstate,
2767 exc, val, tb);
2769 if (tb == NULL) {
2770 Py_INCREF(Py_None);
2771 PUSH(Py_None);
2772 } else
2773 PUSH(tb);
2774 PUSH(val);
2775 PUSH(exc);
2777 else {
2778 if (why & (WHY_RETURN | WHY_CONTINUE))
2779 PUSH(retval);
2780 v = PyInt_FromLong((long)why);
2781 PUSH(v);
2783 why = WHY_NOT;
2784 JUMPTO(b->b_handler);
2785 break;
2787 } /* unwind stack */
2789 /* End the loop if we still have an error (or return) */
2791 if (why != WHY_NOT)
2792 break;
2793 READ_TIMESTAMP(loop1);
2795 } /* main loop */
2797 assert(why != WHY_YIELD);
2798 /* Pop remaining stack entries. */
2799 while (!EMPTY()) {
2800 v = POP();
2801 Py_XDECREF(v);
2804 if (why != WHY_RETURN)
2805 retval = NULL;
2807 fast_yield:
2808 if (tstate->use_tracing) {
2809 if (tstate->c_tracefunc) {
2810 if (why == WHY_RETURN || why == WHY_YIELD) {
2811 if (call_trace(tstate->c_tracefunc,
2812 tstate->c_traceobj, f,
2813 PyTrace_RETURN, retval)) {
2814 Py_XDECREF(retval);
2815 retval = NULL;
2816 why = WHY_EXCEPTION;
2819 else if (why == WHY_EXCEPTION) {
2820 call_trace_protected(tstate->c_tracefunc,
2821 tstate->c_traceobj, f,
2822 PyTrace_RETURN, NULL);
2825 if (tstate->c_profilefunc) {
2826 if (why == WHY_EXCEPTION)
2827 call_trace_protected(tstate->c_profilefunc,
2828 tstate->c_profileobj, f,
2829 PyTrace_RETURN, NULL);
2830 else if (call_trace(tstate->c_profilefunc,
2831 tstate->c_profileobj, f,
2832 PyTrace_RETURN, retval)) {
2833 Py_XDECREF(retval);
2834 retval = NULL;
2835 why = WHY_EXCEPTION;
2840 if (tstate->frame->f_exc_type != NULL)
2841 reset_exc_info(tstate);
2842 else {
2843 assert(tstate->frame->f_exc_value == NULL);
2844 assert(tstate->frame->f_exc_traceback == NULL);
2847 /* pop frame */
2848 exit_eval_frame:
2849 Py_LeaveRecursiveCall();
2850 tstate->frame = f->f_back;
2852 return retval;
2855 /* This is gonna seem *real weird*, but if you put some other code between
2856 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2857 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2859 PyObject *
2860 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
2861 PyObject **args, int argcount, PyObject **kws, int kwcount,
2862 PyObject **defs, int defcount, PyObject *closure)
2864 register PyFrameObject *f;
2865 register PyObject *retval = NULL;
2866 register PyObject **fastlocals, **freevars;
2867 PyThreadState *tstate = PyThreadState_GET();
2868 PyObject *x, *u;
2870 if (globals == NULL) {
2871 PyErr_SetString(PyExc_SystemError,
2872 "PyEval_EvalCodeEx: NULL globals");
2873 return NULL;
2876 assert(tstate != NULL);
2877 assert(globals != NULL);
2878 f = PyFrame_New(tstate, co, globals, locals);
2879 if (f == NULL)
2880 return NULL;
2882 fastlocals = f->f_localsplus;
2883 freevars = f->f_localsplus + co->co_nlocals;
2885 if (co->co_argcount > 0 ||
2886 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2887 int i;
2888 int n = argcount;
2889 PyObject *kwdict = NULL;
2890 if (co->co_flags & CO_VARKEYWORDS) {
2891 kwdict = PyDict_New();
2892 if (kwdict == NULL)
2893 goto fail;
2894 i = co->co_argcount;
2895 if (co->co_flags & CO_VARARGS)
2896 i++;
2897 SETLOCAL(i, kwdict);
2899 if (argcount > co->co_argcount) {
2900 if (!(co->co_flags & CO_VARARGS)) {
2901 PyErr_Format(PyExc_TypeError,
2902 "%.200s() takes %s %d "
2903 "%sargument%s (%d given)",
2904 PyString_AsString(co->co_name),
2905 defcount ? "at most" : "exactly",
2906 co->co_argcount,
2907 kwcount ? "non-keyword " : "",
2908 co->co_argcount == 1 ? "" : "s",
2909 argcount);
2910 goto fail;
2912 n = co->co_argcount;
2914 for (i = 0; i < n; i++) {
2915 x = args[i];
2916 Py_INCREF(x);
2917 SETLOCAL(i, x);
2919 if (co->co_flags & CO_VARARGS) {
2920 u = PyTuple_New(argcount - n);
2921 if (u == NULL)
2922 goto fail;
2923 SETLOCAL(co->co_argcount, u);
2924 for (i = n; i < argcount; i++) {
2925 x = args[i];
2926 Py_INCREF(x);
2927 PyTuple_SET_ITEM(u, i-n, x);
2930 for (i = 0; i < kwcount; i++) {
2931 PyObject **co_varnames;
2932 PyObject *keyword = kws[2*i];
2933 PyObject *value = kws[2*i + 1];
2934 int j;
2935 if (keyword == NULL || !PyString_Check(keyword)) {
2936 PyErr_Format(PyExc_TypeError,
2937 "%.200s() keywords must be strings",
2938 PyString_AsString(co->co_name));
2939 goto fail;
2941 /* Speed hack: do raw pointer compares. As names are
2942 normally interned this should almost always hit. */
2943 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
2944 for (j = 0; j < co->co_argcount; j++) {
2945 PyObject *nm = co_varnames[j];
2946 if (nm == keyword)
2947 goto kw_found;
2949 /* Slow fallback, just in case */
2950 for (j = 0; j < co->co_argcount; j++) {
2951 PyObject *nm = co_varnames[j];
2952 int cmp = PyObject_RichCompareBool(
2953 keyword, nm, Py_EQ);
2954 if (cmp > 0)
2955 goto kw_found;
2956 else if (cmp < 0)
2957 goto fail;
2959 /* Check errors from Compare */
2960 if (PyErr_Occurred())
2961 goto fail;
2962 if (j >= co->co_argcount) {
2963 if (kwdict == NULL) {
2964 PyErr_Format(PyExc_TypeError,
2965 "%.200s() got an unexpected "
2966 "keyword argument '%.400s'",
2967 PyString_AsString(co->co_name),
2968 PyString_AsString(keyword));
2969 goto fail;
2971 PyDict_SetItem(kwdict, keyword, value);
2972 continue;
2974 kw_found:
2975 if (GETLOCAL(j) != NULL) {
2976 PyErr_Format(PyExc_TypeError,
2977 "%.200s() got multiple "
2978 "values for keyword "
2979 "argument '%.400s'",
2980 PyString_AsString(co->co_name),
2981 PyString_AsString(keyword));
2982 goto fail;
2984 Py_INCREF(value);
2985 SETLOCAL(j, value);
2987 if (argcount < co->co_argcount) {
2988 int m = co->co_argcount - defcount;
2989 for (i = argcount; i < m; i++) {
2990 if (GETLOCAL(i) == NULL) {
2991 PyErr_Format(PyExc_TypeError,
2992 "%.200s() takes %s %d "
2993 "%sargument%s (%d given)",
2994 PyString_AsString(co->co_name),
2995 ((co->co_flags & CO_VARARGS) ||
2996 defcount) ? "at least"
2997 : "exactly",
2998 m, kwcount ? "non-keyword " : "",
2999 m == 1 ? "" : "s", i);
3000 goto fail;
3003 if (n > m)
3004 i = n - m;
3005 else
3006 i = 0;
3007 for (; i < defcount; i++) {
3008 if (GETLOCAL(m+i) == NULL) {
3009 PyObject *def = defs[i];
3010 Py_INCREF(def);
3011 SETLOCAL(m+i, def);
3016 else {
3017 if (argcount > 0 || kwcount > 0) {
3018 PyErr_Format(PyExc_TypeError,
3019 "%.200s() takes no arguments (%d given)",
3020 PyString_AsString(co->co_name),
3021 argcount + kwcount);
3022 goto fail;
3025 /* Allocate and initialize storage for cell vars, and copy free
3026 vars into frame. This isn't too efficient right now. */
3027 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3028 int i, j, nargs, found;
3029 char *cellname, *argname;
3030 PyObject *c;
3032 nargs = co->co_argcount;
3033 if (co->co_flags & CO_VARARGS)
3034 nargs++;
3035 if (co->co_flags & CO_VARKEYWORDS)
3036 nargs++;
3038 /* Initialize each cell var, taking into account
3039 cell vars that are initialized from arguments.
3041 Should arrange for the compiler to put cellvars
3042 that are arguments at the beginning of the cellvars
3043 list so that we can march over it more efficiently?
3045 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3046 cellname = PyString_AS_STRING(
3047 PyTuple_GET_ITEM(co->co_cellvars, i));
3048 found = 0;
3049 for (j = 0; j < nargs; j++) {
3050 argname = PyString_AS_STRING(
3051 PyTuple_GET_ITEM(co->co_varnames, j));
3052 if (strcmp(cellname, argname) == 0) {
3053 c = PyCell_New(GETLOCAL(j));
3054 if (c == NULL)
3055 goto fail;
3056 GETLOCAL(co->co_nlocals + i) = c;
3057 found = 1;
3058 break;
3061 if (found == 0) {
3062 c = PyCell_New(NULL);
3063 if (c == NULL)
3064 goto fail;
3065 SETLOCAL(co->co_nlocals + i, c);
3069 if (PyTuple_GET_SIZE(co->co_freevars)) {
3070 int i;
3071 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3072 PyObject *o = PyTuple_GET_ITEM(closure, i);
3073 Py_INCREF(o);
3074 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3078 if (co->co_flags & CO_GENERATOR) {
3079 /* Don't need to keep the reference to f_back, it will be set
3080 * when the generator is resumed. */
3081 Py_XDECREF(f->f_back);
3082 f->f_back = NULL;
3084 PCALL(PCALL_GENERATOR);
3086 /* Create a new generator that owns the ready to run frame
3087 * and return that as the value. */
3088 return PyGen_New(f);
3091 retval = PyEval_EvalFrameEx(f,0);
3093 fail: /* Jump here from prelude on failure */
3095 /* decref'ing the frame can cause __del__ methods to get invoked,
3096 which can call back into Python. While we're done with the
3097 current Python frame (f), the associated C stack is still in use,
3098 so recursion_depth must be boosted for the duration.
3100 assert(tstate != NULL);
3101 ++tstate->recursion_depth;
3102 Py_DECREF(f);
3103 --tstate->recursion_depth;
3104 return retval;
3108 /* Implementation notes for set_exc_info() and reset_exc_info():
3110 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3111 'exc_traceback'. These always travel together.
3113 - tstate->curexc_ZZZ is the "hot" exception that is set by
3114 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3116 - Once an exception is caught by an except clause, it is transferred
3117 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3118 can pick it up. This is the primary task of set_exc_info().
3119 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
3121 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3123 Long ago, when none of this existed, there were just a few globals:
3124 one set corresponding to the "hot" exception, and one set
3125 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3126 globals; they were simply stored as sys.exc_ZZZ. For backwards
3127 compatibility, they still are!) The problem was that in code like
3128 this:
3130 try:
3131 "something that may fail"
3132 except "some exception":
3133 "do something else first"
3134 "print the exception from sys.exc_ZZZ."
3136 if "do something else first" invoked something that raised and caught
3137 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3138 cause of subtle bugs. I fixed this by changing the semantics as
3139 follows:
3141 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3142 *in that frame*.
3144 - But initially, and as long as no exception is caught in a given
3145 frame, sys.exc_ZZZ will hold the last exception caught in the
3146 previous frame (or the frame before that, etc.).
3148 The first bullet fixed the bug in the above example. The second
3149 bullet was for backwards compatibility: it was (and is) common to
3150 have a function that is called when an exception is caught, and to
3151 have that function access the caught exception via sys.exc_ZZZ.
3152 (Example: traceback.print_exc()).
3154 At the same time I fixed the problem that sys.exc_ZZZ weren't
3155 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3156 but that's really a separate improvement.
3158 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3159 variables to what they were before the current frame was called. The
3160 set_exc_info() function saves them on the frame so that
3161 reset_exc_info() can restore them. The invariant is that
3162 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3163 exception (where "catching" an exception applies only to successful
3164 except clauses); and if the current frame ever caught an exception,
3165 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3166 at the start of the current frame.
3170 static void
3171 set_exc_info(PyThreadState *tstate,
3172 PyObject *type, PyObject *value, PyObject *tb)
3174 PyFrameObject *frame = tstate->frame;
3175 PyObject *tmp_type, *tmp_value, *tmp_tb;
3177 assert(type != NULL);
3178 assert(frame != NULL);
3179 if (frame->f_exc_type == NULL) {
3180 assert(frame->f_exc_value == NULL);
3181 assert(frame->f_exc_traceback == NULL);
3182 /* This frame didn't catch an exception before. */
3183 /* Save previous exception of this thread in this frame. */
3184 if (tstate->exc_type == NULL) {
3185 /* XXX Why is this set to Py_None? */
3186 Py_INCREF(Py_None);
3187 tstate->exc_type = Py_None;
3189 Py_INCREF(tstate->exc_type);
3190 Py_XINCREF(tstate->exc_value);
3191 Py_XINCREF(tstate->exc_traceback);
3192 frame->f_exc_type = tstate->exc_type;
3193 frame->f_exc_value = tstate->exc_value;
3194 frame->f_exc_traceback = tstate->exc_traceback;
3196 /* Set new exception for this thread. */
3197 tmp_type = tstate->exc_type;
3198 tmp_value = tstate->exc_value;
3199 tmp_tb = tstate->exc_traceback;
3200 Py_INCREF(type);
3201 Py_XINCREF(value);
3202 Py_XINCREF(tb);
3203 tstate->exc_type = type;
3204 tstate->exc_value = value;
3205 tstate->exc_traceback = tb;
3206 Py_XDECREF(tmp_type);
3207 Py_XDECREF(tmp_value);
3208 Py_XDECREF(tmp_tb);
3209 /* For b/w compatibility */
3210 PySys_SetObject("exc_type", type);
3211 PySys_SetObject("exc_value", value);
3212 PySys_SetObject("exc_traceback", tb);
3215 static void
3216 reset_exc_info(PyThreadState *tstate)
3218 PyFrameObject *frame;
3219 PyObject *tmp_type, *tmp_value, *tmp_tb;
3221 /* It's a precondition that the thread state's frame caught an
3222 * exception -- verify in a debug build.
3224 assert(tstate != NULL);
3225 frame = tstate->frame;
3226 assert(frame != NULL);
3227 assert(frame->f_exc_type != NULL);
3229 /* Copy the frame's exception info back to the thread state. */
3230 tmp_type = tstate->exc_type;
3231 tmp_value = tstate->exc_value;
3232 tmp_tb = tstate->exc_traceback;
3233 Py_INCREF(frame->f_exc_type);
3234 Py_XINCREF(frame->f_exc_value);
3235 Py_XINCREF(frame->f_exc_traceback);
3236 tstate->exc_type = frame->f_exc_type;
3237 tstate->exc_value = frame->f_exc_value;
3238 tstate->exc_traceback = frame->f_exc_traceback;
3239 Py_XDECREF(tmp_type);
3240 Py_XDECREF(tmp_value);
3241 Py_XDECREF(tmp_tb);
3243 /* For b/w compatibility */
3244 PySys_SetObject("exc_type", frame->f_exc_type);
3245 PySys_SetObject("exc_value", frame->f_exc_value);
3246 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3248 /* Clear the frame's exception info. */
3249 tmp_type = frame->f_exc_type;
3250 tmp_value = frame->f_exc_value;
3251 tmp_tb = frame->f_exc_traceback;
3252 frame->f_exc_type = NULL;
3253 frame->f_exc_value = NULL;
3254 frame->f_exc_traceback = NULL;
3255 Py_DECREF(tmp_type);
3256 Py_XDECREF(tmp_value);
3257 Py_XDECREF(tmp_tb);
3260 /* Logic for the raise statement (too complicated for inlining).
3261 This *consumes* a reference count to each of its arguments. */
3262 static enum why_code
3263 do_raise(PyObject *type, PyObject *value, PyObject *tb)
3265 if (type == NULL) {
3266 /* Reraise */
3267 PyThreadState *tstate = PyThreadState_GET();
3268 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3269 value = tstate->exc_value;
3270 tb = tstate->exc_traceback;
3271 Py_XINCREF(type);
3272 Py_XINCREF(value);
3273 Py_XINCREF(tb);
3276 /* We support the following forms of raise:
3277 raise <class>, <classinstance>
3278 raise <class>, <argument tuple>
3279 raise <class>, None
3280 raise <class>, <argument>
3281 raise <classinstance>, None
3282 raise <string>, <object>
3283 raise <string>, None
3285 An omitted second argument is the same as None.
3287 In addition, raise <tuple>, <anything> is the same as
3288 raising the tuple's first item (and it better have one!);
3289 this rule is applied recursively.
3291 Finally, an optional third argument can be supplied, which
3292 gives the traceback to be substituted (useful when
3293 re-raising an exception after examining it). */
3295 /* First, check the traceback argument, replacing None with
3296 NULL. */
3297 if (tb == Py_None) {
3298 Py_DECREF(tb);
3299 tb = NULL;
3301 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3302 PyErr_SetString(PyExc_TypeError,
3303 "raise: arg 3 must be a traceback or None");
3304 goto raise_error;
3307 /* Next, replace a missing value with None */
3308 if (value == NULL) {
3309 value = Py_None;
3310 Py_INCREF(value);
3313 /* Next, repeatedly, replace a tuple exception with its first item */
3314 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3315 PyObject *tmp = type;
3316 type = PyTuple_GET_ITEM(type, 0);
3317 Py_INCREF(type);
3318 Py_DECREF(tmp);
3321 if (PyExceptionClass_Check(type))
3322 PyErr_NormalizeException(&type, &value, &tb);
3324 else if (PyExceptionInstance_Check(type)) {
3325 /* Raising an instance. The value should be a dummy. */
3326 if (value != Py_None) {
3327 PyErr_SetString(PyExc_TypeError,
3328 "instance exception may not have a separate value");
3329 goto raise_error;
3331 else {
3332 /* Normalize to raise <class>, <instance> */
3333 Py_DECREF(value);
3334 value = type;
3335 type = PyExceptionInstance_Class(type);
3336 Py_INCREF(type);
3339 else {
3340 /* Not something you can raise. You get an exception
3341 anyway, just not what you specified :-) */
3342 PyErr_Format(PyExc_TypeError,
3343 "exceptions must be classes or instances, not %s",
3344 type->ob_type->tp_name);
3345 goto raise_error;
3348 assert(PyExceptionClass_Check(type));
3349 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3350 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3351 "exceptions must derive from BaseException "
3352 "in 3.x", 1) < 0)
3353 goto raise_error;
3356 PyErr_Restore(type, value, tb);
3357 if (tb == NULL)
3358 return WHY_EXCEPTION;
3359 else
3360 return WHY_RERAISE;
3361 raise_error:
3362 Py_XDECREF(value);
3363 Py_XDECREF(type);
3364 Py_XDECREF(tb);
3365 return WHY_EXCEPTION;
3368 /* Iterate v argcnt times and store the results on the stack (via decreasing
3369 sp). Return 1 for success, 0 if error. */
3371 static int
3372 unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
3374 int i = 0;
3375 PyObject *it; /* iter(v) */
3376 PyObject *w;
3378 assert(v != NULL);
3380 it = PyObject_GetIter(v);
3381 if (it == NULL)
3382 goto Error;
3384 for (; i < argcnt; i++) {
3385 w = PyIter_Next(it);
3386 if (w == NULL) {
3387 /* Iterator done, via error or exhaustion. */
3388 if (!PyErr_Occurred()) {
3389 PyErr_Format(PyExc_ValueError,
3390 "need more than %d value%s to unpack",
3391 i, i == 1 ? "" : "s");
3393 goto Error;
3395 *--sp = w;
3398 /* We better have exhausted the iterator now. */
3399 w = PyIter_Next(it);
3400 if (w == NULL) {
3401 if (PyErr_Occurred())
3402 goto Error;
3403 Py_DECREF(it);
3404 return 1;
3406 Py_DECREF(w);
3407 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3408 /* fall through */
3409 Error:
3410 for (; i > 0; i--, sp++)
3411 Py_DECREF(*sp);
3412 Py_XDECREF(it);
3413 return 0;
3417 #ifdef LLTRACE
3418 static int
3419 prtrace(PyObject *v, char *str)
3421 printf("%s ", str);
3422 if (PyObject_Print(v, stdout, 0) != 0)
3423 PyErr_Clear(); /* Don't know what else to do */
3424 printf("\n");
3425 return 1;
3427 #endif
3429 static void
3430 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
3432 PyObject *type, *value, *traceback, *arg;
3433 int err;
3434 PyErr_Fetch(&type, &value, &traceback);
3435 if (value == NULL) {
3436 value = Py_None;
3437 Py_INCREF(value);
3439 arg = PyTuple_Pack(3, type, value, traceback);
3440 if (arg == NULL) {
3441 PyErr_Restore(type, value, traceback);
3442 return;
3444 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3445 Py_DECREF(arg);
3446 if (err == 0)
3447 PyErr_Restore(type, value, traceback);
3448 else {
3449 Py_XDECREF(type);
3450 Py_XDECREF(value);
3451 Py_XDECREF(traceback);
3455 static int
3456 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3457 int what, PyObject *arg)
3459 PyObject *type, *value, *traceback;
3460 int err;
3461 PyErr_Fetch(&type, &value, &traceback);
3462 err = call_trace(func, obj, frame, what, arg);
3463 if (err == 0)
3465 PyErr_Restore(type, value, traceback);
3466 return 0;
3468 else {
3469 Py_XDECREF(type);
3470 Py_XDECREF(value);
3471 Py_XDECREF(traceback);
3472 return -1;
3476 static int
3477 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3478 int what, PyObject *arg)
3480 register PyThreadState *tstate = frame->f_tstate;
3481 int result;
3482 if (tstate->tracing)
3483 return 0;
3484 tstate->tracing++;
3485 tstate->use_tracing = 0;
3486 result = func(obj, frame, what, arg);
3487 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3488 || (tstate->c_profilefunc != NULL));
3489 tstate->tracing--;
3490 return result;
3493 PyObject *
3494 _PyEval_CallTracing(PyObject *func, PyObject *args)
3496 PyFrameObject *frame = PyEval_GetFrame();
3497 PyThreadState *tstate = frame->f_tstate;
3498 int save_tracing = tstate->tracing;
3499 int save_use_tracing = tstate->use_tracing;
3500 PyObject *result;
3502 tstate->tracing = 0;
3503 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3504 || (tstate->c_profilefunc != NULL));
3505 result = PyObject_Call(func, args, NULL);
3506 tstate->tracing = save_tracing;
3507 tstate->use_tracing = save_use_tracing;
3508 return result;
3511 static int
3512 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
3513 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3514 int *instr_prev)
3516 int result = 0;
3518 /* If the last instruction executed isn't in the current
3519 instruction window, reset the window. If the last
3520 instruction happens to fall at the start of a line or if it
3521 represents a jump backwards, call the trace function.
3523 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
3524 int line;
3525 PyAddrPair bounds;
3527 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3528 &bounds);
3529 if (line >= 0) {
3530 frame->f_lineno = line;
3531 result = call_trace(func, obj, frame,
3532 PyTrace_LINE, Py_None);
3534 *instr_lb = bounds.ap_lower;
3535 *instr_ub = bounds.ap_upper;
3537 else if (frame->f_lasti <= *instr_prev) {
3538 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3540 *instr_prev = frame->f_lasti;
3541 return result;
3544 void
3545 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
3547 PyThreadState *tstate = PyThreadState_GET();
3548 PyObject *temp = tstate->c_profileobj;
3549 Py_XINCREF(arg);
3550 tstate->c_profilefunc = NULL;
3551 tstate->c_profileobj = NULL;
3552 /* Must make sure that tracing is not ignored if 'temp' is freed */
3553 tstate->use_tracing = tstate->c_tracefunc != NULL;
3554 Py_XDECREF(temp);
3555 tstate->c_profilefunc = func;
3556 tstate->c_profileobj = arg;
3557 /* Flag that tracing or profiling is turned on */
3558 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
3561 void
3562 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3564 PyThreadState *tstate = PyThreadState_GET();
3565 PyObject *temp = tstate->c_traceobj;
3566 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3567 Py_XINCREF(arg);
3568 tstate->c_tracefunc = NULL;
3569 tstate->c_traceobj = NULL;
3570 /* Must make sure that profiling is not ignored if 'temp' is freed */
3571 tstate->use_tracing = tstate->c_profilefunc != NULL;
3572 Py_XDECREF(temp);
3573 tstate->c_tracefunc = func;
3574 tstate->c_traceobj = arg;
3575 /* Flag that tracing or profiling is turned on */
3576 tstate->use_tracing = ((func != NULL)
3577 || (tstate->c_profilefunc != NULL));
3580 PyObject *
3581 PyEval_GetBuiltins(void)
3583 PyFrameObject *current_frame = PyEval_GetFrame();
3584 if (current_frame == NULL)
3585 return PyThreadState_GET()->interp->builtins;
3586 else
3587 return current_frame->f_builtins;
3590 PyObject *
3591 PyEval_GetLocals(void)
3593 PyFrameObject *current_frame = PyEval_GetFrame();
3594 if (current_frame == NULL)
3595 return NULL;
3596 PyFrame_FastToLocals(current_frame);
3597 return current_frame->f_locals;
3600 PyObject *
3601 PyEval_GetGlobals(void)
3603 PyFrameObject *current_frame = PyEval_GetFrame();
3604 if (current_frame == NULL)
3605 return NULL;
3606 else
3607 return current_frame->f_globals;
3610 PyFrameObject *
3611 PyEval_GetFrame(void)
3613 PyThreadState *tstate = PyThreadState_GET();
3614 return _PyThreadState_GetFrame(tstate);
3618 PyEval_GetRestricted(void)
3620 PyFrameObject *current_frame = PyEval_GetFrame();
3621 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
3625 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
3627 PyFrameObject *current_frame = PyEval_GetFrame();
3628 int result = cf->cf_flags != 0;
3630 if (current_frame != NULL) {
3631 const int codeflags = current_frame->f_code->co_flags;
3632 const int compilerflags = codeflags & PyCF_MASK;
3633 if (compilerflags) {
3634 result = 1;
3635 cf->cf_flags |= compilerflags;
3637 #if 0 /* future keyword */
3638 if (codeflags & CO_GENERATOR_ALLOWED) {
3639 result = 1;
3640 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3642 #endif
3644 return result;
3648 Py_FlushLine(void)
3650 PyObject *f = PySys_GetObject("stdout");
3651 if (f == NULL)
3652 return 0;
3653 if (!PyFile_SoftSpace(f, 0))
3654 return 0;
3655 return PyFile_WriteString("\n", f);
3659 /* External interface to call any callable object.
3660 The arg must be a tuple or NULL. */
3662 #undef PyEval_CallObject
3663 /* for backward compatibility: export this interface */
3665 PyObject *
3666 PyEval_CallObject(PyObject *func, PyObject *arg)
3668 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
3670 #define PyEval_CallObject(func,arg) \
3671 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3673 PyObject *
3674 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
3676 PyObject *result;
3678 if (arg == NULL) {
3679 arg = PyTuple_New(0);
3680 if (arg == NULL)
3681 return NULL;
3683 else if (!PyTuple_Check(arg)) {
3684 PyErr_SetString(PyExc_TypeError,
3685 "argument list must be a tuple");
3686 return NULL;
3688 else
3689 Py_INCREF(arg);
3691 if (kw != NULL && !PyDict_Check(kw)) {
3692 PyErr_SetString(PyExc_TypeError,
3693 "keyword list must be a dictionary");
3694 Py_DECREF(arg);
3695 return NULL;
3698 result = PyObject_Call(func, arg, kw);
3699 Py_DECREF(arg);
3700 return result;
3703 const char *
3704 PyEval_GetFuncName(PyObject *func)
3706 if (PyMethod_Check(func))
3707 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3708 else if (PyFunction_Check(func))
3709 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3710 else if (PyCFunction_Check(func))
3711 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3712 else if (PyClass_Check(func))
3713 return PyString_AsString(((PyClassObject*)func)->cl_name);
3714 else if (PyInstance_Check(func)) {
3715 return PyString_AsString(
3716 ((PyInstanceObject*)func)->in_class->cl_name);
3717 } else {
3718 return func->ob_type->tp_name;
3722 const char *
3723 PyEval_GetFuncDesc(PyObject *func)
3725 if (PyMethod_Check(func))
3726 return "()";
3727 else if (PyFunction_Check(func))
3728 return "()";
3729 else if (PyCFunction_Check(func))
3730 return "()";
3731 else if (PyClass_Check(func))
3732 return " constructor";
3733 else if (PyInstance_Check(func)) {
3734 return " instance";
3735 } else {
3736 return " object";
3740 static void
3741 err_args(PyObject *func, int flags, int nargs)
3743 if (flags & METH_NOARGS)
3744 PyErr_Format(PyExc_TypeError,
3745 "%.200s() takes no arguments (%d given)",
3746 ((PyCFunctionObject *)func)->m_ml->ml_name,
3747 nargs);
3748 else
3749 PyErr_Format(PyExc_TypeError,
3750 "%.200s() takes exactly one argument (%d given)",
3751 ((PyCFunctionObject *)func)->m_ml->ml_name,
3752 nargs);
3755 #define C_TRACE(x, call) \
3756 if (tstate->use_tracing && tstate->c_profilefunc) { \
3757 if (call_trace(tstate->c_profilefunc, \
3758 tstate->c_profileobj, \
3759 tstate->frame, PyTrace_C_CALL, \
3760 func)) { \
3761 x = NULL; \
3763 else { \
3764 x = call; \
3765 if (tstate->c_profilefunc != NULL) { \
3766 if (x == NULL) { \
3767 call_trace_protected(tstate->c_profilefunc, \
3768 tstate->c_profileobj, \
3769 tstate->frame, PyTrace_C_EXCEPTION, \
3770 func); \
3771 /* XXX should pass (type, value, tb) */ \
3772 } else { \
3773 if (call_trace(tstate->c_profilefunc, \
3774 tstate->c_profileobj, \
3775 tstate->frame, PyTrace_C_RETURN, \
3776 func)) { \
3777 Py_DECREF(x); \
3778 x = NULL; \
3783 } else { \
3784 x = call; \
3787 static PyObject *
3788 call_function(PyObject ***pp_stack, int oparg
3789 #ifdef WITH_TSC
3790 , uint64* pintr0, uint64* pintr1
3791 #endif
3794 int na = oparg & 0xff;
3795 int nk = (oparg>>8) & 0xff;
3796 int n = na + 2 * nk;
3797 PyObject **pfunc = (*pp_stack) - n - 1;
3798 PyObject *func = *pfunc;
3799 PyObject *x, *w;
3801 /* Always dispatch PyCFunction first, because these are
3802 presumed to be the most frequent callable object.
3804 if (PyCFunction_Check(func) && nk == 0) {
3805 int flags = PyCFunction_GET_FLAGS(func);
3806 PyThreadState *tstate = PyThreadState_GET();
3808 PCALL(PCALL_CFUNCTION);
3809 if (flags & (METH_NOARGS | METH_O)) {
3810 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3811 PyObject *self = PyCFunction_GET_SELF(func);
3812 if (flags & METH_NOARGS && na == 0) {
3813 C_TRACE(x, (*meth)(self,NULL));
3815 else if (flags & METH_O && na == 1) {
3816 PyObject *arg = EXT_POP(*pp_stack);
3817 C_TRACE(x, (*meth)(self,arg));
3818 Py_DECREF(arg);
3820 else {
3821 err_args(func, flags, na);
3822 x = NULL;
3825 else {
3826 PyObject *callargs;
3827 callargs = load_args(pp_stack, na);
3828 READ_TIMESTAMP(*pintr0);
3829 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3830 READ_TIMESTAMP(*pintr1);
3831 Py_XDECREF(callargs);
3833 } else {
3834 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3835 /* optimize access to bound methods */
3836 PyObject *self = PyMethod_GET_SELF(func);
3837 PCALL(PCALL_METHOD);
3838 PCALL(PCALL_BOUND_METHOD);
3839 Py_INCREF(self);
3840 func = PyMethod_GET_FUNCTION(func);
3841 Py_INCREF(func);
3842 Py_DECREF(*pfunc);
3843 *pfunc = self;
3844 na++;
3845 n++;
3846 } else
3847 Py_INCREF(func);
3848 READ_TIMESTAMP(*pintr0);
3849 if (PyFunction_Check(func))
3850 x = fast_function(func, pp_stack, n, na, nk);
3851 else
3852 x = do_call(func, pp_stack, na, nk);
3853 READ_TIMESTAMP(*pintr1);
3854 Py_DECREF(func);
3857 /* Clear the stack of the function object. Also removes
3858 the arguments in case they weren't consumed already
3859 (fast_function() and err_args() leave them on the stack).
3861 while ((*pp_stack) > pfunc) {
3862 w = EXT_POP(*pp_stack);
3863 Py_DECREF(w);
3864 PCALL(PCALL_POP);
3866 return x;
3869 /* The fast_function() function optimize calls for which no argument
3870 tuple is necessary; the objects are passed directly from the stack.
3871 For the simplest case -- a function that takes only positional
3872 arguments and is called with only positional arguments -- it
3873 inlines the most primitive frame setup code from
3874 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3875 done before evaluating the frame.
3878 static PyObject *
3879 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
3881 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3882 PyObject *globals = PyFunction_GET_GLOBALS(func);
3883 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3884 PyObject **d = NULL;
3885 int nd = 0;
3887 PCALL(PCALL_FUNCTION);
3888 PCALL(PCALL_FAST_FUNCTION);
3889 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
3890 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3891 PyFrameObject *f;
3892 PyObject *retval = NULL;
3893 PyThreadState *tstate = PyThreadState_GET();
3894 PyObject **fastlocals, **stack;
3895 int i;
3897 PCALL(PCALL_FASTER_FUNCTION);
3898 assert(globals != NULL);
3899 /* XXX Perhaps we should create a specialized
3900 PyFrame_New() that doesn't take locals, but does
3901 take builtins without sanity checking them.
3903 assert(tstate != NULL);
3904 f = PyFrame_New(tstate, co, globals, NULL);
3905 if (f == NULL)
3906 return NULL;
3908 fastlocals = f->f_localsplus;
3909 stack = (*pp_stack) - n;
3911 for (i = 0; i < n; i++) {
3912 Py_INCREF(*stack);
3913 fastlocals[i] = *stack++;
3915 retval = PyEval_EvalFrameEx(f,0);
3916 ++tstate->recursion_depth;
3917 Py_DECREF(f);
3918 --tstate->recursion_depth;
3919 return retval;
3921 if (argdefs != NULL) {
3922 d = &PyTuple_GET_ITEM(argdefs, 0);
3923 nd = Py_SIZE(argdefs);
3925 return PyEval_EvalCodeEx(co, globals,
3926 (PyObject *)NULL, (*pp_stack)-n, na,
3927 (*pp_stack)-2*nk, nk, d, nd,
3928 PyFunction_GET_CLOSURE(func));
3931 static PyObject *
3932 update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3933 PyObject *func)
3935 PyObject *kwdict = NULL;
3936 if (orig_kwdict == NULL)
3937 kwdict = PyDict_New();
3938 else {
3939 kwdict = PyDict_Copy(orig_kwdict);
3940 Py_DECREF(orig_kwdict);
3942 if (kwdict == NULL)
3943 return NULL;
3944 while (--nk >= 0) {
3945 int err;
3946 PyObject *value = EXT_POP(*pp_stack);
3947 PyObject *key = EXT_POP(*pp_stack);
3948 if (PyDict_GetItem(kwdict, key) != NULL) {
3949 PyErr_Format(PyExc_TypeError,
3950 "%.200s%s got multiple values "
3951 "for keyword argument '%.200s'",
3952 PyEval_GetFuncName(func),
3953 PyEval_GetFuncDesc(func),
3954 PyString_AsString(key));
3955 Py_DECREF(key);
3956 Py_DECREF(value);
3957 Py_DECREF(kwdict);
3958 return NULL;
3960 err = PyDict_SetItem(kwdict, key, value);
3961 Py_DECREF(key);
3962 Py_DECREF(value);
3963 if (err) {
3964 Py_DECREF(kwdict);
3965 return NULL;
3968 return kwdict;
3971 static PyObject *
3972 update_star_args(int nstack, int nstar, PyObject *stararg,
3973 PyObject ***pp_stack)
3975 PyObject *callargs, *w;
3977 callargs = PyTuple_New(nstack + nstar);
3978 if (callargs == NULL) {
3979 return NULL;
3981 if (nstar) {
3982 int i;
3983 for (i = 0; i < nstar; i++) {
3984 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3985 Py_INCREF(a);
3986 PyTuple_SET_ITEM(callargs, nstack + i, a);
3989 while (--nstack >= 0) {
3990 w = EXT_POP(*pp_stack);
3991 PyTuple_SET_ITEM(callargs, nstack, w);
3993 return callargs;
3996 static PyObject *
3997 load_args(PyObject ***pp_stack, int na)
3999 PyObject *args = PyTuple_New(na);
4000 PyObject *w;
4002 if (args == NULL)
4003 return NULL;
4004 while (--na >= 0) {
4005 w = EXT_POP(*pp_stack);
4006 PyTuple_SET_ITEM(args, na, w);
4008 return args;
4011 static PyObject *
4012 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4014 PyObject *callargs = NULL;
4015 PyObject *kwdict = NULL;
4016 PyObject *result = NULL;
4018 if (nk > 0) {
4019 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4020 if (kwdict == NULL)
4021 goto call_fail;
4023 callargs = load_args(pp_stack, na);
4024 if (callargs == NULL)
4025 goto call_fail;
4026 #ifdef CALL_PROFILE
4027 /* At this point, we have to look at the type of func to
4028 update the call stats properly. Do it here so as to avoid
4029 exposing the call stats machinery outside ceval.c
4031 if (PyFunction_Check(func))
4032 PCALL(PCALL_FUNCTION);
4033 else if (PyMethod_Check(func))
4034 PCALL(PCALL_METHOD);
4035 else if (PyType_Check(func))
4036 PCALL(PCALL_TYPE);
4037 else
4038 PCALL(PCALL_OTHER);
4039 #endif
4040 result = PyObject_Call(func, callargs, kwdict);
4041 call_fail:
4042 Py_XDECREF(callargs);
4043 Py_XDECREF(kwdict);
4044 return result;
4047 static PyObject *
4048 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4050 int nstar = 0;
4051 PyObject *callargs = NULL;
4052 PyObject *stararg = NULL;
4053 PyObject *kwdict = NULL;
4054 PyObject *result = NULL;
4056 if (flags & CALL_FLAG_KW) {
4057 kwdict = EXT_POP(*pp_stack);
4058 if (!PyDict_Check(kwdict)) {
4059 PyObject *d;
4060 d = PyDict_New();
4061 if (d == NULL)
4062 goto ext_call_fail;
4063 if (PyDict_Update(d, kwdict) != 0) {
4064 Py_DECREF(d);
4065 /* PyDict_Update raises attribute
4066 * error (percolated from an attempt
4067 * to get 'keys' attribute) instead of
4068 * a type error if its second argument
4069 * is not a mapping.
4071 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4072 PyErr_Format(PyExc_TypeError,
4073 "%.200s%.200s argument after ** "
4074 "must be a mapping, not %.200s",
4075 PyEval_GetFuncName(func),
4076 PyEval_GetFuncDesc(func),
4077 kwdict->ob_type->tp_name);
4079 goto ext_call_fail;
4081 Py_DECREF(kwdict);
4082 kwdict = d;
4085 if (flags & CALL_FLAG_VAR) {
4086 stararg = EXT_POP(*pp_stack);
4087 if (!PyTuple_Check(stararg)) {
4088 PyObject *t = NULL;
4089 t = PySequence_Tuple(stararg);
4090 if (t == NULL) {
4091 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4092 PyErr_Format(PyExc_TypeError,
4093 "%.200s%.200s argument after * "
4094 "must be a sequence, not %200s",
4095 PyEval_GetFuncName(func),
4096 PyEval_GetFuncDesc(func),
4097 stararg->ob_type->tp_name);
4099 goto ext_call_fail;
4101 Py_DECREF(stararg);
4102 stararg = t;
4104 nstar = PyTuple_GET_SIZE(stararg);
4106 if (nk > 0) {
4107 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4108 if (kwdict == NULL)
4109 goto ext_call_fail;
4111 callargs = update_star_args(na, nstar, stararg, pp_stack);
4112 if (callargs == NULL)
4113 goto ext_call_fail;
4114 #ifdef CALL_PROFILE
4115 /* At this point, we have to look at the type of func to
4116 update the call stats properly. Do it here so as to avoid
4117 exposing the call stats machinery outside ceval.c
4119 if (PyFunction_Check(func))
4120 PCALL(PCALL_FUNCTION);
4121 else if (PyMethod_Check(func))
4122 PCALL(PCALL_METHOD);
4123 else if (PyType_Check(func))
4124 PCALL(PCALL_TYPE);
4125 else
4126 PCALL(PCALL_OTHER);
4127 #endif
4128 result = PyObject_Call(func, callargs, kwdict);
4129 ext_call_fail:
4130 Py_XDECREF(callargs);
4131 Py_XDECREF(kwdict);
4132 Py_XDECREF(stararg);
4133 return result;
4136 /* Extract a slice index from a PyInt or PyLong or an object with the
4137 nb_index slot defined, and store in *pi.
4138 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4139 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
4140 Return 0 on error, 1 on success.
4142 /* Note: If v is NULL, return success without storing into *pi. This
4143 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4144 called by the SLICE opcode with v and/or w equal to NULL.
4147 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
4149 if (v != NULL) {
4150 Py_ssize_t x;
4151 if (PyInt_Check(v)) {
4152 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4153 however, it looks like it should be AsSsize_t.
4154 There should be a comment here explaining why.
4156 x = PyInt_AS_LONG(v);
4158 else if (PyIndex_Check(v)) {
4159 x = PyNumber_AsSsize_t(v, NULL);
4160 if (x == -1 && PyErr_Occurred())
4161 return 0;
4163 else {
4164 PyErr_SetString(PyExc_TypeError,
4165 "slice indices must be integers or "
4166 "None or have an __index__ method");
4167 return 0;
4169 *pi = x;
4171 return 1;
4174 #undef ISINDEX
4175 #define ISINDEX(x) ((x) == NULL || \
4176 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
4178 static PyObject *
4179 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
4181 PyTypeObject *tp = u->ob_type;
4182 PySequenceMethods *sq = tp->tp_as_sequence;
4184 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4185 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4186 if (!_PyEval_SliceIndex(v, &ilow))
4187 return NULL;
4188 if (!_PyEval_SliceIndex(w, &ihigh))
4189 return NULL;
4190 return PySequence_GetSlice(u, ilow, ihigh);
4192 else {
4193 PyObject *slice = PySlice_New(v, w, NULL);
4194 if (slice != NULL) {
4195 PyObject *res = PyObject_GetItem(u, slice);
4196 Py_DECREF(slice);
4197 return res;
4199 else
4200 return NULL;
4204 static int
4205 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4206 /* u[v:w] = x */
4208 PyTypeObject *tp = u->ob_type;
4209 PySequenceMethods *sq = tp->tp_as_sequence;
4211 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4212 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4213 if (!_PyEval_SliceIndex(v, &ilow))
4214 return -1;
4215 if (!_PyEval_SliceIndex(w, &ihigh))
4216 return -1;
4217 if (x == NULL)
4218 return PySequence_DelSlice(u, ilow, ihigh);
4219 else
4220 return PySequence_SetSlice(u, ilow, ihigh, x);
4222 else {
4223 PyObject *slice = PySlice_New(v, w, NULL);
4224 if (slice != NULL) {
4225 int res;
4226 if (x != NULL)
4227 res = PyObject_SetItem(u, slice, x);
4228 else
4229 res = PyObject_DelItem(u, slice);
4230 Py_DECREF(slice);
4231 return res;
4233 else
4234 return -1;
4238 #define Py3kExceptionClass_Check(x) \
4239 (PyType_Check((x)) && \
4240 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4242 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4243 "BaseException is not allowed in 3.x"
4245 static PyObject *
4246 cmp_outcome(int op, register PyObject *v, register PyObject *w)
4248 int res = 0;
4249 switch (op) {
4250 case PyCmp_IS:
4251 res = (v == w);
4252 break;
4253 case PyCmp_IS_NOT:
4254 res = (v != w);
4255 break;
4256 case PyCmp_IN:
4257 res = PySequence_Contains(w, v);
4258 if (res < 0)
4259 return NULL;
4260 break;
4261 case PyCmp_NOT_IN:
4262 res = PySequence_Contains(w, v);
4263 if (res < 0)
4264 return NULL;
4265 res = !res;
4266 break;
4267 case PyCmp_EXC_MATCH:
4268 if (PyTuple_Check(w)) {
4269 Py_ssize_t i, length;
4270 length = PyTuple_Size(w);
4271 for (i = 0; i < length; i += 1) {
4272 PyObject *exc = PyTuple_GET_ITEM(w, i);
4273 if (PyString_Check(exc)) {
4274 int ret_val;
4275 ret_val = PyErr_WarnEx(
4276 PyExc_DeprecationWarning,
4277 "catching of string "
4278 "exceptions is deprecated", 1);
4279 if (ret_val < 0)
4280 return NULL;
4282 else if (Py_Py3kWarningFlag &&
4283 !PyTuple_Check(exc) &&
4284 !Py3kExceptionClass_Check(exc))
4286 int ret_val;
4287 ret_val = PyErr_WarnEx(
4288 PyExc_DeprecationWarning,
4289 CANNOT_CATCH_MSG, 1);
4290 if (ret_val < 0)
4291 return NULL;
4295 else {
4296 if (PyString_Check(w)) {
4297 int ret_val;
4298 ret_val = PyErr_WarnEx(
4299 PyExc_DeprecationWarning,
4300 "catching of string "
4301 "exceptions is deprecated", 1);
4302 if (ret_val < 0)
4303 return NULL;
4305 else if (Py_Py3kWarningFlag &&
4306 !PyTuple_Check(w) &&
4307 !Py3kExceptionClass_Check(w))
4309 int ret_val;
4310 ret_val = PyErr_WarnEx(
4311 PyExc_DeprecationWarning,
4312 CANNOT_CATCH_MSG, 1);
4313 if (ret_val < 0)
4314 return NULL;
4317 res = PyErr_GivenExceptionMatches(v, w);
4318 break;
4319 default:
4320 return PyObject_RichCompare(v, w, op);
4322 v = res ? Py_True : Py_False;
4323 Py_INCREF(v);
4324 return v;
4327 static PyObject *
4328 import_from(PyObject *v, PyObject *name)
4330 PyObject *x;
4332 x = PyObject_GetAttr(v, name);
4333 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4334 PyErr_Format(PyExc_ImportError,
4335 "cannot import name %.230s",
4336 PyString_AsString(name));
4338 return x;
4341 static int
4342 import_all_from(PyObject *locals, PyObject *v)
4344 PyObject *all = PyObject_GetAttrString(v, "__all__");
4345 PyObject *dict, *name, *value;
4346 int skip_leading_underscores = 0;
4347 int pos, err;
4349 if (all == NULL) {
4350 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4351 return -1; /* Unexpected error */
4352 PyErr_Clear();
4353 dict = PyObject_GetAttrString(v, "__dict__");
4354 if (dict == NULL) {
4355 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4356 return -1;
4357 PyErr_SetString(PyExc_ImportError,
4358 "from-import-* object has no __dict__ and no __all__");
4359 return -1;
4361 all = PyMapping_Keys(dict);
4362 Py_DECREF(dict);
4363 if (all == NULL)
4364 return -1;
4365 skip_leading_underscores = 1;
4368 for (pos = 0, err = 0; ; pos++) {
4369 name = PySequence_GetItem(all, pos);
4370 if (name == NULL) {
4371 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4372 err = -1;
4373 else
4374 PyErr_Clear();
4375 break;
4377 if (skip_leading_underscores &&
4378 PyString_Check(name) &&
4379 PyString_AS_STRING(name)[0] == '_')
4381 Py_DECREF(name);
4382 continue;
4384 value = PyObject_GetAttr(v, name);
4385 if (value == NULL)
4386 err = -1;
4387 else if (PyDict_CheckExact(locals))
4388 err = PyDict_SetItem(locals, name, value);
4389 else
4390 err = PyObject_SetItem(locals, name, value);
4391 Py_DECREF(name);
4392 Py_XDECREF(value);
4393 if (err != 0)
4394 break;
4396 Py_DECREF(all);
4397 return err;
4400 static PyObject *
4401 build_class(PyObject *methods, PyObject *bases, PyObject *name)
4403 PyObject *metaclass = NULL, *result, *base;
4405 if (PyDict_Check(methods))
4406 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4407 if (metaclass != NULL)
4408 Py_INCREF(metaclass);
4409 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4410 base = PyTuple_GET_ITEM(bases, 0);
4411 metaclass = PyObject_GetAttrString(base, "__class__");
4412 if (metaclass == NULL) {
4413 PyErr_Clear();
4414 metaclass = (PyObject *)base->ob_type;
4415 Py_INCREF(metaclass);
4418 else {
4419 PyObject *g = PyEval_GetGlobals();
4420 if (g != NULL && PyDict_Check(g))
4421 metaclass = PyDict_GetItemString(g, "__metaclass__");
4422 if (metaclass == NULL)
4423 metaclass = (PyObject *) &PyClass_Type;
4424 Py_INCREF(metaclass);
4426 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4427 NULL);
4428 Py_DECREF(metaclass);
4429 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4430 /* A type error here likely means that the user passed
4431 in a base that was not a class (such the random module
4432 instead of the random.random type). Help them out with
4433 by augmenting the error message with more information.*/
4435 PyObject *ptype, *pvalue, *ptraceback;
4437 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4438 if (PyString_Check(pvalue)) {
4439 PyObject *newmsg;
4440 newmsg = PyString_FromFormat(
4441 "Error when calling the metaclass bases\n"
4442 " %s",
4443 PyString_AS_STRING(pvalue));
4444 if (newmsg != NULL) {
4445 Py_DECREF(pvalue);
4446 pvalue = newmsg;
4449 PyErr_Restore(ptype, pvalue, ptraceback);
4451 return result;
4454 static int
4455 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4456 PyObject *locals)
4458 int n;
4459 PyObject *v;
4460 int plain = 0;
4462 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4463 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4464 /* Backward compatibility hack */
4465 globals = PyTuple_GetItem(prog, 1);
4466 if (n == 3)
4467 locals = PyTuple_GetItem(prog, 2);
4468 prog = PyTuple_GetItem(prog, 0);
4470 if (globals == Py_None) {
4471 globals = PyEval_GetGlobals();
4472 if (locals == Py_None) {
4473 locals = PyEval_GetLocals();
4474 plain = 1;
4476 if (!globals || !locals) {
4477 PyErr_SetString(PyExc_SystemError,
4478 "globals and locals cannot be NULL");
4479 return -1;
4482 else if (locals == Py_None)
4483 locals = globals;
4484 if (!PyString_Check(prog) &&
4485 !PyUnicode_Check(prog) &&
4486 !PyCode_Check(prog) &&
4487 !PyFile_Check(prog)) {
4488 PyErr_SetString(PyExc_TypeError,
4489 "exec: arg 1 must be a string, file, or code object");
4490 return -1;
4492 if (!PyDict_Check(globals)) {
4493 PyErr_SetString(PyExc_TypeError,
4494 "exec: arg 2 must be a dictionary or None");
4495 return -1;
4497 if (!PyMapping_Check(locals)) {
4498 PyErr_SetString(PyExc_TypeError,
4499 "exec: arg 3 must be a mapping or None");
4500 return -1;
4502 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4503 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4504 if (PyCode_Check(prog)) {
4505 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4506 PyErr_SetString(PyExc_TypeError,
4507 "code object passed to exec may not contain free variables");
4508 return -1;
4510 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4512 else if (PyFile_Check(prog)) {
4513 FILE *fp = PyFile_AsFile(prog);
4514 char *name = PyString_AsString(PyFile_Name(prog));
4515 PyCompilerFlags cf;
4516 if (name == NULL)
4517 return -1;
4518 cf.cf_flags = 0;
4519 if (PyEval_MergeCompilerFlags(&cf))
4520 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4521 locals, &cf);
4522 else
4523 v = PyRun_File(fp, name, Py_file_input, globals,
4524 locals);
4526 else {
4527 PyObject *tmp = NULL;
4528 char *str;
4529 PyCompilerFlags cf;
4530 cf.cf_flags = 0;
4531 #ifdef Py_USING_UNICODE
4532 if (PyUnicode_Check(prog)) {
4533 tmp = PyUnicode_AsUTF8String(prog);
4534 if (tmp == NULL)
4535 return -1;
4536 prog = tmp;
4537 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4539 #endif
4540 if (PyString_AsStringAndSize(prog, &str, NULL))
4541 return -1;
4542 if (PyEval_MergeCompilerFlags(&cf))
4543 v = PyRun_StringFlags(str, Py_file_input, globals,
4544 locals, &cf);
4545 else
4546 v = PyRun_String(str, Py_file_input, globals, locals);
4547 Py_XDECREF(tmp);
4549 if (plain)
4550 PyFrame_LocalsToFast(f, 0);
4551 if (v == NULL)
4552 return -1;
4553 Py_DECREF(v);
4554 return 0;
4557 static void
4558 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4560 char *obj_str;
4562 if (!obj)
4563 return;
4565 obj_str = PyString_AsString(obj);
4566 if (!obj_str)
4567 return;
4569 PyErr_Format(exc, format_str, obj_str);
4572 static PyObject *
4573 string_concatenate(PyObject *v, PyObject *w,
4574 PyFrameObject *f, unsigned char *next_instr)
4576 /* This function implements 'variable += expr' when both arguments
4577 are strings. */
4578 Py_ssize_t v_len = PyString_GET_SIZE(v);
4579 Py_ssize_t w_len = PyString_GET_SIZE(w);
4580 Py_ssize_t new_len = v_len + w_len;
4581 if (new_len < 0) {
4582 PyErr_SetString(PyExc_OverflowError,
4583 "strings are too large to concat");
4584 return NULL;
4587 if (v->ob_refcnt == 2) {
4588 /* In the common case, there are 2 references to the value
4589 * stored in 'variable' when the += is performed: one on the
4590 * value stack (in 'v') and one still stored in the
4591 * 'variable'. We try to delete the variable now to reduce
4592 * the refcnt to 1.
4594 switch (*next_instr) {
4595 case STORE_FAST:
4597 int oparg = PEEKARG();
4598 PyObject **fastlocals = f->f_localsplus;
4599 if (GETLOCAL(oparg) == v)
4600 SETLOCAL(oparg, NULL);
4601 break;
4603 case STORE_DEREF:
4605 PyObject **freevars = (f->f_localsplus +
4606 f->f_code->co_nlocals);
4607 PyObject *c = freevars[PEEKARG()];
4608 if (PyCell_GET(c) == v)
4609 PyCell_Set(c, NULL);
4610 break;
4612 case STORE_NAME:
4614 PyObject *names = f->f_code->co_names;
4615 PyObject *name = GETITEM(names, PEEKARG());
4616 PyObject *locals = f->f_locals;
4617 if (PyDict_CheckExact(locals) &&
4618 PyDict_GetItem(locals, name) == v) {
4619 if (PyDict_DelItem(locals, name) != 0) {
4620 PyErr_Clear();
4623 break;
4628 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4629 /* Now we own the last reference to 'v', so we can resize it
4630 * in-place.
4632 if (_PyString_Resize(&v, new_len) != 0) {
4633 /* XXX if _PyString_Resize() fails, 'v' has been
4634 * deallocated so it cannot be put back into
4635 * 'variable'. The MemoryError is raised when there
4636 * is no value in 'variable', which might (very
4637 * remotely) be a cause of incompatibilities.
4639 return NULL;
4641 /* copy 'w' into the newly allocated area of 'v' */
4642 memcpy(PyString_AS_STRING(v) + v_len,
4643 PyString_AS_STRING(w), w_len);
4644 return v;
4646 else {
4647 /* When in-place resizing is not an option. */
4648 PyString_Concat(&v, w);
4649 return v;
4653 #ifdef DYNAMIC_EXECUTION_PROFILE
4655 static PyObject *
4656 getarray(long a[256])
4658 int i;
4659 PyObject *l = PyList_New(256);
4660 if (l == NULL) return NULL;
4661 for (i = 0; i < 256; i++) {
4662 PyObject *x = PyInt_FromLong(a[i]);
4663 if (x == NULL) {
4664 Py_DECREF(l);
4665 return NULL;
4667 PyList_SetItem(l, i, x);
4669 for (i = 0; i < 256; i++)
4670 a[i] = 0;
4671 return l;
4674 PyObject *
4675 _Py_GetDXProfile(PyObject *self, PyObject *args)
4677 #ifndef DXPAIRS
4678 return getarray(dxp);
4679 #else
4680 int i;
4681 PyObject *l = PyList_New(257);
4682 if (l == NULL) return NULL;
4683 for (i = 0; i < 257; i++) {
4684 PyObject *x = getarray(dxpairs[i]);
4685 if (x == NULL) {
4686 Py_DECREF(l);
4687 return NULL;
4689 PyList_SetItem(l, i, x);
4691 return l;
4692 #endif
4695 #endif