blob: 7c1d407f19ce96bb34887426644799fcec3b444a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033
Guido van Rossum681d79a1995-07-18 14:51:37 +000034/* XXX TO DO:
35 XXX how to pass arguments to call_trace?
Guido van Rossum681d79a1995-07-18 14:51:37 +000036 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +000037 XXX document it!
38 */
39
Guido van Rossumb209a111997-04-29 18:18:01 +000040#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000041
Guido van Rossum10dc2e81990-11-18 17:27:39 +000042#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000043#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000044#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000045#include "opcode.h"
46
Guido van Rossumc6004111993-11-05 10:22:19 +000047#include <ctype.h>
48
Guido van Rossuma027efa1997-05-05 20:56:21 +000049#ifdef HAVE_LIMITS_H
50#include <limits.h>
51#else
52#define INT_MAX 2147483647
53#endif
54
Guido van Rossum04691fc1992-08-12 15:35:34 +000055/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000056/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000057
Guido van Rossum408027e1996-12-30 16:17:54 +000058#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000059/* For debugging the interpreter: */
60#define LLTRACE 1 /* Low-level trace feature */
61#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000062#endif
63
Guido van Rossum5b722181993-03-30 17:46:03 +000064
Guido van Rossum374a9221991-04-04 10:40:29 +000065/* Forward declarations */
Guido van Rossum374a9221991-04-04 10:40:29 +000066
Guido van Rossumb209a111997-04-29 18:18:01 +000067static PyObject *eval_code2 Py_PROTO((PyCodeObject *,
68 PyObject *, PyObject *,
69 PyObject **, int,
70 PyObject **, int,
71 PyObject **, int,
72 PyObject *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000073#ifdef LLTRACE
Guido van Rossumb209a111997-04-29 18:18:01 +000074static int prtrace Py_PROTO((PyObject *, char *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000075#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +000076static void call_exc_trace Py_PROTO((PyObject **, PyObject**,
77 PyFrameObject *));
78static int call_trace Py_PROTO((PyObject **, PyObject **,
79 PyFrameObject *, char *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000080static PyObject *call_builtin Py_PROTO((PyObject *, PyObject *, PyObject *));
81static PyObject *call_function Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000082static PyObject *loop_subscript Py_PROTO((PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000083static int slice_index Py_PROTO((PyObject *, int *));
Guido van Rossumb209a111997-04-29 18:18:01 +000084static PyObject *apply_slice Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000085static int assign_slice Py_PROTO((PyObject *, PyObject *,
86 PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000087static int cmp_member Py_PROTO((PyObject *, PyObject *));
88static PyObject *cmp_outcome Py_PROTO((int, PyObject *, PyObject *));
89static int import_from Py_PROTO((PyObject *, PyObject *, PyObject *));
90static PyObject *build_class Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000091static int exec_statement Py_PROTO((PyFrameObject *,
92 PyObject *, PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000093static PyObject *find_from_args Py_PROTO((PyFrameObject *, int));
Guido van Rossuma027efa1997-05-05 20:56:21 +000094static void set_exc_info Py_PROTO((PyThreadState *,
95 PyObject *, PyObject *, PyObject *));
96static void reset_exc_info Py_PROTO((PyThreadState *));
Guido van Rossum374a9221991-04-04 10:40:29 +000097
98
Guido van Rossum950361c1997-01-24 13:49:28 +000099/* Dynamic execution profile */
100#ifdef DYNAMIC_EXECUTION_PROFILE
101#ifdef DXPAIRS
102static long dxpairs[257][256];
103#define dxp dxpairs[256]
104#else
105static long dxp[256];
106#endif
107#endif
108
109
Guido van Rossume59214e1994-08-30 08:01:59 +0000110#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000111
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000112#include <errno.h>
113#include "thread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000114
Guido van Rossuma027efa1997-05-05 20:56:21 +0000115extern int _PyThread_Started; /* Flag for Py_Exit */
116
Guido van Rossuma9672091994-09-14 13:31:22 +0000117static type_lock interpreter_lock = 0;
118static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000119
120void
Guido van Rossumb209a111997-04-29 18:18:01 +0000121PyEval_InitThreads()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000122{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000123 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000124 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000125 _PyThread_Started = 1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000126 interpreter_lock = allocate_lock();
127 acquire_lock(interpreter_lock, 1);
Guido van Rossuma9672091994-09-14 13:31:22 +0000128 main_thread = get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000129}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000130
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000131void
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132PyEval_AcquireLock()
133{
134 acquire_lock(interpreter_lock, 1);
135}
136
137void
138PyEval_ReleaseLock()
139{
140 release_lock(interpreter_lock);
141}
142
143void
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000144PyEval_AcquireThread(tstate)
145 PyThreadState *tstate;
146{
147 if (tstate == NULL)
148 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
149 acquire_lock(interpreter_lock, 1);
150 if (PyThreadState_Swap(tstate) != NULL)
151 Py_FatalError(
152 "PyEval_AcquireThread: non-NULL old thread state");
153}
154
155void
156PyEval_ReleaseThread(tstate)
157 PyThreadState *tstate;
158{
159 if (tstate == NULL)
160 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
161 if (PyThreadState_Swap(NULL) != tstate)
162 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
163 release_lock(interpreter_lock);
164}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000165#endif
166
Guido van Rossumff4949e1992-08-05 19:58:53 +0000167/* Functions save_thread and restore_thread are always defined so
168 dynamically loaded modules needn't be compiled separately for use
169 with and without threads: */
170
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000171PyThreadState *
Guido van Rossumb209a111997-04-29 18:18:01 +0000172PyEval_SaveThread()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000173{
Guido van Rossume59214e1994-08-30 08:01:59 +0000174#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000175 if (interpreter_lock) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000176 PyThreadState *tstate = PyThreadState_Swap(NULL);
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000177 if (tstate == NULL)
178 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000179 release_lock(interpreter_lock);
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000180 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000182#endif
Guido van Rossum9575a441993-04-07 14:06:14 +0000183 return NULL;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000184}
185
186void
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000187PyEval_RestoreThread(tstate)
188 PyThreadState *tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000189{
Guido van Rossume59214e1994-08-30 08:01:59 +0000190#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000191 if (interpreter_lock) {
192 int err;
193 err = errno;
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000194 if (tstate == NULL)
195 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000196 acquire_lock(interpreter_lock, 1);
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000197 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000198 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199 }
200#endif
201}
202
203
Guido van Rossuma9672091994-09-14 13:31:22 +0000204/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
205 signal handlers or Mac I/O completion routines) can schedule calls
206 to a function to be called synchronously.
207 The synchronous function is called with one void* argument.
208 It should return 0 for success or -1 for failure -- failure should
209 be accompanied by an exception.
210
211 If registry succeeds, the registry function returns 0; if it fails
212 (e.g. due to too many pending calls) it returns -1 (without setting
213 an exception condition).
214
215 Note that because registry may occur from within signal handlers,
216 or other asynchronous events, calling malloc() is unsafe!
217
218#ifdef WITH_THREAD
219 Any thread can schedule pending calls, but only the main thread
220 will execute them.
221#endif
222
223 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
224 There are two possible race conditions:
225 (1) nested asynchronous registry calls;
226 (2) registry calls made while pending calls are being processed.
227 While (1) is very unlikely, (2) is a real possibility.
228 The current code is safe against (2), but not against (1).
229 The safety against (2) is derived from the fact that only one
230 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000231
Guido van Rossuma027efa1997-05-05 20:56:21 +0000232 XXX Darn! With the advent of thread state, we should have an array
233 of pending calls per thread in the thread state! Later...
234*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000235
Guido van Rossuma9672091994-09-14 13:31:22 +0000236#define NPENDINGCALLS 32
237static struct {
Guido van Rossumb209a111997-04-29 18:18:01 +0000238 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000239 ANY *arg;
240} pendingcalls[NPENDINGCALLS];
241static volatile int pendingfirst = 0;
242static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000243static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000244
245int
246Py_AddPendingCall(func, arg)
Guido van Rossumb209a111997-04-29 18:18:01 +0000247 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000248 ANY *arg;
249{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000250 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000251 int i, j;
252 /* XXX Begin critical section */
253 /* XXX If you want this to be safe against nested
254 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000255 if (busy)
256 return -1;
257 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000258 i = pendinglast;
259 j = (i + 1) % NPENDINGCALLS;
260 if (j == pendingfirst)
261 return -1; /* Queue full */
262 pendingcalls[i].func = func;
263 pendingcalls[i].arg = arg;
264 pendinglast = j;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000265 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000266 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000267 /* XXX End critical section */
268 return 0;
269}
270
Guido van Rossum180d7b41994-09-29 09:45:57 +0000271int
272Py_MakePendingCalls()
Guido van Rossuma9672091994-09-14 13:31:22 +0000273{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000274 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000275#ifdef WITH_THREAD
Guido van Rossuma027efa1997-05-05 20:56:21 +0000276 if (main_thread && get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000277 return 0;
278#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000279 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000280 return 0;
281 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000282 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000283 for (;;) {
284 int i;
Guido van Rossumb209a111997-04-29 18:18:01 +0000285 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000286 ANY *arg;
287 i = pendingfirst;
288 if (i == pendinglast)
289 break; /* Queue empty */
290 func = pendingcalls[i].func;
291 arg = pendingcalls[i].arg;
292 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000293 if (func(arg) < 0) {
294 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000295 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000296 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000297 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000298 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000299 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000300 return 0;
301}
302
303
Guido van Rossum374a9221991-04-04 10:40:29 +0000304/* Status code for main loop (reason for stack unwind) */
305
306enum why_code {
307 WHY_NOT, /* No error */
308 WHY_EXCEPTION, /* Exception occurred */
309 WHY_RERAISE, /* Exception re-raised by 'finally' */
310 WHY_RETURN, /* 'return' statement */
311 WHY_BREAK /* 'break' statement */
312};
313
Guido van Rossumb209a111997-04-29 18:18:01 +0000314static enum why_code do_raise Py_PROTO((PyObject *, PyObject *, PyObject *));
Barry Warsawe42b18f1997-08-25 22:13:04 +0000315static int unpack_sequence Py_PROTO((PyObject *, int, PyObject **));
Guido van Rossum1aa14831997-01-21 05:34:20 +0000316
Guido van Rossum374a9221991-04-04 10:40:29 +0000317
Guido van Rossum681d79a1995-07-18 14:51:37 +0000318/* Backward compatible interface */
Guido van Rossum374a9221991-04-04 10:40:29 +0000319
Guido van Rossumb209a111997-04-29 18:18:01 +0000320PyObject *
321PyEval_EvalCode(co, globals, locals)
322 PyCodeObject *co;
323 PyObject *globals;
324 PyObject *locals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000325{
326 return eval_code2(co,
327 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000328 (PyObject **)NULL, 0,
329 (PyObject **)NULL, 0,
330 (PyObject **)NULL, 0,
331 (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000332}
333
334
335/* Interpreter main loop */
336
Guido van Rossum8861b741996-07-30 16:49:37 +0000337#ifndef MAX_RECURSION_DEPTH
338#define MAX_RECURSION_DEPTH 10000
339#endif
340
Guido van Rossumb209a111997-04-29 18:18:01 +0000341static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +0000342eval_code2(co, globals, locals,
343 args, argcount, kws, kwcount, defs, defcount, owner)
Guido van Rossumb209a111997-04-29 18:18:01 +0000344 PyCodeObject *co;
345 PyObject *globals;
346 PyObject *locals;
347 PyObject **args;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000348 int argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000349 PyObject **kws; /* length: 2*kwcount */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000350 int kwcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000351 PyObject **defs;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000352 int defcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000353 PyObject *owner;
Guido van Rossum374a9221991-04-04 10:40:29 +0000354{
Guido van Rossum950361c1997-01-24 13:49:28 +0000355#ifdef DXPAIRS
356 int lastopcode = 0;
357#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000358 register unsigned char *next_instr;
Guido van Rossum150b2df1996-12-05 23:17:11 +0000359 register int opcode = 0; /* Current opcode */
360 register int oparg = 0; /* Current opcode argument, if any */
Guido van Rossumb209a111997-04-29 18:18:01 +0000361 register PyObject **stack_pointer;
Guido van Rossum374a9221991-04-04 10:40:29 +0000362 register enum why_code why; /* Reason for block stack unwind */
363 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000364 register PyObject *x; /* Result object -- NULL if error */
365 register PyObject *v; /* Temporary objects popped off stack */
366 register PyObject *w;
367 register PyObject *u;
368 register PyObject *t;
369 register PyFrameObject *f; /* Current frame */
370 register PyObject **fastlocals = NULL;
371 PyObject *retval = NULL; /* Return value */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000372 PyThreadState *tstate = PyThreadState_Get();
Guido van Rossum96a42c81992-01-12 02:29:51 +0000373#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000374 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000375#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000376#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000377 /* Make it easier to find out where we are with a debugger */
Guido van Rossumb209a111997-04-29 18:18:01 +0000378 char *filename = PyString_AsString(co->co_filename);
Guido van Rossum99bec951992-09-03 20:29:45 +0000379#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000380
381/* Code access macros */
382
383#define GETCONST(i) Getconst(f, i)
384#define GETNAME(i) Getname(f, i)
385#define GETNAMEV(i) Getnamev(f, i)
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000386#define FIRST_INSTR() (GETUSTRINGVALUE(co->co_code))
Guido van Rossum374a9221991-04-04 10:40:29 +0000387#define INSTR_OFFSET() (next_instr - FIRST_INSTR())
388#define NEXTOP() (*next_instr++)
389#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
390#define JUMPTO(x) (next_instr = FIRST_INSTR() + (x))
391#define JUMPBY(x) (next_instr += (x))
392
393/* Stack manipulation macros */
394
395#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
396#define EMPTY() (STACK_LEVEL() == 0)
397#define TOP() (stack_pointer[-1])
398#define BASIC_PUSH(v) (*stack_pointer++ = (v))
399#define BASIC_POP() (*--stack_pointer)
400
Guido van Rossum96a42c81992-01-12 02:29:51 +0000401#ifdef LLTRACE
402#define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
403#define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
Guido van Rossum374a9221991-04-04 10:40:29 +0000404#else
405#define PUSH(v) BASIC_PUSH(v)
406#define POP() BASIC_POP()
407#endif
408
Guido van Rossum681d79a1995-07-18 14:51:37 +0000409/* Local variable macros */
410
411#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumb209a111997-04-29 18:18:01 +0000412#define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \
Guido van Rossum681d79a1995-07-18 14:51:37 +0000413 GETLOCAL(i) = value; } while (0)
414
Guido van Rossuma027efa1997-05-05 20:56:21 +0000415/* Start of code */
416
Guido van Rossum8861b741996-07-30 16:49:37 +0000417#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000418 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000419 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000420 return NULL;
421 }
422#endif
423
Guido van Rossum681d79a1995-07-18 14:51:37 +0000424 if (globals == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000425 PyErr_SetString(PyExc_SystemError, "eval_code2: NULL globals");
Guido van Rossum6135a871995-01-09 17:53:26 +0000426 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000427 }
428
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000429#ifdef LLTRACE
Guido van Rossumb209a111997-04-29 18:18:01 +0000430 lltrace = PyDict_GetItemString(globals, "__lltrace__") != NULL;
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000431#endif
432
Guido van Rossumb209a111997-04-29 18:18:01 +0000433 f = PyFrame_New(
Guido van Rossuma027efa1997-05-05 20:56:21 +0000434 tstate, /*back*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000435 co, /*code*/
436 globals, /*globals*/
Guido van Rossum76836021997-01-20 04:26:20 +0000437 locals); /*locals*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000438 if (f == NULL)
439 return NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000440
Guido van Rossuma027efa1997-05-05 20:56:21 +0000441 tstate->frame = f;
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000442 fastlocals = f->f_localsplus;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000443
444 if (co->co_argcount > 0 ||
445 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
446 int i;
447 int n = argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000448 PyObject *kwdict = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000449 if (co->co_flags & CO_VARKEYWORDS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000450 kwdict = PyDict_New();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000451 if (kwdict == NULL)
452 goto fail;
Guido van Rossum8c5df061997-01-24 04:19:24 +0000453 i = co->co_argcount;
454 if (co->co_flags & CO_VARARGS)
455 i++;
456 SETLOCAL(i, kwdict);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000457 }
458 if (argcount > co->co_argcount) {
459 if (!(co->co_flags & CO_VARARGS)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000460 PyErr_SetString(PyExc_TypeError,
461 "too many arguments");
Guido van Rossum681d79a1995-07-18 14:51:37 +0000462 goto fail;
463 }
464 n = co->co_argcount;
465 }
466 for (i = 0; i < n; i++) {
467 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000468 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000469 SETLOCAL(i, x);
470 }
471 if (co->co_flags & CO_VARARGS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000472 u = PyTuple_New(argcount - n);
Guido van Rossum8c5df061997-01-24 04:19:24 +0000473 if (u == NULL)
474 goto fail;
475 SETLOCAL(co->co_argcount, u);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000476 for (i = n; i < argcount; i++) {
477 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000478 Py_INCREF(x);
479 PyTuple_SET_ITEM(u, i-n, x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000480 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000481 }
482 for (i = 0; i < kwcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000483 PyObject *keyword = kws[2*i];
484 PyObject *value = kws[2*i + 1];
Guido van Rossum681d79a1995-07-18 14:51:37 +0000485 int j;
486 /* XXX slow -- speed up using dictionary? */
487 for (j = 0; j < co->co_argcount; j++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000488 PyObject *nm = PyTuple_GET_ITEM(
489 co->co_varnames, j);
Guido van Rossumb209a111997-04-29 18:18:01 +0000490 if (PyObject_Compare(keyword, nm) == 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000491 break;
492 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000493 /* Check errors from Compare */
494 if (PyErr_Occurred())
495 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000496 if (j >= co->co_argcount) {
497 if (kwdict == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000498 PyErr_Format(PyExc_TypeError,
Guido van Rossuma027efa1997-05-05 20:56:21 +0000499 "unexpected keyword argument: %.400s",
500 PyString_AsString(keyword));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000501 goto fail;
502 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000503 PyDict_SetItem(kwdict, keyword, value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000504 }
505 else {
506 if (GETLOCAL(j) != NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000507 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000508 "keyword parameter redefined");
509 goto fail;
510 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000511 Py_INCREF(value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000512 SETLOCAL(j, value);
513 }
514 }
515 if (argcount < co->co_argcount) {
516 int m = co->co_argcount - defcount;
517 for (i = argcount; i < m; i++) {
518 if (GETLOCAL(i) == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000519 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000520 "not enough arguments");
521 goto fail;
522 }
523 }
524 if (n > m)
525 i = n - m;
526 else
527 i = 0;
528 for (; i < defcount; i++) {
529 if (GETLOCAL(m+i) == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000530 PyObject *def = defs[i];
531 Py_INCREF(def);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000532 SETLOCAL(m+i, def);
533 }
534 }
535 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000536 }
537 else {
538 if (argcount > 0 || kwcount > 0) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000539 PyErr_SetString(PyExc_TypeError,
540 "no arguments expected");
Guido van Rossum8c5df061997-01-24 04:19:24 +0000541 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000542 }
543 }
544
Guido van Rossuma027efa1997-05-05 20:56:21 +0000545 if (tstate->sys_tracefunc != NULL) {
546 /* tstate->sys_tracefunc, if defined, is a function that
547 will be called on *every* entry to a code block.
Guido van Rossum96a42c81992-01-12 02:29:51 +0000548 Its return value, if not None, is a function that
549 will be called at the start of each executed line
550 of code. (Actually, the function must return
551 itself in order to continue tracing.)
552 The trace functions are called with three arguments:
553 a pointer to the current frame, a string indicating
554 why the function is called, and an argument which
555 depends on the situation. The global trace function
556 (sys.trace) is also called whenever an exception
557 is detected. */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000558 if (call_trace(&tstate->sys_tracefunc,
559 &f->f_trace, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000560 Py_None/*XXX how to compute arguments now?*/)) {
Guido van Rossum96a42c81992-01-12 02:29:51 +0000561 /* Trace function raised an error */
Guido van Rossum8c5df061997-01-24 04:19:24 +0000562 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000563 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000564 }
565
Guido van Rossuma027efa1997-05-05 20:56:21 +0000566 if (tstate->sys_profilefunc != NULL) {
567 /* Similar for sys_profilefunc, except it needn't return
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000568 itself and isn't called for "line" events */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000569 if (call_trace(&tstate->sys_profilefunc,
570 (PyObject**)0, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000571 Py_None/*XXX*/)) {
Guido van Rossum8c5df061997-01-24 04:19:24 +0000572 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000573 }
574 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000575
Guido van Rossuma027efa1997-05-05 20:56:21 +0000576 if (++tstate->recursion_depth > MAX_RECURSION_DEPTH) {
577 --tstate->recursion_depth;
578 PyErr_SetString(PyExc_RuntimeError,
579 "Maximum recursion depth exceeded");
580 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +0000581 Py_DECREF(f);
Guido van Rossum8861b741996-07-30 16:49:37 +0000582 return NULL;
583 }
584
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000585 next_instr = GETUSTRINGVALUE(co->co_code);
Guido van Rossum374a9221991-04-04 10:40:29 +0000586 stack_pointer = f->f_valuestack;
587
Guido van Rossum374a9221991-04-04 10:40:29 +0000588 why = WHY_NOT;
589 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000590 x = Py_None; /* Not a reference, just anything non-NULL */
Guido van Rossum374a9221991-04-04 10:40:29 +0000591
592 for (;;) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000593 /* Do periodic things. Doing this every time through
594 the loop would add too much overhead, so we do it
595 only every Nth instruction. We also do it if
596 ``things_to_do'' is set, i.e. when an asynchronous
597 event needs attention (e.g. a signal handler or
598 async I/O handler); see Py_AddPendingCall() and
599 Py_MakePendingCalls() above. */
Guido van Rossuma9672091994-09-14 13:31:22 +0000600
Guido van Rossuma027efa1997-05-05 20:56:21 +0000601 if (things_to_do || --tstate->ticker < 0) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000602 tstate->ticker = tstate->interp->checkinterval;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000603 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000604 if (Py_MakePendingCalls() < 0) {
605 why = WHY_EXCEPTION;
606 goto on_error;
607 }
608 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000609#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000610 /* If we have true signals, the signal handler
611 will call Py_AddPendingCall() so we don't
612 have to call sigcheck(). On the Mac and
613 DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000614 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000615 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000616 goto on_error;
617 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000618#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000619
Guido van Rossume59214e1994-08-30 08:01:59 +0000620#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000621 if (interpreter_lock) {
622 /* Give another thread a chance */
623
Guido van Rossum25ce5661997-08-02 03:10:38 +0000624 if (PyThreadState_Swap(NULL) != tstate)
625 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000626 release_lock(interpreter_lock);
627
628 /* Other threads may run now */
629
630 acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000631 if (PyThreadState_Swap(tstate) != NULL)
632 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000633 }
634#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000635 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000636
Guido van Rossum374a9221991-04-04 10:40:29 +0000637 /* Extract opcode and argument */
Guido van Rossum99bec951992-09-03 20:29:45 +0000638
Guido van Rossum408027e1996-12-30 16:17:54 +0000639#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum99bec951992-09-03 20:29:45 +0000640 f->f_lasti = INSTR_OFFSET();
641#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000642
643 opcode = NEXTOP();
644 if (HAS_ARG(opcode))
645 oparg = NEXTARG();
Guido van Rossum950361c1997-01-24 13:49:28 +0000646#ifdef DYNAMIC_EXECUTION_PROFILE
647#ifdef DXPAIRS
648 dxpairs[lastopcode][opcode]++;
649 lastopcode = opcode;
650#endif
651 dxp[opcode]++;
652#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000653
Guido van Rossum96a42c81992-01-12 02:29:51 +0000654#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000655 /* Instruction tracing */
656
Guido van Rossum96a42c81992-01-12 02:29:51 +0000657 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000658 if (HAS_ARG(opcode)) {
659 printf("%d: %d, %d\n",
660 (int) (INSTR_OFFSET() - 3),
661 opcode, oparg);
662 }
663 else {
664 printf("%d: %d\n",
665 (int) (INSTR_OFFSET() - 1), opcode);
666 }
667 }
668#endif
669
670 /* Main switch on opcode */
671
672 switch (opcode) {
673
674 /* BEWARE!
675 It is essential that any operation that fails sets either
676 x to NULL, err to nonzero, or why to anything but WHY_NOT,
677 and that no operation that succeeds does this! */
678
679 /* case STOP_CODE: this is an error! */
680
681 case POP_TOP:
682 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000683 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000684 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000685
686 case ROT_TWO:
687 v = POP();
688 w = POP();
689 PUSH(v);
690 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000691 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000692
693 case ROT_THREE:
694 v = POP();
695 w = POP();
696 x = POP();
697 PUSH(v);
698 PUSH(x);
699 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000700 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000701
702 case DUP_TOP:
703 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000704 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000705 PUSH(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000706 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000707
708 case UNARY_POSITIVE:
709 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000710 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000711 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000712 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000713 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000714 break;
715
716 case UNARY_NEGATIVE:
717 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000718 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000719 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000720 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000721 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000722 break;
723
724 case UNARY_NOT:
725 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000726 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000727 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000728 if (err == 0) {
729 Py_INCREF(Py_True);
730 PUSH(Py_True);
731 continue;
732 }
733 else if (err > 0) {
734 Py_INCREF(Py_False);
735 PUSH(Py_False);
736 err = 0;
737 continue;
738 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000739 break;
740
741 case UNARY_CONVERT:
742 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000743 x = PyObject_Repr(v);
744 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000745 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000746 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000747 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000748
749 case UNARY_INVERT:
750 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000751 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000752 Py_DECREF(v);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000753 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000754 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000755 break;
Guido van Rossum374a9221991-04-04 10:40:29 +0000756
Guido van Rossum50564e81996-01-12 01:13:16 +0000757 case BINARY_POWER:
758 w = POP();
759 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000760 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000761 Py_DECREF(v);
762 Py_DECREF(w);
Guido van Rossum50564e81996-01-12 01:13:16 +0000763 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000764 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000765 break;
766
Guido van Rossum374a9221991-04-04 10:40:29 +0000767 case BINARY_MULTIPLY:
768 w = POP();
769 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000770 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000771 Py_DECREF(v);
772 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000773 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000774 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000775 break;
776
777 case BINARY_DIVIDE:
778 w = POP();
779 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000780 x = PyNumber_Divide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000781 Py_DECREF(v);
782 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000783 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000784 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000785 break;
786
787 case BINARY_MODULO:
788 w = POP();
789 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000790 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000791 Py_DECREF(v);
792 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000793 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000794 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000795 break;
796
797 case BINARY_ADD:
798 w = POP();
799 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000800 if (PyInt_Check(v) && PyInt_Check(w)) {
801 /* INLINE: int + int */
802 register long a, b, i;
803 a = ((PyIntObject*) v)->ob_ival;
804 b = ((PyIntObject*) w)->ob_ival;
805 i = a + b;
806 if ((i^a) < 0 && (i^b) < 0) {
807 PyErr_SetString(PyExc_OverflowError,
808 "integer addition");
809 x = NULL;
810 }
811 else
812 x = PyInt_FromLong(i);
813 }
814 else
815 x = PyNumber_Add(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000816 Py_DECREF(v);
817 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000818 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000819 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000820 break;
821
822 case BINARY_SUBTRACT:
823 w = POP();
824 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000825 if (PyInt_Check(v) && PyInt_Check(w)) {
826 /* INLINE: int - int */
827 register long a, b, i;
828 a = ((PyIntObject*) v)->ob_ival;
829 b = ((PyIntObject*) w)->ob_ival;
830 i = a - b;
831 if ((i^a) < 0 && (i^~b) < 0) {
832 PyErr_SetString(PyExc_OverflowError,
833 "integer subtraction");
834 x = NULL;
835 }
836 else
837 x = PyInt_FromLong(i);
838 }
839 else
840 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000841 Py_DECREF(v);
842 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000843 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000844 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000845 break;
846
847 case BINARY_SUBSCR:
848 w = POP();
849 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000850 if (PyList_Check(v) && PyInt_Check(w)) {
851 /* INLINE: list[int] */
852 long i = PyInt_AsLong(w);
853 if (i < 0)
854 i += ((PyListObject*) v)->ob_size;
855 if (i < 0 ||
856 i >= ((PyListObject*) v)->ob_size) {
857 PyErr_SetString(PyExc_IndexError,
858 "list index out of range");
859 x = NULL;
860 }
861 else {
862 x = ((PyListObject*) v)->ob_item[i];
863 Py_INCREF(x);
864 }
865 }
866 else
867 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000868 Py_DECREF(v);
869 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000870 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000871 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000872 break;
873
Guido van Rossum7928cd71991-10-24 14:59:31 +0000874 case BINARY_LSHIFT:
875 w = POP();
876 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000877 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000878 Py_DECREF(v);
879 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000880 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000881 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000882 break;
883
884 case BINARY_RSHIFT:
885 w = POP();
886 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000887 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000888 Py_DECREF(v);
889 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000890 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000891 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000892 break;
893
894 case BINARY_AND:
895 w = POP();
896 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000897 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000898 Py_DECREF(v);
899 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000900 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000901 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000902 break;
903
904 case BINARY_XOR:
905 w = POP();
906 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000907 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000908 Py_DECREF(v);
909 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000910 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000911 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000912 break;
913
914 case BINARY_OR:
915 w = POP();
916 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000917 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000918 Py_DECREF(v);
919 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000920 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000921 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000922 break;
923
Guido van Rossum374a9221991-04-04 10:40:29 +0000924 case SLICE+0:
925 case SLICE+1:
926 case SLICE+2:
927 case SLICE+3:
928 if ((opcode-SLICE) & 2)
929 w = POP();
930 else
931 w = NULL;
932 if ((opcode-SLICE) & 1)
933 v = POP();
934 else
935 v = NULL;
936 u = POP();
937 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000938 Py_DECREF(u);
939 Py_XDECREF(v);
940 Py_XDECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000941 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000942 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000943 break;
944
945 case STORE_SLICE+0:
946 case STORE_SLICE+1:
947 case STORE_SLICE+2:
948 case STORE_SLICE+3:
949 if ((opcode-STORE_SLICE) & 2)
950 w = POP();
951 else
952 w = NULL;
953 if ((opcode-STORE_SLICE) & 1)
954 v = POP();
955 else
956 v = NULL;
957 u = POP();
958 t = POP();
959 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +0000960 Py_DECREF(t);
961 Py_DECREF(u);
962 Py_XDECREF(v);
963 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000964 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000965 break;
966
967 case DELETE_SLICE+0:
968 case DELETE_SLICE+1:
969 case DELETE_SLICE+2:
970 case DELETE_SLICE+3:
971 if ((opcode-DELETE_SLICE) & 2)
972 w = POP();
973 else
974 w = NULL;
975 if ((opcode-DELETE_SLICE) & 1)
976 v = POP();
977 else
978 v = NULL;
979 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000980 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +0000981 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +0000982 Py_DECREF(u);
983 Py_XDECREF(v);
984 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000985 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000986 break;
987
988 case STORE_SUBSCR:
989 w = POP();
990 v = POP();
991 u = POP();
992 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +0000993 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +0000994 Py_DECREF(u);
995 Py_DECREF(v);
996 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000997 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000998 break;
999
1000 case DELETE_SUBSCR:
1001 w = POP();
1002 v = POP();
1003 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001004 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001005 Py_DECREF(v);
1006 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001007 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001008 break;
1009
1010 case PRINT_EXPR:
1011 v = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 /* Print value except if procedure result */
Guido van Rossume59214e1994-08-30 08:01:59 +00001013 /* Before printing, also assign to '_' */
Guido van Rossumb209a111997-04-29 18:18:01 +00001014 if (v != Py_None &&
Guido van Rossuma027efa1997-05-05 20:56:21 +00001015 (err = PyDict_SetItemString(
Guido van Rossum9cc8a201997-07-19 19:55:50 +00001016 f->f_builtins, "_", v)) == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001017 err = Py_FlushLine();
Guido van Rossumc12da691997-07-17 23:12:42 +00001018 if (err == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001019 x = PySys_GetObject("stdout");
1020 if (x == NULL)
1021 err = -1;
1022 }
1023 if (err == 0)
1024 err = PyFile_WriteObject(v, x, 0);
1025 if (err == 0) {
1026 PyFile_SoftSpace(x, 1);
1027 err = Py_FlushLine();
1028 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001029 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001030 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001031 break;
1032
1033 case PRINT_ITEM:
1034 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001035 w = PySys_GetObject("stdout");
1036 if (PyFile_SoftSpace(w, 1))
Guido van Rossumbe270261997-05-22 22:26:18 +00001037 err = PyFile_WriteString(" ", w);
1038 if (err == 0)
1039 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Guido van Rossumb209a111997-04-29 18:18:01 +00001040 if (err == 0 && PyString_Check(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +00001041 /* XXX move into writeobject() ? */
Guido van Rossumb209a111997-04-29 18:18:01 +00001042 char *s = PyString_AsString(v);
1043 int len = PyString_Size(v);
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001044 if (len > 0 &&
1045 isspace(Py_CHARMASK(s[len-1])) &&
Guido van Rossumc6004111993-11-05 10:22:19 +00001046 s[len-1] != ' ')
Guido van Rossumb209a111997-04-29 18:18:01 +00001047 PyFile_SoftSpace(w, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +00001048 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001049 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001050 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001051 break;
1052
1053 case PRINT_NEWLINE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001054 x = PySys_GetObject("stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001055 if (x == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00001056 PyErr_SetString(PyExc_RuntimeError,
1057 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001058 else {
Guido van Rossumbe270261997-05-22 22:26:18 +00001059 err = PyFile_WriteString("\n", x);
1060 if (err == 0)
1061 PyFile_SoftSpace(x, 0);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001062 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001063 break;
1064
1065 case BREAK_LOOP:
1066 why = WHY_BREAK;
1067 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001068
Guido van Rossumf10570b1995-07-07 22:53:21 +00001069 case RAISE_VARARGS:
1070 u = v = w = NULL;
1071 switch (oparg) {
1072 case 3:
1073 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001074 /* Fallthrough */
1075 case 2:
1076 v = POP(); /* value */
1077 /* Fallthrough */
1078 case 1:
1079 w = POP(); /* exc */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001080 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001081 break;
1082 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001083 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001084 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001085 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001086 break;
1087 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001088 break;
1089
1090 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001091 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001092 PyErr_SetString(PyExc_SystemError,
1093 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001094 break;
1095 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001096 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001097 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001098 break;
1099
1100 case RETURN_VALUE:
1101 retval = POP();
1102 why = WHY_RETURN;
1103 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001104
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001105 case EXEC_STMT:
1106 w = POP();
1107 v = POP();
1108 u = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001109 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001110 Py_DECREF(u);
1111 Py_DECREF(v);
1112 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001113 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001114
Guido van Rossum374a9221991-04-04 10:40:29 +00001115 case POP_BLOCK:
1116 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001117 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001118 while (STACK_LEVEL() > b->b_level) {
1119 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001120 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001121 }
1122 }
1123 break;
1124
1125 case END_FINALLY:
1126 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001127 if (PyInt_Check(v)) {
1128 why = (enum why_code) PyInt_AsLong(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001129 if (why == WHY_RETURN)
1130 retval = POP();
1131 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001132 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001133 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001134 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001135 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001136 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001137 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001138 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001139 else if (v != Py_None) {
1140 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001141 "'finally' pops bad exception");
1142 why = WHY_EXCEPTION;
1143 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001144 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001145 break;
1146
1147 case BUILD_CLASS:
Guido van Rossum25831651993-05-19 14:50:45 +00001148 u = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +00001150 w = POP();
1151 x = build_class(u, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001152 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001153 Py_DECREF(u);
1154 Py_DECREF(v);
1155 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001156 break;
1157
1158 case STORE_NAME:
1159 w = GETNAMEV(oparg);
1160 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001161 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001162 PyErr_SetString(PyExc_SystemError,
1163 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001164 break;
1165 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001166 err = PyDict_SetItem(x, w, v);
1167 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001168 break;
1169
1170 case DELETE_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001171 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001172 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001173 PyErr_SetString(PyExc_SystemError,
1174 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001175 break;
1176 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001177 if ((err = PyDict_DelItem(x, w)) != 0)
1178 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001179 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001180
1181#ifdef CASE_TOO_BIG
1182 default: switch (opcode) {
1183#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001184
1185 case UNPACK_TUPLE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001186 case UNPACK_LIST:
1187 v = POP();
Barry Warsawe42b18f1997-08-25 22:13:04 +00001188 if (PyTuple_Check(v)) {
1189 if (PyTuple_Size(v) != oparg) {
1190 PyErr_SetString(PyExc_ValueError,
1191 "unpack tuple of wrong size");
1192 why = WHY_EXCEPTION;
1193 }
1194 else {
1195 for (; --oparg >= 0; ) {
1196 w = PyTuple_GET_ITEM(v, oparg);
1197 Py_INCREF(w);
1198 PUSH(w);
1199 }
1200 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001201 }
Barry Warsawe42b18f1997-08-25 22:13:04 +00001202 else if (PyList_Check(v)) {
1203 if (PyList_Size(v) != oparg) {
1204 PyErr_SetString(PyExc_ValueError,
1205 "unpack list of wrong size");
1206 why = WHY_EXCEPTION;
1207 }
1208 else {
1209 for (; --oparg >= 0; ) {
1210 w = PyList_GET_ITEM(v, oparg);
1211 Py_INCREF(w);
1212 PUSH(w);
1213 }
1214 }
1215 }
1216 else if (PySequence_Check(v)) {
1217 if (unpack_sequence(v, oparg,
1218 stack_pointer + oparg))
1219 stack_pointer += oparg;
1220 else
1221 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001222 }
1223 else {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001224 PyErr_SetString(PyExc_TypeError,
1225 "unpack non-sequence");
1226 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001227 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001228 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001229 break;
1230
1231 case STORE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001232 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001233 v = POP();
1234 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001235 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1236 Py_DECREF(v);
1237 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001238 break;
1239
1240 case DELETE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001241 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001242 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001243 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1244 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001245 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001246 break;
1247
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001248 case STORE_GLOBAL:
1249 w = GETNAMEV(oparg);
1250 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001251 err = PyDict_SetItem(f->f_globals, w, v);
1252 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001253 break;
1254
1255 case DELETE_GLOBAL:
1256 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001257 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1258 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001259 break;
1260
Guido van Rossum374a9221991-04-04 10:40:29 +00001261 case LOAD_CONST:
1262 x = GETCONST(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001263 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001264 PUSH(x);
1265 break;
1266
1267 case LOAD_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001268 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001269 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001270 PyErr_SetString(PyExc_SystemError,
1271 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001272 break;
1273 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001274 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001275 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001276 PyErr_Clear();
1277 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001278 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001279 PyErr_Clear();
1280 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001281 if (x == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001282 PyErr_SetObject(
1283 PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001284 break;
1285 }
1286 }
1287 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001288 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001289 PUSH(x);
1290 break;
1291
1292 case LOAD_GLOBAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001293 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001294 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001295 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001296 PyErr_Clear();
1297 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001298 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001299 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001300 break;
1301 }
1302 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001303 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001304 PUSH(x);
1305 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001306
Guido van Rossum9bfef441993-03-29 10:43:31 +00001307 case LOAD_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001308 x = GETLOCAL(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001309 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001310 PyErr_SetObject(PyExc_NameError,
1311 PyTuple_GetItem(co->co_varnames,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001312 oparg));
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001313 break;
1314 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001315 Py_INCREF(x);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001316 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001317 if (x != NULL) continue;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001318 break;
1319
1320 case STORE_FAST:
Guido van Rossum25831651993-05-19 14:50:45 +00001321 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001322 SETLOCAL(oparg, v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001323 continue;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001324
1325 case DELETE_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001326 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001327 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001328
1329 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001330 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001331 if (x != NULL) {
1332 for (; --oparg >= 0;) {
1333 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001334 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001335 }
1336 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001337 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001338 }
1339 break;
1340
1341 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001342 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001343 if (x != NULL) {
1344 for (; --oparg >= 0;) {
1345 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001346 err = PyList_SetItem(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001347 if (err != 0)
1348 break;
1349 }
1350 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001351 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001352 }
1353 break;
1354
1355 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001356 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001357 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001358 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001359 break;
1360
1361 case LOAD_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001362 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001363 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001364 x = PyObject_GetAttr(v, w);
1365 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001366 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001367 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001368 break;
1369
1370 case COMPARE_OP:
1371 w = POP();
1372 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +00001373 if (PyInt_Check(v) && PyInt_Check(w)) {
1374 /* INLINE: cmp(int, int) */
1375 register long a, b;
1376 register int res;
1377 a = ((PyIntObject*) v)->ob_ival;
1378 b = ((PyIntObject*) w)->ob_ival;
1379 switch (oparg) {
1380 case LT: res = a < b; break;
1381 case LE: res = a <= b; break;
1382 case EQ: res = a == b; break;
1383 case NE: res = a != b; break;
1384 case GT: res = a > b; break;
1385 case GE: res = a >= b; break;
1386 case IS: res = v == w; break;
1387 case IS_NOT: res = v != w; break;
1388 default: goto slow_compare;
1389 }
1390 x = res ? Py_True : Py_False;
1391 Py_INCREF(x);
1392 }
1393 else {
1394 slow_compare:
1395 x = cmp_outcome(oparg, v, w);
1396 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001397 Py_DECREF(v);
1398 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001399 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001400 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001401 break;
1402
1403 case IMPORT_NAME:
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001404 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001405 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001406 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001407 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001408 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001409 break;
1410 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001411 if (PyCFunction_Check(x)) {
1412 u = Py_None;
1413 Py_INCREF(u);
Guido van Rossum24c13741995-02-14 09:42:43 +00001414 }
1415 else {
1416 u = find_from_args(f, INSTR_OFFSET());
1417 if (u == NULL) {
1418 x = u;
1419 break;
1420 }
1421 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001422 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001423 w,
1424 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001425 f->f_locals == NULL ?
1426 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001427 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001428 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001429 if (w == NULL) {
1430 x = NULL;
1431 break;
1432 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001433 x = PyEval_CallObject(x, w);
1434 Py_DECREF(w);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001435 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001436 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001437 break;
1438
1439 case IMPORT_FROM:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001440 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001442 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001443 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001444 PyErr_SetString(PyExc_SystemError,
1445 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001446 break;
1447 }
1448 err = import_from(x, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001449 PyFrame_LocalsToFast(f, 0);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001450 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001451 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001452
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 case JUMP_FORWARD:
1454 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001455 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001456
1457 case JUMP_IF_FALSE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001458 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001459 if (err > 0)
1460 err = 0;
1461 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001463 else
1464 break;
1465 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001466
1467 case JUMP_IF_TRUE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001468 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001469 if (err > 0) {
1470 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001472 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001473 else if (err == 0)
1474 ;
1475 else
1476 break;
1477 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001478
1479 case JUMP_ABSOLUTE:
1480 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001481 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001482
1483 case FOR_LOOP:
1484 /* for v in s: ...
1485 On entry: stack contains s, i.
1486 On exit: stack contains s, i+1, s[i];
1487 but if loop exhausted:
1488 s, i are popped, and we jump */
1489 w = POP(); /* Loop index */
1490 v = POP(); /* Sequence object */
1491 u = loop_subscript(v, w);
1492 if (u != NULL) {
1493 PUSH(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001494 x = PyInt_FromLong(PyInt_AsLong(w)+1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001495 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001496 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 PUSH(u);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001498 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001499 }
1500 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001501 Py_DECREF(v);
1502 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001503 /* A NULL can mean "s exhausted"
1504 but also an error: */
Guido van Rossumb209a111997-04-29 18:18:01 +00001505 if (PyErr_Occurred())
Guido van Rossum374a9221991-04-04 10:40:29 +00001506 why = WHY_EXCEPTION;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001507 else {
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001509 continue;
1510 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 }
1512 break;
1513
1514 case SETUP_LOOP:
1515 case SETUP_EXCEPT:
1516 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00001517 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Guido van Rossum374a9221991-04-04 10:40:29 +00001518 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001519 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001520
1521 case SET_LINENO:
Guido van Rossum96a42c81992-01-12 02:29:51 +00001522#ifdef LLTRACE
1523 if (lltrace)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001524 printf("--- %s:%d \n", filename, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001525#endif
Guido van Rossum96a42c81992-01-12 02:29:51 +00001526 f->f_lineno = oparg;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001527 if (f->f_trace == NULL)
1528 continue;
1529 /* Trace each line of code reached */
1530 f->f_lasti = INSTR_OFFSET();
1531 err = call_trace(&f->f_trace, &f->f_trace,
Guido van Rossumb209a111997-04-29 18:18:01 +00001532 f, "line", Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00001533 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001534
1535 case CALL_FUNCTION:
1536 {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001537 int na = oparg & 0xff;
1538 int nk = (oparg>>8) & 0xff;
1539 int n = na + 2*nk;
Guido van Rossumb209a111997-04-29 18:18:01 +00001540 PyObject **pfunc = stack_pointer - n - 1;
1541 PyObject *func = *pfunc;
1542 PyObject *self = NULL;
1543 PyObject *class = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001544 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
Guido van Rossumb209a111997-04-29 18:18:01 +00001545 if (PyMethod_Check(func)) {
1546 self = PyMethod_Self(func);
1547 class = PyMethod_Class(func);
1548 func = PyMethod_Function(func);
1549 Py_INCREF(func);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001550 if (self != NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001551 Py_INCREF(self);
1552 Py_DECREF(*pfunc);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001553 *pfunc = self;
1554 na++;
1555 n++;
1556 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001557 else {
1558 /* Unbound methods must be
1559 called with an instance of
1560 the class (or a derived
1561 class) as first argument */
1562 if (na > 0 &&
1563 (self = stack_pointer[-n])
1564 != NULL &&
Guido van Rossumb209a111997-04-29 18:18:01 +00001565 PyInstance_Check(self) &&
1566 PyClass_IsSubclass(
1567 (PyObject *)
1568 (((PyInstanceObject *)self)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001569 ->in_class),
1570 class))
1571 /* Handy-dandy */ ;
1572 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001573 PyErr_SetString(
1574 PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001575 "unbound method must be called with class instance 1st argument");
Guido van Rossum72b56e81995-12-10 04:57:42 +00001576 x = NULL;
1577 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001578 }
1579 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001580 }
1581 else
Guido van Rossumb209a111997-04-29 18:18:01 +00001582 Py_INCREF(func);
1583 if (PyFunction_Check(func)) {
1584 PyObject *co = PyFunction_GetCode(func);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001585 PyObject *globals =
1586 PyFunction_GetGlobals(func);
1587 PyObject *argdefs =
1588 PyFunction_GetDefaults(func);
Guido van Rossumb209a111997-04-29 18:18:01 +00001589 PyObject **d;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001590 int nd;
1591 if (argdefs != NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001592 d = &PyTuple_GET_ITEM(argdefs, 0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001593 nd = ((PyTupleObject *)argdefs) ->
1594 ob_size;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001595 }
1596 else {
1597 d = NULL;
1598 nd = 0;
1599 }
1600 x = eval_code2(
Guido van Rossumb209a111997-04-29 18:18:01 +00001601 (PyCodeObject *)co,
1602 globals, (PyObject *)NULL,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001603 stack_pointer-n, na,
1604 stack_pointer-2*nk, nk,
1605 d, nd,
1606 class);
1607 }
1608 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001609 PyObject *args = PyTuple_New(na);
1610 PyObject *kwdict = NULL;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001611 if (args == NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001612 x = NULL;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001613 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001614 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001615 if (nk > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001616 kwdict = PyDict_New();
Guido van Rossumff8b4941995-07-26 18:16:42 +00001617 if (kwdict == NULL) {
1618 x = NULL;
1619 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001620 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001621 err = 0;
1622 while (--nk >= 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001623 PyObject *value = POP();
1624 PyObject *key = POP();
1625 err = PyDict_SetItem(
Guido van Rossumff8b4941995-07-26 18:16:42 +00001626 kwdict, key, value);
Guido van Rossumb209a111997-04-29 18:18:01 +00001627 Py_DECREF(key);
1628 Py_DECREF(value);
Guido van Rossumd0eb4291997-01-27 21:30:09 +00001629 if (err)
Guido van Rossumff8b4941995-07-26 18:16:42 +00001630 break;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001631 }
1632 if (err) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001633 Py_DECREF(args);
1634 Py_DECREF(kwdict);
Guido van Rossumff8b4941995-07-26 18:16:42 +00001635 break;
1636 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001637 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001638 while (--na >= 0) {
1639 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001640 PyTuple_SET_ITEM(args, na, w);
Guido van Rossumff8b4941995-07-26 18:16:42 +00001641 }
1642 x = PyEval_CallObjectWithKeywords(
1643 func, args, kwdict);
Guido van Rossumb209a111997-04-29 18:18:01 +00001644 Py_DECREF(args);
1645 Py_XDECREF(kwdict);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001646 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001647 Py_DECREF(func);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001648 while (stack_pointer > pfunc) {
1649 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001650 Py_DECREF(w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001651 }
1652 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001653 if (x != NULL) continue;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001654 break;
1655 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001656
Guido van Rossum681d79a1995-07-18 14:51:37 +00001657 case MAKE_FUNCTION:
1658 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00001659 x = PyFunction_New(v, f->f_globals);
1660 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001661 /* XXX Maybe this should be a separate opcode? */
1662 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001663 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001664 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001665 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001666 x = NULL;
1667 break;
1668 }
1669 while (--oparg >= 0) {
1670 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001671 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001672 }
1673 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001674 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001675 }
1676 PUSH(x);
1677 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001678
1679 case BUILD_SLICE:
1680 if (oparg == 3)
1681 w = POP();
1682 else
1683 w = NULL;
1684 v = POP();
1685 u = POP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00001686 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001687 Py_DECREF(u);
1688 Py_DECREF(v);
1689 Py_XDECREF(w);
Guido van Rossum8861b741996-07-30 16:49:37 +00001690 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001691 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00001692 break;
1693
1694
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 default:
1696 fprintf(stderr,
1697 "XXX lineno: %d, opcode: %d\n",
Guido van Rossum96a42c81992-01-12 02:29:51 +00001698 f->f_lineno, opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00001699 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 why = WHY_EXCEPTION;
1701 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001702
1703#ifdef CASE_TOO_BIG
1704 }
1705#endif
1706
Guido van Rossum374a9221991-04-04 10:40:29 +00001707 } /* switch */
1708
1709 on_error:
1710
1711 /* Quickly continue if no error occurred */
1712
1713 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001714 if (err == 0 && x != NULL) {
1715#ifdef CHECKEXC
Guido van Rossumb209a111997-04-29 18:18:01 +00001716 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00001717 fprintf(stderr,
1718 "XXX undetected error\n");
1719 else
1720#endif
1721 continue; /* Normal, fast path */
1722 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00001724 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 err = 0;
1726 }
1727
Guido van Rossum801dcae1992-04-08 11:32:32 +00001728#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00001729 /* Double-check exception status */
1730
1731 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001732 if (!PyErr_Occurred()) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001733 fprintf(stderr, "XXX ghost error\n");
Guido van Rossuma027efa1997-05-05 20:56:21 +00001734 PyErr_SetString(PyExc_SystemError,
1735 "ghost error");
Guido van Rossum374a9221991-04-04 10:40:29 +00001736 why = WHY_EXCEPTION;
1737 }
1738 }
1739 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001740 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001741 fprintf(stderr,
1742 "XXX undetected error (why=%d)\n",
1743 why);
1744 why = WHY_EXCEPTION;
1745 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001746 }
1747#endif
1748
1749 /* Log traceback info if this is a real exception */
1750
1751 if (why == WHY_EXCEPTION) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001752 f->f_lasti = INSTR_OFFSET() - 1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 if (HAS_ARG(opcode))
Guido van Rossum96a42c81992-01-12 02:29:51 +00001754 f->f_lasti -= 2;
Guido van Rossumb209a111997-04-29 18:18:01 +00001755 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001756
Guido van Rossume59214e1994-08-30 08:01:59 +00001757 if (f->f_trace)
1758 call_exc_trace(&f->f_trace, &f->f_trace, f);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001759 if (tstate->sys_profilefunc)
1760 call_exc_trace(&tstate->sys_profilefunc,
1761 (PyObject**)0, f);
1762}
Guido van Rossum374a9221991-04-04 10:40:29 +00001763
1764 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
1765
1766 if (why == WHY_RERAISE)
1767 why = WHY_EXCEPTION;
1768
1769 /* Unwind stacks if a (pseudo) exception occurred */
1770
1771 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001772 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 while (STACK_LEVEL() > b->b_level) {
1774 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001775 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 }
1777 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
1778 why = WHY_NOT;
1779 JUMPTO(b->b_handler);
1780 break;
1781 }
1782 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00001783 (b->b_type == SETUP_EXCEPT &&
1784 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001786 PyObject *exc, *val, *tb;
1787 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001788 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001789 val = Py_None;
1790 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00001791 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001792 /* Make the raw exception data
1793 available to the handler,
1794 so a program can emulate the
1795 Python main loop. Don't do
1796 this for 'finally'. */
1797 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00001798 PyErr_NormalizeException(
1799 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001800 set_exc_info(tstate,
1801 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001802 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001803 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001804 PUSH(val);
1805 PUSH(exc);
1806 }
1807 else {
1808 if (why == WHY_RETURN)
1809 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00001810 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00001811 PUSH(v);
1812 }
1813 why = WHY_NOT;
1814 JUMPTO(b->b_handler);
1815 break;
1816 }
1817 } /* unwind stack */
1818
1819 /* End the loop if we still have an error (or return) */
1820
1821 if (why != WHY_NOT)
1822 break;
1823
1824 } /* main loop */
1825
1826 /* Pop remaining stack entries */
1827
1828 while (!EMPTY()) {
1829 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001830 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001831 }
1832
Guido van Rossum96a42c81992-01-12 02:29:51 +00001833 if (why != WHY_RETURN)
1834 retval = NULL;
1835
Guido van Rossume59214e1994-08-30 08:01:59 +00001836 if (f->f_trace) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001837 if (why == WHY_RETURN) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001838 if (call_trace(&f->f_trace, &f->f_trace, f,
1839 "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001840 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001841 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001842 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001843 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001844 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001845 }
1846
Guido van Rossuma027efa1997-05-05 20:56:21 +00001847 if (tstate->sys_profilefunc && why == WHY_RETURN) {
1848 if (call_trace(&tstate->sys_profilefunc, (PyObject**)0,
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001849 f, "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001850 Py_XDECREF(retval);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001851 retval = NULL;
1852 why = WHY_EXCEPTION;
1853 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001854 }
Guido van Rossuma4240131997-01-21 21:18:36 +00001855
Guido van Rossuma027efa1997-05-05 20:56:21 +00001856 reset_exc_info(tstate);
1857
1858 --tstate->recursion_depth;
Guido van Rossum8c5df061997-01-24 04:19:24 +00001859
1860 fail: /* Jump here from prelude on failure */
Guido van Rossum96a42c81992-01-12 02:29:51 +00001861
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 /* Restore previous frame and release the current one */
Guido van Rossum681d79a1995-07-18 14:51:37 +00001863
Guido van Rossuma027efa1997-05-05 20:56:21 +00001864 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +00001865 Py_DECREF(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001866
Guido van Rossum96a42c81992-01-12 02:29:51 +00001867 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00001868}
1869
Guido van Rossuma027efa1997-05-05 20:56:21 +00001870static void
1871set_exc_info(tstate, type, value, tb)
1872 PyThreadState *tstate;
1873 PyObject *type;
1874 PyObject *value;
1875 PyObject *tb;
1876{
1877 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001878 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00001879
Guido van Rossuma027efa1997-05-05 20:56:21 +00001880 frame = tstate->frame;
1881 if (frame->f_exc_type == NULL) {
1882 /* This frame didn't catch an exception before */
1883 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00001884 if (tstate->exc_type == NULL) {
1885 Py_INCREF(Py_None);
1886 tstate->exc_type = Py_None;
1887 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001888 tmp_type = frame->f_exc_type;
1889 tmp_value = frame->f_exc_value;
1890 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001891 Py_XINCREF(tstate->exc_type);
1892 Py_XINCREF(tstate->exc_value);
1893 Py_XINCREF(tstate->exc_traceback);
1894 frame->f_exc_type = tstate->exc_type;
1895 frame->f_exc_value = tstate->exc_value;
1896 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001897 Py_XDECREF(tmp_type);
1898 Py_XDECREF(tmp_value);
1899 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001900 }
1901 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001902 tmp_type = tstate->exc_type;
1903 tmp_value = tstate->exc_value;
1904 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001905 Py_XINCREF(type);
1906 Py_XINCREF(value);
1907 Py_XINCREF(tb);
1908 tstate->exc_type = type;
1909 tstate->exc_value = value;
1910 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001911 Py_XDECREF(tmp_type);
1912 Py_XDECREF(tmp_value);
1913 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001914 /* For b/w compatibility */
1915 PySys_SetObject("exc_type", type);
1916 PySys_SetObject("exc_value", value);
1917 PySys_SetObject("exc_traceback", tb);
1918}
1919
1920static void
1921reset_exc_info(tstate)
1922 PyThreadState *tstate;
1923{
1924 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001925 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001926 frame = tstate->frame;
1927 if (frame->f_exc_type != NULL) {
1928 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001929 tmp_type = tstate->exc_type;
1930 tmp_value = tstate->exc_value;
1931 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001932 Py_XINCREF(frame->f_exc_type);
1933 Py_XINCREF(frame->f_exc_value);
1934 Py_XINCREF(frame->f_exc_traceback);
1935 tstate->exc_type = frame->f_exc_type;
1936 tstate->exc_value = frame->f_exc_value;
1937 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001938 Py_XDECREF(tmp_type);
1939 Py_XDECREF(tmp_value);
1940 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001941 /* For b/w compatibility */
1942 PySys_SetObject("exc_type", frame->f_exc_type);
1943 PySys_SetObject("exc_value", frame->f_exc_value);
1944 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
1945 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001946 tmp_type = frame->f_exc_type;
1947 tmp_value = frame->f_exc_value;
1948 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001949 frame->f_exc_type = NULL;
1950 frame->f_exc_value = NULL;
1951 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001952 Py_XDECREF(tmp_type);
1953 Py_XDECREF(tmp_value);
1954 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001955}
1956
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001957/* Logic for the raise statement (too complicated for inlining).
1958 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00001959static enum why_code
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001960do_raise(type, value, tb)
Guido van Rossumb209a111997-04-29 18:18:01 +00001961 PyObject *type, *value, *tb;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001962{
1963 /* We support the following forms of raise:
1964 raise <class>, <classinstance>
1965 raise <class>, <argument tuple>
1966 raise <class>, None
1967 raise <class>, <argument>
1968 raise <classinstance>, None
1969 raise <string>, <object>
1970 raise <string>, None
1971
1972 An omitted second argument is the same as None.
1973
1974 In addition, raise <tuple>, <anything> is the same as
1975 raising the tuple's first item (and it better have one!);
1976 this rule is applied recursively.
1977
1978 Finally, an optional third argument can be supplied, which
1979 gives the traceback to be substituted (useful when
1980 re-raising an exception after examining it). */
1981
1982 /* First, check the traceback argument, replacing None with
1983 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 if (tb == Py_None) {
1985 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001986 tb = NULL;
1987 }
1988 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001989 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001990 "raise 3rd arg must be traceback or None");
1991 goto raise_error;
1992 }
1993
1994 /* Next, replace a missing value with None */
1995 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001996 value = Py_None;
1997 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001998 }
1999
2000 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002001 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2002 PyObject *tmp = type;
2003 type = PyTuple_GET_ITEM(type, 0);
2004 Py_INCREF(type);
2005 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002006 }
2007
Barry Warsaw4249f541997-08-22 21:26:19 +00002008 if (PyString_Check(type))
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002009 ;
Barry Warsaw4249f541997-08-22 21:26:19 +00002010
2011 else if (PyClass_Check(type))
2012 PyErr_NormalizeException(&type, &value, &tb);
2013
Guido van Rossumb209a111997-04-29 18:18:01 +00002014 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002015 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002016 if (value != Py_None) {
2017 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002018 "instance exception may not have a separate value");
2019 goto raise_error;
2020 }
2021 else {
2022 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002023 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002024 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002025 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2026 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002027 }
2028 }
2029 else {
2030 /* Not something you can raise. You get an exception
2031 anyway, just not what you specified :-) */
Guido van Rossumb209a111997-04-29 18:18:01 +00002032 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002033 "exceptions must be strings, classes, or instances");
2034 goto raise_error;
2035 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002036 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002037 if (tb == NULL)
2038 return WHY_EXCEPTION;
2039 else
2040 return WHY_RERAISE;
2041 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002042 Py_XDECREF(value);
2043 Py_XDECREF(type);
2044 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002045 return WHY_EXCEPTION;
2046}
2047
Barry Warsawe42b18f1997-08-25 22:13:04 +00002048static int
2049unpack_sequence(v, argcnt, sp)
2050 PyObject *v;
2051 int argcnt;
2052 PyObject **sp;
2053{
2054 int i;
2055 PyObject *w;
2056
2057 for (i = 0; i < argcnt; i++) {
2058 if (! (w = PySequence_GetItem(v, i))) {
2059 if (PyErr_ExceptionMatches(PyExc_IndexError))
2060 PyErr_SetString(PyExc_ValueError,
2061 "unpack sequence of wrong size");
2062 goto finally;
2063 }
2064 *--sp = w;
2065 }
2066 /* we better get an IndexError now */
2067 if (PySequence_GetItem(v, i) == NULL) {
2068 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2069 PyErr_Clear();
2070 return 1;
2071 }
2072 /* some other exception occurred. fall through to finally */
2073 }
2074 else
2075 PyErr_SetString(PyExc_ValueError,
2076 "unpack sequence of wrong size");
2077 /* fall through */
2078finally:
Barry Warsaw91010551997-08-25 22:30:51 +00002079 for (; i > 0; i--, sp++)
2080 Py_DECREF(*sp);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002081
2082 return 0;
2083}
2084
2085
Guido van Rossum96a42c81992-01-12 02:29:51 +00002086#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002087static int
2088prtrace(v, str)
Guido van Rossumb209a111997-04-29 18:18:01 +00002089 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002090 char *str;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002091{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002092 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002093 if (PyObject_Print(v, stdout, 0) != 0)
2094 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002095 printf("\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002096}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002097#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002098
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002099static void
2100call_exc_trace(p_trace, p_newtrace, f)
Guido van Rossumb209a111997-04-29 18:18:01 +00002101 PyObject **p_trace, **p_newtrace;
2102 PyFrameObject *f;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002103{
Guido van Rossumb209a111997-04-29 18:18:01 +00002104 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002105 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002106 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002107 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002108 value = Py_None;
2109 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002110 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002111 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002112 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002113 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002114 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002115 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002116 err = call_trace(p_trace, p_newtrace, f, "exception", arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002117 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002118 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002119 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002120 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002121 Py_XDECREF(type);
2122 Py_XDECREF(value);
2123 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002124 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002125}
2126
2127static int
2128call_trace(p_trace, p_newtrace, f, msg, arg)
Guido van Rossumb209a111997-04-29 18:18:01 +00002129 PyObject **p_trace; /* in/out; may not be NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002130 may not point to NULL variable initially */
Guido van Rossumb209a111997-04-29 18:18:01 +00002131 PyObject **p_newtrace; /* in/out; may be NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002132 may point to NULL variable;
2133 may be same variable as p_newtrace */
Guido van Rossumb209a111997-04-29 18:18:01 +00002134 PyFrameObject *f;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002135 char *msg;
Guido van Rossumb209a111997-04-29 18:18:01 +00002136 PyObject *arg;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002137{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002138 PyThreadState *tstate = f->f_tstate;
Guido van Rossumb209a111997-04-29 18:18:01 +00002139 PyObject *args, *what;
2140 PyObject *res = NULL;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002141
Guido van Rossuma027efa1997-05-05 20:56:21 +00002142 if (tstate->tracing) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002143 /* Don't do recursive traces */
2144 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002145 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002146 *p_newtrace = NULL;
2147 }
2148 return 0;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002149 }
2150
Guido van Rossumb209a111997-04-29 18:18:01 +00002151 args = PyTuple_New(3);
Guido van Rossumf10570b1995-07-07 22:53:21 +00002152 if (args == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002153 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002154 what = PyString_FromString(msg);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002155 if (what == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002156 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002157 Py_INCREF(f);
2158 PyTuple_SET_ITEM(args, 0, (PyObject *)f);
2159 PyTuple_SET_ITEM(args, 1, what);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002160 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002161 arg = Py_None;
2162 Py_INCREF(arg);
2163 PyTuple_SET_ITEM(args, 2, arg);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002164 tstate->tracing++;
Guido van Rossumb209a111997-04-29 18:18:01 +00002165 PyFrame_FastToLocals(f);
2166 res = PyEval_CallObject(*p_trace, args); /* May clear *p_trace! */
2167 PyFrame_LocalsToFast(f, 1);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002168 tstate->tracing--;
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002169 cleanup:
Guido van Rossumb209a111997-04-29 18:18:01 +00002170 Py_XDECREF(args);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002171 if (res == NULL) {
2172 /* The trace proc raised an exception */
Guido van Rossumb209a111997-04-29 18:18:01 +00002173 PyTraceBack_Here(f);
2174 Py_XDECREF(*p_trace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002175 *p_trace = NULL;
2176 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002177 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002178 *p_newtrace = NULL;
2179 }
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002180 return -1;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002181 }
2182 else {
2183 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002184 Py_XDECREF(*p_newtrace);
2185 if (res == Py_None)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002186 *p_newtrace = NULL;
2187 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002188 Py_INCREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002189 *p_newtrace = res;
2190 }
2191 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002192 Py_DECREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002193 return 0;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002194 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002195}
2196
Guido van Rossumb209a111997-04-29 18:18:01 +00002197PyObject *
2198PyEval_GetBuiltins()
Guido van Rossum6135a871995-01-09 17:53:26 +00002199{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002200 PyThreadState *tstate = PyThreadState_Get();
2201 PyFrameObject *current_frame = tstate->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002202 if (current_frame == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002203 return tstate->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00002204 else
2205 return current_frame->f_builtins;
2206}
2207
Guido van Rossumb209a111997-04-29 18:18:01 +00002208PyObject *
2209PyEval_GetLocals()
Guido van Rossum5b722181993-03-30 17:46:03 +00002210{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002211 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum5b722181993-03-30 17:46:03 +00002212 if (current_frame == NULL)
2213 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002214 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00002215 return current_frame->f_locals;
2216}
2217
Guido van Rossumb209a111997-04-29 18:18:01 +00002218PyObject *
2219PyEval_GetGlobals()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002220{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002221 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002222 if (current_frame == NULL)
2223 return NULL;
2224 else
2225 return current_frame->f_globals;
2226}
2227
Guido van Rossumb209a111997-04-29 18:18:01 +00002228PyObject *
2229PyEval_GetFrame()
Guido van Rossume59214e1994-08-30 08:01:59 +00002230{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002231 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossumb209a111997-04-29 18:18:01 +00002232 return (PyObject *)current_frame;
Guido van Rossume59214e1994-08-30 08:01:59 +00002233}
2234
Guido van Rossum6135a871995-01-09 17:53:26 +00002235int
Guido van Rossumb209a111997-04-29 18:18:01 +00002236PyEval_GetRestricted()
Guido van Rossum6135a871995-01-09 17:53:26 +00002237{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002238 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002239 return current_frame == NULL ? 0 : current_frame->f_restricted;
2240}
2241
Guido van Rossumbe270261997-05-22 22:26:18 +00002242int
Guido van Rossumb209a111997-04-29 18:18:01 +00002243Py_FlushLine()
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002244{
Guido van Rossumb209a111997-04-29 18:18:01 +00002245 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00002246 if (f == NULL)
2247 return 0;
2248 if (!PyFile_SoftSpace(f, 0))
2249 return 0;
2250 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002251}
2252
Guido van Rossum3f5da241990-12-20 15:06:42 +00002253
Guido van Rossum681d79a1995-07-18 14:51:37 +00002254/* External interface to call any callable object.
2255 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002256
Guido van Rossumb209a111997-04-29 18:18:01 +00002257PyObject *
2258PyEval_CallObject(func, arg)
2259 PyObject *func;
2260 PyObject *arg;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002261{
Guido van Rossumb209a111997-04-29 18:18:01 +00002262 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002263}
Guido van Rossume59214e1994-08-30 08:01:59 +00002264
Guido van Rossumb209a111997-04-29 18:18:01 +00002265PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002266PyEval_CallObjectWithKeywords(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002267 PyObject *func;
2268 PyObject *arg;
2269 PyObject *kw;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002270{
2271 ternaryfunc call;
Guido van Rossumb209a111997-04-29 18:18:01 +00002272 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002273
2274 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002275 arg = PyTuple_New(0);
2276 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002277 PyErr_SetString(PyExc_TypeError,
2278 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002279 return NULL;
2280 }
2281 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002282 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002283
Guido van Rossumb209a111997-04-29 18:18:01 +00002284 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002285 PyErr_SetString(PyExc_TypeError,
2286 "keyword list must be a dictionary");
Guido van Rossume3e61c11995-08-04 04:14:47 +00002287 return NULL;
2288 }
2289
Guido van Rossum150b2df1996-12-05 23:17:11 +00002290 if ((call = func->ob_type->tp_call) != NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002291 result = (*call)(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002292 else if (PyMethod_Check(func) || PyFunction_Check(func))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002293 result = call_function(func, arg, kw);
2294 else
2295 result = call_builtin(func, arg, kw);
2296
Guido van Rossumb209a111997-04-29 18:18:01 +00002297 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002298
Guido van Rossumb209a111997-04-29 18:18:01 +00002299 if (result == NULL && !PyErr_Occurred())
2300 PyErr_SetString(PyExc_SystemError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002301 "NULL result without error in call_object");
Guido van Rossume59214e1994-08-30 08:01:59 +00002302
2303 return result;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002304}
2305
Guido van Rossumb209a111997-04-29 18:18:01 +00002306static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002307call_builtin(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002308 PyObject *func;
2309 PyObject *arg;
2310 PyObject *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002311{
Guido van Rossumb209a111997-04-29 18:18:01 +00002312 if (PyCFunction_Check(func)) {
2313 PyCFunction meth = PyCFunction_GetFunction(func);
2314 PyObject *self = PyCFunction_GetSelf(func);
2315 int flags = PyCFunction_GetFlags(func);
Guido van Rossumff8b4941995-07-26 18:16:42 +00002316 if (!(flags & METH_VARARGS)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002317 int size = PyTuple_Size(arg);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002318 if (size == 1)
Guido van Rossumb209a111997-04-29 18:18:01 +00002319 arg = PyTuple_GET_ITEM(arg, 0);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002320 else if (size == 0)
2321 arg = NULL;
2322 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002323 if (flags & METH_KEYWORDS)
2324 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002325 if (kw != NULL && PyDict_Size(kw) != 0) {
2326 PyErr_SetString(PyExc_TypeError,
Guido van Rossumff8b4941995-07-26 18:16:42 +00002327 "this function takes no keyword arguments");
2328 return NULL;
2329 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002330 return (*meth)(self, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002331 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002332 if (PyClass_Check(func)) {
2333 return PyInstance_New(func, arg, kw);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002334 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002335 if (PyInstance_Check(func)) {
2336 PyObject *res, *call = PyObject_GetAttrString(func,"__call__");
Guido van Rossume59214e1994-08-30 08:01:59 +00002337 if (call == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002338 PyErr_Clear();
2339 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002340 "no __call__ method defined");
Guido van Rossume59214e1994-08-30 08:01:59 +00002341 return NULL;
2342 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002343 res = PyEval_CallObjectWithKeywords(call, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002344 Py_DECREF(call);
Guido van Rossume59214e1994-08-30 08:01:59 +00002345 return res;
2346 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002347 PyErr_SetString(PyExc_TypeError, "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002348 return NULL;
2349}
2350
Guido van Rossumb209a111997-04-29 18:18:01 +00002351static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002352call_function(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002353 PyObject *func;
2354 PyObject *arg;
2355 PyObject *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002356{
Guido van Rossumb209a111997-04-29 18:18:01 +00002357 PyObject *class = NULL; /* == owner */
2358 PyObject *argdefs;
2359 PyObject **d, **k;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002360 int nk, nd;
Guido van Rossumb209a111997-04-29 18:18:01 +00002361 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002362
Guido van Rossumb209a111997-04-29 18:18:01 +00002363 if (kw != NULL && !PyDict_Check(kw)) {
2364 PyErr_BadInternalCall();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002365 return NULL;
2366 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002367
Guido van Rossumb209a111997-04-29 18:18:01 +00002368 if (PyMethod_Check(func)) {
2369 PyObject *self = PyMethod_Self(func);
2370 class = PyMethod_Class(func);
2371 func = PyMethod_Function(func);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002372 if (self == NULL) {
2373 /* Unbound methods must be called with an instance of
2374 the class (or a derived class) as first argument */
Guido van Rossumb209a111997-04-29 18:18:01 +00002375 if (PyTuple_Size(arg) >= 1) {
2376 self = PyTuple_GET_ITEM(arg, 0);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002377 if (self != NULL &&
Guido van Rossumb209a111997-04-29 18:18:01 +00002378 PyInstance_Check(self) &&
2379 PyClass_IsSubclass((PyObject *)
2380 (((PyInstanceObject *)self)->in_class),
Guido van Rossumb3f72581993-05-21 19:56:10 +00002381 class))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002382 /* Handy-dandy */ ;
Guido van Rossumb3f72581993-05-21 19:56:10 +00002383 else
2384 self = NULL;
2385 }
2386 if (self == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002387 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002388 "unbound method must be called with class instance 1st argument");
Guido van Rossumb3f72581993-05-21 19:56:10 +00002389 return NULL;
2390 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002391 Py_INCREF(arg);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002392 }
2393 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002394 int argcount = PyTuple_Size(arg);
2395 PyObject *newarg = PyTuple_New(argcount + 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002396 int i;
Guido van Rossum81daa321993-05-20 14:24:46 +00002397 if (newarg == NULL)
2398 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002399 Py_INCREF(self);
2400 PyTuple_SET_ITEM(newarg, 0, self);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002401 for (i = 0; i < argcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002402 PyObject *v = PyTuple_GET_ITEM(arg, i);
2403 Py_XINCREF(v);
2404 PyTuple_SET_ITEM(newarg, i+1, v);
Guido van Rossum81daa321993-05-20 14:24:46 +00002405 }
2406 arg = newarg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002407 }
2408 }
2409 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002410 if (!PyFunction_Check(func)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002411 PyErr_SetString(PyExc_TypeError,
2412 "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002413 return NULL;
2414 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002415 Py_INCREF(arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002416 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002417
2418 argdefs = PyFunction_GetDefaults(func);
Guido van Rossumb209a111997-04-29 18:18:01 +00002419 if (argdefs != NULL && PyTuple_Check(argdefs)) {
2420 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
2421 nd = PyTuple_Size(argdefs);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002422 }
2423 else {
2424 d = NULL;
2425 nd = 0;
2426 }
2427
2428 if (kw != NULL) {
2429 int pos, i;
Guido van Rossumb209a111997-04-29 18:18:01 +00002430 nk = PyDict_Size(kw);
2431 k = PyMem_NEW(PyObject *, 2*nk);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002432 if (k == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002433 PyErr_NoMemory();
2434 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002435 return NULL;
Guido van Rossume59214e1994-08-30 08:01:59 +00002436 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002437 pos = i = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002438 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002439 i += 2;
2440 nk = i/2;
2441 /* XXX This is broken if the caller deletes dict items! */
2442 }
2443 else {
2444 k = NULL;
2445 nk = 0;
Guido van Rossume59214e1994-08-30 08:01:59 +00002446 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002447
Guido van Rossum681d79a1995-07-18 14:51:37 +00002448 result = eval_code2(
Guido van Rossumb209a111997-04-29 18:18:01 +00002449 (PyCodeObject *)PyFunction_GetCode(func),
2450 PyFunction_GetGlobals(func), (PyObject *)NULL,
2451 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
Guido van Rossum681d79a1995-07-18 14:51:37 +00002452 k, nk,
2453 d, nd,
2454 class);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002455
Guido van Rossumb209a111997-04-29 18:18:01 +00002456 Py_DECREF(arg);
2457 PyMem_XDEL(k);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002458
Guido van Rossum681d79a1995-07-18 14:51:37 +00002459 return result;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002460}
2461
Guido van Rossum3b9c6671996-07-30 18:40:29 +00002462#define SLICE_ERROR_MSG \
2463 "standard sequence type does not support step size other than one"
2464
Guido van Rossumb209a111997-04-29 18:18:01 +00002465static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002466loop_subscript(v, w)
Guido van Rossumb209a111997-04-29 18:18:01 +00002467 PyObject *v, *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002468{
Guido van Rossumb209a111997-04-29 18:18:01 +00002469 PySequenceMethods *sq = v->ob_type->tp_as_sequence;
Guido van Rossume59214e1994-08-30 08:01:59 +00002470 int i;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002471 if (sq == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002472 PyErr_SetString(PyExc_TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002473 return NULL;
2474 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002475 i = PyInt_AsLong(w);
Guido van Rossume59214e1994-08-30 08:01:59 +00002476 v = (*sq->sq_item)(v, i);
2477 if (v)
2478 return v;
Barry Warsaw4249f541997-08-22 21:26:19 +00002479 if (PyErr_ExceptionMatches(PyExc_IndexError))
Guido van Rossumb209a111997-04-29 18:18:01 +00002480 PyErr_Clear();
Guido van Rossume59214e1994-08-30 08:01:59 +00002481 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002482}
2483
2484static int
Guido van Rossuma027efa1997-05-05 20:56:21 +00002485slice_index(v, pi)
Guido van Rossumb209a111997-04-29 18:18:01 +00002486 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002487 int *pi;
2488{
2489 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002490 long x;
Guido van Rossumb209a111997-04-29 18:18:01 +00002491 if (!PyInt_Check(v)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002492 PyErr_SetString(PyExc_TypeError,
2493 "slice index must be int");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002494 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002495 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002496 x = PyInt_AsLong(v);
2497 /* Truncate -- very long indices are truncated anyway */
2498 if (x > INT_MAX)
2499 x = INT_MAX;
2500 else if (x < -INT_MAX)
2501 x = 0;
2502 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002503 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002504 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002505}
2506
Guido van Rossumb209a111997-04-29 18:18:01 +00002507static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002508apply_slice(u, v, w) /* return u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00002509 PyObject *u, *v, *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002510{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002511 int ilow = 0, ihigh = INT_MAX;
2512 if (slice_index(v, &ilow) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002513 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002514 if (slice_index(w, &ihigh) != 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +00002515 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002516 return PySequence_GetSlice(u, ilow, ihigh);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002517}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002518
2519static int
Guido van Rossum3f5da241990-12-20 15:06:42 +00002520assign_slice(u, v, w, x) /* u[v:w] = x */
Guido van Rossumb209a111997-04-29 18:18:01 +00002521 PyObject *u, *v, *w, *x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002522{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002523 int ilow = 0, ihigh = INT_MAX;
2524 if (slice_index(v, &ilow) != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002525 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002526 if (slice_index(w, &ihigh) != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002527 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002528 if (x == NULL)
2529 return PySequence_DelSlice(u, ilow, ihigh);
2530 else
2531 return PySequence_SetSlice(u, ilow, ihigh, x);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002532}
2533
2534static int
Guido van Rossum3f5da241990-12-20 15:06:42 +00002535cmp_member(v, w)
Guido van Rossumb209a111997-04-29 18:18:01 +00002536 PyObject *v, *w;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002537{
Guido van Rossume59214e1994-08-30 08:01:59 +00002538 int i, cmp;
Guido van Rossumb209a111997-04-29 18:18:01 +00002539 PyObject *x;
2540 PySequenceMethods *sq;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002541 /* Special case for char in string */
Guido van Rossumb209a111997-04-29 18:18:01 +00002542 if (PyString_Check(w)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002543 register char *s, *end;
2544 register char c;
Guido van Rossumb209a111997-04-29 18:18:01 +00002545 if (!PyString_Check(v) || PyString_Size(v) != 1) {
2546 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3f5da241990-12-20 15:06:42 +00002547 "string member test needs char left operand");
2548 return -1;
2549 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002550 c = PyString_AsString(v)[0];
2551 s = PyString_AsString(w);
2552 end = s + PyString_Size(w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002553 while (s < end) {
2554 if (c == *s++)
2555 return 1;
2556 }
2557 return 0;
2558 }
2559 sq = w->ob_type->tp_as_sequence;
2560 if (sq == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002561 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3f5da241990-12-20 15:06:42 +00002562 "'in' or 'not in' needs sequence right argument");
2563 return -1;
2564 }
Guido van Rossume59214e1994-08-30 08:01:59 +00002565 for (i = 0; ; i++) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002566 x = (*sq->sq_item)(w, i);
Guido van Rossume59214e1994-08-30 08:01:59 +00002567 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002568 if (PyErr_Occurred() == PyExc_IndexError) {
2569 PyErr_Clear();
Guido van Rossume59214e1994-08-30 08:01:59 +00002570 break;
2571 }
2572 return -1;
2573 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002574 cmp = PyObject_Compare(v, x);
2575 Py_XDECREF(x);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002576 if (cmp == 0)
2577 return 1;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002578 if (PyErr_Occurred())
2579 return -1;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002580 }
2581 return 0;
2582}
2583
Guido van Rossumb209a111997-04-29 18:18:01 +00002584static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002585cmp_outcome(op, v, w)
Guido van Rossum5b7313a1992-04-06 13:24:57 +00002586 int op;
Guido van Rossumb209a111997-04-29 18:18:01 +00002587 register PyObject *v;
2588 register PyObject *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002589{
2590 register int cmp;
2591 register int res = 0;
2592 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002593 case IS:
2594 case IS_NOT:
2595 res = (v == w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002596 if (op == (int) IS_NOT)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002597 res = !res;
2598 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002599 case IN:
2600 case NOT_IN:
Guido van Rossum3f5da241990-12-20 15:06:42 +00002601 res = cmp_member(v, w);
2602 if (res < 0)
2603 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002604 if (op == (int) NOT_IN)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002605 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002606 break;
2607 case EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00002608 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002609 break;
2610 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00002611 cmp = PyObject_Compare(v, w);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002612 if (cmp && PyErr_Occurred())
2613 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002614 switch (op) {
2615 case LT: res = cmp < 0; break;
2616 case LE: res = cmp <= 0; break;
2617 case EQ: res = cmp == 0; break;
2618 case NE: res = cmp != 0; break;
2619 case GT: res = cmp > 0; break;
2620 case GE: res = cmp >= 0; break;
2621 /* XXX no default? (res is initialized to 0 though) */
2622 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002623 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002624 v = res ? Py_True : Py_False;
2625 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002626 return v;
2627}
2628
Guido van Rossum3f5da241990-12-20 15:06:42 +00002629static int
2630import_from(locals, v, name)
Guido van Rossumb209a111997-04-29 18:18:01 +00002631 PyObject *locals;
2632 PyObject *v;
2633 PyObject *name;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002634{
Guido van Rossumb209a111997-04-29 18:18:01 +00002635 PyObject *w, *x;
2636 if (!PyModule_Check(v)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002637 PyErr_SetString(PyExc_TypeError,
2638 "import-from requires module object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002639 return -1;
2640 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002641 w = PyModule_GetDict(v);
2642 if (PyString_AsString(name)[0] == '*') {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002643 int pos, err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002644 PyObject *name, *value;
Guido van Rossum25831651993-05-19 14:50:45 +00002645 pos = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002646 while (PyDict_Next(w, &pos, &name, &value)) {
2647 if (!PyString_Check(name) ||
2648 PyString_AsString(name)[0] == '_')
Guido van Rossume9736fc1990-11-18 17:33:06 +00002649 continue;
Guido van Rossumb209a111997-04-29 18:18:01 +00002650 Py_INCREF(value);
2651 err = PyDict_SetItem(locals, name, value);
2652 Py_DECREF(value);
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002653 if (err != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002654 return -1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002655 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002656 return 0;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002657 }
2658 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002659 x = PyDict_GetItem(w, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002660 if (x == NULL) {
Guido van Rossum98256aa1991-12-24 13:25:19 +00002661 char buf[250];
Guido van Rossum16dfd291992-02-05 11:17:30 +00002662 sprintf(buf, "cannot import name %.230s",
Guido van Rossumb209a111997-04-29 18:18:01 +00002663 PyString_AsString(name));
2664 PyErr_SetString(PyExc_ImportError, buf);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002665 return -1;
2666 }
2667 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002668 return PyDict_SetItem(locals, name, x);
Guido van Rossume9736fc1990-11-18 17:33:06 +00002669 }
2670}
2671
Guido van Rossumb209a111997-04-29 18:18:01 +00002672static PyObject *
Guido van Rossum25831651993-05-19 14:50:45 +00002673build_class(methods, bases, name)
Guido van Rossumb209a111997-04-29 18:18:01 +00002674 PyObject *methods; /* dictionary */
2675 PyObject *bases; /* tuple containing classes */
2676 PyObject *name; /* string */
Guido van Rossume9736fc1990-11-18 17:33:06 +00002677{
Guido van Rossumcd649651997-08-22 16:56:16 +00002678 int i, n;
Guido van Rossumb209a111997-04-29 18:18:01 +00002679 if (!PyTuple_Check(bases)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002680 PyErr_SetString(PyExc_SystemError,
2681 "build_class with non-tuple bases");
Guido van Rossum25831651993-05-19 14:50:45 +00002682 return NULL;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002683 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002684 if (!PyDict_Check(methods)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002685 PyErr_SetString(PyExc_SystemError,
2686 "build_class with non-dictionary");
Guido van Rossume9736fc1990-11-18 17:33:06 +00002687 return NULL;
2688 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002689 if (!PyString_Check(name)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002690 PyErr_SetString(PyExc_SystemError,
2691 "build_class witn non-string name");
Guido van Rossum25831651993-05-19 14:50:45 +00002692 return NULL;
2693 }
Guido van Rossumcd649651997-08-22 16:56:16 +00002694 n = PyTuple_Size(bases);
2695 for (i = 0; i < n; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002696 PyObject *base = PyTuple_GET_ITEM(bases, i);
2697 if (!PyClass_Check(base)) {
Guido van Rossum150b2df1996-12-05 23:17:11 +00002698 /* Call the base's *type*, if it is callable.
2699 This code is a hook for Donald Beaudry's
2700 and Jim Fulton's type extensions. In
2701 unexended Python it will never be triggered
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002702 since its types are not callable.
2703 Ditto: call the bases's *class*, if it has
2704 one. This makes the same thing possible
2705 without writing C code. A true meta-object
2706 protocol! */
2707 PyObject *basetype = (PyObject *)base->ob_type;
2708 PyObject *callable = NULL;
2709 if (PyCallable_Check(basetype))
2710 callable = basetype;
2711 else
2712 callable = PyObject_GetAttrString(
2713 base, "__class__");
2714 if (callable) {
2715 PyObject *args;
2716 PyObject *newclass = NULL;
2717 args = Py_BuildValue(
2718 "(OOO)", name, bases, methods);
2719 if (args != NULL) {
2720 newclass = PyEval_CallObject(
2721 callable, args);
2722 Py_DECREF(args);
2723 }
2724 if (callable != basetype)
2725 Py_DECREF(callable);
2726 return newclass;
Guido van Rossum150b2df1996-12-05 23:17:11 +00002727 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002728 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +00002729 "base is not a class object");
2730 return NULL;
2731 }
2732 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002733 return PyClass_New(bases, methods, name);
Guido van Rossum25831651993-05-19 14:50:45 +00002734}
2735
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002736static int
Guido van Rossuma027efa1997-05-05 20:56:21 +00002737exec_statement(f, prog, globals, locals)
2738 PyFrameObject *f;
Guido van Rossumb209a111997-04-29 18:18:01 +00002739 PyObject *prog;
2740 PyObject *globals;
2741 PyObject *locals;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002742{
2743 char *s;
2744 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00002745 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002746 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002747
Guido van Rossumb209a111997-04-29 18:18:01 +00002748 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
2749 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002750 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00002751 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002752 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00002753 locals = PyTuple_GetItem(prog, 2);
2754 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002755 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002756 if (globals == Py_None) {
2757 globals = PyEval_GetGlobals();
2758 if (locals == Py_None) {
2759 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002760 plain = 1;
2761 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002762 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002763 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002764 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00002765 if (!PyString_Check(prog) &&
2766 !PyCode_Check(prog) &&
2767 !PyFile_Check(prog)) {
2768 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002769 "exec 1st arg must be string, code or file object");
2770 return -1;
2771 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002772 if (!PyDict_Check(globals) || !PyDict_Check(locals)) {
2773 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002774 "exec 2nd/3rd args must be dict or None");
2775 return -1;
2776 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002777 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002778 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00002779 if (PyCode_Check(prog)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002780 if (PyEval_EvalCode((PyCodeObject *) prog,
2781 globals, locals) == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002782 return -1;
2783 return 0;
2784 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002785 if (PyFile_Check(prog)) {
2786 FILE *fp = PyFile_AsFile(prog);
2787 char *name = PyString_AsString(PyFile_Name(prog));
Guido van Rossumb05a5c71997-05-07 17:46:13 +00002788 if (PyRun_File(fp, name, Py_file_input,
2789 globals, locals) == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002790 return -1;
2791 return 0;
2792 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002793 s = PyString_AsString(prog);
Guido van Rossum5f15b961997-05-13 17:50:01 +00002794 if ((int)strlen(s) != PyString_Size(prog)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002795 PyErr_SetString(PyExc_ValueError,
2796 "embedded '\\0' in exec string");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002797 return -1;
2798 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00002799 v = PyRun_String(s, Py_file_input, globals, locals);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002800 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002801 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00002802 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002803 if (plain)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002804 PyFrame_LocalsToFast(f, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002805 return 0;
2806}
Guido van Rossum24c13741995-02-14 09:42:43 +00002807
Guido van Rossum1aa14831997-01-21 05:34:20 +00002808/* Hack for ni.py */
Guido van Rossumb209a111997-04-29 18:18:01 +00002809static PyObject *
Guido van Rossum24c13741995-02-14 09:42:43 +00002810find_from_args(f, nexti)
Guido van Rossumb209a111997-04-29 18:18:01 +00002811 PyFrameObject *f;
Guido van Rossum24c13741995-02-14 09:42:43 +00002812 int nexti;
2813{
2814 int opcode;
2815 int oparg;
Guido van Rossumb209a111997-04-29 18:18:01 +00002816 PyObject *list, *name;
Guido van Rossum24c13741995-02-14 09:42:43 +00002817 unsigned char *next_instr;
2818
2819 next_instr = GETUSTRINGVALUE(f->f_code->co_code) + nexti;
2820 opcode = (*next_instr++);
2821 if (opcode != IMPORT_FROM) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002822 Py_INCREF(Py_None);
2823 return Py_None;
Guido van Rossum24c13741995-02-14 09:42:43 +00002824 }
2825
Guido van Rossumb209a111997-04-29 18:18:01 +00002826 list = PyList_New(0);
Guido van Rossum24c13741995-02-14 09:42:43 +00002827 if (list == NULL)
2828 return NULL;
2829
2830 do {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002831 oparg = (next_instr[1]<<8) + next_instr[0];
2832 next_instr += 2;
Guido van Rossum24c13741995-02-14 09:42:43 +00002833 name = Getnamev(f, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002834 if (PyList_Append(list, name) < 0) {
2835 Py_DECREF(list);
Guido van Rossum24c13741995-02-14 09:42:43 +00002836 break;
2837 }
2838 opcode = (*next_instr++);
2839 } while (opcode == IMPORT_FROM);
2840
2841 return list;
2842}
Guido van Rossum950361c1997-01-24 13:49:28 +00002843
2844
2845#ifdef DYNAMIC_EXECUTION_PROFILE
2846
2847PyObject *
2848getarray(a)
2849 long a[256];
2850{
2851 int i;
2852 PyObject *l = PyList_New(256);
2853 if (l == NULL) return NULL;
2854 for (i = 0; i < 256; i++) {
2855 PyObject *x = PyInt_FromLong(a[i]);
2856 if (x == NULL) {
2857 Py_DECREF(l);
2858 return NULL;
2859 }
2860 PyList_SetItem(l, i, x);
2861 }
2862 for (i = 0; i < 256; i++)
2863 a[i] = 0;
2864 return l;
2865}
2866
2867PyObject *
2868_Py_GetDXProfile(self, args)
2869 PyObject *self, *args;
2870{
2871#ifndef DXPAIRS
2872 return getarray(dxp);
2873#else
2874 int i;
2875 PyObject *l = PyList_New(257);
2876 if (l == NULL) return NULL;
2877 for (i = 0; i < 257; i++) {
2878 PyObject *x = getarray(dxpairs[i]);
2879 if (x == NULL) {
2880 Py_DECREF(l);
2881 return NULL;
2882 }
2883 PyList_SetItem(l, i, x);
2884 }
2885 return l;
2886#endif
2887}
2888
2889#endif