blob: a52168c71fd9c3a90ad4ea5d8a8d7c00749d110d [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:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Martin v. Löwise440e472004-06-01 15:22:42 +000013#include "genobject.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"
Tim Peters6d6c1a32001-08-02 04:15:00 +000016#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017
Guido van Rossumc6004111993-11-05 10:22:19 +000018#include <ctype.h>
19
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000020#ifdef WITH_TSC
21#include <asm/msr.h>
22
23typedef unsigned long long uint64;
24
25void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
26 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
27{
28 uint64 intr, inst, loop;
29 PyThreadState *tstate = PyThreadState_Get();
30 if (!tstate->interp->tscdump)
31 return;
32 intr = intr1 - intr0;
33 inst = inst1 - inst0 - intr;
34 loop = loop1 - loop0 - intr;
35 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
36 opcode, ticked, inst, loop);
37}
38#endif
39
Guido van Rossum04691fc1992-08-12 15:35:34 +000040/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000042
Guido van Rossum408027e1996-12-30 16:17:54 +000043#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000044/* For debugging the interpreter: */
45#define LLTRACE 1 /* Low-level trace feature */
46#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000047#endif
48
Jeremy Hylton52820442001-01-03 23:52:36 +000049typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000050
Guido van Rossum374a9221991-04-04 10:40:29 +000051/* Forward declarations */
Tim Peters5ca576e2001-06-18 22:08:13 +000052static PyObject *eval_frame(PyFrameObject *);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000053#ifdef WITH_TSC
54static PyObject *call_function(PyObject ***, int, uint64*, uint64*);
55#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +000056static PyObject *call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000057#endif
Jeremy Hylton52820442001-01-03 23:52:36 +000058static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000059static PyObject *do_call(PyObject *, PyObject ***, int, int);
60static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000061static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000062static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000063static PyObject *load_args(PyObject ***, int);
64#define CALL_FLAG_VAR 1
65#define CALL_FLAG_KW 2
66
Guido van Rossum0a066c01992-03-27 17:29:15 +000067#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000068static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000069#endif
Fred Drake5755ce62001-06-27 19:19:46 +000070static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
71 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000072static void call_trace_protected(Py_tracefunc, PyObject *,
73 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +000074static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000075static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +000076 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000077
Tim Petersdbd9ba62000-07-09 03:09:57 +000078static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
79static int assign_slice(PyObject *, PyObject *,
80 PyObject *, PyObject *);
81static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000082static PyObject *import_from(PyObject *, PyObject *);
83static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000084static PyObject *build_class(PyObject *, PyObject *, PyObject *);
85static int exec_statement(PyFrameObject *,
86 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000087static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
88static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000089static void format_exc_check_arg(PyObject *, char *, PyObject *);
Guido van Rossum374a9221991-04-04 10:40:29 +000090
Paul Prescode68140d2000-08-30 20:25:01 +000091#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000092 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000093#define GLOBAL_NAME_ERROR_MSG \
94 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000095#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000096 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000097#define UNBOUNDFREE_ERROR_MSG \
98 "free variable '%.200s' referenced before assignment" \
99 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000100
Guido van Rossum950361c1997-01-24 13:49:28 +0000101/* Dynamic execution profile */
102#ifdef DYNAMIC_EXECUTION_PROFILE
103#ifdef DXPAIRS
104static long dxpairs[257][256];
105#define dxp dxpairs[256]
106#else
107static long dxp[256];
108#endif
109#endif
110
Jeremy Hylton985eba52003-02-05 23:13:00 +0000111/* Function call profile */
112#ifdef CALL_PROFILE
113#define PCALL_NUM 11
114static int pcall[PCALL_NUM];
115
116#define PCALL_ALL 0
117#define PCALL_FUNCTION 1
118#define PCALL_FAST_FUNCTION 2
119#define PCALL_FASTER_FUNCTION 3
120#define PCALL_METHOD 4
121#define PCALL_BOUND_METHOD 5
122#define PCALL_CFUNCTION 6
123#define PCALL_TYPE 7
124#define PCALL_GENERATOR 8
125#define PCALL_OTHER 9
126#define PCALL_POP 10
127
128/* Notes about the statistics
129
130 PCALL_FAST stats
131
132 FAST_FUNCTION means no argument tuple needs to be created.
133 FASTER_FUNCTION means that the fast-path frame setup code is used.
134
135 If there is a method call where the call can be optimized by changing
136 the argument tuple and calling the function directly, it gets recorded
137 twice.
138
139 As a result, the relationship among the statistics appears to be
140 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
141 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
142 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
143 PCALL_METHOD > PCALL_BOUND_METHOD
144*/
145
146#define PCALL(POS) pcall[POS]++
147
148PyObject *
149PyEval_GetCallStats(PyObject *self)
150{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000151 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000152 pcall[0], pcall[1], pcall[2], pcall[3],
153 pcall[4], pcall[5], pcall[6], pcall[7],
154 pcall[8], pcall[9]);
155}
156#else
157#define PCALL(O)
158
159PyObject *
160PyEval_GetCallStats(PyObject *self)
161{
162 Py_INCREF(Py_None);
163 return Py_None;
164}
165#endif
166
Tim Peters5ca576e2001-06-18 22:08:13 +0000167
Guido van Rossume59214e1994-08-30 08:01:59 +0000168#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000169
Guido van Rossum2571cc81999-04-07 16:07:23 +0000170#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000171#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000172#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000173#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000174
Guido van Rossuma027efa1997-05-05 20:56:21 +0000175extern int _PyThread_Started; /* Flag for Py_Exit */
176
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000177static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000178static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000179
180void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000181PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000182{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000183 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000184 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000185 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000186 interpreter_lock = PyThread_allocate_lock();
187 PyThread_acquire_lock(interpreter_lock, 1);
188 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000189}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000190
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000191void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000192PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000194 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195}
196
197void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000198PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000200 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000201}
202
203void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000204PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000205{
206 if (tstate == NULL)
207 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000208 /* Check someone has called PyEval_InitThreads() to create the lock */
209 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000210 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000211 if (PyThreadState_Swap(tstate) != NULL)
212 Py_FatalError(
213 "PyEval_AcquireThread: non-NULL old thread state");
214}
215
216void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000217PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000218{
219 if (tstate == NULL)
220 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
221 if (PyThreadState_Swap(NULL) != tstate)
222 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000223 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000224}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000225
226/* This function is called from PyOS_AfterFork to ensure that newly
227 created child processes don't hold locks referring to threads which
228 are not running in the child process. (This could also be done using
229 pthread_atfork mechanism, at least for the pthreads implementation.) */
230
231void
232PyEval_ReInitThreads(void)
233{
234 if (!interpreter_lock)
235 return;
236 /*XXX Can't use PyThread_free_lock here because it does too
237 much error-checking. Doing this cleanly would require
238 adding a new function to each thread_*.h. Instead, just
239 create a new lock and waste a little bit of memory */
240 interpreter_lock = PyThread_allocate_lock();
241 PyThread_acquire_lock(interpreter_lock, 1);
242 main_thread = PyThread_get_thread_ident();
243}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244#endif
245
Guido van Rossumff4949e1992-08-05 19:58:53 +0000246/* Functions save_thread and restore_thread are always defined so
247 dynamically loaded modules needn't be compiled separately for use
248 with and without threads: */
249
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000250PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000251PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000252{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000253 PyThreadState *tstate = PyThreadState_Swap(NULL);
254 if (tstate == NULL)
255 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000256#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000257 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000258 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000259#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000260 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261}
262
263void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000265{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000266 if (tstate == NULL)
267 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000268#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000269 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000270 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000271 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000272 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000273 }
274#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000275 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276}
277
278
Guido van Rossuma9672091994-09-14 13:31:22 +0000279/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
280 signal handlers or Mac I/O completion routines) can schedule calls
281 to a function to be called synchronously.
282 The synchronous function is called with one void* argument.
283 It should return 0 for success or -1 for failure -- failure should
284 be accompanied by an exception.
285
286 If registry succeeds, the registry function returns 0; if it fails
287 (e.g. due to too many pending calls) it returns -1 (without setting
288 an exception condition).
289
290 Note that because registry may occur from within signal handlers,
291 or other asynchronous events, calling malloc() is unsafe!
292
293#ifdef WITH_THREAD
294 Any thread can schedule pending calls, but only the main thread
295 will execute them.
296#endif
297
298 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
299 There are two possible race conditions:
300 (1) nested asynchronous registry calls;
301 (2) registry calls made while pending calls are being processed.
302 While (1) is very unlikely, (2) is a real possibility.
303 The current code is safe against (2), but not against (1).
304 The safety against (2) is derived from the fact that only one
305 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000306
Guido van Rossuma027efa1997-05-05 20:56:21 +0000307 XXX Darn! With the advent of thread state, we should have an array
308 of pending calls per thread in the thread state! Later...
309*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000310
Guido van Rossuma9672091994-09-14 13:31:22 +0000311#define NPENDINGCALLS 32
312static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000313 int (*func)(void *);
314 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000315} pendingcalls[NPENDINGCALLS];
316static volatile int pendingfirst = 0;
317static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000318static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000319
320int
Thomas Wouters334fb892000-07-25 12:56:38 +0000321Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000322{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000323 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000324 int i, j;
325 /* XXX Begin critical section */
326 /* XXX If you want this to be safe against nested
327 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000328 if (busy)
329 return -1;
330 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000331 i = pendinglast;
332 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000333 if (j == pendingfirst) {
334 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000335 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000336 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000337 pendingcalls[i].func = func;
338 pendingcalls[i].arg = arg;
339 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000340
341 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000342 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000343 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000344 /* XXX End critical section */
345 return 0;
346}
347
Guido van Rossum180d7b41994-09-29 09:45:57 +0000348int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000349Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000350{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000351 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000352#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000353 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000354 return 0;
355#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000356 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000357 return 0;
358 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000359 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000360 for (;;) {
361 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000362 int (*func)(void *);
363 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000364 i = pendingfirst;
365 if (i == pendinglast)
366 break; /* Queue empty */
367 func = pendingcalls[i].func;
368 arg = pendingcalls[i].arg;
369 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000370 if (func(arg) < 0) {
371 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000372 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000373 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000374 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000375 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000376 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000377 return 0;
378}
379
380
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000381/* The interpreter's recursion limit */
382
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000383static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000384int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000385
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000386int
387Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000388{
389 return recursion_limit;
390}
391
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000392void
393Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000394{
395 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000396 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000397}
398
Armin Rigo2b3eb402003-10-28 12:05:48 +0000399/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
400 if the recursion_depth reaches _Py_CheckRecursionLimit.
401 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
402 to guarantee that _Py_CheckRecursiveCall() is regularly called.
403 Without USE_STACKCHECK, there is no need for this. */
404int
405_Py_CheckRecursiveCall(char *where)
406{
407 PyThreadState *tstate = PyThreadState_GET();
408
409#ifdef USE_STACKCHECK
410 if (PyOS_CheckStack()) {
411 --tstate->recursion_depth;
412 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
413 return -1;
414 }
415#endif
416 if (tstate->recursion_depth > recursion_limit) {
417 --tstate->recursion_depth;
418 PyErr_Format(PyExc_RuntimeError,
419 "maximum recursion depth exceeded%s",
420 where);
421 return -1;
422 }
423 _Py_CheckRecursionLimit = recursion_limit;
424 return 0;
425}
426
Guido van Rossum374a9221991-04-04 10:40:29 +0000427/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000428enum why_code {
429 WHY_NOT = 0x0001, /* No error */
430 WHY_EXCEPTION = 0x0002, /* Exception occurred */
431 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
432 WHY_RETURN = 0x0008, /* 'return' statement */
433 WHY_BREAK = 0x0010, /* 'break' statement */
434 WHY_CONTINUE = 0x0020, /* 'continue' statement */
435 WHY_YIELD = 0x0040 /* 'yield' operator */
436};
Guido van Rossum374a9221991-04-04 10:40:29 +0000437
Raymond Hettinger7c958652004-04-06 10:11:10 +0000438static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000439static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000440
Skip Montanarod581d772002-09-03 20:10:45 +0000441/* for manipulating the thread switch and periodic "stuff" - used to be
442 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000443int _Py_CheckInterval = 100;
444volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000445
Guido van Rossumb209a111997-04-29 18:18:01 +0000446PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000447PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000448{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000449 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000450 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000451 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000452 (PyObject **)NULL, 0,
453 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000454 (PyObject **)NULL, 0,
455 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000456}
457
458
459/* Interpreter main loop */
460
Tim Peters6d6c1a32001-08-02 04:15:00 +0000461static PyObject *
Tim Peters5ca576e2001-06-18 22:08:13 +0000462eval_frame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000463{
Guido van Rossum950361c1997-01-24 13:49:28 +0000464#ifdef DXPAIRS
465 int lastopcode = 0;
466#endif
Tim Petersb6d14da2001-12-19 04:11:07 +0000467 PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000468 register unsigned char *next_instr;
Moshe Zadkaaa39a7e2000-08-07 06:34:45 +0000469 register int opcode=0; /* Current opcode */
470 register int oparg=0; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000471 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000472 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000473 register PyObject *x; /* Result object -- NULL if error */
474 register PyObject *v; /* Temporary objects popped off stack */
475 register PyObject *w;
476 register PyObject *u;
477 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000478 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000479 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000480 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000481 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000482 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000483
Tim Peters8a5c3c72004-04-05 19:36:21 +0000484 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000485
486 not (instr_lb <= current_bytecode_offset < instr_ub)
487
Tim Peters8a5c3c72004-04-05 19:36:21 +0000488 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000489 initial values are such as to make this false the first
490 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000491 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000492
Guido van Rossumd076c731998-10-07 19:42:25 +0000493 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000494 PyObject *names;
495 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000496#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000497 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000498#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000499#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000500 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000501 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000502#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000503
Neal Norwitza81d2202002-07-14 00:27:26 +0000504/* Tuple access macros */
505
506#ifndef Py_DEBUG
507#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
508#else
509#define GETITEM(v, i) PyTuple_GetItem((v), (i))
510#endif
511
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000512#ifdef WITH_TSC
513/* Use Pentium timestamp counter to mark certain events:
514 inst0 -- beginning of switch statement for opcode dispatch
515 inst1 -- end of switch statement (may be skipped)
516 loop0 -- the top of the mainloop
517 loop1 -- place where control returns again to top of mainloop
518 (may be skipped)
519 intr1 -- beginning of long interruption
520 intr2 -- end of long interruption
521
522 Many opcodes call out to helper C functions. In some cases, the
523 time in those functions should be counted towards the time for the
524 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
525 calls another Python function; there's no point in charge all the
526 bytecode executed by the called function to the caller.
527
528 It's hard to make a useful judgement statically. In the presence
529 of operator overloading, it's impossible to tell if a call will
530 execute new Python code or not.
531
532 It's a case-by-case judgement. I'll use intr1 for the following
533 cases:
534
535 EXEC_STMT
536 IMPORT_STAR
537 IMPORT_FROM
538 CALL_FUNCTION (and friends)
539
540 */
541 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
542 int ticked = 0;
543
544 rdtscll(inst0);
545 rdtscll(inst1);
546 rdtscll(loop0);
547 rdtscll(loop1);
548#endif
549
Guido van Rossum374a9221991-04-04 10:40:29 +0000550/* Code access macros */
551
Guido van Rossumd076c731998-10-07 19:42:25 +0000552#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000553#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000554#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000555#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000556#define JUMPBY(x) (next_instr += (x))
557
Raymond Hettingerf606f872003-03-16 03:11:04 +0000558/* OpCode prediction macros
559 Some opcodes tend to come in pairs thus making it possible to predict
560 the second code when the first is run. For example, COMPARE_OP is often
561 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
562 followed by a POP_TOP.
563
564 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000565 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000566 processor has a high likelihood of making its own successful branch
567 prediction which results in a nearly zero overhead transition to the
568 next opcode.
569
570 A successful prediction saves a trip through the eval-loop including
571 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000572
Tim Peters8a5c3c72004-04-05 19:36:21 +0000573 If collecting opcode statistics, turn off prediction so that
574 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000575 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000576*/
577
Raymond Hettingera7216982004-02-08 19:59:27 +0000578#ifdef DYNAMIC_EXECUTION_PROFILE
579#define PREDICT(op) if (0) goto PRED_##op
580#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000581#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000582#endif
583
Raymond Hettingerf606f872003-03-16 03:11:04 +0000584#define PREDICTED(op) PRED_##op: next_instr++
Armin Rigo9dbf9082004-03-20 21:50:13 +0000585#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
586 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000587
Guido van Rossum374a9221991-04-04 10:40:29 +0000588/* Stack manipulation macros */
589
590#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
591#define EMPTY() (STACK_LEVEL() == 0)
592#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000593#define SECOND() (stack_pointer[-2])
594#define THIRD() (stack_pointer[-3])
595#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000596#define SET_TOP(v) (stack_pointer[-1] = (v))
597#define SET_SECOND(v) (stack_pointer[-2] = (v))
598#define SET_THIRD(v) (stack_pointer[-3] = (v))
599#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000600#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000601#define BASIC_PUSH(v) (*stack_pointer++ = (v))
602#define BASIC_POP() (*--stack_pointer)
603
Guido van Rossum96a42c81992-01-12 02:29:51 +0000604#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000605#define PUSH(v) { (void)(BASIC_PUSH(v), \
606 lltrace && prtrace(TOP(), "push")); \
607 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000608#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000609#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
610 lltrace && prtrace(TOP(), "stackadj")); \
611 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000612#else
613#define PUSH(v) BASIC_PUSH(v)
614#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000615#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000616#endif
617
Guido van Rossum681d79a1995-07-18 14:51:37 +0000618/* Local variable macros */
619
620#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000621
622/* The SETLOCAL() macro must not DECREF the local variable in-place and
623 then store the new value; it must copy the old value to a temporary
624 value, then store the new value, and then DECREF the temporary value.
625 This is because it is possible that during the DECREF the frame is
626 accessed by other code (e.g. a __del__ method or gc.collect()) and the
627 variable would be pointing to already-freed memory. */
628#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
629 GETLOCAL(i) = value; \
630 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000631
Guido van Rossuma027efa1997-05-05 20:56:21 +0000632/* Start of code */
633
Tim Peters5ca576e2001-06-18 22:08:13 +0000634 if (f == NULL)
635 return NULL;
636
Armin Rigo1d313ab2003-10-25 14:33:09 +0000637 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000638 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000639 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000640
Tim Peters5ca576e2001-06-18 22:08:13 +0000641 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000642
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000643 if (tstate->use_tracing) {
644 if (tstate->c_tracefunc != NULL) {
645 /* tstate->c_tracefunc, if defined, is a
646 function that will be called on *every* entry
647 to a code block. Its return value, if not
648 None, is a function that will be called at
649 the start of each executed line of code.
650 (Actually, the function must return itself
651 in order to continue tracing.) The trace
652 functions are called with three arguments:
653 a pointer to the current frame, a string
654 indicating why the function is called, and
655 an argument which depends on the situation.
656 The global trace function is also called
657 whenever an exception is detected. */
658 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
659 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000660 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000661 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000662 }
663 }
664 if (tstate->c_profilefunc != NULL) {
665 /* Similar for c_profilefunc, except it needn't
666 return itself and isn't called for "line" events */
667 if (call_trace(tstate->c_profilefunc,
668 tstate->c_profileobj,
669 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000670 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000671 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000672 }
673 }
674 }
675
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000676 co = f->f_code;
677 names = co->co_names;
678 consts = co->co_consts;
679 fastlocals = f->f_localsplus;
680 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000681 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000682 /* An explanation is in order for the next line.
683
684 f->f_lasti now refers to the index of the last instruction
685 executed. You might think this was obvious from the name, but
686 this wasn't always true before 2.3! PyFrame_New now sets
687 f->f_lasti to -1 (i.e. the index *before* the first instruction)
688 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
689 does work. Promise. */
690 next_instr = first_instr + f->f_lasti + 1;
691 stack_pointer = f->f_stacktop;
692 assert(stack_pointer != NULL);
693 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
694
Tim Peters5ca576e2001-06-18 22:08:13 +0000695#ifdef LLTRACE
696 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
697#endif
698#if defined(Py_DEBUG) || defined(LLTRACE)
699 filename = PyString_AsString(co->co_filename);
700#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000701
Guido van Rossum374a9221991-04-04 10:40:29 +0000702 why = WHY_NOT;
703 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000704 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000705 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000706
Guido van Rossum374a9221991-04-04 10:40:29 +0000707 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000708#ifdef WITH_TSC
709 if (inst1 == 0) {
710 /* Almost surely, the opcode executed a break
711 or a continue, preventing inst1 from being set
712 on the way out of the loop.
713 */
714 rdtscll(inst1);
715 loop1 = inst1;
716 }
717 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
718 intr0, intr1);
719 ticked = 0;
720 inst1 = 0;
721 intr0 = 0;
722 intr1 = 0;
723 rdtscll(loop0);
724#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000725 assert(stack_pointer >= f->f_valuestack); /* else underflow */
726 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
727
Guido van Rossuma027efa1997-05-05 20:56:21 +0000728 /* Do periodic things. Doing this every time through
729 the loop would add too much overhead, so we do it
730 only every Nth instruction. We also do it if
731 ``things_to_do'' is set, i.e. when an asynchronous
732 event needs attention (e.g. a signal handler or
733 async I/O handler); see Py_AddPendingCall() and
734 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000735
Skip Montanarod581d772002-09-03 20:10:45 +0000736 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000737 if (*next_instr == SETUP_FINALLY) {
738 /* Make the last opcode before
739 a try: finally: block uninterruptable. */
740 goto fast_next_opcode;
741 }
Skip Montanarod581d772002-09-03 20:10:45 +0000742 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000743 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000744#ifdef WITH_TSC
745 ticked = 1;
746#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000747 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000748 if (Py_MakePendingCalls() < 0) {
749 why = WHY_EXCEPTION;
750 goto on_error;
751 }
752 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000753#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000754 if (interpreter_lock) {
755 /* Give another thread a chance */
756
Guido van Rossum25ce5661997-08-02 03:10:38 +0000757 if (PyThreadState_Swap(NULL) != tstate)
758 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000759 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000760
761 /* Other threads may run now */
762
Guido van Rossum65d5b571998-12-21 19:32:43 +0000763 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000764 if (PyThreadState_Swap(tstate) != NULL)
765 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000766
767 /* Check for thread interrupts */
768
769 if (tstate->async_exc != NULL) {
770 x = tstate->async_exc;
771 tstate->async_exc = NULL;
772 PyErr_SetNone(x);
773 Py_DECREF(x);
774 why = WHY_EXCEPTION;
775 goto on_error;
776 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000777 }
778#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000779 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000780
Neil Schemenauer63543862002-02-17 19:10:14 +0000781 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000782 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000783
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000784 /* line-by-line tracing support */
785
786 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
787 /* see maybe_call_line_trace
788 for expository comments */
789 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000790
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000791 err = maybe_call_line_trace(tstate->c_tracefunc,
792 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000793 f, &instr_lb, &instr_ub,
794 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000795 /* Reload possibly changed frame fields */
796 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000797 if (f->f_stacktop != NULL) {
798 stack_pointer = f->f_stacktop;
799 f->f_stacktop = NULL;
800 }
801 if (err) {
802 /* trace function raised an exception */
803 goto on_error;
804 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000805 }
806
807 /* Extract opcode and argument */
808
Guido van Rossum374a9221991-04-04 10:40:29 +0000809 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000810 if (HAS_ARG(opcode))
811 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000812 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000813#ifdef DYNAMIC_EXECUTION_PROFILE
814#ifdef DXPAIRS
815 dxpairs[lastopcode][opcode]++;
816 lastopcode = opcode;
817#endif
818 dxp[opcode]++;
819#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000820
Guido van Rossum96a42c81992-01-12 02:29:51 +0000821#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000822 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000823
Guido van Rossum96a42c81992-01-12 02:29:51 +0000824 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000825 if (HAS_ARG(opcode)) {
826 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000827 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000828 }
829 else {
830 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000831 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000832 }
833 }
834#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000835
Guido van Rossum374a9221991-04-04 10:40:29 +0000836 /* Main switch on opcode */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000837#ifdef WITH_TSC
838 rdtscll(inst0);
839#endif
Jeremy Hylton52820442001-01-03 23:52:36 +0000840
Guido van Rossum374a9221991-04-04 10:40:29 +0000841 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000842
Guido van Rossum374a9221991-04-04 10:40:29 +0000843 /* BEWARE!
844 It is essential that any operation that fails sets either
845 x to NULL, err to nonzero, or why to anything but WHY_NOT,
846 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000847
Guido van Rossum374a9221991-04-04 10:40:29 +0000848 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000849
Neil Schemenauer63543862002-02-17 19:10:14 +0000850 case LOAD_FAST:
851 x = GETLOCAL(oparg);
852 if (x != NULL) {
853 Py_INCREF(x);
854 PUSH(x);
855 goto fast_next_opcode;
856 }
857 format_exc_check_arg(PyExc_UnboundLocalError,
858 UNBOUNDLOCAL_ERROR_MSG,
859 PyTuple_GetItem(co->co_varnames, oparg));
860 break;
861
862 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000863 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000864 Py_INCREF(x);
865 PUSH(x);
866 goto fast_next_opcode;
867
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000868 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000869 case STORE_FAST:
870 v = POP();
871 SETLOCAL(oparg, v);
872 goto fast_next_opcode;
873
Raymond Hettingerf606f872003-03-16 03:11:04 +0000874 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000875 case POP_TOP:
876 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000877 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000878 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000879
Guido van Rossum374a9221991-04-04 10:40:29 +0000880 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000881 v = TOP();
882 w = SECOND();
883 SET_TOP(w);
884 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000885 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000886
Guido van Rossum374a9221991-04-04 10:40:29 +0000887 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000888 v = TOP();
889 w = SECOND();
890 x = THIRD();
891 SET_TOP(w);
892 SET_SECOND(x);
893 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000894 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000895
Thomas Wouters434d0822000-08-24 20:11:32 +0000896 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000897 u = TOP();
898 v = SECOND();
899 w = THIRD();
900 x = FOURTH();
901 SET_TOP(v);
902 SET_SECOND(w);
903 SET_THIRD(x);
904 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000905 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000906
Guido van Rossum374a9221991-04-04 10:40:29 +0000907 case DUP_TOP:
908 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000909 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000910 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000911 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000912
Thomas Wouters434d0822000-08-24 20:11:32 +0000913 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000914 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000915 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000916 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000917 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000918 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000919 STACKADJ(2);
920 SET_TOP(x);
921 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000922 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000923 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000924 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000925 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000926 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000927 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000928 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000929 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000930 STACKADJ(3);
931 SET_TOP(x);
932 SET_SECOND(w);
933 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000934 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000935 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000936 Py_FatalError("invalid argument to DUP_TOPX"
937 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000938 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000939
Guido van Rossum374a9221991-04-04 10:40:29 +0000940 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000941 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000942 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000943 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000944 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000945 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000946 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000947
Guido van Rossum374a9221991-04-04 10:40:29 +0000948 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000949 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000950 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000951 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000952 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000953 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000954 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000955
Guido van Rossum374a9221991-04-04 10:40:29 +0000956 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000957 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000958 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000959 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000960 if (err == 0) {
961 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000962 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000963 continue;
964 }
965 else if (err > 0) {
966 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000967 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000968 err = 0;
969 continue;
970 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000971 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000972 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000973
Guido van Rossum374a9221991-04-04 10:40:29 +0000974 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000975 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000976 x = PyObject_Repr(v);
977 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000979 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000980 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000981
Guido van Rossum7928cd71991-10-24 14:59:31 +0000982 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000983 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000984 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000985 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000986 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000987 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000988 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000989
Guido van Rossum50564e81996-01-12 01:13:16 +0000990 case BINARY_POWER:
991 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000993 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000994 Py_DECREF(v);
995 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000996 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000997 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +0000998 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000999
Guido van Rossum374a9221991-04-04 10:40:29 +00001000 case BINARY_MULTIPLY:
1001 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001003 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001004 Py_DECREF(v);
1005 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001006 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001007 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001008 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001009
Guido van Rossum374a9221991-04-04 10:40:29 +00001010 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001011 if (!_Py_QnewFlag) {
1012 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001013 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001014 x = PyNumber_Divide(v, w);
1015 Py_DECREF(v);
1016 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001017 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001018 if (x != NULL) continue;
1019 break;
1020 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001022 BINARY_TRUE_DIVIDE */
1023 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001024 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001025 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001026 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001027 Py_DECREF(v);
1028 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001030 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001031 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001032
Guido van Rossum4668b002001-08-08 05:00:18 +00001033 case BINARY_FLOOR_DIVIDE:
1034 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001035 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001036 x = PyNumber_FloorDivide(v, w);
1037 Py_DECREF(v);
1038 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001040 if (x != NULL) continue;
1041 break;
1042
Guido van Rossum374a9221991-04-04 10:40:29 +00001043 case BINARY_MODULO:
1044 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001045 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001046 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001047 Py_DECREF(v);
1048 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001050 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001051 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001052
Guido van Rossum374a9221991-04-04 10:40:29 +00001053 case BINARY_ADD:
1054 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001055 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001056 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001057 /* INLINE: int + int */
1058 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001059 a = PyInt_AS_LONG(v);
1060 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001061 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001062 if ((i^a) < 0 && (i^b) < 0)
1063 goto slow_add;
1064 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001065 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001066 else {
1067 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001068 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001069 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001070 Py_DECREF(v);
1071 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001072 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001073 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001075
Guido van Rossum374a9221991-04-04 10:40:29 +00001076 case BINARY_SUBTRACT:
1077 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001078 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001079 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001080 /* INLINE: int - int */
1081 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001082 a = PyInt_AS_LONG(v);
1083 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001084 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001085 if ((i^a) < 0 && (i^~b) < 0)
1086 goto slow_sub;
1087 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001088 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001089 else {
1090 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001091 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001092 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001093 Py_DECREF(v);
1094 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001095 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001096 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001097 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001098
Guido van Rossum374a9221991-04-04 10:40:29 +00001099 case BINARY_SUBSCR:
1100 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001101 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001102 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001103 /* INLINE: list[int] */
1104 long i = PyInt_AsLong(w);
1105 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001106 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001107 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001108 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001109 Py_INCREF(x);
1110 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001111 else
1112 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001113 }
1114 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001115 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001116 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001117 Py_DECREF(v);
1118 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001120 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001121 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001122
Guido van Rossum7928cd71991-10-24 14:59:31 +00001123 case BINARY_LSHIFT:
1124 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001125 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001126 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001127 Py_DECREF(v);
1128 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001129 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001130 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001131 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001132
Guido van Rossum7928cd71991-10-24 14:59:31 +00001133 case BINARY_RSHIFT:
1134 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001135 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001136 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001137 Py_DECREF(v);
1138 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001139 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001140 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001141 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001142
Guido van Rossum7928cd71991-10-24 14:59:31 +00001143 case BINARY_AND:
1144 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001145 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001146 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001147 Py_DECREF(v);
1148 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001149 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001150 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001151 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001152
Guido van Rossum7928cd71991-10-24 14:59:31 +00001153 case BINARY_XOR:
1154 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001156 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001157 Py_DECREF(v);
1158 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001160 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001161 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001162
Guido van Rossum7928cd71991-10-24 14:59:31 +00001163 case BINARY_OR:
1164 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001165 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001166 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001167 Py_DECREF(v);
1168 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001170 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001171 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001172
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001173 case LIST_APPEND:
1174 w = POP();
1175 v = POP();
1176 err = PyList_Append(v, w);
1177 Py_DECREF(v);
1178 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001179 if (err == 0) {
1180 PREDICT(JUMP_ABSOLUTE);
1181 continue;
1182 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001183 break;
1184
Thomas Wouters434d0822000-08-24 20:11:32 +00001185 case INPLACE_POWER:
1186 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001187 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001188 x = PyNumber_InPlacePower(v, w, Py_None);
1189 Py_DECREF(v);
1190 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001192 if (x != NULL) continue;
1193 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Thomas Wouters434d0822000-08-24 20:11:32 +00001195 case INPLACE_MULTIPLY:
1196 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001197 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001198 x = PyNumber_InPlaceMultiply(v, w);
1199 Py_DECREF(v);
1200 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001202 if (x != NULL) continue;
1203 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001204
Thomas Wouters434d0822000-08-24 20:11:32 +00001205 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001206 if (!_Py_QnewFlag) {
1207 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001209 x = PyNumber_InPlaceDivide(v, w);
1210 Py_DECREF(v);
1211 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001213 if (x != NULL) continue;
1214 break;
1215 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001217 INPLACE_TRUE_DIVIDE */
1218 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001219 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001221 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001222 Py_DECREF(v);
1223 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001224 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001225 if (x != NULL) continue;
1226 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001227
Guido van Rossum4668b002001-08-08 05:00:18 +00001228 case INPLACE_FLOOR_DIVIDE:
1229 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001230 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001231 x = PyNumber_InPlaceFloorDivide(v, w);
1232 Py_DECREF(v);
1233 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001234 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001235 if (x != NULL) continue;
1236 break;
1237
Thomas Wouters434d0822000-08-24 20:11:32 +00001238 case INPLACE_MODULO:
1239 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001240 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001241 x = PyNumber_InPlaceRemainder(v, w);
1242 Py_DECREF(v);
1243 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001244 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001245 if (x != NULL) continue;
1246 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Thomas Wouters434d0822000-08-24 20:11:32 +00001248 case INPLACE_ADD:
1249 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001250 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001251 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001252 /* INLINE: int + int */
1253 register long a, b, i;
1254 a = PyInt_AS_LONG(v);
1255 b = PyInt_AS_LONG(w);
1256 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001257 if ((i^a) < 0 && (i^b) < 0)
1258 goto slow_iadd;
1259 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001260 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001261 else {
1262 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001264 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001265 Py_DECREF(v);
1266 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001267 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001268 if (x != NULL) continue;
1269 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001270
Thomas Wouters434d0822000-08-24 20:11:32 +00001271 case INPLACE_SUBTRACT:
1272 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001273 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001274 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 /* INLINE: int - int */
1276 register long a, b, i;
1277 a = PyInt_AS_LONG(v);
1278 b = PyInt_AS_LONG(w);
1279 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001280 if ((i^a) < 0 && (i^~b) < 0)
1281 goto slow_isub;
1282 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001284 else {
1285 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001287 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001288 Py_DECREF(v);
1289 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001290 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001291 if (x != NULL) continue;
1292 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001293
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 case INPLACE_LSHIFT:
1295 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001296 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 x = PyNumber_InPlaceLshift(v, w);
1298 Py_DECREF(v);
1299 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001300 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001301 if (x != NULL) continue;
1302 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001303
Thomas Wouters434d0822000-08-24 20:11:32 +00001304 case INPLACE_RSHIFT:
1305 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001306 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001307 x = PyNumber_InPlaceRshift(v, w);
1308 Py_DECREF(v);
1309 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001310 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001311 if (x != NULL) continue;
1312 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001313
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 case INPLACE_AND:
1315 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001316 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 x = PyNumber_InPlaceAnd(v, w);
1318 Py_DECREF(v);
1319 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001320 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001321 if (x != NULL) continue;
1322 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001323
Thomas Wouters434d0822000-08-24 20:11:32 +00001324 case INPLACE_XOR:
1325 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001326 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001327 x = PyNumber_InPlaceXor(v, w);
1328 Py_DECREF(v);
1329 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001330 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 if (x != NULL) continue;
1332 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001333
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 case INPLACE_OR:
1335 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001336 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 x = PyNumber_InPlaceOr(v, w);
1338 Py_DECREF(v);
1339 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001340 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001341 if (x != NULL) continue;
1342 break;
1343
Guido van Rossum374a9221991-04-04 10:40:29 +00001344 case SLICE+0:
1345 case SLICE+1:
1346 case SLICE+2:
1347 case SLICE+3:
1348 if ((opcode-SLICE) & 2)
1349 w = POP();
1350 else
1351 w = NULL;
1352 if ((opcode-SLICE) & 1)
1353 v = POP();
1354 else
1355 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001357 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001358 Py_DECREF(u);
1359 Py_XDECREF(v);
1360 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001362 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001363 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001364
Guido van Rossum374a9221991-04-04 10:40:29 +00001365 case STORE_SLICE+0:
1366 case STORE_SLICE+1:
1367 case STORE_SLICE+2:
1368 case STORE_SLICE+3:
1369 if ((opcode-STORE_SLICE) & 2)
1370 w = POP();
1371 else
1372 w = NULL;
1373 if ((opcode-STORE_SLICE) & 1)
1374 v = POP();
1375 else
1376 v = NULL;
1377 u = POP();
1378 t = POP();
1379 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001380 Py_DECREF(t);
1381 Py_DECREF(u);
1382 Py_XDECREF(v);
1383 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001384 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001385 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Guido van Rossum374a9221991-04-04 10:40:29 +00001387 case DELETE_SLICE+0:
1388 case DELETE_SLICE+1:
1389 case DELETE_SLICE+2:
1390 case DELETE_SLICE+3:
1391 if ((opcode-DELETE_SLICE) & 2)
1392 w = POP();
1393 else
1394 w = NULL;
1395 if ((opcode-DELETE_SLICE) & 1)
1396 v = POP();
1397 else
1398 v = NULL;
1399 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001400 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001401 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001402 Py_DECREF(u);
1403 Py_XDECREF(v);
1404 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001405 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001406 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Guido van Rossum374a9221991-04-04 10:40:29 +00001408 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001409 w = TOP();
1410 v = SECOND();
1411 u = THIRD();
1412 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001413 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001414 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001415 Py_DECREF(u);
1416 Py_DECREF(v);
1417 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001418 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001422 w = TOP();
1423 v = SECOND();
1424 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001425 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001426 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001427 Py_DECREF(v);
1428 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001429 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001430 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001431
Guido van Rossum374a9221991-04-04 10:40:29 +00001432 case PRINT_EXPR:
1433 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001434 w = PySys_GetObject("displayhook");
1435 if (w == NULL) {
1436 PyErr_SetString(PyExc_RuntimeError,
1437 "lost sys.displayhook");
1438 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001439 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001440 }
1441 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001442 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001443 if (x == NULL)
1444 err = -1;
1445 }
1446 if (err == 0) {
1447 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001448 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001449 if (w == NULL)
1450 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001451 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001452 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001453 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001454 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001455
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001456 case PRINT_ITEM_TO:
1457 w = stream = POP();
1458 /* fall through to PRINT_ITEM */
1459
Guido van Rossum374a9221991-04-04 10:40:29 +00001460 case PRINT_ITEM:
1461 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001462 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001463 w = PySys_GetObject("stdout");
1464 if (w == NULL) {
1465 PyErr_SetString(PyExc_RuntimeError,
1466 "lost sys.stdout");
1467 err = -1;
1468 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001469 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001470 /* PyFile_SoftSpace() can exececute arbitrary code
1471 if sys.stdout is an instance with a __getattr__.
1472 If __getattr__ raises an exception, w will
1473 be freed, so we need to prevent that temporarily. */
1474 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001475 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001476 err = PyFile_WriteString(" ", w);
1477 if (err == 0)
1478 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001479 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001480 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001481 if (PyString_Check(v)) {
1482 char *s = PyString_AS_STRING(v);
1483 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001484 if (len == 0 ||
1485 !isspace(Py_CHARMASK(s[len-1])) ||
1486 s[len-1] == ' ')
1487 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001488 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001489#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001490 else if (PyUnicode_Check(v)) {
1491 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1492 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001493 if (len == 0 ||
1494 !Py_UNICODE_ISSPACE(s[len-1]) ||
1495 s[len-1] == ' ')
1496 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001497 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001498#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001499 else
1500 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001501 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001502 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001503 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001504 Py_XDECREF(stream);
1505 stream = NULL;
1506 if (err == 0)
1507 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001509
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001510 case PRINT_NEWLINE_TO:
1511 w = stream = POP();
1512 /* fall through to PRINT_NEWLINE */
1513
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001515 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001516 w = PySys_GetObject("stdout");
1517 if (w == NULL)
1518 PyErr_SetString(PyExc_RuntimeError,
1519 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001520 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001521 if (w != NULL) {
1522 err = PyFile_WriteString("\n", w);
1523 if (err == 0)
1524 PyFile_SoftSpace(w, 0);
1525 }
1526 Py_XDECREF(stream);
1527 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001528 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001529
Thomas Wouters434d0822000-08-24 20:11:32 +00001530
1531#ifdef CASE_TOO_BIG
1532 default: switch (opcode) {
1533#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001534 case RAISE_VARARGS:
1535 u = v = w = NULL;
1536 switch (oparg) {
1537 case 3:
1538 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001539 /* Fallthrough */
1540 case 2:
1541 v = POP(); /* value */
1542 /* Fallthrough */
1543 case 1:
1544 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001545 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001546 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001547 break;
1548 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001549 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001550 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001551 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001552 break;
1553 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001554 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Guido van Rossum374a9221991-04-04 10:40:29 +00001556 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001557 if ((x = f->f_locals) != NULL) {
1558 Py_INCREF(x);
1559 PUSH(x);
1560 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001561 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001562 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001563 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001564
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 case RETURN_VALUE:
1566 retval = POP();
1567 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001568 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001569
Tim Peters5ca576e2001-06-18 22:08:13 +00001570 case YIELD_VALUE:
1571 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001572 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001573 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001574 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001575
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001576 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001577 w = TOP();
1578 v = SECOND();
1579 u = THIRD();
1580 STACKADJ(-3);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001581#ifdef WITH_TSC
1582 rdtscll(intr0);
1583#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001584 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001585#ifdef WITH_TSC
1586 rdtscll(intr1);
1587#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001588 Py_DECREF(u);
1589 Py_DECREF(v);
1590 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001591 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Guido van Rossum374a9221991-04-04 10:40:29 +00001593 case POP_BLOCK:
1594 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001595 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001596 while (STACK_LEVEL() > b->b_level) {
1597 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001598 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001599 }
1600 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001601 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Guido van Rossum374a9221991-04-04 10:40:29 +00001603 case END_FINALLY:
1604 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001605 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001606 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001607 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001608 if (why == WHY_RETURN ||
1609 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001610 retval = POP();
1611 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001612 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001614 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001615 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001617 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001619 else if (v != Py_None) {
1620 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001621 "'finally' pops bad exception");
1622 why = WHY_EXCEPTION;
1623 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001624 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001625 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001626
Guido van Rossum374a9221991-04-04 10:40:29 +00001627 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001628 u = TOP();
1629 v = SECOND();
1630 w = THIRD();
1631 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001632 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001633 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001634 Py_DECREF(u);
1635 Py_DECREF(v);
1636 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Guido van Rossum374a9221991-04-04 10:40:29 +00001639 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001640 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001642 if ((x = f->f_locals) != NULL) {
1643 err = PyDict_SetItem(x, w, v);
1644 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001645 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001646 break;
1647 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001648 PyErr_Format(PyExc_SystemError,
1649 "no locals found when storing %s",
1650 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001651 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001654 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001655 if ((x = f->f_locals) != NULL) {
1656 if ((err = PyDict_DelItem(x, w)) != 0)
1657 format_exc_check_arg(PyExc_NameError,
1658 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001659 break;
1660 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001661 PyErr_Format(PyExc_SystemError,
1662 "no locals when deleting %s",
1663 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001665
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001666 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001667 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001668 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001669 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1670 PyObject **items = ((PyTupleObject *)v)->ob_item;
1671 while (oparg--) {
1672 w = items[oparg];
1673 Py_INCREF(w);
1674 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001675 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001676 Py_DECREF(v);
1677 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001678 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1679 PyObject **items = ((PyListObject *)v)->ob_item;
1680 while (oparg--) {
1681 w = items[oparg];
1682 Py_INCREF(w);
1683 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001684 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001685 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001686 stack_pointer + oparg))
1687 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001688 else {
1689 if (PyErr_ExceptionMatches(PyExc_TypeError))
1690 PyErr_SetString(PyExc_TypeError,
1691 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001692 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001693 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001694 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001695 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Guido van Rossum374a9221991-04-04 10:40:29 +00001697 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001698 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001699 v = TOP();
1700 u = SECOND();
1701 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001702 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1703 Py_DECREF(v);
1704 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001705 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001706 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001707
Guido van Rossum374a9221991-04-04 10:40:29 +00001708 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001709 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001711 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1712 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001713 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001715
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001716 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001717 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001718 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001719 err = PyDict_SetItem(f->f_globals, w, v);
1720 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001721 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001722 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001724 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001725 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001726 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001727 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001728 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001729 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001730
Guido van Rossum374a9221991-04-04 10:40:29 +00001731 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001732 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001733 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001734 PyErr_Format(PyExc_SystemError,
1735 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001736 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001737 break;
1738 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001739 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001740 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001741 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001743 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001744 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001745 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001746 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001747 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001748 break;
1749 }
1750 }
1751 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001752 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001754 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001755
Guido van Rossum374a9221991-04-04 10:40:29 +00001756 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001757 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001758 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001759 /* Inline the PyDict_GetItem() calls.
1760 WARNING: this is an extreme speed hack.
1761 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001762 long hash = ((PyStringObject *)w)->ob_shash;
1763 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001764 PyDictObject *d;
1765 d = (PyDictObject *)(f->f_globals);
1766 x = d->ma_lookup(d, w, hash)->me_value;
1767 if (x != NULL) {
1768 Py_INCREF(x);
1769 PUSH(x);
1770 continue;
1771 }
1772 d = (PyDictObject *)(f->f_builtins);
1773 x = d->ma_lookup(d, w, hash)->me_value;
1774 if (x != NULL) {
1775 Py_INCREF(x);
1776 PUSH(x);
1777 continue;
1778 }
1779 goto load_global_error;
1780 }
1781 }
1782 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001783 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001784 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001785 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001786 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001787 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001788 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001789 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001790 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001791 break;
1792 }
1793 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001794 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001795 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001796 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001797
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001798 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001799 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001800 if (x != NULL) {
1801 SETLOCAL(oparg, NULL);
1802 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001803 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001804 format_exc_check_arg(
1805 PyExc_UnboundLocalError,
1806 UNBOUNDLOCAL_ERROR_MSG,
1807 PyTuple_GetItem(co->co_varnames, oparg)
1808 );
1809 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001811 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001812 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001813 Py_INCREF(x);
1814 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001815 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001816 break;
1817
1818 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001819 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001820 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001821 if (w != NULL) {
1822 PUSH(w);
1823 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001824 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001825 err = -1;
1826 /* Don't stomp existing exception */
1827 if (PyErr_Occurred())
1828 break;
1829 if (oparg < f->f_ncells) {
1830 v = PyTuple_GetItem(co->co_cellvars,
1831 oparg);
1832 format_exc_check_arg(
1833 PyExc_UnboundLocalError,
1834 UNBOUNDLOCAL_ERROR_MSG,
1835 v);
1836 } else {
1837 v = PyTuple_GetItem(
1838 co->co_freevars,
1839 oparg - f->f_ncells);
1840 format_exc_check_arg(
1841 PyExc_NameError,
1842 UNBOUNDFREE_ERROR_MSG,
1843 v);
1844 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001845 break;
1846
1847 case STORE_DEREF:
1848 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001849 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001850 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001851 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001852 continue;
1853
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001855 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001856 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001857 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001859 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 }
1861 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001862 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001863 }
1864 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001865
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001867 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001868 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001869 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001870 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001871 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001872 }
1873 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001874 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001875 }
1876 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001877
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001879 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001881 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001882 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001883
Guido van Rossum374a9221991-04-04 10:40:29 +00001884 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001885 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001886 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001887 x = PyObject_GetAttr(v, w);
1888 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001889 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001890 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001892
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 case COMPARE_OP:
1894 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001895 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001896 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001897 /* INLINE: cmp(int, int) */
1898 register long a, b;
1899 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001900 a = PyInt_AS_LONG(v);
1901 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001902 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001903 case PyCmp_LT: res = a < b; break;
1904 case PyCmp_LE: res = a <= b; break;
1905 case PyCmp_EQ: res = a == b; break;
1906 case PyCmp_NE: res = a != b; break;
1907 case PyCmp_GT: res = a > b; break;
1908 case PyCmp_GE: res = a >= b; break;
1909 case PyCmp_IS: res = v == w; break;
1910 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001911 default: goto slow_compare;
1912 }
1913 x = res ? Py_True : Py_False;
1914 Py_INCREF(x);
1915 }
1916 else {
1917 slow_compare:
1918 x = cmp_outcome(oparg, v, w);
1919 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001920 Py_DECREF(v);
1921 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001922 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001923 if (x == NULL) break;
1924 PREDICT(JUMP_IF_FALSE);
1925 PREDICT(JUMP_IF_TRUE);
1926 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001927
Guido van Rossum374a9221991-04-04 10:40:29 +00001928 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001929 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001930 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001931 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001932 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001933 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001934 break;
1935 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001936 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001937 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001938 w,
1939 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001940 f->f_locals == NULL ?
1941 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001942 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001943 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001944 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001945 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001946 x = NULL;
1947 break;
1948 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001949#ifdef WITH_TSC
1950 rdtscll(intr0);
1951#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001952 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001953#ifdef WITH_TSC
1954 rdtscll(intr1);
1955#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001956 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001957 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001958 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001959 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Thomas Wouters52152252000-08-17 22:55:00 +00001961 case IMPORT_STAR:
1962 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001963 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001964 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001965 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001966 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001967 break;
1968 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001969#ifdef WITH_TSC
1970 rdtscll(intr0);
1971#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001972 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001973#ifdef WITH_TSC
1974 rdtscll(intr1);
1975#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001976 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001977 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001978 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001979 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001980
Thomas Wouters52152252000-08-17 22:55:00 +00001981 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001982 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001983 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001984#ifdef WITH_TSC
1985 rdtscll(intr0);
1986#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001987 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001988#ifdef WITH_TSC
1989 rdtscll(intr1);
1990#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001991 PUSH(x);
1992 if (x != NULL) continue;
1993 break;
1994
Guido van Rossum374a9221991-04-04 10:40:29 +00001995 case JUMP_FORWARD:
1996 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00001997 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001998
Raymond Hettingerf606f872003-03-16 03:11:04 +00001999 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002001 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002002 if (w == Py_True) {
2003 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002004 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002005 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002006 if (w == Py_False) {
2007 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002008 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002009 }
2010 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002011 if (err > 0)
2012 err = 0;
2013 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002014 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002015 else
2016 break;
2017 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002018
Raymond Hettingerf606f872003-03-16 03:11:04 +00002019 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002020 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002021 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002022 if (w == Py_False) {
2023 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002024 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002025 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002026 if (w == Py_True) {
2027 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002028 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002029 }
2030 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002031 if (err > 0) {
2032 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002033 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002034 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002035 else if (err == 0)
2036 ;
2037 else
2038 break;
2039 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002040
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002041 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002042 case JUMP_ABSOLUTE:
2043 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002044 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002045
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002046 case GET_ITER:
2047 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002048 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002049 x = PyObject_GetIter(v);
2050 Py_DECREF(v);
2051 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002052 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002053 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002054 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002055 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002056 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002057 break;
2058
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002059 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002060 case FOR_ITER:
2061 /* before: [iter]; after: [iter, iter()] *or* [] */
2062 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002063 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002064 if (x != NULL) {
2065 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002066 PREDICT(STORE_FAST);
2067 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002068 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002069 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002070 if (PyErr_Occurred()) {
2071 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2072 break;
2073 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002074 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002075 /* iterator ended normally */
2076 x = v = POP();
2077 Py_DECREF(v);
2078 JUMPBY(oparg);
2079 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002080
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002081 case BREAK_LOOP:
2082 why = WHY_BREAK;
2083 goto fast_block_end;
2084
2085 case CONTINUE_LOOP:
2086 retval = PyInt_FromLong(oparg);
2087 why = WHY_CONTINUE;
2088 goto fast_block_end;
2089
Guido van Rossum374a9221991-04-04 10:40:29 +00002090 case SETUP_LOOP:
2091 case SETUP_EXCEPT:
2092 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002093 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002094 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002095 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002096
Guido van Rossumf10570b1995-07-07 22:53:21 +00002097 case CALL_FUNCTION:
Jeremy Hylton985eba52003-02-05 23:13:00 +00002098 PCALL(PCALL_ALL);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002099#ifdef WITH_TSC
2100 x = call_function(&stack_pointer, oparg, &intr0, &intr1);
2101#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002102 x = call_function(&stack_pointer, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002103#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002104 PUSH(x);
2105 if (x != NULL)
2106 continue;
2107 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002108
Jeremy Hylton76901512000-03-28 23:49:17 +00002109 case CALL_FUNCTION_VAR:
2110 case CALL_FUNCTION_KW:
2111 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002112 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002113 int na = oparg & 0xff;
2114 int nk = (oparg>>8) & 0xff;
2115 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002116 int n = na + 2 * nk;
2117 PyObject **pfunc, *func;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002118 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002119 if (flags & CALL_FLAG_VAR)
2120 n++;
2121 if (flags & CALL_FLAG_KW)
2122 n++;
2123 pfunc = stack_pointer - n - 1;
2124 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002125
Guido van Rossumac7be682001-01-17 15:42:30 +00002126 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002127 && PyMethod_GET_SELF(func) != NULL) {
2128 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002129 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002130 func = PyMethod_GET_FUNCTION(func);
2131 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002132 Py_DECREF(*pfunc);
2133 *pfunc = self;
2134 na++;
2135 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002136 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002137 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002138#ifdef WITH_TSC
2139 rdtscll(intr0);
2140#endif
Jeremy Hylton52820442001-01-03 23:52:36 +00002141 x = ext_do_call(func, &stack_pointer, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002142#ifdef WITH_TSC
2143 rdtscll(intr1);
2144#endif
Jeremy Hylton76901512000-03-28 23:49:17 +00002145 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002146
Jeremy Hylton76901512000-03-28 23:49:17 +00002147 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002148 w = POP();
2149 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002150 }
2151 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002152 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002153 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002154 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002155 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002156
Guido van Rossum681d79a1995-07-18 14:51:37 +00002157 case MAKE_FUNCTION:
2158 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002159 x = PyFunction_New(v, f->f_globals);
2160 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002161 /* XXX Maybe this should be a separate opcode? */
2162 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002163 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002164 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002165 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002166 x = NULL;
2167 break;
2168 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002169 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002170 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002171 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002172 }
2173 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002174 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002175 }
2176 PUSH(x);
2177 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002178
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002179 case MAKE_CLOSURE:
2180 {
2181 int nfree;
2182 v = POP(); /* code object */
2183 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002184 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002185 Py_DECREF(v);
2186 /* XXX Maybe this should be a separate opcode? */
2187 if (x != NULL && nfree > 0) {
2188 v = PyTuple_New(nfree);
2189 if (v == NULL) {
2190 Py_DECREF(x);
2191 x = NULL;
2192 break;
2193 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002194 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002195 w = POP();
2196 PyTuple_SET_ITEM(v, nfree, w);
2197 }
2198 err = PyFunction_SetClosure(x, v);
2199 Py_DECREF(v);
2200 }
2201 if (x != NULL && oparg > 0) {
2202 v = PyTuple_New(oparg);
2203 if (v == NULL) {
2204 Py_DECREF(x);
2205 x = NULL;
2206 break;
2207 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002208 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002209 w = POP();
2210 PyTuple_SET_ITEM(v, oparg, w);
2211 }
2212 err = PyFunction_SetDefaults(x, v);
2213 Py_DECREF(v);
2214 }
2215 PUSH(x);
2216 break;
2217 }
2218
Guido van Rossum8861b741996-07-30 16:49:37 +00002219 case BUILD_SLICE:
2220 if (oparg == 3)
2221 w = POP();
2222 else
2223 w = NULL;
2224 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002225 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002226 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002227 Py_DECREF(u);
2228 Py_DECREF(v);
2229 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002230 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002231 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002232 break;
2233
Fred Drakeef8ace32000-08-24 00:32:09 +00002234 case EXTENDED_ARG:
2235 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002236 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002237 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002238
Guido van Rossum374a9221991-04-04 10:40:29 +00002239 default:
2240 fprintf(stderr,
2241 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002242 PyCode_Addr2Line(f->f_code, f->f_lasti),
2243 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002244 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002245 why = WHY_EXCEPTION;
2246 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002247
2248#ifdef CASE_TOO_BIG
2249 }
2250#endif
2251
Guido van Rossum374a9221991-04-04 10:40:29 +00002252 } /* switch */
2253
2254 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002255
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002256#ifdef WITH_TSC
2257 rdtscll(inst1);
2258#endif
2259
Guido van Rossum374a9221991-04-04 10:40:29 +00002260 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002261
Guido van Rossum374a9221991-04-04 10:40:29 +00002262 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002263 if (err == 0 && x != NULL) {
2264#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002265 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002266 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002267 fprintf(stderr,
2268 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002269 else {
2270#endif
2271#ifdef WITH_TSC
2272 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002273#endif
2274 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002275#ifdef CHECKEXC
2276 }
2277#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002278 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002279 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002280 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002281 err = 0;
2282 }
2283
Guido van Rossum374a9221991-04-04 10:40:29 +00002284 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002285
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002286 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002287 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002288 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002289 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002290 why = WHY_EXCEPTION;
2291 }
2292 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002293#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002294 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002295 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002296 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002297 char buf[1024];
2298 sprintf(buf, "Stack unwind with exception "
2299 "set and why=%d", why);
2300 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002301 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002302 }
2303#endif
2304
2305 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002306
Guido van Rossum374a9221991-04-04 10:40:29 +00002307 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002308 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002309
Fred Drake8f51f542001-10-04 14:48:42 +00002310 if (tstate->c_tracefunc != NULL)
2311 call_exc_trace(tstate->c_tracefunc,
2312 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002313 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002314
Guido van Rossum374a9221991-04-04 10:40:29 +00002315 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002316
Guido van Rossum374a9221991-04-04 10:40:29 +00002317 if (why == WHY_RERAISE)
2318 why = WHY_EXCEPTION;
2319
2320 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002322fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002323 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002324 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002325
Tim Peters8a5c3c72004-04-05 19:36:21 +00002326 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002327 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2328 /* For a continue inside a try block,
2329 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002330 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2331 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002332 why = WHY_NOT;
2333 JUMPTO(PyInt_AS_LONG(retval));
2334 Py_DECREF(retval);
2335 break;
2336 }
2337
Guido van Rossum374a9221991-04-04 10:40:29 +00002338 while (STACK_LEVEL() > b->b_level) {
2339 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002340 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002341 }
2342 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2343 why = WHY_NOT;
2344 JUMPTO(b->b_handler);
2345 break;
2346 }
2347 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002348 (b->b_type == SETUP_EXCEPT &&
2349 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002350 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002351 PyObject *exc, *val, *tb;
2352 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002353 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002354 val = Py_None;
2355 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002356 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002357 /* Make the raw exception data
2358 available to the handler,
2359 so a program can emulate the
2360 Python main loop. Don't do
2361 this for 'finally'. */
2362 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002363 PyErr_NormalizeException(
2364 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002365 set_exc_info(tstate,
2366 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002367 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002368 if (tb == NULL) {
2369 Py_INCREF(Py_None);
2370 PUSH(Py_None);
2371 } else
2372 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002373 PUSH(val);
2374 PUSH(exc);
2375 }
2376 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002377 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002378 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002379 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 PUSH(v);
2381 }
2382 why = WHY_NOT;
2383 JUMPTO(b->b_handler);
2384 break;
2385 }
2386 } /* unwind stack */
2387
2388 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002389
Guido van Rossum374a9221991-04-04 10:40:29 +00002390 if (why != WHY_NOT)
2391 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002392#ifdef WITH_TSC
2393 rdtscll(loop1);
2394#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002395
Guido van Rossum374a9221991-04-04 10:40:29 +00002396 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002397
Tim Peters8a5c3c72004-04-05 19:36:21 +00002398 assert(why != WHY_YIELD);
2399 /* Pop remaining stack entries. */
2400 while (!EMPTY()) {
2401 v = POP();
2402 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002403 }
2404
Tim Peters8a5c3c72004-04-05 19:36:21 +00002405 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002406 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002407
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002408fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002409 if (tstate->use_tracing) {
2410 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002411 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002412 if (call_trace(tstate->c_tracefunc,
2413 tstate->c_traceobj, f,
2414 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002415 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002416 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002417 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002418 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002419 }
Fred Drake8f51f542001-10-04 14:48:42 +00002420 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002421 if (why == WHY_EXCEPTION)
2422 call_trace_protected(tstate->c_profilefunc,
2423 tstate->c_profileobj, f,
2424 PyTrace_RETURN);
2425 else if (call_trace(tstate->c_profilefunc,
2426 tstate->c_profileobj, f,
2427 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002428 Py_XDECREF(retval);
2429 retval = NULL;
2430 why = WHY_EXCEPTION;
2431 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002432 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002433 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002434
Guido van Rossuma027efa1997-05-05 20:56:21 +00002435 reset_exc_info(tstate);
2436
Tim Peters5ca576e2001-06-18 22:08:13 +00002437 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002438 exit_eval_frame:
2439 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002440 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002441
Guido van Rossum96a42c81992-01-12 02:29:51 +00002442 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002443}
2444
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002445/* this is gonna seem *real weird*, but if you put some other code between
2446 eval_frame() and PyEval_EvalCodeEx() you will need to adjust the test in
2447 the if statement in Misc/gdbinit:ppystack */
2448
Tim Peters6d6c1a32001-08-02 04:15:00 +00002449PyObject *
2450PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002451 PyObject **args, int argcount, PyObject **kws, int kwcount,
2452 PyObject **defs, int defcount, PyObject *closure)
2453{
2454 register PyFrameObject *f;
2455 register PyObject *retval = NULL;
2456 register PyObject **fastlocals, **freevars;
2457 PyThreadState *tstate = PyThreadState_GET();
2458 PyObject *x, *u;
2459
2460 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002461 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002462 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002463 return NULL;
2464 }
2465
Jeremy Hylton985eba52003-02-05 23:13:00 +00002466 assert(globals != NULL);
2467 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002468 if (f == NULL)
2469 return NULL;
2470
2471 fastlocals = f->f_localsplus;
2472 freevars = f->f_localsplus + f->f_nlocals;
2473
2474 if (co->co_argcount > 0 ||
2475 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2476 int i;
2477 int n = argcount;
2478 PyObject *kwdict = NULL;
2479 if (co->co_flags & CO_VARKEYWORDS) {
2480 kwdict = PyDict_New();
2481 if (kwdict == NULL)
2482 goto fail;
2483 i = co->co_argcount;
2484 if (co->co_flags & CO_VARARGS)
2485 i++;
2486 SETLOCAL(i, kwdict);
2487 }
2488 if (argcount > co->co_argcount) {
2489 if (!(co->co_flags & CO_VARARGS)) {
2490 PyErr_Format(PyExc_TypeError,
2491 "%.200s() takes %s %d "
2492 "%sargument%s (%d given)",
2493 PyString_AsString(co->co_name),
2494 defcount ? "at most" : "exactly",
2495 co->co_argcount,
2496 kwcount ? "non-keyword " : "",
2497 co->co_argcount == 1 ? "" : "s",
2498 argcount);
2499 goto fail;
2500 }
2501 n = co->co_argcount;
2502 }
2503 for (i = 0; i < n; i++) {
2504 x = args[i];
2505 Py_INCREF(x);
2506 SETLOCAL(i, x);
2507 }
2508 if (co->co_flags & CO_VARARGS) {
2509 u = PyTuple_New(argcount - n);
2510 if (u == NULL)
2511 goto fail;
2512 SETLOCAL(co->co_argcount, u);
2513 for (i = n; i < argcount; i++) {
2514 x = args[i];
2515 Py_INCREF(x);
2516 PyTuple_SET_ITEM(u, i-n, x);
2517 }
2518 }
2519 for (i = 0; i < kwcount; i++) {
2520 PyObject *keyword = kws[2*i];
2521 PyObject *value = kws[2*i + 1];
2522 int j;
2523 if (keyword == NULL || !PyString_Check(keyword)) {
2524 PyErr_Format(PyExc_TypeError,
2525 "%.200s() keywords must be strings",
2526 PyString_AsString(co->co_name));
2527 goto fail;
2528 }
2529 /* XXX slow -- speed up using dictionary? */
2530 for (j = 0; j < co->co_argcount; j++) {
2531 PyObject *nm = PyTuple_GET_ITEM(
2532 co->co_varnames, j);
2533 int cmp = PyObject_RichCompareBool(
2534 keyword, nm, Py_EQ);
2535 if (cmp > 0)
2536 break;
2537 else if (cmp < 0)
2538 goto fail;
2539 }
2540 /* Check errors from Compare */
2541 if (PyErr_Occurred())
2542 goto fail;
2543 if (j >= co->co_argcount) {
2544 if (kwdict == NULL) {
2545 PyErr_Format(PyExc_TypeError,
2546 "%.200s() got an unexpected "
2547 "keyword argument '%.400s'",
2548 PyString_AsString(co->co_name),
2549 PyString_AsString(keyword));
2550 goto fail;
2551 }
2552 PyDict_SetItem(kwdict, keyword, value);
2553 }
2554 else {
2555 if (GETLOCAL(j) != NULL) {
2556 PyErr_Format(PyExc_TypeError,
2557 "%.200s() got multiple "
2558 "values for keyword "
2559 "argument '%.400s'",
2560 PyString_AsString(co->co_name),
2561 PyString_AsString(keyword));
2562 goto fail;
2563 }
2564 Py_INCREF(value);
2565 SETLOCAL(j, value);
2566 }
2567 }
2568 if (argcount < co->co_argcount) {
2569 int m = co->co_argcount - defcount;
2570 for (i = argcount; i < m; i++) {
2571 if (GETLOCAL(i) == NULL) {
2572 PyErr_Format(PyExc_TypeError,
2573 "%.200s() takes %s %d "
2574 "%sargument%s (%d given)",
2575 PyString_AsString(co->co_name),
2576 ((co->co_flags & CO_VARARGS) ||
2577 defcount) ? "at least"
2578 : "exactly",
2579 m, kwcount ? "non-keyword " : "",
2580 m == 1 ? "" : "s", i);
2581 goto fail;
2582 }
2583 }
2584 if (n > m)
2585 i = n - m;
2586 else
2587 i = 0;
2588 for (; i < defcount; i++) {
2589 if (GETLOCAL(m+i) == NULL) {
2590 PyObject *def = defs[i];
2591 Py_INCREF(def);
2592 SETLOCAL(m+i, def);
2593 }
2594 }
2595 }
2596 }
2597 else {
2598 if (argcount > 0 || kwcount > 0) {
2599 PyErr_Format(PyExc_TypeError,
2600 "%.200s() takes no arguments (%d given)",
2601 PyString_AsString(co->co_name),
2602 argcount + kwcount);
2603 goto fail;
2604 }
2605 }
2606 /* Allocate and initialize storage for cell vars, and copy free
2607 vars into frame. This isn't too efficient right now. */
2608 if (f->f_ncells) {
2609 int i = 0, j = 0, nargs, found;
2610 char *cellname, *argname;
2611 PyObject *c;
2612
2613 nargs = co->co_argcount;
2614 if (co->co_flags & CO_VARARGS)
2615 nargs++;
2616 if (co->co_flags & CO_VARKEYWORDS)
2617 nargs++;
2618
2619 /* Check for cells that shadow args */
2620 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2621 cellname = PyString_AS_STRING(
2622 PyTuple_GET_ITEM(co->co_cellvars, i));
2623 found = 0;
2624 while (j < nargs) {
2625 argname = PyString_AS_STRING(
2626 PyTuple_GET_ITEM(co->co_varnames, j));
2627 if (strcmp(cellname, argname) == 0) {
2628 c = PyCell_New(GETLOCAL(j));
2629 if (c == NULL)
2630 goto fail;
2631 GETLOCAL(f->f_nlocals + i) = c;
2632 found = 1;
2633 break;
2634 }
2635 j++;
2636 }
2637 if (found == 0) {
2638 c = PyCell_New(NULL);
2639 if (c == NULL)
2640 goto fail;
2641 SETLOCAL(f->f_nlocals + i, c);
2642 }
2643 }
2644 /* Initialize any that are left */
2645 while (i < f->f_ncells) {
2646 c = PyCell_New(NULL);
2647 if (c == NULL)
2648 goto fail;
2649 SETLOCAL(f->f_nlocals + i, c);
2650 i++;
2651 }
2652 }
2653 if (f->f_nfreevars) {
2654 int i;
2655 for (i = 0; i < f->f_nfreevars; ++i) {
2656 PyObject *o = PyTuple_GET_ITEM(closure, i);
2657 Py_INCREF(o);
2658 freevars[f->f_ncells + i] = o;
2659 }
2660 }
2661
Tim Peters5ca576e2001-06-18 22:08:13 +00002662 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002663 /* Don't need to keep the reference to f_back, it will be set
2664 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002665 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002666 f->f_back = NULL;
2667
Jeremy Hylton985eba52003-02-05 23:13:00 +00002668 PCALL(PCALL_GENERATOR);
2669
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002670 /* Create a new generator that owns the ready to run frame
2671 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002672 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002673 }
2674
2675 retval = eval_frame(f);
2676
2677 fail: /* Jump here from prelude on failure */
2678
Tim Petersb13680b2001-11-27 23:29:29 +00002679 /* decref'ing the frame can cause __del__ methods to get invoked,
2680 which can call back into Python. While we're done with the
2681 current Python frame (f), the associated C stack is still in use,
2682 so recursion_depth must be boosted for the duration.
2683 */
2684 assert(tstate != NULL);
2685 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002686 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002687 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002688 return retval;
2689}
2690
2691
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002692/* Implementation notes for set_exc_info() and reset_exc_info():
2693
2694- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2695 'exc_traceback'. These always travel together.
2696
2697- tstate->curexc_ZZZ is the "hot" exception that is set by
2698 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2699
2700- Once an exception is caught by an except clause, it is transferred
2701 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2702 can pick it up. This is the primary task of set_exc_info().
2703
2704- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2705
2706 Long ago, when none of this existed, there were just a few globals:
2707 one set corresponding to the "hot" exception, and one set
2708 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2709 globals; they were simply stored as sys.exc_ZZZ. For backwards
2710 compatibility, they still are!) The problem was that in code like
2711 this:
2712
2713 try:
2714 "something that may fail"
2715 except "some exception":
2716 "do something else first"
2717 "print the exception from sys.exc_ZZZ."
2718
2719 if "do something else first" invoked something that raised and caught
2720 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2721 cause of subtle bugs. I fixed this by changing the semantics as
2722 follows:
2723
2724 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2725 *in that frame*.
2726
2727 - But initially, and as long as no exception is caught in a given
2728 frame, sys.exc_ZZZ will hold the last exception caught in the
2729 previous frame (or the frame before that, etc.).
2730
2731 The first bullet fixed the bug in the above example. The second
2732 bullet was for backwards compatibility: it was (and is) common to
2733 have a function that is called when an exception is caught, and to
2734 have that function access the caught exception via sys.exc_ZZZ.
2735 (Example: traceback.print_exc()).
2736
2737 At the same time I fixed the problem that sys.exc_ZZZ weren't
2738 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2739 but that's really a separate improvement.
2740
2741 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2742 variables to what they were before the current frame was called. The
2743 set_exc_info() function saves them on the frame so that
2744 reset_exc_info() can restore them. The invariant is that
2745 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2746 exception (where "catching" an exception applies only to successful
2747 except clauses); and if the current frame ever caught an exception,
2748 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2749 at the start of the current frame.
2750
2751*/
2752
Guido van Rossuma027efa1997-05-05 20:56:21 +00002753static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002754set_exc_info(PyThreadState *tstate,
2755 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002756{
2757 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002758 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002759
Guido van Rossuma027efa1997-05-05 20:56:21 +00002760 frame = tstate->frame;
2761 if (frame->f_exc_type == NULL) {
2762 /* This frame didn't catch an exception before */
2763 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002764 if (tstate->exc_type == NULL) {
2765 Py_INCREF(Py_None);
2766 tstate->exc_type = Py_None;
2767 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002768 tmp_type = frame->f_exc_type;
2769 tmp_value = frame->f_exc_value;
2770 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002771 Py_XINCREF(tstate->exc_type);
2772 Py_XINCREF(tstate->exc_value);
2773 Py_XINCREF(tstate->exc_traceback);
2774 frame->f_exc_type = tstate->exc_type;
2775 frame->f_exc_value = tstate->exc_value;
2776 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002777 Py_XDECREF(tmp_type);
2778 Py_XDECREF(tmp_value);
2779 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002780 }
2781 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002782 tmp_type = tstate->exc_type;
2783 tmp_value = tstate->exc_value;
2784 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002785 Py_XINCREF(type);
2786 Py_XINCREF(value);
2787 Py_XINCREF(tb);
2788 tstate->exc_type = type;
2789 tstate->exc_value = value;
2790 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002791 Py_XDECREF(tmp_type);
2792 Py_XDECREF(tmp_value);
2793 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002794 /* For b/w compatibility */
2795 PySys_SetObject("exc_type", type);
2796 PySys_SetObject("exc_value", value);
2797 PySys_SetObject("exc_traceback", tb);
2798}
2799
2800static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002801reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002802{
2803 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002804 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002805 frame = tstate->frame;
2806 if (frame->f_exc_type != NULL) {
2807 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002808 tmp_type = tstate->exc_type;
2809 tmp_value = tstate->exc_value;
2810 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002811 Py_XINCREF(frame->f_exc_type);
2812 Py_XINCREF(frame->f_exc_value);
2813 Py_XINCREF(frame->f_exc_traceback);
2814 tstate->exc_type = frame->f_exc_type;
2815 tstate->exc_value = frame->f_exc_value;
2816 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002817 Py_XDECREF(tmp_type);
2818 Py_XDECREF(tmp_value);
2819 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002820 /* For b/w compatibility */
2821 PySys_SetObject("exc_type", frame->f_exc_type);
2822 PySys_SetObject("exc_value", frame->f_exc_value);
2823 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2824 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002825 tmp_type = frame->f_exc_type;
2826 tmp_value = frame->f_exc_value;
2827 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002828 frame->f_exc_type = NULL;
2829 frame->f_exc_value = NULL;
2830 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002831 Py_XDECREF(tmp_type);
2832 Py_XDECREF(tmp_value);
2833 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002834}
2835
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002836/* Logic for the raise statement (too complicated for inlining).
2837 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002838static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002839do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002840{
Guido van Rossumd295f121998-04-09 21:39:57 +00002841 if (type == NULL) {
2842 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002843 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002844 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2845 value = tstate->exc_value;
2846 tb = tstate->exc_traceback;
2847 Py_XINCREF(type);
2848 Py_XINCREF(value);
2849 Py_XINCREF(tb);
2850 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002851
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002852 /* We support the following forms of raise:
2853 raise <class>, <classinstance>
2854 raise <class>, <argument tuple>
2855 raise <class>, None
2856 raise <class>, <argument>
2857 raise <classinstance>, None
2858 raise <string>, <object>
2859 raise <string>, None
2860
2861 An omitted second argument is the same as None.
2862
2863 In addition, raise <tuple>, <anything> is the same as
2864 raising the tuple's first item (and it better have one!);
2865 this rule is applied recursively.
2866
2867 Finally, an optional third argument can be supplied, which
2868 gives the traceback to be substituted (useful when
2869 re-raising an exception after examining it). */
2870
2871 /* First, check the traceback argument, replacing None with
2872 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002873 if (tb == Py_None) {
2874 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002875 tb = NULL;
2876 }
2877 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002878 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002879 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002880 goto raise_error;
2881 }
2882
2883 /* Next, replace a missing value with None */
2884 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002885 value = Py_None;
2886 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002887 }
2888
2889 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002890 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2891 PyObject *tmp = type;
2892 type = PyTuple_GET_ITEM(type, 0);
2893 Py_INCREF(type);
2894 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002895 }
2896
Tim Petersafb2c802002-04-18 18:06:20 +00002897 if (PyString_CheckExact(type))
2898 /* Raising builtin string is deprecated but still allowed --
2899 * do nothing. Raising an instance of a new-style str
2900 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002901 PyErr_Warn(PyExc_PendingDeprecationWarning,
2902 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002903
2904 else if (PyClass_Check(type))
2905 PyErr_NormalizeException(&type, &value, &tb);
2906
Guido van Rossumb209a111997-04-29 18:18:01 +00002907 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002908 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002909 if (value != Py_None) {
2910 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002911 "instance exception may not have a separate value");
2912 goto raise_error;
2913 }
2914 else {
2915 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002916 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002917 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002918 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2919 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002920 }
2921 }
2922 else {
2923 /* Not something you can raise. You get an exception
2924 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002925 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002926 "exceptions must be classes, instances, or "
2927 "strings (deprecated), not %s",
2928 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002929 goto raise_error;
2930 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002931 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002932 if (tb == NULL)
2933 return WHY_EXCEPTION;
2934 else
2935 return WHY_RERAISE;
2936 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002937 Py_XDECREF(value);
2938 Py_XDECREF(type);
2939 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002940 return WHY_EXCEPTION;
2941}
2942
Tim Petersd6d010b2001-06-21 02:49:55 +00002943/* Iterate v argcnt times and store the results on the stack (via decreasing
2944 sp). Return 1 for success, 0 if error. */
2945
Barry Warsawe42b18f1997-08-25 22:13:04 +00002946static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002947unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002948{
Tim Petersd6d010b2001-06-21 02:49:55 +00002949 int i = 0;
2950 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002951 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002952
Tim Petersd6d010b2001-06-21 02:49:55 +00002953 assert(v != NULL);
2954
2955 it = PyObject_GetIter(v);
2956 if (it == NULL)
2957 goto Error;
2958
2959 for (; i < argcnt; i++) {
2960 w = PyIter_Next(it);
2961 if (w == NULL) {
2962 /* Iterator done, via error or exhaustion. */
2963 if (!PyErr_Occurred()) {
2964 PyErr_Format(PyExc_ValueError,
2965 "need more than %d value%s to unpack",
2966 i, i == 1 ? "" : "s");
2967 }
2968 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002969 }
2970 *--sp = w;
2971 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002972
2973 /* We better have exhausted the iterator now. */
2974 w = PyIter_Next(it);
2975 if (w == NULL) {
2976 if (PyErr_Occurred())
2977 goto Error;
2978 Py_DECREF(it);
2979 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002980 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002981 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002982 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002983 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002984Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002985 for (; i > 0; i--, sp++)
2986 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002987 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002988 return 0;
2989}
2990
2991
Guido van Rossum96a42c81992-01-12 02:29:51 +00002992#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00002993static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002994prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002995{
Guido van Rossum3f5da241990-12-20 15:06:42 +00002996 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00002997 if (PyObject_Print(v, stdout, 0) != 0)
2998 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002999 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003000 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003001}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003002#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003003
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003004static void
Fred Drake5755ce62001-06-27 19:19:46 +00003005call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003006{
Guido van Rossumb209a111997-04-29 18:18:01 +00003007 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003008 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003009 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003010 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003011 value = Py_None;
3012 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003013 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003014 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003015 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003016 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003017 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003018 }
Fred Drake5755ce62001-06-27 19:19:46 +00003019 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003020 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003021 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003022 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003023 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003024 Py_XDECREF(type);
3025 Py_XDECREF(value);
3026 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003027 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003028}
3029
Fred Drake4ec5d562001-10-04 19:26:43 +00003030static void
3031call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3032 int what)
3033{
3034 PyObject *type, *value, *traceback;
3035 int err;
3036 PyErr_Fetch(&type, &value, &traceback);
3037 err = call_trace(func, obj, frame, what, NULL);
3038 if (err == 0)
3039 PyErr_Restore(type, value, traceback);
3040 else {
3041 Py_XDECREF(type);
3042 Py_XDECREF(value);
3043 Py_XDECREF(traceback);
3044 }
3045}
3046
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003047static int
Fred Drake5755ce62001-06-27 19:19:46 +00003048call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3049 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003050{
Fred Drake5755ce62001-06-27 19:19:46 +00003051 register PyThreadState *tstate = frame->f_tstate;
3052 int result;
3053 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003054 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003055 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003056 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003057 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003058 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3059 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003060 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003061 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003062}
3063
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003064PyObject *
3065_PyEval_CallTracing(PyObject *func, PyObject *args)
3066{
3067 PyFrameObject *frame = PyEval_GetFrame();
3068 PyThreadState *tstate = frame->f_tstate;
3069 int save_tracing = tstate->tracing;
3070 int save_use_tracing = tstate->use_tracing;
3071 PyObject *result;
3072
3073 tstate->tracing = 0;
3074 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3075 || (tstate->c_profilefunc != NULL));
3076 result = PyObject_Call(func, args, NULL);
3077 tstate->tracing = save_tracing;
3078 tstate->use_tracing = save_use_tracing;
3079 return result;
3080}
3081
Michael W. Hudson006c7522002-11-08 13:08:46 +00003082static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003083maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003084 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3085 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003086{
3087 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003088
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003089 In a nutshell, we use the co_lnotab field of the code object
3090 to tell when execution has moved onto a different line.
3091
3092 As mentioned above, the basic idea is so set things up so
3093 that
3094
3095 *instr_lb <= frame->f_lasti < *instr_ub
3096
3097 is true so long as execution does not change lines.
3098
3099 This is all fairly simple. Digging the information out of
3100 co_lnotab takes some work, but is conceptually clear.
3101
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003102 Somewhat harder to explain is why we don't *always* call the
3103 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003104
3105 Consider this code:
3106
3107 1: def f(a):
3108 2: if a:
3109 3: print 1
3110 4: else:
3111 5: print 2
3112
3113 which compiles to this:
3114
3115 2 0 LOAD_FAST 0 (a)
3116 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003117 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003118
3119 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003120 10 PRINT_ITEM
3121 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003122 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003123 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003124
3125 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003126 19 PRINT_ITEM
3127 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003128 >> 21 LOAD_CONST 0 (None)
3129 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003130
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003131 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003132 15 and the co_lnotab will claim that execution has moved to
3133 line 3. This is at best misleading. In this case we could
3134 associate the POP_TOP with line 4, but that doesn't make
3135 sense in all cases (I think).
3136
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003137 What we do is only call the line trace function if the co_lnotab
3138 indicates we have jumped to the *start* of a line, i.e. if the
3139 current instruction offset matches the offset given for the
3140 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003141
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003142 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003143 Execution will jump from instruction offset 12 to offset 21.
3144 Then the co_lnotab would imply that execution has moved to line
3145 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003146
3147 Why do we set f_lineno when tracing? Well, consider the code
3148 above when 'a' is true. If stepping through this with 'n' in
3149 pdb, you would stop at line 1 with a "call" type event, then
3150 line events on lines 2 and 3, then a "return" type event -- but
3151 you would be shown line 5 during this event. This is a change
3152 from the behaviour in 2.2 and before, and I've found it
3153 confusing in practice. By setting and using f_lineno when
3154 tracing, one can report a line number different from that
3155 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003156 */
3157
Michael W. Hudson006c7522002-11-08 13:08:46 +00003158 int result = 0;
3159
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003160 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003161 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003162 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003163 unsigned char* p;
3164
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003165 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3166 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003167
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003168 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003169 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170
3171 /* possible optimization: if f->f_lasti == instr_ub
3172 (likely to be a common case) then we already know
3173 instr_lb -- if we stored the matching value of p
3174 somwhere we could skip the first while loop. */
3175
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003176 /* see comments in compile.c for the description of
3177 co_lnotab. A point to remember: increments to p
3178 should come in pairs -- although we don't care about
3179 the line increments here, treating them as byte
3180 increments gets confusing, to say the least. */
3181
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003182 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003183 if (addr + *p > frame->f_lasti)
3184 break;
3185 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003186 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003187 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003188 --size;
3189 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003190
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003191 if (addr == frame->f_lasti) {
3192 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003193 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003194 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003195 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003196
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003197 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003198 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003199 addr += *p++;
3200 if (*p++)
3201 break;
3202 }
3203 *instr_ub = addr;
3204 }
3205 else {
3206 *instr_ub = INT_MAX;
3207 }
3208 }
Armin Rigobf57a142004-03-22 19:24:58 +00003209 else if (frame->f_lasti <= *instr_prev) {
3210 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003211 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003212 PyTrace_LINE, Py_None);
3213 }
3214 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003215 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003216}
3217
Fred Drake5755ce62001-06-27 19:19:46 +00003218void
3219PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003220{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003221 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003222 PyObject *temp = tstate->c_profileobj;
3223 Py_XINCREF(arg);
3224 tstate->c_profilefunc = NULL;
3225 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003226 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003227 Py_XDECREF(temp);
3228 tstate->c_profilefunc = func;
3229 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003230 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003231}
3232
3233void
3234PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3235{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003236 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003237 PyObject *temp = tstate->c_traceobj;
3238 Py_XINCREF(arg);
3239 tstate->c_tracefunc = NULL;
3240 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003241 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003242 Py_XDECREF(temp);
3243 tstate->c_tracefunc = func;
3244 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003245 tstate->use_tracing = ((func != NULL)
3246 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003247}
3248
Guido van Rossumb209a111997-04-29 18:18:01 +00003249PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003250PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003251{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003252 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003253 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003254 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003255 else
3256 return current_frame->f_builtins;
3257}
3258
Guido van Rossumb209a111997-04-29 18:18:01 +00003259PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003260PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003261{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003262 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003263 if (current_frame == NULL)
3264 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003265 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003266 return current_frame->f_locals;
3267}
3268
Guido van Rossumb209a111997-04-29 18:18:01 +00003269PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003270PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003271{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003272 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003273 if (current_frame == NULL)
3274 return NULL;
3275 else
3276 return current_frame->f_globals;
3277}
3278
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003279PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003280PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003281{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003282 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003283 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003284}
3285
Guido van Rossum6135a871995-01-09 17:53:26 +00003286int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003287PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003288{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003289 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003290 return current_frame == NULL ? 0 : current_frame->f_restricted;
3291}
3292
Guido van Rossumbe270261997-05-22 22:26:18 +00003293int
Tim Peters5ba58662001-07-16 02:29:45 +00003294PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003295{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003296 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003297 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003298
3299 if (current_frame != NULL) {
3300 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003301 const int compilerflags = codeflags & PyCF_MASK;
3302 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003303 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003304 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003305 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003306#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003307 if (codeflags & CO_GENERATOR_ALLOWED) {
3308 result = 1;
3309 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3310 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003311#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003312 }
3313 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003314}
3315
3316int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003317Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003318{
Guido van Rossumb209a111997-04-29 18:18:01 +00003319 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003320 if (f == NULL)
3321 return 0;
3322 if (!PyFile_SoftSpace(f, 0))
3323 return 0;
3324 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003325}
3326
Guido van Rossum3f5da241990-12-20 15:06:42 +00003327
Guido van Rossum681d79a1995-07-18 14:51:37 +00003328/* External interface to call any callable object.
3329 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003330
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003331#undef PyEval_CallObject
3332/* for backward compatibility: export this interface */
3333
Guido van Rossumb209a111997-04-29 18:18:01 +00003334PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003335PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003336{
Guido van Rossumb209a111997-04-29 18:18:01 +00003337 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003338}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003339#define PyEval_CallObject(func,arg) \
3340 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003341
Guido van Rossumb209a111997-04-29 18:18:01 +00003342PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003343PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003344{
Jeremy Hylton52820442001-01-03 23:52:36 +00003345 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003346
3347 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003348 arg = PyTuple_New(0);
3349 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003350 PyErr_SetString(PyExc_TypeError,
3351 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003352 return NULL;
3353 }
3354 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003355 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003356
Guido van Rossumb209a111997-04-29 18:18:01 +00003357 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003358 PyErr_SetString(PyExc_TypeError,
3359 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003360 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003361 return NULL;
3362 }
3363
Tim Peters6d6c1a32001-08-02 04:15:00 +00003364 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003365 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003366 return result;
3367}
3368
Tim Peters6d6c1a32001-08-02 04:15:00 +00003369char *
3370PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003371{
3372 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003373 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003374 else if (PyFunction_Check(func))
3375 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3376 else if (PyCFunction_Check(func))
3377 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3378 else if (PyClass_Check(func))
3379 return PyString_AsString(((PyClassObject*)func)->cl_name);
3380 else if (PyInstance_Check(func)) {
3381 return PyString_AsString(
3382 ((PyInstanceObject*)func)->in_class->cl_name);
3383 } else {
3384 return func->ob_type->tp_name;
3385 }
3386}
3387
Tim Peters6d6c1a32001-08-02 04:15:00 +00003388char *
3389PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003390{
3391 if (PyMethod_Check(func))
3392 return "()";
3393 else if (PyFunction_Check(func))
3394 return "()";
3395 else if (PyCFunction_Check(func))
3396 return "()";
3397 else if (PyClass_Check(func))
3398 return " constructor";
3399 else if (PyInstance_Check(func)) {
3400 return " instance";
3401 } else {
3402 return " object";
3403 }
3404}
3405
Martin v. Löwise440e472004-06-01 15:22:42 +00003406PyObject *
3407PyEval_EvaluateFrame(PyObject *fo)
3408{
3409 return eval_frame((PyFrameObject *)fo);
3410}
3411
Jeremy Hylton52820442001-01-03 23:52:36 +00003412#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3413
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003414static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003415err_args(PyObject *func, int flags, int nargs)
3416{
3417 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003418 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003419 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003420 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003421 nargs);
3422 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003423 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003424 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003425 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003426 nargs);
3427}
3428
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003429#define BEGIN_C_TRACE \
3430if (tstate->use_tracing) { \
3431 if (tstate->c_profilefunc != NULL) { \
3432 PyObject *func_name = \
3433 PyString_FromString (((PyCFunctionObject *) \
3434 func)->m_ml->ml_name); \
3435 are_tracing = 1; \
3436 if (call_trace(tstate->c_profilefunc, \
3437 tstate->c_profileobj, \
3438 tstate->frame, PyTrace_C_CALL, \
3439 func_name)) \
3440 { return NULL; } \
3441 Py_DECREF (func_name); \
3442 } \
3443 }
3444
3445#define END_C_TRACE \
3446 if (tstate->use_tracing && are_tracing) { \
3447 if (tstate->c_profilefunc != NULL) { \
3448 if (x == NULL) { \
3449 if (call_trace (tstate->c_profilefunc, \
3450 tstate->c_profileobj, \
3451 tstate->frame, PyTrace_C_EXCEPTION, \
3452 NULL)) \
3453 { return NULL; } \
3454 } else { \
3455 if (call_trace(tstate->c_profilefunc, \
3456 tstate->c_profileobj, \
3457 tstate->frame, PyTrace_C_RETURN, \
3458 NULL)) \
3459 { return NULL; } \
3460 } \
3461 } \
3462 }
3463
3464
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003465static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003466call_function(PyObject ***pp_stack, int oparg
3467#ifdef WITH_TSC
3468 , uint64* pintr0, uint64* pintr1
3469#endif
3470 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003471{
3472 int na = oparg & 0xff;
3473 int nk = (oparg>>8) & 0xff;
3474 int n = na + 2 * nk;
3475 PyObject **pfunc = (*pp_stack) - n - 1;
3476 PyObject *func = *pfunc;
3477 PyObject *x, *w;
3478
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003479 int are_tracing = 0;
3480
3481 PyThreadState *tstate = PyThreadState_GET();
3482
Jeremy Hylton985eba52003-02-05 23:13:00 +00003483 /* Always dispatch PyCFunction first, because these are
3484 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003485 */
3486 if (PyCFunction_Check(func) && nk == 0) {
3487 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003488 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003489 if (flags & (METH_NOARGS | METH_O)) {
3490 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3491 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003492 if (flags & METH_NOARGS && na == 0) {
3493 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003494 x = (*meth)(self, NULL);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003495 END_C_TRACE
3496 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003497 else if (flags & METH_O && na == 1) {
3498 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003499 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003500 x = (*meth)(self, arg);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003501 END_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003502 Py_DECREF(arg);
3503 }
3504 else {
3505 err_args(func, flags, na);
3506 x = NULL;
3507 }
3508 }
3509 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003510 PyObject *callargs;
3511 callargs = load_args(pp_stack, na);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003512 BEGIN_C_TRACE
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003513#ifdef WITH_TSC
3514 rdtscll(*pintr0);
3515#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003516 x = PyCFunction_Call(func, callargs, NULL);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003517#ifdef WITH_TSC
3518 rdtscll(*pintr1);
3519#endif
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003520 END_C_TRACE
Tim Peters8a5c3c72004-04-05 19:36:21 +00003521 Py_XDECREF(callargs);
3522 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003523 } else {
3524 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3525 /* optimize access to bound methods */
3526 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003527 PCALL(PCALL_METHOD);
3528 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003529 Py_INCREF(self);
3530 func = PyMethod_GET_FUNCTION(func);
3531 Py_INCREF(func);
3532 Py_DECREF(*pfunc);
3533 *pfunc = self;
3534 na++;
3535 n++;
3536 } else
3537 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003538#ifdef WITH_TSC
3539 rdtscll(*pintr0);
3540#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003541 if (PyFunction_Check(func))
3542 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003543 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003544 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003545#ifdef WITH_TSC
3546 rdtscll(*pintr1);
3547#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003548 Py_DECREF(func);
3549 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003550
Jeremy Hylton985eba52003-02-05 23:13:00 +00003551 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003552 while ((*pp_stack) > pfunc) {
3553 w = EXT_POP(*pp_stack);
3554 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003555 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003556 }
3557 return x;
3558}
3559
Jeremy Hylton192690e2002-08-16 18:36:11 +00003560/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003561 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003562 For the simplest case -- a function that takes only positional
3563 arguments and is called with only positional arguments -- it
3564 inlines the most primitive frame setup code from
3565 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3566 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003567*/
3568
3569static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003570fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003571{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003572 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003573 PyObject *globals = PyFunction_GET_GLOBALS(func);
3574 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3575 PyObject **d = NULL;
3576 int nd = 0;
3577
Jeremy Hylton985eba52003-02-05 23:13:00 +00003578 PCALL(PCALL_FUNCTION);
3579 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003580 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003581 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3582 PyFrameObject *f;
3583 PyObject *retval = NULL;
3584 PyThreadState *tstate = PyThreadState_GET();
3585 PyObject **fastlocals, **stack;
3586 int i;
3587
3588 PCALL(PCALL_FASTER_FUNCTION);
3589 assert(globals != NULL);
3590 /* XXX Perhaps we should create a specialized
3591 PyFrame_New() that doesn't take locals, but does
3592 take builtins without sanity checking them.
3593 */
3594 f = PyFrame_New(tstate, co, globals, NULL);
3595 if (f == NULL)
3596 return NULL;
3597
3598 fastlocals = f->f_localsplus;
3599 stack = (*pp_stack) - n;
3600
3601 for (i = 0; i < n; i++) {
3602 Py_INCREF(*stack);
3603 fastlocals[i] = *stack++;
3604 }
3605 retval = eval_frame(f);
3606 assert(tstate != NULL);
3607 ++tstate->recursion_depth;
3608 Py_DECREF(f);
3609 --tstate->recursion_depth;
3610 return retval;
3611 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003612 if (argdefs != NULL) {
3613 d = &PyTuple_GET_ITEM(argdefs, 0);
3614 nd = ((PyTupleObject *)argdefs)->ob_size;
3615 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003616 return PyEval_EvalCodeEx(co, globals,
3617 (PyObject *)NULL, (*pp_stack)-n, na,
3618 (*pp_stack)-2*nk, nk, d, nd,
3619 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003620}
3621
3622static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003623update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3624 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003625{
3626 PyObject *kwdict = NULL;
3627 if (orig_kwdict == NULL)
3628 kwdict = PyDict_New();
3629 else {
3630 kwdict = PyDict_Copy(orig_kwdict);
3631 Py_DECREF(orig_kwdict);
3632 }
3633 if (kwdict == NULL)
3634 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003635 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003636 int err;
3637 PyObject *value = EXT_POP(*pp_stack);
3638 PyObject *key = EXT_POP(*pp_stack);
3639 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003640 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003641 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003642 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003643 PyEval_GetFuncName(func),
3644 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003645 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003646 Py_DECREF(key);
3647 Py_DECREF(value);
3648 Py_DECREF(kwdict);
3649 return NULL;
3650 }
3651 err = PyDict_SetItem(kwdict, key, value);
3652 Py_DECREF(key);
3653 Py_DECREF(value);
3654 if (err) {
3655 Py_DECREF(kwdict);
3656 return NULL;
3657 }
3658 }
3659 return kwdict;
3660}
3661
3662static PyObject *
3663update_star_args(int nstack, int nstar, PyObject *stararg,
3664 PyObject ***pp_stack)
3665{
3666 PyObject *callargs, *w;
3667
3668 callargs = PyTuple_New(nstack + nstar);
3669 if (callargs == NULL) {
3670 return NULL;
3671 }
3672 if (nstar) {
3673 int i;
3674 for (i = 0; i < nstar; i++) {
3675 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3676 Py_INCREF(a);
3677 PyTuple_SET_ITEM(callargs, nstack + i, a);
3678 }
3679 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003680 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003681 w = EXT_POP(*pp_stack);
3682 PyTuple_SET_ITEM(callargs, nstack, w);
3683 }
3684 return callargs;
3685}
3686
3687static PyObject *
3688load_args(PyObject ***pp_stack, int na)
3689{
3690 PyObject *args = PyTuple_New(na);
3691 PyObject *w;
3692
3693 if (args == NULL)
3694 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003695 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003696 w = EXT_POP(*pp_stack);
3697 PyTuple_SET_ITEM(args, na, w);
3698 }
3699 return args;
3700}
3701
3702static PyObject *
3703do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3704{
3705 PyObject *callargs = NULL;
3706 PyObject *kwdict = NULL;
3707 PyObject *result = NULL;
3708
3709 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003710 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003711 if (kwdict == NULL)
3712 goto call_fail;
3713 }
3714 callargs = load_args(pp_stack, na);
3715 if (callargs == NULL)
3716 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003717#ifdef CALL_PROFILE
3718 /* At this point, we have to look at the type of func to
3719 update the call stats properly. Do it here so as to avoid
3720 exposing the call stats machinery outside ceval.c
3721 */
3722 if (PyFunction_Check(func))
3723 PCALL(PCALL_FUNCTION);
3724 else if (PyMethod_Check(func))
3725 PCALL(PCALL_METHOD);
3726 else if (PyType_Check(func))
3727 PCALL(PCALL_TYPE);
3728 else
3729 PCALL(PCALL_OTHER);
3730#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003731 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003732 call_fail:
3733 Py_XDECREF(callargs);
3734 Py_XDECREF(kwdict);
3735 return result;
3736}
3737
3738static PyObject *
3739ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3740{
3741 int nstar = 0;
3742 PyObject *callargs = NULL;
3743 PyObject *stararg = NULL;
3744 PyObject *kwdict = NULL;
3745 PyObject *result = NULL;
3746
3747 if (flags & CALL_FLAG_KW) {
3748 kwdict = EXT_POP(*pp_stack);
3749 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003750 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003751 "%s%s argument after ** "
3752 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003753 PyEval_GetFuncName(func),
3754 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003755 goto ext_call_fail;
3756 }
3757 }
3758 if (flags & CALL_FLAG_VAR) {
3759 stararg = EXT_POP(*pp_stack);
3760 if (!PyTuple_Check(stararg)) {
3761 PyObject *t = NULL;
3762 t = PySequence_Tuple(stararg);
3763 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003764 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3765 PyErr_Format(PyExc_TypeError,
3766 "%s%s argument after * "
3767 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003768 PyEval_GetFuncName(func),
3769 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003770 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003771 goto ext_call_fail;
3772 }
3773 Py_DECREF(stararg);
3774 stararg = t;
3775 }
3776 nstar = PyTuple_GET_SIZE(stararg);
3777 }
3778 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003779 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003780 if (kwdict == NULL)
3781 goto ext_call_fail;
3782 }
3783 callargs = update_star_args(na, nstar, stararg, pp_stack);
3784 if (callargs == NULL)
3785 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003786#ifdef CALL_PROFILE
3787 /* At this point, we have to look at the type of func to
3788 update the call stats properly. Do it here so as to avoid
3789 exposing the call stats machinery outside ceval.c
3790 */
3791 if (PyFunction_Check(func))
3792 PCALL(PCALL_FUNCTION);
3793 else if (PyMethod_Check(func))
3794 PCALL(PCALL_METHOD);
3795 else if (PyType_Check(func))
3796 PCALL(PCALL_TYPE);
3797 else
3798 PCALL(PCALL_OTHER);
3799#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003800 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003801 ext_call_fail:
3802 Py_XDECREF(callargs);
3803 Py_XDECREF(kwdict);
3804 Py_XDECREF(stararg);
3805 return result;
3806}
3807
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003808#define SLICE_ERROR_MSG \
3809 "standard sequence type does not support step size other than one"
3810
Tim Peterscb479e72001-12-16 19:11:44 +00003811/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3812 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3813 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3814*/
Tim Petersb5196382001-12-16 19:44:20 +00003815/* Note: If v is NULL, return success without storing into *pi. This
3816 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3817 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003818*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003819int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003820_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003821{
Tim Petersb5196382001-12-16 19:44:20 +00003822 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003823 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003824 if (PyInt_Check(v)) {
3825 x = PyInt_AsLong(v);
3826 } else if (PyLong_Check(v)) {
3827 x = PyLong_AsLong(v);
3828 if (x==-1 && PyErr_Occurred()) {
3829 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003830 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003831
Guido van Rossumac7be682001-01-17 15:42:30 +00003832 if (!PyErr_ExceptionMatches(
3833 PyExc_OverflowError)) {
3834 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003835 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003836 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003837 }
3838
Guido van Rossumac7be682001-01-17 15:42:30 +00003839 /* Clear the OverflowError */
3840 PyErr_Clear();
3841
3842 /* It's an overflow error, so we need to
3843 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003844 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003845 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003846
3847 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003848 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003849 if (long_zero == NULL)
3850 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003851
3852 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003853 cmp = PyObject_RichCompareBool(v, long_zero,
3854 Py_GT);
3855 Py_DECREF(long_zero);
3856 if (cmp < 0)
3857 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003858 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003859 x = INT_MAX;
3860 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003861 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003862 }
3863 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003864 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003865 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003866 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003867 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003868 /* Truncate -- very long indices are truncated anyway */
3869 if (x > INT_MAX)
3870 x = INT_MAX;
3871 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003872 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003873 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003874 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003875 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003876}
3877
Guido van Rossum50d756e2001-08-18 17:43:36 +00003878#undef ISINT
3879#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3880
Guido van Rossumb209a111997-04-29 18:18:01 +00003881static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003882apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003883{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003884 PyTypeObject *tp = u->ob_type;
3885 PySequenceMethods *sq = tp->tp_as_sequence;
3886
3887 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3888 int ilow = 0, ihigh = INT_MAX;
3889 if (!_PyEval_SliceIndex(v, &ilow))
3890 return NULL;
3891 if (!_PyEval_SliceIndex(w, &ihigh))
3892 return NULL;
3893 return PySequence_GetSlice(u, ilow, ihigh);
3894 }
3895 else {
3896 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003897 if (slice != NULL) {
3898 PyObject *res = PyObject_GetItem(u, slice);
3899 Py_DECREF(slice);
3900 return res;
3901 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003902 else
3903 return NULL;
3904 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003905}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003906
3907static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003908assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3909 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003910{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003911 PyTypeObject *tp = u->ob_type;
3912 PySequenceMethods *sq = tp->tp_as_sequence;
3913
3914 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3915 int ilow = 0, ihigh = INT_MAX;
3916 if (!_PyEval_SliceIndex(v, &ilow))
3917 return -1;
3918 if (!_PyEval_SliceIndex(w, &ihigh))
3919 return -1;
3920 if (x == NULL)
3921 return PySequence_DelSlice(u, ilow, ihigh);
3922 else
3923 return PySequence_SetSlice(u, ilow, ihigh, x);
3924 }
3925 else {
3926 PyObject *slice = PySlice_New(v, w, NULL);
3927 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003928 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003929 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003930 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003931 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003932 res = PyObject_DelItem(u, slice);
3933 Py_DECREF(slice);
3934 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003935 }
3936 else
3937 return -1;
3938 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003939}
3940
Guido van Rossumb209a111997-04-29 18:18:01 +00003941static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003942cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003943{
Guido van Rossumac7be682001-01-17 15:42:30 +00003944 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003945 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003946 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003947 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003948 break;
3949 case PyCmp_IS_NOT:
3950 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003951 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003952 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003953 res = PySequence_Contains(w, v);
3954 if (res < 0)
3955 return NULL;
3956 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003957 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003958 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003959 if (res < 0)
3960 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003961 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003962 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003963 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003964 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003965 break;
3966 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003967 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003968 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003969 v = res ? Py_True : Py_False;
3970 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003971 return v;
3972}
3973
Thomas Wouters52152252000-08-17 22:55:00 +00003974static PyObject *
3975import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003976{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003977 PyObject *x;
3978
3979 x = PyObject_GetAttr(v, name);
3980 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003981 PyErr_Format(PyExc_ImportError,
3982 "cannot import name %.230s",
3983 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003984 }
Thomas Wouters52152252000-08-17 22:55:00 +00003985 return x;
3986}
Guido van Rossumac7be682001-01-17 15:42:30 +00003987
Thomas Wouters52152252000-08-17 22:55:00 +00003988static int
3989import_all_from(PyObject *locals, PyObject *v)
3990{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003991 PyObject *all = PyObject_GetAttrString(v, "__all__");
3992 PyObject *dict, *name, *value;
3993 int skip_leading_underscores = 0;
3994 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003995
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003996 if (all == NULL) {
3997 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3998 return -1; /* Unexpected error */
3999 PyErr_Clear();
4000 dict = PyObject_GetAttrString(v, "__dict__");
4001 if (dict == NULL) {
4002 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4003 return -1;
4004 PyErr_SetString(PyExc_ImportError,
4005 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004006 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004007 }
4008 all = PyMapping_Keys(dict);
4009 Py_DECREF(dict);
4010 if (all == NULL)
4011 return -1;
4012 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004013 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004014
4015 for (pos = 0, err = 0; ; pos++) {
4016 name = PySequence_GetItem(all, pos);
4017 if (name == NULL) {
4018 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4019 err = -1;
4020 else
4021 PyErr_Clear();
4022 break;
4023 }
4024 if (skip_leading_underscores &&
4025 PyString_Check(name) &&
4026 PyString_AS_STRING(name)[0] == '_')
4027 {
4028 Py_DECREF(name);
4029 continue;
4030 }
4031 value = PyObject_GetAttr(v, name);
4032 if (value == NULL)
4033 err = -1;
4034 else
4035 err = PyDict_SetItem(locals, name, value);
4036 Py_DECREF(name);
4037 Py_XDECREF(value);
4038 if (err != 0)
4039 break;
4040 }
4041 Py_DECREF(all);
4042 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004043}
4044
Guido van Rossumb209a111997-04-29 18:18:01 +00004045static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004046build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004047{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004048 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004049
4050 if (PyDict_Check(methods))
4051 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004052 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004053 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004054 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4055 base = PyTuple_GET_ITEM(bases, 0);
4056 metaclass = PyObject_GetAttrString(base, "__class__");
4057 if (metaclass == NULL) {
4058 PyErr_Clear();
4059 metaclass = (PyObject *)base->ob_type;
4060 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004061 }
4062 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004063 else {
4064 PyObject *g = PyEval_GetGlobals();
4065 if (g != NULL && PyDict_Check(g))
4066 metaclass = PyDict_GetItemString(g, "__metaclass__");
4067 if (metaclass == NULL)
4068 metaclass = (PyObject *) &PyClass_Type;
4069 Py_INCREF(metaclass);
4070 }
4071 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4072 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004073 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4074 /* A type error here likely means that the user passed
4075 in a base that was not a class (such the random module
4076 instead of the random.random type). Help them out with
4077 a more informative error message */
4078 PyErr_SetString(PyExc_TypeError,
4079 "Error when calling the metaclass.\n" \
4080 "Make sure the base arguments are valid.");
4081 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004082 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004083}
4084
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004085static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004086exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4087 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004088{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004089 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004090 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004091 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004092
Guido van Rossumb209a111997-04-29 18:18:01 +00004093 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4094 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004095 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004096 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004097 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004098 locals = PyTuple_GetItem(prog, 2);
4099 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004100 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004101 if (globals == Py_None) {
4102 globals = PyEval_GetGlobals();
4103 if (locals == Py_None) {
4104 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004105 plain = 1;
4106 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004107 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004108 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004109 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004110 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004111 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004112 !PyCode_Check(prog) &&
4113 !PyFile_Check(prog)) {
4114 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004115 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004116 return -1;
4117 }
Fred Drake661ea262000-10-24 19:57:45 +00004118 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004119 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004120 "exec: arg 2 must be a dictionary or None");
4121 return -1;
4122 }
4123 if (!PyDict_Check(locals)) {
4124 PyErr_SetString(PyExc_TypeError,
4125 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004126 return -1;
4127 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004128 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004129 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004130 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004131 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4132 PyErr_SetString(PyExc_TypeError,
4133 "code object passed to exec may not contain free variables");
4134 return -1;
4135 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004136 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004137 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004138 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004139 FILE *fp = PyFile_AsFile(prog);
4140 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004141 PyCompilerFlags cf;
4142 cf.cf_flags = 0;
4143 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004144 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004145 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004146 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004147 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004148 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004149 }
4150 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004151 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004152 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004153 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004154 cf.cf_flags = 0;
4155#ifdef Py_USING_UNICODE
4156 if (PyUnicode_Check(prog)) {
4157 tmp = PyUnicode_AsUTF8String(prog);
4158 if (tmp == NULL)
4159 return -1;
4160 prog = tmp;
4161 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4162 }
4163#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004164 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004165 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004166 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004167 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004168 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004169 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004170 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004171 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004172 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004173 if (plain)
4174 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004175 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004176 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004177 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004178 return 0;
4179}
Guido van Rossum24c13741995-02-14 09:42:43 +00004180
Guido van Rossumac7be682001-01-17 15:42:30 +00004181static void
Paul Prescode68140d2000-08-30 20:25:01 +00004182format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4183{
4184 char *obj_str;
4185
4186 if (!obj)
4187 return;
4188
4189 obj_str = PyString_AsString(obj);
4190 if (!obj_str)
4191 return;
4192
4193 PyErr_Format(exc, format_str, obj_str);
4194}
Guido van Rossum950361c1997-01-24 13:49:28 +00004195
4196#ifdef DYNAMIC_EXECUTION_PROFILE
4197
Skip Montanarof118cb12001-10-15 20:51:38 +00004198static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004199getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004200{
4201 int i;
4202 PyObject *l = PyList_New(256);
4203 if (l == NULL) return NULL;
4204 for (i = 0; i < 256; i++) {
4205 PyObject *x = PyInt_FromLong(a[i]);
4206 if (x == NULL) {
4207 Py_DECREF(l);
4208 return NULL;
4209 }
4210 PyList_SetItem(l, i, x);
4211 }
4212 for (i = 0; i < 256; i++)
4213 a[i] = 0;
4214 return l;
4215}
4216
4217PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004218_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004219{
4220#ifndef DXPAIRS
4221 return getarray(dxp);
4222#else
4223 int i;
4224 PyObject *l = PyList_New(257);
4225 if (l == NULL) return NULL;
4226 for (i = 0; i < 257; i++) {
4227 PyObject *x = getarray(dxpairs[i]);
4228 if (x == NULL) {
4229 Py_DECREF(l);
4230 return NULL;
4231 }
4232 PyList_SetItem(l, i, x);
4233 }
4234 return l;
4235#endif
4236}
4237
4238#endif