Remove C++/C99-style comments.
[python.git] / Python / ceval.c
blobd501a4e10a2b4499ef90e50c0b19fa76f0eb00cd
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 /* cast to avoid undefined behaviour
1325 on overflow */
1326 i = (long)((unsigned long)a + b);
1327 if ((i^a) < 0 && (i^b) < 0)
1328 goto slow_add;
1329 x = PyInt_FromLong(i);
1331 else if (PyString_CheckExact(v) &&
1332 PyString_CheckExact(w)) {
1333 x = string_concatenate(v, w, f, next_instr);
1334 /* string_concatenate consumed the ref to v */
1335 goto skip_decref_vx;
1337 else {
1338 slow_add:
1339 x = PyNumber_Add(v, w);
1341 Py_DECREF(v);
1342 skip_decref_vx:
1343 Py_DECREF(w);
1344 SET_TOP(x);
1345 if (x != NULL) continue;
1346 break;
1348 case BINARY_SUBTRACT:
1349 w = POP();
1350 v = TOP();
1351 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1352 /* INLINE: int - int */
1353 register long a, b, i;
1354 a = PyInt_AS_LONG(v);
1355 b = PyInt_AS_LONG(w);
1356 /* cast to avoid undefined behaviour
1357 on overflow */
1358 i = (long)((unsigned long)a - b);
1359 if ((i^a) < 0 && (i^~b) < 0)
1360 goto slow_sub;
1361 x = PyInt_FromLong(i);
1363 else {
1364 slow_sub:
1365 x = PyNumber_Subtract(v, w);
1367 Py_DECREF(v);
1368 Py_DECREF(w);
1369 SET_TOP(x);
1370 if (x != NULL) continue;
1371 break;
1373 case BINARY_SUBSCR:
1374 w = POP();
1375 v = TOP();
1376 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1377 /* INLINE: list[int] */
1378 Py_ssize_t i = PyInt_AsSsize_t(w);
1379 if (i < 0)
1380 i += PyList_GET_SIZE(v);
1381 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1382 x = PyList_GET_ITEM(v, i);
1383 Py_INCREF(x);
1385 else
1386 goto slow_get;
1388 else
1389 slow_get:
1390 x = PyObject_GetItem(v, w);
1391 Py_DECREF(v);
1392 Py_DECREF(w);
1393 SET_TOP(x);
1394 if (x != NULL) continue;
1395 break;
1397 case BINARY_LSHIFT:
1398 w = POP();
1399 v = TOP();
1400 x = PyNumber_Lshift(v, w);
1401 Py_DECREF(v);
1402 Py_DECREF(w);
1403 SET_TOP(x);
1404 if (x != NULL) continue;
1405 break;
1407 case BINARY_RSHIFT:
1408 w = POP();
1409 v = TOP();
1410 x = PyNumber_Rshift(v, w);
1411 Py_DECREF(v);
1412 Py_DECREF(w);
1413 SET_TOP(x);
1414 if (x != NULL) continue;
1415 break;
1417 case BINARY_AND:
1418 w = POP();
1419 v = TOP();
1420 x = PyNumber_And(v, w);
1421 Py_DECREF(v);
1422 Py_DECREF(w);
1423 SET_TOP(x);
1424 if (x != NULL) continue;
1425 break;
1427 case BINARY_XOR:
1428 w = POP();
1429 v = TOP();
1430 x = PyNumber_Xor(v, w);
1431 Py_DECREF(v);
1432 Py_DECREF(w);
1433 SET_TOP(x);
1434 if (x != NULL) continue;
1435 break;
1437 case BINARY_OR:
1438 w = POP();
1439 v = TOP();
1440 x = PyNumber_Or(v, w);
1441 Py_DECREF(v);
1442 Py_DECREF(w);
1443 SET_TOP(x);
1444 if (x != NULL) continue;
1445 break;
1447 case LIST_APPEND:
1448 w = POP();
1449 v = PEEK(oparg);
1450 err = PyList_Append(v, w);
1451 Py_DECREF(w);
1452 if (err == 0) {
1453 PREDICT(JUMP_ABSOLUTE);
1454 continue;
1456 break;
1458 case SET_ADD:
1459 w = POP();
1460 v = stack_pointer[-oparg];
1461 err = PySet_Add(v, w);
1462 Py_DECREF(w);
1463 if (err == 0) {
1464 PREDICT(JUMP_ABSOLUTE);
1465 continue;
1467 break;
1469 case INPLACE_POWER:
1470 w = POP();
1471 v = TOP();
1472 x = PyNumber_InPlacePower(v, w, Py_None);
1473 Py_DECREF(v);
1474 Py_DECREF(w);
1475 SET_TOP(x);
1476 if (x != NULL) continue;
1477 break;
1479 case INPLACE_MULTIPLY:
1480 w = POP();
1481 v = TOP();
1482 x = PyNumber_InPlaceMultiply(v, w);
1483 Py_DECREF(v);
1484 Py_DECREF(w);
1485 SET_TOP(x);
1486 if (x != NULL) continue;
1487 break;
1489 case INPLACE_DIVIDE:
1490 if (!_Py_QnewFlag) {
1491 w = POP();
1492 v = TOP();
1493 x = PyNumber_InPlaceDivide(v, w);
1494 Py_DECREF(v);
1495 Py_DECREF(w);
1496 SET_TOP(x);
1497 if (x != NULL) continue;
1498 break;
1500 /* -Qnew is in effect: fall through to
1501 INPLACE_TRUE_DIVIDE */
1502 case INPLACE_TRUE_DIVIDE:
1503 w = POP();
1504 v = TOP();
1505 x = PyNumber_InPlaceTrueDivide(v, w);
1506 Py_DECREF(v);
1507 Py_DECREF(w);
1508 SET_TOP(x);
1509 if (x != NULL) continue;
1510 break;
1512 case INPLACE_FLOOR_DIVIDE:
1513 w = POP();
1514 v = TOP();
1515 x = PyNumber_InPlaceFloorDivide(v, w);
1516 Py_DECREF(v);
1517 Py_DECREF(w);
1518 SET_TOP(x);
1519 if (x != NULL) continue;
1520 break;
1522 case INPLACE_MODULO:
1523 w = POP();
1524 v = TOP();
1525 x = PyNumber_InPlaceRemainder(v, w);
1526 Py_DECREF(v);
1527 Py_DECREF(w);
1528 SET_TOP(x);
1529 if (x != NULL) continue;
1530 break;
1532 case INPLACE_ADD:
1533 w = POP();
1534 v = TOP();
1535 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1536 /* INLINE: int + int */
1537 register long a, b, i;
1538 a = PyInt_AS_LONG(v);
1539 b = PyInt_AS_LONG(w);
1540 i = a + b;
1541 if ((i^a) < 0 && (i^b) < 0)
1542 goto slow_iadd;
1543 x = PyInt_FromLong(i);
1545 else if (PyString_CheckExact(v) &&
1546 PyString_CheckExact(w)) {
1547 x = string_concatenate(v, w, f, next_instr);
1548 /* string_concatenate consumed the ref to v */
1549 goto skip_decref_v;
1551 else {
1552 slow_iadd:
1553 x = PyNumber_InPlaceAdd(v, w);
1555 Py_DECREF(v);
1556 skip_decref_v:
1557 Py_DECREF(w);
1558 SET_TOP(x);
1559 if (x != NULL) continue;
1560 break;
1562 case INPLACE_SUBTRACT:
1563 w = POP();
1564 v = TOP();
1565 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1566 /* INLINE: int - int */
1567 register long a, b, i;
1568 a = PyInt_AS_LONG(v);
1569 b = PyInt_AS_LONG(w);
1570 i = a - b;
1571 if ((i^a) < 0 && (i^~b) < 0)
1572 goto slow_isub;
1573 x = PyInt_FromLong(i);
1575 else {
1576 slow_isub:
1577 x = PyNumber_InPlaceSubtract(v, w);
1579 Py_DECREF(v);
1580 Py_DECREF(w);
1581 SET_TOP(x);
1582 if (x != NULL) continue;
1583 break;
1585 case INPLACE_LSHIFT:
1586 w = POP();
1587 v = TOP();
1588 x = PyNumber_InPlaceLshift(v, w);
1589 Py_DECREF(v);
1590 Py_DECREF(w);
1591 SET_TOP(x);
1592 if (x != NULL) continue;
1593 break;
1595 case INPLACE_RSHIFT:
1596 w = POP();
1597 v = TOP();
1598 x = PyNumber_InPlaceRshift(v, w);
1599 Py_DECREF(v);
1600 Py_DECREF(w);
1601 SET_TOP(x);
1602 if (x != NULL) continue;
1603 break;
1605 case INPLACE_AND:
1606 w = POP();
1607 v = TOP();
1608 x = PyNumber_InPlaceAnd(v, w);
1609 Py_DECREF(v);
1610 Py_DECREF(w);
1611 SET_TOP(x);
1612 if (x != NULL) continue;
1613 break;
1615 case INPLACE_XOR:
1616 w = POP();
1617 v = TOP();
1618 x = PyNumber_InPlaceXor(v, w);
1619 Py_DECREF(v);
1620 Py_DECREF(w);
1621 SET_TOP(x);
1622 if (x != NULL) continue;
1623 break;
1625 case INPLACE_OR:
1626 w = POP();
1627 v = TOP();
1628 x = PyNumber_InPlaceOr(v, w);
1629 Py_DECREF(v);
1630 Py_DECREF(w);
1631 SET_TOP(x);
1632 if (x != NULL) continue;
1633 break;
1635 case SLICE+0:
1636 case SLICE+1:
1637 case SLICE+2:
1638 case SLICE+3:
1639 if ((opcode-SLICE) & 2)
1640 w = POP();
1641 else
1642 w = NULL;
1643 if ((opcode-SLICE) & 1)
1644 v = POP();
1645 else
1646 v = NULL;
1647 u = TOP();
1648 x = apply_slice(u, v, w);
1649 Py_DECREF(u);
1650 Py_XDECREF(v);
1651 Py_XDECREF(w);
1652 SET_TOP(x);
1653 if (x != NULL) continue;
1654 break;
1656 case STORE_SLICE+0:
1657 case STORE_SLICE+1:
1658 case STORE_SLICE+2:
1659 case STORE_SLICE+3:
1660 if ((opcode-STORE_SLICE) & 2)
1661 w = POP();
1662 else
1663 w = NULL;
1664 if ((opcode-STORE_SLICE) & 1)
1665 v = POP();
1666 else
1667 v = NULL;
1668 u = POP();
1669 t = POP();
1670 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1671 Py_DECREF(t);
1672 Py_DECREF(u);
1673 Py_XDECREF(v);
1674 Py_XDECREF(w);
1675 if (err == 0) continue;
1676 break;
1678 case DELETE_SLICE+0:
1679 case DELETE_SLICE+1:
1680 case DELETE_SLICE+2:
1681 case DELETE_SLICE+3:
1682 if ((opcode-DELETE_SLICE) & 2)
1683 w = POP();
1684 else
1685 w = NULL;
1686 if ((opcode-DELETE_SLICE) & 1)
1687 v = POP();
1688 else
1689 v = NULL;
1690 u = POP();
1691 err = assign_slice(u, v, w, (PyObject *)NULL);
1692 /* del u[v:w] */
1693 Py_DECREF(u);
1694 Py_XDECREF(v);
1695 Py_XDECREF(w);
1696 if (err == 0) continue;
1697 break;
1699 case STORE_SUBSCR:
1700 w = TOP();
1701 v = SECOND();
1702 u = THIRD();
1703 STACKADJ(-3);
1704 /* v[w] = u */
1705 err = PyObject_SetItem(v, w, u);
1706 Py_DECREF(u);
1707 Py_DECREF(v);
1708 Py_DECREF(w);
1709 if (err == 0) continue;
1710 break;
1712 case DELETE_SUBSCR:
1713 w = TOP();
1714 v = SECOND();
1715 STACKADJ(-2);
1716 /* del v[w] */
1717 err = PyObject_DelItem(v, w);
1718 Py_DECREF(v);
1719 Py_DECREF(w);
1720 if (err == 0) continue;
1721 break;
1723 case PRINT_EXPR:
1724 v = POP();
1725 w = PySys_GetObject("displayhook");
1726 if (w == NULL) {
1727 PyErr_SetString(PyExc_RuntimeError,
1728 "lost sys.displayhook");
1729 err = -1;
1730 x = NULL;
1732 if (err == 0) {
1733 x = PyTuple_Pack(1, v);
1734 if (x == NULL)
1735 err = -1;
1737 if (err == 0) {
1738 w = PyEval_CallObject(w, x);
1739 Py_XDECREF(w);
1740 if (w == NULL)
1741 err = -1;
1743 Py_DECREF(v);
1744 Py_XDECREF(x);
1745 break;
1747 case PRINT_ITEM_TO:
1748 w = stream = POP();
1749 /* fall through to PRINT_ITEM */
1751 case PRINT_ITEM:
1752 v = POP();
1753 if (stream == NULL || stream == Py_None) {
1754 w = PySys_GetObject("stdout");
1755 if (w == NULL) {
1756 PyErr_SetString(PyExc_RuntimeError,
1757 "lost sys.stdout");
1758 err = -1;
1761 /* PyFile_SoftSpace() can exececute arbitrary code
1762 if sys.stdout is an instance with a __getattr__.
1763 If __getattr__ raises an exception, w will
1764 be freed, so we need to prevent that temporarily. */
1765 Py_XINCREF(w);
1766 if (w != NULL && PyFile_SoftSpace(w, 0))
1767 err = PyFile_WriteString(" ", w);
1768 if (err == 0)
1769 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1770 if (err == 0) {
1771 /* XXX move into writeobject() ? */
1772 if (PyString_Check(v)) {
1773 char *s = PyString_AS_STRING(v);
1774 Py_ssize_t len = PyString_GET_SIZE(v);
1775 if (len == 0 ||
1776 !isspace(Py_CHARMASK(s[len-1])) ||
1777 s[len-1] == ' ')
1778 PyFile_SoftSpace(w, 1);
1780 #ifdef Py_USING_UNICODE
1781 else if (PyUnicode_Check(v)) {
1782 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1783 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1784 if (len == 0 ||
1785 !Py_UNICODE_ISSPACE(s[len-1]) ||
1786 s[len-1] == ' ')
1787 PyFile_SoftSpace(w, 1);
1789 #endif
1790 else
1791 PyFile_SoftSpace(w, 1);
1793 Py_XDECREF(w);
1794 Py_DECREF(v);
1795 Py_XDECREF(stream);
1796 stream = NULL;
1797 if (err == 0)
1798 continue;
1799 break;
1801 case PRINT_NEWLINE_TO:
1802 w = stream = POP();
1803 /* fall through to PRINT_NEWLINE */
1805 case PRINT_NEWLINE:
1806 if (stream == NULL || stream == Py_None) {
1807 w = PySys_GetObject("stdout");
1808 if (w == NULL) {
1809 PyErr_SetString(PyExc_RuntimeError,
1810 "lost sys.stdout");
1811 why = WHY_EXCEPTION;
1814 if (w != NULL) {
1815 /* w.write() may replace sys.stdout, so we
1816 * have to keep our reference to it */
1817 Py_INCREF(w);
1818 err = PyFile_WriteString("\n", w);
1819 if (err == 0)
1820 PyFile_SoftSpace(w, 0);
1821 Py_DECREF(w);
1823 Py_XDECREF(stream);
1824 stream = NULL;
1825 break;
1828 #ifdef CASE_TOO_BIG
1829 default: switch (opcode) {
1830 #endif
1831 case RAISE_VARARGS:
1832 u = v = w = NULL;
1833 switch (oparg) {
1834 case 3:
1835 u = POP(); /* traceback */
1836 /* Fallthrough */
1837 case 2:
1838 v = POP(); /* value */
1839 /* Fallthrough */
1840 case 1:
1841 w = POP(); /* exc */
1842 case 0: /* Fallthrough */
1843 why = do_raise(w, v, u);
1844 break;
1845 default:
1846 PyErr_SetString(PyExc_SystemError,
1847 "bad RAISE_VARARGS oparg");
1848 why = WHY_EXCEPTION;
1849 break;
1851 break;
1853 case LOAD_LOCALS:
1854 if ((x = f->f_locals) != NULL) {
1855 Py_INCREF(x);
1856 PUSH(x);
1857 continue;
1859 PyErr_SetString(PyExc_SystemError, "no locals");
1860 break;
1862 case RETURN_VALUE:
1863 retval = POP();
1864 why = WHY_RETURN;
1865 goto fast_block_end;
1867 case YIELD_VALUE:
1868 retval = POP();
1869 f->f_stacktop = stack_pointer;
1870 why = WHY_YIELD;
1871 goto fast_yield;
1873 case EXEC_STMT:
1874 w = TOP();
1875 v = SECOND();
1876 u = THIRD();
1877 STACKADJ(-3);
1878 READ_TIMESTAMP(intr0);
1879 err = exec_statement(f, u, v, w);
1880 READ_TIMESTAMP(intr1);
1881 Py_DECREF(u);
1882 Py_DECREF(v);
1883 Py_DECREF(w);
1884 break;
1886 case POP_BLOCK:
1888 PyTryBlock *b = PyFrame_BlockPop(f);
1889 while (STACK_LEVEL() > b->b_level) {
1890 v = POP();
1891 Py_DECREF(v);
1894 continue;
1896 PREDICTED(END_FINALLY);
1897 case END_FINALLY:
1898 v = POP();
1899 if (PyInt_Check(v)) {
1900 why = (enum why_code) PyInt_AS_LONG(v);
1901 assert(why != WHY_YIELD);
1902 if (why == WHY_RETURN ||
1903 why == WHY_CONTINUE)
1904 retval = POP();
1906 else if (PyExceptionClass_Check(v) ||
1907 PyString_Check(v)) {
1908 w = POP();
1909 u = POP();
1910 PyErr_Restore(v, w, u);
1911 why = WHY_RERAISE;
1912 break;
1914 else if (v != Py_None) {
1915 PyErr_SetString(PyExc_SystemError,
1916 "'finally' pops bad exception");
1917 why = WHY_EXCEPTION;
1919 Py_DECREF(v);
1920 break;
1922 case BUILD_CLASS:
1923 u = TOP();
1924 v = SECOND();
1925 w = THIRD();
1926 STACKADJ(-2);
1927 x = build_class(u, v, w);
1928 SET_TOP(x);
1929 Py_DECREF(u);
1930 Py_DECREF(v);
1931 Py_DECREF(w);
1932 break;
1934 case STORE_NAME:
1935 w = GETITEM(names, oparg);
1936 v = POP();
1937 if ((x = f->f_locals) != NULL) {
1938 if (PyDict_CheckExact(x))
1939 err = PyDict_SetItem(x, w, v);
1940 else
1941 err = PyObject_SetItem(x, w, v);
1942 Py_DECREF(v);
1943 if (err == 0) continue;
1944 break;
1946 PyErr_Format(PyExc_SystemError,
1947 "no locals found when storing %s",
1948 PyObject_REPR(w));
1949 break;
1951 case DELETE_NAME:
1952 w = GETITEM(names, oparg);
1953 if ((x = f->f_locals) != NULL) {
1954 if ((err = PyObject_DelItem(x, w)) != 0)
1955 format_exc_check_arg(PyExc_NameError,
1956 NAME_ERROR_MSG,
1958 break;
1960 PyErr_Format(PyExc_SystemError,
1961 "no locals when deleting %s",
1962 PyObject_REPR(w));
1963 break;
1965 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1966 case UNPACK_SEQUENCE:
1967 v = POP();
1968 if (PyTuple_CheckExact(v) &&
1969 PyTuple_GET_SIZE(v) == oparg) {
1970 PyObject **items = \
1971 ((PyTupleObject *)v)->ob_item;
1972 while (oparg--) {
1973 w = items[oparg];
1974 Py_INCREF(w);
1975 PUSH(w);
1977 Py_DECREF(v);
1978 continue;
1979 } else if (PyList_CheckExact(v) &&
1980 PyList_GET_SIZE(v) == oparg) {
1981 PyObject **items = \
1982 ((PyListObject *)v)->ob_item;
1983 while (oparg--) {
1984 w = items[oparg];
1985 Py_INCREF(w);
1986 PUSH(w);
1988 } else if (unpack_iterable(v, oparg,
1989 stack_pointer + oparg)) {
1990 STACKADJ(oparg);
1991 } else {
1992 /* unpack_iterable() raised an exception */
1993 why = WHY_EXCEPTION;
1995 Py_DECREF(v);
1996 break;
1998 case STORE_ATTR:
1999 w = GETITEM(names, oparg);
2000 v = TOP();
2001 u = SECOND();
2002 STACKADJ(-2);
2003 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2004 Py_DECREF(v);
2005 Py_DECREF(u);
2006 if (err == 0) continue;
2007 break;
2009 case DELETE_ATTR:
2010 w = GETITEM(names, oparg);
2011 v = POP();
2012 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2013 /* del v.w */
2014 Py_DECREF(v);
2015 break;
2017 case STORE_GLOBAL:
2018 w = GETITEM(names, oparg);
2019 v = POP();
2020 err = PyDict_SetItem(f->f_globals, w, v);
2021 Py_DECREF(v);
2022 if (err == 0) continue;
2023 break;
2025 case DELETE_GLOBAL:
2026 w = GETITEM(names, oparg);
2027 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2028 format_exc_check_arg(
2029 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2030 break;
2032 case LOAD_NAME:
2033 w = GETITEM(names, oparg);
2034 if ((v = f->f_locals) == NULL) {
2035 PyErr_Format(PyExc_SystemError,
2036 "no locals when loading %s",
2037 PyObject_REPR(w));
2038 why = WHY_EXCEPTION;
2039 break;
2041 if (PyDict_CheckExact(v)) {
2042 x = PyDict_GetItem(v, w);
2043 Py_XINCREF(x);
2045 else {
2046 x = PyObject_GetItem(v, w);
2047 if (x == NULL && PyErr_Occurred()) {
2048 if (!PyErr_ExceptionMatches(
2049 PyExc_KeyError))
2050 break;
2051 PyErr_Clear();
2054 if (x == NULL) {
2055 x = PyDict_GetItem(f->f_globals, w);
2056 if (x == NULL) {
2057 x = PyDict_GetItem(f->f_builtins, w);
2058 if (x == NULL) {
2059 format_exc_check_arg(
2060 PyExc_NameError,
2061 NAME_ERROR_MSG, w);
2062 break;
2065 Py_INCREF(x);
2067 PUSH(x);
2068 continue;
2070 case LOAD_GLOBAL:
2071 w = GETITEM(names, oparg);
2072 if (PyString_CheckExact(w)) {
2073 /* Inline the PyDict_GetItem() calls.
2074 WARNING: this is an extreme speed hack.
2075 Do not try this at home. */
2076 long hash = ((PyStringObject *)w)->ob_shash;
2077 if (hash != -1) {
2078 PyDictObject *d;
2079 PyDictEntry *e;
2080 d = (PyDictObject *)(f->f_globals);
2081 e = d->ma_lookup(d, w, hash);
2082 if (e == NULL) {
2083 x = NULL;
2084 break;
2086 x = e->me_value;
2087 if (x != NULL) {
2088 Py_INCREF(x);
2089 PUSH(x);
2090 continue;
2092 d = (PyDictObject *)(f->f_builtins);
2093 e = d->ma_lookup(d, w, hash);
2094 if (e == NULL) {
2095 x = NULL;
2096 break;
2098 x = e->me_value;
2099 if (x != NULL) {
2100 Py_INCREF(x);
2101 PUSH(x);
2102 continue;
2104 goto load_global_error;
2107 /* This is the un-inlined version of the code above */
2108 x = PyDict_GetItem(f->f_globals, w);
2109 if (x == NULL) {
2110 x = PyDict_GetItem(f->f_builtins, w);
2111 if (x == NULL) {
2112 load_global_error:
2113 format_exc_check_arg(
2114 PyExc_NameError,
2115 GLOBAL_NAME_ERROR_MSG, w);
2116 break;
2119 Py_INCREF(x);
2120 PUSH(x);
2121 continue;
2123 case DELETE_FAST:
2124 x = GETLOCAL(oparg);
2125 if (x != NULL) {
2126 SETLOCAL(oparg, NULL);
2127 continue;
2129 format_exc_check_arg(
2130 PyExc_UnboundLocalError,
2131 UNBOUNDLOCAL_ERROR_MSG,
2132 PyTuple_GetItem(co->co_varnames, oparg)
2134 break;
2136 case LOAD_CLOSURE:
2137 x = freevars[oparg];
2138 Py_INCREF(x);
2139 PUSH(x);
2140 if (x != NULL) continue;
2141 break;
2143 case LOAD_DEREF:
2144 x = freevars[oparg];
2145 w = PyCell_Get(x);
2146 if (w != NULL) {
2147 PUSH(w);
2148 continue;
2150 err = -1;
2151 /* Don't stomp existing exception */
2152 if (PyErr_Occurred())
2153 break;
2154 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2155 v = PyTuple_GET_ITEM(co->co_cellvars,
2156 oparg);
2157 format_exc_check_arg(
2158 PyExc_UnboundLocalError,
2159 UNBOUNDLOCAL_ERROR_MSG,
2161 } else {
2162 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2163 PyTuple_GET_SIZE(co->co_cellvars));
2164 format_exc_check_arg(PyExc_NameError,
2165 UNBOUNDFREE_ERROR_MSG, v);
2167 break;
2169 case STORE_DEREF:
2170 w = POP();
2171 x = freevars[oparg];
2172 PyCell_Set(x, w);
2173 Py_DECREF(w);
2174 continue;
2176 case BUILD_TUPLE:
2177 x = PyTuple_New(oparg);
2178 if (x != NULL) {
2179 for (; --oparg >= 0;) {
2180 w = POP();
2181 PyTuple_SET_ITEM(x, oparg, w);
2183 PUSH(x);
2184 continue;
2186 break;
2188 case BUILD_LIST:
2189 x = PyList_New(oparg);
2190 if (x != NULL) {
2191 for (; --oparg >= 0;) {
2192 w = POP();
2193 PyList_SET_ITEM(x, oparg, w);
2195 PUSH(x);
2196 continue;
2198 break;
2200 case BUILD_SET:
2201 x = PySet_New(NULL);
2202 if (x != NULL) {
2203 for (; --oparg >= 0;) {
2204 w = POP();
2205 if (err == 0)
2206 err = PySet_Add(x, w);
2207 Py_DECREF(w);
2209 if (err != 0) {
2210 Py_DECREF(x);
2211 break;
2213 PUSH(x);
2214 continue;
2216 break;
2219 case BUILD_MAP:
2220 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2221 PUSH(x);
2222 if (x != NULL) continue;
2223 break;
2225 case STORE_MAP:
2226 w = TOP(); /* key */
2227 u = SECOND(); /* value */
2228 v = THIRD(); /* dict */
2229 STACKADJ(-2);
2230 assert (PyDict_CheckExact(v));
2231 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2232 Py_DECREF(u);
2233 Py_DECREF(w);
2234 if (err == 0) continue;
2235 break;
2237 case MAP_ADD:
2238 w = TOP(); /* key */
2239 u = SECOND(); /* value */
2240 STACKADJ(-2);
2241 v = stack_pointer[-oparg]; /* dict */
2242 assert (PyDict_CheckExact(v));
2243 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2244 Py_DECREF(u);
2245 Py_DECREF(w);
2246 if (err == 0) {
2247 PREDICT(JUMP_ABSOLUTE);
2248 continue;
2250 break;
2252 case LOAD_ATTR:
2253 w = GETITEM(names, oparg);
2254 v = TOP();
2255 x = PyObject_GetAttr(v, w);
2256 Py_DECREF(v);
2257 SET_TOP(x);
2258 if (x != NULL) continue;
2259 break;
2261 case COMPARE_OP:
2262 w = POP();
2263 v = TOP();
2264 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2265 /* INLINE: cmp(int, int) */
2266 register long a, b;
2267 register int res;
2268 a = PyInt_AS_LONG(v);
2269 b = PyInt_AS_LONG(w);
2270 switch (oparg) {
2271 case PyCmp_LT: res = a < b; break;
2272 case PyCmp_LE: res = a <= b; break;
2273 case PyCmp_EQ: res = a == b; break;
2274 case PyCmp_NE: res = a != b; break;
2275 case PyCmp_GT: res = a > b; break;
2276 case PyCmp_GE: res = a >= b; break;
2277 case PyCmp_IS: res = v == w; break;
2278 case PyCmp_IS_NOT: res = v != w; break;
2279 default: goto slow_compare;
2281 x = res ? Py_True : Py_False;
2282 Py_INCREF(x);
2284 else {
2285 slow_compare:
2286 x = cmp_outcome(oparg, v, w);
2288 Py_DECREF(v);
2289 Py_DECREF(w);
2290 SET_TOP(x);
2291 if (x == NULL) break;
2292 PREDICT(POP_JUMP_IF_FALSE);
2293 PREDICT(POP_JUMP_IF_TRUE);
2294 continue;
2296 case IMPORT_NAME:
2297 w = GETITEM(names, oparg);
2298 x = PyDict_GetItemString(f->f_builtins, "__import__");
2299 if (x == NULL) {
2300 PyErr_SetString(PyExc_ImportError,
2301 "__import__ not found");
2302 break;
2304 Py_INCREF(x);
2305 v = POP();
2306 u = TOP();
2307 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2308 w = PyTuple_Pack(5,
2310 f->f_globals,
2311 f->f_locals == NULL ?
2312 Py_None : f->f_locals,
2315 else
2316 w = PyTuple_Pack(4,
2318 f->f_globals,
2319 f->f_locals == NULL ?
2320 Py_None : f->f_locals,
2322 Py_DECREF(v);
2323 Py_DECREF(u);
2324 if (w == NULL) {
2325 u = POP();
2326 Py_DECREF(x);
2327 x = NULL;
2328 break;
2330 READ_TIMESTAMP(intr0);
2331 v = x;
2332 x = PyEval_CallObject(v, w);
2333 Py_DECREF(v);
2334 READ_TIMESTAMP(intr1);
2335 Py_DECREF(w);
2336 SET_TOP(x);
2337 if (x != NULL) continue;
2338 break;
2340 case IMPORT_STAR:
2341 v = POP();
2342 PyFrame_FastToLocals(f);
2343 if ((x = f->f_locals) == NULL) {
2344 PyErr_SetString(PyExc_SystemError,
2345 "no locals found during 'import *'");
2346 break;
2348 READ_TIMESTAMP(intr0);
2349 err = import_all_from(x, v);
2350 READ_TIMESTAMP(intr1);
2351 PyFrame_LocalsToFast(f, 0);
2352 Py_DECREF(v);
2353 if (err == 0) continue;
2354 break;
2356 case IMPORT_FROM:
2357 w = GETITEM(names, oparg);
2358 v = TOP();
2359 READ_TIMESTAMP(intr0);
2360 x = import_from(v, w);
2361 READ_TIMESTAMP(intr1);
2362 PUSH(x);
2363 if (x != NULL) continue;
2364 break;
2366 case JUMP_FORWARD:
2367 JUMPBY(oparg);
2368 goto fast_next_opcode;
2370 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2371 case POP_JUMP_IF_FALSE:
2372 w = POP();
2373 if (w == Py_True) {
2374 Py_DECREF(w);
2375 goto fast_next_opcode;
2377 if (w == Py_False) {
2378 Py_DECREF(w);
2379 JUMPTO(oparg);
2380 goto fast_next_opcode;
2382 err = PyObject_IsTrue(w);
2383 Py_DECREF(w);
2384 if (err > 0)
2385 err = 0;
2386 else if (err == 0)
2387 JUMPTO(oparg);
2388 else
2389 break;
2390 continue;
2392 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2393 case POP_JUMP_IF_TRUE:
2394 w = POP();
2395 if (w == Py_False) {
2396 Py_DECREF(w);
2397 goto fast_next_opcode;
2399 if (w == Py_True) {
2400 Py_DECREF(w);
2401 JUMPTO(oparg);
2402 goto fast_next_opcode;
2404 err = PyObject_IsTrue(w);
2405 Py_DECREF(w);
2406 if (err > 0) {
2407 err = 0;
2408 JUMPTO(oparg);
2410 else if (err == 0)
2412 else
2413 break;
2414 continue;
2416 case JUMP_IF_FALSE_OR_POP:
2417 w = TOP();
2418 if (w == Py_True) {
2419 STACKADJ(-1);
2420 Py_DECREF(w);
2421 goto fast_next_opcode;
2423 if (w == Py_False) {
2424 JUMPTO(oparg);
2425 goto fast_next_opcode;
2427 err = PyObject_IsTrue(w);
2428 if (err > 0) {
2429 STACKADJ(-1);
2430 Py_DECREF(w);
2431 err = 0;
2433 else if (err == 0)
2434 JUMPTO(oparg);
2435 else
2436 break;
2437 continue;
2439 case JUMP_IF_TRUE_OR_POP:
2440 w = TOP();
2441 if (w == Py_False) {
2442 STACKADJ(-1);
2443 Py_DECREF(w);
2444 goto fast_next_opcode;
2446 if (w == Py_True) {
2447 JUMPTO(oparg);
2448 goto fast_next_opcode;
2450 err = PyObject_IsTrue(w);
2451 if (err > 0) {
2452 err = 0;
2453 JUMPTO(oparg);
2455 else if (err == 0) {
2456 STACKADJ(-1);
2457 Py_DECREF(w);
2459 else
2460 break;
2461 continue;
2463 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2464 case JUMP_ABSOLUTE:
2465 JUMPTO(oparg);
2466 #if FAST_LOOPS
2467 /* Enabling this path speeds-up all while and for-loops by bypassing
2468 the per-loop checks for signals. By default, this should be turned-off
2469 because it prevents detection of a control-break in tight loops like
2470 "while 1: pass". Compile with this option turned-on when you need
2471 the speed-up and do not need break checking inside tight loops (ones
2472 that contain only instructions ending with goto fast_next_opcode).
2474 goto fast_next_opcode;
2475 #else
2476 continue;
2477 #endif
2479 case GET_ITER:
2480 /* before: [obj]; after [getiter(obj)] */
2481 v = TOP();
2482 x = PyObject_GetIter(v);
2483 Py_DECREF(v);
2484 if (x != NULL) {
2485 SET_TOP(x);
2486 PREDICT(FOR_ITER);
2487 continue;
2489 STACKADJ(-1);
2490 break;
2492 PREDICTED_WITH_ARG(FOR_ITER);
2493 case FOR_ITER:
2494 /* before: [iter]; after: [iter, iter()] *or* [] */
2495 v = TOP();
2496 x = (*v->ob_type->tp_iternext)(v);
2497 if (x != NULL) {
2498 PUSH(x);
2499 PREDICT(STORE_FAST);
2500 PREDICT(UNPACK_SEQUENCE);
2501 continue;
2503 if (PyErr_Occurred()) {
2504 if (!PyErr_ExceptionMatches(
2505 PyExc_StopIteration))
2506 break;
2507 PyErr_Clear();
2509 /* iterator ended normally */
2510 x = v = POP();
2511 Py_DECREF(v);
2512 JUMPBY(oparg);
2513 continue;
2515 case BREAK_LOOP:
2516 why = WHY_BREAK;
2517 goto fast_block_end;
2519 case CONTINUE_LOOP:
2520 retval = PyInt_FromLong(oparg);
2521 if (!retval) {
2522 x = NULL;
2523 break;
2525 why = WHY_CONTINUE;
2526 goto fast_block_end;
2528 case SETUP_LOOP:
2529 case SETUP_EXCEPT:
2530 case SETUP_FINALLY:
2531 /* NOTE: If you add any new block-setup opcodes that
2532 are not try/except/finally handlers, you may need
2533 to update the PyGen_NeedsFinalizing() function.
2536 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2537 STACK_LEVEL());
2538 continue;
2540 case SETUP_WITH:
2542 static PyObject *exit, *enter;
2543 w = TOP();
2544 x = special_lookup(w, "__exit__", &exit);
2545 if (!x)
2546 break;
2547 SET_TOP(x);
2548 u = special_lookup(w, "__enter__", &enter);
2549 Py_DECREF(w);
2550 if (!u) {
2551 x = NULL;
2552 break;
2554 x = PyObject_CallFunctionObjArgs(u, NULL);
2555 Py_DECREF(u);
2556 if (!x)
2557 break;
2558 /* Setup the finally block before pushing the result
2559 of __enter__ on the stack. */
2560 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2561 STACK_LEVEL());
2563 PUSH(x);
2564 continue;
2567 case WITH_CLEANUP:
2569 /* At the top of the stack are 1-3 values indicating
2570 how/why we entered the finally clause:
2571 - TOP = None
2572 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2573 - TOP = WHY_*; no retval below it
2574 - (TOP, SECOND, THIRD) = exc_info()
2575 Below them is EXIT, the context.__exit__ bound method.
2576 In the last case, we must call
2577 EXIT(TOP, SECOND, THIRD)
2578 otherwise we must call
2579 EXIT(None, None, None)
2581 In all cases, we remove EXIT from the stack, leaving
2582 the rest in the same order.
2584 In addition, if the stack represents an exception,
2585 *and* the function call returns a 'true' value, we
2586 "zap" this information, to prevent END_FINALLY from
2587 re-raising the exception. (But non-local gotos
2588 should still be resumed.)
2591 PyObject *exit_func;
2593 u = POP();
2594 if (u == Py_None) {
2595 exit_func = TOP();
2596 SET_TOP(u);
2597 v = w = Py_None;
2599 else if (PyInt_Check(u)) {
2600 switch(PyInt_AS_LONG(u)) {
2601 case WHY_RETURN:
2602 case WHY_CONTINUE:
2603 /* Retval in TOP. */
2604 exit_func = SECOND();
2605 SET_SECOND(TOP());
2606 SET_TOP(u);
2607 break;
2608 default:
2609 exit_func = TOP();
2610 SET_TOP(u);
2611 break;
2613 u = v = w = Py_None;
2615 else {
2616 v = TOP();
2617 w = SECOND();
2618 exit_func = THIRD();
2619 SET_TOP(u);
2620 SET_SECOND(v);
2621 SET_THIRD(w);
2623 /* XXX Not the fastest way to call it... */
2624 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2625 NULL);
2626 Py_DECREF(exit_func);
2627 if (x == NULL)
2628 break; /* Go to error exit */
2630 if (u != Py_None)
2631 err = PyObject_IsTrue(x);
2632 else
2633 err = 0;
2634 Py_DECREF(x);
2636 if (err < 0)
2637 break; /* Go to error exit */
2638 else if (err > 0) {
2639 err = 0;
2640 /* There was an exception and a true return */
2641 STACKADJ(-2);
2642 Py_INCREF(Py_None);
2643 SET_TOP(Py_None);
2644 Py_DECREF(u);
2645 Py_DECREF(v);
2646 Py_DECREF(w);
2647 } else {
2648 /* The stack was rearranged to remove EXIT
2649 above. Let END_FINALLY do its thing */
2651 PREDICT(END_FINALLY);
2652 break;
2655 case CALL_FUNCTION:
2657 PyObject **sp;
2658 PCALL(PCALL_ALL);
2659 sp = stack_pointer;
2660 #ifdef WITH_TSC
2661 x = call_function(&sp, oparg, &intr0, &intr1);
2662 #else
2663 x = call_function(&sp, oparg);
2664 #endif
2665 stack_pointer = sp;
2666 PUSH(x);
2667 if (x != NULL)
2668 continue;
2669 break;
2672 case CALL_FUNCTION_VAR:
2673 case CALL_FUNCTION_KW:
2674 case CALL_FUNCTION_VAR_KW:
2676 int na = oparg & 0xff;
2677 int nk = (oparg>>8) & 0xff;
2678 int flags = (opcode - CALL_FUNCTION) & 3;
2679 int n = na + 2 * nk;
2680 PyObject **pfunc, *func, **sp;
2681 PCALL(PCALL_ALL);
2682 if (flags & CALL_FLAG_VAR)
2683 n++;
2684 if (flags & CALL_FLAG_KW)
2685 n++;
2686 pfunc = stack_pointer - n - 1;
2687 func = *pfunc;
2689 if (PyMethod_Check(func)
2690 && PyMethod_GET_SELF(func) != NULL) {
2691 PyObject *self = PyMethod_GET_SELF(func);
2692 Py_INCREF(self);
2693 func = PyMethod_GET_FUNCTION(func);
2694 Py_INCREF(func);
2695 Py_DECREF(*pfunc);
2696 *pfunc = self;
2697 na++;
2698 n++;
2699 } else
2700 Py_INCREF(func);
2701 sp = stack_pointer;
2702 READ_TIMESTAMP(intr0);
2703 x = ext_do_call(func, &sp, flags, na, nk);
2704 READ_TIMESTAMP(intr1);
2705 stack_pointer = sp;
2706 Py_DECREF(func);
2708 while (stack_pointer > pfunc) {
2709 w = POP();
2710 Py_DECREF(w);
2712 PUSH(x);
2713 if (x != NULL)
2714 continue;
2715 break;
2718 case MAKE_FUNCTION:
2719 v = POP(); /* code object */
2720 x = PyFunction_New(v, f->f_globals);
2721 Py_DECREF(v);
2722 /* XXX Maybe this should be a separate opcode? */
2723 if (x != NULL && oparg > 0) {
2724 v = PyTuple_New(oparg);
2725 if (v == NULL) {
2726 Py_DECREF(x);
2727 x = NULL;
2728 break;
2730 while (--oparg >= 0) {
2731 w = POP();
2732 PyTuple_SET_ITEM(v, oparg, w);
2734 err = PyFunction_SetDefaults(x, v);
2735 Py_DECREF(v);
2737 PUSH(x);
2738 break;
2740 case MAKE_CLOSURE:
2742 v = POP(); /* code object */
2743 x = PyFunction_New(v, f->f_globals);
2744 Py_DECREF(v);
2745 if (x != NULL) {
2746 v = POP();
2747 if (PyFunction_SetClosure(x, v) != 0) {
2748 /* Can't happen unless bytecode is corrupt. */
2749 why = WHY_EXCEPTION;
2751 Py_DECREF(v);
2753 if (x != NULL && oparg > 0) {
2754 v = PyTuple_New(oparg);
2755 if (v == NULL) {
2756 Py_DECREF(x);
2757 x = NULL;
2758 break;
2760 while (--oparg >= 0) {
2761 w = POP();
2762 PyTuple_SET_ITEM(v, oparg, w);
2764 if (PyFunction_SetDefaults(x, v) != 0) {
2765 /* Can't happen unless
2766 PyFunction_SetDefaults changes. */
2767 why = WHY_EXCEPTION;
2769 Py_DECREF(v);
2771 PUSH(x);
2772 break;
2775 case BUILD_SLICE:
2776 if (oparg == 3)
2777 w = POP();
2778 else
2779 w = NULL;
2780 v = POP();
2781 u = TOP();
2782 x = PySlice_New(u, v, w);
2783 Py_DECREF(u);
2784 Py_DECREF(v);
2785 Py_XDECREF(w);
2786 SET_TOP(x);
2787 if (x != NULL) continue;
2788 break;
2790 case EXTENDED_ARG:
2791 opcode = NEXTOP();
2792 oparg = oparg<<16 | NEXTARG();
2793 goto dispatch_opcode;
2795 default:
2796 fprintf(stderr,
2797 "XXX lineno: %d, opcode: %d\n",
2798 PyFrame_GetLineNumber(f),
2799 opcode);
2800 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2801 why = WHY_EXCEPTION;
2802 break;
2804 #ifdef CASE_TOO_BIG
2806 #endif
2808 } /* switch */
2810 on_error:
2812 READ_TIMESTAMP(inst1);
2814 /* Quickly continue if no error occurred */
2816 if (why == WHY_NOT) {
2817 if (err == 0 && x != NULL) {
2818 #ifdef CHECKEXC
2819 /* This check is expensive! */
2820 if (PyErr_Occurred())
2821 fprintf(stderr,
2822 "XXX undetected error\n");
2823 else {
2824 #endif
2825 READ_TIMESTAMP(loop1);
2826 continue; /* Normal, fast path */
2827 #ifdef CHECKEXC
2829 #endif
2831 why = WHY_EXCEPTION;
2832 x = Py_None;
2833 err = 0;
2836 /* Double-check exception status */
2838 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2839 if (!PyErr_Occurred()) {
2840 PyErr_SetString(PyExc_SystemError,
2841 "error return without exception set");
2842 why = WHY_EXCEPTION;
2845 #ifdef CHECKEXC
2846 else {
2847 /* This check is expensive! */
2848 if (PyErr_Occurred()) {
2849 char buf[128];
2850 sprintf(buf, "Stack unwind with exception "
2851 "set and why=%d", why);
2852 Py_FatalError(buf);
2855 #endif
2857 /* Log traceback info if this is a real exception */
2859 if (why == WHY_EXCEPTION) {
2860 PyTraceBack_Here(f);
2862 if (tstate->c_tracefunc != NULL)
2863 call_exc_trace(tstate->c_tracefunc,
2864 tstate->c_traceobj, f);
2867 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2869 if (why == WHY_RERAISE)
2870 why = WHY_EXCEPTION;
2872 /* Unwind stacks if a (pseudo) exception occurred */
2874 fast_block_end:
2875 while (why != WHY_NOT && f->f_iblock > 0) {
2876 /* Peek at the current block. */
2877 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
2879 assert(why != WHY_YIELD);
2880 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2881 why = WHY_NOT;
2882 JUMPTO(PyInt_AS_LONG(retval));
2883 Py_DECREF(retval);
2884 break;
2887 /* Now we have to pop the block. */
2888 f->f_iblock--;
2890 while (STACK_LEVEL() > b->b_level) {
2891 v = POP();
2892 Py_XDECREF(v);
2894 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2895 why = WHY_NOT;
2896 JUMPTO(b->b_handler);
2897 break;
2899 if (b->b_type == SETUP_FINALLY ||
2900 (b->b_type == SETUP_EXCEPT &&
2901 why == WHY_EXCEPTION)) {
2902 if (why == WHY_EXCEPTION) {
2903 PyObject *exc, *val, *tb;
2904 PyErr_Fetch(&exc, &val, &tb);
2905 if (val == NULL) {
2906 val = Py_None;
2907 Py_INCREF(val);
2909 /* Make the raw exception data
2910 available to the handler,
2911 so a program can emulate the
2912 Python main loop. Don't do
2913 this for 'finally'. */
2914 if (b->b_type == SETUP_EXCEPT) {
2915 PyErr_NormalizeException(
2916 &exc, &val, &tb);
2917 set_exc_info(tstate,
2918 exc, val, tb);
2920 if (tb == NULL) {
2921 Py_INCREF(Py_None);
2922 PUSH(Py_None);
2923 } else
2924 PUSH(tb);
2925 PUSH(val);
2926 PUSH(exc);
2928 else {
2929 if (why & (WHY_RETURN | WHY_CONTINUE))
2930 PUSH(retval);
2931 v = PyInt_FromLong((long)why);
2932 PUSH(v);
2934 why = WHY_NOT;
2935 JUMPTO(b->b_handler);
2936 break;
2938 } /* unwind stack */
2940 /* End the loop if we still have an error (or return) */
2942 if (why != WHY_NOT)
2943 break;
2944 READ_TIMESTAMP(loop1);
2946 } /* main loop */
2948 assert(why != WHY_YIELD);
2949 /* Pop remaining stack entries. */
2950 while (!EMPTY()) {
2951 v = POP();
2952 Py_XDECREF(v);
2955 if (why != WHY_RETURN)
2956 retval = NULL;
2958 fast_yield:
2959 if (tstate->use_tracing) {
2960 if (tstate->c_tracefunc) {
2961 if (why == WHY_RETURN || why == WHY_YIELD) {
2962 if (call_trace(tstate->c_tracefunc,
2963 tstate->c_traceobj, f,
2964 PyTrace_RETURN, retval)) {
2965 Py_XDECREF(retval);
2966 retval = NULL;
2967 why = WHY_EXCEPTION;
2970 else if (why == WHY_EXCEPTION) {
2971 call_trace_protected(tstate->c_tracefunc,
2972 tstate->c_traceobj, f,
2973 PyTrace_RETURN, NULL);
2976 if (tstate->c_profilefunc) {
2977 if (why == WHY_EXCEPTION)
2978 call_trace_protected(tstate->c_profilefunc,
2979 tstate->c_profileobj, f,
2980 PyTrace_RETURN, NULL);
2981 else if (call_trace(tstate->c_profilefunc,
2982 tstate->c_profileobj, f,
2983 PyTrace_RETURN, retval)) {
2984 Py_XDECREF(retval);
2985 retval = NULL;
2986 why = WHY_EXCEPTION;
2991 if (tstate->frame->f_exc_type != NULL)
2992 reset_exc_info(tstate);
2993 else {
2994 assert(tstate->frame->f_exc_value == NULL);
2995 assert(tstate->frame->f_exc_traceback == NULL);
2998 /* pop frame */
2999 exit_eval_frame:
3000 Py_LeaveRecursiveCall();
3001 tstate->frame = f->f_back;
3003 return retval;
3006 /* This is gonna seem *real weird*, but if you put some other code between
3007 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
3008 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
3010 PyObject *
3011 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
3012 PyObject **args, int argcount, PyObject **kws, int kwcount,
3013 PyObject **defs, int defcount, PyObject *closure)
3015 register PyFrameObject *f;
3016 register PyObject *retval = NULL;
3017 register PyObject **fastlocals, **freevars;
3018 PyThreadState *tstate = PyThreadState_GET();
3019 PyObject *x, *u;
3021 if (globals == NULL) {
3022 PyErr_SetString(PyExc_SystemError,
3023 "PyEval_EvalCodeEx: NULL globals");
3024 return NULL;
3027 assert(tstate != NULL);
3028 assert(globals != NULL);
3029 f = PyFrame_New(tstate, co, globals, locals);
3030 if (f == NULL)
3031 return NULL;
3033 fastlocals = f->f_localsplus;
3034 freevars = f->f_localsplus + co->co_nlocals;
3036 if (co->co_argcount > 0 ||
3037 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3038 int i;
3039 int n = argcount;
3040 PyObject *kwdict = NULL;
3041 if (co->co_flags & CO_VARKEYWORDS) {
3042 kwdict = PyDict_New();
3043 if (kwdict == NULL)
3044 goto fail;
3045 i = co->co_argcount;
3046 if (co->co_flags & CO_VARARGS)
3047 i++;
3048 SETLOCAL(i, kwdict);
3050 if (argcount > co->co_argcount) {
3051 if (!(co->co_flags & CO_VARARGS)) {
3052 PyErr_Format(PyExc_TypeError,
3053 "%.200s() takes %s %d "
3054 "%sargument%s (%d given)",
3055 PyString_AsString(co->co_name),
3056 defcount ? "at most" : "exactly",
3057 co->co_argcount,
3058 kwcount ? "non-keyword " : "",
3059 co->co_argcount == 1 ? "" : "s",
3060 argcount);
3061 goto fail;
3063 n = co->co_argcount;
3065 for (i = 0; i < n; i++) {
3066 x = args[i];
3067 Py_INCREF(x);
3068 SETLOCAL(i, x);
3070 if (co->co_flags & CO_VARARGS) {
3071 u = PyTuple_New(argcount - n);
3072 if (u == NULL)
3073 goto fail;
3074 SETLOCAL(co->co_argcount, u);
3075 for (i = n; i < argcount; i++) {
3076 x = args[i];
3077 Py_INCREF(x);
3078 PyTuple_SET_ITEM(u, i-n, x);
3081 for (i = 0; i < kwcount; i++) {
3082 PyObject **co_varnames;
3083 PyObject *keyword = kws[2*i];
3084 PyObject *value = kws[2*i + 1];
3085 int j;
3086 if (keyword == NULL || !(PyString_Check(keyword)
3087 #ifdef Py_USING_UNICODE
3088 || PyUnicode_Check(keyword)
3089 #endif
3090 )) {
3091 PyErr_Format(PyExc_TypeError,
3092 "%.200s() keywords must be strings",
3093 PyString_AsString(co->co_name));
3094 goto fail;
3096 /* Speed hack: do raw pointer compares. As names are
3097 normally interned this should almost always hit. */
3098 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
3099 for (j = 0; j < co->co_argcount; j++) {
3100 PyObject *nm = co_varnames[j];
3101 if (nm == keyword)
3102 goto kw_found;
3104 /* Slow fallback, just in case */
3105 for (j = 0; j < co->co_argcount; j++) {
3106 PyObject *nm = co_varnames[j];
3107 int cmp = PyObject_RichCompareBool(
3108 keyword, nm, Py_EQ);
3109 if (cmp > 0)
3110 goto kw_found;
3111 else if (cmp < 0)
3112 goto fail;
3114 /* Check errors from Compare */
3115 if (PyErr_Occurred())
3116 goto fail;
3117 if (j >= co->co_argcount) {
3118 if (kwdict == NULL) {
3119 PyObject *kwd_str = kwd_as_string(keyword);
3120 if (kwd_str) {
3121 PyErr_Format(PyExc_TypeError,
3122 "%.200s() got an unexpected "
3123 "keyword argument '%.400s'",
3124 PyString_AsString(co->co_name),
3125 PyString_AsString(kwd_str));
3126 Py_DECREF(kwd_str);
3128 goto fail;
3130 PyDict_SetItem(kwdict, keyword, value);
3131 continue;
3133 kw_found:
3134 if (GETLOCAL(j) != NULL) {
3135 PyObject *kwd_str = kwd_as_string(keyword);
3136 if (kwd_str) {
3137 PyErr_Format(PyExc_TypeError,
3138 "%.200s() got multiple "
3139 "values for keyword "
3140 "argument '%.400s'",
3141 PyString_AsString(co->co_name),
3142 PyString_AsString(kwd_str));
3143 Py_DECREF(kwd_str);
3145 goto fail;
3147 Py_INCREF(value);
3148 SETLOCAL(j, value);
3150 if (argcount < co->co_argcount) {
3151 int m = co->co_argcount - defcount;
3152 for (i = argcount; i < m; i++) {
3153 if (GETLOCAL(i) == NULL) {
3154 PyErr_Format(PyExc_TypeError,
3155 "%.200s() takes %s %d "
3156 "%sargument%s (%d given)",
3157 PyString_AsString(co->co_name),
3158 ((co->co_flags & CO_VARARGS) ||
3159 defcount) ? "at least"
3160 : "exactly",
3161 m, kwcount ? "non-keyword " : "",
3162 m == 1 ? "" : "s", i);
3163 goto fail;
3166 if (n > m)
3167 i = n - m;
3168 else
3169 i = 0;
3170 for (; i < defcount; i++) {
3171 if (GETLOCAL(m+i) == NULL) {
3172 PyObject *def = defs[i];
3173 Py_INCREF(def);
3174 SETLOCAL(m+i, def);
3179 else {
3180 if (argcount > 0 || kwcount > 0) {
3181 PyErr_Format(PyExc_TypeError,
3182 "%.200s() takes no arguments (%d given)",
3183 PyString_AsString(co->co_name),
3184 argcount + kwcount);
3185 goto fail;
3188 /* Allocate and initialize storage for cell vars, and copy free
3189 vars into frame. This isn't too efficient right now. */
3190 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3191 int i, j, nargs, found;
3192 char *cellname, *argname;
3193 PyObject *c;
3195 nargs = co->co_argcount;
3196 if (co->co_flags & CO_VARARGS)
3197 nargs++;
3198 if (co->co_flags & CO_VARKEYWORDS)
3199 nargs++;
3201 /* Initialize each cell var, taking into account
3202 cell vars that are initialized from arguments.
3204 Should arrange for the compiler to put cellvars
3205 that are arguments at the beginning of the cellvars
3206 list so that we can march over it more efficiently?
3208 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3209 cellname = PyString_AS_STRING(
3210 PyTuple_GET_ITEM(co->co_cellvars, i));
3211 found = 0;
3212 for (j = 0; j < nargs; j++) {
3213 argname = PyString_AS_STRING(
3214 PyTuple_GET_ITEM(co->co_varnames, j));
3215 if (strcmp(cellname, argname) == 0) {
3216 c = PyCell_New(GETLOCAL(j));
3217 if (c == NULL)
3218 goto fail;
3219 GETLOCAL(co->co_nlocals + i) = c;
3220 found = 1;
3221 break;
3224 if (found == 0) {
3225 c = PyCell_New(NULL);
3226 if (c == NULL)
3227 goto fail;
3228 SETLOCAL(co->co_nlocals + i, c);
3232 if (PyTuple_GET_SIZE(co->co_freevars)) {
3233 int i;
3234 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3235 PyObject *o = PyTuple_GET_ITEM(closure, i);
3236 Py_INCREF(o);
3237 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3241 if (co->co_flags & CO_GENERATOR) {
3242 /* Don't need to keep the reference to f_back, it will be set
3243 * when the generator is resumed. */
3244 Py_XDECREF(f->f_back);
3245 f->f_back = NULL;
3247 PCALL(PCALL_GENERATOR);
3249 /* Create a new generator that owns the ready to run frame
3250 * and return that as the value. */
3251 return PyGen_New(f);
3254 retval = PyEval_EvalFrameEx(f,0);
3256 fail: /* Jump here from prelude on failure */
3258 /* decref'ing the frame can cause __del__ methods to get invoked,
3259 which can call back into Python. While we're done with the
3260 current Python frame (f), the associated C stack is still in use,
3261 so recursion_depth must be boosted for the duration.
3263 assert(tstate != NULL);
3264 ++tstate->recursion_depth;
3265 Py_DECREF(f);
3266 --tstate->recursion_depth;
3267 return retval;
3271 static PyObject *
3272 special_lookup(PyObject *o, char *meth, PyObject **cache)
3274 PyObject *res;
3275 if (PyInstance_Check(o)) {
3276 if (!*cache)
3277 return PyObject_GetAttrString(o, meth);
3278 else
3279 return PyObject_GetAttr(o, *cache);
3281 res = _PyObject_LookupSpecial(o, meth, cache);
3282 if (res == NULL && !PyErr_Occurred()) {
3283 PyErr_SetObject(PyExc_AttributeError, *cache);
3284 return NULL;
3286 return res;
3290 static PyObject *
3291 kwd_as_string(PyObject *kwd) {
3292 #ifdef Py_USING_UNICODE
3293 if (PyString_Check(kwd)) {
3294 #else
3295 assert(PyString_Check(kwd));
3296 #endif
3297 Py_INCREF(kwd);
3298 return kwd;
3299 #ifdef Py_USING_UNICODE
3301 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3302 #endif
3306 /* Implementation notes for set_exc_info() and reset_exc_info():
3308 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3309 'exc_traceback'. These always travel together.
3311 - tstate->curexc_ZZZ is the "hot" exception that is set by
3312 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3314 - Once an exception is caught by an except clause, it is transferred
3315 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3316 can pick it up. This is the primary task of set_exc_info().
3317 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
3319 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3321 Long ago, when none of this existed, there were just a few globals:
3322 one set corresponding to the "hot" exception, and one set
3323 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3324 globals; they were simply stored as sys.exc_ZZZ. For backwards
3325 compatibility, they still are!) The problem was that in code like
3326 this:
3328 try:
3329 "something that may fail"
3330 except "some exception":
3331 "do something else first"
3332 "print the exception from sys.exc_ZZZ."
3334 if "do something else first" invoked something that raised and caught
3335 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3336 cause of subtle bugs. I fixed this by changing the semantics as
3337 follows:
3339 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3340 *in that frame*.
3342 - But initially, and as long as no exception is caught in a given
3343 frame, sys.exc_ZZZ will hold the last exception caught in the
3344 previous frame (or the frame before that, etc.).
3346 The first bullet fixed the bug in the above example. The second
3347 bullet was for backwards compatibility: it was (and is) common to
3348 have a function that is called when an exception is caught, and to
3349 have that function access the caught exception via sys.exc_ZZZ.
3350 (Example: traceback.print_exc()).
3352 At the same time I fixed the problem that sys.exc_ZZZ weren't
3353 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3354 but that's really a separate improvement.
3356 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3357 variables to what they were before the current frame was called. The
3358 set_exc_info() function saves them on the frame so that
3359 reset_exc_info() can restore them. The invariant is that
3360 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3361 exception (where "catching" an exception applies only to successful
3362 except clauses); and if the current frame ever caught an exception,
3363 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3364 at the start of the current frame.
3368 static void
3369 set_exc_info(PyThreadState *tstate,
3370 PyObject *type, PyObject *value, PyObject *tb)
3372 PyFrameObject *frame = tstate->frame;
3373 PyObject *tmp_type, *tmp_value, *tmp_tb;
3375 assert(type != NULL);
3376 assert(frame != NULL);
3377 if (frame->f_exc_type == NULL) {
3378 assert(frame->f_exc_value == NULL);
3379 assert(frame->f_exc_traceback == NULL);
3380 /* This frame didn't catch an exception before. */
3381 /* Save previous exception of this thread in this frame. */
3382 if (tstate->exc_type == NULL) {
3383 /* XXX Why is this set to Py_None? */
3384 Py_INCREF(Py_None);
3385 tstate->exc_type = Py_None;
3387 Py_INCREF(tstate->exc_type);
3388 Py_XINCREF(tstate->exc_value);
3389 Py_XINCREF(tstate->exc_traceback);
3390 frame->f_exc_type = tstate->exc_type;
3391 frame->f_exc_value = tstate->exc_value;
3392 frame->f_exc_traceback = tstate->exc_traceback;
3394 /* Set new exception for this thread. */
3395 tmp_type = tstate->exc_type;
3396 tmp_value = tstate->exc_value;
3397 tmp_tb = tstate->exc_traceback;
3398 Py_INCREF(type);
3399 Py_XINCREF(value);
3400 Py_XINCREF(tb);
3401 tstate->exc_type = type;
3402 tstate->exc_value = value;
3403 tstate->exc_traceback = tb;
3404 Py_XDECREF(tmp_type);
3405 Py_XDECREF(tmp_value);
3406 Py_XDECREF(tmp_tb);
3407 /* For b/w compatibility */
3408 PySys_SetObject("exc_type", type);
3409 PySys_SetObject("exc_value", value);
3410 PySys_SetObject("exc_traceback", tb);
3413 static void
3414 reset_exc_info(PyThreadState *tstate)
3416 PyFrameObject *frame;
3417 PyObject *tmp_type, *tmp_value, *tmp_tb;
3419 /* It's a precondition that the thread state's frame caught an
3420 * exception -- verify in a debug build.
3422 assert(tstate != NULL);
3423 frame = tstate->frame;
3424 assert(frame != NULL);
3425 assert(frame->f_exc_type != NULL);
3427 /* Copy the frame's exception info back to the thread state. */
3428 tmp_type = tstate->exc_type;
3429 tmp_value = tstate->exc_value;
3430 tmp_tb = tstate->exc_traceback;
3431 Py_INCREF(frame->f_exc_type);
3432 Py_XINCREF(frame->f_exc_value);
3433 Py_XINCREF(frame->f_exc_traceback);
3434 tstate->exc_type = frame->f_exc_type;
3435 tstate->exc_value = frame->f_exc_value;
3436 tstate->exc_traceback = frame->f_exc_traceback;
3437 Py_XDECREF(tmp_type);
3438 Py_XDECREF(tmp_value);
3439 Py_XDECREF(tmp_tb);
3441 /* For b/w compatibility */
3442 PySys_SetObject("exc_type", frame->f_exc_type);
3443 PySys_SetObject("exc_value", frame->f_exc_value);
3444 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3446 /* Clear the frame's exception info. */
3447 tmp_type = frame->f_exc_type;
3448 tmp_value = frame->f_exc_value;
3449 tmp_tb = frame->f_exc_traceback;
3450 frame->f_exc_type = NULL;
3451 frame->f_exc_value = NULL;
3452 frame->f_exc_traceback = NULL;
3453 Py_DECREF(tmp_type);
3454 Py_XDECREF(tmp_value);
3455 Py_XDECREF(tmp_tb);
3458 /* Logic for the raise statement (too complicated for inlining).
3459 This *consumes* a reference count to each of its arguments. */
3460 static enum why_code
3461 do_raise(PyObject *type, PyObject *value, PyObject *tb)
3463 if (type == NULL) {
3464 /* Reraise */
3465 PyThreadState *tstate = PyThreadState_GET();
3466 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3467 value = tstate->exc_value;
3468 tb = tstate->exc_traceback;
3469 Py_XINCREF(type);
3470 Py_XINCREF(value);
3471 Py_XINCREF(tb);
3474 /* We support the following forms of raise:
3475 raise <class>, <classinstance>
3476 raise <class>, <argument tuple>
3477 raise <class>, None
3478 raise <class>, <argument>
3479 raise <classinstance>, None
3480 raise <string>, <object>
3481 raise <string>, None
3483 An omitted second argument is the same as None.
3485 In addition, raise <tuple>, <anything> is the same as
3486 raising the tuple's first item (and it better have one!);
3487 this rule is applied recursively.
3489 Finally, an optional third argument can be supplied, which
3490 gives the traceback to be substituted (useful when
3491 re-raising an exception after examining it). */
3493 /* First, check the traceback argument, replacing None with
3494 NULL. */
3495 if (tb == Py_None) {
3496 Py_DECREF(tb);
3497 tb = NULL;
3499 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3500 PyErr_SetString(PyExc_TypeError,
3501 "raise: arg 3 must be a traceback or None");
3502 goto raise_error;
3505 /* Next, replace a missing value with None */
3506 if (value == NULL) {
3507 value = Py_None;
3508 Py_INCREF(value);
3511 /* Next, repeatedly, replace a tuple exception with its first item */
3512 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3513 PyObject *tmp = type;
3514 type = PyTuple_GET_ITEM(type, 0);
3515 Py_INCREF(type);
3516 Py_DECREF(tmp);
3519 if (PyExceptionClass_Check(type))
3520 PyErr_NormalizeException(&type, &value, &tb);
3522 else if (PyExceptionInstance_Check(type)) {
3523 /* Raising an instance. The value should be a dummy. */
3524 if (value != Py_None) {
3525 PyErr_SetString(PyExc_TypeError,
3526 "instance exception may not have a separate value");
3527 goto raise_error;
3529 else {
3530 /* Normalize to raise <class>, <instance> */
3531 Py_DECREF(value);
3532 value = type;
3533 type = PyExceptionInstance_Class(type);
3534 Py_INCREF(type);
3537 else {
3538 /* Not something you can raise. You get an exception
3539 anyway, just not what you specified :-) */
3540 PyErr_Format(PyExc_TypeError,
3541 "exceptions must be classes or instances, not %s",
3542 type->ob_type->tp_name);
3543 goto raise_error;
3546 assert(PyExceptionClass_Check(type));
3547 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3548 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3549 "exceptions must derive from BaseException "
3550 "in 3.x", 1) < 0)
3551 goto raise_error;
3554 PyErr_Restore(type, value, tb);
3555 if (tb == NULL)
3556 return WHY_EXCEPTION;
3557 else
3558 return WHY_RERAISE;
3559 raise_error:
3560 Py_XDECREF(value);
3561 Py_XDECREF(type);
3562 Py_XDECREF(tb);
3563 return WHY_EXCEPTION;
3566 /* Iterate v argcnt times and store the results on the stack (via decreasing
3567 sp). Return 1 for success, 0 if error. */
3569 static int
3570 unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
3572 int i = 0;
3573 PyObject *it; /* iter(v) */
3574 PyObject *w;
3576 assert(v != NULL);
3578 it = PyObject_GetIter(v);
3579 if (it == NULL)
3580 goto Error;
3582 for (; i < argcnt; i++) {
3583 w = PyIter_Next(it);
3584 if (w == NULL) {
3585 /* Iterator done, via error or exhaustion. */
3586 if (!PyErr_Occurred()) {
3587 PyErr_Format(PyExc_ValueError,
3588 "need more than %d value%s to unpack",
3589 i, i == 1 ? "" : "s");
3591 goto Error;
3593 *--sp = w;
3596 /* We better have exhausted the iterator now. */
3597 w = PyIter_Next(it);
3598 if (w == NULL) {
3599 if (PyErr_Occurred())
3600 goto Error;
3601 Py_DECREF(it);
3602 return 1;
3604 Py_DECREF(w);
3605 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3606 /* fall through */
3607 Error:
3608 for (; i > 0; i--, sp++)
3609 Py_DECREF(*sp);
3610 Py_XDECREF(it);
3611 return 0;
3615 #ifdef LLTRACE
3616 static int
3617 prtrace(PyObject *v, char *str)
3619 printf("%s ", str);
3620 if (PyObject_Print(v, stdout, 0) != 0)
3621 PyErr_Clear(); /* Don't know what else to do */
3622 printf("\n");
3623 return 1;
3625 #endif
3627 static void
3628 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
3630 PyObject *type, *value, *traceback, *arg;
3631 int err;
3632 PyErr_Fetch(&type, &value, &traceback);
3633 if (value == NULL) {
3634 value = Py_None;
3635 Py_INCREF(value);
3637 arg = PyTuple_Pack(3, type, value, traceback);
3638 if (arg == NULL) {
3639 PyErr_Restore(type, value, traceback);
3640 return;
3642 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3643 Py_DECREF(arg);
3644 if (err == 0)
3645 PyErr_Restore(type, value, traceback);
3646 else {
3647 Py_XDECREF(type);
3648 Py_XDECREF(value);
3649 Py_XDECREF(traceback);
3653 static int
3654 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3655 int what, PyObject *arg)
3657 PyObject *type, *value, *traceback;
3658 int err;
3659 PyErr_Fetch(&type, &value, &traceback);
3660 err = call_trace(func, obj, frame, what, arg);
3661 if (err == 0)
3663 PyErr_Restore(type, value, traceback);
3664 return 0;
3666 else {
3667 Py_XDECREF(type);
3668 Py_XDECREF(value);
3669 Py_XDECREF(traceback);
3670 return -1;
3674 static int
3675 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3676 int what, PyObject *arg)
3678 register PyThreadState *tstate = frame->f_tstate;
3679 int result;
3680 if (tstate->tracing)
3681 return 0;
3682 tstate->tracing++;
3683 tstate->use_tracing = 0;
3684 result = func(obj, frame, what, arg);
3685 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3686 || (tstate->c_profilefunc != NULL));
3687 tstate->tracing--;
3688 return result;
3691 PyObject *
3692 _PyEval_CallTracing(PyObject *func, PyObject *args)
3694 PyFrameObject *frame = PyEval_GetFrame();
3695 PyThreadState *tstate = frame->f_tstate;
3696 int save_tracing = tstate->tracing;
3697 int save_use_tracing = tstate->use_tracing;
3698 PyObject *result;
3700 tstate->tracing = 0;
3701 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3702 || (tstate->c_profilefunc != NULL));
3703 result = PyObject_Call(func, args, NULL);
3704 tstate->tracing = save_tracing;
3705 tstate->use_tracing = save_use_tracing;
3706 return result;
3709 /* See Objects/lnotab_notes.txt for a description of how tracing works. */
3710 static int
3711 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
3712 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3713 int *instr_prev)
3715 int result = 0;
3716 int line = frame->f_lineno;
3718 /* If the last instruction executed isn't in the current
3719 instruction window, reset the window.
3721 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3722 PyAddrPair bounds;
3723 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3724 &bounds);
3725 *instr_lb = bounds.ap_lower;
3726 *instr_ub = bounds.ap_upper;
3728 /* If the last instruction falls at the start of a line or if
3729 it represents a jump backwards, update the frame's line
3730 number and call the trace function. */
3731 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3732 frame->f_lineno = line;
3733 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3735 *instr_prev = frame->f_lasti;
3736 return result;
3739 void
3740 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
3742 PyThreadState *tstate = PyThreadState_GET();
3743 PyObject *temp = tstate->c_profileobj;
3744 Py_XINCREF(arg);
3745 tstate->c_profilefunc = NULL;
3746 tstate->c_profileobj = NULL;
3747 /* Must make sure that tracing is not ignored if 'temp' is freed */
3748 tstate->use_tracing = tstate->c_tracefunc != NULL;
3749 Py_XDECREF(temp);
3750 tstate->c_profilefunc = func;
3751 tstate->c_profileobj = arg;
3752 /* Flag that tracing or profiling is turned on */
3753 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
3756 void
3757 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3759 PyThreadState *tstate = PyThreadState_GET();
3760 PyObject *temp = tstate->c_traceobj;
3761 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3762 Py_XINCREF(arg);
3763 tstate->c_tracefunc = NULL;
3764 tstate->c_traceobj = NULL;
3765 /* Must make sure that profiling is not ignored if 'temp' is freed */
3766 tstate->use_tracing = tstate->c_profilefunc != NULL;
3767 Py_XDECREF(temp);
3768 tstate->c_tracefunc = func;
3769 tstate->c_traceobj = arg;
3770 /* Flag that tracing or profiling is turned on */
3771 tstate->use_tracing = ((func != NULL)
3772 || (tstate->c_profilefunc != NULL));
3775 PyObject *
3776 PyEval_GetBuiltins(void)
3778 PyFrameObject *current_frame = PyEval_GetFrame();
3779 if (current_frame == NULL)
3780 return PyThreadState_GET()->interp->builtins;
3781 else
3782 return current_frame->f_builtins;
3785 PyObject *
3786 PyEval_GetLocals(void)
3788 PyFrameObject *current_frame = PyEval_GetFrame();
3789 if (current_frame == NULL)
3790 return NULL;
3791 PyFrame_FastToLocals(current_frame);
3792 return current_frame->f_locals;
3795 PyObject *
3796 PyEval_GetGlobals(void)
3798 PyFrameObject *current_frame = PyEval_GetFrame();
3799 if (current_frame == NULL)
3800 return NULL;
3801 else
3802 return current_frame->f_globals;
3805 PyFrameObject *
3806 PyEval_GetFrame(void)
3808 PyThreadState *tstate = PyThreadState_GET();
3809 return _PyThreadState_GetFrame(tstate);
3813 PyEval_GetRestricted(void)
3815 PyFrameObject *current_frame = PyEval_GetFrame();
3816 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
3820 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
3822 PyFrameObject *current_frame = PyEval_GetFrame();
3823 int result = cf->cf_flags != 0;
3825 if (current_frame != NULL) {
3826 const int codeflags = current_frame->f_code->co_flags;
3827 const int compilerflags = codeflags & PyCF_MASK;
3828 if (compilerflags) {
3829 result = 1;
3830 cf->cf_flags |= compilerflags;
3832 #if 0 /* future keyword */
3833 if (codeflags & CO_GENERATOR_ALLOWED) {
3834 result = 1;
3835 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3837 #endif
3839 return result;
3843 Py_FlushLine(void)
3845 PyObject *f = PySys_GetObject("stdout");
3846 if (f == NULL)
3847 return 0;
3848 if (!PyFile_SoftSpace(f, 0))
3849 return 0;
3850 return PyFile_WriteString("\n", f);
3854 /* External interface to call any callable object.
3855 The arg must be a tuple or NULL. */
3857 #undef PyEval_CallObject
3858 /* for backward compatibility: export this interface */
3860 PyObject *
3861 PyEval_CallObject(PyObject *func, PyObject *arg)
3863 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
3865 #define PyEval_CallObject(func,arg) \
3866 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
3868 PyObject *
3869 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
3871 PyObject *result;
3873 if (arg == NULL) {
3874 arg = PyTuple_New(0);
3875 if (arg == NULL)
3876 return NULL;
3878 else if (!PyTuple_Check(arg)) {
3879 PyErr_SetString(PyExc_TypeError,
3880 "argument list must be a tuple");
3881 return NULL;
3883 else
3884 Py_INCREF(arg);
3886 if (kw != NULL && !PyDict_Check(kw)) {
3887 PyErr_SetString(PyExc_TypeError,
3888 "keyword list must be a dictionary");
3889 Py_DECREF(arg);
3890 return NULL;
3893 result = PyObject_Call(func, arg, kw);
3894 Py_DECREF(arg);
3895 return result;
3898 const char *
3899 PyEval_GetFuncName(PyObject *func)
3901 if (PyMethod_Check(func))
3902 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3903 else if (PyFunction_Check(func))
3904 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3905 else if (PyCFunction_Check(func))
3906 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3907 else if (PyClass_Check(func))
3908 return PyString_AsString(((PyClassObject*)func)->cl_name);
3909 else if (PyInstance_Check(func)) {
3910 return PyString_AsString(
3911 ((PyInstanceObject*)func)->in_class->cl_name);
3912 } else {
3913 return func->ob_type->tp_name;
3917 const char *
3918 PyEval_GetFuncDesc(PyObject *func)
3920 if (PyMethod_Check(func))
3921 return "()";
3922 else if (PyFunction_Check(func))
3923 return "()";
3924 else if (PyCFunction_Check(func))
3925 return "()";
3926 else if (PyClass_Check(func))
3927 return " constructor";
3928 else if (PyInstance_Check(func)) {
3929 return " instance";
3930 } else {
3931 return " object";
3935 static void
3936 err_args(PyObject *func, int flags, int nargs)
3938 if (flags & METH_NOARGS)
3939 PyErr_Format(PyExc_TypeError,
3940 "%.200s() takes no arguments (%d given)",
3941 ((PyCFunctionObject *)func)->m_ml->ml_name,
3942 nargs);
3943 else
3944 PyErr_Format(PyExc_TypeError,
3945 "%.200s() takes exactly one argument (%d given)",
3946 ((PyCFunctionObject *)func)->m_ml->ml_name,
3947 nargs);
3950 #define C_TRACE(x, call) \
3951 if (tstate->use_tracing && tstate->c_profilefunc) { \
3952 if (call_trace(tstate->c_profilefunc, \
3953 tstate->c_profileobj, \
3954 tstate->frame, PyTrace_C_CALL, \
3955 func)) { \
3956 x = NULL; \
3958 else { \
3959 x = call; \
3960 if (tstate->c_profilefunc != NULL) { \
3961 if (x == NULL) { \
3962 call_trace_protected(tstate->c_profilefunc, \
3963 tstate->c_profileobj, \
3964 tstate->frame, PyTrace_C_EXCEPTION, \
3965 func); \
3966 /* XXX should pass (type, value, tb) */ \
3967 } else { \
3968 if (call_trace(tstate->c_profilefunc, \
3969 tstate->c_profileobj, \
3970 tstate->frame, PyTrace_C_RETURN, \
3971 func)) { \
3972 Py_DECREF(x); \
3973 x = NULL; \
3978 } else { \
3979 x = call; \
3982 static PyObject *
3983 call_function(PyObject ***pp_stack, int oparg
3984 #ifdef WITH_TSC
3985 , uint64* pintr0, uint64* pintr1
3986 #endif
3989 int na = oparg & 0xff;
3990 int nk = (oparg>>8) & 0xff;
3991 int n = na + 2 * nk;
3992 PyObject **pfunc = (*pp_stack) - n - 1;
3993 PyObject *func = *pfunc;
3994 PyObject *x, *w;
3996 /* Always dispatch PyCFunction first, because these are
3997 presumed to be the most frequent callable object.
3999 if (PyCFunction_Check(func) && nk == 0) {
4000 int flags = PyCFunction_GET_FLAGS(func);
4001 PyThreadState *tstate = PyThreadState_GET();
4003 PCALL(PCALL_CFUNCTION);
4004 if (flags & (METH_NOARGS | METH_O)) {
4005 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4006 PyObject *self = PyCFunction_GET_SELF(func);
4007 if (flags & METH_NOARGS && na == 0) {
4008 C_TRACE(x, (*meth)(self,NULL));
4010 else if (flags & METH_O && na == 1) {
4011 PyObject *arg = EXT_POP(*pp_stack);
4012 C_TRACE(x, (*meth)(self,arg));
4013 Py_DECREF(arg);
4015 else {
4016 err_args(func, flags, na);
4017 x = NULL;
4020 else {
4021 PyObject *callargs;
4022 callargs = load_args(pp_stack, na);
4023 READ_TIMESTAMP(*pintr0);
4024 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4025 READ_TIMESTAMP(*pintr1);
4026 Py_XDECREF(callargs);
4028 } else {
4029 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4030 /* optimize access to bound methods */
4031 PyObject *self = PyMethod_GET_SELF(func);
4032 PCALL(PCALL_METHOD);
4033 PCALL(PCALL_BOUND_METHOD);
4034 Py_INCREF(self);
4035 func = PyMethod_GET_FUNCTION(func);
4036 Py_INCREF(func);
4037 Py_DECREF(*pfunc);
4038 *pfunc = self;
4039 na++;
4040 n++;
4041 } else
4042 Py_INCREF(func);
4043 READ_TIMESTAMP(*pintr0);
4044 if (PyFunction_Check(func))
4045 x = fast_function(func, pp_stack, n, na, nk);
4046 else
4047 x = do_call(func, pp_stack, na, nk);
4048 READ_TIMESTAMP(*pintr1);
4049 Py_DECREF(func);
4052 /* Clear the stack of the function object. Also removes
4053 the arguments in case they weren't consumed already
4054 (fast_function() and err_args() leave them on the stack).
4056 while ((*pp_stack) > pfunc) {
4057 w = EXT_POP(*pp_stack);
4058 Py_DECREF(w);
4059 PCALL(PCALL_POP);
4061 return x;
4064 /* The fast_function() function optimize calls for which no argument
4065 tuple is necessary; the objects are passed directly from the stack.
4066 For the simplest case -- a function that takes only positional
4067 arguments and is called with only positional arguments -- it
4068 inlines the most primitive frame setup code from
4069 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4070 done before evaluating the frame.
4073 static PyObject *
4074 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
4076 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4077 PyObject *globals = PyFunction_GET_GLOBALS(func);
4078 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4079 PyObject **d = NULL;
4080 int nd = 0;
4082 PCALL(PCALL_FUNCTION);
4083 PCALL(PCALL_FAST_FUNCTION);
4084 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4085 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4086 PyFrameObject *f;
4087 PyObject *retval = NULL;
4088 PyThreadState *tstate = PyThreadState_GET();
4089 PyObject **fastlocals, **stack;
4090 int i;
4092 PCALL(PCALL_FASTER_FUNCTION);
4093 assert(globals != NULL);
4094 /* XXX Perhaps we should create a specialized
4095 PyFrame_New() that doesn't take locals, but does
4096 take builtins without sanity checking them.
4098 assert(tstate != NULL);
4099 f = PyFrame_New(tstate, co, globals, NULL);
4100 if (f == NULL)
4101 return NULL;
4103 fastlocals = f->f_localsplus;
4104 stack = (*pp_stack) - n;
4106 for (i = 0; i < n; i++) {
4107 Py_INCREF(*stack);
4108 fastlocals[i] = *stack++;
4110 retval = PyEval_EvalFrameEx(f,0);
4111 ++tstate->recursion_depth;
4112 Py_DECREF(f);
4113 --tstate->recursion_depth;
4114 return retval;
4116 if (argdefs != NULL) {
4117 d = &PyTuple_GET_ITEM(argdefs, 0);
4118 nd = Py_SIZE(argdefs);
4120 return PyEval_EvalCodeEx(co, globals,
4121 (PyObject *)NULL, (*pp_stack)-n, na,
4122 (*pp_stack)-2*nk, nk, d, nd,
4123 PyFunction_GET_CLOSURE(func));
4126 static PyObject *
4127 update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4128 PyObject *func)
4130 PyObject *kwdict = NULL;
4131 if (orig_kwdict == NULL)
4132 kwdict = PyDict_New();
4133 else {
4134 kwdict = PyDict_Copy(orig_kwdict);
4135 Py_DECREF(orig_kwdict);
4137 if (kwdict == NULL)
4138 return NULL;
4139 while (--nk >= 0) {
4140 int err;
4141 PyObject *value = EXT_POP(*pp_stack);
4142 PyObject *key = EXT_POP(*pp_stack);
4143 if (PyDict_GetItem(kwdict, key) != NULL) {
4144 PyErr_Format(PyExc_TypeError,
4145 "%.200s%s got multiple values "
4146 "for keyword argument '%.200s'",
4147 PyEval_GetFuncName(func),
4148 PyEval_GetFuncDesc(func),
4149 PyString_AsString(key));
4150 Py_DECREF(key);
4151 Py_DECREF(value);
4152 Py_DECREF(kwdict);
4153 return NULL;
4155 err = PyDict_SetItem(kwdict, key, value);
4156 Py_DECREF(key);
4157 Py_DECREF(value);
4158 if (err) {
4159 Py_DECREF(kwdict);
4160 return NULL;
4163 return kwdict;
4166 static PyObject *
4167 update_star_args(int nstack, int nstar, PyObject *stararg,
4168 PyObject ***pp_stack)
4170 PyObject *callargs, *w;
4172 callargs = PyTuple_New(nstack + nstar);
4173 if (callargs == NULL) {
4174 return NULL;
4176 if (nstar) {
4177 int i;
4178 for (i = 0; i < nstar; i++) {
4179 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4180 Py_INCREF(a);
4181 PyTuple_SET_ITEM(callargs, nstack + i, a);
4184 while (--nstack >= 0) {
4185 w = EXT_POP(*pp_stack);
4186 PyTuple_SET_ITEM(callargs, nstack, w);
4188 return callargs;
4191 static PyObject *
4192 load_args(PyObject ***pp_stack, int na)
4194 PyObject *args = PyTuple_New(na);
4195 PyObject *w;
4197 if (args == NULL)
4198 return NULL;
4199 while (--na >= 0) {
4200 w = EXT_POP(*pp_stack);
4201 PyTuple_SET_ITEM(args, na, w);
4203 return args;
4206 static PyObject *
4207 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4209 PyObject *callargs = NULL;
4210 PyObject *kwdict = NULL;
4211 PyObject *result = NULL;
4213 if (nk > 0) {
4214 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4215 if (kwdict == NULL)
4216 goto call_fail;
4218 callargs = load_args(pp_stack, na);
4219 if (callargs == NULL)
4220 goto call_fail;
4221 #ifdef CALL_PROFILE
4222 /* At this point, we have to look at the type of func to
4223 update the call stats properly. Do it here so as to avoid
4224 exposing the call stats machinery outside ceval.c
4226 if (PyFunction_Check(func))
4227 PCALL(PCALL_FUNCTION);
4228 else if (PyMethod_Check(func))
4229 PCALL(PCALL_METHOD);
4230 else if (PyType_Check(func))
4231 PCALL(PCALL_TYPE);
4232 else if (PyCFunction_Check(func))
4233 PCALL(PCALL_CFUNCTION);
4234 else
4235 PCALL(PCALL_OTHER);
4236 #endif
4237 if (PyCFunction_Check(func)) {
4238 PyThreadState *tstate = PyThreadState_GET();
4239 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4241 else
4242 result = PyObject_Call(func, callargs, kwdict);
4243 call_fail:
4244 Py_XDECREF(callargs);
4245 Py_XDECREF(kwdict);
4246 return result;
4249 static PyObject *
4250 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4252 int nstar = 0;
4253 PyObject *callargs = NULL;
4254 PyObject *stararg = NULL;
4255 PyObject *kwdict = NULL;
4256 PyObject *result = NULL;
4258 if (flags & CALL_FLAG_KW) {
4259 kwdict = EXT_POP(*pp_stack);
4260 if (!PyDict_Check(kwdict)) {
4261 PyObject *d;
4262 d = PyDict_New();
4263 if (d == NULL)
4264 goto ext_call_fail;
4265 if (PyDict_Update(d, kwdict) != 0) {
4266 Py_DECREF(d);
4267 /* PyDict_Update raises attribute
4268 * error (percolated from an attempt
4269 * to get 'keys' attribute) instead of
4270 * a type error if its second argument
4271 * is not a mapping.
4273 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4274 PyErr_Format(PyExc_TypeError,
4275 "%.200s%.200s argument after ** "
4276 "must be a mapping, not %.200s",
4277 PyEval_GetFuncName(func),
4278 PyEval_GetFuncDesc(func),
4279 kwdict->ob_type->tp_name);
4281 goto ext_call_fail;
4283 Py_DECREF(kwdict);
4284 kwdict = d;
4287 if (flags & CALL_FLAG_VAR) {
4288 stararg = EXT_POP(*pp_stack);
4289 if (!PyTuple_Check(stararg)) {
4290 PyObject *t = NULL;
4291 t = PySequence_Tuple(stararg);
4292 if (t == NULL) {
4293 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4294 PyErr_Format(PyExc_TypeError,
4295 "%.200s%.200s argument after * "
4296 "must be a sequence, not %200s",
4297 PyEval_GetFuncName(func),
4298 PyEval_GetFuncDesc(func),
4299 stararg->ob_type->tp_name);
4301 goto ext_call_fail;
4303 Py_DECREF(stararg);
4304 stararg = t;
4306 nstar = PyTuple_GET_SIZE(stararg);
4308 if (nk > 0) {
4309 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4310 if (kwdict == NULL)
4311 goto ext_call_fail;
4313 callargs = update_star_args(na, nstar, stararg, pp_stack);
4314 if (callargs == NULL)
4315 goto ext_call_fail;
4316 #ifdef CALL_PROFILE
4317 /* At this point, we have to look at the type of func to
4318 update the call stats properly. Do it here so as to avoid
4319 exposing the call stats machinery outside ceval.c
4321 if (PyFunction_Check(func))
4322 PCALL(PCALL_FUNCTION);
4323 else if (PyMethod_Check(func))
4324 PCALL(PCALL_METHOD);
4325 else if (PyType_Check(func))
4326 PCALL(PCALL_TYPE);
4327 else if (PyCFunction_Check(func))
4328 PCALL(PCALL_CFUNCTION);
4329 else
4330 PCALL(PCALL_OTHER);
4331 #endif
4332 if (PyCFunction_Check(func)) {
4333 PyThreadState *tstate = PyThreadState_GET();
4334 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4336 else
4337 result = PyObject_Call(func, callargs, kwdict);
4338 ext_call_fail:
4339 Py_XDECREF(callargs);
4340 Py_XDECREF(kwdict);
4341 Py_XDECREF(stararg);
4342 return result;
4345 /* Extract a slice index from a PyInt or PyLong or an object with the
4346 nb_index slot defined, and store in *pi.
4347 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4348 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
4349 Return 0 on error, 1 on success.
4351 /* Note: If v is NULL, return success without storing into *pi. This
4352 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4353 called by the SLICE opcode with v and/or w equal to NULL.
4356 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
4358 if (v != NULL) {
4359 Py_ssize_t x;
4360 if (PyInt_Check(v)) {
4361 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4362 however, it looks like it should be AsSsize_t.
4363 There should be a comment here explaining why.
4365 x = PyInt_AS_LONG(v);
4367 else if (PyIndex_Check(v)) {
4368 x = PyNumber_AsSsize_t(v, NULL);
4369 if (x == -1 && PyErr_Occurred())
4370 return 0;
4372 else {
4373 PyErr_SetString(PyExc_TypeError,
4374 "slice indices must be integers or "
4375 "None or have an __index__ method");
4376 return 0;
4378 *pi = x;
4380 return 1;
4383 #undef ISINDEX
4384 #define ISINDEX(x) ((x) == NULL || \
4385 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
4387 static PyObject *
4388 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
4390 PyTypeObject *tp = u->ob_type;
4391 PySequenceMethods *sq = tp->tp_as_sequence;
4393 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4394 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4395 if (!_PyEval_SliceIndex(v, &ilow))
4396 return NULL;
4397 if (!_PyEval_SliceIndex(w, &ihigh))
4398 return NULL;
4399 return PySequence_GetSlice(u, ilow, ihigh);
4401 else {
4402 PyObject *slice = PySlice_New(v, w, NULL);
4403 if (slice != NULL) {
4404 PyObject *res = PyObject_GetItem(u, slice);
4405 Py_DECREF(slice);
4406 return res;
4408 else
4409 return NULL;
4413 static int
4414 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4415 /* u[v:w] = x */
4417 PyTypeObject *tp = u->ob_type;
4418 PySequenceMethods *sq = tp->tp_as_sequence;
4420 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4421 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4422 if (!_PyEval_SliceIndex(v, &ilow))
4423 return -1;
4424 if (!_PyEval_SliceIndex(w, &ihigh))
4425 return -1;
4426 if (x == NULL)
4427 return PySequence_DelSlice(u, ilow, ihigh);
4428 else
4429 return PySequence_SetSlice(u, ilow, ihigh, x);
4431 else {
4432 PyObject *slice = PySlice_New(v, w, NULL);
4433 if (slice != NULL) {
4434 int res;
4435 if (x != NULL)
4436 res = PyObject_SetItem(u, slice, x);
4437 else
4438 res = PyObject_DelItem(u, slice);
4439 Py_DECREF(slice);
4440 return res;
4442 else
4443 return -1;
4447 #define Py3kExceptionClass_Check(x) \
4448 (PyType_Check((x)) && \
4449 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4451 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4452 "BaseException is not allowed in 3.x"
4454 static PyObject *
4455 cmp_outcome(int op, register PyObject *v, register PyObject *w)
4457 int res = 0;
4458 switch (op) {
4459 case PyCmp_IS:
4460 res = (v == w);
4461 break;
4462 case PyCmp_IS_NOT:
4463 res = (v != w);
4464 break;
4465 case PyCmp_IN:
4466 res = PySequence_Contains(w, v);
4467 if (res < 0)
4468 return NULL;
4469 break;
4470 case PyCmp_NOT_IN:
4471 res = PySequence_Contains(w, v);
4472 if (res < 0)
4473 return NULL;
4474 res = !res;
4475 break;
4476 case PyCmp_EXC_MATCH:
4477 if (PyTuple_Check(w)) {
4478 Py_ssize_t i, length;
4479 length = PyTuple_Size(w);
4480 for (i = 0; i < length; i += 1) {
4481 PyObject *exc = PyTuple_GET_ITEM(w, i);
4482 if (PyString_Check(exc)) {
4483 int ret_val;
4484 ret_val = PyErr_WarnEx(
4485 PyExc_DeprecationWarning,
4486 "catching of string "
4487 "exceptions is deprecated", 1);
4488 if (ret_val < 0)
4489 return NULL;
4491 else if (Py_Py3kWarningFlag &&
4492 !PyTuple_Check(exc) &&
4493 !Py3kExceptionClass_Check(exc))
4495 int ret_val;
4496 ret_val = PyErr_WarnEx(
4497 PyExc_DeprecationWarning,
4498 CANNOT_CATCH_MSG, 1);
4499 if (ret_val < 0)
4500 return NULL;
4504 else {
4505 if (PyString_Check(w)) {
4506 int ret_val;
4507 ret_val = PyErr_WarnEx(
4508 PyExc_DeprecationWarning,
4509 "catching of string "
4510 "exceptions is deprecated", 1);
4511 if (ret_val < 0)
4512 return NULL;
4514 else if (Py_Py3kWarningFlag &&
4515 !PyTuple_Check(w) &&
4516 !Py3kExceptionClass_Check(w))
4518 int ret_val;
4519 ret_val = PyErr_WarnEx(
4520 PyExc_DeprecationWarning,
4521 CANNOT_CATCH_MSG, 1);
4522 if (ret_val < 0)
4523 return NULL;
4526 res = PyErr_GivenExceptionMatches(v, w);
4527 break;
4528 default:
4529 return PyObject_RichCompare(v, w, op);
4531 v = res ? Py_True : Py_False;
4532 Py_INCREF(v);
4533 return v;
4536 static PyObject *
4537 import_from(PyObject *v, PyObject *name)
4539 PyObject *x;
4541 x = PyObject_GetAttr(v, name);
4542 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4543 PyErr_Format(PyExc_ImportError,
4544 "cannot import name %.230s",
4545 PyString_AsString(name));
4547 return x;
4550 static int
4551 import_all_from(PyObject *locals, PyObject *v)
4553 PyObject *all = PyObject_GetAttrString(v, "__all__");
4554 PyObject *dict, *name, *value;
4555 int skip_leading_underscores = 0;
4556 int pos, err;
4558 if (all == NULL) {
4559 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4560 return -1; /* Unexpected error */
4561 PyErr_Clear();
4562 dict = PyObject_GetAttrString(v, "__dict__");
4563 if (dict == NULL) {
4564 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4565 return -1;
4566 PyErr_SetString(PyExc_ImportError,
4567 "from-import-* object has no __dict__ and no __all__");
4568 return -1;
4570 all = PyMapping_Keys(dict);
4571 Py_DECREF(dict);
4572 if (all == NULL)
4573 return -1;
4574 skip_leading_underscores = 1;
4577 for (pos = 0, err = 0; ; pos++) {
4578 name = PySequence_GetItem(all, pos);
4579 if (name == NULL) {
4580 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4581 err = -1;
4582 else
4583 PyErr_Clear();
4584 break;
4586 if (skip_leading_underscores &&
4587 PyString_Check(name) &&
4588 PyString_AS_STRING(name)[0] == '_')
4590 Py_DECREF(name);
4591 continue;
4593 value = PyObject_GetAttr(v, name);
4594 if (value == NULL)
4595 err = -1;
4596 else if (PyDict_CheckExact(locals))
4597 err = PyDict_SetItem(locals, name, value);
4598 else
4599 err = PyObject_SetItem(locals, name, value);
4600 Py_DECREF(name);
4601 Py_XDECREF(value);
4602 if (err != 0)
4603 break;
4605 Py_DECREF(all);
4606 return err;
4609 static PyObject *
4610 build_class(PyObject *methods, PyObject *bases, PyObject *name)
4612 PyObject *metaclass = NULL, *result, *base;
4614 if (PyDict_Check(methods))
4615 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4616 if (metaclass != NULL)
4617 Py_INCREF(metaclass);
4618 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4619 base = PyTuple_GET_ITEM(bases, 0);
4620 metaclass = PyObject_GetAttrString(base, "__class__");
4621 if (metaclass == NULL) {
4622 PyErr_Clear();
4623 metaclass = (PyObject *)base->ob_type;
4624 Py_INCREF(metaclass);
4627 else {
4628 PyObject *g = PyEval_GetGlobals();
4629 if (g != NULL && PyDict_Check(g))
4630 metaclass = PyDict_GetItemString(g, "__metaclass__");
4631 if (metaclass == NULL)
4632 metaclass = (PyObject *) &PyClass_Type;
4633 Py_INCREF(metaclass);
4635 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4636 NULL);
4637 Py_DECREF(metaclass);
4638 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4639 /* A type error here likely means that the user passed
4640 in a base that was not a class (such the random module
4641 instead of the random.random type). Help them out with
4642 by augmenting the error message with more information.*/
4644 PyObject *ptype, *pvalue, *ptraceback;
4646 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4647 if (PyString_Check(pvalue)) {
4648 PyObject *newmsg;
4649 newmsg = PyString_FromFormat(
4650 "Error when calling the metaclass bases\n"
4651 " %s",
4652 PyString_AS_STRING(pvalue));
4653 if (newmsg != NULL) {
4654 Py_DECREF(pvalue);
4655 pvalue = newmsg;
4658 PyErr_Restore(ptype, pvalue, ptraceback);
4660 return result;
4663 static int
4664 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4665 PyObject *locals)
4667 int n;
4668 PyObject *v;
4669 int plain = 0;
4671 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4672 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4673 /* Backward compatibility hack */
4674 globals = PyTuple_GetItem(prog, 1);
4675 if (n == 3)
4676 locals = PyTuple_GetItem(prog, 2);
4677 prog = PyTuple_GetItem(prog, 0);
4679 if (globals == Py_None) {
4680 globals = PyEval_GetGlobals();
4681 if (locals == Py_None) {
4682 locals = PyEval_GetLocals();
4683 plain = 1;
4685 if (!globals || !locals) {
4686 PyErr_SetString(PyExc_SystemError,
4687 "globals and locals cannot be NULL");
4688 return -1;
4691 else if (locals == Py_None)
4692 locals = globals;
4693 if (!PyString_Check(prog) &&
4694 #ifdef Py_USING_UNICODE
4695 !PyUnicode_Check(prog) &&
4696 #endif
4697 !PyCode_Check(prog) &&
4698 !PyFile_Check(prog)) {
4699 PyErr_SetString(PyExc_TypeError,
4700 "exec: arg 1 must be a string, file, or code object");
4701 return -1;
4703 if (!PyDict_Check(globals)) {
4704 PyErr_SetString(PyExc_TypeError,
4705 "exec: arg 2 must be a dictionary or None");
4706 return -1;
4708 if (!PyMapping_Check(locals)) {
4709 PyErr_SetString(PyExc_TypeError,
4710 "exec: arg 3 must be a mapping or None");
4711 return -1;
4713 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4714 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4715 if (PyCode_Check(prog)) {
4716 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4717 PyErr_SetString(PyExc_TypeError,
4718 "code object passed to exec may not contain free variables");
4719 return -1;
4721 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4723 else if (PyFile_Check(prog)) {
4724 FILE *fp = PyFile_AsFile(prog);
4725 char *name = PyString_AsString(PyFile_Name(prog));
4726 PyCompilerFlags cf;
4727 if (name == NULL)
4728 return -1;
4729 cf.cf_flags = 0;
4730 if (PyEval_MergeCompilerFlags(&cf))
4731 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4732 locals, &cf);
4733 else
4734 v = PyRun_File(fp, name, Py_file_input, globals,
4735 locals);
4737 else {
4738 PyObject *tmp = NULL;
4739 char *str;
4740 PyCompilerFlags cf;
4741 cf.cf_flags = 0;
4742 #ifdef Py_USING_UNICODE
4743 if (PyUnicode_Check(prog)) {
4744 tmp = PyUnicode_AsUTF8String(prog);
4745 if (tmp == NULL)
4746 return -1;
4747 prog = tmp;
4748 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4750 #endif
4751 if (PyString_AsStringAndSize(prog, &str, NULL))
4752 return -1;
4753 if (PyEval_MergeCompilerFlags(&cf))
4754 v = PyRun_StringFlags(str, Py_file_input, globals,
4755 locals, &cf);
4756 else
4757 v = PyRun_String(str, Py_file_input, globals, locals);
4758 Py_XDECREF(tmp);
4760 if (plain)
4761 PyFrame_LocalsToFast(f, 0);
4762 if (v == NULL)
4763 return -1;
4764 Py_DECREF(v);
4765 return 0;
4768 static void
4769 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4771 char *obj_str;
4773 if (!obj)
4774 return;
4776 obj_str = PyString_AsString(obj);
4777 if (!obj_str)
4778 return;
4780 PyErr_Format(exc, format_str, obj_str);
4783 static PyObject *
4784 string_concatenate(PyObject *v, PyObject *w,
4785 PyFrameObject *f, unsigned char *next_instr)
4787 /* This function implements 'variable += expr' when both arguments
4788 are strings. */
4789 Py_ssize_t v_len = PyString_GET_SIZE(v);
4790 Py_ssize_t w_len = PyString_GET_SIZE(w);
4791 Py_ssize_t new_len = v_len + w_len;
4792 if (new_len < 0) {
4793 PyErr_SetString(PyExc_OverflowError,
4794 "strings are too large to concat");
4795 return NULL;
4798 if (v->ob_refcnt == 2) {
4799 /* In the common case, there are 2 references to the value
4800 * stored in 'variable' when the += is performed: one on the
4801 * value stack (in 'v') and one still stored in the
4802 * 'variable'. We try to delete the variable now to reduce
4803 * the refcnt to 1.
4805 switch (*next_instr) {
4806 case STORE_FAST:
4808 int oparg = PEEKARG();
4809 PyObject **fastlocals = f->f_localsplus;
4810 if (GETLOCAL(oparg) == v)
4811 SETLOCAL(oparg, NULL);
4812 break;
4814 case STORE_DEREF:
4816 PyObject **freevars = (f->f_localsplus +
4817 f->f_code->co_nlocals);
4818 PyObject *c = freevars[PEEKARG()];
4819 if (PyCell_GET(c) == v)
4820 PyCell_Set(c, NULL);
4821 break;
4823 case STORE_NAME:
4825 PyObject *names = f->f_code->co_names;
4826 PyObject *name = GETITEM(names, PEEKARG());
4827 PyObject *locals = f->f_locals;
4828 if (PyDict_CheckExact(locals) &&
4829 PyDict_GetItem(locals, name) == v) {
4830 if (PyDict_DelItem(locals, name) != 0) {
4831 PyErr_Clear();
4834 break;
4839 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4840 /* Now we own the last reference to 'v', so we can resize it
4841 * in-place.
4843 if (_PyString_Resize(&v, new_len) != 0) {
4844 /* XXX if _PyString_Resize() fails, 'v' has been
4845 * deallocated so it cannot be put back into
4846 * 'variable'. The MemoryError is raised when there
4847 * is no value in 'variable', which might (very
4848 * remotely) be a cause of incompatibilities.
4850 return NULL;
4852 /* copy 'w' into the newly allocated area of 'v' */
4853 memcpy(PyString_AS_STRING(v) + v_len,
4854 PyString_AS_STRING(w), w_len);
4855 return v;
4857 else {
4858 /* When in-place resizing is not an option. */
4859 PyString_Concat(&v, w);
4860 return v;
4864 #ifdef DYNAMIC_EXECUTION_PROFILE
4866 static PyObject *
4867 getarray(long a[256])
4869 int i;
4870 PyObject *l = PyList_New(256);
4871 if (l == NULL) return NULL;
4872 for (i = 0; i < 256; i++) {
4873 PyObject *x = PyInt_FromLong(a[i]);
4874 if (x == NULL) {
4875 Py_DECREF(l);
4876 return NULL;
4878 PyList_SetItem(l, i, x);
4880 for (i = 0; i < 256; i++)
4881 a[i] = 0;
4882 return l;
4885 PyObject *
4886 _Py_GetDXProfile(PyObject *self, PyObject *args)
4888 #ifndef DXPAIRS
4889 return getarray(dxp);
4890 #else
4891 int i;
4892 PyObject *l = PyList_New(257);
4893 if (l == NULL) return NULL;
4894 for (i = 0; i < 257; i++) {
4895 PyObject *x = getarray(dxpairs[i]);
4896 if (x == NULL) {
4897 Py_DECREF(l);
4898 return NULL;
4900 PyList_SetItem(l, i, x);
4902 return l;
4903 #endif
4906 #endif