blob: 8eaa8bdbf5050f32f00fcb8c063b751aa8bb865a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
5 XXX how to pass arguments to call_trace?
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00007 XXX document it!
8 */
9
Guido van Rossumb209a111997-04-29 18:18:01 +000010#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011
Guido van Rossum10dc2e81990-11-18 17:27:39 +000012#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000013#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000014#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000015#include "opcode.h"
16
Jack Jansencbf630f2000-07-11 21:59:16 +000017#ifdef macintosh
18#include "macglue.h"
19#endif
20
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Guido van Rossum04691fc1992-08-12 15:35:34 +000023/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000024/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000025
Guido van Rossum408027e1996-12-30 16:17:54 +000026#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000027/* For debugging the interpreter: */
28#define LLTRACE 1 /* Low-level trace feature */
29#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#endif
31
Jeremy Hylton52820442001-01-03 23:52:36 +000032typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000033
Guido van Rossum374a9221991-04-04 10:40:29 +000034/* Forward declarations */
Guido van Rossum374a9221991-04-04 10:40:29 +000035
Tim Petersdbd9ba62000-07-09 03:09:57 +000036static PyObject *eval_code2(PyCodeObject *,
37 PyObject *, PyObject *,
38 PyObject **, int,
39 PyObject **, int,
Jeremy Hylton64949cb2001-01-25 20:06:59 +000040 PyObject **, int,
41 PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000042
43static PyObject *call_object(PyObject *, PyObject *, PyObject *);
44static PyObject *call_cfunction(PyObject *, PyObject *, PyObject *);
45static PyObject *call_instance(PyObject *, PyObject *, PyObject *);
46static PyObject *call_method(PyObject *, PyObject *, PyObject *);
47static PyObject *call_eval_code2(PyObject *, PyObject *, PyObject *);
48static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
49static PyObject *fast_cfunction(PyObject *, PyObject ***, int);
50static PyObject *do_call(PyObject *, PyObject ***, int, int);
51static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000052static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000053static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000054static PyObject *load_args(PyObject ***, int);
55#define CALL_FLAG_VAR 1
56#define CALL_FLAG_KW 2
57
Guido van Rossum0a066c01992-03-27 17:29:15 +000058#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000059static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000060#endif
Tim Petersdbd9ba62000-07-09 03:09:57 +000061static void call_exc_trace(PyObject **, PyObject**, PyFrameObject *);
62static int call_trace(PyObject **, PyObject **,
63 PyFrameObject *, char *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000064static PyObject *loop_subscript(PyObject *, PyObject *);
65static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
66static int assign_slice(PyObject *, PyObject *,
67 PyObject *, PyObject *);
68static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000069static PyObject *import_from(PyObject *, PyObject *);
70static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000071static PyObject *build_class(PyObject *, PyObject *, PyObject *);
72static int exec_statement(PyFrameObject *,
73 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000074static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
75static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000076static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000077
Paul Prescode68140d2000-08-30 20:25:01 +000078#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000079 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000080#define GLOBAL_NAME_ERROR_MSG \
81 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000082#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000083 "local variable '%.200s' referenced before assignment"
Guido van Rossum374a9221991-04-04 10:40:29 +000084
Guido van Rossum950361c1997-01-24 13:49:28 +000085/* Dynamic execution profile */
86#ifdef DYNAMIC_EXECUTION_PROFILE
87#ifdef DXPAIRS
88static long dxpairs[257][256];
89#define dxp dxpairs[256]
90#else
91static long dxp[256];
92#endif
93#endif
94
Guido van Rossume59214e1994-08-30 08:01:59 +000095#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +000096
Guido van Rossum2571cc81999-04-07 16:07:23 +000097#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +000098#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +000099#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000100#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000101
Guido van Rossuma027efa1997-05-05 20:56:21 +0000102extern int _PyThread_Started; /* Flag for Py_Exit */
103
Guido van Rossum65d5b571998-12-21 19:32:43 +0000104static PyThread_type_lock interpreter_lock = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000105static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000106
107void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000108PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000109{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000110 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000111 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000112 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000113 interpreter_lock = PyThread_allocate_lock();
114 PyThread_acquire_lock(interpreter_lock, 1);
115 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000116}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000117
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000118void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000119PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000120{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000121 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000122}
123
124void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000126{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000127 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000128}
129
130void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000131PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000132{
133 if (tstate == NULL)
134 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000135 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000136 if (PyThreadState_Swap(tstate) != NULL)
137 Py_FatalError(
138 "PyEval_AcquireThread: non-NULL old thread state");
139}
140
141void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000142PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000143{
144 if (tstate == NULL)
145 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
146 if (PyThreadState_Swap(NULL) != tstate)
147 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000148 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000149}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000150
151/* This function is called from PyOS_AfterFork to ensure that newly
152 created child processes don't hold locks referring to threads which
153 are not running in the child process. (This could also be done using
154 pthread_atfork mechanism, at least for the pthreads implementation.) */
155
156void
157PyEval_ReInitThreads(void)
158{
159 if (!interpreter_lock)
160 return;
161 /*XXX Can't use PyThread_free_lock here because it does too
162 much error-checking. Doing this cleanly would require
163 adding a new function to each thread_*.h. Instead, just
164 create a new lock and waste a little bit of memory */
165 interpreter_lock = PyThread_allocate_lock();
166 PyThread_acquire_lock(interpreter_lock, 1);
167 main_thread = PyThread_get_thread_ident();
168}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000169#endif
170
Guido van Rossumff4949e1992-08-05 19:58:53 +0000171/* Functions save_thread and restore_thread are always defined so
172 dynamically loaded modules needn't be compiled separately for use
173 with and without threads: */
174
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000175PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000176PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000177{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000178 PyThreadState *tstate = PyThreadState_Swap(NULL);
179 if (tstate == NULL)
180 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000181#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000182 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000183 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000184#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000185 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000186}
187
188void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000189PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000190{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000191 if (tstate == NULL)
192 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000193#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000194 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000195 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000196 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000197 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000198 }
199#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000200 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000201}
202
203
Guido van Rossuma9672091994-09-14 13:31:22 +0000204/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
205 signal handlers or Mac I/O completion routines) can schedule calls
206 to a function to be called synchronously.
207 The synchronous function is called with one void* argument.
208 It should return 0 for success or -1 for failure -- failure should
209 be accompanied by an exception.
210
211 If registry succeeds, the registry function returns 0; if it fails
212 (e.g. due to too many pending calls) it returns -1 (without setting
213 an exception condition).
214
215 Note that because registry may occur from within signal handlers,
216 or other asynchronous events, calling malloc() is unsafe!
217
218#ifdef WITH_THREAD
219 Any thread can schedule pending calls, but only the main thread
220 will execute them.
221#endif
222
223 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
224 There are two possible race conditions:
225 (1) nested asynchronous registry calls;
226 (2) registry calls made while pending calls are being processed.
227 While (1) is very unlikely, (2) is a real possibility.
228 The current code is safe against (2), but not against (1).
229 The safety against (2) is derived from the fact that only one
230 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000231
Guido van Rossuma027efa1997-05-05 20:56:21 +0000232 XXX Darn! With the advent of thread state, we should have an array
233 of pending calls per thread in the thread state! Later...
234*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000235
Guido van Rossuma9672091994-09-14 13:31:22 +0000236#define NPENDINGCALLS 32
237static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000238 int (*func)(void *);
239 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000240} pendingcalls[NPENDINGCALLS];
241static volatile int pendingfirst = 0;
242static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000243static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000244
245int
Thomas Wouters334fb892000-07-25 12:56:38 +0000246Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000247{
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
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000270Py_MakePendingCalls(void)
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;
Thomas Wouters334fb892000-07-25 12:56:38 +0000283 int (*func)(void *);
284 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000285 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
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000302/* The interpreter's recursion limit */
303
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000304static int recursion_limit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000305
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000306int
307Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000308{
309 return recursion_limit;
310}
311
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000312void
313Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000314{
315 recursion_limit = new_limit;
316}
317
Guido van Rossum374a9221991-04-04 10:40:29 +0000318/* Status code for main loop (reason for stack unwind) */
319
320enum why_code {
321 WHY_NOT, /* No error */
322 WHY_EXCEPTION, /* Exception occurred */
323 WHY_RERAISE, /* Exception re-raised by 'finally' */
324 WHY_RETURN, /* 'return' statement */
325 WHY_BREAK /* 'break' statement */
326};
327
Tim Petersdbd9ba62000-07-09 03:09:57 +0000328static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
329static int unpack_sequence(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000330
Guido van Rossum374a9221991-04-04 10:40:29 +0000331
Guido van Rossumb209a111997-04-29 18:18:01 +0000332PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000334{
335 return eval_code2(co,
336 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000337 (PyObject **)NULL, 0,
338 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000339 (PyObject **)NULL, 0,
340 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000341}
342
343
344/* Interpreter main loop */
345
Guido van Rossumb209a111997-04-29 18:18:01 +0000346static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000347eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
348 PyObject **args, int argcount, PyObject **kws, int kwcount,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000349 PyObject **defs, int defcount, PyObject *closure)
Guido van Rossum374a9221991-04-04 10:40:29 +0000350{
Guido van Rossum950361c1997-01-24 13:49:28 +0000351#ifdef DXPAIRS
352 int lastopcode = 0;
353#endif
Jeremy Hylton52820442001-01-03 23:52:36 +0000354 PyObject **stack_pointer;
Guido van Rossum374a9221991-04-04 10:40:29 +0000355 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000356 register int opcode=0; /* Current opcode */
357 register int oparg=0; /* Current opcode argument, if any */
Guido van Rossum374a9221991-04-04 10:40:29 +0000358 register enum why_code why; /* Reason for block stack unwind */
359 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000360 register PyObject *x; /* Result object -- NULL if error */
361 register PyObject *v; /* Temporary objects popped off stack */
362 register PyObject *w;
363 register PyObject *u;
364 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000365 register PyObject *stream = NULL; /* for PRINT opcodes */
Guido van Rossumb209a111997-04-29 18:18:01 +0000366 register PyFrameObject *f; /* Current frame */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000367 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000368 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000369 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd076c731998-10-07 19:42:25 +0000370 unsigned char *first_instr;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000371#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000372 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000373#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000374#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000375 /* Make it easier to find out where we are with a debugger */
Guido van Rossumb209a111997-04-29 18:18:01 +0000376 char *filename = PyString_AsString(co->co_filename);
Guido van Rossum99bec951992-09-03 20:29:45 +0000377#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000378
379/* Code access macros */
380
381#define GETCONST(i) Getconst(f, i)
382#define GETNAME(i) Getname(f, i)
383#define GETNAMEV(i) Getnamev(f, i)
Guido van Rossumd076c731998-10-07 19:42:25 +0000384#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000385#define NEXTOP() (*next_instr++)
386#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000387#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000388#define JUMPBY(x) (next_instr += (x))
389
390/* Stack manipulation macros */
391
392#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
393#define EMPTY() (STACK_LEVEL() == 0)
394#define TOP() (stack_pointer[-1])
395#define BASIC_PUSH(v) (*stack_pointer++ = (v))
396#define BASIC_POP() (*--stack_pointer)
397
Guido van Rossum96a42c81992-01-12 02:29:51 +0000398#ifdef LLTRACE
399#define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
400#define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
Guido van Rossum374a9221991-04-04 10:40:29 +0000401#else
402#define PUSH(v) BASIC_PUSH(v)
403#define POP() BASIC_POP()
404#endif
405
Guido van Rossum681d79a1995-07-18 14:51:37 +0000406/* Local variable macros */
407
408#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumb209a111997-04-29 18:18:01 +0000409#define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \
Guido van Rossum681d79a1995-07-18 14:51:37 +0000410 GETLOCAL(i) = value; } while (0)
411
Guido van Rossuma027efa1997-05-05 20:56:21 +0000412/* Start of code */
413
Guido van Rossum8861b741996-07-30 16:49:37 +0000414#ifdef USE_STACKCHECK
Guido van Rossuma027efa1997-05-05 20:56:21 +0000415 if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000416 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
Guido van Rossum8861b741996-07-30 16:49:37 +0000417 return NULL;
418 }
419#endif
420
Guido van Rossum681d79a1995-07-18 14:51:37 +0000421 if (globals == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000422 PyErr_SetString(PyExc_SystemError, "eval_code2: NULL globals");
Guido van Rossum6135a871995-01-09 17:53:26 +0000423 return NULL;
Guido van Rossum5b722181993-03-30 17:46:03 +0000424 }
425
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000426#ifdef LLTRACE
Guido van Rossumb209a111997-04-29 18:18:01 +0000427 lltrace = PyDict_GetItemString(globals, "__lltrace__") != NULL;
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000428#endif
429
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000430 f = PyFrame_New(tstate, /*back*/
Guido van Rossum374a9221991-04-04 10:40:29 +0000431 co, /*code*/
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000432 globals, locals, closure);
Guido van Rossum374a9221991-04-04 10:40:29 +0000433 if (f == NULL)
434 return NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000435
Guido van Rossuma027efa1997-05-05 20:56:21 +0000436 tstate->frame = f;
Guido van Rossumdeb0c5e1997-01-27 23:42:36 +0000437 fastlocals = f->f_localsplus;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000438 freevars = f->f_localsplus + f->f_nlocals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000439
440 if (co->co_argcount > 0 ||
441 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
442 int i;
443 int n = argcount;
Guido van Rossumb209a111997-04-29 18:18:01 +0000444 PyObject *kwdict = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000445 if (co->co_flags & CO_VARKEYWORDS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000446 kwdict = PyDict_New();
Guido van Rossum681d79a1995-07-18 14:51:37 +0000447 if (kwdict == NULL)
448 goto fail;
Guido van Rossum8c5df061997-01-24 04:19:24 +0000449 i = co->co_argcount;
450 if (co->co_flags & CO_VARARGS)
451 i++;
452 SETLOCAL(i, kwdict);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000453 }
454 if (argcount > co->co_argcount) {
455 if (!(co->co_flags & CO_VARARGS)) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000456 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000457 "%.200s() takes %s %d "
458 "%sargument%s (%d given)",
Fred Drake661ea262000-10-24 19:57:45 +0000459 PyString_AsString(co->co_name),
Ka-Ping Yee20579702001-01-15 22:14:16 +0000460 defcount ? "at most" : "exactly",
461 co->co_argcount,
462 kwcount ? "non-keyword " : "",
463 co->co_argcount == 1 ? "" : "s",
464 argcount);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000465 goto fail;
466 }
467 n = co->co_argcount;
468 }
469 for (i = 0; i < n; i++) {
470 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000471 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000472 SETLOCAL(i, x);
473 }
474 if (co->co_flags & CO_VARARGS) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000475 u = PyTuple_New(argcount - n);
Guido van Rossum8c5df061997-01-24 04:19:24 +0000476 if (u == NULL)
477 goto fail;
478 SETLOCAL(co->co_argcount, u);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000479 for (i = n; i < argcount; i++) {
480 x = args[i];
Guido van Rossumb209a111997-04-29 18:18:01 +0000481 Py_INCREF(x);
482 PyTuple_SET_ITEM(u, i-n, x);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000483 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000484 }
485 for (i = 0; i < kwcount; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000486 PyObject *keyword = kws[2*i];
487 PyObject *value = kws[2*i + 1];
Guido van Rossum681d79a1995-07-18 14:51:37 +0000488 int j;
Guido van Rossum25da5be1999-10-26 00:12:20 +0000489 if (keyword == NULL || !PyString_Check(keyword)) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000490 PyErr_Format(PyExc_TypeError,
491 "%.200s() keywords must be strings",
492 PyString_AsString(co->co_name));
Guido van Rossum25da5be1999-10-26 00:12:20 +0000493 goto fail;
494 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000495 /* XXX slow -- speed up using dictionary? */
496 for (j = 0; j < co->co_argcount; j++) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000497 PyObject *nm = PyTuple_GET_ITEM(
498 co->co_varnames, j);
Guido van Rossumac7be682001-01-17 15:42:30 +0000499 int cmp = PyObject_RichCompareBool(
500 keyword, nm, Py_EQ);
501 if (cmp > 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000502 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000503 else if (cmp < 0)
504 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000505 }
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000506 /* Check errors from Compare */
507 if (PyErr_Occurred())
508 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000509 if (j >= co->co_argcount) {
510 if (kwdict == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000511 PyErr_Format(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000512 "%.200s() got an unexpected "
513 "keyword argument '%.400s'",
514 PyString_AsString(co->co_name),
Jeremy Hylton76901512000-03-28 23:49:17 +0000515 PyString_AsString(keyword));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000516 goto fail;
517 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000518 PyDict_SetItem(kwdict, keyword, value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000519 }
520 else {
521 if (GETLOCAL(j) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000522 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000523 "%.200s() got multiple "
524 "values for keyword "
525 "argument '%.400s'",
526 PyString_AsString(co->co_name),
527 PyString_AsString(keyword));
Guido van Rossum681d79a1995-07-18 14:51:37 +0000528 goto fail;
529 }
Guido van Rossumb209a111997-04-29 18:18:01 +0000530 Py_INCREF(value);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000531 SETLOCAL(j, value);
532 }
533 }
534 if (argcount < co->co_argcount) {
535 int m = co->co_argcount - defcount;
536 for (i = argcount; i < m; i++) {
537 if (GETLOCAL(i) == NULL) {
Guido van Rossumdf9db1e1997-11-19 16:05:40 +0000538 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000539 "%.200s() takes %s %d "
540 "%sargument%s (%d given)",
Fred Drake661ea262000-10-24 19:57:45 +0000541 PyString_AsString(co->co_name),
Ka-Ping Yee20579702001-01-15 22:14:16 +0000542 ((co->co_flags & CO_VARARGS) ||
543 defcount) ? "at least"
544 : "exactly",
545 m, kwcount ? "non-keyword " : "",
546 m == 1 ? "" : "s", i);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000547 goto fail;
548 }
549 }
550 if (n > m)
551 i = n - m;
552 else
553 i = 0;
554 for (; i < defcount; i++) {
555 if (GETLOCAL(m+i) == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +0000556 PyObject *def = defs[i];
557 Py_INCREF(def);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000558 SETLOCAL(m+i, def);
559 }
560 }
561 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000562 }
563 else {
564 if (argcount > 0 || kwcount > 0) {
Fred Drake661ea262000-10-24 19:57:45 +0000565 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000566 "%.200s() takes no arguments (%d given)",
567 PyString_AsString(co->co_name),
568 argcount + kwcount);
Guido van Rossum8c5df061997-01-24 04:19:24 +0000569 goto fail;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000570 }
571 }
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000572 /* Allocate storage for cell vars and copy free vars into frame */
573 if (f->f_ncells) {
574 int i;
575 for (i = 0; i < f->f_ncells; ++i)
576 freevars[i] = PyCell_New(NULL);
577 }
578 if (f->f_nfreevars) {
579 int i;
580 for (i = 0; i < f->f_nfreevars; ++i)
581 freevars[f->f_ncells + i] = PyTuple_GET_ITEM(closure, i);
582 }
Guido van Rossum681d79a1995-07-18 14:51:37 +0000583
Guido van Rossuma027efa1997-05-05 20:56:21 +0000584 if (tstate->sys_tracefunc != NULL) {
585 /* tstate->sys_tracefunc, if defined, is a function that
586 will be called on *every* entry to a code block.
Guido van Rossum96a42c81992-01-12 02:29:51 +0000587 Its return value, if not None, is a function that
588 will be called at the start of each executed line
589 of code. (Actually, the function must return
590 itself in order to continue tracing.)
591 The trace functions are called with three arguments:
592 a pointer to the current frame, a string indicating
593 why the function is called, and an argument which
594 depends on the situation. The global trace function
595 (sys.trace) is also called whenever an exception
596 is detected. */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000597 if (call_trace(&tstate->sys_tracefunc,
598 &f->f_trace, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000599 Py_None/*XXX how to compute arguments now?*/)) {
Guido van Rossum96a42c81992-01-12 02:29:51 +0000600 /* Trace function raised an error */
Guido van Rossum8c5df061997-01-24 04:19:24 +0000601 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000602 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000603 }
604
Guido van Rossuma027efa1997-05-05 20:56:21 +0000605 if (tstate->sys_profilefunc != NULL) {
606 /* Similar for sys_profilefunc, except it needn't return
Guido van Rossum9c8d70d1992-03-23 18:19:28 +0000607 itself and isn't called for "line" events */
Guido van Rossuma027efa1997-05-05 20:56:21 +0000608 if (call_trace(&tstate->sys_profilefunc,
609 (PyObject**)0, f, "call",
Guido van Rossumb209a111997-04-29 18:18:01 +0000610 Py_None/*XXX*/)) {
Guido van Rossum8c5df061997-01-24 04:19:24 +0000611 goto fail;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000612 }
613 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000614
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000615 if (++tstate->recursion_depth > recursion_limit) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000616 --tstate->recursion_depth;
617 PyErr_SetString(PyExc_RuntimeError,
Fred Drake661ea262000-10-24 19:57:45 +0000618 "maximum recursion depth exceeded");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000619 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +0000620 Py_DECREF(f);
Guido van Rossum8861b741996-07-30 16:49:37 +0000621 return NULL;
622 }
623
Guido van Rossumd076c731998-10-07 19:42:25 +0000624 _PyCode_GETCODEPTR(co, &first_instr);
625 next_instr = first_instr;
Guido van Rossum374a9221991-04-04 10:40:29 +0000626 stack_pointer = f->f_valuestack;
Guido van Rossumac7be682001-01-17 15:42:30 +0000627
Guido van Rossum374a9221991-04-04 10:40:29 +0000628 why = WHY_NOT;
629 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000630 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000631 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000632
Guido van Rossum374a9221991-04-04 10:40:29 +0000633 for (;;) {
Guido van Rossuma027efa1997-05-05 20:56:21 +0000634 /* Do periodic things. Doing this every time through
635 the loop would add too much overhead, so we do it
636 only every Nth instruction. We also do it if
637 ``things_to_do'' is set, i.e. when an asynchronous
638 event needs attention (e.g. a signal handler or
639 async I/O handler); see Py_AddPendingCall() and
640 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000641
Guido van Rossuma027efa1997-05-05 20:56:21 +0000642 if (things_to_do || --tstate->ticker < 0) {
Guido van Rossum25ce5661997-08-02 03:10:38 +0000643 tstate->ticker = tstate->interp->checkinterval;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000644 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000645 if (Py_MakePendingCalls() < 0) {
646 why = WHY_EXCEPTION;
647 goto on_error;
648 }
649 }
Guido van Rossumdf0d00e1997-05-20 15:57:49 +0000650#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000651 /* If we have true signals, the signal handler
652 will call Py_AddPendingCall() so we don't
653 have to call sigcheck(). On the Mac and
654 DOS, alas, we have to call it. */
Guido van Rossumb209a111997-04-29 18:18:01 +0000655 if (PyErr_CheckSignals()) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000656 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +0000657 goto on_error;
658 }
Guido van Rossum70d44781997-01-21 06:15:24 +0000659#endif
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000660
Guido van Rossume59214e1994-08-30 08:01:59 +0000661#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000662 if (interpreter_lock) {
663 /* Give another thread a chance */
664
Guido van Rossum25ce5661997-08-02 03:10:38 +0000665 if (PyThreadState_Swap(NULL) != tstate)
666 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000667 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000668
669 /* Other threads may run now */
670
Guido van Rossum65d5b571998-12-21 19:32:43 +0000671 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000672 if (PyThreadState_Swap(tstate) != NULL)
673 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000674 }
675#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000676 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000677
Guido van Rossum374a9221991-04-04 10:40:29 +0000678 /* Extract opcode and argument */
Guido van Rossum99bec951992-09-03 20:29:45 +0000679
Guido van Rossum408027e1996-12-30 16:17:54 +0000680#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum99bec951992-09-03 20:29:45 +0000681 f->f_lasti = INSTR_OFFSET();
682#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000683
Guido van Rossum374a9221991-04-04 10:40:29 +0000684 opcode = NEXTOP();
685 if (HAS_ARG(opcode))
686 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000687 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000688#ifdef DYNAMIC_EXECUTION_PROFILE
689#ifdef DXPAIRS
690 dxpairs[lastopcode][opcode]++;
691 lastopcode = opcode;
692#endif
693 dxp[opcode]++;
694#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000695
Guido van Rossum96a42c81992-01-12 02:29:51 +0000696#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000697 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000698
Guido van Rossum96a42c81992-01-12 02:29:51 +0000699 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000700 if (HAS_ARG(opcode)) {
701 printf("%d: %d, %d\n",
702 (int) (INSTR_OFFSET() - 3),
703 opcode, oparg);
704 }
705 else {
706 printf("%d: %d\n",
707 (int) (INSTR_OFFSET() - 1), opcode);
708 }
709 }
710#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000711 /* Main switch on opcode */
Jeremy Hylton52820442001-01-03 23:52:36 +0000712
Guido van Rossum374a9221991-04-04 10:40:29 +0000713 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000714
Guido van Rossum374a9221991-04-04 10:40:29 +0000715 /* BEWARE!
716 It is essential that any operation that fails sets either
717 x to NULL, err to nonzero, or why to anything but WHY_NOT,
718 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000719
Guido van Rossum374a9221991-04-04 10:40:29 +0000720 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000721
Guido van Rossum374a9221991-04-04 10:40:29 +0000722 case POP_TOP:
723 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000724 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000725 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000726
Guido van Rossum374a9221991-04-04 10:40:29 +0000727 case ROT_TWO:
728 v = POP();
729 w = POP();
730 PUSH(v);
731 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000732 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000733
Guido van Rossum374a9221991-04-04 10:40:29 +0000734 case ROT_THREE:
735 v = POP();
736 w = POP();
737 x = POP();
738 PUSH(v);
739 PUSH(x);
740 PUSH(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000741 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000742
Thomas Wouters434d0822000-08-24 20:11:32 +0000743 case ROT_FOUR:
744 u = POP();
745 v = POP();
746 w = POP();
747 x = POP();
748 PUSH(u);
749 PUSH(x);
750 PUSH(w);
751 PUSH(v);
752 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000753
Guido van Rossum374a9221991-04-04 10:40:29 +0000754 case DUP_TOP:
755 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000756 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000757 PUSH(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000758 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +0000759
Thomas Wouters434d0822000-08-24 20:11:32 +0000760 case DUP_TOPX:
761 switch (oparg) {
Thomas Wouters434d0822000-08-24 20:11:32 +0000762 case 1:
Tim Peters35ba6892000-10-11 07:04:49 +0000763 x = TOP();
764 Py_INCREF(x);
765 PUSH(x);
766 continue;
767 case 2:
Thomas Wouters434d0822000-08-24 20:11:32 +0000768 x = POP();
Tim Peters35ba6892000-10-11 07:04:49 +0000769 Py_INCREF(x);
770 w = TOP();
771 Py_INCREF(w);
772 PUSH(x);
773 PUSH(w);
774 PUSH(x);
775 continue;
776 case 3:
777 x = POP();
778 Py_INCREF(x);
Thomas Wouters434d0822000-08-24 20:11:32 +0000779 w = POP();
Tim Peters35ba6892000-10-11 07:04:49 +0000780 Py_INCREF(w);
781 v = TOP();
782 Py_INCREF(v);
783 PUSH(w);
784 PUSH(x);
785 PUSH(v);
786 PUSH(w);
787 PUSH(x);
788 continue;
789 case 4:
790 x = POP();
791 Py_INCREF(x);
792 w = POP();
793 Py_INCREF(w);
Thomas Wouters434d0822000-08-24 20:11:32 +0000794 v = POP();
Tim Peters35ba6892000-10-11 07:04:49 +0000795 Py_INCREF(v);
796 u = TOP();
797 Py_INCREF(u);
798 PUSH(v);
799 PUSH(w);
800 PUSH(x);
801 PUSH(u);
802 PUSH(v);
803 PUSH(w);
804 PUSH(x);
805 continue;
806 case 5:
807 x = POP();
808 Py_INCREF(x);
809 w = POP();
810 Py_INCREF(w);
811 v = POP();
812 Py_INCREF(v);
Thomas Wouters434d0822000-08-24 20:11:32 +0000813 u = POP();
Tim Peters35ba6892000-10-11 07:04:49 +0000814 Py_INCREF(u);
815 t = TOP();
816 Py_INCREF(t);
817 PUSH(u);
818 PUSH(v);
819 PUSH(w);
820 PUSH(x);
821 PUSH(t);
822 PUSH(u);
823 PUSH(v);
824 PUSH(w);
825 PUSH(x);
826 continue;
Thomas Wouters434d0822000-08-24 20:11:32 +0000827 default:
Thomas Wouters0be483f2000-10-11 23:26:11 +0000828 Py_FatalError("invalid argument to DUP_TOPX"
829 " (bytecode corruption?)");
Thomas Wouters434d0822000-08-24 20:11:32 +0000830 }
Tim Peters35ba6892000-10-11 07:04:49 +0000831 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000832
Guido van Rossum374a9221991-04-04 10:40:29 +0000833 case UNARY_POSITIVE:
834 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000835 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000836 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000837 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000838 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000839 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000840
Guido van Rossum374a9221991-04-04 10:40:29 +0000841 case UNARY_NEGATIVE:
842 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000843 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000844 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000845 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000846 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000847 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000848
Guido van Rossum374a9221991-04-04 10:40:29 +0000849 case UNARY_NOT:
850 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000851 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000852 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000853 if (err == 0) {
854 Py_INCREF(Py_True);
855 PUSH(Py_True);
856 continue;
857 }
858 else if (err > 0) {
859 Py_INCREF(Py_False);
860 PUSH(Py_False);
861 err = 0;
862 continue;
863 }
Guido van Rossum374a9221991-04-04 10:40:29 +0000864 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000865
Guido van Rossum374a9221991-04-04 10:40:29 +0000866 case UNARY_CONVERT:
867 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000868 x = PyObject_Repr(v);
869 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000870 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000871 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000872 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000873
Guido van Rossum7928cd71991-10-24 14:59:31 +0000874 case UNARY_INVERT:
875 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000876 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000877 Py_DECREF(v);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000878 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000879 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000880 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000881
Guido van Rossum50564e81996-01-12 01:13:16 +0000882 case BINARY_POWER:
883 w = POP();
884 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000885 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000886 Py_DECREF(v);
887 Py_DECREF(w);
Guido van Rossum50564e81996-01-12 01:13:16 +0000888 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000889 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000890 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000891
Guido van Rossum374a9221991-04-04 10:40:29 +0000892 case BINARY_MULTIPLY:
893 w = POP();
894 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000895 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000896 Py_DECREF(v);
897 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000898 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000899 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000900 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000901
Guido van Rossum374a9221991-04-04 10:40:29 +0000902 case BINARY_DIVIDE:
903 w = POP();
904 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000905 x = PyNumber_Divide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000906 Py_DECREF(v);
907 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000908 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000909 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000910 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000911
Guido van Rossum374a9221991-04-04 10:40:29 +0000912 case BINARY_MODULO:
913 w = POP();
914 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000915 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000916 Py_DECREF(v);
917 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000918 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000919 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000920 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000921
Guido van Rossum374a9221991-04-04 10:40:29 +0000922 case BINARY_ADD:
923 w = POP();
924 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000925 if (PyInt_Check(v) && PyInt_Check(w)) {
926 /* INLINE: int + int */
927 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000928 a = PyInt_AS_LONG(v);
929 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000930 i = a + b;
931 if ((i^a) < 0 && (i^b) < 0) {
932 PyErr_SetString(PyExc_OverflowError,
933 "integer addition");
934 x = NULL;
935 }
936 else
937 x = PyInt_FromLong(i);
938 }
939 else
940 x = PyNumber_Add(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000941 Py_DECREF(v);
942 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000943 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000944 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000945 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000946
Guido van Rossum374a9221991-04-04 10:40:29 +0000947 case BINARY_SUBTRACT:
948 w = POP();
949 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000950 if (PyInt_Check(v) && PyInt_Check(w)) {
951 /* INLINE: int - int */
952 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +0000953 a = PyInt_AS_LONG(v);
954 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +0000955 i = a - b;
956 if ((i^a) < 0 && (i^~b) < 0) {
957 PyErr_SetString(PyExc_OverflowError,
958 "integer subtraction");
959 x = NULL;
960 }
961 else
962 x = PyInt_FromLong(i);
963 }
964 else
965 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000966 Py_DECREF(v);
967 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000968 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000969 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000970 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000971
Guido van Rossum374a9221991-04-04 10:40:29 +0000972 case BINARY_SUBSCR:
973 w = POP();
974 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +0000975 if (PyList_Check(v) && PyInt_Check(w)) {
976 /* INLINE: list[int] */
977 long i = PyInt_AsLong(w);
978 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +0000979 i += PyList_GET_SIZE(v);
Guido van Rossumc12da691997-07-17 23:12:42 +0000980 if (i < 0 ||
Guido van Rossumfa00e951998-07-08 15:02:37 +0000981 i >= PyList_GET_SIZE(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +0000982 PyErr_SetString(PyExc_IndexError,
983 "list index out of range");
984 x = NULL;
985 }
986 else {
Guido van Rossumfa00e951998-07-08 15:02:37 +0000987 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +0000988 Py_INCREF(x);
989 }
990 }
991 else
992 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +0000993 Py_DECREF(v);
994 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +0000995 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000996 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000997 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000998
Guido van Rossum7928cd71991-10-24 14:59:31 +0000999 case BINARY_LSHIFT:
1000 w = POP();
1001 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001002 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001003 Py_DECREF(v);
1004 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001005 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001006 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001007 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001008
Guido van Rossum7928cd71991-10-24 14:59:31 +00001009 case BINARY_RSHIFT:
1010 w = POP();
1011 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001012 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001013 Py_DECREF(v);
1014 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001015 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001016 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001017 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001018
Guido van Rossum7928cd71991-10-24 14:59:31 +00001019 case BINARY_AND:
1020 w = POP();
1021 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001022 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001023 Py_DECREF(v);
1024 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001025 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001026 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001027 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001028
Guido van Rossum7928cd71991-10-24 14:59:31 +00001029 case BINARY_XOR:
1030 w = POP();
1031 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001032 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001033 Py_DECREF(v);
1034 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001035 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001036 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001038
Guido van Rossum7928cd71991-10-24 14:59:31 +00001039 case BINARY_OR:
1040 w = POP();
1041 v = POP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001042 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001043 Py_DECREF(v);
1044 Py_DECREF(w);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001045 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001046 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001047 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001048
1049 case INPLACE_POWER:
1050 w = POP();
1051 v = POP();
1052 x = PyNumber_InPlacePower(v, w, Py_None);
1053 Py_DECREF(v);
1054 Py_DECREF(w);
1055 PUSH(x);
1056 if (x != NULL) continue;
1057 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001058
Thomas Wouters434d0822000-08-24 20:11:32 +00001059 case INPLACE_MULTIPLY:
1060 w = POP();
1061 v = POP();
1062 x = PyNumber_InPlaceMultiply(v, w);
1063 Py_DECREF(v);
1064 Py_DECREF(w);
1065 PUSH(x);
1066 if (x != NULL) continue;
1067 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001068
Thomas Wouters434d0822000-08-24 20:11:32 +00001069 case INPLACE_DIVIDE:
1070 w = POP();
1071 v = POP();
1072 x = PyNumber_InPlaceDivide(v, w);
1073 Py_DECREF(v);
1074 Py_DECREF(w);
1075 PUSH(x);
1076 if (x != NULL) continue;
1077 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001078
Thomas Wouters434d0822000-08-24 20:11:32 +00001079 case INPLACE_MODULO:
1080 w = POP();
1081 v = POP();
1082 x = PyNumber_InPlaceRemainder(v, w);
1083 Py_DECREF(v);
1084 Py_DECREF(w);
1085 PUSH(x);
1086 if (x != NULL) continue;
1087 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001088
Thomas Wouters434d0822000-08-24 20:11:32 +00001089 case INPLACE_ADD:
1090 w = POP();
1091 v = POP();
1092 if (PyInt_Check(v) && PyInt_Check(w)) {
1093 /* INLINE: int + int */
1094 register long a, b, i;
1095 a = PyInt_AS_LONG(v);
1096 b = PyInt_AS_LONG(w);
1097 i = a + b;
1098 if ((i^a) < 0 && (i^b) < 0) {
1099 PyErr_SetString(PyExc_OverflowError,
1100 "integer addition");
1101 x = NULL;
1102 }
1103 else
1104 x = PyInt_FromLong(i);
1105 }
1106 else
1107 x = PyNumber_InPlaceAdd(v, w);
1108 Py_DECREF(v);
1109 Py_DECREF(w);
1110 PUSH(x);
1111 if (x != NULL) continue;
1112 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001113
Thomas Wouters434d0822000-08-24 20:11:32 +00001114 case INPLACE_SUBTRACT:
1115 w = POP();
1116 v = POP();
1117 if (PyInt_Check(v) && PyInt_Check(w)) {
1118 /* INLINE: int - int */
1119 register long a, b, i;
1120 a = PyInt_AS_LONG(v);
1121 b = PyInt_AS_LONG(w);
1122 i = a - b;
1123 if ((i^a) < 0 && (i^~b) < 0) {
1124 PyErr_SetString(PyExc_OverflowError,
1125 "integer subtraction");
1126 x = NULL;
1127 }
1128 else
1129 x = PyInt_FromLong(i);
1130 }
1131 else
1132 x = PyNumber_InPlaceSubtract(v, w);
1133 Py_DECREF(v);
1134 Py_DECREF(w);
1135 PUSH(x);
1136 if (x != NULL) continue;
1137 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001138
Thomas Wouters434d0822000-08-24 20:11:32 +00001139 case INPLACE_LSHIFT:
1140 w = POP();
1141 v = POP();
1142 x = PyNumber_InPlaceLshift(v, w);
1143 Py_DECREF(v);
1144 Py_DECREF(w);
1145 PUSH(x);
1146 if (x != NULL) continue;
1147 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001148
Thomas Wouters434d0822000-08-24 20:11:32 +00001149 case INPLACE_RSHIFT:
1150 w = POP();
1151 v = POP();
1152 x = PyNumber_InPlaceRshift(v, w);
1153 Py_DECREF(v);
1154 Py_DECREF(w);
1155 PUSH(x);
1156 if (x != NULL) continue;
1157 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001158
Thomas Wouters434d0822000-08-24 20:11:32 +00001159 case INPLACE_AND:
1160 w = POP();
1161 v = POP();
1162 x = PyNumber_InPlaceAnd(v, w);
1163 Py_DECREF(v);
1164 Py_DECREF(w);
1165 PUSH(x);
1166 if (x != NULL) continue;
1167 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Thomas Wouters434d0822000-08-24 20:11:32 +00001169 case INPLACE_XOR:
1170 w = POP();
1171 v = POP();
1172 x = PyNumber_InPlaceXor(v, w);
1173 Py_DECREF(v);
1174 Py_DECREF(w);
1175 PUSH(x);
1176 if (x != NULL) continue;
1177 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001178
Thomas Wouters434d0822000-08-24 20:11:32 +00001179 case INPLACE_OR:
1180 w = POP();
1181 v = POP();
1182 x = PyNumber_InPlaceOr(v, w);
1183 Py_DECREF(v);
1184 Py_DECREF(w);
1185 PUSH(x);
1186 if (x != NULL) continue;
1187 break;
1188
Guido van Rossum374a9221991-04-04 10:40:29 +00001189 case SLICE+0:
1190 case SLICE+1:
1191 case SLICE+2:
1192 case SLICE+3:
1193 if ((opcode-SLICE) & 2)
1194 w = POP();
1195 else
1196 w = NULL;
1197 if ((opcode-SLICE) & 1)
1198 v = POP();
1199 else
1200 v = NULL;
1201 u = POP();
1202 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001203 Py_DECREF(u);
1204 Py_XDECREF(v);
1205 Py_XDECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001206 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001207 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001208 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Guido van Rossum374a9221991-04-04 10:40:29 +00001210 case STORE_SLICE+0:
1211 case STORE_SLICE+1:
1212 case STORE_SLICE+2:
1213 case STORE_SLICE+3:
1214 if ((opcode-STORE_SLICE) & 2)
1215 w = POP();
1216 else
1217 w = NULL;
1218 if ((opcode-STORE_SLICE) & 1)
1219 v = POP();
1220 else
1221 v = NULL;
1222 u = POP();
1223 t = POP();
1224 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001225 Py_DECREF(t);
1226 Py_DECREF(u);
1227 Py_XDECREF(v);
1228 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001229 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001230 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001231
Guido van Rossum374a9221991-04-04 10:40:29 +00001232 case DELETE_SLICE+0:
1233 case DELETE_SLICE+1:
1234 case DELETE_SLICE+2:
1235 case DELETE_SLICE+3:
1236 if ((opcode-DELETE_SLICE) & 2)
1237 w = POP();
1238 else
1239 w = NULL;
1240 if ((opcode-DELETE_SLICE) & 1)
1241 v = POP();
1242 else
1243 v = NULL;
1244 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001245 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001246 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001247 Py_DECREF(u);
1248 Py_XDECREF(v);
1249 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001250 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001251 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001252
Guido van Rossum374a9221991-04-04 10:40:29 +00001253 case STORE_SUBSCR:
1254 w = POP();
1255 v = POP();
1256 u = POP();
1257 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001258 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001259 Py_DECREF(u);
1260 Py_DECREF(v);
1261 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001262 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001263 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Guido van Rossum374a9221991-04-04 10:40:29 +00001265 case DELETE_SUBSCR:
1266 w = POP();
1267 v = POP();
1268 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001269 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001270 Py_DECREF(v);
1271 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001272 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001273 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001274
Guido van Rossum374a9221991-04-04 10:40:29 +00001275 case PRINT_EXPR:
1276 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001277 w = PySys_GetObject("displayhook");
1278 if (w == NULL) {
1279 PyErr_SetString(PyExc_RuntimeError,
1280 "lost sys.displayhook");
1281 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001282 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001283 }
1284 if (err == 0) {
1285 x = Py_BuildValue("(O)", v);
1286 if (x == NULL)
1287 err = -1;
1288 }
1289 if (err == 0) {
1290 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001291 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001292 if (w == NULL)
1293 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001294 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001295 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001296 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001297 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001298
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001299 case PRINT_ITEM_TO:
1300 w = stream = POP();
1301 /* fall through to PRINT_ITEM */
1302
Guido van Rossum374a9221991-04-04 10:40:29 +00001303 case PRINT_ITEM:
1304 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001305 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001306 w = PySys_GetObject("stdout");
1307 if (w == NULL) {
1308 PyErr_SetString(PyExc_RuntimeError,
1309 "lost sys.stdout");
1310 err = -1;
1311 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001312 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001313 if (w != NULL && PyFile_SoftSpace(w, 1))
Guido van Rossumbe270261997-05-22 22:26:18 +00001314 err = PyFile_WriteString(" ", w);
1315 if (err == 0)
1316 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Guido van Rossumb209a111997-04-29 18:18:01 +00001317 if (err == 0 && PyString_Check(v)) {
Guido van Rossumc6004111993-11-05 10:22:19 +00001318 /* XXX move into writeobject() ? */
Guido van Rossumb209a111997-04-29 18:18:01 +00001319 char *s = PyString_AsString(v);
1320 int len = PyString_Size(v);
Guido van Rossum7f7f2741995-02-10 17:01:56 +00001321 if (len > 0 &&
1322 isspace(Py_CHARMASK(s[len-1])) &&
Guido van Rossumc6004111993-11-05 10:22:19 +00001323 s[len-1] != ' ')
Guido van Rossumb209a111997-04-29 18:18:01 +00001324 PyFile_SoftSpace(w, 0);
Guido van Rossum374a9221991-04-04 10:40:29 +00001325 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001326 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001327 Py_XDECREF(stream);
1328 stream = NULL;
1329 if (err == 0)
1330 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001331 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001333 case PRINT_NEWLINE_TO:
1334 w = stream = POP();
1335 /* fall through to PRINT_NEWLINE */
1336
Guido van Rossum374a9221991-04-04 10:40:29 +00001337 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001338 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001339 w = PySys_GetObject("stdout");
1340 if (w == NULL)
1341 PyErr_SetString(PyExc_RuntimeError,
1342 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001343 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001344 if (w != NULL) {
1345 err = PyFile_WriteString("\n", w);
1346 if (err == 0)
1347 PyFile_SoftSpace(w, 0);
1348 }
1349 Py_XDECREF(stream);
1350 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001351 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001352
Thomas Wouters434d0822000-08-24 20:11:32 +00001353
1354#ifdef CASE_TOO_BIG
1355 default: switch (opcode) {
1356#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001357 case BREAK_LOOP:
1358 why = WHY_BREAK;
1359 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001360
Guido van Rossumf10570b1995-07-07 22:53:21 +00001361 case RAISE_VARARGS:
1362 u = v = w = NULL;
1363 switch (oparg) {
1364 case 3:
1365 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001366 /* Fallthrough */
1367 case 2:
1368 v = POP(); /* value */
1369 /* Fallthrough */
1370 case 1:
1371 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001372 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001373 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001374 break;
1375 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001376 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001377 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001378 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001379 break;
1380 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001381 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Guido van Rossum374a9221991-04-04 10:40:29 +00001383 case LOAD_LOCALS:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001384 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001385 PyErr_SetString(PyExc_SystemError,
1386 "no locals");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001387 break;
1388 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001389 Py_INCREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001390 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001391 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Guido van Rossum374a9221991-04-04 10:40:29 +00001393 case RETURN_VALUE:
1394 retval = POP();
1395 why = WHY_RETURN;
1396 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001397
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001398 case EXEC_STMT:
1399 w = POP();
1400 v = POP();
1401 u = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001402 err = exec_statement(f, u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001403 Py_DECREF(u);
1404 Py_DECREF(v);
1405 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001406 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Guido van Rossum374a9221991-04-04 10:40:29 +00001408 case POP_BLOCK:
1409 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001410 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 while (STACK_LEVEL() > b->b_level) {
1412 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001413 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001414 }
1415 }
1416 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001417
Guido van Rossum374a9221991-04-04 10:40:29 +00001418 case END_FINALLY:
1419 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001420 if (PyInt_Check(v)) {
1421 why = (enum why_code) PyInt_AsLong(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 if (why == WHY_RETURN)
1423 retval = POP();
1424 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001425 else if (PyString_Check(v) || PyClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001426 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001427 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001428 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001429 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001430 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001431 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001432 else if (v != Py_None) {
1433 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001434 "'finally' pops bad exception");
1435 why = WHY_EXCEPTION;
1436 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001437 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001438 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001439
Guido van Rossum374a9221991-04-04 10:40:29 +00001440 case BUILD_CLASS:
Guido van Rossum25831651993-05-19 14:50:45 +00001441 u = POP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001442 v = POP();
Guido van Rossum25831651993-05-19 14:50:45 +00001443 w = POP();
1444 x = build_class(u, v, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001445 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001446 Py_DECREF(u);
1447 Py_DECREF(v);
1448 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001449 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001450
Guido van Rossum374a9221991-04-04 10:40:29 +00001451 case STORE_NAME:
1452 w = GETNAMEV(oparg);
1453 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001454 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001455 PyErr_Format(PyExc_SystemError,
1456 "no locals found when storing %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001457 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001458 break;
1459 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001460 err = PyDict_SetItem(x, w, v);
1461 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Guido van Rossum374a9221991-04-04 10:40:29 +00001464 case DELETE_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001465 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001466 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001467 PyErr_Format(PyExc_SystemError,
1468 "no locals when deleting %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001469 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001470 break;
1471 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001472 if ((err = PyDict_DelItem(x, w)) != 0)
Guido van Rossumac7be682001-01-17 15:42:30 +00001473 format_exc_check_arg(PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001474 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001476
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001477 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 v = POP();
Barry Warsawe42b18f1997-08-25 22:13:04 +00001479 if (PyTuple_Check(v)) {
1480 if (PyTuple_Size(v) != oparg) {
1481 PyErr_SetString(PyExc_ValueError,
1482 "unpack tuple of wrong size");
1483 why = WHY_EXCEPTION;
1484 }
1485 else {
1486 for (; --oparg >= 0; ) {
1487 w = PyTuple_GET_ITEM(v, oparg);
1488 Py_INCREF(w);
1489 PUSH(w);
1490 }
1491 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001492 }
Barry Warsawe42b18f1997-08-25 22:13:04 +00001493 else if (PyList_Check(v)) {
1494 if (PyList_Size(v) != oparg) {
1495 PyErr_SetString(PyExc_ValueError,
1496 "unpack list of wrong size");
1497 why = WHY_EXCEPTION;
1498 }
1499 else {
1500 for (; --oparg >= 0; ) {
1501 w = PyList_GET_ITEM(v, oparg);
1502 Py_INCREF(w);
1503 PUSH(w);
1504 }
1505 }
1506 }
1507 else if (PySequence_Check(v)) {
1508 if (unpack_sequence(v, oparg,
1509 stack_pointer + oparg))
1510 stack_pointer += oparg;
1511 else
1512 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001513 }
1514 else {
Barry Warsawe42b18f1997-08-25 22:13:04 +00001515 PyErr_SetString(PyExc_TypeError,
1516 "unpack non-sequence");
1517 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00001518 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001519 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001521
Guido van Rossum374a9221991-04-04 10:40:29 +00001522 case STORE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001523 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001524 v = POP();
1525 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001526 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1527 Py_DECREF(v);
1528 Py_DECREF(u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001529 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001530
Guido van Rossum374a9221991-04-04 10:40:29 +00001531 case DELETE_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001532 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001533 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001534 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1535 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001536 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001537 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001539 case STORE_GLOBAL:
1540 w = GETNAMEV(oparg);
1541 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001542 err = PyDict_SetItem(f->f_globals, w, v);
1543 Py_DECREF(v);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001544 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001545
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001546 case DELETE_GLOBAL:
1547 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001548 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001549 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001550 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001551 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001552
Guido van Rossum374a9221991-04-04 10:40:29 +00001553 case LOAD_CONST:
1554 x = GETCONST(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001555 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001556 PUSH(x);
1557 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 case LOAD_NAME:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001560 w = GETNAMEV(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001561 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001562 PyErr_Format(PyExc_SystemError,
1563 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001564 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001565 break;
1566 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001567 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001569 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001570 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001571 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001572 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001573 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001574 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001575 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001576 break;
1577 }
1578 }
1579 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001580 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001581 PUSH(x);
1582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 case LOAD_GLOBAL:
Guido van Rossumdf62e441991-08-16 08:56:04 +00001585 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001586 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001587 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001588 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001589 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001590 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001591 PyExc_NameError,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001592 GLOBAL_NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001593 break;
1594 }
1595 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001596 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001597 PUSH(x);
1598 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001599
Guido van Rossum9bfef441993-03-29 10:43:31 +00001600 case LOAD_FAST:
Guido van Rossum681d79a1995-07-18 14:51:37 +00001601 x = GETLOCAL(oparg);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001602 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001603 format_exc_check_arg(
1604 PyExc_UnboundLocalError,
1605 UNBOUNDLOCAL_ERROR_MSG,
1606 PyTuple_GetItem(co->co_varnames, oparg)
1607 );
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001608 break;
1609 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001610 Py_INCREF(x);
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001611 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001612 if (x != NULL) continue;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001613 break;
1614
1615 case STORE_FAST:
Guido van Rossum25831651993-05-19 14:50:45 +00001616 v = POP();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001617 SETLOCAL(oparg, v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001618 continue;
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001619
1620 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001621 x = GETLOCAL(oparg);
1622 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001623 format_exc_check_arg(
1624 PyExc_UnboundLocalError,
1625 UNBOUNDLOCAL_ERROR_MSG,
1626 PyTuple_GetItem(co->co_varnames, oparg)
1627 );
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001628 break;
1629 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00001630 SETLOCAL(oparg, NULL);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001631 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001632
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001633 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001634 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001635 Py_INCREF(x);
1636 PUSH(x);
1637 break;
1638
1639 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001640 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001641 w = PyCell_Get(x);
1642 Py_INCREF(w);
1643 PUSH(w);
1644 break;
1645
1646 case STORE_DEREF:
1647 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001648 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001649 PyCell_Set(x, w);
1650 continue;
1651
Guido van Rossum374a9221991-04-04 10:40:29 +00001652 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001653 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 if (x != NULL) {
1655 for (; --oparg >= 0;) {
1656 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001657 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 }
1659 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001660 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001661 }
1662 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001663
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001665 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 if (x != NULL) {
1667 for (; --oparg >= 0;) {
1668 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001669 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 }
1671 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001672 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 }
1674 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001675
Guido van Rossum374a9221991-04-04 10:40:29 +00001676 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001677 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001678 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001679 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001680 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001681
Guido van Rossum374a9221991-04-04 10:40:29 +00001682 case LOAD_ATTR:
Guido van Rossum9bfef441993-03-29 10:43:31 +00001683 w = GETNAMEV(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001685 x = PyObject_GetAttr(v, w);
1686 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001687 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001688 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001689 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001690
Guido van Rossum374a9221991-04-04 10:40:29 +00001691 case COMPARE_OP:
1692 w = POP();
1693 v = POP();
Guido van Rossumc12da691997-07-17 23:12:42 +00001694 if (PyInt_Check(v) && PyInt_Check(w)) {
1695 /* INLINE: cmp(int, int) */
1696 register long a, b;
1697 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001698 a = PyInt_AS_LONG(v);
1699 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001700 switch (oparg) {
1701 case LT: res = a < b; break;
1702 case LE: res = a <= b; break;
1703 case EQ: res = a == b; break;
1704 case NE: res = a != b; break;
1705 case GT: res = a > b; break;
1706 case GE: res = a >= b; break;
1707 case IS: res = v == w; break;
1708 case IS_NOT: res = v != w; break;
1709 default: goto slow_compare;
1710 }
1711 x = res ? Py_True : Py_False;
1712 Py_INCREF(x);
1713 }
1714 else {
1715 slow_compare:
1716 x = cmp_outcome(oparg, v, w);
1717 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001718 Py_DECREF(v);
1719 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001721 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Guido van Rossum374a9221991-04-04 10:40:29 +00001724 case IMPORT_NAME:
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001725 w = GETNAMEV(oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001726 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001727 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001728 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001729 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001730 break;
1731 }
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00001732 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001733 w = Py_BuildValue("(OOOO)",
Guido van Rossum681d79a1995-07-18 14:51:37 +00001734 w,
1735 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001736 f->f_locals == NULL ?
1737 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001738 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001739 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001740 if (w == NULL) {
1741 x = NULL;
1742 break;
1743 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 x = PyEval_CallObject(x, w);
1745 Py_DECREF(w);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001746 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001747 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001748 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001749
Thomas Wouters52152252000-08-17 22:55:00 +00001750 case IMPORT_STAR:
1751 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001752 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001753 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001754 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001755 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001756 break;
1757 }
Thomas Wouters52152252000-08-17 22:55:00 +00001758 err = import_all_from(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001759 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001760 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001761 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001762 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001763
Thomas Wouters52152252000-08-17 22:55:00 +00001764 case IMPORT_FROM:
1765 w = GETNAMEV(oparg);
1766 v = TOP();
1767 x = import_from(v, w);
1768 PUSH(x);
1769 if (x != NULL) continue;
1770 break;
1771
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 case JUMP_FORWARD:
1773 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001774 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 case JUMP_IF_FALSE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001777 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001778 if (err > 0)
1779 err = 0;
1780 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00001781 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001782 else
1783 break;
1784 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001785
Guido van Rossum374a9221991-04-04 10:40:29 +00001786 case JUMP_IF_TRUE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001787 err = PyObject_IsTrue(TOP());
Guido van Rossum04691fc1992-08-12 15:35:34 +00001788 if (err > 0) {
1789 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001791 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001792 else if (err == 0)
1793 ;
1794 else
1795 break;
1796 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Guido van Rossum374a9221991-04-04 10:40:29 +00001798 case JUMP_ABSOLUTE:
1799 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001800 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001801
Guido van Rossum374a9221991-04-04 10:40:29 +00001802 case FOR_LOOP:
1803 /* for v in s: ...
1804 On entry: stack contains s, i.
1805 On exit: stack contains s, i+1, s[i];
1806 but if loop exhausted:
1807 s, i are popped, and we jump */
1808 w = POP(); /* Loop index */
1809 v = POP(); /* Sequence object */
1810 u = loop_subscript(v, w);
1811 if (u != NULL) {
1812 PUSH(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001813 x = PyInt_FromLong(PyInt_AsLong(w)+1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001814 PUSH(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001815 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001816 PUSH(u);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001817 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001818 }
1819 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00001820 Py_DECREF(v);
1821 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001822 /* A NULL can mean "s exhausted"
1823 but also an error: */
Guido van Rossumb209a111997-04-29 18:18:01 +00001824 if (PyErr_Occurred())
Guido van Rossum374a9221991-04-04 10:40:29 +00001825 why = WHY_EXCEPTION;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001826 else {
Guido van Rossum374a9221991-04-04 10:40:29 +00001827 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001828 continue;
1829 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001830 }
1831 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001832
Guido van Rossum374a9221991-04-04 10:40:29 +00001833 case SETUP_LOOP:
1834 case SETUP_EXCEPT:
1835 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00001836 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001838 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001839
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 case SET_LINENO:
Guido van Rossum96a42c81992-01-12 02:29:51 +00001841#ifdef LLTRACE
1842 if (lltrace)
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001843 printf("--- %s:%d \n", filename, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001844#endif
Guido van Rossum96a42c81992-01-12 02:29:51 +00001845 f->f_lineno = oparg;
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001846 if (f->f_trace == NULL)
1847 continue;
1848 /* Trace each line of code reached */
1849 f->f_lasti = INSTR_OFFSET();
1850 err = call_trace(&f->f_trace, &f->f_trace,
Guido van Rossumb209a111997-04-29 18:18:01 +00001851 f, "line", Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001853
1854 case CALL_FUNCTION:
Jeremy Hylton52820442001-01-03 23:52:36 +00001855 {
1856 int na = oparg & 0xff;
1857 int nk = (oparg>>8) & 0xff;
1858 int n = na + 2 * nk;
1859 PyObject **pfunc = stack_pointer - n - 1;
1860 PyObject *func = *pfunc;
1861 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
1862
1863 /* Always dispatch PyCFunction first, because
1864 these are presumed to be the most frequent
1865 callable object.
1866 */
1867 if (PyCFunction_Check(func)) {
1868 if (PyCFunction_GET_FLAGS(func) == 0) {
1869 x = fast_cfunction(func,
Guido van Rossumac7be682001-01-17 15:42:30 +00001870 &stack_pointer, na);
Jeremy Hylton52820442001-01-03 23:52:36 +00001871 } else {
1872 x = do_call(func, &stack_pointer,
1873 na, nk);
1874 }
1875 } else {
Guido van Rossumac7be682001-01-17 15:42:30 +00001876 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00001877 && PyMethod_GET_SELF(func) != NULL) {
1878 /* optimize access to bound methods */
1879 PyObject *self = PyMethod_GET_SELF(func);
1880 Py_INCREF(self);
1881 func = PyMethod_GET_FUNCTION(func);
1882 Py_INCREF(func);
1883 Py_DECREF(*pfunc);
1884 *pfunc = self;
1885 na++;
1886 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00001887 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00001888 Py_INCREF(func);
1889 if (PyFunction_Check(func)) {
1890 x = fast_function(func, &stack_pointer,
Guido van Rossumac7be682001-01-17 15:42:30 +00001891 n, na, nk);
Jeremy Hylton52820442001-01-03 23:52:36 +00001892 } else {
1893 x = do_call(func, &stack_pointer,
1894 na, nk);
1895 }
1896 Py_DECREF(func);
1897 }
1898
1899 while (stack_pointer > pfunc) {
1900 w = POP();
1901 Py_DECREF(w);
1902 }
1903 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00001904 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00001905 continue;
1906 break;
1907 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001908
Jeremy Hylton76901512000-03-28 23:49:17 +00001909 case CALL_FUNCTION_VAR:
1910 case CALL_FUNCTION_KW:
1911 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00001912 {
Jeremy Hylton76901512000-03-28 23:49:17 +00001913 int na = oparg & 0xff;
1914 int nk = (oparg>>8) & 0xff;
1915 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00001916 int n = na + 2 * nk;
1917 PyObject **pfunc, *func;
1918 if (flags & CALL_FLAG_VAR)
1919 n++;
1920 if (flags & CALL_FLAG_KW)
1921 n++;
1922 pfunc = stack_pointer - n - 1;
1923 func = *pfunc;
Jeremy Hylton76901512000-03-28 23:49:17 +00001924 f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
Jeremy Hylton52820442001-01-03 23:52:36 +00001925
Guido van Rossumac7be682001-01-17 15:42:30 +00001926 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00001927 && PyMethod_GET_SELF(func) != NULL) {
1928 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00001929 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00001930 func = PyMethod_GET_FUNCTION(func);
1931 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00001932 Py_DECREF(*pfunc);
1933 *pfunc = self;
1934 na++;
1935 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00001936 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00001937 Py_INCREF(func);
1938 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Jeremy Hylton76901512000-03-28 23:49:17 +00001939 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00001940
Jeremy Hylton76901512000-03-28 23:49:17 +00001941 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00001942 w = POP();
1943 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00001944 }
1945 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00001946 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00001947 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00001948 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001949 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Guido van Rossum681d79a1995-07-18 14:51:37 +00001951 case MAKE_FUNCTION:
1952 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00001953 x = PyFunction_New(v, f->f_globals);
1954 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001955 /* XXX Maybe this should be a separate opcode? */
1956 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001958 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001960 x = NULL;
1961 break;
1962 }
1963 while (--oparg >= 0) {
1964 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001965 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001966 }
1967 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001968 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001969 }
1970 PUSH(x);
1971 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00001972
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001973 case MAKE_CLOSURE:
1974 {
1975 int nfree;
1976 v = POP(); /* code object */
1977 x = PyFunction_New(v, f->f_globals);
1978 nfree = PyTuple_GET_SIZE(((PyCodeObject *)v)->co_freevars);
1979 Py_DECREF(v);
1980 /* XXX Maybe this should be a separate opcode? */
1981 if (x != NULL && nfree > 0) {
1982 v = PyTuple_New(nfree);
1983 if (v == NULL) {
1984 Py_DECREF(x);
1985 x = NULL;
1986 break;
1987 }
1988 while (--nfree >= 0) {
1989 w = POP();
1990 PyTuple_SET_ITEM(v, nfree, w);
1991 }
1992 err = PyFunction_SetClosure(x, v);
1993 Py_DECREF(v);
1994 }
1995 if (x != NULL && oparg > 0) {
1996 v = PyTuple_New(oparg);
1997 if (v == NULL) {
1998 Py_DECREF(x);
1999 x = NULL;
2000 break;
2001 }
2002 while (--oparg >= 0) {
2003 w = POP();
2004 PyTuple_SET_ITEM(v, oparg, w);
2005 }
2006 err = PyFunction_SetDefaults(x, v);
2007 Py_DECREF(v);
2008 }
2009 PUSH(x);
2010 break;
2011 }
2012
Guido van Rossum8861b741996-07-30 16:49:37 +00002013 case BUILD_SLICE:
2014 if (oparg == 3)
2015 w = POP();
2016 else
2017 w = NULL;
2018 v = POP();
2019 u = POP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002020 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002021 Py_DECREF(u);
2022 Py_DECREF(v);
2023 Py_XDECREF(w);
Guido van Rossum8861b741996-07-30 16:49:37 +00002024 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002025 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002026 break;
2027
Fred Drakeef8ace32000-08-24 00:32:09 +00002028 case EXTENDED_ARG:
2029 opcode = NEXTOP();
2030 oparg = oparg<<16 | NEXTARG();
2031 goto dispatch_opcode;
2032 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002033
Guido van Rossum374a9221991-04-04 10:40:29 +00002034 default:
2035 fprintf(stderr,
2036 "XXX lineno: %d, opcode: %d\n",
Guido van Rossum96a42c81992-01-12 02:29:51 +00002037 f->f_lineno, opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002038 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002039 why = WHY_EXCEPTION;
2040 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002041
2042#ifdef CASE_TOO_BIG
2043 }
2044#endif
2045
Guido van Rossum374a9221991-04-04 10:40:29 +00002046 } /* switch */
2047
2048 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002049
Guido van Rossum374a9221991-04-04 10:40:29 +00002050 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002051
Guido van Rossum374a9221991-04-04 10:40:29 +00002052 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002053 if (err == 0 && x != NULL) {
2054#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002055 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002056 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002057 fprintf(stderr,
2058 "XXX undetected error\n");
2059 else
2060#endif
2061 continue; /* Normal, fast path */
2062 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002063 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002064 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002065 err = 0;
2066 }
2067
Guido van Rossum374a9221991-04-04 10:40:29 +00002068 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002069
Guido van Rossum374a9221991-04-04 10:40:29 +00002070 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002071 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002072 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002073 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002074 why = WHY_EXCEPTION;
2075 }
2076 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002077#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002078 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002079 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002080 if (PyErr_Occurred()) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002081 fprintf(stderr,
2082 "XXX undetected error (why=%d)\n",
2083 why);
2084 why = WHY_EXCEPTION;
2085 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002086 }
2087#endif
2088
2089 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002090
Guido van Rossum374a9221991-04-04 10:40:29 +00002091 if (why == WHY_EXCEPTION) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00002092 f->f_lasti = INSTR_OFFSET() - 1;
Guido van Rossum374a9221991-04-04 10:40:29 +00002093 if (HAS_ARG(opcode))
Guido van Rossum96a42c81992-01-12 02:29:51 +00002094 f->f_lasti -= 2;
Guido van Rossumb209a111997-04-29 18:18:01 +00002095 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002096
Guido van Rossume59214e1994-08-30 08:01:59 +00002097 if (f->f_trace)
2098 call_exc_trace(&f->f_trace, &f->f_trace, f);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002099 if (tstate->sys_profilefunc)
2100 call_exc_trace(&tstate->sys_profilefunc,
2101 (PyObject**)0, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002102 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002103
Guido van Rossum374a9221991-04-04 10:40:29 +00002104 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002105
Guido van Rossum374a9221991-04-04 10:40:29 +00002106 if (why == WHY_RERAISE)
2107 why = WHY_EXCEPTION;
2108
2109 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002110
Guido van Rossum374a9221991-04-04 10:40:29 +00002111 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002112 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00002113 while (STACK_LEVEL() > b->b_level) {
2114 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002115 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002116 }
2117 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2118 why = WHY_NOT;
2119 JUMPTO(b->b_handler);
2120 break;
2121 }
2122 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002123 (b->b_type == SETUP_EXCEPT &&
2124 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002125 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002126 PyObject *exc, *val, *tb;
2127 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002128 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002129 val = Py_None;
2130 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002131 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002132 /* Make the raw exception data
2133 available to the handler,
2134 so a program can emulate the
2135 Python main loop. Don't do
2136 this for 'finally'. */
2137 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002138 PyErr_NormalizeException(
2139 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002140 set_exc_info(tstate,
2141 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002142 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002143 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002144 PUSH(val);
2145 PUSH(exc);
2146 }
2147 else {
2148 if (why == WHY_RETURN)
2149 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002150 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002151 PUSH(v);
2152 }
2153 why = WHY_NOT;
2154 JUMPTO(b->b_handler);
2155 break;
2156 }
2157 } /* unwind stack */
2158
2159 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002160
Guido van Rossum374a9221991-04-04 10:40:29 +00002161 if (why != WHY_NOT)
2162 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002163
Guido van Rossum374a9221991-04-04 10:40:29 +00002164 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002165
Guido van Rossum374a9221991-04-04 10:40:29 +00002166 /* Pop remaining stack entries */
Guido van Rossumac7be682001-01-17 15:42:30 +00002167
Guido van Rossum374a9221991-04-04 10:40:29 +00002168 while (!EMPTY()) {
2169 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002170 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002171 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002172
Guido van Rossum96a42c81992-01-12 02:29:51 +00002173 if (why != WHY_RETURN)
2174 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002175
Guido van Rossume59214e1994-08-30 08:01:59 +00002176 if (f->f_trace) {
Guido van Rossum96a42c81992-01-12 02:29:51 +00002177 if (why == WHY_RETURN) {
Guido van Rossume59214e1994-08-30 08:01:59 +00002178 if (call_trace(&f->f_trace, &f->f_trace, f,
2179 "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002180 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002181 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002182 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002183 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002184 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002185 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002186
Guido van Rossuma027efa1997-05-05 20:56:21 +00002187 if (tstate->sys_profilefunc && why == WHY_RETURN) {
2188 if (call_trace(&tstate->sys_profilefunc, (PyObject**)0,
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002189 f, "return", retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002190 Py_XDECREF(retval);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002191 retval = NULL;
2192 why = WHY_EXCEPTION;
2193 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002194 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002195
Guido van Rossuma027efa1997-05-05 20:56:21 +00002196 reset_exc_info(tstate);
2197
2198 --tstate->recursion_depth;
Guido van Rossum8c5df061997-01-24 04:19:24 +00002199
2200 fail: /* Jump here from prelude on failure */
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Guido van Rossum374a9221991-04-04 10:40:29 +00002202 /* Restore previous frame and release the current one */
Guido van Rossum681d79a1995-07-18 14:51:37 +00002203
Guido van Rossuma027efa1997-05-05 20:56:21 +00002204 tstate->frame = f->f_back;
Guido van Rossumb209a111997-04-29 18:18:01 +00002205 Py_DECREF(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00002206
Guido van Rossum96a42c81992-01-12 02:29:51 +00002207 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002208}
2209
Guido van Rossuma027efa1997-05-05 20:56:21 +00002210static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002211set_exc_info(PyThreadState *tstate,
2212 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002213{
2214 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002215 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002216
Guido van Rossuma027efa1997-05-05 20:56:21 +00002217 frame = tstate->frame;
2218 if (frame->f_exc_type == NULL) {
2219 /* This frame didn't catch an exception before */
2220 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002221 if (tstate->exc_type == NULL) {
2222 Py_INCREF(Py_None);
2223 tstate->exc_type = Py_None;
2224 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002225 tmp_type = frame->f_exc_type;
2226 tmp_value = frame->f_exc_value;
2227 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002228 Py_XINCREF(tstate->exc_type);
2229 Py_XINCREF(tstate->exc_value);
2230 Py_XINCREF(tstate->exc_traceback);
2231 frame->f_exc_type = tstate->exc_type;
2232 frame->f_exc_value = tstate->exc_value;
2233 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002234 Py_XDECREF(tmp_type);
2235 Py_XDECREF(tmp_value);
2236 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002237 }
2238 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002239 tmp_type = tstate->exc_type;
2240 tmp_value = tstate->exc_value;
2241 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002242 Py_XINCREF(type);
2243 Py_XINCREF(value);
2244 Py_XINCREF(tb);
2245 tstate->exc_type = type;
2246 tstate->exc_value = value;
2247 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002248 Py_XDECREF(tmp_type);
2249 Py_XDECREF(tmp_value);
2250 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002251 /* For b/w compatibility */
2252 PySys_SetObject("exc_type", type);
2253 PySys_SetObject("exc_value", value);
2254 PySys_SetObject("exc_traceback", tb);
2255}
2256
2257static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002258reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002259{
2260 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002261 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002262 frame = tstate->frame;
2263 if (frame->f_exc_type != NULL) {
2264 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002265 tmp_type = tstate->exc_type;
2266 tmp_value = tstate->exc_value;
2267 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002268 Py_XINCREF(frame->f_exc_type);
2269 Py_XINCREF(frame->f_exc_value);
2270 Py_XINCREF(frame->f_exc_traceback);
2271 tstate->exc_type = frame->f_exc_type;
2272 tstate->exc_value = frame->f_exc_value;
2273 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002274 Py_XDECREF(tmp_type);
2275 Py_XDECREF(tmp_value);
2276 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002277 /* For b/w compatibility */
2278 PySys_SetObject("exc_type", frame->f_exc_type);
2279 PySys_SetObject("exc_value", frame->f_exc_value);
2280 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2281 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002282 tmp_type = frame->f_exc_type;
2283 tmp_value = frame->f_exc_value;
2284 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002285 frame->f_exc_type = NULL;
2286 frame->f_exc_value = NULL;
2287 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002288 Py_XDECREF(tmp_type);
2289 Py_XDECREF(tmp_value);
2290 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002291}
2292
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002293/* Logic for the raise statement (too complicated for inlining).
2294 This *consumes* a reference count to each of its arguments. */
Guido van Rossum1aa14831997-01-21 05:34:20 +00002295static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002296do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002297{
Guido van Rossumd295f121998-04-09 21:39:57 +00002298 if (type == NULL) {
2299 /* Reraise */
2300 PyThreadState *tstate = PyThreadState_Get();
2301 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2302 value = tstate->exc_value;
2303 tb = tstate->exc_traceback;
2304 Py_XINCREF(type);
2305 Py_XINCREF(value);
2306 Py_XINCREF(tb);
2307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002308
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002309 /* We support the following forms of raise:
2310 raise <class>, <classinstance>
2311 raise <class>, <argument tuple>
2312 raise <class>, None
2313 raise <class>, <argument>
2314 raise <classinstance>, None
2315 raise <string>, <object>
2316 raise <string>, None
2317
2318 An omitted second argument is the same as None.
2319
2320 In addition, raise <tuple>, <anything> is the same as
2321 raising the tuple's first item (and it better have one!);
2322 this rule is applied recursively.
2323
2324 Finally, an optional third argument can be supplied, which
2325 gives the traceback to be substituted (useful when
2326 re-raising an exception after examining it). */
2327
2328 /* First, check the traceback argument, replacing None with
2329 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002330 if (tb == Py_None) {
2331 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002332 tb = NULL;
2333 }
2334 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002335 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002336 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002337 goto raise_error;
2338 }
2339
2340 /* Next, replace a missing value with None */
2341 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002342 value = Py_None;
2343 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002344 }
2345
2346 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002347 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2348 PyObject *tmp = type;
2349 type = PyTuple_GET_ITEM(type, 0);
2350 Py_INCREF(type);
2351 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002352 }
2353
Barry Warsaw4249f541997-08-22 21:26:19 +00002354 if (PyString_Check(type))
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002355 ;
Barry Warsaw4249f541997-08-22 21:26:19 +00002356
2357 else if (PyClass_Check(type))
2358 PyErr_NormalizeException(&type, &value, &tb);
2359
Guido van Rossumb209a111997-04-29 18:18:01 +00002360 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002361 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002362 if (value != Py_None) {
2363 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002364 "instance exception may not have a separate value");
2365 goto raise_error;
2366 }
2367 else {
2368 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002369 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002370 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002371 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2372 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002373 }
2374 }
2375 else {
2376 /* Not something you can raise. You get an exception
2377 anyway, just not what you specified :-) */
Guido van Rossumb209a111997-04-29 18:18:01 +00002378 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002379 "exceptions must be strings, classes, or instances");
2380 goto raise_error;
2381 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002382 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002383 if (tb == NULL)
2384 return WHY_EXCEPTION;
2385 else
2386 return WHY_RERAISE;
2387 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002388 Py_XDECREF(value);
2389 Py_XDECREF(type);
2390 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002391 return WHY_EXCEPTION;
2392}
2393
Barry Warsawe42b18f1997-08-25 22:13:04 +00002394static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002395unpack_sequence(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002396{
2397 int i;
2398 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002399
Barry Warsawe42b18f1997-08-25 22:13:04 +00002400 for (i = 0; i < argcnt; i++) {
2401 if (! (w = PySequence_GetItem(v, i))) {
2402 if (PyErr_ExceptionMatches(PyExc_IndexError))
2403 PyErr_SetString(PyExc_ValueError,
2404 "unpack sequence of wrong size");
2405 goto finally;
2406 }
2407 *--sp = w;
2408 }
2409 /* we better get an IndexError now */
2410 if (PySequence_GetItem(v, i) == NULL) {
2411 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
2412 PyErr_Clear();
2413 return 1;
2414 }
2415 /* some other exception occurred. fall through to finally */
2416 }
2417 else
2418 PyErr_SetString(PyExc_ValueError,
2419 "unpack sequence of wrong size");
2420 /* fall through */
2421finally:
Barry Warsaw91010551997-08-25 22:30:51 +00002422 for (; i > 0; i--, sp++)
2423 Py_DECREF(*sp);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002424
2425 return 0;
2426}
2427
2428
Guido van Rossum96a42c81992-01-12 02:29:51 +00002429#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002430static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002431prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002432{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002433 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002434 if (PyObject_Print(v, stdout, 0) != 0)
2435 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002436 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00002437 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002438}
Guido van Rossum3f5da241990-12-20 15:06:42 +00002439#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002440
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002441static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002442call_exc_trace(PyObject **p_trace, PyObject **p_newtrace, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002443{
Guido van Rossumb209a111997-04-29 18:18:01 +00002444 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002445 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00002446 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002447 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002448 value = Py_None;
2449 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00002450 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002451 arg = Py_BuildValue("(OOO)", type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002452 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002453 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002454 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002455 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002456 err = call_trace(p_trace, p_newtrace, f, "exception", arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002457 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002458 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00002459 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002460 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002461 Py_XDECREF(type);
2462 Py_XDECREF(value);
2463 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002464 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002465}
2466
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002467/* PyObject **p_trace: in/out; may not be NULL;
2468 may not point to NULL variable initially
2469 PyObject **p_newtrace: in/out; may be NULL;
2470 may point to NULL variable;
2471 may be same variable as p_newtrace */
2472
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002473static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002474call_trace(PyObject **p_trace, PyObject **p_newtrace, PyFrameObject *f,
2475 char *msg, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002476{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002477 PyThreadState *tstate = f->f_tstate;
Guido van Rossumb209a111997-04-29 18:18:01 +00002478 PyObject *args, *what;
2479 PyObject *res = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002480
Guido van Rossuma027efa1997-05-05 20:56:21 +00002481 if (tstate->tracing) {
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002482 /* Don't do recursive traces */
2483 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002484 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002485 *p_newtrace = NULL;
2486 }
2487 return 0;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002489
Guido van Rossumb209a111997-04-29 18:18:01 +00002490 args = PyTuple_New(3);
Guido van Rossumf10570b1995-07-07 22:53:21 +00002491 if (args == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002492 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002493 what = PyString_FromString(msg);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002494 if (what == NULL)
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002495 goto cleanup;
Guido van Rossumb209a111997-04-29 18:18:01 +00002496 Py_INCREF(f);
2497 PyTuple_SET_ITEM(args, 0, (PyObject *)f);
2498 PyTuple_SET_ITEM(args, 1, what);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002499 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002500 arg = Py_None;
2501 Py_INCREF(arg);
2502 PyTuple_SET_ITEM(args, 2, arg);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002503 tstate->tracing++;
Guido van Rossumb209a111997-04-29 18:18:01 +00002504 PyFrame_FastToLocals(f);
2505 res = PyEval_CallObject(*p_trace, args); /* May clear *p_trace! */
2506 PyFrame_LocalsToFast(f, 1);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002507 tstate->tracing--;
Guido van Rossumf9c90c51997-08-05 02:18:01 +00002508 cleanup:
Guido van Rossumb209a111997-04-29 18:18:01 +00002509 Py_XDECREF(args);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002510 if (res == NULL) {
2511 /* The trace proc raised an exception */
Guido van Rossumb209a111997-04-29 18:18:01 +00002512 PyTraceBack_Here(f);
2513 Py_XDECREF(*p_trace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002514 *p_trace = NULL;
2515 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002516 Py_XDECREF(*p_newtrace);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002517 *p_newtrace = NULL;
2518 }
Barry Warsawf6202631999-09-08 16:26:33 +00002519 /* to be extra double plus sure we don't get recursive
2520 * calls inf either tracefunc or profilefunc gets an
2521 * exception, zap the global variables.
2522 */
2523 Py_XDECREF(tstate->sys_tracefunc);
2524 tstate->sys_tracefunc = NULL;
2525 Py_XDECREF(tstate->sys_profilefunc);
2526 tstate->sys_profilefunc = NULL;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002527 return -1;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002528 }
2529 else {
2530 if (p_newtrace) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002531 Py_XDECREF(*p_newtrace);
2532 if (res == Py_None)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002533 *p_newtrace = NULL;
2534 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00002535 Py_INCREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002536 *p_newtrace = res;
2537 }
2538 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002539 Py_DECREF(res);
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002540 return 0;
Guido van Rossumeee3fd41992-04-05 14:18:13 +00002541 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002542}
2543
Guido van Rossumb209a111997-04-29 18:18:01 +00002544PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002545PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00002546{
Guido van Rossum25ce5661997-08-02 03:10:38 +00002547 PyThreadState *tstate = PyThreadState_Get();
2548 PyFrameObject *current_frame = tstate->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002549 if (current_frame == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +00002550 return tstate->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00002551 else
2552 return current_frame->f_builtins;
2553}
2554
Guido van Rossumb209a111997-04-29 18:18:01 +00002555PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002556PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00002557{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002558 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum5b722181993-03-30 17:46:03 +00002559 if (current_frame == NULL)
2560 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00002561 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00002562 return current_frame->f_locals;
2563}
2564
Guido van Rossumb209a111997-04-29 18:18:01 +00002565PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002566PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002567{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002568 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002569 if (current_frame == NULL)
2570 return NULL;
2571 else
2572 return current_frame->f_globals;
2573}
2574
Guido van Rossumb209a111997-04-29 18:18:01 +00002575PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002576PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00002577{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002578 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossumb209a111997-04-29 18:18:01 +00002579 return (PyObject *)current_frame;
Guido van Rossume59214e1994-08-30 08:01:59 +00002580}
2581
Guido van Rossum6135a871995-01-09 17:53:26 +00002582int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002583PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00002584{
Guido van Rossuma027efa1997-05-05 20:56:21 +00002585 PyFrameObject *current_frame = PyThreadState_Get()->frame;
Guido van Rossum6135a871995-01-09 17:53:26 +00002586 return current_frame == NULL ? 0 : current_frame->f_restricted;
2587}
2588
Guido van Rossumbe270261997-05-22 22:26:18 +00002589int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002590Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002591{
Guido van Rossumb209a111997-04-29 18:18:01 +00002592 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00002593 if (f == NULL)
2594 return 0;
2595 if (!PyFile_SoftSpace(f, 0))
2596 return 0;
2597 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002598}
2599
Guido van Rossum3f5da241990-12-20 15:06:42 +00002600
Guido van Rossum681d79a1995-07-18 14:51:37 +00002601/* External interface to call any callable object.
2602 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002603
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002604#undef PyEval_CallObject
2605/* for backward compatibility: export this interface */
2606
Guido van Rossumb209a111997-04-29 18:18:01 +00002607PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002608PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002609{
Guido van Rossumb209a111997-04-29 18:18:01 +00002610 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002611}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00002612#define PyEval_CallObject(func,arg) \
2613 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00002614
Guido van Rossumb209a111997-04-29 18:18:01 +00002615PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002616PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00002617{
Jeremy Hylton52820442001-01-03 23:52:36 +00002618 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002619
2620 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00002621 arg = PyTuple_New(0);
2622 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002623 PyErr_SetString(PyExc_TypeError,
2624 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002625 return NULL;
2626 }
2627 else
Guido van Rossumb209a111997-04-29 18:18:01 +00002628 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002629
Guido van Rossumb209a111997-04-29 18:18:01 +00002630 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002631 PyErr_SetString(PyExc_TypeError,
2632 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00002633 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00002634 return NULL;
2635 }
2636
Jeremy Hylton52820442001-01-03 23:52:36 +00002637 result = call_object(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00002638 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00002639 return result;
2640}
2641
2642/* How often is each kind of object called? The answer depends on the
2643 program. An instrumented call_object() was used to run the Python
2644 regression test suite. The results were:
2645 4200000 PyCFunctions
2646 390000 fast_function() calls
2647 94000 other functions
2648 480000 all functions (sum of prev two)
2649 150000 methods
2650 100000 classes
2651
2652 Tests on other bodies of code show that PyCFunctions are still
2653 most common, but not by such a large margin.
2654*/
2655
2656static PyObject *
2657call_object(PyObject *func, PyObject *arg, PyObject *kw)
2658{
2659 ternaryfunc call;
2660 PyObject *result;
2661
2662 if (PyMethod_Check(func))
2663 result = call_method(func, arg, kw);
2664 else if (PyFunction_Check(func))
2665 result = call_eval_code2(func, arg, kw);
2666 else if (PyCFunction_Check(func))
2667 result = call_cfunction(func, arg, kw);
2668 else if (PyClass_Check(func))
2669 result = PyInstance_New(func, arg, kw);
2670 else if (PyInstance_Check(func))
2671 result = call_instance(func, arg, kw);
2672 else if ((call = func->ob_type->tp_call) != NULL)
2673 result = (*call)(func, arg, kw);
2674 else {
2675 PyErr_Format(PyExc_TypeError, "object is not callable: %s",
2676 PyString_AS_STRING(PyObject_Repr(func)));
2677 return NULL;
2678 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002679 if (result == NULL && !PyErr_Occurred())
2680 PyErr_SetString(PyExc_SystemError,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002681 "NULL result without error in call_object");
Guido van Rossumac7be682001-01-17 15:42:30 +00002682
Guido van Rossume59214e1994-08-30 08:01:59 +00002683 return result;
Guido van Rossum83bf35c1991-07-27 21:32:34 +00002684}
2685
Guido van Rossumb209a111997-04-29 18:18:01 +00002686static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00002687call_cfunction(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002688{
Jeremy Hylton52820442001-01-03 23:52:36 +00002689 PyCFunctionObject* f = (PyCFunctionObject*)func;
2690 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
2691 PyObject *self = PyCFunction_GET_SELF(func);
2692 int flags = PyCFunction_GET_FLAGS(func);
2693
Jeremy Hylton52820442001-01-03 23:52:36 +00002694 if (flags & METH_KEYWORDS) {
Fred Drake1a7aab72001-01-04 22:33:02 +00002695 if (kw == NULL) {
2696 static PyObject *dict = NULL;
2697 if (dict == NULL) {
2698 dict = PyDict_New();
2699 if (dict == NULL)
2700 return NULL;
2701 }
2702 kw = dict;
2703 Py_INCREF(dict);
2704 }
Jeremy Hylton52820442001-01-03 23:52:36 +00002705 return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002706 }
Jeremy Hylton52820442001-01-03 23:52:36 +00002707 if (kw != NULL && PyDict_Size(kw) != 0) {
2708 PyErr_Format(PyExc_TypeError,
2709 "%.200s() takes no keyword arguments",
2710 f->m_ml->ml_name);
2711 return NULL;
2712 }
Fred Drake1a7aab72001-01-04 22:33:02 +00002713 if (flags & METH_VARARGS) {
2714 return (*meth)(self, arg);
2715 }
2716 if (!(flags & METH_VARARGS)) {
2717 /* the really old style */
2718 int size = PyTuple_GET_SIZE(arg);
2719 if (size == 1)
2720 arg = PyTuple_GET_ITEM(arg, 0);
2721 else if (size == 0)
2722 arg = NULL;
2723 return (*meth)(self, arg);
2724 }
Jeremy Hylton52820442001-01-03 23:52:36 +00002725 /* should never get here ??? */
2726 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002727 return NULL;
2728}
2729
Guido van Rossumb209a111997-04-29 18:18:01 +00002730static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00002731call_instance(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002732{
Jeremy Hylton52820442001-01-03 23:52:36 +00002733 PyObject *res, *call = PyObject_GetAttrString(func, "__call__");
2734 if (call == NULL) {
2735 PyInstanceObject *inst = (PyInstanceObject*) func;
2736 PyErr_Clear();
2737 PyErr_Format(PyExc_AttributeError,
2738 "%.200s instance has no __call__ method",
2739 PyString_AsString(inst->in_class->cl_name));
Guido van Rossum681d79a1995-07-18 14:51:37 +00002740 return NULL;
2741 }
Jeremy Hylton52820442001-01-03 23:52:36 +00002742 res = call_object(call, arg, kw);
2743 Py_DECREF(call);
2744 return res;
2745}
2746
2747static PyObject *
2748call_method(PyObject *func, PyObject *arg, PyObject *kw)
2749{
2750 PyObject *self = PyMethod_GET_SELF(func);
2751 PyObject *class = PyMethod_GET_CLASS(func);
2752 PyObject *result;
2753
2754 func = PyMethod_GET_FUNCTION(func);
2755 if (self == NULL) {
2756 /* Unbound methods must be called with an instance of
2757 the class (or a derived class) as first argument */
2758 if (PyTuple_Size(arg) >= 1)
2759 self = PyTuple_GET_ITEM(arg, 0);
2760 if (!(self != NULL && PyInstance_Check(self)
2761 && PyClass_IsSubclass((PyObject *)
2762 (((PyInstanceObject *)self)->in_class),
2763 class))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00002764 PyObject* fn = ((PyFunctionObject*) func)->func_name;
2765 PyErr_Format(PyExc_TypeError,
2766 "unbound method %s%smust be "
2767 "called with instance as first argument",
2768 fn ? PyString_AsString(fn) : "",
2769 fn ? "() " : "");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002770 return NULL;
2771 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002772 Py_INCREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00002773 } else {
2774 int argcount = PyTuple_Size(arg);
2775 PyObject *newarg = PyTuple_New(argcount + 1);
2776 int i;
2777 if (newarg == NULL)
2778 return NULL;
2779 Py_INCREF(self);
2780 PyTuple_SET_ITEM(newarg, 0, self);
2781 for (i = 0; i < argcount; i++) {
2782 PyObject *v = PyTuple_GET_ITEM(arg, i);
2783 Py_XINCREF(v);
2784 PyTuple_SET_ITEM(newarg, i+1, v);
2785 }
2786 arg = newarg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002787 }
Jeremy Hylton52820442001-01-03 23:52:36 +00002788 result = call_object(func, arg, kw);
2789 Py_DECREF(arg);
2790 return result;
2791}
2792
2793static PyObject *
2794call_eval_code2(PyObject *func, PyObject *arg, PyObject *kw)
2795{
2796 PyObject *result;
2797 PyObject *argdefs;
2798 PyObject **d, **k;
2799 int nk, nd;
2800
2801 argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossumb209a111997-04-29 18:18:01 +00002802 if (argdefs != NULL && PyTuple_Check(argdefs)) {
2803 d = &PyTuple_GET_ITEM((PyTupleObject *)argdefs, 0);
2804 nd = PyTuple_Size(argdefs);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002805 }
2806 else {
2807 d = NULL;
2808 nd = 0;
2809 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002810
Guido van Rossum681d79a1995-07-18 14:51:37 +00002811 if (kw != NULL) {
2812 int pos, i;
Guido van Rossumb209a111997-04-29 18:18:01 +00002813 nk = PyDict_Size(kw);
2814 k = PyMem_NEW(PyObject *, 2*nk);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002815 if (k == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002816 PyErr_NoMemory();
2817 Py_DECREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002818 return NULL;
Guido van Rossume59214e1994-08-30 08:01:59 +00002819 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002820 pos = i = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00002821 while (PyDict_Next(kw, &pos, &k[i], &k[i+1]))
Guido van Rossum681d79a1995-07-18 14:51:37 +00002822 i += 2;
2823 nk = i/2;
2824 /* XXX This is broken if the caller deletes dict items! */
2825 }
2826 else {
2827 k = NULL;
2828 nk = 0;
Guido van Rossume59214e1994-08-30 08:01:59 +00002829 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002830
Guido van Rossum681d79a1995-07-18 14:51:37 +00002831 result = eval_code2(
Jeremy Hylton52820442001-01-03 23:52:36 +00002832 (PyCodeObject *)PyFunction_GET_CODE(func),
2833 PyFunction_GET_GLOBALS(func), (PyObject *)NULL,
Guido van Rossumb209a111997-04-29 18:18:01 +00002834 &PyTuple_GET_ITEM(arg, 0), PyTuple_Size(arg),
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002835 k, nk, d, nd,
2836 PyFunction_GET_CLOSURE(func));
Guido van Rossumac7be682001-01-17 15:42:30 +00002837
Guido van Rossumb18618d2000-05-03 23:44:39 +00002838 if (k != NULL)
2839 PyMem_DEL(k);
Guido van Rossumac7be682001-01-17 15:42:30 +00002840
Guido van Rossum681d79a1995-07-18 14:51:37 +00002841 return result;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002842}
2843
Jeremy Hylton52820442001-01-03 23:52:36 +00002844#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
2845
2846/* The two fast_xxx() functions optimize calls for which no argument
2847 tuple is necessary; the objects are passed directly from the stack.
2848 fast_cfunction() is called for METH_OLDARGS functions.
2849 fast_function() is for functions with no special argument handling.
2850*/
2851
2852static PyObject *
2853fast_cfunction(PyObject *func, PyObject ***pp_stack, int na)
2854{
2855 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
2856 PyObject *self = PyCFunction_GET_SELF(func);
2857
2858 if (na == 0)
2859 return (*meth)(self, NULL);
Charles G. Waldmaneec72a72001-01-10 22:11:59 +00002860 else if (na == 1) {
2861 PyObject *arg = EXT_POP(*pp_stack);
Guido van Rossumac7be682001-01-17 15:42:30 +00002862 PyObject *result = (*meth)(self, arg);
Charles G. Waldmaneec72a72001-01-10 22:11:59 +00002863 Py_DECREF(arg);
2864 return result;
2865 } else {
Jeremy Hylton52820442001-01-03 23:52:36 +00002866 PyObject *args = load_args(pp_stack, na);
2867 PyObject *result = (*meth)(self, args);
2868 Py_DECREF(args);
2869 return result;
2870 }
2871}
2872
2873static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00002874fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00002875{
2876 PyObject *co = PyFunction_GET_CODE(func);
2877 PyObject *globals = PyFunction_GET_GLOBALS(func);
2878 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002879 PyObject *closure = PyFunction_GET_CLOSURE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002880 PyObject **d = NULL;
2881 int nd = 0;
2882
2883 if (argdefs != NULL) {
2884 d = &PyTuple_GET_ITEM(argdefs, 0);
2885 nd = ((PyTupleObject *)argdefs)->ob_size;
2886 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002887 return eval_code2((PyCodeObject *)co, globals,
Jeremy Hylton52820442001-01-03 23:52:36 +00002888 (PyObject *)NULL, (*pp_stack)-n, na,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002889 (*pp_stack)-2*nk, nk, d, nd,
2890 closure);
Jeremy Hylton52820442001-01-03 23:52:36 +00002891}
2892
2893static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00002894update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
2895 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002896{
2897 PyObject *kwdict = NULL;
2898 if (orig_kwdict == NULL)
2899 kwdict = PyDict_New();
2900 else {
2901 kwdict = PyDict_Copy(orig_kwdict);
2902 Py_DECREF(orig_kwdict);
2903 }
2904 if (kwdict == NULL)
2905 return NULL;
2906 while (--nk >= 0) {
2907 int err;
2908 PyObject *value = EXT_POP(*pp_stack);
2909 PyObject *key = EXT_POP(*pp_stack);
2910 if (PyDict_GetItem(kwdict, key) != NULL) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00002911 PyObject* fn = ((PyFunctionObject*) func)->func_name;
Guido van Rossumac7be682001-01-17 15:42:30 +00002912 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00002913 "%.200s%s got multiple values "
2914 "for keyword argument '%.400s'",
2915 fn ? PyString_AsString(fn) : "function",
2916 fn ? "()" : "", PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00002917 Py_DECREF(key);
2918 Py_DECREF(value);
2919 Py_DECREF(kwdict);
2920 return NULL;
2921 }
2922 err = PyDict_SetItem(kwdict, key, value);
2923 Py_DECREF(key);
2924 Py_DECREF(value);
2925 if (err) {
2926 Py_DECREF(kwdict);
2927 return NULL;
2928 }
2929 }
2930 return kwdict;
2931}
2932
2933static PyObject *
2934update_star_args(int nstack, int nstar, PyObject *stararg,
2935 PyObject ***pp_stack)
2936{
2937 PyObject *callargs, *w;
2938
2939 callargs = PyTuple_New(nstack + nstar);
2940 if (callargs == NULL) {
2941 return NULL;
2942 }
2943 if (nstar) {
2944 int i;
2945 for (i = 0; i < nstar; i++) {
2946 PyObject *a = PyTuple_GET_ITEM(stararg, i);
2947 Py_INCREF(a);
2948 PyTuple_SET_ITEM(callargs, nstack + i, a);
2949 }
2950 }
2951 while (--nstack >= 0) {
2952 w = EXT_POP(*pp_stack);
2953 PyTuple_SET_ITEM(callargs, nstack, w);
2954 }
2955 return callargs;
2956}
2957
2958static PyObject *
2959load_args(PyObject ***pp_stack, int na)
2960{
2961 PyObject *args = PyTuple_New(na);
2962 PyObject *w;
2963
2964 if (args == NULL)
2965 return NULL;
2966 while (--na >= 0) {
2967 w = EXT_POP(*pp_stack);
2968 PyTuple_SET_ITEM(args, na, w);
2969 }
2970 return args;
2971}
2972
2973static PyObject *
2974do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
2975{
2976 PyObject *callargs = NULL;
2977 PyObject *kwdict = NULL;
2978 PyObject *result = NULL;
2979
2980 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00002981 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002982 if (kwdict == NULL)
2983 goto call_fail;
2984 }
2985 callargs = load_args(pp_stack, na);
2986 if (callargs == NULL)
2987 goto call_fail;
2988 result = call_object(func, callargs, kwdict);
2989 call_fail:
2990 Py_XDECREF(callargs);
2991 Py_XDECREF(kwdict);
2992 return result;
2993}
2994
2995static PyObject *
2996ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
2997{
2998 int nstar = 0;
2999 PyObject *callargs = NULL;
3000 PyObject *stararg = NULL;
3001 PyObject *kwdict = NULL;
3002 PyObject *result = NULL;
3003
3004 if (flags & CALL_FLAG_KW) {
3005 kwdict = EXT_POP(*pp_stack);
3006 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003007 PyObject* fn = ((PyFunctionObject*) func)->func_name;
3008 PyErr_Format(PyExc_TypeError,
3009 "%s%s argument after ** must be a dictionary",
3010 fn ? PyString_AsString(fn) : "function",
3011 fn ? "()" : "");
Jeremy Hylton52820442001-01-03 23:52:36 +00003012 goto ext_call_fail;
3013 }
3014 }
3015 if (flags & CALL_FLAG_VAR) {
3016 stararg = EXT_POP(*pp_stack);
3017 if (!PyTuple_Check(stararg)) {
3018 PyObject *t = NULL;
3019 t = PySequence_Tuple(stararg);
3020 if (t == NULL) {
3021 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003022 PyObject* fn =
3023 ((PyFunctionObject*) func)->func_name;
3024 PyErr_Format(PyExc_TypeError,
3025 "%s%s argument after * must be a sequence",
3026 fn ? PyString_AsString(fn) : "function",
3027 fn ? "()" : "");
Jeremy Hylton52820442001-01-03 23:52:36 +00003028 }
3029 goto ext_call_fail;
3030 }
3031 Py_DECREF(stararg);
3032 stararg = t;
3033 }
3034 nstar = PyTuple_GET_SIZE(stararg);
3035 }
3036 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003037 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003038 if (kwdict == NULL)
3039 goto ext_call_fail;
3040 }
3041 callargs = update_star_args(na, nstar, stararg, pp_stack);
3042 if (callargs == NULL)
3043 goto ext_call_fail;
3044 result = call_object(func, callargs, kwdict);
3045 ext_call_fail:
3046 Py_XDECREF(callargs);
3047 Py_XDECREF(kwdict);
3048 Py_XDECREF(stararg);
3049 return result;
3050}
3051
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003052#define SLICE_ERROR_MSG \
3053 "standard sequence type does not support step size other than one"
3054
Guido van Rossumb209a111997-04-29 18:18:01 +00003055static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003056loop_subscript(PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003057{
Guido van Rossumb209a111997-04-29 18:18:01 +00003058 PySequenceMethods *sq = v->ob_type->tp_as_sequence;
Guido van Rossume59214e1994-08-30 08:01:59 +00003059 int i;
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003060 if (sq == NULL || sq->sq_item == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003061 PyErr_SetString(PyExc_TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003062 return NULL;
3063 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003064 i = PyInt_AsLong(w);
Guido van Rossume59214e1994-08-30 08:01:59 +00003065 v = (*sq->sq_item)(v, i);
3066 if (v)
3067 return v;
Barry Warsaw4249f541997-08-22 21:26:19 +00003068 if (PyErr_ExceptionMatches(PyExc_IndexError))
Guido van Rossumb209a111997-04-29 18:18:01 +00003069 PyErr_Clear();
Guido van Rossume59214e1994-08-30 08:01:59 +00003070 return NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003071}
3072
Guido van Rossum20c6add2000-05-08 14:06:50 +00003073/* Extract a slice index from a PyInt or PyLong, the index is bound to
3074 the range [-INT_MAX+1, INTMAX]. Returns 0 and an exception if there is
3075 and error. Returns 1 on success.*/
3076
3077int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003078_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003079{
3080 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003081 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003082 if (PyInt_Check(v)) {
3083 x = PyInt_AsLong(v);
3084 } else if (PyLong_Check(v)) {
3085 x = PyLong_AsLong(v);
3086 if (x==-1 && PyErr_Occurred()) {
3087 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003088 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003089
Guido van Rossumac7be682001-01-17 15:42:30 +00003090 if (!PyErr_ExceptionMatches(
3091 PyExc_OverflowError)) {
3092 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003093 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003094 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003095 }
3096
Guido van Rossumac7be682001-01-17 15:42:30 +00003097 /* Clear the OverflowError */
3098 PyErr_Clear();
3099
3100 /* It's an overflow error, so we need to
3101 check the sign of the long integer,
3102 set the value to INT_MAX or 0, and clear
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003103 the error. */
3104
3105 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003106 long_zero = PyLong_FromLong(0L);
Guido van Rossum20c6add2000-05-08 14:06:50 +00003107 if (long_zero == NULL) return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003108
3109 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003110 cmp = PyObject_RichCompareBool(v, long_zero,
3111 Py_GT);
3112 Py_DECREF(long_zero);
3113 if (cmp < 0)
3114 return 0;
3115 else if (cmp > 0)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003116 x = INT_MAX;
3117 else
3118 x = 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003119 }
3120 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003121 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003122 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003123 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003124 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003125 /* Truncate -- very long indices are truncated anyway */
3126 if (x > INT_MAX)
3127 x = INT_MAX;
3128 else if (x < -INT_MAX)
3129 x = 0;
3130 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003131 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003132 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003133}
3134
Guido van Rossumb209a111997-04-29 18:18:01 +00003135static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003136apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003137{
Guido van Rossuma027efa1997-05-05 20:56:21 +00003138 int ilow = 0, ihigh = INT_MAX;
Guido van Rossum20c6add2000-05-08 14:06:50 +00003139 if (!_PyEval_SliceIndex(v, &ilow))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003140 return NULL;
Guido van Rossum20c6add2000-05-08 14:06:50 +00003141 if (!_PyEval_SliceIndex(w, &ihigh))
Guido van Rossum04691fc1992-08-12 15:35:34 +00003142 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003143 return PySequence_GetSlice(u, ilow, ihigh);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003144}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003145
3146static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003147assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3148 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003149{
Guido van Rossuma027efa1997-05-05 20:56:21 +00003150 int ilow = 0, ihigh = INT_MAX;
Guido van Rossum20c6add2000-05-08 14:06:50 +00003151 if (!_PyEval_SliceIndex(v, &ilow))
Guido van Rossum3f5da241990-12-20 15:06:42 +00003152 return -1;
Guido van Rossum20c6add2000-05-08 14:06:50 +00003153 if (!_PyEval_SliceIndex(w, &ihigh))
Guido van Rossum3f5da241990-12-20 15:06:42 +00003154 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003155 if (x == NULL)
3156 return PySequence_DelSlice(u, ilow, ihigh);
3157 else
3158 return PySequence_SetSlice(u, ilow, ihigh, x);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003159}
3160
Guido van Rossumb209a111997-04-29 18:18:01 +00003161static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003162cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003163{
Guido van Rossumac7be682001-01-17 15:42:30 +00003164 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003165 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00003166 case IS:
3167 case IS_NOT:
3168 res = (v == w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00003169 if (op == (int) IS_NOT)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003170 res = !res;
3171 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003172 case IN:
3173 case NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003174 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003175 if (res < 0)
3176 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003177 if (op == (int) NOT_IN)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003178 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003179 break;
3180 case EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003181 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003182 break;
3183 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003184 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003185 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003186 v = res ? Py_True : Py_False;
3187 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003188 return v;
3189}
3190
Thomas Wouters52152252000-08-17 22:55:00 +00003191static PyObject *
3192import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003193{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003194 PyObject *x;
3195
3196 x = PyObject_GetAttr(v, name);
3197 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003198 PyErr_Format(PyExc_ImportError,
3199 "cannot import name %.230s",
3200 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003201 }
Thomas Wouters52152252000-08-17 22:55:00 +00003202 return x;
3203}
Guido van Rossumac7be682001-01-17 15:42:30 +00003204
Thomas Wouters52152252000-08-17 22:55:00 +00003205static int
3206import_all_from(PyObject *locals, PyObject *v)
3207{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003208 PyObject *all = PyObject_GetAttrString(v, "__all__");
3209 PyObject *dict, *name, *value;
3210 int skip_leading_underscores = 0;
3211 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003212
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003213 if (all == NULL) {
3214 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3215 return -1; /* Unexpected error */
3216 PyErr_Clear();
3217 dict = PyObject_GetAttrString(v, "__dict__");
3218 if (dict == NULL) {
3219 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3220 return -1;
3221 PyErr_SetString(PyExc_ImportError,
3222 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003223 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003224 }
3225 all = PyMapping_Keys(dict);
3226 Py_DECREF(dict);
3227 if (all == NULL)
3228 return -1;
3229 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003230 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003231
3232 for (pos = 0, err = 0; ; pos++) {
3233 name = PySequence_GetItem(all, pos);
3234 if (name == NULL) {
3235 if (!PyErr_ExceptionMatches(PyExc_IndexError))
3236 err = -1;
3237 else
3238 PyErr_Clear();
3239 break;
3240 }
3241 if (skip_leading_underscores &&
3242 PyString_Check(name) &&
3243 PyString_AS_STRING(name)[0] == '_')
3244 {
3245 Py_DECREF(name);
3246 continue;
3247 }
3248 value = PyObject_GetAttr(v, name);
3249 if (value == NULL)
3250 err = -1;
3251 else
3252 err = PyDict_SetItem(locals, name, value);
3253 Py_DECREF(name);
3254 Py_XDECREF(value);
3255 if (err != 0)
3256 break;
3257 }
3258 Py_DECREF(all);
3259 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003260}
3261
Guido van Rossumb209a111997-04-29 18:18:01 +00003262static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003263build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003264{
Guido van Rossumcd649651997-08-22 16:56:16 +00003265 int i, n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003266 if (!PyTuple_Check(bases)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003267 PyErr_SetString(PyExc_SystemError,
3268 "build_class with non-tuple bases");
Guido van Rossum25831651993-05-19 14:50:45 +00003269 return NULL;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003270 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003271 if (!PyDict_Check(methods)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003272 PyErr_SetString(PyExc_SystemError,
3273 "build_class with non-dictionary");
Guido van Rossume9736fc1990-11-18 17:33:06 +00003274 return NULL;
3275 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003276 if (!PyString_Check(name)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003277 PyErr_SetString(PyExc_SystemError,
Guido van Rossum7a5b7962000-03-31 13:52:29 +00003278 "build_class with non-string name");
Guido van Rossum25831651993-05-19 14:50:45 +00003279 return NULL;
3280 }
Guido van Rossumcd649651997-08-22 16:56:16 +00003281 n = PyTuple_Size(bases);
3282 for (i = 0; i < n; i++) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003283 PyObject *base = PyTuple_GET_ITEM(bases, i);
3284 if (!PyClass_Check(base)) {
Guido van Rossum150b2df1996-12-05 23:17:11 +00003285 /* Call the base's *type*, if it is callable.
3286 This code is a hook for Donald Beaudry's
3287 and Jim Fulton's type extensions. In
Thomas Wouters7e474022000-07-16 12:04:32 +00003288 unextended Python it will never be triggered
Guido van Rossum55b9ab51997-07-31 03:54:02 +00003289 since its types are not callable.
3290 Ditto: call the bases's *class*, if it has
3291 one. This makes the same thing possible
3292 without writing C code. A true meta-object
3293 protocol! */
3294 PyObject *basetype = (PyObject *)base->ob_type;
3295 PyObject *callable = NULL;
3296 if (PyCallable_Check(basetype))
3297 callable = basetype;
3298 else
3299 callable = PyObject_GetAttrString(
3300 base, "__class__");
3301 if (callable) {
3302 PyObject *args;
3303 PyObject *newclass = NULL;
3304 args = Py_BuildValue(
3305 "(OOO)", name, bases, methods);
3306 if (args != NULL) {
3307 newclass = PyEval_CallObject(
3308 callable, args);
3309 Py_DECREF(args);
3310 }
Guido van Rossum730806d1998-04-10 22:27:42 +00003311 if (callable != basetype) {
Guido van Rossum55b9ab51997-07-31 03:54:02 +00003312 Py_DECREF(callable);
Guido van Rossum730806d1998-04-10 22:27:42 +00003313 }
Guido van Rossum55b9ab51997-07-31 03:54:02 +00003314 return newclass;
Guido van Rossum150b2df1996-12-05 23:17:11 +00003315 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003316 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +00003317 "base is not a class object");
3318 return NULL;
3319 }
3320 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003321 return PyClass_New(bases, methods, name);
Guido van Rossum25831651993-05-19 14:50:45 +00003322}
3323
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003324static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003325exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
3326 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003327{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003328 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00003329 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003330 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003331
Guido van Rossumb209a111997-04-29 18:18:01 +00003332 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
3333 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003334 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00003335 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003336 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00003337 locals = PyTuple_GetItem(prog, 2);
3338 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003339 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003340 if (globals == Py_None) {
3341 globals = PyEval_GetGlobals();
3342 if (locals == Py_None) {
3343 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00003344 plain = 1;
3345 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003346 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003347 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003348 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00003349 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003350 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00003351 !PyCode_Check(prog) &&
3352 !PyFile_Check(prog)) {
3353 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00003354 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003355 return -1;
3356 }
Fred Drake661ea262000-10-24 19:57:45 +00003357 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003358 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003359 "exec: arg 2 must be a dictionary or None");
3360 return -1;
3361 }
3362 if (!PyDict_Check(locals)) {
3363 PyErr_SetString(PyExc_TypeError,
3364 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003365 return -1;
3366 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003367 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003368 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00003369 if (PyCode_Check(prog)) {
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003370 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003371 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003372 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003373 FILE *fp = PyFile_AsFile(prog);
3374 char *name = PyString_AsString(PyFile_Name(prog));
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003375 v = PyRun_File(fp, name, Py_file_input, globals, locals);
3376 }
3377 else {
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003378 char *str;
3379 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003380 return -1;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00003381 v = PyRun_String(str, Py_file_input, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003382 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00003383 if (plain)
3384 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003385 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003386 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00003387 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003388 return 0;
3389}
Guido van Rossum24c13741995-02-14 09:42:43 +00003390
Guido van Rossumac7be682001-01-17 15:42:30 +00003391static void
Paul Prescode68140d2000-08-30 20:25:01 +00003392format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
3393{
3394 char *obj_str;
3395
3396 if (!obj)
3397 return;
3398
3399 obj_str = PyString_AsString(obj);
3400 if (!obj_str)
3401 return;
3402
3403 PyErr_Format(exc, format_str, obj_str);
3404}
Guido van Rossum950361c1997-01-24 13:49:28 +00003405
3406#ifdef DYNAMIC_EXECUTION_PROFILE
3407
3408PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003409getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00003410{
3411 int i;
3412 PyObject *l = PyList_New(256);
3413 if (l == NULL) return NULL;
3414 for (i = 0; i < 256; i++) {
3415 PyObject *x = PyInt_FromLong(a[i]);
3416 if (x == NULL) {
3417 Py_DECREF(l);
3418 return NULL;
3419 }
3420 PyList_SetItem(l, i, x);
3421 }
3422 for (i = 0; i < 256; i++)
3423 a[i] = 0;
3424 return l;
3425}
3426
3427PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003428_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00003429{
3430#ifndef DXPAIRS
3431 return getarray(dxp);
3432#else
3433 int i;
3434 PyObject *l = PyList_New(257);
3435 if (l == NULL) return NULL;
3436 for (i = 0; i < 257; i++) {
3437 PyObject *x = getarray(dxpairs[i]);
3438 if (x == NULL) {
3439 Py_DECREF(l);
3440 return NULL;
3441 }
3442 PyList_SetItem(l, i, x);
3443 }
3444 return l;
3445#endif
3446}
3447
3448#endif