Hum, test skipping when the URL isn't reachable hadn't been applied to trunk.
[python.git] / Python / ceval.c
blobdd820f298512df684c4534e4dc89e1f99b3ac9f8
2 /* Execute compiled code */
4 /* XXX TO DO:
5 XXX speed up searching for keywords by using a dictionary
6 XXX document it!
7 */
9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE
12 #include "Python.h"
14 #include "code.h"
15 #include "frameobject.h"
16 #include "eval.h"
17 #include "opcode.h"
18 #include "structmember.h"
20 #include <ctype.h>
22 #ifndef WITH_TSC
24 #define READ_TIMESTAMP(var)
26 #else
28 typedef unsigned long long uint64;
30 #if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
31 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
35 #define READ_TIMESTAMP(var) ppc_getcounter(&var)
37 static void
38 ppc_getcounter(uint64 *v)
40 register unsigned long tbu, tb, tbu2;
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
48 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
54 #elif defined(__i386__)
56 /* this is for linux/x86 (and probably any other GCC/x86 combo) */
58 #define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
61 #elif defined(__x86_64__)
63 /* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
68 #define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
73 #else
75 #error "Don't know how to implement timestamp counter for this architecture"
77 #endif
79 void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
80 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
82 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
90 opcode, ticked, inst, loop);
93 #endif
95 /* Turn this on if your compiler chokes on the big switch: */
96 /* #define CASE_TOO_BIG 1 */
98 #ifdef Py_DEBUG
99 /* For debugging the interpreter: */
100 #define LLTRACE 1 /* Low-level trace feature */
101 #define CHECKEXC 1 /* Double-check exception checking */
102 #endif
104 typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
106 /* Forward declarations */
107 #ifdef WITH_TSC
108 static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
109 #else
110 static PyObject * call_function(PyObject ***, int);
111 #endif
112 static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113 static PyObject * do_call(PyObject *, PyObject ***, int, int);
114 static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
115 static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
116 PyObject *);
117 static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118 static PyObject * load_args(PyObject ***, int);
119 #define CALL_FLAG_VAR 1
120 #define CALL_FLAG_KW 2
122 #ifdef LLTRACE
123 static int lltrace;
124 static int prtrace(PyObject *, char *);
125 #endif
126 static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
127 int, PyObject *);
128 static int call_trace_protected(Py_tracefunc, PyObject *,
129 PyFrameObject *, int, PyObject *);
130 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
131 static int maybe_call_line_trace(Py_tracefunc, PyObject *,
132 PyFrameObject *, int *, int *, int *);
134 static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
135 static int assign_slice(PyObject *, PyObject *,
136 PyObject *, PyObject *);
137 static PyObject * cmp_outcome(int, PyObject *, PyObject *);
138 static PyObject * import_from(PyObject *, PyObject *);
139 static int import_all_from(PyObject *, PyObject *);
140 static PyObject * build_class(PyObject *, PyObject *, PyObject *);
141 static int exec_statement(PyFrameObject *,
142 PyObject *, PyObject *, PyObject *);
143 static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
144 static void reset_exc_info(PyThreadState *);
145 static void format_exc_check_arg(PyObject *, char *, PyObject *);
146 static PyObject * string_concatenate(PyObject *, PyObject *,
147 PyFrameObject *, unsigned char *);
148 static PyObject * kwd_as_string(PyObject *);
149 static PyObject * special_lookup(PyObject *, char *, PyObject **);
151 #define NAME_ERROR_MSG \
152 "name '%.200s' is not defined"
153 #define GLOBAL_NAME_ERROR_MSG \
154 "global name '%.200s' is not defined"
155 #define UNBOUNDLOCAL_ERROR_MSG \
156 "local variable '%.200s' referenced before assignment"
157 #define UNBOUNDFREE_ERROR_MSG \
158 "free variable '%.200s' referenced before assignment" \
159 " in enclosing scope"
161 /* Dynamic execution profile */
162 #ifdef DYNAMIC_EXECUTION_PROFILE
163 #ifdef DXPAIRS
164 static long dxpairs[257][256];
165 #define dxp dxpairs[256]
166 #else
167 static long dxp[256];
168 #endif
169 #endif
171 /* Function call profile */
172 #ifdef CALL_PROFILE
173 #define PCALL_NUM 11
174 static int pcall[PCALL_NUM];
176 #define PCALL_ALL 0
177 #define PCALL_FUNCTION 1
178 #define PCALL_FAST_FUNCTION 2
179 #define PCALL_FASTER_FUNCTION 3
180 #define PCALL_METHOD 4
181 #define PCALL_BOUND_METHOD 5
182 #define PCALL_CFUNCTION 6
183 #define PCALL_TYPE 7
184 #define PCALL_GENERATOR 8
185 #define PCALL_OTHER 9
186 #define PCALL_POP 10
188 /* Notes about the statistics
190 PCALL_FAST stats
192 FAST_FUNCTION means no argument tuple needs to be created.
193 FASTER_FUNCTION means that the fast-path frame setup code is used.
195 If there is a method call where the call can be optimized by changing
196 the argument tuple and calling the function directly, it gets recorded
197 twice.
199 As a result, the relationship among the statistics appears to be
200 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
201 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
202 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
203 PCALL_METHOD > PCALL_BOUND_METHOD
206 #define PCALL(POS) pcall[POS]++
208 PyObject *
209 PyEval_GetCallStats(PyObject *self)
211 return Py_BuildValue("iiiiiiiiiii",
212 pcall[0], pcall[1], pcall[2], pcall[3],
213 pcall[4], pcall[5], pcall[6], pcall[7],
214 pcall[8], pcall[9], pcall[10]);
216 #else
217 #define PCALL(O)
219 PyObject *
220 PyEval_GetCallStats(PyObject *self)
222 Py_INCREF(Py_None);
223 return Py_None;
225 #endif
228 #ifdef WITH_THREAD
230 #ifdef HAVE_ERRNO_H
231 #include <errno.h>
232 #endif
233 #include "pythread.h"
235 static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
236 static PyThread_type_lock pending_lock = 0; /* for pending calls */
237 static long main_thread = 0;
240 PyEval_ThreadsInitialized(void)
242 return interpreter_lock != 0;
245 void
246 PyEval_InitThreads(void)
248 if (interpreter_lock)
249 return;
250 interpreter_lock = PyThread_allocate_lock();
251 PyThread_acquire_lock(interpreter_lock, 1);
252 main_thread = PyThread_get_thread_ident();
255 void
256 PyEval_AcquireLock(void)
258 PyThread_acquire_lock(interpreter_lock, 1);
261 void
262 PyEval_ReleaseLock(void)
264 PyThread_release_lock(interpreter_lock);
267 void
268 PyEval_AcquireThread(PyThreadState *tstate)
270 if (tstate == NULL)
271 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
272 /* Check someone has called PyEval_InitThreads() to create the lock */
273 assert(interpreter_lock);
274 PyThread_acquire_lock(interpreter_lock, 1);
275 if (PyThreadState_Swap(tstate) != NULL)
276 Py_FatalError(
277 "PyEval_AcquireThread: non-NULL old thread state");
280 void
281 PyEval_ReleaseThread(PyThreadState *tstate)
283 if (tstate == NULL)
284 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
285 if (PyThreadState_Swap(NULL) != tstate)
286 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
287 PyThread_release_lock(interpreter_lock);
290 /* This function is called from PyOS_AfterFork to ensure that newly
291 created child processes don't hold locks referring to threads which
292 are not running in the child process. (This could also be done using
293 pthread_atfork mechanism, at least for the pthreads implementation.) */
295 void
296 PyEval_ReInitThreads(void)
298 PyObject *threading, *result;
299 PyThreadState *tstate;
301 if (!interpreter_lock)
302 return;
303 /*XXX Can't use PyThread_free_lock here because it does too
304 much error-checking. Doing this cleanly would require
305 adding a new function to each thread_*.h. Instead, just
306 create a new lock and waste a little bit of memory */
307 interpreter_lock = PyThread_allocate_lock();
308 pending_lock = PyThread_allocate_lock();
309 PyThread_acquire_lock(interpreter_lock, 1);
310 main_thread = PyThread_get_thread_ident();
312 /* Update the threading module with the new state.
314 tstate = PyThreadState_GET();
315 threading = PyMapping_GetItemString(tstate->interp->modules,
316 "threading");
317 if (threading == NULL) {
318 /* threading not imported */
319 PyErr_Clear();
320 return;
322 result = PyObject_CallMethod(threading, "_after_fork", NULL);
323 if (result == NULL)
324 PyErr_WriteUnraisable(threading);
325 else
326 Py_DECREF(result);
327 Py_DECREF(threading);
329 #endif
331 /* Functions save_thread and restore_thread are always defined so
332 dynamically loaded modules needn't be compiled separately for use
333 with and without threads: */
335 PyThreadState *
336 PyEval_SaveThread(void)
338 PyThreadState *tstate = PyThreadState_Swap(NULL);
339 if (tstate == NULL)
340 Py_FatalError("PyEval_SaveThread: NULL tstate");
341 #ifdef WITH_THREAD
342 if (interpreter_lock)
343 PyThread_release_lock(interpreter_lock);
344 #endif
345 return tstate;
348 void
349 PyEval_RestoreThread(PyThreadState *tstate)
351 if (tstate == NULL)
352 Py_FatalError("PyEval_RestoreThread: NULL tstate");
353 #ifdef WITH_THREAD
354 if (interpreter_lock) {
355 int err = errno;
356 PyThread_acquire_lock(interpreter_lock, 1);
357 errno = err;
359 #endif
360 PyThreadState_Swap(tstate);
364 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
365 signal handlers or Mac I/O completion routines) can schedule calls
366 to a function to be called synchronously.
367 The synchronous function is called with one void* argument.
368 It should return 0 for success or -1 for failure -- failure should
369 be accompanied by an exception.
371 If registry succeeds, the registry function returns 0; if it fails
372 (e.g. due to too many pending calls) it returns -1 (without setting
373 an exception condition).
375 Note that because registry may occur from within signal handlers,
376 or other asynchronous events, calling malloc() is unsafe!
378 #ifdef WITH_THREAD
379 Any thread can schedule pending calls, but only the main thread
380 will execute them.
381 There is no facility to schedule calls to a particular thread, but
382 that should be easy to change, should that ever be required. In
383 that case, the static variables here should go into the python
384 threadstate.
385 #endif
388 #ifdef WITH_THREAD
390 /* The WITH_THREAD implementation is thread-safe. It allows
391 scheduling to be made from any thread, and even from an executing
392 callback.
395 #define NPENDINGCALLS 32
396 static struct {
397 int (*func)(void *);
398 void *arg;
399 } pendingcalls[NPENDINGCALLS];
400 static int pendingfirst = 0;
401 static int pendinglast = 0;
402 static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
403 static char pendingbusy = 0;
406 Py_AddPendingCall(int (*func)(void *), void *arg)
408 int i, j, result=0;
409 PyThread_type_lock lock = pending_lock;
411 /* try a few times for the lock. Since this mechanism is used
412 * for signal handling (on the main thread), there is a (slim)
413 * chance that a signal is delivered on the same thread while we
414 * hold the lock during the Py_MakePendingCalls() function.
415 * This avoids a deadlock in that case.
416 * Note that signals can be delivered on any thread. In particular,
417 * on Windows, a SIGINT is delivered on a system-created worker
418 * thread.
419 * We also check for lock being NULL, in the unlikely case that
420 * this function is called before any bytecode evaluation takes place.
422 if (lock != NULL) {
423 for (i = 0; i<100; i++) {
424 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
425 break;
427 if (i == 100)
428 return -1;
431 i = pendinglast;
432 j = (i + 1) % NPENDINGCALLS;
433 if (j == pendingfirst) {
434 result = -1; /* Queue full */
435 } else {
436 pendingcalls[i].func = func;
437 pendingcalls[i].arg = arg;
438 pendinglast = j;
440 /* signal main loop */
441 _Py_Ticker = 0;
442 pendingcalls_to_do = 1;
443 if (lock != NULL)
444 PyThread_release_lock(lock);
445 return result;
449 Py_MakePendingCalls(void)
451 int i;
452 int r = 0;
454 if (!pending_lock) {
455 /* initial allocation of the lock */
456 pending_lock = PyThread_allocate_lock();
457 if (pending_lock == NULL)
458 return -1;
461 /* only service pending calls on main thread */
462 if (main_thread && PyThread_get_thread_ident() != main_thread)
463 return 0;
464 /* don't perform recursive pending calls */
465 if (pendingbusy)
466 return 0;
467 pendingbusy = 1;
468 /* perform a bounded number of calls, in case of recursion */
469 for (i=0; i<NPENDINGCALLS; i++) {
470 int j;
471 int (*func)(void *);
472 void *arg = NULL;
474 /* pop one item off the queue while holding the lock */
475 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
476 j = pendingfirst;
477 if (j == pendinglast) {
478 func = NULL; /* Queue empty */
479 } else {
480 func = pendingcalls[j].func;
481 arg = pendingcalls[j].arg;
482 pendingfirst = (j + 1) % NPENDINGCALLS;
484 pendingcalls_to_do = pendingfirst != pendinglast;
485 PyThread_release_lock(pending_lock);
486 /* having released the lock, perform the callback */
487 if (func == NULL)
488 break;
489 r = func(arg);
490 if (r)
491 break;
493 pendingbusy = 0;
494 return r;
497 #else /* if ! defined WITH_THREAD */
500 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
501 This code is used for signal handling in python that isn't built
502 with WITH_THREAD.
503 Don't use this implementation when Py_AddPendingCalls() can happen
504 on a different thread!
506 There are two possible race conditions:
507 (1) nested asynchronous calls to Py_AddPendingCall()
508 (2) AddPendingCall() calls made while pending calls are being processed.
510 (1) is very unlikely because typically signal delivery
511 is blocked during signal handling. So it should be impossible.
512 (2) is a real possibility.
513 The current code is safe against (2), but not against (1).
514 The safety against (2) is derived from the fact that only one
515 thread is present, interrupted by signals, and that the critical
516 section is protected with the "busy" variable. On Windows, which
517 delivers SIGINT on a system thread, this does not hold and therefore
518 Windows really shouldn't use this version.
519 The two threads could theoretically wiggle around the "busy" variable.
522 #define NPENDINGCALLS 32
523 static struct {
524 int (*func)(void *);
525 void *arg;
526 } pendingcalls[NPENDINGCALLS];
527 static volatile int pendingfirst = 0;
528 static volatile int pendinglast = 0;
529 static volatile int pendingcalls_to_do = 0;
532 Py_AddPendingCall(int (*func)(void *), void *arg)
534 static volatile int busy = 0;
535 int i, j;
536 /* XXX Begin critical section */
537 if (busy)
538 return -1;
539 busy = 1;
540 i = pendinglast;
541 j = (i + 1) % NPENDINGCALLS;
542 if (j == pendingfirst) {
543 busy = 0;
544 return -1; /* Queue full */
546 pendingcalls[i].func = func;
547 pendingcalls[i].arg = arg;
548 pendinglast = j;
550 _Py_Ticker = 0;
551 pendingcalls_to_do = 1; /* Signal main loop */
552 busy = 0;
553 /* XXX End critical section */
554 return 0;
558 Py_MakePendingCalls(void)
560 static int busy = 0;
561 if (busy)
562 return 0;
563 busy = 1;
564 pendingcalls_to_do = 0;
565 for (;;) {
566 int i;
567 int (*func)(void *);
568 void *arg;
569 i = pendingfirst;
570 if (i == pendinglast)
571 break; /* Queue empty */
572 func = pendingcalls[i].func;
573 arg = pendingcalls[i].arg;
574 pendingfirst = (i + 1) % NPENDINGCALLS;
575 if (func(arg) < 0) {
576 busy = 0;
577 pendingcalls_to_do = 1; /* We're not done yet */
578 return -1;
581 busy = 0;
582 return 0;
585 #endif /* WITH_THREAD */
588 /* The interpreter's recursion limit */
590 #ifndef Py_DEFAULT_RECURSION_LIMIT
591 #define Py_DEFAULT_RECURSION_LIMIT 1000
592 #endif
593 static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
594 int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
597 Py_GetRecursionLimit(void)
599 return recursion_limit;
602 void
603 Py_SetRecursionLimit(int new_limit)
605 recursion_limit = new_limit;
606 _Py_CheckRecursionLimit = recursion_limit;
609 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
610 if the recursion_depth reaches _Py_CheckRecursionLimit.
611 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
612 to guarantee that _Py_CheckRecursiveCall() is regularly called.
613 Without USE_STACKCHECK, there is no need for this. */
615 _Py_CheckRecursiveCall(char *where)
617 PyThreadState *tstate = PyThreadState_GET();
619 #ifdef USE_STACKCHECK
620 if (PyOS_CheckStack()) {
621 --tstate->recursion_depth;
622 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
623 return -1;
625 #endif
626 if (tstate->recursion_depth > recursion_limit) {
627 --tstate->recursion_depth;
628 PyErr_Format(PyExc_RuntimeError,
629 "maximum recursion depth exceeded%s",
630 where);
631 return -1;
633 _Py_CheckRecursionLimit = recursion_limit;
634 return 0;
637 /* Status code for main loop (reason for stack unwind) */
638 enum why_code {
639 WHY_NOT = 0x0001, /* No error */
640 WHY_EXCEPTION = 0x0002, /* Exception occurred */
641 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
642 WHY_RETURN = 0x0008, /* 'return' statement */
643 WHY_BREAK = 0x0010, /* 'break' statement */
644 WHY_CONTINUE = 0x0020, /* 'continue' statement */
645 WHY_YIELD = 0x0040 /* 'yield' operator */
648 static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
649 static int unpack_iterable(PyObject *, int, PyObject **);
651 /* Records whether tracing is on for any thread. Counts the number of
652 threads for which tstate->c_tracefunc is non-NULL, so if the value
653 is 0, we know we don't have to check this thread's c_tracefunc.
654 This speeds up the if statement in PyEval_EvalFrameEx() after
655 fast_next_opcode*/
656 static int _Py_TracingPossible = 0;
658 /* for manipulating the thread switch and periodic "stuff" - used to be
659 per thread, now just a pair o' globals */
660 int _Py_CheckInterval = 100;
661 volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
663 PyObject *
664 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
666 return PyEval_EvalCodeEx(co,
667 globals, locals,
668 (PyObject **)NULL, 0,
669 (PyObject **)NULL, 0,
670 (PyObject **)NULL, 0,
671 NULL);
675 /* Interpreter main loop */
677 PyObject *
678 PyEval_EvalFrame(PyFrameObject *f) {
679 /* This is for backward compatibility with extension modules that
680 used this API; core interpreter code should call
681 PyEval_EvalFrameEx() */
682 return PyEval_EvalFrameEx(f, 0);
685 PyObject *
686 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
688 #ifdef DXPAIRS
689 int lastopcode = 0;
690 #endif
691 register PyObject **stack_pointer; /* Next free slot in value stack */
692 register unsigned char *next_instr;
693 register int opcode; /* Current opcode */
694 register int oparg; /* Current opcode argument, if any */
695 register enum why_code why; /* Reason for block stack unwind */
696 register int err; /* Error status -- nonzero if error */
697 register PyObject *x; /* Result object -- NULL if error */
698 register PyObject *v; /* Temporary objects popped off stack */
699 register PyObject *w;
700 register PyObject *u;
701 register PyObject *t;
702 register PyObject *stream = NULL; /* for PRINT opcodes */
703 register PyObject **fastlocals, **freevars;
704 PyObject *retval = NULL; /* Return value */
705 PyThreadState *tstate = PyThreadState_GET();
706 PyCodeObject *co;
708 /* when tracing we set things up so that
710 not (instr_lb <= current_bytecode_offset < instr_ub)
712 is true when the line being executed has changed. The
713 initial values are such as to make this false the first
714 time it is tested. */
715 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
717 unsigned char *first_instr;
718 PyObject *names;
719 PyObject *consts;
720 #if defined(Py_DEBUG) || defined(LLTRACE)
721 /* Make it easier to find out where we are with a debugger */
722 char *filename;
723 #endif
725 /* Tuple access macros */
727 #ifndef Py_DEBUG
728 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
729 #else
730 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
731 #endif
733 #ifdef WITH_TSC
734 /* Use Pentium timestamp counter to mark certain events:
735 inst0 -- beginning of switch statement for opcode dispatch
736 inst1 -- end of switch statement (may be skipped)
737 loop0 -- the top of the mainloop
738 loop1 -- place where control returns again to top of mainloop
739 (may be skipped)
740 intr1 -- beginning of long interruption
741 intr2 -- end of long interruption
743 Many opcodes call out to helper C functions. In some cases, the
744 time in those functions should be counted towards the time for the
745 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
746 calls another Python function; there's no point in charge all the
747 bytecode executed by the called function to the caller.
749 It's hard to make a useful judgement statically. In the presence
750 of operator overloading, it's impossible to tell if a call will
751 execute new Python code or not.
753 It's a case-by-case judgement. I'll use intr1 for the following
754 cases:
756 EXEC_STMT
757 IMPORT_STAR
758 IMPORT_FROM
759 CALL_FUNCTION (and friends)
762 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
763 int ticked = 0;
765 READ_TIMESTAMP(inst0);
766 READ_TIMESTAMP(inst1);
767 READ_TIMESTAMP(loop0);
768 READ_TIMESTAMP(loop1);
770 /* shut up the compiler */
771 opcode = 0;
772 #endif
774 /* Code access macros */
776 #define INSTR_OFFSET() ((int)(next_instr - first_instr))
777 #define NEXTOP() (*next_instr++)
778 #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
779 #define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
780 #define JUMPTO(x) (next_instr = first_instr + (x))
781 #define JUMPBY(x) (next_instr += (x))
783 /* OpCode prediction macros
784 Some opcodes tend to come in pairs thus making it possible to
785 predict the second code when the first is run. For example,
786 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
787 followed by STORE_FAST or UNPACK_SEQUENCE.
789 Verifying the prediction costs a single high-speed test of a register
790 variable against a constant. If the pairing was good, then the
791 processor's own internal branch predication has a high likelihood of
792 success, resulting in a nearly zero-overhead transition to the
793 next opcode. A successful prediction saves a trip through the eval-loop
794 including its two unpredictable branches, the HAS_ARG test and the
795 switch-case. Combined with the processor's internal branch prediction,
796 a successful PREDICT has the effect of making the two opcodes run as if
797 they were a single new opcode with the bodies combined.
799 If collecting opcode statistics, your choices are to either keep the
800 predictions turned-on and interpret the results as if some opcodes
801 had been combined or turn-off predictions so that the opcode frequency
802 counter updates for both opcodes.
805 #ifdef DYNAMIC_EXECUTION_PROFILE
806 #define PREDICT(op) if (0) goto PRED_##op
807 #else
808 #define PREDICT(op) if (*next_instr == op) goto PRED_##op
809 #endif
811 #define PREDICTED(op) PRED_##op: next_instr++
812 #define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
814 /* Stack manipulation macros */
816 /* The stack can grow at most MAXINT deep, as co_nlocals and
817 co_stacksize are ints. */
818 #define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
819 #define EMPTY() (STACK_LEVEL() == 0)
820 #define TOP() (stack_pointer[-1])
821 #define SECOND() (stack_pointer[-2])
822 #define THIRD() (stack_pointer[-3])
823 #define FOURTH() (stack_pointer[-4])
824 #define PEEK(n) (stack_pointer[-(n)])
825 #define SET_TOP(v) (stack_pointer[-1] = (v))
826 #define SET_SECOND(v) (stack_pointer[-2] = (v))
827 #define SET_THIRD(v) (stack_pointer[-3] = (v))
828 #define SET_FOURTH(v) (stack_pointer[-4] = (v))
829 #define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
830 #define BASIC_STACKADJ(n) (stack_pointer += n)
831 #define BASIC_PUSH(v) (*stack_pointer++ = (v))
832 #define BASIC_POP() (*--stack_pointer)
834 #ifdef LLTRACE
835 #define PUSH(v) { (void)(BASIC_PUSH(v), \
836 lltrace && prtrace(TOP(), "push")); \
837 assert(STACK_LEVEL() <= co->co_stacksize); }
838 #define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
839 BASIC_POP())
840 #define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
841 lltrace && prtrace(TOP(), "stackadj")); \
842 assert(STACK_LEVEL() <= co->co_stacksize); }
843 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
844 prtrace((STACK_POINTER)[-1], "ext_pop")), \
845 *--(STACK_POINTER))
846 #else
847 #define PUSH(v) BASIC_PUSH(v)
848 #define POP() BASIC_POP()
849 #define STACKADJ(n) BASIC_STACKADJ(n)
850 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
851 #endif
853 /* Local variable macros */
855 #define GETLOCAL(i) (fastlocals[i])
857 /* The SETLOCAL() macro must not DECREF the local variable in-place and
858 then store the new value; it must copy the old value to a temporary
859 value, then store the new value, and then DECREF the temporary value.
860 This is because it is possible that during the DECREF the frame is
861 accessed by other code (e.g. a __del__ method or gc.collect()) and the
862 variable would be pointing to already-freed memory. */
863 #define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
864 GETLOCAL(i) = value; \
865 Py_XDECREF(tmp); } while (0)
867 /* Start of code */
869 if (f == NULL)
870 return NULL;
872 /* push frame */
873 if (Py_EnterRecursiveCall(""))
874 return NULL;
876 tstate->frame = f;
878 if (tstate->use_tracing) {
879 if (tstate->c_tracefunc != NULL) {
880 /* tstate->c_tracefunc, if defined, is a
881 function that will be called on *every* entry
882 to a code block. Its return value, if not
883 None, is a function that will be called at
884 the start of each executed line of code.
885 (Actually, the function must return itself
886 in order to continue tracing.) The trace
887 functions are called with three arguments:
888 a pointer to the current frame, a string
889 indicating why the function is called, and
890 an argument which depends on the situation.
891 The global trace function is also called
892 whenever an exception is detected. */
893 if (call_trace_protected(tstate->c_tracefunc,
894 tstate->c_traceobj,
895 f, PyTrace_CALL, Py_None)) {
896 /* Trace function raised an error */
897 goto exit_eval_frame;
900 if (tstate->c_profilefunc != NULL) {
901 /* Similar for c_profilefunc, except it needn't
902 return itself and isn't called for "line" events */
903 if (call_trace_protected(tstate->c_profilefunc,
904 tstate->c_profileobj,
905 f, PyTrace_CALL, Py_None)) {
906 /* Profile function raised an error */
907 goto exit_eval_frame;
912 co = f->f_code;
913 names = co->co_names;
914 consts = co->co_consts;
915 fastlocals = f->f_localsplus;
916 freevars = f->f_localsplus + co->co_nlocals;
917 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
918 /* An explanation is in order for the next line.
920 f->f_lasti now refers to the index of the last instruction
921 executed. You might think this was obvious from the name, but
922 this wasn't always true before 2.3! PyFrame_New now sets
923 f->f_lasti to -1 (i.e. the index *before* the first instruction)
924 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
925 does work. Promise.
927 When the PREDICT() macros are enabled, some opcode pairs follow in
928 direct succession without updating f->f_lasti. A successful
929 prediction effectively links the two codes together as if they
930 were a single new opcode; accordingly,f->f_lasti will point to
931 the first code in the pair (for instance, GET_ITER followed by
932 FOR_ITER is effectively a single opcode and f->f_lasti will point
933 at to the beginning of the combined pair.)
935 next_instr = first_instr + f->f_lasti + 1;
936 stack_pointer = f->f_stacktop;
937 assert(stack_pointer != NULL);
938 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
940 #ifdef LLTRACE
941 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
942 #endif
943 #if defined(Py_DEBUG) || defined(LLTRACE)
944 filename = PyString_AsString(co->co_filename);
945 #endif
947 why = WHY_NOT;
948 err = 0;
949 x = Py_None; /* Not a reference, just anything non-NULL */
950 w = NULL;
952 if (throwflag) { /* support for generator.throw() */
953 why = WHY_EXCEPTION;
954 goto on_error;
957 for (;;) {
958 #ifdef WITH_TSC
959 if (inst1 == 0) {
960 /* Almost surely, the opcode executed a break
961 or a continue, preventing inst1 from being set
962 on the way out of the loop.
964 READ_TIMESTAMP(inst1);
965 loop1 = inst1;
967 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
968 intr0, intr1);
969 ticked = 0;
970 inst1 = 0;
971 intr0 = 0;
972 intr1 = 0;
973 READ_TIMESTAMP(loop0);
974 #endif
975 assert(stack_pointer >= f->f_valuestack); /* else underflow */
976 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
978 /* Do periodic things. Doing this every time through
979 the loop would add too much overhead, so we do it
980 only every Nth instruction. We also do it if
981 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
982 event needs attention (e.g. a signal handler or
983 async I/O handler); see Py_AddPendingCall() and
984 Py_MakePendingCalls() above. */
986 if (--_Py_Ticker < 0) {
987 if (*next_instr == SETUP_FINALLY) {
988 /* Make the last opcode before
989 a try: finally: block uninterruptable. */
990 goto fast_next_opcode;
992 _Py_Ticker = _Py_CheckInterval;
993 tstate->tick_counter++;
994 #ifdef WITH_TSC
995 ticked = 1;
996 #endif
997 if (pendingcalls_to_do) {
998 if (Py_MakePendingCalls() < 0) {
999 why = WHY_EXCEPTION;
1000 goto on_error;
1002 if (pendingcalls_to_do)
1003 /* MakePendingCalls() didn't succeed.
1004 Force early re-execution of this
1005 "periodic" code, possibly after
1006 a thread switch */
1007 _Py_Ticker = 0;
1009 #ifdef WITH_THREAD
1010 if (interpreter_lock) {
1011 /* Give another thread a chance */
1013 if (PyThreadState_Swap(NULL) != tstate)
1014 Py_FatalError("ceval: tstate mix-up");
1015 PyThread_release_lock(interpreter_lock);
1017 /* Other threads may run now */
1019 PyThread_acquire_lock(interpreter_lock, 1);
1020 if (PyThreadState_Swap(tstate) != NULL)
1021 Py_FatalError("ceval: orphan tstate");
1023 /* Check for thread interrupts */
1025 if (tstate->async_exc != NULL) {
1026 x = tstate->async_exc;
1027 tstate->async_exc = NULL;
1028 PyErr_SetNone(x);
1029 Py_DECREF(x);
1030 why = WHY_EXCEPTION;
1031 goto on_error;
1034 #endif
1037 fast_next_opcode:
1038 f->f_lasti = INSTR_OFFSET();
1040 /* line-by-line tracing support */
1042 if (_Py_TracingPossible &&
1043 tstate->c_tracefunc != NULL && !tstate->tracing) {
1044 /* see maybe_call_line_trace
1045 for expository comments */
1046 f->f_stacktop = stack_pointer;
1048 err = maybe_call_line_trace(tstate->c_tracefunc,
1049 tstate->c_traceobj,
1050 f, &instr_lb, &instr_ub,
1051 &instr_prev);
1052 /* Reload possibly changed frame fields */
1053 JUMPTO(f->f_lasti);
1054 if (f->f_stacktop != NULL) {
1055 stack_pointer = f->f_stacktop;
1056 f->f_stacktop = NULL;
1058 if (err) {
1059 /* trace function raised an exception */
1060 goto on_error;
1064 /* Extract opcode and argument */
1066 opcode = NEXTOP();
1067 oparg = 0; /* allows oparg to be stored in a register because
1068 it doesn't have to be remembered across a full loop */
1069 if (HAS_ARG(opcode))
1070 oparg = NEXTARG();
1071 dispatch_opcode:
1072 #ifdef DYNAMIC_EXECUTION_PROFILE
1073 #ifdef DXPAIRS
1074 dxpairs[lastopcode][opcode]++;
1075 lastopcode = opcode;
1076 #endif
1077 dxp[opcode]++;
1078 #endif
1080 #ifdef LLTRACE
1081 /* Instruction tracing */
1083 if (lltrace) {
1084 if (HAS_ARG(opcode)) {
1085 printf("%d: %d, %d\n",
1086 f->f_lasti, opcode, oparg);
1088 else {
1089 printf("%d: %d\n",
1090 f->f_lasti, opcode);
1093 #endif
1095 /* Main switch on opcode */
1096 READ_TIMESTAMP(inst0);
1098 switch (opcode) {
1100 /* BEWARE!
1101 It is essential that any operation that fails sets either
1102 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1103 and that no operation that succeeds does this! */
1105 /* case STOP_CODE: this is an error! */
1107 case NOP:
1108 goto fast_next_opcode;
1110 case LOAD_FAST:
1111 x = GETLOCAL(oparg);
1112 if (x != NULL) {
1113 Py_INCREF(x);
1114 PUSH(x);
1115 goto fast_next_opcode;
1117 format_exc_check_arg(PyExc_UnboundLocalError,
1118 UNBOUNDLOCAL_ERROR_MSG,
1119 PyTuple_GetItem(co->co_varnames, oparg));
1120 break;
1122 case LOAD_CONST:
1123 x = GETITEM(consts, oparg);
1124 Py_INCREF(x);
1125 PUSH(x);
1126 goto fast_next_opcode;
1128 PREDICTED_WITH_ARG(STORE_FAST);
1129 case STORE_FAST:
1130 v = POP();
1131 SETLOCAL(oparg, v);
1132 goto fast_next_opcode;
1134 case POP_TOP:
1135 v = POP();
1136 Py_DECREF(v);
1137 goto fast_next_opcode;
1139 case ROT_TWO:
1140 v = TOP();
1141 w = SECOND();
1142 SET_TOP(w);
1143 SET_SECOND(v);
1144 goto fast_next_opcode;
1146 case ROT_THREE:
1147 v = TOP();
1148 w = SECOND();
1149 x = THIRD();
1150 SET_TOP(w);
1151 SET_SECOND(x);
1152 SET_THIRD(v);
1153 goto fast_next_opcode;
1155 case ROT_FOUR:
1156 u = TOP();
1157 v = SECOND();
1158 w = THIRD();
1159 x = FOURTH();
1160 SET_TOP(v);
1161 SET_SECOND(w);
1162 SET_THIRD(x);
1163 SET_FOURTH(u);
1164 goto fast_next_opcode;
1166 case DUP_TOP:
1167 v = TOP();
1168 Py_INCREF(v);
1169 PUSH(v);
1170 goto fast_next_opcode;
1172 case DUP_TOPX:
1173 if (oparg == 2) {
1174 x = TOP();
1175 Py_INCREF(x);
1176 w = SECOND();
1177 Py_INCREF(w);
1178 STACKADJ(2);
1179 SET_TOP(x);
1180 SET_SECOND(w);
1181 goto fast_next_opcode;
1182 } else if (oparg == 3) {
1183 x = TOP();
1184 Py_INCREF(x);
1185 w = SECOND();
1186 Py_INCREF(w);
1187 v = THIRD();
1188 Py_INCREF(v);
1189 STACKADJ(3);
1190 SET_TOP(x);
1191 SET_SECOND(w);
1192 SET_THIRD(v);
1193 goto fast_next_opcode;
1195 Py_FatalError("invalid argument to DUP_TOPX"
1196 " (bytecode corruption?)");
1197 /* Never returns, so don't bother to set why. */
1198 break;
1200 case UNARY_POSITIVE:
1201 v = TOP();
1202 x = PyNumber_Positive(v);
1203 Py_DECREF(v);
1204 SET_TOP(x);
1205 if (x != NULL) continue;
1206 break;
1208 case UNARY_NEGATIVE:
1209 v = TOP();
1210 x = PyNumber_Negative(v);
1211 Py_DECREF(v);
1212 SET_TOP(x);
1213 if (x != NULL) continue;
1214 break;
1216 case UNARY_NOT:
1217 v = TOP();
1218 err = PyObject_IsTrue(v);
1219 Py_DECREF(v);
1220 if (err == 0) {
1221 Py_INCREF(Py_True);
1222 SET_TOP(Py_True);
1223 continue;
1225 else if (err > 0) {
1226 Py_INCREF(Py_False);
1227 SET_TOP(Py_False);
1228 err = 0;
1229 continue;
1231 STACKADJ(-1);
1232 break;
1234 case UNARY_CONVERT:
1235 v = TOP();
1236 x = PyObject_Repr(v);
1237 Py_DECREF(v);
1238 SET_TOP(x);
1239 if (x != NULL) continue;
1240 break;
1242 case UNARY_INVERT:
1243 v = TOP();
1244 x = PyNumber_Invert(v);
1245 Py_DECREF(v);
1246 SET_TOP(x);
1247 if (x != NULL) continue;
1248 break;
1250 case BINARY_POWER:
1251 w = POP();
1252 v = TOP();
1253 x = PyNumber_Power(v, w, Py_None);
1254 Py_DECREF(v);
1255 Py_DECREF(w);
1256 SET_TOP(x);
1257 if (x != NULL) continue;
1258 break;
1260 case BINARY_MULTIPLY:
1261 w = POP();
1262 v = TOP();
1263 x = PyNumber_Multiply(v, w);
1264 Py_DECREF(v);
1265 Py_DECREF(w);
1266 SET_TOP(x);
1267 if (x != NULL) continue;
1268 break;
1270 case BINARY_DIVIDE:
1271 if (!_Py_QnewFlag) {
1272 w = POP();
1273 v = TOP();
1274 x = PyNumber_Divide(v, w);
1275 Py_DECREF(v);
1276 Py_DECREF(w);
1277 SET_TOP(x);
1278 if (x != NULL) continue;
1279 break;
1281 /* -Qnew is in effect: fall through to
1282 BINARY_TRUE_DIVIDE */
1283 case BINARY_TRUE_DIVIDE:
1284 w = POP();
1285 v = TOP();
1286 x = PyNumber_TrueDivide(v, w);
1287 Py_DECREF(v);
1288 Py_DECREF(w);
1289 SET_TOP(x);
1290 if (x != NULL) continue;
1291 break;
1293 case BINARY_FLOOR_DIVIDE:
1294 w = POP();
1295 v = TOP();
1296 x = PyNumber_FloorDivide(v, w);
1297 Py_DECREF(v);
1298 Py_DECREF(w);
1299 SET_TOP(x);
1300 if (x != NULL) continue;
1301 break;
1303 case BINARY_MODULO:
1304 w = POP();
1305 v = TOP();
1306 if (PyString_CheckExact(v))
1307 x = PyString_Format(v, w);
1308 else
1309 x = PyNumber_Remainder(v, w);
1310 Py_DECREF(v);
1311 Py_DECREF(w);
1312 SET_TOP(x);
1313 if (x != NULL) continue;
1314 break;
1316 case BINARY_ADD:
1317 w = POP();
1318 v = TOP();
1319 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1320 /* INLINE: int + int */
1321 register long a, b, i;
1322 a = PyInt_AS_LONG(v);
1323 b = PyInt_AS_LONG(w);
1324 i = a + b;
1325 if ((i^a) < 0 && (i^b) < 0)
1326 goto slow_add;
1327 x = PyInt_FromLong(i);
1329 else if (PyString_CheckExact(v) &&
1330 PyString_CheckExact(w)) {
1331 x = string_concatenate(v, w, f, next_instr);
1332 /* string_concatenate consumed the ref to v */
1333 goto skip_decref_vx;
1335 else {
1336 slow_add:
1337 x = PyNumber_Add(v, w);
1339 Py_DECREF(v);
1340 skip_decref_vx:
1341 Py_DECREF(w);
1342 SET_TOP(x);
1343 if (x != NULL) continue;
1344 break;
1346 case BINARY_SUBTRACT:
1347 w = POP();
1348 v = TOP();
1349 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1350 /* INLINE: int - int */
1351 register long a, b, i;
1352 a = PyInt_AS_LONG(v);
1353 b = PyInt_AS_LONG(w);
1354 i = a - b;
1355 if ((i^a) < 0 && (i^~b) < 0)
1356 goto slow_sub;
1357 x = PyInt_FromLong(i);
1359 else {
1360 slow_sub:
1361 x = PyNumber_Subtract(v, w);
1363 Py_DECREF(v);
1364 Py_DECREF(w);
1365 SET_TOP(x);
1366 if (x != NULL) continue;
1367 break;
1369 case BINARY_SUBSCR:
1370 w = POP();
1371 v = TOP();
1372 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1373 /* INLINE: list[int] */
1374 Py_ssize_t i = PyInt_AsSsize_t(w);
1375 if (i < 0)
1376 i += PyList_GET_SIZE(v);
1377 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1378 x = PyList_GET_ITEM(v, i);
1379 Py_INCREF(x);
1381 else
1382 goto slow_get;
1384 else
1385 slow_get:
1386 x = PyObject_GetItem(v, w);
1387 Py_DECREF(v);
1388 Py_DECREF(w);
1389 SET_TOP(x);
1390 if (x != NULL) continue;
1391 break;
1393 case BINARY_LSHIFT:
1394 w = POP();
1395 v = TOP();
1396 x = PyNumber_Lshift(v, w);
1397 Py_DECREF(v);
1398 Py_DECREF(w);
1399 SET_TOP(x);
1400 if (x != NULL) continue;
1401 break;
1403 case BINARY_RSHIFT:
1404 w = POP();
1405 v = TOP();
1406 x = PyNumber_Rshift(v, w);
1407 Py_DECREF(v);
1408 Py_DECREF(w);
1409 SET_TOP(x);
1410 if (x != NULL) continue;
1411 break;
1413 case BINARY_AND:
1414 w = POP();
1415 v = TOP();
1416 x = PyNumber_And(v, w);
1417 Py_DECREF(v);
1418 Py_DECREF(w);
1419 SET_TOP(x);
1420 if (x != NULL) continue;
1421 break;
1423 case BINARY_XOR:
1424 w = POP();
1425 v = TOP();
1426 x = PyNumber_Xor(v, w);
1427 Py_DECREF(v);
1428 Py_DECREF(w);
1429 SET_TOP(x);
1430 if (x != NULL) continue;
1431 break;
1433 case BINARY_OR:
1434 w = POP();
1435 v = TOP();
1436 x = PyNumber_Or(v, w);
1437 Py_DECREF(v);
1438 Py_DECREF(w);
1439 SET_TOP(x);
1440 if (x != NULL) continue;
1441 break;
1443 case LIST_APPEND:
1444 w = POP();
1445 v = PEEK(oparg);
1446 err = PyList_Append(v, w);
1447 Py_DECREF(w);
1448 if (err == 0) {
1449 PREDICT(JUMP_ABSOLUTE);
1450 continue;
1452 break;
1454 case INPLACE_POWER:
1455 w = POP();
1456 v = TOP();
1457 x = PyNumber_InPlacePower(v, w, Py_None);
1458 Py_DECREF(v);
1459 Py_DECREF(w);
1460 SET_TOP(x);
1461 if (x != NULL) continue;
1462 break;
1464 case INPLACE_MULTIPLY:
1465 w = POP();
1466 v = TOP();
1467 x = PyNumber_InPlaceMultiply(v, w);
1468 Py_DECREF(v);
1469 Py_DECREF(w);
1470 SET_TOP(x);
1471 if (x != NULL) continue;
1472 break;
1474 case INPLACE_DIVIDE:
1475 if (!_Py_QnewFlag) {
1476 w = POP();
1477 v = TOP();
1478 x = PyNumber_InPlaceDivide(v, w);
1479 Py_DECREF(v);
1480 Py_DECREF(w);
1481 SET_TOP(x);
1482 if (x != NULL) continue;
1483 break;
1485 /* -Qnew is in effect: fall through to
1486 INPLACE_TRUE_DIVIDE */
1487 case INPLACE_TRUE_DIVIDE:
1488 w = POP();
1489 v = TOP();
1490 x = PyNumber_InPlaceTrueDivide(v, w);
1491 Py_DECREF(v);
1492 Py_DECREF(w);
1493 SET_TOP(x);
1494 if (x != NULL) continue;
1495 break;
1497 case INPLACE_FLOOR_DIVIDE:
1498 w = POP();
1499 v = TOP();
1500 x = PyNumber_InPlaceFloorDivide(v, w);
1501 Py_DECREF(v);
1502 Py_DECREF(w);
1503 SET_TOP(x);
1504 if (x != NULL) continue;
1505 break;
1507 case INPLACE_MODULO:
1508 w = POP();
1509 v = TOP();
1510 x = PyNumber_InPlaceRemainder(v, w);
1511 Py_DECREF(v);
1512 Py_DECREF(w);
1513 SET_TOP(x);
1514 if (x != NULL) continue;
1515 break;
1517 case INPLACE_ADD:
1518 w = POP();
1519 v = TOP();
1520 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1521 /* INLINE: int + int */
1522 register long a, b, i;
1523 a = PyInt_AS_LONG(v);
1524 b = PyInt_AS_LONG(w);
1525 i = a + b;
1526 if ((i^a) < 0 && (i^b) < 0)
1527 goto slow_iadd;
1528 x = PyInt_FromLong(i);
1530 else if (PyString_CheckExact(v) &&
1531 PyString_CheckExact(w)) {
1532 x = string_concatenate(v, w, f, next_instr);
1533 /* string_concatenate consumed the ref to v */
1534 goto skip_decref_v;
1536 else {
1537 slow_iadd:
1538 x = PyNumber_InPlaceAdd(v, w);
1540 Py_DECREF(v);
1541 skip_decref_v:
1542 Py_DECREF(w);
1543 SET_TOP(x);
1544 if (x != NULL) continue;
1545 break;
1547 case INPLACE_SUBTRACT:
1548 w = POP();
1549 v = TOP();
1550 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1551 /* INLINE: int - int */
1552 register long a, b, i;
1553 a = PyInt_AS_LONG(v);
1554 b = PyInt_AS_LONG(w);
1555 i = a - b;
1556 if ((i^a) < 0 && (i^~b) < 0)
1557 goto slow_isub;
1558 x = PyInt_FromLong(i);
1560 else {
1561 slow_isub:
1562 x = PyNumber_InPlaceSubtract(v, w);
1564 Py_DECREF(v);
1565 Py_DECREF(w);
1566 SET_TOP(x);
1567 if (x != NULL) continue;
1568 break;
1570 case INPLACE_LSHIFT:
1571 w = POP();
1572 v = TOP();
1573 x = PyNumber_InPlaceLshift(v, w);
1574 Py_DECREF(v);
1575 Py_DECREF(w);
1576 SET_TOP(x);
1577 if (x != NULL) continue;
1578 break;
1580 case INPLACE_RSHIFT:
1581 w = POP();
1582 v = TOP();
1583 x = PyNumber_InPlaceRshift(v, w);
1584 Py_DECREF(v);
1585 Py_DECREF(w);
1586 SET_TOP(x);
1587 if (x != NULL) continue;
1588 break;
1590 case INPLACE_AND:
1591 w = POP();
1592 v = TOP();
1593 x = PyNumber_InPlaceAnd(v, w);
1594 Py_DECREF(v);
1595 Py_DECREF(w);
1596 SET_TOP(x);
1597 if (x != NULL) continue;
1598 break;
1600 case INPLACE_XOR:
1601 w = POP();
1602 v = TOP();
1603 x = PyNumber_InPlaceXor(v, w);
1604 Py_DECREF(v);
1605 Py_DECREF(w);
1606 SET_TOP(x);
1607 if (x != NULL) continue;
1608 break;
1610 case INPLACE_OR:
1611 w = POP();
1612 v = TOP();
1613 x = PyNumber_InPlaceOr(v, w);
1614 Py_DECREF(v);
1615 Py_DECREF(w);
1616 SET_TOP(x);
1617 if (x != NULL) continue;
1618 break;
1620 case SLICE+0:
1621 case SLICE+1:
1622 case SLICE+2:
1623 case SLICE+3:
1624 if ((opcode-SLICE) & 2)
1625 w = POP();
1626 else
1627 w = NULL;
1628 if ((opcode-SLICE) & 1)
1629 v = POP();
1630 else
1631 v = NULL;
1632 u = TOP();
1633 x = apply_slice(u, v, w);
1634 Py_DECREF(u);
1635 Py_XDECREF(v);
1636 Py_XDECREF(w);
1637 SET_TOP(x);
1638 if (x != NULL) continue;
1639 break;
1641 case STORE_SLICE+0:
1642 case STORE_SLICE+1:
1643 case STORE_SLICE+2:
1644 case STORE_SLICE+3:
1645 if ((opcode-STORE_SLICE) & 2)
1646 w = POP();
1647 else
1648 w = NULL;
1649 if ((opcode-STORE_SLICE) & 1)
1650 v = POP();
1651 else
1652 v = NULL;
1653 u = POP();
1654 t = POP();
1655 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1656 Py_DECREF(t);
1657 Py_DECREF(u);
1658 Py_XDECREF(v);
1659 Py_XDECREF(w);
1660 if (err == 0) continue;
1661 break;
1663 case DELETE_SLICE+0:
1664 case DELETE_SLICE+1:
1665 case DELETE_SLICE+2:
1666 case DELETE_SLICE+3:
1667 if ((opcode-DELETE_SLICE) & 2)
1668 w = POP();
1669 else
1670 w = NULL;
1671 if ((opcode-DELETE_SLICE) & 1)
1672 v = POP();
1673 else
1674 v = NULL;
1675 u = POP();
1676 err = assign_slice(u, v, w, (PyObject *)NULL);
1677 /* del u[v:w] */
1678 Py_DECREF(u);
1679 Py_XDECREF(v);
1680 Py_XDECREF(w);
1681 if (err == 0) continue;
1682 break;
1684 case STORE_SUBSCR:
1685 w = TOP();
1686 v = SECOND();
1687 u = THIRD();
1688 STACKADJ(-3);
1689 /* v[w] = u */
1690 err = PyObject_SetItem(v, w, u);
1691 Py_DECREF(u);
1692 Py_DECREF(v);
1693 Py_DECREF(w);
1694 if (err == 0) continue;
1695 break;
1697 case DELETE_SUBSCR:
1698 w = TOP();
1699 v = SECOND();
1700 STACKADJ(-2);
1701 /* del v[w] */
1702 err = PyObject_DelItem(v, w);
1703 Py_DECREF(v);
1704 Py_DECREF(w);
1705 if (err == 0) continue;
1706 break;
1708 case PRINT_EXPR:
1709 v = POP();
1710 w = PySys_GetObject("displayhook");
1711 if (w == NULL) {
1712 PyErr_SetString(PyExc_RuntimeError,
1713 "lost sys.displayhook");
1714 err = -1;
1715 x = NULL;
1717 if (err == 0) {
1718 x = PyTuple_Pack(1, v);
1719 if (x == NULL)
1720 err = -1;
1722 if (err == 0) {
1723 w = PyEval_CallObject(w, x);
1724 Py_XDECREF(w);
1725 if (w == NULL)
1726 err = -1;
1728 Py_DECREF(v);
1729 Py_XDECREF(x);
1730 break;
1732 case PRINT_ITEM_TO:
1733 w = stream = POP();
1734 /* fall through to PRINT_ITEM */
1736 case PRINT_ITEM:
1737 v = POP();
1738 if (stream == NULL || stream == Py_None) {
1739 w = PySys_GetObject("stdout");
1740 if (w == NULL) {
1741 PyErr_SetString(PyExc_RuntimeError,
1742 "lost sys.stdout");
1743 err = -1;
1746 /* PyFile_SoftSpace() can exececute arbitrary code
1747 if sys.stdout is an instance with a __getattr__.
1748 If __getattr__ raises an exception, w will
1749 be freed, so we need to prevent that temporarily. */
1750 Py_XINCREF(w);
1751 if (w != NULL && PyFile_SoftSpace(w, 0))
1752 err = PyFile_WriteString(" ", w);
1753 if (err == 0)
1754 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1755 if (err == 0) {
1756 /* XXX move into writeobject() ? */
1757 if (PyString_Check(v)) {
1758 char *s = PyString_AS_STRING(v);
1759 Py_ssize_t len = PyString_GET_SIZE(v);
1760 if (len == 0 ||
1761 !isspace(Py_CHARMASK(s[len-1])) ||
1762 s[len-1] == ' ')
1763 PyFile_SoftSpace(w, 1);
1765 #ifdef Py_USING_UNICODE
1766 else if (PyUnicode_Check(v)) {
1767 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1768 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1769 if (len == 0 ||
1770 !Py_UNICODE_ISSPACE(s[len-1]) ||
1771 s[len-1] == ' ')
1772 PyFile_SoftSpace(w, 1);
1774 #endif
1775 else
1776 PyFile_SoftSpace(w, 1);
1778 Py_XDECREF(w);
1779 Py_DECREF(v);
1780 Py_XDECREF(stream);
1781 stream = NULL;
1782 if (err == 0)
1783 continue;
1784 break;
1786 case PRINT_NEWLINE_TO:
1787 w = stream = POP();
1788 /* fall through to PRINT_NEWLINE */
1790 case PRINT_NEWLINE:
1791 if (stream == NULL || stream == Py_None) {
1792 w = PySys_GetObject("stdout");
1793 if (w == NULL) {
1794 PyErr_SetString(PyExc_RuntimeError,
1795 "lost sys.stdout");
1796 why = WHY_EXCEPTION;
1799 if (w != NULL) {
1800 /* w.write() may replace sys.stdout, so we
1801 * have to keep our reference to it */
1802 Py_INCREF(w);
1803 err = PyFile_WriteString("\n", w);
1804 if (err == 0)
1805 PyFile_SoftSpace(w, 0);
1806 Py_DECREF(w);
1808 Py_XDECREF(stream);
1809 stream = NULL;
1810 break;
1813 #ifdef CASE_TOO_BIG
1814 default: switch (opcode) {
1815 #endif
1816 case RAISE_VARARGS:
1817 u = v = w = NULL;
1818 switch (oparg) {
1819 case 3:
1820 u = POP(); /* traceback */
1821 /* Fallthrough */
1822 case 2:
1823 v = POP(); /* value */
1824 /* Fallthrough */
1825 case 1:
1826 w = POP(); /* exc */
1827 case 0: /* Fallthrough */
1828 why = do_raise(w, v, u);
1829 break;
1830 default:
1831 PyErr_SetString(PyExc_SystemError,
1832 "bad RAISE_VARARGS oparg");
1833 why = WHY_EXCEPTION;
1834 break;
1836 break;
1838 case LOAD_LOCALS:
1839 if ((x = f->f_locals) != NULL) {
1840 Py_INCREF(x);
1841 PUSH(x);
1842 continue;
1844 PyErr_SetString(PyExc_SystemError, "no locals");
1845 break;
1847 case RETURN_VALUE:
1848 retval = POP();
1849 why = WHY_RETURN;
1850 goto fast_block_end;
1852 case YIELD_VALUE:
1853 retval = POP();
1854 f->f_stacktop = stack_pointer;
1855 why = WHY_YIELD;
1856 goto fast_yield;
1858 case EXEC_STMT:
1859 w = TOP();
1860 v = SECOND();
1861 u = THIRD();
1862 STACKADJ(-3);
1863 READ_TIMESTAMP(intr0);
1864 err = exec_statement(f, u, v, w);
1865 READ_TIMESTAMP(intr1);
1866 Py_DECREF(u);
1867 Py_DECREF(v);
1868 Py_DECREF(w);
1869 break;
1871 case POP_BLOCK:
1873 PyTryBlock *b = PyFrame_BlockPop(f);
1874 while (STACK_LEVEL() > b->b_level) {
1875 v = POP();
1876 Py_DECREF(v);
1879 continue;
1881 PREDICTED(END_FINALLY);
1882 case END_FINALLY:
1883 v = POP();
1884 if (PyInt_Check(v)) {
1885 why = (enum why_code) PyInt_AS_LONG(v);
1886 assert(why != WHY_YIELD);
1887 if (why == WHY_RETURN ||
1888 why == WHY_CONTINUE)
1889 retval = POP();
1891 else if (PyExceptionClass_Check(v) ||
1892 PyString_Check(v)) {
1893 w = POP();
1894 u = POP();
1895 PyErr_Restore(v, w, u);
1896 why = WHY_RERAISE;
1897 break;
1899 else if (v != Py_None) {
1900 PyErr_SetString(PyExc_SystemError,
1901 "'finally' pops bad exception");
1902 why = WHY_EXCEPTION;
1904 Py_DECREF(v);
1905 break;
1907 case BUILD_CLASS:
1908 u = TOP();
1909 v = SECOND();
1910 w = THIRD();
1911 STACKADJ(-2);
1912 x = build_class(u, v, w);
1913 SET_TOP(x);
1914 Py_DECREF(u);
1915 Py_DECREF(v);
1916 Py_DECREF(w);
1917 break;
1919 case STORE_NAME:
1920 w = GETITEM(names, oparg);
1921 v = POP();
1922 if ((x = f->f_locals) != NULL) {
1923 if (PyDict_CheckExact(x))
1924 err = PyDict_SetItem(x, w, v);
1925 else
1926 err = PyObject_SetItem(x, w, v);
1927 Py_DECREF(v);
1928 if (err == 0) continue;
1929 break;
1931 PyErr_Format(PyExc_SystemError,
1932 "no locals found when storing %s",
1933 PyObject_REPR(w));
1934 break;
1936 case DELETE_NAME:
1937 w = GETITEM(names, oparg);
1938 if ((x = f->f_locals) != NULL) {
1939 if ((err = PyObject_DelItem(x, w)) != 0)
1940 format_exc_check_arg(PyExc_NameError,
1941 NAME_ERROR_MSG,
1943 break;
1945 PyErr_Format(PyExc_SystemError,
1946 "no locals when deleting %s",
1947 PyObject_REPR(w));
1948 break;
1950 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1951 case UNPACK_SEQUENCE:
1952 v = POP();
1953 if (PyTuple_CheckExact(v) &&
1954 PyTuple_GET_SIZE(v) == oparg) {
1955 PyObject **items = \
1956 ((PyTupleObject *)v)->ob_item;
1957 while (oparg--) {
1958 w = items[oparg];
1959 Py_INCREF(w);
1960 PUSH(w);
1962 Py_DECREF(v);
1963 continue;
1964 } else if (PyList_CheckExact(v) &&
1965 PyList_GET_SIZE(v) == oparg) {
1966 PyObject **items = \
1967 ((PyListObject *)v)->ob_item;
1968 while (oparg--) {
1969 w = items[oparg];
1970 Py_INCREF(w);
1971 PUSH(w);
1973 } else if (unpack_iterable(v, oparg,
1974 stack_pointer + oparg)) {
1975 STACKADJ(oparg);
1976 } else {
1977 /* unpack_iterable() raised an exception */
1978 why = WHY_EXCEPTION;
1980 Py_DECREF(v);
1981 break;
1983 case STORE_ATTR:
1984 w = GETITEM(names, oparg);
1985 v = TOP();
1986 u = SECOND();
1987 STACKADJ(-2);
1988 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1989 Py_DECREF(v);
1990 Py_DECREF(u);
1991 if (err == 0) continue;
1992 break;
1994 case DELETE_ATTR:
1995 w = GETITEM(names, oparg);
1996 v = POP();
1997 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1998 /* del v.w */
1999 Py_DECREF(v);
2000 break;
2002 case STORE_GLOBAL:
2003 w = GETITEM(names, oparg);
2004 v = POP();
2005 err = PyDict_SetItem(f->f_globals, w, v);
2006 Py_DECREF(v);
2007 if (err == 0) continue;
2008 break;
2010 case DELETE_GLOBAL:
2011 w = GETITEM(names, oparg);
2012 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2013 format_exc_check_arg(
2014 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2015 break;
2017 case LOAD_NAME:
2018 w = GETITEM(names, oparg);
2019 if ((v = f->f_locals) == NULL) {
2020 PyErr_Format(PyExc_SystemError,
2021 "no locals when loading %s",
2022 PyObject_REPR(w));
2023 why = WHY_EXCEPTION;
2024 break;
2026 if (PyDict_CheckExact(v)) {
2027 x = PyDict_GetItem(v, w);
2028 Py_XINCREF(x);
2030 else {
2031 x = PyObject_GetItem(v, w);
2032 if (x == NULL && PyErr_Occurred()) {
2033 if (!PyErr_ExceptionMatches(
2034 PyExc_KeyError))
2035 break;
2036 PyErr_Clear();
2039 if (x == NULL) {
2040 x = PyDict_GetItem(f->f_globals, w);
2041 if (x == NULL) {
2042 x = PyDict_GetItem(f->f_builtins, w);
2043 if (x == NULL) {
2044 format_exc_check_arg(
2045 PyExc_NameError,
2046 NAME_ERROR_MSG, w);
2047 break;
2050 Py_INCREF(x);
2052 PUSH(x);
2053 continue;
2055 case LOAD_GLOBAL:
2056 w = GETITEM(names, oparg);
2057 if (PyString_CheckExact(w)) {
2058 /* Inline the PyDict_GetItem() calls.
2059 WARNING: this is an extreme speed hack.
2060 Do not try this at home. */
2061 long hash = ((PyStringObject *)w)->ob_shash;
2062 if (hash != -1) {
2063 PyDictObject *d;
2064 PyDictEntry *e;
2065 d = (PyDictObject *)(f->f_globals);
2066 e = d->ma_lookup(d, w, hash);
2067 if (e == NULL) {
2068 x = NULL;
2069 break;
2071 x = e->me_value;
2072 if (x != NULL) {
2073 Py_INCREF(x);
2074 PUSH(x);
2075 continue;
2077 d = (PyDictObject *)(f->f_builtins);
2078 e = d->ma_lookup(d, w, hash);
2079 if (e == NULL) {
2080 x = NULL;
2081 break;
2083 x = e->me_value;
2084 if (x != NULL) {
2085 Py_INCREF(x);
2086 PUSH(x);
2087 continue;
2089 goto load_global_error;
2092 /* This is the un-inlined version of the code above */
2093 x = PyDict_GetItem(f->f_globals, w);
2094 if (x == NULL) {
2095 x = PyDict_GetItem(f->f_builtins, w);
2096 if (x == NULL) {
2097 load_global_error:
2098 format_exc_check_arg(
2099 PyExc_NameError,
2100 GLOBAL_NAME_ERROR_MSG, w);
2101 break;
2104 Py_INCREF(x);
2105 PUSH(x);
2106 continue;
2108 case DELETE_FAST:
2109 x = GETLOCAL(oparg);
2110 if (x != NULL) {
2111 SETLOCAL(oparg, NULL);
2112 continue;
2114 format_exc_check_arg(
2115 PyExc_UnboundLocalError,
2116 UNBOUNDLOCAL_ERROR_MSG,
2117 PyTuple_GetItem(co->co_varnames, oparg)
2119 break;
2121 case LOAD_CLOSURE:
2122 x = freevars[oparg];
2123 Py_INCREF(x);
2124 PUSH(x);
2125 if (x != NULL) continue;
2126 break;
2128 case LOAD_DEREF:
2129 x = freevars[oparg];
2130 w = PyCell_Get(x);
2131 if (w != NULL) {
2132 PUSH(w);
2133 continue;
2135 err = -1;
2136 /* Don't stomp existing exception */
2137 if (PyErr_Occurred())
2138 break;
2139 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2140 v = PyTuple_GET_ITEM(co->co_cellvars,
2141 oparg);
2142 format_exc_check_arg(
2143 PyExc_UnboundLocalError,
2144 UNBOUNDLOCAL_ERROR_MSG,
2146 } else {
2147 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2148 PyTuple_GET_SIZE(co->co_cellvars));
2149 format_exc_check_arg(PyExc_NameError,
2150 UNBOUNDFREE_ERROR_MSG, v);
2152 break;
2154 case STORE_DEREF:
2155 w = POP();
2156 x = freevars[oparg];
2157 PyCell_Set(x, w);
2158 Py_DECREF(w);
2159 continue;
2161 case BUILD_TUPLE:
2162 x = PyTuple_New(oparg);
2163 if (x != NULL) {
2164 for (; --oparg >= 0;) {
2165 w = POP();
2166 PyTuple_SET_ITEM(x, oparg, w);
2168 PUSH(x);
2169 continue;
2171 break;
2173 case BUILD_LIST:
2174 x = PyList_New(oparg);
2175 if (x != NULL) {
2176 for (; --oparg >= 0;) {
2177 w = POP();
2178 PyList_SET_ITEM(x, oparg, w);
2180 PUSH(x);
2181 continue;
2183 break;
2185 case BUILD_MAP:
2186 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2187 PUSH(x);
2188 if (x != NULL) continue;
2189 break;
2191 case STORE_MAP:
2192 w = TOP(); /* key */
2193 u = SECOND(); /* value */
2194 v = THIRD(); /* dict */
2195 STACKADJ(-2);
2196 assert (PyDict_CheckExact(v));
2197 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2198 Py_DECREF(u);
2199 Py_DECREF(w);
2200 if (err == 0) continue;
2201 break;
2203 case LOAD_ATTR:
2204 w = GETITEM(names, oparg);
2205 v = TOP();
2206 x = PyObject_GetAttr(v, w);
2207 Py_DECREF(v);
2208 SET_TOP(x);
2209 if (x != NULL) continue;
2210 break;
2212 case COMPARE_OP:
2213 w = POP();
2214 v = TOP();
2215 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2216 /* INLINE: cmp(int, int) */
2217 register long a, b;
2218 register int res;
2219 a = PyInt_AS_LONG(v);
2220 b = PyInt_AS_LONG(w);
2221 switch (oparg) {
2222 case PyCmp_LT: res = a < b; break;
2223 case PyCmp_LE: res = a <= b; break;
2224 case PyCmp_EQ: res = a == b; break;
2225 case PyCmp_NE: res = a != b; break;
2226 case PyCmp_GT: res = a > b; break;
2227 case PyCmp_GE: res = a >= b; break;
2228 case PyCmp_IS: res = v == w; break;
2229 case PyCmp_IS_NOT: res = v != w; break;
2230 default: goto slow_compare;
2232 x = res ? Py_True : Py_False;
2233 Py_INCREF(x);
2235 else {
2236 slow_compare:
2237 x = cmp_outcome(oparg, v, w);
2239 Py_DECREF(v);
2240 Py_DECREF(w);
2241 SET_TOP(x);
2242 if (x == NULL) break;
2243 PREDICT(POP_JUMP_IF_FALSE);
2244 PREDICT(POP_JUMP_IF_TRUE);
2245 continue;
2247 case IMPORT_NAME:
2248 w = GETITEM(names, oparg);
2249 x = PyDict_GetItemString(f->f_builtins, "__import__");
2250 if (x == NULL) {
2251 PyErr_SetString(PyExc_ImportError,
2252 "__import__ not found");
2253 break;
2255 Py_INCREF(x);
2256 v = POP();
2257 u = TOP();
2258 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2259 w = PyTuple_Pack(5,
2261 f->f_globals,
2262 f->f_locals == NULL ?
2263 Py_None : f->f_locals,
2266 else
2267 w = PyTuple_Pack(4,
2269 f->f_globals,
2270 f->f_locals == NULL ?
2271 Py_None : f->f_locals,
2273 Py_DECREF(v);
2274 Py_DECREF(u);
2275 if (w == NULL) {
2276 u = POP();
2277 Py_DECREF(x);
2278 x = NULL;
2279 break;
2281 READ_TIMESTAMP(intr0);
2282 v = x;
2283 x = PyEval_CallObject(v, w);
2284 Py_DECREF(v);
2285 READ_TIMESTAMP(intr1);
2286 Py_DECREF(w);
2287 SET_TOP(x);
2288 if (x != NULL) continue;
2289 break;
2291 case IMPORT_STAR:
2292 v = POP();
2293 PyFrame_FastToLocals(f);
2294 if ((x = f->f_locals) == NULL) {
2295 PyErr_SetString(PyExc_SystemError,
2296 "no locals found during 'import *'");
2297 break;
2299 READ_TIMESTAMP(intr0);
2300 err = import_all_from(x, v);
2301 READ_TIMESTAMP(intr1);
2302 PyFrame_LocalsToFast(f, 0);
2303 Py_DECREF(v);
2304 if (err == 0) continue;
2305 break;
2307 case IMPORT_FROM:
2308 w = GETITEM(names, oparg);
2309 v = TOP();
2310 READ_TIMESTAMP(intr0);
2311 x = import_from(v, w);
2312 READ_TIMESTAMP(intr1);
2313 PUSH(x);
2314 if (x != NULL) continue;
2315 break;
2317 case JUMP_FORWARD:
2318 JUMPBY(oparg);
2319 goto fast_next_opcode;
2321 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2322 case POP_JUMP_IF_FALSE:
2323 w = POP();
2324 if (w == Py_True) {
2325 Py_DECREF(w);
2326 goto fast_next_opcode;
2328 if (w == Py_False) {
2329 Py_DECREF(w);
2330 JUMPTO(oparg);
2331 goto fast_next_opcode;
2333 err = PyObject_IsTrue(w);
2334 Py_DECREF(w);
2335 if (err > 0)
2336 err = 0;
2337 else if (err == 0)
2338 JUMPTO(oparg);
2339 else
2340 break;
2341 continue;
2343 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2344 case POP_JUMP_IF_TRUE:
2345 w = POP();
2346 if (w == Py_False) {
2347 Py_DECREF(w);
2348 goto fast_next_opcode;
2350 if (w == Py_True) {
2351 Py_DECREF(w);
2352 JUMPTO(oparg);
2353 goto fast_next_opcode;
2355 err = PyObject_IsTrue(w);
2356 Py_DECREF(w);
2357 if (err > 0) {
2358 err = 0;
2359 JUMPTO(oparg);
2361 else if (err == 0)
2363 else
2364 break;
2365 continue;
2367 case JUMP_IF_FALSE_OR_POP:
2368 w = TOP();
2369 if (w == Py_True) {
2370 STACKADJ(-1);
2371 Py_DECREF(w);
2372 goto fast_next_opcode;
2374 if (w == Py_False) {
2375 JUMPTO(oparg);
2376 goto fast_next_opcode;
2378 err = PyObject_IsTrue(w);
2379 if (err > 0) {
2380 STACKADJ(-1);
2381 Py_DECREF(w);
2382 err = 0;
2384 else if (err == 0)
2385 JUMPTO(oparg);
2386 else
2387 break;
2388 continue;
2390 case JUMP_IF_TRUE_OR_POP:
2391 w = TOP();
2392 if (w == Py_False) {
2393 STACKADJ(-1);
2394 Py_DECREF(w);
2395 goto fast_next_opcode;
2397 if (w == Py_True) {
2398 JUMPTO(oparg);
2399 goto fast_next_opcode;
2401 err = PyObject_IsTrue(w);
2402 if (err > 0) {
2403 err = 0;
2404 JUMPTO(oparg);
2406 else if (err == 0) {
2407 STACKADJ(-1);
2408 Py_DECREF(w);
2410 else
2411 break;
2412 continue;
2414 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2415 case JUMP_ABSOLUTE:
2416 JUMPTO(oparg);
2417 #if FAST_LOOPS
2418 /* Enabling this path speeds-up all while and for-loops by bypassing
2419 the per-loop checks for signals. By default, this should be turned-off
2420 because it prevents detection of a control-break in tight loops like
2421 "while 1: pass". Compile with this option turned-on when you need
2422 the speed-up and do not need break checking inside tight loops (ones
2423 that contain only instructions ending with goto fast_next_opcode).
2425 goto fast_next_opcode;
2426 #else
2427 continue;
2428 #endif
2430 case GET_ITER:
2431 /* before: [obj]; after [getiter(obj)] */
2432 v = TOP();
2433 x = PyObject_GetIter(v);
2434 Py_DECREF(v);
2435 if (x != NULL) {
2436 SET_TOP(x);
2437 PREDICT(FOR_ITER);
2438 continue;
2440 STACKADJ(-1);
2441 break;
2443 PREDICTED_WITH_ARG(FOR_ITER);
2444 case FOR_ITER:
2445 /* before: [iter]; after: [iter, iter()] *or* [] */
2446 v = TOP();
2447 x = (*v->ob_type->tp_iternext)(v);
2448 if (x != NULL) {
2449 PUSH(x);
2450 PREDICT(STORE_FAST);
2451 PREDICT(UNPACK_SEQUENCE);
2452 continue;
2454 if (PyErr_Occurred()) {
2455 if (!PyErr_ExceptionMatches(
2456 PyExc_StopIteration))
2457 break;
2458 PyErr_Clear();
2460 /* iterator ended normally */
2461 x = v = POP();
2462 Py_DECREF(v);
2463 JUMPBY(oparg);
2464 continue;
2466 case BREAK_LOOP:
2467 why = WHY_BREAK;
2468 goto fast_block_end;
2470 case CONTINUE_LOOP:
2471 retval = PyInt_FromLong(oparg);
2472 if (!retval) {
2473 x = NULL;
2474 break;
2476 why = WHY_CONTINUE;
2477 goto fast_block_end;
2479 case SETUP_LOOP:
2480 case SETUP_EXCEPT:
2481 case SETUP_FINALLY:
2482 /* NOTE: If you add any new block-setup opcodes that
2483 are not try/except/finally handlers, you may need
2484 to update the PyGen_NeedsFinalizing() function.
2487 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2488 STACK_LEVEL());
2489 continue;
2491 case SETUP_WITH:
2493 static PyObject *exit, *enter;
2494 w = TOP();
2495 x = special_lookup(w, "__exit__", &exit);
2496 if (!x)
2497 break;
2498 SET_TOP(x);
2499 u = special_lookup(w, "__enter__", &enter);
2500 Py_DECREF(w);
2501 if (!u) {
2502 x = NULL;
2503 break;
2505 x = PyObject_CallFunctionObjArgs(u, NULL);
2506 Py_DECREF(u);
2507 if (!x)
2508 break;
2509 /* Setup the finally block before pushing the result
2510 of __enter__ on the stack. */
2511 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2512 STACK_LEVEL());
2514 PUSH(x);
2515 continue;
2518 case WITH_CLEANUP:
2520 /* At the top of the stack are 1-3 values indicating
2521 how/why we entered the finally clause:
2522 - TOP = None
2523 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2524 - TOP = WHY_*; no retval below it
2525 - (TOP, SECOND, THIRD) = exc_info()
2526 Below them is EXIT, the context.__exit__ bound method.
2527 In the last case, we must call
2528 EXIT(TOP, SECOND, THIRD)
2529 otherwise we must call
2530 EXIT(None, None, None)
2532 In all cases, we remove EXIT from the stack, leaving
2533 the rest in the same order.
2535 In addition, if the stack represents an exception,
2536 *and* the function call returns a 'true' value, we
2537 "zap" this information, to prevent END_FINALLY from
2538 re-raising the exception. (But non-local gotos
2539 should still be resumed.)
2542 PyObject *exit_func;
2544 u = POP();
2545 if (u == Py_None) {
2546 exit_func = TOP();
2547 SET_TOP(u);
2548 v = w = Py_None;
2550 else if (PyInt_Check(u)) {
2551 switch(PyInt_AS_LONG(u)) {
2552 case WHY_RETURN:
2553 case WHY_CONTINUE:
2554 /* Retval in TOP. */
2555 exit_func = SECOND();
2556 SET_SECOND(TOP());
2557 SET_TOP(u);
2558 break;
2559 default:
2560 exit_func = TOP();
2561 SET_TOP(u);
2562 break;
2564 u = v = w = Py_None;
2566 else {
2567 v = TOP();
2568 w = SECOND();
2569 exit_func = THIRD();
2570 SET_TOP(u);
2571 SET_SECOND(v);
2572 SET_THIRD(w);
2574 /* XXX Not the fastest way to call it... */
2575 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2576 NULL);
2577 Py_DECREF(exit_func);
2578 if (x == NULL)
2579 break; /* Go to error exit */
2581 if (u != Py_None)
2582 err = PyObject_IsTrue(x);
2583 else
2584 err = 0;
2585 Py_DECREF(x);
2587 if (err < 0)
2588 break; /* Go to error exit */
2589 else if (err > 0) {
2590 err = 0;
2591 /* There was an exception and a true return */
2592 STACKADJ(-2);
2593 Py_INCREF(Py_None);
2594 SET_TOP(Py_None);
2595 Py_DECREF(u);
2596 Py_DECREF(v);
2597 Py_DECREF(w);
2598 } else {
2599 /* The stack was rearranged to remove EXIT
2600 above. Let END_FINALLY do its thing */
2602 PREDICT(END_FINALLY);
2603 break;
2606 case CALL_FUNCTION:
2608 PyObject **sp;
2609 PCALL(PCALL_ALL);
2610 sp = stack_pointer;
2611 #ifdef WITH_TSC
2612 x = call_function(&sp, oparg, &intr0, &intr1);
2613 #else
2614 x = call_function(&sp, oparg);
2615 #endif
2616 stack_pointer = sp;
2617 PUSH(x);
2618 if (x != NULL)
2619 continue;
2620 break;
2623 case CALL_FUNCTION_VAR:
2624 case CALL_FUNCTION_KW:
2625 case CALL_FUNCTION_VAR_KW:
2627 int na = oparg & 0xff;
2628 int nk = (oparg>>8) & 0xff;
2629 int flags = (opcode - CALL_FUNCTION) & 3;
2630 int n = na + 2 * nk;
2631 PyObject **pfunc, *func, **sp;
2632 PCALL(PCALL_ALL);
2633 if (flags & CALL_FLAG_VAR)
2634 n++;
2635 if (flags & CALL_FLAG_KW)
2636 n++;
2637 pfunc = stack_pointer - n - 1;
2638 func = *pfunc;
2640 if (PyMethod_Check(func)
2641 && PyMethod_GET_SELF(func) != NULL) {
2642 PyObject *self = PyMethod_GET_SELF(func);
2643 Py_INCREF(self);
2644 func = PyMethod_GET_FUNCTION(func);
2645 Py_INCREF(func);
2646 Py_DECREF(*pfunc);
2647 *pfunc = self;
2648 na++;
2649 n++;
2650 } else
2651 Py_INCREF(func);
2652 sp = stack_pointer;
2653 READ_TIMESTAMP(intr0);
2654 x = ext_do_call(func, &sp, flags, na, nk);
2655 READ_TIMESTAMP(intr1);
2656 stack_pointer = sp;
2657 Py_DECREF(func);
2659 while (stack_pointer > pfunc) {
2660 w = POP();
2661 Py_DECREF(w);
2663 PUSH(x);
2664 if (x != NULL)
2665 continue;
2666 break;
2669 case MAKE_FUNCTION:
2670 v = POP(); /* code object */
2671 x = PyFunction_New(v, f->f_globals);
2672 Py_DECREF(v);
2673 /* XXX Maybe this should be a separate opcode? */
2674 if (x != NULL && oparg > 0) {
2675 v = PyTuple_New(oparg);
2676 if (v == NULL) {
2677 Py_DECREF(x);
2678 x = NULL;
2679 break;
2681 while (--oparg >= 0) {
2682 w = POP();
2683 PyTuple_SET_ITEM(v, oparg, w);
2685 err = PyFunction_SetDefaults(x, v);
2686 Py_DECREF(v);
2688 PUSH(x);
2689 break;
2691 case MAKE_CLOSURE:
2693 v = POP(); /* code object */
2694 x = PyFunction_New(v, f->f_globals);
2695 Py_DECREF(v);
2696 if (x != NULL) {
2697 v = POP();
2698 if (PyFunction_SetClosure(x, v) != 0) {
2699 /* Can't happen unless bytecode is corrupt. */
2700 why = WHY_EXCEPTION;
2702 Py_DECREF(v);
2704 if (x != NULL && oparg > 0) {
2705 v = PyTuple_New(oparg);
2706 if (v == NULL) {
2707 Py_DECREF(x);
2708 x = NULL;
2709 break;
2711 while (--oparg >= 0) {
2712 w = POP();
2713 PyTuple_SET_ITEM(v, oparg, w);
2715 if (PyFunction_SetDefaults(x, v) != 0) {
2716 /* Can't happen unless
2717 PyFunction_SetDefaults changes. */
2718 why = WHY_EXCEPTION;
2720 Py_DECREF(v);
2722 PUSH(x);
2723 break;
2726 case BUILD_SLICE:
2727 if (oparg == 3)
2728 w = POP();
2729 else
2730 w = NULL;
2731 v = POP();
2732 u = TOP();
2733 x = PySlice_New(u, v, w);
2734 Py_DECREF(u);
2735 Py_DECREF(v);
2736 Py_XDECREF(w);
2737 SET_TOP(x);
2738 if (x != NULL) continue;
2739 break;
2741 case EXTENDED_ARG:
2742 opcode = NEXTOP();
2743 oparg = oparg<<16 | NEXTARG();
2744 goto dispatch_opcode;
2746 default:
2747 fprintf(stderr,
2748 "XXX lineno: %d, opcode: %d\n",
2749 PyFrame_GetLineNumber(f),
2750 opcode);
2751 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2752 why = WHY_EXCEPTION;
2753 break;
2755 #ifdef CASE_TOO_BIG
2757 #endif
2759 } /* switch */
2761 on_error:
2763 READ_TIMESTAMP(inst1);
2765 /* Quickly continue if no error occurred */
2767 if (why == WHY_NOT) {
2768 if (err == 0 && x != NULL) {
2769 #ifdef CHECKEXC
2770 /* This check is expensive! */
2771 if (PyErr_Occurred())
2772 fprintf(stderr,
2773 "XXX undetected error\n");
2774 else {
2775 #endif
2776 READ_TIMESTAMP(loop1);
2777 continue; /* Normal, fast path */
2778 #ifdef CHECKEXC
2780 #endif
2782 why = WHY_EXCEPTION;
2783 x = Py_None;
2784 err = 0;
2787 /* Double-check exception status */
2789 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2790 if (!PyErr_Occurred()) {
2791 PyErr_SetString(PyExc_SystemError,
2792 "error return without exception set");
2793 why = WHY_EXCEPTION;
2796 #ifdef CHECKEXC
2797 else {
2798 /* This check is expensive! */
2799 if (PyErr_Occurred()) {
2800 char buf[128];
2801 sprintf(buf, "Stack unwind with exception "
2802 "set and why=%d", why);
2803 Py_FatalError(buf);
2806 #endif
2808 /* Log traceback info if this is a real exception */
2810 if (why == WHY_EXCEPTION) {
2811 PyTraceBack_Here(f);
2813 if (tstate->c_tracefunc != NULL)
2814 call_exc_trace(tstate->c_tracefunc,
2815 tstate->c_traceobj, f);
2818 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2820 if (why == WHY_RERAISE)
2821 why = WHY_EXCEPTION;
2823 /* Unwind stacks if a (pseudo) exception occurred */
2825 fast_block_end:
2826 while (why != WHY_NOT && f->f_iblock > 0) {
2827 /* Peek at the current block. */
2828 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
2830 assert(why != WHY_YIELD);
2831 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2832 why = WHY_NOT;
2833 JUMPTO(PyInt_AS_LONG(retval));
2834 Py_DECREF(retval);
2835 break;
2838 /* Now we have to pop the block. */
2839 f->f_iblock--;
2841 while (STACK_LEVEL() > b->b_level) {
2842 v = POP();
2843 Py_XDECREF(v);
2845 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2846 why = WHY_NOT;
2847 JUMPTO(b->b_handler);
2848 break;
2850 if (b->b_type == SETUP_FINALLY ||
2851 (b->b_type == SETUP_EXCEPT &&
2852 why == WHY_EXCEPTION)) {
2853 if (why == WHY_EXCEPTION) {
2854 PyObject *exc, *val, *tb;
2855 PyErr_Fetch(&exc, &val, &tb);
2856 if (val == NULL) {
2857 val = Py_None;
2858 Py_INCREF(val);
2860 /* Make the raw exception data
2861 available to the handler,
2862 so a program can emulate the
2863 Python main loop. Don't do
2864 this for 'finally'. */
2865 if (b->b_type == SETUP_EXCEPT) {
2866 PyErr_NormalizeException(
2867 &exc, &val, &tb);
2868 set_exc_info(tstate,
2869 exc, val, tb);
2871 if (tb == NULL) {
2872 Py_INCREF(Py_None);
2873 PUSH(Py_None);
2874 } else
2875 PUSH(tb);
2876 PUSH(val);
2877 PUSH(exc);
2879 else {
2880 if (why & (WHY_RETURN | WHY_CONTINUE))
2881 PUSH(retval);
2882 v = PyInt_FromLong((long)why);
2883 PUSH(v);
2885 why = WHY_NOT;
2886 JUMPTO(b->b_handler);
2887 break;
2889 } /* unwind stack */
2891 /* End the loop if we still have an error (or return) */
2893 if (why != WHY_NOT)
2894 break;
2895 READ_TIMESTAMP(loop1);
2897 } /* main loop */
2899 assert(why != WHY_YIELD);
2900 /* Pop remaining stack entries. */
2901 while (!EMPTY()) {
2902 v = POP();
2903 Py_XDECREF(v);
2906 if (why != WHY_RETURN)
2907 retval = NULL;
2909 fast_yield:
2910 if (tstate->use_tracing) {
2911 if (tstate->c_tracefunc) {
2912 if (why == WHY_RETURN || why == WHY_YIELD) {
2913 if (call_trace(tstate->c_tracefunc,
2914 tstate->c_traceobj, f,
2915 PyTrace_RETURN, retval)) {
2916 Py_XDECREF(retval);
2917 retval = NULL;
2918 why = WHY_EXCEPTION;
2921 else if (why == WHY_EXCEPTION) {
2922 call_trace_protected(tstate->c_tracefunc,
2923 tstate->c_traceobj, f,
2924 PyTrace_RETURN, NULL);
2927 if (tstate->c_profilefunc) {
2928 if (why == WHY_EXCEPTION)
2929 call_trace_protected(tstate->c_profilefunc,
2930 tstate->c_profileobj, f,
2931 PyTrace_RETURN, NULL);
2932 else if (call_trace(tstate->c_profilefunc,
2933 tstate->c_profileobj, f,
2934 PyTrace_RETURN, retval)) {
2935 Py_XDECREF(retval);
2936 retval = NULL;
2937 why = WHY_EXCEPTION;
2942 if (tstate->frame->f_exc_type != NULL)
2943 reset_exc_info(tstate);
2944 else {
2945 assert(tstate->frame->f_exc_value == NULL);
2946 assert(tstate->frame->f_exc_traceback == NULL);
2949 /* pop frame */
2950 exit_eval_frame:
2951 Py_LeaveRecursiveCall();
2952 tstate->frame = f->f_back;
2954 return retval;
2957 /* This is gonna seem *real weird*, but if you put some other code between
2958 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2959 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
2961 PyObject *
2962 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
2963 PyObject **args, int argcount, PyObject **kws, int kwcount,
2964 PyObject **defs, int defcount, PyObject *closure)
2966 register PyFrameObject *f;
2967 register PyObject *retval = NULL;
2968 register PyObject **fastlocals, **freevars;
2969 PyThreadState *tstate = PyThreadState_GET();
2970 PyObject *x, *u;
2972 if (globals == NULL) {
2973 PyErr_SetString(PyExc_SystemError,
2974 "PyEval_EvalCodeEx: NULL globals");
2975 return NULL;
2978 assert(tstate != NULL);
2979 assert(globals != NULL);
2980 f = PyFrame_New(tstate, co, globals, locals);
2981 if (f == NULL)
2982 return NULL;
2984 fastlocals = f->f_localsplus;
2985 freevars = f->f_localsplus + co->co_nlocals;
2987 if (co->co_argcount > 0 ||
2988 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2989 int i;
2990 int n = argcount;
2991 PyObject *kwdict = NULL;
2992 if (co->co_flags & CO_VARKEYWORDS) {
2993 kwdict = PyDict_New();
2994 if (kwdict == NULL)
2995 goto fail;
2996 i = co->co_argcount;
2997 if (co->co_flags & CO_VARARGS)
2998 i++;
2999 SETLOCAL(i, kwdict);
3001 if (argcount > co->co_argcount) {
3002 if (!(co->co_flags & CO_VARARGS)) {
3003 PyErr_Format(PyExc_TypeError,
3004 "%.200s() takes %s %d "
3005 "%sargument%s (%d given)",
3006 PyString_AsString(co->co_name),
3007 defcount ? "at most" : "exactly",
3008 co->co_argcount,
3009 kwcount ? "non-keyword " : "",
3010 co->co_argcount == 1 ? "" : "s",
3011 argcount);
3012 goto fail;
3014 n = co->co_argcount;
3016 for (i = 0; i < n; i++) {
3017 x = args[i];
3018 Py_INCREF(x);
3019 SETLOCAL(i, x);
3021 if (co->co_flags & CO_VARARGS) {
3022 u = PyTuple_New(argcount - n);
3023 if (u == NULL)
3024 goto fail;
3025 SETLOCAL(co->co_argcount, u);
3026 for (i = n; i < argcount; i++) {
3027 x = args[i];
3028 Py_INCREF(x);
3029 PyTuple_SET_ITEM(u, i-n, x);
3032 for (i = 0; i < kwcount; i++) {
3033 PyObject **co_varnames;
3034 PyObject *keyword = kws[2*i];
3035 PyObject *value = kws[2*i + 1];
3036 int j;
3037 if (keyword == NULL || !(PyString_Check(keyword)
3038 #ifdef Py_USING_UNICODE
3039 || PyUnicode_Check(keyword)
3040 #endif
3041 )) {
3042 PyErr_Format(PyExc_TypeError,
3043 "%.200s() keywords must be strings",
3044 PyString_AsString(co->co_name));
3045 goto fail;
3047 /* Speed hack: do raw pointer compares. As names are
3048 normally interned this should almost always hit. */
3049 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
3050 for (j = 0; j < co->co_argcount; j++) {
3051 PyObject *nm = co_varnames[j];
3052 if (nm == keyword)
3053 goto kw_found;
3055 /* Slow fallback, just in case */
3056 for (j = 0; j < co->co_argcount; j++) {
3057 PyObject *nm = co_varnames[j];
3058 int cmp = PyObject_RichCompareBool(
3059 keyword, nm, Py_EQ);
3060 if (cmp > 0)
3061 goto kw_found;
3062 else if (cmp < 0)
3063 goto fail;
3065 /* Check errors from Compare */
3066 if (PyErr_Occurred())
3067 goto fail;
3068 if (j >= co->co_argcount) {
3069 if (kwdict == NULL) {
3070 PyObject *kwd_str = kwd_as_string(keyword);
3071 if (kwd_str) {
3072 PyErr_Format(PyExc_TypeError,
3073 "%.200s() got an unexpected "
3074 "keyword argument '%.400s'",
3075 PyString_AsString(co->co_name),
3076 PyString_AsString(kwd_str));
3077 Py_DECREF(kwd_str);
3079 goto fail;
3081 PyDict_SetItem(kwdict, keyword, value);
3082 continue;
3084 kw_found:
3085 if (GETLOCAL(j) != NULL) {
3086 PyObject *kwd_str = kwd_as_string(keyword);
3087 if (kwd_str) {
3088 PyErr_Format(PyExc_TypeError,
3089 "%.200s() got multiple "
3090 "values for keyword "
3091 "argument '%.400s'",
3092 PyString_AsString(co->co_name),
3093 PyString_AsString(kwd_str));
3094 Py_DECREF(kwd_str);
3096 goto fail;
3098 Py_INCREF(value);
3099 SETLOCAL(j, value);
3101 if (argcount < co->co_argcount) {
3102 int m = co->co_argcount - defcount;
3103 for (i = argcount; i < m; i++) {
3104 if (GETLOCAL(i) == NULL) {
3105 PyErr_Format(PyExc_TypeError,
3106 "%.200s() takes %s %d "
3107 "%sargument%s (%d given)",
3108 PyString_AsString(co->co_name),
3109 ((co->co_flags & CO_VARARGS) ||
3110 defcount) ? "at least"
3111 : "exactly",
3112 m, kwcount ? "non-keyword " : "",
3113 m == 1 ? "" : "s", i);
3114 goto fail;
3117 if (n > m)
3118 i = n - m;
3119 else
3120 i = 0;
3121 for (; i < defcount; i++) {
3122 if (GETLOCAL(m+i) == NULL) {
3123 PyObject *def = defs[i];
3124 Py_INCREF(def);
3125 SETLOCAL(m+i, def);
3130 else {
3131 if (argcount > 0 || kwcount > 0) {
3132 PyErr_Format(PyExc_TypeError,
3133 "%.200s() takes no arguments (%d given)",
3134 PyString_AsString(co->co_name),
3135 argcount + kwcount);
3136 goto fail;
3139 /* Allocate and initialize storage for cell vars, and copy free
3140 vars into frame. This isn't too efficient right now. */
3141 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3142 int i, j, nargs, found;
3143 char *cellname, *argname;
3144 PyObject *c;
3146 nargs = co->co_argcount;
3147 if (co->co_flags & CO_VARARGS)
3148 nargs++;
3149 if (co->co_flags & CO_VARKEYWORDS)
3150 nargs++;
3152 /* Initialize each cell var, taking into account
3153 cell vars that are initialized from arguments.
3155 Should arrange for the compiler to put cellvars
3156 that are arguments at the beginning of the cellvars
3157 list so that we can march over it more efficiently?
3159 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3160 cellname = PyString_AS_STRING(
3161 PyTuple_GET_ITEM(co->co_cellvars, i));
3162 found = 0;
3163 for (j = 0; j < nargs; j++) {
3164 argname = PyString_AS_STRING(
3165 PyTuple_GET_ITEM(co->co_varnames, j));
3166 if (strcmp(cellname, argname) == 0) {
3167 c = PyCell_New(GETLOCAL(j));
3168 if (c == NULL)
3169 goto fail;
3170 GETLOCAL(co->co_nlocals + i) = c;
3171 found = 1;
3172 break;
3175 if (found == 0) {
3176 c = PyCell_New(NULL);
3177 if (c == NULL)
3178 goto fail;
3179 SETLOCAL(co->co_nlocals + i, c);
3183 if (PyTuple_GET_SIZE(co->co_freevars)) {
3184 int i;
3185 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3186 PyObject *o = PyTuple_GET_ITEM(closure, i);
3187 Py_INCREF(o);
3188 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3192 if (co->co_flags & CO_GENERATOR) {
3193 /* Don't need to keep the reference to f_back, it will be set
3194 * when the generator is resumed. */
3195 Py_XDECREF(f->f_back);
3196 f->f_back = NULL;
3198 PCALL(PCALL_GENERATOR);
3200 /* Create a new generator that owns the ready to run frame
3201 * and return that as the value. */
3202 return PyGen_New(f);
3205 retval = PyEval_EvalFrameEx(f,0);
3207 fail: /* Jump here from prelude on failure */
3209 /* decref'ing the frame can cause __del__ methods to get invoked,
3210 which can call back into Python. While we're done with the
3211 current Python frame (f), the associated C stack is still in use,
3212 so recursion_depth must be boosted for the duration.
3214 assert(tstate != NULL);
3215 ++tstate->recursion_depth;
3216 Py_DECREF(f);
3217 --tstate->recursion_depth;
3218 return retval;
3222 static PyObject *
3223 special_lookup(PyObject *o, char *meth, PyObject **cache)
3225 PyObject *res;
3226 if (PyInstance_Check(o)) {
3227 if (!*cache)
3228 return PyObject_GetAttrString(o, meth);
3229 else
3230 return PyObject_GetAttr(o, *cache);
3232 res = _PyObject_LookupSpecial(o, meth, cache);
3233 if (res == NULL && !PyErr_Occurred()) {
3234 PyErr_SetObject(PyExc_AttributeError, *cache);
3235 return NULL;
3237 return res;
3241 static PyObject *
3242 kwd_as_string(PyObject *kwd) {
3243 #ifdef Py_USING_UNICODE
3244 if (PyString_Check(kwd)) {
3245 #else
3246 assert(PyString_Check(kwd));
3247 #endif
3248 Py_INCREF(kwd);
3249 return kwd;
3250 #ifdef Py_USING_UNICODE
3252 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3253 #endif
3257 /* Implementation notes for set_exc_info() and reset_exc_info():
3259 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3260 'exc_traceback'. These always travel together.
3262 - tstate->curexc_ZZZ is the "hot" exception that is set by
3263 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3265 - Once an exception is caught by an except clause, it is transferred
3266 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3267 can pick it up. This is the primary task of set_exc_info().
3268 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
3270 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3272 Long ago, when none of this existed, there were just a few globals:
3273 one set corresponding to the "hot" exception, and one set
3274 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3275 globals; they were simply stored as sys.exc_ZZZ. For backwards
3276 compatibility, they still are!) The problem was that in code like
3277 this:
3279 try:
3280 "something that may fail"
3281 except "some exception":
3282 "do something else first"
3283 "print the exception from sys.exc_ZZZ."
3285 if "do something else first" invoked something that raised and caught
3286 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3287 cause of subtle bugs. I fixed this by changing the semantics as
3288 follows:
3290 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3291 *in that frame*.
3293 - But initially, and as long as no exception is caught in a given
3294 frame, sys.exc_ZZZ will hold the last exception caught in the
3295 previous frame (or the frame before that, etc.).
3297 The first bullet fixed the bug in the above example. The second
3298 bullet was for backwards compatibility: it was (and is) common to
3299 have a function that is called when an exception is caught, and to
3300 have that function access the caught exception via sys.exc_ZZZ.
3301 (Example: traceback.print_exc()).
3303 At the same time I fixed the problem that sys.exc_ZZZ weren't
3304 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3305 but that's really a separate improvement.
3307 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3308 variables to what they were before the current frame was called. The
3309 set_exc_info() function saves them on the frame so that
3310 reset_exc_info() can restore them. The invariant is that
3311 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3312 exception (where "catching" an exception applies only to successful
3313 except clauses); and if the current frame ever caught an exception,
3314 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3315 at the start of the current frame.
3319 static void
3320 set_exc_info(PyThreadState *tstate,
3321 PyObject *type, PyObject *value, PyObject *tb)
3323 PyFrameObject *frame = tstate->frame;
3324 PyObject *tmp_type, *tmp_value, *tmp_tb;
3326 assert(type != NULL);
3327 assert(frame != NULL);
3328 if (frame->f_exc_type == NULL) {
3329 assert(frame->f_exc_value == NULL);
3330 assert(frame->f_exc_traceback == NULL);
3331 /* This frame didn't catch an exception before. */
3332 /* Save previous exception of this thread in this frame. */
3333 if (tstate->exc_type == NULL) {
3334 /* XXX Why is this set to Py_None? */
3335 Py_INCREF(Py_None);
3336 tstate->exc_type = Py_None;
3338 Py_INCREF(tstate->exc_type);
3339 Py_XINCREF(tstate->exc_value);
3340 Py_XINCREF(tstate->exc_traceback);
3341 frame->f_exc_type = tstate->exc_type;
3342 frame->f_exc_value = tstate->exc_value;
3343 frame->f_exc_traceback = tstate->exc_traceback;
3345 /* Set new exception for this thread. */
3346 tmp_type = tstate->exc_type;
3347 tmp_value = tstate->exc_value;
3348 tmp_tb = tstate->exc_traceback;
3349 Py_INCREF(type);
3350 Py_XINCREF(value);
3351 Py_XINCREF(tb);
3352 tstate->exc_type = type;
3353 tstate->exc_value = value;
3354 tstate->exc_traceback = tb;
3355 Py_XDECREF(tmp_type);
3356 Py_XDECREF(tmp_value);
3357 Py_XDECREF(tmp_tb);
3358 /* For b/w compatibility */
3359 PySys_SetObject("exc_type", type);
3360 PySys_SetObject("exc_value", value);
3361 PySys_SetObject("exc_traceback", tb);
3364 static void
3365 reset_exc_info(PyThreadState *tstate)
3367 PyFrameObject *frame;
3368 PyObject *tmp_type, *tmp_value, *tmp_tb;
3370 /* It's a precondition that the thread state's frame caught an
3371 * exception -- verify in a debug build.
3373 assert(tstate != NULL);
3374 frame = tstate->frame;
3375 assert(frame != NULL);
3376 assert(frame->f_exc_type != NULL);
3378 /* Copy the frame's exception info back to the thread state. */
3379 tmp_type = tstate->exc_type;
3380 tmp_value = tstate->exc_value;
3381 tmp_tb = tstate->exc_traceback;
3382 Py_INCREF(frame->f_exc_type);
3383 Py_XINCREF(frame->f_exc_value);
3384 Py_XINCREF(frame->f_exc_traceback);
3385 tstate->exc_type = frame->f_exc_type;
3386 tstate->exc_value = frame->f_exc_value;
3387 tstate->exc_traceback = frame->f_exc_traceback;
3388 Py_XDECREF(tmp_type);
3389 Py_XDECREF(tmp_value);
3390 Py_XDECREF(tmp_tb);
3392 /* For b/w compatibility */
3393 PySys_SetObject("exc_type", frame->f_exc_type);
3394 PySys_SetObject("exc_value", frame->f_exc_value);
3395 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3397 /* Clear the frame's exception info. */
3398 tmp_type = frame->f_exc_type;
3399 tmp_value = frame->f_exc_value;
3400 tmp_tb = frame->f_exc_traceback;
3401 frame->f_exc_type = NULL;
3402 frame->f_exc_value = NULL;
3403 frame->f_exc_traceback = NULL;
3404 Py_DECREF(tmp_type);
3405 Py_XDECREF(tmp_value);
3406 Py_XDECREF(tmp_tb);
3409 /* Logic for the raise statement (too complicated for inlining).
3410 This *consumes* a reference count to each of its arguments. */
3411 static enum why_code
3412 do_raise(PyObject *type, PyObject *value, PyObject *tb)
3414 if (type == NULL) {
3415 /* Reraise */
3416 PyThreadState *tstate = PyThreadState_GET();
3417 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3418 value = tstate->exc_value;
3419 tb = tstate->exc_traceback;
3420 Py_XINCREF(type);
3421 Py_XINCREF(value);
3422 Py_XINCREF(tb);
3425 /* We support the following forms of raise:
3426 raise <class>, <classinstance>
3427 raise <class>, <argument tuple>
3428 raise <class>, None
3429 raise <class>, <argument>
3430 raise <classinstance>, None
3431 raise <string>, <object>
3432 raise <string>, None
3434 An omitted second argument is the same as None.
3436 In addition, raise <tuple>, <anything> is the same as
3437 raising the tuple's first item (and it better have one!);
3438 this rule is applied recursively.
3440 Finally, an optional third argument can be supplied, which
3441 gives the traceback to be substituted (useful when
3442 re-raising an exception after examining it). */
3444 /* First, check the traceback argument, replacing None with
3445 NULL. */
3446 if (tb == Py_None) {
3447 Py_DECREF(tb);
3448 tb = NULL;
3450 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3451 PyErr_SetString(PyExc_TypeError,
3452 "raise: arg 3 must be a traceback or None");
3453 goto raise_error;
3456 /* Next, replace a missing value with None */
3457 if (value == NULL) {
3458 value = Py_None;
3459 Py_INCREF(value);
3462 /* Next, repeatedly, replace a tuple exception with its first item */
3463 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3464 PyObject *tmp = type;
3465 type = PyTuple_GET_ITEM(type, 0);
3466 Py_INCREF(type);
3467 Py_DECREF(tmp);
3470 if (PyExceptionClass_Check(type))
3471 PyErr_NormalizeException(&type, &value, &tb);
3473 else if (PyExceptionInstance_Check(type)) {
3474 /* Raising an instance. The value should be a dummy. */
3475 if (value != Py_None) {
3476 PyErr_SetString(PyExc_TypeError,
3477 "instance exception may not have a separate value");
3478 goto raise_error;
3480 else {
3481 /* Normalize to raise <class>, <instance> */
3482 Py_DECREF(value);
3483 value = type;
3484 type = PyExceptionInstance_Class(type);
3485 Py_INCREF(type);
3488 else {
3489 /* Not something you can raise. You get an exception
3490 anyway, just not what you specified :-) */
3491 PyErr_Format(PyExc_TypeError,
3492 "exceptions must be classes or instances, not %s",
3493 type->ob_type->tp_name);
3494 goto raise_error;
3497 assert(PyExceptionClass_Check(type));
3498 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3499 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3500 "exceptions must derive from BaseException "
3501 "in 3.x", 1) < 0)
3502 goto raise_error;
3505 PyErr_Restore(type, value, tb);
3506 if (tb == NULL)
3507 return WHY_EXCEPTION;
3508 else
3509 return WHY_RERAISE;
3510 raise_error:
3511 Py_XDECREF(value);
3512 Py_XDECREF(type);
3513 Py_XDECREF(tb);
3514 return WHY_EXCEPTION;
3517 /* Iterate v argcnt times and store the results on the stack (via decreasing
3518 sp). Return 1 for success, 0 if error. */
3520 static int
3521 unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
3523 int i = 0;
3524 PyObject *it; /* iter(v) */
3525 PyObject *w;
3527 assert(v != NULL);
3529 it = PyObject_GetIter(v);
3530 if (it == NULL)
3531 goto Error;
3533 for (; i < argcnt; i++) {
3534 w = PyIter_Next(it);
3535 if (w == NULL) {
3536 /* Iterator done, via error or exhaustion. */
3537 if (!PyErr_Occurred()) {
3538 PyErr_Format(PyExc_ValueError,
3539 "need more than %d value%s to unpack",
3540 i, i == 1 ? "" : "s");
3542 goto Error;
3544 *--sp = w;
3547 /* We better have exhausted the iterator now. */
3548 w = PyIter_Next(it);
3549 if (w == NULL) {
3550 if (PyErr_Occurred())
3551 goto Error;
3552 Py_DECREF(it);
3553 return 1;
3555 Py_DECREF(w);
3556 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3557 /* fall through */
3558 Error:
3559 for (; i > 0; i--, sp++)
3560 Py_DECREF(*sp);
3561 Py_XDECREF(it);
3562 return 0;
3566 #ifdef LLTRACE
3567 static int
3568 prtrace(PyObject *v, char *str)
3570 printf("%s ", str);
3571 if (PyObject_Print(v, stdout, 0) != 0)
3572 PyErr_Clear(); /* Don't know what else to do */
3573 printf("\n");
3574 return 1;
3576 #endif
3578 static void
3579 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
3581 PyObject *type, *value, *traceback, *arg;
3582 int err;
3583 PyErr_Fetch(&type, &value, &traceback);
3584 if (value == NULL) {
3585 value = Py_None;
3586 Py_INCREF(value);
3588 arg = PyTuple_Pack(3, type, value, traceback);
3589 if (arg == NULL) {
3590 PyErr_Restore(type, value, traceback);
3591 return;
3593 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3594 Py_DECREF(arg);
3595 if (err == 0)
3596 PyErr_Restore(type, value, traceback);
3597 else {
3598 Py_XDECREF(type);
3599 Py_XDECREF(value);
3600 Py_XDECREF(traceback);
3604 static int
3605 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3606 int what, PyObject *arg)
3608 PyObject *type, *value, *traceback;
3609 int err;
3610 PyErr_Fetch(&type, &value, &traceback);
3611 err = call_trace(func, obj, frame, what, arg);
3612 if (err == 0)
3614 PyErr_Restore(type, value, traceback);
3615 return 0;
3617 else {
3618 Py_XDECREF(type);
3619 Py_XDECREF(value);
3620 Py_XDECREF(traceback);
3621 return -1;
3625 static int
3626 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3627 int what, PyObject *arg)
3629 register PyThreadState *tstate = frame->f_tstate;
3630 int result;
3631 if (tstate->tracing)
3632 return 0;
3633 tstate->tracing++;
3634 tstate->use_tracing = 0;
3635 result = func(obj, frame, what, arg);
3636 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3637 || (tstate->c_profilefunc != NULL));
3638 tstate->tracing--;
3639 return result;
3642 PyObject *
3643 _PyEval_CallTracing(PyObject *func, PyObject *args)
3645 PyFrameObject *frame = PyEval_GetFrame();
3646 PyThreadState *tstate = frame->f_tstate;
3647 int save_tracing = tstate->tracing;
3648 int save_use_tracing = tstate->use_tracing;
3649 PyObject *result;
3651 tstate->tracing = 0;
3652 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3653 || (tstate->c_profilefunc != NULL));
3654 result = PyObject_Call(func, args, NULL);
3655 tstate->tracing = save_tracing;
3656 tstate->use_tracing = save_use_tracing;
3657 return result;
3660 /* See Objects/lnotab_notes.txt for a description of how tracing works. */
3661 static int
3662 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
3663 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3664 int *instr_prev)
3666 int result = 0;
3667 int line = frame->f_lineno;
3669 /* If the last instruction executed isn't in the current
3670 instruction window, reset the window.
3672 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3673 PyAddrPair bounds;
3674 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3675 &bounds);
3676 *instr_lb = bounds.ap_lower;
3677 *instr_ub = bounds.ap_upper;
3679 /* If the last instruction falls at the start of a line or if
3680 it represents a jump backwards, update the frame's line
3681 number and call the trace function. */
3682 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3683 frame->f_lineno = line;
3684 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3686 *instr_prev = frame->f_lasti;
3687 return result;
3690 void
3691 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
3693 PyThreadState *tstate = PyThreadState_GET();
3694 PyObject *temp = tstate->c_profileobj;
3695 Py_XINCREF(arg);
3696 tstate->c_profilefunc = NULL;
3697 tstate->c_profileobj = NULL;
3698 /* Must make sure that tracing is not ignored if 'temp' is freed */
3699 tstate->use_tracing = tstate->c_tracefunc != NULL;
3700 Py_XDECREF(temp);
3701 tstate->c_profilefunc = func;
3702 tstate->c_profileobj = arg;
3703 /* Flag that tracing or profiling is turned on */
3704 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
3707 void
3708 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3710 PyThreadState *tstate = PyThreadState_GET();
3711 PyObject *temp = tstate->c_traceobj;
3712 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3713 Py_XINCREF(arg);
3714 tstate->c_tracefunc = NULL;
3715 tstate->c_traceobj = NULL;
3716 /* Must make sure that profiling is not ignored if 'temp' is freed */
3717 tstate->use_tracing = tstate->c_profilefunc != NULL;
3718 Py_XDECREF(temp);
3719 tstate->c_tracefunc = func;
3720 tstate->c_traceobj = arg;
3721 /* Flag that tracing or profiling is turned on */
3722 tstate->use_tracing = ((func != NULL)
3723 || (tstate->c_profilefunc != NULL));
3726 PyObject *
3727 PyEval_GetBuiltins(void)
3729 PyFrameObject *current_frame = PyEval_GetFrame();
3730 if (current_frame == NULL)
3731 return PyThreadState_GET()->interp->builtins;
3732 else
3733 return current_frame->f_builtins;
3736 PyObject *
3737 PyEval_GetLocals(void)
3739 PyFrameObject *current_frame = PyEval_GetFrame();
3740 if (current_frame == NULL)
3741 return NULL;
3742 PyFrame_FastToLocals(current_frame);
3743 return current_frame->f_locals;
3746 PyObject *
3747 PyEval_GetGlobals(void)
3749 PyFrameObject *current_frame = PyEval_GetFrame();
3750 if (current_frame == NULL)
3751 return NULL;
3752 else
3753 return current_frame->f_globals;
3756 PyFrameObject *
3757 PyEval_GetFrame(void)
3759 PyThreadState *tstate = PyThreadState_GET();
3760 return _PyThreadState_GetFrame(tstate);
3764 PyEval_GetRestricted(void)
3766 PyFrameObject *current_frame = PyEval_GetFrame();
3767 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
3771 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
3773 PyFrameObject *current_frame = PyEval_GetFrame();
3774 int result = cf->cf_flags != 0;
3776 if (current_frame != NULL) {
3777 const int codeflags = current_frame->f_code->co_flags;
3778 const int compilerflags = codeflags & PyCF_MASK;
3779 if (compilerflags) {
3780 result = 1;
3781 cf->cf_flags |= compilerflags;
3783 #if 0 /* future keyword */
3784 if (codeflags & CO_GENERATOR_ALLOWED) {
3785 result = 1;
3786 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3788 #endif
3790 return result;
3794 Py_FlushLine(void)
3796 PyObject *f = PySys_GetObject("stdout");
3797 if (f == NULL)
3798 return 0;
3799 if (!PyFile_SoftSpace(f, 0))
3800 return 0;
3801 return PyFile_WriteString("\n", f);
3805 /* External interface to call any callable object.
3806 The arg must be a tuple or NULL. */
3808 #undef PyEval_CallObject
3809 /* for backward compatibility: export this interface */
3811 PyObject *
3812 PyEval_CallObject(PyObject *func, PyObject *arg)
3814 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
3816 #define PyEval_CallObject(func,arg) \
3817 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3819 PyObject *
3820 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
3822 PyObject *result;
3824 if (arg == NULL) {
3825 arg = PyTuple_New(0);
3826 if (arg == NULL)
3827 return NULL;
3829 else if (!PyTuple_Check(arg)) {
3830 PyErr_SetString(PyExc_TypeError,
3831 "argument list must be a tuple");
3832 return NULL;
3834 else
3835 Py_INCREF(arg);
3837 if (kw != NULL && !PyDict_Check(kw)) {
3838 PyErr_SetString(PyExc_TypeError,
3839 "keyword list must be a dictionary");
3840 Py_DECREF(arg);
3841 return NULL;
3844 result = PyObject_Call(func, arg, kw);
3845 Py_DECREF(arg);
3846 return result;
3849 const char *
3850 PyEval_GetFuncName(PyObject *func)
3852 if (PyMethod_Check(func))
3853 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3854 else if (PyFunction_Check(func))
3855 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3856 else if (PyCFunction_Check(func))
3857 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3858 else if (PyClass_Check(func))
3859 return PyString_AsString(((PyClassObject*)func)->cl_name);
3860 else if (PyInstance_Check(func)) {
3861 return PyString_AsString(
3862 ((PyInstanceObject*)func)->in_class->cl_name);
3863 } else {
3864 return func->ob_type->tp_name;
3868 const char *
3869 PyEval_GetFuncDesc(PyObject *func)
3871 if (PyMethod_Check(func))
3872 return "()";
3873 else if (PyFunction_Check(func))
3874 return "()";
3875 else if (PyCFunction_Check(func))
3876 return "()";
3877 else if (PyClass_Check(func))
3878 return " constructor";
3879 else if (PyInstance_Check(func)) {
3880 return " instance";
3881 } else {
3882 return " object";
3886 static void
3887 err_args(PyObject *func, int flags, int nargs)
3889 if (flags & METH_NOARGS)
3890 PyErr_Format(PyExc_TypeError,
3891 "%.200s() takes no arguments (%d given)",
3892 ((PyCFunctionObject *)func)->m_ml->ml_name,
3893 nargs);
3894 else
3895 PyErr_Format(PyExc_TypeError,
3896 "%.200s() takes exactly one argument (%d given)",
3897 ((PyCFunctionObject *)func)->m_ml->ml_name,
3898 nargs);
3901 #define C_TRACE(x, call) \
3902 if (tstate->use_tracing && tstate->c_profilefunc) { \
3903 if (call_trace(tstate->c_profilefunc, \
3904 tstate->c_profileobj, \
3905 tstate->frame, PyTrace_C_CALL, \
3906 func)) { \
3907 x = NULL; \
3909 else { \
3910 x = call; \
3911 if (tstate->c_profilefunc != NULL) { \
3912 if (x == NULL) { \
3913 call_trace_protected(tstate->c_profilefunc, \
3914 tstate->c_profileobj, \
3915 tstate->frame, PyTrace_C_EXCEPTION, \
3916 func); \
3917 /* XXX should pass (type, value, tb) */ \
3918 } else { \
3919 if (call_trace(tstate->c_profilefunc, \
3920 tstate->c_profileobj, \
3921 tstate->frame, PyTrace_C_RETURN, \
3922 func)) { \
3923 Py_DECREF(x); \
3924 x = NULL; \
3929 } else { \
3930 x = call; \
3933 static PyObject *
3934 call_function(PyObject ***pp_stack, int oparg
3935 #ifdef WITH_TSC
3936 , uint64* pintr0, uint64* pintr1
3937 #endif
3940 int na = oparg & 0xff;
3941 int nk = (oparg>>8) & 0xff;
3942 int n = na + 2 * nk;
3943 PyObject **pfunc = (*pp_stack) - n - 1;
3944 PyObject *func = *pfunc;
3945 PyObject *x, *w;
3947 /* Always dispatch PyCFunction first, because these are
3948 presumed to be the most frequent callable object.
3950 if (PyCFunction_Check(func) && nk == 0) {
3951 int flags = PyCFunction_GET_FLAGS(func);
3952 PyThreadState *tstate = PyThreadState_GET();
3954 PCALL(PCALL_CFUNCTION);
3955 if (flags & (METH_NOARGS | METH_O)) {
3956 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3957 PyObject *self = PyCFunction_GET_SELF(func);
3958 if (flags & METH_NOARGS && na == 0) {
3959 C_TRACE(x, (*meth)(self,NULL));
3961 else if (flags & METH_O && na == 1) {
3962 PyObject *arg = EXT_POP(*pp_stack);
3963 C_TRACE(x, (*meth)(self,arg));
3964 Py_DECREF(arg);
3966 else {
3967 err_args(func, flags, na);
3968 x = NULL;
3971 else {
3972 PyObject *callargs;
3973 callargs = load_args(pp_stack, na);
3974 READ_TIMESTAMP(*pintr0);
3975 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3976 READ_TIMESTAMP(*pintr1);
3977 Py_XDECREF(callargs);
3979 } else {
3980 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3981 /* optimize access to bound methods */
3982 PyObject *self = PyMethod_GET_SELF(func);
3983 PCALL(PCALL_METHOD);
3984 PCALL(PCALL_BOUND_METHOD);
3985 Py_INCREF(self);
3986 func = PyMethod_GET_FUNCTION(func);
3987 Py_INCREF(func);
3988 Py_DECREF(*pfunc);
3989 *pfunc = self;
3990 na++;
3991 n++;
3992 } else
3993 Py_INCREF(func);
3994 READ_TIMESTAMP(*pintr0);
3995 if (PyFunction_Check(func))
3996 x = fast_function(func, pp_stack, n, na, nk);
3997 else
3998 x = do_call(func, pp_stack, na, nk);
3999 READ_TIMESTAMP(*pintr1);
4000 Py_DECREF(func);
4003 /* Clear the stack of the function object. Also removes
4004 the arguments in case they weren't consumed already
4005 (fast_function() and err_args() leave them on the stack).
4007 while ((*pp_stack) > pfunc) {
4008 w = EXT_POP(*pp_stack);
4009 Py_DECREF(w);
4010 PCALL(PCALL_POP);
4012 return x;
4015 /* The fast_function() function optimize calls for which no argument
4016 tuple is necessary; the objects are passed directly from the stack.
4017 For the simplest case -- a function that takes only positional
4018 arguments and is called with only positional arguments -- it
4019 inlines the most primitive frame setup code from
4020 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4021 done before evaluating the frame.
4024 static PyObject *
4025 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
4027 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4028 PyObject *globals = PyFunction_GET_GLOBALS(func);
4029 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4030 PyObject **d = NULL;
4031 int nd = 0;
4033 PCALL(PCALL_FUNCTION);
4034 PCALL(PCALL_FAST_FUNCTION);
4035 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4036 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4037 PyFrameObject *f;
4038 PyObject *retval = NULL;
4039 PyThreadState *tstate = PyThreadState_GET();
4040 PyObject **fastlocals, **stack;
4041 int i;
4043 PCALL(PCALL_FASTER_FUNCTION);
4044 assert(globals != NULL);
4045 /* XXX Perhaps we should create a specialized
4046 PyFrame_New() that doesn't take locals, but does
4047 take builtins without sanity checking them.
4049 assert(tstate != NULL);
4050 f = PyFrame_New(tstate, co, globals, NULL);
4051 if (f == NULL)
4052 return NULL;
4054 fastlocals = f->f_localsplus;
4055 stack = (*pp_stack) - n;
4057 for (i = 0; i < n; i++) {
4058 Py_INCREF(*stack);
4059 fastlocals[i] = *stack++;
4061 retval = PyEval_EvalFrameEx(f,0);
4062 ++tstate->recursion_depth;
4063 Py_DECREF(f);
4064 --tstate->recursion_depth;
4065 return retval;
4067 if (argdefs != NULL) {
4068 d = &PyTuple_GET_ITEM(argdefs, 0);
4069 nd = Py_SIZE(argdefs);
4071 return PyEval_EvalCodeEx(co, globals,
4072 (PyObject *)NULL, (*pp_stack)-n, na,
4073 (*pp_stack)-2*nk, nk, d, nd,
4074 PyFunction_GET_CLOSURE(func));
4077 static PyObject *
4078 update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4079 PyObject *func)
4081 PyObject *kwdict = NULL;
4082 if (orig_kwdict == NULL)
4083 kwdict = PyDict_New();
4084 else {
4085 kwdict = PyDict_Copy(orig_kwdict);
4086 Py_DECREF(orig_kwdict);
4088 if (kwdict == NULL)
4089 return NULL;
4090 while (--nk >= 0) {
4091 int err;
4092 PyObject *value = EXT_POP(*pp_stack);
4093 PyObject *key = EXT_POP(*pp_stack);
4094 if (PyDict_GetItem(kwdict, key) != NULL) {
4095 PyErr_Format(PyExc_TypeError,
4096 "%.200s%s got multiple values "
4097 "for keyword argument '%.200s'",
4098 PyEval_GetFuncName(func),
4099 PyEval_GetFuncDesc(func),
4100 PyString_AsString(key));
4101 Py_DECREF(key);
4102 Py_DECREF(value);
4103 Py_DECREF(kwdict);
4104 return NULL;
4106 err = PyDict_SetItem(kwdict, key, value);
4107 Py_DECREF(key);
4108 Py_DECREF(value);
4109 if (err) {
4110 Py_DECREF(kwdict);
4111 return NULL;
4114 return kwdict;
4117 static PyObject *
4118 update_star_args(int nstack, int nstar, PyObject *stararg,
4119 PyObject ***pp_stack)
4121 PyObject *callargs, *w;
4123 callargs = PyTuple_New(nstack + nstar);
4124 if (callargs == NULL) {
4125 return NULL;
4127 if (nstar) {
4128 int i;
4129 for (i = 0; i < nstar; i++) {
4130 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4131 Py_INCREF(a);
4132 PyTuple_SET_ITEM(callargs, nstack + i, a);
4135 while (--nstack >= 0) {
4136 w = EXT_POP(*pp_stack);
4137 PyTuple_SET_ITEM(callargs, nstack, w);
4139 return callargs;
4142 static PyObject *
4143 load_args(PyObject ***pp_stack, int na)
4145 PyObject *args = PyTuple_New(na);
4146 PyObject *w;
4148 if (args == NULL)
4149 return NULL;
4150 while (--na >= 0) {
4151 w = EXT_POP(*pp_stack);
4152 PyTuple_SET_ITEM(args, na, w);
4154 return args;
4157 static PyObject *
4158 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4160 PyObject *callargs = NULL;
4161 PyObject *kwdict = NULL;
4162 PyObject *result = NULL;
4164 if (nk > 0) {
4165 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4166 if (kwdict == NULL)
4167 goto call_fail;
4169 callargs = load_args(pp_stack, na);
4170 if (callargs == NULL)
4171 goto call_fail;
4172 #ifdef CALL_PROFILE
4173 /* At this point, we have to look at the type of func to
4174 update the call stats properly. Do it here so as to avoid
4175 exposing the call stats machinery outside ceval.c
4177 if (PyFunction_Check(func))
4178 PCALL(PCALL_FUNCTION);
4179 else if (PyMethod_Check(func))
4180 PCALL(PCALL_METHOD);
4181 else if (PyType_Check(func))
4182 PCALL(PCALL_TYPE);
4183 else if (PyCFunction_Check(func))
4184 PCALL(PCALL_CFUNCTION);
4185 else
4186 PCALL(PCALL_OTHER);
4187 #endif
4188 if (PyCFunction_Check(func)) {
4189 PyThreadState *tstate = PyThreadState_GET();
4190 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4192 else
4193 result = PyObject_Call(func, callargs, kwdict);
4194 call_fail:
4195 Py_XDECREF(callargs);
4196 Py_XDECREF(kwdict);
4197 return result;
4200 static PyObject *
4201 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4203 int nstar = 0;
4204 PyObject *callargs = NULL;
4205 PyObject *stararg = NULL;
4206 PyObject *kwdict = NULL;
4207 PyObject *result = NULL;
4209 if (flags & CALL_FLAG_KW) {
4210 kwdict = EXT_POP(*pp_stack);
4211 if (!PyDict_Check(kwdict)) {
4212 PyObject *d;
4213 d = PyDict_New();
4214 if (d == NULL)
4215 goto ext_call_fail;
4216 if (PyDict_Update(d, kwdict) != 0) {
4217 Py_DECREF(d);
4218 /* PyDict_Update raises attribute
4219 * error (percolated from an attempt
4220 * to get 'keys' attribute) instead of
4221 * a type error if its second argument
4222 * is not a mapping.
4224 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4225 PyErr_Format(PyExc_TypeError,
4226 "%.200s%.200s argument after ** "
4227 "must be a mapping, not %.200s",
4228 PyEval_GetFuncName(func),
4229 PyEval_GetFuncDesc(func),
4230 kwdict->ob_type->tp_name);
4232 goto ext_call_fail;
4234 Py_DECREF(kwdict);
4235 kwdict = d;
4238 if (flags & CALL_FLAG_VAR) {
4239 stararg = EXT_POP(*pp_stack);
4240 if (!PyTuple_Check(stararg)) {
4241 PyObject *t = NULL;
4242 t = PySequence_Tuple(stararg);
4243 if (t == NULL) {
4244 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4245 PyErr_Format(PyExc_TypeError,
4246 "%.200s%.200s argument after * "
4247 "must be a sequence, not %200s",
4248 PyEval_GetFuncName(func),
4249 PyEval_GetFuncDesc(func),
4250 stararg->ob_type->tp_name);
4252 goto ext_call_fail;
4254 Py_DECREF(stararg);
4255 stararg = t;
4257 nstar = PyTuple_GET_SIZE(stararg);
4259 if (nk > 0) {
4260 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4261 if (kwdict == NULL)
4262 goto ext_call_fail;
4264 callargs = update_star_args(na, nstar, stararg, pp_stack);
4265 if (callargs == NULL)
4266 goto ext_call_fail;
4267 #ifdef CALL_PROFILE
4268 /* At this point, we have to look at the type of func to
4269 update the call stats properly. Do it here so as to avoid
4270 exposing the call stats machinery outside ceval.c
4272 if (PyFunction_Check(func))
4273 PCALL(PCALL_FUNCTION);
4274 else if (PyMethod_Check(func))
4275 PCALL(PCALL_METHOD);
4276 else if (PyType_Check(func))
4277 PCALL(PCALL_TYPE);
4278 else if (PyCFunction_Check(func))
4279 PCALL(PCALL_CFUNCTION);
4280 else
4281 PCALL(PCALL_OTHER);
4282 #endif
4283 if (PyCFunction_Check(func)) {
4284 PyThreadState *tstate = PyThreadState_GET();
4285 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4287 else
4288 result = PyObject_Call(func, callargs, kwdict);
4289 ext_call_fail:
4290 Py_XDECREF(callargs);
4291 Py_XDECREF(kwdict);
4292 Py_XDECREF(stararg);
4293 return result;
4296 /* Extract a slice index from a PyInt or PyLong or an object with the
4297 nb_index slot defined, and store in *pi.
4298 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4299 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
4300 Return 0 on error, 1 on success.
4302 /* Note: If v is NULL, return success without storing into *pi. This
4303 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4304 called by the SLICE opcode with v and/or w equal to NULL.
4307 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
4309 if (v != NULL) {
4310 Py_ssize_t x;
4311 if (PyInt_Check(v)) {
4312 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4313 however, it looks like it should be AsSsize_t.
4314 There should be a comment here explaining why.
4316 x = PyInt_AS_LONG(v);
4318 else if (PyIndex_Check(v)) {
4319 x = PyNumber_AsSsize_t(v, NULL);
4320 if (x == -1 && PyErr_Occurred())
4321 return 0;
4323 else {
4324 PyErr_SetString(PyExc_TypeError,
4325 "slice indices must be integers or "
4326 "None or have an __index__ method");
4327 return 0;
4329 *pi = x;
4331 return 1;
4334 #undef ISINDEX
4335 #define ISINDEX(x) ((x) == NULL || \
4336 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
4338 static PyObject *
4339 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
4341 PyTypeObject *tp = u->ob_type;
4342 PySequenceMethods *sq = tp->tp_as_sequence;
4344 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4345 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4346 if (!_PyEval_SliceIndex(v, &ilow))
4347 return NULL;
4348 if (!_PyEval_SliceIndex(w, &ihigh))
4349 return NULL;
4350 return PySequence_GetSlice(u, ilow, ihigh);
4352 else {
4353 PyObject *slice = PySlice_New(v, w, NULL);
4354 if (slice != NULL) {
4355 PyObject *res = PyObject_GetItem(u, slice);
4356 Py_DECREF(slice);
4357 return res;
4359 else
4360 return NULL;
4364 static int
4365 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4366 /* u[v:w] = x */
4368 PyTypeObject *tp = u->ob_type;
4369 PySequenceMethods *sq = tp->tp_as_sequence;
4371 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4372 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4373 if (!_PyEval_SliceIndex(v, &ilow))
4374 return -1;
4375 if (!_PyEval_SliceIndex(w, &ihigh))
4376 return -1;
4377 if (x == NULL)
4378 return PySequence_DelSlice(u, ilow, ihigh);
4379 else
4380 return PySequence_SetSlice(u, ilow, ihigh, x);
4382 else {
4383 PyObject *slice = PySlice_New(v, w, NULL);
4384 if (slice != NULL) {
4385 int res;
4386 if (x != NULL)
4387 res = PyObject_SetItem(u, slice, x);
4388 else
4389 res = PyObject_DelItem(u, slice);
4390 Py_DECREF(slice);
4391 return res;
4393 else
4394 return -1;
4398 #define Py3kExceptionClass_Check(x) \
4399 (PyType_Check((x)) && \
4400 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4402 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4403 "BaseException is not allowed in 3.x"
4405 static PyObject *
4406 cmp_outcome(int op, register PyObject *v, register PyObject *w)
4408 int res = 0;
4409 switch (op) {
4410 case PyCmp_IS:
4411 res = (v == w);
4412 break;
4413 case PyCmp_IS_NOT:
4414 res = (v != w);
4415 break;
4416 case PyCmp_IN:
4417 res = PySequence_Contains(w, v);
4418 if (res < 0)
4419 return NULL;
4420 break;
4421 case PyCmp_NOT_IN:
4422 res = PySequence_Contains(w, v);
4423 if (res < 0)
4424 return NULL;
4425 res = !res;
4426 break;
4427 case PyCmp_EXC_MATCH:
4428 if (PyTuple_Check(w)) {
4429 Py_ssize_t i, length;
4430 length = PyTuple_Size(w);
4431 for (i = 0; i < length; i += 1) {
4432 PyObject *exc = PyTuple_GET_ITEM(w, i);
4433 if (PyString_Check(exc)) {
4434 int ret_val;
4435 ret_val = PyErr_WarnEx(
4436 PyExc_DeprecationWarning,
4437 "catching of string "
4438 "exceptions is deprecated", 1);
4439 if (ret_val < 0)
4440 return NULL;
4442 else if (Py_Py3kWarningFlag &&
4443 !PyTuple_Check(exc) &&
4444 !Py3kExceptionClass_Check(exc))
4446 int ret_val;
4447 ret_val = PyErr_WarnEx(
4448 PyExc_DeprecationWarning,
4449 CANNOT_CATCH_MSG, 1);
4450 if (ret_val < 0)
4451 return NULL;
4455 else {
4456 if (PyString_Check(w)) {
4457 int ret_val;
4458 ret_val = PyErr_WarnEx(
4459 PyExc_DeprecationWarning,
4460 "catching of string "
4461 "exceptions is deprecated", 1);
4462 if (ret_val < 0)
4463 return NULL;
4465 else if (Py_Py3kWarningFlag &&
4466 !PyTuple_Check(w) &&
4467 !Py3kExceptionClass_Check(w))
4469 int ret_val;
4470 ret_val = PyErr_WarnEx(
4471 PyExc_DeprecationWarning,
4472 CANNOT_CATCH_MSG, 1);
4473 if (ret_val < 0)
4474 return NULL;
4477 res = PyErr_GivenExceptionMatches(v, w);
4478 break;
4479 default:
4480 return PyObject_RichCompare(v, w, op);
4482 v = res ? Py_True : Py_False;
4483 Py_INCREF(v);
4484 return v;
4487 static PyObject *
4488 import_from(PyObject *v, PyObject *name)
4490 PyObject *x;
4492 x = PyObject_GetAttr(v, name);
4493 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4494 PyErr_Format(PyExc_ImportError,
4495 "cannot import name %.230s",
4496 PyString_AsString(name));
4498 return x;
4501 static int
4502 import_all_from(PyObject *locals, PyObject *v)
4504 PyObject *all = PyObject_GetAttrString(v, "__all__");
4505 PyObject *dict, *name, *value;
4506 int skip_leading_underscores = 0;
4507 int pos, err;
4509 if (all == NULL) {
4510 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4511 return -1; /* Unexpected error */
4512 PyErr_Clear();
4513 dict = PyObject_GetAttrString(v, "__dict__");
4514 if (dict == NULL) {
4515 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4516 return -1;
4517 PyErr_SetString(PyExc_ImportError,
4518 "from-import-* object has no __dict__ and no __all__");
4519 return -1;
4521 all = PyMapping_Keys(dict);
4522 Py_DECREF(dict);
4523 if (all == NULL)
4524 return -1;
4525 skip_leading_underscores = 1;
4528 for (pos = 0, err = 0; ; pos++) {
4529 name = PySequence_GetItem(all, pos);
4530 if (name == NULL) {
4531 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4532 err = -1;
4533 else
4534 PyErr_Clear();
4535 break;
4537 if (skip_leading_underscores &&
4538 PyString_Check(name) &&
4539 PyString_AS_STRING(name)[0] == '_')
4541 Py_DECREF(name);
4542 continue;
4544 value = PyObject_GetAttr(v, name);
4545 if (value == NULL)
4546 err = -1;
4547 else if (PyDict_CheckExact(locals))
4548 err = PyDict_SetItem(locals, name, value);
4549 else
4550 err = PyObject_SetItem(locals, name, value);
4551 Py_DECREF(name);
4552 Py_XDECREF(value);
4553 if (err != 0)
4554 break;
4556 Py_DECREF(all);
4557 return err;
4560 static PyObject *
4561 build_class(PyObject *methods, PyObject *bases, PyObject *name)
4563 PyObject *metaclass = NULL, *result, *base;
4565 if (PyDict_Check(methods))
4566 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4567 if (metaclass != NULL)
4568 Py_INCREF(metaclass);
4569 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4570 base = PyTuple_GET_ITEM(bases, 0);
4571 metaclass = PyObject_GetAttrString(base, "__class__");
4572 if (metaclass == NULL) {
4573 PyErr_Clear();
4574 metaclass = (PyObject *)base->ob_type;
4575 Py_INCREF(metaclass);
4578 else {
4579 PyObject *g = PyEval_GetGlobals();
4580 if (g != NULL && PyDict_Check(g))
4581 metaclass = PyDict_GetItemString(g, "__metaclass__");
4582 if (metaclass == NULL)
4583 metaclass = (PyObject *) &PyClass_Type;
4584 Py_INCREF(metaclass);
4586 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4587 NULL);
4588 Py_DECREF(metaclass);
4589 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4590 /* A type error here likely means that the user passed
4591 in a base that was not a class (such the random module
4592 instead of the random.random type). Help them out with
4593 by augmenting the error message with more information.*/
4595 PyObject *ptype, *pvalue, *ptraceback;
4597 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4598 if (PyString_Check(pvalue)) {
4599 PyObject *newmsg;
4600 newmsg = PyString_FromFormat(
4601 "Error when calling the metaclass bases\n"
4602 " %s",
4603 PyString_AS_STRING(pvalue));
4604 if (newmsg != NULL) {
4605 Py_DECREF(pvalue);
4606 pvalue = newmsg;
4609 PyErr_Restore(ptype, pvalue, ptraceback);
4611 return result;
4614 static int
4615 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4616 PyObject *locals)
4618 int n;
4619 PyObject *v;
4620 int plain = 0;
4622 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4623 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4624 /* Backward compatibility hack */
4625 globals = PyTuple_GetItem(prog, 1);
4626 if (n == 3)
4627 locals = PyTuple_GetItem(prog, 2);
4628 prog = PyTuple_GetItem(prog, 0);
4630 if (globals == Py_None) {
4631 globals = PyEval_GetGlobals();
4632 if (locals == Py_None) {
4633 locals = PyEval_GetLocals();
4634 plain = 1;
4636 if (!globals || !locals) {
4637 PyErr_SetString(PyExc_SystemError,
4638 "globals and locals cannot be NULL");
4639 return -1;
4642 else if (locals == Py_None)
4643 locals = globals;
4644 if (!PyString_Check(prog) &&
4645 #ifdef Py_USING_UNICODE
4646 !PyUnicode_Check(prog) &&
4647 #endif
4648 !PyCode_Check(prog) &&
4649 !PyFile_Check(prog)) {
4650 PyErr_SetString(PyExc_TypeError,
4651 "exec: arg 1 must be a string, file, or code object");
4652 return -1;
4654 if (!PyDict_Check(globals)) {
4655 PyErr_SetString(PyExc_TypeError,
4656 "exec: arg 2 must be a dictionary or None");
4657 return -1;
4659 if (!PyMapping_Check(locals)) {
4660 PyErr_SetString(PyExc_TypeError,
4661 "exec: arg 3 must be a mapping or None");
4662 return -1;
4664 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4665 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4666 if (PyCode_Check(prog)) {
4667 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4668 PyErr_SetString(PyExc_TypeError,
4669 "code object passed to exec may not contain free variables");
4670 return -1;
4672 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4674 else if (PyFile_Check(prog)) {
4675 FILE *fp = PyFile_AsFile(prog);
4676 char *name = PyString_AsString(PyFile_Name(prog));
4677 PyCompilerFlags cf;
4678 if (name == NULL)
4679 return -1;
4680 cf.cf_flags = 0;
4681 if (PyEval_MergeCompilerFlags(&cf))
4682 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4683 locals, &cf);
4684 else
4685 v = PyRun_File(fp, name, Py_file_input, globals,
4686 locals);
4688 else {
4689 PyObject *tmp = NULL;
4690 char *str;
4691 PyCompilerFlags cf;
4692 cf.cf_flags = 0;
4693 #ifdef Py_USING_UNICODE
4694 if (PyUnicode_Check(prog)) {
4695 tmp = PyUnicode_AsUTF8String(prog);
4696 if (tmp == NULL)
4697 return -1;
4698 prog = tmp;
4699 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4701 #endif
4702 if (PyString_AsStringAndSize(prog, &str, NULL))
4703 return -1;
4704 if (PyEval_MergeCompilerFlags(&cf))
4705 v = PyRun_StringFlags(str, Py_file_input, globals,
4706 locals, &cf);
4707 else
4708 v = PyRun_String(str, Py_file_input, globals, locals);
4709 Py_XDECREF(tmp);
4711 if (plain)
4712 PyFrame_LocalsToFast(f, 0);
4713 if (v == NULL)
4714 return -1;
4715 Py_DECREF(v);
4716 return 0;
4719 static void
4720 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4722 char *obj_str;
4724 if (!obj)
4725 return;
4727 obj_str = PyString_AsString(obj);
4728 if (!obj_str)
4729 return;
4731 PyErr_Format(exc, format_str, obj_str);
4734 static PyObject *
4735 string_concatenate(PyObject *v, PyObject *w,
4736 PyFrameObject *f, unsigned char *next_instr)
4738 /* This function implements 'variable += expr' when both arguments
4739 are strings. */
4740 Py_ssize_t v_len = PyString_GET_SIZE(v);
4741 Py_ssize_t w_len = PyString_GET_SIZE(w);
4742 Py_ssize_t new_len = v_len + w_len;
4743 if (new_len < 0) {
4744 PyErr_SetString(PyExc_OverflowError,
4745 "strings are too large to concat");
4746 return NULL;
4749 if (v->ob_refcnt == 2) {
4750 /* In the common case, there are 2 references to the value
4751 * stored in 'variable' when the += is performed: one on the
4752 * value stack (in 'v') and one still stored in the
4753 * 'variable'. We try to delete the variable now to reduce
4754 * the refcnt to 1.
4756 switch (*next_instr) {
4757 case STORE_FAST:
4759 int oparg = PEEKARG();
4760 PyObject **fastlocals = f->f_localsplus;
4761 if (GETLOCAL(oparg) == v)
4762 SETLOCAL(oparg, NULL);
4763 break;
4765 case STORE_DEREF:
4767 PyObject **freevars = (f->f_localsplus +
4768 f->f_code->co_nlocals);
4769 PyObject *c = freevars[PEEKARG()];
4770 if (PyCell_GET(c) == v)
4771 PyCell_Set(c, NULL);
4772 break;
4774 case STORE_NAME:
4776 PyObject *names = f->f_code->co_names;
4777 PyObject *name = GETITEM(names, PEEKARG());
4778 PyObject *locals = f->f_locals;
4779 if (PyDict_CheckExact(locals) &&
4780 PyDict_GetItem(locals, name) == v) {
4781 if (PyDict_DelItem(locals, name) != 0) {
4782 PyErr_Clear();
4785 break;
4790 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4791 /* Now we own the last reference to 'v', so we can resize it
4792 * in-place.
4794 if (_PyString_Resize(&v, new_len) != 0) {
4795 /* XXX if _PyString_Resize() fails, 'v' has been
4796 * deallocated so it cannot be put back into
4797 * 'variable'. The MemoryError is raised when there
4798 * is no value in 'variable', which might (very
4799 * remotely) be a cause of incompatibilities.
4801 return NULL;
4803 /* copy 'w' into the newly allocated area of 'v' */
4804 memcpy(PyString_AS_STRING(v) + v_len,
4805 PyString_AS_STRING(w), w_len);
4806 return v;
4808 else {
4809 /* When in-place resizing is not an option. */
4810 PyString_Concat(&v, w);
4811 return v;
4815 #ifdef DYNAMIC_EXECUTION_PROFILE
4817 static PyObject *
4818 getarray(long a[256])
4820 int i;
4821 PyObject *l = PyList_New(256);
4822 if (l == NULL) return NULL;
4823 for (i = 0; i < 256; i++) {
4824 PyObject *x = PyInt_FromLong(a[i]);
4825 if (x == NULL) {
4826 Py_DECREF(l);
4827 return NULL;
4829 PyList_SetItem(l, i, x);
4831 for (i = 0; i < 256; i++)
4832 a[i] = 0;
4833 return l;
4836 PyObject *
4837 _Py_GetDXProfile(PyObject *self, PyObject *args)
4839 #ifndef DXPAIRS
4840 return getarray(dxp);
4841 #else
4842 int i;
4843 PyObject *l = PyList_New(257);
4844 if (l == NULL) return NULL;
4845 for (i = 0; i < 257; i++) {
4846 PyObject *x = getarray(dxpairs[i]);
4847 if (x == NULL) {
4848 Py_DECREF(l);
4849 return NULL;
4851 PyList_SetItem(l, i, x);
4853 return l;
4854 #endif
4857 #endif