blob: 09ae132c2fe188c7681125b0a2780de67ecf69d6 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
5 XXX how to pass arguments to call_trace?
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00007 XXX document it!
8 */
9
Guido van Rossumb209a111997-04-29 18:18:01 +000010#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011
Guido van Rossum10dc2e81990-11-18 17:27:39 +000012#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000013#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000014#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000015#include "opcode.h"
16
Jack Jansencbf630f2000-07-11 21:59:16 +000017#ifdef macintosh
18#include "macglue.h"
19#endif
20
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Guido van Rossuma027efa1997-05-05 20:56:21 +000023#ifdef HAVE_LIMITS_H
24#include <limits.h>
25#else
26#define INT_MAX 2147483647
27#endif
28
Guido van Rossum04691fc1992-08-12 15:35:34 +000029/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000030/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000031
Guido van Rossum408027e1996-12-30 16:17:54 +000032#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000033/* For debugging the interpreter: */
34#define LLTRACE 1 /* Low-level trace feature */
35#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000036#endif
37
Guido van Rossum5b722181993-03-30 17:46:03 +000038
Guido van Rossum374a9221991-04-04 10:40:29 +000039/* Forward declarations */
Guido van Rossum374a9221991-04-04 10:40:29 +000040
Tim Petersdbd9ba62000-07-09 03:09:57 +000041static PyObject *eval_code2(PyCodeObject *,
42 PyObject *, PyObject *,
43 PyObject **, int,
44 PyObject **, int,
45 PyObject **, int,
46 PyObject *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000047#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000048static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000049#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000050static void call_exc_trace(PyObject **, PyObject**, PyFrameObject *);
51static int call_trace(PyObject **, PyObject **,
52 PyFrameObject *, char *, PyObject *);
53static PyObject *call_builtin(PyObject *, PyObject *, PyObject *);
54static PyObject *call_function(PyObject *, PyObject *, PyObject *);
55static PyObject *loop_subscript(PyObject *, PyObject *);
56static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
57static int assign_slice(PyObject *, PyObject *,
58 PyObject *, PyObject *);
59static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000060static PyObject *import_from(PyObject *, PyObject *);
61static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000062static PyObject *build_class(PyObject *, PyObject *, PyObject *);
63static int exec_statement(PyFrameObject *,
64 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000065static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
66static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000067static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000068
Paul Prescode68140d2000-08-30 20:25:01 +000069#define NAME_ERROR_MSG \
70 "There is no variable named '%s'"
71#define UNBOUNDLOCAL_ERROR_MSG \
72 "Local variable '%.200s' referenced before assignment"
Guido van Rossum374a9221991-04-04 10:40:29 +000073
Guido van Rossum950361c1997-01-24 13:49:28 +000074/* Dynamic execution profile */
75#ifdef DYNAMIC_EXECUTION_PROFILE
76#ifdef DXPAIRS
77static long dxpairs[257][256];
78#define dxp dxpairs[256]
79#else
80static long dxp[256];
81#endif
82#endif
83
84
Guido van Rossume59214e1994-08-30 08:01:59 +000085#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +000086
Guido van Rossum2571cc81999-04-07 16:07:23 +000087#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +000088#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000089#endif
Guido van Rossum49b56061998-10-01 20:42:43 +000090#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000091
Guido van Rossuma027efa1997-05-05 20:56:21 +000092extern int _PyThread_Started; /* Flag for Py_Exit */
93
Guido van Rossum65d5b571998-12-21 19:32:43 +000094static PyThread_type_lock interpreter_lock = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +000095static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000096
97void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000098PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +000099{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000100 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000101 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000102 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000103 interpreter_lock = PyThread_allocate_lock();
104 PyThread_acquire_lock(interpreter_lock, 1);
105 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000106}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000107
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000108void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000109PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000110{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000111 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112}
113
114void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000115PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000116{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000117 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000118}
119
120void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000121PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000122{
123 if (tstate == NULL)
124 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000125 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000126 if (PyThreadState_Swap(tstate) != NULL)
127 Py_FatalError(
128 "PyEval_AcquireThread: non-NULL old thread state");
129}
130
131void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000132PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000133{
134 if (tstate == NULL)
135 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
136 if (PyThreadState_Swap(NULL) != tstate)
137 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000138 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000139}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000140
141/* This function is called from PyOS_AfterFork to ensure that newly
142 created child processes don't hold locks referring to threads which
143 are not running in the child process. (This could also be done using
144 pthread_atfork mechanism, at least for the pthreads implementation.) */
145
146void
147PyEval_ReInitThreads(void)
148{
149 if (!interpreter_lock)
150 return;
151 /*XXX Can't use PyThread_free_lock here because it does too
152 much error-checking. Doing this cleanly would require
153 adding a new function to each thread_*.h. Instead, just
154 create a new lock and waste a little bit of memory */
155 interpreter_lock = PyThread_allocate_lock();
156 PyThread_acquire_lock(interpreter_lock, 1);
157 main_thread = PyThread_get_thread_ident();
158}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000159#endif
160
Guido van Rossumff4949e1992-08-05 19:58:53 +0000161/* Functions save_thread and restore_thread are always defined so
162 dynamically loaded modules needn't be compiled separately for use
163 with and without threads: */
164
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000165PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000166PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000167{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000168 PyThreadState *tstate = PyThreadState_Swap(NULL);
169 if (tstate == NULL)
170 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000171#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000172 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000173 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000175 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000176}
177
178void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000179PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000181 if (tstate == NULL)
182 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000183#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000184 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000185 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000186 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000187 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000188 }
189#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000190 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000191}
192
193
Guido van Rossuma9672091994-09-14 13:31:22 +0000194/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
195 signal handlers or Mac I/O completion routines) can schedule calls
196 to a function to be called synchronously.
197 The synchronous function is called with one void* argument.
198 It should return 0 for success or -1 for failure -- failure should
199 be accompanied by an exception.
200
201 If registry succeeds, the registry function returns 0; if it fails
202 (e.g. due to too many pending calls) it returns -1 (without setting
203 an exception condition).
204
205 Note that because registry may occur from within signal handlers,
206 or other asynchronous events, calling malloc() is unsafe!
207
208#ifdef WITH_THREAD
209 Any thread can schedule pending calls, but only the main thread
210 will execute them.
211#endif
212
213 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
214 There are two possible race conditions:
215 (1) nested asynchronous registry calls;
216 (2) registry calls made while pending calls are being processed.
217 While (1) is very unlikely, (2) is a real possibility.
218 The current code is safe against (2), but not against (1).
219 The safety against (2) is derived from the fact that only one
220 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000221
Guido van Rossuma027efa1997-05-05 20:56:21 +0000222 XXX Darn! With the advent of thread state, we should have an array
223 of pending calls per thread in the thread state! Later...
224*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000225
Guido van Rossuma9672091994-09-14 13:31:22 +0000226#define NPENDINGCALLS 32
227static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000228 int (*func)(void *);
229 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000230} pendingcalls[NPENDINGCALLS];
231static volatile int pendingfirst = 0;
232static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000233static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000234
235int
Thomas Wouters334fb892000-07-25 12:56:38 +0000236Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000237{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000238 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000239 int i, j;
240 /* XXX Begin critical section */
241 /* XXX If you want this to be safe against nested
242 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000243 if (busy)
244 return -1;
245 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000246 i = pendinglast;
247 j = (i + 1) % NPENDINGCALLS;
248 if (j == pendingfirst)
249 return -1; /* Queue full */
250 pendingcalls[i].func = func;
251 pendingcalls[i].arg = arg;
252 pendinglast = j;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000253 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000254 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000255 /* XXX End critical section */
256 return 0;
257}
258
Guido van Rossum180d7b41994-09-29 09:45:57 +0000259int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000260Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000261{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000262 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000263#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000264 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000265 return 0;
266#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000267 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000268 return 0;
269 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000270 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000271 for (;;) {
272 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000273 int (*func)(void *);
274 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000275 i = pendingfirst;
276 if (i == pendinglast)
277 break; /* Queue empty */
278 func = pendingcalls[i].func;
279 arg = pendingcalls[i].arg;
280 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000281 if (func(arg) < 0) {
282 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000283 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000284 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000285 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000286 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000287 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000288 return 0;
289}
290
291
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000292/* The interpreter's recursion limit */
293
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000294static int recursion_limit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000295
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000296int
297Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000298{
299 return recursion_limit;
300}
301
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000302void
303Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000304{
305 recursion_limit = new_limit;
306}
307
Guido van Rossum374a9221991-04-04 10:40:29 +0000308/* Status code for main loop (reason for stack unwind) */
309
310enum why_code {
311 WHY_NOT, /* No error */
312 WHY_EXCEPTION, /* Exception occurred */
313 WHY_RERAISE, /* Exception re-raised by 'finally' */
314 WHY_RETURN, /* 'return' statement */
315 WHY_BREAK /* 'break' statement */
316};
317
Tim Petersdbd9ba62000-07-09 03:09:57 +0000318static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
319static int unpack_sequence(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000320
Guido van Rossum374a9221991-04-04 10:40:29 +0000321
Guido van Rossumb209a111997-04-29 18:18:01 +0000322PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000323PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000324{
325 return eval_code2(co,
326 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000327 (PyObject **)NULL, 0,
328 (PyObject **)NULL, 0,
329 (PyObject **)NULL, 0,
330 (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000331}
332
333
334/* Interpreter main loop */
335
Guido van Rossumb209a111997-04-29 18:18:01 +0000336static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000337eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
338 PyObject **args, int argcount, PyObject **kws, int kwcount,
339 PyObject **defs, int defcount, PyObject *owner)
Guido van Rossum374a9221991-04-04 10:40:29 +0000340{
Guido van Rossum950361c1997-01-24 13:49:28 +0000341#ifdef DXPAIRS
342 int lastopcode = 0;
343#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000344 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000345 register int opcode=0; /* Current opcode */
346 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossumb209a111997-04-29 18:18:01 +0000347 register PyObject **stack_pointer;
Guido van Rossum374a9221991-04-04 10:40:29 +0000348 register enum why_code why; /* Reason for block stack unwind */
349 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000350 register PyObject *x; /* Result object -- NULL if error */
351 register PyObject *v; /* Temporary objects popped off stack */
352 register PyObject *w;
353 register PyObject *u;
354 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000355 register PyObject *stream = NULL; /* for PRINT opcodes */
Guido van Rossumb209a111997-04-29 18:18:01 +0000356 register PyFrameObject *f; /* Current frame */
Guido van Rossum50cd3481998-11-17 17:02:51 +0000357 register PyObject **fastlocals;
Guido van Rossum014518f1998-11-23 21:09:51 +0000358 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000359 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd076c731998-10-07 19:42:25 +0000360 unsigned char *first_instr;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000361#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000362 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000363#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000364#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000365 /* Make it easier to find out where we are with a debugger */
Guido van Rossumb209a111997-04-29 18:18:01 +0000366 char *filename = PyString_AsString(co->co_filename);
Guido van Rossum99bec951992-09-03 20:29:45 +0000367#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000368
369/* Code access macros */
370
371#define GETCONST(i) Getconst(f, i)
372#define GETNAME(i) Getname(f, i)
373#define GETNAMEV(i) Getnamev(f, i)
Guido van Rossumd076c731998-10-07 19:42:25 +0000374#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000375#define NEXTOP() (*next_instr++)
376#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000377#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000378#define JUMPBY(x) (next_instr += (x))
379
380/* Stack manipulation macros */
381
382#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
383#define EMPTY() (STACK_LEVEL() == 0)
384#define TOP() (stack_pointer[-1])
385#define BASIC_PUSH(v) (*stack_pointer++ = (v))
386#define BASIC_POP() (*--stack_pointer)
387
Guido van Rossum96a42c81992-01-12 02:29:51 +0000388#ifdef LLTRACE
389#define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
390#define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
Guido van Rossum374a9221991-04-04 10:40:29 +0000391#else
392#define PUSH(v) BASIC_PUSH(v)
393#define POP() BASIC_POP()
394#endif
395
Guido van Rossum681d79a1995-07-18 14:51:37 +0000396/* Local variable macros */
397
398#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumb209a111997-04-29 18:18:01 +0000399#define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \
Guido van Rossum681d79a1995-07-18 14:51:37 +0000400 GETLOCAL(i) = value; } while (0)
401
Guido van Rossuma027efa1997-05-05 20:56:21 +0000402/* Start of code */
403
Guido van Rossum8861b741996-07-30 16:49:37 +0000404#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000405 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000406 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000407 return NULL;
408 }
409#endif
410
Guido van Rossum681d79a1995-07-18 14:51:37 +0000411 if (globals == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000412 PyErr_SetString(PyExc_SystemError, "eval_code2: NULL globals");
Guido van Rossum6135a871995-01-09 17:53:26 +0000413 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000414 }
415
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000416#ifdef LLTRACE
Guido van Rossumb209a111997-04-29 18:18:01 +0000417 lltrace = PyDict_GetItemString(globals, "__lltrace__") != NULL;
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000418#endif
419
Guido van Rossumb209a111997-04-29 18:18:01 +0000420 f = PyFrame_New(
Guido van Rossuma027efa1997-05-05 20:56:21 +0000421 tstate, /*back*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000422 co, /*code*/
423 globals, /*globals*/
Guido van Rossum76836021997-01-20 04:26:20 +0000424 locals); /*locals*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000425 if (f == NULL)
426 return NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000427
Guido van Rossuma027efa1997-05-05 20:56:21 +0000428 tstate->frame = f;
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000429 fastlocals = f->f_localsplus;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000430
431 if (co->co_argcount > 0 ||
432 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
433 int i;
434 int n = argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000435 PyObject *kwdict = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000436 if (co->co_flags & CO_VARKEYWORDS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000437 kwdict = PyDict_New();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000438 if (kwdict == NULL)
439 goto fail;
Guido van Rossum8c5df061997-01-24 04:19:24 +0000440 i = co->co_argcount;
441 if (co->co_flags & CO_VARARGS)
442 i++;
443 SETLOCAL(i, kwdict);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000444 }
445 if (argcount > co->co_argcount) {
446 if (!(co->co_flags & CO_VARARGS)) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000447 PyErr_Format(PyExc_TypeError,
448 "too many arguments; expected %d, got %d",
449 co->co_argcount, argcount);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000450 goto fail;
451 }
452 n = co->co_argcount;
453 }
454 for (i = 0; i < n; i++) {
455 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000456 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000457 SETLOCAL(i, x);
458 }
459 if (co->co_flags & CO_VARARGS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000460 u = PyTuple_New(argcount - n);
Guido van Rossum8c5df061997-01-24 04:19:24 +0000461 if (u == NULL)
462 goto fail;
463 SETLOCAL(co->co_argcount, u);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000464 for (i = n; i < argcount; i++) {
465 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000466 Py_INCREF(x);
467 PyTuple_SET_ITEM(u, i-n, x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000468 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000469 }
470 for (i = 0; i < kwcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000471 PyObject *keyword = kws[2*i];
472 PyObject *value = kws[2*i + 1];
Guido van Rossum681d79a1995-07-18 14:51:37 +0000473 int j;
Guido van Rossum25da5be1999-10-26 00:12:20 +0000474 if (keyword == NULL || !PyString_Check(keyword)) {
475 PyErr_SetString(PyExc_TypeError,
476 "keywords must be strings");
477 goto fail;
478 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000479 /* XXX slow -- speed up using dictionary? */
480 for (j = 0; j < co->co_argcount; j++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000481 PyObject *nm = PyTuple_GET_ITEM(
482 co->co_varnames, j);
Guido van Rossumb209a111997-04-29 18:18:01 +0000483 if (PyObject_Compare(keyword, nm) == 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000484 break;
485 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000486 /* Check errors from Compare */
487 if (PyErr_Occurred())
488 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000489 if (j >= co->co_argcount) {
490 if (kwdict == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000491 PyErr_Format(PyExc_TypeError,
Guido van Rossuma027efa1997-05-05 20:56:21 +0000492 "unexpected keyword argument: %.400s",
Jeremy Hylton76901512000-03-28 23:49:17 +0000493 PyString_AsString(keyword));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000494 goto fail;
495 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000496 PyDict_SetItem(kwdict, keyword, value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000497 }
498 else {
499 if (GETLOCAL(j) != NULL) {
Jeremy Hylton76901512000-03-28 23:49:17 +0000500 PyErr_Format(PyExc_TypeError,
501 "keyword parameter redefined: %.400s",
502 PyString_AsString(keyword));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000503 goto fail;
504 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000505 Py_INCREF(value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000506 SETLOCAL(j, value);
507 }
508 }
509 if (argcount < co->co_argcount) {
510 int m = co->co_argcount - defcount;
511 for (i = argcount; i < m; i++) {
512 if (GETLOCAL(i) == NULL) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000513 PyErr_Format(PyExc_TypeError,
514 "not enough arguments; expected %d, got %d",
515 m, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000516 goto fail;
517 }
518 }
519 if (n > m)
520 i = n - m;
521 else
522 i = 0;
523 for (; i < defcount; i++) {
524 if (GETLOCAL(m+i) == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000525 PyObject *def = defs[i];
526 Py_INCREF(def);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000527 SETLOCAL(m+i, def);
528 }
529 }
530 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000531 }
532 else {
533 if (argcount > 0 || kwcount > 0) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000534 PyErr_SetString(PyExc_TypeError,
535 "no arguments expected");
Guido van Rossum8c5df061997-01-24 04:19:24 +0000536 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000537 }
538 }
539
Guido van Rossuma027efa1997-05-05 20:56:21 +0000540 if (tstate->sys_tracefunc != NULL) {
541 /* tstate->sys_tracefunc, if defined, is a function that
542 will be called on *every* entry to a code block.
Guido van Rossum96a42c81992-01-12 02:29:51 +0000543 Its return value, if not None, is a function that
544 will be called at the start of each executed line
545 of code. (Actually, the function must return
546 itself in order to continue tracing.)
547 The trace functions are called with three arguments:
548 a pointer to the current frame, a string indicating
549 why the function is called, and an argument which
550 depends on the situation. The global trace function
551 (sys.trace) is also called whenever an exception
552 is detected. */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000553 if (call_trace(&tstate->sys_tracefunc,
554 &f->f_trace, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000555 Py_None/*XXX how to compute arguments now?*/)) {
Guido van Rossum96a42c81992-01-12 02:29:51 +0000556 /* Trace function raised an error */
Guido van Rossum8c5df061997-01-24 04:19:24 +0000557 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000558 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000559 }
560
Guido van Rossuma027efa1997-05-05 20:56:21 +0000561 if (tstate->sys_profilefunc != NULL) {
562 /* Similar for sys_profilefunc, except it needn't return
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000563 itself and isn't called for "line" events */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000564 if (call_trace(&tstate->sys_profilefunc,
565 (PyObject**)0, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000566 Py_None/*XXX*/)) {
Guido van Rossum8c5df061997-01-24 04:19:24 +0000567 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000568 }
569 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000570
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000571 if (++tstate->recursion_depth > recursion_limit) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000572 --tstate->recursion_depth;
573 PyErr_SetString(PyExc_RuntimeError,
574 "Maximum recursion depth exceeded");
575 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +0000576 Py_DECREF(f);
Guido van Rossum8861b741996-07-30 16:49:37 +0000577 return NULL;
578 }
579
Guido van Rossumd076c731998-10-07 19:42:25 +0000580 _PyCode_GETCODEPTR(co, &first_instr);
581 next_instr = first_instr;
Guido van Rossum374a9221991-04-04 10:40:29 +0000582 stack_pointer = f->f_valuestack;
583
Guido van Rossum374a9221991-04-04 10:40:29 +0000584 why = WHY_NOT;
585 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000586 x = Py_None; /* Not a reference, just anything non-NULL */
Guido van Rossum374a9221991-04-04 10:40:29 +0000587
588 for (;;) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000589 /* Do periodic things. Doing this every time through
590 the loop would add too much overhead, so we do it
591 only every Nth instruction. We also do it if
592 ``things_to_do'' is set, i.e. when an asynchronous
593 event needs attention (e.g. a signal handler or
594 async I/O handler); see Py_AddPendingCall() and
595 Py_MakePendingCalls() above. */
Guido van Rossuma9672091994-09-14 13:31:22 +0000596
Guido van Rossuma027efa1997-05-05 20:56:21 +0000597 if (things_to_do || --tstate->ticker < 0) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598 tstate->ticker = tstate->interp->checkinterval;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000599 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000600 if (Py_MakePendingCalls() < 0) {
601 why = WHY_EXCEPTION;
602 goto on_error;
603 }
604 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000605#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000606 /* If we have true signals, the signal handler
607 will call Py_AddPendingCall() so we don't
608 have to call sigcheck(). On the Mac and
609 DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000610 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000611 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000612 goto on_error;
613 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000614#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000615
Guido van Rossume59214e1994-08-30 08:01:59 +0000616#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000617 if (interpreter_lock) {
618 /* Give another thread a chance */
619
Guido van Rossum25ce5661997-08-02 03:10:38 +0000620 if (PyThreadState_Swap(NULL) != tstate)
621 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000622 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000623
624 /* Other threads may run now */
625
Guido van Rossum65d5b571998-12-21 19:32:43 +0000626 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000627 if (PyThreadState_Swap(tstate) != NULL)
628 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000629 }
630#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000631 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000632
Guido van Rossum374a9221991-04-04 10:40:29 +0000633 /* Extract opcode and argument */
Guido van Rossum99bec951992-09-03 20:29:45 +0000634
Guido van Rossum408027e1996-12-30 16:17:54 +0000635#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum99bec951992-09-03 20:29:45 +0000636 f->f_lasti = INSTR_OFFSET();
637#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000638
639 opcode = NEXTOP();
640 if (HAS_ARG(opcode))
641 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000642 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000643#ifdef DYNAMIC_EXECUTION_PROFILE
644#ifdef DXPAIRS
645 dxpairs[lastopcode][opcode]++;
646 lastopcode = opcode;
647#endif
648 dxp[opcode]++;
649#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000650
Guido van Rossum96a42c81992-01-12 02:29:51 +0000651#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000652 /* Instruction tracing */
653
Guido van Rossum96a42c81992-01-12 02:29:51 +0000654 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000655 if (HAS_ARG(opcode)) {
656 printf("%d: %d, %d\n",
657 (int) (INSTR_OFFSET() - 3),
658 opcode, oparg);
659 }
660 else {
661 printf("%d: %d\n",
662 (int) (INSTR_OFFSET() - 1), opcode);
663 }
664 }
665#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000666 /* Main switch on opcode */
667
668 switch (opcode) {
669
670 /* BEWARE!
671 It is essential that any operation that fails sets either
672 x to NULL, err to nonzero, or why to anything but WHY_NOT,
673 and that no operation that succeeds does this! */
674
675 /* case STOP_CODE: this is an error! */
676
677 case POP_TOP:
678 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000679 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000680 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000681
682 case ROT_TWO:
683 v = POP();
684 w = POP();
685 PUSH(v);
686 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000687 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000688
689 case ROT_THREE:
690 v = POP();
691 w = POP();
692 x = POP();
693 PUSH(v);
694 PUSH(x);
695 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000696 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000697
Thomas Wouters434d0822000-08-24 20:11:32 +0000698 case ROT_FOUR:
699 u = POP();
700 v = POP();
701 w = POP();
702 x = POP();
703 PUSH(u);
704 PUSH(x);
705 PUSH(w);
706 PUSH(v);
707 continue;
708
Guido van Rossum374a9221991-04-04 10:40:29 +0000709 case DUP_TOP:
710 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000711 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000712 PUSH(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000713 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000714
Thomas Wouters434d0822000-08-24 20:11:32 +0000715 case DUP_TOPX:
716 switch (oparg) {
717 case 5:
718 case 4:
719 case 3:
720 case 2:
721 case 1:
722 x = POP();
723 if (oparg == 1) break;
724 w = POP();
725 if (oparg == 2) break;
726 v = POP();
727 if (oparg == 3) break;
728 u = POP();
729 if (oparg == 4) break;
730 t = POP();
731 break;
732 default:
733 fprintf(stderr, "Invalid argument to DUP_TOPX: %d!\n", oparg);
734 PyErr_SetString(PyExc_SystemError,
735 "invalid argument to DUP_TOPX");
736 x = NULL;
737 }
738 if (x == NULL)
739 break;
740 switch (oparg) {
741 case 5: PUSH(t);
742 Py_INCREF(t); /* Fallthrough */
743 case 4: PUSH(u);
744 Py_INCREF(u); /* Fallthrough */
745 case 3: PUSH(v);
746 Py_INCREF(v); /* Fallthrough */
747 case 2: PUSH(w);
748 Py_INCREF(w); /* Fallthrough */
749 case 1: PUSH(x);
750 Py_INCREF(x); /* Fallthrough */
751 }
752 switch (oparg) {
753 case 5: PUSH(t); /* Fallthrough */
754 case 4: PUSH(u); /* Fallthrough */
755 case 3: PUSH(v); /* Fallthrough */
756 case 2: PUSH(w); /* Fallthrough */
757 case 1: PUSH(x); /* Fallthrough */
758 }
759 continue;
760
Guido van Rossum374a9221991-04-04 10:40:29 +0000761 case UNARY_POSITIVE:
762 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000763 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000764 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000765 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000766 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000767 break;
768
769 case UNARY_NEGATIVE:
770 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000771 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000772 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000773 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000774 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000775 break;
776
777 case UNARY_NOT:
778 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000779 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000780 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000781 if (err == 0) {
782 Py_INCREF(Py_True);
783 PUSH(Py_True);
784 continue;
785 }
786 else if (err > 0) {
787 Py_INCREF(Py_False);
788 PUSH(Py_False);
789 err = 0;
790 continue;
791 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000792 break;
793
794 case UNARY_CONVERT:
795 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000796 x = PyObject_Repr(v);
797 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000798 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000799 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000800 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000801
802 case UNARY_INVERT:
803 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000804 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000805 Py_DECREF(v);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000806 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000807 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000808 break;
Guido van Rossum374a9221991-04-04 10:40:29 +0000809
Guido van Rossum50564e81996-01-12 01:13:16 +0000810 case BINARY_POWER:
811 w = POP();
812 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000813 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000814 Py_DECREF(v);
815 Py_DECREF(w);
Guido van Rossum50564e81996-01-12 01:13:16 +0000816 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000817 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000818 break;
819
Guido van Rossum374a9221991-04-04 10:40:29 +0000820 case BINARY_MULTIPLY:
821 w = POP();
822 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000823 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000824 Py_DECREF(v);
825 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000826 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000827 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000828 break;
829
830 case BINARY_DIVIDE:
831 w = POP();
832 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000833 x = PyNumber_Divide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000834 Py_DECREF(v);
835 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000836 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000837 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000838 break;
839
840 case BINARY_MODULO:
841 w = POP();
842 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000843 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000844 Py_DECREF(v);
845 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000846 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000847 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000848 break;
849
850 case BINARY_ADD:
851 w = POP();
852 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000853 if (PyInt_Check(v) && PyInt_Check(w)) {
854 /* INLINE: int + int */
855 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000856 a = PyInt_AS_LONG(v);
857 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000858 i = a + b;
859 if ((i^a) < 0 && (i^b) < 0) {
860 PyErr_SetString(PyExc_OverflowError,
861 "integer addition");
862 x = NULL;
863 }
864 else
865 x = PyInt_FromLong(i);
866 }
867 else
868 x = PyNumber_Add(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000869 Py_DECREF(v);
870 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000871 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000872 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000873 break;
874
875 case BINARY_SUBTRACT:
876 w = POP();
877 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000878 if (PyInt_Check(v) && PyInt_Check(w)) {
879 /* INLINE: int - int */
880 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000881 a = PyInt_AS_LONG(v);
882 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000883 i = a - b;
884 if ((i^a) < 0 && (i^~b) < 0) {
885 PyErr_SetString(PyExc_OverflowError,
886 "integer subtraction");
887 x = NULL;
888 }
889 else
890 x = PyInt_FromLong(i);
891 }
892 else
893 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000894 Py_DECREF(v);
895 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000896 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000897 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000898 break;
899
900 case BINARY_SUBSCR:
901 w = POP();
902 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000903 if (PyList_Check(v) && PyInt_Check(w)) {
904 /* INLINE: list[int] */
905 long i = PyInt_AsLong(w);
906 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +0000907 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +0000908 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +0000909 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000910 PyErr_SetString(PyExc_IndexError,
911 "list index out of range");
912 x = NULL;
913 }
914 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +0000915 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +0000916 Py_INCREF(x);
917 }
918 }
919 else
920 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000921 Py_DECREF(v);
922 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000923 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000924 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000925 break;
926
Guido van Rossum7928cd71991-10-24 14:59:31 +0000927 case BINARY_LSHIFT:
928 w = POP();
929 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000930 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000931 Py_DECREF(v);
932 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000933 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000934 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000935 break;
936
937 case BINARY_RSHIFT:
938 w = POP();
939 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000940 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000941 Py_DECREF(v);
942 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000943 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000944 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000945 break;
946
947 case BINARY_AND:
948 w = POP();
949 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000950 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000951 Py_DECREF(v);
952 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000953 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000954 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000955 break;
956
957 case BINARY_XOR:
958 w = POP();
959 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000960 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000961 Py_DECREF(v);
962 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000963 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000964 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000965 break;
966
967 case BINARY_OR:
968 w = POP();
969 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000970 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000971 Py_DECREF(v);
972 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000973 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000974 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000975 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000976
977 case INPLACE_POWER:
978 w = POP();
979 v = POP();
980 x = PyNumber_InPlacePower(v, w, Py_None);
981 Py_DECREF(v);
982 Py_DECREF(w);
983 PUSH(x);
984 if (x != NULL) continue;
985 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000986
Thomas Wouters434d0822000-08-24 20:11:32 +0000987 case INPLACE_MULTIPLY:
988 w = POP();
989 v = POP();
990 x = PyNumber_InPlaceMultiply(v, w);
991 Py_DECREF(v);
992 Py_DECREF(w);
993 PUSH(x);
994 if (x != NULL) continue;
995 break;
996
997 case INPLACE_DIVIDE:
998 w = POP();
999 v = POP();
1000 x = PyNumber_InPlaceDivide(v, w);
1001 Py_DECREF(v);
1002 Py_DECREF(w);
1003 PUSH(x);
1004 if (x != NULL) continue;
1005 break;
1006
1007 case INPLACE_MODULO:
1008 w = POP();
1009 v = POP();
1010 x = PyNumber_InPlaceRemainder(v, w);
1011 Py_DECREF(v);
1012 Py_DECREF(w);
1013 PUSH(x);
1014 if (x != NULL) continue;
1015 break;
1016
1017 case INPLACE_ADD:
1018 w = POP();
1019 v = POP();
1020 if (PyInt_Check(v) && PyInt_Check(w)) {
1021 /* INLINE: int + int */
1022 register long a, b, i;
1023 a = PyInt_AS_LONG(v);
1024 b = PyInt_AS_LONG(w);
1025 i = a + b;
1026 if ((i^a) < 0 && (i^b) < 0) {
1027 PyErr_SetString(PyExc_OverflowError,
1028 "integer addition");
1029 x = NULL;
1030 }
1031 else
1032 x = PyInt_FromLong(i);
1033 }
1034 else
1035 x = PyNumber_InPlaceAdd(v, w);
1036 Py_DECREF(v);
1037 Py_DECREF(w);
1038 PUSH(x);
1039 if (x != NULL) continue;
1040 break;
1041
1042 case INPLACE_SUBTRACT:
1043 w = POP();
1044 v = POP();
1045 if (PyInt_Check(v) && PyInt_Check(w)) {
1046 /* INLINE: int - int */
1047 register long a, b, i;
1048 a = PyInt_AS_LONG(v);
1049 b = PyInt_AS_LONG(w);
1050 i = a - b;
1051 if ((i^a) < 0 && (i^~b) < 0) {
1052 PyErr_SetString(PyExc_OverflowError,
1053 "integer subtraction");
1054 x = NULL;
1055 }
1056 else
1057 x = PyInt_FromLong(i);
1058 }
1059 else
1060 x = PyNumber_InPlaceSubtract(v, w);
1061 Py_DECREF(v);
1062 Py_DECREF(w);
1063 PUSH(x);
1064 if (x != NULL) continue;
1065 break;
1066
1067 case INPLACE_LSHIFT:
1068 w = POP();
1069 v = POP();
1070 x = PyNumber_InPlaceLshift(v, w);
1071 Py_DECREF(v);
1072 Py_DECREF(w);
1073 PUSH(x);
1074 if (x != NULL) continue;
1075 break;
1076
1077 case INPLACE_RSHIFT:
1078 w = POP();
1079 v = POP();
1080 x = PyNumber_InPlaceRshift(v, w);
1081 Py_DECREF(v);
1082 Py_DECREF(w);
1083 PUSH(x);
1084 if (x != NULL) continue;
1085 break;
1086
1087 case INPLACE_AND:
1088 w = POP();
1089 v = POP();
1090 x = PyNumber_InPlaceAnd(v, w);
1091 Py_DECREF(v);
1092 Py_DECREF(w);
1093 PUSH(x);
1094 if (x != NULL) continue;
1095 break;
1096
1097 case INPLACE_XOR:
1098 w = POP();
1099 v = POP();
1100 x = PyNumber_InPlaceXor(v, w);
1101 Py_DECREF(v);
1102 Py_DECREF(w);
1103 PUSH(x);
1104 if (x != NULL) continue;
1105 break;
1106
1107 case INPLACE_OR:
1108 w = POP();
1109 v = POP();
1110 x = PyNumber_InPlaceOr(v, w);
1111 Py_DECREF(v);
1112 Py_DECREF(w);
1113 PUSH(x);
1114 if (x != NULL) continue;
1115 break;
1116
Guido van Rossum374a9221991-04-04 10:40:29 +00001117 case SLICE+0:
1118 case SLICE+1:
1119 case SLICE+2:
1120 case SLICE+3:
1121 if ((opcode-SLICE) & 2)
1122 w = POP();
1123 else
1124 w = NULL;
1125 if ((opcode-SLICE) & 1)
1126 v = POP();
1127 else
1128 v = NULL;
1129 u = POP();
1130 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001131 Py_DECREF(u);
1132 Py_XDECREF(v);
1133 Py_XDECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001134 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001135 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001136 break;
1137
1138 case STORE_SLICE+0:
1139 case STORE_SLICE+1:
1140 case STORE_SLICE+2:
1141 case STORE_SLICE+3:
1142 if ((opcode-STORE_SLICE) & 2)
1143 w = POP();
1144 else
1145 w = NULL;
1146 if ((opcode-STORE_SLICE) & 1)
1147 v = POP();
1148 else
1149 v = NULL;
1150 u = POP();
1151 t = POP();
1152 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001153 Py_DECREF(t);
1154 Py_DECREF(u);
1155 Py_XDECREF(v);
1156 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001157 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001158 break;
1159
1160 case DELETE_SLICE+0:
1161 case DELETE_SLICE+1:
1162 case DELETE_SLICE+2:
1163 case DELETE_SLICE+3:
1164 if ((opcode-DELETE_SLICE) & 2)
1165 w = POP();
1166 else
1167 w = NULL;
1168 if ((opcode-DELETE_SLICE) & 1)
1169 v = POP();
1170 else
1171 v = NULL;
1172 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001173 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001174 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001175 Py_DECREF(u);
1176 Py_XDECREF(v);
1177 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001178 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001179 break;
1180
1181 case STORE_SUBSCR:
1182 w = POP();
1183 v = POP();
1184 u = POP();
1185 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001186 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001187 Py_DECREF(u);
1188 Py_DECREF(v);
1189 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001190 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001191 break;
1192
1193 case DELETE_SUBSCR:
1194 w = POP();
1195 v = POP();
1196 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001197 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001198 Py_DECREF(v);
1199 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001200 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001201 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001202
Guido van Rossum374a9221991-04-04 10:40:29 +00001203 case PRINT_EXPR:
1204 v = POP();
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001205 /* Print value except if None */
1206 /* After printing, also assign to '_' */
1207 /* Before, set '_' to None to avoid recursion */
Guido van Rossumb209a111997-04-29 18:18:01 +00001208 if (v != Py_None &&
Guido van Rossuma027efa1997-05-05 20:56:21 +00001209 (err = PyDict_SetItemString(
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001210 f->f_builtins, "_", Py_None)) == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001211 err = Py_FlushLine();
Guido van Rossumc12da691997-07-17 23:12:42 +00001212 if (err == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001213 x = PySys_GetObject("stdout");
Guido van Rossum8f183201997-12-31 05:53:15 +00001214 if (x == NULL) {
1215 PyErr_SetString(
1216 PyExc_RuntimeError,
1217 "lost sys.stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00001218 err = -1;
Guido van Rossum8f183201997-12-31 05:53:15 +00001219 }
Guido van Rossumbe270261997-05-22 22:26:18 +00001220 }
1221 if (err == 0)
1222 err = PyFile_WriteObject(v, x, 0);
1223 if (err == 0) {
1224 PyFile_SoftSpace(x, 1);
1225 err = Py_FlushLine();
1226 }
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001227 if (err == 0) {
1228 err = PyDict_SetItemString(
1229 f->f_builtins, "_", v);
1230 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001231 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001232 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001233 break;
1234
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001235 case PRINT_ITEM_TO:
1236 w = stream = POP();
1237 /* fall through to PRINT_ITEM */
1238
Guido van Rossum374a9221991-04-04 10:40:29 +00001239 case PRINT_ITEM:
1240 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001241 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001242 w = PySys_GetObject("stdout");
1243 if (w == NULL) {
1244 PyErr_SetString(PyExc_RuntimeError,
1245 "lost sys.stdout");
1246 err = -1;
1247 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001248 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001249 if (w != NULL && PyFile_SoftSpace(w, 1))
Guido van Rossumbe270261997-05-22 22:26:18 +00001250 err = PyFile_WriteString(" ", w);
1251 if (err == 0)
1252 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Guido van Rossumb209a111997-04-29 18:18:01 +00001253 if (err == 0 && PyString_Check(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +00001254 /* XXX move into writeobject() ? */
Guido van Rossumb209a111997-04-29 18:18:01 +00001255 char *s = PyString_AsString(v);
1256 int len = PyString_Size(v);
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001257 if (len > 0 &&
1258 isspace(Py_CHARMASK(s[len-1])) &&
Guido van Rossumc6004111993-11-05 10:22:19 +00001259 s[len-1] != ' ')
Guido van Rossumb209a111997-04-29 18:18:01 +00001260 PyFile_SoftSpace(w, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +00001261 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001262 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001263 Py_XDECREF(stream);
1264 stream = NULL;
1265 if (err == 0)
1266 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001267 break;
1268
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001269 case PRINT_NEWLINE_TO:
1270 w = stream = POP();
1271 /* fall through to PRINT_NEWLINE */
1272
Guido van Rossum374a9221991-04-04 10:40:29 +00001273 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001274 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001275 w = PySys_GetObject("stdout");
1276 if (w == NULL)
1277 PyErr_SetString(PyExc_RuntimeError,
1278 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001279 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001280 if (w != NULL) {
1281 err = PyFile_WriteString("\n", w);
1282 if (err == 0)
1283 PyFile_SoftSpace(w, 0);
1284 }
1285 Py_XDECREF(stream);
1286 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001287 break;
1288
Thomas Wouters434d0822000-08-24 20:11:32 +00001289
1290#ifdef CASE_TOO_BIG
1291 default: switch (opcode) {
1292#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001293 case BREAK_LOOP:
1294 why = WHY_BREAK;
1295 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001296
Guido van Rossumf10570b1995-07-07 22:53:21 +00001297 case RAISE_VARARGS:
1298 u = v = w = NULL;
1299 switch (oparg) {
1300 case 3:
1301 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001302 /* Fallthrough */
1303 case 2:
1304 v = POP(); /* value */
1305 /* Fallthrough */
1306 case 1:
1307 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001308 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001309 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001310 break;
1311 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001312 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001313 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001314 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001315 break;
1316 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001317 break;
1318
1319 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001320 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001321 PyErr_SetString(PyExc_SystemError,
1322 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001323 break;
1324 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001325 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001326 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001327 break;
1328
1329 case RETURN_VALUE:
1330 retval = POP();
1331 why = WHY_RETURN;
1332 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001333
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001334 case EXEC_STMT:
1335 w = POP();
1336 v = POP();
1337 u = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001338 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001339 Py_DECREF(u);
1340 Py_DECREF(v);
1341 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001342 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001343
Guido van Rossum374a9221991-04-04 10:40:29 +00001344 case POP_BLOCK:
1345 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001346 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001347 while (STACK_LEVEL() > b->b_level) {
1348 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001349 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001350 }
1351 }
1352 break;
1353
1354 case END_FINALLY:
1355 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001356 if (PyInt_Check(v)) {
1357 why = (enum why_code) PyInt_AsLong(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001358 if (why == WHY_RETURN)
1359 retval = POP();
1360 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001361 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001362 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001363 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001364 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001365 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001366 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001367 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001368 else if (v != Py_None) {
1369 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001370 "'finally' pops bad exception");
1371 why = WHY_EXCEPTION;
1372 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001373 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001374 break;
1375
1376 case BUILD_CLASS:
Guido van Rossum25831651993-05-19 14:50:45 +00001377 u = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001378 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +00001379 w = POP();
1380 x = build_class(u, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001381 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001382 Py_DECREF(u);
1383 Py_DECREF(v);
1384 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001385 break;
1386
1387 case STORE_NAME:
1388 w = GETNAMEV(oparg);
1389 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001390 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001391 PyErr_SetString(PyExc_SystemError,
1392 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001393 break;
1394 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001395 err = PyDict_SetItem(x, w, v);
1396 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001397 break;
1398
1399 case DELETE_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001400 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001401 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001402 PyErr_SetString(PyExc_SystemError,
1403 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001404 break;
1405 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001406 if ((err = PyDict_DelItem(x, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001407 format_exc_check_arg(PyExc_NameError,
1408 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001409 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001410
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001411 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001412 v = POP();
Barry Warsawe42b18f1997-08-25 22:13:04 +00001413 if (PyTuple_Check(v)) {
1414 if (PyTuple_Size(v) != oparg) {
1415 PyErr_SetString(PyExc_ValueError,
1416 "unpack tuple of wrong size");
1417 why = WHY_EXCEPTION;
1418 }
1419 else {
1420 for (; --oparg >= 0; ) {
1421 w = PyTuple_GET_ITEM(v, oparg);
1422 Py_INCREF(w);
1423 PUSH(w);
1424 }
1425 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001426 }
Barry Warsawe42b18f1997-08-25 22:13:04 +00001427 else if (PyList_Check(v)) {
1428 if (PyList_Size(v) != oparg) {
1429 PyErr_SetString(PyExc_ValueError,
1430 "unpack list of wrong size");
1431 why = WHY_EXCEPTION;
1432 }
1433 else {
1434 for (; --oparg >= 0; ) {
1435 w = PyList_GET_ITEM(v, oparg);
1436 Py_INCREF(w);
1437 PUSH(w);
1438 }
1439 }
1440 }
1441 else if (PySequence_Check(v)) {
1442 if (unpack_sequence(v, oparg,
1443 stack_pointer + oparg))
1444 stack_pointer += oparg;
1445 else
1446 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 }
1448 else {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001449 PyErr_SetString(PyExc_TypeError,
1450 "unpack non-sequence");
1451 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001453 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001454 break;
1455
1456 case STORE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001457 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001458 v = POP();
1459 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001460 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1461 Py_DECREF(v);
1462 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001463 break;
1464
1465 case DELETE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001466 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001467 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001468 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1469 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001470 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 break;
1472
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001473 case STORE_GLOBAL:
1474 w = GETNAMEV(oparg);
1475 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001476 err = PyDict_SetItem(f->f_globals, w, v);
1477 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001478 break;
1479
1480 case DELETE_GLOBAL:
1481 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001482 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001483 format_exc_check_arg(
1484 PyExc_NameError, NAME_ERROR_MSG ,w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001485 break;
1486
Guido van Rossum374a9221991-04-04 10:40:29 +00001487 case LOAD_CONST:
1488 x = GETCONST(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001489 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001490 PUSH(x);
1491 break;
1492
1493 case LOAD_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001494 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001495 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001496 PyErr_SetString(PyExc_SystemError,
1497 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001498 break;
1499 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001500 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001501 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001502 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001503 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001504 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001506 format_exc_check_arg(
1507 PyExc_NameError,
1508 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001509 break;
1510 }
1511 }
1512 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001513 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 PUSH(x);
1515 break;
1516
1517 case LOAD_GLOBAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001518 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001519 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001521 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001522 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001523 format_exc_check_arg(
1524 PyExc_NameError,
1525 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001526 break;
1527 }
1528 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001529 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001530 PUSH(x);
1531 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001532
Guido van Rossum9bfef441993-03-29 10:43:31 +00001533 case LOAD_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001534 x = GETLOCAL(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001535 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001536 format_exc_check_arg(
1537 PyExc_UnboundLocalError,
1538 UNBOUNDLOCAL_ERROR_MSG,
1539 PyTuple_GetItem(co->co_varnames, oparg)
1540 );
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001541 break;
1542 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001543 Py_INCREF(x);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001544 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001545 if (x != NULL) continue;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001546 break;
1547
1548 case STORE_FAST:
Guido van Rossum25831651993-05-19 14:50:45 +00001549 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001550 SETLOCAL(oparg, v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001551 continue;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001552
1553 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001554 x = GETLOCAL(oparg);
1555 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001556 format_exc_check_arg(
1557 PyExc_UnboundLocalError,
1558 UNBOUNDLOCAL_ERROR_MSG,
1559 PyTuple_GetItem(co->co_varnames, oparg)
1560 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001561 break;
1562 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001563 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001564 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001565
1566 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001567 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 if (x != NULL) {
1569 for (; --oparg >= 0;) {
1570 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001571 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001572 }
1573 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001574 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001575 }
1576 break;
1577
1578 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001579 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001580 if (x != NULL) {
1581 for (; --oparg >= 0;) {
1582 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001583 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 }
1585 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001586 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001587 }
1588 break;
1589
1590 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001591 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001592 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001593 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001594 break;
1595
1596 case LOAD_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001597 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001598 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001599 x = PyObject_GetAttr(v, w);
1600 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001601 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001602 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001603 break;
1604
1605 case COMPARE_OP:
1606 w = POP();
1607 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +00001608 if (PyInt_Check(v) && PyInt_Check(w)) {
1609 /* INLINE: cmp(int, int) */
1610 register long a, b;
1611 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001612 a = PyInt_AS_LONG(v);
1613 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001614 switch (oparg) {
1615 case LT: res = a < b; break;
1616 case LE: res = a <= b; break;
1617 case EQ: res = a == b; break;
1618 case NE: res = a != b; break;
1619 case GT: res = a > b; break;
1620 case GE: res = a >= b; break;
1621 case IS: res = v == w; break;
1622 case IS_NOT: res = v != w; break;
1623 default: goto slow_compare;
1624 }
1625 x = res ? Py_True : Py_False;
1626 Py_INCREF(x);
1627 }
1628 else {
1629 slow_compare:
1630 x = cmp_outcome(oparg, v, w);
1631 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001632 Py_DECREF(v);
1633 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001634 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001635 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001636 break;
1637
1638 case IMPORT_NAME:
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001639 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001640 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001641 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001642 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001643 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001644 break;
1645 }
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00001646 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001647 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001648 w,
1649 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001650 f->f_locals == NULL ?
1651 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001652 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001653 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001654 if (w == NULL) {
1655 x = NULL;
1656 break;
1657 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001658 x = PyEval_CallObject(x, w);
1659 Py_DECREF(w);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001660 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001661 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 break;
1663
Thomas Wouters52152252000-08-17 22:55:00 +00001664 case IMPORT_STAR:
1665 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001666 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001667 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001668 PyErr_SetString(PyExc_SystemError,
1669 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001670 break;
1671 }
Thomas Wouters52152252000-08-17 22:55:00 +00001672 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001673 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001674 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001675 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001676 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001677
Thomas Wouters52152252000-08-17 22:55:00 +00001678 case IMPORT_FROM:
1679 w = GETNAMEV(oparg);
1680 v = TOP();
1681 x = import_from(v, w);
1682 PUSH(x);
1683 if (x != NULL) continue;
1684 break;
1685
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 case JUMP_FORWARD:
1687 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001688 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001689
1690 case JUMP_IF_FALSE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001691 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001692 if (err > 0)
1693 err = 0;
1694 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001696 else
1697 break;
1698 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001699
1700 case JUMP_IF_TRUE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001701 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001702 if (err > 0) {
1703 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001704 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001705 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001706 else if (err == 0)
1707 ;
1708 else
1709 break;
1710 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001711
1712 case JUMP_ABSOLUTE:
1713 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001714 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001715
1716 case FOR_LOOP:
1717 /* for v in s: ...
1718 On entry: stack contains s, i.
1719 On exit: stack contains s, i+1, s[i];
1720 but if loop exhausted:
1721 s, i are popped, and we jump */
1722 w = POP(); /* Loop index */
1723 v = POP(); /* Sequence object */
1724 u = loop_subscript(v, w);
1725 if (u != NULL) {
1726 PUSH(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001727 x = PyInt_FromLong(PyInt_AsLong(w)+1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001728 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001729 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 PUSH(u);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001731 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001732 }
1733 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001734 Py_DECREF(v);
1735 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001736 /* A NULL can mean "s exhausted"
1737 but also an error: */
Guido van Rossumb209a111997-04-29 18:18:01 +00001738 if (PyErr_Occurred())
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 why = WHY_EXCEPTION;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001740 else {
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001742 continue;
1743 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001744 }
1745 break;
1746
1747 case SETUP_LOOP:
1748 case SETUP_EXCEPT:
1749 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00001750 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001752 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001753
1754 case SET_LINENO:
Guido van Rossum96a42c81992-01-12 02:29:51 +00001755#ifdef LLTRACE
1756 if (lltrace)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001757 printf("--- %s:%d \n", filename, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001758#endif
Guido van Rossum96a42c81992-01-12 02:29:51 +00001759 f->f_lineno = oparg;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001760 if (f->f_trace == NULL)
1761 continue;
1762 /* Trace each line of code reached */
1763 f->f_lasti = INSTR_OFFSET();
1764 err = call_trace(&f->f_trace, &f->f_trace,
Guido van Rossumb209a111997-04-29 18:18:01 +00001765 f, "line", Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001767
1768 case CALL_FUNCTION:
Jeremy Hylton76901512000-03-28 23:49:17 +00001769 case CALL_FUNCTION_VAR:
1770 case CALL_FUNCTION_KW:
1771 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00001772 {
Jeremy Hylton76901512000-03-28 23:49:17 +00001773 int na = oparg & 0xff;
1774 int nk = (oparg>>8) & 0xff;
1775 int flags = (opcode - CALL_FUNCTION) & 3;
1776 int n = na + 2*nk + (flags & 1) + ((flags >> 1) & 1);
1777 PyObject **pfunc = stack_pointer - n - 1;
1778 PyObject *func = *pfunc;
1779 PyObject *self = NULL;
1780 PyObject *class = NULL;
1781 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
1782 if (PyMethod_Check(func)) {
1783 self = PyMethod_Self(func);
1784 class = PyMethod_Class(func);
1785 func = PyMethod_Function(func);
1786 Py_INCREF(func);
1787 if (self != NULL) {
1788 Py_INCREF(self);
1789 Py_DECREF(*pfunc);
1790 *pfunc = self;
1791 na++;
1792 n++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001793 }
1794 else {
Jeremy Hylton76901512000-03-28 23:49:17 +00001795 /* Unbound methods must be called with an
1796 instance of the class (or a derived
1797 class) as first argument */
1798 if (na > 0 && (self = stack_pointer[-n]) != NULL
1799 && PyInstance_Check(self)
1800 && PyClass_IsSubclass((PyObject *)
1801 (((PyInstanceObject *)self)->in_class),
1802 class))
1803 /* Handy-dandy */ ;
1804 else {
1805 PyErr_SetString(PyExc_TypeError,
1806 "unbound method must be called with class instance 1st argument");
1807 x = NULL;
1808 break;
1809 }
1810 }
1811 }
1812 else
1813 Py_INCREF(func);
1814 if (PyFunction_Check(func) && flags == 0) {
1815 PyObject *co = PyFunction_GetCode(func);
1816 PyObject *globals = PyFunction_GetGlobals(func);
1817 PyObject *argdefs = PyFunction_GetDefaults(func);
1818 PyObject **d;
1819 int nd;
1820 if (argdefs != NULL) {
1821 d = &PyTuple_GET_ITEM(argdefs, 0);
1822 nd = ((PyTupleObject *)argdefs)->ob_size;
1823 }
1824 else {
1825 d = NULL;
1826 nd = 0;
1827 }
1828 x = eval_code2((PyCodeObject *)co, globals,
1829 (PyObject *)NULL, stack_pointer-n, na,
1830 stack_pointer-2*nk, nk, d, nd,
1831 class);
1832 }
1833 else {
1834 int nstar = 0;
Jeremy Hylton387b1012000-03-31 01:22:54 +00001835 PyObject *callargs;
Jeremy Hylton76901512000-03-28 23:49:17 +00001836 PyObject *stararg = 0;
1837 PyObject *kwdict = NULL;
1838 if (flags & 2) {
1839 kwdict = POP();
1840 if (!PyDict_Check(kwdict)) {
1841 PyErr_SetString(PyExc_TypeError,
1842 "** argument must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00001843 goto extcall_fail;
Jeremy Hylton76901512000-03-28 23:49:17 +00001844 }
1845 }
1846 if (flags & 1) {
1847 stararg = POP();
1848 if (!PySequence_Check(stararg)) {
1849 PyErr_SetString(PyExc_TypeError,
1850 "* argument must be a sequence");
Guido van Rossum25826c92000-04-21 21:17:39 +00001851 goto extcall_fail;
Jeremy Hylton76901512000-03-28 23:49:17 +00001852 }
Jeremy Hylton074c3e62000-03-30 23:55:31 +00001853 /* Convert abstract sequence to concrete tuple */
1854 if (!PyTuple_Check(stararg)) {
1855 PyObject *t = NULL;
1856 t = PySequence_Tuple(stararg);
1857 if (t == NULL) {
Guido van Rossum25826c92000-04-21 21:17:39 +00001858 goto extcall_fail;
Jeremy Hylton074c3e62000-03-30 23:55:31 +00001859 }
1860 Py_DECREF(stararg);
1861 stararg = t;
1862 }
1863 nstar = PyTuple_GET_SIZE(stararg);
Barry Warsaw4961ef72000-03-29 18:30:03 +00001864 if (nstar < 0) {
Guido van Rossum25826c92000-04-21 21:17:39 +00001865 goto extcall_fail;
Barry Warsaw4961ef72000-03-29 18:30:03 +00001866 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001867 }
1868 if (nk > 0) {
1869 if (kwdict == NULL) {
1870 kwdict = PyDict_New();
1871 if (kwdict == NULL) {
Guido van Rossum25826c92000-04-21 21:17:39 +00001872 goto extcall_fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001873 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001874 }
Jeremy Hylton074c3e62000-03-30 23:55:31 +00001875 else {
1876 PyObject *d = PyDict_Copy(kwdict);
1877 if (d == NULL) {
Guido van Rossum25826c92000-04-21 21:17:39 +00001878 goto extcall_fail;
Jeremy Hylton074c3e62000-03-30 23:55:31 +00001879 }
1880 Py_DECREF(kwdict);
1881 kwdict = d;
1882 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001883 err = 0;
1884 while (--nk >= 0) {
1885 PyObject *value = POP();
1886 PyObject *key = POP();
1887 if (PyDict_GetItem(kwdict, key) != NULL) {
1888 err = 1;
1889 PyErr_Format(PyExc_TypeError,
1890 "keyword parameter redefined: %.400s",
1891 PyString_AsString(key));
Jeremy Hylton387b1012000-03-31 01:22:54 +00001892 Py_DECREF(key);
1893 Py_DECREF(value);
Guido van Rossum25826c92000-04-21 21:17:39 +00001894 goto extcall_fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001895 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001896 err = PyDict_SetItem(kwdict, key, value);
1897 Py_DECREF(key);
1898 Py_DECREF(value);
1899 if (err)
1900 break;
1901 }
1902 if (err) {
Guido van Rossum25826c92000-04-21 21:17:39 +00001903 extcall_fail:
1904 Py_XDECREF(kwdict);
1905 Py_XDECREF(stararg);
1906 Py_DECREF(func);
1907 x=NULL;
Jeremy Hylton76901512000-03-28 23:49:17 +00001908 break;
1909 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001910 }
Jeremy Hylton387b1012000-03-31 01:22:54 +00001911 callargs = PyTuple_New(na + nstar);
1912 if (callargs == NULL) {
Jeremy Hylton76901512000-03-28 23:49:17 +00001913 x = NULL;
1914 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001915 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001916 if (stararg) {
Jeremy Hylton76901512000-03-28 23:49:17 +00001917 int i;
Jeremy Hylton76901512000-03-28 23:49:17 +00001918 for (i = 0; i < nstar; i++) {
1919 PyObject *a = PyTuple_GET_ITEM(stararg, i);
1920 Py_INCREF(a);
Jeremy Hylton387b1012000-03-31 01:22:54 +00001921 PyTuple_SET_ITEM(callargs, na + i, a);
Jeremy Hylton76901512000-03-28 23:49:17 +00001922 }
1923 Py_DECREF(stararg);
1924 }
1925 while (--na >= 0) {
1926 w = POP();
Jeremy Hylton387b1012000-03-31 01:22:54 +00001927 PyTuple_SET_ITEM(callargs, na, w);
Jeremy Hylton76901512000-03-28 23:49:17 +00001928 }
Jeremy Hylton387b1012000-03-31 01:22:54 +00001929 x = PyEval_CallObjectWithKeywords(func,
1930 callargs,
1931 kwdict);
1932 Py_DECREF(callargs);
Jeremy Hylton76901512000-03-28 23:49:17 +00001933 Py_XDECREF(kwdict);
1934 }
1935 Py_DECREF(func);
1936 while (stack_pointer > pfunc) {
1937 w = POP();
1938 Py_DECREF(w);
1939 }
1940 PUSH(x);
1941 if (x != NULL) continue;
1942 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001943 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001944
Guido van Rossum681d79a1995-07-18 14:51:37 +00001945 case MAKE_FUNCTION:
1946 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00001947 x = PyFunction_New(v, f->f_globals);
1948 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001949 /* XXX Maybe this should be a separate opcode? */
1950 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001951 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001952 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001953 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001954 x = NULL;
1955 break;
1956 }
1957 while (--oparg >= 0) {
1958 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001960 }
1961 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001962 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001963 }
1964 PUSH(x);
1965 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001966
1967 case BUILD_SLICE:
1968 if (oparg == 3)
1969 w = POP();
1970 else
1971 w = NULL;
1972 v = POP();
1973 u = POP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00001974 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001975 Py_DECREF(u);
1976 Py_DECREF(v);
1977 Py_XDECREF(w);
Guido van Rossum8861b741996-07-30 16:49:37 +00001978 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001979 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00001980 break;
1981
Fred Drakeef8ace32000-08-24 00:32:09 +00001982 case EXTENDED_ARG:
1983 opcode = NEXTOP();
1984 oparg = oparg<<16 | NEXTARG();
1985 goto dispatch_opcode;
1986 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001987
Guido van Rossum374a9221991-04-04 10:40:29 +00001988 default:
1989 fprintf(stderr,
1990 "XXX lineno: %d, opcode: %d\n",
Guido van Rossum96a42c81992-01-12 02:29:51 +00001991 f->f_lineno, opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00001992 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00001993 why = WHY_EXCEPTION;
1994 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001995
1996#ifdef CASE_TOO_BIG
1997 }
1998#endif
1999
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 } /* switch */
2001
2002 on_error:
2003
2004 /* Quickly continue if no error occurred */
2005
2006 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002007 if (err == 0 && x != NULL) {
2008#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002009 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002010 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002011 fprintf(stderr,
2012 "XXX undetected error\n");
2013 else
2014#endif
2015 continue; /* Normal, fast path */
2016 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002017 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002018 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002019 err = 0;
2020 }
2021
Guido van Rossum374a9221991-04-04 10:40:29 +00002022 /* Double-check exception status */
2023
2024 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002025 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002026 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002027 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002028 why = WHY_EXCEPTION;
2029 }
2030 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002031#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002032 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002033 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002034 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002035 fprintf(stderr,
2036 "XXX undetected error (why=%d)\n",
2037 why);
2038 why = WHY_EXCEPTION;
2039 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002040 }
2041#endif
2042
2043 /* Log traceback info if this is a real exception */
2044
2045 if (why == WHY_EXCEPTION) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00002046 f->f_lasti = INSTR_OFFSET() - 1;
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 if (HAS_ARG(opcode))
Guido van Rossum96a42c81992-01-12 02:29:51 +00002048 f->f_lasti -= 2;
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002050
Guido van Rossume59214e1994-08-30 08:01:59 +00002051 if (f->f_trace)
2052 call_exc_trace(&f->f_trace, &f->f_trace, f);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002053 if (tstate->sys_profilefunc)
2054 call_exc_trace(&tstate->sys_profilefunc,
2055 (PyObject**)0, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002056 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002057
2058 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
2059
2060 if (why == WHY_RERAISE)
2061 why = WHY_EXCEPTION;
2062
2063 /* Unwind stacks if a (pseudo) exception occurred */
2064
2065 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002066 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00002067 while (STACK_LEVEL() > b->b_level) {
2068 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002069 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002070 }
2071 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2072 why = WHY_NOT;
2073 JUMPTO(b->b_handler);
2074 break;
2075 }
2076 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002077 (b->b_type == SETUP_EXCEPT &&
2078 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002079 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002080 PyObject *exc, *val, *tb;
2081 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002082 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002083 val = Py_None;
2084 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002085 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002086 /* Make the raw exception data
2087 available to the handler,
2088 so a program can emulate the
2089 Python main loop. Don't do
2090 this for 'finally'. */
2091 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002092 PyErr_NormalizeException(
2093 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002094 set_exc_info(tstate,
2095 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002096 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002097 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002098 PUSH(val);
2099 PUSH(exc);
2100 }
2101 else {
2102 if (why == WHY_RETURN)
2103 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002104 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002105 PUSH(v);
2106 }
2107 why = WHY_NOT;
2108 JUMPTO(b->b_handler);
2109 break;
2110 }
2111 } /* unwind stack */
2112
2113 /* End the loop if we still have an error (or return) */
2114
2115 if (why != WHY_NOT)
2116 break;
2117
2118 } /* main loop */
2119
2120 /* Pop remaining stack entries */
2121
2122 while (!EMPTY()) {
2123 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002124 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002125 }
2126
Guido van Rossum96a42c81992-01-12 02:29:51 +00002127 if (why != WHY_RETURN)
2128 retval = NULL;
2129
Guido van Rossume59214e1994-08-30 08:01:59 +00002130 if (f->f_trace) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00002131 if (why == WHY_RETURN) {
Guido van Rossume59214e1994-08-30 08:01:59 +00002132 if (call_trace(&f->f_trace, &f->f_trace, f,
2133 "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002134 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002135 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002136 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002137 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002138 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002139 }
2140
Guido van Rossuma027efa1997-05-05 20:56:21 +00002141 if (tstate->sys_profilefunc && why == WHY_RETURN) {
2142 if (call_trace(&tstate->sys_profilefunc, (PyObject**)0,
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002143 f, "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002144 Py_XDECREF(retval);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002145 retval = NULL;
2146 why = WHY_EXCEPTION;
2147 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002148 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002149
Guido van Rossuma027efa1997-05-05 20:56:21 +00002150 reset_exc_info(tstate);
2151
2152 --tstate->recursion_depth;
Guido van Rossum8c5df061997-01-24 04:19:24 +00002153
2154 fail: /* Jump here from prelude on failure */
Guido van Rossum96a42c81992-01-12 02:29:51 +00002155
Guido van Rossum374a9221991-04-04 10:40:29 +00002156 /* Restore previous frame and release the current one */
Guido van Rossum681d79a1995-07-18 14:51:37 +00002157
Guido van Rossuma027efa1997-05-05 20:56:21 +00002158 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +00002159 Py_DECREF(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00002160
Guido van Rossum96a42c81992-01-12 02:29:51 +00002161 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002162}
2163
Guido van Rossuma027efa1997-05-05 20:56:21 +00002164static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002165set_exc_info(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002166{
2167 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002168 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002169
Guido van Rossuma027efa1997-05-05 20:56:21 +00002170 frame = tstate->frame;
2171 if (frame->f_exc_type == NULL) {
2172 /* This frame didn't catch an exception before */
2173 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002174 if (tstate->exc_type == NULL) {
2175 Py_INCREF(Py_None);
2176 tstate->exc_type = Py_None;
2177 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002178 tmp_type = frame->f_exc_type;
2179 tmp_value = frame->f_exc_value;
2180 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002181 Py_XINCREF(tstate->exc_type);
2182 Py_XINCREF(tstate->exc_value);
2183 Py_XINCREF(tstate->exc_traceback);
2184 frame->f_exc_type = tstate->exc_type;
2185 frame->f_exc_value = tstate->exc_value;
2186 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002187 Py_XDECREF(tmp_type);
2188 Py_XDECREF(tmp_value);
2189 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002190 }
2191 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002192 tmp_type = tstate->exc_type;
2193 tmp_value = tstate->exc_value;
2194 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002195 Py_XINCREF(type);
2196 Py_XINCREF(value);
2197 Py_XINCREF(tb);
2198 tstate->exc_type = type;
2199 tstate->exc_value = value;
2200 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002201 Py_XDECREF(tmp_type);
2202 Py_XDECREF(tmp_value);
2203 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002204 /* For b/w compatibility */
2205 PySys_SetObject("exc_type", type);
2206 PySys_SetObject("exc_value", value);
2207 PySys_SetObject("exc_traceback", tb);
2208}
2209
2210static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002211reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002212{
2213 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002214 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002215 frame = tstate->frame;
2216 if (frame->f_exc_type != NULL) {
2217 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002218 tmp_type = tstate->exc_type;
2219 tmp_value = tstate->exc_value;
2220 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002221 Py_XINCREF(frame->f_exc_type);
2222 Py_XINCREF(frame->f_exc_value);
2223 Py_XINCREF(frame->f_exc_traceback);
2224 tstate->exc_type = frame->f_exc_type;
2225 tstate->exc_value = frame->f_exc_value;
2226 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002227 Py_XDECREF(tmp_type);
2228 Py_XDECREF(tmp_value);
2229 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002230 /* For b/w compatibility */
2231 PySys_SetObject("exc_type", frame->f_exc_type);
2232 PySys_SetObject("exc_value", frame->f_exc_value);
2233 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2234 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002235 tmp_type = frame->f_exc_type;
2236 tmp_value = frame->f_exc_value;
2237 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002238 frame->f_exc_type = NULL;
2239 frame->f_exc_value = NULL;
2240 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002241 Py_XDECREF(tmp_type);
2242 Py_XDECREF(tmp_value);
2243 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002244}
2245
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002246/* Logic for the raise statement (too complicated for inlining).
2247 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002248static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002249do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002250{
Guido van Rossumd295f121998-04-09 21:39:57 +00002251 if (type == NULL) {
2252 /* Reraise */
2253 PyThreadState *tstate = PyThreadState_Get();
2254 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2255 value = tstate->exc_value;
2256 tb = tstate->exc_traceback;
2257 Py_XINCREF(type);
2258 Py_XINCREF(value);
2259 Py_XINCREF(tb);
2260 }
2261
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002262 /* We support the following forms of raise:
2263 raise <class>, <classinstance>
2264 raise <class>, <argument tuple>
2265 raise <class>, None
2266 raise <class>, <argument>
2267 raise <classinstance>, None
2268 raise <string>, <object>
2269 raise <string>, None
2270
2271 An omitted second argument is the same as None.
2272
2273 In addition, raise <tuple>, <anything> is the same as
2274 raising the tuple's first item (and it better have one!);
2275 this rule is applied recursively.
2276
2277 Finally, an optional third argument can be supplied, which
2278 gives the traceback to be substituted (useful when
2279 re-raising an exception after examining it). */
2280
2281 /* First, check the traceback argument, replacing None with
2282 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002283 if (tb == Py_None) {
2284 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002285 tb = NULL;
2286 }
2287 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002288 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002289 "raise 3rd arg must be traceback or None");
2290 goto raise_error;
2291 }
2292
2293 /* Next, replace a missing value with None */
2294 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002295 value = Py_None;
2296 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002297 }
2298
2299 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002300 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2301 PyObject *tmp = type;
2302 type = PyTuple_GET_ITEM(type, 0);
2303 Py_INCREF(type);
2304 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002305 }
2306
Barry Warsaw4249f541997-08-22 21:26:19 +00002307 if (PyString_Check(type))
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002308 ;
Barry Warsaw4249f541997-08-22 21:26:19 +00002309
2310 else if (PyClass_Check(type))
2311 PyErr_NormalizeException(&type, &value, &tb);
2312
Guido van Rossumb209a111997-04-29 18:18:01 +00002313 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002314 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002315 if (value != Py_None) {
2316 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002317 "instance exception may not have a separate value");
2318 goto raise_error;
2319 }
2320 else {
2321 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002322 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002323 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002324 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2325 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002326 }
2327 }
2328 else {
2329 /* Not something you can raise. You get an exception
2330 anyway, just not what you specified :-) */
Guido van Rossumb209a111997-04-29 18:18:01 +00002331 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002332 "exceptions must be strings, classes, or instances");
2333 goto raise_error;
2334 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002335 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002336 if (tb == NULL)
2337 return WHY_EXCEPTION;
2338 else
2339 return WHY_RERAISE;
2340 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002341 Py_XDECREF(value);
2342 Py_XDECREF(type);
2343 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002344 return WHY_EXCEPTION;
2345}
2346
Barry Warsawe42b18f1997-08-25 22:13:04 +00002347static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002348unpack_sequence(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002349{
2350 int i;
2351 PyObject *w;
2352
2353 for (i = 0; i < argcnt; i++) {
2354 if (! (w = PySequence_GetItem(v, i))) {
2355 if (PyErr_ExceptionMatches(PyExc_IndexError))
2356 PyErr_SetString(PyExc_ValueError,
2357 "unpack sequence of wrong size");
2358 goto finally;
2359 }
2360 *--sp = w;
2361 }
2362 /* we better get an IndexError now */
2363 if (PySequence_GetItem(v, i) == NULL) {
2364 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2365 PyErr_Clear();
2366 return 1;
2367 }
2368 /* some other exception occurred. fall through to finally */
2369 }
2370 else
2371 PyErr_SetString(PyExc_ValueError,
2372 "unpack sequence of wrong size");
2373 /* fall through */
2374finally:
Barry Warsaw91010551997-08-25 22:30:51 +00002375 for (; i > 0; i--, sp++)
2376 Py_DECREF(*sp);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002377
2378 return 0;
2379}
2380
2381
Guido van Rossum96a42c81992-01-12 02:29:51 +00002382#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002383static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002384prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002385{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002386 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002387 if (PyObject_Print(v, stdout, 0) != 0)
2388 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002389 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002390 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002391}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002392#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002393
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002394static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002395call_exc_trace(PyObject **p_trace, PyObject **p_newtrace, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002396{
Guido van Rossumb209a111997-04-29 18:18:01 +00002397 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002398 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002399 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002400 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002401 value = Py_None;
2402 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002403 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002404 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002405 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002406 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002407 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002408 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002409 err = call_trace(p_trace, p_newtrace, f, "exception", arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002410 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002411 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002412 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002413 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002414 Py_XDECREF(type);
2415 Py_XDECREF(value);
2416 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002417 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002418}
2419
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002420/* PyObject **p_trace: in/out; may not be NULL;
2421 may not point to NULL variable initially
2422 PyObject **p_newtrace: in/out; may be NULL;
2423 may point to NULL variable;
2424 may be same variable as p_newtrace */
2425
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002426static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002427call_trace(PyObject **p_trace, PyObject **p_newtrace, PyFrameObject *f,
2428 char *msg, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002429{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002430 PyThreadState *tstate = f->f_tstate;
Guido van Rossumb209a111997-04-29 18:18:01 +00002431 PyObject *args, *what;
2432 PyObject *res = NULL;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002433
Guido van Rossuma027efa1997-05-05 20:56:21 +00002434 if (tstate->tracing) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002435 /* Don't do recursive traces */
2436 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002437 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002438 *p_newtrace = NULL;
2439 }
2440 return 0;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002441 }
2442
Guido van Rossumb209a111997-04-29 18:18:01 +00002443 args = PyTuple_New(3);
Guido van Rossumf10570b1995-07-07 22:53:21 +00002444 if (args == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002445 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002446 what = PyString_FromString(msg);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002447 if (what == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002448 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002449 Py_INCREF(f);
2450 PyTuple_SET_ITEM(args, 0, (PyObject *)f);
2451 PyTuple_SET_ITEM(args, 1, what);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002452 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002453 arg = Py_None;
2454 Py_INCREF(arg);
2455 PyTuple_SET_ITEM(args, 2, arg);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002456 tstate->tracing++;
Guido van Rossumb209a111997-04-29 18:18:01 +00002457 PyFrame_FastToLocals(f);
2458 res = PyEval_CallObject(*p_trace, args); /* May clear *p_trace! */
2459 PyFrame_LocalsToFast(f, 1);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002460 tstate->tracing--;
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002461 cleanup:
Guido van Rossumb209a111997-04-29 18:18:01 +00002462 Py_XDECREF(args);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002463 if (res == NULL) {
2464 /* The trace proc raised an exception */
Guido van Rossumb209a111997-04-29 18:18:01 +00002465 PyTraceBack_Here(f);
2466 Py_XDECREF(*p_trace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002467 *p_trace = NULL;
2468 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002469 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002470 *p_newtrace = NULL;
2471 }
Barry Warsawf6202631999-09-08 16:26:33 +00002472 /* to be extra double plus sure we don't get recursive
2473 * calls inf either tracefunc or profilefunc gets an
2474 * exception, zap the global variables.
2475 */
2476 Py_XDECREF(tstate->sys_tracefunc);
2477 tstate->sys_tracefunc = NULL;
2478 Py_XDECREF(tstate->sys_profilefunc);
2479 tstate->sys_profilefunc = NULL;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002480 return -1;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002481 }
2482 else {
2483 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002484 Py_XDECREF(*p_newtrace);
2485 if (res == Py_None)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002486 *p_newtrace = NULL;
2487 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002488 Py_INCREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002489 *p_newtrace = res;
2490 }
2491 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002492 Py_DECREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002493 return 0;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002494 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002495}
2496
Guido van Rossumb209a111997-04-29 18:18:01 +00002497PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002498PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00002499{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002500 PyThreadState *tstate = PyThreadState_Get();
2501 PyFrameObject *current_frame = tstate->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002502 if (current_frame == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002503 return tstate->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00002504 else
2505 return current_frame->f_builtins;
2506}
2507
Guido van Rossumb209a111997-04-29 18:18:01 +00002508PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002509PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00002510{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002511 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum5b722181993-03-30 17:46:03 +00002512 if (current_frame == NULL)
2513 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002514 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00002515 return current_frame->f_locals;
2516}
2517
Guido van Rossumb209a111997-04-29 18:18:01 +00002518PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002519PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002520{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002521 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002522 if (current_frame == NULL)
2523 return NULL;
2524 else
2525 return current_frame->f_globals;
2526}
2527
Guido van Rossumb209a111997-04-29 18:18:01 +00002528PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002529PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00002530{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002531 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossumb209a111997-04-29 18:18:01 +00002532 return (PyObject *)current_frame;
Guido van Rossume59214e1994-08-30 08:01:59 +00002533}
2534
Guido van Rossum6135a871995-01-09 17:53:26 +00002535int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002536PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00002537{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002538 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002539 return current_frame == NULL ? 0 : current_frame->f_restricted;
2540}
2541
Guido van Rossumbe270261997-05-22 22:26:18 +00002542int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002543Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002544{
Guido van Rossumb209a111997-04-29 18:18:01 +00002545 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00002546 if (f == NULL)
2547 return 0;
2548 if (!PyFile_SoftSpace(f, 0))
2549 return 0;
2550 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002551}
2552
Guido van Rossum3f5da241990-12-20 15:06:42 +00002553
Guido van Rossum681d79a1995-07-18 14:51:37 +00002554/* External interface to call any callable object.
2555 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002556
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002557#undef PyEval_CallObject
2558/* for backward compatibility: export this interface */
2559
Guido van Rossumb209a111997-04-29 18:18:01 +00002560PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002561PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002562{
Guido van Rossumb209a111997-04-29 18:18:01 +00002563 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002564}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002565#define PyEval_CallObject(func,arg) \
2566 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00002567
Guido van Rossumb209a111997-04-29 18:18:01 +00002568PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002569PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002570{
2571 ternaryfunc call;
Guido van Rossumb209a111997-04-29 18:18:01 +00002572 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002573
2574 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002575 arg = PyTuple_New(0);
2576 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002577 PyErr_SetString(PyExc_TypeError,
2578 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002579 return NULL;
2580 }
2581 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002582 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002583
Guido van Rossumb209a111997-04-29 18:18:01 +00002584 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002585 PyErr_SetString(PyExc_TypeError,
2586 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00002587 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00002588 return NULL;
2589 }
2590
Guido van Rossum150b2df1996-12-05 23:17:11 +00002591 if ((call = func->ob_type->tp_call) != NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002592 result = (*call)(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002593 else if (PyMethod_Check(func) || PyFunction_Check(func))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002594 result = call_function(func, arg, kw);
2595 else
2596 result = call_builtin(func, arg, kw);
2597
Guido van Rossumb209a111997-04-29 18:18:01 +00002598 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002599
Guido van Rossumb209a111997-04-29 18:18:01 +00002600 if (result == NULL && !PyErr_Occurred())
2601 PyErr_SetString(PyExc_SystemError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002602 "NULL result without error in call_object");
Guido van Rossume59214e1994-08-30 08:01:59 +00002603
2604 return result;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002605}
2606
Guido van Rossumb209a111997-04-29 18:18:01 +00002607static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002608call_builtin(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002609{
Guido van Rossumb209a111997-04-29 18:18:01 +00002610 if (PyCFunction_Check(func)) {
2611 PyCFunction meth = PyCFunction_GetFunction(func);
2612 PyObject *self = PyCFunction_GetSelf(func);
2613 int flags = PyCFunction_GetFlags(func);
Guido van Rossumff8b4941995-07-26 18:16:42 +00002614 if (!(flags & METH_VARARGS)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002615 int size = PyTuple_Size(arg);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002616 if (size == 1)
Guido van Rossumb209a111997-04-29 18:18:01 +00002617 arg = PyTuple_GET_ITEM(arg, 0);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002618 else if (size == 0)
2619 arg = NULL;
2620 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002621 if (flags & METH_KEYWORDS)
2622 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002623 if (kw != NULL && PyDict_Size(kw) != 0) {
2624 PyErr_SetString(PyExc_TypeError,
Guido van Rossumff8b4941995-07-26 18:16:42 +00002625 "this function takes no keyword arguments");
2626 return NULL;
2627 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002628 return (*meth)(self, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002629 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002630 if (PyClass_Check(func)) {
2631 return PyInstance_New(func, arg, kw);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002632 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002633 if (PyInstance_Check(func)) {
2634 PyObject *res, *call = PyObject_GetAttrString(func,"__call__");
Guido van Rossume59214e1994-08-30 08:01:59 +00002635 if (call == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002636 PyErr_Clear();
2637 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002638 "no __call__ method defined");
Guido van Rossume59214e1994-08-30 08:01:59 +00002639 return NULL;
2640 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002641 res = PyEval_CallObjectWithKeywords(call, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002642 Py_DECREF(call);
Guido van Rossume59214e1994-08-30 08:01:59 +00002643 return res;
2644 }
Guido van Rossumba98a421999-11-15 19:29:33 +00002645 PyErr_Format(PyExc_TypeError, "call of non-function (type %.400s)",
Guido van Rossum2d1ad391998-08-25 18:16:54 +00002646 func->ob_type->tp_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002647 return NULL;
2648}
2649
Guido van Rossumb209a111997-04-29 18:18:01 +00002650static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002651call_function(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002652{
Guido van Rossumb209a111997-04-29 18:18:01 +00002653 PyObject *class = NULL; /* == owner */
2654 PyObject *argdefs;
2655 PyObject **d, **k;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002656 int nk, nd;
Guido van Rossumb209a111997-04-29 18:18:01 +00002657 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002658
Guido van Rossumb209a111997-04-29 18:18:01 +00002659 if (kw != NULL && !PyDict_Check(kw)) {
2660 PyErr_BadInternalCall();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002661 return NULL;
2662 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002663
Guido van Rossumb209a111997-04-29 18:18:01 +00002664 if (PyMethod_Check(func)) {
2665 PyObject *self = PyMethod_Self(func);
2666 class = PyMethod_Class(func);
2667 func = PyMethod_Function(func);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002668 if (self == NULL) {
2669 /* Unbound methods must be called with an instance of
2670 the class (or a derived class) as first argument */
Guido van Rossumb209a111997-04-29 18:18:01 +00002671 if (PyTuple_Size(arg) >= 1) {
2672 self = PyTuple_GET_ITEM(arg, 0);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002673 if (self != NULL &&
Guido van Rossumb209a111997-04-29 18:18:01 +00002674 PyInstance_Check(self) &&
2675 PyClass_IsSubclass((PyObject *)
2676 (((PyInstanceObject *)self)->in_class),
Guido van Rossumb3f72581993-05-21 19:56:10 +00002677 class))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002678 /* Handy-dandy */ ;
Guido van Rossumb3f72581993-05-21 19:56:10 +00002679 else
2680 self = NULL;
2681 }
2682 if (self == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002683 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002684 "unbound method must be called with class instance 1st argument");
Guido van Rossumb3f72581993-05-21 19:56:10 +00002685 return NULL;
2686 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002687 Py_INCREF(arg);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002688 }
2689 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002690 int argcount = PyTuple_Size(arg);
2691 PyObject *newarg = PyTuple_New(argcount + 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002692 int i;
Guido van Rossum81daa321993-05-20 14:24:46 +00002693 if (newarg == NULL)
2694 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002695 Py_INCREF(self);
2696 PyTuple_SET_ITEM(newarg, 0, self);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002697 for (i = 0; i < argcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002698 PyObject *v = PyTuple_GET_ITEM(arg, i);
2699 Py_XINCREF(v);
2700 PyTuple_SET_ITEM(newarg, i+1, v);
Guido van Rossum81daa321993-05-20 14:24:46 +00002701 }
2702 arg = newarg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002703 }
Guido van Rossum7859f871998-07-08 14:58:16 +00002704 if (!PyFunction_Check(func)) {
2705 result = PyEval_CallObjectWithKeywords(func, arg, kw);
2706 Py_DECREF(arg);
2707 return result;
2708 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002709 }
2710 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002711 if (!PyFunction_Check(func)) {
Guido van Rossum2d1ad391998-08-25 18:16:54 +00002712 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +00002713 "call of non-function (type %.200s)",
Guido van Rossum2d1ad391998-08-25 18:16:54 +00002714 func->ob_type->tp_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002715 return NULL;
2716 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002717 Py_INCREF(arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002718 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002719
2720 argdefs = PyFunction_GetDefaults(func);
Guido van Rossumb209a111997-04-29 18:18:01 +00002721 if (argdefs != NULL && PyTuple_Check(argdefs)) {
2722 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
2723 nd = PyTuple_Size(argdefs);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002724 }
2725 else {
2726 d = NULL;
2727 nd = 0;
2728 }
2729
2730 if (kw != NULL) {
2731 int pos, i;
Guido van Rossumb209a111997-04-29 18:18:01 +00002732 nk = PyDict_Size(kw);
2733 k = PyMem_NEW(PyObject *, 2*nk);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002734 if (k == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002735 PyErr_NoMemory();
2736 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002737 return NULL;
Guido van Rossume59214e1994-08-30 08:01:59 +00002738 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002739 pos = i = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002740 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002741 i += 2;
2742 nk = i/2;
2743 /* XXX This is broken if the caller deletes dict items! */
2744 }
2745 else {
2746 k = NULL;
2747 nk = 0;
Guido van Rossume59214e1994-08-30 08:01:59 +00002748 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002749
Guido van Rossum681d79a1995-07-18 14:51:37 +00002750 result = eval_code2(
Guido van Rossumb209a111997-04-29 18:18:01 +00002751 (PyCodeObject *)PyFunction_GetCode(func),
2752 PyFunction_GetGlobals(func), (PyObject *)NULL,
2753 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
Guido van Rossum681d79a1995-07-18 14:51:37 +00002754 k, nk,
2755 d, nd,
2756 class);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002757
Guido van Rossumb209a111997-04-29 18:18:01 +00002758 Py_DECREF(arg);
Guido van Rossumb18618d2000-05-03 23:44:39 +00002759 if (k != NULL)
2760 PyMem_DEL(k);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002761
Guido van Rossum681d79a1995-07-18 14:51:37 +00002762 return result;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002763}
2764
Guido van Rossum3b9c6671996-07-30 18:40:29 +00002765#define SLICE_ERROR_MSG \
2766 "standard sequence type does not support step size other than one"
2767
Guido van Rossumb209a111997-04-29 18:18:01 +00002768static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002769loop_subscript(PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002770{
Guido van Rossumb209a111997-04-29 18:18:01 +00002771 PySequenceMethods *sq = v->ob_type->tp_as_sequence;
Guido van Rossume59214e1994-08-30 08:01:59 +00002772 int i;
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00002773 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002774 PyErr_SetString(PyExc_TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002775 return NULL;
2776 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002777 i = PyInt_AsLong(w);
Guido van Rossume59214e1994-08-30 08:01:59 +00002778 v = (*sq->sq_item)(v, i);
2779 if (v)
2780 return v;
Barry Warsaw4249f541997-08-22 21:26:19 +00002781 if (PyErr_ExceptionMatches(PyExc_IndexError))
Guido van Rossumb209a111997-04-29 18:18:01 +00002782 PyErr_Clear();
Guido van Rossume59214e1994-08-30 08:01:59 +00002783 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002784}
2785
Guido van Rossum20c6add2000-05-08 14:06:50 +00002786/* Extract a slice index from a PyInt or PyLong, the index is bound to
2787 the range [-INT_MAX+1, INTMAX]. Returns 0 and an exception if there is
2788 and error. Returns 1 on success.*/
2789
2790int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002791_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002792{
2793 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002794 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00002795 if (PyInt_Check(v)) {
2796 x = PyInt_AsLong(v);
2797 } else if (PyLong_Check(v)) {
2798 x = PyLong_AsLong(v);
2799 if (x==-1 && PyErr_Occurred()) {
2800 PyObject *long_zero;
2801
2802 if (!PyErr_ExceptionMatches( PyExc_OverflowError ) ) {
2803 /* It's not an overflow error, so just
2804 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00002805 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00002806 }
2807
2808 /* It's an overflow error, so we need to
2809 check the sign of the long integer,
2810 set the value to INT_MAX or 0, and clear
2811 the error. */
2812
2813 /* Create a long integer with a value of 0 */
2814 long_zero = PyLong_FromLong( 0L );
Guido van Rossum20c6add2000-05-08 14:06:50 +00002815 if (long_zero == NULL) return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00002816
2817 /* Check sign */
2818 if (PyObject_Compare(long_zero, v) < 0)
2819 x = INT_MAX;
2820 else
2821 x = 0;
2822
2823 /* Free the long integer we created, and clear the
2824 OverflowError */
2825 Py_DECREF(long_zero);
2826 PyErr_Clear();
2827 }
2828 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002829 PyErr_SetString(PyExc_TypeError,
2830 "slice index must be int");
Guido van Rossum20c6add2000-05-08 14:06:50 +00002831 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002832 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002833 /* Truncate -- very long indices are truncated anyway */
2834 if (x > INT_MAX)
2835 x = INT_MAX;
2836 else if (x < -INT_MAX)
2837 x = 0;
2838 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002839 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00002840 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002841}
2842
Guido van Rossumb209a111997-04-29 18:18:01 +00002843static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002844apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002845{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002846 int ilow = 0, ihigh = INT_MAX;
Guido van Rossum20c6add2000-05-08 14:06:50 +00002847 if (!_PyEval_SliceIndex(v, &ilow))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002848 return NULL;
Guido van Rossum20c6add2000-05-08 14:06:50 +00002849 if (!_PyEval_SliceIndex(w, &ihigh))
Guido van Rossum04691fc1992-08-12 15:35:34 +00002850 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002851 return PySequence_GetSlice(u, ilow, ihigh);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002852}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002853
2854static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002855assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x) /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002856{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002857 int ilow = 0, ihigh = INT_MAX;
Guido van Rossum20c6add2000-05-08 14:06:50 +00002858 if (!_PyEval_SliceIndex(v, &ilow))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002859 return -1;
Guido van Rossum20c6add2000-05-08 14:06:50 +00002860 if (!_PyEval_SliceIndex(w, &ihigh))
Guido van Rossum3f5da241990-12-20 15:06:42 +00002861 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002862 if (x == NULL)
2863 return PySequence_DelSlice(u, ilow, ihigh);
2864 else
2865 return PySequence_SetSlice(u, ilow, ihigh, x);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002866}
2867
Guido van Rossumb209a111997-04-29 18:18:01 +00002868static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002869cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002870{
2871 register int cmp;
2872 register int res = 0;
2873 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002874 case IS:
2875 case IS_NOT:
2876 res = (v == w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002877 if (op == (int) IS_NOT)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002878 res = !res;
2879 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002880 case IN:
2881 case NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00002882 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002883 if (res < 0)
2884 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002885 if (op == (int) NOT_IN)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002886 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002887 break;
2888 case EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00002889 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002890 break;
2891 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00002892 cmp = PyObject_Compare(v, w);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002893 if (cmp && PyErr_Occurred())
2894 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002895 switch (op) {
2896 case LT: res = cmp < 0; break;
2897 case LE: res = cmp <= 0; break;
2898 case EQ: res = cmp == 0; break;
2899 case NE: res = cmp != 0; break;
2900 case GT: res = cmp > 0; break;
2901 case GE: res = cmp >= 0; break;
2902 /* XXX no default? (res is initialized to 0 though) */
2903 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002904 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002905 v = res ? Py_True : Py_False;
2906 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002907 return v;
2908}
2909
Thomas Wouters52152252000-08-17 22:55:00 +00002910static PyObject *
2911import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00002912{
Guido van Rossumb209a111997-04-29 18:18:01 +00002913 PyObject *w, *x;
2914 if (!PyModule_Check(v)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002915 PyErr_SetString(PyExc_TypeError,
2916 "import-from requires module object");
Thomas Wouters52152252000-08-17 22:55:00 +00002917 return NULL;
2918 }
2919 w = PyModule_GetDict(v); /* TDB: can this not fail ? */
2920 x = PyDict_GetItem(w, name);
2921 if (x == NULL) {
2922 PyErr_Format(PyExc_ImportError,
2923 "cannot import name %.230s",
2924 PyString_AsString(name));
2925 } else
2926 Py_INCREF(x);
2927 return x;
2928}
2929
2930static int
2931import_all_from(PyObject *locals, PyObject *v)
2932{
2933 int pos = 0, err;
2934 PyObject *name, *value;
2935 PyObject *w;
2936
2937 if (!PyModule_Check(v)) {
2938 PyErr_SetString(PyExc_TypeError,
2939 "import-from requires module object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002940 return -1;
2941 }
Thomas Wouters52152252000-08-17 22:55:00 +00002942 w = PyModule_GetDict(v); /* TBD: can this not fail ? */
2943
2944 while (PyDict_Next(w, &pos, &name, &value)) {
2945 if (!PyString_Check(name) ||
2946 PyString_AsString(name)[0] == '_')
Guido van Rossume9736fc1990-11-18 17:33:06 +00002947 continue;
Thomas Wouters52152252000-08-17 22:55:00 +00002948 Py_INCREF(value);
2949 err = PyDict_SetItem(locals, name, value);
2950 Py_DECREF(value);
2951 if (err != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002952 return -1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002953 }
Thomas Wouters52152252000-08-17 22:55:00 +00002954 return 0;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002955}
2956
Guido van Rossumb209a111997-04-29 18:18:01 +00002957static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002958build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00002959{
Guido van Rossumcd649651997-08-22 16:56:16 +00002960 int i, n;
Guido van Rossumb209a111997-04-29 18:18:01 +00002961 if (!PyTuple_Check(bases)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002962 PyErr_SetString(PyExc_SystemError,
2963 "build_class with non-tuple bases");
Guido van Rossum25831651993-05-19 14:50:45 +00002964 return NULL;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002965 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002966 if (!PyDict_Check(methods)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002967 PyErr_SetString(PyExc_SystemError,
2968 "build_class with non-dictionary");
Guido van Rossume9736fc1990-11-18 17:33:06 +00002969 return NULL;
2970 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002971 if (!PyString_Check(name)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002972 PyErr_SetString(PyExc_SystemError,
Guido van Rossum7a5b7962000-03-31 13:52:29 +00002973 "build_class with non-string name");
Guido van Rossum25831651993-05-19 14:50:45 +00002974 return NULL;
2975 }
Guido van Rossumcd649651997-08-22 16:56:16 +00002976 n = PyTuple_Size(bases);
2977 for (i = 0; i < n; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002978 PyObject *base = PyTuple_GET_ITEM(bases, i);
2979 if (!PyClass_Check(base)) {
Guido van Rossum150b2df1996-12-05 23:17:11 +00002980 /* Call the base's *type*, if it is callable.
2981 This code is a hook for Donald Beaudry's
2982 and Jim Fulton's type extensions. In
Thomas Wouters7e474022000-07-16 12:04:32 +00002983 unextended Python it will never be triggered
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002984 since its types are not callable.
2985 Ditto: call the bases's *class*, if it has
2986 one. This makes the same thing possible
2987 without writing C code. A true meta-object
2988 protocol! */
2989 PyObject *basetype = (PyObject *)base->ob_type;
2990 PyObject *callable = NULL;
2991 if (PyCallable_Check(basetype))
2992 callable = basetype;
2993 else
2994 callable = PyObject_GetAttrString(
2995 base, "__class__");
2996 if (callable) {
2997 PyObject *args;
2998 PyObject *newclass = NULL;
2999 args = Py_BuildValue(
3000 "(OOO)", name, bases, methods);
3001 if (args != NULL) {
3002 newclass = PyEval_CallObject(
3003 callable, args);
3004 Py_DECREF(args);
3005 }
Guido van Rossum730806d1998-04-10 22:27:42 +00003006 if (callable != basetype) {
Guido van Rossum55b9ab51997-07-31 03:54:02 +00003007 Py_DECREF(callable);
Guido van Rossum730806d1998-04-10 22:27:42 +00003008 }
Guido van Rossum55b9ab51997-07-31 03:54:02 +00003009 return newclass;
Guido van Rossum150b2df1996-12-05 23:17:11 +00003010 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003011 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +00003012 "base is not a class object");
3013 return NULL;
3014 }
3015 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003016 return PyClass_New(bases, methods, name);
Guido van Rossum25831651993-05-19 14:50:45 +00003017}
3018
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003019static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003020exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3021 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003022{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003023 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003024 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003025 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003026
Guido van Rossumb209a111997-04-29 18:18:01 +00003027 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3028 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003029 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003030 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003031 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003032 locals = PyTuple_GetItem(prog, 2);
3033 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003034 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003035 if (globals == Py_None) {
3036 globals = PyEval_GetGlobals();
3037 if (locals == Py_None) {
3038 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003039 plain = 1;
3040 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003041 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003042 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003043 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003044 if (!PyString_Check(prog) &&
3045 !PyCode_Check(prog) &&
3046 !PyFile_Check(prog)) {
3047 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003048 "exec 1st arg must be string, code or file object");
3049 return -1;
3050 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003051 if (!PyDict_Check(globals) || !PyDict_Check(locals)) {
3052 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003053 "exec 2nd/3rd args must be dict or None");
3054 return -1;
3055 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003056 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003057 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00003058 if (PyCode_Check(prog)) {
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003059 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003060 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003061 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003062 FILE *fp = PyFile_AsFile(prog);
3063 char *name = PyString_AsString(PyFile_Name(prog));
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003064 v = PyRun_File(fp, name, Py_file_input, globals, locals);
3065 }
3066 else {
3067 char *s = PyString_AsString(prog);
Guido van Rossum582acec2000-06-28 22:07:35 +00003068 if (strlen(s) != (size_t)PyString_Size(prog)) {
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003069 PyErr_SetString(PyExc_ValueError,
3070 "embedded '\\0' in exec string");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003071 return -1;
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003072 }
3073 v = PyRun_String(s, Py_file_input, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003074 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003075 if (plain)
3076 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003077 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003078 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00003079 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003080 return 0;
3081}
Guido van Rossum24c13741995-02-14 09:42:43 +00003082
Paul Prescode68140d2000-08-30 20:25:01 +00003083static void
3084format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
3085{
3086 char *obj_str;
3087
3088 if (!obj)
3089 return;
3090
3091 obj_str = PyString_AsString(obj);
3092 if (!obj_str)
3093 return;
3094
3095 PyErr_Format(exc, format_str, obj_str);
3096}
Guido van Rossum950361c1997-01-24 13:49:28 +00003097
3098#ifdef DYNAMIC_EXECUTION_PROFILE
3099
3100PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003101getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00003102{
3103 int i;
3104 PyObject *l = PyList_New(256);
3105 if (l == NULL) return NULL;
3106 for (i = 0; i < 256; i++) {
3107 PyObject *x = PyInt_FromLong(a[i]);
3108 if (x == NULL) {
3109 Py_DECREF(l);
3110 return NULL;
3111 }
3112 PyList_SetItem(l, i, x);
3113 }
3114 for (i = 0; i < 256; i++)
3115 a[i] = 0;
3116 return l;
3117}
3118
3119PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003120_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00003121{
3122#ifndef DXPAIRS
3123 return getarray(dxp);
3124#else
3125 int i;
3126 PyObject *l = PyList_New(257);
3127 if (l == NULL) return NULL;
3128 for (i = 0; i < 257; i++) {
3129 PyObject *x = getarray(dxpairs[i]);
3130 if (x == NULL) {
3131 Py_DECREF(l);
3132 return NULL;
3133 }
3134 PyList_SetItem(l, i, x);
3135 }
3136 return l;
3137#endif
3138}
3139
3140#endif