home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Python / ceval.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  67KB  |  2,881 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Execute compiled code */
  33.  
  34. /* XXX TO DO:
  35.    XXX how to pass arguments to call_trace?
  36.    XXX speed up searching for keywords by using a dictionary
  37.    XXX document it!
  38.    */
  39.  
  40. #include "Python.h"
  41.  
  42. #include "compile.h"
  43. #include "frameobject.h"
  44. #include "eval.h"
  45. #include "opcode.h"
  46.  
  47. #include <ctype.h>
  48.  
  49. #ifdef HAVE_LIMITS_H
  50. #include <limits.h>
  51. #else
  52. #define INT_MAX 2147483647
  53. #endif
  54.  
  55. /* Turn this on if your compiler chokes on the big switch: */
  56. /* #define CASE_TOO_BIG 1 */
  57.  
  58. #ifdef Py_DEBUG
  59. /* For debugging the interpreter: */
  60. #define LLTRACE  1    /* Low-level trace feature */
  61. #define CHECKEXC 1    /* Double-check exception checking */
  62. #endif
  63.  
  64.  
  65. /* Forward declarations */
  66.  
  67. static PyObject *eval_code2 Py_PROTO((PyCodeObject *,
  68.                  PyObject *, PyObject *,
  69.                  PyObject **, int,
  70.                  PyObject **, int,
  71.                  PyObject **, int,
  72.                  PyObject *));
  73. #ifdef LLTRACE
  74. static int prtrace Py_PROTO((PyObject *, char *));
  75. #endif
  76. static void call_exc_trace Py_PROTO((PyObject **, PyObject**,
  77.                      PyFrameObject *));
  78. static int call_trace Py_PROTO((PyObject **, PyObject **,
  79.                 PyFrameObject *, char *, PyObject *));
  80. static PyObject *call_builtin Py_PROTO((PyObject *, PyObject *, PyObject *));
  81. static PyObject *call_function Py_PROTO((PyObject *, PyObject *, PyObject *));
  82. static PyObject *loop_subscript Py_PROTO((PyObject *, PyObject *));
  83. static int slice_index Py_PROTO((PyObject *, int *));
  84. static PyObject *apply_slice Py_PROTO((PyObject *, PyObject *, PyObject *));
  85. static int assign_slice Py_PROTO((PyObject *, PyObject *,
  86.                   PyObject *, PyObject *));
  87. static PyObject *cmp_outcome Py_PROTO((int, PyObject *, PyObject *));
  88. static int import_from Py_PROTO((PyObject *, PyObject *, PyObject *));
  89. static PyObject *build_class Py_PROTO((PyObject *, PyObject *, PyObject *));
  90. static int exec_statement Py_PROTO((PyFrameObject *,
  91.                     PyObject *, PyObject *, PyObject *));
  92. static PyObject *find_from_args Py_PROTO((PyFrameObject *, int));
  93. static void set_exc_info Py_PROTO((PyThreadState *,
  94.                 PyObject *, PyObject *, PyObject *));
  95. static void reset_exc_info Py_PROTO((PyThreadState *));
  96.  
  97.  
  98. /* Dynamic execution profile */
  99. #ifdef DYNAMIC_EXECUTION_PROFILE
  100. #ifdef DXPAIRS
  101. static long dxpairs[257][256];
  102. #define dxp dxpairs[256]
  103. #else
  104. static long dxp[256];
  105. #endif
  106. #endif
  107.  
  108.  
  109. #ifdef WITH_THREAD
  110.  
  111. #ifndef DONT_HAVE_ERRNO_H
  112. #include <errno.h>
  113. #endif
  114. #include "pythread.h"
  115.  
  116. extern int _PyThread_Started; /* Flag for Py_Exit */
  117.  
  118. static PyThread_type_lock interpreter_lock = 0;
  119. static long main_thread = 0;
  120.  
  121. void
  122. PyEval_InitThreads()
  123. {
  124.     if (interpreter_lock)
  125.         return;
  126.     _PyThread_Started = 1;
  127.     interpreter_lock = PyThread_allocate_lock();
  128.     PyThread_acquire_lock(interpreter_lock, 1);
  129.     main_thread = PyThread_get_thread_ident();
  130. }
  131.  
  132. void
  133. PyEval_AcquireLock()
  134. {
  135.     PyThread_acquire_lock(interpreter_lock, 1);
  136. }
  137.  
  138. void
  139. PyEval_ReleaseLock()
  140. {
  141.     PyThread_release_lock(interpreter_lock);
  142. }
  143.  
  144. void
  145. PyEval_AcquireThread(tstate)
  146.     PyThreadState *tstate;
  147. {
  148.     if (tstate == NULL)
  149.         Py_FatalError("PyEval_AcquireThread: NULL new thread state");
  150.     PyThread_acquire_lock(interpreter_lock, 1);
  151.     if (PyThreadState_Swap(tstate) != NULL)
  152.         Py_FatalError(
  153.             "PyEval_AcquireThread: non-NULL old thread state");
  154. }
  155.  
  156. void
  157. PyEval_ReleaseThread(tstate)
  158.     PyThreadState *tstate;
  159. {
  160.     if (tstate == NULL)
  161.         Py_FatalError("PyEval_ReleaseThread: NULL thread state");
  162.     if (PyThreadState_Swap(NULL) != tstate)
  163.         Py_FatalError("PyEval_ReleaseThread: wrong thread state");
  164.     PyThread_release_lock(interpreter_lock);
  165. }
  166. #endif
  167.  
  168. /* Functions save_thread and restore_thread are always defined so
  169.    dynamically loaded modules needn't be compiled separately for use
  170.    with and without threads: */
  171.  
  172. PyThreadState *
  173. PyEval_SaveThread()
  174. {
  175.     PyThreadState *tstate = PyThreadState_Swap(NULL);
  176.     if (tstate == NULL)
  177.         Py_FatalError("PyEval_SaveThread: NULL tstate");
  178. #ifdef WITH_THREAD
  179.     if (interpreter_lock)
  180.         PyThread_release_lock(interpreter_lock);
  181. #endif
  182.     return tstate;
  183. }
  184.  
  185. void
  186. PyEval_RestoreThread(tstate)
  187.     PyThreadState *tstate;
  188. {
  189.     if (tstate == NULL)
  190.         Py_FatalError("PyEval_RestoreThread: NULL tstate");
  191. #ifdef WITH_THREAD
  192.     if (interpreter_lock) {
  193.         int err = errno;
  194.         PyThread_acquire_lock(interpreter_lock, 1);
  195.         errno = err;
  196.     }
  197. #endif
  198.     PyThreadState_Swap(tstate);
  199. }
  200.  
  201.  
  202. /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
  203.    signal handlers or Mac I/O completion routines) can schedule calls
  204.    to a function to be called synchronously.
  205.    The synchronous function is called with one void* argument.
  206.    It should return 0 for success or -1 for failure -- failure should
  207.    be accompanied by an exception.
  208.  
  209.    If registry succeeds, the registry function returns 0; if it fails
  210.    (e.g. due to too many pending calls) it returns -1 (without setting
  211.    an exception condition).
  212.  
  213.    Note that because registry may occur from within signal handlers,
  214.    or other asynchronous events, calling malloc() is unsafe!
  215.  
  216. #ifdef WITH_THREAD
  217.    Any thread can schedule pending calls, but only the main thread
  218.    will execute them.
  219. #endif
  220.  
  221.    XXX WARNING!  ASYNCHRONOUSLY EXECUTING CODE!
  222.    There are two possible race conditions:
  223.    (1) nested asynchronous registry calls;
  224.    (2) registry calls made while pending calls are being processed.
  225.    While (1) is very unlikely, (2) is a real possibility.
  226.    The current code is safe against (2), but not against (1).
  227.    The safety against (2) is derived from the fact that only one
  228.    thread (the main thread) ever takes things out of the queue.
  229.  
  230.    XXX Darn!  With the advent of thread state, we should have an array
  231.    of pending calls per thread in the thread state!  Later...
  232. */
  233.  
  234. #define NPENDINGCALLS 32
  235. static struct {
  236.     int (*func) Py_PROTO((ANY *));
  237.     ANY *arg;
  238. } pendingcalls[NPENDINGCALLS];
  239. static volatile int pendingfirst = 0;
  240. static volatile int pendinglast = 0;
  241. static volatile int things_to_do = 0;
  242.  
  243. int
  244. Py_AddPendingCall(func, arg)
  245.     int (*func) Py_PROTO((ANY *));
  246.     ANY *arg;
  247. {
  248.     static int busy = 0;
  249.     int i, j;
  250.     /* XXX Begin critical section */
  251.     /* XXX If you want this to be safe against nested
  252.        XXX asynchronous calls, you'll have to work harder! */
  253.     if (busy)
  254.         return -1;
  255.     busy = 1;
  256.     i = pendinglast;
  257.     j = (i + 1) % NPENDINGCALLS;
  258.     if (j == pendingfirst)
  259.         return -1; /* Queue full */
  260.     pendingcalls[i].func = func;
  261.     pendingcalls[i].arg = arg;
  262.     pendinglast = j;
  263.     things_to_do = 1; /* Signal main loop */
  264.     busy = 0;
  265.     /* XXX End critical section */
  266.     return 0;
  267. }
  268.  
  269. int
  270. Py_MakePendingCalls()
  271. {
  272.     static int busy = 0;
  273. #ifdef WITH_THREAD
  274.     if (main_thread && PyThread_get_thread_ident() != main_thread)
  275.         return 0;
  276. #endif
  277.     if (busy)
  278.         return 0;
  279.     busy = 1;
  280.     things_to_do = 0;
  281.     for (;;) {
  282.         int i;
  283.         int (*func) Py_PROTO((ANY *));
  284.         ANY *arg;
  285.         i = pendingfirst;
  286.         if (i == pendinglast)
  287.             break; /* Queue empty */
  288.         func = pendingcalls[i].func;
  289.         arg = pendingcalls[i].arg;
  290.         pendingfirst = (i + 1) % NPENDINGCALLS;
  291.         if (func(arg) < 0) {
  292.             busy = 0;
  293.             things_to_do = 1; /* We're not done yet */
  294.             return -1;
  295.         }
  296.     }
  297.     busy = 0;
  298.     return 0;
  299. }
  300.  
  301.  
  302. /* Status code for main loop (reason for stack unwind) */
  303.  
  304. enum why_code {
  305.         WHY_NOT,    /* No error */
  306.         WHY_EXCEPTION,    /* Exception occurred */
  307.         WHY_RERAISE,    /* Exception re-raised by 'finally' */
  308.         WHY_RETURN,    /* 'return' statement */
  309.         WHY_BREAK    /* 'break' statement */
  310. };
  311.  
  312. static enum why_code do_raise Py_PROTO((PyObject *, PyObject *, PyObject *));
  313. static int unpack_sequence Py_PROTO((PyObject *, int, PyObject **));
  314.  
  315.  
  316. /* Backward compatible interface */
  317.  
  318. PyObject *
  319. PyEval_EvalCode(co, globals, locals)
  320.     PyCodeObject *co;
  321.     PyObject *globals;
  322.     PyObject *locals;
  323. {
  324.     return eval_code2(co,
  325.               globals, locals,
  326.               (PyObject **)NULL, 0,
  327.               (PyObject **)NULL, 0,
  328.               (PyObject **)NULL, 0,
  329.               (PyObject *)NULL);
  330. }
  331.  
  332.  
  333. /* Interpreter main loop */
  334.  
  335. #ifndef MAX_RECURSION_DEPTH
  336. #define MAX_RECURSION_DEPTH 10000
  337. #endif
  338.  
  339. static PyObject *
  340. eval_code2(co, globals, locals,
  341.        args, argcount, kws, kwcount, defs, defcount, owner)
  342.     PyCodeObject *co;
  343.     PyObject *globals;
  344.     PyObject *locals;
  345.     PyObject **args;
  346.     int argcount;
  347.     PyObject **kws; /* length: 2*kwcount */
  348.     int kwcount;
  349.     PyObject **defs;
  350.     int defcount;
  351.     PyObject *owner;
  352. {
  353. #ifdef DXPAIRS
  354.     int lastopcode = 0;
  355. #endif
  356.     register unsigned char *next_instr;
  357.     register int opcode;    /* Current opcode */
  358.     register int oparg;    /* Current opcode argument, if any */
  359.     register PyObject **stack_pointer;
  360.     register enum why_code why; /* Reason for block stack unwind */
  361.     register int err;    /* Error status -- nonzero if error */
  362.     register PyObject *x;    /* Result object -- NULL if error */
  363.     register PyObject *v;    /* Temporary objects popped off stack */
  364.     register PyObject *w;
  365.     register PyObject *u;
  366.     register PyObject *t;
  367.     register PyFrameObject *f; /* Current frame */
  368.     register PyObject **fastlocals;
  369.     PyObject *retval = NULL;    /* Return value */
  370.     PyThreadState *tstate = PyThreadState_GET();
  371.     unsigned char *first_instr;
  372. #ifdef LLTRACE
  373.     int lltrace;
  374. #endif
  375. #if defined(Py_DEBUG) || defined(LLTRACE)
  376.     /* Make it easier to find out where we are with a debugger */
  377.     char *filename = PyString_AsString(co->co_filename);
  378. #endif
  379.  
  380. /* Code access macros */
  381.  
  382. #define GETCONST(i)    Getconst(f, i)
  383. #define GETNAME(i)    Getname(f, i)
  384. #define GETNAMEV(i)    Getnamev(f, i)
  385. #define INSTR_OFFSET()    (next_instr - first_instr)
  386. #define NEXTOP()    (*next_instr++)
  387. #define NEXTARG()    (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
  388. #define JUMPTO(x)    (next_instr = first_instr + (x))
  389. #define JUMPBY(x)    (next_instr += (x))
  390.  
  391. /* Stack manipulation macros */
  392.  
  393. #define STACK_LEVEL()    (stack_pointer - f->f_valuestack)
  394. #define EMPTY()        (STACK_LEVEL() == 0)
  395. #define TOP()        (stack_pointer[-1])
  396. #define BASIC_PUSH(v)    (*stack_pointer++ = (v))
  397. #define BASIC_POP()    (*--stack_pointer)
  398.  
  399. #ifdef LLTRACE
  400. #define PUSH(v)        (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
  401. #define POP()        (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
  402. #else
  403. #define PUSH(v)        BASIC_PUSH(v)
  404. #define POP()        BASIC_POP()
  405. #endif
  406.  
  407. /* Local variable macros */
  408.  
  409. #define GETLOCAL(i)    (fastlocals[i])
  410. #define SETLOCAL(i, value)    do { Py_XDECREF(GETLOCAL(i)); \
  411.                      GETLOCAL(i) = value; } while (0)
  412.  
  413. /* Start of code */
  414.  
  415. #ifdef USE_STACKCHECK
  416.     if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
  417.         PyErr_SetString(PyExc_MemoryError, "Stack overflow");
  418.         return NULL;
  419.     }
  420. #endif
  421.  
  422.     if (globals == NULL) {
  423.         PyErr_SetString(PyExc_SystemError, "eval_code2: NULL globals");
  424.         return NULL;
  425.     }
  426.  
  427. #ifdef LLTRACE
  428.     lltrace = PyDict_GetItemString(globals, "__lltrace__") != NULL;
  429. #endif
  430.  
  431.     f = PyFrame_New(
  432.             tstate,            /*back*/
  433.             co,            /*code*/
  434.             globals,        /*globals*/
  435.             locals);        /*locals*/
  436.     if (f == NULL)
  437.         return NULL;
  438.  
  439.     tstate->frame = f;
  440.     fastlocals = f->f_localsplus;
  441.  
  442.     if (co->co_argcount > 0 ||
  443.         co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
  444.         int i;
  445.         int n = argcount;
  446.         PyObject *kwdict = NULL;
  447.         if (co->co_flags & CO_VARKEYWORDS) {
  448.             kwdict = PyDict_New();
  449.             if (kwdict == NULL)
  450.                 goto fail;
  451.             i = co->co_argcount;
  452.             if (co->co_flags & CO_VARARGS)
  453.                 i++;
  454.             SETLOCAL(i, kwdict);
  455.         }
  456.         if (argcount > co->co_argcount) {
  457.             if (!(co->co_flags & CO_VARARGS)) {
  458.                 PyErr_Format(PyExc_TypeError,
  459.                 "too many arguments; expected %d, got %d",
  460.                          co->co_argcount, argcount);
  461.                 goto fail;
  462.             }
  463.             n = co->co_argcount;
  464.         }
  465.         for (i = 0; i < n; i++) {
  466.             x = args[i];
  467.             Py_INCREF(x);
  468.             SETLOCAL(i, x);
  469.         }
  470.         if (co->co_flags & CO_VARARGS) {
  471.             u = PyTuple_New(argcount - n);
  472.             if (u == NULL)
  473.                 goto fail;
  474.             SETLOCAL(co->co_argcount, u);
  475.             for (i = n; i < argcount; i++) {
  476.                 x = args[i];
  477.                 Py_INCREF(x);
  478.                 PyTuple_SET_ITEM(u, i-n, x);
  479.             }
  480.         }
  481.         for (i = 0; i < kwcount; i++) {
  482.             PyObject *keyword = kws[2*i];
  483.             PyObject *value = kws[2*i + 1];
  484.             int j;
  485.             /* XXX slow -- speed up using dictionary? */
  486.             for (j = 0; j < co->co_argcount; j++) {
  487.                 PyObject *nm = PyTuple_GET_ITEM(
  488.                     co->co_varnames, j);
  489.                 if (PyObject_Compare(keyword, nm) == 0)
  490.                     break;
  491.             }
  492.             /* Check errors from Compare */
  493.             if (PyErr_Occurred())
  494.                 goto fail;
  495.             if (j >= co->co_argcount) {
  496.                 if (kwdict == NULL) {
  497.                     PyErr_Format(PyExc_TypeError,
  498.                      "unexpected keyword argument: %.400s",
  499.                      PyString_AsString(keyword));
  500.                     goto fail;
  501.                 }
  502.                 PyDict_SetItem(kwdict, keyword, value);
  503.             }
  504.             else {
  505.                 if (GETLOCAL(j) != NULL) {
  506.                     PyErr_SetString(PyExc_TypeError,
  507.                         "keyword parameter redefined");
  508.                     goto fail;
  509.                 }
  510.                 Py_INCREF(value);
  511.                 SETLOCAL(j, value);
  512.             }
  513.         }
  514.         if (argcount < co->co_argcount) {
  515.             int m = co->co_argcount - defcount;
  516.             for (i = argcount; i < m; i++) {
  517.                 if (GETLOCAL(i) == NULL) {
  518.                     PyErr_Format(PyExc_TypeError,
  519.                 "not enough arguments; expected %d, got %d",
  520.                              m, i);
  521.                     goto fail;
  522.                 }
  523.             }
  524.             if (n > m)
  525.                 i = n - m;
  526.             else
  527.                 i = 0;
  528.             for (; i < defcount; i++) {
  529.                 if (GETLOCAL(m+i) == NULL) {
  530.                     PyObject *def = defs[i];
  531.                     Py_INCREF(def);
  532.                     SETLOCAL(m+i, def);
  533.                 }
  534.             }
  535.         }
  536.     }
  537.     else {
  538.         if (argcount > 0 || kwcount > 0) {
  539.             PyErr_SetString(PyExc_TypeError,
  540.                     "no arguments expected");
  541.             goto fail;
  542.         }
  543.     }
  544.  
  545.     if (tstate->sys_tracefunc != NULL) {
  546.         /* tstate->sys_tracefunc, if defined, is a function that
  547.            will be called  on *every* entry to a code block.
  548.            Its return value, if not None, is a function that
  549.            will be called at the start of each executed line
  550.            of code.  (Actually, the function must return
  551.            itself in order to continue tracing.)
  552.            The trace functions are called with three arguments:
  553.            a pointer to the current frame, a string indicating
  554.            why the function is called, and an argument which
  555.            depends on the situation.  The global trace function
  556.            (sys.trace) is also called whenever an exception
  557.            is detected. */
  558.         if (call_trace(&tstate->sys_tracefunc,
  559.                    &f->f_trace, f, "call",
  560.                    Py_None/*XXX how to compute arguments now?*/)) {
  561.             /* Trace function raised an error */
  562.             goto fail;
  563.         }
  564.     }
  565.  
  566.     if (tstate->sys_profilefunc != NULL) {
  567.         /* Similar for sys_profilefunc, except it needn't return
  568.            itself and isn't called for "line" events */
  569.         if (call_trace(&tstate->sys_profilefunc,
  570.                    (PyObject**)0, f, "call",
  571.                    Py_None/*XXX*/)) {
  572.             goto fail;
  573.         }
  574.     }
  575.  
  576.     if (++tstate->recursion_depth > MAX_RECURSION_DEPTH) {
  577.         --tstate->recursion_depth;
  578.         PyErr_SetString(PyExc_RuntimeError,
  579.                 "Maximum recursion depth exceeded");
  580.         tstate->frame = f->f_back;
  581.         Py_DECREF(f);
  582.         return NULL;
  583.     }
  584.  
  585.     _PyCode_GETCODEPTR(co, &first_instr);
  586.     next_instr = first_instr;
  587.     stack_pointer = f->f_valuestack;
  588.     
  589.     why = WHY_NOT;
  590.     err = 0;
  591.     x = Py_None;    /* Not a reference, just anything non-NULL */
  592.     
  593.     for (;;) {
  594.         /* Do periodic things.  Doing this every time through
  595.            the loop would add too much overhead, so we do it
  596.            only every Nth instruction.  We also do it if
  597.            ``things_to_do'' is set, i.e. when an asynchronous
  598.            event needs attention (e.g. a signal handler or
  599.            async I/O handler); see Py_AddPendingCall() and
  600.            Py_MakePendingCalls() above. */
  601.         
  602.         if (things_to_do || --tstate->ticker < 0) {
  603.             tstate->ticker = tstate->interp->checkinterval;
  604.             if (things_to_do) {
  605.                 if (Py_MakePendingCalls() < 0) {
  606.                     why = WHY_EXCEPTION;
  607.                     goto on_error;
  608.                 }
  609.             }
  610. #if !defined(HAVE_SIGNAL_H) || defined(macintosh)
  611.             /* If we have true signals, the signal handler
  612.                will call Py_AddPendingCall() so we don't
  613.                have to call sigcheck().  On the Mac and
  614.                DOS, alas, we have to call it. */
  615.             if (PyErr_CheckSignals()) {
  616.                 why = WHY_EXCEPTION;
  617.                 goto on_error;
  618.             }
  619. #endif
  620.  
  621. #ifdef WITH_THREAD
  622.             if (interpreter_lock) {
  623.                 /* Give another thread a chance */
  624.  
  625.                 if (PyThreadState_Swap(NULL) != tstate)
  626.                     Py_FatalError("ceval: tstate mix-up");
  627.                 PyThread_release_lock(interpreter_lock);
  628.  
  629.                 /* Other threads may run now */
  630.  
  631.                 PyThread_acquire_lock(interpreter_lock, 1);
  632.                 if (PyThreadState_Swap(tstate) != NULL)
  633.                     Py_FatalError("ceval: orphan tstate");
  634.             }
  635. #endif
  636.         }
  637.  
  638.         /* Extract opcode and argument */
  639.  
  640. #if defined(Py_DEBUG) || defined(LLTRACE)
  641.         f->f_lasti = INSTR_OFFSET();
  642. #endif
  643.         
  644.         opcode = NEXTOP();
  645.         if (HAS_ARG(opcode))
  646.             oparg = NEXTARG();
  647. #ifdef DYNAMIC_EXECUTION_PROFILE
  648. #ifdef DXPAIRS
  649.         dxpairs[lastopcode][opcode]++;
  650.         lastopcode = opcode;
  651. #endif
  652.         dxp[opcode]++;
  653. #endif
  654.  
  655. #ifdef LLTRACE
  656.         /* Instruction tracing */
  657.         
  658.         if (lltrace) {
  659.             if (HAS_ARG(opcode)) {
  660.                 printf("%d: %d, %d\n",
  661.                     (int) (INSTR_OFFSET() - 3),
  662.                     opcode, oparg);
  663.             }
  664.             else {
  665.                 printf("%d: %d\n",
  666.                     (int) (INSTR_OFFSET() - 1), opcode);
  667.             }
  668.         }
  669. #endif
  670.  
  671.         /* Main switch on opcode */
  672.         
  673.         switch (opcode) {
  674.         
  675.         /* BEWARE!
  676.            It is essential that any operation that fails sets either
  677.            x to NULL, err to nonzero, or why to anything but WHY_NOT,
  678.            and that no operation that succeeds does this! */
  679.         
  680.         /* case STOP_CODE: this is an error! */
  681.         
  682.         case POP_TOP:
  683.             v = POP();
  684.             Py_DECREF(v);
  685.             continue;
  686.         
  687.         case ROT_TWO:
  688.             v = POP();
  689.             w = POP();
  690.             PUSH(v);
  691.             PUSH(w);
  692.             continue;
  693.         
  694.         case ROT_THREE:
  695.             v = POP();
  696.             w = POP();
  697.             x = POP();
  698.             PUSH(v);
  699.             PUSH(x);
  700.             PUSH(w);
  701.             continue;
  702.         
  703.         case DUP_TOP:
  704.             v = TOP();
  705.             Py_INCREF(v);
  706.             PUSH(v);
  707.             continue;
  708.         
  709.         case UNARY_POSITIVE:
  710.             v = POP();
  711.             x = PyNumber_Positive(v);
  712.             Py_DECREF(v);
  713.             PUSH(x);
  714.             if (x != NULL) continue;
  715.             break;
  716.         
  717.         case UNARY_NEGATIVE:
  718.             v = POP();
  719.             x = PyNumber_Negative(v);
  720.             Py_DECREF(v);
  721.             PUSH(x);
  722.             if (x != NULL) continue;
  723.             break;
  724.         
  725.         case UNARY_NOT:
  726.             v = POP();
  727.             err = PyObject_IsTrue(v);
  728.             Py_DECREF(v);
  729.             if (err == 0) {
  730.                 Py_INCREF(Py_True);
  731.                 PUSH(Py_True);
  732.                 continue;
  733.             }
  734.             else if (err > 0) {
  735.                 Py_INCREF(Py_False);
  736.                 PUSH(Py_False);
  737.                 err = 0;
  738.                 continue;
  739.             }
  740.             break;
  741.         
  742.         case UNARY_CONVERT:
  743.             v = POP();
  744.             x = PyObject_Repr(v);
  745.             Py_DECREF(v);
  746.             PUSH(x);
  747.             if (x != NULL) continue;
  748.             break;
  749.             
  750.         case UNARY_INVERT:
  751.             v = POP();
  752.             x = PyNumber_Invert(v);
  753.             Py_DECREF(v);
  754.             PUSH(x);
  755.             if (x != NULL) continue;
  756.             break;
  757.         
  758.         case BINARY_POWER:
  759.             w = POP();
  760.             v = POP();
  761.             x = PyNumber_Power(v, w, Py_None);
  762.             Py_DECREF(v);
  763.             Py_DECREF(w);
  764.             PUSH(x);
  765.             if (x != NULL) continue;
  766.             break;
  767.         
  768.         case BINARY_MULTIPLY:
  769.             w = POP();
  770.             v = POP();
  771.             x = PyNumber_Multiply(v, w);
  772.             Py_DECREF(v);
  773.             Py_DECREF(w);
  774.             PUSH(x);
  775.             if (x != NULL) continue;
  776.             break;
  777.         
  778.         case BINARY_DIVIDE:
  779.             w = POP();
  780.             v = POP();
  781.             x = PyNumber_Divide(v, w);
  782.             Py_DECREF(v);
  783.             Py_DECREF(w);
  784.             PUSH(x);
  785.             if (x != NULL) continue;
  786.             break;
  787.         
  788.         case BINARY_MODULO:
  789.             w = POP();
  790.             v = POP();
  791.             x = PyNumber_Remainder(v, w);
  792.             Py_DECREF(v);
  793.             Py_DECREF(w);
  794.             PUSH(x);
  795.             if (x != NULL) continue;
  796.             break;
  797.         
  798.         case BINARY_ADD:
  799.             w = POP();
  800.             v = POP();
  801.             if (PyInt_Check(v) && PyInt_Check(w)) {
  802.                 /* INLINE: int + int */
  803.                 register long a, b, i;
  804.                 a = PyInt_AS_LONG(v);
  805.                 b = PyInt_AS_LONG(w);
  806.                 i = a + b;
  807.                 if ((i^a) < 0 && (i^b) < 0) {
  808.                     PyErr_SetString(PyExc_OverflowError,
  809.                             "integer addition");
  810.                     x = NULL;
  811.                 }
  812.                 else
  813.                     x = PyInt_FromLong(i);
  814.             }
  815.             else
  816.                 x = PyNumber_Add(v, w);
  817.             Py_DECREF(v);
  818.             Py_DECREF(w);
  819.             PUSH(x);
  820.             if (x != NULL) continue;
  821.             break;
  822.         
  823.         case BINARY_SUBTRACT:
  824.             w = POP();
  825.             v = POP();
  826.             if (PyInt_Check(v) && PyInt_Check(w)) {
  827.                 /* INLINE: int - int */
  828.                 register long a, b, i;
  829.                 a = PyInt_AS_LONG(v);
  830.                 b = PyInt_AS_LONG(w);
  831.                 i = a - b;
  832.                 if ((i^a) < 0 && (i^~b) < 0) {
  833.                     PyErr_SetString(PyExc_OverflowError,
  834.                             "integer subtraction");
  835.                     x = NULL;
  836.                 }
  837.                 else
  838.                     x = PyInt_FromLong(i);
  839.             }
  840.             else
  841.                 x = PyNumber_Subtract(v, w);
  842.             Py_DECREF(v);
  843.             Py_DECREF(w);
  844.             PUSH(x);
  845.             if (x != NULL) continue;
  846.             break;
  847.         
  848.         case BINARY_SUBSCR:
  849.             w = POP();
  850.             v = POP();
  851.             if (PyList_Check(v) && PyInt_Check(w)) {
  852.                 /* INLINE: list[int] */
  853.                 long i = PyInt_AsLong(w);
  854.                 if (i < 0)
  855.                     i += PyList_GET_SIZE(v);
  856.                 if (i < 0 ||
  857.                     i >= PyList_GET_SIZE(v)) {
  858.                     PyErr_SetString(PyExc_IndexError,
  859.                         "list index out of range");
  860.                     x = NULL;
  861.                 }
  862.                 else {
  863.                     x = PyList_GET_ITEM(v, i);
  864.                     Py_INCREF(x);
  865.                 }
  866.             }
  867.             else
  868.                 x = PyObject_GetItem(v, w);
  869.             Py_DECREF(v);
  870.             Py_DECREF(w);
  871.             PUSH(x);
  872.             if (x != NULL) continue;
  873.             break;
  874.         
  875.         case BINARY_LSHIFT:
  876.             w = POP();
  877.             v = POP();
  878.             x = PyNumber_Lshift(v, w);
  879.             Py_DECREF(v);
  880.             Py_DECREF(w);
  881.             PUSH(x);
  882.             if (x != NULL) continue;
  883.             break;
  884.         
  885.         case BINARY_RSHIFT:
  886.             w = POP();
  887.             v = POP();
  888.             x = PyNumber_Rshift(v, w);
  889.             Py_DECREF(v);
  890.             Py_DECREF(w);
  891.             PUSH(x);
  892.             if (x != NULL) continue;
  893.             break;
  894.         
  895.         case BINARY_AND:
  896.             w = POP();
  897.             v = POP();
  898.             x = PyNumber_And(v, w);
  899.             Py_DECREF(v);
  900.             Py_DECREF(w);
  901.             PUSH(x);
  902.             if (x != NULL) continue;
  903.             break;
  904.         
  905.         case BINARY_XOR:
  906.             w = POP();
  907.             v = POP();
  908.             x = PyNumber_Xor(v, w);
  909.             Py_DECREF(v);
  910.             Py_DECREF(w);
  911.             PUSH(x);
  912.             if (x != NULL) continue;
  913.             break;
  914.         
  915.         case BINARY_OR:
  916.             w = POP();
  917.             v = POP();
  918.             x = PyNumber_Or(v, w);
  919.             Py_DECREF(v);
  920.             Py_DECREF(w);
  921.             PUSH(x);
  922.             if (x != NULL) continue;
  923.             break;
  924.         
  925.         case SLICE+0:
  926.         case SLICE+1:
  927.         case SLICE+2:
  928.         case SLICE+3:
  929.             if ((opcode-SLICE) & 2)
  930.                 w = POP();
  931.             else
  932.                 w = NULL;
  933.             if ((opcode-SLICE) & 1)
  934.                 v = POP();
  935.             else
  936.                 v = NULL;
  937.             u = POP();
  938.             x = apply_slice(u, v, w);
  939.             Py_DECREF(u);
  940.             Py_XDECREF(v);
  941.             Py_XDECREF(w);
  942.             PUSH(x);
  943.             if (x != NULL) continue;
  944.             break;
  945.         
  946.         case STORE_SLICE+0:
  947.         case STORE_SLICE+1:
  948.         case STORE_SLICE+2:
  949.         case STORE_SLICE+3:
  950.             if ((opcode-STORE_SLICE) & 2)
  951.                 w = POP();
  952.             else
  953.                 w = NULL;
  954.             if ((opcode-STORE_SLICE) & 1)
  955.                 v = POP();
  956.             else
  957.                 v = NULL;
  958.             u = POP();
  959.             t = POP();
  960.             err = assign_slice(u, v, w, t); /* u[v:w] = t */
  961.             Py_DECREF(t);
  962.             Py_DECREF(u);
  963.             Py_XDECREF(v);
  964.             Py_XDECREF(w);
  965.             if (err == 0) continue;
  966.             break;
  967.         
  968.         case DELETE_SLICE+0:
  969.         case DELETE_SLICE+1:
  970.         case DELETE_SLICE+2:
  971.         case DELETE_SLICE+3:
  972.             if ((opcode-DELETE_SLICE) & 2)
  973.                 w = POP();
  974.             else
  975.                 w = NULL;
  976.             if ((opcode-DELETE_SLICE) & 1)
  977.                 v = POP();
  978.             else
  979.                 v = NULL;
  980.             u = POP();
  981.             err = assign_slice(u, v, w, (PyObject *)NULL);
  982.                             /* del u[v:w] */
  983.             Py_DECREF(u);
  984.             Py_XDECREF(v);
  985.             Py_XDECREF(w);
  986.             if (err == 0) continue;
  987.             break;
  988.         
  989.         case STORE_SUBSCR:
  990.             w = POP();
  991.             v = POP();
  992.             u = POP();
  993.             /* v[w] = u */
  994.             err = PyObject_SetItem(v, w, u);
  995.             Py_DECREF(u);
  996.             Py_DECREF(v);
  997.             Py_DECREF(w);
  998.             if (err == 0) continue;
  999.             break;
  1000.         
  1001.         case DELETE_SUBSCR:
  1002.             w = POP();
  1003.             v = POP();
  1004.             /* del v[w] */
  1005.             err = PyObject_DelItem(v, w);
  1006.             Py_DECREF(v);
  1007.             Py_DECREF(w);
  1008.             if (err == 0) continue;
  1009.             break;
  1010.         
  1011.         case PRINT_EXPR:
  1012.             v = POP();
  1013.             /* Print value except if None */
  1014.             /* After printing, also assign to '_' */
  1015.             /* Before, set '_' to None to avoid recursion */
  1016.             if (v != Py_None &&
  1017.                 (err = PyDict_SetItemString(
  1018.                     f->f_builtins, "_", Py_None)) == 0) {
  1019.                 err = Py_FlushLine();
  1020.                 if (err == 0) {
  1021.                     x = PySys_GetObject("stdout");
  1022.                     if (x == NULL) {
  1023.                         PyErr_SetString(
  1024.                             PyExc_RuntimeError,
  1025.                             "lost sys.stdout");
  1026.                         err = -1;
  1027.                     }
  1028.                 }
  1029.                 if (err == 0)
  1030.                     err = PyFile_WriteObject(v, x, 0);
  1031.                 if (err == 0) {
  1032.                     PyFile_SoftSpace(x, 1);
  1033.                     err = Py_FlushLine();
  1034.                 }
  1035.                 if (err == 0) {
  1036.                     err = PyDict_SetItemString(
  1037.                         f->f_builtins, "_", v);
  1038.                 }
  1039.             }
  1040.             Py_DECREF(v);
  1041.             break;
  1042.         
  1043.         case PRINT_ITEM:
  1044.             v = POP();
  1045.             w = PySys_GetObject("stdout");
  1046.             if (w == NULL) {
  1047.                 PyErr_SetString(PyExc_RuntimeError,
  1048.                         "lost sys.stdout");
  1049.                 err = -1;
  1050.             }
  1051.             else if (PyFile_SoftSpace(w, 1))
  1052.                 err = PyFile_WriteString(" ", w);
  1053.             if (err == 0)
  1054.                 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
  1055.             if (err == 0 && PyString_Check(v)) {
  1056.                 /* XXX move into writeobject() ? */
  1057.                 char *s = PyString_AsString(v);
  1058.                 int len = PyString_Size(v);
  1059.                 if (len > 0 &&
  1060.                     isspace(Py_CHARMASK(s[len-1])) &&
  1061.                     s[len-1] != ' ')
  1062.                     PyFile_SoftSpace(w, 0);
  1063.             }
  1064.             Py_DECREF(v);
  1065.             if (err == 0) continue;
  1066.             break;
  1067.         
  1068.         case PRINT_NEWLINE:
  1069.             x = PySys_GetObject("stdout");
  1070.             if (x == NULL)
  1071.                 PyErr_SetString(PyExc_RuntimeError,
  1072.                         "lost sys.stdout");
  1073.             else {
  1074.                 err = PyFile_WriteString("\n", x);
  1075.                 if (err == 0)
  1076.                     PyFile_SoftSpace(x, 0);
  1077.             }
  1078.             break;
  1079.         
  1080.         case BREAK_LOOP:
  1081.             why = WHY_BREAK;
  1082.             break;
  1083.  
  1084.         case RAISE_VARARGS:
  1085.             u = v = w = NULL;
  1086.             switch (oparg) {
  1087.             case 3:
  1088.                 u = POP(); /* traceback */
  1089.                 /* Fallthrough */
  1090.             case 2:
  1091.                 v = POP(); /* value */
  1092.                 /* Fallthrough */
  1093.             case 1:
  1094.                 w = POP(); /* exc */
  1095.             case 0: /* Fallthrough */
  1096.                 why = do_raise(w, v, u);
  1097.                 break;
  1098.             default:
  1099.                 PyErr_SetString(PyExc_SystemError,
  1100.                        "bad RAISE_VARARGS oparg");
  1101.                 why = WHY_EXCEPTION;
  1102.                 break;
  1103.             }
  1104.             break;
  1105.         
  1106.         case LOAD_LOCALS:
  1107.             if ((x = f->f_locals) == NULL) {
  1108.                 PyErr_SetString(PyExc_SystemError,
  1109.                         "no locals");
  1110.                 break;
  1111.             }
  1112.             Py_INCREF(x);
  1113.             PUSH(x);
  1114.             break;
  1115.         
  1116.         case RETURN_VALUE:
  1117.             retval = POP();
  1118.             why = WHY_RETURN;
  1119.             break;
  1120.  
  1121.         case EXEC_STMT:
  1122.             w = POP();
  1123.             v = POP();
  1124.             u = POP();
  1125.             err = exec_statement(f, u, v, w);
  1126.             Py_DECREF(u);
  1127.             Py_DECREF(v);
  1128.             Py_DECREF(w);
  1129.             break;
  1130.         
  1131.         case POP_BLOCK:
  1132.             {
  1133.                 PyTryBlock *b = PyFrame_BlockPop(f);
  1134.                 while (STACK_LEVEL() > b->b_level) {
  1135.                     v = POP();
  1136.                     Py_DECREF(v);
  1137.                 }
  1138.             }
  1139.             break;
  1140.         
  1141.         case END_FINALLY:
  1142.             v = POP();
  1143.             if (PyInt_Check(v)) {
  1144.                 why = (enum why_code) PyInt_AsLong(v);
  1145.                 if (why == WHY_RETURN)
  1146.                     retval = POP();
  1147.             }
  1148.             else if (PyString_Check(v) || PyClass_Check(v)) {
  1149.                 w = POP();
  1150.                 u = POP();
  1151.                 PyErr_Restore(v, w, u);
  1152.                 why = WHY_RERAISE;
  1153.                 break;
  1154.             }
  1155.             else if (v != Py_None) {
  1156.                 PyErr_SetString(PyExc_SystemError,
  1157.                     "'finally' pops bad exception");
  1158.                 why = WHY_EXCEPTION;
  1159.             }
  1160.             Py_DECREF(v);
  1161.             break;
  1162.         
  1163.         case BUILD_CLASS:
  1164.             u = POP();
  1165.             v = POP();
  1166.             w = POP();
  1167.             x = build_class(u, v, w);
  1168.             PUSH(x);
  1169.             Py_DECREF(u);
  1170.             Py_DECREF(v);
  1171.             Py_DECREF(w);
  1172.             break;
  1173.         
  1174.         case STORE_NAME:
  1175.             w = GETNAMEV(oparg);
  1176.             v = POP();
  1177.             if ((x = f->f_locals) == NULL) {
  1178.                 PyErr_SetString(PyExc_SystemError,
  1179.                         "no locals");
  1180.                 break;
  1181.             }
  1182.             err = PyDict_SetItem(x, w, v);
  1183.             Py_DECREF(v);
  1184.             break;
  1185.         
  1186.         case DELETE_NAME:
  1187.             w = GETNAMEV(oparg);
  1188.             if ((x = f->f_locals) == NULL) {
  1189.                 PyErr_SetString(PyExc_SystemError,
  1190.                         "no locals");
  1191.                 break;
  1192.             }
  1193.             if ((err = PyDict_DelItem(x, w)) != 0)
  1194.                 PyErr_SetObject(PyExc_NameError, w);
  1195.             break;
  1196.  
  1197. #ifdef CASE_TOO_BIG
  1198.         default: switch (opcode) {
  1199. #endif
  1200.         
  1201.         case UNPACK_TUPLE:
  1202.         case UNPACK_LIST:
  1203.             v = POP();
  1204.             if (PyTuple_Check(v)) {
  1205.                 if (PyTuple_Size(v) != oparg) {
  1206.                     PyErr_SetString(PyExc_ValueError,
  1207.                          "unpack tuple of wrong size");
  1208.                     why = WHY_EXCEPTION;
  1209.                 }
  1210.                 else {
  1211.                     for (; --oparg >= 0; ) {
  1212.                         w = PyTuple_GET_ITEM(v, oparg);
  1213.                         Py_INCREF(w);
  1214.                         PUSH(w);
  1215.                     }
  1216.                 }
  1217.             }
  1218.             else if (PyList_Check(v)) {
  1219.                 if (PyList_Size(v) != oparg) {
  1220.                     PyErr_SetString(PyExc_ValueError,
  1221.                           "unpack list of wrong size");
  1222.                     why = WHY_EXCEPTION;
  1223.                 }
  1224.                 else {
  1225.                     for (; --oparg >= 0; ) {
  1226.                         w = PyList_GET_ITEM(v, oparg);
  1227.                         Py_INCREF(w);
  1228.                         PUSH(w);
  1229.                     }
  1230.                 }
  1231.             }
  1232.             else if (PySequence_Check(v)) {
  1233.                 if (unpack_sequence(v, oparg,
  1234.                             stack_pointer + oparg))
  1235.                     stack_pointer += oparg;
  1236.                 else
  1237.                     why = WHY_EXCEPTION;
  1238.             }
  1239.             else {
  1240.                 PyErr_SetString(PyExc_TypeError,
  1241.                         "unpack non-sequence");
  1242.                 why = WHY_EXCEPTION;
  1243.             }
  1244.             Py_DECREF(v);
  1245.             break;
  1246.         
  1247.         case STORE_ATTR:
  1248.             w = GETNAMEV(oparg);
  1249.             v = POP();
  1250.             u = POP();
  1251.             err = PyObject_SetAttr(v, w, u); /* v.w = u */
  1252.             Py_DECREF(v);
  1253.             Py_DECREF(u);
  1254.             break;
  1255.         
  1256.         case DELETE_ATTR:
  1257.             w = GETNAMEV(oparg);
  1258.             v = POP();
  1259.             err = PyObject_SetAttr(v, w, (PyObject *)NULL);
  1260.                             /* del v.w */
  1261.             Py_DECREF(v);
  1262.             break;
  1263.         
  1264.         case STORE_GLOBAL:
  1265.             w = GETNAMEV(oparg);
  1266.             v = POP();
  1267.             err = PyDict_SetItem(f->f_globals, w, v);
  1268.             Py_DECREF(v);
  1269.             break;
  1270.         
  1271.         case DELETE_GLOBAL:
  1272.             w = GETNAMEV(oparg);
  1273.             if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
  1274.                 PyErr_SetObject(PyExc_NameError, w);
  1275.             break;
  1276.         
  1277.         case LOAD_CONST:
  1278.             x = GETCONST(oparg);
  1279.             Py_INCREF(x);
  1280.             PUSH(x);
  1281.             break;
  1282.         
  1283.         case LOAD_NAME:
  1284.             w = GETNAMEV(oparg);
  1285.             if ((x = f->f_locals) == NULL) {
  1286.                 PyErr_SetString(PyExc_SystemError,
  1287.                         "no locals");
  1288.                 break;
  1289.             }
  1290.             x = PyDict_GetItem(x, w);
  1291.             if (x == NULL) {
  1292.                 x = PyDict_GetItem(f->f_globals, w);
  1293.                 if (x == NULL) {
  1294.                     x = PyDict_GetItem(f->f_builtins, w);
  1295.                     if (x == NULL) {
  1296.                         PyErr_SetObject(
  1297.                             PyExc_NameError, w);
  1298.                         break;
  1299.                     }
  1300.                 }
  1301.             }
  1302.             Py_INCREF(x);
  1303.             PUSH(x);
  1304.             break;
  1305.         
  1306.         case LOAD_GLOBAL:
  1307.             w = GETNAMEV(oparg);
  1308.             x = PyDict_GetItem(f->f_globals, w);
  1309.             if (x == NULL) {
  1310.                 x = PyDict_GetItem(f->f_builtins, w);
  1311.                 if (x == NULL) {
  1312.                     PyErr_SetObject(PyExc_NameError, w);
  1313.                     break;
  1314.                 }
  1315.             }
  1316.             Py_INCREF(x);
  1317.             PUSH(x);
  1318.             break;
  1319.  
  1320.         case LOAD_FAST:
  1321.             x = GETLOCAL(oparg);
  1322.             if (x == NULL) {
  1323.                 PyErr_SetObject(PyExc_NameError,
  1324.                        PyTuple_GetItem(co->co_varnames,
  1325.                             oparg));
  1326.                 break;
  1327.             }
  1328.             Py_INCREF(x);
  1329.             PUSH(x);
  1330.             if (x != NULL) continue;
  1331.             break;
  1332.  
  1333.         case STORE_FAST:
  1334.             v = POP();
  1335.             SETLOCAL(oparg, v);
  1336.             continue;
  1337.  
  1338.         case DELETE_FAST:
  1339.             x = GETLOCAL(oparg);
  1340.             if (x == NULL) {
  1341.                 PyErr_SetObject(PyExc_NameError,
  1342.                        PyTuple_GetItem(co->co_varnames,
  1343.                             oparg));
  1344.                 break;
  1345.             }
  1346.             SETLOCAL(oparg, NULL);
  1347.             continue;
  1348.         
  1349.         case BUILD_TUPLE:
  1350.             x = PyTuple_New(oparg);
  1351.             if (x != NULL) {
  1352.                 for (; --oparg >= 0;) {
  1353.                     w = POP();
  1354.                     PyTuple_SET_ITEM(x, oparg, w);
  1355.                 }
  1356.                 PUSH(x);
  1357.                 continue;
  1358.             }
  1359.             break;
  1360.         
  1361.         case BUILD_LIST:
  1362.             x =  PyList_New(oparg);
  1363.             if (x != NULL) {
  1364.                 for (; --oparg >= 0;) {
  1365.                     w = POP();
  1366.                     PyList_SET_ITEM(x, oparg, w);
  1367.                 }
  1368.                 PUSH(x);
  1369.                 continue;
  1370.             }
  1371.             break;
  1372.         
  1373.         case BUILD_MAP:
  1374.             x = PyDict_New();
  1375.             PUSH(x);
  1376.             if (x != NULL) continue;
  1377.             break;
  1378.         
  1379.         case LOAD_ATTR:
  1380.             w = GETNAMEV(oparg);
  1381.             v = POP();
  1382.             x = PyObject_GetAttr(v, w);
  1383.             Py_DECREF(v);
  1384.             PUSH(x);
  1385.             if (x != NULL) continue;
  1386.             break;
  1387.         
  1388.         case COMPARE_OP:
  1389.             w = POP();
  1390.             v = POP();
  1391.             if (PyInt_Check(v) && PyInt_Check(w)) {
  1392.                 /* INLINE: cmp(int, int) */
  1393.                 register long a, b;
  1394.                 register int res;
  1395.                 a = PyInt_AS_LONG(v);
  1396.                 b = PyInt_AS_LONG(w);
  1397.                 switch (oparg) {
  1398.                 case LT: res = a <  b; break;
  1399.                 case LE: res = a <= b; break;
  1400.                 case EQ: res = a == b; break;
  1401.                 case NE: res = a != b; break;
  1402.                 case GT: res = a >  b; break;
  1403.                 case GE: res = a >= b; break;
  1404.                 case IS: res = v == w; break;
  1405.                 case IS_NOT: res = v != w; break;
  1406.                 default: goto slow_compare;
  1407.                 }
  1408.                 x = res ? Py_True : Py_False;
  1409.                 Py_INCREF(x);
  1410.             }
  1411.             else {
  1412.               slow_compare:
  1413.                 x = cmp_outcome(oparg, v, w);
  1414.             }
  1415.             Py_DECREF(v);
  1416.             Py_DECREF(w);
  1417.             PUSH(x);
  1418.             if (x != NULL) continue;
  1419.             break;
  1420.         
  1421.         case IMPORT_NAME:
  1422.             w = GETNAMEV(oparg);
  1423.             x = PyDict_GetItemString(f->f_builtins, "__import__");
  1424.             if (x == NULL) {
  1425.                 PyErr_SetString(PyExc_ImportError,
  1426.                         "__import__ not found");
  1427.                 break;
  1428.             }
  1429.             u = find_from_args(f, INSTR_OFFSET());
  1430.             if (u == NULL) {
  1431.                 x = u;
  1432.                 break;
  1433.             }
  1434.             w = Py_BuildValue("(OOOO)",
  1435.                     w,
  1436.                     f->f_globals,
  1437.                     f->f_locals == NULL ?
  1438.                       Py_None : f->f_locals,
  1439.                     u);
  1440.             Py_DECREF(u);
  1441.             if (w == NULL) {
  1442.                 x = NULL;
  1443.                 break;
  1444.             }
  1445.             x = PyEval_CallObject(x, w);
  1446.             Py_DECREF(w);
  1447.             PUSH(x);
  1448.             if (x != NULL) continue;
  1449.             break;
  1450.         
  1451.         case IMPORT_FROM:
  1452.             w = GETNAMEV(oparg);
  1453.             v = TOP();
  1454.             PyFrame_FastToLocals(f);
  1455.             if ((x = f->f_locals) == NULL) {
  1456.                 PyErr_SetString(PyExc_SystemError,
  1457.                         "no locals");
  1458.                 break;
  1459.             }
  1460.             err = import_from(x, v, w);
  1461.             PyFrame_LocalsToFast(f, 0);
  1462.             if (err == 0) continue;
  1463.             break;
  1464.  
  1465.         case JUMP_FORWARD:
  1466.             JUMPBY(oparg);
  1467.             continue;
  1468.         
  1469.         case JUMP_IF_FALSE:
  1470.             err = PyObject_IsTrue(TOP());
  1471.             if (err > 0)
  1472.                 err = 0;
  1473.             else if (err == 0)
  1474.                 JUMPBY(oparg);
  1475.             else
  1476.                 break;
  1477.             continue;
  1478.         
  1479.         case JUMP_IF_TRUE:
  1480.             err = PyObject_IsTrue(TOP());
  1481.             if (err > 0) {
  1482.                 err = 0;
  1483.                 JUMPBY(oparg);
  1484.             }
  1485.             else if (err == 0)
  1486.                 ;
  1487.             else
  1488.                 break;
  1489.             continue;
  1490.         
  1491.         case JUMP_ABSOLUTE:
  1492.             JUMPTO(oparg);
  1493.             continue;
  1494.         
  1495.         case FOR_LOOP:
  1496.             /* for v in s: ...
  1497.                On entry: stack contains s, i.
  1498.                On exit: stack contains s, i+1, s[i];
  1499.                but if loop exhausted:
  1500.                    s, i are popped, and we jump */
  1501.             w = POP(); /* Loop index */
  1502.             v = POP(); /* Sequence object */
  1503.             u = loop_subscript(v, w);
  1504.             if (u != NULL) {
  1505.                 PUSH(v);
  1506.                 x = PyInt_FromLong(PyInt_AsLong(w)+1);
  1507.                 PUSH(x);
  1508.                 Py_DECREF(w);
  1509.                 PUSH(u);
  1510.                 if (x != NULL) continue;
  1511.             }
  1512.             else {
  1513.                 Py_DECREF(v);
  1514.                 Py_DECREF(w);
  1515.                 /* A NULL can mean "s exhausted"
  1516.                    but also an error: */
  1517.                 if (PyErr_Occurred())
  1518.                     why = WHY_EXCEPTION;
  1519.                 else {
  1520.                     JUMPBY(oparg);
  1521.                     continue;
  1522.                 }
  1523.             }
  1524.             break;
  1525.         
  1526.         case SETUP_LOOP:
  1527.         case SETUP_EXCEPT:
  1528.         case SETUP_FINALLY:
  1529.             PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
  1530.                         STACK_LEVEL());
  1531.             continue;
  1532.         
  1533.         case SET_LINENO:
  1534. #ifdef LLTRACE
  1535.             if (lltrace)
  1536.                 printf("--- %s:%d \n", filename, oparg);
  1537. #endif
  1538.             f->f_lineno = oparg;
  1539.             if (f->f_trace == NULL)
  1540.                 continue;
  1541.             /* Trace each line of code reached */
  1542.             f->f_lasti = INSTR_OFFSET();
  1543.             err = call_trace(&f->f_trace, &f->f_trace,
  1544.                      f, "line", Py_None);
  1545.             break;
  1546.  
  1547.         case CALL_FUNCTION:
  1548.         {
  1549.             int na = oparg & 0xff;
  1550.             int nk = (oparg>>8) & 0xff;
  1551.             int n = na + 2*nk;
  1552.             PyObject **pfunc = stack_pointer - n - 1;
  1553.             PyObject *func = *pfunc;
  1554.             PyObject *self = NULL;
  1555.             PyObject *class = NULL;
  1556.             f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
  1557.             if (PyMethod_Check(func)) {
  1558.                 self = PyMethod_Self(func);
  1559.                 class = PyMethod_Class(func);
  1560.                 func = PyMethod_Function(func);
  1561.                 Py_INCREF(func);
  1562.                 if (self != NULL) {
  1563.                     Py_INCREF(self);
  1564.                     Py_DECREF(*pfunc);
  1565.                     *pfunc = self;
  1566.                     na++;
  1567.                     n++;
  1568.                 }
  1569.                 else {
  1570.                     /* Unbound methods must be
  1571.                        called with an instance of
  1572.                        the class (or a derived
  1573.                        class) as first argument */
  1574.                     if (na > 0 &&
  1575.                         (self = stack_pointer[-n])
  1576.                          != NULL &&
  1577.                         PyInstance_Check(self) &&
  1578.                         PyClass_IsSubclass(
  1579.                             (PyObject *)
  1580.                             (((PyInstanceObject *)self)
  1581.                              ->in_class),
  1582.                             class))
  1583.                         /* Handy-dandy */ ;
  1584.                     else {
  1585.                         PyErr_SetString(
  1586.                             PyExc_TypeError,
  1587.        "unbound method must be called with class instance 1st argument");
  1588.                         x = NULL;
  1589.                         break;
  1590.                     }
  1591.                 }
  1592.             }
  1593.             else
  1594.                 Py_INCREF(func);
  1595.             if (PyFunction_Check(func)) {
  1596.                 PyObject *co = PyFunction_GetCode(func);
  1597.                 PyObject *globals =
  1598.                     PyFunction_GetGlobals(func);
  1599.                 PyObject *argdefs =
  1600.                     PyFunction_GetDefaults(func);
  1601.                 PyObject **d;
  1602.                 int nd;
  1603.                 if (argdefs != NULL) {
  1604.                     d = &PyTuple_GET_ITEM(argdefs, 0);
  1605.                     nd = ((PyTupleObject *)argdefs) ->
  1606.                         ob_size;
  1607.                 }
  1608.                 else {
  1609.                     d = NULL;
  1610.                     nd = 0;
  1611.                 }
  1612.                 x = eval_code2(
  1613.                     (PyCodeObject *)co,
  1614.                     globals, (PyObject *)NULL,
  1615.                     stack_pointer-n, na,
  1616.                     stack_pointer-2*nk, nk,
  1617.                     d, nd,
  1618.                     class);
  1619.             }
  1620.             else {
  1621.                 PyObject *args = PyTuple_New(na);
  1622.                 PyObject *kwdict = NULL;
  1623.                 if (args == NULL) {
  1624.                     x = NULL;
  1625.                     break;
  1626.                 }
  1627.                 if (nk > 0) {
  1628.                     kwdict = PyDict_New();
  1629.                     if (kwdict == NULL) {
  1630.                         x = NULL;
  1631.                         break;
  1632.                     }
  1633.                     err = 0;
  1634.                     while (--nk >= 0) {
  1635.                         PyObject *value = POP();
  1636.                         PyObject *key = POP();
  1637.                         err = PyDict_SetItem(
  1638.                             kwdict, key, value);
  1639.                         Py_DECREF(key);
  1640.                         Py_DECREF(value);
  1641.                         if (err)
  1642.                             break;
  1643.                     }
  1644.                     if (err) {
  1645.                         Py_DECREF(args);
  1646.                         Py_DECREF(kwdict);
  1647.                         break;
  1648.                     }
  1649.                 }
  1650.                 while (--na >= 0) {
  1651.                     w = POP();
  1652.                     PyTuple_SET_ITEM(args, na, w);
  1653.                 }
  1654.                 x = PyEval_CallObjectWithKeywords(
  1655.                     func, args, kwdict);
  1656.                 Py_DECREF(args);
  1657.                 Py_XDECREF(kwdict);
  1658.             }
  1659.             Py_DECREF(func);
  1660.             while (stack_pointer > pfunc) {
  1661.                 w = POP();
  1662.                 Py_DECREF(w);
  1663.             }
  1664.             PUSH(x);
  1665.             if (x != NULL) continue;
  1666.             break;
  1667.         }
  1668.         
  1669.         case MAKE_FUNCTION:
  1670.             v = POP(); /* code object */
  1671.             x = PyFunction_New(v, f->f_globals);
  1672.             Py_DECREF(v);
  1673.             /* XXX Maybe this should be a separate opcode? */
  1674.             if (x != NULL && oparg > 0) {
  1675.                 v = PyTuple_New(oparg);
  1676.                 if (v == NULL) {
  1677.                     Py_DECREF(x);
  1678.                     x = NULL;
  1679.                     break;
  1680.                 }
  1681.                 while (--oparg >= 0) {
  1682.                     w = POP();
  1683.                     PyTuple_SET_ITEM(v, oparg, w);
  1684.                 }
  1685.                 err = PyFunction_SetDefaults(x, v);
  1686.                 Py_DECREF(v);
  1687.             }
  1688.             PUSH(x);
  1689.             break;
  1690.  
  1691.         case BUILD_SLICE:
  1692.             if (oparg == 3)
  1693.                 w = POP();
  1694.             else
  1695.                 w = NULL;
  1696.             v = POP();
  1697.             u = POP();
  1698.             x = PySlice_New(u, v, w);
  1699.             Py_DECREF(u);
  1700.             Py_DECREF(v);
  1701.             Py_XDECREF(w);
  1702.             PUSH(x);
  1703.             if (x != NULL) continue;
  1704.             break;
  1705.  
  1706.  
  1707.         default:
  1708.             fprintf(stderr,
  1709.                 "XXX lineno: %d, opcode: %d\n",
  1710.                 f->f_lineno, opcode);
  1711.             PyErr_SetString(PyExc_SystemError, "unknown opcode");
  1712.             why = WHY_EXCEPTION;
  1713.             break;
  1714.  
  1715. #ifdef CASE_TOO_BIG
  1716.         }
  1717. #endif
  1718.  
  1719.         } /* switch */
  1720.  
  1721.         on_error:
  1722.         
  1723.         /* Quickly continue if no error occurred */
  1724.         
  1725.         if (why == WHY_NOT) {
  1726.             if (err == 0 && x != NULL) {
  1727. #ifdef CHECKEXC
  1728.                 /* This check is expensive! */
  1729.                 if (PyErr_Occurred())
  1730.                     fprintf(stderr,
  1731.                         "XXX undetected error\n");
  1732.                 else
  1733. #endif
  1734.                     continue; /* Normal, fast path */
  1735.             }
  1736.             why = WHY_EXCEPTION;
  1737.             x = Py_None;
  1738.             err = 0;
  1739.         }
  1740.  
  1741.         /* Double-check exception status */
  1742.         
  1743.         if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
  1744.             if (!PyErr_Occurred()) {
  1745.                 PyErr_SetString(PyExc_SystemError,
  1746.                     "error return without exception set");
  1747.                 why = WHY_EXCEPTION;
  1748.             }
  1749.         }
  1750. #ifdef CHECKEXC
  1751.         else {
  1752.             /* This check is expensive! */
  1753.             if (PyErr_Occurred()) {
  1754.                 fprintf(stderr,
  1755.                     "XXX undetected error (why=%d)\n",
  1756.                     why);
  1757.                 why = WHY_EXCEPTION;
  1758.             }
  1759.         }
  1760. #endif
  1761.  
  1762.         /* Log traceback info if this is a real exception */
  1763.         
  1764.         if (why == WHY_EXCEPTION) {
  1765.             f->f_lasti = INSTR_OFFSET() - 1;
  1766.             if (HAS_ARG(opcode))
  1767.                 f->f_lasti -= 2;
  1768.             PyTraceBack_Here(f);
  1769.  
  1770.             if (f->f_trace)
  1771.                 call_exc_trace(&f->f_trace, &f->f_trace, f);
  1772.             if (tstate->sys_profilefunc)
  1773.                 call_exc_trace(&tstate->sys_profilefunc,
  1774.                            (PyObject**)0, f);
  1775.         }
  1776.         
  1777.         /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
  1778.         
  1779.         if (why == WHY_RERAISE)
  1780.             why = WHY_EXCEPTION;
  1781.  
  1782.         /* Unwind stacks if a (pseudo) exception occurred */
  1783.         
  1784.         while (why != WHY_NOT && f->f_iblock > 0) {
  1785.             PyTryBlock *b = PyFrame_BlockPop(f);
  1786.             while (STACK_LEVEL() > b->b_level) {
  1787.                 v = POP();
  1788.                 Py_XDECREF(v);
  1789.             }
  1790.             if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
  1791.                 why = WHY_NOT;
  1792.                 JUMPTO(b->b_handler);
  1793.                 break;
  1794.             }
  1795.             if (b->b_type == SETUP_FINALLY ||
  1796.                 (b->b_type == SETUP_EXCEPT &&
  1797.                  why == WHY_EXCEPTION)) {
  1798.                 if (why == WHY_EXCEPTION) {
  1799.                     PyObject *exc, *val, *tb;
  1800.                     PyErr_Fetch(&exc, &val, &tb);
  1801.                     if (val == NULL) {
  1802.                         val = Py_None;
  1803.                         Py_INCREF(val);
  1804.                     }
  1805.                     /* Make the raw exception data
  1806.                        available to the handler,
  1807.                        so a program can emulate the
  1808.                        Python main loop.  Don't do
  1809.                        this for 'finally'. */
  1810.                     if (b->b_type == SETUP_EXCEPT) {
  1811.                         PyErr_NormalizeException(
  1812.                             &exc, &val, &tb);
  1813.                         set_exc_info(tstate,
  1814.                                  exc, val, tb);
  1815.                     }
  1816.                     PUSH(tb);
  1817.                     PUSH(val);
  1818.                     PUSH(exc);
  1819.                 }
  1820.                 else {
  1821.                     if (why == WHY_RETURN)
  1822.                         PUSH(retval);
  1823.                     v = PyInt_FromLong((long)why);
  1824.                     PUSH(v);
  1825.                 }
  1826.                 why = WHY_NOT;
  1827.                 JUMPTO(b->b_handler);
  1828.                 break;
  1829.             }
  1830.         } /* unwind stack */
  1831.  
  1832.         /* End the loop if we still have an error (or return) */
  1833.         
  1834.         if (why != WHY_NOT)
  1835.             break;
  1836.         
  1837.     } /* main loop */
  1838.     
  1839.     /* Pop remaining stack entries */
  1840.     
  1841.     while (!EMPTY()) {
  1842.         v = POP();
  1843.         Py_XDECREF(v);
  1844.     }
  1845.     
  1846.     if (why != WHY_RETURN)
  1847.         retval = NULL;
  1848.     
  1849.     if (f->f_trace) {
  1850.         if (why == WHY_RETURN) {
  1851.             if (call_trace(&f->f_trace, &f->f_trace, f,
  1852.                        "return", retval)) {
  1853.                 Py_XDECREF(retval);
  1854.                 retval = NULL;
  1855.                 why = WHY_EXCEPTION;
  1856.             }
  1857.         }
  1858.     }
  1859.     
  1860.     if (tstate->sys_profilefunc && why == WHY_RETURN) {
  1861.         if (call_trace(&tstate->sys_profilefunc, (PyObject**)0,
  1862.                    f, "return", retval)) {
  1863.             Py_XDECREF(retval);
  1864.             retval = NULL;
  1865.             why = WHY_EXCEPTION;
  1866.         }
  1867.     }
  1868.  
  1869.     reset_exc_info(tstate);
  1870.  
  1871.     --tstate->recursion_depth;
  1872.  
  1873.   fail: /* Jump here from prelude on failure */
  1874.     
  1875.     /* Restore previous frame and release the current one */
  1876.  
  1877.     tstate->frame = f->f_back;
  1878.     Py_DECREF(f);
  1879.     
  1880.     return retval;
  1881. }
  1882.  
  1883. static void
  1884. set_exc_info(tstate, type, value, tb)
  1885.     PyThreadState *tstate;
  1886.     PyObject *type;
  1887.     PyObject *value;
  1888.     PyObject *tb;
  1889. {
  1890.     PyFrameObject *frame;
  1891.     PyObject *tmp_type, *tmp_value, *tmp_tb;
  1892.  
  1893.     frame = tstate->frame;
  1894.     if (frame->f_exc_type == NULL) {
  1895.         /* This frame didn't catch an exception before */
  1896.         /* Save previous exception of this thread in this frame */
  1897.         if (tstate->exc_type == NULL) {
  1898.             Py_INCREF(Py_None);
  1899.             tstate->exc_type = Py_None;
  1900.         }
  1901.         tmp_type = frame->f_exc_type;
  1902.         tmp_value = frame->f_exc_value;
  1903.         tmp_tb = frame->f_exc_traceback;
  1904.         Py_XINCREF(tstate->exc_type);
  1905.         Py_XINCREF(tstate->exc_value);
  1906.         Py_XINCREF(tstate->exc_traceback);
  1907.         frame->f_exc_type = tstate->exc_type;
  1908.         frame->f_exc_value = tstate->exc_value;
  1909.         frame->f_exc_traceback = tstate->exc_traceback;
  1910.         Py_XDECREF(tmp_type);
  1911.         Py_XDECREF(tmp_value);
  1912.         Py_XDECREF(tmp_tb);
  1913.     }
  1914.     /* Set new exception for this thread */
  1915.     tmp_type = tstate->exc_type;
  1916.     tmp_value = tstate->exc_value;
  1917.     tmp_tb = tstate->exc_traceback;
  1918.     Py_XINCREF(type);
  1919.     Py_XINCREF(value);
  1920.     Py_XINCREF(tb);
  1921.     tstate->exc_type = type;
  1922.     tstate->exc_value = value;
  1923.     tstate->exc_traceback = tb;
  1924.     Py_XDECREF(tmp_type);
  1925.     Py_XDECREF(tmp_value);
  1926.     Py_XDECREF(tmp_tb);
  1927.     /* For b/w compatibility */
  1928.     PySys_SetObject("exc_type", type);
  1929.     PySys_SetObject("exc_value", value);
  1930.     PySys_SetObject("exc_traceback", tb);
  1931. }
  1932.  
  1933. static void
  1934. reset_exc_info(tstate)
  1935.     PyThreadState *tstate;
  1936. {
  1937.     PyFrameObject *frame;
  1938.     PyObject *tmp_type, *tmp_value, *tmp_tb;
  1939.     frame = tstate->frame;
  1940.     if (frame->f_exc_type != NULL) {
  1941.         /* This frame caught an exception */
  1942.         tmp_type = tstate->exc_type;
  1943.         tmp_value = tstate->exc_value;
  1944.         tmp_tb = tstate->exc_traceback;
  1945.         Py_XINCREF(frame->f_exc_type);
  1946.         Py_XINCREF(frame->f_exc_value);
  1947.         Py_XINCREF(frame->f_exc_traceback);
  1948.         tstate->exc_type = frame->f_exc_type;
  1949.         tstate->exc_value = frame->f_exc_value;
  1950.         tstate->exc_traceback = frame->f_exc_traceback;
  1951.         Py_XDECREF(tmp_type);
  1952.         Py_XDECREF(tmp_value);
  1953.         Py_XDECREF(tmp_tb);
  1954.         /* For b/w compatibility */
  1955.         PySys_SetObject("exc_type", frame->f_exc_type);
  1956.         PySys_SetObject("exc_value", frame->f_exc_value);
  1957.         PySys_SetObject("exc_traceback", frame->f_exc_traceback);
  1958.     }
  1959.     tmp_type = frame->f_exc_type;
  1960.     tmp_value = frame->f_exc_value;
  1961.     tmp_tb = frame->f_exc_traceback;
  1962.     frame->f_exc_type = NULL;
  1963.     frame->f_exc_value = NULL;
  1964.     frame->f_exc_traceback = NULL;
  1965.     Py_XDECREF(tmp_type);
  1966.     Py_XDECREF(tmp_value);
  1967.     Py_XDECREF(tmp_tb);
  1968. }
  1969.  
  1970. /* Logic for the raise statement (too complicated for inlining).
  1971.    This *consumes* a reference count to each of its arguments. */
  1972. static enum why_code
  1973. do_raise(type, value, tb)
  1974.     PyObject *type, *value, *tb;
  1975. {
  1976.     if (type == NULL) {
  1977.         /* Reraise */
  1978.         PyThreadState *tstate = PyThreadState_Get();
  1979.         type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
  1980.         value = tstate->exc_value;
  1981.         tb = tstate->exc_traceback;
  1982.         Py_XINCREF(type);
  1983.         Py_XINCREF(value);
  1984.         Py_XINCREF(tb);
  1985.     }
  1986.         
  1987.     /* We support the following forms of raise:
  1988.        raise <class>, <classinstance>
  1989.        raise <class>, <argument tuple>
  1990.        raise <class>, None
  1991.        raise <class>, <argument>
  1992.        raise <classinstance>, None
  1993.        raise <string>, <object>
  1994.        raise <string>, None
  1995.  
  1996.        An omitted second argument is the same as None.
  1997.  
  1998.        In addition, raise <tuple>, <anything> is the same as
  1999.        raising the tuple's first item (and it better have one!);
  2000.        this rule is applied recursively.
  2001.  
  2002.        Finally, an optional third argument can be supplied, which
  2003.        gives the traceback to be substituted (useful when
  2004.        re-raising an exception after examining it).  */
  2005.  
  2006.     /* First, check the traceback argument, replacing None with
  2007.        NULL. */
  2008.     if (tb == Py_None) {
  2009.         Py_DECREF(tb);
  2010.         tb = NULL;
  2011.     }
  2012.     else if (tb != NULL && !PyTraceBack_Check(tb)) {
  2013.         PyErr_SetString(PyExc_TypeError,
  2014.                "raise 3rd arg must be traceback or None");
  2015.         goto raise_error;
  2016.     }
  2017.  
  2018.     /* Next, replace a missing value with None */
  2019.     if (value == NULL) {
  2020.         value = Py_None;
  2021.         Py_INCREF(value);
  2022.     }
  2023.  
  2024.     /* Next, repeatedly, replace a tuple exception with its first item */
  2025.     while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
  2026.         PyObject *tmp = type;
  2027.         type = PyTuple_GET_ITEM(type, 0);
  2028.         Py_INCREF(type);
  2029.         Py_DECREF(tmp);
  2030.     }
  2031.  
  2032.     if (PyString_Check(type))
  2033.         ;
  2034.  
  2035.     else if (PyClass_Check(type))
  2036.         PyErr_NormalizeException(&type, &value, &tb);
  2037.  
  2038.     else if (PyInstance_Check(type)) {
  2039.         /* Raising an instance.  The value should be a dummy. */
  2040.         if (value != Py_None) {
  2041.             PyErr_SetString(PyExc_TypeError,
  2042.               "instance exception may not have a separate value");
  2043.             goto raise_error;
  2044.         }
  2045.         else {
  2046.             /* Normalize to raise <class>, <instance> */
  2047.             Py_DECREF(value);
  2048.             value = type;
  2049.             type = (PyObject*) ((PyInstanceObject*)type)->in_class;
  2050.             Py_INCREF(type);
  2051.         }
  2052.     }
  2053.     else {
  2054.         /* Not something you can raise.  You get an exception
  2055.            anyway, just not what you specified :-) */
  2056.         PyErr_SetString(PyExc_TypeError,
  2057.             "exceptions must be strings, classes, or instances");
  2058.         goto raise_error;
  2059.     }
  2060.     PyErr_Restore(type, value, tb);
  2061.     if (tb == NULL)
  2062.         return WHY_EXCEPTION;
  2063.     else
  2064.         return WHY_RERAISE;
  2065.  raise_error:
  2066.     Py_XDECREF(value);
  2067.     Py_XDECREF(type);
  2068.     Py_XDECREF(tb);
  2069.     return WHY_EXCEPTION;
  2070. }
  2071.  
  2072. static int
  2073. unpack_sequence(v, argcnt, sp)
  2074.      PyObject *v;
  2075.      int argcnt;
  2076.      PyObject **sp;
  2077. {
  2078.     int i;
  2079.     PyObject *w;
  2080.     
  2081.     for (i = 0; i < argcnt; i++) {
  2082.         if (! (w = PySequence_GetItem(v, i))) {
  2083.             if (PyErr_ExceptionMatches(PyExc_IndexError))
  2084.                 PyErr_SetString(PyExc_ValueError,
  2085.                           "unpack sequence of wrong size");
  2086.             goto finally;
  2087.         }
  2088.         *--sp = w;
  2089.     }
  2090.     /* we better get an IndexError now */
  2091.     if (PySequence_GetItem(v, i) == NULL) {
  2092.         if (PyErr_ExceptionMatches(PyExc_IndexError)) {
  2093.             PyErr_Clear();
  2094.             return 1;
  2095.         }
  2096.         /* some other exception occurred. fall through to finally */
  2097.     }
  2098.     else
  2099.         PyErr_SetString(PyExc_ValueError,
  2100.                 "unpack sequence of wrong size");
  2101.     /* fall through */
  2102. finally:
  2103.     for (; i > 0; i--, sp++)
  2104.         Py_DECREF(*sp);
  2105.  
  2106.     return 0;
  2107. }
  2108.  
  2109.  
  2110. #ifdef LLTRACE
  2111. static int
  2112. prtrace(v, str)
  2113.     PyObject *v;
  2114.     char *str;
  2115. {
  2116.     printf("%s ", str);
  2117.     if (PyObject_Print(v, stdout, 0) != 0)
  2118.         PyErr_Clear(); /* Don't know what else to do */
  2119.     printf("\n");
  2120. }
  2121. #endif
  2122.  
  2123. static void
  2124. call_exc_trace(p_trace, p_newtrace, f)
  2125.     PyObject **p_trace, **p_newtrace;
  2126.     PyFrameObject *f;
  2127. {
  2128.     PyObject *type, *value, *traceback, *arg;
  2129.     int err;
  2130.     PyErr_Fetch(&type, &value, &traceback);
  2131.     if (value == NULL) {
  2132.         value = Py_None;
  2133.         Py_INCREF(value);
  2134.     }
  2135.     arg = Py_BuildValue("(OOO)", type, value, traceback);
  2136.     if (arg == NULL) {
  2137.         PyErr_Restore(type, value, traceback);
  2138.         return;
  2139.     }
  2140.     err = call_trace(p_trace, p_newtrace, f, "exception", arg);
  2141.     Py_DECREF(arg);
  2142.     if (err == 0)
  2143.         PyErr_Restore(type, value, traceback);
  2144.     else {
  2145.         Py_XDECREF(type);
  2146.         Py_XDECREF(value);
  2147.         Py_XDECREF(traceback);
  2148.     }
  2149. }
  2150.  
  2151. static int
  2152. call_trace(p_trace, p_newtrace, f, msg, arg)
  2153.     PyObject **p_trace; /* in/out; may not be NULL;
  2154.                  may not point to NULL variable initially */
  2155.     PyObject **p_newtrace; /* in/out; may be NULL;
  2156.                 may point to NULL variable;
  2157.                 may be same variable as p_newtrace */
  2158.     PyFrameObject *f;
  2159.     char *msg;
  2160.     PyObject *arg;
  2161. {
  2162.     PyThreadState *tstate = f->f_tstate;
  2163.     PyObject *args, *what;
  2164.     PyObject *res = NULL;
  2165.     
  2166.     if (tstate->tracing) {
  2167.         /* Don't do recursive traces */
  2168.         if (p_newtrace) {
  2169.             Py_XDECREF(*p_newtrace);
  2170.             *p_newtrace = NULL;
  2171.         }
  2172.         return 0;
  2173.     }
  2174.     
  2175.     args = PyTuple_New(3);
  2176.     if (args == NULL)
  2177.         goto cleanup;
  2178.     what = PyString_FromString(msg);
  2179.     if (what == NULL)
  2180.         goto cleanup;
  2181.     Py_INCREF(f);
  2182.     PyTuple_SET_ITEM(args, 0, (PyObject *)f);
  2183.     PyTuple_SET_ITEM(args, 1, what);
  2184.     if (arg == NULL)
  2185.         arg = Py_None;
  2186.     Py_INCREF(arg);
  2187.     PyTuple_SET_ITEM(args, 2, arg);
  2188.     tstate->tracing++;
  2189.     PyFrame_FastToLocals(f);
  2190.     res = PyEval_CallObject(*p_trace, args); /* May clear *p_trace! */
  2191.     PyFrame_LocalsToFast(f, 1);
  2192.     tstate->tracing--;
  2193.  cleanup:
  2194.     Py_XDECREF(args);
  2195.     if (res == NULL) {
  2196.         /* The trace proc raised an exception */
  2197.         PyTraceBack_Here(f);
  2198.         Py_XDECREF(*p_trace);
  2199.         *p_trace = NULL;
  2200.         if (p_newtrace) {
  2201.             Py_XDECREF(*p_newtrace);
  2202.             *p_newtrace = NULL;
  2203.         }
  2204.         return -1;
  2205.     }
  2206.     else {
  2207.         if (p_newtrace) {
  2208.             Py_XDECREF(*p_newtrace);
  2209.             if (res == Py_None)
  2210.                 *p_newtrace = NULL;
  2211.             else {
  2212.                 Py_INCREF(res);
  2213.                 *p_newtrace = res;
  2214.             }
  2215.         }
  2216.         Py_DECREF(res);
  2217.         return 0;
  2218.     }
  2219. }
  2220.  
  2221. PyObject *
  2222. PyEval_GetBuiltins()
  2223. {
  2224.     PyThreadState *tstate = PyThreadState_Get();
  2225.     PyFrameObject *current_frame = tstate->frame;
  2226.     if (current_frame == NULL)
  2227.         return tstate->interp->builtins;
  2228.     else
  2229.         return current_frame->f_builtins;
  2230. }
  2231.  
  2232. PyObject *
  2233. PyEval_GetLocals()
  2234. {
  2235.     PyFrameObject *current_frame = PyThreadState_Get()->frame;
  2236.     if (current_frame == NULL)
  2237.         return NULL;
  2238.     PyFrame_FastToLocals(current_frame);
  2239.     return current_frame->f_locals;
  2240. }
  2241.  
  2242. PyObject *
  2243. PyEval_GetGlobals()
  2244. {
  2245.     PyFrameObject *current_frame = PyThreadState_Get()->frame;
  2246.     if (current_frame == NULL)
  2247.         return NULL;
  2248.     else
  2249.         return current_frame->f_globals;
  2250. }
  2251.  
  2252. PyObject *
  2253. PyEval_GetFrame()
  2254. {
  2255.     PyFrameObject *current_frame = PyThreadState_Get()->frame;
  2256.     return (PyObject *)current_frame;
  2257. }
  2258.  
  2259. int
  2260. PyEval_GetRestricted()
  2261. {
  2262.     PyFrameObject *current_frame = PyThreadState_Get()->frame;
  2263.     return current_frame == NULL ? 0 : current_frame->f_restricted;
  2264. }
  2265.  
  2266. int
  2267. Py_FlushLine()
  2268. {
  2269.     PyObject *f = PySys_GetObject("stdout");
  2270.     if (f == NULL)
  2271.         return 0;
  2272.     if (!PyFile_SoftSpace(f, 0))
  2273.         return 0;
  2274.     return PyFile_WriteString("\n", f);
  2275. }
  2276.  
  2277.  
  2278. /* External interface to call any callable object.
  2279.    The arg must be a tuple or NULL. */
  2280.  
  2281. #undef PyEval_CallObject
  2282. /* for backward compatibility: export this interface */
  2283.  
  2284. PyObject *
  2285. PyEval_CallObject(func, arg)
  2286.     PyObject *func;
  2287.     PyObject *arg;
  2288. {
  2289.     return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
  2290. }
  2291. #define PyEval_CallObject(func,arg) \
  2292.         PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
  2293.  
  2294. PyObject *
  2295. PyEval_CallObjectWithKeywords(func, arg, kw)
  2296.     PyObject *func;
  2297.     PyObject *arg;
  2298.     PyObject *kw;
  2299. {
  2300.         ternaryfunc call;
  2301.         PyObject *result;
  2302.  
  2303.     if (arg == NULL)
  2304.         arg = PyTuple_New(0);
  2305.     else if (!PyTuple_Check(arg)) {
  2306.         PyErr_SetString(PyExc_TypeError,
  2307.                 "argument list must be a tuple");
  2308.         return NULL;
  2309.     }
  2310.     else
  2311.         Py_INCREF(arg);
  2312.  
  2313.     if (kw != NULL && !PyDict_Check(kw)) {
  2314.         PyErr_SetString(PyExc_TypeError,
  2315.                 "keyword list must be a dictionary");
  2316.         return NULL;
  2317.     }
  2318.  
  2319.         if ((call = func->ob_type->tp_call) != NULL)
  2320.                 result = (*call)(func, arg, kw);
  2321.         else if (PyMethod_Check(func) || PyFunction_Check(func))
  2322.         result = call_function(func, arg, kw);
  2323.     else
  2324.         result = call_builtin(func, arg, kw);
  2325.  
  2326.     Py_DECREF(arg);
  2327.     
  2328.         if (result == NULL && !PyErr_Occurred())
  2329.         PyErr_SetString(PyExc_SystemError,
  2330.                "NULL result without error in call_object");
  2331.         
  2332.         return result;
  2333. }
  2334.  
  2335. static PyObject *
  2336. call_builtin(func, arg, kw)
  2337.     PyObject *func;
  2338.     PyObject *arg;
  2339.     PyObject *kw;
  2340. {
  2341.     if (PyCFunction_Check(func)) {
  2342.         PyCFunction meth = PyCFunction_GetFunction(func);
  2343.         PyObject *self = PyCFunction_GetSelf(func);
  2344.         int flags = PyCFunction_GetFlags(func);
  2345.         if (!(flags & METH_VARARGS)) {
  2346.             int size = PyTuple_Size(arg);
  2347.             if (size == 1)
  2348.                 arg = PyTuple_GET_ITEM(arg, 0);
  2349.             else if (size == 0)
  2350.                 arg = NULL;
  2351.         }
  2352.         if (flags & METH_KEYWORDS)
  2353.             return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
  2354.         if (kw != NULL && PyDict_Size(kw) != 0) {
  2355.             PyErr_SetString(PyExc_TypeError,
  2356.                    "this function takes no keyword arguments");
  2357.             return NULL;
  2358.         }
  2359.         return (*meth)(self, arg);
  2360.     }
  2361.     if (PyClass_Check(func)) {
  2362.         return PyInstance_New(func, arg, kw);
  2363.     }
  2364.     if (PyInstance_Check(func)) {
  2365.             PyObject *res, *call = PyObject_GetAttrString(func,"__call__");
  2366.         if (call == NULL) {
  2367.             PyErr_Clear();
  2368.             PyErr_SetString(PyExc_AttributeError,
  2369.                    "no __call__ method defined");
  2370.             return NULL;
  2371.         }
  2372.         res = PyEval_CallObjectWithKeywords(call, arg, kw);
  2373.         Py_DECREF(call);
  2374.         return res;
  2375.     }
  2376.     PyErr_Format(PyExc_TypeError, "call of non-function (type %s)",
  2377.              func->ob_type->tp_name);
  2378.     return NULL;
  2379. }
  2380.  
  2381. static PyObject *
  2382. call_function(func, arg, kw)
  2383.     PyObject *func;
  2384.     PyObject *arg;
  2385.     PyObject *kw;
  2386. {
  2387.     PyObject *class = NULL; /* == owner */
  2388.     PyObject *argdefs;
  2389.     PyObject **d, **k;
  2390.     int nk, nd;
  2391.     PyObject *result;
  2392.     
  2393.     if (kw != NULL && !PyDict_Check(kw)) {
  2394.         PyErr_BadInternalCall();
  2395.         return NULL;
  2396.     }
  2397.     
  2398.     if (PyMethod_Check(func)) {
  2399.         PyObject *self = PyMethod_Self(func);
  2400.         class = PyMethod_Class(func);
  2401.         func = PyMethod_Function(func);
  2402.         if (self == NULL) {
  2403.             /* Unbound methods must be called with an instance of
  2404.                the class (or a derived class) as first argument */
  2405.             if (PyTuple_Size(arg) >= 1) {
  2406.                 self = PyTuple_GET_ITEM(arg, 0);
  2407.                 if (self != NULL &&
  2408.                     PyInstance_Check(self) &&
  2409.                     PyClass_IsSubclass((PyObject *)
  2410.                       (((PyInstanceObject *)self)->in_class),
  2411.                            class))
  2412.                     /* Handy-dandy */ ;
  2413.                 else
  2414.                     self = NULL;
  2415.             }
  2416.             if (self == NULL) {
  2417.                 PyErr_SetString(PyExc_TypeError,
  2418.        "unbound method must be called with class instance 1st argument");
  2419.                 return NULL;
  2420.             }
  2421.             Py_INCREF(arg);
  2422.         }
  2423.         else {
  2424.             int argcount = PyTuple_Size(arg);
  2425.             PyObject *newarg = PyTuple_New(argcount + 1);
  2426.             int i;
  2427.             if (newarg == NULL)
  2428.                 return NULL;
  2429.             Py_INCREF(self);
  2430.             PyTuple_SET_ITEM(newarg, 0, self);
  2431.             for (i = 0; i < argcount; i++) {
  2432.                 PyObject *v = PyTuple_GET_ITEM(arg, i);
  2433.                 Py_XINCREF(v);
  2434.                 PyTuple_SET_ITEM(newarg, i+1, v);
  2435.             }
  2436.             arg = newarg;
  2437.         }
  2438.         if (!PyFunction_Check(func)) {
  2439.             result = PyEval_CallObjectWithKeywords(func, arg, kw);
  2440.             Py_DECREF(arg);
  2441.             return result;
  2442.         }
  2443.     }
  2444.     else {
  2445.         if (!PyFunction_Check(func)) {
  2446.             PyErr_Format(PyExc_TypeError,
  2447.                      "call of non-function (type %s)",
  2448.                      func->ob_type->tp_name);
  2449.             return NULL;
  2450.         }
  2451.         Py_INCREF(arg);
  2452.     }
  2453.     
  2454.     argdefs = PyFunction_GetDefaults(func);
  2455.     if (argdefs != NULL && PyTuple_Check(argdefs)) {
  2456.         d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
  2457.         nd = PyTuple_Size(argdefs);
  2458.     }
  2459.     else {
  2460.         d = NULL;
  2461.         nd = 0;
  2462.     }
  2463.     
  2464.     if (kw != NULL) {
  2465.         int pos, i;
  2466.         nk = PyDict_Size(kw);
  2467.         k = PyMem_NEW(PyObject *, 2*nk);
  2468.         if (k == NULL) {
  2469.             PyErr_NoMemory();
  2470.             Py_DECREF(arg);
  2471.             return NULL;
  2472.         }
  2473.         pos = i = 0;
  2474.         while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
  2475.             i += 2;
  2476.         nk = i/2;
  2477.         /* XXX This is broken if the caller deletes dict items! */
  2478.     }
  2479.     else {
  2480.         k = NULL;
  2481.         nk = 0;
  2482.     }
  2483.     
  2484.     result = eval_code2(
  2485.         (PyCodeObject *)PyFunction_GetCode(func),
  2486.         PyFunction_GetGlobals(func), (PyObject *)NULL,
  2487.         &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
  2488.         k, nk,
  2489.         d, nd,
  2490.         class);
  2491.     
  2492.     Py_DECREF(arg);
  2493.     PyMem_XDEL(k);
  2494.     
  2495.     return result;
  2496. }
  2497.  
  2498. #define SLICE_ERROR_MSG \
  2499.     "standard sequence type does not support step size other than one"
  2500.  
  2501. static PyObject *
  2502. loop_subscript(v, w)
  2503.     PyObject *v, *w;
  2504. {
  2505.     PySequenceMethods *sq = v->ob_type->tp_as_sequence;
  2506.     int i;
  2507.     if (sq == NULL || sq->sq_item == NULL) {
  2508.         PyErr_SetString(PyExc_TypeError, "loop over non-sequence");
  2509.         return NULL;
  2510.     }
  2511.     i = PyInt_AsLong(w);
  2512.     v = (*sq->sq_item)(v, i);
  2513.     if (v)
  2514.         return v;
  2515.     if (PyErr_ExceptionMatches(PyExc_IndexError))
  2516.         PyErr_Clear();
  2517.     return NULL;
  2518. }
  2519.  
  2520. static int
  2521. slice_index(v, pi)
  2522.     PyObject *v;
  2523.     int *pi;
  2524. {
  2525.     if (v != NULL) {
  2526.         long x;
  2527.         if (!PyInt_Check(v)) {
  2528.             PyErr_SetString(PyExc_TypeError,
  2529.                     "slice index must be int");
  2530.             return -1;
  2531.         }
  2532.         x = PyInt_AsLong(v);
  2533.         /* Truncate -- very long indices are truncated anyway */
  2534.         if (x > INT_MAX)
  2535.             x = INT_MAX;
  2536.         else if (x < -INT_MAX)
  2537.             x = 0;
  2538.         *pi = x;
  2539.     }
  2540.     return 0;
  2541. }
  2542.  
  2543. static PyObject *
  2544. apply_slice(u, v, w) /* return u[v:w] */
  2545.     PyObject *u, *v, *w;
  2546. {
  2547.     int ilow = 0, ihigh = INT_MAX;
  2548.     if (slice_index(v, &ilow) != 0)
  2549.         return NULL;
  2550.     if (slice_index(w, &ihigh) != 0)
  2551.         return NULL;
  2552.     return PySequence_GetSlice(u, ilow, ihigh);
  2553. }
  2554.  
  2555. static int
  2556. assign_slice(u, v, w, x) /* u[v:w] = x */
  2557.     PyObject *u, *v, *w, *x;
  2558. {
  2559.     int ilow = 0, ihigh = INT_MAX;
  2560.     if (slice_index(v, &ilow) != 0)
  2561.         return -1;
  2562.     if (slice_index(w, &ihigh) != 0)
  2563.         return -1;
  2564.     if (x == NULL)
  2565.         return PySequence_DelSlice(u, ilow, ihigh);
  2566.     else
  2567.         return PySequence_SetSlice(u, ilow, ihigh, x);
  2568. }
  2569.  
  2570. static PyObject *
  2571. cmp_outcome(op, v, w)
  2572.     int op;
  2573.     register PyObject *v;
  2574.     register PyObject *w;
  2575. {
  2576.     register int cmp;
  2577.     register int res = 0;
  2578.     switch (op) {
  2579.     case IS:
  2580.     case IS_NOT:
  2581.         res = (v == w);
  2582.         if (op == (int) IS_NOT)
  2583.             res = !res;
  2584.         break;
  2585.     case IN:
  2586.     case NOT_IN:
  2587.         res = PySequence_Contains(w, v);
  2588.         if (res < 0)
  2589.             return NULL;
  2590.         if (op == (int) NOT_IN)
  2591.             res = !res;
  2592.         break;
  2593.     case EXC_MATCH:
  2594.         res = PyErr_GivenExceptionMatches(v, w);
  2595.         break;
  2596.     default:
  2597.         cmp = PyObject_Compare(v, w);
  2598.         if (cmp && PyErr_Occurred())
  2599.             return NULL;
  2600.         switch (op) {
  2601.         case LT: res = cmp <  0; break;
  2602.         case LE: res = cmp <= 0; break;
  2603.         case EQ: res = cmp == 0; break;
  2604.         case NE: res = cmp != 0; break;
  2605.         case GT: res = cmp >  0; break;
  2606.         case GE: res = cmp >= 0; break;
  2607.         /* XXX no default? (res is initialized to 0 though) */
  2608.         }
  2609.     }
  2610.     v = res ? Py_True : Py_False;
  2611.     Py_INCREF(v);
  2612.     return v;
  2613. }
  2614.  
  2615. static int
  2616. import_from(locals, v, name)
  2617.     PyObject *locals;
  2618.     PyObject *v;
  2619.     PyObject *name;
  2620. {
  2621.     PyObject *w, *x;
  2622.     if (!PyModule_Check(v)) {
  2623.         PyErr_SetString(PyExc_TypeError,
  2624.                 "import-from requires module object");
  2625.         return -1;
  2626.     }
  2627.     w = PyModule_GetDict(v);
  2628.     if (PyString_AsString(name)[0] == '*') {
  2629.         int pos, err;
  2630.         PyObject *name, *value;
  2631.         pos = 0;
  2632.         while (PyDict_Next(w, &pos, &name, &value)) {
  2633.             if (!PyString_Check(name) ||
  2634.                 PyString_AsString(name)[0] == '_')
  2635.                 continue;
  2636.             Py_INCREF(value);
  2637.             err = PyDict_SetItem(locals, name, value);
  2638.             Py_DECREF(value);
  2639.             if (err != 0)
  2640.                 return -1;
  2641.         }
  2642.         return 0;
  2643.     }
  2644.     else {
  2645.         x = PyDict_GetItem(w, name);
  2646.         if (x == NULL) {
  2647.             char buf[250];
  2648.             sprintf(buf, "cannot import name %.230s",
  2649.                 PyString_AsString(name));
  2650.             PyErr_SetString(PyExc_ImportError, buf);
  2651.             return -1;
  2652.         }
  2653.         else
  2654.             return PyDict_SetItem(locals, name, x);
  2655.     }
  2656. }
  2657.  
  2658. static PyObject *
  2659. build_class(methods, bases, name)
  2660.     PyObject *methods; /* dictionary */
  2661.     PyObject *bases;  /* tuple containing classes */
  2662.     PyObject *name;   /* string */
  2663. {
  2664.     int i, n;
  2665.     if (!PyTuple_Check(bases)) {
  2666.         PyErr_SetString(PyExc_SystemError,
  2667.                 "build_class with non-tuple bases");
  2668.         return NULL;
  2669.     }
  2670.     if (!PyDict_Check(methods)) {
  2671.         PyErr_SetString(PyExc_SystemError,
  2672.                 "build_class with non-dictionary");
  2673.         return NULL;
  2674.     }
  2675.     if (!PyString_Check(name)) {
  2676.         PyErr_SetString(PyExc_SystemError,
  2677.                 "build_class witn non-string name");
  2678.         return NULL;
  2679.     }
  2680.     n = PyTuple_Size(bases);
  2681.     for (i = 0; i < n; i++) {
  2682.         PyObject *base = PyTuple_GET_ITEM(bases, i);
  2683.         if (!PyClass_Check(base)) {
  2684.             /* Call the base's *type*, if it is callable.
  2685.                This code is a hook for Donald Beaudry's
  2686.                and Jim Fulton's type extensions.  In
  2687.                unexended Python it will never be triggered
  2688.                since its types are not callable.
  2689.                Ditto: call the bases's *class*, if it has
  2690.                one.  This makes the same thing possible
  2691.                without writing C code.  A true meta-object
  2692.                protocol! */
  2693.             PyObject *basetype = (PyObject *)base->ob_type;
  2694.             PyObject *callable = NULL;
  2695.             if (PyCallable_Check(basetype))
  2696.                 callable = basetype;
  2697.             else
  2698.                 callable = PyObject_GetAttrString(
  2699.                     base, "__class__");
  2700.             if (callable) {
  2701.                 PyObject *args;
  2702.                 PyObject *newclass = NULL;
  2703.                 args = Py_BuildValue(
  2704.                     "(OOO)", name, bases, methods);
  2705.                 if (args != NULL) {
  2706.                     newclass = PyEval_CallObject(
  2707.                         callable, args);
  2708.                     Py_DECREF(args);
  2709.                 }
  2710.                 if (callable != basetype) {
  2711.                     Py_DECREF(callable);
  2712.                 }
  2713.                 return newclass;
  2714.             }
  2715.             PyErr_SetString(PyExc_TypeError,
  2716.                 "base is not a class object");
  2717.             return NULL;
  2718.         }
  2719.     }
  2720.     return PyClass_New(bases, methods, name);
  2721. }
  2722.  
  2723. static int
  2724. exec_statement(f, prog, globals, locals)
  2725.     PyFrameObject *f;
  2726.     PyObject *prog;
  2727.     PyObject *globals;
  2728.     PyObject *locals;
  2729. {
  2730.     char *s;
  2731.     int n;
  2732.     PyObject *v;
  2733.     int plain = 0;
  2734.  
  2735.     if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
  2736.         ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
  2737.         /* Backward compatibility hack */
  2738.         globals = PyTuple_GetItem(prog, 1);
  2739.         if (n == 3)
  2740.             locals = PyTuple_GetItem(prog, 2);
  2741.         prog = PyTuple_GetItem(prog, 0);
  2742.     }
  2743.     if (globals == Py_None) {
  2744.         globals = PyEval_GetGlobals();
  2745.         if (locals == Py_None) {
  2746.             locals = PyEval_GetLocals();
  2747.             plain = 1;
  2748.         }
  2749.     }
  2750.     else if (locals == Py_None)
  2751.         locals = globals;
  2752.     if (!PyString_Check(prog) &&
  2753.         !PyCode_Check(prog) &&
  2754.         !PyFile_Check(prog)) {
  2755.         PyErr_SetString(PyExc_TypeError,
  2756.                "exec 1st arg must be string, code or file object");
  2757.         return -1;
  2758.     }
  2759.     if (!PyDict_Check(globals) || !PyDict_Check(locals)) {
  2760.         PyErr_SetString(PyExc_TypeError,
  2761.             "exec 2nd/3rd args must be dict or None");
  2762.         return -1;
  2763.     }
  2764.     if (PyDict_GetItemString(globals, "__builtins__") == NULL)
  2765.         PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
  2766.     if (PyCode_Check(prog)) {
  2767.         v = PyEval_EvalCode((PyCodeObject *) prog,
  2768.                     globals, locals);
  2769.         if (v == NULL)
  2770.             return -1;
  2771.         Py_DECREF(v);
  2772.         return 0;
  2773.     }
  2774.     if (PyFile_Check(prog)) {
  2775.         FILE *fp = PyFile_AsFile(prog);
  2776.         char *name = PyString_AsString(PyFile_Name(prog));
  2777.         if (PyRun_File(fp, name, Py_file_input,
  2778.                    globals, locals) == NULL)
  2779.             return -1;
  2780.         return 0;
  2781.     }
  2782.     s = PyString_AsString(prog);
  2783.     if ((int)strlen(s) != PyString_Size(prog)) {
  2784.         PyErr_SetString(PyExc_ValueError,
  2785.                 "embedded '\\0' in exec string");
  2786.         return -1;
  2787.     }
  2788.     v = PyRun_String(s, Py_file_input, globals, locals);
  2789.     if (v == NULL)
  2790.         return -1;
  2791.     Py_DECREF(v);
  2792.     if (plain)
  2793.         PyFrame_LocalsToFast(f, 0);
  2794.     return 0;
  2795. }
  2796.  
  2797. /* Hack for ni.py */
  2798. static PyObject *
  2799. find_from_args(f, nexti)
  2800.     PyFrameObject *f;
  2801.     int nexti;
  2802. {
  2803.     int opcode;
  2804.     int oparg;
  2805.     PyObject *list, *name;
  2806.     unsigned char *next_instr;
  2807.     
  2808.     _PyCode_GETCODEPTR(f->f_code, &next_instr);
  2809.     next_instr += nexti;
  2810.  
  2811.     opcode = (*next_instr++);
  2812.     if (opcode != IMPORT_FROM) {
  2813.         Py_INCREF(Py_None);
  2814.         return Py_None;
  2815.     }
  2816.     
  2817.     list = PyList_New(0);
  2818.     if (list == NULL)
  2819.         return NULL;
  2820.     
  2821.     do {
  2822.         oparg = (next_instr[1]<<8) + next_instr[0];
  2823.         next_instr += 2;
  2824.         name = Getnamev(f, oparg);
  2825.         if (PyList_Append(list, name) < 0) {
  2826.             Py_DECREF(list);
  2827.             break;
  2828.         }
  2829.         opcode = (*next_instr++);
  2830.     } while (opcode == IMPORT_FROM);
  2831.     
  2832.     return list;
  2833. }
  2834.  
  2835.  
  2836. #ifdef DYNAMIC_EXECUTION_PROFILE
  2837.  
  2838. PyObject *
  2839. getarray(a)
  2840.     long a[256];
  2841. {
  2842.     int i;
  2843.     PyObject *l = PyList_New(256);
  2844.     if (l == NULL) return NULL;
  2845.     for (i = 0; i < 256; i++) {
  2846.         PyObject *x = PyInt_FromLong(a[i]);
  2847.         if (x == NULL) {
  2848.             Py_DECREF(l);
  2849.             return NULL;
  2850.         }
  2851.         PyList_SetItem(l, i, x);
  2852.     }
  2853.     for (i = 0; i < 256; i++)
  2854.         a[i] = 0;
  2855.     return l;
  2856. }
  2857.  
  2858. PyObject *
  2859. _Py_GetDXProfile(self, args)
  2860.     PyObject *self, *args;
  2861. {
  2862. #ifndef DXPAIRS
  2863.     return getarray(dxp);
  2864. #else
  2865.     int i;
  2866.     PyObject *l = PyList_New(257);
  2867.     if (l == NULL) return NULL;
  2868.     for (i = 0; i < 257; i++) {
  2869.         PyObject *x = getarray(dxpairs[i]);
  2870.         if (x == NULL) {
  2871.             Py_DECREF(l);
  2872.             return NULL;
  2873.         }
  2874.         PyList_SetItem(l, i, x);
  2875.     }
  2876.     return l;
  2877. #endif
  2878. }
  2879.  
  2880. #endif
  2881.