On 64-bit platforms running test_struct after test_tarfile would fail
[python.git] / Python / ceval.c
blob803815e8e1bafc91259d33d8053d9109eeb20fe0
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 platform,
32 irrespective of OS. POWER? Who knows :-) */
34 #define READ_TIMESTAMP(var) ppc_getcounter(&var)
36 static void
37 ppc_getcounter(uint64 *v)
39 register unsigned long tbu, tb, tbu2;
41 loop:
42 asm volatile ("mftbu %0" : "=r" (tbu) );
43 asm volatile ("mftb %0" : "=r" (tb) );
44 asm volatile ("mftbu %0" : "=r" (tbu2));
45 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47 /* The slightly peculiar way of writing the next lines is
48 compiled better by GCC than any other way I tried. */
49 ((long*)(v))[0] = tbu;
50 ((long*)(v))[1] = tb;
53 #else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
55 #define READ_TIMESTAMP(val) \
56 __asm__ __volatile__("rdtsc" : "=A" (val))
58 #endif
60 void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
61 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63 uint64 intr, inst, loop;
64 PyThreadState *tstate = PyThreadState_Get();
65 if (!tstate->interp->tscdump)
66 return;
67 intr = intr1 - intr0;
68 inst = inst1 - inst0 - intr;
69 loop = loop1 - loop0 - intr;
70 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
71 opcode, ticked, inst, loop);
74 #endif
76 /* Turn this on if your compiler chokes on the big switch: */
77 /* #define CASE_TOO_BIG 1 */
79 #ifdef Py_DEBUG
80 /* For debugging the interpreter: */
81 #define LLTRACE 1 /* Low-level trace feature */
82 #define CHECKEXC 1 /* Double-check exception checking */
83 #endif
85 typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
87 /* Forward declarations */
88 #ifdef WITH_TSC
89 static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
90 #else
91 static PyObject * call_function(PyObject ***, int);
92 #endif
93 static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
94 static PyObject * do_call(PyObject *, PyObject ***, int, int);
95 static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
96 static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
97 static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
98 static PyObject * load_args(PyObject ***, int);
99 #define CALL_FLAG_VAR 1
100 #define CALL_FLAG_KW 2
102 #ifdef LLTRACE
103 static int lltrace;
104 static int prtrace(PyObject *, char *);
105 #endif
106 static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
107 int, PyObject *);
108 static void call_trace_protected(Py_tracefunc, PyObject *,
109 PyFrameObject *, int, PyObject *);
110 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
111 static int maybe_call_line_trace(Py_tracefunc, PyObject *,
112 PyFrameObject *, int *, int *, int *);
114 static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
115 static int assign_slice(PyObject *, PyObject *,
116 PyObject *, PyObject *);
117 static PyObject * cmp_outcome(int, PyObject *, PyObject *);
118 static PyObject * import_from(PyObject *, PyObject *);
119 static int import_all_from(PyObject *, PyObject *);
120 static PyObject * build_class(PyObject *, PyObject *, PyObject *);
121 static int exec_statement(PyFrameObject *,
122 PyObject *, PyObject *, PyObject *);
123 static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
124 static void reset_exc_info(PyThreadState *);
125 static void format_exc_check_arg(PyObject *, char *, PyObject *);
126 static PyObject * string_concatenate(PyObject *, PyObject *,
127 PyFrameObject *, unsigned char *);
129 #define NAME_ERROR_MSG \
130 "name '%.200s' is not defined"
131 #define GLOBAL_NAME_ERROR_MSG \
132 "global name '%.200s' is not defined"
133 #define UNBOUNDLOCAL_ERROR_MSG \
134 "local variable '%.200s' referenced before assignment"
135 #define UNBOUNDFREE_ERROR_MSG \
136 "free variable '%.200s' referenced before assignment" \
137 " in enclosing scope"
139 /* Dynamic execution profile */
140 #ifdef DYNAMIC_EXECUTION_PROFILE
141 #ifdef DXPAIRS
142 static long dxpairs[257][256];
143 #define dxp dxpairs[256]
144 #else
145 static long dxp[256];
146 #endif
147 #endif
149 /* Function call profile */
150 #ifdef CALL_PROFILE
151 #define PCALL_NUM 11
152 static int pcall[PCALL_NUM];
154 #define PCALL_ALL 0
155 #define PCALL_FUNCTION 1
156 #define PCALL_FAST_FUNCTION 2
157 #define PCALL_FASTER_FUNCTION 3
158 #define PCALL_METHOD 4
159 #define PCALL_BOUND_METHOD 5
160 #define PCALL_CFUNCTION 6
161 #define PCALL_TYPE 7
162 #define PCALL_GENERATOR 8
163 #define PCALL_OTHER 9
164 #define PCALL_POP 10
166 /* Notes about the statistics
168 PCALL_FAST stats
170 FAST_FUNCTION means no argument tuple needs to be created.
171 FASTER_FUNCTION means that the fast-path frame setup code is used.
173 If there is a method call where the call can be optimized by changing
174 the argument tuple and calling the function directly, it gets recorded
175 twice.
177 As a result, the relationship among the statistics appears to be
178 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
179 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
180 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
181 PCALL_METHOD > PCALL_BOUND_METHOD
184 #define PCALL(POS) pcall[POS]++
186 PyObject *
187 PyEval_GetCallStats(PyObject *self)
189 return Py_BuildValue("iiiiiiiiii",
190 pcall[0], pcall[1], pcall[2], pcall[3],
191 pcall[4], pcall[5], pcall[6], pcall[7],
192 pcall[8], pcall[9]);
194 #else
195 #define PCALL(O)
197 PyObject *
198 PyEval_GetCallStats(PyObject *self)
200 Py_INCREF(Py_None);
201 return Py_None;
203 #endif
206 #ifdef WITH_THREAD
208 #ifndef DONT_HAVE_ERRNO_H
209 #include <errno.h>
210 #endif
211 #include "pythread.h"
213 static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
214 static long main_thread = 0;
217 PyEval_ThreadsInitialized(void)
219 return interpreter_lock != 0;
222 void
223 PyEval_InitThreads(void)
225 if (interpreter_lock)
226 return;
227 interpreter_lock = PyThread_allocate_lock();
228 PyThread_acquire_lock(interpreter_lock, 1);
229 main_thread = PyThread_get_thread_ident();
232 void
233 PyEval_AcquireLock(void)
235 PyThread_acquire_lock(interpreter_lock, 1);
238 void
239 PyEval_ReleaseLock(void)
241 PyThread_release_lock(interpreter_lock);
244 void
245 PyEval_AcquireThread(PyThreadState *tstate)
247 if (tstate == NULL)
248 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
249 /* Check someone has called PyEval_InitThreads() to create the lock */
250 assert(interpreter_lock);
251 PyThread_acquire_lock(interpreter_lock, 1);
252 if (PyThreadState_Swap(tstate) != NULL)
253 Py_FatalError(
254 "PyEval_AcquireThread: non-NULL old thread state");
257 void
258 PyEval_ReleaseThread(PyThreadState *tstate)
260 if (tstate == NULL)
261 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
262 if (PyThreadState_Swap(NULL) != tstate)
263 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
264 PyThread_release_lock(interpreter_lock);
267 /* This function is called from PyOS_AfterFork to ensure that newly
268 created child processes don't hold locks referring to threads which
269 are not running in the child process. (This could also be done using
270 pthread_atfork mechanism, at least for the pthreads implementation.) */
272 void
273 PyEval_ReInitThreads(void)
275 if (!interpreter_lock)
276 return;
277 /*XXX Can't use PyThread_free_lock here because it does too
278 much error-checking. Doing this cleanly would require
279 adding a new function to each thread_*.h. Instead, just
280 create a new lock and waste a little bit of memory */
281 interpreter_lock = PyThread_allocate_lock();
282 PyThread_acquire_lock(interpreter_lock, 1);
283 main_thread = PyThread_get_thread_ident();
285 #endif
287 /* Functions save_thread and restore_thread are always defined so
288 dynamically loaded modules needn't be compiled separately for use
289 with and without threads: */
291 PyThreadState *
292 PyEval_SaveThread(void)
294 PyThreadState *tstate = PyThreadState_Swap(NULL);
295 if (tstate == NULL)
296 Py_FatalError("PyEval_SaveThread: NULL tstate");
297 #ifdef WITH_THREAD
298 if (interpreter_lock)
299 PyThread_release_lock(interpreter_lock);
300 #endif
301 return tstate;
304 void
305 PyEval_RestoreThread(PyThreadState *tstate)
307 if (tstate == NULL)
308 Py_FatalError("PyEval_RestoreThread: NULL tstate");
309 #ifdef WITH_THREAD
310 if (interpreter_lock) {
311 int err = errno;
312 PyThread_acquire_lock(interpreter_lock, 1);
313 errno = err;
315 #endif
316 PyThreadState_Swap(tstate);
320 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
321 signal handlers or Mac I/O completion routines) can schedule calls
322 to a function to be called synchronously.
323 The synchronous function is called with one void* argument.
324 It should return 0 for success or -1 for failure -- failure should
325 be accompanied by an exception.
327 If registry succeeds, the registry function returns 0; if it fails
328 (e.g. due to too many pending calls) it returns -1 (without setting
329 an exception condition).
331 Note that because registry may occur from within signal handlers,
332 or other asynchronous events, calling malloc() is unsafe!
334 #ifdef WITH_THREAD
335 Any thread can schedule pending calls, but only the main thread
336 will execute them.
337 #endif
339 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
340 There are two possible race conditions:
341 (1) nested asynchronous registry calls;
342 (2) registry calls made while pending calls are being processed.
343 While (1) is very unlikely, (2) is a real possibility.
344 The current code is safe against (2), but not against (1).
345 The safety against (2) is derived from the fact that only one
346 thread (the main thread) ever takes things out of the queue.
348 XXX Darn! With the advent of thread state, we should have an array
349 of pending calls per thread in the thread state! Later...
352 #define NPENDINGCALLS 32
353 static struct {
354 int (*func)(void *);
355 void *arg;
356 } pendingcalls[NPENDINGCALLS];
357 static volatile int pendingfirst = 0;
358 static volatile int pendinglast = 0;
359 static volatile int things_to_do = 0;
362 Py_AddPendingCall(int (*func)(void *), void *arg)
364 static volatile int busy = 0;
365 int i, j;
366 /* XXX Begin critical section */
367 /* XXX If you want this to be safe against nested
368 XXX asynchronous calls, you'll have to work harder! */
369 if (busy)
370 return -1;
371 busy = 1;
372 i = pendinglast;
373 j = (i + 1) % NPENDINGCALLS;
374 if (j == pendingfirst) {
375 busy = 0;
376 return -1; /* Queue full */
378 pendingcalls[i].func = func;
379 pendingcalls[i].arg = arg;
380 pendinglast = j;
382 _Py_Ticker = 0;
383 things_to_do = 1; /* Signal main loop */
384 busy = 0;
385 /* XXX End critical section */
386 return 0;
390 Py_MakePendingCalls(void)
392 static int busy = 0;
393 #ifdef WITH_THREAD
394 if (main_thread && PyThread_get_thread_ident() != main_thread)
395 return 0;
396 #endif
397 if (busy)
398 return 0;
399 busy = 1;
400 things_to_do = 0;
401 for (;;) {
402 int i;
403 int (*func)(void *);
404 void *arg;
405 i = pendingfirst;
406 if (i == pendinglast)
407 break; /* Queue empty */
408 func = pendingcalls[i].func;
409 arg = pendingcalls[i].arg;
410 pendingfirst = (i + 1) % NPENDINGCALLS;
411 if (func(arg) < 0) {
412 busy = 0;
413 things_to_do = 1; /* We're not done yet */
414 return -1;
417 busy = 0;
418 return 0;
422 /* The interpreter's recursion limit */
424 #ifndef Py_DEFAULT_RECURSION_LIMIT
425 #define Py_DEFAULT_RECURSION_LIMIT 1000
426 #endif
427 static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
428 int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
431 Py_GetRecursionLimit(void)
433 return recursion_limit;
436 void
437 Py_SetRecursionLimit(int new_limit)
439 recursion_limit = new_limit;
440 _Py_CheckRecursionLimit = recursion_limit;
443 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
444 if the recursion_depth reaches _Py_CheckRecursionLimit.
445 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
446 to guarantee that _Py_CheckRecursiveCall() is regularly called.
447 Without USE_STACKCHECK, there is no need for this. */
449 _Py_CheckRecursiveCall(char *where)
451 PyThreadState *tstate = PyThreadState_GET();
453 #ifdef USE_STACKCHECK
454 if (PyOS_CheckStack()) {
455 --tstate->recursion_depth;
456 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
457 return -1;
459 #endif
460 if (tstate->recursion_depth > recursion_limit) {
461 --tstate->recursion_depth;
462 PyErr_Format(PyExc_RuntimeError,
463 "maximum recursion depth exceeded%s",
464 where);
465 return -1;
467 _Py_CheckRecursionLimit = recursion_limit;
468 return 0;
471 /* Status code for main loop (reason for stack unwind) */
472 enum why_code {
473 WHY_NOT = 0x0001, /* No error */
474 WHY_EXCEPTION = 0x0002, /* Exception occurred */
475 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
476 WHY_RETURN = 0x0008, /* 'return' statement */
477 WHY_BREAK = 0x0010, /* 'break' statement */
478 WHY_CONTINUE = 0x0020, /* 'continue' statement */
479 WHY_YIELD = 0x0040 /* 'yield' operator */
482 static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
483 static int unpack_iterable(PyObject *, int, PyObject **);
485 /* for manipulating the thread switch and periodic "stuff" - used to be
486 per thread, now just a pair o' globals */
487 int _Py_CheckInterval = 100;
488 volatile int _Py_Ticker = 100;
490 PyObject *
491 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
493 /* XXX raise SystemError if globals is NULL */
494 return PyEval_EvalCodeEx(co,
495 globals, locals,
496 (PyObject **)NULL, 0,
497 (PyObject **)NULL, 0,
498 (PyObject **)NULL, 0,
499 NULL);
503 /* Interpreter main loop */
505 PyObject *
506 PyEval_EvalFrame(PyFrameObject *f) {
507 /* This is for backward compatibility with extension modules that
508 used this API; core interpreter code should call PyEval_EvalFrameEx() */
509 return PyEval_EvalFrameEx(f, 0);
512 PyObject *
513 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
515 #ifdef DXPAIRS
516 int lastopcode = 0;
517 #endif
518 register PyObject **stack_pointer; /* Next free slot in value stack */
519 register unsigned char *next_instr;
520 register int opcode; /* Current opcode */
521 register int oparg; /* Current opcode argument, if any */
522 register enum why_code why; /* Reason for block stack unwind */
523 register int err; /* Error status -- nonzero if error */
524 register PyObject *x; /* Result object -- NULL if error */
525 register PyObject *v; /* Temporary objects popped off stack */
526 register PyObject *w;
527 register PyObject *u;
528 register PyObject *t;
529 register PyObject *stream = NULL; /* for PRINT opcodes */
530 register PyObject **fastlocals, **freevars;
531 PyObject *retval = NULL; /* Return value */
532 PyThreadState *tstate = PyThreadState_GET();
533 PyCodeObject *co;
535 /* when tracing we set things up so that
537 not (instr_lb <= current_bytecode_offset < instr_ub)
539 is true when the line being executed has changed. The
540 initial values are such as to make this false the first
541 time it is tested. */
542 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
544 unsigned char *first_instr;
545 PyObject *names;
546 PyObject *consts;
547 #if defined(Py_DEBUG) || defined(LLTRACE)
548 /* Make it easier to find out where we are with a debugger */
549 char *filename;
550 #endif
552 /* Tuple access macros */
554 #ifndef Py_DEBUG
555 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
556 #else
557 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
558 #endif
560 #ifdef WITH_TSC
561 /* Use Pentium timestamp counter to mark certain events:
562 inst0 -- beginning of switch statement for opcode dispatch
563 inst1 -- end of switch statement (may be skipped)
564 loop0 -- the top of the mainloop
565 loop1 -- place where control returns again to top of mainloop
566 (may be skipped)
567 intr1 -- beginning of long interruption
568 intr2 -- end of long interruption
570 Many opcodes call out to helper C functions. In some cases, the
571 time in those functions should be counted towards the time for the
572 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
573 calls another Python function; there's no point in charge all the
574 bytecode executed by the called function to the caller.
576 It's hard to make a useful judgement statically. In the presence
577 of operator overloading, it's impossible to tell if a call will
578 execute new Python code or not.
580 It's a case-by-case judgement. I'll use intr1 for the following
581 cases:
583 EXEC_STMT
584 IMPORT_STAR
585 IMPORT_FROM
586 CALL_FUNCTION (and friends)
589 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
590 int ticked = 0;
592 READ_TIMESTAMP(inst0);
593 READ_TIMESTAMP(inst1);
594 READ_TIMESTAMP(loop0);
595 READ_TIMESTAMP(loop1);
597 /* shut up the compiler */
598 opcode = 0;
599 #endif
601 /* Code access macros */
603 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
604 #define NEXTOP() (*next_instr++)
605 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
606 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
607 #define JUMPTO(x) (next_instr = first_instr + (x))
608 #define JUMPBY(x) (next_instr += (x))
610 /* OpCode prediction macros
611 Some opcodes tend to come in pairs thus making it possible to predict
612 the second code when the first is run. For example, COMPARE_OP is often
613 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
614 followed by a POP_TOP.
616 Verifying the prediction costs a single high-speed test of register
617 variable against a constant. If the pairing was good, then the
618 processor has a high likelihood of making its own successful branch
619 prediction which results in a nearly zero overhead transition to the
620 next opcode.
622 A successful prediction saves a trip through the eval-loop including
623 its two unpredictable branches, the HASARG test and the switch-case.
625 If collecting opcode statistics, turn off prediction so that
626 statistics are accurately maintained (the predictions bypass
627 the opcode frequency counter updates).
630 #ifdef DYNAMIC_EXECUTION_PROFILE
631 #define PREDICT(op) if (0) goto PRED_##op
632 #else
633 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
634 #endif
636 #define PREDICTED(op) PRED_##op: next_instr++
637 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
639 /* Stack manipulation macros */
641 /* The stack can grow at most MAXINT deep, as co_nlocals and
642 co_stacksize are ints. */
643 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
644 #define EMPTY() (STACK_LEVEL() == 0)
645 #define TOP() (stack_pointer[-1])
646 #define SECOND() (stack_pointer[-2])
647 #define THIRD() (stack_pointer[-3])
648 #define FOURTH() (stack_pointer[-4])
649 #define SET_TOP(v) (stack_pointer[-1] = (v))
650 #define SET_SECOND(v) (stack_pointer[-2] = (v))
651 #define SET_THIRD(v) (stack_pointer[-3] = (v))
652 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
653 #define BASIC_STACKADJ(n) (stack_pointer += n)
654 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
655 #define BASIC_POP() (*--stack_pointer)
657 #ifdef LLTRACE
658 #define PUSH(v) { (void)(BASIC_PUSH(v), \
659 lltrace && prtrace(TOP(), "push")); \
660 assert(STACK_LEVEL() <= co->co_stacksize); }
661 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
662 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
663 lltrace && prtrace(TOP(), "stackadj")); \
664 assert(STACK_LEVEL() <= co->co_stacksize); }
665 #define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER))
666 #else
667 #define PUSH(v) BASIC_PUSH(v)
668 #define POP() BASIC_POP()
669 #define STACKADJ(n) BASIC_STACKADJ(n)
670 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
671 #endif
673 /* Local variable macros */
675 #define GETLOCAL(i) (fastlocals[i])
677 /* The SETLOCAL() macro must not DECREF the local variable in-place and
678 then store the new value; it must copy the old value to a temporary
679 value, then store the new value, and then DECREF the temporary value.
680 This is because it is possible that during the DECREF the frame is
681 accessed by other code (e.g. a __del__ method or gc.collect()) and the
682 variable would be pointing to already-freed memory. */
683 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
684 GETLOCAL(i) = value; \
685 Py_XDECREF(tmp); } while (0)
687 /* Start of code */
689 if (f == NULL)
690 return NULL;
692 /* push frame */
693 if (Py_EnterRecursiveCall(""))
694 return NULL;
696 tstate->frame = f;
698 if (tstate->use_tracing) {
699 if (tstate->c_tracefunc != NULL) {
700 /* tstate->c_tracefunc, if defined, is a
701 function that will be called on *every* entry
702 to a code block. Its return value, if not
703 None, is a function that will be called at
704 the start of each executed line of code.
705 (Actually, the function must return itself
706 in order to continue tracing.) The trace
707 functions are called with three arguments:
708 a pointer to the current frame, a string
709 indicating why the function is called, and
710 an argument which depends on the situation.
711 The global trace function is also called
712 whenever an exception is detected. */
713 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
714 f, PyTrace_CALL, Py_None)) {
715 /* Trace function raised an error */
716 goto exit_eval_frame;
719 if (tstate->c_profilefunc != NULL) {
720 /* Similar for c_profilefunc, except it needn't
721 return itself and isn't called for "line" events */
722 if (call_trace(tstate->c_profilefunc,
723 tstate->c_profileobj,
724 f, PyTrace_CALL, Py_None)) {
725 /* Profile function raised an error */
726 goto exit_eval_frame;
731 co = f->f_code;
732 names = co->co_names;
733 consts = co->co_consts;
734 fastlocals = f->f_localsplus;
735 freevars = f->f_localsplus + co->co_nlocals;
736 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
737 /* An explanation is in order for the next line.
739 f->f_lasti now refers to the index of the last instruction
740 executed. You might think this was obvious from the name, but
741 this wasn't always true before 2.3! PyFrame_New now sets
742 f->f_lasti to -1 (i.e. the index *before* the first instruction)
743 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
744 does work. Promise. */
745 next_instr = first_instr + f->f_lasti + 1;
746 stack_pointer = f->f_stacktop;
747 assert(stack_pointer != NULL);
748 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
750 #ifdef LLTRACE
751 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
752 #endif
753 #if defined(Py_DEBUG) || defined(LLTRACE)
754 filename = PyString_AsString(co->co_filename);
755 #endif
757 why = WHY_NOT;
758 err = 0;
759 x = Py_None; /* Not a reference, just anything non-NULL */
760 w = NULL;
762 if (throwflag) { /* support for generator.throw() */
763 why = WHY_EXCEPTION;
764 goto on_error;
767 for (;;) {
768 #ifdef WITH_TSC
769 if (inst1 == 0) {
770 /* Almost surely, the opcode executed a break
771 or a continue, preventing inst1 from being set
772 on the way out of the loop.
774 READ_TIMESTAMP(inst1);
775 loop1 = inst1;
777 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
778 intr0, intr1);
779 ticked = 0;
780 inst1 = 0;
781 intr0 = 0;
782 intr1 = 0;
783 READ_TIMESTAMP(loop0);
784 #endif
785 assert(stack_pointer >= f->f_valuestack); /* else underflow */
786 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
788 /* Do periodic things. Doing this every time through
789 the loop would add too much overhead, so we do it
790 only every Nth instruction. We also do it if
791 ``things_to_do'' is set, i.e. when an asynchronous
792 event needs attention (e.g. a signal handler or
793 async I/O handler); see Py_AddPendingCall() and
794 Py_MakePendingCalls() above. */
796 if (--_Py_Ticker < 0) {
797 if (*next_instr == SETUP_FINALLY) {
798 /* Make the last opcode before
799 a try: finally: block uninterruptable. */
800 goto fast_next_opcode;
802 _Py_Ticker = _Py_CheckInterval;
803 tstate->tick_counter++;
804 #ifdef WITH_TSC
805 ticked = 1;
806 #endif
807 if (things_to_do) {
808 if (Py_MakePendingCalls() < 0) {
809 why = WHY_EXCEPTION;
810 goto on_error;
812 if (things_to_do)
813 /* MakePendingCalls() didn't succeed.
814 Force early re-execution of this
815 "periodic" code, possibly after
816 a thread switch */
817 _Py_Ticker = 0;
819 #ifdef WITH_THREAD
820 if (interpreter_lock) {
821 /* Give another thread a chance */
823 if (PyThreadState_Swap(NULL) != tstate)
824 Py_FatalError("ceval: tstate mix-up");
825 PyThread_release_lock(interpreter_lock);
827 /* Other threads may run now */
829 PyThread_acquire_lock(interpreter_lock, 1);
830 if (PyThreadState_Swap(tstate) != NULL)
831 Py_FatalError("ceval: orphan tstate");
833 /* Check for thread interrupts */
835 if (tstate->async_exc != NULL) {
836 x = tstate->async_exc;
837 tstate->async_exc = NULL;
838 PyErr_SetNone(x);
839 Py_DECREF(x);
840 why = WHY_EXCEPTION;
841 goto on_error;
844 #endif
847 fast_next_opcode:
848 f->f_lasti = INSTR_OFFSET();
850 /* line-by-line tracing support */
852 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
853 /* see maybe_call_line_trace
854 for expository comments */
855 f->f_stacktop = stack_pointer;
857 err = maybe_call_line_trace(tstate->c_tracefunc,
858 tstate->c_traceobj,
859 f, &instr_lb, &instr_ub,
860 &instr_prev);
861 /* Reload possibly changed frame fields */
862 JUMPTO(f->f_lasti);
863 if (f->f_stacktop != NULL) {
864 stack_pointer = f->f_stacktop;
865 f->f_stacktop = NULL;
867 if (err) {
868 /* trace function raised an exception */
869 goto on_error;
873 /* Extract opcode and argument */
875 opcode = NEXTOP();
876 oparg = 0; /* allows oparg to be stored in a register because
877 it doesn't have to be remembered across a full loop */
878 if (HAS_ARG(opcode))
879 oparg = NEXTARG();
880 dispatch_opcode:
881 #ifdef DYNAMIC_EXECUTION_PROFILE
882 #ifdef DXPAIRS
883 dxpairs[lastopcode][opcode]++;
884 lastopcode = opcode;
885 #endif
886 dxp[opcode]++;
887 #endif
889 #ifdef LLTRACE
890 /* Instruction tracing */
892 if (lltrace) {
893 if (HAS_ARG(opcode)) {
894 printf("%d: %d, %d\n",
895 f->f_lasti, opcode, oparg);
897 else {
898 printf("%d: %d\n",
899 f->f_lasti, opcode);
902 #endif
904 /* Main switch on opcode */
905 READ_TIMESTAMP(inst0);
907 switch (opcode) {
909 /* BEWARE!
910 It is essential that any operation that fails sets either
911 x to NULL, err to nonzero, or why to anything but WHY_NOT,
912 and that no operation that succeeds does this! */
914 /* case STOP_CODE: this is an error! */
916 case NOP:
917 goto fast_next_opcode;
919 case LOAD_FAST:
920 x = GETLOCAL(oparg);
921 if (x != NULL) {
922 Py_INCREF(x);
923 PUSH(x);
924 goto fast_next_opcode;
926 format_exc_check_arg(PyExc_UnboundLocalError,
927 UNBOUNDLOCAL_ERROR_MSG,
928 PyTuple_GetItem(co->co_varnames, oparg));
929 break;
931 case LOAD_CONST:
932 x = GETITEM(consts, oparg);
933 Py_INCREF(x);
934 PUSH(x);
935 goto fast_next_opcode;
937 PREDICTED_WITH_ARG(STORE_FAST);
938 case STORE_FAST:
939 v = POP();
940 SETLOCAL(oparg, v);
941 goto fast_next_opcode;
943 PREDICTED(POP_TOP);
944 case POP_TOP:
945 v = POP();
946 Py_DECREF(v);
947 goto fast_next_opcode;
949 case ROT_TWO:
950 v = TOP();
951 w = SECOND();
952 SET_TOP(w);
953 SET_SECOND(v);
954 goto fast_next_opcode;
956 case ROT_THREE:
957 v = TOP();
958 w = SECOND();
959 x = THIRD();
960 SET_TOP(w);
961 SET_SECOND(x);
962 SET_THIRD(v);
963 goto fast_next_opcode;
965 case ROT_FOUR:
966 u = TOP();
967 v = SECOND();
968 w = THIRD();
969 x = FOURTH();
970 SET_TOP(v);
971 SET_SECOND(w);
972 SET_THIRD(x);
973 SET_FOURTH(u);
974 goto fast_next_opcode;
976 case DUP_TOP:
977 v = TOP();
978 Py_INCREF(v);
979 PUSH(v);
980 goto fast_next_opcode;
982 case DUP_TOPX:
983 if (oparg == 2) {
984 x = TOP();
985 Py_INCREF(x);
986 w = SECOND();
987 Py_INCREF(w);
988 STACKADJ(2);
989 SET_TOP(x);
990 SET_SECOND(w);
991 goto fast_next_opcode;
992 } else if (oparg == 3) {
993 x = TOP();
994 Py_INCREF(x);
995 w = SECOND();
996 Py_INCREF(w);
997 v = THIRD();
998 Py_INCREF(v);
999 STACKADJ(3);
1000 SET_TOP(x);
1001 SET_SECOND(w);
1002 SET_THIRD(v);
1003 goto fast_next_opcode;
1005 Py_FatalError("invalid argument to DUP_TOPX"
1006 " (bytecode corruption?)");
1007 break;
1009 case UNARY_POSITIVE:
1010 v = TOP();
1011 x = PyNumber_Positive(v);
1012 Py_DECREF(v);
1013 SET_TOP(x);
1014 if (x != NULL) continue;
1015 break;
1017 case UNARY_NEGATIVE:
1018 v = TOP();
1019 x = PyNumber_Negative(v);
1020 Py_DECREF(v);
1021 SET_TOP(x);
1022 if (x != NULL) continue;
1023 break;
1025 case UNARY_NOT:
1026 v = TOP();
1027 err = PyObject_IsTrue(v);
1028 Py_DECREF(v);
1029 if (err == 0) {
1030 Py_INCREF(Py_True);
1031 SET_TOP(Py_True);
1032 continue;
1034 else if (err > 0) {
1035 Py_INCREF(Py_False);
1036 SET_TOP(Py_False);
1037 err = 0;
1038 continue;
1040 STACKADJ(-1);
1041 break;
1043 case UNARY_CONVERT:
1044 v = TOP();
1045 x = PyObject_Repr(v);
1046 Py_DECREF(v);
1047 SET_TOP(x);
1048 if (x != NULL) continue;
1049 break;
1051 case UNARY_INVERT:
1052 v = TOP();
1053 x = PyNumber_Invert(v);
1054 Py_DECREF(v);
1055 SET_TOP(x);
1056 if (x != NULL) continue;
1057 break;
1059 case BINARY_POWER:
1060 w = POP();
1061 v = TOP();
1062 x = PyNumber_Power(v, w, Py_None);
1063 Py_DECREF(v);
1064 Py_DECREF(w);
1065 SET_TOP(x);
1066 if (x != NULL) continue;
1067 break;
1069 case BINARY_MULTIPLY:
1070 w = POP();
1071 v = TOP();
1072 x = PyNumber_Multiply(v, w);
1073 Py_DECREF(v);
1074 Py_DECREF(w);
1075 SET_TOP(x);
1076 if (x != NULL) continue;
1077 break;
1079 case BINARY_DIVIDE:
1080 if (!_Py_QnewFlag) {
1081 w = POP();
1082 v = TOP();
1083 x = PyNumber_Divide(v, w);
1084 Py_DECREF(v);
1085 Py_DECREF(w);
1086 SET_TOP(x);
1087 if (x != NULL) continue;
1088 break;
1090 /* -Qnew is in effect: fall through to
1091 BINARY_TRUE_DIVIDE */
1092 case BINARY_TRUE_DIVIDE:
1093 w = POP();
1094 v = TOP();
1095 x = PyNumber_TrueDivide(v, w);
1096 Py_DECREF(v);
1097 Py_DECREF(w);
1098 SET_TOP(x);
1099 if (x != NULL) continue;
1100 break;
1102 case BINARY_FLOOR_DIVIDE:
1103 w = POP();
1104 v = TOP();
1105 x = PyNumber_FloorDivide(v, w);
1106 Py_DECREF(v);
1107 Py_DECREF(w);
1108 SET_TOP(x);
1109 if (x != NULL) continue;
1110 break;
1112 case BINARY_MODULO:
1113 w = POP();
1114 v = TOP();
1115 x = PyNumber_Remainder(v, w);
1116 Py_DECREF(v);
1117 Py_DECREF(w);
1118 SET_TOP(x);
1119 if (x != NULL) continue;
1120 break;
1122 case BINARY_ADD:
1123 w = POP();
1124 v = TOP();
1125 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1126 /* INLINE: int + int */
1127 register long a, b, i;
1128 a = PyInt_AS_LONG(v);
1129 b = PyInt_AS_LONG(w);
1130 i = a + b;
1131 if ((i^a) < 0 && (i^b) < 0)
1132 goto slow_add;
1133 x = PyInt_FromLong(i);
1135 else if (PyString_CheckExact(v) &&
1136 PyString_CheckExact(w)) {
1137 x = string_concatenate(v, w, f, next_instr);
1138 /* string_concatenate consumed the ref to v */
1139 goto skip_decref_vx;
1141 else {
1142 slow_add:
1143 x = PyNumber_Add(v, w);
1145 Py_DECREF(v);
1146 skip_decref_vx:
1147 Py_DECREF(w);
1148 SET_TOP(x);
1149 if (x != NULL) continue;
1150 break;
1152 case BINARY_SUBTRACT:
1153 w = POP();
1154 v = TOP();
1155 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1156 /* INLINE: int - int */
1157 register long a, b, i;
1158 a = PyInt_AS_LONG(v);
1159 b = PyInt_AS_LONG(w);
1160 i = a - b;
1161 if ((i^a) < 0 && (i^~b) < 0)
1162 goto slow_sub;
1163 x = PyInt_FromLong(i);
1165 else {
1166 slow_sub:
1167 x = PyNumber_Subtract(v, w);
1169 Py_DECREF(v);
1170 Py_DECREF(w);
1171 SET_TOP(x);
1172 if (x != NULL) continue;
1173 break;
1175 case BINARY_SUBSCR:
1176 w = POP();
1177 v = TOP();
1178 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1179 /* INLINE: list[int] */
1180 Py_ssize_t i = PyInt_AsSsize_t(w);
1181 if (i < 0)
1182 i += PyList_GET_SIZE(v);
1183 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1184 x = PyList_GET_ITEM(v, i);
1185 Py_INCREF(x);
1187 else
1188 goto slow_get;
1190 else
1191 slow_get:
1192 x = PyObject_GetItem(v, w);
1193 Py_DECREF(v);
1194 Py_DECREF(w);
1195 SET_TOP(x);
1196 if (x != NULL) continue;
1197 break;
1199 case BINARY_LSHIFT:
1200 w = POP();
1201 v = TOP();
1202 x = PyNumber_Lshift(v, w);
1203 Py_DECREF(v);
1204 Py_DECREF(w);
1205 SET_TOP(x);
1206 if (x != NULL) continue;
1207 break;
1209 case BINARY_RSHIFT:
1210 w = POP();
1211 v = TOP();
1212 x = PyNumber_Rshift(v, w);
1213 Py_DECREF(v);
1214 Py_DECREF(w);
1215 SET_TOP(x);
1216 if (x != NULL) continue;
1217 break;
1219 case BINARY_AND:
1220 w = POP();
1221 v = TOP();
1222 x = PyNumber_And(v, w);
1223 Py_DECREF(v);
1224 Py_DECREF(w);
1225 SET_TOP(x);
1226 if (x != NULL) continue;
1227 break;
1229 case BINARY_XOR:
1230 w = POP();
1231 v = TOP();
1232 x = PyNumber_Xor(v, w);
1233 Py_DECREF(v);
1234 Py_DECREF(w);
1235 SET_TOP(x);
1236 if (x != NULL) continue;
1237 break;
1239 case BINARY_OR:
1240 w = POP();
1241 v = TOP();
1242 x = PyNumber_Or(v, w);
1243 Py_DECREF(v);
1244 Py_DECREF(w);
1245 SET_TOP(x);
1246 if (x != NULL) continue;
1247 break;
1249 case LIST_APPEND:
1250 w = POP();
1251 v = POP();
1252 err = PyList_Append(v, w);
1253 Py_DECREF(v);
1254 Py_DECREF(w);
1255 if (err == 0) {
1256 PREDICT(JUMP_ABSOLUTE);
1257 continue;
1259 break;
1261 case INPLACE_POWER:
1262 w = POP();
1263 v = TOP();
1264 x = PyNumber_InPlacePower(v, w, Py_None);
1265 Py_DECREF(v);
1266 Py_DECREF(w);
1267 SET_TOP(x);
1268 if (x != NULL) continue;
1269 break;
1271 case INPLACE_MULTIPLY:
1272 w = POP();
1273 v = TOP();
1274 x = PyNumber_InPlaceMultiply(v, w);
1275 Py_DECREF(v);
1276 Py_DECREF(w);
1277 SET_TOP(x);
1278 if (x != NULL) continue;
1279 break;
1281 case INPLACE_DIVIDE:
1282 if (!_Py_QnewFlag) {
1283 w = POP();
1284 v = TOP();
1285 x = PyNumber_InPlaceDivide(v, w);
1286 Py_DECREF(v);
1287 Py_DECREF(w);
1288 SET_TOP(x);
1289 if (x != NULL) continue;
1290 break;
1292 /* -Qnew is in effect: fall through to
1293 INPLACE_TRUE_DIVIDE */
1294 case INPLACE_TRUE_DIVIDE:
1295 w = POP();
1296 v = TOP();
1297 x = PyNumber_InPlaceTrueDivide(v, w);
1298 Py_DECREF(v);
1299 Py_DECREF(w);
1300 SET_TOP(x);
1301 if (x != NULL) continue;
1302 break;
1304 case INPLACE_FLOOR_DIVIDE:
1305 w = POP();
1306 v = TOP();
1307 x = PyNumber_InPlaceFloorDivide(v, w);
1308 Py_DECREF(v);
1309 Py_DECREF(w);
1310 SET_TOP(x);
1311 if (x != NULL) continue;
1312 break;
1314 case INPLACE_MODULO:
1315 w = POP();
1316 v = TOP();
1317 x = PyNumber_InPlaceRemainder(v, w);
1318 Py_DECREF(v);
1319 Py_DECREF(w);
1320 SET_TOP(x);
1321 if (x != NULL) continue;
1322 break;
1324 case INPLACE_ADD:
1325 w = POP();
1326 v = TOP();
1327 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1328 /* INLINE: int + int */
1329 register long a, b, i;
1330 a = PyInt_AS_LONG(v);
1331 b = PyInt_AS_LONG(w);
1332 i = a + b;
1333 if ((i^a) < 0 && (i^b) < 0)
1334 goto slow_iadd;
1335 x = PyInt_FromLong(i);
1337 else if (PyString_CheckExact(v) &&
1338 PyString_CheckExact(w)) {
1339 x = string_concatenate(v, w, f, next_instr);
1340 /* string_concatenate consumed the ref to v */
1341 goto skip_decref_v;
1343 else {
1344 slow_iadd:
1345 x = PyNumber_InPlaceAdd(v, w);
1347 Py_DECREF(v);
1348 skip_decref_v:
1349 Py_DECREF(w);
1350 SET_TOP(x);
1351 if (x != NULL) continue;
1352 break;
1354 case INPLACE_SUBTRACT:
1355 w = POP();
1356 v = TOP();
1357 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1358 /* INLINE: int - int */
1359 register long a, b, i;
1360 a = PyInt_AS_LONG(v);
1361 b = PyInt_AS_LONG(w);
1362 i = a - b;
1363 if ((i^a) < 0 && (i^~b) < 0)
1364 goto slow_isub;
1365 x = PyInt_FromLong(i);
1367 else {
1368 slow_isub:
1369 x = PyNumber_InPlaceSubtract(v, w);
1371 Py_DECREF(v);
1372 Py_DECREF(w);
1373 SET_TOP(x);
1374 if (x != NULL) continue;
1375 break;
1377 case INPLACE_LSHIFT:
1378 w = POP();
1379 v = TOP();
1380 x = PyNumber_InPlaceLshift(v, w);
1381 Py_DECREF(v);
1382 Py_DECREF(w);
1383 SET_TOP(x);
1384 if (x != NULL) continue;
1385 break;
1387 case INPLACE_RSHIFT:
1388 w = POP();
1389 v = TOP();
1390 x = PyNumber_InPlaceRshift(v, w);
1391 Py_DECREF(v);
1392 Py_DECREF(w);
1393 SET_TOP(x);
1394 if (x != NULL) continue;
1395 break;
1397 case INPLACE_AND:
1398 w = POP();
1399 v = TOP();
1400 x = PyNumber_InPlaceAnd(v, w);
1401 Py_DECREF(v);
1402 Py_DECREF(w);
1403 SET_TOP(x);
1404 if (x != NULL) continue;
1405 break;
1407 case INPLACE_XOR:
1408 w = POP();
1409 v = TOP();
1410 x = PyNumber_InPlaceXor(v, w);
1411 Py_DECREF(v);
1412 Py_DECREF(w);
1413 SET_TOP(x);
1414 if (x != NULL) continue;
1415 break;
1417 case INPLACE_OR:
1418 w = POP();
1419 v = TOP();
1420 x = PyNumber_InPlaceOr(v, w);
1421 Py_DECREF(v);
1422 Py_DECREF(w);
1423 SET_TOP(x);
1424 if (x != NULL) continue;
1425 break;
1427 case SLICE+0:
1428 case SLICE+1:
1429 case SLICE+2:
1430 case SLICE+3:
1431 if ((opcode-SLICE) & 2)
1432 w = POP();
1433 else
1434 w = NULL;
1435 if ((opcode-SLICE) & 1)
1436 v = POP();
1437 else
1438 v = NULL;
1439 u = TOP();
1440 x = apply_slice(u, v, w);
1441 Py_DECREF(u);
1442 Py_XDECREF(v);
1443 Py_XDECREF(w);
1444 SET_TOP(x);
1445 if (x != NULL) continue;
1446 break;
1448 case STORE_SLICE+0:
1449 case STORE_SLICE+1:
1450 case STORE_SLICE+2:
1451 case STORE_SLICE+3:
1452 if ((opcode-STORE_SLICE) & 2)
1453 w = POP();
1454 else
1455 w = NULL;
1456 if ((opcode-STORE_SLICE) & 1)
1457 v = POP();
1458 else
1459 v = NULL;
1460 u = POP();
1461 t = POP();
1462 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1463 Py_DECREF(t);
1464 Py_DECREF(u);
1465 Py_XDECREF(v);
1466 Py_XDECREF(w);
1467 if (err == 0) continue;
1468 break;
1470 case DELETE_SLICE+0:
1471 case DELETE_SLICE+1:
1472 case DELETE_SLICE+2:
1473 case DELETE_SLICE+3:
1474 if ((opcode-DELETE_SLICE) & 2)
1475 w = POP();
1476 else
1477 w = NULL;
1478 if ((opcode-DELETE_SLICE) & 1)
1479 v = POP();
1480 else
1481 v = NULL;
1482 u = POP();
1483 err = assign_slice(u, v, w, (PyObject *)NULL);
1484 /* del u[v:w] */
1485 Py_DECREF(u);
1486 Py_XDECREF(v);
1487 Py_XDECREF(w);
1488 if (err == 0) continue;
1489 break;
1491 case STORE_SUBSCR:
1492 w = TOP();
1493 v = SECOND();
1494 u = THIRD();
1495 STACKADJ(-3);
1496 /* v[w] = u */
1497 err = PyObject_SetItem(v, w, u);
1498 Py_DECREF(u);
1499 Py_DECREF(v);
1500 Py_DECREF(w);
1501 if (err == 0) continue;
1502 break;
1504 case DELETE_SUBSCR:
1505 w = TOP();
1506 v = SECOND();
1507 STACKADJ(-2);
1508 /* del v[w] */
1509 err = PyObject_DelItem(v, w);
1510 Py_DECREF(v);
1511 Py_DECREF(w);
1512 if (err == 0) continue;
1513 break;
1515 case PRINT_EXPR:
1516 v = POP();
1517 w = PySys_GetObject("displayhook");
1518 if (w == NULL) {
1519 PyErr_SetString(PyExc_RuntimeError,
1520 "lost sys.displayhook");
1521 err = -1;
1522 x = NULL;
1524 if (err == 0) {
1525 x = PyTuple_Pack(1, v);
1526 if (x == NULL)
1527 err = -1;
1529 if (err == 0) {
1530 w = PyEval_CallObject(w, x);
1531 Py_XDECREF(w);
1532 if (w == NULL)
1533 err = -1;
1535 Py_DECREF(v);
1536 Py_XDECREF(x);
1537 break;
1539 case PRINT_ITEM_TO:
1540 w = stream = POP();
1541 /* fall through to PRINT_ITEM */
1543 case PRINT_ITEM:
1544 v = POP();
1545 if (stream == NULL || stream == Py_None) {
1546 w = PySys_GetObject("stdout");
1547 if (w == NULL) {
1548 PyErr_SetString(PyExc_RuntimeError,
1549 "lost sys.stdout");
1550 err = -1;
1553 /* PyFile_SoftSpace() can exececute arbitrary code
1554 if sys.stdout is an instance with a __getattr__.
1555 If __getattr__ raises an exception, w will
1556 be freed, so we need to prevent that temporarily. */
1557 Py_XINCREF(w);
1558 if (w != NULL && PyFile_SoftSpace(w, 0))
1559 err = PyFile_WriteString(" ", w);
1560 if (err == 0)
1561 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1562 if (err == 0) {
1563 /* XXX move into writeobject() ? */
1564 if (PyString_Check(v)) {
1565 char *s = PyString_AS_STRING(v);
1566 Py_ssize_t len = PyString_GET_SIZE(v);
1567 if (len == 0 ||
1568 !isspace(Py_CHARMASK(s[len-1])) ||
1569 s[len-1] == ' ')
1570 PyFile_SoftSpace(w, 1);
1572 #ifdef Py_USING_UNICODE
1573 else if (PyUnicode_Check(v)) {
1574 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1575 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1576 if (len == 0 ||
1577 !Py_UNICODE_ISSPACE(s[len-1]) ||
1578 s[len-1] == ' ')
1579 PyFile_SoftSpace(w, 1);
1581 #endif
1582 else
1583 PyFile_SoftSpace(w, 1);
1585 Py_XDECREF(w);
1586 Py_DECREF(v);
1587 Py_XDECREF(stream);
1588 stream = NULL;
1589 if (err == 0)
1590 continue;
1591 break;
1593 case PRINT_NEWLINE_TO:
1594 w = stream = POP();
1595 /* fall through to PRINT_NEWLINE */
1597 case PRINT_NEWLINE:
1598 if (stream == NULL || stream == Py_None) {
1599 w = PySys_GetObject("stdout");
1600 if (w == NULL)
1601 PyErr_SetString(PyExc_RuntimeError,
1602 "lost sys.stdout");
1604 if (w != NULL) {
1605 err = PyFile_WriteString("\n", w);
1606 if (err == 0)
1607 PyFile_SoftSpace(w, 0);
1609 Py_XDECREF(stream);
1610 stream = NULL;
1611 break;
1614 #ifdef CASE_TOO_BIG
1615 default: switch (opcode) {
1616 #endif
1617 case RAISE_VARARGS:
1618 u = v = w = NULL;
1619 switch (oparg) {
1620 case 3:
1621 u = POP(); /* traceback */
1622 /* Fallthrough */
1623 case 2:
1624 v = POP(); /* value */
1625 /* Fallthrough */
1626 case 1:
1627 w = POP(); /* exc */
1628 case 0: /* Fallthrough */
1629 why = do_raise(w, v, u);
1630 break;
1631 default:
1632 PyErr_SetString(PyExc_SystemError,
1633 "bad RAISE_VARARGS oparg");
1634 why = WHY_EXCEPTION;
1635 break;
1637 break;
1639 case LOAD_LOCALS:
1640 if ((x = f->f_locals) != NULL) {
1641 Py_INCREF(x);
1642 PUSH(x);
1643 continue;
1645 PyErr_SetString(PyExc_SystemError, "no locals");
1646 break;
1648 case RETURN_VALUE:
1649 retval = POP();
1650 why = WHY_RETURN;
1651 goto fast_block_end;
1653 case YIELD_VALUE:
1654 retval = POP();
1655 f->f_stacktop = stack_pointer;
1656 why = WHY_YIELD;
1657 goto fast_yield;
1659 case EXEC_STMT:
1660 w = TOP();
1661 v = SECOND();
1662 u = THIRD();
1663 STACKADJ(-3);
1664 READ_TIMESTAMP(intr0);
1665 err = exec_statement(f, u, v, w);
1666 READ_TIMESTAMP(intr1);
1667 Py_DECREF(u);
1668 Py_DECREF(v);
1669 Py_DECREF(w);
1670 break;
1672 case POP_BLOCK:
1674 PyTryBlock *b = PyFrame_BlockPop(f);
1675 while (STACK_LEVEL() > b->b_level) {
1676 v = POP();
1677 Py_DECREF(v);
1680 continue;
1682 case END_FINALLY:
1683 v = POP();
1684 if (PyInt_Check(v)) {
1685 why = (enum why_code) PyInt_AS_LONG(v);
1686 assert(why != WHY_YIELD);
1687 if (why == WHY_RETURN ||
1688 why == WHY_CONTINUE)
1689 retval = POP();
1691 else if (PyExceptionClass_Check(v) || PyString_Check(v)) {
1692 w = POP();
1693 u = POP();
1694 PyErr_Restore(v, w, u);
1695 why = WHY_RERAISE;
1696 break;
1698 else if (v != Py_None) {
1699 PyErr_SetString(PyExc_SystemError,
1700 "'finally' pops bad exception");
1701 why = WHY_EXCEPTION;
1703 Py_DECREF(v);
1704 break;
1706 case BUILD_CLASS:
1707 u = TOP();
1708 v = SECOND();
1709 w = THIRD();
1710 STACKADJ(-2);
1711 x = build_class(u, v, w);
1712 SET_TOP(x);
1713 Py_DECREF(u);
1714 Py_DECREF(v);
1715 Py_DECREF(w);
1716 break;
1718 case STORE_NAME:
1719 w = GETITEM(names, oparg);
1720 v = POP();
1721 if ((x = f->f_locals) != NULL) {
1722 if (PyDict_CheckExact(x))
1723 err = PyDict_SetItem(x, w, v);
1724 else
1725 err = PyObject_SetItem(x, w, v);
1726 Py_DECREF(v);
1727 if (err == 0) continue;
1728 break;
1730 PyErr_Format(PyExc_SystemError,
1731 "no locals found when storing %s",
1732 PyObject_REPR(w));
1733 break;
1735 case DELETE_NAME:
1736 w = GETITEM(names, oparg);
1737 if ((x = f->f_locals) != NULL) {
1738 if ((err = PyObject_DelItem(x, w)) != 0)
1739 format_exc_check_arg(PyExc_NameError,
1740 NAME_ERROR_MSG ,w);
1741 break;
1743 PyErr_Format(PyExc_SystemError,
1744 "no locals when deleting %s",
1745 PyObject_REPR(w));
1746 break;
1748 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1749 case UNPACK_SEQUENCE:
1750 v = POP();
1751 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1752 PyObject **items = ((PyTupleObject *)v)->ob_item;
1753 while (oparg--) {
1754 w = items[oparg];
1755 Py_INCREF(w);
1756 PUSH(w);
1758 Py_DECREF(v);
1759 continue;
1760 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1761 PyObject **items = ((PyListObject *)v)->ob_item;
1762 while (oparg--) {
1763 w = items[oparg];
1764 Py_INCREF(w);
1765 PUSH(w);
1767 } else if (unpack_iterable(v, oparg,
1768 stack_pointer + oparg))
1769 stack_pointer += oparg;
1770 else {
1771 if (PyErr_ExceptionMatches(PyExc_TypeError))
1772 PyErr_SetString(PyExc_TypeError,
1773 "unpack non-sequence");
1774 why = WHY_EXCEPTION;
1776 Py_DECREF(v);
1777 break;
1779 case STORE_ATTR:
1780 w = GETITEM(names, oparg);
1781 v = TOP();
1782 u = SECOND();
1783 STACKADJ(-2);
1784 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1785 Py_DECREF(v);
1786 Py_DECREF(u);
1787 if (err == 0) continue;
1788 break;
1790 case DELETE_ATTR:
1791 w = GETITEM(names, oparg);
1792 v = POP();
1793 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1794 /* del v.w */
1795 Py_DECREF(v);
1796 break;
1798 case STORE_GLOBAL:
1799 w = GETITEM(names, oparg);
1800 v = POP();
1801 err = PyDict_SetItem(f->f_globals, w, v);
1802 Py_DECREF(v);
1803 if (err == 0) continue;
1804 break;
1806 case DELETE_GLOBAL:
1807 w = GETITEM(names, oparg);
1808 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1809 format_exc_check_arg(
1810 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1811 break;
1813 case LOAD_NAME:
1814 w = GETITEM(names, oparg);
1815 if ((v = f->f_locals) == NULL) {
1816 PyErr_Format(PyExc_SystemError,
1817 "no locals when loading %s",
1818 PyObject_REPR(w));
1819 break;
1821 if (PyDict_CheckExact(v)) {
1822 x = PyDict_GetItem(v, w);
1823 Py_XINCREF(x);
1825 else {
1826 x = PyObject_GetItem(v, w);
1827 if (x == NULL && PyErr_Occurred()) {
1828 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1829 break;
1830 PyErr_Clear();
1833 if (x == NULL) {
1834 x = PyDict_GetItem(f->f_globals, w);
1835 if (x == NULL) {
1836 x = PyDict_GetItem(f->f_builtins, w);
1837 if (x == NULL) {
1838 format_exc_check_arg(
1839 PyExc_NameError,
1840 NAME_ERROR_MSG ,w);
1841 break;
1844 Py_INCREF(x);
1846 PUSH(x);
1847 continue;
1849 case LOAD_GLOBAL:
1850 w = GETITEM(names, oparg);
1851 if (PyString_CheckExact(w)) {
1852 /* Inline the PyDict_GetItem() calls.
1853 WARNING: this is an extreme speed hack.
1854 Do not try this at home. */
1855 long hash = ((PyStringObject *)w)->ob_shash;
1856 if (hash != -1) {
1857 PyDictObject *d;
1858 d = (PyDictObject *)(f->f_globals);
1859 x = d->ma_lookup(d, w, hash)->me_value;
1860 if (x != NULL) {
1861 Py_INCREF(x);
1862 PUSH(x);
1863 continue;
1865 d = (PyDictObject *)(f->f_builtins);
1866 x = d->ma_lookup(d, w, hash)->me_value;
1867 if (x != NULL) {
1868 Py_INCREF(x);
1869 PUSH(x);
1870 continue;
1872 goto load_global_error;
1875 /* This is the un-inlined version of the code above */
1876 x = PyDict_GetItem(f->f_globals, w);
1877 if (x == NULL) {
1878 x = PyDict_GetItem(f->f_builtins, w);
1879 if (x == NULL) {
1880 load_global_error:
1881 format_exc_check_arg(
1882 PyExc_NameError,
1883 GLOBAL_NAME_ERROR_MSG, w);
1884 break;
1887 Py_INCREF(x);
1888 PUSH(x);
1889 continue;
1891 case DELETE_FAST:
1892 x = GETLOCAL(oparg);
1893 if (x != NULL) {
1894 SETLOCAL(oparg, NULL);
1895 continue;
1897 format_exc_check_arg(
1898 PyExc_UnboundLocalError,
1899 UNBOUNDLOCAL_ERROR_MSG,
1900 PyTuple_GetItem(co->co_varnames, oparg)
1902 break;
1904 case LOAD_CLOSURE:
1905 x = freevars[oparg];
1906 Py_INCREF(x);
1907 PUSH(x);
1908 if (x != NULL) continue;
1909 break;
1911 case LOAD_DEREF:
1912 x = freevars[oparg];
1913 w = PyCell_Get(x);
1914 if (w != NULL) {
1915 PUSH(w);
1916 continue;
1918 err = -1;
1919 /* Don't stomp existing exception */
1920 if (PyErr_Occurred())
1921 break;
1922 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1923 v = PyTuple_GET_ITEM(co->co_cellvars,
1924 oparg);
1925 format_exc_check_arg(
1926 PyExc_UnboundLocalError,
1927 UNBOUNDLOCAL_ERROR_MSG,
1929 } else {
1930 v = PyTuple_GET_ITEM(
1931 co->co_freevars,
1932 oparg - PyTuple_GET_SIZE(co->co_cellvars));
1933 format_exc_check_arg(
1934 PyExc_NameError,
1935 UNBOUNDFREE_ERROR_MSG,
1938 break;
1940 case STORE_DEREF:
1941 w = POP();
1942 x = freevars[oparg];
1943 PyCell_Set(x, w);
1944 Py_DECREF(w);
1945 continue;
1947 case BUILD_TUPLE:
1948 x = PyTuple_New(oparg);
1949 if (x != NULL) {
1950 for (; --oparg >= 0;) {
1951 w = POP();
1952 PyTuple_SET_ITEM(x, oparg, w);
1954 PUSH(x);
1955 continue;
1957 break;
1959 case BUILD_LIST:
1960 x = PyList_New(oparg);
1961 if (x != NULL) {
1962 for (; --oparg >= 0;) {
1963 w = POP();
1964 PyList_SET_ITEM(x, oparg, w);
1966 PUSH(x);
1967 continue;
1969 break;
1971 case BUILD_MAP:
1972 x = PyDict_New();
1973 PUSH(x);
1974 if (x != NULL) continue;
1975 break;
1977 case LOAD_ATTR:
1978 w = GETITEM(names, oparg);
1979 v = TOP();
1980 x = PyObject_GetAttr(v, w);
1981 Py_DECREF(v);
1982 SET_TOP(x);
1983 if (x != NULL) continue;
1984 break;
1986 case COMPARE_OP:
1987 w = POP();
1988 v = TOP();
1989 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
1990 /* INLINE: cmp(int, int) */
1991 register long a, b;
1992 register int res;
1993 a = PyInt_AS_LONG(v);
1994 b = PyInt_AS_LONG(w);
1995 switch (oparg) {
1996 case PyCmp_LT: res = a < b; break;
1997 case PyCmp_LE: res = a <= b; break;
1998 case PyCmp_EQ: res = a == b; break;
1999 case PyCmp_NE: res = a != b; break;
2000 case PyCmp_GT: res = a > b; break;
2001 case PyCmp_GE: res = a >= b; break;
2002 case PyCmp_IS: res = v == w; break;
2003 case PyCmp_IS_NOT: res = v != w; break;
2004 default: goto slow_compare;
2006 x = res ? Py_True : Py_False;
2007 Py_INCREF(x);
2009 else {
2010 slow_compare:
2011 x = cmp_outcome(oparg, v, w);
2013 Py_DECREF(v);
2014 Py_DECREF(w);
2015 SET_TOP(x);
2016 if (x == NULL) break;
2017 PREDICT(JUMP_IF_FALSE);
2018 PREDICT(JUMP_IF_TRUE);
2019 continue;
2021 case IMPORT_NAME:
2022 w = GETITEM(names, oparg);
2023 x = PyDict_GetItemString(f->f_builtins, "__import__");
2024 if (x == NULL) {
2025 PyErr_SetString(PyExc_ImportError,
2026 "__import__ not found");
2027 break;
2029 v = POP();
2030 u = TOP();
2031 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2032 w = PyTuple_Pack(5,
2034 f->f_globals,
2035 f->f_locals == NULL ?
2036 Py_None : f->f_locals,
2039 else
2040 w = PyTuple_Pack(4,
2042 f->f_globals,
2043 f->f_locals == NULL ?
2044 Py_None : f->f_locals,
2046 Py_DECREF(v);
2047 Py_DECREF(u);
2048 if (w == NULL) {
2049 u = POP();
2050 x = NULL;
2051 break;
2053 READ_TIMESTAMP(intr0);
2054 x = PyEval_CallObject(x, w);
2055 READ_TIMESTAMP(intr1);
2056 Py_DECREF(w);
2057 SET_TOP(x);
2058 if (x != NULL) continue;
2059 break;
2061 case IMPORT_STAR:
2062 v = POP();
2063 PyFrame_FastToLocals(f);
2064 if ((x = f->f_locals) == NULL) {
2065 PyErr_SetString(PyExc_SystemError,
2066 "no locals found during 'import *'");
2067 break;
2069 READ_TIMESTAMP(intr0);
2070 err = import_all_from(x, v);
2071 READ_TIMESTAMP(intr1);
2072 PyFrame_LocalsToFast(f, 0);
2073 Py_DECREF(v);
2074 if (err == 0) continue;
2075 break;
2077 case IMPORT_FROM:
2078 w = GETITEM(names, oparg);
2079 v = TOP();
2080 READ_TIMESTAMP(intr0);
2081 x = import_from(v, w);
2082 READ_TIMESTAMP(intr1);
2083 PUSH(x);
2084 if (x != NULL) continue;
2085 break;
2087 case JUMP_FORWARD:
2088 JUMPBY(oparg);
2089 goto fast_next_opcode;
2091 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
2092 case JUMP_IF_FALSE:
2093 w = TOP();
2094 if (w == Py_True) {
2095 PREDICT(POP_TOP);
2096 goto fast_next_opcode;
2098 if (w == Py_False) {
2099 JUMPBY(oparg);
2100 goto fast_next_opcode;
2102 err = PyObject_IsTrue(w);
2103 if (err > 0)
2104 err = 0;
2105 else if (err == 0)
2106 JUMPBY(oparg);
2107 else
2108 break;
2109 continue;
2111 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
2112 case JUMP_IF_TRUE:
2113 w = TOP();
2114 if (w == Py_False) {
2115 PREDICT(POP_TOP);
2116 goto fast_next_opcode;
2118 if (w == Py_True) {
2119 JUMPBY(oparg);
2120 goto fast_next_opcode;
2122 err = PyObject_IsTrue(w);
2123 if (err > 0) {
2124 err = 0;
2125 JUMPBY(oparg);
2127 else if (err == 0)
2129 else
2130 break;
2131 continue;
2133 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2134 case JUMP_ABSOLUTE:
2135 JUMPTO(oparg);
2136 continue;
2138 case GET_ITER:
2139 /* before: [obj]; after [getiter(obj)] */
2140 v = TOP();
2141 x = PyObject_GetIter(v);
2142 Py_DECREF(v);
2143 if (x != NULL) {
2144 SET_TOP(x);
2145 PREDICT(FOR_ITER);
2146 continue;
2148 STACKADJ(-1);
2149 break;
2151 PREDICTED_WITH_ARG(FOR_ITER);
2152 case FOR_ITER:
2153 /* before: [iter]; after: [iter, iter()] *or* [] */
2154 v = TOP();
2155 x = (*v->ob_type->tp_iternext)(v);
2156 if (x != NULL) {
2157 PUSH(x);
2158 PREDICT(STORE_FAST);
2159 PREDICT(UNPACK_SEQUENCE);
2160 continue;
2162 if (PyErr_Occurred()) {
2163 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2164 break;
2165 PyErr_Clear();
2167 /* iterator ended normally */
2168 x = v = POP();
2169 Py_DECREF(v);
2170 JUMPBY(oparg);
2171 continue;
2173 case BREAK_LOOP:
2174 why = WHY_BREAK;
2175 goto fast_block_end;
2177 case CONTINUE_LOOP:
2178 retval = PyInt_FromLong(oparg);
2179 if (!retval) {
2180 x = NULL;
2181 break;
2183 why = WHY_CONTINUE;
2184 goto fast_block_end;
2186 case SETUP_LOOP:
2187 case SETUP_EXCEPT:
2188 case SETUP_FINALLY:
2189 /* NOTE: If you add any new block-setup opcodes that are not try/except/finally
2190 handlers, you may need to update the PyGen_NeedsFinalizing() function. */
2192 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2193 STACK_LEVEL());
2194 continue;
2196 case WITH_CLEANUP:
2198 /* TOP is the context.__exit__ bound method.
2199 Below that are 1-3 values indicating how/why
2200 we entered the finally clause:
2201 - SECOND = None
2202 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
2203 - SECOND = WHY_*; no retval below it
2204 - (SECOND, THIRD, FOURTH) = exc_info()
2205 In the last case, we must call
2206 TOP(SECOND, THIRD, FOURTH)
2207 otherwise we must call
2208 TOP(None, None, None)
2210 In addition, if the stack represents an exception,
2211 *and* the function call returns a 'true' value, we
2212 "zap" this information, to prevent END_FINALLY from
2213 re-raising the exception. (But non-local gotos
2214 should still be resumed.)
2217 x = TOP();
2218 u = SECOND();
2219 if (PyInt_Check(u) || u == Py_None) {
2220 u = v = w = Py_None;
2222 else {
2223 v = THIRD();
2224 w = FOURTH();
2226 /* XXX Not the fastest way to call it... */
2227 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2228 if (x == NULL)
2229 break; /* Go to error exit */
2230 if (u != Py_None && PyObject_IsTrue(x)) {
2231 /* There was an exception and a true return */
2232 Py_DECREF(x);
2233 x = TOP(); /* Again */
2234 STACKADJ(-3);
2235 Py_INCREF(Py_None);
2236 SET_TOP(Py_None);
2237 Py_DECREF(x);
2238 Py_DECREF(u);
2239 Py_DECREF(v);
2240 Py_DECREF(w);
2241 } else {
2242 /* Let END_FINALLY do its thing */
2243 Py_DECREF(x);
2244 x = POP();
2245 Py_DECREF(x);
2247 break;
2250 case CALL_FUNCTION:
2252 PyObject **sp;
2253 PCALL(PCALL_ALL);
2254 sp = stack_pointer;
2255 #ifdef WITH_TSC
2256 x = call_function(&sp, oparg, &intr0, &intr1);
2257 #else
2258 x = call_function(&sp, oparg);
2259 #endif
2260 stack_pointer = sp;
2261 PUSH(x);
2262 if (x != NULL)
2263 continue;
2264 break;
2267 case CALL_FUNCTION_VAR:
2268 case CALL_FUNCTION_KW:
2269 case CALL_FUNCTION_VAR_KW:
2271 int na = oparg & 0xff;
2272 int nk = (oparg>>8) & 0xff;
2273 int flags = (opcode - CALL_FUNCTION) & 3;
2274 int n = na + 2 * nk;
2275 PyObject **pfunc, *func, **sp;
2276 PCALL(PCALL_ALL);
2277 if (flags & CALL_FLAG_VAR)
2278 n++;
2279 if (flags & CALL_FLAG_KW)
2280 n++;
2281 pfunc = stack_pointer - n - 1;
2282 func = *pfunc;
2284 if (PyMethod_Check(func)
2285 && PyMethod_GET_SELF(func) != NULL) {
2286 PyObject *self = PyMethod_GET_SELF(func);
2287 Py_INCREF(self);
2288 func = PyMethod_GET_FUNCTION(func);
2289 Py_INCREF(func);
2290 Py_DECREF(*pfunc);
2291 *pfunc = self;
2292 na++;
2293 n++;
2294 } else
2295 Py_INCREF(func);
2296 sp = stack_pointer;
2297 READ_TIMESTAMP(intr0);
2298 x = ext_do_call(func, &sp, flags, na, nk);
2299 READ_TIMESTAMP(intr1);
2300 stack_pointer = sp;
2301 Py_DECREF(func);
2303 while (stack_pointer > pfunc) {
2304 w = POP();
2305 Py_DECREF(w);
2307 PUSH(x);
2308 if (x != NULL)
2309 continue;
2310 break;
2313 case MAKE_FUNCTION:
2314 v = POP(); /* code object */
2315 x = PyFunction_New(v, f->f_globals);
2316 Py_DECREF(v);
2317 /* XXX Maybe this should be a separate opcode? */
2318 if (x != NULL && oparg > 0) {
2319 v = PyTuple_New(oparg);
2320 if (v == NULL) {
2321 Py_DECREF(x);
2322 x = NULL;
2323 break;
2325 while (--oparg >= 0) {
2326 w = POP();
2327 PyTuple_SET_ITEM(v, oparg, w);
2329 err = PyFunction_SetDefaults(x, v);
2330 Py_DECREF(v);
2332 PUSH(x);
2333 break;
2335 case MAKE_CLOSURE:
2337 v = POP(); /* code object */
2338 x = PyFunction_New(v, f->f_globals);
2339 Py_DECREF(v);
2340 if (x != NULL) {
2341 v = POP();
2342 err = PyFunction_SetClosure(x, v);
2343 Py_DECREF(v);
2345 if (x != NULL && oparg > 0) {
2346 v = PyTuple_New(oparg);
2347 if (v == NULL) {
2348 Py_DECREF(x);
2349 x = NULL;
2350 break;
2352 while (--oparg >= 0) {
2353 w = POP();
2354 PyTuple_SET_ITEM(v, oparg, w);
2356 err = PyFunction_SetDefaults(x, v);
2357 Py_DECREF(v);
2359 PUSH(x);
2360 break;
2363 case BUILD_SLICE:
2364 if (oparg == 3)
2365 w = POP();
2366 else
2367 w = NULL;
2368 v = POP();
2369 u = TOP();
2370 x = PySlice_New(u, v, w);
2371 Py_DECREF(u);
2372 Py_DECREF(v);
2373 Py_XDECREF(w);
2374 SET_TOP(x);
2375 if (x != NULL) continue;
2376 break;
2378 case EXTENDED_ARG:
2379 opcode = NEXTOP();
2380 oparg = oparg<<16 | NEXTARG();
2381 goto dispatch_opcode;
2383 default:
2384 fprintf(stderr,
2385 "XXX lineno: %d, opcode: %d\n",
2386 PyCode_Addr2Line(f->f_code, f->f_lasti),
2387 opcode);
2388 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2389 why = WHY_EXCEPTION;
2390 break;
2392 #ifdef CASE_TOO_BIG
2394 #endif
2396 } /* switch */
2398 on_error:
2400 READ_TIMESTAMP(inst1);
2402 /* Quickly continue if no error occurred */
2404 if (why == WHY_NOT) {
2405 if (err == 0 && x != NULL) {
2406 #ifdef CHECKEXC
2407 /* This check is expensive! */
2408 if (PyErr_Occurred())
2409 fprintf(stderr,
2410 "XXX undetected error\n");
2411 else {
2412 #endif
2413 READ_TIMESTAMP(loop1);
2414 continue; /* Normal, fast path */
2415 #ifdef CHECKEXC
2417 #endif
2419 why = WHY_EXCEPTION;
2420 x = Py_None;
2421 err = 0;
2424 /* Double-check exception status */
2426 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2427 if (!PyErr_Occurred()) {
2428 PyErr_SetString(PyExc_SystemError,
2429 "error return without exception set");
2430 why = WHY_EXCEPTION;
2433 #ifdef CHECKEXC
2434 else {
2435 /* This check is expensive! */
2436 if (PyErr_Occurred()) {
2437 char buf[1024];
2438 sprintf(buf, "Stack unwind with exception "
2439 "set and why=%d", why);
2440 Py_FatalError(buf);
2443 #endif
2445 /* Log traceback info if this is a real exception */
2447 if (why == WHY_EXCEPTION) {
2448 PyTraceBack_Here(f);
2450 if (tstate->c_tracefunc != NULL)
2451 call_exc_trace(tstate->c_tracefunc,
2452 tstate->c_traceobj, f);
2455 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2457 if (why == WHY_RERAISE)
2458 why = WHY_EXCEPTION;
2460 /* Unwind stacks if a (pseudo) exception occurred */
2462 fast_block_end:
2463 while (why != WHY_NOT && f->f_iblock > 0) {
2464 PyTryBlock *b = PyFrame_BlockPop(f);
2466 assert(why != WHY_YIELD);
2467 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2468 /* For a continue inside a try block,
2469 don't pop the block for the loop. */
2470 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2471 b->b_level);
2472 why = WHY_NOT;
2473 JUMPTO(PyInt_AS_LONG(retval));
2474 Py_DECREF(retval);
2475 break;
2478 while (STACK_LEVEL() > b->b_level) {
2479 v = POP();
2480 Py_XDECREF(v);
2482 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2483 why = WHY_NOT;
2484 JUMPTO(b->b_handler);
2485 break;
2487 if (b->b_type == SETUP_FINALLY ||
2488 (b->b_type == SETUP_EXCEPT &&
2489 why == WHY_EXCEPTION)) {
2490 if (why == WHY_EXCEPTION) {
2491 PyObject *exc, *val, *tb;
2492 PyErr_Fetch(&exc, &val, &tb);
2493 if (val == NULL) {
2494 val = Py_None;
2495 Py_INCREF(val);
2497 /* Make the raw exception data
2498 available to the handler,
2499 so a program can emulate the
2500 Python main loop. Don't do
2501 this for 'finally'. */
2502 if (b->b_type == SETUP_EXCEPT) {
2503 PyErr_NormalizeException(
2504 &exc, &val, &tb);
2505 set_exc_info(tstate,
2506 exc, val, tb);
2508 if (tb == NULL) {
2509 Py_INCREF(Py_None);
2510 PUSH(Py_None);
2511 } else
2512 PUSH(tb);
2513 PUSH(val);
2514 PUSH(exc);
2516 else {
2517 if (why & (WHY_RETURN | WHY_CONTINUE))
2518 PUSH(retval);
2519 v = PyInt_FromLong((long)why);
2520 PUSH(v);
2522 why = WHY_NOT;
2523 JUMPTO(b->b_handler);
2524 break;
2526 } /* unwind stack */
2528 /* End the loop if we still have an error (or return) */
2530 if (why != WHY_NOT)
2531 break;
2532 READ_TIMESTAMP(loop1);
2534 } /* main loop */
2536 assert(why != WHY_YIELD);
2537 /* Pop remaining stack entries. */
2538 while (!EMPTY()) {
2539 v = POP();
2540 Py_XDECREF(v);
2543 if (why != WHY_RETURN)
2544 retval = NULL;
2546 fast_yield:
2547 if (tstate->use_tracing) {
2548 if (tstate->c_tracefunc) {
2549 if (why == WHY_RETURN || why == WHY_YIELD) {
2550 if (call_trace(tstate->c_tracefunc,
2551 tstate->c_traceobj, f,
2552 PyTrace_RETURN, retval)) {
2553 Py_XDECREF(retval);
2554 retval = NULL;
2555 why = WHY_EXCEPTION;
2558 else if (why == WHY_EXCEPTION) {
2559 call_trace_protected(tstate->c_tracefunc,
2560 tstate->c_traceobj, f,
2561 PyTrace_RETURN, NULL);
2564 if (tstate->c_profilefunc) {
2565 if (why == WHY_EXCEPTION)
2566 call_trace_protected(tstate->c_profilefunc,
2567 tstate->c_profileobj, f,
2568 PyTrace_RETURN, NULL);
2569 else if (call_trace(tstate->c_profilefunc,
2570 tstate->c_profileobj, f,
2571 PyTrace_RETURN, retval)) {
2572 Py_XDECREF(retval);
2573 retval = NULL;
2574 why = WHY_EXCEPTION;
2579 if (tstate->frame->f_exc_type != NULL)
2580 reset_exc_info(tstate);
2581 else {
2582 assert(tstate->frame->f_exc_value == NULL);
2583 assert(tstate->frame->f_exc_traceback == NULL);
2586 /* pop frame */
2587 exit_eval_frame:
2588 Py_LeaveRecursiveCall();
2589 tstate->frame = f->f_back;
2591 return retval;
2594 /* This is gonna seem *real weird*, but if you put some other code between
2595 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2596 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2598 PyObject *
2599 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
2600 PyObject **args, int argcount, PyObject **kws, int kwcount,
2601 PyObject **defs, int defcount, PyObject *closure)
2603 register PyFrameObject *f;
2604 register PyObject *retval = NULL;
2605 register PyObject **fastlocals, **freevars;
2606 PyThreadState *tstate = PyThreadState_GET();
2607 PyObject *x, *u;
2609 if (globals == NULL) {
2610 PyErr_SetString(PyExc_SystemError,
2611 "PyEval_EvalCodeEx: NULL globals");
2612 return NULL;
2615 assert(globals != NULL);
2616 f = PyFrame_New(tstate, co, globals, locals);
2617 if (f == NULL)
2618 return NULL;
2620 fastlocals = f->f_localsplus;
2621 freevars = f->f_localsplus + co->co_nlocals;
2623 if (co->co_argcount > 0 ||
2624 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2625 int i;
2626 int n = argcount;
2627 PyObject *kwdict = NULL;
2628 if (co->co_flags & CO_VARKEYWORDS) {
2629 kwdict = PyDict_New();
2630 if (kwdict == NULL)
2631 goto fail;
2632 i = co->co_argcount;
2633 if (co->co_flags & CO_VARARGS)
2634 i++;
2635 SETLOCAL(i, kwdict);
2637 if (argcount > co->co_argcount) {
2638 if (!(co->co_flags & CO_VARARGS)) {
2639 PyErr_Format(PyExc_TypeError,
2640 "%.200s() takes %s %d "
2641 "%sargument%s (%d given)",
2642 PyString_AsString(co->co_name),
2643 defcount ? "at most" : "exactly",
2644 co->co_argcount,
2645 kwcount ? "non-keyword " : "",
2646 co->co_argcount == 1 ? "" : "s",
2647 argcount);
2648 goto fail;
2650 n = co->co_argcount;
2652 for (i = 0; i < n; i++) {
2653 x = args[i];
2654 Py_INCREF(x);
2655 SETLOCAL(i, x);
2657 if (co->co_flags & CO_VARARGS) {
2658 u = PyTuple_New(argcount - n);
2659 if (u == NULL)
2660 goto fail;
2661 SETLOCAL(co->co_argcount, u);
2662 for (i = n; i < argcount; i++) {
2663 x = args[i];
2664 Py_INCREF(x);
2665 PyTuple_SET_ITEM(u, i-n, x);
2668 for (i = 0; i < kwcount; i++) {
2669 PyObject *keyword = kws[2*i];
2670 PyObject *value = kws[2*i + 1];
2671 int j;
2672 if (keyword == NULL || !PyString_Check(keyword)) {
2673 PyErr_Format(PyExc_TypeError,
2674 "%.200s() keywords must be strings",
2675 PyString_AsString(co->co_name));
2676 goto fail;
2678 /* XXX slow -- speed up using dictionary? */
2679 for (j = 0; j < co->co_argcount; j++) {
2680 PyObject *nm = PyTuple_GET_ITEM(
2681 co->co_varnames, j);
2682 int cmp = PyObject_RichCompareBool(
2683 keyword, nm, Py_EQ);
2684 if (cmp > 0)
2685 break;
2686 else if (cmp < 0)
2687 goto fail;
2689 /* Check errors from Compare */
2690 if (PyErr_Occurred())
2691 goto fail;
2692 if (j >= co->co_argcount) {
2693 if (kwdict == NULL) {
2694 PyErr_Format(PyExc_TypeError,
2695 "%.200s() got an unexpected "
2696 "keyword argument '%.400s'",
2697 PyString_AsString(co->co_name),
2698 PyString_AsString(keyword));
2699 goto fail;
2701 PyDict_SetItem(kwdict, keyword, value);
2703 else {
2704 if (GETLOCAL(j) != NULL) {
2705 PyErr_Format(PyExc_TypeError,
2706 "%.200s() got multiple "
2707 "values for keyword "
2708 "argument '%.400s'",
2709 PyString_AsString(co->co_name),
2710 PyString_AsString(keyword));
2711 goto fail;
2713 Py_INCREF(value);
2714 SETLOCAL(j, value);
2717 if (argcount < co->co_argcount) {
2718 int m = co->co_argcount - defcount;
2719 for (i = argcount; i < m; i++) {
2720 if (GETLOCAL(i) == NULL) {
2721 PyErr_Format(PyExc_TypeError,
2722 "%.200s() takes %s %d "
2723 "%sargument%s (%d given)",
2724 PyString_AsString(co->co_name),
2725 ((co->co_flags & CO_VARARGS) ||
2726 defcount) ? "at least"
2727 : "exactly",
2728 m, kwcount ? "non-keyword " : "",
2729 m == 1 ? "" : "s", i);
2730 goto fail;
2733 if (n > m)
2734 i = n - m;
2735 else
2736 i = 0;
2737 for (; i < defcount; i++) {
2738 if (GETLOCAL(m+i) == NULL) {
2739 PyObject *def = defs[i];
2740 Py_INCREF(def);
2741 SETLOCAL(m+i, def);
2746 else {
2747 if (argcount > 0 || kwcount > 0) {
2748 PyErr_Format(PyExc_TypeError,
2749 "%.200s() takes no arguments (%d given)",
2750 PyString_AsString(co->co_name),
2751 argcount + kwcount);
2752 goto fail;
2755 /* Allocate and initialize storage for cell vars, and copy free
2756 vars into frame. This isn't too efficient right now. */
2757 if (PyTuple_GET_SIZE(co->co_cellvars)) {
2758 int i = 0, j = 0, nargs, found;
2759 char *cellname, *argname;
2760 PyObject *c;
2762 nargs = co->co_argcount;
2763 if (co->co_flags & CO_VARARGS)
2764 nargs++;
2765 if (co->co_flags & CO_VARKEYWORDS)
2766 nargs++;
2768 /* Initialize each cell var, taking into account
2769 cell vars that are initialized from arguments.
2771 Should arrange for the compiler to put cellvars
2772 that are arguments at the beginning of the cellvars
2773 list so that we can march over it more efficiently?
2775 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
2776 cellname = PyString_AS_STRING(
2777 PyTuple_GET_ITEM(co->co_cellvars, i));
2778 found = 0;
2779 for (j = 0; j < nargs; j++) {
2780 argname = PyString_AS_STRING(
2781 PyTuple_GET_ITEM(co->co_varnames, j));
2782 if (strcmp(cellname, argname) == 0) {
2783 c = PyCell_New(GETLOCAL(j));
2784 if (c == NULL)
2785 goto fail;
2786 GETLOCAL(co->co_nlocals + i) = c;
2787 found = 1;
2788 break;
2791 if (found == 0) {
2792 c = PyCell_New(NULL);
2793 if (c == NULL)
2794 goto fail;
2795 SETLOCAL(co->co_nlocals + i, c);
2799 if (PyTuple_GET_SIZE(co->co_freevars)) {
2800 int i;
2801 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
2802 PyObject *o = PyTuple_GET_ITEM(closure, i);
2803 Py_INCREF(o);
2804 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
2808 if (co->co_flags & CO_GENERATOR) {
2809 /* Don't need to keep the reference to f_back, it will be set
2810 * when the generator is resumed. */
2811 Py_XDECREF(f->f_back);
2812 f->f_back = NULL;
2814 PCALL(PCALL_GENERATOR);
2816 /* Create a new generator that owns the ready to run frame
2817 * and return that as the value. */
2818 return PyGen_New(f);
2821 retval = PyEval_EvalFrameEx(f,0);
2823 fail: /* Jump here from prelude on failure */
2825 /* decref'ing the frame can cause __del__ methods to get invoked,
2826 which can call back into Python. While we're done with the
2827 current Python frame (f), the associated C stack is still in use,
2828 so recursion_depth must be boosted for the duration.
2830 assert(tstate != NULL);
2831 ++tstate->recursion_depth;
2832 Py_DECREF(f);
2833 --tstate->recursion_depth;
2834 return retval;
2838 /* Implementation notes for set_exc_info() and reset_exc_info():
2840 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2841 'exc_traceback'. These always travel together.
2843 - tstate->curexc_ZZZ is the "hot" exception that is set by
2844 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2846 - Once an exception is caught by an except clause, it is transferred
2847 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2848 can pick it up. This is the primary task of set_exc_info().
2849 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
2851 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
2853 Long ago, when none of this existed, there were just a few globals:
2854 one set corresponding to the "hot" exception, and one set
2855 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2856 globals; they were simply stored as sys.exc_ZZZ. For backwards
2857 compatibility, they still are!) The problem was that in code like
2858 this:
2860 try:
2861 "something that may fail"
2862 except "some exception":
2863 "do something else first"
2864 "print the exception from sys.exc_ZZZ."
2866 if "do something else first" invoked something that raised and caught
2867 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2868 cause of subtle bugs. I fixed this by changing the semantics as
2869 follows:
2871 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2872 *in that frame*.
2874 - But initially, and as long as no exception is caught in a given
2875 frame, sys.exc_ZZZ will hold the last exception caught in the
2876 previous frame (or the frame before that, etc.).
2878 The first bullet fixed the bug in the above example. The second
2879 bullet was for backwards compatibility: it was (and is) common to
2880 have a function that is called when an exception is caught, and to
2881 have that function access the caught exception via sys.exc_ZZZ.
2882 (Example: traceback.print_exc()).
2884 At the same time I fixed the problem that sys.exc_ZZZ weren't
2885 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2886 but that's really a separate improvement.
2888 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2889 variables to what they were before the current frame was called. The
2890 set_exc_info() function saves them on the frame so that
2891 reset_exc_info() can restore them. The invariant is that
2892 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2893 exception (where "catching" an exception applies only to successful
2894 except clauses); and if the current frame ever caught an exception,
2895 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2896 at the start of the current frame.
2900 static void
2901 set_exc_info(PyThreadState *tstate,
2902 PyObject *type, PyObject *value, PyObject *tb)
2904 PyFrameObject *frame = tstate->frame;
2905 PyObject *tmp_type, *tmp_value, *tmp_tb;
2907 assert(type != NULL);
2908 assert(frame != NULL);
2909 if (frame->f_exc_type == NULL) {
2910 assert(frame->f_exc_value == NULL);
2911 assert(frame->f_exc_traceback == NULL);
2912 /* This frame didn't catch an exception before. */
2913 /* Save previous exception of this thread in this frame. */
2914 if (tstate->exc_type == NULL) {
2915 /* XXX Why is this set to Py_None? */
2916 Py_INCREF(Py_None);
2917 tstate->exc_type = Py_None;
2919 Py_INCREF(tstate->exc_type);
2920 Py_XINCREF(tstate->exc_value);
2921 Py_XINCREF(tstate->exc_traceback);
2922 frame->f_exc_type = tstate->exc_type;
2923 frame->f_exc_value = tstate->exc_value;
2924 frame->f_exc_traceback = tstate->exc_traceback;
2926 /* Set new exception for this thread. */
2927 tmp_type = tstate->exc_type;
2928 tmp_value = tstate->exc_value;
2929 tmp_tb = tstate->exc_traceback;
2930 Py_INCREF(type);
2931 Py_XINCREF(value);
2932 Py_XINCREF(tb);
2933 tstate->exc_type = type;
2934 tstate->exc_value = value;
2935 tstate->exc_traceback = tb;
2936 Py_XDECREF(tmp_type);
2937 Py_XDECREF(tmp_value);
2938 Py_XDECREF(tmp_tb);
2939 /* For b/w compatibility */
2940 PySys_SetObject("exc_type", type);
2941 PySys_SetObject("exc_value", value);
2942 PySys_SetObject("exc_traceback", tb);
2945 static void
2946 reset_exc_info(PyThreadState *tstate)
2948 PyFrameObject *frame;
2949 PyObject *tmp_type, *tmp_value, *tmp_tb;
2951 /* It's a precondition that the thread state's frame caught an
2952 * exception -- verify in a debug build.
2954 assert(tstate != NULL);
2955 frame = tstate->frame;
2956 assert(frame != NULL);
2957 assert(frame->f_exc_type != NULL);
2959 /* Copy the frame's exception info back to the thread state. */
2960 tmp_type = tstate->exc_type;
2961 tmp_value = tstate->exc_value;
2962 tmp_tb = tstate->exc_traceback;
2963 Py_INCREF(frame->f_exc_type);
2964 Py_XINCREF(frame->f_exc_value);
2965 Py_XINCREF(frame->f_exc_traceback);
2966 tstate->exc_type = frame->f_exc_type;
2967 tstate->exc_value = frame->f_exc_value;
2968 tstate->exc_traceback = frame->f_exc_traceback;
2969 Py_XDECREF(tmp_type);
2970 Py_XDECREF(tmp_value);
2971 Py_XDECREF(tmp_tb);
2973 /* For b/w compatibility */
2974 PySys_SetObject("exc_type", frame->f_exc_type);
2975 PySys_SetObject("exc_value", frame->f_exc_value);
2976 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2978 /* Clear the frame's exception info. */
2979 tmp_type = frame->f_exc_type;
2980 tmp_value = frame->f_exc_value;
2981 tmp_tb = frame->f_exc_traceback;
2982 frame->f_exc_type = NULL;
2983 frame->f_exc_value = NULL;
2984 frame->f_exc_traceback = NULL;
2985 Py_DECREF(tmp_type);
2986 Py_XDECREF(tmp_value);
2987 Py_XDECREF(tmp_tb);
2990 /* Logic for the raise statement (too complicated for inlining).
2991 This *consumes* a reference count to each of its arguments. */
2992 static enum why_code
2993 do_raise(PyObject *type, PyObject *value, PyObject *tb)
2995 if (type == NULL) {
2996 /* Reraise */
2997 PyThreadState *tstate = PyThreadState_GET();
2998 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2999 value = tstate->exc_value;
3000 tb = tstate->exc_traceback;
3001 Py_XINCREF(type);
3002 Py_XINCREF(value);
3003 Py_XINCREF(tb);
3006 /* We support the following forms of raise:
3007 raise <class>, <classinstance>
3008 raise <class>, <argument tuple>
3009 raise <class>, None
3010 raise <class>, <argument>
3011 raise <classinstance>, None
3012 raise <string>, <object>
3013 raise <string>, None
3015 An omitted second argument is the same as None.
3017 In addition, raise <tuple>, <anything> is the same as
3018 raising the tuple's first item (and it better have one!);
3019 this rule is applied recursively.
3021 Finally, an optional third argument can be supplied, which
3022 gives the traceback to be substituted (useful when
3023 re-raising an exception after examining it). */
3025 /* First, check the traceback argument, replacing None with
3026 NULL. */
3027 if (tb == Py_None) {
3028 Py_DECREF(tb);
3029 tb = NULL;
3031 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3032 PyErr_SetString(PyExc_TypeError,
3033 "raise: arg 3 must be a traceback or None");
3034 goto raise_error;
3037 /* Next, replace a missing value with None */
3038 if (value == NULL) {
3039 value = Py_None;
3040 Py_INCREF(value);
3043 /* Next, repeatedly, replace a tuple exception with its first item */
3044 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3045 PyObject *tmp = type;
3046 type = PyTuple_GET_ITEM(type, 0);
3047 Py_INCREF(type);
3048 Py_DECREF(tmp);
3051 if (PyString_CheckExact(type)) {
3052 /* Raising builtin string is deprecated but still allowed --
3053 * do nothing. Raising an instance of a new-style str
3054 * subclass is right out. */
3055 if (PyErr_Warn(PyExc_DeprecationWarning,
3056 "raising a string exception is deprecated"))
3057 goto raise_error;
3059 else if (PyExceptionClass_Check(type))
3060 PyErr_NormalizeException(&type, &value, &tb);
3062 else if (PyExceptionInstance_Check(type)) {
3063 /* Raising an instance. The value should be a dummy. */
3064 if (value != Py_None) {
3065 PyErr_SetString(PyExc_TypeError,
3066 "instance exception may not have a separate value");
3067 goto raise_error;
3069 else {
3070 /* Normalize to raise <class>, <instance> */
3071 Py_DECREF(value);
3072 value = type;
3073 type = PyExceptionInstance_Class(type);
3074 Py_INCREF(type);
3077 else {
3078 /* Not something you can raise. You get an exception
3079 anyway, just not what you specified :-) */
3080 PyErr_Format(PyExc_TypeError,
3081 "exceptions must be classes, instances, or "
3082 "strings (deprecated), not %s",
3083 type->ob_type->tp_name);
3084 goto raise_error;
3086 PyErr_Restore(type, value, tb);
3087 if (tb == NULL)
3088 return WHY_EXCEPTION;
3089 else
3090 return WHY_RERAISE;
3091 raise_error:
3092 Py_XDECREF(value);
3093 Py_XDECREF(type);
3094 Py_XDECREF(tb);
3095 return WHY_EXCEPTION;
3098 /* Iterate v argcnt times and store the results on the stack (via decreasing
3099 sp). Return 1 for success, 0 if error. */
3101 static int
3102 unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
3104 int i = 0;
3105 PyObject *it; /* iter(v) */
3106 PyObject *w;
3108 assert(v != NULL);
3110 it = PyObject_GetIter(v);
3111 if (it == NULL)
3112 goto Error;
3114 for (; i < argcnt; i++) {
3115 w = PyIter_Next(it);
3116 if (w == NULL) {
3117 /* Iterator done, via error or exhaustion. */
3118 if (!PyErr_Occurred()) {
3119 PyErr_Format(PyExc_ValueError,
3120 "need more than %d value%s to unpack",
3121 i, i == 1 ? "" : "s");
3123 goto Error;
3125 *--sp = w;
3128 /* We better have exhausted the iterator now. */
3129 w = PyIter_Next(it);
3130 if (w == NULL) {
3131 if (PyErr_Occurred())
3132 goto Error;
3133 Py_DECREF(it);
3134 return 1;
3136 Py_DECREF(w);
3137 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3138 /* fall through */
3139 Error:
3140 for (; i > 0; i--, sp++)
3141 Py_DECREF(*sp);
3142 Py_XDECREF(it);
3143 return 0;
3147 #ifdef LLTRACE
3148 static int
3149 prtrace(PyObject *v, char *str)
3151 printf("%s ", str);
3152 if (PyObject_Print(v, stdout, 0) != 0)
3153 PyErr_Clear(); /* Don't know what else to do */
3154 printf("\n");
3155 return 1;
3157 #endif
3159 static void
3160 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
3162 PyObject *type, *value, *traceback, *arg;
3163 int err;
3164 PyErr_Fetch(&type, &value, &traceback);
3165 if (value == NULL) {
3166 value = Py_None;
3167 Py_INCREF(value);
3169 arg = PyTuple_Pack(3, type, value, traceback);
3170 if (arg == NULL) {
3171 PyErr_Restore(type, value, traceback);
3172 return;
3174 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3175 Py_DECREF(arg);
3176 if (err == 0)
3177 PyErr_Restore(type, value, traceback);
3178 else {
3179 Py_XDECREF(type);
3180 Py_XDECREF(value);
3181 Py_XDECREF(traceback);
3185 static void
3186 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3187 int what, PyObject *arg)
3189 PyObject *type, *value, *traceback;
3190 int err;
3191 PyErr_Fetch(&type, &value, &traceback);
3192 err = call_trace(func, obj, frame, what, arg);
3193 if (err == 0)
3194 PyErr_Restore(type, value, traceback);
3195 else {
3196 Py_XDECREF(type);
3197 Py_XDECREF(value);
3198 Py_XDECREF(traceback);
3202 static int
3203 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3204 int what, PyObject *arg)
3206 register PyThreadState *tstate = frame->f_tstate;
3207 int result;
3208 if (tstate->tracing)
3209 return 0;
3210 tstate->tracing++;
3211 tstate->use_tracing = 0;
3212 result = func(obj, frame, what, arg);
3213 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3214 || (tstate->c_profilefunc != NULL));
3215 tstate->tracing--;
3216 return result;
3219 PyObject *
3220 _PyEval_CallTracing(PyObject *func, PyObject *args)
3222 PyFrameObject *frame = PyEval_GetFrame();
3223 PyThreadState *tstate = frame->f_tstate;
3224 int save_tracing = tstate->tracing;
3225 int save_use_tracing = tstate->use_tracing;
3226 PyObject *result;
3228 tstate->tracing = 0;
3229 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3230 || (tstate->c_profilefunc != NULL));
3231 result = PyObject_Call(func, args, NULL);
3232 tstate->tracing = save_tracing;
3233 tstate->use_tracing = save_use_tracing;
3234 return result;
3237 static int
3238 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
3239 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3240 int *instr_prev)
3242 int result = 0;
3244 /* If the last instruction executed isn't in the current
3245 instruction window, reset the window. If the last
3246 instruction happens to fall at the start of a line or if it
3247 represents a jump backwards, call the trace function.
3249 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
3250 int line;
3251 PyAddrPair bounds;
3253 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3254 &bounds);
3255 if (line >= 0) {
3256 frame->f_lineno = line;
3257 result = call_trace(func, obj, frame,
3258 PyTrace_LINE, Py_None);
3260 *instr_lb = bounds.ap_lower;
3261 *instr_ub = bounds.ap_upper;
3263 else if (frame->f_lasti <= *instr_prev) {
3264 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3266 *instr_prev = frame->f_lasti;
3267 return result;
3270 void
3271 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
3273 PyThreadState *tstate = PyThreadState_GET();
3274 PyObject *temp = tstate->c_profileobj;
3275 Py_XINCREF(arg);
3276 tstate->c_profilefunc = NULL;
3277 tstate->c_profileobj = NULL;
3278 /* Must make sure that tracing is not ignored if 'temp' is freed */
3279 tstate->use_tracing = tstate->c_tracefunc != NULL;
3280 Py_XDECREF(temp);
3281 tstate->c_profilefunc = func;
3282 tstate->c_profileobj = arg;
3283 /* Flag that tracing or profiling is turned on */
3284 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
3287 void
3288 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3290 PyThreadState *tstate = PyThreadState_GET();
3291 PyObject *temp = tstate->c_traceobj;
3292 Py_XINCREF(arg);
3293 tstate->c_tracefunc = NULL;
3294 tstate->c_traceobj = NULL;
3295 /* Must make sure that profiling is not ignored if 'temp' is freed */
3296 tstate->use_tracing = tstate->c_profilefunc != NULL;
3297 Py_XDECREF(temp);
3298 tstate->c_tracefunc = func;
3299 tstate->c_traceobj = arg;
3300 /* Flag that tracing or profiling is turned on */
3301 tstate->use_tracing = ((func != NULL)
3302 || (tstate->c_profilefunc != NULL));
3305 PyObject *
3306 PyEval_GetBuiltins(void)
3308 PyFrameObject *current_frame = PyEval_GetFrame();
3309 if (current_frame == NULL)
3310 return PyThreadState_GET()->interp->builtins;
3311 else
3312 return current_frame->f_builtins;
3315 PyObject *
3316 PyEval_GetLocals(void)
3318 PyFrameObject *current_frame = PyEval_GetFrame();
3319 if (current_frame == NULL)
3320 return NULL;
3321 PyFrame_FastToLocals(current_frame);
3322 return current_frame->f_locals;
3325 PyObject *
3326 PyEval_GetGlobals(void)
3328 PyFrameObject *current_frame = PyEval_GetFrame();
3329 if (current_frame == NULL)
3330 return NULL;
3331 else
3332 return current_frame->f_globals;
3335 PyFrameObject *
3336 PyEval_GetFrame(void)
3338 PyThreadState *tstate = PyThreadState_GET();
3339 return _PyThreadState_GetFrame(tstate);
3343 PyEval_GetRestricted(void)
3345 PyFrameObject *current_frame = PyEval_GetFrame();
3346 return current_frame == NULL ? 0 : current_frame->f_restricted;
3350 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
3352 PyFrameObject *current_frame = PyEval_GetFrame();
3353 int result = cf->cf_flags != 0;
3355 if (current_frame != NULL) {
3356 const int codeflags = current_frame->f_code->co_flags;
3357 const int compilerflags = codeflags & PyCF_MASK;
3358 if (compilerflags) {
3359 result = 1;
3360 cf->cf_flags |= compilerflags;
3362 #if 0 /* future keyword */
3363 if (codeflags & CO_GENERATOR_ALLOWED) {
3364 result = 1;
3365 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3367 #endif
3369 return result;
3373 Py_FlushLine(void)
3375 PyObject *f = PySys_GetObject("stdout");
3376 if (f == NULL)
3377 return 0;
3378 if (!PyFile_SoftSpace(f, 0))
3379 return 0;
3380 return PyFile_WriteString("\n", f);
3384 /* External interface to call any callable object.
3385 The arg must be a tuple or NULL. */
3387 #undef PyEval_CallObject
3388 /* for backward compatibility: export this interface */
3390 PyObject *
3391 PyEval_CallObject(PyObject *func, PyObject *arg)
3393 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
3395 #define PyEval_CallObject(func,arg) \
3396 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3398 PyObject *
3399 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
3401 PyObject *result;
3403 if (arg == NULL) {
3404 arg = PyTuple_New(0);
3405 if (arg == NULL)
3406 return NULL;
3408 else if (!PyTuple_Check(arg)) {
3409 PyErr_SetString(PyExc_TypeError,
3410 "argument list must be a tuple");
3411 return NULL;
3413 else
3414 Py_INCREF(arg);
3416 if (kw != NULL && !PyDict_Check(kw)) {
3417 PyErr_SetString(PyExc_TypeError,
3418 "keyword list must be a dictionary");
3419 Py_DECREF(arg);
3420 return NULL;
3423 result = PyObject_Call(func, arg, kw);
3424 Py_DECREF(arg);
3425 return result;
3428 const char *
3429 PyEval_GetFuncName(PyObject *func)
3431 if (PyMethod_Check(func))
3432 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3433 else if (PyFunction_Check(func))
3434 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3435 else if (PyCFunction_Check(func))
3436 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3437 else if (PyClass_Check(func))
3438 return PyString_AsString(((PyClassObject*)func)->cl_name);
3439 else if (PyInstance_Check(func)) {
3440 return PyString_AsString(
3441 ((PyInstanceObject*)func)->in_class->cl_name);
3442 } else {
3443 return func->ob_type->tp_name;
3447 const char *
3448 PyEval_GetFuncDesc(PyObject *func)
3450 if (PyMethod_Check(func))
3451 return "()";
3452 else if (PyFunction_Check(func))
3453 return "()";
3454 else if (PyCFunction_Check(func))
3455 return "()";
3456 else if (PyClass_Check(func))
3457 return " constructor";
3458 else if (PyInstance_Check(func)) {
3459 return " instance";
3460 } else {
3461 return " object";
3465 static void
3466 err_args(PyObject *func, int flags, int nargs)
3468 if (flags & METH_NOARGS)
3469 PyErr_Format(PyExc_TypeError,
3470 "%.200s() takes no arguments (%d given)",
3471 ((PyCFunctionObject *)func)->m_ml->ml_name,
3472 nargs);
3473 else
3474 PyErr_Format(PyExc_TypeError,
3475 "%.200s() takes exactly one argument (%d given)",
3476 ((PyCFunctionObject *)func)->m_ml->ml_name,
3477 nargs);
3480 #define C_TRACE(x, call) \
3481 if (tstate->use_tracing && tstate->c_profilefunc) { \
3482 if (call_trace(tstate->c_profilefunc, \
3483 tstate->c_profileobj, \
3484 tstate->frame, PyTrace_C_CALL, \
3485 func)) { \
3486 x = NULL; \
3488 else { \
3489 x = call; \
3490 if (tstate->c_profilefunc != NULL) { \
3491 if (x == NULL) { \
3492 call_trace_protected(tstate->c_profilefunc, \
3493 tstate->c_profileobj, \
3494 tstate->frame, PyTrace_C_EXCEPTION, \
3495 func); \
3496 /* XXX should pass (type, value, tb) */ \
3497 } else { \
3498 if (call_trace(tstate->c_profilefunc, \
3499 tstate->c_profileobj, \
3500 tstate->frame, PyTrace_C_RETURN, \
3501 func)) { \
3502 Py_DECREF(x); \
3503 x = NULL; \
3508 } else { \
3509 x = call; \
3512 static PyObject *
3513 call_function(PyObject ***pp_stack, int oparg
3514 #ifdef WITH_TSC
3515 , uint64* pintr0, uint64* pintr1
3516 #endif
3519 int na = oparg & 0xff;
3520 int nk = (oparg>>8) & 0xff;
3521 int n = na + 2 * nk;
3522 PyObject **pfunc = (*pp_stack) - n - 1;
3523 PyObject *func = *pfunc;
3524 PyObject *x, *w;
3526 /* Always dispatch PyCFunction first, because these are
3527 presumed to be the most frequent callable object.
3529 if (PyCFunction_Check(func) && nk == 0) {
3530 int flags = PyCFunction_GET_FLAGS(func);
3531 PyThreadState *tstate = PyThreadState_GET();
3533 PCALL(PCALL_CFUNCTION);
3534 if (flags & (METH_NOARGS | METH_O)) {
3535 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3536 PyObject *self = PyCFunction_GET_SELF(func);
3537 if (flags & METH_NOARGS && na == 0) {
3538 C_TRACE(x, (*meth)(self,NULL));
3540 else if (flags & METH_O && na == 1) {
3541 PyObject *arg = EXT_POP(*pp_stack);
3542 C_TRACE(x, (*meth)(self,arg));
3543 Py_DECREF(arg);
3545 else {
3546 err_args(func, flags, na);
3547 x = NULL;
3550 else {
3551 PyObject *callargs;
3552 callargs = load_args(pp_stack, na);
3553 READ_TIMESTAMP(*pintr0);
3554 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3555 READ_TIMESTAMP(*pintr1);
3556 Py_XDECREF(callargs);
3558 } else {
3559 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3560 /* optimize access to bound methods */
3561 PyObject *self = PyMethod_GET_SELF(func);
3562 PCALL(PCALL_METHOD);
3563 PCALL(PCALL_BOUND_METHOD);
3564 Py_INCREF(self);
3565 func = PyMethod_GET_FUNCTION(func);
3566 Py_INCREF(func);
3567 Py_DECREF(*pfunc);
3568 *pfunc = self;
3569 na++;
3570 n++;
3571 } else
3572 Py_INCREF(func);
3573 READ_TIMESTAMP(*pintr0);
3574 if (PyFunction_Check(func))
3575 x = fast_function(func, pp_stack, n, na, nk);
3576 else
3577 x = do_call(func, pp_stack, na, nk);
3578 READ_TIMESTAMP(*pintr1);
3579 Py_DECREF(func);
3582 /* Clear the stack of the function object. Also removes
3583 the arguments in case they weren't consumed already
3584 (fast_function() and err_args() leave them on the stack).
3586 while ((*pp_stack) > pfunc) {
3587 w = EXT_POP(*pp_stack);
3588 Py_DECREF(w);
3589 PCALL(PCALL_POP);
3591 return x;
3594 /* The fast_function() function optimize calls for which no argument
3595 tuple is necessary; the objects are passed directly from the stack.
3596 For the simplest case -- a function that takes only positional
3597 arguments and is called with only positional arguments -- it
3598 inlines the most primitive frame setup code from
3599 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3600 done before evaluating the frame.
3603 static PyObject *
3604 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
3606 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3607 PyObject *globals = PyFunction_GET_GLOBALS(func);
3608 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3609 PyObject **d = NULL;
3610 int nd = 0;
3612 PCALL(PCALL_FUNCTION);
3613 PCALL(PCALL_FAST_FUNCTION);
3614 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
3615 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3616 PyFrameObject *f;
3617 PyObject *retval = NULL;
3618 PyThreadState *tstate = PyThreadState_GET();
3619 PyObject **fastlocals, **stack;
3620 int i;
3622 PCALL(PCALL_FASTER_FUNCTION);
3623 assert(globals != NULL);
3624 /* XXX Perhaps we should create a specialized
3625 PyFrame_New() that doesn't take locals, but does
3626 take builtins without sanity checking them.
3628 f = PyFrame_New(tstate, co, globals, NULL);
3629 if (f == NULL)
3630 return NULL;
3632 fastlocals = f->f_localsplus;
3633 stack = (*pp_stack) - n;
3635 for (i = 0; i < n; i++) {
3636 Py_INCREF(*stack);
3637 fastlocals[i] = *stack++;
3639 retval = PyEval_EvalFrameEx(f,0);
3640 assert(tstate != NULL);
3641 ++tstate->recursion_depth;
3642 Py_DECREF(f);
3643 --tstate->recursion_depth;
3644 return retval;
3646 if (argdefs != NULL) {
3647 d = &PyTuple_GET_ITEM(argdefs, 0);
3648 nd = ((PyTupleObject *)argdefs)->ob_size;
3650 return PyEval_EvalCodeEx(co, globals,
3651 (PyObject *)NULL, (*pp_stack)-n, na,
3652 (*pp_stack)-2*nk, nk, d, nd,
3653 PyFunction_GET_CLOSURE(func));
3656 static PyObject *
3657 update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3658 PyObject *func)
3660 PyObject *kwdict = NULL;
3661 if (orig_kwdict == NULL)
3662 kwdict = PyDict_New();
3663 else {
3664 kwdict = PyDict_Copy(orig_kwdict);
3665 Py_DECREF(orig_kwdict);
3667 if (kwdict == NULL)
3668 return NULL;
3669 while (--nk >= 0) {
3670 int err;
3671 PyObject *value = EXT_POP(*pp_stack);
3672 PyObject *key = EXT_POP(*pp_stack);
3673 if (PyDict_GetItem(kwdict, key) != NULL) {
3674 PyErr_Format(PyExc_TypeError,
3675 "%.200s%s got multiple values "
3676 "for keyword argument '%.200s'",
3677 PyEval_GetFuncName(func),
3678 PyEval_GetFuncDesc(func),
3679 PyString_AsString(key));
3680 Py_DECREF(key);
3681 Py_DECREF(value);
3682 Py_DECREF(kwdict);
3683 return NULL;
3685 err = PyDict_SetItem(kwdict, key, value);
3686 Py_DECREF(key);
3687 Py_DECREF(value);
3688 if (err) {
3689 Py_DECREF(kwdict);
3690 return NULL;
3693 return kwdict;
3696 static PyObject *
3697 update_star_args(int nstack, int nstar, PyObject *stararg,
3698 PyObject ***pp_stack)
3700 PyObject *callargs, *w;
3702 callargs = PyTuple_New(nstack + nstar);
3703 if (callargs == NULL) {
3704 return NULL;
3706 if (nstar) {
3707 int i;
3708 for (i = 0; i < nstar; i++) {
3709 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3710 Py_INCREF(a);
3711 PyTuple_SET_ITEM(callargs, nstack + i, a);
3714 while (--nstack >= 0) {
3715 w = EXT_POP(*pp_stack);
3716 PyTuple_SET_ITEM(callargs, nstack, w);
3718 return callargs;
3721 static PyObject *
3722 load_args(PyObject ***pp_stack, int na)
3724 PyObject *args = PyTuple_New(na);
3725 PyObject *w;
3727 if (args == NULL)
3728 return NULL;
3729 while (--na >= 0) {
3730 w = EXT_POP(*pp_stack);
3731 PyTuple_SET_ITEM(args, na, w);
3733 return args;
3736 static PyObject *
3737 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3739 PyObject *callargs = NULL;
3740 PyObject *kwdict = NULL;
3741 PyObject *result = NULL;
3743 if (nk > 0) {
3744 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
3745 if (kwdict == NULL)
3746 goto call_fail;
3748 callargs = load_args(pp_stack, na);
3749 if (callargs == NULL)
3750 goto call_fail;
3751 #ifdef CALL_PROFILE
3752 /* At this point, we have to look at the type of func to
3753 update the call stats properly. Do it here so as to avoid
3754 exposing the call stats machinery outside ceval.c
3756 if (PyFunction_Check(func))
3757 PCALL(PCALL_FUNCTION);
3758 else if (PyMethod_Check(func))
3759 PCALL(PCALL_METHOD);
3760 else if (PyType_Check(func))
3761 PCALL(PCALL_TYPE);
3762 else
3763 PCALL(PCALL_OTHER);
3764 #endif
3765 result = PyObject_Call(func, callargs, kwdict);
3766 call_fail:
3767 Py_XDECREF(callargs);
3768 Py_XDECREF(kwdict);
3769 return result;
3772 static PyObject *
3773 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3775 int nstar = 0;
3776 PyObject *callargs = NULL;
3777 PyObject *stararg = NULL;
3778 PyObject *kwdict = NULL;
3779 PyObject *result = NULL;
3781 if (flags & CALL_FLAG_KW) {
3782 kwdict = EXT_POP(*pp_stack);
3783 if (!(kwdict && PyDict_Check(kwdict))) {
3784 PyErr_Format(PyExc_TypeError,
3785 "%s%s argument after ** "
3786 "must be a dictionary",
3787 PyEval_GetFuncName(func),
3788 PyEval_GetFuncDesc(func));
3789 goto ext_call_fail;
3792 if (flags & CALL_FLAG_VAR) {
3793 stararg = EXT_POP(*pp_stack);
3794 if (!PyTuple_Check(stararg)) {
3795 PyObject *t = NULL;
3796 t = PySequence_Tuple(stararg);
3797 if (t == NULL) {
3798 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3799 PyErr_Format(PyExc_TypeError,
3800 "%s%s argument after * "
3801 "must be a sequence",
3802 PyEval_GetFuncName(func),
3803 PyEval_GetFuncDesc(func));
3805 goto ext_call_fail;
3807 Py_DECREF(stararg);
3808 stararg = t;
3810 nstar = PyTuple_GET_SIZE(stararg);
3812 if (nk > 0) {
3813 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
3814 if (kwdict == NULL)
3815 goto ext_call_fail;
3817 callargs = update_star_args(na, nstar, stararg, pp_stack);
3818 if (callargs == NULL)
3819 goto ext_call_fail;
3820 #ifdef CALL_PROFILE
3821 /* At this point, we have to look at the type of func to
3822 update the call stats properly. Do it here so as to avoid
3823 exposing the call stats machinery outside ceval.c
3825 if (PyFunction_Check(func))
3826 PCALL(PCALL_FUNCTION);
3827 else if (PyMethod_Check(func))
3828 PCALL(PCALL_METHOD);
3829 else if (PyType_Check(func))
3830 PCALL(PCALL_TYPE);
3831 else
3832 PCALL(PCALL_OTHER);
3833 #endif
3834 result = PyObject_Call(func, callargs, kwdict);
3835 ext_call_fail:
3836 Py_XDECREF(callargs);
3837 Py_XDECREF(kwdict);
3838 Py_XDECREF(stararg);
3839 return result;
3842 /* Extract a slice index from a PyInt or PyLong or an object with the
3843 nb_index slot defined, and store in *pi.
3844 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3845 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
3846 Return 0 on error, 1 on success.
3848 /* Note: If v is NULL, return success without storing into *pi. This
3849 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3850 called by the SLICE opcode with v and/or w equal to NULL.
3853 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
3855 if (v != NULL) {
3856 Py_ssize_t x;
3857 if (PyInt_Check(v)) {
3858 x = PyInt_AsSsize_t(v);
3860 else if (v->ob_type->tp_as_number &&
3861 PyType_HasFeature(v->ob_type, Py_TPFLAGS_HAVE_INDEX)
3862 && v->ob_type->tp_as_number->nb_index) {
3863 x = v->ob_type->tp_as_number->nb_index(v);
3864 if (x == -1 && PyErr_Occurred())
3865 return 0;
3867 else {
3868 PyErr_SetString(PyExc_TypeError,
3869 "slice indices must be integers or "
3870 "None or have an __index__ method");
3871 return 0;
3873 *pi = x;
3875 return 1;
3878 #undef ISINDEX
3879 #define ISINDEX(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x) || \
3880 ((x)->ob_type->tp_as_number && \
3881 PyType_HasFeature((x)->ob_type, Py_TPFLAGS_HAVE_INDEX) \
3882 && (x)->ob_type->tp_as_number->nb_index))
3884 static PyObject *
3885 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
3887 PyTypeObject *tp = u->ob_type;
3888 PySequenceMethods *sq = tp->tp_as_sequence;
3890 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
3891 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
3892 if (!_PyEval_SliceIndex(v, &ilow))
3893 return NULL;
3894 if (!_PyEval_SliceIndex(w, &ihigh))
3895 return NULL;
3896 return PySequence_GetSlice(u, ilow, ihigh);
3898 else {
3899 PyObject *slice = PySlice_New(v, w, NULL);
3900 if (slice != NULL) {
3901 PyObject *res = PyObject_GetItem(u, slice);
3902 Py_DECREF(slice);
3903 return res;
3905 else
3906 return NULL;
3910 static int
3911 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3912 /* u[v:w] = x */
3914 PyTypeObject *tp = u->ob_type;
3915 PySequenceMethods *sq = tp->tp_as_sequence;
3917 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
3918 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
3919 if (!_PyEval_SliceIndex(v, &ilow))
3920 return -1;
3921 if (!_PyEval_SliceIndex(w, &ihigh))
3922 return -1;
3923 if (x == NULL)
3924 return PySequence_DelSlice(u, ilow, ihigh);
3925 else
3926 return PySequence_SetSlice(u, ilow, ihigh, x);
3928 else {
3929 PyObject *slice = PySlice_New(v, w, NULL);
3930 if (slice != NULL) {
3931 int res;
3932 if (x != NULL)
3933 res = PyObject_SetItem(u, slice, x);
3934 else
3935 res = PyObject_DelItem(u, slice);
3936 Py_DECREF(slice);
3937 return res;
3939 else
3940 return -1;
3944 static PyObject *
3945 cmp_outcome(int op, register PyObject *v, register PyObject *w)
3947 int res = 0;
3948 switch (op) {
3949 case PyCmp_IS:
3950 res = (v == w);
3951 break;
3952 case PyCmp_IS_NOT:
3953 res = (v != w);
3954 break;
3955 case PyCmp_IN:
3956 res = PySequence_Contains(w, v);
3957 if (res < 0)
3958 return NULL;
3959 break;
3960 case PyCmp_NOT_IN:
3961 res = PySequence_Contains(w, v);
3962 if (res < 0)
3963 return NULL;
3964 res = !res;
3965 break;
3966 case PyCmp_EXC_MATCH:
3967 res = PyErr_GivenExceptionMatches(v, w);
3968 break;
3969 default:
3970 return PyObject_RichCompare(v, w, op);
3972 v = res ? Py_True : Py_False;
3973 Py_INCREF(v);
3974 return v;
3977 static PyObject *
3978 import_from(PyObject *v, PyObject *name)
3980 PyObject *x;
3982 x = PyObject_GetAttr(v, name);
3983 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
3984 PyErr_Format(PyExc_ImportError,
3985 "cannot import name %.230s",
3986 PyString_AsString(name));
3988 return x;
3991 static int
3992 import_all_from(PyObject *locals, PyObject *v)
3994 PyObject *all = PyObject_GetAttrString(v, "__all__");
3995 PyObject *dict, *name, *value;
3996 int skip_leading_underscores = 0;
3997 int pos, err;
3999 if (all == NULL) {
4000 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4001 return -1; /* Unexpected error */
4002 PyErr_Clear();
4003 dict = PyObject_GetAttrString(v, "__dict__");
4004 if (dict == NULL) {
4005 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4006 return -1;
4007 PyErr_SetString(PyExc_ImportError,
4008 "from-import-* object has no __dict__ and no __all__");
4009 return -1;
4011 all = PyMapping_Keys(dict);
4012 Py_DECREF(dict);
4013 if (all == NULL)
4014 return -1;
4015 skip_leading_underscores = 1;
4018 for (pos = 0, err = 0; ; pos++) {
4019 name = PySequence_GetItem(all, pos);
4020 if (name == NULL) {
4021 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4022 err = -1;
4023 else
4024 PyErr_Clear();
4025 break;
4027 if (skip_leading_underscores &&
4028 PyString_Check(name) &&
4029 PyString_AS_STRING(name)[0] == '_')
4031 Py_DECREF(name);
4032 continue;
4034 value = PyObject_GetAttr(v, name);
4035 if (value == NULL)
4036 err = -1;
4037 else
4038 err = PyDict_SetItem(locals, name, value);
4039 Py_DECREF(name);
4040 Py_XDECREF(value);
4041 if (err != 0)
4042 break;
4044 Py_DECREF(all);
4045 return err;
4048 static PyObject *
4049 build_class(PyObject *methods, PyObject *bases, PyObject *name)
4051 PyObject *metaclass = NULL, *result, *base;
4053 if (PyDict_Check(methods))
4054 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4055 if (metaclass != NULL)
4056 Py_INCREF(metaclass);
4057 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4058 base = PyTuple_GET_ITEM(bases, 0);
4059 metaclass = PyObject_GetAttrString(base, "__class__");
4060 if (metaclass == NULL) {
4061 PyErr_Clear();
4062 metaclass = (PyObject *)base->ob_type;
4063 Py_INCREF(metaclass);
4066 else {
4067 PyObject *g = PyEval_GetGlobals();
4068 if (g != NULL && PyDict_Check(g))
4069 metaclass = PyDict_GetItemString(g, "__metaclass__");
4070 if (metaclass == NULL)
4071 metaclass = (PyObject *) &PyClass_Type;
4072 Py_INCREF(metaclass);
4074 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
4075 Py_DECREF(metaclass);
4076 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4077 /* A type error here likely means that the user passed
4078 in a base that was not a class (such the random module
4079 instead of the random.random type). Help them out with
4080 by augmenting the error message with more information.*/
4082 PyObject *ptype, *pvalue, *ptraceback;
4084 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4085 if (PyString_Check(pvalue)) {
4086 PyObject *newmsg;
4087 newmsg = PyString_FromFormat(
4088 "Error when calling the metaclass bases\n %s",
4089 PyString_AS_STRING(pvalue));
4090 if (newmsg != NULL) {
4091 Py_DECREF(pvalue);
4092 pvalue = newmsg;
4095 PyErr_Restore(ptype, pvalue, ptraceback);
4097 return result;
4100 static int
4101 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4102 PyObject *locals)
4104 int n;
4105 PyObject *v;
4106 int plain = 0;
4108 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4109 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4110 /* Backward compatibility hack */
4111 globals = PyTuple_GetItem(prog, 1);
4112 if (n == 3)
4113 locals = PyTuple_GetItem(prog, 2);
4114 prog = PyTuple_GetItem(prog, 0);
4116 if (globals == Py_None) {
4117 globals = PyEval_GetGlobals();
4118 if (locals == Py_None) {
4119 locals = PyEval_GetLocals();
4120 plain = 1;
4123 else if (locals == Py_None)
4124 locals = globals;
4125 if (!PyString_Check(prog) &&
4126 !PyUnicode_Check(prog) &&
4127 !PyCode_Check(prog) &&
4128 !PyFile_Check(prog)) {
4129 PyErr_SetString(PyExc_TypeError,
4130 "exec: arg 1 must be a string, file, or code object");
4131 return -1;
4133 if (!PyDict_Check(globals)) {
4134 PyErr_SetString(PyExc_TypeError,
4135 "exec: arg 2 must be a dictionary or None");
4136 return -1;
4138 if (!PyMapping_Check(locals)) {
4139 PyErr_SetString(PyExc_TypeError,
4140 "exec: arg 3 must be a mapping or None");
4141 return -1;
4143 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4144 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4145 if (PyCode_Check(prog)) {
4146 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4147 PyErr_SetString(PyExc_TypeError,
4148 "code object passed to exec may not contain free variables");
4149 return -1;
4151 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4153 else if (PyFile_Check(prog)) {
4154 FILE *fp = PyFile_AsFile(prog);
4155 char *name = PyString_AsString(PyFile_Name(prog));
4156 PyCompilerFlags cf;
4157 cf.cf_flags = 0;
4158 if (PyEval_MergeCompilerFlags(&cf))
4159 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4160 locals, &cf);
4161 else
4162 v = PyRun_File(fp, name, Py_file_input, globals,
4163 locals);
4165 else {
4166 PyObject *tmp = NULL;
4167 char *str;
4168 PyCompilerFlags cf;
4169 cf.cf_flags = 0;
4170 #ifdef Py_USING_UNICODE
4171 if (PyUnicode_Check(prog)) {
4172 tmp = PyUnicode_AsUTF8String(prog);
4173 if (tmp == NULL)
4174 return -1;
4175 prog = tmp;
4176 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4178 #endif
4179 if (PyString_AsStringAndSize(prog, &str, NULL))
4180 return -1;
4181 if (PyEval_MergeCompilerFlags(&cf))
4182 v = PyRun_StringFlags(str, Py_file_input, globals,
4183 locals, &cf);
4184 else
4185 v = PyRun_String(str, Py_file_input, globals, locals);
4186 Py_XDECREF(tmp);
4188 if (plain)
4189 PyFrame_LocalsToFast(f, 0);
4190 if (v == NULL)
4191 return -1;
4192 Py_DECREF(v);
4193 return 0;
4196 static void
4197 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4199 char *obj_str;
4201 if (!obj)
4202 return;
4204 obj_str = PyString_AsString(obj);
4205 if (!obj_str)
4206 return;
4208 PyErr_Format(exc, format_str, obj_str);
4211 static PyObject *
4212 string_concatenate(PyObject *v, PyObject *w,
4213 PyFrameObject *f, unsigned char *next_instr)
4215 /* This function implements 'variable += expr' when both arguments
4216 are strings. */
4218 if (v->ob_refcnt == 2) {
4219 /* In the common case, there are 2 references to the value
4220 * stored in 'variable' when the += is performed: one on the
4221 * value stack (in 'v') and one still stored in the 'variable'.
4222 * We try to delete the variable now to reduce the refcnt to 1.
4224 switch (*next_instr) {
4225 case STORE_FAST:
4227 int oparg = PEEKARG();
4228 PyObject **fastlocals = f->f_localsplus;
4229 if (GETLOCAL(oparg) == v)
4230 SETLOCAL(oparg, NULL);
4231 break;
4233 case STORE_DEREF:
4235 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
4236 PyObject *c = freevars[PEEKARG()];
4237 if (PyCell_GET(c) == v)
4238 PyCell_Set(c, NULL);
4239 break;
4241 case STORE_NAME:
4243 PyObject *names = f->f_code->co_names;
4244 PyObject *name = GETITEM(names, PEEKARG());
4245 PyObject *locals = f->f_locals;
4246 if (PyDict_CheckExact(locals) &&
4247 PyDict_GetItem(locals, name) == v) {
4248 if (PyDict_DelItem(locals, name) != 0) {
4249 PyErr_Clear();
4252 break;
4257 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4258 /* Now we own the last reference to 'v', so we can resize it
4259 * in-place.
4261 Py_ssize_t v_len = PyString_GET_SIZE(v);
4262 Py_ssize_t w_len = PyString_GET_SIZE(w);
4263 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4264 /* XXX if _PyString_Resize() fails, 'v' has been
4265 * deallocated so it cannot be put back into 'variable'.
4266 * The MemoryError is raised when there is no value in
4267 * 'variable', which might (very remotely) be a cause
4268 * of incompatibilities.
4270 return NULL;
4272 /* copy 'w' into the newly allocated area of 'v' */
4273 memcpy(PyString_AS_STRING(v) + v_len,
4274 PyString_AS_STRING(w), w_len);
4275 return v;
4277 else {
4278 /* When in-place resizing is not an option. */
4279 PyString_Concat(&v, w);
4280 return v;
4284 #ifdef DYNAMIC_EXECUTION_PROFILE
4286 static PyObject *
4287 getarray(long a[256])
4289 int i;
4290 PyObject *l = PyList_New(256);
4291 if (l == NULL) return NULL;
4292 for (i = 0; i < 256; i++) {
4293 PyObject *x = PyInt_FromLong(a[i]);
4294 if (x == NULL) {
4295 Py_DECREF(l);
4296 return NULL;
4298 PyList_SetItem(l, i, x);
4300 for (i = 0; i < 256; i++)
4301 a[i] = 0;
4302 return l;
4305 PyObject *
4306 _Py_GetDXProfile(PyObject *self, PyObject *args)
4308 #ifndef DXPAIRS
4309 return getarray(dxp);
4310 #else
4311 int i;
4312 PyObject *l = PyList_New(257);
4313 if (l == NULL) return NULL;
4314 for (i = 0; i < 257; i++) {
4315 PyObject *x = getarray(dxpairs[i]);
4316 if (x == NULL) {
4317 Py_DECREF(l);
4318 return NULL;
4320 PyList_SetItem(l, i, x);
4322 return l;
4323 #endif
4326 #endif