blob: 621839790fb6364032a70486ff9bc56a85a292c2 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033
Guido van Rossum681d79a1995-07-18 14:51:37 +000034/* XXX TO DO:
35 XXX how to pass arguments to call_trace?
Guido van Rossum681d79a1995-07-18 14:51:37 +000036 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +000037 XXX document it!
38 */
39
Guido van Rossumb209a111997-04-29 18:18:01 +000040#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000041
Guido van Rossum10dc2e81990-11-18 17:27:39 +000042#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000043#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000044#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000045#include "opcode.h"
46
Guido van Rossumc6004111993-11-05 10:22:19 +000047#include <ctype.h>
48
Guido van Rossuma027efa1997-05-05 20:56:21 +000049#ifdef HAVE_LIMITS_H
50#include <limits.h>
51#else
52#define INT_MAX 2147483647
53#endif
54
Guido van Rossum04691fc1992-08-12 15:35:34 +000055/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000056/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000057
Guido van Rossum408027e1996-12-30 16:17:54 +000058#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000059/* For debugging the interpreter: */
60#define LLTRACE 1 /* Low-level trace feature */
61#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000062#endif
63
Guido van Rossum5b722181993-03-30 17:46:03 +000064
Guido van Rossum374a9221991-04-04 10:40:29 +000065/* Forward declarations */
Guido van Rossum374a9221991-04-04 10:40:29 +000066
Guido van Rossumb209a111997-04-29 18:18:01 +000067static PyObject *eval_code2 Py_PROTO((PyCodeObject *,
68 PyObject *, PyObject *,
69 PyObject **, int,
70 PyObject **, int,
71 PyObject **, int,
72 PyObject *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000073#ifdef LLTRACE
Guido van Rossumb209a111997-04-29 18:18:01 +000074static int prtrace Py_PROTO((PyObject *, char *));
Guido van Rossum0a066c01992-03-27 17:29:15 +000075#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +000076static void call_exc_trace Py_PROTO((PyObject **, PyObject**,
77 PyFrameObject *));
78static int call_trace Py_PROTO((PyObject **, PyObject **,
79 PyFrameObject *, char *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000080static PyObject *call_builtin Py_PROTO((PyObject *, PyObject *, PyObject *));
81static PyObject *call_function Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000082static PyObject *loop_subscript Py_PROTO((PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000083static int slice_index Py_PROTO((PyObject *, int *));
Guido van Rossumb209a111997-04-29 18:18:01 +000084static PyObject *apply_slice Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000085static int assign_slice Py_PROTO((PyObject *, PyObject *,
86 PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000087static PyObject *cmp_outcome Py_PROTO((int, PyObject *, PyObject *));
88static int import_from Py_PROTO((PyObject *, PyObject *, PyObject *));
89static PyObject *build_class Py_PROTO((PyObject *, PyObject *, PyObject *));
Guido van Rossuma027efa1997-05-05 20:56:21 +000090static int exec_statement Py_PROTO((PyFrameObject *,
91 PyObject *, PyObject *, PyObject *));
Guido van Rossumb209a111997-04-29 18:18:01 +000092static PyObject *find_from_args Py_PROTO((PyFrameObject *, int));
Guido van Rossuma027efa1997-05-05 20:56:21 +000093static void set_exc_info Py_PROTO((PyThreadState *,
94 PyObject *, PyObject *, PyObject *));
95static void reset_exc_info Py_PROTO((PyThreadState *));
Guido van Rossum374a9221991-04-04 10:40:29 +000096
97
Guido van Rossum950361c1997-01-24 13:49:28 +000098/* Dynamic execution profile */
99#ifdef DYNAMIC_EXECUTION_PROFILE
100#ifdef DXPAIRS
101static long dxpairs[257][256];
102#define dxp dxpairs[256]
103#else
104static long dxp[256];
105#endif
106#endif
107
108
Guido van Rossume59214e1994-08-30 08:01:59 +0000109#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000110
Guido van Rossum2571cc81999-04-07 16:07:23 +0000111#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000112#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000113#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000114#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000115
Guido van Rossuma027efa1997-05-05 20:56:21 +0000116extern int _PyThread_Started; /* Flag for Py_Exit */
117
Guido van Rossum65d5b571998-12-21 19:32:43 +0000118static PyThread_type_lock interpreter_lock = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000119static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000120
121void
Guido van Rossumb209a111997-04-29 18:18:01 +0000122PyEval_InitThreads()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000123{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000124 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000125 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000126 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000127 interpreter_lock = PyThread_allocate_lock();
128 PyThread_acquire_lock(interpreter_lock, 1);
129 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000130}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000131
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000132void
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133PyEval_AcquireLock()
134{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000135 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136}
137
138void
139PyEval_ReleaseLock()
140{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000141 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000142}
143
144void
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000145PyEval_AcquireThread(tstate)
146 PyThreadState *tstate;
147{
148 if (tstate == NULL)
149 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000150 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000151 if (PyThreadState_Swap(tstate) != NULL)
152 Py_FatalError(
153 "PyEval_AcquireThread: non-NULL old thread state");
154}
155
156void
157PyEval_ReleaseThread(tstate)
158 PyThreadState *tstate;
159{
160 if (tstate == NULL)
161 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
162 if (PyThreadState_Swap(NULL) != tstate)
163 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000164 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000165}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000166#endif
167
Guido van Rossumff4949e1992-08-05 19:58:53 +0000168/* Functions save_thread and restore_thread are always defined so
169 dynamically loaded modules needn't be compiled separately for use
170 with and without threads: */
171
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000172PyThreadState *
Guido van Rossumb209a111997-04-29 18:18:01 +0000173PyEval_SaveThread()
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000174{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000175 PyThreadState *tstate = PyThreadState_Swap(NULL);
176 if (tstate == NULL)
177 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000178#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000179 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000180 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000182 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000183}
184
185void
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000186PyEval_RestoreThread(tstate)
187 PyThreadState *tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000188{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000189 if (tstate == NULL)
190 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000191#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000192 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000193 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000194 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000195 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000196 }
197#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000198 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199}
200
201
Guido van Rossuma9672091994-09-14 13:31:22 +0000202/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
203 signal handlers or Mac I/O completion routines) can schedule calls
204 to a function to be called synchronously.
205 The synchronous function is called with one void* argument.
206 It should return 0 for success or -1 for failure -- failure should
207 be accompanied by an exception.
208
209 If registry succeeds, the registry function returns 0; if it fails
210 (e.g. due to too many pending calls) it returns -1 (without setting
211 an exception condition).
212
213 Note that because registry may occur from within signal handlers,
214 or other asynchronous events, calling malloc() is unsafe!
215
216#ifdef WITH_THREAD
217 Any thread can schedule pending calls, but only the main thread
218 will execute them.
219#endif
220
221 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
222 There are two possible race conditions:
223 (1) nested asynchronous registry calls;
224 (2) registry calls made while pending calls are being processed.
225 While (1) is very unlikely, (2) is a real possibility.
226 The current code is safe against (2), but not against (1).
227 The safety against (2) is derived from the fact that only one
228 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000229
Guido van Rossuma027efa1997-05-05 20:56:21 +0000230 XXX Darn! With the advent of thread state, we should have an array
231 of pending calls per thread in the thread state! Later...
232*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000233
Guido van Rossuma9672091994-09-14 13:31:22 +0000234#define NPENDINGCALLS 32
235static struct {
Guido van Rossumb209a111997-04-29 18:18:01 +0000236 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000237 ANY *arg;
238} pendingcalls[NPENDINGCALLS];
239static volatile int pendingfirst = 0;
240static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000241static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000242
243int
244Py_AddPendingCall(func, arg)
Guido van Rossumb209a111997-04-29 18:18:01 +0000245 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000246 ANY *arg;
247{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000248 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000249 int i, j;
250 /* XXX Begin critical section */
251 /* XXX If you want this to be safe against nested
252 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000253 if (busy)
254 return -1;
255 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000256 i = pendinglast;
257 j = (i + 1) % NPENDINGCALLS;
258 if (j == pendingfirst)
259 return -1; /* Queue full */
260 pendingcalls[i].func = func;
261 pendingcalls[i].arg = arg;
262 pendinglast = j;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000263 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000264 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000265 /* XXX End critical section */
266 return 0;
267}
268
Guido van Rossum180d7b41994-09-29 09:45:57 +0000269int
270Py_MakePendingCalls()
Guido van Rossuma9672091994-09-14 13:31:22 +0000271{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000272 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000273#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000274 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000275 return 0;
276#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000277 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000278 return 0;
279 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000280 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000281 for (;;) {
282 int i;
Guido van Rossumb209a111997-04-29 18:18:01 +0000283 int (*func) Py_PROTO((ANY *));
Guido van Rossuma9672091994-09-14 13:31:22 +0000284 ANY *arg;
285 i = pendingfirst;
286 if (i == pendinglast)
287 break; /* Queue empty */
288 func = pendingcalls[i].func;
289 arg = pendingcalls[i].arg;
290 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000291 if (func(arg) < 0) {
292 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000293 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000294 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000295 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000296 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000297 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000298 return 0;
299}
300
301
Guido van Rossum374a9221991-04-04 10:40:29 +0000302/* Status code for main loop (reason for stack unwind) */
303
304enum why_code {
305 WHY_NOT, /* No error */
306 WHY_EXCEPTION, /* Exception occurred */
307 WHY_RERAISE, /* Exception re-raised by 'finally' */
308 WHY_RETURN, /* 'return' statement */
309 WHY_BREAK /* 'break' statement */
310};
311
Guido van Rossumb209a111997-04-29 18:18:01 +0000312static enum why_code do_raise Py_PROTO((PyObject *, PyObject *, PyObject *));
Barry Warsawe42b18f1997-08-25 22:13:04 +0000313static int unpack_sequence Py_PROTO((PyObject *, int, PyObject **));
Guido van Rossum1aa14831997-01-21 05:34:20 +0000314
Guido van Rossum374a9221991-04-04 10:40:29 +0000315
Guido van Rossum681d79a1995-07-18 14:51:37 +0000316/* Backward compatible interface */
Guido van Rossum374a9221991-04-04 10:40:29 +0000317
Guido van Rossumb209a111997-04-29 18:18:01 +0000318PyObject *
319PyEval_EvalCode(co, globals, locals)
320 PyCodeObject *co;
321 PyObject *globals;
322 PyObject *locals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000323{
324 return eval_code2(co,
325 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000326 (PyObject **)NULL, 0,
327 (PyObject **)NULL, 0,
328 (PyObject **)NULL, 0,
329 (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000330}
331
332
333/* Interpreter main loop */
334
Guido van Rossum8861b741996-07-30 16:49:37 +0000335#ifndef MAX_RECURSION_DEPTH
336#define MAX_RECURSION_DEPTH 10000
337#endif
338
Guido van Rossumb209a111997-04-29 18:18:01 +0000339static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +0000340eval_code2(co, globals, locals,
341 args, argcount, kws, kwcount, defs, defcount, owner)
Guido van Rossumb209a111997-04-29 18:18:01 +0000342 PyCodeObject *co;
343 PyObject *globals;
344 PyObject *locals;
345 PyObject **args;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000346 int argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000347 PyObject **kws; /* length: 2*kwcount */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000348 int kwcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000349 PyObject **defs;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000350 int defcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000351 PyObject *owner;
Guido van Rossum374a9221991-04-04 10:40:29 +0000352{
Guido van Rossum950361c1997-01-24 13:49:28 +0000353#ifdef DXPAIRS
354 int lastopcode = 0;
355#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000356 register unsigned char *next_instr;
Guido van Rossum50cd3481998-11-17 17:02:51 +0000357 register int opcode; /* Current opcode */
358 register int oparg; /* Current opcode argument, if any */
Guido van Rossumb209a111997-04-29 18:18:01 +0000359 register PyObject **stack_pointer;
Guido van Rossum374a9221991-04-04 10:40:29 +0000360 register enum why_code why; /* Reason for block stack unwind */
361 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000362 register PyObject *x; /* Result object -- NULL if error */
363 register PyObject *v; /* Temporary objects popped off stack */
364 register PyObject *w;
365 register PyObject *u;
366 register PyObject *t;
367 register PyFrameObject *f; /* Current frame */
Guido van Rossum50cd3481998-11-17 17:02:51 +0000368 register PyObject **fastlocals;
Guido van Rossum014518f1998-11-23 21:09:51 +0000369 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000370 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd076c731998-10-07 19:42:25 +0000371 unsigned char *first_instr;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000372#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000373 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000374#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000375#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000376 /* Make it easier to find out where we are with a debugger */
Guido van Rossumb209a111997-04-29 18:18:01 +0000377 char *filename = PyString_AsString(co->co_filename);
Guido van Rossum99bec951992-09-03 20:29:45 +0000378#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000379
380/* Code access macros */
381
382#define GETCONST(i) Getconst(f, i)
383#define GETNAME(i) Getname(f, i)
384#define GETNAMEV(i) Getnamev(f, i)
Guido van Rossumd076c731998-10-07 19:42:25 +0000385#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000386#define NEXTOP() (*next_instr++)
387#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000388#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000389#define JUMPBY(x) (next_instr += (x))
390
391/* Stack manipulation macros */
392
393#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
394#define EMPTY() (STACK_LEVEL() == 0)
395#define TOP() (stack_pointer[-1])
396#define BASIC_PUSH(v) (*stack_pointer++ = (v))
397#define BASIC_POP() (*--stack_pointer)
398
Guido van Rossum96a42c81992-01-12 02:29:51 +0000399#ifdef LLTRACE
400#define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
401#define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
Guido van Rossum374a9221991-04-04 10:40:29 +0000402#else
403#define PUSH(v) BASIC_PUSH(v)
404#define POP() BASIC_POP()
405#endif
406
Guido van Rossum681d79a1995-07-18 14:51:37 +0000407/* Local variable macros */
408
409#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumb209a111997-04-29 18:18:01 +0000410#define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \
Guido van Rossum681d79a1995-07-18 14:51:37 +0000411 GETLOCAL(i) = value; } while (0)
412
Guido van Rossuma027efa1997-05-05 20:56:21 +0000413/* Start of code */
414
Guido van Rossum8861b741996-07-30 16:49:37 +0000415#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000416 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000417 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000418 return NULL;
419 }
420#endif
421
Guido van Rossum681d79a1995-07-18 14:51:37 +0000422 if (globals == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000423 PyErr_SetString(PyExc_SystemError, "eval_code2: NULL globals");
Guido van Rossum6135a871995-01-09 17:53:26 +0000424 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000425 }
426
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000427#ifdef LLTRACE
Guido van Rossumb209a111997-04-29 18:18:01 +0000428 lltrace = PyDict_GetItemString(globals, "__lltrace__") != NULL;
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000429#endif
430
Guido van Rossumb209a111997-04-29 18:18:01 +0000431 f = PyFrame_New(
Guido van Rossuma027efa1997-05-05 20:56:21 +0000432 tstate, /*back*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000433 co, /*code*/
434 globals, /*globals*/
Guido van Rossum76836021997-01-20 04:26:20 +0000435 locals); /*locals*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000436 if (f == NULL)
437 return NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000438
Guido van Rossuma027efa1997-05-05 20:56:21 +0000439 tstate->frame = f;
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000440 fastlocals = f->f_localsplus;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000441
442 if (co->co_argcount > 0 ||
443 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
444 int i;
445 int n = argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000446 PyObject *kwdict = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000447 if (co->co_flags & CO_VARKEYWORDS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000448 kwdict = PyDict_New();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000449 if (kwdict == NULL)
450 goto fail;
Guido van Rossum8c5df061997-01-24 04:19:24 +0000451 i = co->co_argcount;
452 if (co->co_flags & CO_VARARGS)
453 i++;
454 SETLOCAL(i, kwdict);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000455 }
456 if (argcount > co->co_argcount) {
457 if (!(co->co_flags & CO_VARARGS)) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000458 PyErr_Format(PyExc_TypeError,
459 "too many arguments; expected %d, got %d",
460 co->co_argcount, argcount);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000461 goto fail;
462 }
463 n = co->co_argcount;
464 }
465 for (i = 0; i < n; i++) {
466 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000467 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000468 SETLOCAL(i, x);
469 }
470 if (co->co_flags & CO_VARARGS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000471 u = PyTuple_New(argcount - n);
Guido van Rossum8c5df061997-01-24 04:19:24 +0000472 if (u == NULL)
473 goto fail;
474 SETLOCAL(co->co_argcount, u);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000475 for (i = n; i < argcount; i++) {
476 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000477 Py_INCREF(x);
478 PyTuple_SET_ITEM(u, i-n, x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000479 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000480 }
481 for (i = 0; i < kwcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000482 PyObject *keyword = kws[2*i];
483 PyObject *value = kws[2*i + 1];
Guido van Rossum681d79a1995-07-18 14:51:37 +0000484 int j;
485 /* XXX slow -- speed up using dictionary? */
486 for (j = 0; j < co->co_argcount; j++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000487 PyObject *nm = PyTuple_GET_ITEM(
488 co->co_varnames, j);
Guido van Rossumb209a111997-04-29 18:18:01 +0000489 if (PyObject_Compare(keyword, nm) == 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000490 break;
491 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000492 /* Check errors from Compare */
493 if (PyErr_Occurred())
494 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000495 if (j >= co->co_argcount) {
496 if (kwdict == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000497 PyErr_Format(PyExc_TypeError,
Guido van Rossuma027efa1997-05-05 20:56:21 +0000498 "unexpected keyword argument: %.400s",
499 PyString_AsString(keyword));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000500 goto fail;
501 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000502 PyDict_SetItem(kwdict, keyword, value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000503 }
504 else {
505 if (GETLOCAL(j) != NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000506 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000507 "keyword parameter redefined");
508 goto fail;
509 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000510 Py_INCREF(value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000511 SETLOCAL(j, value);
512 }
513 }
514 if (argcount < co->co_argcount) {
515 int m = co->co_argcount - defcount;
516 for (i = argcount; i < m; i++) {
517 if (GETLOCAL(i) == NULL) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000518 PyErr_Format(PyExc_TypeError,
519 "not enough arguments; expected %d, got %d",
520 m, i);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000521 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 Rossumd076c731998-10-07 19:42:25 +0000585 _PyCode_GETCODEPTR(co, &first_instr);
586 next_instr = first_instr;
Guido van Rossum374a9221991-04-04 10:40:29 +0000587 stack_pointer = f->f_valuestack;
588
Guido van Rossum374a9221991-04-04 10:40:29 +0000589 why = WHY_NOT;
590 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000591 x = Py_None; /* Not a reference, just anything non-NULL */
Guido van Rossum374a9221991-04-04 10:40:29 +0000592
593 for (;;) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000594 /* Do periodic things. Doing this every time through
595 the loop would add too much overhead, so we do it
596 only every Nth instruction. We also do it if
597 ``things_to_do'' is set, i.e. when an asynchronous
598 event needs attention (e.g. a signal handler or
599 async I/O handler); see Py_AddPendingCall() and
600 Py_MakePendingCalls() above. */
Guido van Rossuma9672091994-09-14 13:31:22 +0000601
Guido van Rossuma027efa1997-05-05 20:56:21 +0000602 if (things_to_do || --tstate->ticker < 0) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603 tstate->ticker = tstate->interp->checkinterval;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000604 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000605 if (Py_MakePendingCalls() < 0) {
606 why = WHY_EXCEPTION;
607 goto on_error;
608 }
609 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000610#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000611 /* If we have true signals, the signal handler
612 will call Py_AddPendingCall() so we don't
613 have to call sigcheck(). On the Mac and
614 DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000615 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000616 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000617 goto on_error;
618 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000619#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000620
Guido van Rossume59214e1994-08-30 08:01:59 +0000621#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000622 if (interpreter_lock) {
623 /* Give another thread a chance */
624
Guido van Rossum25ce5661997-08-02 03:10:38 +0000625 if (PyThreadState_Swap(NULL) != tstate)
626 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000627 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000628
629 /* Other threads may run now */
630
Guido van Rossum65d5b571998-12-21 19:32:43 +0000631 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000632 if (PyThreadState_Swap(tstate) != NULL)
633 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000634 }
635#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000636 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000637
Guido van Rossum374a9221991-04-04 10:40:29 +0000638 /* Extract opcode and argument */
Guido van Rossum99bec951992-09-03 20:29:45 +0000639
Guido van Rossum408027e1996-12-30 16:17:54 +0000640#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum99bec951992-09-03 20:29:45 +0000641 f->f_lasti = INSTR_OFFSET();
642#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000643
644 opcode = NEXTOP();
645 if (HAS_ARG(opcode))
646 oparg = NEXTARG();
Guido van Rossum950361c1997-01-24 13:49:28 +0000647#ifdef DYNAMIC_EXECUTION_PROFILE
648#ifdef DXPAIRS
649 dxpairs[lastopcode][opcode]++;
650 lastopcode = opcode;
651#endif
652 dxp[opcode]++;
653#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000654
Guido van Rossum96a42c81992-01-12 02:29:51 +0000655#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000656 /* Instruction tracing */
657
Guido van Rossum96a42c81992-01-12 02:29:51 +0000658 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000659 if (HAS_ARG(opcode)) {
660 printf("%d: %d, %d\n",
661 (int) (INSTR_OFFSET() - 3),
662 opcode, oparg);
663 }
664 else {
665 printf("%d: %d\n",
666 (int) (INSTR_OFFSET() - 1), opcode);
667 }
668 }
669#endif
670
671 /* Main switch on opcode */
672
673 switch (opcode) {
674
675 /* BEWARE!
676 It is essential that any operation that fails sets either
677 x to NULL, err to nonzero, or why to anything but WHY_NOT,
678 and that no operation that succeeds does this! */
679
680 /* case STOP_CODE: this is an error! */
681
682 case POP_TOP:
683 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000684 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000685 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000686
687 case ROT_TWO:
688 v = POP();
689 w = POP();
690 PUSH(v);
691 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000692 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000693
694 case ROT_THREE:
695 v = POP();
696 w = POP();
697 x = POP();
698 PUSH(v);
699 PUSH(x);
700 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000701 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000702
703 case DUP_TOP:
704 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000705 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000706 PUSH(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000707 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000708
709 case UNARY_POSITIVE:
710 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000711 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000712 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000713 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000714 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000715 break;
716
717 case UNARY_NEGATIVE:
718 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000719 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000720 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000721 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000722 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000723 break;
724
725 case UNARY_NOT:
726 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000727 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000728 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000729 if (err == 0) {
730 Py_INCREF(Py_True);
731 PUSH(Py_True);
732 continue;
733 }
734 else if (err > 0) {
735 Py_INCREF(Py_False);
736 PUSH(Py_False);
737 err = 0;
738 continue;
739 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000740 break;
741
742 case UNARY_CONVERT:
743 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000744 x = PyObject_Repr(v);
745 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000746 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000747 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000748 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000749
750 case UNARY_INVERT:
751 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000752 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000753 Py_DECREF(v);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000754 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000755 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000756 break;
Guido van Rossum374a9221991-04-04 10:40:29 +0000757
Guido van Rossum50564e81996-01-12 01:13:16 +0000758 case BINARY_POWER:
759 w = POP();
760 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000761 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000762 Py_DECREF(v);
763 Py_DECREF(w);
Guido van Rossum50564e81996-01-12 01:13:16 +0000764 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000765 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000766 break;
767
Guido van Rossum374a9221991-04-04 10:40:29 +0000768 case BINARY_MULTIPLY:
769 w = POP();
770 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000771 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000772 Py_DECREF(v);
773 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000774 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000775 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000776 break;
777
778 case BINARY_DIVIDE:
779 w = POP();
780 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000781 x = PyNumber_Divide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000782 Py_DECREF(v);
783 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000784 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000785 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000786 break;
787
788 case BINARY_MODULO:
789 w = POP();
790 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000791 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000792 Py_DECREF(v);
793 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000794 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000795 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000796 break;
797
798 case BINARY_ADD:
799 w = POP();
800 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000801 if (PyInt_Check(v) && PyInt_Check(w)) {
802 /* INLINE: int + int */
803 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000804 a = PyInt_AS_LONG(v);
805 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000806 i = a + b;
807 if ((i^a) < 0 && (i^b) < 0) {
808 PyErr_SetString(PyExc_OverflowError,
809 "integer addition");
810 x = NULL;
811 }
812 else
813 x = PyInt_FromLong(i);
814 }
815 else
816 x = PyNumber_Add(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000817 Py_DECREF(v);
818 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000819 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000820 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000821 break;
822
823 case BINARY_SUBTRACT:
824 w = POP();
825 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000826 if (PyInt_Check(v) && PyInt_Check(w)) {
827 /* INLINE: int - int */
828 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000829 a = PyInt_AS_LONG(v);
830 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000831 i = a - b;
832 if ((i^a) < 0 && (i^~b) < 0) {
833 PyErr_SetString(PyExc_OverflowError,
834 "integer subtraction");
835 x = NULL;
836 }
837 else
838 x = PyInt_FromLong(i);
839 }
840 else
841 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000842 Py_DECREF(v);
843 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000844 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000845 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000846 break;
847
848 case BINARY_SUBSCR:
849 w = POP();
850 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000851 if (PyList_Check(v) && PyInt_Check(w)) {
852 /* INLINE: list[int] */
853 long i = PyInt_AsLong(w);
854 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +0000855 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +0000856 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +0000857 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000858 PyErr_SetString(PyExc_IndexError,
859 "list index out of range");
860 x = NULL;
861 }
862 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +0000863 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +0000864 Py_INCREF(x);
865 }
866 }
867 else
868 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000869 Py_DECREF(v);
870 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000871 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000872 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000873 break;
874
Guido van Rossum7928cd71991-10-24 14:59:31 +0000875 case BINARY_LSHIFT:
876 w = POP();
877 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000878 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000879 Py_DECREF(v);
880 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000881 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000882 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000883 break;
884
885 case BINARY_RSHIFT:
886 w = POP();
887 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000888 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000889 Py_DECREF(v);
890 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000891 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000892 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000893 break;
894
895 case BINARY_AND:
896 w = POP();
897 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000898 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000899 Py_DECREF(v);
900 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000901 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000902 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000903 break;
904
905 case BINARY_XOR:
906 w = POP();
907 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000908 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000909 Py_DECREF(v);
910 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000911 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000912 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000913 break;
914
915 case BINARY_OR:
916 w = POP();
917 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000918 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000919 Py_DECREF(v);
920 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000921 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000922 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000923 break;
924
Guido van Rossum374a9221991-04-04 10:40:29 +0000925 case SLICE+0:
926 case SLICE+1:
927 case SLICE+2:
928 case SLICE+3:
929 if ((opcode-SLICE) & 2)
930 w = POP();
931 else
932 w = NULL;
933 if ((opcode-SLICE) & 1)
934 v = POP();
935 else
936 v = NULL;
937 u = POP();
938 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000939 Py_DECREF(u);
940 Py_XDECREF(v);
941 Py_XDECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000942 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000943 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000944 break;
945
946 case STORE_SLICE+0:
947 case STORE_SLICE+1:
948 case STORE_SLICE+2:
949 case STORE_SLICE+3:
950 if ((opcode-STORE_SLICE) & 2)
951 w = POP();
952 else
953 w = NULL;
954 if ((opcode-STORE_SLICE) & 1)
955 v = POP();
956 else
957 v = NULL;
958 u = POP();
959 t = POP();
960 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +0000961 Py_DECREF(t);
962 Py_DECREF(u);
963 Py_XDECREF(v);
964 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000965 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000966 break;
967
968 case DELETE_SLICE+0:
969 case DELETE_SLICE+1:
970 case DELETE_SLICE+2:
971 case DELETE_SLICE+3:
972 if ((opcode-DELETE_SLICE) & 2)
973 w = POP();
974 else
975 w = NULL;
976 if ((opcode-DELETE_SLICE) & 1)
977 v = POP();
978 else
979 v = NULL;
980 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000981 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +0000982 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +0000983 Py_DECREF(u);
984 Py_XDECREF(v);
985 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000986 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000987 break;
988
989 case STORE_SUBSCR:
990 w = POP();
991 v = POP();
992 u = POP();
993 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +0000994 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +0000995 Py_DECREF(u);
996 Py_DECREF(v);
997 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000998 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000999 break;
1000
1001 case DELETE_SUBSCR:
1002 w = POP();
1003 v = POP();
1004 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001005 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001006 Py_DECREF(v);
1007 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001008 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001009 break;
1010
1011 case PRINT_EXPR:
1012 v = POP();
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001013 /* Print value except if None */
1014 /* After printing, also assign to '_' */
1015 /* Before, set '_' to None to avoid recursion */
Guido van Rossumb209a111997-04-29 18:18:01 +00001016 if (v != Py_None &&
Guido van Rossuma027efa1997-05-05 20:56:21 +00001017 (err = PyDict_SetItemString(
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001018 f->f_builtins, "_", Py_None)) == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001019 err = Py_FlushLine();
Guido van Rossumc12da691997-07-17 23:12:42 +00001020 if (err == 0) {
Guido van Rossumbe270261997-05-22 22:26:18 +00001021 x = PySys_GetObject("stdout");
Guido van Rossum8f183201997-12-31 05:53:15 +00001022 if (x == NULL) {
1023 PyErr_SetString(
1024 PyExc_RuntimeError,
1025 "lost sys.stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00001026 err = -1;
Guido van Rossum8f183201997-12-31 05:53:15 +00001027 }
Guido van Rossumbe270261997-05-22 22:26:18 +00001028 }
1029 if (err == 0)
1030 err = PyFile_WriteObject(v, x, 0);
1031 if (err == 0) {
1032 PyFile_SoftSpace(x, 1);
1033 err = Py_FlushLine();
1034 }
Guido van Rossumaa06b0e1997-12-26 22:15:57 +00001035 if (err == 0) {
1036 err = PyDict_SetItemString(
1037 f->f_builtins, "_", v);
1038 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001039 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001040 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001041 break;
1042
1043 case PRINT_ITEM:
1044 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001045 w = PySys_GetObject("stdout");
Guido van Rossum8f183201997-12-31 05:53:15 +00001046 if (w == NULL) {
1047 PyErr_SetString(PyExc_RuntimeError,
1048 "lost sys.stdout");
1049 err = -1;
1050 }
1051 else if (PyFile_SoftSpace(w, 1))
Guido van Rossumbe270261997-05-22 22:26:18 +00001052 err = PyFile_WriteString(" ", w);
1053 if (err == 0)
1054 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Guido van Rossumb209a111997-04-29 18:18:01 +00001055 if (err == 0 && PyString_Check(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +00001056 /* XXX move into writeobject() ? */
Guido van Rossumb209a111997-04-29 18:18:01 +00001057 char *s = PyString_AsString(v);
1058 int len = PyString_Size(v);
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001059 if (len > 0 &&
1060 isspace(Py_CHARMASK(s[len-1])) &&
Guido van Rossumc6004111993-11-05 10:22:19 +00001061 s[len-1] != ' ')
Guido van Rossumb209a111997-04-29 18:18:01 +00001062 PyFile_SoftSpace(w, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +00001063 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001064 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001065 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001066 break;
1067
1068 case PRINT_NEWLINE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001069 x = PySys_GetObject("stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001070 if (x == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00001071 PyErr_SetString(PyExc_RuntimeError,
1072 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001073 else {
Guido van Rossumbe270261997-05-22 22:26:18 +00001074 err = PyFile_WriteString("\n", x);
1075 if (err == 0)
1076 PyFile_SoftSpace(x, 0);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001077 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001078 break;
1079
1080 case BREAK_LOOP:
1081 why = WHY_BREAK;
1082 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001083
Guido van Rossumf10570b1995-07-07 22:53:21 +00001084 case RAISE_VARARGS:
1085 u = v = w = NULL;
1086 switch (oparg) {
1087 case 3:
1088 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001089 /* Fallthrough */
1090 case 2:
1091 v = POP(); /* value */
1092 /* Fallthrough */
1093 case 1:
1094 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001095 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001096 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001097 break;
1098 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001099 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001100 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001101 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001102 break;
1103 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001104 break;
1105
1106 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001107 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001108 PyErr_SetString(PyExc_SystemError,
1109 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001110 break;
1111 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001112 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001113 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001114 break;
1115
1116 case RETURN_VALUE:
1117 retval = POP();
1118 why = WHY_RETURN;
1119 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001120
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001121 case EXEC_STMT:
1122 w = POP();
1123 v = POP();
1124 u = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001125 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001126 Py_DECREF(u);
1127 Py_DECREF(v);
1128 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001129 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001130
Guido van Rossum374a9221991-04-04 10:40:29 +00001131 case POP_BLOCK:
1132 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001133 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001134 while (STACK_LEVEL() > b->b_level) {
1135 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001136 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001137 }
1138 }
1139 break;
1140
1141 case END_FINALLY:
1142 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001143 if (PyInt_Check(v)) {
1144 why = (enum why_code) PyInt_AsLong(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001145 if (why == WHY_RETURN)
1146 retval = POP();
1147 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001148 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001150 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001151 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001152 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001153 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001154 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001155 else if (v != Py_None) {
1156 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001157 "'finally' pops bad exception");
1158 why = WHY_EXCEPTION;
1159 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001160 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001161 break;
1162
1163 case BUILD_CLASS:
Guido van Rossum25831651993-05-19 14:50:45 +00001164 u = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001165 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +00001166 w = POP();
1167 x = build_class(u, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001168 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001169 Py_DECREF(u);
1170 Py_DECREF(v);
1171 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001172 break;
1173
1174 case STORE_NAME:
1175 w = GETNAMEV(oparg);
1176 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001177 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001178 PyErr_SetString(PyExc_SystemError,
1179 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001180 break;
1181 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001182 err = PyDict_SetItem(x, w, v);
1183 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001184 break;
1185
1186 case DELETE_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001187 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001188 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001189 PyErr_SetString(PyExc_SystemError,
1190 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001191 break;
1192 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001193 if ((err = PyDict_DelItem(x, w)) != 0)
1194 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001195 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001196
1197#ifdef CASE_TOO_BIG
1198 default: switch (opcode) {
1199#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001200
1201 case UNPACK_TUPLE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001202 case UNPACK_LIST:
1203 v = POP();
Barry Warsawe42b18f1997-08-25 22:13:04 +00001204 if (PyTuple_Check(v)) {
1205 if (PyTuple_Size(v) != oparg) {
1206 PyErr_SetString(PyExc_ValueError,
1207 "unpack tuple of wrong size");
1208 why = WHY_EXCEPTION;
1209 }
1210 else {
1211 for (; --oparg >= 0; ) {
1212 w = PyTuple_GET_ITEM(v, oparg);
1213 Py_INCREF(w);
1214 PUSH(w);
1215 }
1216 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001217 }
Barry Warsawe42b18f1997-08-25 22:13:04 +00001218 else if (PyList_Check(v)) {
1219 if (PyList_Size(v) != oparg) {
1220 PyErr_SetString(PyExc_ValueError,
1221 "unpack list of wrong size");
1222 why = WHY_EXCEPTION;
1223 }
1224 else {
1225 for (; --oparg >= 0; ) {
1226 w = PyList_GET_ITEM(v, oparg);
1227 Py_INCREF(w);
1228 PUSH(w);
1229 }
1230 }
1231 }
1232 else if (PySequence_Check(v)) {
1233 if (unpack_sequence(v, oparg,
1234 stack_pointer + oparg))
1235 stack_pointer += oparg;
1236 else
1237 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001238 }
1239 else {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001240 PyErr_SetString(PyExc_TypeError,
1241 "unpack non-sequence");
1242 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001243 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001244 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001245 break;
1246
1247 case STORE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001248 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001249 v = POP();
1250 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001251 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1252 Py_DECREF(v);
1253 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001254 break;
1255
1256 case DELETE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001257 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001258 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001259 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1260 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001261 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001262 break;
1263
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001264 case STORE_GLOBAL:
1265 w = GETNAMEV(oparg);
1266 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001267 err = PyDict_SetItem(f->f_globals, w, v);
1268 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001269 break;
1270
1271 case DELETE_GLOBAL:
1272 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001273 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1274 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001275 break;
1276
Guido van Rossum374a9221991-04-04 10:40:29 +00001277 case LOAD_CONST:
1278 x = GETCONST(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001279 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001280 PUSH(x);
1281 break;
1282
1283 case LOAD_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001284 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001285 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001286 PyErr_SetString(PyExc_SystemError,
1287 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001288 break;
1289 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001290 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001291 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001292 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001293 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001294 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001295 if (x == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001296 PyErr_SetObject(
1297 PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001298 break;
1299 }
1300 }
1301 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001302 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001303 PUSH(x);
1304 break;
1305
1306 case LOAD_GLOBAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001307 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001308 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001309 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001310 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001311 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001312 PyErr_SetObject(PyExc_NameError, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001313 break;
1314 }
1315 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001316 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001317 PUSH(x);
1318 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001319
Guido van Rossum9bfef441993-03-29 10:43:31 +00001320 case LOAD_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001321 x = GETLOCAL(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001322 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001323 PyErr_SetObject(PyExc_NameError,
1324 PyTuple_GetItem(co->co_varnames,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001325 oparg));
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001326 break;
1327 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001328 Py_INCREF(x);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001329 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001330 if (x != NULL) continue;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001331 break;
1332
1333 case STORE_FAST:
Guido van Rossum25831651993-05-19 14:50:45 +00001334 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001335 SETLOCAL(oparg, v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001336 continue;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001337
1338 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001339 x = GETLOCAL(oparg);
1340 if (x == NULL) {
1341 PyErr_SetObject(PyExc_NameError,
1342 PyTuple_GetItem(co->co_varnames,
1343 oparg));
1344 break;
1345 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001346 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001347 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001348
1349 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001350 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001351 if (x != NULL) {
1352 for (; --oparg >= 0;) {
1353 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001354 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001355 }
1356 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001357 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001358 }
1359 break;
1360
1361 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001362 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001363 if (x != NULL) {
1364 for (; --oparg >= 0;) {
1365 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001366 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001367 }
1368 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001369 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001370 }
1371 break;
1372
1373 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001374 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001375 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001376 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001377 break;
1378
1379 case LOAD_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001380 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001381 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001382 x = PyObject_GetAttr(v, w);
1383 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001384 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001385 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001386 break;
1387
1388 case COMPARE_OP:
1389 w = POP();
1390 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +00001391 if (PyInt_Check(v) && PyInt_Check(w)) {
1392 /* INLINE: cmp(int, int) */
1393 register long a, b;
1394 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001395 a = PyInt_AS_LONG(v);
1396 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001397 switch (oparg) {
1398 case LT: res = a < b; break;
1399 case LE: res = a <= b; break;
1400 case EQ: res = a == b; break;
1401 case NE: res = a != b; break;
1402 case GT: res = a > b; break;
1403 case GE: res = a >= b; break;
1404 case IS: res = v == w; break;
1405 case IS_NOT: res = v != w; break;
1406 default: goto slow_compare;
1407 }
1408 x = res ? Py_True : Py_False;
1409 Py_INCREF(x);
1410 }
1411 else {
1412 slow_compare:
1413 x = cmp_outcome(oparg, v, w);
1414 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001415 Py_DECREF(v);
1416 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001418 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 break;
1420
1421 case IMPORT_NAME:
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001422 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001423 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001424 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001425 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001426 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001427 break;
1428 }
Guido van Rossumaee0bad1997-09-05 07:33:22 +00001429 u = find_from_args(f, INSTR_OFFSET());
1430 if (u == NULL) {
1431 x = u;
1432 break;
Guido van Rossum24c13741995-02-14 09:42:43 +00001433 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001434 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001435 w,
1436 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001437 f->f_locals == NULL ?
1438 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001439 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001440 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001441 if (w == NULL) {
1442 x = NULL;
1443 break;
1444 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001445 x = PyEval_CallObject(x, w);
1446 Py_DECREF(w);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001447 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001448 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001449 break;
1450
1451 case IMPORT_FROM:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001452 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001454 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001455 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001456 PyErr_SetString(PyExc_SystemError,
1457 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001458 break;
1459 }
1460 err = import_from(x, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001461 PyFrame_LocalsToFast(f, 0);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001462 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001463 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001464
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 case JUMP_FORWARD:
1466 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001467 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001468
1469 case JUMP_IF_FALSE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001470 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001471 if (err > 0)
1472 err = 0;
1473 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001474 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001475 else
1476 break;
1477 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001478
1479 case JUMP_IF_TRUE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001480 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001481 if (err > 0) {
1482 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001483 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001484 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001485 else if (err == 0)
1486 ;
1487 else
1488 break;
1489 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001490
1491 case JUMP_ABSOLUTE:
1492 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001493 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001494
1495 case FOR_LOOP:
1496 /* for v in s: ...
1497 On entry: stack contains s, i.
1498 On exit: stack contains s, i+1, s[i];
1499 but if loop exhausted:
1500 s, i are popped, and we jump */
1501 w = POP(); /* Loop index */
1502 v = POP(); /* Sequence object */
1503 u = loop_subscript(v, w);
1504 if (u != NULL) {
1505 PUSH(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001506 x = PyInt_FromLong(PyInt_AsLong(w)+1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001507 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001508 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001509 PUSH(u);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001510 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 }
1512 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001513 Py_DECREF(v);
1514 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001515 /* A NULL can mean "s exhausted"
1516 but also an error: */
Guido van Rossumb209a111997-04-29 18:18:01 +00001517 if (PyErr_Occurred())
Guido van Rossum374a9221991-04-04 10:40:29 +00001518 why = WHY_EXCEPTION;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001519 else {
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001521 continue;
1522 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001523 }
1524 break;
1525
1526 case SETUP_LOOP:
1527 case SETUP_EXCEPT:
1528 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00001529 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Guido van Rossum374a9221991-04-04 10:40:29 +00001530 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001531 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001532
1533 case SET_LINENO:
Guido van Rossum96a42c81992-01-12 02:29:51 +00001534#ifdef LLTRACE
1535 if (lltrace)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001536 printf("--- %s:%d \n", filename, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001537#endif
Guido van Rossum96a42c81992-01-12 02:29:51 +00001538 f->f_lineno = oparg;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001539 if (f->f_trace == NULL)
1540 continue;
1541 /* Trace each line of code reached */
1542 f->f_lasti = INSTR_OFFSET();
1543 err = call_trace(&f->f_trace, &f->f_trace,
Guido van Rossumb209a111997-04-29 18:18:01 +00001544 f, "line", Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00001545 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001546
1547 case CALL_FUNCTION:
1548 {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001549 int na = oparg & 0xff;
1550 int nk = (oparg>>8) & 0xff;
1551 int n = na + 2*nk;
Guido van Rossumb209a111997-04-29 18:18:01 +00001552 PyObject **pfunc = stack_pointer - n - 1;
1553 PyObject *func = *pfunc;
1554 PyObject *self = NULL;
1555 PyObject *class = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001556 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
Guido van Rossumb209a111997-04-29 18:18:01 +00001557 if (PyMethod_Check(func)) {
1558 self = PyMethod_Self(func);
1559 class = PyMethod_Class(func);
1560 func = PyMethod_Function(func);
1561 Py_INCREF(func);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001562 if (self != NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001563 Py_INCREF(self);
1564 Py_DECREF(*pfunc);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001565 *pfunc = self;
1566 na++;
1567 n++;
1568 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001569 else {
1570 /* Unbound methods must be
1571 called with an instance of
1572 the class (or a derived
1573 class) as first argument */
1574 if (na > 0 &&
1575 (self = stack_pointer[-n])
1576 != NULL &&
Guido van Rossumb209a111997-04-29 18:18:01 +00001577 PyInstance_Check(self) &&
1578 PyClass_IsSubclass(
1579 (PyObject *)
1580 (((PyInstanceObject *)self)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001581 ->in_class),
1582 class))
1583 /* Handy-dandy */ ;
1584 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001585 PyErr_SetString(
1586 PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001587 "unbound method must be called with class instance 1st argument");
Guido van Rossum72b56e81995-12-10 04:57:42 +00001588 x = NULL;
1589 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001590 }
1591 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001592 }
1593 else
Guido van Rossumb209a111997-04-29 18:18:01 +00001594 Py_INCREF(func);
1595 if (PyFunction_Check(func)) {
1596 PyObject *co = PyFunction_GetCode(func);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001597 PyObject *globals =
1598 PyFunction_GetGlobals(func);
1599 PyObject *argdefs =
1600 PyFunction_GetDefaults(func);
Guido van Rossumb209a111997-04-29 18:18:01 +00001601 PyObject **d;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001602 int nd;
1603 if (argdefs != NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001604 d = &PyTuple_GET_ITEM(argdefs, 0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001605 nd = ((PyTupleObject *)argdefs) ->
1606 ob_size;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001607 }
1608 else {
1609 d = NULL;
1610 nd = 0;
1611 }
1612 x = eval_code2(
Guido van Rossumb209a111997-04-29 18:18:01 +00001613 (PyCodeObject *)co,
1614 globals, (PyObject *)NULL,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001615 stack_pointer-n, na,
1616 stack_pointer-2*nk, nk,
1617 d, nd,
1618 class);
1619 }
1620 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001621 PyObject *args = PyTuple_New(na);
1622 PyObject *kwdict = NULL;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001623 if (args == NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001624 x = NULL;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001625 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001626 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001627 if (nk > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001628 kwdict = PyDict_New();
Guido van Rossumff8b4941995-07-26 18:16:42 +00001629 if (kwdict == NULL) {
1630 x = NULL;
1631 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001632 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001633 err = 0;
1634 while (--nk >= 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001635 PyObject *value = POP();
1636 PyObject *key = POP();
1637 err = PyDict_SetItem(
Guido van Rossumff8b4941995-07-26 18:16:42 +00001638 kwdict, key, value);
Guido van Rossumb209a111997-04-29 18:18:01 +00001639 Py_DECREF(key);
1640 Py_DECREF(value);
Guido van Rossumd0eb4291997-01-27 21:30:09 +00001641 if (err)
Guido van Rossumff8b4941995-07-26 18:16:42 +00001642 break;
Guido van Rossumff8b4941995-07-26 18:16:42 +00001643 }
1644 if (err) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001645 Py_DECREF(args);
1646 Py_DECREF(kwdict);
Guido van Rossumff8b4941995-07-26 18:16:42 +00001647 break;
1648 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001649 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00001650 while (--na >= 0) {
1651 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001652 PyTuple_SET_ITEM(args, na, w);
Guido van Rossumff8b4941995-07-26 18:16:42 +00001653 }
1654 x = PyEval_CallObjectWithKeywords(
1655 func, args, kwdict);
Guido van Rossumb209a111997-04-29 18:18:01 +00001656 Py_DECREF(args);
1657 Py_XDECREF(kwdict);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001658 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001659 Py_DECREF(func);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001660 while (stack_pointer > pfunc) {
1661 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001662 Py_DECREF(w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001663 }
1664 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001665 if (x != NULL) continue;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001666 break;
1667 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001668
Guido van Rossum681d79a1995-07-18 14:51:37 +00001669 case MAKE_FUNCTION:
1670 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00001671 x = PyFunction_New(v, f->f_globals);
1672 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001673 /* XXX Maybe this should be a separate opcode? */
1674 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001675 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001676 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001677 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001678 x = NULL;
1679 break;
1680 }
1681 while (--oparg >= 0) {
1682 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001683 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001684 }
1685 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001686 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001687 }
1688 PUSH(x);
1689 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001690
1691 case BUILD_SLICE:
1692 if (oparg == 3)
1693 w = POP();
1694 else
1695 w = NULL;
1696 v = POP();
1697 u = POP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00001698 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001699 Py_DECREF(u);
1700 Py_DECREF(v);
1701 Py_XDECREF(w);
Guido van Rossum8861b741996-07-30 16:49:37 +00001702 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001703 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00001704 break;
1705
1706
Guido van Rossum374a9221991-04-04 10:40:29 +00001707 default:
1708 fprintf(stderr,
1709 "XXX lineno: %d, opcode: %d\n",
Guido van Rossum96a42c81992-01-12 02:29:51 +00001710 f->f_lineno, opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00001711 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 why = WHY_EXCEPTION;
1713 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001714
1715#ifdef CASE_TOO_BIG
1716 }
1717#endif
1718
Guido van Rossum374a9221991-04-04 10:40:29 +00001719 } /* switch */
1720
1721 on_error:
1722
1723 /* Quickly continue if no error occurred */
1724
1725 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001726 if (err == 0 && x != NULL) {
1727#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00001728 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00001729 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00001730 fprintf(stderr,
1731 "XXX undetected error\n");
1732 else
1733#endif
1734 continue; /* Normal, fast path */
1735 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001736 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00001737 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00001738 err = 0;
1739 }
1740
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 /* Double-check exception status */
1742
1743 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001745 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00001746 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 why = WHY_EXCEPTION;
1748 }
1749 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00001750#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00001752 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00001753 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00001754 fprintf(stderr,
1755 "XXX undetected error (why=%d)\n",
1756 why);
1757 why = WHY_EXCEPTION;
1758 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 }
1760#endif
1761
1762 /* Log traceback info if this is a real exception */
1763
1764 if (why == WHY_EXCEPTION) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001765 f->f_lasti = INSTR_OFFSET() - 1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 if (HAS_ARG(opcode))
Guido van Rossum96a42c81992-01-12 02:29:51 +00001767 f->f_lasti -= 2;
Guido van Rossumb209a111997-04-29 18:18:01 +00001768 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001769
Guido van Rossume59214e1994-08-30 08:01:59 +00001770 if (f->f_trace)
1771 call_exc_trace(&f->f_trace, &f->f_trace, f);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001772 if (tstate->sys_profilefunc)
1773 call_exc_trace(&tstate->sys_profilefunc,
1774 (PyObject**)0, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00001775 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001776
1777 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
1778
1779 if (why == WHY_RERAISE)
1780 why = WHY_EXCEPTION;
1781
1782 /* Unwind stacks if a (pseudo) exception occurred */
1783
1784 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001785 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001786 while (STACK_LEVEL() > b->b_level) {
1787 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001788 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 }
1790 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
1791 why = WHY_NOT;
1792 JUMPTO(b->b_handler);
1793 break;
1794 }
1795 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00001796 (b->b_type == SETUP_EXCEPT &&
1797 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001798 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001799 PyObject *exc, *val, *tb;
1800 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001801 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001802 val = Py_None;
1803 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00001804 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 /* Make the raw exception data
1806 available to the handler,
1807 so a program can emulate the
1808 Python main loop. Don't do
1809 this for 'finally'. */
1810 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00001811 PyErr_NormalizeException(
1812 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001813 set_exc_info(tstate,
1814 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001815 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001816 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 PUSH(val);
1818 PUSH(exc);
1819 }
1820 else {
1821 if (why == WHY_RETURN)
1822 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00001823 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00001824 PUSH(v);
1825 }
1826 why = WHY_NOT;
1827 JUMPTO(b->b_handler);
1828 break;
1829 }
1830 } /* unwind stack */
1831
1832 /* End the loop if we still have an error (or return) */
1833
1834 if (why != WHY_NOT)
1835 break;
1836
1837 } /* main loop */
1838
1839 /* Pop remaining stack entries */
1840
1841 while (!EMPTY()) {
1842 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001843 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001844 }
1845
Guido van Rossum96a42c81992-01-12 02:29:51 +00001846 if (why != WHY_RETURN)
1847 retval = NULL;
1848
Guido van Rossume59214e1994-08-30 08:01:59 +00001849 if (f->f_trace) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00001850 if (why == WHY_RETURN) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001851 if (call_trace(&f->f_trace, &f->f_trace, f,
1852 "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001853 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00001854 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001855 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00001856 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001857 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001858 }
1859
Guido van Rossuma027efa1997-05-05 20:56:21 +00001860 if (tstate->sys_profilefunc && why == WHY_RETURN) {
1861 if (call_trace(&tstate->sys_profilefunc, (PyObject**)0,
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001862 f, "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001863 Py_XDECREF(retval);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00001864 retval = NULL;
1865 why = WHY_EXCEPTION;
1866 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00001867 }
Guido van Rossuma4240131997-01-21 21:18:36 +00001868
Guido van Rossuma027efa1997-05-05 20:56:21 +00001869 reset_exc_info(tstate);
1870
1871 --tstate->recursion_depth;
Guido van Rossum8c5df061997-01-24 04:19:24 +00001872
1873 fail: /* Jump here from prelude on failure */
Guido van Rossum96a42c81992-01-12 02:29:51 +00001874
Guido van Rossum374a9221991-04-04 10:40:29 +00001875 /* Restore previous frame and release the current one */
Guido van Rossum681d79a1995-07-18 14:51:37 +00001876
Guido van Rossuma027efa1997-05-05 20:56:21 +00001877 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +00001878 Py_DECREF(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001879
Guido van Rossum96a42c81992-01-12 02:29:51 +00001880 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00001881}
1882
Guido van Rossuma027efa1997-05-05 20:56:21 +00001883static void
1884set_exc_info(tstate, type, value, tb)
1885 PyThreadState *tstate;
1886 PyObject *type;
1887 PyObject *value;
1888 PyObject *tb;
1889{
1890 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001891 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00001892
Guido van Rossuma027efa1997-05-05 20:56:21 +00001893 frame = tstate->frame;
1894 if (frame->f_exc_type == NULL) {
1895 /* This frame didn't catch an exception before */
1896 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00001897 if (tstate->exc_type == NULL) {
1898 Py_INCREF(Py_None);
1899 tstate->exc_type = Py_None;
1900 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001901 tmp_type = frame->f_exc_type;
1902 tmp_value = frame->f_exc_value;
1903 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001904 Py_XINCREF(tstate->exc_type);
1905 Py_XINCREF(tstate->exc_value);
1906 Py_XINCREF(tstate->exc_traceback);
1907 frame->f_exc_type = tstate->exc_type;
1908 frame->f_exc_value = tstate->exc_value;
1909 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001910 Py_XDECREF(tmp_type);
1911 Py_XDECREF(tmp_value);
1912 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001913 }
1914 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001915 tmp_type = tstate->exc_type;
1916 tmp_value = tstate->exc_value;
1917 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001918 Py_XINCREF(type);
1919 Py_XINCREF(value);
1920 Py_XINCREF(tb);
1921 tstate->exc_type = type;
1922 tstate->exc_value = value;
1923 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001924 Py_XDECREF(tmp_type);
1925 Py_XDECREF(tmp_value);
1926 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001927 /* For b/w compatibility */
1928 PySys_SetObject("exc_type", type);
1929 PySys_SetObject("exc_value", value);
1930 PySys_SetObject("exc_traceback", tb);
1931}
1932
1933static void
1934reset_exc_info(tstate)
1935 PyThreadState *tstate;
1936{
1937 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001938 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001939 frame = tstate->frame;
1940 if (frame->f_exc_type != NULL) {
1941 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001942 tmp_type = tstate->exc_type;
1943 tmp_value = tstate->exc_value;
1944 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001945 Py_XINCREF(frame->f_exc_type);
1946 Py_XINCREF(frame->f_exc_value);
1947 Py_XINCREF(frame->f_exc_traceback);
1948 tstate->exc_type = frame->f_exc_type;
1949 tstate->exc_value = frame->f_exc_value;
1950 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001951 Py_XDECREF(tmp_type);
1952 Py_XDECREF(tmp_value);
1953 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001954 /* For b/w compatibility */
1955 PySys_SetObject("exc_type", frame->f_exc_type);
1956 PySys_SetObject("exc_value", frame->f_exc_value);
1957 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
1958 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001959 tmp_type = frame->f_exc_type;
1960 tmp_value = frame->f_exc_value;
1961 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001962 frame->f_exc_type = NULL;
1963 frame->f_exc_value = NULL;
1964 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00001965 Py_XDECREF(tmp_type);
1966 Py_XDECREF(tmp_value);
1967 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001968}
1969
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001970/* Logic for the raise statement (too complicated for inlining).
1971 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00001972static enum why_code
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001973do_raise(type, value, tb)
Guido van Rossumb209a111997-04-29 18:18:01 +00001974 PyObject *type, *value, *tb;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001975{
Guido van Rossumd295f121998-04-09 21:39:57 +00001976 if (type == NULL) {
1977 /* Reraise */
1978 PyThreadState *tstate = PyThreadState_Get();
1979 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
1980 value = tstate->exc_value;
1981 tb = tstate->exc_traceback;
1982 Py_XINCREF(type);
1983 Py_XINCREF(value);
1984 Py_XINCREF(tb);
1985 }
1986
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001987 /* We support the following forms of raise:
1988 raise <class>, <classinstance>
1989 raise <class>, <argument tuple>
1990 raise <class>, None
1991 raise <class>, <argument>
1992 raise <classinstance>, None
1993 raise <string>, <object>
1994 raise <string>, None
1995
1996 An omitted second argument is the same as None.
1997
1998 In addition, raise <tuple>, <anything> is the same as
1999 raising the tuple's first item (and it better have one!);
2000 this rule is applied recursively.
2001
2002 Finally, an optional third argument can be supplied, which
2003 gives the traceback to be substituted (useful when
2004 re-raising an exception after examining it). */
2005
2006 /* First, check the traceback argument, replacing None with
2007 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002008 if (tb == Py_None) {
2009 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002010 tb = NULL;
2011 }
2012 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002013 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002014 "raise 3rd arg must be traceback or None");
2015 goto raise_error;
2016 }
2017
2018 /* Next, replace a missing value with None */
2019 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002020 value = Py_None;
2021 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002022 }
2023
2024 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002025 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2026 PyObject *tmp = type;
2027 type = PyTuple_GET_ITEM(type, 0);
2028 Py_INCREF(type);
2029 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002030 }
2031
Barry Warsaw4249f541997-08-22 21:26:19 +00002032 if (PyString_Check(type))
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002033 ;
Barry Warsaw4249f541997-08-22 21:26:19 +00002034
2035 else if (PyClass_Check(type))
2036 PyErr_NormalizeException(&type, &value, &tb);
2037
Guido van Rossumb209a111997-04-29 18:18:01 +00002038 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002039 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002040 if (value != Py_None) {
2041 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002042 "instance exception may not have a separate value");
2043 goto raise_error;
2044 }
2045 else {
2046 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002047 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002048 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2050 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002051 }
2052 }
2053 else {
2054 /* Not something you can raise. You get an exception
2055 anyway, just not what you specified :-) */
Guido van Rossumb209a111997-04-29 18:18:01 +00002056 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002057 "exceptions must be strings, classes, or instances");
2058 goto raise_error;
2059 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002060 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002061 if (tb == NULL)
2062 return WHY_EXCEPTION;
2063 else
2064 return WHY_RERAISE;
2065 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002066 Py_XDECREF(value);
2067 Py_XDECREF(type);
2068 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002069 return WHY_EXCEPTION;
2070}
2071
Barry Warsawe42b18f1997-08-25 22:13:04 +00002072static int
2073unpack_sequence(v, argcnt, sp)
2074 PyObject *v;
2075 int argcnt;
2076 PyObject **sp;
2077{
2078 int i;
2079 PyObject *w;
2080
2081 for (i = 0; i < argcnt; i++) {
2082 if (! (w = PySequence_GetItem(v, i))) {
2083 if (PyErr_ExceptionMatches(PyExc_IndexError))
2084 PyErr_SetString(PyExc_ValueError,
2085 "unpack sequence of wrong size");
2086 goto finally;
2087 }
2088 *--sp = w;
2089 }
2090 /* we better get an IndexError now */
2091 if (PySequence_GetItem(v, i) == NULL) {
2092 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2093 PyErr_Clear();
2094 return 1;
2095 }
2096 /* some other exception occurred. fall through to finally */
2097 }
2098 else
2099 PyErr_SetString(PyExc_ValueError,
2100 "unpack sequence of wrong size");
2101 /* fall through */
2102finally:
Barry Warsaw91010551997-08-25 22:30:51 +00002103 for (; i > 0; i--, sp++)
2104 Py_DECREF(*sp);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002105
2106 return 0;
2107}
2108
2109
Guido van Rossum96a42c81992-01-12 02:29:51 +00002110#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002111static int
2112prtrace(v, str)
Guido van Rossumb209a111997-04-29 18:18:01 +00002113 PyObject *v;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002114 char *str;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002115{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002116 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002117 if (PyObject_Print(v, stdout, 0) != 0)
2118 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002119 printf("\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002120}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002121#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002122
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002123static void
2124call_exc_trace(p_trace, p_newtrace, f)
Guido van Rossumb209a111997-04-29 18:18:01 +00002125 PyObject **p_trace, **p_newtrace;
2126 PyFrameObject *f;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002127{
Guido van Rossumb209a111997-04-29 18:18:01 +00002128 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002129 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002130 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002131 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002132 value = Py_None;
2133 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002134 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002135 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002136 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002137 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002138 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002139 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002140 err = call_trace(p_trace, p_newtrace, f, "exception", arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002141 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002142 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002143 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002144 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002145 Py_XDECREF(type);
2146 Py_XDECREF(value);
2147 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002148 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002149}
2150
2151static int
2152call_trace(p_trace, p_newtrace, f, msg, arg)
Guido van Rossumb209a111997-04-29 18:18:01 +00002153 PyObject **p_trace; /* in/out; may not be NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002154 may not point to NULL variable initially */
Guido van Rossumb209a111997-04-29 18:18:01 +00002155 PyObject **p_newtrace; /* in/out; may be NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002156 may point to NULL variable;
2157 may be same variable as p_newtrace */
Guido van Rossumb209a111997-04-29 18:18:01 +00002158 PyFrameObject *f;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002159 char *msg;
Guido van Rossumb209a111997-04-29 18:18:01 +00002160 PyObject *arg;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002161{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002162 PyThreadState *tstate = f->f_tstate;
Guido van Rossumb209a111997-04-29 18:18:01 +00002163 PyObject *args, *what;
2164 PyObject *res = NULL;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002165
Guido van Rossuma027efa1997-05-05 20:56:21 +00002166 if (tstate->tracing) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002167 /* Don't do recursive traces */
2168 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002169 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002170 *p_newtrace = NULL;
2171 }
2172 return 0;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002173 }
2174
Guido van Rossumb209a111997-04-29 18:18:01 +00002175 args = PyTuple_New(3);
Guido van Rossumf10570b1995-07-07 22:53:21 +00002176 if (args == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002177 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002178 what = PyString_FromString(msg);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002179 if (what == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002180 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002181 Py_INCREF(f);
2182 PyTuple_SET_ITEM(args, 0, (PyObject *)f);
2183 PyTuple_SET_ITEM(args, 1, what);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002184 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002185 arg = Py_None;
2186 Py_INCREF(arg);
2187 PyTuple_SET_ITEM(args, 2, arg);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002188 tstate->tracing++;
Guido van Rossumb209a111997-04-29 18:18:01 +00002189 PyFrame_FastToLocals(f);
2190 res = PyEval_CallObject(*p_trace, args); /* May clear *p_trace! */
2191 PyFrame_LocalsToFast(f, 1);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002192 tstate->tracing--;
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002193 cleanup:
Guido van Rossumb209a111997-04-29 18:18:01 +00002194 Py_XDECREF(args);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002195 if (res == NULL) {
2196 /* The trace proc raised an exception */
Guido van Rossumb209a111997-04-29 18:18:01 +00002197 PyTraceBack_Here(f);
2198 Py_XDECREF(*p_trace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002199 *p_trace = NULL;
2200 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002201 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002202 *p_newtrace = NULL;
2203 }
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002204 return -1;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002205 }
2206 else {
2207 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002208 Py_XDECREF(*p_newtrace);
2209 if (res == Py_None)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002210 *p_newtrace = NULL;
2211 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002212 Py_INCREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002213 *p_newtrace = res;
2214 }
2215 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002216 Py_DECREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002217 return 0;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002218 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002219}
2220
Guido van Rossumb209a111997-04-29 18:18:01 +00002221PyObject *
2222PyEval_GetBuiltins()
Guido van Rossum6135a871995-01-09 17:53:26 +00002223{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002224 PyThreadState *tstate = PyThreadState_Get();
2225 PyFrameObject *current_frame = tstate->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002226 if (current_frame == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002227 return tstate->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00002228 else
2229 return current_frame->f_builtins;
2230}
2231
Guido van Rossumb209a111997-04-29 18:18:01 +00002232PyObject *
2233PyEval_GetLocals()
Guido van Rossum5b722181993-03-30 17:46:03 +00002234{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002235 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum5b722181993-03-30 17:46:03 +00002236 if (current_frame == NULL)
2237 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002238 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00002239 return current_frame->f_locals;
2240}
2241
Guido van Rossumb209a111997-04-29 18:18:01 +00002242PyObject *
2243PyEval_GetGlobals()
Guido van Rossum3f5da241990-12-20 15:06:42 +00002244{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002245 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002246 if (current_frame == NULL)
2247 return NULL;
2248 else
2249 return current_frame->f_globals;
2250}
2251
Guido van Rossumb209a111997-04-29 18:18:01 +00002252PyObject *
2253PyEval_GetFrame()
Guido van Rossume59214e1994-08-30 08:01:59 +00002254{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002255 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossumb209a111997-04-29 18:18:01 +00002256 return (PyObject *)current_frame;
Guido van Rossume59214e1994-08-30 08:01:59 +00002257}
2258
Guido van Rossum6135a871995-01-09 17:53:26 +00002259int
Guido van Rossumb209a111997-04-29 18:18:01 +00002260PyEval_GetRestricted()
Guido van Rossum6135a871995-01-09 17:53:26 +00002261{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002262 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002263 return current_frame == NULL ? 0 : current_frame->f_restricted;
2264}
2265
Guido van Rossumbe270261997-05-22 22:26:18 +00002266int
Guido van Rossumb209a111997-04-29 18:18:01 +00002267Py_FlushLine()
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002268{
Guido van Rossumb209a111997-04-29 18:18:01 +00002269 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00002270 if (f == NULL)
2271 return 0;
2272 if (!PyFile_SoftSpace(f, 0))
2273 return 0;
2274 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002275}
2276
Guido van Rossum3f5da241990-12-20 15:06:42 +00002277
Guido van Rossum681d79a1995-07-18 14:51:37 +00002278/* External interface to call any callable object.
2279 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002280
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002281#undef PyEval_CallObject
2282/* for backward compatibility: export this interface */
2283
Guido van Rossumb209a111997-04-29 18:18:01 +00002284PyObject *
2285PyEval_CallObject(func, arg)
2286 PyObject *func;
2287 PyObject *arg;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002288{
Guido van Rossumb209a111997-04-29 18:18:01 +00002289 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002290}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002291#define PyEval_CallObject(func,arg) \
2292 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00002293
Guido van Rossumb209a111997-04-29 18:18:01 +00002294PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002295PyEval_CallObjectWithKeywords(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002296 PyObject *func;
2297 PyObject *arg;
2298 PyObject *kw;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002299{
2300 ternaryfunc call;
Guido van Rossumb209a111997-04-29 18:18:01 +00002301 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002302
2303 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002304 arg = PyTuple_New(0);
2305 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002306 PyErr_SetString(PyExc_TypeError,
2307 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002308 return NULL;
2309 }
2310 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002311 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002312
Guido van Rossumb209a111997-04-29 18:18:01 +00002313 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002314 PyErr_SetString(PyExc_TypeError,
2315 "keyword list must be a dictionary");
Guido van Rossume3e61c11995-08-04 04:14:47 +00002316 return NULL;
2317 }
2318
Guido van Rossum150b2df1996-12-05 23:17:11 +00002319 if ((call = func->ob_type->tp_call) != NULL)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002320 result = (*call)(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002321 else if (PyMethod_Check(func) || PyFunction_Check(func))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002322 result = call_function(func, arg, kw);
2323 else
2324 result = call_builtin(func, arg, kw);
2325
Guido van Rossumb209a111997-04-29 18:18:01 +00002326 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002327
Guido van Rossumb209a111997-04-29 18:18:01 +00002328 if (result == NULL && !PyErr_Occurred())
2329 PyErr_SetString(PyExc_SystemError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002330 "NULL result without error in call_object");
Guido van Rossume59214e1994-08-30 08:01:59 +00002331
2332 return result;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002333}
2334
Guido van Rossumb209a111997-04-29 18:18:01 +00002335static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002336call_builtin(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002337 PyObject *func;
2338 PyObject *arg;
2339 PyObject *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002340{
Guido van Rossumb209a111997-04-29 18:18:01 +00002341 if (PyCFunction_Check(func)) {
2342 PyCFunction meth = PyCFunction_GetFunction(func);
2343 PyObject *self = PyCFunction_GetSelf(func);
2344 int flags = PyCFunction_GetFlags(func);
Guido van Rossumff8b4941995-07-26 18:16:42 +00002345 if (!(flags & METH_VARARGS)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002346 int size = PyTuple_Size(arg);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002347 if (size == 1)
Guido van Rossumb209a111997-04-29 18:18:01 +00002348 arg = PyTuple_GET_ITEM(arg, 0);
Guido van Rossum9c7b8611991-12-16 13:04:47 +00002349 else if (size == 0)
2350 arg = NULL;
2351 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002352 if (flags & METH_KEYWORDS)
2353 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002354 if (kw != NULL && PyDict_Size(kw) != 0) {
2355 PyErr_SetString(PyExc_TypeError,
Guido van Rossumff8b4941995-07-26 18:16:42 +00002356 "this function takes no keyword arguments");
2357 return NULL;
2358 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002359 return (*meth)(self, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002360 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002361 if (PyClass_Check(func)) {
2362 return PyInstance_New(func, arg, kw);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002363 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002364 if (PyInstance_Check(func)) {
2365 PyObject *res, *call = PyObject_GetAttrString(func,"__call__");
Guido van Rossume59214e1994-08-30 08:01:59 +00002366 if (call == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002367 PyErr_Clear();
2368 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002369 "no __call__ method defined");
Guido van Rossume59214e1994-08-30 08:01:59 +00002370 return NULL;
2371 }
Guido van Rossumff8b4941995-07-26 18:16:42 +00002372 res = PyEval_CallObjectWithKeywords(call, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002373 Py_DECREF(call);
Guido van Rossume59214e1994-08-30 08:01:59 +00002374 return res;
2375 }
Guido van Rossum2d1ad391998-08-25 18:16:54 +00002376 PyErr_Format(PyExc_TypeError, "call of non-function (type %s)",
2377 func->ob_type->tp_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002378 return NULL;
2379}
2380
Guido van Rossumb209a111997-04-29 18:18:01 +00002381static PyObject *
Guido van Rossum681d79a1995-07-18 14:51:37 +00002382call_function(func, arg, kw)
Guido van Rossumb209a111997-04-29 18:18:01 +00002383 PyObject *func;
2384 PyObject *arg;
2385 PyObject *kw;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002386{
Guido van Rossumb209a111997-04-29 18:18:01 +00002387 PyObject *class = NULL; /* == owner */
2388 PyObject *argdefs;
2389 PyObject **d, **k;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002390 int nk, nd;
Guido van Rossumb209a111997-04-29 18:18:01 +00002391 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002392
Guido van Rossumb209a111997-04-29 18:18:01 +00002393 if (kw != NULL && !PyDict_Check(kw)) {
2394 PyErr_BadInternalCall();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002395 return NULL;
2396 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002397
Guido van Rossumb209a111997-04-29 18:18:01 +00002398 if (PyMethod_Check(func)) {
2399 PyObject *self = PyMethod_Self(func);
2400 class = PyMethod_Class(func);
2401 func = PyMethod_Function(func);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002402 if (self == NULL) {
2403 /* Unbound methods must be called with an instance of
2404 the class (or a derived class) as first argument */
Guido van Rossumb209a111997-04-29 18:18:01 +00002405 if (PyTuple_Size(arg) >= 1) {
2406 self = PyTuple_GET_ITEM(arg, 0);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002407 if (self != NULL &&
Guido van Rossumb209a111997-04-29 18:18:01 +00002408 PyInstance_Check(self) &&
2409 PyClass_IsSubclass((PyObject *)
2410 (((PyInstanceObject *)self)->in_class),
Guido van Rossumb3f72581993-05-21 19:56:10 +00002411 class))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002412 /* Handy-dandy */ ;
Guido van Rossumb3f72581993-05-21 19:56:10 +00002413 else
2414 self = NULL;
2415 }
2416 if (self == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002417 PyErr_SetString(PyExc_TypeError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002418 "unbound method must be called with class instance 1st argument");
Guido van Rossumb3f72581993-05-21 19:56:10 +00002419 return NULL;
2420 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002421 Py_INCREF(arg);
Guido van Rossumb3f72581993-05-21 19:56:10 +00002422 }
2423 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002424 int argcount = PyTuple_Size(arg);
2425 PyObject *newarg = PyTuple_New(argcount + 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002426 int i;
Guido van Rossum81daa321993-05-20 14:24:46 +00002427 if (newarg == NULL)
2428 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002429 Py_INCREF(self);
2430 PyTuple_SET_ITEM(newarg, 0, self);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002431 for (i = 0; i < argcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002432 PyObject *v = PyTuple_GET_ITEM(arg, i);
2433 Py_XINCREF(v);
2434 PyTuple_SET_ITEM(newarg, i+1, v);
Guido van Rossum81daa321993-05-20 14:24:46 +00002435 }
2436 arg = newarg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002437 }
Guido van Rossum7859f871998-07-08 14:58:16 +00002438 if (!PyFunction_Check(func)) {
2439 result = PyEval_CallObjectWithKeywords(func, arg, kw);
2440 Py_DECREF(arg);
2441 return result;
2442 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002443 }
2444 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002445 if (!PyFunction_Check(func)) {
Guido van Rossum2d1ad391998-08-25 18:16:54 +00002446 PyErr_Format(PyExc_TypeError,
2447 "call of non-function (type %s)",
2448 func->ob_type->tp_name);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002449 return NULL;
2450 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002451 Py_INCREF(arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002452 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002453
2454 argdefs = PyFunction_GetDefaults(func);
Guido van Rossumb209a111997-04-29 18:18:01 +00002455 if (argdefs != NULL && PyTuple_Check(argdefs)) {
2456 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
2457 nd = PyTuple_Size(argdefs);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002458 }
2459 else {
2460 d = NULL;
2461 nd = 0;
2462 }
2463
2464 if (kw != NULL) {
2465 int pos, i;
Guido van Rossumb209a111997-04-29 18:18:01 +00002466 nk = PyDict_Size(kw);
2467 k = PyMem_NEW(PyObject *, 2*nk);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002468 if (k == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002469 PyErr_NoMemory();
2470 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002471 return NULL;
Guido van Rossume59214e1994-08-30 08:01:59 +00002472 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002473 pos = i = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002474 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002475 i += 2;
2476 nk = i/2;
2477 /* XXX This is broken if the caller deletes dict items! */
2478 }
2479 else {
2480 k = NULL;
2481 nk = 0;
Guido van Rossume59214e1994-08-30 08:01:59 +00002482 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002483
Guido van Rossum681d79a1995-07-18 14:51:37 +00002484 result = eval_code2(
Guido van Rossumb209a111997-04-29 18:18:01 +00002485 (PyCodeObject *)PyFunction_GetCode(func),
2486 PyFunction_GetGlobals(func), (PyObject *)NULL,
2487 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
Guido van Rossum681d79a1995-07-18 14:51:37 +00002488 k, nk,
2489 d, nd,
2490 class);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002491
Guido van Rossumb209a111997-04-29 18:18:01 +00002492 Py_DECREF(arg);
2493 PyMem_XDEL(k);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002494
Guido van Rossum681d79a1995-07-18 14:51:37 +00002495 return result;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002496}
2497
Guido van Rossum3b9c6671996-07-30 18:40:29 +00002498#define SLICE_ERROR_MSG \
2499 "standard sequence type does not support step size other than one"
2500
Guido van Rossumb209a111997-04-29 18:18:01 +00002501static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002502loop_subscript(v, w)
Guido van Rossumb209a111997-04-29 18:18:01 +00002503 PyObject *v, *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002504{
Guido van Rossumb209a111997-04-29 18:18:01 +00002505 PySequenceMethods *sq = v->ob_type->tp_as_sequence;
Guido van Rossume59214e1994-08-30 08:01:59 +00002506 int i;
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00002507 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002508 PyErr_SetString(PyExc_TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002509 return NULL;
2510 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002511 i = PyInt_AsLong(w);
Guido van Rossume59214e1994-08-30 08:01:59 +00002512 v = (*sq->sq_item)(v, i);
2513 if (v)
2514 return v;
Barry Warsaw4249f541997-08-22 21:26:19 +00002515 if (PyErr_ExceptionMatches(PyExc_IndexError))
Guido van Rossumb209a111997-04-29 18:18:01 +00002516 PyErr_Clear();
Guido van Rossume59214e1994-08-30 08:01:59 +00002517 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002518}
2519
2520static int
Guido van Rossuma027efa1997-05-05 20:56:21 +00002521slice_index(v, pi)
Guido van Rossumb209a111997-04-29 18:18:01 +00002522 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002523 int *pi;
2524{
2525 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002526 long x;
Guido van Rossumb209a111997-04-29 18:18:01 +00002527 if (!PyInt_Check(v)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002528 PyErr_SetString(PyExc_TypeError,
2529 "slice index must be int");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002530 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002531 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002532 x = PyInt_AsLong(v);
2533 /* Truncate -- very long indices are truncated anyway */
2534 if (x > INT_MAX)
2535 x = INT_MAX;
2536 else if (x < -INT_MAX)
2537 x = 0;
2538 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002539 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002540 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002541}
2542
Guido van Rossumb209a111997-04-29 18:18:01 +00002543static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002544apply_slice(u, v, w) /* return u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00002545 PyObject *u, *v, *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002546{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002547 int ilow = 0, ihigh = INT_MAX;
2548 if (slice_index(v, &ilow) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002549 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002550 if (slice_index(w, &ihigh) != 0)
Guido van Rossum04691fc1992-08-12 15:35:34 +00002551 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002552 return PySequence_GetSlice(u, ilow, ihigh);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002553}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002554
2555static int
Guido van Rossum3f5da241990-12-20 15:06:42 +00002556assign_slice(u, v, w, x) /* u[v:w] = x */
Guido van Rossumb209a111997-04-29 18:18:01 +00002557 PyObject *u, *v, *w, *x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002558{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002559 int ilow = 0, ihigh = INT_MAX;
2560 if (slice_index(v, &ilow) != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002561 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002562 if (slice_index(w, &ihigh) != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002563 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002564 if (x == NULL)
2565 return PySequence_DelSlice(u, ilow, ihigh);
2566 else
2567 return PySequence_SetSlice(u, ilow, ihigh, x);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002568}
2569
Guido van Rossumb209a111997-04-29 18:18:01 +00002570static PyObject *
Guido van Rossum3f5da241990-12-20 15:06:42 +00002571cmp_outcome(op, v, w)
Guido van Rossum5b7313a1992-04-06 13:24:57 +00002572 int op;
Guido van Rossumb209a111997-04-29 18:18:01 +00002573 register PyObject *v;
2574 register PyObject *w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002575{
2576 register int cmp;
2577 register int res = 0;
2578 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00002579 case IS:
2580 case IS_NOT:
2581 res = (v == w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002582 if (op == (int) IS_NOT)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002583 res = !res;
2584 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002585 case IN:
2586 case NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00002587 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002588 if (res < 0)
2589 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002590 if (op == (int) NOT_IN)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002591 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002592 break;
2593 case EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00002594 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002595 break;
2596 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00002597 cmp = PyObject_Compare(v, w);
Guido van Rossumc8b6df91997-05-23 00:06:51 +00002598 if (cmp && PyErr_Occurred())
2599 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002600 switch (op) {
2601 case LT: res = cmp < 0; break;
2602 case LE: res = cmp <= 0; break;
2603 case EQ: res = cmp == 0; break;
2604 case NE: res = cmp != 0; break;
2605 case GT: res = cmp > 0; break;
2606 case GE: res = cmp >= 0; break;
2607 /* XXX no default? (res is initialized to 0 though) */
2608 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002609 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002610 v = res ? Py_True : Py_False;
2611 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002612 return v;
2613}
2614
Guido van Rossum3f5da241990-12-20 15:06:42 +00002615static int
2616import_from(locals, v, name)
Guido van Rossumb209a111997-04-29 18:18:01 +00002617 PyObject *locals;
2618 PyObject *v;
2619 PyObject *name;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002620{
Guido van Rossumb209a111997-04-29 18:18:01 +00002621 PyObject *w, *x;
2622 if (!PyModule_Check(v)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002623 PyErr_SetString(PyExc_TypeError,
2624 "import-from requires module object");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002625 return -1;
2626 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002627 w = PyModule_GetDict(v);
2628 if (PyString_AsString(name)[0] == '*') {
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002629 int pos, err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002630 PyObject *name, *value;
Guido van Rossum25831651993-05-19 14:50:45 +00002631 pos = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002632 while (PyDict_Next(w, &pos, &name, &value)) {
2633 if (!PyString_Check(name) ||
2634 PyString_AsString(name)[0] == '_')
Guido van Rossume9736fc1990-11-18 17:33:06 +00002635 continue;
Guido van Rossumb209a111997-04-29 18:18:01 +00002636 Py_INCREF(value);
2637 err = PyDict_SetItem(locals, name, value);
2638 Py_DECREF(value);
Guido van Rossumeb6b33a1993-05-25 09:38:27 +00002639 if (err != 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002640 return -1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002641 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002642 return 0;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002643 }
2644 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002645 x = PyDict_GetItem(w, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002646 if (x == NULL) {
Guido van Rossum98256aa1991-12-24 13:25:19 +00002647 char buf[250];
Guido van Rossum16dfd291992-02-05 11:17:30 +00002648 sprintf(buf, "cannot import name %.230s",
Guido van Rossumb209a111997-04-29 18:18:01 +00002649 PyString_AsString(name));
2650 PyErr_SetString(PyExc_ImportError, buf);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002651 return -1;
2652 }
2653 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002654 return PyDict_SetItem(locals, name, x);
Guido van Rossume9736fc1990-11-18 17:33:06 +00002655 }
2656}
2657
Guido van Rossumb209a111997-04-29 18:18:01 +00002658static PyObject *
Guido van Rossum25831651993-05-19 14:50:45 +00002659build_class(methods, bases, name)
Guido van Rossumb209a111997-04-29 18:18:01 +00002660 PyObject *methods; /* dictionary */
2661 PyObject *bases; /* tuple containing classes */
2662 PyObject *name; /* string */
Guido van Rossume9736fc1990-11-18 17:33:06 +00002663{
Guido van Rossumcd649651997-08-22 16:56:16 +00002664 int i, n;
Guido van Rossumb209a111997-04-29 18:18:01 +00002665 if (!PyTuple_Check(bases)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002666 PyErr_SetString(PyExc_SystemError,
2667 "build_class with non-tuple bases");
Guido van Rossum25831651993-05-19 14:50:45 +00002668 return NULL;
Guido van Rossume9736fc1990-11-18 17:33:06 +00002669 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002670 if (!PyDict_Check(methods)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002671 PyErr_SetString(PyExc_SystemError,
2672 "build_class with non-dictionary");
Guido van Rossume9736fc1990-11-18 17:33:06 +00002673 return NULL;
2674 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002675 if (!PyString_Check(name)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002676 PyErr_SetString(PyExc_SystemError,
2677 "build_class witn non-string name");
Guido van Rossum25831651993-05-19 14:50:45 +00002678 return NULL;
2679 }
Guido van Rossumcd649651997-08-22 16:56:16 +00002680 n = PyTuple_Size(bases);
2681 for (i = 0; i < n; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002682 PyObject *base = PyTuple_GET_ITEM(bases, i);
2683 if (!PyClass_Check(base)) {
Guido van Rossum150b2df1996-12-05 23:17:11 +00002684 /* Call the base's *type*, if it is callable.
2685 This code is a hook for Donald Beaudry's
2686 and Jim Fulton's type extensions. In
2687 unexended Python it will never be triggered
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002688 since its types are not callable.
2689 Ditto: call the bases's *class*, if it has
2690 one. This makes the same thing possible
2691 without writing C code. A true meta-object
2692 protocol! */
2693 PyObject *basetype = (PyObject *)base->ob_type;
2694 PyObject *callable = NULL;
2695 if (PyCallable_Check(basetype))
2696 callable = basetype;
2697 else
2698 callable = PyObject_GetAttrString(
2699 base, "__class__");
2700 if (callable) {
2701 PyObject *args;
2702 PyObject *newclass = NULL;
2703 args = Py_BuildValue(
2704 "(OOO)", name, bases, methods);
2705 if (args != NULL) {
2706 newclass = PyEval_CallObject(
2707 callable, args);
2708 Py_DECREF(args);
2709 }
Guido van Rossum730806d1998-04-10 22:27:42 +00002710 if (callable != basetype) {
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002711 Py_DECREF(callable);
Guido van Rossum730806d1998-04-10 22:27:42 +00002712 }
Guido van Rossum55b9ab51997-07-31 03:54:02 +00002713 return newclass;
Guido van Rossum150b2df1996-12-05 23:17:11 +00002714 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002715 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +00002716 "base is not a class object");
2717 return NULL;
2718 }
2719 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002720 return PyClass_New(bases, methods, name);
Guido van Rossum25831651993-05-19 14:50:45 +00002721}
2722
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002723static int
Guido van Rossuma027efa1997-05-05 20:56:21 +00002724exec_statement(f, prog, globals, locals)
2725 PyFrameObject *f;
Guido van Rossumb209a111997-04-29 18:18:01 +00002726 PyObject *prog;
2727 PyObject *globals;
2728 PyObject *locals;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002729{
2730 char *s;
2731 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00002732 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002733 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002734
Guido van Rossumb209a111997-04-29 18:18:01 +00002735 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
2736 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002737 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00002738 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002739 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00002740 locals = PyTuple_GetItem(prog, 2);
2741 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002742 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002743 if (globals == Py_None) {
2744 globals = PyEval_GetGlobals();
2745 if (locals == Py_None) {
2746 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002747 plain = 1;
2748 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002749 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002750 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002751 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00002752 if (!PyString_Check(prog) &&
2753 !PyCode_Check(prog) &&
2754 !PyFile_Check(prog)) {
2755 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002756 "exec 1st arg must be string, code or file object");
2757 return -1;
2758 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002759 if (!PyDict_Check(globals) || !PyDict_Check(locals)) {
2760 PyErr_SetString(PyExc_TypeError,
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002761 "exec 2nd/3rd args must be dict or None");
2762 return -1;
2763 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002764 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002765 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00002766 if (PyCode_Check(prog)) {
Guido van Rossumdfed7251997-11-11 16:29:38 +00002767 v = PyEval_EvalCode((PyCodeObject *) prog,
2768 globals, locals);
2769 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002770 return -1;
Guido van Rossumdfed7251997-11-11 16:29:38 +00002771 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002772 return 0;
2773 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002774 if (PyFile_Check(prog)) {
2775 FILE *fp = PyFile_AsFile(prog);
2776 char *name = PyString_AsString(PyFile_Name(prog));
Guido van Rossumb05a5c71997-05-07 17:46:13 +00002777 if (PyRun_File(fp, name, Py_file_input,
2778 globals, locals) == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002779 return -1;
2780 return 0;
2781 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002782 s = PyString_AsString(prog);
Guido van Rossum5f15b961997-05-13 17:50:01 +00002783 if ((int)strlen(s) != PyString_Size(prog)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002784 PyErr_SetString(PyExc_ValueError,
2785 "embedded '\\0' in exec string");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002786 return -1;
2787 }
Guido van Rossumb05a5c71997-05-07 17:46:13 +00002788 v = PyRun_String(s, Py_file_input, globals, locals);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002789 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002790 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00002791 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002792 if (plain)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002793 PyFrame_LocalsToFast(f, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002794 return 0;
2795}
Guido van Rossum24c13741995-02-14 09:42:43 +00002796
Guido van Rossum1aa14831997-01-21 05:34:20 +00002797/* Hack for ni.py */
Guido van Rossumb209a111997-04-29 18:18:01 +00002798static PyObject *
Guido van Rossum24c13741995-02-14 09:42:43 +00002799find_from_args(f, nexti)
Guido van Rossumb209a111997-04-29 18:18:01 +00002800 PyFrameObject *f;
Guido van Rossum24c13741995-02-14 09:42:43 +00002801 int nexti;
2802{
2803 int opcode;
2804 int oparg;
Guido van Rossumb209a111997-04-29 18:18:01 +00002805 PyObject *list, *name;
Guido van Rossum24c13741995-02-14 09:42:43 +00002806 unsigned char *next_instr;
2807
Guido van Rossumd076c731998-10-07 19:42:25 +00002808 _PyCode_GETCODEPTR(f->f_code, &next_instr);
2809 next_instr += nexti;
2810
Guido van Rossum24c13741995-02-14 09:42:43 +00002811 opcode = (*next_instr++);
2812 if (opcode != IMPORT_FROM) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002813 Py_INCREF(Py_None);
2814 return Py_None;
Guido van Rossum24c13741995-02-14 09:42:43 +00002815 }
2816
Guido van Rossumb209a111997-04-29 18:18:01 +00002817 list = PyList_New(0);
Guido van Rossum24c13741995-02-14 09:42:43 +00002818 if (list == NULL)
2819 return NULL;
2820
2821 do {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002822 oparg = (next_instr[1]<<8) + next_instr[0];
2823 next_instr += 2;
Guido van Rossum24c13741995-02-14 09:42:43 +00002824 name = Getnamev(f, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002825 if (PyList_Append(list, name) < 0) {
2826 Py_DECREF(list);
Guido van Rossum24c13741995-02-14 09:42:43 +00002827 break;
2828 }
2829 opcode = (*next_instr++);
2830 } while (opcode == IMPORT_FROM);
2831
2832 return list;
2833}
Guido van Rossum950361c1997-01-24 13:49:28 +00002834
2835
2836#ifdef DYNAMIC_EXECUTION_PROFILE
2837
2838PyObject *
2839getarray(a)
2840 long a[256];
2841{
2842 int i;
2843 PyObject *l = PyList_New(256);
2844 if (l == NULL) return NULL;
2845 for (i = 0; i < 256; i++) {
2846 PyObject *x = PyInt_FromLong(a[i]);
2847 if (x == NULL) {
2848 Py_DECREF(l);
2849 return NULL;
2850 }
2851 PyList_SetItem(l, i, x);
2852 }
2853 for (i = 0; i < 256; i++)
2854 a[i] = 0;
2855 return l;
2856}
2857
2858PyObject *
2859_Py_GetDXProfile(self, args)
2860 PyObject *self, *args;
2861{
2862#ifndef DXPAIRS
2863 return getarray(dxp);
2864#else
2865 int i;
2866 PyObject *l = PyList_New(257);
2867 if (l == NULL) return NULL;
2868 for (i = 0; i < 257; i++) {
2869 PyObject *x = getarray(dxpairs[i]);
2870 if (x == NULL) {
2871 Py_DECREF(l);
2872 return NULL;
2873 }
2874 PyList_SetItem(l, i, x);
2875 }
2876 return l;
2877#endif
2878}
2879
2880#endif