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