blob: 989e17f9bfd097e9ab88acd52a15f48924048cae [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 PyObject *cmp_outcome Py_PROTO((int, PyObject *, PyObject *));
88static int import_from Py_PROTO((PyObject *, PyObject *, PyObject *));
89static PyObject *build_class Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000090static int exec_statement Py_PROTO((PyFrameObject *,
91 PyObject *, PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000092static PyObject *find_from_args Py_PROTO((PyFrameObject *, int));
Guido van Rossuma027efa1997-05-05 20:56:21 +000093static void set_exc_info Py_PROTO((PyThreadState *,
94 PyObject *, PyObject *, PyObject *));
95static void reset_exc_info Py_PROTO((PyThreadState *));
Guido van Rossum374a9221991-04-04 10:40:29 +000096
97
Guido van Rossum950361c1997-01-24 13:49:28 +000098/* Dynamic execution profile */
99#ifdef DYNAMIC_EXECUTION_PROFILE
100#ifdef DXPAIRS
101static long dxpairs[257][256];
102#define dxp dxpairs[256]
103#else
104static long dxp[256];
105#endif
106#endif
107
108
Guido van Rossume59214e1994-08-30 08:01:59 +0000109#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000110
Guido van Rossum2571cc81999-04-07 16:07:23 +0000111#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000112#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000113#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000114#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000115
Guido van Rossuma027efa1997-05-05 20:56:21 +0000116extern int _PyThread_Started; /* Flag for Py_Exit */
117
Guido van Rossum65d5b571998-12-21 19:32:43 +0000118static PyThread_type_lock interpreter_lock = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000119static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000120
121void
Guido van Rossumb209a111997-04-29 18:18:01 +0000122PyEval_InitThreads()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000123{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000124 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000125 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000126 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000127 interpreter_lock = PyThread_allocate_lock();
128 PyThread_acquire_lock(interpreter_lock, 1);
129 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000130}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000131
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000132void
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133PyEval_AcquireLock()
134{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000135 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136}
137
138void
139PyEval_ReleaseLock()
140{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000141 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000142}
143
144void
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000145PyEval_AcquireThread(tstate)
146 PyThreadState *tstate;
147{
148 if (tstate == NULL)
149 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000150 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000151 if (PyThreadState_Swap(tstate) != NULL)
152 Py_FatalError(
153 "PyEval_AcquireThread: non-NULL old thread state");
154}
155
156void
157PyEval_ReleaseThread(tstate)
158 PyThreadState *tstate;
159{
160 if (tstate == NULL)
161 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
162 if (PyThreadState_Swap(NULL) != tstate)
163 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000164 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000165}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000166#endif
167
Guido van Rossumff4949e1992-08-05 19:58:53 +0000168/* Functions save_thread and restore_thread are always defined so
169 dynamically loaded modules needn't be compiled separately for use
170 with and without threads: */
171
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000172PyThreadState *
Guido van Rossumb209a111997-04-29 18:18:01 +0000173PyEval_SaveThread()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000175 PyThreadState *tstate = PyThreadState_Swap(NULL);
176 if (tstate == NULL)
177 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000178#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000179 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000180 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000182 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000183}
184
185void
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000186PyEval_RestoreThread(tstate)
187 PyThreadState *tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000188{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000189 if (tstate == NULL)
190 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000191#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000192 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000193 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000194 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000195 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000196 }
197#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000198 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199}
200
201
Guido van Rossuma9672091994-09-14 13:31:22 +0000202/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
203 signal handlers or Mac I/O completion routines) can schedule calls
204 to a function to be called synchronously.
205 The synchronous function is called with one void* argument.
206 It should return 0 for success or -1 for failure -- failure should
207 be accompanied by an exception.
208
209 If registry succeeds, the registry function returns 0; if it fails
210 (e.g. due to too many pending calls) it returns -1 (without setting
211 an exception condition).
212
213 Note that because registry may occur from within signal handlers,
214 or other asynchronous events, calling malloc() is unsafe!
215
216#ifdef WITH_THREAD
217 Any thread can schedule pending calls, but only the main thread
218 will execute them.
219#endif
220
221 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
222 There are two possible race conditions:
223 (1) nested asynchronous registry calls;
224 (2) registry calls made while pending calls are being processed.
225 While (1) is very unlikely, (2) is a real possibility.
226 The current code is safe against (2), but not against (1).
227 The safety against (2) is derived from the fact that only one
228 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000229
Guido van Rossuma027efa1997-05-05 20:56:21 +0000230 XXX Darn! With the advent of thread state, we should have an array
231 of pending calls per thread in the thread state! Later...
232*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000233
Guido van Rossuma9672091994-09-14 13:31:22 +0000234#define NPENDINGCALLS 32
235static struct {
Guido van Rossumb209a111997-04-29 18:18:01 +0000236 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000237 ANY *arg;
238} pendingcalls[NPENDINGCALLS];
239static volatile int pendingfirst = 0;
240static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000241static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000242
243int
244Py_AddPendingCall(func, arg)
Guido van Rossumb209a111997-04-29 18:18:01 +0000245 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000246 ANY *arg;
247{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000248 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000249 int i, j;
250 /* XXX Begin critical section */
251 /* XXX If you want this to be safe against nested
252 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000253 if (busy)
254 return -1;
255 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000256 i = pendinglast;
257 j = (i + 1) % NPENDINGCALLS;
258 if (j == pendingfirst)
259 return -1; /* Queue full */
260 pendingcalls[i].func = func;
261 pendingcalls[i].arg = arg;
262 pendinglast = j;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000263 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000264 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000265 /* XXX End critical section */
266 return 0;
267}
268
Guido van Rossum180d7b41994-09-29 09:45:57 +0000269int
270Py_MakePendingCalls()
Guido van Rossuma9672091994-09-14 13:31:22 +0000271{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000272 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000273#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000274 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000275 return 0;
276#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000277 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000278 return 0;
279 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000280 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000281 for (;;) {
282 int i;
Guido van Rossumb209a111997-04-29 18:18:01 +0000283 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000284 ANY *arg;
285 i = pendingfirst;
286 if (i == pendinglast)
287 break; /* Queue empty */
288 func = pendingcalls[i].func;
289 arg = pendingcalls[i].arg;
290 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000291 if (func(arg) < 0) {
292 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000293 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000294 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000295 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000296 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000297 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000298 return 0;
299}
300
301
Guido van Rossum374a9221991-04-04 10:40:29 +0000302/* Status code for main loop (reason for stack unwind) */
303
304enum why_code {
305 WHY_NOT, /* No error */
306 WHY_EXCEPTION, /* Exception occurred */
307 WHY_RERAISE, /* Exception re-raised by 'finally' */
308 WHY_RETURN, /* 'return' statement */
309 WHY_BREAK /* 'break' statement */
310};
311
Guido van Rossumb209a111997-04-29 18:18:01 +0000312static enum why_code do_raise Py_PROTO((PyObject *, PyObject *, PyObject *));
Barry Warsawe42b18f1997-08-25 22:13:04 +0000313static int unpack_sequence Py_PROTO((PyObject *, int, PyObject **));
Guido van Rossum1aa14831997-01-21 05:34:20 +0000314
Guido van Rossum374a9221991-04-04 10:40:29 +0000315
Guido van Rossumb209a111997-04-29 18:18:01 +0000316PyObject *
317PyEval_EvalCode(co, globals, locals)
318 PyCodeObject *co;
319 PyObject *globals;
320 PyObject *locals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000321{
322 return eval_code2(co,
323 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000324 (PyObject **)NULL, 0,
325 (PyObject **)NULL, 0,
326 (PyObject **)NULL, 0,
327 (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000328}
329
330
331/* Interpreter main loop */
332
Guido van Rossum8861b741996-07-30 16:49:37 +0000333#ifndef MAX_RECURSION_DEPTH
334#define MAX_RECURSION_DEPTH 10000
335#endif
336
Guido van Rossumb209a111997-04-29 18:18:01 +0000337static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +0000338eval_code2(co, globals, locals,
339 args, argcount, kws, kwcount, defs, defcount, owner)
Guido van Rossumb209a111997-04-29 18:18:01 +0000340 PyCodeObject *co;
341 PyObject *globals;
342 PyObject *locals;
343 PyObject **args;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000344 int argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000345 PyObject **kws; /* length: 2*kwcount */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000346 int kwcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000347 PyObject **defs;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000348 int defcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000349 PyObject *owner;
Guido van Rossum374a9221991-04-04 10:40:29 +0000350{
Guido van Rossum950361c1997-01-24 13:49:28 +0000351#ifdef DXPAIRS
352 int lastopcode = 0;
353#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000354 register unsigned char *next_instr;
Guido van Rossum50cd3481998-11-17 17:02:51 +0000355 register int opcode; /* Current opcode */
356 register int oparg; /* Current opcode argument, if any */
Guido van Rossumb209a111997-04-29 18:18:01 +0000357 register PyObject **stack_pointer;
Guido van Rossum374a9221991-04-04 10:40:29 +0000358 register enum why_code why; /* Reason for block stack unwind */
359 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000360 register PyObject *x; /* Result object -- NULL if error */
361 register PyObject *v; /* Temporary objects popped off stack */
362 register PyObject *w;
363 register PyObject *u;
364 register PyObject *t;
365 register PyFrameObject *f; /* Current frame */
Guido van Rossum50cd3481998-11-17 17:02:51 +0000366 register PyObject **fastlocals;
Guido van Rossum014518f1998-11-23 21:09:51 +0000367 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000368 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd076c731998-10-07 19:42:25 +0000369 unsigned char *first_instr;
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 Rossumd076c731998-10-07 19:42:25 +0000383#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000384#define NEXTOP() (*next_instr++)
385#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000386#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000387#define JUMPBY(x) (next_instr += (x))
388
389/* Stack manipulation macros */
390
391#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
392#define EMPTY() (STACK_LEVEL() == 0)
393#define TOP() (stack_pointer[-1])
394#define BASIC_PUSH(v) (*stack_pointer++ = (v))
395#define BASIC_POP() (*--stack_pointer)
396
Guido van Rossum96a42c81992-01-12 02:29:51 +0000397#ifdef LLTRACE
398#define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
399#define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
Guido van Rossum374a9221991-04-04 10:40:29 +0000400#else
401#define PUSH(v) BASIC_PUSH(v)
402#define POP() BASIC_POP()
403#endif
404
Guido van Rossum681d79a1995-07-18 14:51:37 +0000405/* Local variable macros */
406
407#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumb209a111997-04-29 18:18:01 +0000408#define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \
Guido van Rossum681d79a1995-07-18 14:51:37 +0000409 GETLOCAL(i) = value; } while (0)
410
Guido van Rossuma027efa1997-05-05 20:56:21 +0000411/* Start of code */
412
Guido van Rossum8861b741996-07-30 16:49:37 +0000413#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000414 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000415 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000416 return NULL;
417 }
418#endif
419
Guido van Rossum681d79a1995-07-18 14:51:37 +0000420 if (globals == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000421 PyErr_SetString(PyExc_SystemError, "eval_code2: NULL globals");
Guido van Rossum6135a871995-01-09 17:53:26 +0000422 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000423 }
424
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000425#ifdef LLTRACE
Guido van Rossumb209a111997-04-29 18:18:01 +0000426 lltrace = PyDict_GetItemString(globals, "__lltrace__") != NULL;
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000427#endif
428
Guido van Rossumb209a111997-04-29 18:18:01 +0000429 f = PyFrame_New(
Guido van Rossuma027efa1997-05-05 20:56:21 +0000430 tstate, /*back*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000431 co, /*code*/
432 globals, /*globals*/
Guido van Rossum76836021997-01-20 04:26:20 +0000433 locals); /*locals*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000434 if (f == NULL)
435 return NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000436
Guido van Rossuma027efa1997-05-05 20:56:21 +0000437 tstate->frame = f;
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000438 fastlocals = f->f_localsplus;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000439
440 if (co->co_argcount > 0 ||
441 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
442 int i;
443 int n = argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000444 PyObject *kwdict = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000445 if (co->co_flags & CO_VARKEYWORDS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000446 kwdict = PyDict_New();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000447 if (kwdict == NULL)
448 goto fail;
Guido van Rossum8c5df061997-01-24 04:19:24 +0000449 i = co->co_argcount;
450 if (co->co_flags & CO_VARARGS)
451 i++;
452 SETLOCAL(i, kwdict);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000453 }
454 if (argcount > co->co_argcount) {
455 if (!(co->co_flags & CO_VARARGS)) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000456 PyErr_Format(PyExc_TypeError,
457 "too many arguments; expected %d, got %d",
458 co->co_argcount, argcount);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000459 goto fail;
460 }
461 n = co->co_argcount;
462 }
463 for (i = 0; i < n; i++) {
464 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000465 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000466 SETLOCAL(i, x);
467 }
468 if (co->co_flags & CO_VARARGS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000469 u = PyTuple_New(argcount - n);
Guido van Rossum8c5df061997-01-24 04:19:24 +0000470 if (u == NULL)
471 goto fail;
472 SETLOCAL(co->co_argcount, u);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000473 for (i = n; i < argcount; i++) {
474 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000475 Py_INCREF(x);
476 PyTuple_SET_ITEM(u, i-n, x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000477 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000478 }
479 for (i = 0; i < kwcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000480 PyObject *keyword = kws[2*i];
481 PyObject *value = kws[2*i + 1];
Guido van Rossum681d79a1995-07-18 14:51:37 +0000482 int j;
Guido van Rossum25da5be1999-10-26 00:12:20 +0000483 if (keyword == NULL || !PyString_Check(keyword)) {
484 PyErr_SetString(PyExc_TypeError,
485 "keywords must be strings");
486 goto fail;
487 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000488 /* XXX slow -- speed up using dictionary? */
489 for (j = 0; j < co->co_argcount; j++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000490 PyObject *nm = PyTuple_GET_ITEM(
491 co->co_varnames, j);
Guido van Rossumb209a111997-04-29 18:18:01 +0000492 if (PyObject_Compare(keyword, nm) == 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000493 break;
494 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000495 /* Check errors from Compare */
496 if (PyErr_Occurred())
497 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000498 if (j >= co->co_argcount) {
499 if (kwdict == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000500 PyErr_Format(PyExc_TypeError,
Guido van Rossuma027efa1997-05-05 20:56:21 +0000501 "unexpected keyword argument: %.400s",
Jeremy Hylton76901512000-03-28 23:49:17 +0000502 PyString_AsString(keyword));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000503 goto fail;
504 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000505 PyDict_SetItem(kwdict, keyword, value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000506 }
507 else {
508 if (GETLOCAL(j) != NULL) {
Jeremy Hylton76901512000-03-28 23:49:17 +0000509 PyErr_Format(PyExc_TypeError,
510 "keyword parameter redefined: %.400s",
511 PyString_AsString(keyword));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000512 goto fail;
513 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000514 Py_INCREF(value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000515 SETLOCAL(j, value);
516 }
517 }
518 if (argcount < co->co_argcount) {
519 int m = co->co_argcount - defcount;
520 for (i = argcount; i < m; i++) {
521 if (GETLOCAL(i) == NULL) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000522 PyErr_Format(PyExc_TypeError,
523 "not enough arguments; expected %d, got %d",
524 m, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000525 goto fail;
526 }
527 }
528 if (n > m)
529 i = n - m;
530 else
531 i = 0;
532 for (; i < defcount; i++) {
533 if (GETLOCAL(m+i) == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000534 PyObject *def = defs[i];
535 Py_INCREF(def);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000536 SETLOCAL(m+i, def);
537 }
538 }
539 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000540 }
541 else {
542 if (argcount > 0 || kwcount > 0) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000543 PyErr_SetString(PyExc_TypeError,
544 "no arguments expected");
Guido van Rossum8c5df061997-01-24 04:19:24 +0000545 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000546 }
547 }
548
Guido van Rossuma027efa1997-05-05 20:56:21 +0000549 if (tstate->sys_tracefunc != NULL) {
550 /* tstate->sys_tracefunc, if defined, is a function that
551 will be called on *every* entry to a code block.
Guido van Rossum96a42c81992-01-12 02:29:51 +0000552 Its return value, if not None, is a function that
553 will be called at the start of each executed line
554 of code. (Actually, the function must return
555 itself in order to continue tracing.)
556 The trace functions are called with three arguments:
557 a pointer to the current frame, a string indicating
558 why the function is called, and an argument which
559 depends on the situation. The global trace function
560 (sys.trace) is also called whenever an exception
561 is detected. */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000562 if (call_trace(&tstate->sys_tracefunc,
563 &f->f_trace, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000564 Py_None/*XXX how to compute arguments now?*/)) {
Guido van Rossum96a42c81992-01-12 02:29:51 +0000565 /* Trace function raised an error */
Guido van Rossum8c5df061997-01-24 04:19:24 +0000566 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000567 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000568 }
569
Guido van Rossuma027efa1997-05-05 20:56:21 +0000570 if (tstate->sys_profilefunc != NULL) {
571 /* Similar for sys_profilefunc, except it needn't return
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000572 itself and isn't called for "line" events */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000573 if (call_trace(&tstate->sys_profilefunc,
574 (PyObject**)0, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000575 Py_None/*XXX*/)) {
Guido van Rossum8c5df061997-01-24 04:19:24 +0000576 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000577 }
578 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000579
Guido van Rossuma027efa1997-05-05 20:56:21 +0000580 if (++tstate->recursion_depth > MAX_RECURSION_DEPTH) {
581 --tstate->recursion_depth;
582 PyErr_SetString(PyExc_RuntimeError,
583 "Maximum recursion depth exceeded");
584 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +0000585 Py_DECREF(f);
Guido van Rossum8861b741996-07-30 16:49:37 +0000586 return NULL;
587 }
588
Guido van Rossumd076c731998-10-07 19:42:25 +0000589 _PyCode_GETCODEPTR(co, &first_instr);
590 next_instr = first_instr;
Guido van Rossum374a9221991-04-04 10:40:29 +0000591 stack_pointer = f->f_valuestack;
592
Guido van Rossum374a9221991-04-04 10:40:29 +0000593 why = WHY_NOT;
594 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000595 x = Py_None; /* Not a reference, just anything non-NULL */
Guido van Rossum374a9221991-04-04 10:40:29 +0000596
597 for (;;) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000598 /* Do periodic things. Doing this every time through
599 the loop would add too much overhead, so we do it
600 only every Nth instruction. We also do it if
601 ``things_to_do'' is set, i.e. when an asynchronous
602 event needs attention (e.g. a signal handler or
603 async I/O handler); see Py_AddPendingCall() and
604 Py_MakePendingCalls() above. */
Guido van Rossuma9672091994-09-14 13:31:22 +0000605
Guido van Rossuma027efa1997-05-05 20:56:21 +0000606 if (things_to_do || --tstate->ticker < 0) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607 tstate->ticker = tstate->interp->checkinterval;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000608 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000609 if (Py_MakePendingCalls() < 0) {
610 why = WHY_EXCEPTION;
611 goto on_error;
612 }
613 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000614#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000615 /* If we have true signals, the signal handler
616 will call Py_AddPendingCall() so we don't
617 have to call sigcheck(). On the Mac and
618 DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000619 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000620 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000621 goto on_error;
622 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000623#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000624
Guido van Rossume59214e1994-08-30 08:01:59 +0000625#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000626 if (interpreter_lock) {
627 /* Give another thread a chance */
628
Guido van Rossum25ce5661997-08-02 03:10:38 +0000629 if (PyThreadState_Swap(NULL) != tstate)
630 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000631 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000632
633 /* Other threads may run now */
634
Guido van Rossum65d5b571998-12-21 19:32:43 +0000635 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000636 if (PyThreadState_Swap(tstate) != NULL)
637 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000638 }
639#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000640 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000641
Guido van Rossum374a9221991-04-04 10:40:29 +0000642 /* Extract opcode and argument */
Guido van Rossum99bec951992-09-03 20:29:45 +0000643
Guido van Rossum408027e1996-12-30 16:17:54 +0000644#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum99bec951992-09-03 20:29:45 +0000645 f->f_lasti = INSTR_OFFSET();
646#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000647
648 opcode = NEXTOP();
649 if (HAS_ARG(opcode))
650 oparg = NEXTARG();
Guido van Rossum950361c1997-01-24 13:49:28 +0000651#ifdef DYNAMIC_EXECUTION_PROFILE
652#ifdef DXPAIRS
653 dxpairs[lastopcode][opcode]++;
654 lastopcode = opcode;
655#endif
656 dxp[opcode]++;
657#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000658
Guido van Rossum96a42c81992-01-12 02:29:51 +0000659#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000660 /* Instruction tracing */
661
Guido van Rossum96a42c81992-01-12 02:29:51 +0000662 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000663 if (HAS_ARG(opcode)) {
664 printf("%d: %d, %d\n",
665 (int) (INSTR_OFFSET() - 3),
666 opcode, oparg);
667 }
668 else {
669 printf("%d: %d\n",
670 (int) (INSTR_OFFSET() - 1), opcode);
671 }
672 }
673#endif
674
675 /* Main switch on opcode */
676
677 switch (opcode) {
678
679 /* BEWARE!
680 It is essential that any operation that fails sets either
681 x to NULL, err to nonzero, or why to anything but WHY_NOT,
682 and that no operation that succeeds does this! */
683
684 /* case STOP_CODE: this is an error! */
685
686 case POP_TOP:
687 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000688 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000689 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000690
691 case ROT_TWO:
692 v = POP();
693 w = POP();
694 PUSH(v);
695 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000696 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000697
698 case ROT_THREE:
699 v = POP();
700 w = POP();
701 x = POP();
702 PUSH(v);
703 PUSH(x);
704 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000705 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000706
707 case DUP_TOP:
708 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000709 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000710 PUSH(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000711 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000712
713 case UNARY_POSITIVE:
714 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000715 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000716 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000717 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000718 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000719 break;
720
721 case UNARY_NEGATIVE:
722 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000723 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000724 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000725 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000726 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000727 break;
728
729 case UNARY_NOT:
730 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000731 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000732 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000733 if (err == 0) {
734 Py_INCREF(Py_True);
735 PUSH(Py_True);
736 continue;
737 }
738 else if (err > 0) {
739 Py_INCREF(Py_False);
740 PUSH(Py_False);
741 err = 0;
742 continue;
743 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000744 break;
745
746 case UNARY_CONVERT:
747 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000748 x = PyObject_Repr(v);
749 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000750 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000751 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000752 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000753
754 case UNARY_INVERT:
755 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000756 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000757 Py_DECREF(v);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000758 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000759 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000760 break;
Guido van Rossum374a9221991-04-04 10:40:29 +0000761
Guido van Rossum50564e81996-01-12 01:13:16 +0000762 case BINARY_POWER:
763 w = POP();
764 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000765 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000766 Py_DECREF(v);
767 Py_DECREF(w);
Guido van Rossum50564e81996-01-12 01:13:16 +0000768 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000769 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000770 break;
771
Guido van Rossum374a9221991-04-04 10:40:29 +0000772 case BINARY_MULTIPLY:
773 w = POP();
774 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000775 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000776 Py_DECREF(v);
777 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000778 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000779 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000780 break;
781
782 case BINARY_DIVIDE:
783 w = POP();
784 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000785 x = PyNumber_Divide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000786 Py_DECREF(v);
787 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000788 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000789 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000790 break;
791
792 case BINARY_MODULO:
793 w = POP();
794 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000795 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000796 Py_DECREF(v);
797 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000798 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000799 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000800 break;
801
802 case BINARY_ADD:
803 w = POP();
804 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000805 if (PyInt_Check(v) && PyInt_Check(w)) {
806 /* INLINE: int + int */
807 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000808 a = PyInt_AS_LONG(v);
809 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000810 i = a + b;
811 if ((i^a) < 0 && (i^b) < 0) {
812 PyErr_SetString(PyExc_OverflowError,
813 "integer addition");
814 x = NULL;
815 }
816 else
817 x = PyInt_FromLong(i);
818 }
819 else
820 x = PyNumber_Add(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000821 Py_DECREF(v);
822 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000823 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000824 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000825 break;
826
827 case BINARY_SUBTRACT:
828 w = POP();
829 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000830 if (PyInt_Check(v) && PyInt_Check(w)) {
831 /* INLINE: int - int */
832 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000833 a = PyInt_AS_LONG(v);
834 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000835 i = a - b;
836 if ((i^a) < 0 && (i^~b) < 0) {
837 PyErr_SetString(PyExc_OverflowError,
838 "integer subtraction");
839 x = NULL;
840 }
841 else
842 x = PyInt_FromLong(i);
843 }
844 else
845 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000846 Py_DECREF(v);
847 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000848 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000849 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000850 break;
851
852 case BINARY_SUBSCR:
853 w = POP();
854 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000855 if (PyList_Check(v) && PyInt_Check(w)) {
856 /* INLINE: list[int] */
857 long i = PyInt_AsLong(w);
858 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +0000859 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +0000860 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +0000861 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000862 PyErr_SetString(PyExc_IndexError,
863 "list index out of range");
864 x = NULL;
865 }
866 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +0000867 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +0000868 Py_INCREF(x);
869 }
870 }
871 else
872 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000873 Py_DECREF(v);
874 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000875 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000876 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000877 break;
878
Guido van Rossum7928cd71991-10-24 14:59:31 +0000879 case BINARY_LSHIFT:
880 w = POP();
881 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000882 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000883 Py_DECREF(v);
884 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000885 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000886 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000887 break;
888
889 case BINARY_RSHIFT:
890 w = POP();
891 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000892 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000893 Py_DECREF(v);
894 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000895 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000896 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000897 break;
898
899 case BINARY_AND:
900 w = POP();
901 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000902 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000903 Py_DECREF(v);
904 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000905 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000906 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000907 break;
908
909 case BINARY_XOR:
910 w = POP();
911 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000912 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000913 Py_DECREF(v);
914 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000915 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000916 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000917 break;
918
919 case BINARY_OR:
920 w = POP();
921 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000922 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000923 Py_DECREF(v);
924 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000925 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000926 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000927 break;
928
Guido van Rossum374a9221991-04-04 10:40:29 +0000929 case SLICE+0:
930 case SLICE+1:
931 case SLICE+2:
932 case SLICE+3:
933 if ((opcode-SLICE) & 2)
934 w = POP();
935 else
936 w = NULL;
937 if ((opcode-SLICE) & 1)
938 v = POP();
939 else
940 v = NULL;
941 u = POP();
942 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000943 Py_DECREF(u);
944 Py_XDECREF(v);
945 Py_XDECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000946 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000947 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000948 break;
949
950 case STORE_SLICE+0:
951 case STORE_SLICE+1:
952 case STORE_SLICE+2:
953 case STORE_SLICE+3:
954 if ((opcode-STORE_SLICE) & 2)
955 w = POP();
956 else
957 w = NULL;
958 if ((opcode-STORE_SLICE) & 1)
959 v = POP();
960 else
961 v = NULL;
962 u = POP();
963 t = POP();
964 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +0000965 Py_DECREF(t);
966 Py_DECREF(u);
967 Py_XDECREF(v);
968 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000969 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000970 break;
971
972 case DELETE_SLICE+0:
973 case DELETE_SLICE+1:
974 case DELETE_SLICE+2:
975 case DELETE_SLICE+3:
976 if ((opcode-DELETE_SLICE) & 2)
977 w = POP();
978 else
979 w = NULL;
980 if ((opcode-DELETE_SLICE) & 1)
981 v = POP();
982 else
983 v = NULL;
984 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000985 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +0000986 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +0000987 Py_DECREF(u);
988 Py_XDECREF(v);
989 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000990 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000991 break;
992
993 case STORE_SUBSCR:
994 w = POP();
995 v = POP();
996 u = POP();
997 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +0000998 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +0000999 Py_DECREF(u);
1000 Py_DECREF(v);
1001 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001002 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001003 break;
1004
1005 case DELETE_SUBSCR:
1006 w = POP();
1007 v = POP();
1008 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001009 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001010 Py_DECREF(v);
1011 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001012 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001013 break;
1014
1015 case PRINT_EXPR:
1016 v = POP();
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001017 /* Print value except if None */
1018 /* After printing, also assign to '_' */
1019 /* Before, set '_' to None to avoid recursion */
Guido van Rossumb209a111997-04-29 18:18:01 +00001020 if (v != Py_None &&
Guido van Rossuma027efa1997-05-05 20:56:21 +00001021 (err = PyDict_SetItemString(
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001022 f->f_builtins, "_", Py_None)) == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001023 err = Py_FlushLine();
Guido van Rossumc12da691997-07-17 23:12:42 +00001024 if (err == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001025 x = PySys_GetObject("stdout");
Guido van Rossum8f183201997-12-31 05:53:15 +00001026 if (x == NULL) {
1027 PyErr_SetString(
1028 PyExc_RuntimeError,
1029 "lost sys.stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00001030 err = -1;
Guido van Rossum8f183201997-12-31 05:53:15 +00001031 }
Guido van Rossumbe270261997-05-22 22:26:18 +00001032 }
1033 if (err == 0)
1034 err = PyFile_WriteObject(v, x, 0);
1035 if (err == 0) {
1036 PyFile_SoftSpace(x, 1);
1037 err = Py_FlushLine();
1038 }
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001039 if (err == 0) {
1040 err = PyDict_SetItemString(
1041 f->f_builtins, "_", v);
1042 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001043 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001044 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001045 break;
1046
1047 case PRINT_ITEM:
1048 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001049 w = PySys_GetObject("stdout");
Guido van Rossum8f183201997-12-31 05:53:15 +00001050 if (w == NULL) {
1051 PyErr_SetString(PyExc_RuntimeError,
1052 "lost sys.stdout");
1053 err = -1;
1054 }
1055 else if (PyFile_SoftSpace(w, 1))
Guido van Rossumbe270261997-05-22 22:26:18 +00001056 err = PyFile_WriteString(" ", w);
1057 if (err == 0)
1058 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Guido van Rossumb209a111997-04-29 18:18:01 +00001059 if (err == 0 && PyString_Check(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +00001060 /* XXX move into writeobject() ? */
Guido van Rossumb209a111997-04-29 18:18:01 +00001061 char *s = PyString_AsString(v);
1062 int len = PyString_Size(v);
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001063 if (len > 0 &&
1064 isspace(Py_CHARMASK(s[len-1])) &&
Guido van Rossumc6004111993-11-05 10:22:19 +00001065 s[len-1] != ' ')
Guido van Rossumb209a111997-04-29 18:18:01 +00001066 PyFile_SoftSpace(w, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +00001067 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001068 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001069 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001070 break;
1071
1072 case PRINT_NEWLINE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001073 x = PySys_GetObject("stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001074 if (x == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00001075 PyErr_SetString(PyExc_RuntimeError,
1076 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001077 else {
Guido van Rossumbe270261997-05-22 22:26:18 +00001078 err = PyFile_WriteString("\n", x);
1079 if (err == 0)
1080 PyFile_SoftSpace(x, 0);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001081 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001082 break;
1083
1084 case BREAK_LOOP:
1085 why = WHY_BREAK;
1086 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001087
Guido van Rossumf10570b1995-07-07 22:53:21 +00001088 case RAISE_VARARGS:
1089 u = v = w = NULL;
1090 switch (oparg) {
1091 case 3:
1092 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001093 /* Fallthrough */
1094 case 2:
1095 v = POP(); /* value */
1096 /* Fallthrough */
1097 case 1:
1098 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001099 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001100 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001101 break;
1102 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001103 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001104 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001105 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001106 break;
1107 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001108 break;
1109
1110 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001111 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001112 PyErr_SetString(PyExc_SystemError,
1113 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001114 break;
1115 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001116 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001117 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001118 break;
1119
1120 case RETURN_VALUE:
1121 retval = POP();
1122 why = WHY_RETURN;
1123 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001124
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001125 case EXEC_STMT:
1126 w = POP();
1127 v = POP();
1128 u = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001129 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001130 Py_DECREF(u);
1131 Py_DECREF(v);
1132 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001133 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001134
Guido van Rossum374a9221991-04-04 10:40:29 +00001135 case POP_BLOCK:
1136 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001137 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001138 while (STACK_LEVEL() > b->b_level) {
1139 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001140 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001141 }
1142 }
1143 break;
1144
1145 case END_FINALLY:
1146 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001147 if (PyInt_Check(v)) {
1148 why = (enum why_code) PyInt_AsLong(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 if (why == WHY_RETURN)
1150 retval = POP();
1151 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001152 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001153 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001154 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001155 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001156 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001157 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001158 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001159 else if (v != Py_None) {
1160 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001161 "'finally' pops bad exception");
1162 why = WHY_EXCEPTION;
1163 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001164 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001165 break;
1166
1167 case BUILD_CLASS:
Guido van Rossum25831651993-05-19 14:50:45 +00001168 u = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001169 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +00001170 w = POP();
1171 x = build_class(u, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001172 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001173 Py_DECREF(u);
1174 Py_DECREF(v);
1175 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001176 break;
1177
1178 case STORE_NAME:
1179 w = GETNAMEV(oparg);
1180 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001181 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001182 PyErr_SetString(PyExc_SystemError,
1183 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001184 break;
1185 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001186 err = PyDict_SetItem(x, w, v);
1187 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001188 break;
1189
1190 case DELETE_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001191 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001192 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001193 PyErr_SetString(PyExc_SystemError,
1194 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001195 break;
1196 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001197 if ((err = PyDict_DelItem(x, w)) != 0)
1198 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001199 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001200
1201#ifdef CASE_TOO_BIG
1202 default: switch (opcode) {
1203#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001204
1205 case UNPACK_TUPLE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001206 case UNPACK_LIST:
1207 v = POP();
Barry Warsawe42b18f1997-08-25 22:13:04 +00001208 if (PyTuple_Check(v)) {
1209 if (PyTuple_Size(v) != oparg) {
1210 PyErr_SetString(PyExc_ValueError,
1211 "unpack tuple of wrong size");
1212 why = WHY_EXCEPTION;
1213 }
1214 else {
1215 for (; --oparg >= 0; ) {
1216 w = PyTuple_GET_ITEM(v, oparg);
1217 Py_INCREF(w);
1218 PUSH(w);
1219 }
1220 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001221 }
Barry Warsawe42b18f1997-08-25 22:13:04 +00001222 else if (PyList_Check(v)) {
1223 if (PyList_Size(v) != oparg) {
1224 PyErr_SetString(PyExc_ValueError,
1225 "unpack list of wrong size");
1226 why = WHY_EXCEPTION;
1227 }
1228 else {
1229 for (; --oparg >= 0; ) {
1230 w = PyList_GET_ITEM(v, oparg);
1231 Py_INCREF(w);
1232 PUSH(w);
1233 }
1234 }
1235 }
1236 else if (PySequence_Check(v)) {
1237 if (unpack_sequence(v, oparg,
1238 stack_pointer + oparg))
1239 stack_pointer += oparg;
1240 else
1241 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001242 }
1243 else {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001244 PyErr_SetString(PyExc_TypeError,
1245 "unpack non-sequence");
1246 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001247 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001248 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001249 break;
1250
1251 case STORE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001252 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001253 v = POP();
1254 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001255 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1256 Py_DECREF(v);
1257 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001258 break;
1259
1260 case DELETE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001261 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001262 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001263 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1264 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001265 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001266 break;
1267
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001268 case STORE_GLOBAL:
1269 w = GETNAMEV(oparg);
1270 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001271 err = PyDict_SetItem(f->f_globals, w, v);
1272 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001273 break;
1274
1275 case DELETE_GLOBAL:
1276 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001277 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1278 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001279 break;
1280
Guido van Rossum374a9221991-04-04 10:40:29 +00001281 case LOAD_CONST:
1282 x = GETCONST(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001283 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001284 PUSH(x);
1285 break;
1286
1287 case LOAD_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001288 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001289 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001290 PyErr_SetString(PyExc_SystemError,
1291 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001292 break;
1293 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001294 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001295 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001296 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001297 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001298 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001299 if (x == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001300 PyErr_SetObject(
1301 PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001302 break;
1303 }
1304 }
1305 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001306 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001307 PUSH(x);
1308 break;
1309
1310 case LOAD_GLOBAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001311 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001312 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001313 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001314 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001315 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001316 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001317 break;
1318 }
1319 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001320 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001321 PUSH(x);
1322 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001323
Guido van Rossum9bfef441993-03-29 10:43:31 +00001324 case LOAD_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001325 x = GETLOCAL(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001326 if (x == NULL) {
Guido van Rossum87460821999-06-22 14:47:32 +00001327 PyErr_SetObject(PyExc_UnboundLocalError,
Guido van Rossumb209a111997-04-29 18:18:01 +00001328 PyTuple_GetItem(co->co_varnames,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001329 oparg));
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001330 break;
1331 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001332 Py_INCREF(x);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001333 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001334 if (x != NULL) continue;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001335 break;
1336
1337 case STORE_FAST:
Guido van Rossum25831651993-05-19 14:50:45 +00001338 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001339 SETLOCAL(oparg, v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001340 continue;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001341
1342 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001343 x = GETLOCAL(oparg);
1344 if (x == NULL) {
Guido van Rossum87460821999-06-22 14:47:32 +00001345 PyErr_SetObject(PyExc_UnboundLocalError,
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001346 PyTuple_GetItem(co->co_varnames,
1347 oparg));
1348 break;
1349 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001350 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001351 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001352
1353 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001354 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001355 if (x != NULL) {
1356 for (; --oparg >= 0;) {
1357 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001358 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001359 }
1360 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001361 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001362 }
1363 break;
1364
1365 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001366 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001367 if (x != NULL) {
1368 for (; --oparg >= 0;) {
1369 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001370 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001371 }
1372 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001373 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001374 }
1375 break;
1376
1377 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001378 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001379 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001380 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001381 break;
1382
1383 case LOAD_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001384 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001385 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001386 x = PyObject_GetAttr(v, w);
1387 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001388 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001389 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001390 break;
1391
1392 case COMPARE_OP:
1393 w = POP();
1394 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +00001395 if (PyInt_Check(v) && PyInt_Check(w)) {
1396 /* INLINE: cmp(int, int) */
1397 register long a, b;
1398 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001399 a = PyInt_AS_LONG(v);
1400 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001401 switch (oparg) {
1402 case LT: res = a < b; break;
1403 case LE: res = a <= b; break;
1404 case EQ: res = a == b; break;
1405 case NE: res = a != b; break;
1406 case GT: res = a > b; break;
1407 case GE: res = a >= b; break;
1408 case IS: res = v == w; break;
1409 case IS_NOT: res = v != w; break;
1410 default: goto slow_compare;
1411 }
1412 x = res ? Py_True : Py_False;
1413 Py_INCREF(x);
1414 }
1415 else {
1416 slow_compare:
1417 x = cmp_outcome(oparg, v, w);
1418 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001419 Py_DECREF(v);
1420 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001422 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001423 break;
1424
1425 case IMPORT_NAME:
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001426 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001427 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001428 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001429 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001430 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001431 break;
1432 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001433 u = find_from_args(f, INSTR_OFFSET());
1434 if (u == NULL) {
1435 x = u;
1436 break;
Guido van Rossum24c13741995-02-14 09:42:43 +00001437 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001438 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001439 w,
1440 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001441 f->f_locals == NULL ?
1442 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001443 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001444 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001445 if (w == NULL) {
1446 x = NULL;
1447 break;
1448 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001449 x = PyEval_CallObject(x, w);
1450 Py_DECREF(w);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001451 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001452 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 break;
1454
1455 case IMPORT_FROM:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001456 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001458 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001459 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001460 PyErr_SetString(PyExc_SystemError,
1461 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001462 break;
1463 }
1464 err = import_from(x, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001465 PyFrame_LocalsToFast(f, 0);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001466 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001467 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001468
Guido van Rossum374a9221991-04-04 10:40:29 +00001469 case JUMP_FORWARD:
1470 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001471 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001472
1473 case JUMP_IF_FALSE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001474 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001475 if (err > 0)
1476 err = 0;
1477 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001479 else
1480 break;
1481 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001482
1483 case JUMP_IF_TRUE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001484 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001485 if (err > 0) {
1486 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001487 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001488 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001489 else if (err == 0)
1490 ;
1491 else
1492 break;
1493 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001494
1495 case JUMP_ABSOLUTE:
1496 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001497 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001498
1499 case FOR_LOOP:
1500 /* for v in s: ...
1501 On entry: stack contains s, i.
1502 On exit: stack contains s, i+1, s[i];
1503 but if loop exhausted:
1504 s, i are popped, and we jump */
1505 w = POP(); /* Loop index */
1506 v = POP(); /* Sequence object */
1507 u = loop_subscript(v, w);
1508 if (u != NULL) {
1509 PUSH(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001510 x = PyInt_FromLong(PyInt_AsLong(w)+1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001512 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001513 PUSH(u);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001514 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001515 }
1516 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001517 Py_DECREF(v);
1518 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001519 /* A NULL can mean "s exhausted"
1520 but also an error: */
Guido van Rossumb209a111997-04-29 18:18:01 +00001521 if (PyErr_Occurred())
Guido van Rossum374a9221991-04-04 10:40:29 +00001522 why = WHY_EXCEPTION;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001523 else {
Guido van Rossum374a9221991-04-04 10:40:29 +00001524 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001525 continue;
1526 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001527 }
1528 break;
1529
1530 case SETUP_LOOP:
1531 case SETUP_EXCEPT:
1532 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00001533 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Guido van Rossum374a9221991-04-04 10:40:29 +00001534 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001535 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001536
1537 case SET_LINENO:
Guido van Rossum96a42c81992-01-12 02:29:51 +00001538#ifdef LLTRACE
1539 if (lltrace)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001540 printf("--- %s:%d \n", filename, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001541#endif
Guido van Rossum96a42c81992-01-12 02:29:51 +00001542 f->f_lineno = oparg;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001543 if (f->f_trace == NULL)
1544 continue;
1545 /* Trace each line of code reached */
1546 f->f_lasti = INSTR_OFFSET();
1547 err = call_trace(&f->f_trace, &f->f_trace,
Guido van Rossumb209a111997-04-29 18:18:01 +00001548 f, "line", Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00001549 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001550
1551 case CALL_FUNCTION:
Jeremy Hylton76901512000-03-28 23:49:17 +00001552 case CALL_FUNCTION_VAR:
1553 case CALL_FUNCTION_KW:
1554 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00001555 {
Jeremy Hylton76901512000-03-28 23:49:17 +00001556 int na = oparg & 0xff;
1557 int nk = (oparg>>8) & 0xff;
1558 int flags = (opcode - CALL_FUNCTION) & 3;
1559 int n = na + 2*nk + (flags & 1) + ((flags >> 1) & 1);
1560 PyObject **pfunc = stack_pointer - n - 1;
1561 PyObject *func = *pfunc;
1562 PyObject *self = NULL;
1563 PyObject *class = NULL;
1564 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
1565 if (PyMethod_Check(func)) {
1566 self = PyMethod_Self(func);
1567 class = PyMethod_Class(func);
1568 func = PyMethod_Function(func);
1569 Py_INCREF(func);
1570 if (self != NULL) {
1571 Py_INCREF(self);
1572 Py_DECREF(*pfunc);
1573 *pfunc = self;
1574 na++;
1575 n++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001576 }
1577 else {
Jeremy Hylton76901512000-03-28 23:49:17 +00001578 /* Unbound methods must be called with an
1579 instance of the class (or a derived
1580 class) as first argument */
1581 if (na > 0 && (self = stack_pointer[-n]) != NULL
1582 && PyInstance_Check(self)
1583 && PyClass_IsSubclass((PyObject *)
1584 (((PyInstanceObject *)self)->in_class),
1585 class))
1586 /* Handy-dandy */ ;
1587 else {
1588 PyErr_SetString(PyExc_TypeError,
1589 "unbound method must be called with class instance 1st argument");
1590 x = NULL;
1591 break;
1592 }
1593 }
1594 }
1595 else
1596 Py_INCREF(func);
1597 if (PyFunction_Check(func) && flags == 0) {
1598 PyObject *co = PyFunction_GetCode(func);
1599 PyObject *globals = PyFunction_GetGlobals(func);
1600 PyObject *argdefs = PyFunction_GetDefaults(func);
1601 PyObject **d;
1602 int nd;
1603 if (argdefs != NULL) {
1604 d = &PyTuple_GET_ITEM(argdefs, 0);
1605 nd = ((PyTupleObject *)argdefs)->ob_size;
1606 }
1607 else {
1608 d = NULL;
1609 nd = 0;
1610 }
1611 x = eval_code2((PyCodeObject *)co, globals,
1612 (PyObject *)NULL, stack_pointer-n, na,
1613 stack_pointer-2*nk, nk, d, nd,
1614 class);
1615 }
1616 else {
1617 int nstar = 0;
Jeremy Hylton387b1012000-03-31 01:22:54 +00001618 PyObject *callargs;
Jeremy Hylton76901512000-03-28 23:49:17 +00001619 PyObject *stararg = 0;
1620 PyObject *kwdict = NULL;
1621 if (flags & 2) {
1622 kwdict = POP();
1623 if (!PyDict_Check(kwdict)) {
1624 PyErr_SetString(PyExc_TypeError,
1625 "** argument must be a dictionary");
1626 x = NULL;
1627 break;
1628 }
1629 }
1630 if (flags & 1) {
1631 stararg = POP();
1632 if (!PySequence_Check(stararg)) {
1633 PyErr_SetString(PyExc_TypeError,
1634 "* argument must be a sequence");
1635 x = NULL;
1636 break;
1637 }
Jeremy Hylton074c3e62000-03-30 23:55:31 +00001638 /* Convert abstract sequence to concrete tuple */
1639 if (!PyTuple_Check(stararg)) {
1640 PyObject *t = NULL;
1641 t = PySequence_Tuple(stararg);
1642 if (t == NULL) {
1643 x = NULL;
1644 break;
1645 }
1646 Py_DECREF(stararg);
1647 stararg = t;
1648 }
1649 nstar = PyTuple_GET_SIZE(stararg);
Barry Warsaw4961ef72000-03-29 18:30:03 +00001650 if (nstar < 0) {
Barry Warsawb2ba9d82000-03-29 18:36:49 +00001651 x = NULL;
1652 break;
Barry Warsaw4961ef72000-03-29 18:30:03 +00001653 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001654 }
1655 if (nk > 0) {
1656 if (kwdict == NULL) {
1657 kwdict = PyDict_New();
1658 if (kwdict == NULL) {
1659 x = NULL;
1660 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001661 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001662 }
Jeremy Hylton074c3e62000-03-30 23:55:31 +00001663 else {
1664 PyObject *d = PyDict_Copy(kwdict);
1665 if (d == NULL) {
1666 x = NULL;
1667 break;
1668 }
1669 Py_DECREF(kwdict);
1670 kwdict = d;
1671 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001672 err = 0;
1673 while (--nk >= 0) {
1674 PyObject *value = POP();
1675 PyObject *key = POP();
1676 if (PyDict_GetItem(kwdict, key) != NULL) {
1677 err = 1;
1678 PyErr_Format(PyExc_TypeError,
1679 "keyword parameter redefined: %.400s",
1680 PyString_AsString(key));
Jeremy Hylton387b1012000-03-31 01:22:54 +00001681 Py_DECREF(key);
1682 Py_DECREF(value);
Jeremy Hylton76901512000-03-28 23:49:17 +00001683 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001684 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001685 err = PyDict_SetItem(kwdict, key, value);
1686 Py_DECREF(key);
1687 Py_DECREF(value);
1688 if (err)
1689 break;
1690 }
1691 if (err) {
Jeremy Hylton76901512000-03-28 23:49:17 +00001692 Py_DECREF(kwdict);
1693 break;
1694 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001695 }
Jeremy Hylton387b1012000-03-31 01:22:54 +00001696 callargs = PyTuple_New(na + nstar);
1697 if (callargs == NULL) {
Jeremy Hylton76901512000-03-28 23:49:17 +00001698 x = NULL;
1699 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001700 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001701 if (stararg) {
Jeremy Hylton76901512000-03-28 23:49:17 +00001702 int i;
Jeremy Hylton76901512000-03-28 23:49:17 +00001703 for (i = 0; i < nstar; i++) {
1704 PyObject *a = PyTuple_GET_ITEM(stararg, i);
1705 Py_INCREF(a);
Jeremy Hylton387b1012000-03-31 01:22:54 +00001706 PyTuple_SET_ITEM(callargs, na + i, a);
Jeremy Hylton76901512000-03-28 23:49:17 +00001707 }
1708 Py_DECREF(stararg);
1709 }
1710 while (--na >= 0) {
1711 w = POP();
Jeremy Hylton387b1012000-03-31 01:22:54 +00001712 PyTuple_SET_ITEM(callargs, na, w);
Jeremy Hylton76901512000-03-28 23:49:17 +00001713 }
Jeremy Hylton387b1012000-03-31 01:22:54 +00001714 x = PyEval_CallObjectWithKeywords(func,
1715 callargs,
1716 kwdict);
1717 Py_DECREF(callargs);
Jeremy Hylton76901512000-03-28 23:49:17 +00001718 Py_XDECREF(kwdict);
1719 }
1720 Py_DECREF(func);
1721 while (stack_pointer > pfunc) {
1722 w = POP();
1723 Py_DECREF(w);
1724 }
1725 PUSH(x);
1726 if (x != NULL) continue;
1727 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001728 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001729
Guido van Rossum681d79a1995-07-18 14:51:37 +00001730 case MAKE_FUNCTION:
1731 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00001732 x = PyFunction_New(v, f->f_globals);
1733 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001734 /* XXX Maybe this should be a separate opcode? */
1735 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001736 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001737 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001738 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001739 x = NULL;
1740 break;
1741 }
1742 while (--oparg >= 0) {
1743 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001745 }
1746 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001747 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001748 }
1749 PUSH(x);
1750 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001751
1752 case BUILD_SLICE:
1753 if (oparg == 3)
1754 w = POP();
1755 else
1756 w = NULL;
1757 v = POP();
1758 u = POP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00001759 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001760 Py_DECREF(u);
1761 Py_DECREF(v);
1762 Py_XDECREF(w);
Guido van Rossum8861b741996-07-30 16:49:37 +00001763 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001764 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00001765 break;
1766
1767
Guido van Rossum374a9221991-04-04 10:40:29 +00001768 default:
1769 fprintf(stderr,
1770 "XXX lineno: %d, opcode: %d\n",
Guido van Rossum96a42c81992-01-12 02:29:51 +00001771 f->f_lineno, opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00001772 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 why = WHY_EXCEPTION;
1774 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001775
1776#ifdef CASE_TOO_BIG
1777 }
1778#endif
1779
Guido van Rossum374a9221991-04-04 10:40:29 +00001780 } /* switch */
1781
1782 on_error:
1783
1784 /* Quickly continue if no error occurred */
1785
1786 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001787 if (err == 0 && x != NULL) {
1788#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00001789 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00001790 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00001791 fprintf(stderr,
1792 "XXX undetected error\n");
1793 else
1794#endif
1795 continue; /* Normal, fast path */
1796 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001797 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00001798 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00001799 err = 0;
1800 }
1801
Guido van Rossum374a9221991-04-04 10:40:29 +00001802 /* Double-check exception status */
1803
1804 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001805 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001806 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00001807 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00001808 why = WHY_EXCEPTION;
1809 }
1810 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00001811#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00001813 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00001814 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001815 fprintf(stderr,
1816 "XXX undetected error (why=%d)\n",
1817 why);
1818 why = WHY_EXCEPTION;
1819 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001820 }
1821#endif
1822
1823 /* Log traceback info if this is a real exception */
1824
1825 if (why == WHY_EXCEPTION) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001826 f->f_lasti = INSTR_OFFSET() - 1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001827 if (HAS_ARG(opcode))
Guido van Rossum96a42c81992-01-12 02:29:51 +00001828 f->f_lasti -= 2;
Guido van Rossumb209a111997-04-29 18:18:01 +00001829 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001830
Guido van Rossume59214e1994-08-30 08:01:59 +00001831 if (f->f_trace)
1832 call_exc_trace(&f->f_trace, &f->f_trace, f);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001833 if (tstate->sys_profilefunc)
1834 call_exc_trace(&tstate->sys_profilefunc,
1835 (PyObject**)0, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00001836 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001837
1838 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
1839
1840 if (why == WHY_RERAISE)
1841 why = WHY_EXCEPTION;
1842
1843 /* Unwind stacks if a (pseudo) exception occurred */
1844
1845 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001846 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001847 while (STACK_LEVEL() > b->b_level) {
1848 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001849 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 }
1851 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
1852 why = WHY_NOT;
1853 JUMPTO(b->b_handler);
1854 break;
1855 }
1856 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00001857 (b->b_type == SETUP_EXCEPT &&
1858 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001859 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001860 PyObject *exc, *val, *tb;
1861 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001863 val = Py_None;
1864 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00001865 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 /* Make the raw exception data
1867 available to the handler,
1868 so a program can emulate the
1869 Python main loop. Don't do
1870 this for 'finally'. */
1871 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00001872 PyErr_NormalizeException(
1873 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001874 set_exc_info(tstate,
1875 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001877 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 PUSH(val);
1879 PUSH(exc);
1880 }
1881 else {
1882 if (why == WHY_RETURN)
1883 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00001884 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 PUSH(v);
1886 }
1887 why = WHY_NOT;
1888 JUMPTO(b->b_handler);
1889 break;
1890 }
1891 } /* unwind stack */
1892
1893 /* End the loop if we still have an error (or return) */
1894
1895 if (why != WHY_NOT)
1896 break;
1897
1898 } /* main loop */
1899
1900 /* Pop remaining stack entries */
1901
1902 while (!EMPTY()) {
1903 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001904 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001905 }
1906
Guido van Rossum96a42c81992-01-12 02:29:51 +00001907 if (why != WHY_RETURN)
1908 retval = NULL;
1909
Guido van Rossume59214e1994-08-30 08:01:59 +00001910 if (f->f_trace) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001911 if (why == WHY_RETURN) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001912 if (call_trace(&f->f_trace, &f->f_trace, f,
1913 "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001914 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001915 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001916 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001917 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001918 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001919 }
1920
Guido van Rossuma027efa1997-05-05 20:56:21 +00001921 if (tstate->sys_profilefunc && why == WHY_RETURN) {
1922 if (call_trace(&tstate->sys_profilefunc, (PyObject**)0,
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001923 f, "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001924 Py_XDECREF(retval);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001925 retval = NULL;
1926 why = WHY_EXCEPTION;
1927 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001928 }
Guido van Rossuma4240131997-01-21 21:18:36 +00001929
Guido van Rossuma027efa1997-05-05 20:56:21 +00001930 reset_exc_info(tstate);
1931
1932 --tstate->recursion_depth;
Guido van Rossum8c5df061997-01-24 04:19:24 +00001933
1934 fail: /* Jump here from prelude on failure */
Guido van Rossum96a42c81992-01-12 02:29:51 +00001935
Guido van Rossum374a9221991-04-04 10:40:29 +00001936 /* Restore previous frame and release the current one */
Guido van Rossum681d79a1995-07-18 14:51:37 +00001937
Guido van Rossuma027efa1997-05-05 20:56:21 +00001938 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +00001939 Py_DECREF(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001940
Guido van Rossum96a42c81992-01-12 02:29:51 +00001941 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00001942}
1943
Guido van Rossuma027efa1997-05-05 20:56:21 +00001944static void
1945set_exc_info(tstate, type, value, tb)
1946 PyThreadState *tstate;
1947 PyObject *type;
1948 PyObject *value;
1949 PyObject *tb;
1950{
1951 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001952 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00001953
Guido van Rossuma027efa1997-05-05 20:56:21 +00001954 frame = tstate->frame;
1955 if (frame->f_exc_type == NULL) {
1956 /* This frame didn't catch an exception before */
1957 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00001958 if (tstate->exc_type == NULL) {
1959 Py_INCREF(Py_None);
1960 tstate->exc_type = Py_None;
1961 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001962 tmp_type = frame->f_exc_type;
1963 tmp_value = frame->f_exc_value;
1964 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001965 Py_XINCREF(tstate->exc_type);
1966 Py_XINCREF(tstate->exc_value);
1967 Py_XINCREF(tstate->exc_traceback);
1968 frame->f_exc_type = tstate->exc_type;
1969 frame->f_exc_value = tstate->exc_value;
1970 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001971 Py_XDECREF(tmp_type);
1972 Py_XDECREF(tmp_value);
1973 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001974 }
1975 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001976 tmp_type = tstate->exc_type;
1977 tmp_value = tstate->exc_value;
1978 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001979 Py_XINCREF(type);
1980 Py_XINCREF(value);
1981 Py_XINCREF(tb);
1982 tstate->exc_type = type;
1983 tstate->exc_value = value;
1984 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001985 Py_XDECREF(tmp_type);
1986 Py_XDECREF(tmp_value);
1987 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001988 /* For b/w compatibility */
1989 PySys_SetObject("exc_type", type);
1990 PySys_SetObject("exc_value", value);
1991 PySys_SetObject("exc_traceback", tb);
1992}
1993
1994static void
1995reset_exc_info(tstate)
1996 PyThreadState *tstate;
1997{
1998 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001999 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002000 frame = tstate->frame;
2001 if (frame->f_exc_type != NULL) {
2002 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002003 tmp_type = tstate->exc_type;
2004 tmp_value = tstate->exc_value;
2005 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002006 Py_XINCREF(frame->f_exc_type);
2007 Py_XINCREF(frame->f_exc_value);
2008 Py_XINCREF(frame->f_exc_traceback);
2009 tstate->exc_type = frame->f_exc_type;
2010 tstate->exc_value = frame->f_exc_value;
2011 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002012 Py_XDECREF(tmp_type);
2013 Py_XDECREF(tmp_value);
2014 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002015 /* For b/w compatibility */
2016 PySys_SetObject("exc_type", frame->f_exc_type);
2017 PySys_SetObject("exc_value", frame->f_exc_value);
2018 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2019 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002020 tmp_type = frame->f_exc_type;
2021 tmp_value = frame->f_exc_value;
2022 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002023 frame->f_exc_type = NULL;
2024 frame->f_exc_value = NULL;
2025 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002026 Py_XDECREF(tmp_type);
2027 Py_XDECREF(tmp_value);
2028 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002029}
2030
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002031/* Logic for the raise statement (too complicated for inlining).
2032 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002033static enum why_code
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002034do_raise(type, value, tb)
Guido van Rossumb209a111997-04-29 18:18:01 +00002035 PyObject *type, *value, *tb;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002036{
Guido van Rossumd295f121998-04-09 21:39:57 +00002037 if (type == NULL) {
2038 /* Reraise */
2039 PyThreadState *tstate = PyThreadState_Get();
2040 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2041 value = tstate->exc_value;
2042 tb = tstate->exc_traceback;
2043 Py_XINCREF(type);
2044 Py_XINCREF(value);
2045 Py_XINCREF(tb);
2046 }
2047
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002048 /* We support the following forms of raise:
2049 raise <class>, <classinstance>
2050 raise <class>, <argument tuple>
2051 raise <class>, None
2052 raise <class>, <argument>
2053 raise <classinstance>, None
2054 raise <string>, <object>
2055 raise <string>, None
2056
2057 An omitted second argument is the same as None.
2058
2059 In addition, raise <tuple>, <anything> is the same as
2060 raising the tuple's first item (and it better have one!);
2061 this rule is applied recursively.
2062
2063 Finally, an optional third argument can be supplied, which
2064 gives the traceback to be substituted (useful when
2065 re-raising an exception after examining it). */
2066
2067 /* First, check the traceback argument, replacing None with
2068 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002069 if (tb == Py_None) {
2070 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002071 tb = NULL;
2072 }
2073 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002074 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002075 "raise 3rd arg must be traceback or None");
2076 goto raise_error;
2077 }
2078
2079 /* Next, replace a missing value with None */
2080 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002081 value = Py_None;
2082 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002083 }
2084
2085 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002086 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2087 PyObject *tmp = type;
2088 type = PyTuple_GET_ITEM(type, 0);
2089 Py_INCREF(type);
2090 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002091 }
2092
Barry Warsaw4249f541997-08-22 21:26:19 +00002093 if (PyString_Check(type))
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002094 ;
Barry Warsaw4249f541997-08-22 21:26:19 +00002095
2096 else if (PyClass_Check(type))
2097 PyErr_NormalizeException(&type, &value, &tb);
2098
Guido van Rossumb209a111997-04-29 18:18:01 +00002099 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002100 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002101 if (value != Py_None) {
2102 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002103 "instance exception may not have a separate value");
2104 goto raise_error;
2105 }
2106 else {
2107 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002108 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002109 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002110 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2111 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002112 }
2113 }
2114 else {
2115 /* Not something you can raise. You get an exception
2116 anyway, just not what you specified :-) */
Guido van Rossumb209a111997-04-29 18:18:01 +00002117 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002118 "exceptions must be strings, classes, or instances");
2119 goto raise_error;
2120 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002121 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002122 if (tb == NULL)
2123 return WHY_EXCEPTION;
2124 else
2125 return WHY_RERAISE;
2126 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002127 Py_XDECREF(value);
2128 Py_XDECREF(type);
2129 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002130 return WHY_EXCEPTION;
2131}
2132
Barry Warsawe42b18f1997-08-25 22:13:04 +00002133static int
2134unpack_sequence(v, argcnt, sp)
2135 PyObject *v;
2136 int argcnt;
2137 PyObject **sp;
2138{
2139 int i;
2140 PyObject *w;
2141
2142 for (i = 0; i < argcnt; i++) {
2143 if (! (w = PySequence_GetItem(v, i))) {
2144 if (PyErr_ExceptionMatches(PyExc_IndexError))
2145 PyErr_SetString(PyExc_ValueError,
2146 "unpack sequence of wrong size");
2147 goto finally;
2148 }
2149 *--sp = w;
2150 }
2151 /* we better get an IndexError now */
2152 if (PySequence_GetItem(v, i) == NULL) {
2153 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2154 PyErr_Clear();
2155 return 1;
2156 }
2157 /* some other exception occurred. fall through to finally */
2158 }
2159 else
2160 PyErr_SetString(PyExc_ValueError,
2161 "unpack sequence of wrong size");
2162 /* fall through */
2163finally:
Barry Warsaw91010551997-08-25 22:30:51 +00002164 for (; i > 0; i--, sp++)
2165 Py_DECREF(*sp);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002166
2167 return 0;
2168}
2169
2170
Guido van Rossum96a42c81992-01-12 02:29:51 +00002171#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002172static int
2173prtrace(v, str)
Guido van Rossumb209a111997-04-29 18:18:01 +00002174 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002175 char *str;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002176{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002177 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002178 if (PyObject_Print(v, stdout, 0) != 0)
2179 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002180 printf("\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002181}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002182#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002183
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002184static void
2185call_exc_trace(p_trace, p_newtrace, f)
Guido van Rossumb209a111997-04-29 18:18:01 +00002186 PyObject **p_trace, **p_newtrace;
2187 PyFrameObject *f;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002188{
Guido van Rossumb209a111997-04-29 18:18:01 +00002189 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002190 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002191 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002192 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002193 value = Py_None;
2194 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002195 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002196 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002197 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002198 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002199 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002200 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002201 err = call_trace(p_trace, p_newtrace, f, "exception", arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002202 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002203 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002204 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002205 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002206 Py_XDECREF(type);
2207 Py_XDECREF(value);
2208 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002209 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002210}
2211
2212static int
2213call_trace(p_trace, p_newtrace, f, msg, arg)
Guido van Rossumb209a111997-04-29 18:18:01 +00002214 PyObject **p_trace; /* in/out; may not be NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002215 may not point to NULL variable initially */
Guido van Rossumb209a111997-04-29 18:18:01 +00002216 PyObject **p_newtrace; /* in/out; may be NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002217 may point to NULL variable;
2218 may be same variable as p_newtrace */
Guido van Rossumb209a111997-04-29 18:18:01 +00002219 PyFrameObject *f;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002220 char *msg;
Guido van Rossumb209a111997-04-29 18:18:01 +00002221 PyObject *arg;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002222{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002223 PyThreadState *tstate = f->f_tstate;
Guido van Rossumb209a111997-04-29 18:18:01 +00002224 PyObject *args, *what;
2225 PyObject *res = NULL;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002226
Guido van Rossuma027efa1997-05-05 20:56:21 +00002227 if (tstate->tracing) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002228 /* Don't do recursive traces */
2229 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002230 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002231 *p_newtrace = NULL;
2232 }
2233 return 0;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002234 }
2235
Guido van Rossumb209a111997-04-29 18:18:01 +00002236 args = PyTuple_New(3);
Guido van Rossumf10570b1995-07-07 22:53:21 +00002237 if (args == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002238 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002239 what = PyString_FromString(msg);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002240 if (what == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002241 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002242 Py_INCREF(f);
2243 PyTuple_SET_ITEM(args, 0, (PyObject *)f);
2244 PyTuple_SET_ITEM(args, 1, what);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002245 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002246 arg = Py_None;
2247 Py_INCREF(arg);
2248 PyTuple_SET_ITEM(args, 2, arg);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002249 tstate->tracing++;
Guido van Rossumb209a111997-04-29 18:18:01 +00002250 PyFrame_FastToLocals(f);
2251 res = PyEval_CallObject(*p_trace, args); /* May clear *p_trace! */
2252 PyFrame_LocalsToFast(f, 1);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002253 tstate->tracing--;
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002254 cleanup:
Guido van Rossumb209a111997-04-29 18:18:01 +00002255 Py_XDECREF(args);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002256 if (res == NULL) {
2257 /* The trace proc raised an exception */
Guido van Rossumb209a111997-04-29 18:18:01 +00002258 PyTraceBack_Here(f);
2259 Py_XDECREF(*p_trace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002260 *p_trace = NULL;
2261 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002262 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002263 *p_newtrace = NULL;
2264 }
Barry Warsawf6202631999-09-08 16:26:33 +00002265 /* to be extra double plus sure we don't get recursive
2266 * calls inf either tracefunc or profilefunc gets an
2267 * exception, zap the global variables.
2268 */
2269 Py_XDECREF(tstate->sys_tracefunc);
2270 tstate->sys_tracefunc = NULL;
2271 Py_XDECREF(tstate->sys_profilefunc);
2272 tstate->sys_profilefunc = NULL;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002273 return -1;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002274 }
2275 else {
2276 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002277 Py_XDECREF(*p_newtrace);
2278 if (res == Py_None)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002279 *p_newtrace = NULL;
2280 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002281 Py_INCREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002282 *p_newtrace = res;
2283 }
2284 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002285 Py_DECREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002286 return 0;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002287 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002288}
2289
Guido van Rossumb209a111997-04-29 18:18:01 +00002290PyObject *
2291PyEval_GetBuiltins()
Guido van Rossum6135a871995-01-09 17:53:26 +00002292{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002293 PyThreadState *tstate = PyThreadState_Get();
2294 PyFrameObject *current_frame = tstate->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002295 if (current_frame == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002296 return tstate->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00002297 else
2298 return current_frame->f_builtins;
2299}
2300
Guido van Rossumb209a111997-04-29 18:18:01 +00002301PyObject *
2302PyEval_GetLocals()
Guido van Rossum5b722181993-03-30 17:46:03 +00002303{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002304 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum5b722181993-03-30 17:46:03 +00002305 if (current_frame == NULL)
2306 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002307 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00002308 return current_frame->f_locals;
2309}
2310
Guido van Rossumb209a111997-04-29 18:18:01 +00002311PyObject *
2312PyEval_GetGlobals()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002313{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002314 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002315 if (current_frame == NULL)
2316 return NULL;
2317 else
2318 return current_frame->f_globals;
2319}
2320
Guido van Rossumb209a111997-04-29 18:18:01 +00002321PyObject *
2322PyEval_GetFrame()
Guido van Rossume59214e1994-08-30 08:01:59 +00002323{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002324 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossumb209a111997-04-29 18:18:01 +00002325 return (PyObject *)current_frame;
Guido van Rossume59214e1994-08-30 08:01:59 +00002326}
2327
Guido van Rossum6135a871995-01-09 17:53:26 +00002328int
Guido van Rossumb209a111997-04-29 18:18:01 +00002329PyEval_GetRestricted()
Guido van Rossum6135a871995-01-09 17:53:26 +00002330{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002331 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002332 return current_frame == NULL ? 0 : current_frame->f_restricted;
2333}
2334
Guido van Rossumbe270261997-05-22 22:26:18 +00002335int
Guido van Rossumb209a111997-04-29 18:18:01 +00002336Py_FlushLine()
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002337{
Guido van Rossumb209a111997-04-29 18:18:01 +00002338 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00002339 if (f == NULL)
2340 return 0;
2341 if (!PyFile_SoftSpace(f, 0))
2342 return 0;
2343 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002344}
2345
Guido van Rossum3f5da241990-12-20 15:06:42 +00002346
Guido van Rossum681d79a1995-07-18 14:51:37 +00002347/* External interface to call any callable object.
2348 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002349
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002350#undef PyEval_CallObject
2351/* for backward compatibility: export this interface */
2352
Guido van Rossumb209a111997-04-29 18:18:01 +00002353PyObject *
2354PyEval_CallObject(func, arg)
2355 PyObject *func;
2356 PyObject *arg;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002357{
Guido van Rossumb209a111997-04-29 18:18:01 +00002358 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002359}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002360#define PyEval_CallObject(func,arg) \
2361 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00002362
Guido van Rossumb209a111997-04-29 18:18:01 +00002363PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002364PyEval_CallObjectWithKeywords(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002365 PyObject *func;
2366 PyObject *arg;
2367 PyObject *kw;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002368{
2369 ternaryfunc call;
Guido van Rossumb209a111997-04-29 18:18:01 +00002370 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002371
2372 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002373 arg = PyTuple_New(0);
2374 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002375 PyErr_SetString(PyExc_TypeError,
2376 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002377 return NULL;
2378 }
2379 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002380 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002381
Guido van Rossumb209a111997-04-29 18:18:01 +00002382 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002383 PyErr_SetString(PyExc_TypeError,
2384 "keyword list must be a dictionary");
Guido van Rossume3e61c11995-08-04 04:14:47 +00002385 return NULL;
2386 }
2387
Guido van Rossum150b2df1996-12-05 23:17:11 +00002388 if ((call = func->ob_type->tp_call) != NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002389 result = (*call)(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002390 else if (PyMethod_Check(func) || PyFunction_Check(func))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002391 result = call_function(func, arg, kw);
2392 else
2393 result = call_builtin(func, arg, kw);
2394
Guido van Rossumb209a111997-04-29 18:18:01 +00002395 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002396
Guido van Rossumb209a111997-04-29 18:18:01 +00002397 if (result == NULL && !PyErr_Occurred())
2398 PyErr_SetString(PyExc_SystemError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002399 "NULL result without error in call_object");
Guido van Rossume59214e1994-08-30 08:01:59 +00002400
2401 return result;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002402}
2403
Guido van Rossumb209a111997-04-29 18:18:01 +00002404static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002405call_builtin(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002406 PyObject *func;
2407 PyObject *arg;
2408 PyObject *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002409{
Guido van Rossumb209a111997-04-29 18:18:01 +00002410 if (PyCFunction_Check(func)) {
2411 PyCFunction meth = PyCFunction_GetFunction(func);
2412 PyObject *self = PyCFunction_GetSelf(func);
2413 int flags = PyCFunction_GetFlags(func);
Guido van Rossumff8b4941995-07-26 18:16:42 +00002414 if (!(flags & METH_VARARGS)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002415 int size = PyTuple_Size(arg);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002416 if (size == 1)
Guido van Rossumb209a111997-04-29 18:18:01 +00002417 arg = PyTuple_GET_ITEM(arg, 0);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002418 else if (size == 0)
2419 arg = NULL;
2420 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002421 if (flags & METH_KEYWORDS)
2422 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002423 if (kw != NULL && PyDict_Size(kw) != 0) {
2424 PyErr_SetString(PyExc_TypeError,
Guido van Rossumff8b4941995-07-26 18:16:42 +00002425 "this function takes no keyword arguments");
2426 return NULL;
2427 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002428 return (*meth)(self, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002429 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002430 if (PyClass_Check(func)) {
2431 return PyInstance_New(func, arg, kw);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002432 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002433 if (PyInstance_Check(func)) {
2434 PyObject *res, *call = PyObject_GetAttrString(func,"__call__");
Guido van Rossume59214e1994-08-30 08:01:59 +00002435 if (call == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002436 PyErr_Clear();
2437 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002438 "no __call__ method defined");
Guido van Rossume59214e1994-08-30 08:01:59 +00002439 return NULL;
2440 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002441 res = PyEval_CallObjectWithKeywords(call, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002442 Py_DECREF(call);
Guido van Rossume59214e1994-08-30 08:01:59 +00002443 return res;
2444 }
Guido van Rossumba98a421999-11-15 19:29:33 +00002445 PyErr_Format(PyExc_TypeError, "call of non-function (type %.400s)",
Guido van Rossum2d1ad391998-08-25 18:16:54 +00002446 func->ob_type->tp_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002447 return NULL;
2448}
2449
Guido van Rossumb209a111997-04-29 18:18:01 +00002450static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002451call_function(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002452 PyObject *func;
2453 PyObject *arg;
2454 PyObject *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002455{
Guido van Rossumb209a111997-04-29 18:18:01 +00002456 PyObject *class = NULL; /* == owner */
2457 PyObject *argdefs;
2458 PyObject **d, **k;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002459 int nk, nd;
Guido van Rossumb209a111997-04-29 18:18:01 +00002460 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002461
Guido van Rossumb209a111997-04-29 18:18:01 +00002462 if (kw != NULL && !PyDict_Check(kw)) {
2463 PyErr_BadInternalCall();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002464 return NULL;
2465 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002466
Guido van Rossumb209a111997-04-29 18:18:01 +00002467 if (PyMethod_Check(func)) {
2468 PyObject *self = PyMethod_Self(func);
2469 class = PyMethod_Class(func);
2470 func = PyMethod_Function(func);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002471 if (self == NULL) {
2472 /* Unbound methods must be called with an instance of
2473 the class (or a derived class) as first argument */
Guido van Rossumb209a111997-04-29 18:18:01 +00002474 if (PyTuple_Size(arg) >= 1) {
2475 self = PyTuple_GET_ITEM(arg, 0);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002476 if (self != NULL &&
Guido van Rossumb209a111997-04-29 18:18:01 +00002477 PyInstance_Check(self) &&
2478 PyClass_IsSubclass((PyObject *)
2479 (((PyInstanceObject *)self)->in_class),
Guido van Rossumb3f72581993-05-21 19:56:10 +00002480 class))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002481 /* Handy-dandy */ ;
Guido van Rossumb3f72581993-05-21 19:56:10 +00002482 else
2483 self = NULL;
2484 }
2485 if (self == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002486 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002487 "unbound method must be called with class instance 1st argument");
Guido van Rossumb3f72581993-05-21 19:56:10 +00002488 return NULL;
2489 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002490 Py_INCREF(arg);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002491 }
2492 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002493 int argcount = PyTuple_Size(arg);
2494 PyObject *newarg = PyTuple_New(argcount + 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002495 int i;
Guido van Rossum81daa321993-05-20 14:24:46 +00002496 if (newarg == NULL)
2497 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002498 Py_INCREF(self);
2499 PyTuple_SET_ITEM(newarg, 0, self);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002500 for (i = 0; i < argcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002501 PyObject *v = PyTuple_GET_ITEM(arg, i);
2502 Py_XINCREF(v);
2503 PyTuple_SET_ITEM(newarg, i+1, v);
Guido van Rossum81daa321993-05-20 14:24:46 +00002504 }
2505 arg = newarg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002506 }
Guido van Rossum7859f871998-07-08 14:58:16 +00002507 if (!PyFunction_Check(func)) {
2508 result = PyEval_CallObjectWithKeywords(func, arg, kw);
2509 Py_DECREF(arg);
2510 return result;
2511 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002512 }
2513 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002514 if (!PyFunction_Check(func)) {
Guido van Rossum2d1ad391998-08-25 18:16:54 +00002515 PyErr_Format(PyExc_TypeError,
Guido van Rossum5db862d2000-04-10 12:46:51 +00002516 "call of non-function (type %.200s)",
Guido van Rossum2d1ad391998-08-25 18:16:54 +00002517 func->ob_type->tp_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002518 return NULL;
2519 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002520 Py_INCREF(arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002521 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002522
2523 argdefs = PyFunction_GetDefaults(func);
Guido van Rossumb209a111997-04-29 18:18:01 +00002524 if (argdefs != NULL && PyTuple_Check(argdefs)) {
2525 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
2526 nd = PyTuple_Size(argdefs);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002527 }
2528 else {
2529 d = NULL;
2530 nd = 0;
2531 }
2532
2533 if (kw != NULL) {
2534 int pos, i;
Guido van Rossumb209a111997-04-29 18:18:01 +00002535 nk = PyDict_Size(kw);
2536 k = PyMem_NEW(PyObject *, 2*nk);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002537 if (k == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002538 PyErr_NoMemory();
2539 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002540 return NULL;
Guido van Rossume59214e1994-08-30 08:01:59 +00002541 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002542 pos = i = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002543 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002544 i += 2;
2545 nk = i/2;
2546 /* XXX This is broken if the caller deletes dict items! */
2547 }
2548 else {
2549 k = NULL;
2550 nk = 0;
Guido van Rossume59214e1994-08-30 08:01:59 +00002551 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002552
Guido van Rossum681d79a1995-07-18 14:51:37 +00002553 result = eval_code2(
Guido van Rossumb209a111997-04-29 18:18:01 +00002554 (PyCodeObject *)PyFunction_GetCode(func),
2555 PyFunction_GetGlobals(func), (PyObject *)NULL,
2556 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
Guido van Rossum681d79a1995-07-18 14:51:37 +00002557 k, nk,
2558 d, nd,
2559 class);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002560
Guido van Rossumb209a111997-04-29 18:18:01 +00002561 Py_DECREF(arg);
2562 PyMem_XDEL(k);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002563
Guido van Rossum681d79a1995-07-18 14:51:37 +00002564 return result;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002565}
2566
Guido van Rossum3b9c6671996-07-30 18:40:29 +00002567#define SLICE_ERROR_MSG \
2568 "standard sequence type does not support step size other than one"
2569
Guido van Rossumb209a111997-04-29 18:18:01 +00002570static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002571loop_subscript(v, w)
Guido van Rossumb209a111997-04-29 18:18:01 +00002572 PyObject *v, *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002573{
Guido van Rossumb209a111997-04-29 18:18:01 +00002574 PySequenceMethods *sq = v->ob_type->tp_as_sequence;
Guido van Rossume59214e1994-08-30 08:01:59 +00002575 int i;
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00002576 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002577 PyErr_SetString(PyExc_TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002578 return NULL;
2579 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002580 i = PyInt_AsLong(w);
Guido van Rossume59214e1994-08-30 08:01:59 +00002581 v = (*sq->sq_item)(v, i);
2582 if (v)
2583 return v;
Barry Warsaw4249f541997-08-22 21:26:19 +00002584 if (PyErr_ExceptionMatches(PyExc_IndexError))
Guido van Rossumb209a111997-04-29 18:18:01 +00002585 PyErr_Clear();
Guido van Rossume59214e1994-08-30 08:01:59 +00002586 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002587}
2588
2589static int
Guido van Rossuma027efa1997-05-05 20:56:21 +00002590slice_index(v, pi)
Guido van Rossumb209a111997-04-29 18:18:01 +00002591 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002592 int *pi;
2593{
2594 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002595 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00002596 if (PyInt_Check(v)) {
2597 x = PyInt_AsLong(v);
2598 } else if (PyLong_Check(v)) {
2599 x = PyLong_AsLong(v);
2600 if (x==-1 && PyErr_Occurred()) {
2601 PyObject *long_zero;
2602
2603 if (!PyErr_ExceptionMatches( PyExc_OverflowError ) ) {
2604 /* It's not an overflow error, so just
2605 signal an error */
2606 return -1;
2607 }
2608
2609 /* It's an overflow error, so we need to
2610 check the sign of the long integer,
2611 set the value to INT_MAX or 0, and clear
2612 the error. */
2613
2614 /* Create a long integer with a value of 0 */
2615 long_zero = PyLong_FromLong( 0L );
2616 if (long_zero == NULL) return -1;
2617
2618 /* Check sign */
2619 if (PyObject_Compare(long_zero, v) < 0)
2620 x = INT_MAX;
2621 else
2622 x = 0;
2623
2624 /* Free the long integer we created, and clear the
2625 OverflowError */
2626 Py_DECREF(long_zero);
2627 PyErr_Clear();
2628 }
2629 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002630 PyErr_SetString(PyExc_TypeError,
2631 "slice index must be int");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002632 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002633 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002634 /* Truncate -- very long indices are truncated anyway */
2635 if (x > INT_MAX)
2636 x = INT_MAX;
2637 else if (x < -INT_MAX)
2638 x = 0;
2639 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002640 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002641 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002642}
2643
Guido van Rossumb209a111997-04-29 18:18:01 +00002644static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002645apply_slice(u, v, w) /* return u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00002646 PyObject *u, *v, *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002647{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002648 int ilow = 0, ihigh = INT_MAX;
2649 if (slice_index(v, &ilow) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002650 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002651 if (slice_index(w, &ihigh) != 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +00002652 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002653 return PySequence_GetSlice(u, ilow, ihigh);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002654}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002655
2656static int
Guido van Rossum3f5da241990-12-20 15:06:42 +00002657assign_slice(u, v, w, x) /* u[v:w] = x */
Guido van Rossumb209a111997-04-29 18:18:01 +00002658 PyObject *u, *v, *w, *x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002659{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002660 int ilow = 0, ihigh = INT_MAX;
2661 if (slice_index(v, &ilow) != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002662 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002663 if (slice_index(w, &ihigh) != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002664 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002665 if (x == NULL)
2666 return PySequence_DelSlice(u, ilow, ihigh);
2667 else
2668 return PySequence_SetSlice(u, ilow, ihigh, x);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002669}
2670
Guido van Rossumb209a111997-04-29 18:18:01 +00002671static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002672cmp_outcome(op, v, w)
Guido van Rossum5b7313a1992-04-06 13:24:57 +00002673 int op;
Guido van Rossumb209a111997-04-29 18:18:01 +00002674 register PyObject *v;
2675 register PyObject *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002676{
2677 register int cmp;
2678 register int res = 0;
2679 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002680 case IS:
2681 case IS_NOT:
2682 res = (v == w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002683 if (op == (int) IS_NOT)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002684 res = !res;
2685 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002686 case IN:
2687 case NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00002688 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002689 if (res < 0)
2690 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002691 if (op == (int) NOT_IN)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002692 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002693 break;
2694 case EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00002695 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002696 break;
2697 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00002698 cmp = PyObject_Compare(v, w);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002699 if (cmp && PyErr_Occurred())
2700 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002701 switch (op) {
2702 case LT: res = cmp < 0; break;
2703 case LE: res = cmp <= 0; break;
2704 case EQ: res = cmp == 0; break;
2705 case NE: res = cmp != 0; break;
2706 case GT: res = cmp > 0; break;
2707 case GE: res = cmp >= 0; break;
2708 /* XXX no default? (res is initialized to 0 though) */
2709 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002710 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002711 v = res ? Py_True : Py_False;
2712 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002713 return v;
2714}
2715
Guido van Rossum3f5da241990-12-20 15:06:42 +00002716static int
2717import_from(locals, v, name)
Guido van Rossumb209a111997-04-29 18:18:01 +00002718 PyObject *locals;
2719 PyObject *v;
2720 PyObject *name;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002721{
Guido van Rossumb209a111997-04-29 18:18:01 +00002722 PyObject *w, *x;
2723 if (!PyModule_Check(v)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002724 PyErr_SetString(PyExc_TypeError,
2725 "import-from requires module object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002726 return -1;
2727 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002728 w = PyModule_GetDict(v);
2729 if (PyString_AsString(name)[0] == '*') {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002730 int pos, err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002731 PyObject *name, *value;
Guido van Rossum25831651993-05-19 14:50:45 +00002732 pos = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002733 while (PyDict_Next(w, &pos, &name, &value)) {
2734 if (!PyString_Check(name) ||
2735 PyString_AsString(name)[0] == '_')
Guido van Rossume9736fc1990-11-18 17:33:06 +00002736 continue;
Guido van Rossumb209a111997-04-29 18:18:01 +00002737 Py_INCREF(value);
2738 err = PyDict_SetItem(locals, name, value);
2739 Py_DECREF(value);
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002740 if (err != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002741 return -1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002742 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002743 return 0;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002744 }
2745 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002746 x = PyDict_GetItem(w, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002747 if (x == NULL) {
Jeremy Hylton76901512000-03-28 23:49:17 +00002748 PyErr_Format(PyExc_ImportError,
2749 "cannot import name %.230s",
2750 PyString_AsString(name));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002751 return -1;
2752 }
2753 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002754 return PyDict_SetItem(locals, name, x);
Guido van Rossume9736fc1990-11-18 17:33:06 +00002755 }
2756}
2757
Guido van Rossumb209a111997-04-29 18:18:01 +00002758static PyObject *
Guido van Rossum25831651993-05-19 14:50:45 +00002759build_class(methods, bases, name)
Guido van Rossumb209a111997-04-29 18:18:01 +00002760 PyObject *methods; /* dictionary */
2761 PyObject *bases; /* tuple containing classes */
2762 PyObject *name; /* string */
Guido van Rossume9736fc1990-11-18 17:33:06 +00002763{
Guido van Rossumcd649651997-08-22 16:56:16 +00002764 int i, n;
Guido van Rossumb209a111997-04-29 18:18:01 +00002765 if (!PyTuple_Check(bases)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002766 PyErr_SetString(PyExc_SystemError,
2767 "build_class with non-tuple bases");
Guido van Rossum25831651993-05-19 14:50:45 +00002768 return NULL;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002769 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002770 if (!PyDict_Check(methods)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002771 PyErr_SetString(PyExc_SystemError,
2772 "build_class with non-dictionary");
Guido van Rossume9736fc1990-11-18 17:33:06 +00002773 return NULL;
2774 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002775 if (!PyString_Check(name)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002776 PyErr_SetString(PyExc_SystemError,
Guido van Rossum7a5b7962000-03-31 13:52:29 +00002777 "build_class with non-string name");
Guido van Rossum25831651993-05-19 14:50:45 +00002778 return NULL;
2779 }
Guido van Rossumcd649651997-08-22 16:56:16 +00002780 n = PyTuple_Size(bases);
2781 for (i = 0; i < n; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002782 PyObject *base = PyTuple_GET_ITEM(bases, i);
2783 if (!PyClass_Check(base)) {
Guido van Rossum150b2df1996-12-05 23:17:11 +00002784 /* Call the base's *type*, if it is callable.
2785 This code is a hook for Donald Beaudry's
2786 and Jim Fulton's type extensions. In
2787 unexended Python it will never be triggered
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002788 since its types are not callable.
2789 Ditto: call the bases's *class*, if it has
2790 one. This makes the same thing possible
2791 without writing C code. A true meta-object
2792 protocol! */
2793 PyObject *basetype = (PyObject *)base->ob_type;
2794 PyObject *callable = NULL;
2795 if (PyCallable_Check(basetype))
2796 callable = basetype;
2797 else
2798 callable = PyObject_GetAttrString(
2799 base, "__class__");
2800 if (callable) {
2801 PyObject *args;
2802 PyObject *newclass = NULL;
2803 args = Py_BuildValue(
2804 "(OOO)", name, bases, methods);
2805 if (args != NULL) {
2806 newclass = PyEval_CallObject(
2807 callable, args);
2808 Py_DECREF(args);
2809 }
Guido van Rossum730806d1998-04-10 22:27:42 +00002810 if (callable != basetype) {
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002811 Py_DECREF(callable);
Guido van Rossum730806d1998-04-10 22:27:42 +00002812 }
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002813 return newclass;
Guido van Rossum150b2df1996-12-05 23:17:11 +00002814 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002815 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +00002816 "base is not a class object");
2817 return NULL;
2818 }
2819 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002820 return PyClass_New(bases, methods, name);
Guido van Rossum25831651993-05-19 14:50:45 +00002821}
2822
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002823static int
Guido van Rossuma027efa1997-05-05 20:56:21 +00002824exec_statement(f, prog, globals, locals)
2825 PyFrameObject *f;
Guido van Rossumb209a111997-04-29 18:18:01 +00002826 PyObject *prog;
2827 PyObject *globals;
2828 PyObject *locals;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002829{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002830 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00002831 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002832 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002833
Guido van Rossumb209a111997-04-29 18:18:01 +00002834 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
2835 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002836 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00002837 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002838 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00002839 locals = PyTuple_GetItem(prog, 2);
2840 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002841 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002842 if (globals == Py_None) {
2843 globals = PyEval_GetGlobals();
2844 if (locals == Py_None) {
2845 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002846 plain = 1;
2847 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002848 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002849 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002850 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00002851 if (!PyString_Check(prog) &&
2852 !PyCode_Check(prog) &&
2853 !PyFile_Check(prog)) {
2854 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002855 "exec 1st arg must be string, code or file object");
2856 return -1;
2857 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002858 if (!PyDict_Check(globals) || !PyDict_Check(locals)) {
2859 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002860 "exec 2nd/3rd args must be dict or None");
2861 return -1;
2862 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002863 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002864 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00002865 if (PyCode_Check(prog)) {
Guido van Rossuma400d8a2000-01-12 22:45:54 +00002866 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002867 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00002868 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002869 FILE *fp = PyFile_AsFile(prog);
2870 char *name = PyString_AsString(PyFile_Name(prog));
Guido van Rossuma400d8a2000-01-12 22:45:54 +00002871 v = PyRun_File(fp, name, Py_file_input, globals, locals);
2872 }
2873 else {
2874 char *s = PyString_AsString(prog);
2875 if ((int)strlen(s) != PyString_Size(prog)) {
2876 PyErr_SetString(PyExc_ValueError,
2877 "embedded '\\0' in exec string");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002878 return -1;
Guido van Rossuma400d8a2000-01-12 22:45:54 +00002879 }
2880 v = PyRun_String(s, Py_file_input, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002881 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00002882 if (plain)
2883 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002884 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002885 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00002886 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002887 return 0;
2888}
Guido van Rossum24c13741995-02-14 09:42:43 +00002889
Guido van Rossum1aa14831997-01-21 05:34:20 +00002890/* Hack for ni.py */
Guido van Rossumb209a111997-04-29 18:18:01 +00002891static PyObject *
Guido van Rossum24c13741995-02-14 09:42:43 +00002892find_from_args(f, nexti)
Guido van Rossumb209a111997-04-29 18:18:01 +00002893 PyFrameObject *f;
Guido van Rossum24c13741995-02-14 09:42:43 +00002894 int nexti;
2895{
2896 int opcode;
2897 int oparg;
Guido van Rossumb209a111997-04-29 18:18:01 +00002898 PyObject *list, *name;
Guido van Rossum24c13741995-02-14 09:42:43 +00002899 unsigned char *next_instr;
2900
Guido van Rossumd076c731998-10-07 19:42:25 +00002901 _PyCode_GETCODEPTR(f->f_code, &next_instr);
2902 next_instr += nexti;
2903
Guido van Rossum24c13741995-02-14 09:42:43 +00002904 opcode = (*next_instr++);
2905 if (opcode != IMPORT_FROM) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002906 Py_INCREF(Py_None);
2907 return Py_None;
Guido van Rossum24c13741995-02-14 09:42:43 +00002908 }
2909
Guido van Rossumb209a111997-04-29 18:18:01 +00002910 list = PyList_New(0);
Guido van Rossum24c13741995-02-14 09:42:43 +00002911 if (list == NULL)
2912 return NULL;
2913
2914 do {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002915 oparg = (next_instr[1]<<8) + next_instr[0];
2916 next_instr += 2;
Guido van Rossum24c13741995-02-14 09:42:43 +00002917 name = Getnamev(f, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002918 if (PyList_Append(list, name) < 0) {
2919 Py_DECREF(list);
Guido van Rossum24c13741995-02-14 09:42:43 +00002920 break;
2921 }
2922 opcode = (*next_instr++);
2923 } while (opcode == IMPORT_FROM);
2924
2925 return list;
2926}
Guido van Rossum950361c1997-01-24 13:49:28 +00002927
2928
2929#ifdef DYNAMIC_EXECUTION_PROFILE
2930
2931PyObject *
2932getarray(a)
2933 long a[256];
2934{
2935 int i;
2936 PyObject *l = PyList_New(256);
2937 if (l == NULL) return NULL;
2938 for (i = 0; i < 256; i++) {
2939 PyObject *x = PyInt_FromLong(a[i]);
2940 if (x == NULL) {
2941 Py_DECREF(l);
2942 return NULL;
2943 }
2944 PyList_SetItem(l, i, x);
2945 }
2946 for (i = 0; i < 256; i++)
2947 a[i] = 0;
2948 return l;
2949}
2950
2951PyObject *
2952_Py_GetDXProfile(self, args)
2953 PyObject *self, *args;
2954{
2955#ifndef DXPAIRS
2956 return getarray(dxp);
2957#else
2958 int i;
2959 PyObject *l = PyList_New(257);
2960 if (l == NULL) return NULL;
2961 for (i = 0; i < 257; i++) {
2962 PyObject *x = getarray(dxpairs[i]);
2963 if (x == NULL) {
2964 Py_DECREF(l);
2965 return NULL;
2966 }
2967 PyList_SetItem(l, i, x);
2968 }
2969 return l;
2970#endif
2971}
2972
2973#endif