Update itertools recipes to use next().
[python.git] / Python / ceval.c
blob419ecfaaaaac53862a0aec91e4e54376abf94d2d
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 *);
130 static PyObject * kwd_as_string(PyObject *);
132 #define NAME_ERROR_MSG \
133 "name '%.200s' is not defined"
134 #define GLOBAL_NAME_ERROR_MSG \
135 "global name '%.200s' is not defined"
136 #define UNBOUNDLOCAL_ERROR_MSG \
137 "local variable '%.200s' referenced before assignment"
138 #define UNBOUNDFREE_ERROR_MSG \
139 "free variable '%.200s' referenced before assignment" \
140 " in enclosing scope"
142 /* Dynamic execution profile */
143 #ifdef DYNAMIC_EXECUTION_PROFILE
144 #ifdef DXPAIRS
145 static long dxpairs[257][256];
146 #define dxp dxpairs[256]
147 #else
148 static long dxp[256];
149 #endif
150 #endif
152 /* Function call profile */
153 #ifdef CALL_PROFILE
154 #define PCALL_NUM 11
155 static int pcall[PCALL_NUM];
157 #define PCALL_ALL 0
158 #define PCALL_FUNCTION 1
159 #define PCALL_FAST_FUNCTION 2
160 #define PCALL_FASTER_FUNCTION 3
161 #define PCALL_METHOD 4
162 #define PCALL_BOUND_METHOD 5
163 #define PCALL_CFUNCTION 6
164 #define PCALL_TYPE 7
165 #define PCALL_GENERATOR 8
166 #define PCALL_OTHER 9
167 #define PCALL_POP 10
169 /* Notes about the statistics
171 PCALL_FAST stats
173 FAST_FUNCTION means no argument tuple needs to be created.
174 FASTER_FUNCTION means that the fast-path frame setup code is used.
176 If there is a method call where the call can be optimized by changing
177 the argument tuple and calling the function directly, it gets recorded
178 twice.
180 As a result, the relationship among the statistics appears to be
181 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
182 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
183 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
184 PCALL_METHOD > PCALL_BOUND_METHOD
187 #define PCALL(POS) pcall[POS]++
189 PyObject *
190 PyEval_GetCallStats(PyObject *self)
192 return Py_BuildValue("iiiiiiiiiii",
193 pcall[0], pcall[1], pcall[2], pcall[3],
194 pcall[4], pcall[5], pcall[6], pcall[7],
195 pcall[8], pcall[9], pcall[10]);
197 #else
198 #define PCALL(O)
200 PyObject *
201 PyEval_GetCallStats(PyObject *self)
203 Py_INCREF(Py_None);
204 return Py_None;
206 #endif
209 #ifdef WITH_THREAD
211 #ifdef HAVE_ERRNO_H
212 #include <errno.h>
213 #endif
214 #include "pythread.h"
216 static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
217 static PyThread_type_lock pending_lock = 0; /* for pending calls */
218 static long main_thread = 0;
221 PyEval_ThreadsInitialized(void)
223 return interpreter_lock != 0;
226 void
227 PyEval_InitThreads(void)
229 if (interpreter_lock)
230 return;
231 interpreter_lock = PyThread_allocate_lock();
232 PyThread_acquire_lock(interpreter_lock, 1);
233 main_thread = PyThread_get_thread_ident();
236 void
237 PyEval_AcquireLock(void)
239 PyThread_acquire_lock(interpreter_lock, 1);
242 void
243 PyEval_ReleaseLock(void)
245 PyThread_release_lock(interpreter_lock);
248 void
249 PyEval_AcquireThread(PyThreadState *tstate)
251 if (tstate == NULL)
252 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
253 /* Check someone has called PyEval_InitThreads() to create the lock */
254 assert(interpreter_lock);
255 PyThread_acquire_lock(interpreter_lock, 1);
256 if (PyThreadState_Swap(tstate) != NULL)
257 Py_FatalError(
258 "PyEval_AcquireThread: non-NULL old thread state");
261 void
262 PyEval_ReleaseThread(PyThreadState *tstate)
264 if (tstate == NULL)
265 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
266 if (PyThreadState_Swap(NULL) != tstate)
267 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
268 PyThread_release_lock(interpreter_lock);
271 /* This function is called from PyOS_AfterFork to ensure that newly
272 created child processes don't hold locks referring to threads which
273 are not running in the child process. (This could also be done using
274 pthread_atfork mechanism, at least for the pthreads implementation.) */
276 void
277 PyEval_ReInitThreads(void)
279 PyObject *threading, *result;
280 PyThreadState *tstate;
282 if (!interpreter_lock)
283 return;
284 /*XXX Can't use PyThread_free_lock here because it does too
285 much error-checking. Doing this cleanly would require
286 adding a new function to each thread_*.h. Instead, just
287 create a new lock and waste a little bit of memory */
288 interpreter_lock = PyThread_allocate_lock();
289 pending_lock = PyThread_allocate_lock();
290 PyThread_acquire_lock(interpreter_lock, 1);
291 main_thread = PyThread_get_thread_ident();
293 /* Update the threading module with the new state.
295 tstate = PyThreadState_GET();
296 threading = PyMapping_GetItemString(tstate->interp->modules,
297 "threading");
298 if (threading == NULL) {
299 /* threading not imported */
300 PyErr_Clear();
301 return;
303 result = PyObject_CallMethod(threading, "_after_fork", NULL);
304 if (result == NULL)
305 PyErr_WriteUnraisable(threading);
306 else
307 Py_DECREF(result);
308 Py_DECREF(threading);
310 #endif
312 /* Functions save_thread and restore_thread are always defined so
313 dynamically loaded modules needn't be compiled separately for use
314 with and without threads: */
316 PyThreadState *
317 PyEval_SaveThread(void)
319 PyThreadState *tstate = PyThreadState_Swap(NULL);
320 if (tstate == NULL)
321 Py_FatalError("PyEval_SaveThread: NULL tstate");
322 #ifdef WITH_THREAD
323 if (interpreter_lock)
324 PyThread_release_lock(interpreter_lock);
325 #endif
326 return tstate;
329 void
330 PyEval_RestoreThread(PyThreadState *tstate)
332 if (tstate == NULL)
333 Py_FatalError("PyEval_RestoreThread: NULL tstate");
334 #ifdef WITH_THREAD
335 if (interpreter_lock) {
336 int err = errno;
337 PyThread_acquire_lock(interpreter_lock, 1);
338 errno = err;
340 #endif
341 PyThreadState_Swap(tstate);
345 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
346 signal handlers or Mac I/O completion routines) can schedule calls
347 to a function to be called synchronously.
348 The synchronous function is called with one void* argument.
349 It should return 0 for success or -1 for failure -- failure should
350 be accompanied by an exception.
352 If registry succeeds, the registry function returns 0; if it fails
353 (e.g. due to too many pending calls) it returns -1 (without setting
354 an exception condition).
356 Note that because registry may occur from within signal handlers,
357 or other asynchronous events, calling malloc() is unsafe!
359 #ifdef WITH_THREAD
360 Any thread can schedule pending calls, but only the main thread
361 will execute them.
362 There is no facility to schedule calls to a particular thread, but
363 that should be easy to change, should that ever be required. In
364 that case, the static variables here should go into the python
365 threadstate.
366 #endif
369 #ifdef WITH_THREAD
371 /* The WITH_THREAD implementation is thread-safe. It allows
372 scheduling to be made from any thread, and even from an executing
373 callback.
376 #define NPENDINGCALLS 32
377 static struct {
378 int (*func)(void *);
379 void *arg;
380 } pendingcalls[NPENDINGCALLS];
381 static int pendingfirst = 0;
382 static int pendinglast = 0;
383 static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
384 static char pendingbusy = 0;
387 Py_AddPendingCall(int (*func)(void *), void *arg)
389 int i, j, result=0;
390 PyThread_type_lock lock = pending_lock;
392 /* try a few times for the lock. Since this mechanism is used
393 * for signal handling (on the main thread), there is a (slim)
394 * chance that a signal is delivered on the same thread while we
395 * hold the lock during the Py_MakePendingCalls() function.
396 * This avoids a deadlock in that case.
397 * Note that signals can be delivered on any thread. In particular,
398 * on Windows, a SIGINT is delivered on a system-created worker
399 * thread.
400 * We also check for lock being NULL, in the unlikely case that
401 * this function is called before any bytecode evaluation takes place.
403 if (lock != NULL) {
404 for (i = 0; i<100; i++) {
405 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
406 break;
408 if (i == 100)
409 return -1;
412 i = pendinglast;
413 j = (i + 1) % NPENDINGCALLS;
414 if (j == pendingfirst) {
415 result = -1; /* Queue full */
416 } else {
417 pendingcalls[i].func = func;
418 pendingcalls[i].arg = arg;
419 pendinglast = j;
421 /* signal main loop */
422 _Py_Ticker = 0;
423 pendingcalls_to_do = 1;
424 if (lock != NULL)
425 PyThread_release_lock(lock);
426 return result;
430 Py_MakePendingCalls(void)
432 int i;
433 int r = 0;
435 if (!pending_lock) {
436 /* initial allocation of the lock */
437 pending_lock = PyThread_allocate_lock();
438 if (pending_lock == NULL)
439 return -1;
442 /* only service pending calls on main thread */
443 if (main_thread && PyThread_get_thread_ident() != main_thread)
444 return 0;
445 /* don't perform recursive pending calls */
446 if (pendingbusy)
447 return 0;
448 pendingbusy = 1;
449 /* perform a bounded number of calls, in case of recursion */
450 for (i=0; i<NPENDINGCALLS; i++) {
451 int j;
452 int (*func)(void *);
453 void *arg = NULL;
455 /* pop one item off the queue while holding the lock */
456 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
457 j = pendingfirst;
458 if (j == pendinglast) {
459 func = NULL; /* Queue empty */
460 } else {
461 func = pendingcalls[j].func;
462 arg = pendingcalls[j].arg;
463 pendingfirst = (j + 1) % NPENDINGCALLS;
465 pendingcalls_to_do = pendingfirst != pendinglast;
466 PyThread_release_lock(pending_lock);
467 /* having released the lock, perform the callback */
468 if (func == NULL)
469 break;
470 r = func(arg);
471 if (r)
472 break;
474 pendingbusy = 0;
475 return r;
478 #else /* if ! defined WITH_THREAD */
481 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
482 This code is used for signal handling in python that isn't built
483 with WITH_THREAD.
484 Don't use this implementation when Py_AddPendingCalls() can happen
485 on a different thread!
487 There are two possible race conditions:
488 (1) nested asynchronous calls to Py_AddPendingCall()
489 (2) AddPendingCall() calls made while pending calls are being processed.
491 (1) is very unlikely because typically signal delivery
492 is blocked during signal handling. So it should be impossible.
493 (2) is a real possibility.
494 The current code is safe against (2), but not against (1).
495 The safety against (2) is derived from the fact that only one
496 thread is present, interrupted by signals, and that the critical
497 section is protected with the "busy" variable. On Windows, which
498 delivers SIGINT on a system thread, this does not hold and therefore
499 Windows really shouldn't use this version.
500 The two threads could theoretically wiggle around the "busy" variable.
503 #define NPENDINGCALLS 32
504 static struct {
505 int (*func)(void *);
506 void *arg;
507 } pendingcalls[NPENDINGCALLS];
508 static volatile int pendingfirst = 0;
509 static volatile int pendinglast = 0;
510 static volatile int pendingcalls_to_do = 0;
513 Py_AddPendingCall(int (*func)(void *), void *arg)
515 static volatile int busy = 0;
516 int i, j;
517 /* XXX Begin critical section */
518 if (busy)
519 return -1;
520 busy = 1;
521 i = pendinglast;
522 j = (i + 1) % NPENDINGCALLS;
523 if (j == pendingfirst) {
524 busy = 0;
525 return -1; /* Queue full */
527 pendingcalls[i].func = func;
528 pendingcalls[i].arg = arg;
529 pendinglast = j;
531 _Py_Ticker = 0;
532 pendingcalls_to_do = 1; /* Signal main loop */
533 busy = 0;
534 /* XXX End critical section */
535 return 0;
539 Py_MakePendingCalls(void)
541 static int busy = 0;
542 if (busy)
543 return 0;
544 busy = 1;
545 pendingcalls_to_do = 0;
546 for (;;) {
547 int i;
548 int (*func)(void *);
549 void *arg;
550 i = pendingfirst;
551 if (i == pendinglast)
552 break; /* Queue empty */
553 func = pendingcalls[i].func;
554 arg = pendingcalls[i].arg;
555 pendingfirst = (i + 1) % NPENDINGCALLS;
556 if (func(arg) < 0) {
557 busy = 0;
558 pendingcalls_to_do = 1; /* We're not done yet */
559 return -1;
562 busy = 0;
563 return 0;
566 #endif /* WITH_THREAD */
569 /* The interpreter's recursion limit */
571 #ifndef Py_DEFAULT_RECURSION_LIMIT
572 #define Py_DEFAULT_RECURSION_LIMIT 1000
573 #endif
574 static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
575 int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
578 Py_GetRecursionLimit(void)
580 return recursion_limit;
583 void
584 Py_SetRecursionLimit(int new_limit)
586 recursion_limit = new_limit;
587 _Py_CheckRecursionLimit = recursion_limit;
590 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
591 if the recursion_depth reaches _Py_CheckRecursionLimit.
592 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
593 to guarantee that _Py_CheckRecursiveCall() is regularly called.
594 Without USE_STACKCHECK, there is no need for this. */
596 _Py_CheckRecursiveCall(char *where)
598 PyThreadState *tstate = PyThreadState_GET();
600 #ifdef USE_STACKCHECK
601 if (PyOS_CheckStack()) {
602 --tstate->recursion_depth;
603 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
604 return -1;
606 #endif
607 if (tstate->recursion_depth > recursion_limit) {
608 --tstate->recursion_depth;
609 PyErr_Format(PyExc_RuntimeError,
610 "maximum recursion depth exceeded%s",
611 where);
612 return -1;
614 _Py_CheckRecursionLimit = recursion_limit;
615 return 0;
618 /* Status code for main loop (reason for stack unwind) */
619 enum why_code {
620 WHY_NOT = 0x0001, /* No error */
621 WHY_EXCEPTION = 0x0002, /* Exception occurred */
622 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
623 WHY_RETURN = 0x0008, /* 'return' statement */
624 WHY_BREAK = 0x0010, /* 'break' statement */
625 WHY_CONTINUE = 0x0020, /* 'continue' statement */
626 WHY_YIELD = 0x0040 /* 'yield' operator */
629 static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
630 static int unpack_iterable(PyObject *, int, PyObject **);
632 /* Records whether tracing is on for any thread. Counts the number of
633 threads for which tstate->c_tracefunc is non-NULL, so if the value
634 is 0, we know we don't have to check this thread's c_tracefunc.
635 This speeds up the if statement in PyEval_EvalFrameEx() after
636 fast_next_opcode*/
637 static int _Py_TracingPossible = 0;
639 /* for manipulating the thread switch and periodic "stuff" - used to be
640 per thread, now just a pair o' globals */
641 int _Py_CheckInterval = 100;
642 volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
644 PyObject *
645 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
647 return PyEval_EvalCodeEx(co,
648 globals, locals,
649 (PyObject **)NULL, 0,
650 (PyObject **)NULL, 0,
651 (PyObject **)NULL, 0,
652 NULL);
656 /* Interpreter main loop */
658 PyObject *
659 PyEval_EvalFrame(PyFrameObject *f) {
660 /* This is for backward compatibility with extension modules that
661 used this API; core interpreter code should call
662 PyEval_EvalFrameEx() */
663 return PyEval_EvalFrameEx(f, 0);
666 PyObject *
667 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
669 #ifdef DXPAIRS
670 int lastopcode = 0;
671 #endif
672 register PyObject **stack_pointer; /* Next free slot in value stack */
673 register unsigned char *next_instr;
674 register int opcode; /* Current opcode */
675 register int oparg; /* Current opcode argument, if any */
676 register enum why_code why; /* Reason for block stack unwind */
677 register int err; /* Error status -- nonzero if error */
678 register PyObject *x; /* Result object -- NULL if error */
679 register PyObject *v; /* Temporary objects popped off stack */
680 register PyObject *w;
681 register PyObject *u;
682 register PyObject *t;
683 register PyObject *stream = NULL; /* for PRINT opcodes */
684 register PyObject **fastlocals, **freevars;
685 PyObject *retval = NULL; /* Return value */
686 PyThreadState *tstate = PyThreadState_GET();
687 PyCodeObject *co;
689 /* when tracing we set things up so that
691 not (instr_lb <= current_bytecode_offset < instr_ub)
693 is true when the line being executed has changed. The
694 initial values are such as to make this false the first
695 time it is tested. */
696 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
698 unsigned char *first_instr;
699 PyObject *names;
700 PyObject *consts;
701 #if defined(Py_DEBUG) || defined(LLTRACE)
702 /* Make it easier to find out where we are with a debugger */
703 char *filename;
704 #endif
706 /* Tuple access macros */
708 #ifndef Py_DEBUG
709 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
710 #else
711 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
712 #endif
714 #ifdef WITH_TSC
715 /* Use Pentium timestamp counter to mark certain events:
716 inst0 -- beginning of switch statement for opcode dispatch
717 inst1 -- end of switch statement (may be skipped)
718 loop0 -- the top of the mainloop
719 loop1 -- place where control returns again to top of mainloop
720 (may be skipped)
721 intr1 -- beginning of long interruption
722 intr2 -- end of long interruption
724 Many opcodes call out to helper C functions. In some cases, the
725 time in those functions should be counted towards the time for the
726 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
727 calls another Python function; there's no point in charge all the
728 bytecode executed by the called function to the caller.
730 It's hard to make a useful judgement statically. In the presence
731 of operator overloading, it's impossible to tell if a call will
732 execute new Python code or not.
734 It's a case-by-case judgement. I'll use intr1 for the following
735 cases:
737 EXEC_STMT
738 IMPORT_STAR
739 IMPORT_FROM
740 CALL_FUNCTION (and friends)
743 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
744 int ticked = 0;
746 READ_TIMESTAMP(inst0);
747 READ_TIMESTAMP(inst1);
748 READ_TIMESTAMP(loop0);
749 READ_TIMESTAMP(loop1);
751 /* shut up the compiler */
752 opcode = 0;
753 #endif
755 /* Code access macros */
757 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
758 #define NEXTOP() (*next_instr++)
759 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
760 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
761 #define JUMPTO(x) (next_instr = first_instr + (x))
762 #define JUMPBY(x) (next_instr += (x))
764 /* OpCode prediction macros
765 Some opcodes tend to come in pairs thus making it possible to
766 predict the second code when the first is run. For example,
767 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
768 those opcodes are often followed by a POP_TOP.
770 Verifying the prediction costs a single high-speed test of a register
771 variable against a constant. If the pairing was good, then the
772 processor's own internal branch predication has a high likelihood of
773 success, resulting in a nearly zero-overhead transition to the
774 next opcode. A successful prediction saves a trip through the eval-loop
775 including its two unpredictable branches, the HAS_ARG test and the
776 switch-case. Combined with the processor's internal branch prediction,
777 a successful PREDICT has the effect of making the two opcodes run as if
778 they were a single new opcode with the bodies combined.
780 If collecting opcode statistics, your choices are to either keep the
781 predictions turned-on and interpret the results as if some opcodes
782 had been combined or turn-off predictions so that the opcode frequency
783 counter updates for both opcodes.
786 #ifdef DYNAMIC_EXECUTION_PROFILE
787 #define PREDICT(op) if (0) goto PRED_##op
788 #else
789 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
790 #endif
792 #define PREDICTED(op) PRED_##op: next_instr++
793 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
795 /* Stack manipulation macros */
797 /* The stack can grow at most MAXINT deep, as co_nlocals and
798 co_stacksize are ints. */
799 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
800 #define EMPTY() (STACK_LEVEL() == 0)
801 #define TOP() (stack_pointer[-1])
802 #define SECOND() (stack_pointer[-2])
803 #define THIRD() (stack_pointer[-3])
804 #define FOURTH() (stack_pointer[-4])
805 #define SET_TOP(v) (stack_pointer[-1] = (v))
806 #define SET_SECOND(v) (stack_pointer[-2] = (v))
807 #define SET_THIRD(v) (stack_pointer[-3] = (v))
808 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
809 #define BASIC_STACKADJ(n) (stack_pointer += n)
810 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
811 #define BASIC_POP() (*--stack_pointer)
813 #ifdef LLTRACE
814 #define PUSH(v) { (void)(BASIC_PUSH(v), \
815 lltrace && prtrace(TOP(), "push")); \
816 assert(STACK_LEVEL() <= co->co_stacksize); }
817 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
818 BASIC_POP())
819 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
820 lltrace && prtrace(TOP(), "stackadj")); \
821 assert(STACK_LEVEL() <= co->co_stacksize); }
822 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
823 prtrace((STACK_POINTER)[-1], "ext_pop")), \
824 *--(STACK_POINTER))
825 #else
826 #define PUSH(v) BASIC_PUSH(v)
827 #define POP() BASIC_POP()
828 #define STACKADJ(n) BASIC_STACKADJ(n)
829 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
830 #endif
832 /* Local variable macros */
834 #define GETLOCAL(i) (fastlocals[i])
836 /* The SETLOCAL() macro must not DECREF the local variable in-place and
837 then store the new value; it must copy the old value to a temporary
838 value, then store the new value, and then DECREF the temporary value.
839 This is because it is possible that during the DECREF the frame is
840 accessed by other code (e.g. a __del__ method or gc.collect()) and the
841 variable would be pointing to already-freed memory. */
842 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
843 GETLOCAL(i) = value; \
844 Py_XDECREF(tmp); } while (0)
846 /* Start of code */
848 if (f == NULL)
849 return NULL;
851 /* push frame */
852 if (Py_EnterRecursiveCall(""))
853 return NULL;
855 tstate->frame = f;
857 if (tstate->use_tracing) {
858 if (tstate->c_tracefunc != NULL) {
859 /* tstate->c_tracefunc, if defined, is a
860 function that will be called on *every* entry
861 to a code block. Its return value, if not
862 None, is a function that will be called at
863 the start of each executed line of code.
864 (Actually, the function must return itself
865 in order to continue tracing.) The trace
866 functions are called with three arguments:
867 a pointer to the current frame, a string
868 indicating why the function is called, and
869 an argument which depends on the situation.
870 The global trace function is also called
871 whenever an exception is detected. */
872 if (call_trace_protected(tstate->c_tracefunc,
873 tstate->c_traceobj,
874 f, PyTrace_CALL, Py_None)) {
875 /* Trace function raised an error */
876 goto exit_eval_frame;
879 if (tstate->c_profilefunc != NULL) {
880 /* Similar for c_profilefunc, except it needn't
881 return itself and isn't called for "line" events */
882 if (call_trace_protected(tstate->c_profilefunc,
883 tstate->c_profileobj,
884 f, PyTrace_CALL, Py_None)) {
885 /* Profile function raised an error */
886 goto exit_eval_frame;
891 co = f->f_code;
892 names = co->co_names;
893 consts = co->co_consts;
894 fastlocals = f->f_localsplus;
895 freevars = f->f_localsplus + co->co_nlocals;
896 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
897 /* An explanation is in order for the next line.
899 f->f_lasti now refers to the index of the last instruction
900 executed. You might think this was obvious from the name, but
901 this wasn't always true before 2.3! PyFrame_New now sets
902 f->f_lasti to -1 (i.e. the index *before* the first instruction)
903 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
904 does work. Promise.
906 When the PREDICT() macros are enabled, some opcode pairs follow in
907 direct succession without updating f->f_lasti. A successful
908 prediction effectively links the two codes together as if they
909 were a single new opcode; accordingly,f->f_lasti will point to
910 the first code in the pair (for instance, GET_ITER followed by
911 FOR_ITER is effectively a single opcode and f->f_lasti will point
912 at to the beginning of the combined pair.)
914 next_instr = first_instr + f->f_lasti + 1;
915 stack_pointer = f->f_stacktop;
916 assert(stack_pointer != NULL);
917 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
919 #ifdef LLTRACE
920 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
921 #endif
922 #if defined(Py_DEBUG) || defined(LLTRACE)
923 filename = PyString_AsString(co->co_filename);
924 #endif
926 why = WHY_NOT;
927 err = 0;
928 x = Py_None; /* Not a reference, just anything non-NULL */
929 w = NULL;
931 if (throwflag) { /* support for generator.throw() */
932 why = WHY_EXCEPTION;
933 goto on_error;
936 for (;;) {
937 #ifdef WITH_TSC
938 if (inst1 == 0) {
939 /* Almost surely, the opcode executed a break
940 or a continue, preventing inst1 from being set
941 on the way out of the loop.
943 READ_TIMESTAMP(inst1);
944 loop1 = inst1;
946 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
947 intr0, intr1);
948 ticked = 0;
949 inst1 = 0;
950 intr0 = 0;
951 intr1 = 0;
952 READ_TIMESTAMP(loop0);
953 #endif
954 assert(stack_pointer >= f->f_valuestack); /* else underflow */
955 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
957 /* Do periodic things. Doing this every time through
958 the loop would add too much overhead, so we do it
959 only every Nth instruction. We also do it if
960 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
961 event needs attention (e.g. a signal handler or
962 async I/O handler); see Py_AddPendingCall() and
963 Py_MakePendingCalls() above. */
965 if (--_Py_Ticker < 0) {
966 if (*next_instr == SETUP_FINALLY) {
967 /* Make the last opcode before
968 a try: finally: block uninterruptable. */
969 goto fast_next_opcode;
971 _Py_Ticker = _Py_CheckInterval;
972 tstate->tick_counter++;
973 #ifdef WITH_TSC
974 ticked = 1;
975 #endif
976 if (pendingcalls_to_do) {
977 if (Py_MakePendingCalls() < 0) {
978 why = WHY_EXCEPTION;
979 goto on_error;
981 if (pendingcalls_to_do)
982 /* MakePendingCalls() didn't succeed.
983 Force early re-execution of this
984 "periodic" code, possibly after
985 a thread switch */
986 _Py_Ticker = 0;
988 #ifdef WITH_THREAD
989 if (interpreter_lock) {
990 /* Give another thread a chance */
992 if (PyThreadState_Swap(NULL) != tstate)
993 Py_FatalError("ceval: tstate mix-up");
994 PyThread_release_lock(interpreter_lock);
996 /* Other threads may run now */
998 PyThread_acquire_lock(interpreter_lock, 1);
999 if (PyThreadState_Swap(tstate) != NULL)
1000 Py_FatalError("ceval: orphan tstate");
1002 /* Check for thread interrupts */
1004 if (tstate->async_exc != NULL) {
1005 x = tstate->async_exc;
1006 tstate->async_exc = NULL;
1007 PyErr_SetNone(x);
1008 Py_DECREF(x);
1009 why = WHY_EXCEPTION;
1010 goto on_error;
1013 #endif
1016 fast_next_opcode:
1017 f->f_lasti = INSTR_OFFSET();
1019 /* line-by-line tracing support */
1021 if (_Py_TracingPossible &&
1022 tstate->c_tracefunc != NULL && !tstate->tracing) {
1023 /* see maybe_call_line_trace
1024 for expository comments */
1025 f->f_stacktop = stack_pointer;
1027 err = maybe_call_line_trace(tstate->c_tracefunc,
1028 tstate->c_traceobj,
1029 f, &instr_lb, &instr_ub,
1030 &instr_prev);
1031 /* Reload possibly changed frame fields */
1032 JUMPTO(f->f_lasti);
1033 if (f->f_stacktop != NULL) {
1034 stack_pointer = f->f_stacktop;
1035 f->f_stacktop = NULL;
1037 if (err) {
1038 /* trace function raised an exception */
1039 goto on_error;
1043 /* Extract opcode and argument */
1045 opcode = NEXTOP();
1046 oparg = 0; /* allows oparg to be stored in a register because
1047 it doesn't have to be remembered across a full loop */
1048 if (HAS_ARG(opcode))
1049 oparg = NEXTARG();
1050 dispatch_opcode:
1051 #ifdef DYNAMIC_EXECUTION_PROFILE
1052 #ifdef DXPAIRS
1053 dxpairs[lastopcode][opcode]++;
1054 lastopcode = opcode;
1055 #endif
1056 dxp[opcode]++;
1057 #endif
1059 #ifdef LLTRACE
1060 /* Instruction tracing */
1062 if (lltrace) {
1063 if (HAS_ARG(opcode)) {
1064 printf("%d: %d, %d\n",
1065 f->f_lasti, opcode, oparg);
1067 else {
1068 printf("%d: %d\n",
1069 f->f_lasti, opcode);
1072 #endif
1074 /* Main switch on opcode */
1075 READ_TIMESTAMP(inst0);
1077 switch (opcode) {
1079 /* BEWARE!
1080 It is essential that any operation that fails sets either
1081 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1082 and that no operation that succeeds does this! */
1084 /* case STOP_CODE: this is an error! */
1086 case NOP:
1087 goto fast_next_opcode;
1089 case LOAD_FAST:
1090 x = GETLOCAL(oparg);
1091 if (x != NULL) {
1092 Py_INCREF(x);
1093 PUSH(x);
1094 goto fast_next_opcode;
1096 format_exc_check_arg(PyExc_UnboundLocalError,
1097 UNBOUNDLOCAL_ERROR_MSG,
1098 PyTuple_GetItem(co->co_varnames, oparg));
1099 break;
1101 case LOAD_CONST:
1102 x = GETITEM(consts, oparg);
1103 Py_INCREF(x);
1104 PUSH(x);
1105 goto fast_next_opcode;
1107 PREDICTED_WITH_ARG(STORE_FAST);
1108 case STORE_FAST:
1109 v = POP();
1110 SETLOCAL(oparg, v);
1111 goto fast_next_opcode;
1113 PREDICTED(POP_TOP);
1114 case POP_TOP:
1115 v = POP();
1116 Py_DECREF(v);
1117 goto fast_next_opcode;
1119 case ROT_TWO:
1120 v = TOP();
1121 w = SECOND();
1122 SET_TOP(w);
1123 SET_SECOND(v);
1124 goto fast_next_opcode;
1126 case ROT_THREE:
1127 v = TOP();
1128 w = SECOND();
1129 x = THIRD();
1130 SET_TOP(w);
1131 SET_SECOND(x);
1132 SET_THIRD(v);
1133 goto fast_next_opcode;
1135 case ROT_FOUR:
1136 u = TOP();
1137 v = SECOND();
1138 w = THIRD();
1139 x = FOURTH();
1140 SET_TOP(v);
1141 SET_SECOND(w);
1142 SET_THIRD(x);
1143 SET_FOURTH(u);
1144 goto fast_next_opcode;
1146 case DUP_TOP:
1147 v = TOP();
1148 Py_INCREF(v);
1149 PUSH(v);
1150 goto fast_next_opcode;
1152 case DUP_TOPX:
1153 if (oparg == 2) {
1154 x = TOP();
1155 Py_INCREF(x);
1156 w = SECOND();
1157 Py_INCREF(w);
1158 STACKADJ(2);
1159 SET_TOP(x);
1160 SET_SECOND(w);
1161 goto fast_next_opcode;
1162 } else if (oparg == 3) {
1163 x = TOP();
1164 Py_INCREF(x);
1165 w = SECOND();
1166 Py_INCREF(w);
1167 v = THIRD();
1168 Py_INCREF(v);
1169 STACKADJ(3);
1170 SET_TOP(x);
1171 SET_SECOND(w);
1172 SET_THIRD(v);
1173 goto fast_next_opcode;
1175 Py_FatalError("invalid argument to DUP_TOPX"
1176 " (bytecode corruption?)");
1177 /* Never returns, so don't bother to set why. */
1178 break;
1180 case UNARY_POSITIVE:
1181 v = TOP();
1182 x = PyNumber_Positive(v);
1183 Py_DECREF(v);
1184 SET_TOP(x);
1185 if (x != NULL) continue;
1186 break;
1188 case UNARY_NEGATIVE:
1189 v = TOP();
1190 x = PyNumber_Negative(v);
1191 Py_DECREF(v);
1192 SET_TOP(x);
1193 if (x != NULL) continue;
1194 break;
1196 case UNARY_NOT:
1197 v = TOP();
1198 err = PyObject_IsTrue(v);
1199 Py_DECREF(v);
1200 if (err == 0) {
1201 Py_INCREF(Py_True);
1202 SET_TOP(Py_True);
1203 continue;
1205 else if (err > 0) {
1206 Py_INCREF(Py_False);
1207 SET_TOP(Py_False);
1208 err = 0;
1209 continue;
1211 STACKADJ(-1);
1212 break;
1214 case UNARY_CONVERT:
1215 v = TOP();
1216 x = PyObject_Repr(v);
1217 Py_DECREF(v);
1218 SET_TOP(x);
1219 if (x != NULL) continue;
1220 break;
1222 case UNARY_INVERT:
1223 v = TOP();
1224 x = PyNumber_Invert(v);
1225 Py_DECREF(v);
1226 SET_TOP(x);
1227 if (x != NULL) continue;
1228 break;
1230 case BINARY_POWER:
1231 w = POP();
1232 v = TOP();
1233 x = PyNumber_Power(v, w, Py_None);
1234 Py_DECREF(v);
1235 Py_DECREF(w);
1236 SET_TOP(x);
1237 if (x != NULL) continue;
1238 break;
1240 case BINARY_MULTIPLY:
1241 w = POP();
1242 v = TOP();
1243 x = PyNumber_Multiply(v, w);
1244 Py_DECREF(v);
1245 Py_DECREF(w);
1246 SET_TOP(x);
1247 if (x != NULL) continue;
1248 break;
1250 case BINARY_DIVIDE:
1251 if (!_Py_QnewFlag) {
1252 w = POP();
1253 v = TOP();
1254 x = PyNumber_Divide(v, w);
1255 Py_DECREF(v);
1256 Py_DECREF(w);
1257 SET_TOP(x);
1258 if (x != NULL) continue;
1259 break;
1261 /* -Qnew is in effect: fall through to
1262 BINARY_TRUE_DIVIDE */
1263 case BINARY_TRUE_DIVIDE:
1264 w = POP();
1265 v = TOP();
1266 x = PyNumber_TrueDivide(v, w);
1267 Py_DECREF(v);
1268 Py_DECREF(w);
1269 SET_TOP(x);
1270 if (x != NULL) continue;
1271 break;
1273 case BINARY_FLOOR_DIVIDE:
1274 w = POP();
1275 v = TOP();
1276 x = PyNumber_FloorDivide(v, w);
1277 Py_DECREF(v);
1278 Py_DECREF(w);
1279 SET_TOP(x);
1280 if (x != NULL) continue;
1281 break;
1283 case BINARY_MODULO:
1284 w = POP();
1285 v = TOP();
1286 if (PyString_CheckExact(v))
1287 x = PyString_Format(v, w);
1288 else
1289 x = PyNumber_Remainder(v, w);
1290 Py_DECREF(v);
1291 Py_DECREF(w);
1292 SET_TOP(x);
1293 if (x != NULL) continue;
1294 break;
1296 case BINARY_ADD:
1297 w = POP();
1298 v = TOP();
1299 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1300 /* INLINE: int + int */
1301 register long a, b, i;
1302 a = PyInt_AS_LONG(v);
1303 b = PyInt_AS_LONG(w);
1304 i = a + b;
1305 if ((i^a) < 0 && (i^b) < 0)
1306 goto slow_add;
1307 x = PyInt_FromLong(i);
1309 else if (PyString_CheckExact(v) &&
1310 PyString_CheckExact(w)) {
1311 x = string_concatenate(v, w, f, next_instr);
1312 /* string_concatenate consumed the ref to v */
1313 goto skip_decref_vx;
1315 else {
1316 slow_add:
1317 x = PyNumber_Add(v, w);
1319 Py_DECREF(v);
1320 skip_decref_vx:
1321 Py_DECREF(w);
1322 SET_TOP(x);
1323 if (x != NULL) continue;
1324 break;
1326 case BINARY_SUBTRACT:
1327 w = POP();
1328 v = TOP();
1329 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1330 /* INLINE: int - int */
1331 register long a, b, i;
1332 a = PyInt_AS_LONG(v);
1333 b = PyInt_AS_LONG(w);
1334 i = a - b;
1335 if ((i^a) < 0 && (i^~b) < 0)
1336 goto slow_sub;
1337 x = PyInt_FromLong(i);
1339 else {
1340 slow_sub:
1341 x = PyNumber_Subtract(v, w);
1343 Py_DECREF(v);
1344 Py_DECREF(w);
1345 SET_TOP(x);
1346 if (x != NULL) continue;
1347 break;
1349 case BINARY_SUBSCR:
1350 w = POP();
1351 v = TOP();
1352 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1353 /* INLINE: list[int] */
1354 Py_ssize_t i = PyInt_AsSsize_t(w);
1355 if (i < 0)
1356 i += PyList_GET_SIZE(v);
1357 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1358 x = PyList_GET_ITEM(v, i);
1359 Py_INCREF(x);
1361 else
1362 goto slow_get;
1364 else
1365 slow_get:
1366 x = PyObject_GetItem(v, w);
1367 Py_DECREF(v);
1368 Py_DECREF(w);
1369 SET_TOP(x);
1370 if (x != NULL) continue;
1371 break;
1373 case BINARY_LSHIFT:
1374 w = POP();
1375 v = TOP();
1376 x = PyNumber_Lshift(v, w);
1377 Py_DECREF(v);
1378 Py_DECREF(w);
1379 SET_TOP(x);
1380 if (x != NULL) continue;
1381 break;
1383 case BINARY_RSHIFT:
1384 w = POP();
1385 v = TOP();
1386 x = PyNumber_Rshift(v, w);
1387 Py_DECREF(v);
1388 Py_DECREF(w);
1389 SET_TOP(x);
1390 if (x != NULL) continue;
1391 break;
1393 case BINARY_AND:
1394 w = POP();
1395 v = TOP();
1396 x = PyNumber_And(v, w);
1397 Py_DECREF(v);
1398 Py_DECREF(w);
1399 SET_TOP(x);
1400 if (x != NULL) continue;
1401 break;
1403 case BINARY_XOR:
1404 w = POP();
1405 v = TOP();
1406 x = PyNumber_Xor(v, w);
1407 Py_DECREF(v);
1408 Py_DECREF(w);
1409 SET_TOP(x);
1410 if (x != NULL) continue;
1411 break;
1413 case BINARY_OR:
1414 w = POP();
1415 v = TOP();
1416 x = PyNumber_Or(v, w);
1417 Py_DECREF(v);
1418 Py_DECREF(w);
1419 SET_TOP(x);
1420 if (x != NULL) continue;
1421 break;
1423 case LIST_APPEND:
1424 w = POP();
1425 v = stack_pointer[-oparg];
1426 err = PyList_Append(v, w);
1427 Py_DECREF(w);
1428 if (err == 0) {
1429 PREDICT(JUMP_ABSOLUTE);
1430 continue;
1432 break;
1434 case INPLACE_POWER:
1435 w = POP();
1436 v = TOP();
1437 x = PyNumber_InPlacePower(v, w, Py_None);
1438 Py_DECREF(v);
1439 Py_DECREF(w);
1440 SET_TOP(x);
1441 if (x != NULL) continue;
1442 break;
1444 case INPLACE_MULTIPLY:
1445 w = POP();
1446 v = TOP();
1447 x = PyNumber_InPlaceMultiply(v, w);
1448 Py_DECREF(v);
1449 Py_DECREF(w);
1450 SET_TOP(x);
1451 if (x != NULL) continue;
1452 break;
1454 case INPLACE_DIVIDE:
1455 if (!_Py_QnewFlag) {
1456 w = POP();
1457 v = TOP();
1458 x = PyNumber_InPlaceDivide(v, w);
1459 Py_DECREF(v);
1460 Py_DECREF(w);
1461 SET_TOP(x);
1462 if (x != NULL) continue;
1463 break;
1465 /* -Qnew is in effect: fall through to
1466 INPLACE_TRUE_DIVIDE */
1467 case INPLACE_TRUE_DIVIDE:
1468 w = POP();
1469 v = TOP();
1470 x = PyNumber_InPlaceTrueDivide(v, w);
1471 Py_DECREF(v);
1472 Py_DECREF(w);
1473 SET_TOP(x);
1474 if (x != NULL) continue;
1475 break;
1477 case INPLACE_FLOOR_DIVIDE:
1478 w = POP();
1479 v = TOP();
1480 x = PyNumber_InPlaceFloorDivide(v, w);
1481 Py_DECREF(v);
1482 Py_DECREF(w);
1483 SET_TOP(x);
1484 if (x != NULL) continue;
1485 break;
1487 case INPLACE_MODULO:
1488 w = POP();
1489 v = TOP();
1490 x = PyNumber_InPlaceRemainder(v, w);
1491 Py_DECREF(v);
1492 Py_DECREF(w);
1493 SET_TOP(x);
1494 if (x != NULL) continue;
1495 break;
1497 case INPLACE_ADD:
1498 w = POP();
1499 v = TOP();
1500 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1501 /* INLINE: int + int */
1502 register long a, b, i;
1503 a = PyInt_AS_LONG(v);
1504 b = PyInt_AS_LONG(w);
1505 i = a + b;
1506 if ((i^a) < 0 && (i^b) < 0)
1507 goto slow_iadd;
1508 x = PyInt_FromLong(i);
1510 else if (PyString_CheckExact(v) &&
1511 PyString_CheckExact(w)) {
1512 x = string_concatenate(v, w, f, next_instr);
1513 /* string_concatenate consumed the ref to v */
1514 goto skip_decref_v;
1516 else {
1517 slow_iadd:
1518 x = PyNumber_InPlaceAdd(v, w);
1520 Py_DECREF(v);
1521 skip_decref_v:
1522 Py_DECREF(w);
1523 SET_TOP(x);
1524 if (x != NULL) continue;
1525 break;
1527 case INPLACE_SUBTRACT:
1528 w = POP();
1529 v = TOP();
1530 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1531 /* INLINE: int - int */
1532 register long a, b, i;
1533 a = PyInt_AS_LONG(v);
1534 b = PyInt_AS_LONG(w);
1535 i = a - b;
1536 if ((i^a) < 0 && (i^~b) < 0)
1537 goto slow_isub;
1538 x = PyInt_FromLong(i);
1540 else {
1541 slow_isub:
1542 x = PyNumber_InPlaceSubtract(v, w);
1544 Py_DECREF(v);
1545 Py_DECREF(w);
1546 SET_TOP(x);
1547 if (x != NULL) continue;
1548 break;
1550 case INPLACE_LSHIFT:
1551 w = POP();
1552 v = TOP();
1553 x = PyNumber_InPlaceLshift(v, w);
1554 Py_DECREF(v);
1555 Py_DECREF(w);
1556 SET_TOP(x);
1557 if (x != NULL) continue;
1558 break;
1560 case INPLACE_RSHIFT:
1561 w = POP();
1562 v = TOP();
1563 x = PyNumber_InPlaceRshift(v, w);
1564 Py_DECREF(v);
1565 Py_DECREF(w);
1566 SET_TOP(x);
1567 if (x != NULL) continue;
1568 break;
1570 case INPLACE_AND:
1571 w = POP();
1572 v = TOP();
1573 x = PyNumber_InPlaceAnd(v, w);
1574 Py_DECREF(v);
1575 Py_DECREF(w);
1576 SET_TOP(x);
1577 if (x != NULL) continue;
1578 break;
1580 case INPLACE_XOR:
1581 w = POP();
1582 v = TOP();
1583 x = PyNumber_InPlaceXor(v, w);
1584 Py_DECREF(v);
1585 Py_DECREF(w);
1586 SET_TOP(x);
1587 if (x != NULL) continue;
1588 break;
1590 case INPLACE_OR:
1591 w = POP();
1592 v = TOP();
1593 x = PyNumber_InPlaceOr(v, w);
1594 Py_DECREF(v);
1595 Py_DECREF(w);
1596 SET_TOP(x);
1597 if (x != NULL) continue;
1598 break;
1600 case SLICE+0:
1601 case SLICE+1:
1602 case SLICE+2:
1603 case SLICE+3:
1604 if ((opcode-SLICE) & 2)
1605 w = POP();
1606 else
1607 w = NULL;
1608 if ((opcode-SLICE) & 1)
1609 v = POP();
1610 else
1611 v = NULL;
1612 u = TOP();
1613 x = apply_slice(u, v, w);
1614 Py_DECREF(u);
1615 Py_XDECREF(v);
1616 Py_XDECREF(w);
1617 SET_TOP(x);
1618 if (x != NULL) continue;
1619 break;
1621 case STORE_SLICE+0:
1622 case STORE_SLICE+1:
1623 case STORE_SLICE+2:
1624 case STORE_SLICE+3:
1625 if ((opcode-STORE_SLICE) & 2)
1626 w = POP();
1627 else
1628 w = NULL;
1629 if ((opcode-STORE_SLICE) & 1)
1630 v = POP();
1631 else
1632 v = NULL;
1633 u = POP();
1634 t = POP();
1635 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1636 Py_DECREF(t);
1637 Py_DECREF(u);
1638 Py_XDECREF(v);
1639 Py_XDECREF(w);
1640 if (err == 0) continue;
1641 break;
1643 case DELETE_SLICE+0:
1644 case DELETE_SLICE+1:
1645 case DELETE_SLICE+2:
1646 case DELETE_SLICE+3:
1647 if ((opcode-DELETE_SLICE) & 2)
1648 w = POP();
1649 else
1650 w = NULL;
1651 if ((opcode-DELETE_SLICE) & 1)
1652 v = POP();
1653 else
1654 v = NULL;
1655 u = POP();
1656 err = assign_slice(u, v, w, (PyObject *)NULL);
1657 /* del u[v:w] */
1658 Py_DECREF(u);
1659 Py_XDECREF(v);
1660 Py_XDECREF(w);
1661 if (err == 0) continue;
1662 break;
1664 case STORE_SUBSCR:
1665 w = TOP();
1666 v = SECOND();
1667 u = THIRD();
1668 STACKADJ(-3);
1669 /* v[w] = u */
1670 err = PyObject_SetItem(v, w, u);
1671 Py_DECREF(u);
1672 Py_DECREF(v);
1673 Py_DECREF(w);
1674 if (err == 0) continue;
1675 break;
1677 case DELETE_SUBSCR:
1678 w = TOP();
1679 v = SECOND();
1680 STACKADJ(-2);
1681 /* del v[w] */
1682 err = PyObject_DelItem(v, w);
1683 Py_DECREF(v);
1684 Py_DECREF(w);
1685 if (err == 0) continue;
1686 break;
1688 case PRINT_EXPR:
1689 v = POP();
1690 w = PySys_GetObject("displayhook");
1691 if (w == NULL) {
1692 PyErr_SetString(PyExc_RuntimeError,
1693 "lost sys.displayhook");
1694 err = -1;
1695 x = NULL;
1697 if (err == 0) {
1698 x = PyTuple_Pack(1, v);
1699 if (x == NULL)
1700 err = -1;
1702 if (err == 0) {
1703 w = PyEval_CallObject(w, x);
1704 Py_XDECREF(w);
1705 if (w == NULL)
1706 err = -1;
1708 Py_DECREF(v);
1709 Py_XDECREF(x);
1710 break;
1712 case PRINT_ITEM_TO:
1713 w = stream = POP();
1714 /* fall through to PRINT_ITEM */
1716 case PRINT_ITEM:
1717 v = POP();
1718 if (stream == NULL || stream == Py_None) {
1719 w = PySys_GetObject("stdout");
1720 if (w == NULL) {
1721 PyErr_SetString(PyExc_RuntimeError,
1722 "lost sys.stdout");
1723 err = -1;
1726 /* PyFile_SoftSpace() can exececute arbitrary code
1727 if sys.stdout is an instance with a __getattr__.
1728 If __getattr__ raises an exception, w will
1729 be freed, so we need to prevent that temporarily. */
1730 Py_XINCREF(w);
1731 if (w != NULL && PyFile_SoftSpace(w, 0))
1732 err = PyFile_WriteString(" ", w);
1733 if (err == 0)
1734 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1735 if (err == 0) {
1736 /* XXX move into writeobject() ? */
1737 if (PyString_Check(v)) {
1738 char *s = PyString_AS_STRING(v);
1739 Py_ssize_t len = PyString_GET_SIZE(v);
1740 if (len == 0 ||
1741 !isspace(Py_CHARMASK(s[len-1])) ||
1742 s[len-1] == ' ')
1743 PyFile_SoftSpace(w, 1);
1745 #ifdef Py_USING_UNICODE
1746 else if (PyUnicode_Check(v)) {
1747 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1748 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1749 if (len == 0 ||
1750 !Py_UNICODE_ISSPACE(s[len-1]) ||
1751 s[len-1] == ' ')
1752 PyFile_SoftSpace(w, 1);
1754 #endif
1755 else
1756 PyFile_SoftSpace(w, 1);
1758 Py_XDECREF(w);
1759 Py_DECREF(v);
1760 Py_XDECREF(stream);
1761 stream = NULL;
1762 if (err == 0)
1763 continue;
1764 break;
1766 case PRINT_NEWLINE_TO:
1767 w = stream = POP();
1768 /* fall through to PRINT_NEWLINE */
1770 case PRINT_NEWLINE:
1771 if (stream == NULL || stream == Py_None) {
1772 w = PySys_GetObject("stdout");
1773 if (w == NULL) {
1774 PyErr_SetString(PyExc_RuntimeError,
1775 "lost sys.stdout");
1776 why = WHY_EXCEPTION;
1779 if (w != NULL) {
1780 /* w.write() may replace sys.stdout, so we
1781 * have to keep our reference to it */
1782 Py_INCREF(w);
1783 err = PyFile_WriteString("\n", w);
1784 if (err == 0)
1785 PyFile_SoftSpace(w, 0);
1786 Py_DECREF(w);
1788 Py_XDECREF(stream);
1789 stream = NULL;
1790 break;
1793 #ifdef CASE_TOO_BIG
1794 default: switch (opcode) {
1795 #endif
1796 case RAISE_VARARGS:
1797 u = v = w = NULL;
1798 switch (oparg) {
1799 case 3:
1800 u = POP(); /* traceback */
1801 /* Fallthrough */
1802 case 2:
1803 v = POP(); /* value */
1804 /* Fallthrough */
1805 case 1:
1806 w = POP(); /* exc */
1807 case 0: /* Fallthrough */
1808 why = do_raise(w, v, u);
1809 break;
1810 default:
1811 PyErr_SetString(PyExc_SystemError,
1812 "bad RAISE_VARARGS oparg");
1813 why = WHY_EXCEPTION;
1814 break;
1816 break;
1818 case LOAD_LOCALS:
1819 if ((x = f->f_locals) != NULL) {
1820 Py_INCREF(x);
1821 PUSH(x);
1822 continue;
1824 PyErr_SetString(PyExc_SystemError, "no locals");
1825 break;
1827 case RETURN_VALUE:
1828 retval = POP();
1829 why = WHY_RETURN;
1830 goto fast_block_end;
1832 case YIELD_VALUE:
1833 retval = POP();
1834 f->f_stacktop = stack_pointer;
1835 why = WHY_YIELD;
1836 goto fast_yield;
1838 case EXEC_STMT:
1839 w = TOP();
1840 v = SECOND();
1841 u = THIRD();
1842 STACKADJ(-3);
1843 READ_TIMESTAMP(intr0);
1844 err = exec_statement(f, u, v, w);
1845 READ_TIMESTAMP(intr1);
1846 Py_DECREF(u);
1847 Py_DECREF(v);
1848 Py_DECREF(w);
1849 break;
1851 case POP_BLOCK:
1853 PyTryBlock *b = PyFrame_BlockPop(f);
1854 while (STACK_LEVEL() > b->b_level) {
1855 v = POP();
1856 Py_DECREF(v);
1859 continue;
1861 PREDICTED(END_FINALLY);
1862 case END_FINALLY:
1863 v = POP();
1864 if (PyInt_Check(v)) {
1865 why = (enum why_code) PyInt_AS_LONG(v);
1866 assert(why != WHY_YIELD);
1867 if (why == WHY_RETURN ||
1868 why == WHY_CONTINUE)
1869 retval = POP();
1871 else if (PyExceptionClass_Check(v) ||
1872 PyString_Check(v)) {
1873 w = POP();
1874 u = POP();
1875 PyErr_Restore(v, w, u);
1876 why = WHY_RERAISE;
1877 break;
1879 else if (v != Py_None) {
1880 PyErr_SetString(PyExc_SystemError,
1881 "'finally' pops bad exception");
1882 why = WHY_EXCEPTION;
1884 Py_DECREF(v);
1885 break;
1887 case BUILD_CLASS:
1888 u = TOP();
1889 v = SECOND();
1890 w = THIRD();
1891 STACKADJ(-2);
1892 x = build_class(u, v, w);
1893 SET_TOP(x);
1894 Py_DECREF(u);
1895 Py_DECREF(v);
1896 Py_DECREF(w);
1897 break;
1899 case STORE_NAME:
1900 w = GETITEM(names, oparg);
1901 v = POP();
1902 if ((x = f->f_locals) != NULL) {
1903 if (PyDict_CheckExact(x))
1904 err = PyDict_SetItem(x, w, v);
1905 else
1906 err = PyObject_SetItem(x, w, v);
1907 Py_DECREF(v);
1908 if (err == 0) continue;
1909 break;
1911 PyErr_Format(PyExc_SystemError,
1912 "no locals found when storing %s",
1913 PyObject_REPR(w));
1914 break;
1916 case DELETE_NAME:
1917 w = GETITEM(names, oparg);
1918 if ((x = f->f_locals) != NULL) {
1919 if ((err = PyObject_DelItem(x, w)) != 0)
1920 format_exc_check_arg(PyExc_NameError,
1921 NAME_ERROR_MSG,
1923 break;
1925 PyErr_Format(PyExc_SystemError,
1926 "no locals when deleting %s",
1927 PyObject_REPR(w));
1928 break;
1930 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1931 case UNPACK_SEQUENCE:
1932 v = POP();
1933 if (PyTuple_CheckExact(v) &&
1934 PyTuple_GET_SIZE(v) == oparg) {
1935 PyObject **items = \
1936 ((PyTupleObject *)v)->ob_item;
1937 while (oparg--) {
1938 w = items[oparg];
1939 Py_INCREF(w);
1940 PUSH(w);
1942 Py_DECREF(v);
1943 continue;
1944 } else if (PyList_CheckExact(v) &&
1945 PyList_GET_SIZE(v) == oparg) {
1946 PyObject **items = \
1947 ((PyListObject *)v)->ob_item;
1948 while (oparg--) {
1949 w = items[oparg];
1950 Py_INCREF(w);
1951 PUSH(w);
1953 } else if (unpack_iterable(v, oparg,
1954 stack_pointer + oparg)) {
1955 stack_pointer += oparg;
1956 } else {
1957 /* unpack_iterable() raised an exception */
1958 why = WHY_EXCEPTION;
1960 Py_DECREF(v);
1961 break;
1963 case STORE_ATTR:
1964 w = GETITEM(names, oparg);
1965 v = TOP();
1966 u = SECOND();
1967 STACKADJ(-2);
1968 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1969 Py_DECREF(v);
1970 Py_DECREF(u);
1971 if (err == 0) continue;
1972 break;
1974 case DELETE_ATTR:
1975 w = GETITEM(names, oparg);
1976 v = POP();
1977 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1978 /* del v.w */
1979 Py_DECREF(v);
1980 break;
1982 case STORE_GLOBAL:
1983 w = GETITEM(names, oparg);
1984 v = POP();
1985 err = PyDict_SetItem(f->f_globals, w, v);
1986 Py_DECREF(v);
1987 if (err == 0) continue;
1988 break;
1990 case DELETE_GLOBAL:
1991 w = GETITEM(names, oparg);
1992 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1993 format_exc_check_arg(
1994 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1995 break;
1997 case LOAD_NAME:
1998 w = GETITEM(names, oparg);
1999 if ((v = f->f_locals) == NULL) {
2000 PyErr_Format(PyExc_SystemError,
2001 "no locals when loading %s",
2002 PyObject_REPR(w));
2003 why = WHY_EXCEPTION;
2004 break;
2006 if (PyDict_CheckExact(v)) {
2007 x = PyDict_GetItem(v, w);
2008 Py_XINCREF(x);
2010 else {
2011 x = PyObject_GetItem(v, w);
2012 if (x == NULL && PyErr_Occurred()) {
2013 if (!PyErr_ExceptionMatches(
2014 PyExc_KeyError))
2015 break;
2016 PyErr_Clear();
2019 if (x == NULL) {
2020 x = PyDict_GetItem(f->f_globals, w);
2021 if (x == NULL) {
2022 x = PyDict_GetItem(f->f_builtins, w);
2023 if (x == NULL) {
2024 format_exc_check_arg(
2025 PyExc_NameError,
2026 NAME_ERROR_MSG, w);
2027 break;
2030 Py_INCREF(x);
2032 PUSH(x);
2033 continue;
2035 case LOAD_GLOBAL:
2036 w = GETITEM(names, oparg);
2037 if (PyString_CheckExact(w)) {
2038 /* Inline the PyDict_GetItem() calls.
2039 WARNING: this is an extreme speed hack.
2040 Do not try this at home. */
2041 long hash = ((PyStringObject *)w)->ob_shash;
2042 if (hash != -1) {
2043 PyDictObject *d;
2044 PyDictEntry *e;
2045 d = (PyDictObject *)(f->f_globals);
2046 e = d->ma_lookup(d, w, hash);
2047 if (e == NULL) {
2048 x = NULL;
2049 break;
2051 x = e->me_value;
2052 if (x != NULL) {
2053 Py_INCREF(x);
2054 PUSH(x);
2055 continue;
2057 d = (PyDictObject *)(f->f_builtins);
2058 e = d->ma_lookup(d, w, hash);
2059 if (e == NULL) {
2060 x = NULL;
2061 break;
2063 x = e->me_value;
2064 if (x != NULL) {
2065 Py_INCREF(x);
2066 PUSH(x);
2067 continue;
2069 goto load_global_error;
2072 /* This is the un-inlined version of the code above */
2073 x = PyDict_GetItem(f->f_globals, w);
2074 if (x == NULL) {
2075 x = PyDict_GetItem(f->f_builtins, w);
2076 if (x == NULL) {
2077 load_global_error:
2078 format_exc_check_arg(
2079 PyExc_NameError,
2080 GLOBAL_NAME_ERROR_MSG, w);
2081 break;
2084 Py_INCREF(x);
2085 PUSH(x);
2086 continue;
2088 case DELETE_FAST:
2089 x = GETLOCAL(oparg);
2090 if (x != NULL) {
2091 SETLOCAL(oparg, NULL);
2092 continue;
2094 format_exc_check_arg(
2095 PyExc_UnboundLocalError,
2096 UNBOUNDLOCAL_ERROR_MSG,
2097 PyTuple_GetItem(co->co_varnames, oparg)
2099 break;
2101 case LOAD_CLOSURE:
2102 x = freevars[oparg];
2103 Py_INCREF(x);
2104 PUSH(x);
2105 if (x != NULL) continue;
2106 break;
2108 case LOAD_DEREF:
2109 x = freevars[oparg];
2110 w = PyCell_Get(x);
2111 if (w != NULL) {
2112 PUSH(w);
2113 continue;
2115 err = -1;
2116 /* Don't stomp existing exception */
2117 if (PyErr_Occurred())
2118 break;
2119 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2120 v = PyTuple_GET_ITEM(co->co_cellvars,
2121 oparg);
2122 format_exc_check_arg(
2123 PyExc_UnboundLocalError,
2124 UNBOUNDLOCAL_ERROR_MSG,
2126 } else {
2127 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2128 PyTuple_GET_SIZE(co->co_cellvars));
2129 format_exc_check_arg(PyExc_NameError,
2130 UNBOUNDFREE_ERROR_MSG, v);
2132 break;
2134 case STORE_DEREF:
2135 w = POP();
2136 x = freevars[oparg];
2137 PyCell_Set(x, w);
2138 Py_DECREF(w);
2139 continue;
2141 case BUILD_TUPLE:
2142 x = PyTuple_New(oparg);
2143 if (x != NULL) {
2144 for (; --oparg >= 0;) {
2145 w = POP();
2146 PyTuple_SET_ITEM(x, oparg, w);
2148 PUSH(x);
2149 continue;
2151 break;
2153 case BUILD_LIST:
2154 x = PyList_New(oparg);
2155 if (x != NULL) {
2156 for (; --oparg >= 0;) {
2157 w = POP();
2158 PyList_SET_ITEM(x, oparg, w);
2160 PUSH(x);
2161 continue;
2163 break;
2165 case BUILD_MAP:
2166 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2167 PUSH(x);
2168 if (x != NULL) continue;
2169 break;
2171 case STORE_MAP:
2172 w = TOP(); /* key */
2173 u = SECOND(); /* value */
2174 v = THIRD(); /* dict */
2175 STACKADJ(-2);
2176 assert (PyDict_CheckExact(v));
2177 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2178 Py_DECREF(u);
2179 Py_DECREF(w);
2180 if (err == 0) continue;
2181 break;
2183 case LOAD_ATTR:
2184 w = GETITEM(names, oparg);
2185 v = TOP();
2186 x = PyObject_GetAttr(v, w);
2187 Py_DECREF(v);
2188 SET_TOP(x);
2189 if (x != NULL) continue;
2190 break;
2192 case COMPARE_OP:
2193 w = POP();
2194 v = TOP();
2195 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2196 /* INLINE: cmp(int, int) */
2197 register long a, b;
2198 register int res;
2199 a = PyInt_AS_LONG(v);
2200 b = PyInt_AS_LONG(w);
2201 switch (oparg) {
2202 case PyCmp_LT: res = a < b; break;
2203 case PyCmp_LE: res = a <= b; break;
2204 case PyCmp_EQ: res = a == b; break;
2205 case PyCmp_NE: res = a != b; break;
2206 case PyCmp_GT: res = a > b; break;
2207 case PyCmp_GE: res = a >= b; break;
2208 case PyCmp_IS: res = v == w; break;
2209 case PyCmp_IS_NOT: res = v != w; break;
2210 default: goto slow_compare;
2212 x = res ? Py_True : Py_False;
2213 Py_INCREF(x);
2215 else {
2216 slow_compare:
2217 x = cmp_outcome(oparg, v, w);
2219 Py_DECREF(v);
2220 Py_DECREF(w);
2221 SET_TOP(x);
2222 if (x == NULL) break;
2223 PREDICT(JUMP_IF_FALSE);
2224 PREDICT(JUMP_IF_TRUE);
2225 continue;
2227 case IMPORT_NAME:
2228 w = GETITEM(names, oparg);
2229 x = PyDict_GetItemString(f->f_builtins, "__import__");
2230 if (x == NULL) {
2231 PyErr_SetString(PyExc_ImportError,
2232 "__import__ not found");
2233 break;
2235 Py_INCREF(x);
2236 v = POP();
2237 u = TOP();
2238 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2239 w = PyTuple_Pack(5,
2241 f->f_globals,
2242 f->f_locals == NULL ?
2243 Py_None : f->f_locals,
2246 else
2247 w = PyTuple_Pack(4,
2249 f->f_globals,
2250 f->f_locals == NULL ?
2251 Py_None : f->f_locals,
2253 Py_DECREF(v);
2254 Py_DECREF(u);
2255 if (w == NULL) {
2256 u = POP();
2257 Py_DECREF(x);
2258 x = NULL;
2259 break;
2261 READ_TIMESTAMP(intr0);
2262 v = x;
2263 x = PyEval_CallObject(v, w);
2264 Py_DECREF(v);
2265 READ_TIMESTAMP(intr1);
2266 Py_DECREF(w);
2267 SET_TOP(x);
2268 if (x != NULL) continue;
2269 break;
2271 case IMPORT_STAR:
2272 v = POP();
2273 PyFrame_FastToLocals(f);
2274 if ((x = f->f_locals) == NULL) {
2275 PyErr_SetString(PyExc_SystemError,
2276 "no locals found during 'import *'");
2277 break;
2279 READ_TIMESTAMP(intr0);
2280 err = import_all_from(x, v);
2281 READ_TIMESTAMP(intr1);
2282 PyFrame_LocalsToFast(f, 0);
2283 Py_DECREF(v);
2284 if (err == 0) continue;
2285 break;
2287 case IMPORT_FROM:
2288 w = GETITEM(names, oparg);
2289 v = TOP();
2290 READ_TIMESTAMP(intr0);
2291 x = import_from(v, w);
2292 READ_TIMESTAMP(intr1);
2293 PUSH(x);
2294 if (x != NULL) continue;
2295 break;
2297 case JUMP_FORWARD:
2298 JUMPBY(oparg);
2299 goto fast_next_opcode;
2301 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
2302 case JUMP_IF_FALSE:
2303 w = TOP();
2304 if (w == Py_True) {
2305 PREDICT(POP_TOP);
2306 goto fast_next_opcode;
2308 if (w == Py_False) {
2309 JUMPBY(oparg);
2310 goto fast_next_opcode;
2312 err = PyObject_IsTrue(w);
2313 if (err > 0)
2314 err = 0;
2315 else if (err == 0)
2316 JUMPBY(oparg);
2317 else
2318 break;
2319 continue;
2321 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
2322 case JUMP_IF_TRUE:
2323 w = TOP();
2324 if (w == Py_False) {
2325 PREDICT(POP_TOP);
2326 goto fast_next_opcode;
2328 if (w == Py_True) {
2329 JUMPBY(oparg);
2330 goto fast_next_opcode;
2332 err = PyObject_IsTrue(w);
2333 if (err > 0) {
2334 err = 0;
2335 JUMPBY(oparg);
2337 else if (err == 0)
2339 else
2340 break;
2341 continue;
2343 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2344 case JUMP_ABSOLUTE:
2345 JUMPTO(oparg);
2346 #if FAST_LOOPS
2347 /* Enabling this path speeds-up all while and for-loops by bypassing
2348 the per-loop checks for signals. By default, this should be turned-off
2349 because it prevents detection of a control-break in tight loops like
2350 "while 1: pass". Compile with this option turned-on when you need
2351 the speed-up and do not need break checking inside tight loops (ones
2352 that contain only instructions ending with goto fast_next_opcode).
2354 goto fast_next_opcode;
2355 #else
2356 continue;
2357 #endif
2359 case GET_ITER:
2360 /* before: [obj]; after [getiter(obj)] */
2361 v = TOP();
2362 x = PyObject_GetIter(v);
2363 Py_DECREF(v);
2364 if (x != NULL) {
2365 SET_TOP(x);
2366 PREDICT(FOR_ITER);
2367 continue;
2369 STACKADJ(-1);
2370 break;
2372 PREDICTED_WITH_ARG(FOR_ITER);
2373 case FOR_ITER:
2374 /* before: [iter]; after: [iter, iter()] *or* [] */
2375 v = TOP();
2376 x = (*v->ob_type->tp_iternext)(v);
2377 if (x != NULL) {
2378 PUSH(x);
2379 PREDICT(STORE_FAST);
2380 PREDICT(UNPACK_SEQUENCE);
2381 continue;
2383 if (PyErr_Occurred()) {
2384 if (!PyErr_ExceptionMatches(
2385 PyExc_StopIteration))
2386 break;
2387 PyErr_Clear();
2389 /* iterator ended normally */
2390 x = v = POP();
2391 Py_DECREF(v);
2392 JUMPBY(oparg);
2393 continue;
2395 case BREAK_LOOP:
2396 why = WHY_BREAK;
2397 goto fast_block_end;
2399 case CONTINUE_LOOP:
2400 retval = PyInt_FromLong(oparg);
2401 if (!retval) {
2402 x = NULL;
2403 break;
2405 why = WHY_CONTINUE;
2406 goto fast_block_end;
2408 case SETUP_LOOP:
2409 case SETUP_EXCEPT:
2410 case SETUP_FINALLY:
2411 /* NOTE: If you add any new block-setup opcodes that
2412 are not try/except/finally handlers, you may need
2413 to update the PyGen_NeedsFinalizing() function.
2416 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2417 STACK_LEVEL());
2418 continue;
2420 case WITH_CLEANUP:
2422 /* At the top of the stack are 1-3 values indicating
2423 how/why we entered the finally clause:
2424 - TOP = None
2425 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2426 - TOP = WHY_*; no retval below it
2427 - (TOP, SECOND, THIRD) = exc_info()
2428 Below them is EXIT, the context.__exit__ bound method.
2429 In the last case, we must call
2430 EXIT(TOP, SECOND, THIRD)
2431 otherwise we must call
2432 EXIT(None, None, None)
2434 In all cases, we remove EXIT from the stack, leaving
2435 the rest in the same order.
2437 In addition, if the stack represents an exception,
2438 *and* the function call returns a 'true' value, we
2439 "zap" this information, to prevent END_FINALLY from
2440 re-raising the exception. (But non-local gotos
2441 should still be resumed.)
2444 PyObject *exit_func;
2446 u = POP();
2447 if (u == Py_None) {
2448 exit_func = TOP();
2449 SET_TOP(u);
2450 v = w = Py_None;
2452 else if (PyInt_Check(u)) {
2453 switch(PyInt_AS_LONG(u)) {
2454 case WHY_RETURN:
2455 case WHY_CONTINUE:
2456 /* Retval in TOP. */
2457 exit_func = SECOND();
2458 SET_SECOND(TOP());
2459 SET_TOP(u);
2460 break;
2461 default:
2462 exit_func = TOP();
2463 SET_TOP(u);
2464 break;
2466 u = v = w = Py_None;
2468 else {
2469 v = TOP();
2470 w = SECOND();
2471 exit_func = THIRD();
2472 SET_TOP(u);
2473 SET_SECOND(v);
2474 SET_THIRD(w);
2476 /* XXX Not the fastest way to call it... */
2477 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2478 NULL);
2479 Py_DECREF(exit_func);
2480 if (x == NULL)
2481 break; /* Go to error exit */
2483 if (u != Py_None)
2484 err = PyObject_IsTrue(x);
2485 else
2486 err = 0;
2487 Py_DECREF(x);
2489 if (err < 0)
2490 break; /* Go to error exit */
2491 else if (err > 0) {
2492 err = 0;
2493 /* There was an exception and a true return */
2494 STACKADJ(-2);
2495 Py_INCREF(Py_None);
2496 SET_TOP(Py_None);
2497 Py_DECREF(u);
2498 Py_DECREF(v);
2499 Py_DECREF(w);
2500 } else {
2501 /* The stack was rearranged to remove EXIT
2502 above. Let END_FINALLY do its thing */
2504 PREDICT(END_FINALLY);
2505 break;
2508 case CALL_FUNCTION:
2510 PyObject **sp;
2511 PCALL(PCALL_ALL);
2512 sp = stack_pointer;
2513 #ifdef WITH_TSC
2514 x = call_function(&sp, oparg, &intr0, &intr1);
2515 #else
2516 x = call_function(&sp, oparg);
2517 #endif
2518 stack_pointer = sp;
2519 PUSH(x);
2520 if (x != NULL)
2521 continue;
2522 break;
2525 case CALL_FUNCTION_VAR:
2526 case CALL_FUNCTION_KW:
2527 case CALL_FUNCTION_VAR_KW:
2529 int na = oparg & 0xff;
2530 int nk = (oparg>>8) & 0xff;
2531 int flags = (opcode - CALL_FUNCTION) & 3;
2532 int n = na + 2 * nk;
2533 PyObject **pfunc, *func, **sp;
2534 PCALL(PCALL_ALL);
2535 if (flags & CALL_FLAG_VAR)
2536 n++;
2537 if (flags & CALL_FLAG_KW)
2538 n++;
2539 pfunc = stack_pointer - n - 1;
2540 func = *pfunc;
2542 if (PyMethod_Check(func)
2543 && PyMethod_GET_SELF(func) != NULL) {
2544 PyObject *self = PyMethod_GET_SELF(func);
2545 Py_INCREF(self);
2546 func = PyMethod_GET_FUNCTION(func);
2547 Py_INCREF(func);
2548 Py_DECREF(*pfunc);
2549 *pfunc = self;
2550 na++;
2551 n++;
2552 } else
2553 Py_INCREF(func);
2554 sp = stack_pointer;
2555 READ_TIMESTAMP(intr0);
2556 x = ext_do_call(func, &sp, flags, na, nk);
2557 READ_TIMESTAMP(intr1);
2558 stack_pointer = sp;
2559 Py_DECREF(func);
2561 while (stack_pointer > pfunc) {
2562 w = POP();
2563 Py_DECREF(w);
2565 PUSH(x);
2566 if (x != NULL)
2567 continue;
2568 break;
2571 case MAKE_FUNCTION:
2572 v = POP(); /* code object */
2573 x = PyFunction_New(v, f->f_globals);
2574 Py_DECREF(v);
2575 /* XXX Maybe this should be a separate opcode? */
2576 if (x != NULL && oparg > 0) {
2577 v = PyTuple_New(oparg);
2578 if (v == NULL) {
2579 Py_DECREF(x);
2580 x = NULL;
2581 break;
2583 while (--oparg >= 0) {
2584 w = POP();
2585 PyTuple_SET_ITEM(v, oparg, w);
2587 err = PyFunction_SetDefaults(x, v);
2588 Py_DECREF(v);
2590 PUSH(x);
2591 break;
2593 case MAKE_CLOSURE:
2595 v = POP(); /* code object */
2596 x = PyFunction_New(v, f->f_globals);
2597 Py_DECREF(v);
2598 if (x != NULL) {
2599 v = POP();
2600 if (PyFunction_SetClosure(x, v) != 0) {
2601 /* Can't happen unless bytecode is corrupt. */
2602 why = WHY_EXCEPTION;
2604 Py_DECREF(v);
2606 if (x != NULL && oparg > 0) {
2607 v = PyTuple_New(oparg);
2608 if (v == NULL) {
2609 Py_DECREF(x);
2610 x = NULL;
2611 break;
2613 while (--oparg >= 0) {
2614 w = POP();
2615 PyTuple_SET_ITEM(v, oparg, w);
2617 if (PyFunction_SetDefaults(x, v) != 0) {
2618 /* Can't happen unless
2619 PyFunction_SetDefaults changes. */
2620 why = WHY_EXCEPTION;
2622 Py_DECREF(v);
2624 PUSH(x);
2625 break;
2628 case BUILD_SLICE:
2629 if (oparg == 3)
2630 w = POP();
2631 else
2632 w = NULL;
2633 v = POP();
2634 u = TOP();
2635 x = PySlice_New(u, v, w);
2636 Py_DECREF(u);
2637 Py_DECREF(v);
2638 Py_XDECREF(w);
2639 SET_TOP(x);
2640 if (x != NULL) continue;
2641 break;
2643 case EXTENDED_ARG:
2644 opcode = NEXTOP();
2645 oparg = oparg<<16 | NEXTARG();
2646 goto dispatch_opcode;
2648 default:
2649 fprintf(stderr,
2650 "XXX lineno: %d, opcode: %d\n",
2651 PyCode_Addr2Line(f->f_code, f->f_lasti),
2652 opcode);
2653 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2654 why = WHY_EXCEPTION;
2655 break;
2657 #ifdef CASE_TOO_BIG
2659 #endif
2661 } /* switch */
2663 on_error:
2665 READ_TIMESTAMP(inst1);
2667 /* Quickly continue if no error occurred */
2669 if (why == WHY_NOT) {
2670 if (err == 0 && x != NULL) {
2671 #ifdef CHECKEXC
2672 /* This check is expensive! */
2673 if (PyErr_Occurred())
2674 fprintf(stderr,
2675 "XXX undetected error\n");
2676 else {
2677 #endif
2678 READ_TIMESTAMP(loop1);
2679 continue; /* Normal, fast path */
2680 #ifdef CHECKEXC
2682 #endif
2684 why = WHY_EXCEPTION;
2685 x = Py_None;
2686 err = 0;
2689 /* Double-check exception status */
2691 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2692 if (!PyErr_Occurred()) {
2693 PyErr_SetString(PyExc_SystemError,
2694 "error return without exception set");
2695 why = WHY_EXCEPTION;
2698 #ifdef CHECKEXC
2699 else {
2700 /* This check is expensive! */
2701 if (PyErr_Occurred()) {
2702 char buf[128];
2703 sprintf(buf, "Stack unwind with exception "
2704 "set and why=%d", why);
2705 Py_FatalError(buf);
2708 #endif
2710 /* Log traceback info if this is a real exception */
2712 if (why == WHY_EXCEPTION) {
2713 PyTraceBack_Here(f);
2715 if (tstate->c_tracefunc != NULL)
2716 call_exc_trace(tstate->c_tracefunc,
2717 tstate->c_traceobj, f);
2720 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2722 if (why == WHY_RERAISE)
2723 why = WHY_EXCEPTION;
2725 /* Unwind stacks if a (pseudo) exception occurred */
2727 fast_block_end:
2728 while (why != WHY_NOT && f->f_iblock > 0) {
2729 PyTryBlock *b = PyFrame_BlockPop(f);
2731 assert(why != WHY_YIELD);
2732 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2733 /* For a continue inside a try block,
2734 don't pop the block for the loop. */
2735 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2736 b->b_level);
2737 why = WHY_NOT;
2738 JUMPTO(PyInt_AS_LONG(retval));
2739 Py_DECREF(retval);
2740 break;
2743 while (STACK_LEVEL() > b->b_level) {
2744 v = POP();
2745 Py_XDECREF(v);
2747 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2748 why = WHY_NOT;
2749 JUMPTO(b->b_handler);
2750 break;
2752 if (b->b_type == SETUP_FINALLY ||
2753 (b->b_type == SETUP_EXCEPT &&
2754 why == WHY_EXCEPTION)) {
2755 if (why == WHY_EXCEPTION) {
2756 PyObject *exc, *val, *tb;
2757 PyErr_Fetch(&exc, &val, &tb);
2758 if (val == NULL) {
2759 val = Py_None;
2760 Py_INCREF(val);
2762 /* Make the raw exception data
2763 available to the handler,
2764 so a program can emulate the
2765 Python main loop. Don't do
2766 this for 'finally'. */
2767 if (b->b_type == SETUP_EXCEPT) {
2768 PyErr_NormalizeException(
2769 &exc, &val, &tb);
2770 set_exc_info(tstate,
2771 exc, val, tb);
2773 if (tb == NULL) {
2774 Py_INCREF(Py_None);
2775 PUSH(Py_None);
2776 } else
2777 PUSH(tb);
2778 PUSH(val);
2779 PUSH(exc);
2781 else {
2782 if (why & (WHY_RETURN | WHY_CONTINUE))
2783 PUSH(retval);
2784 v = PyInt_FromLong((long)why);
2785 PUSH(v);
2787 why = WHY_NOT;
2788 JUMPTO(b->b_handler);
2789 break;
2791 } /* unwind stack */
2793 /* End the loop if we still have an error (or return) */
2795 if (why != WHY_NOT)
2796 break;
2797 READ_TIMESTAMP(loop1);
2799 } /* main loop */
2801 assert(why != WHY_YIELD);
2802 /* Pop remaining stack entries. */
2803 while (!EMPTY()) {
2804 v = POP();
2805 Py_XDECREF(v);
2808 if (why != WHY_RETURN)
2809 retval = NULL;
2811 fast_yield:
2812 if (tstate->use_tracing) {
2813 if (tstate->c_tracefunc) {
2814 if (why == WHY_RETURN || why == WHY_YIELD) {
2815 if (call_trace(tstate->c_tracefunc,
2816 tstate->c_traceobj, f,
2817 PyTrace_RETURN, retval)) {
2818 Py_XDECREF(retval);
2819 retval = NULL;
2820 why = WHY_EXCEPTION;
2823 else if (why == WHY_EXCEPTION) {
2824 call_trace_protected(tstate->c_tracefunc,
2825 tstate->c_traceobj, f,
2826 PyTrace_RETURN, NULL);
2829 if (tstate->c_profilefunc) {
2830 if (why == WHY_EXCEPTION)
2831 call_trace_protected(tstate->c_profilefunc,
2832 tstate->c_profileobj, f,
2833 PyTrace_RETURN, NULL);
2834 else if (call_trace(tstate->c_profilefunc,
2835 tstate->c_profileobj, f,
2836 PyTrace_RETURN, retval)) {
2837 Py_XDECREF(retval);
2838 retval = NULL;
2839 why = WHY_EXCEPTION;
2844 if (tstate->frame->f_exc_type != NULL)
2845 reset_exc_info(tstate);
2846 else {
2847 assert(tstate->frame->f_exc_value == NULL);
2848 assert(tstate->frame->f_exc_traceback == NULL);
2851 /* pop frame */
2852 exit_eval_frame:
2853 Py_LeaveRecursiveCall();
2854 tstate->frame = f->f_back;
2856 return retval;
2859 /* This is gonna seem *real weird*, but if you put some other code between
2860 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2861 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2863 PyObject *
2864 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
2865 PyObject **args, int argcount, PyObject **kws, int kwcount,
2866 PyObject **defs, int defcount, PyObject *closure)
2868 register PyFrameObject *f;
2869 register PyObject *retval = NULL;
2870 register PyObject **fastlocals, **freevars;
2871 PyThreadState *tstate = PyThreadState_GET();
2872 PyObject *x, *u;
2874 if (globals == NULL) {
2875 PyErr_SetString(PyExc_SystemError,
2876 "PyEval_EvalCodeEx: NULL globals");
2877 return NULL;
2880 assert(tstate != NULL);
2881 assert(globals != NULL);
2882 f = PyFrame_New(tstate, co, globals, locals);
2883 if (f == NULL)
2884 return NULL;
2886 fastlocals = f->f_localsplus;
2887 freevars = f->f_localsplus + co->co_nlocals;
2889 if (co->co_argcount > 0 ||
2890 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2891 int i;
2892 int n = argcount;
2893 PyObject *kwdict = NULL;
2894 if (co->co_flags & CO_VARKEYWORDS) {
2895 kwdict = PyDict_New();
2896 if (kwdict == NULL)
2897 goto fail;
2898 i = co->co_argcount;
2899 if (co->co_flags & CO_VARARGS)
2900 i++;
2901 SETLOCAL(i, kwdict);
2903 if (argcount > co->co_argcount) {
2904 if (!(co->co_flags & CO_VARARGS)) {
2905 PyErr_Format(PyExc_TypeError,
2906 "%.200s() takes %s %d "
2907 "%sargument%s (%d given)",
2908 PyString_AsString(co->co_name),
2909 defcount ? "at most" : "exactly",
2910 co->co_argcount,
2911 kwcount ? "non-keyword " : "",
2912 co->co_argcount == 1 ? "" : "s",
2913 argcount);
2914 goto fail;
2916 n = co->co_argcount;
2918 for (i = 0; i < n; i++) {
2919 x = args[i];
2920 Py_INCREF(x);
2921 SETLOCAL(i, x);
2923 if (co->co_flags & CO_VARARGS) {
2924 u = PyTuple_New(argcount - n);
2925 if (u == NULL)
2926 goto fail;
2927 SETLOCAL(co->co_argcount, u);
2928 for (i = n; i < argcount; i++) {
2929 x = args[i];
2930 Py_INCREF(x);
2931 PyTuple_SET_ITEM(u, i-n, x);
2934 for (i = 0; i < kwcount; i++) {
2935 PyObject **co_varnames;
2936 PyObject *keyword = kws[2*i];
2937 PyObject *value = kws[2*i + 1];
2938 int j;
2939 if (keyword == NULL || !(PyString_Check(keyword)
2940 #ifdef Py_USING_UNICODE
2941 || PyUnicode_Check(keyword)
2942 #endif
2943 )) {
2944 PyErr_Format(PyExc_TypeError,
2945 "%.200s() keywords must be strings",
2946 PyString_AsString(co->co_name));
2947 goto fail;
2949 /* Speed hack: do raw pointer compares. As names are
2950 normally interned this should almost always hit. */
2951 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
2952 for (j = 0; j < co->co_argcount; j++) {
2953 PyObject *nm = co_varnames[j];
2954 if (nm == keyword)
2955 goto kw_found;
2957 /* Slow fallback, just in case */
2958 for (j = 0; j < co->co_argcount; j++) {
2959 PyObject *nm = co_varnames[j];
2960 int cmp = PyObject_RichCompareBool(
2961 keyword, nm, Py_EQ);
2962 if (cmp > 0)
2963 goto kw_found;
2964 else if (cmp < 0)
2965 goto fail;
2967 /* Check errors from Compare */
2968 if (PyErr_Occurred())
2969 goto fail;
2970 if (j >= co->co_argcount) {
2971 if (kwdict == NULL) {
2972 PyObject *kwd_str = kwd_as_string(keyword);
2973 if (kwd_str) {
2974 PyErr_Format(PyExc_TypeError,
2975 "%.200s() got an unexpected "
2976 "keyword argument '%.400s'",
2977 PyString_AsString(co->co_name),
2978 PyString_AsString(kwd_str));
2979 Py_DECREF(kwd_str);
2981 goto fail;
2983 PyDict_SetItem(kwdict, keyword, value);
2984 continue;
2986 kw_found:
2987 if (GETLOCAL(j) != NULL) {
2988 PyObject *kwd_str = kwd_as_string(keyword);
2989 if (kwd_str) {
2990 PyErr_Format(PyExc_TypeError,
2991 "%.200s() got multiple "
2992 "values for keyword "
2993 "argument '%.400s'",
2994 PyString_AsString(co->co_name),
2995 PyString_AsString(kwd_str));
2996 Py_DECREF(kwd_str);
2998 goto fail;
3000 Py_INCREF(value);
3001 SETLOCAL(j, value);
3003 if (argcount < co->co_argcount) {
3004 int m = co->co_argcount - defcount;
3005 for (i = argcount; i < m; i++) {
3006 if (GETLOCAL(i) == NULL) {
3007 PyErr_Format(PyExc_TypeError,
3008 "%.200s() takes %s %d "
3009 "%sargument%s (%d given)",
3010 PyString_AsString(co->co_name),
3011 ((co->co_flags & CO_VARARGS) ||
3012 defcount) ? "at least"
3013 : "exactly",
3014 m, kwcount ? "non-keyword " : "",
3015 m == 1 ? "" : "s", i);
3016 goto fail;
3019 if (n > m)
3020 i = n - m;
3021 else
3022 i = 0;
3023 for (; i < defcount; i++) {
3024 if (GETLOCAL(m+i) == NULL) {
3025 PyObject *def = defs[i];
3026 Py_INCREF(def);
3027 SETLOCAL(m+i, def);
3032 else {
3033 if (argcount > 0 || kwcount > 0) {
3034 PyErr_Format(PyExc_TypeError,
3035 "%.200s() takes no arguments (%d given)",
3036 PyString_AsString(co->co_name),
3037 argcount + kwcount);
3038 goto fail;
3041 /* Allocate and initialize storage for cell vars, and copy free
3042 vars into frame. This isn't too efficient right now. */
3043 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3044 int i, j, nargs, found;
3045 char *cellname, *argname;
3046 PyObject *c;
3048 nargs = co->co_argcount;
3049 if (co->co_flags & CO_VARARGS)
3050 nargs++;
3051 if (co->co_flags & CO_VARKEYWORDS)
3052 nargs++;
3054 /* Initialize each cell var, taking into account
3055 cell vars that are initialized from arguments.
3057 Should arrange for the compiler to put cellvars
3058 that are arguments at the beginning of the cellvars
3059 list so that we can march over it more efficiently?
3061 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3062 cellname = PyString_AS_STRING(
3063 PyTuple_GET_ITEM(co->co_cellvars, i));
3064 found = 0;
3065 for (j = 0; j < nargs; j++) {
3066 argname = PyString_AS_STRING(
3067 PyTuple_GET_ITEM(co->co_varnames, j));
3068 if (strcmp(cellname, argname) == 0) {
3069 c = PyCell_New(GETLOCAL(j));
3070 if (c == NULL)
3071 goto fail;
3072 GETLOCAL(co->co_nlocals + i) = c;
3073 found = 1;
3074 break;
3077 if (found == 0) {
3078 c = PyCell_New(NULL);
3079 if (c == NULL)
3080 goto fail;
3081 SETLOCAL(co->co_nlocals + i, c);
3085 if (PyTuple_GET_SIZE(co->co_freevars)) {
3086 int i;
3087 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3088 PyObject *o = PyTuple_GET_ITEM(closure, i);
3089 Py_INCREF(o);
3090 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3094 if (co->co_flags & CO_GENERATOR) {
3095 /* Don't need to keep the reference to f_back, it will be set
3096 * when the generator is resumed. */
3097 Py_XDECREF(f->f_back);
3098 f->f_back = NULL;
3100 PCALL(PCALL_GENERATOR);
3102 /* Create a new generator that owns the ready to run frame
3103 * and return that as the value. */
3104 return PyGen_New(f);
3107 retval = PyEval_EvalFrameEx(f,0);
3109 fail: /* Jump here from prelude on failure */
3111 /* decref'ing the frame can cause __del__ methods to get invoked,
3112 which can call back into Python. While we're done with the
3113 current Python frame (f), the associated C stack is still in use,
3114 so recursion_depth must be boosted for the duration.
3116 assert(tstate != NULL);
3117 ++tstate->recursion_depth;
3118 Py_DECREF(f);
3119 --tstate->recursion_depth;
3120 return retval;
3125 static PyObject *
3126 kwd_as_string(PyObject *kwd) {
3127 #ifdef Py_USING_UNICODE
3128 if (PyString_Check(kwd)) {
3129 #else
3130 assert(PyString_Check(kwd));
3131 #endif
3132 Py_INCREF(kwd);
3133 return kwd;
3134 #ifdef Py_USING_UNICODE
3136 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3137 #endif
3141 /* Implementation notes for set_exc_info() and reset_exc_info():
3143 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3144 'exc_traceback'. These always travel together.
3146 - tstate->curexc_ZZZ is the "hot" exception that is set by
3147 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3149 - Once an exception is caught by an except clause, it is transferred
3150 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3151 can pick it up. This is the primary task of set_exc_info().
3152 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
3154 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3156 Long ago, when none of this existed, there were just a few globals:
3157 one set corresponding to the "hot" exception, and one set
3158 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3159 globals; they were simply stored as sys.exc_ZZZ. For backwards
3160 compatibility, they still are!) The problem was that in code like
3161 this:
3163 try:
3164 "something that may fail"
3165 except "some exception":
3166 "do something else first"
3167 "print the exception from sys.exc_ZZZ."
3169 if "do something else first" invoked something that raised and caught
3170 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3171 cause of subtle bugs. I fixed this by changing the semantics as
3172 follows:
3174 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3175 *in that frame*.
3177 - But initially, and as long as no exception is caught in a given
3178 frame, sys.exc_ZZZ will hold the last exception caught in the
3179 previous frame (or the frame before that, etc.).
3181 The first bullet fixed the bug in the above example. The second
3182 bullet was for backwards compatibility: it was (and is) common to
3183 have a function that is called when an exception is caught, and to
3184 have that function access the caught exception via sys.exc_ZZZ.
3185 (Example: traceback.print_exc()).
3187 At the same time I fixed the problem that sys.exc_ZZZ weren't
3188 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3189 but that's really a separate improvement.
3191 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3192 variables to what they were before the current frame was called. The
3193 set_exc_info() function saves them on the frame so that
3194 reset_exc_info() can restore them. The invariant is that
3195 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3196 exception (where "catching" an exception applies only to successful
3197 except clauses); and if the current frame ever caught an exception,
3198 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3199 at the start of the current frame.
3203 static void
3204 set_exc_info(PyThreadState *tstate,
3205 PyObject *type, PyObject *value, PyObject *tb)
3207 PyFrameObject *frame = tstate->frame;
3208 PyObject *tmp_type, *tmp_value, *tmp_tb;
3210 assert(type != NULL);
3211 assert(frame != NULL);
3212 if (frame->f_exc_type == NULL) {
3213 assert(frame->f_exc_value == NULL);
3214 assert(frame->f_exc_traceback == NULL);
3215 /* This frame didn't catch an exception before. */
3216 /* Save previous exception of this thread in this frame. */
3217 if (tstate->exc_type == NULL) {
3218 /* XXX Why is this set to Py_None? */
3219 Py_INCREF(Py_None);
3220 tstate->exc_type = Py_None;
3222 Py_INCREF(tstate->exc_type);
3223 Py_XINCREF(tstate->exc_value);
3224 Py_XINCREF(tstate->exc_traceback);
3225 frame->f_exc_type = tstate->exc_type;
3226 frame->f_exc_value = tstate->exc_value;
3227 frame->f_exc_traceback = tstate->exc_traceback;
3229 /* Set new exception for this thread. */
3230 tmp_type = tstate->exc_type;
3231 tmp_value = tstate->exc_value;
3232 tmp_tb = tstate->exc_traceback;
3233 Py_INCREF(type);
3234 Py_XINCREF(value);
3235 Py_XINCREF(tb);
3236 tstate->exc_type = type;
3237 tstate->exc_value = value;
3238 tstate->exc_traceback = tb;
3239 Py_XDECREF(tmp_type);
3240 Py_XDECREF(tmp_value);
3241 Py_XDECREF(tmp_tb);
3242 /* For b/w compatibility */
3243 PySys_SetObject("exc_type", type);
3244 PySys_SetObject("exc_value", value);
3245 PySys_SetObject("exc_traceback", tb);
3248 static void
3249 reset_exc_info(PyThreadState *tstate)
3251 PyFrameObject *frame;
3252 PyObject *tmp_type, *tmp_value, *tmp_tb;
3254 /* It's a precondition that the thread state's frame caught an
3255 * exception -- verify in a debug build.
3257 assert(tstate != NULL);
3258 frame = tstate->frame;
3259 assert(frame != NULL);
3260 assert(frame->f_exc_type != NULL);
3262 /* Copy the frame's exception info back to the thread state. */
3263 tmp_type = tstate->exc_type;
3264 tmp_value = tstate->exc_value;
3265 tmp_tb = tstate->exc_traceback;
3266 Py_INCREF(frame->f_exc_type);
3267 Py_XINCREF(frame->f_exc_value);
3268 Py_XINCREF(frame->f_exc_traceback);
3269 tstate->exc_type = frame->f_exc_type;
3270 tstate->exc_value = frame->f_exc_value;
3271 tstate->exc_traceback = frame->f_exc_traceback;
3272 Py_XDECREF(tmp_type);
3273 Py_XDECREF(tmp_value);
3274 Py_XDECREF(tmp_tb);
3276 /* For b/w compatibility */
3277 PySys_SetObject("exc_type", frame->f_exc_type);
3278 PySys_SetObject("exc_value", frame->f_exc_value);
3279 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3281 /* Clear the frame's exception info. */
3282 tmp_type = frame->f_exc_type;
3283 tmp_value = frame->f_exc_value;
3284 tmp_tb = frame->f_exc_traceback;
3285 frame->f_exc_type = NULL;
3286 frame->f_exc_value = NULL;
3287 frame->f_exc_traceback = NULL;
3288 Py_DECREF(tmp_type);
3289 Py_XDECREF(tmp_value);
3290 Py_XDECREF(tmp_tb);
3293 /* Logic for the raise statement (too complicated for inlining).
3294 This *consumes* a reference count to each of its arguments. */
3295 static enum why_code
3296 do_raise(PyObject *type, PyObject *value, PyObject *tb)
3298 if (type == NULL) {
3299 /* Reraise */
3300 PyThreadState *tstate = PyThreadState_GET();
3301 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3302 value = tstate->exc_value;
3303 tb = tstate->exc_traceback;
3304 Py_XINCREF(type);
3305 Py_XINCREF(value);
3306 Py_XINCREF(tb);
3309 /* We support the following forms of raise:
3310 raise <class>, <classinstance>
3311 raise <class>, <argument tuple>
3312 raise <class>, None
3313 raise <class>, <argument>
3314 raise <classinstance>, None
3315 raise <string>, <object>
3316 raise <string>, None
3318 An omitted second argument is the same as None.
3320 In addition, raise <tuple>, <anything> is the same as
3321 raising the tuple's first item (and it better have one!);
3322 this rule is applied recursively.
3324 Finally, an optional third argument can be supplied, which
3325 gives the traceback to be substituted (useful when
3326 re-raising an exception after examining it). */
3328 /* First, check the traceback argument, replacing None with
3329 NULL. */
3330 if (tb == Py_None) {
3331 Py_DECREF(tb);
3332 tb = NULL;
3334 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3335 PyErr_SetString(PyExc_TypeError,
3336 "raise: arg 3 must be a traceback or None");
3337 goto raise_error;
3340 /* Next, replace a missing value with None */
3341 if (value == NULL) {
3342 value = Py_None;
3343 Py_INCREF(value);
3346 /* Next, repeatedly, replace a tuple exception with its first item */
3347 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3348 PyObject *tmp = type;
3349 type = PyTuple_GET_ITEM(type, 0);
3350 Py_INCREF(type);
3351 Py_DECREF(tmp);
3354 if (PyExceptionClass_Check(type))
3355 PyErr_NormalizeException(&type, &value, &tb);
3357 else if (PyExceptionInstance_Check(type)) {
3358 /* Raising an instance. The value should be a dummy. */
3359 if (value != Py_None) {
3360 PyErr_SetString(PyExc_TypeError,
3361 "instance exception may not have a separate value");
3362 goto raise_error;
3364 else {
3365 /* Normalize to raise <class>, <instance> */
3366 Py_DECREF(value);
3367 value = type;
3368 type = PyExceptionInstance_Class(type);
3369 Py_INCREF(type);
3372 else {
3373 /* Not something you can raise. You get an exception
3374 anyway, just not what you specified :-) */
3375 PyErr_Format(PyExc_TypeError,
3376 "exceptions must be classes or instances, not %s",
3377 type->ob_type->tp_name);
3378 goto raise_error;
3381 assert(PyExceptionClass_Check(type));
3382 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3383 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3384 "exceptions must derive from BaseException "
3385 "in 3.x", 1) < 0)
3386 goto raise_error;
3389 PyErr_Restore(type, value, tb);
3390 if (tb == NULL)
3391 return WHY_EXCEPTION;
3392 else
3393 return WHY_RERAISE;
3394 raise_error:
3395 Py_XDECREF(value);
3396 Py_XDECREF(type);
3397 Py_XDECREF(tb);
3398 return WHY_EXCEPTION;
3401 /* Iterate v argcnt times and store the results on the stack (via decreasing
3402 sp). Return 1 for success, 0 if error. */
3404 static int
3405 unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
3407 int i = 0;
3408 PyObject *it; /* iter(v) */
3409 PyObject *w;
3411 assert(v != NULL);
3413 it = PyObject_GetIter(v);
3414 if (it == NULL)
3415 goto Error;
3417 for (; i < argcnt; i++) {
3418 w = PyIter_Next(it);
3419 if (w == NULL) {
3420 /* Iterator done, via error or exhaustion. */
3421 if (!PyErr_Occurred()) {
3422 PyErr_Format(PyExc_ValueError,
3423 "need more than %d value%s to unpack",
3424 i, i == 1 ? "" : "s");
3426 goto Error;
3428 *--sp = w;
3431 /* We better have exhausted the iterator now. */
3432 w = PyIter_Next(it);
3433 if (w == NULL) {
3434 if (PyErr_Occurred())
3435 goto Error;
3436 Py_DECREF(it);
3437 return 1;
3439 Py_DECREF(w);
3440 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3441 /* fall through */
3442 Error:
3443 for (; i > 0; i--, sp++)
3444 Py_DECREF(*sp);
3445 Py_XDECREF(it);
3446 return 0;
3450 #ifdef LLTRACE
3451 static int
3452 prtrace(PyObject *v, char *str)
3454 printf("%s ", str);
3455 if (PyObject_Print(v, stdout, 0) != 0)
3456 PyErr_Clear(); /* Don't know what else to do */
3457 printf("\n");
3458 return 1;
3460 #endif
3462 static void
3463 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
3465 PyObject *type, *value, *traceback, *arg;
3466 int err;
3467 PyErr_Fetch(&type, &value, &traceback);
3468 if (value == NULL) {
3469 value = Py_None;
3470 Py_INCREF(value);
3472 arg = PyTuple_Pack(3, type, value, traceback);
3473 if (arg == NULL) {
3474 PyErr_Restore(type, value, traceback);
3475 return;
3477 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3478 Py_DECREF(arg);
3479 if (err == 0)
3480 PyErr_Restore(type, value, traceback);
3481 else {
3482 Py_XDECREF(type);
3483 Py_XDECREF(value);
3484 Py_XDECREF(traceback);
3488 static int
3489 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3490 int what, PyObject *arg)
3492 PyObject *type, *value, *traceback;
3493 int err;
3494 PyErr_Fetch(&type, &value, &traceback);
3495 err = call_trace(func, obj, frame, what, arg);
3496 if (err == 0)
3498 PyErr_Restore(type, value, traceback);
3499 return 0;
3501 else {
3502 Py_XDECREF(type);
3503 Py_XDECREF(value);
3504 Py_XDECREF(traceback);
3505 return -1;
3509 static int
3510 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3511 int what, PyObject *arg)
3513 register PyThreadState *tstate = frame->f_tstate;
3514 int result;
3515 if (tstate->tracing)
3516 return 0;
3517 tstate->tracing++;
3518 tstate->use_tracing = 0;
3519 result = func(obj, frame, what, arg);
3520 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3521 || (tstate->c_profilefunc != NULL));
3522 tstate->tracing--;
3523 return result;
3526 PyObject *
3527 _PyEval_CallTracing(PyObject *func, PyObject *args)
3529 PyFrameObject *frame = PyEval_GetFrame();
3530 PyThreadState *tstate = frame->f_tstate;
3531 int save_tracing = tstate->tracing;
3532 int save_use_tracing = tstate->use_tracing;
3533 PyObject *result;
3535 tstate->tracing = 0;
3536 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3537 || (tstate->c_profilefunc != NULL));
3538 result = PyObject_Call(func, args, NULL);
3539 tstate->tracing = save_tracing;
3540 tstate->use_tracing = save_use_tracing;
3541 return result;
3544 static int
3545 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
3546 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3547 int *instr_prev)
3549 int result = 0;
3551 /* If the last instruction executed isn't in the current
3552 instruction window, reset the window. If the last
3553 instruction happens to fall at the start of a line or if it
3554 represents a jump backwards, call the trace function.
3556 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
3557 int line;
3558 PyAddrPair bounds;
3560 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3561 &bounds);
3562 if (line >= 0) {
3563 frame->f_lineno = line;
3564 result = call_trace(func, obj, frame,
3565 PyTrace_LINE, Py_None);
3567 *instr_lb = bounds.ap_lower;
3568 *instr_ub = bounds.ap_upper;
3570 else if (frame->f_lasti <= *instr_prev) {
3571 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3573 *instr_prev = frame->f_lasti;
3574 return result;
3577 void
3578 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
3580 PyThreadState *tstate = PyThreadState_GET();
3581 PyObject *temp = tstate->c_profileobj;
3582 Py_XINCREF(arg);
3583 tstate->c_profilefunc = NULL;
3584 tstate->c_profileobj = NULL;
3585 /* Must make sure that tracing is not ignored if 'temp' is freed */
3586 tstate->use_tracing = tstate->c_tracefunc != NULL;
3587 Py_XDECREF(temp);
3588 tstate->c_profilefunc = func;
3589 tstate->c_profileobj = arg;
3590 /* Flag that tracing or profiling is turned on */
3591 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
3594 void
3595 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3597 PyThreadState *tstate = PyThreadState_GET();
3598 PyObject *temp = tstate->c_traceobj;
3599 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3600 Py_XINCREF(arg);
3601 tstate->c_tracefunc = NULL;
3602 tstate->c_traceobj = NULL;
3603 /* Must make sure that profiling is not ignored if 'temp' is freed */
3604 tstate->use_tracing = tstate->c_profilefunc != NULL;
3605 Py_XDECREF(temp);
3606 tstate->c_tracefunc = func;
3607 tstate->c_traceobj = arg;
3608 /* Flag that tracing or profiling is turned on */
3609 tstate->use_tracing = ((func != NULL)
3610 || (tstate->c_profilefunc != NULL));
3613 PyObject *
3614 PyEval_GetBuiltins(void)
3616 PyFrameObject *current_frame = PyEval_GetFrame();
3617 if (current_frame == NULL)
3618 return PyThreadState_GET()->interp->builtins;
3619 else
3620 return current_frame->f_builtins;
3623 PyObject *
3624 PyEval_GetLocals(void)
3626 PyFrameObject *current_frame = PyEval_GetFrame();
3627 if (current_frame == NULL)
3628 return NULL;
3629 PyFrame_FastToLocals(current_frame);
3630 return current_frame->f_locals;
3633 PyObject *
3634 PyEval_GetGlobals(void)
3636 PyFrameObject *current_frame = PyEval_GetFrame();
3637 if (current_frame == NULL)
3638 return NULL;
3639 else
3640 return current_frame->f_globals;
3643 PyFrameObject *
3644 PyEval_GetFrame(void)
3646 PyThreadState *tstate = PyThreadState_GET();
3647 return _PyThreadState_GetFrame(tstate);
3651 PyEval_GetRestricted(void)
3653 PyFrameObject *current_frame = PyEval_GetFrame();
3654 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
3658 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
3660 PyFrameObject *current_frame = PyEval_GetFrame();
3661 int result = cf->cf_flags != 0;
3663 if (current_frame != NULL) {
3664 const int codeflags = current_frame->f_code->co_flags;
3665 const int compilerflags = codeflags & PyCF_MASK;
3666 if (compilerflags) {
3667 result = 1;
3668 cf->cf_flags |= compilerflags;
3670 #if 0 /* future keyword */
3671 if (codeflags & CO_GENERATOR_ALLOWED) {
3672 result = 1;
3673 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3675 #endif
3677 return result;
3681 Py_FlushLine(void)
3683 PyObject *f = PySys_GetObject("stdout");
3684 if (f == NULL)
3685 return 0;
3686 if (!PyFile_SoftSpace(f, 0))
3687 return 0;
3688 return PyFile_WriteString("\n", f);
3692 /* External interface to call any callable object.
3693 The arg must be a tuple or NULL. */
3695 #undef PyEval_CallObject
3696 /* for backward compatibility: export this interface */
3698 PyObject *
3699 PyEval_CallObject(PyObject *func, PyObject *arg)
3701 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
3703 #define PyEval_CallObject(func,arg) \
3704 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3706 PyObject *
3707 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
3709 PyObject *result;
3711 if (arg == NULL) {
3712 arg = PyTuple_New(0);
3713 if (arg == NULL)
3714 return NULL;
3716 else if (!PyTuple_Check(arg)) {
3717 PyErr_SetString(PyExc_TypeError,
3718 "argument list must be a tuple");
3719 return NULL;
3721 else
3722 Py_INCREF(arg);
3724 if (kw != NULL && !PyDict_Check(kw)) {
3725 PyErr_SetString(PyExc_TypeError,
3726 "keyword list must be a dictionary");
3727 Py_DECREF(arg);
3728 return NULL;
3731 result = PyObject_Call(func, arg, kw);
3732 Py_DECREF(arg);
3733 return result;
3736 const char *
3737 PyEval_GetFuncName(PyObject *func)
3739 if (PyMethod_Check(func))
3740 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3741 else if (PyFunction_Check(func))
3742 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3743 else if (PyCFunction_Check(func))
3744 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3745 else if (PyClass_Check(func))
3746 return PyString_AsString(((PyClassObject*)func)->cl_name);
3747 else if (PyInstance_Check(func)) {
3748 return PyString_AsString(
3749 ((PyInstanceObject*)func)->in_class->cl_name);
3750 } else {
3751 return func->ob_type->tp_name;
3755 const char *
3756 PyEval_GetFuncDesc(PyObject *func)
3758 if (PyMethod_Check(func))
3759 return "()";
3760 else if (PyFunction_Check(func))
3761 return "()";
3762 else if (PyCFunction_Check(func))
3763 return "()";
3764 else if (PyClass_Check(func))
3765 return " constructor";
3766 else if (PyInstance_Check(func)) {
3767 return " instance";
3768 } else {
3769 return " object";
3773 static void
3774 err_args(PyObject *func, int flags, int nargs)
3776 if (flags & METH_NOARGS)
3777 PyErr_Format(PyExc_TypeError,
3778 "%.200s() takes no arguments (%d given)",
3779 ((PyCFunctionObject *)func)->m_ml->ml_name,
3780 nargs);
3781 else
3782 PyErr_Format(PyExc_TypeError,
3783 "%.200s() takes exactly one argument (%d given)",
3784 ((PyCFunctionObject *)func)->m_ml->ml_name,
3785 nargs);
3788 #define C_TRACE(x, call) \
3789 if (tstate->use_tracing && tstate->c_profilefunc) { \
3790 if (call_trace(tstate->c_profilefunc, \
3791 tstate->c_profileobj, \
3792 tstate->frame, PyTrace_C_CALL, \
3793 func)) { \
3794 x = NULL; \
3796 else { \
3797 x = call; \
3798 if (tstate->c_profilefunc != NULL) { \
3799 if (x == NULL) { \
3800 call_trace_protected(tstate->c_profilefunc, \
3801 tstate->c_profileobj, \
3802 tstate->frame, PyTrace_C_EXCEPTION, \
3803 func); \
3804 /* XXX should pass (type, value, tb) */ \
3805 } else { \
3806 if (call_trace(tstate->c_profilefunc, \
3807 tstate->c_profileobj, \
3808 tstate->frame, PyTrace_C_RETURN, \
3809 func)) { \
3810 Py_DECREF(x); \
3811 x = NULL; \
3816 } else { \
3817 x = call; \
3820 static PyObject *
3821 call_function(PyObject ***pp_stack, int oparg
3822 #ifdef WITH_TSC
3823 , uint64* pintr0, uint64* pintr1
3824 #endif
3827 int na = oparg & 0xff;
3828 int nk = (oparg>>8) & 0xff;
3829 int n = na + 2 * nk;
3830 PyObject **pfunc = (*pp_stack) - n - 1;
3831 PyObject *func = *pfunc;
3832 PyObject *x, *w;
3834 /* Always dispatch PyCFunction first, because these are
3835 presumed to be the most frequent callable object.
3837 if (PyCFunction_Check(func) && nk == 0) {
3838 int flags = PyCFunction_GET_FLAGS(func);
3839 PyThreadState *tstate = PyThreadState_GET();
3841 PCALL(PCALL_CFUNCTION);
3842 if (flags & (METH_NOARGS | METH_O)) {
3843 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3844 PyObject *self = PyCFunction_GET_SELF(func);
3845 if (flags & METH_NOARGS && na == 0) {
3846 C_TRACE(x, (*meth)(self,NULL));
3848 else if (flags & METH_O && na == 1) {
3849 PyObject *arg = EXT_POP(*pp_stack);
3850 C_TRACE(x, (*meth)(self,arg));
3851 Py_DECREF(arg);
3853 else {
3854 err_args(func, flags, na);
3855 x = NULL;
3858 else {
3859 PyObject *callargs;
3860 callargs = load_args(pp_stack, na);
3861 READ_TIMESTAMP(*pintr0);
3862 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3863 READ_TIMESTAMP(*pintr1);
3864 Py_XDECREF(callargs);
3866 } else {
3867 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3868 /* optimize access to bound methods */
3869 PyObject *self = PyMethod_GET_SELF(func);
3870 PCALL(PCALL_METHOD);
3871 PCALL(PCALL_BOUND_METHOD);
3872 Py_INCREF(self);
3873 func = PyMethod_GET_FUNCTION(func);
3874 Py_INCREF(func);
3875 Py_DECREF(*pfunc);
3876 *pfunc = self;
3877 na++;
3878 n++;
3879 } else
3880 Py_INCREF(func);
3881 READ_TIMESTAMP(*pintr0);
3882 if (PyFunction_Check(func))
3883 x = fast_function(func, pp_stack, n, na, nk);
3884 else
3885 x = do_call(func, pp_stack, na, nk);
3886 READ_TIMESTAMP(*pintr1);
3887 Py_DECREF(func);
3890 /* Clear the stack of the function object. Also removes
3891 the arguments in case they weren't consumed already
3892 (fast_function() and err_args() leave them on the stack).
3894 while ((*pp_stack) > pfunc) {
3895 w = EXT_POP(*pp_stack);
3896 Py_DECREF(w);
3897 PCALL(PCALL_POP);
3899 return x;
3902 /* The fast_function() function optimize calls for which no argument
3903 tuple is necessary; the objects are passed directly from the stack.
3904 For the simplest case -- a function that takes only positional
3905 arguments and is called with only positional arguments -- it
3906 inlines the most primitive frame setup code from
3907 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3908 done before evaluating the frame.
3911 static PyObject *
3912 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
3914 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3915 PyObject *globals = PyFunction_GET_GLOBALS(func);
3916 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3917 PyObject **d = NULL;
3918 int nd = 0;
3920 PCALL(PCALL_FUNCTION);
3921 PCALL(PCALL_FAST_FUNCTION);
3922 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
3923 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3924 PyFrameObject *f;
3925 PyObject *retval = NULL;
3926 PyThreadState *tstate = PyThreadState_GET();
3927 PyObject **fastlocals, **stack;
3928 int i;
3930 PCALL(PCALL_FASTER_FUNCTION);
3931 assert(globals != NULL);
3932 /* XXX Perhaps we should create a specialized
3933 PyFrame_New() that doesn't take locals, but does
3934 take builtins without sanity checking them.
3936 assert(tstate != NULL);
3937 f = PyFrame_New(tstate, co, globals, NULL);
3938 if (f == NULL)
3939 return NULL;
3941 fastlocals = f->f_localsplus;
3942 stack = (*pp_stack) - n;
3944 for (i = 0; i < n; i++) {
3945 Py_INCREF(*stack);
3946 fastlocals[i] = *stack++;
3948 retval = PyEval_EvalFrameEx(f,0);
3949 ++tstate->recursion_depth;
3950 Py_DECREF(f);
3951 --tstate->recursion_depth;
3952 return retval;
3954 if (argdefs != NULL) {
3955 d = &PyTuple_GET_ITEM(argdefs, 0);
3956 nd = Py_SIZE(argdefs);
3958 return PyEval_EvalCodeEx(co, globals,
3959 (PyObject *)NULL, (*pp_stack)-n, na,
3960 (*pp_stack)-2*nk, nk, d, nd,
3961 PyFunction_GET_CLOSURE(func));
3964 static PyObject *
3965 update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3966 PyObject *func)
3968 PyObject *kwdict = NULL;
3969 if (orig_kwdict == NULL)
3970 kwdict = PyDict_New();
3971 else {
3972 kwdict = PyDict_Copy(orig_kwdict);
3973 Py_DECREF(orig_kwdict);
3975 if (kwdict == NULL)
3976 return NULL;
3977 while (--nk >= 0) {
3978 int err;
3979 PyObject *value = EXT_POP(*pp_stack);
3980 PyObject *key = EXT_POP(*pp_stack);
3981 if (PyDict_GetItem(kwdict, key) != NULL) {
3982 PyErr_Format(PyExc_TypeError,
3983 "%.200s%s got multiple values "
3984 "for keyword argument '%.200s'",
3985 PyEval_GetFuncName(func),
3986 PyEval_GetFuncDesc(func),
3987 PyString_AsString(key));
3988 Py_DECREF(key);
3989 Py_DECREF(value);
3990 Py_DECREF(kwdict);
3991 return NULL;
3993 err = PyDict_SetItem(kwdict, key, value);
3994 Py_DECREF(key);
3995 Py_DECREF(value);
3996 if (err) {
3997 Py_DECREF(kwdict);
3998 return NULL;
4001 return kwdict;
4004 static PyObject *
4005 update_star_args(int nstack, int nstar, PyObject *stararg,
4006 PyObject ***pp_stack)
4008 PyObject *callargs, *w;
4010 callargs = PyTuple_New(nstack + nstar);
4011 if (callargs == NULL) {
4012 return NULL;
4014 if (nstar) {
4015 int i;
4016 for (i = 0; i < nstar; i++) {
4017 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4018 Py_INCREF(a);
4019 PyTuple_SET_ITEM(callargs, nstack + i, a);
4022 while (--nstack >= 0) {
4023 w = EXT_POP(*pp_stack);
4024 PyTuple_SET_ITEM(callargs, nstack, w);
4026 return callargs;
4029 static PyObject *
4030 load_args(PyObject ***pp_stack, int na)
4032 PyObject *args = PyTuple_New(na);
4033 PyObject *w;
4035 if (args == NULL)
4036 return NULL;
4037 while (--na >= 0) {
4038 w = EXT_POP(*pp_stack);
4039 PyTuple_SET_ITEM(args, na, w);
4041 return args;
4044 static PyObject *
4045 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4047 PyObject *callargs = NULL;
4048 PyObject *kwdict = NULL;
4049 PyObject *result = NULL;
4051 if (nk > 0) {
4052 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4053 if (kwdict == NULL)
4054 goto call_fail;
4056 callargs = load_args(pp_stack, na);
4057 if (callargs == NULL)
4058 goto call_fail;
4059 #ifdef CALL_PROFILE
4060 /* At this point, we have to look at the type of func to
4061 update the call stats properly. Do it here so as to avoid
4062 exposing the call stats machinery outside ceval.c
4064 if (PyFunction_Check(func))
4065 PCALL(PCALL_FUNCTION);
4066 else if (PyMethod_Check(func))
4067 PCALL(PCALL_METHOD);
4068 else if (PyType_Check(func))
4069 PCALL(PCALL_TYPE);
4070 else
4071 PCALL(PCALL_OTHER);
4072 #endif
4073 result = PyObject_Call(func, callargs, kwdict);
4074 call_fail:
4075 Py_XDECREF(callargs);
4076 Py_XDECREF(kwdict);
4077 return result;
4080 static PyObject *
4081 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4083 int nstar = 0;
4084 PyObject *callargs = NULL;
4085 PyObject *stararg = NULL;
4086 PyObject *kwdict = NULL;
4087 PyObject *result = NULL;
4089 if (flags & CALL_FLAG_KW) {
4090 kwdict = EXT_POP(*pp_stack);
4091 if (!PyDict_Check(kwdict)) {
4092 PyObject *d;
4093 d = PyDict_New();
4094 if (d == NULL)
4095 goto ext_call_fail;
4096 if (PyDict_Update(d, kwdict) != 0) {
4097 Py_DECREF(d);
4098 /* PyDict_Update raises attribute
4099 * error (percolated from an attempt
4100 * to get 'keys' attribute) instead of
4101 * a type error if its second argument
4102 * is not a mapping.
4104 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4105 PyErr_Format(PyExc_TypeError,
4106 "%.200s%.200s argument after ** "
4107 "must be a mapping, not %.200s",
4108 PyEval_GetFuncName(func),
4109 PyEval_GetFuncDesc(func),
4110 kwdict->ob_type->tp_name);
4112 goto ext_call_fail;
4114 Py_DECREF(kwdict);
4115 kwdict = d;
4118 if (flags & CALL_FLAG_VAR) {
4119 stararg = EXT_POP(*pp_stack);
4120 if (!PyTuple_Check(stararg)) {
4121 PyObject *t = NULL;
4122 t = PySequence_Tuple(stararg);
4123 if (t == NULL) {
4124 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4125 PyErr_Format(PyExc_TypeError,
4126 "%.200s%.200s argument after * "
4127 "must be a sequence, not %200s",
4128 PyEval_GetFuncName(func),
4129 PyEval_GetFuncDesc(func),
4130 stararg->ob_type->tp_name);
4132 goto ext_call_fail;
4134 Py_DECREF(stararg);
4135 stararg = t;
4137 nstar = PyTuple_GET_SIZE(stararg);
4139 if (nk > 0) {
4140 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4141 if (kwdict == NULL)
4142 goto ext_call_fail;
4144 callargs = update_star_args(na, nstar, stararg, pp_stack);
4145 if (callargs == NULL)
4146 goto ext_call_fail;
4147 #ifdef CALL_PROFILE
4148 /* At this point, we have to look at the type of func to
4149 update the call stats properly. Do it here so as to avoid
4150 exposing the call stats machinery outside ceval.c
4152 if (PyFunction_Check(func))
4153 PCALL(PCALL_FUNCTION);
4154 else if (PyMethod_Check(func))
4155 PCALL(PCALL_METHOD);
4156 else if (PyType_Check(func))
4157 PCALL(PCALL_TYPE);
4158 else
4159 PCALL(PCALL_OTHER);
4160 #endif
4161 result = PyObject_Call(func, callargs, kwdict);
4162 ext_call_fail:
4163 Py_XDECREF(callargs);
4164 Py_XDECREF(kwdict);
4165 Py_XDECREF(stararg);
4166 return result;
4169 /* Extract a slice index from a PyInt or PyLong or an object with the
4170 nb_index slot defined, and store in *pi.
4171 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4172 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
4173 Return 0 on error, 1 on success.
4175 /* Note: If v is NULL, return success without storing into *pi. This
4176 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4177 called by the SLICE opcode with v and/or w equal to NULL.
4180 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
4182 if (v != NULL) {
4183 Py_ssize_t x;
4184 if (PyInt_Check(v)) {
4185 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4186 however, it looks like it should be AsSsize_t.
4187 There should be a comment here explaining why.
4189 x = PyInt_AS_LONG(v);
4191 else if (PyIndex_Check(v)) {
4192 x = PyNumber_AsSsize_t(v, NULL);
4193 if (x == -1 && PyErr_Occurred())
4194 return 0;
4196 else {
4197 PyErr_SetString(PyExc_TypeError,
4198 "slice indices must be integers or "
4199 "None or have an __index__ method");
4200 return 0;
4202 *pi = x;
4204 return 1;
4207 #undef ISINDEX
4208 #define ISINDEX(x) ((x) == NULL || \
4209 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
4211 static PyObject *
4212 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
4214 PyTypeObject *tp = u->ob_type;
4215 PySequenceMethods *sq = tp->tp_as_sequence;
4217 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4218 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4219 if (!_PyEval_SliceIndex(v, &ilow))
4220 return NULL;
4221 if (!_PyEval_SliceIndex(w, &ihigh))
4222 return NULL;
4223 return PySequence_GetSlice(u, ilow, ihigh);
4225 else {
4226 PyObject *slice = PySlice_New(v, w, NULL);
4227 if (slice != NULL) {
4228 PyObject *res = PyObject_GetItem(u, slice);
4229 Py_DECREF(slice);
4230 return res;
4232 else
4233 return NULL;
4237 static int
4238 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4239 /* u[v:w] = x */
4241 PyTypeObject *tp = u->ob_type;
4242 PySequenceMethods *sq = tp->tp_as_sequence;
4244 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4245 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4246 if (!_PyEval_SliceIndex(v, &ilow))
4247 return -1;
4248 if (!_PyEval_SliceIndex(w, &ihigh))
4249 return -1;
4250 if (x == NULL)
4251 return PySequence_DelSlice(u, ilow, ihigh);
4252 else
4253 return PySequence_SetSlice(u, ilow, ihigh, x);
4255 else {
4256 PyObject *slice = PySlice_New(v, w, NULL);
4257 if (slice != NULL) {
4258 int res;
4259 if (x != NULL)
4260 res = PyObject_SetItem(u, slice, x);
4261 else
4262 res = PyObject_DelItem(u, slice);
4263 Py_DECREF(slice);
4264 return res;
4266 else
4267 return -1;
4271 #define Py3kExceptionClass_Check(x) \
4272 (PyType_Check((x)) && \
4273 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4275 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4276 "BaseException is not allowed in 3.x"
4278 static PyObject *
4279 cmp_outcome(int op, register PyObject *v, register PyObject *w)
4281 int res = 0;
4282 switch (op) {
4283 case PyCmp_IS:
4284 res = (v == w);
4285 break;
4286 case PyCmp_IS_NOT:
4287 res = (v != w);
4288 break;
4289 case PyCmp_IN:
4290 res = PySequence_Contains(w, v);
4291 if (res < 0)
4292 return NULL;
4293 break;
4294 case PyCmp_NOT_IN:
4295 res = PySequence_Contains(w, v);
4296 if (res < 0)
4297 return NULL;
4298 res = !res;
4299 break;
4300 case PyCmp_EXC_MATCH:
4301 if (PyTuple_Check(w)) {
4302 Py_ssize_t i, length;
4303 length = PyTuple_Size(w);
4304 for (i = 0; i < length; i += 1) {
4305 PyObject *exc = PyTuple_GET_ITEM(w, i);
4306 if (PyString_Check(exc)) {
4307 int ret_val;
4308 ret_val = PyErr_WarnEx(
4309 PyExc_DeprecationWarning,
4310 "catching of string "
4311 "exceptions is deprecated", 1);
4312 if (ret_val < 0)
4313 return NULL;
4315 else if (Py_Py3kWarningFlag &&
4316 !PyTuple_Check(exc) &&
4317 !Py3kExceptionClass_Check(exc))
4319 int ret_val;
4320 ret_val = PyErr_WarnEx(
4321 PyExc_DeprecationWarning,
4322 CANNOT_CATCH_MSG, 1);
4323 if (ret_val < 0)
4324 return NULL;
4328 else {
4329 if (PyString_Check(w)) {
4330 int ret_val;
4331 ret_val = PyErr_WarnEx(
4332 PyExc_DeprecationWarning,
4333 "catching of string "
4334 "exceptions is deprecated", 1);
4335 if (ret_val < 0)
4336 return NULL;
4338 else if (Py_Py3kWarningFlag &&
4339 !PyTuple_Check(w) &&
4340 !Py3kExceptionClass_Check(w))
4342 int ret_val;
4343 ret_val = PyErr_WarnEx(
4344 PyExc_DeprecationWarning,
4345 CANNOT_CATCH_MSG, 1);
4346 if (ret_val < 0)
4347 return NULL;
4350 res = PyErr_GivenExceptionMatches(v, w);
4351 break;
4352 default:
4353 return PyObject_RichCompare(v, w, op);
4355 v = res ? Py_True : Py_False;
4356 Py_INCREF(v);
4357 return v;
4360 static PyObject *
4361 import_from(PyObject *v, PyObject *name)
4363 PyObject *x;
4365 x = PyObject_GetAttr(v, name);
4366 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4367 PyErr_Format(PyExc_ImportError,
4368 "cannot import name %.230s",
4369 PyString_AsString(name));
4371 return x;
4374 static int
4375 import_all_from(PyObject *locals, PyObject *v)
4377 PyObject *all = PyObject_GetAttrString(v, "__all__");
4378 PyObject *dict, *name, *value;
4379 int skip_leading_underscores = 0;
4380 int pos, err;
4382 if (all == NULL) {
4383 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4384 return -1; /* Unexpected error */
4385 PyErr_Clear();
4386 dict = PyObject_GetAttrString(v, "__dict__");
4387 if (dict == NULL) {
4388 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4389 return -1;
4390 PyErr_SetString(PyExc_ImportError,
4391 "from-import-* object has no __dict__ and no __all__");
4392 return -1;
4394 all = PyMapping_Keys(dict);
4395 Py_DECREF(dict);
4396 if (all == NULL)
4397 return -1;
4398 skip_leading_underscores = 1;
4401 for (pos = 0, err = 0; ; pos++) {
4402 name = PySequence_GetItem(all, pos);
4403 if (name == NULL) {
4404 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4405 err = -1;
4406 else
4407 PyErr_Clear();
4408 break;
4410 if (skip_leading_underscores &&
4411 PyString_Check(name) &&
4412 PyString_AS_STRING(name)[0] == '_')
4414 Py_DECREF(name);
4415 continue;
4417 value = PyObject_GetAttr(v, name);
4418 if (value == NULL)
4419 err = -1;
4420 else if (PyDict_CheckExact(locals))
4421 err = PyDict_SetItem(locals, name, value);
4422 else
4423 err = PyObject_SetItem(locals, name, value);
4424 Py_DECREF(name);
4425 Py_XDECREF(value);
4426 if (err != 0)
4427 break;
4429 Py_DECREF(all);
4430 return err;
4433 static PyObject *
4434 build_class(PyObject *methods, PyObject *bases, PyObject *name)
4436 PyObject *metaclass = NULL, *result, *base;
4438 if (PyDict_Check(methods))
4439 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4440 if (metaclass != NULL)
4441 Py_INCREF(metaclass);
4442 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4443 base = PyTuple_GET_ITEM(bases, 0);
4444 metaclass = PyObject_GetAttrString(base, "__class__");
4445 if (metaclass == NULL) {
4446 PyErr_Clear();
4447 metaclass = (PyObject *)base->ob_type;
4448 Py_INCREF(metaclass);
4451 else {
4452 PyObject *g = PyEval_GetGlobals();
4453 if (g != NULL && PyDict_Check(g))
4454 metaclass = PyDict_GetItemString(g, "__metaclass__");
4455 if (metaclass == NULL)
4456 metaclass = (PyObject *) &PyClass_Type;
4457 Py_INCREF(metaclass);
4459 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4460 NULL);
4461 Py_DECREF(metaclass);
4462 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4463 /* A type error here likely means that the user passed
4464 in a base that was not a class (such the random module
4465 instead of the random.random type). Help them out with
4466 by augmenting the error message with more information.*/
4468 PyObject *ptype, *pvalue, *ptraceback;
4470 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4471 if (PyString_Check(pvalue)) {
4472 PyObject *newmsg;
4473 newmsg = PyString_FromFormat(
4474 "Error when calling the metaclass bases\n"
4475 " %s",
4476 PyString_AS_STRING(pvalue));
4477 if (newmsg != NULL) {
4478 Py_DECREF(pvalue);
4479 pvalue = newmsg;
4482 PyErr_Restore(ptype, pvalue, ptraceback);
4484 return result;
4487 static int
4488 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4489 PyObject *locals)
4491 int n;
4492 PyObject *v;
4493 int plain = 0;
4495 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4496 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4497 /* Backward compatibility hack */
4498 globals = PyTuple_GetItem(prog, 1);
4499 if (n == 3)
4500 locals = PyTuple_GetItem(prog, 2);
4501 prog = PyTuple_GetItem(prog, 0);
4503 if (globals == Py_None) {
4504 globals = PyEval_GetGlobals();
4505 if (locals == Py_None) {
4506 locals = PyEval_GetLocals();
4507 plain = 1;
4509 if (!globals || !locals) {
4510 PyErr_SetString(PyExc_SystemError,
4511 "globals and locals cannot be NULL");
4512 return -1;
4515 else if (locals == Py_None)
4516 locals = globals;
4517 if (!PyString_Check(prog) &&
4518 #ifdef Py_USING_UNICODE
4519 !PyUnicode_Check(prog) &&
4520 #endif
4521 !PyCode_Check(prog) &&
4522 !PyFile_Check(prog)) {
4523 PyErr_SetString(PyExc_TypeError,
4524 "exec: arg 1 must be a string, file, or code object");
4525 return -1;
4527 if (!PyDict_Check(globals)) {
4528 PyErr_SetString(PyExc_TypeError,
4529 "exec: arg 2 must be a dictionary or None");
4530 return -1;
4532 if (!PyMapping_Check(locals)) {
4533 PyErr_SetString(PyExc_TypeError,
4534 "exec: arg 3 must be a mapping or None");
4535 return -1;
4537 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4538 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4539 if (PyCode_Check(prog)) {
4540 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4541 PyErr_SetString(PyExc_TypeError,
4542 "code object passed to exec may not contain free variables");
4543 return -1;
4545 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4547 else if (PyFile_Check(prog)) {
4548 FILE *fp = PyFile_AsFile(prog);
4549 char *name = PyString_AsString(PyFile_Name(prog));
4550 PyCompilerFlags cf;
4551 if (name == NULL)
4552 return -1;
4553 cf.cf_flags = 0;
4554 if (PyEval_MergeCompilerFlags(&cf))
4555 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4556 locals, &cf);
4557 else
4558 v = PyRun_File(fp, name, Py_file_input, globals,
4559 locals);
4561 else {
4562 PyObject *tmp = NULL;
4563 char *str;
4564 PyCompilerFlags cf;
4565 cf.cf_flags = 0;
4566 #ifdef Py_USING_UNICODE
4567 if (PyUnicode_Check(prog)) {
4568 tmp = PyUnicode_AsUTF8String(prog);
4569 if (tmp == NULL)
4570 return -1;
4571 prog = tmp;
4572 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4574 #endif
4575 if (PyString_AsStringAndSize(prog, &str, NULL))
4576 return -1;
4577 if (PyEval_MergeCompilerFlags(&cf))
4578 v = PyRun_StringFlags(str, Py_file_input, globals,
4579 locals, &cf);
4580 else
4581 v = PyRun_String(str, Py_file_input, globals, locals);
4582 Py_XDECREF(tmp);
4584 if (plain)
4585 PyFrame_LocalsToFast(f, 0);
4586 if (v == NULL)
4587 return -1;
4588 Py_DECREF(v);
4589 return 0;
4592 static void
4593 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4595 char *obj_str;
4597 if (!obj)
4598 return;
4600 obj_str = PyString_AsString(obj);
4601 if (!obj_str)
4602 return;
4604 PyErr_Format(exc, format_str, obj_str);
4607 static PyObject *
4608 string_concatenate(PyObject *v, PyObject *w,
4609 PyFrameObject *f, unsigned char *next_instr)
4611 /* This function implements 'variable += expr' when both arguments
4612 are strings. */
4613 Py_ssize_t v_len = PyString_GET_SIZE(v);
4614 Py_ssize_t w_len = PyString_GET_SIZE(w);
4615 Py_ssize_t new_len = v_len + w_len;
4616 if (new_len < 0) {
4617 PyErr_SetString(PyExc_OverflowError,
4618 "strings are too large to concat");
4619 return NULL;
4622 if (v->ob_refcnt == 2) {
4623 /* In the common case, there are 2 references to the value
4624 * stored in 'variable' when the += is performed: one on the
4625 * value stack (in 'v') and one still stored in the
4626 * 'variable'. We try to delete the variable now to reduce
4627 * the refcnt to 1.
4629 switch (*next_instr) {
4630 case STORE_FAST:
4632 int oparg = PEEKARG();
4633 PyObject **fastlocals = f->f_localsplus;
4634 if (GETLOCAL(oparg) == v)
4635 SETLOCAL(oparg, NULL);
4636 break;
4638 case STORE_DEREF:
4640 PyObject **freevars = (f->f_localsplus +
4641 f->f_code->co_nlocals);
4642 PyObject *c = freevars[PEEKARG()];
4643 if (PyCell_GET(c) == v)
4644 PyCell_Set(c, NULL);
4645 break;
4647 case STORE_NAME:
4649 PyObject *names = f->f_code->co_names;
4650 PyObject *name = GETITEM(names, PEEKARG());
4651 PyObject *locals = f->f_locals;
4652 if (PyDict_CheckExact(locals) &&
4653 PyDict_GetItem(locals, name) == v) {
4654 if (PyDict_DelItem(locals, name) != 0) {
4655 PyErr_Clear();
4658 break;
4663 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4664 /* Now we own the last reference to 'v', so we can resize it
4665 * in-place.
4667 if (_PyString_Resize(&v, new_len) != 0) {
4668 /* XXX if _PyString_Resize() fails, 'v' has been
4669 * deallocated so it cannot be put back into
4670 * 'variable'. The MemoryError is raised when there
4671 * is no value in 'variable', which might (very
4672 * remotely) be a cause of incompatibilities.
4674 return NULL;
4676 /* copy 'w' into the newly allocated area of 'v' */
4677 memcpy(PyString_AS_STRING(v) + v_len,
4678 PyString_AS_STRING(w), w_len);
4679 return v;
4681 else {
4682 /* When in-place resizing is not an option. */
4683 PyString_Concat(&v, w);
4684 return v;
4688 #ifdef DYNAMIC_EXECUTION_PROFILE
4690 static PyObject *
4691 getarray(long a[256])
4693 int i;
4694 PyObject *l = PyList_New(256);
4695 if (l == NULL) return NULL;
4696 for (i = 0; i < 256; i++) {
4697 PyObject *x = PyInt_FromLong(a[i]);
4698 if (x == NULL) {
4699 Py_DECREF(l);
4700 return NULL;
4702 PyList_SetItem(l, i, x);
4704 for (i = 0; i < 256; i++)
4705 a[i] = 0;
4706 return l;
4709 PyObject *
4710 _Py_GetDXProfile(PyObject *self, PyObject *args)
4712 #ifndef DXPAIRS
4713 return getarray(dxp);
4714 #else
4715 int i;
4716 PyObject *l = PyList_New(257);
4717 if (l == NULL) return NULL;
4718 for (i = 0; i < 257; i++) {
4719 PyObject *x = getarray(dxpairs[i]);
4720 if (x == NULL) {
4721 Py_DECREF(l);
4722 return NULL;
4724 PyList_SetItem(l, i, x);
4726 return l;
4727 #endif
4730 #endif