blob: 7c358dbd934631faedd7dfad6f979b4a25ed0688 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033
Guido van Rossum681d79a1995-07-18 14:51:37 +000034/* XXX TO DO:
35 XXX how to pass arguments to call_trace?
Guido van Rossum681d79a1995-07-18 14:51:37 +000036 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +000037 XXX document it!
38 */
39
Guido van Rossumb209a111997-04-29 18:18:01 +000040#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000041
Guido van Rossum10dc2e81990-11-18 17:27:39 +000042#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000043#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000044#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000045#include "opcode.h"
46
Guido van Rossumc6004111993-11-05 10:22:19 +000047#include <ctype.h>
48
Guido van Rossuma027efa1997-05-05 20:56:21 +000049#ifdef HAVE_LIMITS_H
50#include <limits.h>
51#else
52#define INT_MAX 2147483647
53#endif
54
Guido van Rossum04691fc1992-08-12 15:35:34 +000055/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000056/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000057
Guido van Rossum408027e1996-12-30 16:17:54 +000058#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000059/* For debugging the interpreter: */
60#define LLTRACE 1 /* Low-level trace feature */
61#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000062#endif
63
Guido van Rossum5b722181993-03-30 17:46:03 +000064
Guido van Rossum374a9221991-04-04 10:40:29 +000065/* Forward declarations */
Guido van Rossum374a9221991-04-04 10:40:29 +000066
Guido van Rossumb209a111997-04-29 18:18:01 +000067static PyObject *eval_code2 Py_PROTO((PyCodeObject *,
68 PyObject *, PyObject *,
69 PyObject **, int,
70 PyObject **, int,
71 PyObject **, int,
72 PyObject *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000073#ifdef LLTRACE
Guido van Rossumb209a111997-04-29 18:18:01 +000074static int prtrace Py_PROTO((PyObject *, char *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000075#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +000076static void call_exc_trace Py_PROTO((PyObject **, PyObject**,
77 PyFrameObject *));
78static int call_trace Py_PROTO((PyObject **, PyObject **,
79 PyFrameObject *, char *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000080static PyObject *call_builtin Py_PROTO((PyObject *, PyObject *, PyObject *));
81static PyObject *call_function Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000082static PyObject *loop_subscript Py_PROTO((PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000083static int slice_index Py_PROTO((PyObject *, int *));
Guido van Rossumb209a111997-04-29 18:18:01 +000084static PyObject *apply_slice Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000085static int assign_slice Py_PROTO((PyObject *, PyObject *,
86 PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000087static int cmp_member Py_PROTO((PyObject *, PyObject *));
88static PyObject *cmp_outcome Py_PROTO((int, PyObject *, PyObject *));
89static int import_from Py_PROTO((PyObject *, PyObject *, PyObject *));
90static PyObject *build_class Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000091static int exec_statement Py_PROTO((PyFrameObject *,
92 PyObject *, PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000093static PyObject *find_from_args Py_PROTO((PyFrameObject *, int));
Guido van Rossuma027efa1997-05-05 20:56:21 +000094static void set_exc_info Py_PROTO((PyThreadState *,
95 PyObject *, PyObject *, PyObject *));
96static void reset_exc_info Py_PROTO((PyThreadState *));
Guido van Rossum374a9221991-04-04 10:40:29 +000097
98
Guido van Rossum950361c1997-01-24 13:49:28 +000099/* Dynamic execution profile */
100#ifdef DYNAMIC_EXECUTION_PROFILE
101#ifdef DXPAIRS
102static long dxpairs[257][256];
103#define dxp dxpairs[256]
104#else
105static long dxp[256];
106#endif
107#endif
108
109
Guido van Rossume59214e1994-08-30 08:01:59 +0000110#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000111
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000112#include <errno.h>
113#include "thread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000114
Guido van Rossuma027efa1997-05-05 20:56:21 +0000115extern int _PyThread_Started; /* Flag for Py_Exit */
116
Guido van Rossuma9672091994-09-14 13:31:22 +0000117static type_lock interpreter_lock = 0;
118static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000119
120void
Guido van Rossumb209a111997-04-29 18:18:01 +0000121PyEval_InitThreads()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000122{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000123 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000124 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000125 _PyThread_Started = 1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000126 interpreter_lock = allocate_lock();
127 acquire_lock(interpreter_lock, 1);
Guido van Rossuma9672091994-09-14 13:31:22 +0000128 main_thread = get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000129}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000130
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000131void
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132PyEval_AcquireLock()
133{
134 acquire_lock(interpreter_lock, 1);
135}
136
137void
138PyEval_ReleaseLock()
139{
140 release_lock(interpreter_lock);
141}
142
143void
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000144PyEval_AcquireThread(tstate)
145 PyThreadState *tstate;
146{
147 if (tstate == NULL)
148 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
149 acquire_lock(interpreter_lock, 1);
150 if (PyThreadState_Swap(tstate) != NULL)
151 Py_FatalError(
152 "PyEval_AcquireThread: non-NULL old thread state");
153}
154
155void
156PyEval_ReleaseThread(tstate)
157 PyThreadState *tstate;
158{
159 if (tstate == NULL)
160 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
161 if (PyThreadState_Swap(NULL) != tstate)
162 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
163 release_lock(interpreter_lock);
164}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165#endif
166
Guido van Rossumff4949e1992-08-05 19:58:53 +0000167/* Functions save_thread and restore_thread are always defined so
168 dynamically loaded modules needn't be compiled separately for use
169 with and without threads: */
170
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000171PyThreadState *
Guido van Rossumb209a111997-04-29 18:18:01 +0000172PyEval_SaveThread()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000173{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000174 PyThreadState *tstate = PyThreadState_Swap(NULL);
175 if (tstate == NULL)
176 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000177#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000178 if (interpreter_lock)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000179 release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000181 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000182}
183
184void
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000185PyEval_RestoreThread(tstate)
186 PyThreadState *tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000187{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000188 if (tstate == NULL)
189 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000190#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000191 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000192 int err = errno;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000193 acquire_lock(interpreter_lock, 1);
194 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000195 }
196#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000197 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000198}
199
200
Guido van Rossuma9672091994-09-14 13:31:22 +0000201/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
202 signal handlers or Mac I/O completion routines) can schedule calls
203 to a function to be called synchronously.
204 The synchronous function is called with one void* argument.
205 It should return 0 for success or -1 for failure -- failure should
206 be accompanied by an exception.
207
208 If registry succeeds, the registry function returns 0; if it fails
209 (e.g. due to too many pending calls) it returns -1 (without setting
210 an exception condition).
211
212 Note that because registry may occur from within signal handlers,
213 or other asynchronous events, calling malloc() is unsafe!
214
215#ifdef WITH_THREAD
216 Any thread can schedule pending calls, but only the main thread
217 will execute them.
218#endif
219
220 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
221 There are two possible race conditions:
222 (1) nested asynchronous registry calls;
223 (2) registry calls made while pending calls are being processed.
224 While (1) is very unlikely, (2) is a real possibility.
225 The current code is safe against (2), but not against (1).
226 The safety against (2) is derived from the fact that only one
227 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000228
Guido van Rossuma027efa1997-05-05 20:56:21 +0000229 XXX Darn! With the advent of thread state, we should have an array
230 of pending calls per thread in the thread state! Later...
231*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000232
Guido van Rossuma9672091994-09-14 13:31:22 +0000233#define NPENDINGCALLS 32
234static struct {
Guido van Rossumb209a111997-04-29 18:18:01 +0000235 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000236 ANY *arg;
237} pendingcalls[NPENDINGCALLS];
238static volatile int pendingfirst = 0;
239static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000240static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000241
242int
243Py_AddPendingCall(func, arg)
Guido van Rossumb209a111997-04-29 18:18:01 +0000244 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000245 ANY *arg;
246{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000247 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000248 int i, j;
249 /* XXX Begin critical section */
250 /* XXX If you want this to be safe against nested
251 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000252 if (busy)
253 return -1;
254 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000255 i = pendinglast;
256 j = (i + 1) % NPENDINGCALLS;
257 if (j == pendingfirst)
258 return -1; /* Queue full */
259 pendingcalls[i].func = func;
260 pendingcalls[i].arg = arg;
261 pendinglast = j;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000262 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000263 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000264 /* XXX End critical section */
265 return 0;
266}
267
Guido van Rossum180d7b41994-09-29 09:45:57 +0000268int
269Py_MakePendingCalls()
Guido van Rossuma9672091994-09-14 13:31:22 +0000270{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000271 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000272#ifdef WITH_THREAD
Guido van Rossuma027efa1997-05-05 20:56:21 +0000273 if (main_thread && get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000274 return 0;
275#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000276 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000277 return 0;
278 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000279 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000280 for (;;) {
281 int i;
Guido van Rossumb209a111997-04-29 18:18:01 +0000282 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000283 ANY *arg;
284 i = pendingfirst;
285 if (i == pendinglast)
286 break; /* Queue empty */
287 func = pendingcalls[i].func;
288 arg = pendingcalls[i].arg;
289 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000290 if (func(arg) < 0) {
291 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000292 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000293 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000294 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000295 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000296 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000297 return 0;
298}
299
300
Guido van Rossum374a9221991-04-04 10:40:29 +0000301/* Status code for main loop (reason for stack unwind) */
302
303enum why_code {
304 WHY_NOT, /* No error */
305 WHY_EXCEPTION, /* Exception occurred */
306 WHY_RERAISE, /* Exception re-raised by 'finally' */
307 WHY_RETURN, /* 'return' statement */
308 WHY_BREAK /* 'break' statement */
309};
310
Guido van Rossumb209a111997-04-29 18:18:01 +0000311static enum why_code do_raise Py_PROTO((PyObject *, PyObject *, PyObject *));
Barry Warsawe42b18f1997-08-25 22:13:04 +0000312static int unpack_sequence Py_PROTO((PyObject *, int, PyObject **));
Guido van Rossum1aa14831997-01-21 05:34:20 +0000313
Guido van Rossum374a9221991-04-04 10:40:29 +0000314
Guido van Rossum681d79a1995-07-18 14:51:37 +0000315/* Backward compatible interface */
Guido van Rossum374a9221991-04-04 10:40:29 +0000316
Guido van Rossumb209a111997-04-29 18:18:01 +0000317PyObject *
318PyEval_EvalCode(co, globals, locals)
319 PyCodeObject *co;
320 PyObject *globals;
321 PyObject *locals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000322{
323 return eval_code2(co,
324 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000325 (PyObject **)NULL, 0,
326 (PyObject **)NULL, 0,
327 (PyObject **)NULL, 0,
328 (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000329}
330
331
332/* Interpreter main loop */
333
Guido van Rossum8861b741996-07-30 16:49:37 +0000334#ifndef MAX_RECURSION_DEPTH
335#define MAX_RECURSION_DEPTH 10000
336#endif
337
Guido van Rossumb209a111997-04-29 18:18:01 +0000338static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +0000339eval_code2(co, globals, locals,
340 args, argcount, kws, kwcount, defs, defcount, owner)
Guido van Rossumb209a111997-04-29 18:18:01 +0000341 PyCodeObject *co;
342 PyObject *globals;
343 PyObject *locals;
344 PyObject **args;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000345 int argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000346 PyObject **kws; /* length: 2*kwcount */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000347 int kwcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000348 PyObject **defs;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000349 int defcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000350 PyObject *owner;
Guido van Rossum374a9221991-04-04 10:40:29 +0000351{
Guido van Rossum950361c1997-01-24 13:49:28 +0000352#ifdef DXPAIRS
353 int lastopcode = 0;
354#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000355 register unsigned char *next_instr;
Guido van Rossum150b2df1996-12-05 23:17:11 +0000356 register int opcode = 0; /* Current opcode */
357 register int oparg = 0; /* Current opcode argument, if any */
Guido van Rossumb209a111997-04-29 18:18:01 +0000358 register PyObject **stack_pointer;
Guido van Rossum374a9221991-04-04 10:40:29 +0000359 register enum why_code why; /* Reason for block stack unwind */
360 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000361 register PyObject *x; /* Result object -- NULL if error */
362 register PyObject *v; /* Temporary objects popped off stack */
363 register PyObject *w;
364 register PyObject *u;
365 register PyObject *t;
366 register PyFrameObject *f; /* Current frame */
367 register PyObject **fastlocals = NULL;
368 PyObject *retval = NULL; /* Return value */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000369 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum96a42c81992-01-12 02:29:51 +0000370#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000371 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000372#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000373#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000374 /* Make it easier to find out where we are with a debugger */
Guido van Rossumb209a111997-04-29 18:18:01 +0000375 char *filename = PyString_AsString(co->co_filename);
Guido van Rossum99bec951992-09-03 20:29:45 +0000376#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000377
378/* Code access macros */
379
380#define GETCONST(i) Getconst(f, i)
381#define GETNAME(i) Getname(f, i)
382#define GETNAMEV(i) Getnamev(f, i)
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000383#define FIRST_INSTR() (GETUSTRINGVALUE(co->co_code))
Guido van Rossum374a9221991-04-04 10:40:29 +0000384#define INSTR_OFFSET() (next_instr - FIRST_INSTR())
385#define NEXTOP() (*next_instr++)
386#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
387#define JUMPTO(x) (next_instr = FIRST_INSTR() + (x))
388#define JUMPBY(x) (next_instr += (x))
389
390/* Stack manipulation macros */
391
392#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
393#define EMPTY() (STACK_LEVEL() == 0)
394#define TOP() (stack_pointer[-1])
395#define BASIC_PUSH(v) (*stack_pointer++ = (v))
396#define BASIC_POP() (*--stack_pointer)
397
Guido van Rossum96a42c81992-01-12 02:29:51 +0000398#ifdef LLTRACE
399#define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
400#define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
Guido van Rossum374a9221991-04-04 10:40:29 +0000401#else
402#define PUSH(v) BASIC_PUSH(v)
403#define POP() BASIC_POP()
404#endif
405
Guido van Rossum681d79a1995-07-18 14:51:37 +0000406/* Local variable macros */
407
408#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumb209a111997-04-29 18:18:01 +0000409#define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \
Guido van Rossum681d79a1995-07-18 14:51:37 +0000410 GETLOCAL(i) = value; } while (0)
411
Guido van Rossuma027efa1997-05-05 20:56:21 +0000412/* Start of code */
413
Guido van Rossum8861b741996-07-30 16:49:37 +0000414#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000415 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000416 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000417 return NULL;
418 }
419#endif
420
Guido van Rossum681d79a1995-07-18 14:51:37 +0000421 if (globals == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000422 PyErr_SetString(PyExc_SystemError, "eval_code2: NULL globals");
Guido van Rossum6135a871995-01-09 17:53:26 +0000423 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000424 }
425
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000426#ifdef LLTRACE
Guido van Rossumb209a111997-04-29 18:18:01 +0000427 lltrace = PyDict_GetItemString(globals, "__lltrace__") != NULL;
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000428#endif
429
Guido van Rossumb209a111997-04-29 18:18:01 +0000430 f = PyFrame_New(
Guido van Rossuma027efa1997-05-05 20:56:21 +0000431 tstate, /*back*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000432 co, /*code*/
433 globals, /*globals*/
Guido van Rossum76836021997-01-20 04:26:20 +0000434 locals); /*locals*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000435 if (f == NULL)
436 return NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000437
Guido van Rossuma027efa1997-05-05 20:56:21 +0000438 tstate->frame = f;
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000439 fastlocals = f->f_localsplus;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000440
441 if (co->co_argcount > 0 ||
442 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
443 int i;
444 int n = argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000445 PyObject *kwdict = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000446 if (co->co_flags & CO_VARKEYWORDS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000447 kwdict = PyDict_New();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000448 if (kwdict == NULL)
449 goto fail;
Guido van Rossum8c5df061997-01-24 04:19:24 +0000450 i = co->co_argcount;
451 if (co->co_flags & CO_VARARGS)
452 i++;
453 SETLOCAL(i, kwdict);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000454 }
455 if (argcount > co->co_argcount) {
456 if (!(co->co_flags & CO_VARARGS)) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000457 PyErr_Format(PyExc_TypeError,
458 "too many arguments; expected %d, got %d",
459 co->co_argcount, argcount);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000460 goto fail;
461 }
462 n = co->co_argcount;
463 }
464 for (i = 0; i < n; i++) {
465 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000466 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000467 SETLOCAL(i, x);
468 }
469 if (co->co_flags & CO_VARARGS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000470 u = PyTuple_New(argcount - n);
Guido van Rossum8c5df061997-01-24 04:19:24 +0000471 if (u == NULL)
472 goto fail;
473 SETLOCAL(co->co_argcount, u);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000474 for (i = n; i < argcount; i++) {
475 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000476 Py_INCREF(x);
477 PyTuple_SET_ITEM(u, i-n, x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000478 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000479 }
480 for (i = 0; i < kwcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000481 PyObject *keyword = kws[2*i];
482 PyObject *value = kws[2*i + 1];
Guido van Rossum681d79a1995-07-18 14:51:37 +0000483 int j;
484 /* XXX slow -- speed up using dictionary? */
485 for (j = 0; j < co->co_argcount; j++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000486 PyObject *nm = PyTuple_GET_ITEM(
487 co->co_varnames, j);
Guido van Rossumb209a111997-04-29 18:18:01 +0000488 if (PyObject_Compare(keyword, nm) == 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000489 break;
490 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000491 /* Check errors from Compare */
492 if (PyErr_Occurred())
493 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000494 if (j >= co->co_argcount) {
495 if (kwdict == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000496 PyErr_Format(PyExc_TypeError,
Guido van Rossuma027efa1997-05-05 20:56:21 +0000497 "unexpected keyword argument: %.400s",
498 PyString_AsString(keyword));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000499 goto fail;
500 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000501 PyDict_SetItem(kwdict, keyword, value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000502 }
503 else {
504 if (GETLOCAL(j) != NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000505 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000506 "keyword parameter redefined");
507 goto fail;
508 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000509 Py_INCREF(value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000510 SETLOCAL(j, value);
511 }
512 }
513 if (argcount < co->co_argcount) {
514 int m = co->co_argcount - defcount;
515 for (i = argcount; i < m; i++) {
516 if (GETLOCAL(i) == NULL) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000517 PyErr_Format(PyExc_TypeError,
518 "not enough arguments; expected %d, got %d",
519 m, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000520 goto fail;
521 }
522 }
523 if (n > m)
524 i = n - m;
525 else
526 i = 0;
527 for (; i < defcount; i++) {
528 if (GETLOCAL(m+i) == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000529 PyObject *def = defs[i];
530 Py_INCREF(def);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000531 SETLOCAL(m+i, def);
532 }
533 }
534 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000535 }
536 else {
537 if (argcount > 0 || kwcount > 0) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000538 PyErr_SetString(PyExc_TypeError,
539 "no arguments expected");
Guido van Rossum8c5df061997-01-24 04:19:24 +0000540 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000541 }
542 }
543
Guido van Rossuma027efa1997-05-05 20:56:21 +0000544 if (tstate->sys_tracefunc != NULL) {
545 /* tstate->sys_tracefunc, if defined, is a function that
546 will be called on *every* entry to a code block.
Guido van Rossum96a42c81992-01-12 02:29:51 +0000547 Its return value, if not None, is a function that
548 will be called at the start of each executed line
549 of code. (Actually, the function must return
550 itself in order to continue tracing.)
551 The trace functions are called with three arguments:
552 a pointer to the current frame, a string indicating
553 why the function is called, and an argument which
554 depends on the situation. The global trace function
555 (sys.trace) is also called whenever an exception
556 is detected. */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000557 if (call_trace(&tstate->sys_tracefunc,
558 &f->f_trace, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000559 Py_None/*XXX how to compute arguments now?*/)) {
Guido van Rossum96a42c81992-01-12 02:29:51 +0000560 /* Trace function raised an error */
Guido van Rossum8c5df061997-01-24 04:19:24 +0000561 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000562 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000563 }
564
Guido van Rossuma027efa1997-05-05 20:56:21 +0000565 if (tstate->sys_profilefunc != NULL) {
566 /* Similar for sys_profilefunc, except it needn't return
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000567 itself and isn't called for "line" events */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000568 if (call_trace(&tstate->sys_profilefunc,
569 (PyObject**)0, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000570 Py_None/*XXX*/)) {
Guido van Rossum8c5df061997-01-24 04:19:24 +0000571 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000572 }
573 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000574
Guido van Rossuma027efa1997-05-05 20:56:21 +0000575 if (++tstate->recursion_depth > MAX_RECURSION_DEPTH) {
576 --tstate->recursion_depth;
577 PyErr_SetString(PyExc_RuntimeError,
578 "Maximum recursion depth exceeded");
579 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +0000580 Py_DECREF(f);
Guido van Rossum8861b741996-07-30 16:49:37 +0000581 return NULL;
582 }
583
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000584 next_instr = GETUSTRINGVALUE(co->co_code);
Guido van Rossum374a9221991-04-04 10:40:29 +0000585 stack_pointer = f->f_valuestack;
586
Guido van Rossum374a9221991-04-04 10:40:29 +0000587 why = WHY_NOT;
588 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000589 x = Py_None; /* Not a reference, just anything non-NULL */
Guido van Rossum374a9221991-04-04 10:40:29 +0000590
591 for (;;) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000592 /* Do periodic things. Doing this every time through
593 the loop would add too much overhead, so we do it
594 only every Nth instruction. We also do it if
595 ``things_to_do'' is set, i.e. when an asynchronous
596 event needs attention (e.g. a signal handler or
597 async I/O handler); see Py_AddPendingCall() and
598 Py_MakePendingCalls() above. */
Guido van Rossuma9672091994-09-14 13:31:22 +0000599
Guido van Rossuma027efa1997-05-05 20:56:21 +0000600 if (things_to_do || --tstate->ticker < 0) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000601 tstate->ticker = tstate->interp->checkinterval;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000602 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000603 if (Py_MakePendingCalls() < 0) {
604 why = WHY_EXCEPTION;
605 goto on_error;
606 }
607 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000608#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000609 /* If we have true signals, the signal handler
610 will call Py_AddPendingCall() so we don't
611 have to call sigcheck(). On the Mac and
612 DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000613 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000614 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000615 goto on_error;
616 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000617#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000618
Guido van Rossume59214e1994-08-30 08:01:59 +0000619#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000620 if (interpreter_lock) {
621 /* Give another thread a chance */
622
Guido van Rossum25ce5661997-08-02 03:10:38 +0000623 if (PyThreadState_Swap(NULL) != tstate)
624 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000625 release_lock(interpreter_lock);
626
627 /* Other threads may run now */
628
629 acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000630 if (PyThreadState_Swap(tstate) != NULL)
631 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000632 }
633#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000634 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000635
Guido van Rossum374a9221991-04-04 10:40:29 +0000636 /* Extract opcode and argument */
Guido van Rossum99bec951992-09-03 20:29:45 +0000637
Guido van Rossum408027e1996-12-30 16:17:54 +0000638#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum99bec951992-09-03 20:29:45 +0000639 f->f_lasti = INSTR_OFFSET();
640#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000641
642 opcode = NEXTOP();
643 if (HAS_ARG(opcode))
644 oparg = NEXTARG();
Guido van Rossum950361c1997-01-24 13:49:28 +0000645#ifdef DYNAMIC_EXECUTION_PROFILE
646#ifdef DXPAIRS
647 dxpairs[lastopcode][opcode]++;
648 lastopcode = opcode;
649#endif
650 dxp[opcode]++;
651#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000652
Guido van Rossum96a42c81992-01-12 02:29:51 +0000653#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000654 /* Instruction tracing */
655
Guido van Rossum96a42c81992-01-12 02:29:51 +0000656 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000657 if (HAS_ARG(opcode)) {
658 printf("%d: %d, %d\n",
659 (int) (INSTR_OFFSET() - 3),
660 opcode, oparg);
661 }
662 else {
663 printf("%d: %d\n",
664 (int) (INSTR_OFFSET() - 1), opcode);
665 }
666 }
667#endif
668
669 /* Main switch on opcode */
670
671 switch (opcode) {
672
673 /* BEWARE!
674 It is essential that any operation that fails sets either
675 x to NULL, err to nonzero, or why to anything but WHY_NOT,
676 and that no operation that succeeds does this! */
677
678 /* case STOP_CODE: this is an error! */
679
680 case POP_TOP:
681 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000682 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000683 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000684
685 case ROT_TWO:
686 v = POP();
687 w = POP();
688 PUSH(v);
689 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000690 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000691
692 case ROT_THREE:
693 v = POP();
694 w = POP();
695 x = POP();
696 PUSH(v);
697 PUSH(x);
698 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000699 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000700
701 case DUP_TOP:
702 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000703 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000704 PUSH(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000705 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000706
707 case UNARY_POSITIVE:
708 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000709 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000710 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000711 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000712 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000713 break;
714
715 case UNARY_NEGATIVE:
716 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000717 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000718 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000719 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000720 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000721 break;
722
723 case UNARY_NOT:
724 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000725 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000726 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000727 if (err == 0) {
728 Py_INCREF(Py_True);
729 PUSH(Py_True);
730 continue;
731 }
732 else if (err > 0) {
733 Py_INCREF(Py_False);
734 PUSH(Py_False);
735 err = 0;
736 continue;
737 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000738 break;
739
740 case UNARY_CONVERT:
741 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000742 x = PyObject_Repr(v);
743 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000744 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000745 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000746 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000747
748 case UNARY_INVERT:
749 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000750 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000751 Py_DECREF(v);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000752 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000753 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000754 break;
Guido van Rossum374a9221991-04-04 10:40:29 +0000755
Guido van Rossum50564e81996-01-12 01:13:16 +0000756 case BINARY_POWER:
757 w = POP();
758 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000759 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000760 Py_DECREF(v);
761 Py_DECREF(w);
Guido van Rossum50564e81996-01-12 01:13:16 +0000762 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000763 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000764 break;
765
Guido van Rossum374a9221991-04-04 10:40:29 +0000766 case BINARY_MULTIPLY:
767 w = POP();
768 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000769 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000770 Py_DECREF(v);
771 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000772 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000773 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000774 break;
775
776 case BINARY_DIVIDE:
777 w = POP();
778 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000779 x = PyNumber_Divide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000780 Py_DECREF(v);
781 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000782 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000783 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000784 break;
785
786 case BINARY_MODULO:
787 w = POP();
788 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000789 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000790 Py_DECREF(v);
791 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000792 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000793 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000794 break;
795
796 case BINARY_ADD:
797 w = POP();
798 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000799 if (PyInt_Check(v) && PyInt_Check(w)) {
800 /* INLINE: int + int */
801 register long a, b, i;
802 a = ((PyIntObject*) v)->ob_ival;
803 b = ((PyIntObject*) w)->ob_ival;
804 i = a + b;
805 if ((i^a) < 0 && (i^b) < 0) {
806 PyErr_SetString(PyExc_OverflowError,
807 "integer addition");
808 x = NULL;
809 }
810 else
811 x = PyInt_FromLong(i);
812 }
813 else
814 x = PyNumber_Add(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000815 Py_DECREF(v);
816 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000817 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000818 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000819 break;
820
821 case BINARY_SUBTRACT:
822 w = POP();
823 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000824 if (PyInt_Check(v) && PyInt_Check(w)) {
825 /* INLINE: int - int */
826 register long a, b, i;
827 a = ((PyIntObject*) v)->ob_ival;
828 b = ((PyIntObject*) w)->ob_ival;
829 i = a - b;
830 if ((i^a) < 0 && (i^~b) < 0) {
831 PyErr_SetString(PyExc_OverflowError,
832 "integer subtraction");
833 x = NULL;
834 }
835 else
836 x = PyInt_FromLong(i);
837 }
838 else
839 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000840 Py_DECREF(v);
841 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000842 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000843 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000844 break;
845
846 case BINARY_SUBSCR:
847 w = POP();
848 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000849 if (PyList_Check(v) && PyInt_Check(w)) {
850 /* INLINE: list[int] */
851 long i = PyInt_AsLong(w);
852 if (i < 0)
853 i += ((PyListObject*) v)->ob_size;
854 if (i < 0 ||
855 i >= ((PyListObject*) v)->ob_size) {
856 PyErr_SetString(PyExc_IndexError,
857 "list index out of range");
858 x = NULL;
859 }
860 else {
861 x = ((PyListObject*) v)->ob_item[i];
862 Py_INCREF(x);
863 }
864 }
865 else
866 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000867 Py_DECREF(v);
868 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000869 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000870 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000871 break;
872
Guido van Rossum7928cd71991-10-24 14:59:31 +0000873 case BINARY_LSHIFT:
874 w = POP();
875 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000876 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000877 Py_DECREF(v);
878 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000879 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000880 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000881 break;
882
883 case BINARY_RSHIFT:
884 w = POP();
885 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000886 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000887 Py_DECREF(v);
888 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000889 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000890 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000891 break;
892
893 case BINARY_AND:
894 w = POP();
895 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000896 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000897 Py_DECREF(v);
898 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000899 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000900 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000901 break;
902
903 case BINARY_XOR:
904 w = POP();
905 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000906 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000907 Py_DECREF(v);
908 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000909 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000910 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000911 break;
912
913 case BINARY_OR:
914 w = POP();
915 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000916 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000917 Py_DECREF(v);
918 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000919 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000920 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000921 break;
922
Guido van Rossum374a9221991-04-04 10:40:29 +0000923 case SLICE+0:
924 case SLICE+1:
925 case SLICE+2:
926 case SLICE+3:
927 if ((opcode-SLICE) & 2)
928 w = POP();
929 else
930 w = NULL;
931 if ((opcode-SLICE) & 1)
932 v = POP();
933 else
934 v = NULL;
935 u = POP();
936 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000937 Py_DECREF(u);
938 Py_XDECREF(v);
939 Py_XDECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000940 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000941 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000942 break;
943
944 case STORE_SLICE+0:
945 case STORE_SLICE+1:
946 case STORE_SLICE+2:
947 case STORE_SLICE+3:
948 if ((opcode-STORE_SLICE) & 2)
949 w = POP();
950 else
951 w = NULL;
952 if ((opcode-STORE_SLICE) & 1)
953 v = POP();
954 else
955 v = NULL;
956 u = POP();
957 t = POP();
958 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +0000959 Py_DECREF(t);
960 Py_DECREF(u);
961 Py_XDECREF(v);
962 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000963 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000964 break;
965
966 case DELETE_SLICE+0:
967 case DELETE_SLICE+1:
968 case DELETE_SLICE+2:
969 case DELETE_SLICE+3:
970 if ((opcode-DELETE_SLICE) & 2)
971 w = POP();
972 else
973 w = NULL;
974 if ((opcode-DELETE_SLICE) & 1)
975 v = POP();
976 else
977 v = NULL;
978 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000979 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +0000980 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +0000981 Py_DECREF(u);
982 Py_XDECREF(v);
983 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000984 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000985 break;
986
987 case STORE_SUBSCR:
988 w = POP();
989 v = POP();
990 u = POP();
991 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +0000992 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +0000993 Py_DECREF(u);
994 Py_DECREF(v);
995 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000996 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000997 break;
998
999 case DELETE_SUBSCR:
1000 w = POP();
1001 v = POP();
1002 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001003 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001004 Py_DECREF(v);
1005 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001006 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001007 break;
1008
1009 case PRINT_EXPR:
1010 v = POP();
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001011 /* Print value except if None */
1012 /* After printing, also assign to '_' */
1013 /* Before, set '_' to None to avoid recursion */
Guido van Rossumb209a111997-04-29 18:18:01 +00001014 if (v != Py_None &&
Guido van Rossuma027efa1997-05-05 20:56:21 +00001015 (err = PyDict_SetItemString(
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001016 f->f_builtins, "_", Py_None)) == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001017 err = Py_FlushLine();
Guido van Rossumc12da691997-07-17 23:12:42 +00001018 if (err == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001019 x = PySys_GetObject("stdout");
Guido van Rossum8f183201997-12-31 05:53:15 +00001020 if (x == NULL) {
1021 PyErr_SetString(
1022 PyExc_RuntimeError,
1023 "lost sys.stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00001024 err = -1;
Guido van Rossum8f183201997-12-31 05:53:15 +00001025 }
Guido van Rossumbe270261997-05-22 22:26:18 +00001026 }
1027 if (err == 0)
1028 err = PyFile_WriteObject(v, x, 0);
1029 if (err == 0) {
1030 PyFile_SoftSpace(x, 1);
1031 err = Py_FlushLine();
1032 }
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001033 if (err == 0) {
1034 err = PyDict_SetItemString(
1035 f->f_builtins, "_", v);
1036 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001037 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001038 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001039 break;
1040
1041 case PRINT_ITEM:
1042 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001043 w = PySys_GetObject("stdout");
Guido van Rossum8f183201997-12-31 05:53:15 +00001044 if (w == NULL) {
1045 PyErr_SetString(PyExc_RuntimeError,
1046 "lost sys.stdout");
1047 err = -1;
1048 }
1049 else if (PyFile_SoftSpace(w, 1))
Guido van Rossumbe270261997-05-22 22:26:18 +00001050 err = PyFile_WriteString(" ", w);
1051 if (err == 0)
1052 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Guido van Rossumb209a111997-04-29 18:18:01 +00001053 if (err == 0 && PyString_Check(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +00001054 /* XXX move into writeobject() ? */
Guido van Rossumb209a111997-04-29 18:18:01 +00001055 char *s = PyString_AsString(v);
1056 int len = PyString_Size(v);
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001057 if (len > 0 &&
1058 isspace(Py_CHARMASK(s[len-1])) &&
Guido van Rossumc6004111993-11-05 10:22:19 +00001059 s[len-1] != ' ')
Guido van Rossumb209a111997-04-29 18:18:01 +00001060 PyFile_SoftSpace(w, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +00001061 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001062 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001063 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 break;
1065
1066 case PRINT_NEWLINE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001067 x = PySys_GetObject("stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001068 if (x == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00001069 PyErr_SetString(PyExc_RuntimeError,
1070 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001071 else {
Guido van Rossumbe270261997-05-22 22:26:18 +00001072 err = PyFile_WriteString("\n", x);
1073 if (err == 0)
1074 PyFile_SoftSpace(x, 0);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001075 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001076 break;
1077
1078 case BREAK_LOOP:
1079 why = WHY_BREAK;
1080 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001081
Guido van Rossumf10570b1995-07-07 22:53:21 +00001082 case RAISE_VARARGS:
1083 u = v = w = NULL;
1084 switch (oparg) {
1085 case 3:
1086 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001087 /* Fallthrough */
1088 case 2:
1089 v = POP(); /* value */
1090 /* Fallthrough */
1091 case 1:
1092 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001093 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001094 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001095 break;
1096 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001097 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001098 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001099 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001100 break;
1101 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001102 break;
1103
1104 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001105 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001106 PyErr_SetString(PyExc_SystemError,
1107 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001108 break;
1109 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001110 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001111 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001112 break;
1113
1114 case RETURN_VALUE:
1115 retval = POP();
1116 why = WHY_RETURN;
1117 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001118
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001119 case EXEC_STMT:
1120 w = POP();
1121 v = POP();
1122 u = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001123 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001124 Py_DECREF(u);
1125 Py_DECREF(v);
1126 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001127 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001128
Guido van Rossum374a9221991-04-04 10:40:29 +00001129 case POP_BLOCK:
1130 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001131 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001132 while (STACK_LEVEL() > b->b_level) {
1133 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001134 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001135 }
1136 }
1137 break;
1138
1139 case END_FINALLY:
1140 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001141 if (PyInt_Check(v)) {
1142 why = (enum why_code) PyInt_AsLong(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001143 if (why == WHY_RETURN)
1144 retval = POP();
1145 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001146 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001147 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001148 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001149 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001150 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001151 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001152 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001153 else if (v != Py_None) {
1154 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001155 "'finally' pops bad exception");
1156 why = WHY_EXCEPTION;
1157 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001158 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001159 break;
1160
1161 case BUILD_CLASS:
Guido van Rossum25831651993-05-19 14:50:45 +00001162 u = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001163 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +00001164 w = POP();
1165 x = build_class(u, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001166 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001167 Py_DECREF(u);
1168 Py_DECREF(v);
1169 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001170 break;
1171
1172 case STORE_NAME:
1173 w = GETNAMEV(oparg);
1174 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001175 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001176 PyErr_SetString(PyExc_SystemError,
1177 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001178 break;
1179 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001180 err = PyDict_SetItem(x, w, v);
1181 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001182 break;
1183
1184 case DELETE_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001185 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001186 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001187 PyErr_SetString(PyExc_SystemError,
1188 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001189 break;
1190 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001191 if ((err = PyDict_DelItem(x, w)) != 0)
1192 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001193 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001194
1195#ifdef CASE_TOO_BIG
1196 default: switch (opcode) {
1197#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001198
1199 case UNPACK_TUPLE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001200 case UNPACK_LIST:
1201 v = POP();
Barry Warsawe42b18f1997-08-25 22:13:04 +00001202 if (PyTuple_Check(v)) {
1203 if (PyTuple_Size(v) != oparg) {
1204 PyErr_SetString(PyExc_ValueError,
1205 "unpack tuple of wrong size");
1206 why = WHY_EXCEPTION;
1207 }
1208 else {
1209 for (; --oparg >= 0; ) {
1210 w = PyTuple_GET_ITEM(v, oparg);
1211 Py_INCREF(w);
1212 PUSH(w);
1213 }
1214 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001215 }
Barry Warsawe42b18f1997-08-25 22:13:04 +00001216 else if (PyList_Check(v)) {
1217 if (PyList_Size(v) != oparg) {
1218 PyErr_SetString(PyExc_ValueError,
1219 "unpack list of wrong size");
1220 why = WHY_EXCEPTION;
1221 }
1222 else {
1223 for (; --oparg >= 0; ) {
1224 w = PyList_GET_ITEM(v, oparg);
1225 Py_INCREF(w);
1226 PUSH(w);
1227 }
1228 }
1229 }
1230 else if (PySequence_Check(v)) {
1231 if (unpack_sequence(v, oparg,
1232 stack_pointer + oparg))
1233 stack_pointer += oparg;
1234 else
1235 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001236 }
1237 else {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001238 PyErr_SetString(PyExc_TypeError,
1239 "unpack non-sequence");
1240 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001241 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001242 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001243 break;
1244
1245 case STORE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001246 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001247 v = POP();
1248 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001249 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1250 Py_DECREF(v);
1251 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001252 break;
1253
1254 case DELETE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001255 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001256 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001257 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1258 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001259 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001260 break;
1261
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001262 case STORE_GLOBAL:
1263 w = GETNAMEV(oparg);
1264 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001265 err = PyDict_SetItem(f->f_globals, w, v);
1266 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001267 break;
1268
1269 case DELETE_GLOBAL:
1270 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001271 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1272 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001273 break;
1274
Guido van Rossum374a9221991-04-04 10:40:29 +00001275 case LOAD_CONST:
1276 x = GETCONST(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001277 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001278 PUSH(x);
1279 break;
1280
1281 case LOAD_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001282 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001283 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001284 PyErr_SetString(PyExc_SystemError,
1285 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001286 break;
1287 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001288 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001289 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001290 PyErr_Clear();
1291 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001292 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001293 PyErr_Clear();
1294 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001295 if (x == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001296 PyErr_SetObject(
1297 PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001298 break;
1299 }
1300 }
1301 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001302 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001303 PUSH(x);
1304 break;
1305
1306 case LOAD_GLOBAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001307 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001308 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001309 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001310 PyErr_Clear();
1311 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001312 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001313 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001314 break;
1315 }
1316 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001317 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001318 PUSH(x);
1319 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001320
Guido van Rossum9bfef441993-03-29 10:43:31 +00001321 case LOAD_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001322 x = GETLOCAL(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001323 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001324 PyErr_SetObject(PyExc_NameError,
1325 PyTuple_GetItem(co->co_varnames,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001326 oparg));
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001327 break;
1328 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001329 Py_INCREF(x);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001330 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001331 if (x != NULL) continue;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001332 break;
1333
1334 case STORE_FAST:
Guido van Rossum25831651993-05-19 14:50:45 +00001335 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001336 SETLOCAL(oparg, v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001337 continue;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001338
1339 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001340 x = GETLOCAL(oparg);
1341 if (x == NULL) {
1342 PyErr_SetObject(PyExc_NameError,
1343 PyTuple_GetItem(co->co_varnames,
1344 oparg));
1345 break;
1346 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001347 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001348 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001349
1350 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001351 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001352 if (x != NULL) {
1353 for (; --oparg >= 0;) {
1354 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001355 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001356 }
1357 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001358 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001359 }
1360 break;
1361
1362 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001363 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001364 if (x != NULL) {
1365 for (; --oparg >= 0;) {
1366 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001367 err = PyList_SetItem(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001368 if (err != 0)
1369 break;
1370 }
1371 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001372 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001373 }
1374 break;
1375
1376 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001377 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001378 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001379 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001380 break;
1381
1382 case LOAD_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001383 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001384 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001385 x = PyObject_GetAttr(v, w);
1386 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001387 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001388 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001389 break;
1390
1391 case COMPARE_OP:
1392 w = POP();
1393 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +00001394 if (PyInt_Check(v) && PyInt_Check(w)) {
1395 /* INLINE: cmp(int, int) */
1396 register long a, b;
1397 register int res;
1398 a = ((PyIntObject*) v)->ob_ival;
1399 b = ((PyIntObject*) w)->ob_ival;
1400 switch (oparg) {
1401 case LT: res = a < b; break;
1402 case LE: res = a <= b; break;
1403 case EQ: res = a == b; break;
1404 case NE: res = a != b; break;
1405 case GT: res = a > b; break;
1406 case GE: res = a >= b; break;
1407 case IS: res = v == w; break;
1408 case IS_NOT: res = v != w; break;
1409 default: goto slow_compare;
1410 }
1411 x = res ? Py_True : Py_False;
1412 Py_INCREF(x);
1413 }
1414 else {
1415 slow_compare:
1416 x = cmp_outcome(oparg, v, w);
1417 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001418 Py_DECREF(v);
1419 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001420 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001421 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 break;
1423
1424 case IMPORT_NAME:
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001425 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001426 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001427 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001428 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001429 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001430 break;
1431 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001432 u = find_from_args(f, INSTR_OFFSET());
1433 if (u == NULL) {
1434 x = u;
1435 break;
Guido van Rossum24c13741995-02-14 09:42:43 +00001436 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001437 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001438 w,
1439 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001440 f->f_locals == NULL ?
1441 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001442 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001443 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001444 if (w == NULL) {
1445 x = NULL;
1446 break;
1447 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001448 x = PyEval_CallObject(x, w);
1449 Py_DECREF(w);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001450 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001451 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 break;
1453
1454 case IMPORT_FROM:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001455 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001456 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001457 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001458 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001459 PyErr_SetString(PyExc_SystemError,
1460 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001461 break;
1462 }
1463 err = import_from(x, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001464 PyFrame_LocalsToFast(f, 0);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001465 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001466 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001467
Guido van Rossum374a9221991-04-04 10:40:29 +00001468 case JUMP_FORWARD:
1469 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001470 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001471
1472 case JUMP_IF_FALSE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001473 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001474 if (err > 0)
1475 err = 0;
1476 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001478 else
1479 break;
1480 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001481
1482 case JUMP_IF_TRUE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001483 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001484 if (err > 0) {
1485 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001487 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001488 else if (err == 0)
1489 ;
1490 else
1491 break;
1492 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001493
1494 case JUMP_ABSOLUTE:
1495 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001496 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001497
1498 case FOR_LOOP:
1499 /* for v in s: ...
1500 On entry: stack contains s, i.
1501 On exit: stack contains s, i+1, s[i];
1502 but if loop exhausted:
1503 s, i are popped, and we jump */
1504 w = POP(); /* Loop index */
1505 v = POP(); /* Sequence object */
1506 u = loop_subscript(v, w);
1507 if (u != NULL) {
1508 PUSH(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001509 x = PyInt_FromLong(PyInt_AsLong(w)+1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001511 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001512 PUSH(u);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001513 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 }
1515 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001516 Py_DECREF(v);
1517 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001518 /* A NULL can mean "s exhausted"
1519 but also an error: */
Guido van Rossumb209a111997-04-29 18:18:01 +00001520 if (PyErr_Occurred())
Guido van Rossum374a9221991-04-04 10:40:29 +00001521 why = WHY_EXCEPTION;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001522 else {
Guido van Rossum374a9221991-04-04 10:40:29 +00001523 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001524 continue;
1525 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001526 }
1527 break;
1528
1529 case SETUP_LOOP:
1530 case SETUP_EXCEPT:
1531 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00001532 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Guido van Rossum374a9221991-04-04 10:40:29 +00001533 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001534 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001535
1536 case SET_LINENO:
Guido van Rossum96a42c81992-01-12 02:29:51 +00001537#ifdef LLTRACE
1538 if (lltrace)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001539 printf("--- %s:%d \n", filename, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001540#endif
Guido van Rossum96a42c81992-01-12 02:29:51 +00001541 f->f_lineno = oparg;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001542 if (f->f_trace == NULL)
1543 continue;
1544 /* Trace each line of code reached */
1545 f->f_lasti = INSTR_OFFSET();
1546 err = call_trace(&f->f_trace, &f->f_trace,
Guido van Rossumb209a111997-04-29 18:18:01 +00001547 f, "line", Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00001548 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001549
1550 case CALL_FUNCTION:
1551 {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001552 int na = oparg & 0xff;
1553 int nk = (oparg>>8) & 0xff;
1554 int n = na + 2*nk;
Guido van Rossumb209a111997-04-29 18:18:01 +00001555 PyObject **pfunc = stack_pointer - n - 1;
1556 PyObject *func = *pfunc;
1557 PyObject *self = NULL;
1558 PyObject *class = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001559 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
Guido van Rossumb209a111997-04-29 18:18:01 +00001560 if (PyMethod_Check(func)) {
1561 self = PyMethod_Self(func);
1562 class = PyMethod_Class(func);
1563 func = PyMethod_Function(func);
1564 Py_INCREF(func);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001565 if (self != NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001566 Py_INCREF(self);
1567 Py_DECREF(*pfunc);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001568 *pfunc = self;
1569 na++;
1570 n++;
1571 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001572 else {
1573 /* Unbound methods must be
1574 called with an instance of
1575 the class (or a derived
1576 class) as first argument */
1577 if (na > 0 &&
1578 (self = stack_pointer[-n])
1579 != NULL &&
Guido van Rossumb209a111997-04-29 18:18:01 +00001580 PyInstance_Check(self) &&
1581 PyClass_IsSubclass(
1582 (PyObject *)
1583 (((PyInstanceObject *)self)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001584 ->in_class),
1585 class))
1586 /* Handy-dandy */ ;
1587 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001588 PyErr_SetString(
1589 PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001590 "unbound method must be called with class instance 1st argument");
Guido van Rossum72b56e81995-12-10 04:57:42 +00001591 x = NULL;
1592 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001593 }
1594 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001595 }
1596 else
Guido van Rossumb209a111997-04-29 18:18:01 +00001597 Py_INCREF(func);
1598 if (PyFunction_Check(func)) {
1599 PyObject *co = PyFunction_GetCode(func);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001600 PyObject *globals =
1601 PyFunction_GetGlobals(func);
1602 PyObject *argdefs =
1603 PyFunction_GetDefaults(func);
Guido van Rossumb209a111997-04-29 18:18:01 +00001604 PyObject **d;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001605 int nd;
1606 if (argdefs != NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001607 d = &PyTuple_GET_ITEM(argdefs, 0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001608 nd = ((PyTupleObject *)argdefs) ->
1609 ob_size;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001610 }
1611 else {
1612 d = NULL;
1613 nd = 0;
1614 }
1615 x = eval_code2(
Guido van Rossumb209a111997-04-29 18:18:01 +00001616 (PyCodeObject *)co,
1617 globals, (PyObject *)NULL,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001618 stack_pointer-n, na,
1619 stack_pointer-2*nk, nk,
1620 d, nd,
1621 class);
1622 }
1623 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001624 PyObject *args = PyTuple_New(na);
1625 PyObject *kwdict = NULL;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001626 if (args == NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001627 x = NULL;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001628 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001629 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001630 if (nk > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001631 kwdict = PyDict_New();
Guido van Rossumff8b4941995-07-26 18:16:42 +00001632 if (kwdict == NULL) {
1633 x = NULL;
1634 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001635 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001636 err = 0;
1637 while (--nk >= 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001638 PyObject *value = POP();
1639 PyObject *key = POP();
1640 err = PyDict_SetItem(
Guido van Rossumff8b4941995-07-26 18:16:42 +00001641 kwdict, key, value);
Guido van Rossumb209a111997-04-29 18:18:01 +00001642 Py_DECREF(key);
1643 Py_DECREF(value);
Guido van Rossumd0eb4291997-01-27 21:30:09 +00001644 if (err)
Guido van Rossumff8b4941995-07-26 18:16:42 +00001645 break;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001646 }
1647 if (err) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001648 Py_DECREF(args);
1649 Py_DECREF(kwdict);
Guido van Rossumff8b4941995-07-26 18:16:42 +00001650 break;
1651 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001652 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001653 while (--na >= 0) {
1654 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001655 PyTuple_SET_ITEM(args, na, w);
Guido van Rossumff8b4941995-07-26 18:16:42 +00001656 }
1657 x = PyEval_CallObjectWithKeywords(
1658 func, args, kwdict);
Guido van Rossumb209a111997-04-29 18:18:01 +00001659 Py_DECREF(args);
1660 Py_XDECREF(kwdict);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001661 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001662 Py_DECREF(func);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001663 while (stack_pointer > pfunc) {
1664 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001665 Py_DECREF(w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001666 }
1667 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001668 if (x != NULL) continue;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001669 break;
1670 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001671
Guido van Rossum681d79a1995-07-18 14:51:37 +00001672 case MAKE_FUNCTION:
1673 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00001674 x = PyFunction_New(v, f->f_globals);
1675 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001676 /* XXX Maybe this should be a separate opcode? */
1677 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001678 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001679 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001680 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001681 x = NULL;
1682 break;
1683 }
1684 while (--oparg >= 0) {
1685 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001686 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001687 }
1688 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001689 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001690 }
1691 PUSH(x);
1692 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001693
1694 case BUILD_SLICE:
1695 if (oparg == 3)
1696 w = POP();
1697 else
1698 w = NULL;
1699 v = POP();
1700 u = POP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00001701 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001702 Py_DECREF(u);
1703 Py_DECREF(v);
1704 Py_XDECREF(w);
Guido van Rossum8861b741996-07-30 16:49:37 +00001705 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001706 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00001707 break;
1708
1709
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 default:
1711 fprintf(stderr,
1712 "XXX lineno: %d, opcode: %d\n",
Guido van Rossum96a42c81992-01-12 02:29:51 +00001713 f->f_lineno, opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00001714 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00001715 why = WHY_EXCEPTION;
1716 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001717
1718#ifdef CASE_TOO_BIG
1719 }
1720#endif
1721
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 } /* switch */
1723
1724 on_error:
1725
1726 /* Quickly continue if no error occurred */
1727
1728 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001729 if (err == 0 && x != NULL) {
1730#ifdef CHECKEXC
Guido van Rossumb209a111997-04-29 18:18:01 +00001731 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00001732 fprintf(stderr,
1733 "XXX undetected error\n");
1734 else
1735#endif
1736 continue; /* Normal, fast path */
1737 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001738 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00001739 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00001740 err = 0;
1741 }
1742
Guido van Rossum801dcae1992-04-08 11:32:32 +00001743#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00001744 /* Double-check exception status */
1745
1746 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001747 if (!PyErr_Occurred()) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001748 fprintf(stderr, "XXX ghost error\n");
Guido van Rossuma027efa1997-05-05 20:56:21 +00001749 PyErr_SetString(PyExc_SystemError,
1750 "ghost error");
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 why = WHY_EXCEPTION;
1752 }
1753 }
1754 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001755 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001756 fprintf(stderr,
1757 "XXX undetected error (why=%d)\n",
1758 why);
1759 why = WHY_EXCEPTION;
1760 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001761 }
1762#endif
1763
1764 /* Log traceback info if this is a real exception */
1765
1766 if (why == WHY_EXCEPTION) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001767 f->f_lasti = INSTR_OFFSET() - 1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001768 if (HAS_ARG(opcode))
Guido van Rossum96a42c81992-01-12 02:29:51 +00001769 f->f_lasti -= 2;
Guido van Rossumb209a111997-04-29 18:18:01 +00001770 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001771
Guido van Rossume59214e1994-08-30 08:01:59 +00001772 if (f->f_trace)
1773 call_exc_trace(&f->f_trace, &f->f_trace, f);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001774 if (tstate->sys_profilefunc)
1775 call_exc_trace(&tstate->sys_profilefunc,
1776 (PyObject**)0, f);
1777}
Guido van Rossum374a9221991-04-04 10:40:29 +00001778
1779 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
1780
1781 if (why == WHY_RERAISE)
1782 why = WHY_EXCEPTION;
1783
1784 /* Unwind stacks if a (pseudo) exception occurred */
1785
1786 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001787 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001788 while (STACK_LEVEL() > b->b_level) {
1789 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001790 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001791 }
1792 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
1793 why = WHY_NOT;
1794 JUMPTO(b->b_handler);
1795 break;
1796 }
1797 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00001798 (b->b_type == SETUP_EXCEPT &&
1799 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001800 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001801 PyObject *exc, *val, *tb;
1802 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001803 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001804 val = Py_None;
1805 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00001806 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 /* Make the raw exception data
1808 available to the handler,
1809 so a program can emulate the
1810 Python main loop. Don't do
1811 this for 'finally'. */
1812 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00001813 PyErr_NormalizeException(
1814 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001815 set_exc_info(tstate,
1816 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001818 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001819 PUSH(val);
1820 PUSH(exc);
1821 }
1822 else {
1823 if (why == WHY_RETURN)
1824 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00001825 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00001826 PUSH(v);
1827 }
1828 why = WHY_NOT;
1829 JUMPTO(b->b_handler);
1830 break;
1831 }
1832 } /* unwind stack */
1833
1834 /* End the loop if we still have an error (or return) */
1835
1836 if (why != WHY_NOT)
1837 break;
1838
1839 } /* main loop */
1840
1841 /* Pop remaining stack entries */
1842
1843 while (!EMPTY()) {
1844 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001845 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001846 }
1847
Guido van Rossum96a42c81992-01-12 02:29:51 +00001848 if (why != WHY_RETURN)
1849 retval = NULL;
1850
Guido van Rossume59214e1994-08-30 08:01:59 +00001851 if (f->f_trace) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001852 if (why == WHY_RETURN) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001853 if (call_trace(&f->f_trace, &f->f_trace, f,
1854 "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001855 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001856 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001857 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001858 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001859 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001860 }
1861
Guido van Rossuma027efa1997-05-05 20:56:21 +00001862 if (tstate->sys_profilefunc && why == WHY_RETURN) {
1863 if (call_trace(&tstate->sys_profilefunc, (PyObject**)0,
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001864 f, "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001865 Py_XDECREF(retval);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001866 retval = NULL;
1867 why = WHY_EXCEPTION;
1868 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001869 }
Guido van Rossuma4240131997-01-21 21:18:36 +00001870
Guido van Rossuma027efa1997-05-05 20:56:21 +00001871 reset_exc_info(tstate);
1872
1873 --tstate->recursion_depth;
Guido van Rossum8c5df061997-01-24 04:19:24 +00001874
1875 fail: /* Jump here from prelude on failure */
Guido van Rossum96a42c81992-01-12 02:29:51 +00001876
Guido van Rossum374a9221991-04-04 10:40:29 +00001877 /* Restore previous frame and release the current one */
Guido van Rossum681d79a1995-07-18 14:51:37 +00001878
Guido van Rossuma027efa1997-05-05 20:56:21 +00001879 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +00001880 Py_DECREF(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001881
Guido van Rossum96a42c81992-01-12 02:29:51 +00001882 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00001883}
1884
Guido van Rossuma027efa1997-05-05 20:56:21 +00001885static void
1886set_exc_info(tstate, type, value, tb)
1887 PyThreadState *tstate;
1888 PyObject *type;
1889 PyObject *value;
1890 PyObject *tb;
1891{
1892 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001893 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00001894
Guido van Rossuma027efa1997-05-05 20:56:21 +00001895 frame = tstate->frame;
1896 if (frame->f_exc_type == NULL) {
1897 /* This frame didn't catch an exception before */
1898 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00001899 if (tstate->exc_type == NULL) {
1900 Py_INCREF(Py_None);
1901 tstate->exc_type = Py_None;
1902 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001903 tmp_type = frame->f_exc_type;
1904 tmp_value = frame->f_exc_value;
1905 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001906 Py_XINCREF(tstate->exc_type);
1907 Py_XINCREF(tstate->exc_value);
1908 Py_XINCREF(tstate->exc_traceback);
1909 frame->f_exc_type = tstate->exc_type;
1910 frame->f_exc_value = tstate->exc_value;
1911 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001912 Py_XDECREF(tmp_type);
1913 Py_XDECREF(tmp_value);
1914 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001915 }
1916 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001917 tmp_type = tstate->exc_type;
1918 tmp_value = tstate->exc_value;
1919 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001920 Py_XINCREF(type);
1921 Py_XINCREF(value);
1922 Py_XINCREF(tb);
1923 tstate->exc_type = type;
1924 tstate->exc_value = value;
1925 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001926 Py_XDECREF(tmp_type);
1927 Py_XDECREF(tmp_value);
1928 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001929 /* For b/w compatibility */
1930 PySys_SetObject("exc_type", type);
1931 PySys_SetObject("exc_value", value);
1932 PySys_SetObject("exc_traceback", tb);
1933}
1934
1935static void
1936reset_exc_info(tstate)
1937 PyThreadState *tstate;
1938{
1939 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001940 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001941 frame = tstate->frame;
1942 if (frame->f_exc_type != NULL) {
1943 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001944 tmp_type = tstate->exc_type;
1945 tmp_value = tstate->exc_value;
1946 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001947 Py_XINCREF(frame->f_exc_type);
1948 Py_XINCREF(frame->f_exc_value);
1949 Py_XINCREF(frame->f_exc_traceback);
1950 tstate->exc_type = frame->f_exc_type;
1951 tstate->exc_value = frame->f_exc_value;
1952 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001953 Py_XDECREF(tmp_type);
1954 Py_XDECREF(tmp_value);
1955 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001956 /* For b/w compatibility */
1957 PySys_SetObject("exc_type", frame->f_exc_type);
1958 PySys_SetObject("exc_value", frame->f_exc_value);
1959 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
1960 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001961 tmp_type = frame->f_exc_type;
1962 tmp_value = frame->f_exc_value;
1963 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001964 frame->f_exc_type = NULL;
1965 frame->f_exc_value = NULL;
1966 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001967 Py_XDECREF(tmp_type);
1968 Py_XDECREF(tmp_value);
1969 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001970}
1971
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001972/* Logic for the raise statement (too complicated for inlining).
1973 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00001974static enum why_code
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001975do_raise(type, value, tb)
Guido van Rossumb209a111997-04-29 18:18:01 +00001976 PyObject *type, *value, *tb;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001977{
Guido van Rossumd295f121998-04-09 21:39:57 +00001978 if (type == NULL) {
1979 /* Reraise */
1980 PyThreadState *tstate = PyThreadState_Get();
1981 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
1982 value = tstate->exc_value;
1983 tb = tstate->exc_traceback;
1984 Py_XINCREF(type);
1985 Py_XINCREF(value);
1986 Py_XINCREF(tb);
1987 }
1988
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001989 /* We support the following forms of raise:
1990 raise <class>, <classinstance>
1991 raise <class>, <argument tuple>
1992 raise <class>, None
1993 raise <class>, <argument>
1994 raise <classinstance>, None
1995 raise <string>, <object>
1996 raise <string>, None
1997
1998 An omitted second argument is the same as None.
1999
2000 In addition, raise <tuple>, <anything> is the same as
2001 raising the tuple's first item (and it better have one!);
2002 this rule is applied recursively.
2003
2004 Finally, an optional third argument can be supplied, which
2005 gives the traceback to be substituted (useful when
2006 re-raising an exception after examining it). */
2007
2008 /* First, check the traceback argument, replacing None with
2009 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002010 if (tb == Py_None) {
2011 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002012 tb = NULL;
2013 }
2014 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002015 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002016 "raise 3rd arg must be traceback or None");
2017 goto raise_error;
2018 }
2019
2020 /* Next, replace a missing value with None */
2021 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002022 value = Py_None;
2023 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002024 }
2025
2026 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002027 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2028 PyObject *tmp = type;
2029 type = PyTuple_GET_ITEM(type, 0);
2030 Py_INCREF(type);
2031 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002032 }
2033
Barry Warsaw4249f541997-08-22 21:26:19 +00002034 if (PyString_Check(type))
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002035 ;
Barry Warsaw4249f541997-08-22 21:26:19 +00002036
2037 else if (PyClass_Check(type))
2038 PyErr_NormalizeException(&type, &value, &tb);
2039
Guido van Rossumb209a111997-04-29 18:18:01 +00002040 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002041 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002042 if (value != Py_None) {
2043 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002044 "instance exception may not have a separate value");
2045 goto raise_error;
2046 }
2047 else {
2048 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002050 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002051 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2052 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002053 }
2054 }
2055 else {
2056 /* Not something you can raise. You get an exception
2057 anyway, just not what you specified :-) */
Guido van Rossumb209a111997-04-29 18:18:01 +00002058 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002059 "exceptions must be strings, classes, or instances");
2060 goto raise_error;
2061 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002062 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002063 if (tb == NULL)
2064 return WHY_EXCEPTION;
2065 else
2066 return WHY_RERAISE;
2067 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002068 Py_XDECREF(value);
2069 Py_XDECREF(type);
2070 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002071 return WHY_EXCEPTION;
2072}
2073
Barry Warsawe42b18f1997-08-25 22:13:04 +00002074static int
2075unpack_sequence(v, argcnt, sp)
2076 PyObject *v;
2077 int argcnt;
2078 PyObject **sp;
2079{
2080 int i;
2081 PyObject *w;
2082
2083 for (i = 0; i < argcnt; i++) {
2084 if (! (w = PySequence_GetItem(v, i))) {
2085 if (PyErr_ExceptionMatches(PyExc_IndexError))
2086 PyErr_SetString(PyExc_ValueError,
2087 "unpack sequence of wrong size");
2088 goto finally;
2089 }
2090 *--sp = w;
2091 }
2092 /* we better get an IndexError now */
2093 if (PySequence_GetItem(v, i) == NULL) {
2094 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2095 PyErr_Clear();
2096 return 1;
2097 }
2098 /* some other exception occurred. fall through to finally */
2099 }
2100 else
2101 PyErr_SetString(PyExc_ValueError,
2102 "unpack sequence of wrong size");
2103 /* fall through */
2104finally:
Barry Warsaw91010551997-08-25 22:30:51 +00002105 for (; i > 0; i--, sp++)
2106 Py_DECREF(*sp);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002107
2108 return 0;
2109}
2110
2111
Guido van Rossum96a42c81992-01-12 02:29:51 +00002112#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002113static int
2114prtrace(v, str)
Guido van Rossumb209a111997-04-29 18:18:01 +00002115 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002116 char *str;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002117{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002118 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002119 if (PyObject_Print(v, stdout, 0) != 0)
2120 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002121 printf("\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002122}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002123#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002124
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002125static void
2126call_exc_trace(p_trace, p_newtrace, f)
Guido van Rossumb209a111997-04-29 18:18:01 +00002127 PyObject **p_trace, **p_newtrace;
2128 PyFrameObject *f;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002129{
Guido van Rossumb209a111997-04-29 18:18:01 +00002130 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002131 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002132 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002133 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002134 value = Py_None;
2135 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002136 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002137 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002138 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002139 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002140 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002141 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002142 err = call_trace(p_trace, p_newtrace, f, "exception", arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002143 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002144 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002145 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002146 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002147 Py_XDECREF(type);
2148 Py_XDECREF(value);
2149 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002150 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002151}
2152
2153static int
2154call_trace(p_trace, p_newtrace, f, msg, arg)
Guido van Rossumb209a111997-04-29 18:18:01 +00002155 PyObject **p_trace; /* in/out; may not be NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002156 may not point to NULL variable initially */
Guido van Rossumb209a111997-04-29 18:18:01 +00002157 PyObject **p_newtrace; /* in/out; may be NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002158 may point to NULL variable;
2159 may be same variable as p_newtrace */
Guido van Rossumb209a111997-04-29 18:18:01 +00002160 PyFrameObject *f;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002161 char *msg;
Guido van Rossumb209a111997-04-29 18:18:01 +00002162 PyObject *arg;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002163{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002164 PyThreadState *tstate = f->f_tstate;
Guido van Rossumb209a111997-04-29 18:18:01 +00002165 PyObject *args, *what;
2166 PyObject *res = NULL;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002167
Guido van Rossuma027efa1997-05-05 20:56:21 +00002168 if (tstate->tracing) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002169 /* Don't do recursive traces */
2170 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002171 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002172 *p_newtrace = NULL;
2173 }
2174 return 0;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002175 }
2176
Guido van Rossumb209a111997-04-29 18:18:01 +00002177 args = PyTuple_New(3);
Guido van Rossumf10570b1995-07-07 22:53:21 +00002178 if (args == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002179 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002180 what = PyString_FromString(msg);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002181 if (what == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002182 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002183 Py_INCREF(f);
2184 PyTuple_SET_ITEM(args, 0, (PyObject *)f);
2185 PyTuple_SET_ITEM(args, 1, what);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002186 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002187 arg = Py_None;
2188 Py_INCREF(arg);
2189 PyTuple_SET_ITEM(args, 2, arg);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002190 tstate->tracing++;
Guido van Rossumb209a111997-04-29 18:18:01 +00002191 PyFrame_FastToLocals(f);
2192 res = PyEval_CallObject(*p_trace, args); /* May clear *p_trace! */
2193 PyFrame_LocalsToFast(f, 1);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002194 tstate->tracing--;
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002195 cleanup:
Guido van Rossumb209a111997-04-29 18:18:01 +00002196 Py_XDECREF(args);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002197 if (res == NULL) {
2198 /* The trace proc raised an exception */
Guido van Rossumb209a111997-04-29 18:18:01 +00002199 PyTraceBack_Here(f);
2200 Py_XDECREF(*p_trace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002201 *p_trace = NULL;
2202 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002203 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002204 *p_newtrace = NULL;
2205 }
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002206 return -1;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002207 }
2208 else {
2209 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002210 Py_XDECREF(*p_newtrace);
2211 if (res == Py_None)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002212 *p_newtrace = NULL;
2213 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002214 Py_INCREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002215 *p_newtrace = res;
2216 }
2217 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002218 Py_DECREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002219 return 0;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002220 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002221}
2222
Guido van Rossumb209a111997-04-29 18:18:01 +00002223PyObject *
2224PyEval_GetBuiltins()
Guido van Rossum6135a871995-01-09 17:53:26 +00002225{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002226 PyThreadState *tstate = PyThreadState_Get();
2227 PyFrameObject *current_frame = tstate->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002228 if (current_frame == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002229 return tstate->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00002230 else
2231 return current_frame->f_builtins;
2232}
2233
Guido van Rossumb209a111997-04-29 18:18:01 +00002234PyObject *
2235PyEval_GetLocals()
Guido van Rossum5b722181993-03-30 17:46:03 +00002236{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002237 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum5b722181993-03-30 17:46:03 +00002238 if (current_frame == NULL)
2239 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002240 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00002241 return current_frame->f_locals;
2242}
2243
Guido van Rossumb209a111997-04-29 18:18:01 +00002244PyObject *
2245PyEval_GetGlobals()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002246{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002247 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002248 if (current_frame == NULL)
2249 return NULL;
2250 else
2251 return current_frame->f_globals;
2252}
2253
Guido van Rossumb209a111997-04-29 18:18:01 +00002254PyObject *
2255PyEval_GetFrame()
Guido van Rossume59214e1994-08-30 08:01:59 +00002256{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002257 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossumb209a111997-04-29 18:18:01 +00002258 return (PyObject *)current_frame;
Guido van Rossume59214e1994-08-30 08:01:59 +00002259}
2260
Guido van Rossum6135a871995-01-09 17:53:26 +00002261int
Guido van Rossumb209a111997-04-29 18:18:01 +00002262PyEval_GetRestricted()
Guido van Rossum6135a871995-01-09 17:53:26 +00002263{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002264 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002265 return current_frame == NULL ? 0 : current_frame->f_restricted;
2266}
2267
Guido van Rossumbe270261997-05-22 22:26:18 +00002268int
Guido van Rossumb209a111997-04-29 18:18:01 +00002269Py_FlushLine()
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002270{
Guido van Rossumb209a111997-04-29 18:18:01 +00002271 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00002272 if (f == NULL)
2273 return 0;
2274 if (!PyFile_SoftSpace(f, 0))
2275 return 0;
2276 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002277}
2278
Guido van Rossum3f5da241990-12-20 15:06:42 +00002279
Guido van Rossum681d79a1995-07-18 14:51:37 +00002280/* External interface to call any callable object.
2281 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002282
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002283#undef PyEval_CallObject
2284/* for backward compatibility: export this interface */
2285
Guido van Rossumb209a111997-04-29 18:18:01 +00002286PyObject *
2287PyEval_CallObject(func, arg)
2288 PyObject *func;
2289 PyObject *arg;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002290{
Guido van Rossumb209a111997-04-29 18:18:01 +00002291 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002292}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002293#define PyEval_CallObject(func,arg) \
2294 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00002295
Guido van Rossumb209a111997-04-29 18:18:01 +00002296PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002297PyEval_CallObjectWithKeywords(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002298 PyObject *func;
2299 PyObject *arg;
2300 PyObject *kw;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002301{
2302 ternaryfunc call;
Guido van Rossumb209a111997-04-29 18:18:01 +00002303 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002304
2305 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002306 arg = PyTuple_New(0);
2307 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002308 PyErr_SetString(PyExc_TypeError,
2309 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002310 return NULL;
2311 }
2312 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002313 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002314
Guido van Rossumb209a111997-04-29 18:18:01 +00002315 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002316 PyErr_SetString(PyExc_TypeError,
2317 "keyword list must be a dictionary");
Guido van Rossume3e61c11995-08-04 04:14:47 +00002318 return NULL;
2319 }
2320
Guido van Rossum150b2df1996-12-05 23:17:11 +00002321 if ((call = func->ob_type->tp_call) != NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002322 result = (*call)(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002323 else if (PyMethod_Check(func) || PyFunction_Check(func))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002324 result = call_function(func, arg, kw);
2325 else
2326 result = call_builtin(func, arg, kw);
2327
Guido van Rossumb209a111997-04-29 18:18:01 +00002328 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002329
Guido van Rossumb209a111997-04-29 18:18:01 +00002330 if (result == NULL && !PyErr_Occurred())
2331 PyErr_SetString(PyExc_SystemError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002332 "NULL result without error in call_object");
Guido van Rossume59214e1994-08-30 08:01:59 +00002333
2334 return result;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002335}
2336
Guido van Rossumb209a111997-04-29 18:18:01 +00002337static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002338call_builtin(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002339 PyObject *func;
2340 PyObject *arg;
2341 PyObject *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002342{
Guido van Rossumb209a111997-04-29 18:18:01 +00002343 if (PyCFunction_Check(func)) {
2344 PyCFunction meth = PyCFunction_GetFunction(func);
2345 PyObject *self = PyCFunction_GetSelf(func);
2346 int flags = PyCFunction_GetFlags(func);
Guido van Rossumff8b4941995-07-26 18:16:42 +00002347 if (!(flags & METH_VARARGS)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002348 int size = PyTuple_Size(arg);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002349 if (size == 1)
Guido van Rossumb209a111997-04-29 18:18:01 +00002350 arg = PyTuple_GET_ITEM(arg, 0);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002351 else if (size == 0)
2352 arg = NULL;
2353 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002354 if (flags & METH_KEYWORDS)
2355 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002356 if (kw != NULL && PyDict_Size(kw) != 0) {
2357 PyErr_SetString(PyExc_TypeError,
Guido van Rossumff8b4941995-07-26 18:16:42 +00002358 "this function takes no keyword arguments");
2359 return NULL;
2360 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002361 return (*meth)(self, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002362 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002363 if (PyClass_Check(func)) {
2364 return PyInstance_New(func, arg, kw);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002365 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002366 if (PyInstance_Check(func)) {
2367 PyObject *res, *call = PyObject_GetAttrString(func,"__call__");
Guido van Rossume59214e1994-08-30 08:01:59 +00002368 if (call == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002369 PyErr_Clear();
2370 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002371 "no __call__ method defined");
Guido van Rossume59214e1994-08-30 08:01:59 +00002372 return NULL;
2373 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002374 res = PyEval_CallObjectWithKeywords(call, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002375 Py_DECREF(call);
Guido van Rossume59214e1994-08-30 08:01:59 +00002376 return res;
2377 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002378 PyErr_SetString(PyExc_TypeError, "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002379 return NULL;
2380}
2381
Guido van Rossumb209a111997-04-29 18:18:01 +00002382static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002383call_function(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002384 PyObject *func;
2385 PyObject *arg;
2386 PyObject *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002387{
Guido van Rossumb209a111997-04-29 18:18:01 +00002388 PyObject *class = NULL; /* == owner */
2389 PyObject *argdefs;
2390 PyObject **d, **k;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002391 int nk, nd;
Guido van Rossumb209a111997-04-29 18:18:01 +00002392 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002393
Guido van Rossumb209a111997-04-29 18:18:01 +00002394 if (kw != NULL && !PyDict_Check(kw)) {
2395 PyErr_BadInternalCall();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002396 return NULL;
2397 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002398
Guido van Rossumb209a111997-04-29 18:18:01 +00002399 if (PyMethod_Check(func)) {
2400 PyObject *self = PyMethod_Self(func);
2401 class = PyMethod_Class(func);
2402 func = PyMethod_Function(func);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002403 if (self == NULL) {
2404 /* Unbound methods must be called with an instance of
2405 the class (or a derived class) as first argument */
Guido van Rossumb209a111997-04-29 18:18:01 +00002406 if (PyTuple_Size(arg) >= 1) {
2407 self = PyTuple_GET_ITEM(arg, 0);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002408 if (self != NULL &&
Guido van Rossumb209a111997-04-29 18:18:01 +00002409 PyInstance_Check(self) &&
2410 PyClass_IsSubclass((PyObject *)
2411 (((PyInstanceObject *)self)->in_class),
Guido van Rossumb3f72581993-05-21 19:56:10 +00002412 class))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002413 /* Handy-dandy */ ;
Guido van Rossumb3f72581993-05-21 19:56:10 +00002414 else
2415 self = NULL;
2416 }
2417 if (self == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002418 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002419 "unbound method must be called with class instance 1st argument");
Guido van Rossumb3f72581993-05-21 19:56:10 +00002420 return NULL;
2421 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002422 Py_INCREF(arg);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002423 }
2424 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002425 int argcount = PyTuple_Size(arg);
2426 PyObject *newarg = PyTuple_New(argcount + 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002427 int i;
Guido van Rossum81daa321993-05-20 14:24:46 +00002428 if (newarg == NULL)
2429 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002430 Py_INCREF(self);
2431 PyTuple_SET_ITEM(newarg, 0, self);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002432 for (i = 0; i < argcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002433 PyObject *v = PyTuple_GET_ITEM(arg, i);
2434 Py_XINCREF(v);
2435 PyTuple_SET_ITEM(newarg, i+1, v);
Guido van Rossum81daa321993-05-20 14:24:46 +00002436 }
2437 arg = newarg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002438 }
2439 }
2440 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002441 if (!PyFunction_Check(func)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002442 PyErr_SetString(PyExc_TypeError,
2443 "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002444 return NULL;
2445 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002446 Py_INCREF(arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002447 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002448
2449 argdefs = PyFunction_GetDefaults(func);
Guido van Rossumb209a111997-04-29 18:18:01 +00002450 if (argdefs != NULL && PyTuple_Check(argdefs)) {
2451 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
2452 nd = PyTuple_Size(argdefs);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002453 }
2454 else {
2455 d = NULL;
2456 nd = 0;
2457 }
2458
2459 if (kw != NULL) {
2460 int pos, i;
Guido van Rossumb209a111997-04-29 18:18:01 +00002461 nk = PyDict_Size(kw);
2462 k = PyMem_NEW(PyObject *, 2*nk);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002463 if (k == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002464 PyErr_NoMemory();
2465 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002466 return NULL;
Guido van Rossume59214e1994-08-30 08:01:59 +00002467 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002468 pos = i = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002469 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002470 i += 2;
2471 nk = i/2;
2472 /* XXX This is broken if the caller deletes dict items! */
2473 }
2474 else {
2475 k = NULL;
2476 nk = 0;
Guido van Rossume59214e1994-08-30 08:01:59 +00002477 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002478
Guido van Rossum681d79a1995-07-18 14:51:37 +00002479 result = eval_code2(
Guido van Rossumb209a111997-04-29 18:18:01 +00002480 (PyCodeObject *)PyFunction_GetCode(func),
2481 PyFunction_GetGlobals(func), (PyObject *)NULL,
2482 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
Guido van Rossum681d79a1995-07-18 14:51:37 +00002483 k, nk,
2484 d, nd,
2485 class);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002486
Guido van Rossumb209a111997-04-29 18:18:01 +00002487 Py_DECREF(arg);
2488 PyMem_XDEL(k);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002489
Guido van Rossum681d79a1995-07-18 14:51:37 +00002490 return result;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002491}
2492
Guido van Rossum3b9c6671996-07-30 18:40:29 +00002493#define SLICE_ERROR_MSG \
2494 "standard sequence type does not support step size other than one"
2495
Guido van Rossumb209a111997-04-29 18:18:01 +00002496static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002497loop_subscript(v, w)
Guido van Rossumb209a111997-04-29 18:18:01 +00002498 PyObject *v, *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002499{
Guido van Rossumb209a111997-04-29 18:18:01 +00002500 PySequenceMethods *sq = v->ob_type->tp_as_sequence;
Guido van Rossume59214e1994-08-30 08:01:59 +00002501 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002502 if (sq == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002503 PyErr_SetString(PyExc_TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002504 return NULL;
2505 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002506 i = PyInt_AsLong(w);
Guido van Rossume59214e1994-08-30 08:01:59 +00002507 v = (*sq->sq_item)(v, i);
2508 if (v)
2509 return v;
Barry Warsaw4249f541997-08-22 21:26:19 +00002510 if (PyErr_ExceptionMatches(PyExc_IndexError))
Guido van Rossumb209a111997-04-29 18:18:01 +00002511 PyErr_Clear();
Guido van Rossume59214e1994-08-30 08:01:59 +00002512 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002513}
2514
2515static int
Guido van Rossuma027efa1997-05-05 20:56:21 +00002516slice_index(v, pi)
Guido van Rossumb209a111997-04-29 18:18:01 +00002517 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002518 int *pi;
2519{
2520 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002521 long x;
Guido van Rossumb209a111997-04-29 18:18:01 +00002522 if (!PyInt_Check(v)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002523 PyErr_SetString(PyExc_TypeError,
2524 "slice index must be int");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002525 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002526 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002527 x = PyInt_AsLong(v);
2528 /* Truncate -- very long indices are truncated anyway */
2529 if (x > INT_MAX)
2530 x = INT_MAX;
2531 else if (x < -INT_MAX)
2532 x = 0;
2533 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002534 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002535 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002536}
2537
Guido van Rossumb209a111997-04-29 18:18:01 +00002538static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002539apply_slice(u, v, w) /* return u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00002540 PyObject *u, *v, *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002541{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002542 int ilow = 0, ihigh = INT_MAX;
2543 if (slice_index(v, &ilow) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002544 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002545 if (slice_index(w, &ihigh) != 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +00002546 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002547 return PySequence_GetSlice(u, ilow, ihigh);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002548}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002549
2550static int
Guido van Rossum3f5da241990-12-20 15:06:42 +00002551assign_slice(u, v, w, x) /* u[v:w] = x */
Guido van Rossumb209a111997-04-29 18:18:01 +00002552 PyObject *u, *v, *w, *x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002553{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002554 int ilow = 0, ihigh = INT_MAX;
2555 if (slice_index(v, &ilow) != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002556 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002557 if (slice_index(w, &ihigh) != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002558 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002559 if (x == NULL)
2560 return PySequence_DelSlice(u, ilow, ihigh);
2561 else
2562 return PySequence_SetSlice(u, ilow, ihigh, x);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002563}
2564
2565static int
Guido van Rossum3f5da241990-12-20 15:06:42 +00002566cmp_member(v, w)
Guido van Rossumb209a111997-04-29 18:18:01 +00002567 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002568{
Guido van Rossume59214e1994-08-30 08:01:59 +00002569 int i, cmp;
Guido van Rossumb209a111997-04-29 18:18:01 +00002570 PyObject *x;
2571 PySequenceMethods *sq;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002572 /* Special case for char in string */
Guido van Rossumb209a111997-04-29 18:18:01 +00002573 if (PyString_Check(w)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002574 register char *s, *end;
2575 register char c;
Guido van Rossumb209a111997-04-29 18:18:01 +00002576 if (!PyString_Check(v) || PyString_Size(v) != 1) {
2577 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3f5da241990-12-20 15:06:42 +00002578 "string member test needs char left operand");
2579 return -1;
2580 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002581 c = PyString_AsString(v)[0];
2582 s = PyString_AsString(w);
2583 end = s + PyString_Size(w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002584 while (s < end) {
2585 if (c == *s++)
2586 return 1;
2587 }
2588 return 0;
2589 }
2590 sq = w->ob_type->tp_as_sequence;
2591 if (sq == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002592 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3f5da241990-12-20 15:06:42 +00002593 "'in' or 'not in' needs sequence right argument");
2594 return -1;
2595 }
Guido van Rossume59214e1994-08-30 08:01:59 +00002596 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002597 x = (*sq->sq_item)(w, i);
Guido van Rossume59214e1994-08-30 08:01:59 +00002598 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002599 if (PyErr_Occurred() == PyExc_IndexError) {
2600 PyErr_Clear();
Guido van Rossume59214e1994-08-30 08:01:59 +00002601 break;
2602 }
2603 return -1;
2604 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002605 cmp = PyObject_Compare(v, x);
2606 Py_XDECREF(x);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002607 if (cmp == 0)
2608 return 1;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002609 if (PyErr_Occurred())
2610 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002611 }
2612 return 0;
2613}
2614
Guido van Rossumb209a111997-04-29 18:18:01 +00002615static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002616cmp_outcome(op, v, w)
Guido van Rossum5b7313a1992-04-06 13:24:57 +00002617 int op;
Guido van Rossumb209a111997-04-29 18:18:01 +00002618 register PyObject *v;
2619 register PyObject *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002620{
2621 register int cmp;
2622 register int res = 0;
2623 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002624 case IS:
2625 case IS_NOT:
2626 res = (v == w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002627 if (op == (int) IS_NOT)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002628 res = !res;
2629 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002630 case IN:
2631 case NOT_IN:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002632 res = cmp_member(v, w);
2633 if (res < 0)
2634 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002635 if (op == (int) NOT_IN)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002636 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002637 break;
2638 case EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00002639 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002640 break;
2641 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00002642 cmp = PyObject_Compare(v, w);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002643 if (cmp && PyErr_Occurred())
2644 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002645 switch (op) {
2646 case LT: res = cmp < 0; break;
2647 case LE: res = cmp <= 0; break;
2648 case EQ: res = cmp == 0; break;
2649 case NE: res = cmp != 0; break;
2650 case GT: res = cmp > 0; break;
2651 case GE: res = cmp >= 0; break;
2652 /* XXX no default? (res is initialized to 0 though) */
2653 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002654 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002655 v = res ? Py_True : Py_False;
2656 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002657 return v;
2658}
2659
Guido van Rossum3f5da241990-12-20 15:06:42 +00002660static int
2661import_from(locals, v, name)
Guido van Rossumb209a111997-04-29 18:18:01 +00002662 PyObject *locals;
2663 PyObject *v;
2664 PyObject *name;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002665{
Guido van Rossumb209a111997-04-29 18:18:01 +00002666 PyObject *w, *x;
2667 if (!PyModule_Check(v)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002668 PyErr_SetString(PyExc_TypeError,
2669 "import-from requires module object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002670 return -1;
2671 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002672 w = PyModule_GetDict(v);
2673 if (PyString_AsString(name)[0] == '*') {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002674 int pos, err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002675 PyObject *name, *value;
Guido van Rossum25831651993-05-19 14:50:45 +00002676 pos = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002677 while (PyDict_Next(w, &pos, &name, &value)) {
2678 if (!PyString_Check(name) ||
2679 PyString_AsString(name)[0] == '_')
Guido van Rossume9736fc1990-11-18 17:33:06 +00002680 continue;
Guido van Rossumb209a111997-04-29 18:18:01 +00002681 Py_INCREF(value);
2682 err = PyDict_SetItem(locals, name, value);
2683 Py_DECREF(value);
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002684 if (err != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002685 return -1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002686 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002687 return 0;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002688 }
2689 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002690 x = PyDict_GetItem(w, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002691 if (x == NULL) {
Guido van Rossum98256aa1991-12-24 13:25:19 +00002692 char buf[250];
Guido van Rossum16dfd291992-02-05 11:17:30 +00002693 sprintf(buf, "cannot import name %.230s",
Guido van Rossumb209a111997-04-29 18:18:01 +00002694 PyString_AsString(name));
2695 PyErr_SetString(PyExc_ImportError, buf);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002696 return -1;
2697 }
2698 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002699 return PyDict_SetItem(locals, name, x);
Guido van Rossume9736fc1990-11-18 17:33:06 +00002700 }
2701}
2702
Guido van Rossumb209a111997-04-29 18:18:01 +00002703static PyObject *
Guido van Rossum25831651993-05-19 14:50:45 +00002704build_class(methods, bases, name)
Guido van Rossumb209a111997-04-29 18:18:01 +00002705 PyObject *methods; /* dictionary */
2706 PyObject *bases; /* tuple containing classes */
2707 PyObject *name; /* string */
Guido van Rossume9736fc1990-11-18 17:33:06 +00002708{
Guido van Rossumcd649651997-08-22 16:56:16 +00002709 int i, n;
Guido van Rossumb209a111997-04-29 18:18:01 +00002710 if (!PyTuple_Check(bases)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002711 PyErr_SetString(PyExc_SystemError,
2712 "build_class with non-tuple bases");
Guido van Rossum25831651993-05-19 14:50:45 +00002713 return NULL;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002714 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002715 if (!PyDict_Check(methods)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002716 PyErr_SetString(PyExc_SystemError,
2717 "build_class with non-dictionary");
Guido van Rossume9736fc1990-11-18 17:33:06 +00002718 return NULL;
2719 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002720 if (!PyString_Check(name)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002721 PyErr_SetString(PyExc_SystemError,
2722 "build_class witn non-string name");
Guido van Rossum25831651993-05-19 14:50:45 +00002723 return NULL;
2724 }
Guido van Rossumcd649651997-08-22 16:56:16 +00002725 n = PyTuple_Size(bases);
2726 for (i = 0; i < n; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002727 PyObject *base = PyTuple_GET_ITEM(bases, i);
2728 if (!PyClass_Check(base)) {
Guido van Rossum150b2df1996-12-05 23:17:11 +00002729 /* Call the base's *type*, if it is callable.
2730 This code is a hook for Donald Beaudry's
2731 and Jim Fulton's type extensions. In
2732 unexended Python it will never be triggered
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002733 since its types are not callable.
2734 Ditto: call the bases's *class*, if it has
2735 one. This makes the same thing possible
2736 without writing C code. A true meta-object
2737 protocol! */
2738 PyObject *basetype = (PyObject *)base->ob_type;
2739 PyObject *callable = NULL;
2740 if (PyCallable_Check(basetype))
2741 callable = basetype;
2742 else
2743 callable = PyObject_GetAttrString(
2744 base, "__class__");
2745 if (callable) {
2746 PyObject *args;
2747 PyObject *newclass = NULL;
2748 args = Py_BuildValue(
2749 "(OOO)", name, bases, methods);
2750 if (args != NULL) {
2751 newclass = PyEval_CallObject(
2752 callable, args);
2753 Py_DECREF(args);
2754 }
Guido van Rossum730806d1998-04-10 22:27:42 +00002755 if (callable != basetype) {
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002756 Py_DECREF(callable);
Guido van Rossum730806d1998-04-10 22:27:42 +00002757 }
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002758 return newclass;
Guido van Rossum150b2df1996-12-05 23:17:11 +00002759 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002760 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +00002761 "base is not a class object");
2762 return NULL;
2763 }
2764 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002765 return PyClass_New(bases, methods, name);
Guido van Rossum25831651993-05-19 14:50:45 +00002766}
2767
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002768static int
Guido van Rossuma027efa1997-05-05 20:56:21 +00002769exec_statement(f, prog, globals, locals)
2770 PyFrameObject *f;
Guido van Rossumb209a111997-04-29 18:18:01 +00002771 PyObject *prog;
2772 PyObject *globals;
2773 PyObject *locals;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002774{
2775 char *s;
2776 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00002777 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002778 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002779
Guido van Rossumb209a111997-04-29 18:18:01 +00002780 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
2781 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002782 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00002783 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002784 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00002785 locals = PyTuple_GetItem(prog, 2);
2786 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002787 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002788 if (globals == Py_None) {
2789 globals = PyEval_GetGlobals();
2790 if (locals == Py_None) {
2791 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002792 plain = 1;
2793 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002794 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002795 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002796 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00002797 if (!PyString_Check(prog) &&
2798 !PyCode_Check(prog) &&
2799 !PyFile_Check(prog)) {
2800 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002801 "exec 1st arg must be string, code or file object");
2802 return -1;
2803 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002804 if (!PyDict_Check(globals) || !PyDict_Check(locals)) {
2805 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002806 "exec 2nd/3rd args must be dict or None");
2807 return -1;
2808 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002809 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002810 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00002811 if (PyCode_Check(prog)) {
Guido van Rossumdfed7251997-11-11 16:29:38 +00002812 v = PyEval_EvalCode((PyCodeObject *) prog,
2813 globals, locals);
2814 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002815 return -1;
Guido van Rossumdfed7251997-11-11 16:29:38 +00002816 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002817 return 0;
2818 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002819 if (PyFile_Check(prog)) {
2820 FILE *fp = PyFile_AsFile(prog);
2821 char *name = PyString_AsString(PyFile_Name(prog));
Guido van Rossumb05a5c71997-05-07 17:46:13 +00002822 if (PyRun_File(fp, name, Py_file_input,
2823 globals, locals) == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002824 return -1;
2825 return 0;
2826 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002827 s = PyString_AsString(prog);
Guido van Rossum5f15b961997-05-13 17:50:01 +00002828 if ((int)strlen(s) != PyString_Size(prog)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002829 PyErr_SetString(PyExc_ValueError,
2830 "embedded '\\0' in exec string");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002831 return -1;
2832 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00002833 v = PyRun_String(s, Py_file_input, globals, locals);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002834 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002835 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00002836 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002837 if (plain)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002838 PyFrame_LocalsToFast(f, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002839 return 0;
2840}
Guido van Rossum24c13741995-02-14 09:42:43 +00002841
Guido van Rossum1aa14831997-01-21 05:34:20 +00002842/* Hack for ni.py */
Guido van Rossumb209a111997-04-29 18:18:01 +00002843static PyObject *
Guido van Rossum24c13741995-02-14 09:42:43 +00002844find_from_args(f, nexti)
Guido van Rossumb209a111997-04-29 18:18:01 +00002845 PyFrameObject *f;
Guido van Rossum24c13741995-02-14 09:42:43 +00002846 int nexti;
2847{
2848 int opcode;
2849 int oparg;
Guido van Rossumb209a111997-04-29 18:18:01 +00002850 PyObject *list, *name;
Guido van Rossum24c13741995-02-14 09:42:43 +00002851 unsigned char *next_instr;
2852
2853 next_instr = GETUSTRINGVALUE(f->f_code->co_code) + nexti;
2854 opcode = (*next_instr++);
2855 if (opcode != IMPORT_FROM) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002856 Py_INCREF(Py_None);
2857 return Py_None;
Guido van Rossum24c13741995-02-14 09:42:43 +00002858 }
2859
Guido van Rossumb209a111997-04-29 18:18:01 +00002860 list = PyList_New(0);
Guido van Rossum24c13741995-02-14 09:42:43 +00002861 if (list == NULL)
2862 return NULL;
2863
2864 do {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002865 oparg = (next_instr[1]<<8) + next_instr[0];
2866 next_instr += 2;
Guido van Rossum24c13741995-02-14 09:42:43 +00002867 name = Getnamev(f, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002868 if (PyList_Append(list, name) < 0) {
2869 Py_DECREF(list);
Guido van Rossum24c13741995-02-14 09:42:43 +00002870 break;
2871 }
2872 opcode = (*next_instr++);
2873 } while (opcode == IMPORT_FROM);
2874
2875 return list;
2876}
Guido van Rossum950361c1997-01-24 13:49:28 +00002877
2878
2879#ifdef DYNAMIC_EXECUTION_PROFILE
2880
2881PyObject *
2882getarray(a)
2883 long a[256];
2884{
2885 int i;
2886 PyObject *l = PyList_New(256);
2887 if (l == NULL) return NULL;
2888 for (i = 0; i < 256; i++) {
2889 PyObject *x = PyInt_FromLong(a[i]);
2890 if (x == NULL) {
2891 Py_DECREF(l);
2892 return NULL;
2893 }
2894 PyList_SetItem(l, i, x);
2895 }
2896 for (i = 0; i < 256; i++)
2897 a[i] = 0;
2898 return l;
2899}
2900
2901PyObject *
2902_Py_GetDXProfile(self, args)
2903 PyObject *self, *args;
2904{
2905#ifndef DXPAIRS
2906 return getarray(dxp);
2907#else
2908 int i;
2909 PyObject *l = PyList_New(257);
2910 if (l == NULL) return NULL;
2911 for (i = 0; i < 257; i++) {
2912 PyObject *x = getarray(dxpairs[i]);
2913 if (x == NULL) {
2914 Py_DECREF(l);
2915 return NULL;
2916 }
2917 PyList_SetItem(l, i, x);
2918 }
2919 return l;
2920#endif
2921}
2922
2923#endif