blob: f66e318f5819c79b83fbeeffe2831e55e84edb59 [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
Armin Rigo8817fcd2004-06-17 10:22:40 +0000467 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000468 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000469 register int opcode; /* Current opcode */
470 register int oparg; /* 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();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000810 oparg = 0; /* allows oparg to be stored in a register because
811 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000812 if (HAS_ARG(opcode))
813 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000814 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000815#ifdef DYNAMIC_EXECUTION_PROFILE
816#ifdef DXPAIRS
817 dxpairs[lastopcode][opcode]++;
818 lastopcode = opcode;
819#endif
820 dxp[opcode]++;
821#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000822
Guido van Rossum96a42c81992-01-12 02:29:51 +0000823#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000824 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000825
Guido van Rossum96a42c81992-01-12 02:29:51 +0000826 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000827 if (HAS_ARG(opcode)) {
828 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000829 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000830 }
831 else {
832 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000833 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000834 }
835 }
836#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000837
Guido van Rossum374a9221991-04-04 10:40:29 +0000838 /* Main switch on opcode */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000839#ifdef WITH_TSC
840 rdtscll(inst0);
841#endif
Jeremy Hylton52820442001-01-03 23:52:36 +0000842
Guido van Rossum374a9221991-04-04 10:40:29 +0000843 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000844
Guido van Rossum374a9221991-04-04 10:40:29 +0000845 /* BEWARE!
846 It is essential that any operation that fails sets either
847 x to NULL, err to nonzero, or why to anything but WHY_NOT,
848 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000849
Guido van Rossum374a9221991-04-04 10:40:29 +0000850 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000851
Neil Schemenauer63543862002-02-17 19:10:14 +0000852 case LOAD_FAST:
853 x = GETLOCAL(oparg);
854 if (x != NULL) {
855 Py_INCREF(x);
856 PUSH(x);
857 goto fast_next_opcode;
858 }
859 format_exc_check_arg(PyExc_UnboundLocalError,
860 UNBOUNDLOCAL_ERROR_MSG,
861 PyTuple_GetItem(co->co_varnames, oparg));
862 break;
863
864 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000865 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000866 Py_INCREF(x);
867 PUSH(x);
868 goto fast_next_opcode;
869
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000870 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000871 case STORE_FAST:
872 v = POP();
873 SETLOCAL(oparg, v);
874 goto fast_next_opcode;
875
Raymond Hettingerf606f872003-03-16 03:11:04 +0000876 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000877 case POP_TOP:
878 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000879 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000880 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000881
Guido van Rossum374a9221991-04-04 10:40:29 +0000882 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000883 v = TOP();
884 w = SECOND();
885 SET_TOP(w);
886 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000887 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000888
Guido van Rossum374a9221991-04-04 10:40:29 +0000889 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000890 v = TOP();
891 w = SECOND();
892 x = THIRD();
893 SET_TOP(w);
894 SET_SECOND(x);
895 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000896 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000897
Thomas Wouters434d0822000-08-24 20:11:32 +0000898 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000899 u = TOP();
900 v = SECOND();
901 w = THIRD();
902 x = FOURTH();
903 SET_TOP(v);
904 SET_SECOND(w);
905 SET_THIRD(x);
906 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000907 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000908
Guido van Rossum374a9221991-04-04 10:40:29 +0000909 case DUP_TOP:
910 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000911 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000912 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000913 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000914
Thomas Wouters434d0822000-08-24 20:11:32 +0000915 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000916 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000917 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000918 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000919 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000920 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000921 STACKADJ(2);
922 SET_TOP(x);
923 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000924 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000925 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000926 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000927 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000928 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000929 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000930 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000931 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000932 STACKADJ(3);
933 SET_TOP(x);
934 SET_SECOND(w);
935 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000936 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000937 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000938 Py_FatalError("invalid argument to DUP_TOPX"
939 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000940 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000941
Guido van Rossum374a9221991-04-04 10:40:29 +0000942 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000943 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000944 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000945 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000946 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000947 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000948 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000949
Guido van Rossum374a9221991-04-04 10:40:29 +0000950 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000951 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000952 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000953 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000954 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000955 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000956 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000957
Guido van Rossum374a9221991-04-04 10:40:29 +0000958 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000959 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000960 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000961 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000962 if (err == 0) {
963 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000964 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000965 continue;
966 }
967 else if (err > 0) {
968 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000969 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000970 err = 0;
971 continue;
972 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000973 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000974 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000975
Guido van Rossum374a9221991-04-04 10:40:29 +0000976 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000977 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000978 x = PyObject_Repr(v);
979 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000980 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000981 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000982 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000983
Guido van Rossum7928cd71991-10-24 14:59:31 +0000984 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000985 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000986 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000987 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000988 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000989 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000990 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000991
Guido van Rossum50564e81996-01-12 01:13:16 +0000992 case BINARY_POWER:
993 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000995 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000996 Py_DECREF(v);
997 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000998 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000999 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001000 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001001
Guido van Rossum374a9221991-04-04 10:40:29 +00001002 case BINARY_MULTIPLY:
1003 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001004 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001005 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001006 Py_DECREF(v);
1007 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001008 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001009 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001010 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001011
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001013 if (!_Py_QnewFlag) {
1014 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001016 x = PyNumber_Divide(v, w);
1017 Py_DECREF(v);
1018 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001019 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001020 if (x != NULL) continue;
1021 break;
1022 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001023 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001024 BINARY_TRUE_DIVIDE */
1025 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001026 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001027 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001028 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001029 Py_DECREF(v);
1030 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001031 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001032 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001033 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001034
Guido van Rossum4668b002001-08-08 05:00:18 +00001035 case BINARY_FLOOR_DIVIDE:
1036 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001037 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001038 x = PyNumber_FloorDivide(v, w);
1039 Py_DECREF(v);
1040 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001041 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001042 if (x != NULL) continue;
1043 break;
1044
Guido van Rossum374a9221991-04-04 10:40:29 +00001045 case BINARY_MODULO:
1046 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001047 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001048 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001049 Py_DECREF(v);
1050 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001051 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001052 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001053 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001054
Guido van Rossum374a9221991-04-04 10:40:29 +00001055 case BINARY_ADD:
1056 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001057 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001058 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001059 /* INLINE: int + int */
1060 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001061 a = PyInt_AS_LONG(v);
1062 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001063 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001064 if ((i^a) < 0 && (i^b) < 0)
1065 goto slow_add;
1066 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001067 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001068 else {
1069 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001070 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001071 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001072 Py_DECREF(v);
1073 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001075 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001076 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001077
Guido van Rossum374a9221991-04-04 10:40:29 +00001078 case BINARY_SUBTRACT:
1079 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001080 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001081 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001082 /* INLINE: int - int */
1083 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001084 a = PyInt_AS_LONG(v);
1085 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001086 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001087 if ((i^a) < 0 && (i^~b) < 0)
1088 goto slow_sub;
1089 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001090 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001091 else {
1092 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001093 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001094 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001095 Py_DECREF(v);
1096 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001097 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001098 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001099 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001100
Guido van Rossum374a9221991-04-04 10:40:29 +00001101 case BINARY_SUBSCR:
1102 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001103 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001104 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001105 /* INLINE: list[int] */
1106 long i = PyInt_AsLong(w);
1107 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001108 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001109 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001110 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 Py_INCREF(x);
1112 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001113 else
1114 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001115 }
1116 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001117 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001118 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001119 Py_DECREF(v);
1120 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001121 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001122 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001123 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001124
Guido van Rossum7928cd71991-10-24 14:59:31 +00001125 case BINARY_LSHIFT:
1126 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001127 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001128 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001129 Py_DECREF(v);
1130 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001131 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001132 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001133 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001134
Guido van Rossum7928cd71991-10-24 14:59:31 +00001135 case BINARY_RSHIFT:
1136 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001137 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001138 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001139 Py_DECREF(v);
1140 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001141 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001142 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001143 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001144
Guido van Rossum7928cd71991-10-24 14:59:31 +00001145 case BINARY_AND:
1146 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001147 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001148 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001149 Py_DECREF(v);
1150 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001151 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001152 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001153 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Guido van Rossum7928cd71991-10-24 14:59:31 +00001155 case BINARY_XOR:
1156 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001157 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001158 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001159 Py_DECREF(v);
1160 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001161 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001162 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001163 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001164
Guido van Rossum7928cd71991-10-24 14:59:31 +00001165 case BINARY_OR:
1166 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001167 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001168 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001169 Py_DECREF(v);
1170 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001171 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001172 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001173 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001174
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001175 case LIST_APPEND:
1176 w = POP();
1177 v = POP();
1178 err = PyList_Append(v, w);
1179 Py_DECREF(v);
1180 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001181 if (err == 0) {
1182 PREDICT(JUMP_ABSOLUTE);
1183 continue;
1184 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001185 break;
1186
Thomas Wouters434d0822000-08-24 20:11:32 +00001187 case INPLACE_POWER:
1188 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001190 x = PyNumber_InPlacePower(v, w, Py_None);
1191 Py_DECREF(v);
1192 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001193 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001194 if (x != NULL) continue;
1195 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Thomas Wouters434d0822000-08-24 20:11:32 +00001197 case INPLACE_MULTIPLY:
1198 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001199 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001200 x = PyNumber_InPlaceMultiply(v, w);
1201 Py_DECREF(v);
1202 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001203 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001204 if (x != NULL) continue;
1205 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Thomas Wouters434d0822000-08-24 20:11:32 +00001207 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001208 if (!_Py_QnewFlag) {
1209 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001210 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001211 x = PyNumber_InPlaceDivide(v, w);
1212 Py_DECREF(v);
1213 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001214 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001215 if (x != NULL) continue;
1216 break;
1217 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001219 INPLACE_TRUE_DIVIDE */
1220 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001221 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001222 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001223 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001224 Py_DECREF(v);
1225 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001227 if (x != NULL) continue;
1228 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Guido van Rossum4668b002001-08-08 05:00:18 +00001230 case INPLACE_FLOOR_DIVIDE:
1231 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001233 x = PyNumber_InPlaceFloorDivide(v, w);
1234 Py_DECREF(v);
1235 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001237 if (x != NULL) continue;
1238 break;
1239
Thomas Wouters434d0822000-08-24 20:11:32 +00001240 case INPLACE_MODULO:
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001243 x = PyNumber_InPlaceRemainder(v, w);
1244 Py_DECREF(v);
1245 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001247 if (x != NULL) continue;
1248 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001249
Thomas Wouters434d0822000-08-24 20:11:32 +00001250 case INPLACE_ADD:
1251 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001253 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001254 /* INLINE: int + int */
1255 register long a, b, i;
1256 a = PyInt_AS_LONG(v);
1257 b = PyInt_AS_LONG(w);
1258 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001259 if ((i^a) < 0 && (i^b) < 0)
1260 goto slow_iadd;
1261 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001262 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001263 else {
1264 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001265 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001266 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001267 Py_DECREF(v);
1268 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001270 if (x != NULL) continue;
1271 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001272
Thomas Wouters434d0822000-08-24 20:11:32 +00001273 case INPLACE_SUBTRACT:
1274 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001275 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001276 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001277 /* INLINE: int - int */
1278 register long a, b, i;
1279 a = PyInt_AS_LONG(v);
1280 b = PyInt_AS_LONG(w);
1281 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001282 if ((i^a) < 0 && (i^~b) < 0)
1283 goto slow_isub;
1284 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001285 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001286 else {
1287 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001288 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001289 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001290 Py_DECREF(v);
1291 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001292 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 if (x != NULL) continue;
1294 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001295
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 case INPLACE_LSHIFT:
1297 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 x = PyNumber_InPlaceLshift(v, w);
1300 Py_DECREF(v);
1301 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001302 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 if (x != NULL) continue;
1304 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001305
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 case INPLACE_RSHIFT:
1307 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001308 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 x = PyNumber_InPlaceRshift(v, w);
1310 Py_DECREF(v);
1311 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001312 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001313 if (x != NULL) continue;
1314 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001315
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 case INPLACE_AND:
1317 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 x = PyNumber_InPlaceAnd(v, w);
1320 Py_DECREF(v);
1321 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 if (x != NULL) continue;
1324 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 case INPLACE_XOR:
1327 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 x = PyNumber_InPlaceXor(v, w);
1330 Py_DECREF(v);
1331 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001332 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 if (x != NULL) continue;
1334 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001335
Thomas Wouters434d0822000-08-24 20:11:32 +00001336 case INPLACE_OR:
1337 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 x = PyNumber_InPlaceOr(v, w);
1340 Py_DECREF(v);
1341 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001342 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001343 if (x != NULL) continue;
1344 break;
1345
Guido van Rossum374a9221991-04-04 10:40:29 +00001346 case SLICE+0:
1347 case SLICE+1:
1348 case SLICE+2:
1349 case SLICE+3:
1350 if ((opcode-SLICE) & 2)
1351 w = POP();
1352 else
1353 w = NULL;
1354 if ((opcode-SLICE) & 1)
1355 v = POP();
1356 else
1357 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001358 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001359 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001360 Py_DECREF(u);
1361 Py_XDECREF(v);
1362 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001363 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001364 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001365 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001366
Guido van Rossum374a9221991-04-04 10:40:29 +00001367 case STORE_SLICE+0:
1368 case STORE_SLICE+1:
1369 case STORE_SLICE+2:
1370 case STORE_SLICE+3:
1371 if ((opcode-STORE_SLICE) & 2)
1372 w = POP();
1373 else
1374 w = NULL;
1375 if ((opcode-STORE_SLICE) & 1)
1376 v = POP();
1377 else
1378 v = NULL;
1379 u = POP();
1380 t = POP();
1381 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001382 Py_DECREF(t);
1383 Py_DECREF(u);
1384 Py_XDECREF(v);
1385 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001386 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001387 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Guido van Rossum374a9221991-04-04 10:40:29 +00001389 case DELETE_SLICE+0:
1390 case DELETE_SLICE+1:
1391 case DELETE_SLICE+2:
1392 case DELETE_SLICE+3:
1393 if ((opcode-DELETE_SLICE) & 2)
1394 w = POP();
1395 else
1396 w = NULL;
1397 if ((opcode-DELETE_SLICE) & 1)
1398 v = POP();
1399 else
1400 v = NULL;
1401 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001402 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001403 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001404 Py_DECREF(u);
1405 Py_XDECREF(v);
1406 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001407 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001408 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Guido van Rossum374a9221991-04-04 10:40:29 +00001410 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001411 w = TOP();
1412 v = SECOND();
1413 u = THIRD();
1414 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001415 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001416 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001417 Py_DECREF(u);
1418 Py_DECREF(v);
1419 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001420 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Guido van Rossum374a9221991-04-04 10:40:29 +00001423 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001424 w = TOP();
1425 v = SECOND();
1426 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001427 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001428 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001429 Py_DECREF(v);
1430 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001431 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001432 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001433
Guido van Rossum374a9221991-04-04 10:40:29 +00001434 case PRINT_EXPR:
1435 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001436 w = PySys_GetObject("displayhook");
1437 if (w == NULL) {
1438 PyErr_SetString(PyExc_RuntimeError,
1439 "lost sys.displayhook");
1440 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001441 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001442 }
1443 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001444 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001445 if (x == NULL)
1446 err = -1;
1447 }
1448 if (err == 0) {
1449 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001450 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001451 if (w == NULL)
1452 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001453 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001454 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001455 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001456 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001457
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001458 case PRINT_ITEM_TO:
1459 w = stream = POP();
1460 /* fall through to PRINT_ITEM */
1461
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 case PRINT_ITEM:
1463 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001464 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001465 w = PySys_GetObject("stdout");
1466 if (w == NULL) {
1467 PyErr_SetString(PyExc_RuntimeError,
1468 "lost sys.stdout");
1469 err = -1;
1470 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001471 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001472 /* PyFile_SoftSpace() can exececute arbitrary code
1473 if sys.stdout is an instance with a __getattr__.
1474 If __getattr__ raises an exception, w will
1475 be freed, so we need to prevent that temporarily. */
1476 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001477 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001478 err = PyFile_WriteString(" ", w);
1479 if (err == 0)
1480 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001481 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001482 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001483 if (PyString_Check(v)) {
1484 char *s = PyString_AS_STRING(v);
1485 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001486 if (len == 0 ||
1487 !isspace(Py_CHARMASK(s[len-1])) ||
1488 s[len-1] == ' ')
1489 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001490 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001491#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001492 else if (PyUnicode_Check(v)) {
1493 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1494 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001495 if (len == 0 ||
1496 !Py_UNICODE_ISSPACE(s[len-1]) ||
1497 s[len-1] == ' ')
1498 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001499 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001500#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001501 else
1502 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001503 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001504 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001505 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001506 Py_XDECREF(stream);
1507 stream = NULL;
1508 if (err == 0)
1509 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001512 case PRINT_NEWLINE_TO:
1513 w = stream = POP();
1514 /* fall through to PRINT_NEWLINE */
1515
Guido van Rossum374a9221991-04-04 10:40:29 +00001516 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001517 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001518 w = PySys_GetObject("stdout");
1519 if (w == NULL)
1520 PyErr_SetString(PyExc_RuntimeError,
1521 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001522 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001523 if (w != NULL) {
1524 err = PyFile_WriteString("\n", w);
1525 if (err == 0)
1526 PyFile_SoftSpace(w, 0);
1527 }
1528 Py_XDECREF(stream);
1529 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001530 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001531
Thomas Wouters434d0822000-08-24 20:11:32 +00001532
1533#ifdef CASE_TOO_BIG
1534 default: switch (opcode) {
1535#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001536 case RAISE_VARARGS:
1537 u = v = w = NULL;
1538 switch (oparg) {
1539 case 3:
1540 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001541 /* Fallthrough */
1542 case 2:
1543 v = POP(); /* value */
1544 /* Fallthrough */
1545 case 1:
1546 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001547 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001548 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001549 break;
1550 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001551 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001552 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001553 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001554 break;
1555 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001556 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001557
Guido van Rossum374a9221991-04-04 10:40:29 +00001558 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001559 if ((x = f->f_locals) != NULL) {
1560 Py_INCREF(x);
1561 PUSH(x);
1562 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001563 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001564 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001566
Guido van Rossum374a9221991-04-04 10:40:29 +00001567 case RETURN_VALUE:
1568 retval = POP();
1569 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001570 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001571
Tim Peters5ca576e2001-06-18 22:08:13 +00001572 case YIELD_VALUE:
1573 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001574 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001575 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001576 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001577
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001578 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001579 w = TOP();
1580 v = SECOND();
1581 u = THIRD();
1582 STACKADJ(-3);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001583#ifdef WITH_TSC
1584 rdtscll(intr0);
1585#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001586 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001587#ifdef WITH_TSC
1588 rdtscll(intr1);
1589#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001590 Py_DECREF(u);
1591 Py_DECREF(v);
1592 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001593 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001594
Guido van Rossum374a9221991-04-04 10:40:29 +00001595 case POP_BLOCK:
1596 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001597 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001598 while (STACK_LEVEL() > b->b_level) {
1599 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001600 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001601 }
1602 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001603 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001604
Guido van Rossum374a9221991-04-04 10:40:29 +00001605 case END_FINALLY:
1606 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001607 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001608 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001609 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001610 if (why == WHY_RETURN ||
1611 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 retval = POP();
1613 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001614 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001615 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001616 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001617 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001619 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001620 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001621 else if (v != Py_None) {
1622 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001623 "'finally' pops bad exception");
1624 why = WHY_EXCEPTION;
1625 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001626 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001627 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001630 u = TOP();
1631 v = SECOND();
1632 w = THIRD();
1633 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001634 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001635 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001636 Py_DECREF(u);
1637 Py_DECREF(v);
1638 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001639 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001642 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001644 if ((x = f->f_locals) != NULL) {
1645 err = PyDict_SetItem(x, w, v);
1646 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001647 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001648 break;
1649 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001650 PyErr_Format(PyExc_SystemError,
1651 "no locals found when storing %s",
1652 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001654
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001656 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001657 if ((x = f->f_locals) != NULL) {
1658 if ((err = PyDict_DelItem(x, w)) != 0)
1659 format_exc_check_arg(PyExc_NameError,
1660 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001661 break;
1662 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001663 PyErr_Format(PyExc_SystemError,
1664 "no locals when deleting %s",
1665 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001667
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001668 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001669 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001671 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1672 PyObject **items = ((PyTupleObject *)v)->ob_item;
1673 while (oparg--) {
1674 w = items[oparg];
1675 Py_INCREF(w);
1676 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001677 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001678 Py_DECREF(v);
1679 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001680 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1681 PyObject **items = ((PyListObject *)v)->ob_item;
1682 while (oparg--) {
1683 w = items[oparg];
1684 Py_INCREF(w);
1685 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001686 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001687 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001688 stack_pointer + oparg))
1689 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001690 else {
1691 if (PyErr_ExceptionMatches(PyExc_TypeError))
1692 PyErr_SetString(PyExc_TypeError,
1693 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001694 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001695 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001696 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001697 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Guido van Rossum374a9221991-04-04 10:40:29 +00001699 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001700 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001701 v = TOP();
1702 u = SECOND();
1703 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001704 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1705 Py_DECREF(v);
1706 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001707 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001708 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001711 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001713 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1714 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001715 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001717
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001718 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001719 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001720 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001721 err = PyDict_SetItem(f->f_globals, w, v);
1722 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001723 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001724 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001725
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001726 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001727 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001728 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001729 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001730 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001731 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001732
Guido van Rossum374a9221991-04-04 10:40:29 +00001733 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001734 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001735 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001736 PyErr_Format(PyExc_SystemError,
1737 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001738 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001739 break;
1740 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001741 x = PyDict_GetItem(x, 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_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001744 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001745 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001746 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001747 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001748 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001749 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001750 break;
1751 }
1752 }
1753 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001754 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001756 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001757
Guido van Rossum374a9221991-04-04 10:40:29 +00001758 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001759 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001760 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001761 /* Inline the PyDict_GetItem() calls.
1762 WARNING: this is an extreme speed hack.
1763 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001764 long hash = ((PyStringObject *)w)->ob_shash;
1765 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001766 PyDictObject *d;
1767 d = (PyDictObject *)(f->f_globals);
1768 x = d->ma_lookup(d, w, hash)->me_value;
1769 if (x != NULL) {
1770 Py_INCREF(x);
1771 PUSH(x);
1772 continue;
1773 }
1774 d = (PyDictObject *)(f->f_builtins);
1775 x = d->ma_lookup(d, w, hash)->me_value;
1776 if (x != NULL) {
1777 Py_INCREF(x);
1778 PUSH(x);
1779 continue;
1780 }
1781 goto load_global_error;
1782 }
1783 }
1784 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001785 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001786 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001787 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001788 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001789 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001790 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001791 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001792 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001793 break;
1794 }
1795 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001796 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001797 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001798 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001799
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001800 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001801 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001802 if (x != NULL) {
1803 SETLOCAL(oparg, NULL);
1804 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001805 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001806 format_exc_check_arg(
1807 PyExc_UnboundLocalError,
1808 UNBOUNDLOCAL_ERROR_MSG,
1809 PyTuple_GetItem(co->co_varnames, oparg)
1810 );
1811 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001812
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001813 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001814 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001815 Py_INCREF(x);
1816 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001817 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001818 break;
1819
1820 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001821 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001822 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001823 if (w != NULL) {
1824 PUSH(w);
1825 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001826 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001827 err = -1;
1828 /* Don't stomp existing exception */
1829 if (PyErr_Occurred())
1830 break;
1831 if (oparg < f->f_ncells) {
1832 v = PyTuple_GetItem(co->co_cellvars,
1833 oparg);
1834 format_exc_check_arg(
1835 PyExc_UnboundLocalError,
1836 UNBOUNDLOCAL_ERROR_MSG,
1837 v);
1838 } else {
1839 v = PyTuple_GetItem(
1840 co->co_freevars,
1841 oparg - f->f_ncells);
1842 format_exc_check_arg(
1843 PyExc_NameError,
1844 UNBOUNDFREE_ERROR_MSG,
1845 v);
1846 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001847 break;
1848
1849 case STORE_DEREF:
1850 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001851 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001852 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001853 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001854 continue;
1855
Guido van Rossum374a9221991-04-04 10:40:29 +00001856 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001857 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001859 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001861 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 }
1863 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001864 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001865 }
1866 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001867
Guido van Rossum374a9221991-04-04 10:40:29 +00001868 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001869 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001870 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001871 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001872 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001873 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001874 }
1875 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001876 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001877 }
1878 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001879
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001881 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001882 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001883 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001884 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001885
Guido van Rossum374a9221991-04-04 10:40:29 +00001886 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001887 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001888 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001889 x = PyObject_GetAttr(v, w);
1890 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001891 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001892 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001894
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 case COMPARE_OP:
1896 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001897 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001898 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001899 /* INLINE: cmp(int, int) */
1900 register long a, b;
1901 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001902 a = PyInt_AS_LONG(v);
1903 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001904 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001905 case PyCmp_LT: res = a < b; break;
1906 case PyCmp_LE: res = a <= b; break;
1907 case PyCmp_EQ: res = a == b; break;
1908 case PyCmp_NE: res = a != b; break;
1909 case PyCmp_GT: res = a > b; break;
1910 case PyCmp_GE: res = a >= b; break;
1911 case PyCmp_IS: res = v == w; break;
1912 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001913 default: goto slow_compare;
1914 }
1915 x = res ? Py_True : Py_False;
1916 Py_INCREF(x);
1917 }
1918 else {
1919 slow_compare:
1920 x = cmp_outcome(oparg, v, w);
1921 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001922 Py_DECREF(v);
1923 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001924 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001925 if (x == NULL) break;
1926 PREDICT(JUMP_IF_FALSE);
1927 PREDICT(JUMP_IF_TRUE);
1928 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001929
Guido van Rossum374a9221991-04-04 10:40:29 +00001930 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001931 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001932 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001933 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001934 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001935 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001936 break;
1937 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001938 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001939 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001940 w,
1941 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001942 f->f_locals == NULL ?
1943 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001944 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001945 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001946 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001947 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001948 x = NULL;
1949 break;
1950 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001951#ifdef WITH_TSC
1952 rdtscll(intr0);
1953#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001954 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001955#ifdef WITH_TSC
1956 rdtscll(intr1);
1957#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001958 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001959 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001960 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001961 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001962
Thomas Wouters52152252000-08-17 22:55:00 +00001963 case IMPORT_STAR:
1964 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001965 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001966 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001967 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001968 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001969 break;
1970 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001971#ifdef WITH_TSC
1972 rdtscll(intr0);
1973#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001974 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001975#ifdef WITH_TSC
1976 rdtscll(intr1);
1977#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001978 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001979 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001980 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001981 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001982
Thomas Wouters52152252000-08-17 22:55:00 +00001983 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001984 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001985 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001986#ifdef WITH_TSC
1987 rdtscll(intr0);
1988#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001989 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001990#ifdef WITH_TSC
1991 rdtscll(intr1);
1992#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001993 PUSH(x);
1994 if (x != NULL) continue;
1995 break;
1996
Guido van Rossum374a9221991-04-04 10:40:29 +00001997 case JUMP_FORWARD:
1998 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00001999 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002000
Raymond Hettingerf606f872003-03-16 03:11:04 +00002001 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002002 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002003 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002004 if (w == Py_True) {
2005 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002006 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002007 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002008 if (w == Py_False) {
2009 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002010 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002011 }
2012 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002013 if (err > 0)
2014 err = 0;
2015 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002016 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002017 else
2018 break;
2019 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002020
Raymond Hettingerf606f872003-03-16 03:11:04 +00002021 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002022 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002023 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002024 if (w == Py_False) {
2025 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002026 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002027 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002028 if (w == Py_True) {
2029 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002030 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002031 }
2032 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002033 if (err > 0) {
2034 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002035 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002036 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002037 else if (err == 0)
2038 ;
2039 else
2040 break;
2041 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002042
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002043 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002044 case JUMP_ABSOLUTE:
2045 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002046 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002047
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002048 case GET_ITER:
2049 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002050 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002051 x = PyObject_GetIter(v);
2052 Py_DECREF(v);
2053 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002054 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002055 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002056 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002057 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002058 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002059 break;
2060
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002061 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002062 case FOR_ITER:
2063 /* before: [iter]; after: [iter, iter()] *or* [] */
2064 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002065 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002066 if (x != NULL) {
2067 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002068 PREDICT(STORE_FAST);
2069 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002070 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002071 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002072 if (PyErr_Occurred()) {
2073 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2074 break;
2075 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002076 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002077 /* iterator ended normally */
2078 x = v = POP();
2079 Py_DECREF(v);
2080 JUMPBY(oparg);
2081 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002082
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002083 case BREAK_LOOP:
2084 why = WHY_BREAK;
2085 goto fast_block_end;
2086
2087 case CONTINUE_LOOP:
2088 retval = PyInt_FromLong(oparg);
2089 why = WHY_CONTINUE;
2090 goto fast_block_end;
2091
Guido van Rossum374a9221991-04-04 10:40:29 +00002092 case SETUP_LOOP:
2093 case SETUP_EXCEPT:
2094 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002095 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002096 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002097 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002098
Guido van Rossumf10570b1995-07-07 22:53:21 +00002099 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002100 {
2101 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002102 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002103 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002104#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002105 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002106#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002107 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002108#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002109 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002110 PUSH(x);
2111 if (x != NULL)
2112 continue;
2113 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002114 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002115
Jeremy Hylton76901512000-03-28 23:49:17 +00002116 case CALL_FUNCTION_VAR:
2117 case CALL_FUNCTION_KW:
2118 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002119 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002120 int na = oparg & 0xff;
2121 int nk = (oparg>>8) & 0xff;
2122 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002123 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002124 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002125 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002126 if (flags & CALL_FLAG_VAR)
2127 n++;
2128 if (flags & CALL_FLAG_KW)
2129 n++;
2130 pfunc = stack_pointer - n - 1;
2131 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002132
Guido van Rossumac7be682001-01-17 15:42:30 +00002133 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002134 && PyMethod_GET_SELF(func) != NULL) {
2135 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002136 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002137 func = PyMethod_GET_FUNCTION(func);
2138 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002139 Py_DECREF(*pfunc);
2140 *pfunc = self;
2141 na++;
2142 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002143 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002144 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002145 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002146#ifdef WITH_TSC
2147 rdtscll(intr0);
2148#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002149 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002150#ifdef WITH_TSC
2151 rdtscll(intr1);
2152#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002153 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002154 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002155
Jeremy Hylton76901512000-03-28 23:49:17 +00002156 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002157 w = POP();
2158 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002159 }
2160 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002161 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002162 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002163 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002164 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002165
Guido van Rossum681d79a1995-07-18 14:51:37 +00002166 case MAKE_FUNCTION:
2167 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002168 x = PyFunction_New(v, f->f_globals);
2169 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002170 /* XXX Maybe this should be a separate opcode? */
2171 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002172 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002173 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002174 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002175 x = NULL;
2176 break;
2177 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002178 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002179 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002180 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002181 }
2182 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002183 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002184 }
2185 PUSH(x);
2186 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002187
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002188 case MAKE_CLOSURE:
2189 {
2190 int nfree;
2191 v = POP(); /* code object */
2192 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002193 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002194 Py_DECREF(v);
2195 /* XXX Maybe this should be a separate opcode? */
2196 if (x != NULL && nfree > 0) {
2197 v = PyTuple_New(nfree);
2198 if (v == NULL) {
2199 Py_DECREF(x);
2200 x = NULL;
2201 break;
2202 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002203 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002204 w = POP();
2205 PyTuple_SET_ITEM(v, nfree, w);
2206 }
2207 err = PyFunction_SetClosure(x, v);
2208 Py_DECREF(v);
2209 }
2210 if (x != NULL && oparg > 0) {
2211 v = PyTuple_New(oparg);
2212 if (v == NULL) {
2213 Py_DECREF(x);
2214 x = NULL;
2215 break;
2216 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002217 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002218 w = POP();
2219 PyTuple_SET_ITEM(v, oparg, w);
2220 }
2221 err = PyFunction_SetDefaults(x, v);
2222 Py_DECREF(v);
2223 }
2224 PUSH(x);
2225 break;
2226 }
2227
Guido van Rossum8861b741996-07-30 16:49:37 +00002228 case BUILD_SLICE:
2229 if (oparg == 3)
2230 w = POP();
2231 else
2232 w = NULL;
2233 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002234 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002235 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002236 Py_DECREF(u);
2237 Py_DECREF(v);
2238 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002239 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002240 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002241 break;
2242
Fred Drakeef8ace32000-08-24 00:32:09 +00002243 case EXTENDED_ARG:
2244 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002245 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002246 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002247
Guido van Rossum374a9221991-04-04 10:40:29 +00002248 default:
2249 fprintf(stderr,
2250 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002251 PyCode_Addr2Line(f->f_code, f->f_lasti),
2252 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002253 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002254 why = WHY_EXCEPTION;
2255 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002256
2257#ifdef CASE_TOO_BIG
2258 }
2259#endif
2260
Guido van Rossum374a9221991-04-04 10:40:29 +00002261 } /* switch */
2262
2263 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002264
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002265#ifdef WITH_TSC
2266 rdtscll(inst1);
2267#endif
2268
Guido van Rossum374a9221991-04-04 10:40:29 +00002269 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002270
Guido van Rossum374a9221991-04-04 10:40:29 +00002271 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002272 if (err == 0 && x != NULL) {
2273#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002274 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002275 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002276 fprintf(stderr,
2277 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002278 else {
2279#endif
2280#ifdef WITH_TSC
2281 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002282#endif
2283 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002284#ifdef CHECKEXC
2285 }
2286#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002287 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002288 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002289 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002290 err = 0;
2291 }
2292
Guido van Rossum374a9221991-04-04 10:40:29 +00002293 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002294
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002295 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002296 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002297 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002298 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002299 why = WHY_EXCEPTION;
2300 }
2301 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002302#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002303 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002304 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002305 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002306 char buf[1024];
2307 sprintf(buf, "Stack unwind with exception "
2308 "set and why=%d", why);
2309 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002310 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002311 }
2312#endif
2313
2314 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002315
Guido van Rossum374a9221991-04-04 10:40:29 +00002316 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002317 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002318
Fred Drake8f51f542001-10-04 14:48:42 +00002319 if (tstate->c_tracefunc != NULL)
2320 call_exc_trace(tstate->c_tracefunc,
2321 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002322 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Guido van Rossum374a9221991-04-04 10:40:29 +00002326 if (why == WHY_RERAISE)
2327 why = WHY_EXCEPTION;
2328
2329 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002330
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002331fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002332 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002333 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002334
Tim Peters8a5c3c72004-04-05 19:36:21 +00002335 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002336 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2337 /* For a continue inside a try block,
2338 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002339 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2340 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002341 why = WHY_NOT;
2342 JUMPTO(PyInt_AS_LONG(retval));
2343 Py_DECREF(retval);
2344 break;
2345 }
2346
Guido van Rossum374a9221991-04-04 10:40:29 +00002347 while (STACK_LEVEL() > b->b_level) {
2348 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002349 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002350 }
2351 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2352 why = WHY_NOT;
2353 JUMPTO(b->b_handler);
2354 break;
2355 }
2356 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002357 (b->b_type == SETUP_EXCEPT &&
2358 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002359 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002360 PyObject *exc, *val, *tb;
2361 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002362 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002363 val = Py_None;
2364 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 /* Make the raw exception data
2367 available to the handler,
2368 so a program can emulate the
2369 Python main loop. Don't do
2370 this for 'finally'. */
2371 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002372 PyErr_NormalizeException(
2373 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002374 set_exc_info(tstate,
2375 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002376 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002377 if (tb == NULL) {
2378 Py_INCREF(Py_None);
2379 PUSH(Py_None);
2380 } else
2381 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002382 PUSH(val);
2383 PUSH(exc);
2384 }
2385 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002386 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002387 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002388 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002389 PUSH(v);
2390 }
2391 why = WHY_NOT;
2392 JUMPTO(b->b_handler);
2393 break;
2394 }
2395 } /* unwind stack */
2396
2397 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Guido van Rossum374a9221991-04-04 10:40:29 +00002399 if (why != WHY_NOT)
2400 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002401#ifdef WITH_TSC
2402 rdtscll(loop1);
2403#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002404
Guido van Rossum374a9221991-04-04 10:40:29 +00002405 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002406
Tim Peters8a5c3c72004-04-05 19:36:21 +00002407 assert(why != WHY_YIELD);
2408 /* Pop remaining stack entries. */
2409 while (!EMPTY()) {
2410 v = POP();
2411 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002412 }
2413
Tim Peters8a5c3c72004-04-05 19:36:21 +00002414 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002415 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002416
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002417fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002418 if (tstate->use_tracing) {
2419 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002420 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002421 if (call_trace(tstate->c_tracefunc,
2422 tstate->c_traceobj, f,
2423 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002424 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002425 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002426 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002427 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002428 }
Fred Drake8f51f542001-10-04 14:48:42 +00002429 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002430 if (why == WHY_EXCEPTION)
2431 call_trace_protected(tstate->c_profilefunc,
2432 tstate->c_profileobj, f,
2433 PyTrace_RETURN);
2434 else if (call_trace(tstate->c_profilefunc,
2435 tstate->c_profileobj, f,
2436 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002437 Py_XDECREF(retval);
2438 retval = NULL;
2439 why = WHY_EXCEPTION;
2440 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002441 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002442 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002443
Guido van Rossuma027efa1997-05-05 20:56:21 +00002444 reset_exc_info(tstate);
2445
Tim Peters5ca576e2001-06-18 22:08:13 +00002446 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002447 exit_eval_frame:
2448 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002449 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002450
Guido van Rossum96a42c81992-01-12 02:29:51 +00002451 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002452}
2453
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002454/* this is gonna seem *real weird*, but if you put some other code between
2455 eval_frame() and PyEval_EvalCodeEx() you will need to adjust the test in
2456 the if statement in Misc/gdbinit:ppystack */
2457
Tim Peters6d6c1a32001-08-02 04:15:00 +00002458PyObject *
2459PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002460 PyObject **args, int argcount, PyObject **kws, int kwcount,
2461 PyObject **defs, int defcount, PyObject *closure)
2462{
2463 register PyFrameObject *f;
2464 register PyObject *retval = NULL;
2465 register PyObject **fastlocals, **freevars;
2466 PyThreadState *tstate = PyThreadState_GET();
2467 PyObject *x, *u;
2468
2469 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002470 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002471 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002472 return NULL;
2473 }
2474
Jeremy Hylton985eba52003-02-05 23:13:00 +00002475 assert(globals != NULL);
2476 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002477 if (f == NULL)
2478 return NULL;
2479
2480 fastlocals = f->f_localsplus;
2481 freevars = f->f_localsplus + f->f_nlocals;
2482
2483 if (co->co_argcount > 0 ||
2484 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2485 int i;
2486 int n = argcount;
2487 PyObject *kwdict = NULL;
2488 if (co->co_flags & CO_VARKEYWORDS) {
2489 kwdict = PyDict_New();
2490 if (kwdict == NULL)
2491 goto fail;
2492 i = co->co_argcount;
2493 if (co->co_flags & CO_VARARGS)
2494 i++;
2495 SETLOCAL(i, kwdict);
2496 }
2497 if (argcount > co->co_argcount) {
2498 if (!(co->co_flags & CO_VARARGS)) {
2499 PyErr_Format(PyExc_TypeError,
2500 "%.200s() takes %s %d "
2501 "%sargument%s (%d given)",
2502 PyString_AsString(co->co_name),
2503 defcount ? "at most" : "exactly",
2504 co->co_argcount,
2505 kwcount ? "non-keyword " : "",
2506 co->co_argcount == 1 ? "" : "s",
2507 argcount);
2508 goto fail;
2509 }
2510 n = co->co_argcount;
2511 }
2512 for (i = 0; i < n; i++) {
2513 x = args[i];
2514 Py_INCREF(x);
2515 SETLOCAL(i, x);
2516 }
2517 if (co->co_flags & CO_VARARGS) {
2518 u = PyTuple_New(argcount - n);
2519 if (u == NULL)
2520 goto fail;
2521 SETLOCAL(co->co_argcount, u);
2522 for (i = n; i < argcount; i++) {
2523 x = args[i];
2524 Py_INCREF(x);
2525 PyTuple_SET_ITEM(u, i-n, x);
2526 }
2527 }
2528 for (i = 0; i < kwcount; i++) {
2529 PyObject *keyword = kws[2*i];
2530 PyObject *value = kws[2*i + 1];
2531 int j;
2532 if (keyword == NULL || !PyString_Check(keyword)) {
2533 PyErr_Format(PyExc_TypeError,
2534 "%.200s() keywords must be strings",
2535 PyString_AsString(co->co_name));
2536 goto fail;
2537 }
2538 /* XXX slow -- speed up using dictionary? */
2539 for (j = 0; j < co->co_argcount; j++) {
2540 PyObject *nm = PyTuple_GET_ITEM(
2541 co->co_varnames, j);
2542 int cmp = PyObject_RichCompareBool(
2543 keyword, nm, Py_EQ);
2544 if (cmp > 0)
2545 break;
2546 else if (cmp < 0)
2547 goto fail;
2548 }
2549 /* Check errors from Compare */
2550 if (PyErr_Occurred())
2551 goto fail;
2552 if (j >= co->co_argcount) {
2553 if (kwdict == NULL) {
2554 PyErr_Format(PyExc_TypeError,
2555 "%.200s() got an unexpected "
2556 "keyword argument '%.400s'",
2557 PyString_AsString(co->co_name),
2558 PyString_AsString(keyword));
2559 goto fail;
2560 }
2561 PyDict_SetItem(kwdict, keyword, value);
2562 }
2563 else {
2564 if (GETLOCAL(j) != NULL) {
2565 PyErr_Format(PyExc_TypeError,
2566 "%.200s() got multiple "
2567 "values for keyword "
2568 "argument '%.400s'",
2569 PyString_AsString(co->co_name),
2570 PyString_AsString(keyword));
2571 goto fail;
2572 }
2573 Py_INCREF(value);
2574 SETLOCAL(j, value);
2575 }
2576 }
2577 if (argcount < co->co_argcount) {
2578 int m = co->co_argcount - defcount;
2579 for (i = argcount; i < m; i++) {
2580 if (GETLOCAL(i) == NULL) {
2581 PyErr_Format(PyExc_TypeError,
2582 "%.200s() takes %s %d "
2583 "%sargument%s (%d given)",
2584 PyString_AsString(co->co_name),
2585 ((co->co_flags & CO_VARARGS) ||
2586 defcount) ? "at least"
2587 : "exactly",
2588 m, kwcount ? "non-keyword " : "",
2589 m == 1 ? "" : "s", i);
2590 goto fail;
2591 }
2592 }
2593 if (n > m)
2594 i = n - m;
2595 else
2596 i = 0;
2597 for (; i < defcount; i++) {
2598 if (GETLOCAL(m+i) == NULL) {
2599 PyObject *def = defs[i];
2600 Py_INCREF(def);
2601 SETLOCAL(m+i, def);
2602 }
2603 }
2604 }
2605 }
2606 else {
2607 if (argcount > 0 || kwcount > 0) {
2608 PyErr_Format(PyExc_TypeError,
2609 "%.200s() takes no arguments (%d given)",
2610 PyString_AsString(co->co_name),
2611 argcount + kwcount);
2612 goto fail;
2613 }
2614 }
2615 /* Allocate and initialize storage for cell vars, and copy free
2616 vars into frame. This isn't too efficient right now. */
2617 if (f->f_ncells) {
2618 int i = 0, j = 0, nargs, found;
2619 char *cellname, *argname;
2620 PyObject *c;
2621
2622 nargs = co->co_argcount;
2623 if (co->co_flags & CO_VARARGS)
2624 nargs++;
2625 if (co->co_flags & CO_VARKEYWORDS)
2626 nargs++;
2627
2628 /* Check for cells that shadow args */
2629 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2630 cellname = PyString_AS_STRING(
2631 PyTuple_GET_ITEM(co->co_cellvars, i));
2632 found = 0;
2633 while (j < nargs) {
2634 argname = PyString_AS_STRING(
2635 PyTuple_GET_ITEM(co->co_varnames, j));
2636 if (strcmp(cellname, argname) == 0) {
2637 c = PyCell_New(GETLOCAL(j));
2638 if (c == NULL)
2639 goto fail;
2640 GETLOCAL(f->f_nlocals + i) = c;
2641 found = 1;
2642 break;
2643 }
2644 j++;
2645 }
2646 if (found == 0) {
2647 c = PyCell_New(NULL);
2648 if (c == NULL)
2649 goto fail;
2650 SETLOCAL(f->f_nlocals + i, c);
2651 }
2652 }
2653 /* Initialize any that are left */
2654 while (i < f->f_ncells) {
2655 c = PyCell_New(NULL);
2656 if (c == NULL)
2657 goto fail;
2658 SETLOCAL(f->f_nlocals + i, c);
2659 i++;
2660 }
2661 }
2662 if (f->f_nfreevars) {
2663 int i;
2664 for (i = 0; i < f->f_nfreevars; ++i) {
2665 PyObject *o = PyTuple_GET_ITEM(closure, i);
2666 Py_INCREF(o);
2667 freevars[f->f_ncells + i] = o;
2668 }
2669 }
2670
Tim Peters5ca576e2001-06-18 22:08:13 +00002671 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002672 /* Don't need to keep the reference to f_back, it will be set
2673 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002674 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002675 f->f_back = NULL;
2676
Jeremy Hylton985eba52003-02-05 23:13:00 +00002677 PCALL(PCALL_GENERATOR);
2678
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002679 /* Create a new generator that owns the ready to run frame
2680 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002681 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002682 }
2683
2684 retval = eval_frame(f);
2685
2686 fail: /* Jump here from prelude on failure */
2687
Tim Petersb13680b2001-11-27 23:29:29 +00002688 /* decref'ing the frame can cause __del__ methods to get invoked,
2689 which can call back into Python. While we're done with the
2690 current Python frame (f), the associated C stack is still in use,
2691 so recursion_depth must be boosted for the duration.
2692 */
2693 assert(tstate != NULL);
2694 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002695 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002696 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002697 return retval;
2698}
2699
2700
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002701/* Implementation notes for set_exc_info() and reset_exc_info():
2702
2703- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2704 'exc_traceback'. These always travel together.
2705
2706- tstate->curexc_ZZZ is the "hot" exception that is set by
2707 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2708
2709- Once an exception is caught by an except clause, it is transferred
2710 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2711 can pick it up. This is the primary task of set_exc_info().
2712
2713- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2714
2715 Long ago, when none of this existed, there were just a few globals:
2716 one set corresponding to the "hot" exception, and one set
2717 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2718 globals; they were simply stored as sys.exc_ZZZ. For backwards
2719 compatibility, they still are!) The problem was that in code like
2720 this:
2721
2722 try:
2723 "something that may fail"
2724 except "some exception":
2725 "do something else first"
2726 "print the exception from sys.exc_ZZZ."
2727
2728 if "do something else first" invoked something that raised and caught
2729 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2730 cause of subtle bugs. I fixed this by changing the semantics as
2731 follows:
2732
2733 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2734 *in that frame*.
2735
2736 - But initially, and as long as no exception is caught in a given
2737 frame, sys.exc_ZZZ will hold the last exception caught in the
2738 previous frame (or the frame before that, etc.).
2739
2740 The first bullet fixed the bug in the above example. The second
2741 bullet was for backwards compatibility: it was (and is) common to
2742 have a function that is called when an exception is caught, and to
2743 have that function access the caught exception via sys.exc_ZZZ.
2744 (Example: traceback.print_exc()).
2745
2746 At the same time I fixed the problem that sys.exc_ZZZ weren't
2747 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2748 but that's really a separate improvement.
2749
2750 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2751 variables to what they were before the current frame was called. The
2752 set_exc_info() function saves them on the frame so that
2753 reset_exc_info() can restore them. The invariant is that
2754 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2755 exception (where "catching" an exception applies only to successful
2756 except clauses); and if the current frame ever caught an exception,
2757 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2758 at the start of the current frame.
2759
2760*/
2761
Guido van Rossuma027efa1997-05-05 20:56:21 +00002762static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002763set_exc_info(PyThreadState *tstate,
2764 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002765{
2766 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002767 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002768
Guido van Rossuma027efa1997-05-05 20:56:21 +00002769 frame = tstate->frame;
2770 if (frame->f_exc_type == NULL) {
2771 /* This frame didn't catch an exception before */
2772 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002773 if (tstate->exc_type == NULL) {
2774 Py_INCREF(Py_None);
2775 tstate->exc_type = Py_None;
2776 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002777 tmp_type = frame->f_exc_type;
2778 tmp_value = frame->f_exc_value;
2779 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002780 Py_XINCREF(tstate->exc_type);
2781 Py_XINCREF(tstate->exc_value);
2782 Py_XINCREF(tstate->exc_traceback);
2783 frame->f_exc_type = tstate->exc_type;
2784 frame->f_exc_value = tstate->exc_value;
2785 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002786 Py_XDECREF(tmp_type);
2787 Py_XDECREF(tmp_value);
2788 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002789 }
2790 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002791 tmp_type = tstate->exc_type;
2792 tmp_value = tstate->exc_value;
2793 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002794 Py_XINCREF(type);
2795 Py_XINCREF(value);
2796 Py_XINCREF(tb);
2797 tstate->exc_type = type;
2798 tstate->exc_value = value;
2799 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002800 Py_XDECREF(tmp_type);
2801 Py_XDECREF(tmp_value);
2802 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002803 /* For b/w compatibility */
2804 PySys_SetObject("exc_type", type);
2805 PySys_SetObject("exc_value", value);
2806 PySys_SetObject("exc_traceback", tb);
2807}
2808
2809static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002810reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002811{
2812 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002813 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002814 frame = tstate->frame;
2815 if (frame->f_exc_type != NULL) {
2816 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002817 tmp_type = tstate->exc_type;
2818 tmp_value = tstate->exc_value;
2819 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002820 Py_XINCREF(frame->f_exc_type);
2821 Py_XINCREF(frame->f_exc_value);
2822 Py_XINCREF(frame->f_exc_traceback);
2823 tstate->exc_type = frame->f_exc_type;
2824 tstate->exc_value = frame->f_exc_value;
2825 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002826 Py_XDECREF(tmp_type);
2827 Py_XDECREF(tmp_value);
2828 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002829 /* For b/w compatibility */
2830 PySys_SetObject("exc_type", frame->f_exc_type);
2831 PySys_SetObject("exc_value", frame->f_exc_value);
2832 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2833 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002834 tmp_type = frame->f_exc_type;
2835 tmp_value = frame->f_exc_value;
2836 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002837 frame->f_exc_type = NULL;
2838 frame->f_exc_value = NULL;
2839 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002840 Py_XDECREF(tmp_type);
2841 Py_XDECREF(tmp_value);
2842 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002843}
2844
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002845/* Logic for the raise statement (too complicated for inlining).
2846 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002847static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002848do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002849{
Guido van Rossumd295f121998-04-09 21:39:57 +00002850 if (type == NULL) {
2851 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002852 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002853 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2854 value = tstate->exc_value;
2855 tb = tstate->exc_traceback;
2856 Py_XINCREF(type);
2857 Py_XINCREF(value);
2858 Py_XINCREF(tb);
2859 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002860
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002861 /* We support the following forms of raise:
2862 raise <class>, <classinstance>
2863 raise <class>, <argument tuple>
2864 raise <class>, None
2865 raise <class>, <argument>
2866 raise <classinstance>, None
2867 raise <string>, <object>
2868 raise <string>, None
2869
2870 An omitted second argument is the same as None.
2871
2872 In addition, raise <tuple>, <anything> is the same as
2873 raising the tuple's first item (and it better have one!);
2874 this rule is applied recursively.
2875
2876 Finally, an optional third argument can be supplied, which
2877 gives the traceback to be substituted (useful when
2878 re-raising an exception after examining it). */
2879
2880 /* First, check the traceback argument, replacing None with
2881 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002882 if (tb == Py_None) {
2883 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002884 tb = NULL;
2885 }
2886 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002887 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002888 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002889 goto raise_error;
2890 }
2891
2892 /* Next, replace a missing value with None */
2893 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002894 value = Py_None;
2895 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002896 }
2897
2898 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002899 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2900 PyObject *tmp = type;
2901 type = PyTuple_GET_ITEM(type, 0);
2902 Py_INCREF(type);
2903 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002904 }
2905
Tim Petersafb2c802002-04-18 18:06:20 +00002906 if (PyString_CheckExact(type))
2907 /* Raising builtin string is deprecated but still allowed --
2908 * do nothing. Raising an instance of a new-style str
2909 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002910 PyErr_Warn(PyExc_PendingDeprecationWarning,
2911 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002912
2913 else if (PyClass_Check(type))
2914 PyErr_NormalizeException(&type, &value, &tb);
2915
Guido van Rossumb209a111997-04-29 18:18:01 +00002916 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002917 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002918 if (value != Py_None) {
2919 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002920 "instance exception may not have a separate value");
2921 goto raise_error;
2922 }
2923 else {
2924 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002925 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002926 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002927 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2928 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002929 }
2930 }
2931 else {
2932 /* Not something you can raise. You get an exception
2933 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002934 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002935 "exceptions must be classes, instances, or "
2936 "strings (deprecated), not %s",
2937 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002938 goto raise_error;
2939 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002940 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002941 if (tb == NULL)
2942 return WHY_EXCEPTION;
2943 else
2944 return WHY_RERAISE;
2945 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002946 Py_XDECREF(value);
2947 Py_XDECREF(type);
2948 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002949 return WHY_EXCEPTION;
2950}
2951
Tim Petersd6d010b2001-06-21 02:49:55 +00002952/* Iterate v argcnt times and store the results on the stack (via decreasing
2953 sp). Return 1 for success, 0 if error. */
2954
Barry Warsawe42b18f1997-08-25 22:13:04 +00002955static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002956unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002957{
Tim Petersd6d010b2001-06-21 02:49:55 +00002958 int i = 0;
2959 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002960 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002961
Tim Petersd6d010b2001-06-21 02:49:55 +00002962 assert(v != NULL);
2963
2964 it = PyObject_GetIter(v);
2965 if (it == NULL)
2966 goto Error;
2967
2968 for (; i < argcnt; i++) {
2969 w = PyIter_Next(it);
2970 if (w == NULL) {
2971 /* Iterator done, via error or exhaustion. */
2972 if (!PyErr_Occurred()) {
2973 PyErr_Format(PyExc_ValueError,
2974 "need more than %d value%s to unpack",
2975 i, i == 1 ? "" : "s");
2976 }
2977 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002978 }
2979 *--sp = w;
2980 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002981
2982 /* We better have exhausted the iterator now. */
2983 w = PyIter_Next(it);
2984 if (w == NULL) {
2985 if (PyErr_Occurred())
2986 goto Error;
2987 Py_DECREF(it);
2988 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002989 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002990 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002991 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002992 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002993Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002994 for (; i > 0; i--, sp++)
2995 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002996 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002997 return 0;
2998}
2999
3000
Guido van Rossum96a42c81992-01-12 02:29:51 +00003001#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003002static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003003prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003004{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003005 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003006 if (PyObject_Print(v, stdout, 0) != 0)
3007 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003008 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003009 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003010}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003011#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003012
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003013static void
Fred Drake5755ce62001-06-27 19:19:46 +00003014call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003015{
Guido van Rossumb209a111997-04-29 18:18:01 +00003016 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003017 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003018 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003019 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003020 value = Py_None;
3021 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003022 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003023 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003024 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003025 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003026 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003027 }
Fred Drake5755ce62001-06-27 19:19:46 +00003028 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003029 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003030 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003031 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003032 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003033 Py_XDECREF(type);
3034 Py_XDECREF(value);
3035 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003036 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003037}
3038
Fred Drake4ec5d562001-10-04 19:26:43 +00003039static void
3040call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3041 int what)
3042{
3043 PyObject *type, *value, *traceback;
3044 int err;
3045 PyErr_Fetch(&type, &value, &traceback);
3046 err = call_trace(func, obj, frame, what, NULL);
3047 if (err == 0)
3048 PyErr_Restore(type, value, traceback);
3049 else {
3050 Py_XDECREF(type);
3051 Py_XDECREF(value);
3052 Py_XDECREF(traceback);
3053 }
3054}
3055
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003056static int
Fred Drake5755ce62001-06-27 19:19:46 +00003057call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3058 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003059{
Fred Drake5755ce62001-06-27 19:19:46 +00003060 register PyThreadState *tstate = frame->f_tstate;
3061 int result;
3062 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003063 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003064 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003065 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003066 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003067 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3068 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003069 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003070 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003071}
3072
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003073PyObject *
3074_PyEval_CallTracing(PyObject *func, PyObject *args)
3075{
3076 PyFrameObject *frame = PyEval_GetFrame();
3077 PyThreadState *tstate = frame->f_tstate;
3078 int save_tracing = tstate->tracing;
3079 int save_use_tracing = tstate->use_tracing;
3080 PyObject *result;
3081
3082 tstate->tracing = 0;
3083 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3084 || (tstate->c_profilefunc != NULL));
3085 result = PyObject_Call(func, args, NULL);
3086 tstate->tracing = save_tracing;
3087 tstate->use_tracing = save_use_tracing;
3088 return result;
3089}
3090
Michael W. Hudson006c7522002-11-08 13:08:46 +00003091static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003092maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003093 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3094 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003095{
3096 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003097
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003098 In a nutshell, we use the co_lnotab field of the code object
3099 to tell when execution has moved onto a different line.
3100
3101 As mentioned above, the basic idea is so set things up so
3102 that
3103
3104 *instr_lb <= frame->f_lasti < *instr_ub
3105
3106 is true so long as execution does not change lines.
3107
3108 This is all fairly simple. Digging the information out of
3109 co_lnotab takes some work, but is conceptually clear.
3110
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003111 Somewhat harder to explain is why we don't *always* call the
3112 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003113
3114 Consider this code:
3115
3116 1: def f(a):
3117 2: if a:
3118 3: print 1
3119 4: else:
3120 5: print 2
3121
3122 which compiles to this:
3123
3124 2 0 LOAD_FAST 0 (a)
3125 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003126 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003127
3128 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003129 10 PRINT_ITEM
3130 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003131 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003132 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003133
3134 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003135 19 PRINT_ITEM
3136 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003137 >> 21 LOAD_CONST 0 (None)
3138 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003139
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003140 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003141 15 and the co_lnotab will claim that execution has moved to
3142 line 3. This is at best misleading. In this case we could
3143 associate the POP_TOP with line 4, but that doesn't make
3144 sense in all cases (I think).
3145
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003146 What we do is only call the line trace function if the co_lnotab
3147 indicates we have jumped to the *start* of a line, i.e. if the
3148 current instruction offset matches the offset given for the
3149 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003150
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003151 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003152 Execution will jump from instruction offset 12 to offset 21.
3153 Then the co_lnotab would imply that execution has moved to line
3154 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003155
3156 Why do we set f_lineno when tracing? Well, consider the code
3157 above when 'a' is true. If stepping through this with 'n' in
3158 pdb, you would stop at line 1 with a "call" type event, then
3159 line events on lines 2 and 3, then a "return" type event -- but
3160 you would be shown line 5 during this event. This is a change
3161 from the behaviour in 2.2 and before, and I've found it
3162 confusing in practice. By setting and using f_lineno when
3163 tracing, one can report a line number different from that
3164 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003165 */
3166
Michael W. Hudson006c7522002-11-08 13:08:46 +00003167 int result = 0;
3168
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003169 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003171 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003172 unsigned char* p;
3173
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003174 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3175 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003176
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003177 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003178 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003179
3180 /* possible optimization: if f->f_lasti == instr_ub
3181 (likely to be a common case) then we already know
3182 instr_lb -- if we stored the matching value of p
3183 somwhere we could skip the first while loop. */
3184
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003185 /* see comments in compile.c for the description of
3186 co_lnotab. A point to remember: increments to p
3187 should come in pairs -- although we don't care about
3188 the line increments here, treating them as byte
3189 increments gets confusing, to say the least. */
3190
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003191 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003192 if (addr + *p > frame->f_lasti)
3193 break;
3194 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003195 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003196 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003197 --size;
3198 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003199
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003200 if (addr == frame->f_lasti) {
3201 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003202 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003203 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003204 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003205
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003206 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003207 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003208 addr += *p++;
3209 if (*p++)
3210 break;
3211 }
3212 *instr_ub = addr;
3213 }
3214 else {
3215 *instr_ub = INT_MAX;
3216 }
3217 }
Armin Rigobf57a142004-03-22 19:24:58 +00003218 else if (frame->f_lasti <= *instr_prev) {
3219 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003220 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003221 PyTrace_LINE, Py_None);
3222 }
3223 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003224 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003225}
3226
Fred Drake5755ce62001-06-27 19:19:46 +00003227void
3228PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003229{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003230 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003231 PyObject *temp = tstate->c_profileobj;
3232 Py_XINCREF(arg);
3233 tstate->c_profilefunc = NULL;
3234 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003235 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003236 Py_XDECREF(temp);
3237 tstate->c_profilefunc = func;
3238 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003239 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003240}
3241
3242void
3243PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3244{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003245 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003246 PyObject *temp = tstate->c_traceobj;
3247 Py_XINCREF(arg);
3248 tstate->c_tracefunc = NULL;
3249 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003250 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003251 Py_XDECREF(temp);
3252 tstate->c_tracefunc = func;
3253 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003254 tstate->use_tracing = ((func != NULL)
3255 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003256}
3257
Guido van Rossumb209a111997-04-29 18:18:01 +00003258PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003259PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003260{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003261 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003262 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003263 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003264 else
3265 return current_frame->f_builtins;
3266}
3267
Guido van Rossumb209a111997-04-29 18:18:01 +00003268PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003269PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003270{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003271 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003272 if (current_frame == NULL)
3273 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003274 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003275 return current_frame->f_locals;
3276}
3277
Guido van Rossumb209a111997-04-29 18:18:01 +00003278PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003279PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003280{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003281 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003282 if (current_frame == NULL)
3283 return NULL;
3284 else
3285 return current_frame->f_globals;
3286}
3287
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003288PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003289PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003290{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003291 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003292 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003293}
3294
Guido van Rossum6135a871995-01-09 17:53:26 +00003295int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003296PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003297{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003298 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003299 return current_frame == NULL ? 0 : current_frame->f_restricted;
3300}
3301
Guido van Rossumbe270261997-05-22 22:26:18 +00003302int
Tim Peters5ba58662001-07-16 02:29:45 +00003303PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003304{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003305 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003306 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003307
3308 if (current_frame != NULL) {
3309 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003310 const int compilerflags = codeflags & PyCF_MASK;
3311 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003312 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003313 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003314 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003315#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003316 if (codeflags & CO_GENERATOR_ALLOWED) {
3317 result = 1;
3318 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3319 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003320#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003321 }
3322 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003323}
3324
3325int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003326Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003327{
Guido van Rossumb209a111997-04-29 18:18:01 +00003328 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003329 if (f == NULL)
3330 return 0;
3331 if (!PyFile_SoftSpace(f, 0))
3332 return 0;
3333 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003334}
3335
Guido van Rossum3f5da241990-12-20 15:06:42 +00003336
Guido van Rossum681d79a1995-07-18 14:51:37 +00003337/* External interface to call any callable object.
3338 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003339
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003340#undef PyEval_CallObject
3341/* for backward compatibility: export this interface */
3342
Guido van Rossumb209a111997-04-29 18:18:01 +00003343PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003344PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003345{
Guido van Rossumb209a111997-04-29 18:18:01 +00003346 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003347}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003348#define PyEval_CallObject(func,arg) \
3349 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003350
Guido van Rossumb209a111997-04-29 18:18:01 +00003351PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003352PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003353{
Jeremy Hylton52820442001-01-03 23:52:36 +00003354 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003355
3356 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003357 arg = PyTuple_New(0);
3358 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003359 PyErr_SetString(PyExc_TypeError,
3360 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003361 return NULL;
3362 }
3363 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003364 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003365
Guido van Rossumb209a111997-04-29 18:18:01 +00003366 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003367 PyErr_SetString(PyExc_TypeError,
3368 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003369 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003370 return NULL;
3371 }
3372
Tim Peters6d6c1a32001-08-02 04:15:00 +00003373 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003374 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003375 return result;
3376}
3377
Tim Peters6d6c1a32001-08-02 04:15:00 +00003378char *
3379PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003380{
3381 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003382 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003383 else if (PyFunction_Check(func))
3384 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3385 else if (PyCFunction_Check(func))
3386 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3387 else if (PyClass_Check(func))
3388 return PyString_AsString(((PyClassObject*)func)->cl_name);
3389 else if (PyInstance_Check(func)) {
3390 return PyString_AsString(
3391 ((PyInstanceObject*)func)->in_class->cl_name);
3392 } else {
3393 return func->ob_type->tp_name;
3394 }
3395}
3396
Tim Peters6d6c1a32001-08-02 04:15:00 +00003397char *
3398PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003399{
3400 if (PyMethod_Check(func))
3401 return "()";
3402 else if (PyFunction_Check(func))
3403 return "()";
3404 else if (PyCFunction_Check(func))
3405 return "()";
3406 else if (PyClass_Check(func))
3407 return " constructor";
3408 else if (PyInstance_Check(func)) {
3409 return " instance";
3410 } else {
3411 return " object";
3412 }
3413}
3414
Martin v. Löwise440e472004-06-01 15:22:42 +00003415PyObject *
3416PyEval_EvaluateFrame(PyObject *fo)
3417{
3418 return eval_frame((PyFrameObject *)fo);
3419}
3420
Jeremy Hylton52820442001-01-03 23:52:36 +00003421#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3422
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003423static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003424err_args(PyObject *func, int flags, int nargs)
3425{
3426 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003427 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003428 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003429 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003430 nargs);
3431 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003432 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003433 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003434 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003435 nargs);
3436}
3437
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003438#define BEGIN_C_TRACE \
3439if (tstate->use_tracing) { \
3440 if (tstate->c_profilefunc != NULL) { \
3441 PyObject *func_name = \
3442 PyString_FromString (((PyCFunctionObject *) \
3443 func)->m_ml->ml_name); \
3444 are_tracing = 1; \
3445 if (call_trace(tstate->c_profilefunc, \
3446 tstate->c_profileobj, \
3447 tstate->frame, PyTrace_C_CALL, \
3448 func_name)) \
3449 { return NULL; } \
3450 Py_DECREF (func_name); \
3451 } \
3452 }
3453
3454#define END_C_TRACE \
3455 if (tstate->use_tracing && are_tracing) { \
3456 if (tstate->c_profilefunc != NULL) { \
3457 if (x == NULL) { \
3458 if (call_trace (tstate->c_profilefunc, \
3459 tstate->c_profileobj, \
3460 tstate->frame, PyTrace_C_EXCEPTION, \
3461 NULL)) \
3462 { return NULL; } \
3463 } else { \
3464 if (call_trace(tstate->c_profilefunc, \
3465 tstate->c_profileobj, \
3466 tstate->frame, PyTrace_C_RETURN, \
3467 NULL)) \
3468 { return NULL; } \
3469 } \
3470 } \
3471 }
3472
3473
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003474static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003475call_function(PyObject ***pp_stack, int oparg
3476#ifdef WITH_TSC
3477 , uint64* pintr0, uint64* pintr1
3478#endif
3479 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003480{
3481 int na = oparg & 0xff;
3482 int nk = (oparg>>8) & 0xff;
3483 int n = na + 2 * nk;
3484 PyObject **pfunc = (*pp_stack) - n - 1;
3485 PyObject *func = *pfunc;
3486 PyObject *x, *w;
3487
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003488 int are_tracing = 0;
3489
3490 PyThreadState *tstate = PyThreadState_GET();
3491
Jeremy Hylton985eba52003-02-05 23:13:00 +00003492 /* Always dispatch PyCFunction first, because these are
3493 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003494 */
3495 if (PyCFunction_Check(func) && nk == 0) {
3496 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003497 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003498 if (flags & (METH_NOARGS | METH_O)) {
3499 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3500 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003501 if (flags & METH_NOARGS && na == 0) {
3502 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003503 x = (*meth)(self, NULL);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003504 END_C_TRACE
3505 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003506 else if (flags & METH_O && na == 1) {
3507 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003508 BEGIN_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003509 x = (*meth)(self, arg);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003510 END_C_TRACE
Jeremy Hylton192690e2002-08-16 18:36:11 +00003511 Py_DECREF(arg);
3512 }
3513 else {
3514 err_args(func, flags, na);
3515 x = NULL;
3516 }
3517 }
3518 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003519 PyObject *callargs;
3520 callargs = load_args(pp_stack, na);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003521 BEGIN_C_TRACE
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003522#ifdef WITH_TSC
3523 rdtscll(*pintr0);
3524#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003525 x = PyCFunction_Call(func, callargs, NULL);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003526#ifdef WITH_TSC
3527 rdtscll(*pintr1);
3528#endif
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003529 END_C_TRACE
Tim Peters8a5c3c72004-04-05 19:36:21 +00003530 Py_XDECREF(callargs);
3531 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003532 } else {
3533 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3534 /* optimize access to bound methods */
3535 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003536 PCALL(PCALL_METHOD);
3537 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003538 Py_INCREF(self);
3539 func = PyMethod_GET_FUNCTION(func);
3540 Py_INCREF(func);
3541 Py_DECREF(*pfunc);
3542 *pfunc = self;
3543 na++;
3544 n++;
3545 } else
3546 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003547#ifdef WITH_TSC
3548 rdtscll(*pintr0);
3549#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003550 if (PyFunction_Check(func))
3551 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003552 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003553 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003554#ifdef WITH_TSC
3555 rdtscll(*pintr1);
3556#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003557 Py_DECREF(func);
3558 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003559
Jeremy Hylton985eba52003-02-05 23:13:00 +00003560 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003561 while ((*pp_stack) > pfunc) {
3562 w = EXT_POP(*pp_stack);
3563 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003564 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003565 }
3566 return x;
3567}
3568
Jeremy Hylton192690e2002-08-16 18:36:11 +00003569/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003570 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003571 For the simplest case -- a function that takes only positional
3572 arguments and is called with only positional arguments -- it
3573 inlines the most primitive frame setup code from
3574 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3575 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003576*/
3577
3578static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003579fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003580{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003581 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003582 PyObject *globals = PyFunction_GET_GLOBALS(func);
3583 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3584 PyObject **d = NULL;
3585 int nd = 0;
3586
Jeremy Hylton985eba52003-02-05 23:13:00 +00003587 PCALL(PCALL_FUNCTION);
3588 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003589 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003590 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3591 PyFrameObject *f;
3592 PyObject *retval = NULL;
3593 PyThreadState *tstate = PyThreadState_GET();
3594 PyObject **fastlocals, **stack;
3595 int i;
3596
3597 PCALL(PCALL_FASTER_FUNCTION);
3598 assert(globals != NULL);
3599 /* XXX Perhaps we should create a specialized
3600 PyFrame_New() that doesn't take locals, but does
3601 take builtins without sanity checking them.
3602 */
3603 f = PyFrame_New(tstate, co, globals, NULL);
3604 if (f == NULL)
3605 return NULL;
3606
3607 fastlocals = f->f_localsplus;
3608 stack = (*pp_stack) - n;
3609
3610 for (i = 0; i < n; i++) {
3611 Py_INCREF(*stack);
3612 fastlocals[i] = *stack++;
3613 }
3614 retval = eval_frame(f);
3615 assert(tstate != NULL);
3616 ++tstate->recursion_depth;
3617 Py_DECREF(f);
3618 --tstate->recursion_depth;
3619 return retval;
3620 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003621 if (argdefs != NULL) {
3622 d = &PyTuple_GET_ITEM(argdefs, 0);
3623 nd = ((PyTupleObject *)argdefs)->ob_size;
3624 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003625 return PyEval_EvalCodeEx(co, globals,
3626 (PyObject *)NULL, (*pp_stack)-n, na,
3627 (*pp_stack)-2*nk, nk, d, nd,
3628 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003629}
3630
3631static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003632update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3633 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003634{
3635 PyObject *kwdict = NULL;
3636 if (orig_kwdict == NULL)
3637 kwdict = PyDict_New();
3638 else {
3639 kwdict = PyDict_Copy(orig_kwdict);
3640 Py_DECREF(orig_kwdict);
3641 }
3642 if (kwdict == NULL)
3643 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003644 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003645 int err;
3646 PyObject *value = EXT_POP(*pp_stack);
3647 PyObject *key = EXT_POP(*pp_stack);
3648 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003649 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003650 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003651 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003652 PyEval_GetFuncName(func),
3653 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003654 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003655 Py_DECREF(key);
3656 Py_DECREF(value);
3657 Py_DECREF(kwdict);
3658 return NULL;
3659 }
3660 err = PyDict_SetItem(kwdict, key, value);
3661 Py_DECREF(key);
3662 Py_DECREF(value);
3663 if (err) {
3664 Py_DECREF(kwdict);
3665 return NULL;
3666 }
3667 }
3668 return kwdict;
3669}
3670
3671static PyObject *
3672update_star_args(int nstack, int nstar, PyObject *stararg,
3673 PyObject ***pp_stack)
3674{
3675 PyObject *callargs, *w;
3676
3677 callargs = PyTuple_New(nstack + nstar);
3678 if (callargs == NULL) {
3679 return NULL;
3680 }
3681 if (nstar) {
3682 int i;
3683 for (i = 0; i < nstar; i++) {
3684 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3685 Py_INCREF(a);
3686 PyTuple_SET_ITEM(callargs, nstack + i, a);
3687 }
3688 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003689 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003690 w = EXT_POP(*pp_stack);
3691 PyTuple_SET_ITEM(callargs, nstack, w);
3692 }
3693 return callargs;
3694}
3695
3696static PyObject *
3697load_args(PyObject ***pp_stack, int na)
3698{
3699 PyObject *args = PyTuple_New(na);
3700 PyObject *w;
3701
3702 if (args == NULL)
3703 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003704 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003705 w = EXT_POP(*pp_stack);
3706 PyTuple_SET_ITEM(args, na, w);
3707 }
3708 return args;
3709}
3710
3711static PyObject *
3712do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3713{
3714 PyObject *callargs = NULL;
3715 PyObject *kwdict = NULL;
3716 PyObject *result = NULL;
3717
3718 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003719 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003720 if (kwdict == NULL)
3721 goto call_fail;
3722 }
3723 callargs = load_args(pp_stack, na);
3724 if (callargs == NULL)
3725 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003726#ifdef CALL_PROFILE
3727 /* At this point, we have to look at the type of func to
3728 update the call stats properly. Do it here so as to avoid
3729 exposing the call stats machinery outside ceval.c
3730 */
3731 if (PyFunction_Check(func))
3732 PCALL(PCALL_FUNCTION);
3733 else if (PyMethod_Check(func))
3734 PCALL(PCALL_METHOD);
3735 else if (PyType_Check(func))
3736 PCALL(PCALL_TYPE);
3737 else
3738 PCALL(PCALL_OTHER);
3739#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003740 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003741 call_fail:
3742 Py_XDECREF(callargs);
3743 Py_XDECREF(kwdict);
3744 return result;
3745}
3746
3747static PyObject *
3748ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3749{
3750 int nstar = 0;
3751 PyObject *callargs = NULL;
3752 PyObject *stararg = NULL;
3753 PyObject *kwdict = NULL;
3754 PyObject *result = NULL;
3755
3756 if (flags & CALL_FLAG_KW) {
3757 kwdict = EXT_POP(*pp_stack);
3758 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003759 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003760 "%s%s argument after ** "
3761 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003762 PyEval_GetFuncName(func),
3763 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003764 goto ext_call_fail;
3765 }
3766 }
3767 if (flags & CALL_FLAG_VAR) {
3768 stararg = EXT_POP(*pp_stack);
3769 if (!PyTuple_Check(stararg)) {
3770 PyObject *t = NULL;
3771 t = PySequence_Tuple(stararg);
3772 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003773 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3774 PyErr_Format(PyExc_TypeError,
3775 "%s%s argument after * "
3776 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003777 PyEval_GetFuncName(func),
3778 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003779 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003780 goto ext_call_fail;
3781 }
3782 Py_DECREF(stararg);
3783 stararg = t;
3784 }
3785 nstar = PyTuple_GET_SIZE(stararg);
3786 }
3787 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003788 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003789 if (kwdict == NULL)
3790 goto ext_call_fail;
3791 }
3792 callargs = update_star_args(na, nstar, stararg, pp_stack);
3793 if (callargs == NULL)
3794 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003795#ifdef CALL_PROFILE
3796 /* At this point, we have to look at the type of func to
3797 update the call stats properly. Do it here so as to avoid
3798 exposing the call stats machinery outside ceval.c
3799 */
3800 if (PyFunction_Check(func))
3801 PCALL(PCALL_FUNCTION);
3802 else if (PyMethod_Check(func))
3803 PCALL(PCALL_METHOD);
3804 else if (PyType_Check(func))
3805 PCALL(PCALL_TYPE);
3806 else
3807 PCALL(PCALL_OTHER);
3808#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003809 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003810 ext_call_fail:
3811 Py_XDECREF(callargs);
3812 Py_XDECREF(kwdict);
3813 Py_XDECREF(stararg);
3814 return result;
3815}
3816
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003817#define SLICE_ERROR_MSG \
3818 "standard sequence type does not support step size other than one"
3819
Tim Peterscb479e72001-12-16 19:11:44 +00003820/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3821 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3822 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3823*/
Tim Petersb5196382001-12-16 19:44:20 +00003824/* Note: If v is NULL, return success without storing into *pi. This
3825 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3826 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003827*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003828int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003829_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003830{
Tim Petersb5196382001-12-16 19:44:20 +00003831 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003832 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003833 if (PyInt_Check(v)) {
3834 x = PyInt_AsLong(v);
3835 } else if (PyLong_Check(v)) {
3836 x = PyLong_AsLong(v);
3837 if (x==-1 && PyErr_Occurred()) {
3838 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003839 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003840
Guido van Rossumac7be682001-01-17 15:42:30 +00003841 if (!PyErr_ExceptionMatches(
3842 PyExc_OverflowError)) {
3843 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003844 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003845 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003846 }
3847
Guido van Rossumac7be682001-01-17 15:42:30 +00003848 /* Clear the OverflowError */
3849 PyErr_Clear();
3850
3851 /* It's an overflow error, so we need to
3852 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003853 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003854 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003855
3856 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003857 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003858 if (long_zero == NULL)
3859 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003860
3861 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003862 cmp = PyObject_RichCompareBool(v, long_zero,
3863 Py_GT);
3864 Py_DECREF(long_zero);
3865 if (cmp < 0)
3866 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003867 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003868 x = INT_MAX;
3869 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003870 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003871 }
3872 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003873 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003874 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003875 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003876 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003877 /* Truncate -- very long indices are truncated anyway */
3878 if (x > INT_MAX)
3879 x = INT_MAX;
3880 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003881 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003882 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003883 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003884 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003885}
3886
Guido van Rossum50d756e2001-08-18 17:43:36 +00003887#undef ISINT
3888#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3889
Guido van Rossumb209a111997-04-29 18:18:01 +00003890static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003891apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003892{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003893 PyTypeObject *tp = u->ob_type;
3894 PySequenceMethods *sq = tp->tp_as_sequence;
3895
3896 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3897 int ilow = 0, ihigh = INT_MAX;
3898 if (!_PyEval_SliceIndex(v, &ilow))
3899 return NULL;
3900 if (!_PyEval_SliceIndex(w, &ihigh))
3901 return NULL;
3902 return PySequence_GetSlice(u, ilow, ihigh);
3903 }
3904 else {
3905 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003906 if (slice != NULL) {
3907 PyObject *res = PyObject_GetItem(u, slice);
3908 Py_DECREF(slice);
3909 return res;
3910 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003911 else
3912 return NULL;
3913 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003914}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003915
3916static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003917assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3918 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003919{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003920 PyTypeObject *tp = u->ob_type;
3921 PySequenceMethods *sq = tp->tp_as_sequence;
3922
3923 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3924 int ilow = 0, ihigh = INT_MAX;
3925 if (!_PyEval_SliceIndex(v, &ilow))
3926 return -1;
3927 if (!_PyEval_SliceIndex(w, &ihigh))
3928 return -1;
3929 if (x == NULL)
3930 return PySequence_DelSlice(u, ilow, ihigh);
3931 else
3932 return PySequence_SetSlice(u, ilow, ihigh, x);
3933 }
3934 else {
3935 PyObject *slice = PySlice_New(v, w, NULL);
3936 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003937 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003938 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003939 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003940 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003941 res = PyObject_DelItem(u, slice);
3942 Py_DECREF(slice);
3943 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003944 }
3945 else
3946 return -1;
3947 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003948}
3949
Guido van Rossumb209a111997-04-29 18:18:01 +00003950static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003951cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003952{
Guido van Rossumac7be682001-01-17 15:42:30 +00003953 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003954 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003955 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003956 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003957 break;
3958 case PyCmp_IS_NOT:
3959 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003960 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003961 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003962 res = PySequence_Contains(w, v);
3963 if (res < 0)
3964 return NULL;
3965 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003966 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003967 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003968 if (res < 0)
3969 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003970 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003971 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003972 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003973 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003974 break;
3975 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003976 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003977 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003978 v = res ? Py_True : Py_False;
3979 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003980 return v;
3981}
3982
Thomas Wouters52152252000-08-17 22:55:00 +00003983static PyObject *
3984import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003985{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003986 PyObject *x;
3987
3988 x = PyObject_GetAttr(v, name);
3989 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003990 PyErr_Format(PyExc_ImportError,
3991 "cannot import name %.230s",
3992 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003993 }
Thomas Wouters52152252000-08-17 22:55:00 +00003994 return x;
3995}
Guido van Rossumac7be682001-01-17 15:42:30 +00003996
Thomas Wouters52152252000-08-17 22:55:00 +00003997static int
3998import_all_from(PyObject *locals, PyObject *v)
3999{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004000 PyObject *all = PyObject_GetAttrString(v, "__all__");
4001 PyObject *dict, *name, *value;
4002 int skip_leading_underscores = 0;
4003 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004004
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004005 if (all == NULL) {
4006 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4007 return -1; /* Unexpected error */
4008 PyErr_Clear();
4009 dict = PyObject_GetAttrString(v, "__dict__");
4010 if (dict == NULL) {
4011 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4012 return -1;
4013 PyErr_SetString(PyExc_ImportError,
4014 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004015 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004016 }
4017 all = PyMapping_Keys(dict);
4018 Py_DECREF(dict);
4019 if (all == NULL)
4020 return -1;
4021 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004022 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004023
4024 for (pos = 0, err = 0; ; pos++) {
4025 name = PySequence_GetItem(all, pos);
4026 if (name == NULL) {
4027 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4028 err = -1;
4029 else
4030 PyErr_Clear();
4031 break;
4032 }
4033 if (skip_leading_underscores &&
4034 PyString_Check(name) &&
4035 PyString_AS_STRING(name)[0] == '_')
4036 {
4037 Py_DECREF(name);
4038 continue;
4039 }
4040 value = PyObject_GetAttr(v, name);
4041 if (value == NULL)
4042 err = -1;
4043 else
4044 err = PyDict_SetItem(locals, name, value);
4045 Py_DECREF(name);
4046 Py_XDECREF(value);
4047 if (err != 0)
4048 break;
4049 }
4050 Py_DECREF(all);
4051 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004052}
4053
Guido van Rossumb209a111997-04-29 18:18:01 +00004054static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004055build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004056{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004057 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004058
4059 if (PyDict_Check(methods))
4060 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004061 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004062 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004063 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4064 base = PyTuple_GET_ITEM(bases, 0);
4065 metaclass = PyObject_GetAttrString(base, "__class__");
4066 if (metaclass == NULL) {
4067 PyErr_Clear();
4068 metaclass = (PyObject *)base->ob_type;
4069 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004070 }
4071 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004072 else {
4073 PyObject *g = PyEval_GetGlobals();
4074 if (g != NULL && PyDict_Check(g))
4075 metaclass = PyDict_GetItemString(g, "__metaclass__");
4076 if (metaclass == NULL)
4077 metaclass = (PyObject *) &PyClass_Type;
4078 Py_INCREF(metaclass);
4079 }
4080 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4081 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004082 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4083 /* A type error here likely means that the user passed
4084 in a base that was not a class (such the random module
4085 instead of the random.random type). Help them out with
4086 a more informative error message */
4087 PyErr_SetString(PyExc_TypeError,
4088 "Error when calling the metaclass.\n" \
4089 "Make sure the base arguments are valid.");
4090 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004091 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004092}
4093
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004094static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004095exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4096 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004097{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004098 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004099 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004100 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004101
Guido van Rossumb209a111997-04-29 18:18:01 +00004102 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4103 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004104 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004105 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004106 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004107 locals = PyTuple_GetItem(prog, 2);
4108 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004109 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004110 if (globals == Py_None) {
4111 globals = PyEval_GetGlobals();
4112 if (locals == Py_None) {
4113 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004114 plain = 1;
4115 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004116 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004117 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004118 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004119 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004120 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004121 !PyCode_Check(prog) &&
4122 !PyFile_Check(prog)) {
4123 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004124 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004125 return -1;
4126 }
Fred Drake661ea262000-10-24 19:57:45 +00004127 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004128 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004129 "exec: arg 2 must be a dictionary or None");
4130 return -1;
4131 }
4132 if (!PyDict_Check(locals)) {
4133 PyErr_SetString(PyExc_TypeError,
4134 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004135 return -1;
4136 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004137 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004138 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004139 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004140 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4141 PyErr_SetString(PyExc_TypeError,
4142 "code object passed to exec may not contain free variables");
4143 return -1;
4144 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004145 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004146 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004147 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004148 FILE *fp = PyFile_AsFile(prog);
4149 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004150 PyCompilerFlags cf;
4151 cf.cf_flags = 0;
4152 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004153 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004154 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004155 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004156 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004157 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004158 }
4159 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004160 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004161 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004162 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004163 cf.cf_flags = 0;
4164#ifdef Py_USING_UNICODE
4165 if (PyUnicode_Check(prog)) {
4166 tmp = PyUnicode_AsUTF8String(prog);
4167 if (tmp == NULL)
4168 return -1;
4169 prog = tmp;
4170 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4171 }
4172#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004173 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004174 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004175 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004176 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004177 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004178 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004179 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004180 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004181 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004182 if (plain)
4183 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004184 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004185 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004186 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004187 return 0;
4188}
Guido van Rossum24c13741995-02-14 09:42:43 +00004189
Guido van Rossumac7be682001-01-17 15:42:30 +00004190static void
Paul Prescode68140d2000-08-30 20:25:01 +00004191format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4192{
4193 char *obj_str;
4194
4195 if (!obj)
4196 return;
4197
4198 obj_str = PyString_AsString(obj);
4199 if (!obj_str)
4200 return;
4201
4202 PyErr_Format(exc, format_str, obj_str);
4203}
Guido van Rossum950361c1997-01-24 13:49:28 +00004204
4205#ifdef DYNAMIC_EXECUTION_PROFILE
4206
Skip Montanarof118cb12001-10-15 20:51:38 +00004207static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004208getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004209{
4210 int i;
4211 PyObject *l = PyList_New(256);
4212 if (l == NULL) return NULL;
4213 for (i = 0; i < 256; i++) {
4214 PyObject *x = PyInt_FromLong(a[i]);
4215 if (x == NULL) {
4216 Py_DECREF(l);
4217 return NULL;
4218 }
4219 PyList_SetItem(l, i, x);
4220 }
4221 for (i = 0; i < 256; i++)
4222 a[i] = 0;
4223 return l;
4224}
4225
4226PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004227_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004228{
4229#ifndef DXPAIRS
4230 return getarray(dxp);
4231#else
4232 int i;
4233 PyObject *l = PyList_New(257);
4234 if (l == NULL) return NULL;
4235 for (i = 0; i < 257; i++) {
4236 PyObject *x = getarray(dxpairs[i]);
4237 if (x == NULL) {
4238 Py_DECREF(l);
4239 return NULL;
4240 }
4241 PyList_SetItem(l, i, x);
4242 }
4243 return l;
4244#endif
4245}
4246
4247#endif