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