blob: b320f61681ce1a1213e5d89d7d38eb2387bbdc67 [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
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000852 case NOP:
853 goto fast_next_opcode;
854
Neil Schemenauer63543862002-02-17 19:10:14 +0000855 case LOAD_FAST:
856 x = GETLOCAL(oparg);
857 if (x != NULL) {
858 Py_INCREF(x);
859 PUSH(x);
860 goto fast_next_opcode;
861 }
862 format_exc_check_arg(PyExc_UnboundLocalError,
863 UNBOUNDLOCAL_ERROR_MSG,
864 PyTuple_GetItem(co->co_varnames, oparg));
865 break;
866
867 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000868 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000869 Py_INCREF(x);
870 PUSH(x);
871 goto fast_next_opcode;
872
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000873 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000874 case STORE_FAST:
875 v = POP();
876 SETLOCAL(oparg, v);
877 goto fast_next_opcode;
878
Raymond Hettingerf606f872003-03-16 03:11:04 +0000879 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000880 case POP_TOP:
881 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000882 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000883 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000884
Guido van Rossum374a9221991-04-04 10:40:29 +0000885 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000886 v = TOP();
887 w = SECOND();
888 SET_TOP(w);
889 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000890 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000891
Guido van Rossum374a9221991-04-04 10:40:29 +0000892 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000893 v = TOP();
894 w = SECOND();
895 x = THIRD();
896 SET_TOP(w);
897 SET_SECOND(x);
898 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000899 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000900
Thomas Wouters434d0822000-08-24 20:11:32 +0000901 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000902 u = TOP();
903 v = SECOND();
904 w = THIRD();
905 x = FOURTH();
906 SET_TOP(v);
907 SET_SECOND(w);
908 SET_THIRD(x);
909 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000910 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000911
Guido van Rossum374a9221991-04-04 10:40:29 +0000912 case DUP_TOP:
913 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000914 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000915 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000916 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000917
Thomas Wouters434d0822000-08-24 20:11:32 +0000918 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000919 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000920 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000921 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000922 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000923 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000924 STACKADJ(2);
925 SET_TOP(x);
926 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000927 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000928 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000929 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000930 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000931 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000932 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000933 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000934 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000935 STACKADJ(3);
936 SET_TOP(x);
937 SET_SECOND(w);
938 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000939 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000940 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000941 Py_FatalError("invalid argument to DUP_TOPX"
942 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000943 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000944
Guido van Rossum374a9221991-04-04 10:40:29 +0000945 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000946 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000947 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000948 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000949 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000950 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000951 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000952
Guido van Rossum374a9221991-04-04 10:40:29 +0000953 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000954 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000955 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000956 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000957 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000958 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000959 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000960
Guido van Rossum374a9221991-04-04 10:40:29 +0000961 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000962 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000963 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000964 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000965 if (err == 0) {
966 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000967 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000968 continue;
969 }
970 else if (err > 0) {
971 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000972 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000973 err = 0;
974 continue;
975 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000976 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000977 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000978
Guido van Rossum374a9221991-04-04 10:40:29 +0000979 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000980 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000981 x = PyObject_Repr(v);
982 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000983 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000984 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000985 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000986
Guido van Rossum7928cd71991-10-24 14:59:31 +0000987 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000988 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000989 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000990 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000991 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000992 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000993 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000994
Guido van Rossum50564e81996-01-12 01:13:16 +0000995 case BINARY_POWER:
996 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000997 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000998 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000999 Py_DECREF(v);
1000 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001001 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001002 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001003 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001004
Guido van Rossum374a9221991-04-04 10:40:29 +00001005 case BINARY_MULTIPLY:
1006 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001008 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001009 Py_DECREF(v);
1010 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001011 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001012 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001013 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001014
Guido van Rossum374a9221991-04-04 10:40:29 +00001015 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001016 if (!_Py_QnewFlag) {
1017 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001019 x = PyNumber_Divide(v, w);
1020 Py_DECREF(v);
1021 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001022 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001023 if (x != NULL) continue;
1024 break;
1025 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001027 BINARY_TRUE_DIVIDE */
1028 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001029 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001030 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001031 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001032 Py_DECREF(v);
1033 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001034 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001035 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001036 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001037
Guido van Rossum4668b002001-08-08 05:00:18 +00001038 case BINARY_FLOOR_DIVIDE:
1039 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001040 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001041 x = PyNumber_FloorDivide(v, w);
1042 Py_DECREF(v);
1043 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001044 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001045 if (x != NULL) continue;
1046 break;
1047
Guido van Rossum374a9221991-04-04 10:40:29 +00001048 case BINARY_MODULO:
1049 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001051 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001052 Py_DECREF(v);
1053 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001054 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001055 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001056 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Guido van Rossum374a9221991-04-04 10:40:29 +00001058 case BINARY_ADD:
1059 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001061 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001062 /* INLINE: int + int */
1063 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001064 a = PyInt_AS_LONG(v);
1065 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001066 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001067 if ((i^a) < 0 && (i^b) < 0)
1068 goto slow_add;
1069 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001070 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001071 else {
1072 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001073 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001074 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001075 Py_DECREF(v);
1076 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001077 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001078 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001079 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001080
Guido van Rossum374a9221991-04-04 10:40:29 +00001081 case BINARY_SUBTRACT:
1082 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001083 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001084 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001085 /* INLINE: int - int */
1086 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001087 a = PyInt_AS_LONG(v);
1088 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001089 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001090 if ((i^a) < 0 && (i^~b) < 0)
1091 goto slow_sub;
1092 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001093 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001094 else {
1095 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001096 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001097 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001098 Py_DECREF(v);
1099 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001100 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001101 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001102 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Guido van Rossum374a9221991-04-04 10:40:29 +00001104 case BINARY_SUBSCR:
1105 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001106 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001107 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001108 /* INLINE: list[int] */
1109 long i = PyInt_AsLong(w);
1110 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001111 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001112 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001113 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001114 Py_INCREF(x);
1115 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001116 else
1117 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001118 }
1119 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001120 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001121 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001122 Py_DECREF(v);
1123 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001124 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001125 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001126 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001127
Guido van Rossum7928cd71991-10-24 14:59:31 +00001128 case BINARY_LSHIFT:
1129 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001130 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001131 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001132 Py_DECREF(v);
1133 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001134 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001135 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001136 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001137
Guido van Rossum7928cd71991-10-24 14:59:31 +00001138 case BINARY_RSHIFT:
1139 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001140 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001141 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001142 Py_DECREF(v);
1143 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001144 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001145 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001146 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001147
Guido van Rossum7928cd71991-10-24 14:59:31 +00001148 case BINARY_AND:
1149 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001150 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001151 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001152 Py_DECREF(v);
1153 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001154 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001155 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001156 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001157
Guido van Rossum7928cd71991-10-24 14:59:31 +00001158 case BINARY_XOR:
1159 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001160 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001161 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001162 Py_DECREF(v);
1163 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001164 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001165 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001166 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001167
Guido van Rossum7928cd71991-10-24 14:59:31 +00001168 case BINARY_OR:
1169 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001170 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001171 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001172 Py_DECREF(v);
1173 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001174 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001175 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001176 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001177
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001178 case LIST_APPEND:
1179 w = POP();
1180 v = POP();
1181 err = PyList_Append(v, w);
1182 Py_DECREF(v);
1183 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001184 if (err == 0) {
1185 PREDICT(JUMP_ABSOLUTE);
1186 continue;
1187 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001188 break;
1189
Thomas Wouters434d0822000-08-24 20:11:32 +00001190 case INPLACE_POWER:
1191 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001193 x = PyNumber_InPlacePower(v, w, Py_None);
1194 Py_DECREF(v);
1195 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001197 if (x != NULL) continue;
1198 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001199
Thomas Wouters434d0822000-08-24 20:11:32 +00001200 case INPLACE_MULTIPLY:
1201 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001203 x = PyNumber_InPlaceMultiply(v, w);
1204 Py_DECREF(v);
1205 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001207 if (x != NULL) continue;
1208 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Thomas Wouters434d0822000-08-24 20:11:32 +00001210 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001211 if (!_Py_QnewFlag) {
1212 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001213 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001214 x = PyNumber_InPlaceDivide(v, w);
1215 Py_DECREF(v);
1216 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001217 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001218 if (x != NULL) continue;
1219 break;
1220 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001222 INPLACE_TRUE_DIVIDE */
1223 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001224 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001226 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001227 Py_DECREF(v);
1228 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001229 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001230 if (x != NULL) continue;
1231 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001232
Guido van Rossum4668b002001-08-08 05:00:18 +00001233 case INPLACE_FLOOR_DIVIDE:
1234 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001236 x = PyNumber_InPlaceFloorDivide(v, w);
1237 Py_DECREF(v);
1238 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001239 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001240 if (x != NULL) continue;
1241 break;
1242
Thomas Wouters434d0822000-08-24 20:11:32 +00001243 case INPLACE_MODULO:
1244 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001245 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001246 x = PyNumber_InPlaceRemainder(v, w);
1247 Py_DECREF(v);
1248 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001249 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001250 if (x != NULL) continue;
1251 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001252
Thomas Wouters434d0822000-08-24 20:11:32 +00001253 case INPLACE_ADD:
1254 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001255 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001256 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001257 /* INLINE: int + int */
1258 register long a, b, i;
1259 a = PyInt_AS_LONG(v);
1260 b = PyInt_AS_LONG(w);
1261 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001262 if ((i^a) < 0 && (i^b) < 0)
1263 goto slow_iadd;
1264 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001265 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001266 else {
1267 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001268 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001269 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001270 Py_DECREF(v);
1271 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001273 if (x != NULL) continue;
1274 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001275
Thomas Wouters434d0822000-08-24 20:11:32 +00001276 case INPLACE_SUBTRACT:
1277 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001278 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001279 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001280 /* INLINE: int - int */
1281 register long a, b, i;
1282 a = PyInt_AS_LONG(v);
1283 b = PyInt_AS_LONG(w);
1284 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001285 if ((i^a) < 0 && (i^~b) < 0)
1286 goto slow_isub;
1287 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001288 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001289 else {
1290 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001291 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001292 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 Py_DECREF(v);
1294 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001295 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 if (x != NULL) continue;
1297 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001298
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 case INPLACE_LSHIFT:
1300 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001301 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 x = PyNumber_InPlaceLshift(v, w);
1303 Py_DECREF(v);
1304 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 if (x != NULL) continue;
1307 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001308
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 case INPLACE_RSHIFT:
1310 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001311 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 x = PyNumber_InPlaceRshift(v, w);
1313 Py_DECREF(v);
1314 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001315 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 if (x != NULL) continue;
1317 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 case INPLACE_AND:
1320 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 x = PyNumber_InPlaceAnd(v, w);
1323 Py_DECREF(v);
1324 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001325 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 if (x != NULL) continue;
1327 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001328
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 case INPLACE_XOR:
1330 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001331 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 x = PyNumber_InPlaceXor(v, w);
1333 Py_DECREF(v);
1334 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001335 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001336 if (x != NULL) continue;
1337 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001338
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 case INPLACE_OR:
1340 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 x = PyNumber_InPlaceOr(v, w);
1343 Py_DECREF(v);
1344 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 if (x != NULL) continue;
1347 break;
1348
Guido van Rossum374a9221991-04-04 10:40:29 +00001349 case SLICE+0:
1350 case SLICE+1:
1351 case SLICE+2:
1352 case SLICE+3:
1353 if ((opcode-SLICE) & 2)
1354 w = POP();
1355 else
1356 w = NULL;
1357 if ((opcode-SLICE) & 1)
1358 v = POP();
1359 else
1360 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001362 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001363 Py_DECREF(u);
1364 Py_XDECREF(v);
1365 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001366 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001367 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001368 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001369
Guido van Rossum374a9221991-04-04 10:40:29 +00001370 case STORE_SLICE+0:
1371 case STORE_SLICE+1:
1372 case STORE_SLICE+2:
1373 case STORE_SLICE+3:
1374 if ((opcode-STORE_SLICE) & 2)
1375 w = POP();
1376 else
1377 w = NULL;
1378 if ((opcode-STORE_SLICE) & 1)
1379 v = POP();
1380 else
1381 v = NULL;
1382 u = POP();
1383 t = POP();
1384 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001385 Py_DECREF(t);
1386 Py_DECREF(u);
1387 Py_XDECREF(v);
1388 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001389 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001390 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001391
Guido van Rossum374a9221991-04-04 10:40:29 +00001392 case DELETE_SLICE+0:
1393 case DELETE_SLICE+1:
1394 case DELETE_SLICE+2:
1395 case DELETE_SLICE+3:
1396 if ((opcode-DELETE_SLICE) & 2)
1397 w = POP();
1398 else
1399 w = NULL;
1400 if ((opcode-DELETE_SLICE) & 1)
1401 v = POP();
1402 else
1403 v = NULL;
1404 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001405 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001406 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001407 Py_DECREF(u);
1408 Py_XDECREF(v);
1409 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001410 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001412
Guido van Rossum374a9221991-04-04 10:40:29 +00001413 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001414 w = TOP();
1415 v = SECOND();
1416 u = THIRD();
1417 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001418 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001419 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001420 Py_DECREF(u);
1421 Py_DECREF(v);
1422 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001423 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001425
Guido van Rossum374a9221991-04-04 10:40:29 +00001426 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001427 w = TOP();
1428 v = SECOND();
1429 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001430 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001431 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001432 Py_DECREF(v);
1433 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001434 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001436
Guido van Rossum374a9221991-04-04 10:40:29 +00001437 case PRINT_EXPR:
1438 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001439 w = PySys_GetObject("displayhook");
1440 if (w == NULL) {
1441 PyErr_SetString(PyExc_RuntimeError,
1442 "lost sys.displayhook");
1443 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001444 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001445 }
1446 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001447 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001448 if (x == NULL)
1449 err = -1;
1450 }
1451 if (err == 0) {
1452 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001453 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001454 if (w == NULL)
1455 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001456 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001457 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001458 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001459 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001460
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001461 case PRINT_ITEM_TO:
1462 w = stream = POP();
1463 /* fall through to PRINT_ITEM */
1464
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 case PRINT_ITEM:
1466 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001467 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001468 w = PySys_GetObject("stdout");
1469 if (w == NULL) {
1470 PyErr_SetString(PyExc_RuntimeError,
1471 "lost sys.stdout");
1472 err = -1;
1473 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001474 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001475 /* PyFile_SoftSpace() can exececute arbitrary code
1476 if sys.stdout is an instance with a __getattr__.
1477 If __getattr__ raises an exception, w will
1478 be freed, so we need to prevent that temporarily. */
1479 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001480 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001481 err = PyFile_WriteString(" ", w);
1482 if (err == 0)
1483 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001484 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001485 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001486 if (PyString_Check(v)) {
1487 char *s = PyString_AS_STRING(v);
1488 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001489 if (len == 0 ||
1490 !isspace(Py_CHARMASK(s[len-1])) ||
1491 s[len-1] == ' ')
1492 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001493 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001494#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001495 else if (PyUnicode_Check(v)) {
1496 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1497 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001498 if (len == 0 ||
1499 !Py_UNICODE_ISSPACE(s[len-1]) ||
1500 s[len-1] == ' ')
1501 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001502 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001503#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001504 else
1505 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001506 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001507 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001508 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001509 Py_XDECREF(stream);
1510 stream = NULL;
1511 if (err == 0)
1512 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001513 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001514
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001515 case PRINT_NEWLINE_TO:
1516 w = stream = POP();
1517 /* fall through to PRINT_NEWLINE */
1518
Guido van Rossum374a9221991-04-04 10:40:29 +00001519 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001520 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001521 w = PySys_GetObject("stdout");
1522 if (w == NULL)
1523 PyErr_SetString(PyExc_RuntimeError,
1524 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001525 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001526 if (w != NULL) {
1527 err = PyFile_WriteString("\n", w);
1528 if (err == 0)
1529 PyFile_SoftSpace(w, 0);
1530 }
1531 Py_XDECREF(stream);
1532 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001533 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Thomas Wouters434d0822000-08-24 20:11:32 +00001535
1536#ifdef CASE_TOO_BIG
1537 default: switch (opcode) {
1538#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001539 case RAISE_VARARGS:
1540 u = v = w = NULL;
1541 switch (oparg) {
1542 case 3:
1543 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001544 /* Fallthrough */
1545 case 2:
1546 v = POP(); /* value */
1547 /* Fallthrough */
1548 case 1:
1549 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001550 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001551 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001552 break;
1553 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001554 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001555 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001556 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001557 break;
1558 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Guido van Rossum374a9221991-04-04 10:40:29 +00001561 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001562 if ((x = f->f_locals) != NULL) {
1563 Py_INCREF(x);
1564 PUSH(x);
1565 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001566 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001567 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001569
Guido van Rossum374a9221991-04-04 10:40:29 +00001570 case RETURN_VALUE:
1571 retval = POP();
1572 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001573 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001574
Tim Peters5ca576e2001-06-18 22:08:13 +00001575 case YIELD_VALUE:
1576 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001577 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001578 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001579 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001580
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001581 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001582 w = TOP();
1583 v = SECOND();
1584 u = THIRD();
1585 STACKADJ(-3);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001586#ifdef WITH_TSC
1587 rdtscll(intr0);
1588#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001589 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001590#ifdef WITH_TSC
1591 rdtscll(intr1);
1592#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001593 Py_DECREF(u);
1594 Py_DECREF(v);
1595 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001596 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Guido van Rossum374a9221991-04-04 10:40:29 +00001598 case POP_BLOCK:
1599 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001600 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001601 while (STACK_LEVEL() > b->b_level) {
1602 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001603 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001604 }
1605 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001606 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Guido van Rossum374a9221991-04-04 10:40:29 +00001608 case END_FINALLY:
1609 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001610 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001611 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001612 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001613 if (why == WHY_RETURN ||
1614 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001615 retval = POP();
1616 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001617 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001619 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001620 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001621 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001622 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001623 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001624 else if (v != Py_None) {
1625 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001626 "'finally' pops bad exception");
1627 why = WHY_EXCEPTION;
1628 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001629 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001630 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001631
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001633 u = TOP();
1634 v = SECOND();
1635 w = THIRD();
1636 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001637 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001638 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001639 Py_DECREF(u);
1640 Py_DECREF(v);
1641 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001643
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001645 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001646 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001647 if ((x = f->f_locals) != NULL) {
1648 err = PyDict_SetItem(x, w, v);
1649 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001650 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001651 break;
1652 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001653 PyErr_Format(PyExc_SystemError,
1654 "no locals found when storing %s",
1655 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001657
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001659 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001660 if ((x = f->f_locals) != NULL) {
1661 if ((err = PyDict_DelItem(x, w)) != 0)
1662 format_exc_check_arg(PyExc_NameError,
1663 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001664 break;
1665 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001666 PyErr_Format(PyExc_SystemError,
1667 "no locals when deleting %s",
1668 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001670
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001671 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001672 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001674 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1675 PyObject **items = ((PyTupleObject *)v)->ob_item;
1676 while (oparg--) {
1677 w = items[oparg];
1678 Py_INCREF(w);
1679 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001680 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001681 Py_DECREF(v);
1682 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001683 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1684 PyObject **items = ((PyListObject *)v)->ob_item;
1685 while (oparg--) {
1686 w = items[oparg];
1687 Py_INCREF(w);
1688 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001689 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001690 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001691 stack_pointer + oparg))
1692 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001693 else {
1694 if (PyErr_ExceptionMatches(PyExc_TypeError))
1695 PyErr_SetString(PyExc_TypeError,
1696 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001697 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001698 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001699 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Guido van Rossum374a9221991-04-04 10:40:29 +00001702 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001703 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001704 v = TOP();
1705 u = SECOND();
1706 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001707 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1708 Py_DECREF(v);
1709 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001710 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001711 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001714 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001715 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001716 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1717 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001718 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001719 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001720
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001721 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001722 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001723 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001724 err = PyDict_SetItem(f->f_globals, w, v);
1725 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001726 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001727 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001728
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001729 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001730 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001731 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001732 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001733 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001734 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001735
Guido van Rossum374a9221991-04-04 10:40:29 +00001736 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001737 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001738 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001739 PyErr_Format(PyExc_SystemError,
1740 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001741 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001742 break;
1743 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001746 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001748 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001749 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001750 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001751 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001752 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 break;
1754 }
1755 }
1756 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001757 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001758 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001759 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001760
Guido van Rossum374a9221991-04-04 10:40:29 +00001761 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001762 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001763 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001764 /* Inline the PyDict_GetItem() calls.
1765 WARNING: this is an extreme speed hack.
1766 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001767 long hash = ((PyStringObject *)w)->ob_shash;
1768 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001769 PyDictObject *d;
1770 d = (PyDictObject *)(f->f_globals);
1771 x = d->ma_lookup(d, w, hash)->me_value;
1772 if (x != NULL) {
1773 Py_INCREF(x);
1774 PUSH(x);
1775 continue;
1776 }
1777 d = (PyDictObject *)(f->f_builtins);
1778 x = d->ma_lookup(d, w, hash)->me_value;
1779 if (x != NULL) {
1780 Py_INCREF(x);
1781 PUSH(x);
1782 continue;
1783 }
1784 goto load_global_error;
1785 }
1786 }
1787 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001788 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001790 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001791 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001792 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001793 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001794 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001795 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001796 break;
1797 }
1798 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001799 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001800 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001801 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001802
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001803 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001804 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001805 if (x != NULL) {
1806 SETLOCAL(oparg, NULL);
1807 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001808 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001809 format_exc_check_arg(
1810 PyExc_UnboundLocalError,
1811 UNBOUNDLOCAL_ERROR_MSG,
1812 PyTuple_GetItem(co->co_varnames, oparg)
1813 );
1814 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001815
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001816 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001817 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001818 Py_INCREF(x);
1819 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001820 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001821 break;
1822
1823 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001824 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001825 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001826 if (w != NULL) {
1827 PUSH(w);
1828 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001829 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001830 err = -1;
1831 /* Don't stomp existing exception */
1832 if (PyErr_Occurred())
1833 break;
1834 if (oparg < f->f_ncells) {
1835 v = PyTuple_GetItem(co->co_cellvars,
1836 oparg);
1837 format_exc_check_arg(
1838 PyExc_UnboundLocalError,
1839 UNBOUNDLOCAL_ERROR_MSG,
1840 v);
1841 } else {
1842 v = PyTuple_GetItem(
1843 co->co_freevars,
1844 oparg - f->f_ncells);
1845 format_exc_check_arg(
1846 PyExc_NameError,
1847 UNBOUNDFREE_ERROR_MSG,
1848 v);
1849 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001850 break;
1851
1852 case STORE_DEREF:
1853 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001854 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001855 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001856 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001857 continue;
1858
Guido van Rossum374a9221991-04-04 10:40:29 +00001859 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001860 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001861 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001862 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001863 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001864 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001865 }
1866 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001867 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001868 }
1869 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001870
Guido van Rossum374a9221991-04-04 10:40:29 +00001871 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001872 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001873 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001874 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001875 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001876 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001877 }
1878 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001879 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 }
1881 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001882
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001884 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001886 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001888
Guido van Rossum374a9221991-04-04 10:40:29 +00001889 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001890 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001891 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001892 x = PyObject_GetAttr(v, w);
1893 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001894 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001895 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001896 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001897
Guido van Rossum374a9221991-04-04 10:40:29 +00001898 case COMPARE_OP:
1899 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001900 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001901 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001902 /* INLINE: cmp(int, int) */
1903 register long a, b;
1904 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001905 a = PyInt_AS_LONG(v);
1906 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001907 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001908 case PyCmp_LT: res = a < b; break;
1909 case PyCmp_LE: res = a <= b; break;
1910 case PyCmp_EQ: res = a == b; break;
1911 case PyCmp_NE: res = a != b; break;
1912 case PyCmp_GT: res = a > b; break;
1913 case PyCmp_GE: res = a >= b; break;
1914 case PyCmp_IS: res = v == w; break;
1915 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001916 default: goto slow_compare;
1917 }
1918 x = res ? Py_True : Py_False;
1919 Py_INCREF(x);
1920 }
1921 else {
1922 slow_compare:
1923 x = cmp_outcome(oparg, v, w);
1924 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001925 Py_DECREF(v);
1926 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001927 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001928 if (x == NULL) break;
1929 PREDICT(JUMP_IF_FALSE);
1930 PREDICT(JUMP_IF_TRUE);
1931 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001932
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001934 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001935 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001936 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001937 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001938 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001939 break;
1940 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001941 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001942 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001943 w,
1944 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001945 f->f_locals == NULL ?
1946 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001947 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001948 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001949 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001950 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001951 x = NULL;
1952 break;
1953 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001954#ifdef WITH_TSC
1955 rdtscll(intr0);
1956#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001958#ifdef WITH_TSC
1959 rdtscll(intr1);
1960#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001961 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001962 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001963 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001964 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001965
Thomas Wouters52152252000-08-17 22:55:00 +00001966 case IMPORT_STAR:
1967 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001968 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001969 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001970 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001971 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001972 break;
1973 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001974#ifdef WITH_TSC
1975 rdtscll(intr0);
1976#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001977 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001978#ifdef WITH_TSC
1979 rdtscll(intr1);
1980#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001981 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001982 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001983 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001984 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001985
Thomas Wouters52152252000-08-17 22:55:00 +00001986 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001987 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001988 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001989#ifdef WITH_TSC
1990 rdtscll(intr0);
1991#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001992 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001993#ifdef WITH_TSC
1994 rdtscll(intr1);
1995#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001996 PUSH(x);
1997 if (x != NULL) continue;
1998 break;
1999
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 case JUMP_FORWARD:
2001 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002002 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002003
Raymond Hettingerf606f872003-03-16 03:11:04 +00002004 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002005 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002006 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002007 if (w == Py_True) {
2008 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002009 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002010 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002011 if (w == Py_False) {
2012 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002013 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002014 }
2015 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002016 if (err > 0)
2017 err = 0;
2018 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002019 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002020 else
2021 break;
2022 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002023
Raymond Hettingerf606f872003-03-16 03:11:04 +00002024 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002025 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002026 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002027 if (w == Py_False) {
2028 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002029 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002030 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002031 if (w == Py_True) {
2032 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002033 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002034 }
2035 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002036 if (err > 0) {
2037 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002038 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002039 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002040 else if (err == 0)
2041 ;
2042 else
2043 break;
2044 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002045
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002046 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 case JUMP_ABSOLUTE:
2048 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002049 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002050
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002051 case GET_ITER:
2052 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002053 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002054 x = PyObject_GetIter(v);
2055 Py_DECREF(v);
2056 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002057 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002058 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002059 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002060 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002061 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002062 break;
2063
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002064 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002065 case FOR_ITER:
2066 /* before: [iter]; after: [iter, iter()] *or* [] */
2067 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002068 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002069 if (x != NULL) {
2070 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002071 PREDICT(STORE_FAST);
2072 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002073 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002074 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002075 if (PyErr_Occurred()) {
2076 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2077 break;
2078 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002079 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002080 /* iterator ended normally */
2081 x = v = POP();
2082 Py_DECREF(v);
2083 JUMPBY(oparg);
2084 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002085
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002086 case BREAK_LOOP:
2087 why = WHY_BREAK;
2088 goto fast_block_end;
2089
2090 case CONTINUE_LOOP:
2091 retval = PyInt_FromLong(oparg);
2092 why = WHY_CONTINUE;
2093 goto fast_block_end;
2094
Guido van Rossum374a9221991-04-04 10:40:29 +00002095 case SETUP_LOOP:
2096 case SETUP_EXCEPT:
2097 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002098 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002099 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002100 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002101
Guido van Rossumf10570b1995-07-07 22:53:21 +00002102 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002103 {
2104 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002105 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002106 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002107#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002108 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002109#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002110 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002111#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002112 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002113 PUSH(x);
2114 if (x != NULL)
2115 continue;
2116 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002117 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002118
Jeremy Hylton76901512000-03-28 23:49:17 +00002119 case CALL_FUNCTION_VAR:
2120 case CALL_FUNCTION_KW:
2121 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002122 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002123 int na = oparg & 0xff;
2124 int nk = (oparg>>8) & 0xff;
2125 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002126 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002127 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002128 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002129 if (flags & CALL_FLAG_VAR)
2130 n++;
2131 if (flags & CALL_FLAG_KW)
2132 n++;
2133 pfunc = stack_pointer - n - 1;
2134 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002135
Guido van Rossumac7be682001-01-17 15:42:30 +00002136 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002137 && PyMethod_GET_SELF(func) != NULL) {
2138 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002139 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002140 func = PyMethod_GET_FUNCTION(func);
2141 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002142 Py_DECREF(*pfunc);
2143 *pfunc = self;
2144 na++;
2145 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002146 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002147 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002148 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002149#ifdef WITH_TSC
2150 rdtscll(intr0);
2151#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002152 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002153#ifdef WITH_TSC
2154 rdtscll(intr1);
2155#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002156 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002157 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002158
Jeremy Hylton76901512000-03-28 23:49:17 +00002159 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002160 w = POP();
2161 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002162 }
2163 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002164 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002165 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002166 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002167 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002168
Guido van Rossum681d79a1995-07-18 14:51:37 +00002169 case MAKE_FUNCTION:
2170 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002171 x = PyFunction_New(v, f->f_globals);
2172 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002173 /* XXX Maybe this should be a separate opcode? */
2174 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002175 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002176 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002177 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002178 x = NULL;
2179 break;
2180 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002181 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002182 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002183 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002184 }
2185 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002186 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002187 }
2188 PUSH(x);
2189 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002190
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002191 case MAKE_CLOSURE:
2192 {
2193 int nfree;
2194 v = POP(); /* code object */
2195 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002196 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002197 Py_DECREF(v);
2198 /* XXX Maybe this should be a separate opcode? */
2199 if (x != NULL && nfree > 0) {
2200 v = PyTuple_New(nfree);
2201 if (v == NULL) {
2202 Py_DECREF(x);
2203 x = NULL;
2204 break;
2205 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002206 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002207 w = POP();
2208 PyTuple_SET_ITEM(v, nfree, w);
2209 }
2210 err = PyFunction_SetClosure(x, v);
2211 Py_DECREF(v);
2212 }
2213 if (x != NULL && oparg > 0) {
2214 v = PyTuple_New(oparg);
2215 if (v == NULL) {
2216 Py_DECREF(x);
2217 x = NULL;
2218 break;
2219 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002220 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002221 w = POP();
2222 PyTuple_SET_ITEM(v, oparg, w);
2223 }
2224 err = PyFunction_SetDefaults(x, v);
2225 Py_DECREF(v);
2226 }
2227 PUSH(x);
2228 break;
2229 }
2230
Guido van Rossum8861b741996-07-30 16:49:37 +00002231 case BUILD_SLICE:
2232 if (oparg == 3)
2233 w = POP();
2234 else
2235 w = NULL;
2236 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002237 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002238 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002239 Py_DECREF(u);
2240 Py_DECREF(v);
2241 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002242 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002243 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002244 break;
2245
Fred Drakeef8ace32000-08-24 00:32:09 +00002246 case EXTENDED_ARG:
2247 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002248 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002249 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002250
Guido van Rossum374a9221991-04-04 10:40:29 +00002251 default:
2252 fprintf(stderr,
2253 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002254 PyCode_Addr2Line(f->f_code, f->f_lasti),
2255 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002256 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002257 why = WHY_EXCEPTION;
2258 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002259
2260#ifdef CASE_TOO_BIG
2261 }
2262#endif
2263
Guido van Rossum374a9221991-04-04 10:40:29 +00002264 } /* switch */
2265
2266 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002267
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002268#ifdef WITH_TSC
2269 rdtscll(inst1);
2270#endif
2271
Guido van Rossum374a9221991-04-04 10:40:29 +00002272 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002273
Guido van Rossum374a9221991-04-04 10:40:29 +00002274 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002275 if (err == 0 && x != NULL) {
2276#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002277 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002278 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002279 fprintf(stderr,
2280 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002281 else {
2282#endif
2283#ifdef WITH_TSC
2284 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002285#endif
2286 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002287#ifdef CHECKEXC
2288 }
2289#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002290 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002292 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002293 err = 0;
2294 }
2295
Guido van Rossum374a9221991-04-04 10:40:29 +00002296 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002297
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002298 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002299 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002300 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002301 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002302 why = WHY_EXCEPTION;
2303 }
2304 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002305#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002306 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002307 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002308 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002309 char buf[1024];
2310 sprintf(buf, "Stack unwind with exception "
2311 "set and why=%d", why);
2312 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002313 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002314 }
2315#endif
2316
2317 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Guido van Rossum374a9221991-04-04 10:40:29 +00002319 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002320 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002321
Fred Drake8f51f542001-10-04 14:48:42 +00002322 if (tstate->c_tracefunc != NULL)
2323 call_exc_trace(tstate->c_tracefunc,
2324 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002325 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002328
Guido van Rossum374a9221991-04-04 10:40:29 +00002329 if (why == WHY_RERAISE)
2330 why = WHY_EXCEPTION;
2331
2332 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002334fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002335 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002336 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002337
Tim Peters8a5c3c72004-04-05 19:36:21 +00002338 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002339 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2340 /* For a continue inside a try block,
2341 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002342 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2343 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002344 why = WHY_NOT;
2345 JUMPTO(PyInt_AS_LONG(retval));
2346 Py_DECREF(retval);
2347 break;
2348 }
2349
Guido van Rossum374a9221991-04-04 10:40:29 +00002350 while (STACK_LEVEL() > b->b_level) {
2351 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002352 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002353 }
2354 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2355 why = WHY_NOT;
2356 JUMPTO(b->b_handler);
2357 break;
2358 }
2359 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002360 (b->b_type == SETUP_EXCEPT &&
2361 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002362 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002363 PyObject *exc, *val, *tb;
2364 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002366 val = Py_None;
2367 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002368 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002369 /* Make the raw exception data
2370 available to the handler,
2371 so a program can emulate the
2372 Python main loop. Don't do
2373 this for 'finally'. */
2374 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002375 PyErr_NormalizeException(
2376 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002377 set_exc_info(tstate,
2378 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002379 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002380 if (tb == NULL) {
2381 Py_INCREF(Py_None);
2382 PUSH(Py_None);
2383 } else
2384 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002385 PUSH(val);
2386 PUSH(exc);
2387 }
2388 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002389 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002390 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002391 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002392 PUSH(v);
2393 }
2394 why = WHY_NOT;
2395 JUMPTO(b->b_handler);
2396 break;
2397 }
2398 } /* unwind stack */
2399
2400 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002401
Guido van Rossum374a9221991-04-04 10:40:29 +00002402 if (why != WHY_NOT)
2403 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002404#ifdef WITH_TSC
2405 rdtscll(loop1);
2406#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002407
Guido van Rossum374a9221991-04-04 10:40:29 +00002408 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002409
Tim Peters8a5c3c72004-04-05 19:36:21 +00002410 assert(why != WHY_YIELD);
2411 /* Pop remaining stack entries. */
2412 while (!EMPTY()) {
2413 v = POP();
2414 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002415 }
2416
Tim Peters8a5c3c72004-04-05 19:36:21 +00002417 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002418 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002419
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002420fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002421 if (tstate->use_tracing) {
2422 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002423 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002424 if (call_trace(tstate->c_tracefunc,
2425 tstate->c_traceobj, f,
2426 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002427 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002428 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002429 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002430 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002431 }
Fred Drake8f51f542001-10-04 14:48:42 +00002432 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002433 if (why == WHY_EXCEPTION)
2434 call_trace_protected(tstate->c_profilefunc,
2435 tstate->c_profileobj, f,
2436 PyTrace_RETURN);
2437 else if (call_trace(tstate->c_profilefunc,
2438 tstate->c_profileobj, f,
2439 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002440 Py_XDECREF(retval);
2441 retval = NULL;
2442 why = WHY_EXCEPTION;
2443 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002444 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002445 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002446
Guido van Rossuma027efa1997-05-05 20:56:21 +00002447 reset_exc_info(tstate);
2448
Tim Peters5ca576e2001-06-18 22:08:13 +00002449 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002450 exit_eval_frame:
2451 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002452 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002453
Guido van Rossum96a42c81992-01-12 02:29:51 +00002454 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002455}
2456
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002457/* this is gonna seem *real weird*, but if you put some other code between
2458 eval_frame() and PyEval_EvalCodeEx() you will need to adjust the test in
2459 the if statement in Misc/gdbinit:ppystack */
2460
Tim Peters6d6c1a32001-08-02 04:15:00 +00002461PyObject *
2462PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002463 PyObject **args, int argcount, PyObject **kws, int kwcount,
2464 PyObject **defs, int defcount, PyObject *closure)
2465{
2466 register PyFrameObject *f;
2467 register PyObject *retval = NULL;
2468 register PyObject **fastlocals, **freevars;
2469 PyThreadState *tstate = PyThreadState_GET();
2470 PyObject *x, *u;
2471
2472 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002473 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002474 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002475 return NULL;
2476 }
2477
Jeremy Hylton985eba52003-02-05 23:13:00 +00002478 assert(globals != NULL);
2479 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002480 if (f == NULL)
2481 return NULL;
2482
2483 fastlocals = f->f_localsplus;
2484 freevars = f->f_localsplus + f->f_nlocals;
2485
2486 if (co->co_argcount > 0 ||
2487 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2488 int i;
2489 int n = argcount;
2490 PyObject *kwdict = NULL;
2491 if (co->co_flags & CO_VARKEYWORDS) {
2492 kwdict = PyDict_New();
2493 if (kwdict == NULL)
2494 goto fail;
2495 i = co->co_argcount;
2496 if (co->co_flags & CO_VARARGS)
2497 i++;
2498 SETLOCAL(i, kwdict);
2499 }
2500 if (argcount > co->co_argcount) {
2501 if (!(co->co_flags & CO_VARARGS)) {
2502 PyErr_Format(PyExc_TypeError,
2503 "%.200s() takes %s %d "
2504 "%sargument%s (%d given)",
2505 PyString_AsString(co->co_name),
2506 defcount ? "at most" : "exactly",
2507 co->co_argcount,
2508 kwcount ? "non-keyword " : "",
2509 co->co_argcount == 1 ? "" : "s",
2510 argcount);
2511 goto fail;
2512 }
2513 n = co->co_argcount;
2514 }
2515 for (i = 0; i < n; i++) {
2516 x = args[i];
2517 Py_INCREF(x);
2518 SETLOCAL(i, x);
2519 }
2520 if (co->co_flags & CO_VARARGS) {
2521 u = PyTuple_New(argcount - n);
2522 if (u == NULL)
2523 goto fail;
2524 SETLOCAL(co->co_argcount, u);
2525 for (i = n; i < argcount; i++) {
2526 x = args[i];
2527 Py_INCREF(x);
2528 PyTuple_SET_ITEM(u, i-n, x);
2529 }
2530 }
2531 for (i = 0; i < kwcount; i++) {
2532 PyObject *keyword = kws[2*i];
2533 PyObject *value = kws[2*i + 1];
2534 int j;
2535 if (keyword == NULL || !PyString_Check(keyword)) {
2536 PyErr_Format(PyExc_TypeError,
2537 "%.200s() keywords must be strings",
2538 PyString_AsString(co->co_name));
2539 goto fail;
2540 }
2541 /* XXX slow -- speed up using dictionary? */
2542 for (j = 0; j < co->co_argcount; j++) {
2543 PyObject *nm = PyTuple_GET_ITEM(
2544 co->co_varnames, j);
2545 int cmp = PyObject_RichCompareBool(
2546 keyword, nm, Py_EQ);
2547 if (cmp > 0)
2548 break;
2549 else if (cmp < 0)
2550 goto fail;
2551 }
2552 /* Check errors from Compare */
2553 if (PyErr_Occurred())
2554 goto fail;
2555 if (j >= co->co_argcount) {
2556 if (kwdict == NULL) {
2557 PyErr_Format(PyExc_TypeError,
2558 "%.200s() got an unexpected "
2559 "keyword argument '%.400s'",
2560 PyString_AsString(co->co_name),
2561 PyString_AsString(keyword));
2562 goto fail;
2563 }
2564 PyDict_SetItem(kwdict, keyword, value);
2565 }
2566 else {
2567 if (GETLOCAL(j) != NULL) {
2568 PyErr_Format(PyExc_TypeError,
2569 "%.200s() got multiple "
2570 "values for keyword "
2571 "argument '%.400s'",
2572 PyString_AsString(co->co_name),
2573 PyString_AsString(keyword));
2574 goto fail;
2575 }
2576 Py_INCREF(value);
2577 SETLOCAL(j, value);
2578 }
2579 }
2580 if (argcount < co->co_argcount) {
2581 int m = co->co_argcount - defcount;
2582 for (i = argcount; i < m; i++) {
2583 if (GETLOCAL(i) == NULL) {
2584 PyErr_Format(PyExc_TypeError,
2585 "%.200s() takes %s %d "
2586 "%sargument%s (%d given)",
2587 PyString_AsString(co->co_name),
2588 ((co->co_flags & CO_VARARGS) ||
2589 defcount) ? "at least"
2590 : "exactly",
2591 m, kwcount ? "non-keyword " : "",
2592 m == 1 ? "" : "s", i);
2593 goto fail;
2594 }
2595 }
2596 if (n > m)
2597 i = n - m;
2598 else
2599 i = 0;
2600 for (; i < defcount; i++) {
2601 if (GETLOCAL(m+i) == NULL) {
2602 PyObject *def = defs[i];
2603 Py_INCREF(def);
2604 SETLOCAL(m+i, def);
2605 }
2606 }
2607 }
2608 }
2609 else {
2610 if (argcount > 0 || kwcount > 0) {
2611 PyErr_Format(PyExc_TypeError,
2612 "%.200s() takes no arguments (%d given)",
2613 PyString_AsString(co->co_name),
2614 argcount + kwcount);
2615 goto fail;
2616 }
2617 }
2618 /* Allocate and initialize storage for cell vars, and copy free
2619 vars into frame. This isn't too efficient right now. */
2620 if (f->f_ncells) {
2621 int i = 0, j = 0, nargs, found;
2622 char *cellname, *argname;
2623 PyObject *c;
2624
2625 nargs = co->co_argcount;
2626 if (co->co_flags & CO_VARARGS)
2627 nargs++;
2628 if (co->co_flags & CO_VARKEYWORDS)
2629 nargs++;
2630
2631 /* Check for cells that shadow args */
2632 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2633 cellname = PyString_AS_STRING(
2634 PyTuple_GET_ITEM(co->co_cellvars, i));
2635 found = 0;
2636 while (j < nargs) {
2637 argname = PyString_AS_STRING(
2638 PyTuple_GET_ITEM(co->co_varnames, j));
2639 if (strcmp(cellname, argname) == 0) {
2640 c = PyCell_New(GETLOCAL(j));
2641 if (c == NULL)
2642 goto fail;
2643 GETLOCAL(f->f_nlocals + i) = c;
2644 found = 1;
2645 break;
2646 }
2647 j++;
2648 }
2649 if (found == 0) {
2650 c = PyCell_New(NULL);
2651 if (c == NULL)
2652 goto fail;
2653 SETLOCAL(f->f_nlocals + i, c);
2654 }
2655 }
2656 /* Initialize any that are left */
2657 while (i < f->f_ncells) {
2658 c = PyCell_New(NULL);
2659 if (c == NULL)
2660 goto fail;
2661 SETLOCAL(f->f_nlocals + i, c);
2662 i++;
2663 }
2664 }
2665 if (f->f_nfreevars) {
2666 int i;
2667 for (i = 0; i < f->f_nfreevars; ++i) {
2668 PyObject *o = PyTuple_GET_ITEM(closure, i);
2669 Py_INCREF(o);
2670 freevars[f->f_ncells + i] = o;
2671 }
2672 }
2673
Tim Peters5ca576e2001-06-18 22:08:13 +00002674 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002675 /* Don't need to keep the reference to f_back, it will be set
2676 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002677 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002678 f->f_back = NULL;
2679
Jeremy Hylton985eba52003-02-05 23:13:00 +00002680 PCALL(PCALL_GENERATOR);
2681
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002682 /* Create a new generator that owns the ready to run frame
2683 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002684 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002685 }
2686
2687 retval = eval_frame(f);
2688
2689 fail: /* Jump here from prelude on failure */
2690
Tim Petersb13680b2001-11-27 23:29:29 +00002691 /* decref'ing the frame can cause __del__ methods to get invoked,
2692 which can call back into Python. While we're done with the
2693 current Python frame (f), the associated C stack is still in use,
2694 so recursion_depth must be boosted for the duration.
2695 */
2696 assert(tstate != NULL);
2697 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002698 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002699 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002700 return retval;
2701}
2702
2703
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002704/* Implementation notes for set_exc_info() and reset_exc_info():
2705
2706- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2707 'exc_traceback'. These always travel together.
2708
2709- tstate->curexc_ZZZ is the "hot" exception that is set by
2710 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2711
2712- Once an exception is caught by an except clause, it is transferred
2713 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2714 can pick it up. This is the primary task of set_exc_info().
2715
2716- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2717
2718 Long ago, when none of this existed, there were just a few globals:
2719 one set corresponding to the "hot" exception, and one set
2720 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2721 globals; they were simply stored as sys.exc_ZZZ. For backwards
2722 compatibility, they still are!) The problem was that in code like
2723 this:
2724
2725 try:
2726 "something that may fail"
2727 except "some exception":
2728 "do something else first"
2729 "print the exception from sys.exc_ZZZ."
2730
2731 if "do something else first" invoked something that raised and caught
2732 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2733 cause of subtle bugs. I fixed this by changing the semantics as
2734 follows:
2735
2736 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2737 *in that frame*.
2738
2739 - But initially, and as long as no exception is caught in a given
2740 frame, sys.exc_ZZZ will hold the last exception caught in the
2741 previous frame (or the frame before that, etc.).
2742
2743 The first bullet fixed the bug in the above example. The second
2744 bullet was for backwards compatibility: it was (and is) common to
2745 have a function that is called when an exception is caught, and to
2746 have that function access the caught exception via sys.exc_ZZZ.
2747 (Example: traceback.print_exc()).
2748
2749 At the same time I fixed the problem that sys.exc_ZZZ weren't
2750 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2751 but that's really a separate improvement.
2752
2753 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2754 variables to what they were before the current frame was called. The
2755 set_exc_info() function saves them on the frame so that
2756 reset_exc_info() can restore them. The invariant is that
2757 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2758 exception (where "catching" an exception applies only to successful
2759 except clauses); and if the current frame ever caught an exception,
2760 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2761 at the start of the current frame.
2762
2763*/
2764
Guido van Rossuma027efa1997-05-05 20:56:21 +00002765static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002766set_exc_info(PyThreadState *tstate,
2767 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002768{
2769 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002770 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002771
Guido van Rossuma027efa1997-05-05 20:56:21 +00002772 frame = tstate->frame;
2773 if (frame->f_exc_type == NULL) {
2774 /* This frame didn't catch an exception before */
2775 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002776 if (tstate->exc_type == NULL) {
2777 Py_INCREF(Py_None);
2778 tstate->exc_type = Py_None;
2779 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002780 tmp_type = frame->f_exc_type;
2781 tmp_value = frame->f_exc_value;
2782 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002783 Py_XINCREF(tstate->exc_type);
2784 Py_XINCREF(tstate->exc_value);
2785 Py_XINCREF(tstate->exc_traceback);
2786 frame->f_exc_type = tstate->exc_type;
2787 frame->f_exc_value = tstate->exc_value;
2788 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002789 Py_XDECREF(tmp_type);
2790 Py_XDECREF(tmp_value);
2791 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002792 }
2793 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002794 tmp_type = tstate->exc_type;
2795 tmp_value = tstate->exc_value;
2796 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002797 Py_XINCREF(type);
2798 Py_XINCREF(value);
2799 Py_XINCREF(tb);
2800 tstate->exc_type = type;
2801 tstate->exc_value = value;
2802 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002803 Py_XDECREF(tmp_type);
2804 Py_XDECREF(tmp_value);
2805 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002806 /* For b/w compatibility */
2807 PySys_SetObject("exc_type", type);
2808 PySys_SetObject("exc_value", value);
2809 PySys_SetObject("exc_traceback", tb);
2810}
2811
2812static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002813reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002814{
2815 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002816 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002817 frame = tstate->frame;
2818 if (frame->f_exc_type != NULL) {
2819 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002820 tmp_type = tstate->exc_type;
2821 tmp_value = tstate->exc_value;
2822 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002823 Py_XINCREF(frame->f_exc_type);
2824 Py_XINCREF(frame->f_exc_value);
2825 Py_XINCREF(frame->f_exc_traceback);
2826 tstate->exc_type = frame->f_exc_type;
2827 tstate->exc_value = frame->f_exc_value;
2828 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002829 Py_XDECREF(tmp_type);
2830 Py_XDECREF(tmp_value);
2831 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002832 /* For b/w compatibility */
2833 PySys_SetObject("exc_type", frame->f_exc_type);
2834 PySys_SetObject("exc_value", frame->f_exc_value);
2835 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2836 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002837 tmp_type = frame->f_exc_type;
2838 tmp_value = frame->f_exc_value;
2839 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002840 frame->f_exc_type = NULL;
2841 frame->f_exc_value = NULL;
2842 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002843 Py_XDECREF(tmp_type);
2844 Py_XDECREF(tmp_value);
2845 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002846}
2847
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002848/* Logic for the raise statement (too complicated for inlining).
2849 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002850static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002851do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002852{
Guido van Rossumd295f121998-04-09 21:39:57 +00002853 if (type == NULL) {
2854 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002855 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002856 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2857 value = tstate->exc_value;
2858 tb = tstate->exc_traceback;
2859 Py_XINCREF(type);
2860 Py_XINCREF(value);
2861 Py_XINCREF(tb);
2862 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002863
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002864 /* We support the following forms of raise:
2865 raise <class>, <classinstance>
2866 raise <class>, <argument tuple>
2867 raise <class>, None
2868 raise <class>, <argument>
2869 raise <classinstance>, None
2870 raise <string>, <object>
2871 raise <string>, None
2872
2873 An omitted second argument is the same as None.
2874
2875 In addition, raise <tuple>, <anything> is the same as
2876 raising the tuple's first item (and it better have one!);
2877 this rule is applied recursively.
2878
2879 Finally, an optional third argument can be supplied, which
2880 gives the traceback to be substituted (useful when
2881 re-raising an exception after examining it). */
2882
2883 /* First, check the traceback argument, replacing None with
2884 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002885 if (tb == Py_None) {
2886 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002887 tb = NULL;
2888 }
2889 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002890 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002891 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002892 goto raise_error;
2893 }
2894
2895 /* Next, replace a missing value with None */
2896 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002897 value = Py_None;
2898 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002899 }
2900
2901 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002902 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2903 PyObject *tmp = type;
2904 type = PyTuple_GET_ITEM(type, 0);
2905 Py_INCREF(type);
2906 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002907 }
2908
Tim Petersafb2c802002-04-18 18:06:20 +00002909 if (PyString_CheckExact(type))
2910 /* Raising builtin string is deprecated but still allowed --
2911 * do nothing. Raising an instance of a new-style str
2912 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002913 PyErr_Warn(PyExc_PendingDeprecationWarning,
2914 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002915
2916 else if (PyClass_Check(type))
2917 PyErr_NormalizeException(&type, &value, &tb);
2918
Guido van Rossumb209a111997-04-29 18:18:01 +00002919 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002920 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002921 if (value != Py_None) {
2922 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002923 "instance exception may not have a separate value");
2924 goto raise_error;
2925 }
2926 else {
2927 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002928 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002929 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002930 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2931 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002932 }
2933 }
2934 else {
2935 /* Not something you can raise. You get an exception
2936 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002937 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002938 "exceptions must be classes, instances, or "
2939 "strings (deprecated), not %s",
2940 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002941 goto raise_error;
2942 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002943 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002944 if (tb == NULL)
2945 return WHY_EXCEPTION;
2946 else
2947 return WHY_RERAISE;
2948 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002949 Py_XDECREF(value);
2950 Py_XDECREF(type);
2951 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002952 return WHY_EXCEPTION;
2953}
2954
Tim Petersd6d010b2001-06-21 02:49:55 +00002955/* Iterate v argcnt times and store the results on the stack (via decreasing
2956 sp). Return 1 for success, 0 if error. */
2957
Barry Warsawe42b18f1997-08-25 22:13:04 +00002958static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002959unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002960{
Tim Petersd6d010b2001-06-21 02:49:55 +00002961 int i = 0;
2962 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002963 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002964
Tim Petersd6d010b2001-06-21 02:49:55 +00002965 assert(v != NULL);
2966
2967 it = PyObject_GetIter(v);
2968 if (it == NULL)
2969 goto Error;
2970
2971 for (; i < argcnt; i++) {
2972 w = PyIter_Next(it);
2973 if (w == NULL) {
2974 /* Iterator done, via error or exhaustion. */
2975 if (!PyErr_Occurred()) {
2976 PyErr_Format(PyExc_ValueError,
2977 "need more than %d value%s to unpack",
2978 i, i == 1 ? "" : "s");
2979 }
2980 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002981 }
2982 *--sp = w;
2983 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002984
2985 /* We better have exhausted the iterator now. */
2986 w = PyIter_Next(it);
2987 if (w == NULL) {
2988 if (PyErr_Occurred())
2989 goto Error;
2990 Py_DECREF(it);
2991 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002992 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002993 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002994 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002995 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002996Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002997 for (; i > 0; i--, sp++)
2998 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002999 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003000 return 0;
3001}
3002
3003
Guido van Rossum96a42c81992-01-12 02:29:51 +00003004#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003005static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003006prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003007{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003008 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003009 if (PyObject_Print(v, stdout, 0) != 0)
3010 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003011 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003012 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003013}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003014#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003015
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003016static void
Fred Drake5755ce62001-06-27 19:19:46 +00003017call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003018{
Guido van Rossumb209a111997-04-29 18:18:01 +00003019 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003020 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003021 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003022 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003023 value = Py_None;
3024 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003025 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003026 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003027 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003028 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003029 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003030 }
Fred Drake5755ce62001-06-27 19:19:46 +00003031 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003032 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003033 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003034 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003035 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003036 Py_XDECREF(type);
3037 Py_XDECREF(value);
3038 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003039 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003040}
3041
Fred Drake4ec5d562001-10-04 19:26:43 +00003042static void
3043call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3044 int what)
3045{
3046 PyObject *type, *value, *traceback;
3047 int err;
3048 PyErr_Fetch(&type, &value, &traceback);
3049 err = call_trace(func, obj, frame, what, NULL);
3050 if (err == 0)
3051 PyErr_Restore(type, value, traceback);
3052 else {
3053 Py_XDECREF(type);
3054 Py_XDECREF(value);
3055 Py_XDECREF(traceback);
3056 }
3057}
3058
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003059static int
Fred Drake5755ce62001-06-27 19:19:46 +00003060call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3061 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003062{
Fred Drake5755ce62001-06-27 19:19:46 +00003063 register PyThreadState *tstate = frame->f_tstate;
3064 int result;
3065 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003066 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003067 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003068 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003069 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003070 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3071 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003072 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003073 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003074}
3075
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003076PyObject *
3077_PyEval_CallTracing(PyObject *func, PyObject *args)
3078{
3079 PyFrameObject *frame = PyEval_GetFrame();
3080 PyThreadState *tstate = frame->f_tstate;
3081 int save_tracing = tstate->tracing;
3082 int save_use_tracing = tstate->use_tracing;
3083 PyObject *result;
3084
3085 tstate->tracing = 0;
3086 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3087 || (tstate->c_profilefunc != NULL));
3088 result = PyObject_Call(func, args, NULL);
3089 tstate->tracing = save_tracing;
3090 tstate->use_tracing = save_use_tracing;
3091 return result;
3092}
3093
Michael W. Hudson006c7522002-11-08 13:08:46 +00003094static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003095maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003096 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3097 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003098{
3099 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003100
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003101 In a nutshell, we use the co_lnotab field of the code object
3102 to tell when execution has moved onto a different line.
3103
3104 As mentioned above, the basic idea is so set things up so
3105 that
3106
3107 *instr_lb <= frame->f_lasti < *instr_ub
3108
3109 is true so long as execution does not change lines.
3110
3111 This is all fairly simple. Digging the information out of
3112 co_lnotab takes some work, but is conceptually clear.
3113
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003114 Somewhat harder to explain is why we don't *always* call the
3115 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003116
3117 Consider this code:
3118
3119 1: def f(a):
3120 2: if a:
3121 3: print 1
3122 4: else:
3123 5: print 2
3124
3125 which compiles to this:
3126
3127 2 0 LOAD_FAST 0 (a)
3128 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003129 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003130
3131 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003132 10 PRINT_ITEM
3133 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003134 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003135 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003136
3137 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003138 19 PRINT_ITEM
3139 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003140 >> 21 LOAD_CONST 0 (None)
3141 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003142
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003143 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003144 15 and the co_lnotab will claim that execution has moved to
3145 line 3. This is at best misleading. In this case we could
3146 associate the POP_TOP with line 4, but that doesn't make
3147 sense in all cases (I think).
3148
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003149 What we do is only call the line trace function if the co_lnotab
3150 indicates we have jumped to the *start* of a line, i.e. if the
3151 current instruction offset matches the offset given for the
3152 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003153
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003154 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003155 Execution will jump from instruction offset 12 to offset 21.
3156 Then the co_lnotab would imply that execution has moved to line
3157 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003158
3159 Why do we set f_lineno when tracing? Well, consider the code
3160 above when 'a' is true. If stepping through this with 'n' in
3161 pdb, you would stop at line 1 with a "call" type event, then
3162 line events on lines 2 and 3, then a "return" type event -- but
3163 you would be shown line 5 during this event. This is a change
3164 from the behaviour in 2.2 and before, and I've found it
3165 confusing in practice. By setting and using f_lineno when
3166 tracing, one can report a line number different from that
3167 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003168 */
3169
Michael W. Hudson006c7522002-11-08 13:08:46 +00003170 int result = 0;
3171
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003172 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003173 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003174 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003175 unsigned char* p;
3176
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003177 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3178 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003179
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003180 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003181 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003182
3183 /* possible optimization: if f->f_lasti == instr_ub
3184 (likely to be a common case) then we already know
3185 instr_lb -- if we stored the matching value of p
3186 somwhere we could skip the first while loop. */
3187
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003188 /* see comments in compile.c for the description of
3189 co_lnotab. A point to remember: increments to p
3190 should come in pairs -- although we don't care about
3191 the line increments here, treating them as byte
3192 increments gets confusing, to say the least. */
3193
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003194 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003195 if (addr + *p > frame->f_lasti)
3196 break;
3197 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003198 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003199 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003200 --size;
3201 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003202
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003203 if (addr == frame->f_lasti) {
3204 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003205 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003206 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003207 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003208
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003209 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003210 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003211 addr += *p++;
3212 if (*p++)
3213 break;
3214 }
3215 *instr_ub = addr;
3216 }
3217 else {
3218 *instr_ub = INT_MAX;
3219 }
3220 }
Armin Rigobf57a142004-03-22 19:24:58 +00003221 else if (frame->f_lasti <= *instr_prev) {
3222 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003223 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003224 PyTrace_LINE, Py_None);
3225 }
3226 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003227 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003228}
3229
Fred Drake5755ce62001-06-27 19:19:46 +00003230void
3231PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003232{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003233 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003234 PyObject *temp = tstate->c_profileobj;
3235 Py_XINCREF(arg);
3236 tstate->c_profilefunc = NULL;
3237 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003238 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003239 Py_XDECREF(temp);
3240 tstate->c_profilefunc = func;
3241 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003242 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003243}
3244
3245void
3246PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3247{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003248 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003249 PyObject *temp = tstate->c_traceobj;
3250 Py_XINCREF(arg);
3251 tstate->c_tracefunc = NULL;
3252 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003253 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003254 Py_XDECREF(temp);
3255 tstate->c_tracefunc = func;
3256 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003257 tstate->use_tracing = ((func != NULL)
3258 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003259}
3260
Guido van Rossumb209a111997-04-29 18:18:01 +00003261PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003262PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003263{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003264 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003265 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003266 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003267 else
3268 return current_frame->f_builtins;
3269}
3270
Guido van Rossumb209a111997-04-29 18:18:01 +00003271PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003272PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003273{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003274 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003275 if (current_frame == NULL)
3276 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003277 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003278 return current_frame->f_locals;
3279}
3280
Guido van Rossumb209a111997-04-29 18:18:01 +00003281PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003282PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003283{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003284 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003285 if (current_frame == NULL)
3286 return NULL;
3287 else
3288 return current_frame->f_globals;
3289}
3290
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003291PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003292PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003293{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003294 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003295 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003296}
3297
Guido van Rossum6135a871995-01-09 17:53:26 +00003298int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003299PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003300{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003301 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003302 return current_frame == NULL ? 0 : current_frame->f_restricted;
3303}
3304
Guido van Rossumbe270261997-05-22 22:26:18 +00003305int
Tim Peters5ba58662001-07-16 02:29:45 +00003306PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003307{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003308 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003309 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003310
3311 if (current_frame != NULL) {
3312 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003313 const int compilerflags = codeflags & PyCF_MASK;
3314 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003315 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003316 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003317 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003318#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003319 if (codeflags & CO_GENERATOR_ALLOWED) {
3320 result = 1;
3321 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3322 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003323#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003324 }
3325 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003326}
3327
3328int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003329Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003330{
Guido van Rossumb209a111997-04-29 18:18:01 +00003331 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003332 if (f == NULL)
3333 return 0;
3334 if (!PyFile_SoftSpace(f, 0))
3335 return 0;
3336 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003337}
3338
Guido van Rossum3f5da241990-12-20 15:06:42 +00003339
Guido van Rossum681d79a1995-07-18 14:51:37 +00003340/* External interface to call any callable object.
3341 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003342
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003343#undef PyEval_CallObject
3344/* for backward compatibility: export this interface */
3345
Guido van Rossumb209a111997-04-29 18:18:01 +00003346PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003347PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003348{
Guido van Rossumb209a111997-04-29 18:18:01 +00003349 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003350}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003351#define PyEval_CallObject(func,arg) \
3352 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003353
Guido van Rossumb209a111997-04-29 18:18:01 +00003354PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003355PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003356{
Jeremy Hylton52820442001-01-03 23:52:36 +00003357 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003358
3359 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003360 arg = PyTuple_New(0);
3361 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003362 PyErr_SetString(PyExc_TypeError,
3363 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003364 return NULL;
3365 }
3366 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003367 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003368
Guido van Rossumb209a111997-04-29 18:18:01 +00003369 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003370 PyErr_SetString(PyExc_TypeError,
3371 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003372 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003373 return NULL;
3374 }
3375
Tim Peters6d6c1a32001-08-02 04:15:00 +00003376 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003377 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003378 return result;
3379}
3380
Tim Peters6d6c1a32001-08-02 04:15:00 +00003381char *
3382PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003383{
3384 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003385 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003386 else if (PyFunction_Check(func))
3387 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3388 else if (PyCFunction_Check(func))
3389 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3390 else if (PyClass_Check(func))
3391 return PyString_AsString(((PyClassObject*)func)->cl_name);
3392 else if (PyInstance_Check(func)) {
3393 return PyString_AsString(
3394 ((PyInstanceObject*)func)->in_class->cl_name);
3395 } else {
3396 return func->ob_type->tp_name;
3397 }
3398}
3399
Tim Peters6d6c1a32001-08-02 04:15:00 +00003400char *
3401PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003402{
3403 if (PyMethod_Check(func))
3404 return "()";
3405 else if (PyFunction_Check(func))
3406 return "()";
3407 else if (PyCFunction_Check(func))
3408 return "()";
3409 else if (PyClass_Check(func))
3410 return " constructor";
3411 else if (PyInstance_Check(func)) {
3412 return " instance";
3413 } else {
3414 return " object";
3415 }
3416}
3417
Martin v. Löwise440e472004-06-01 15:22:42 +00003418PyObject *
3419PyEval_EvaluateFrame(PyObject *fo)
3420{
3421 return eval_frame((PyFrameObject *)fo);
3422}
3423
Jeremy Hylton52820442001-01-03 23:52:36 +00003424#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3425
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003426static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003427err_args(PyObject *func, int flags, int nargs)
3428{
3429 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003430 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003431 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003432 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003433 nargs);
3434 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003435 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003436 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003437 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003438 nargs);
3439}
3440
Nicholas Bastind858a772004-06-25 23:31:06 +00003441#define C_TRACE(call) \
3442if (tstate->use_tracing && tstate->c_profilefunc) { \
3443 if (call_trace(tstate->c_profilefunc, \
3444 tstate->c_profileobj, \
3445 tstate->frame, PyTrace_C_CALL, \
3446 func)) \
3447 { return NULL; } \
3448 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003449 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003450 if (x == NULL) { \
3451 if (call_trace (tstate->c_profilefunc, \
3452 tstate->c_profileobj, \
3453 tstate->frame, PyTrace_C_EXCEPTION, \
3454 func)) \
3455 { return NULL; } \
3456 } else { \
3457 if (call_trace(tstate->c_profilefunc, \
3458 tstate->c_profileobj, \
3459 tstate->frame, PyTrace_C_RETURN, \
3460 func)) \
3461 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003462 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003463 } \
3464} else { \
3465 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003466 }
3467
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003468static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003469call_function(PyObject ***pp_stack, int oparg
3470#ifdef WITH_TSC
3471 , uint64* pintr0, uint64* pintr1
3472#endif
3473 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003474{
3475 int na = oparg & 0xff;
3476 int nk = (oparg>>8) & 0xff;
3477 int n = na + 2 * nk;
3478 PyObject **pfunc = (*pp_stack) - n - 1;
3479 PyObject *func = *pfunc;
3480 PyObject *x, *w;
3481
Jeremy Hylton985eba52003-02-05 23:13:00 +00003482 /* Always dispatch PyCFunction first, because these are
3483 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003484 */
3485 if (PyCFunction_Check(func) && nk == 0) {
3486 int flags = PyCFunction_GET_FLAGS(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003487 PCALL(PCALL_CFUNCTION);
Nicholas Bastind858a772004-06-25 23:31:06 +00003488 PyThreadState *tstate = PyThreadState_GET();
Jeremy Hylton192690e2002-08-16 18:36:11 +00003489 if (flags & (METH_NOARGS | METH_O)) {
3490 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3491 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003492 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003493 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003494 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003495 else if (flags & METH_O && na == 1) {
3496 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003497 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003498 Py_DECREF(arg);
3499 }
3500 else {
3501 err_args(func, flags, na);
3502 x = NULL;
3503 }
3504 }
3505 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003506 PyObject *callargs;
3507 callargs = load_args(pp_stack, na);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003508#ifdef WITH_TSC
3509 rdtscll(*pintr0);
3510#endif
Nicholas Bastind858a772004-06-25 23:31:06 +00003511 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003512#ifdef WITH_TSC
3513 rdtscll(*pintr1);
3514#endif
Tim Peters8a5c3c72004-04-05 19:36:21 +00003515 Py_XDECREF(callargs);
3516 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003517 } else {
3518 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3519 /* optimize access to bound methods */
3520 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003521 PCALL(PCALL_METHOD);
3522 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003523 Py_INCREF(self);
3524 func = PyMethod_GET_FUNCTION(func);
3525 Py_INCREF(func);
3526 Py_DECREF(*pfunc);
3527 *pfunc = self;
3528 na++;
3529 n++;
3530 } else
3531 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003532#ifdef WITH_TSC
3533 rdtscll(*pintr0);
3534#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003535 if (PyFunction_Check(func))
3536 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003537 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003538 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003539#ifdef WITH_TSC
3540 rdtscll(*pintr1);
3541#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003542 Py_DECREF(func);
3543 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003544
Jeremy Hylton985eba52003-02-05 23:13:00 +00003545 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003546 while ((*pp_stack) > pfunc) {
3547 w = EXT_POP(*pp_stack);
3548 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003549 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003550 }
3551 return x;
3552}
3553
Jeremy Hylton192690e2002-08-16 18:36:11 +00003554/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003555 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003556 For the simplest case -- a function that takes only positional
3557 arguments and is called with only positional arguments -- it
3558 inlines the most primitive frame setup code from
3559 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3560 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003561*/
3562
3563static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003564fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003565{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003566 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003567 PyObject *globals = PyFunction_GET_GLOBALS(func);
3568 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3569 PyObject **d = NULL;
3570 int nd = 0;
3571
Jeremy Hylton985eba52003-02-05 23:13:00 +00003572 PCALL(PCALL_FUNCTION);
3573 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003574 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003575 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3576 PyFrameObject *f;
3577 PyObject *retval = NULL;
3578 PyThreadState *tstate = PyThreadState_GET();
3579 PyObject **fastlocals, **stack;
3580 int i;
3581
3582 PCALL(PCALL_FASTER_FUNCTION);
3583 assert(globals != NULL);
3584 /* XXX Perhaps we should create a specialized
3585 PyFrame_New() that doesn't take locals, but does
3586 take builtins without sanity checking them.
3587 */
3588 f = PyFrame_New(tstate, co, globals, NULL);
3589 if (f == NULL)
3590 return NULL;
3591
3592 fastlocals = f->f_localsplus;
3593 stack = (*pp_stack) - n;
3594
3595 for (i = 0; i < n; i++) {
3596 Py_INCREF(*stack);
3597 fastlocals[i] = *stack++;
3598 }
3599 retval = eval_frame(f);
3600 assert(tstate != NULL);
3601 ++tstate->recursion_depth;
3602 Py_DECREF(f);
3603 --tstate->recursion_depth;
3604 return retval;
3605 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003606 if (argdefs != NULL) {
3607 d = &PyTuple_GET_ITEM(argdefs, 0);
3608 nd = ((PyTupleObject *)argdefs)->ob_size;
3609 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003610 return PyEval_EvalCodeEx(co, globals,
3611 (PyObject *)NULL, (*pp_stack)-n, na,
3612 (*pp_stack)-2*nk, nk, d, nd,
3613 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003614}
3615
3616static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003617update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3618 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003619{
3620 PyObject *kwdict = NULL;
3621 if (orig_kwdict == NULL)
3622 kwdict = PyDict_New();
3623 else {
3624 kwdict = PyDict_Copy(orig_kwdict);
3625 Py_DECREF(orig_kwdict);
3626 }
3627 if (kwdict == NULL)
3628 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003629 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003630 int err;
3631 PyObject *value = EXT_POP(*pp_stack);
3632 PyObject *key = EXT_POP(*pp_stack);
3633 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003634 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003635 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003636 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003637 PyEval_GetFuncName(func),
3638 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003639 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003640 Py_DECREF(key);
3641 Py_DECREF(value);
3642 Py_DECREF(kwdict);
3643 return NULL;
3644 }
3645 err = PyDict_SetItem(kwdict, key, value);
3646 Py_DECREF(key);
3647 Py_DECREF(value);
3648 if (err) {
3649 Py_DECREF(kwdict);
3650 return NULL;
3651 }
3652 }
3653 return kwdict;
3654}
3655
3656static PyObject *
3657update_star_args(int nstack, int nstar, PyObject *stararg,
3658 PyObject ***pp_stack)
3659{
3660 PyObject *callargs, *w;
3661
3662 callargs = PyTuple_New(nstack + nstar);
3663 if (callargs == NULL) {
3664 return NULL;
3665 }
3666 if (nstar) {
3667 int i;
3668 for (i = 0; i < nstar; i++) {
3669 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3670 Py_INCREF(a);
3671 PyTuple_SET_ITEM(callargs, nstack + i, a);
3672 }
3673 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003674 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003675 w = EXT_POP(*pp_stack);
3676 PyTuple_SET_ITEM(callargs, nstack, w);
3677 }
3678 return callargs;
3679}
3680
3681static PyObject *
3682load_args(PyObject ***pp_stack, int na)
3683{
3684 PyObject *args = PyTuple_New(na);
3685 PyObject *w;
3686
3687 if (args == NULL)
3688 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003689 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003690 w = EXT_POP(*pp_stack);
3691 PyTuple_SET_ITEM(args, na, w);
3692 }
3693 return args;
3694}
3695
3696static PyObject *
3697do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3698{
3699 PyObject *callargs = NULL;
3700 PyObject *kwdict = NULL;
3701 PyObject *result = NULL;
3702
3703 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003704 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003705 if (kwdict == NULL)
3706 goto call_fail;
3707 }
3708 callargs = load_args(pp_stack, na);
3709 if (callargs == NULL)
3710 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003711#ifdef CALL_PROFILE
3712 /* At this point, we have to look at the type of func to
3713 update the call stats properly. Do it here so as to avoid
3714 exposing the call stats machinery outside ceval.c
3715 */
3716 if (PyFunction_Check(func))
3717 PCALL(PCALL_FUNCTION);
3718 else if (PyMethod_Check(func))
3719 PCALL(PCALL_METHOD);
3720 else if (PyType_Check(func))
3721 PCALL(PCALL_TYPE);
3722 else
3723 PCALL(PCALL_OTHER);
3724#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003725 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003726 call_fail:
3727 Py_XDECREF(callargs);
3728 Py_XDECREF(kwdict);
3729 return result;
3730}
3731
3732static PyObject *
3733ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3734{
3735 int nstar = 0;
3736 PyObject *callargs = NULL;
3737 PyObject *stararg = NULL;
3738 PyObject *kwdict = NULL;
3739 PyObject *result = NULL;
3740
3741 if (flags & CALL_FLAG_KW) {
3742 kwdict = EXT_POP(*pp_stack);
3743 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003744 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003745 "%s%s argument after ** "
3746 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003747 PyEval_GetFuncName(func),
3748 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003749 goto ext_call_fail;
3750 }
3751 }
3752 if (flags & CALL_FLAG_VAR) {
3753 stararg = EXT_POP(*pp_stack);
3754 if (!PyTuple_Check(stararg)) {
3755 PyObject *t = NULL;
3756 t = PySequence_Tuple(stararg);
3757 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003758 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3759 PyErr_Format(PyExc_TypeError,
3760 "%s%s argument after * "
3761 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003762 PyEval_GetFuncName(func),
3763 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003764 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003765 goto ext_call_fail;
3766 }
3767 Py_DECREF(stararg);
3768 stararg = t;
3769 }
3770 nstar = PyTuple_GET_SIZE(stararg);
3771 }
3772 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003773 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003774 if (kwdict == NULL)
3775 goto ext_call_fail;
3776 }
3777 callargs = update_star_args(na, nstar, stararg, pp_stack);
3778 if (callargs == NULL)
3779 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003780#ifdef CALL_PROFILE
3781 /* At this point, we have to look at the type of func to
3782 update the call stats properly. Do it here so as to avoid
3783 exposing the call stats machinery outside ceval.c
3784 */
3785 if (PyFunction_Check(func))
3786 PCALL(PCALL_FUNCTION);
3787 else if (PyMethod_Check(func))
3788 PCALL(PCALL_METHOD);
3789 else if (PyType_Check(func))
3790 PCALL(PCALL_TYPE);
3791 else
3792 PCALL(PCALL_OTHER);
3793#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003794 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003795 ext_call_fail:
3796 Py_XDECREF(callargs);
3797 Py_XDECREF(kwdict);
3798 Py_XDECREF(stararg);
3799 return result;
3800}
3801
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003802#define SLICE_ERROR_MSG \
3803 "standard sequence type does not support step size other than one"
3804
Tim Peterscb479e72001-12-16 19:11:44 +00003805/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3806 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3807 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3808*/
Tim Petersb5196382001-12-16 19:44:20 +00003809/* Note: If v is NULL, return success without storing into *pi. This
3810 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3811 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003812*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003813int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003814_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003815{
Tim Petersb5196382001-12-16 19:44:20 +00003816 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003817 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003818 if (PyInt_Check(v)) {
3819 x = PyInt_AsLong(v);
3820 } else if (PyLong_Check(v)) {
3821 x = PyLong_AsLong(v);
3822 if (x==-1 && PyErr_Occurred()) {
3823 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003824 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003825
Guido van Rossumac7be682001-01-17 15:42:30 +00003826 if (!PyErr_ExceptionMatches(
3827 PyExc_OverflowError)) {
3828 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003829 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003830 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003831 }
3832
Guido van Rossumac7be682001-01-17 15:42:30 +00003833 /* Clear the OverflowError */
3834 PyErr_Clear();
3835
3836 /* It's an overflow error, so we need to
3837 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003838 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003839 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003840
3841 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003842 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003843 if (long_zero == NULL)
3844 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003845
3846 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003847 cmp = PyObject_RichCompareBool(v, long_zero,
3848 Py_GT);
3849 Py_DECREF(long_zero);
3850 if (cmp < 0)
3851 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003852 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003853 x = INT_MAX;
3854 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003855 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003856 }
3857 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003858 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003859 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003860 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003861 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003862 /* Truncate -- very long indices are truncated anyway */
3863 if (x > INT_MAX)
3864 x = INT_MAX;
3865 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003866 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003867 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003868 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003869 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003870}
3871
Guido van Rossum50d756e2001-08-18 17:43:36 +00003872#undef ISINT
3873#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3874
Guido van Rossumb209a111997-04-29 18:18:01 +00003875static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003876apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003877{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003878 PyTypeObject *tp = u->ob_type;
3879 PySequenceMethods *sq = tp->tp_as_sequence;
3880
3881 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3882 int ilow = 0, ihigh = INT_MAX;
3883 if (!_PyEval_SliceIndex(v, &ilow))
3884 return NULL;
3885 if (!_PyEval_SliceIndex(w, &ihigh))
3886 return NULL;
3887 return PySequence_GetSlice(u, ilow, ihigh);
3888 }
3889 else {
3890 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003891 if (slice != NULL) {
3892 PyObject *res = PyObject_GetItem(u, slice);
3893 Py_DECREF(slice);
3894 return res;
3895 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003896 else
3897 return NULL;
3898 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003899}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003900
3901static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003902assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3903 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003904{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003905 PyTypeObject *tp = u->ob_type;
3906 PySequenceMethods *sq = tp->tp_as_sequence;
3907
3908 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3909 int ilow = 0, ihigh = INT_MAX;
3910 if (!_PyEval_SliceIndex(v, &ilow))
3911 return -1;
3912 if (!_PyEval_SliceIndex(w, &ihigh))
3913 return -1;
3914 if (x == NULL)
3915 return PySequence_DelSlice(u, ilow, ihigh);
3916 else
3917 return PySequence_SetSlice(u, ilow, ihigh, x);
3918 }
3919 else {
3920 PyObject *slice = PySlice_New(v, w, NULL);
3921 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003922 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003923 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003924 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003925 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003926 res = PyObject_DelItem(u, slice);
3927 Py_DECREF(slice);
3928 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003929 }
3930 else
3931 return -1;
3932 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003933}
3934
Guido van Rossumb209a111997-04-29 18:18:01 +00003935static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003936cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003937{
Guido van Rossumac7be682001-01-17 15:42:30 +00003938 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003939 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003940 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003941 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003942 break;
3943 case PyCmp_IS_NOT:
3944 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003945 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003946 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003947 res = PySequence_Contains(w, v);
3948 if (res < 0)
3949 return NULL;
3950 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003951 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003952 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003953 if (res < 0)
3954 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003955 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003956 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003957 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003958 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003959 break;
3960 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003961 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003962 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003963 v = res ? Py_True : Py_False;
3964 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003965 return v;
3966}
3967
Thomas Wouters52152252000-08-17 22:55:00 +00003968static PyObject *
3969import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003970{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003971 PyObject *x;
3972
3973 x = PyObject_GetAttr(v, name);
3974 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003975 PyErr_Format(PyExc_ImportError,
3976 "cannot import name %.230s",
3977 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003978 }
Thomas Wouters52152252000-08-17 22:55:00 +00003979 return x;
3980}
Guido van Rossumac7be682001-01-17 15:42:30 +00003981
Thomas Wouters52152252000-08-17 22:55:00 +00003982static int
3983import_all_from(PyObject *locals, PyObject *v)
3984{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003985 PyObject *all = PyObject_GetAttrString(v, "__all__");
3986 PyObject *dict, *name, *value;
3987 int skip_leading_underscores = 0;
3988 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003989
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003990 if (all == NULL) {
3991 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3992 return -1; /* Unexpected error */
3993 PyErr_Clear();
3994 dict = PyObject_GetAttrString(v, "__dict__");
3995 if (dict == NULL) {
3996 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3997 return -1;
3998 PyErr_SetString(PyExc_ImportError,
3999 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004000 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004001 }
4002 all = PyMapping_Keys(dict);
4003 Py_DECREF(dict);
4004 if (all == NULL)
4005 return -1;
4006 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004007 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004008
4009 for (pos = 0, err = 0; ; pos++) {
4010 name = PySequence_GetItem(all, pos);
4011 if (name == NULL) {
4012 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4013 err = -1;
4014 else
4015 PyErr_Clear();
4016 break;
4017 }
4018 if (skip_leading_underscores &&
4019 PyString_Check(name) &&
4020 PyString_AS_STRING(name)[0] == '_')
4021 {
4022 Py_DECREF(name);
4023 continue;
4024 }
4025 value = PyObject_GetAttr(v, name);
4026 if (value == NULL)
4027 err = -1;
4028 else
4029 err = PyDict_SetItem(locals, name, value);
4030 Py_DECREF(name);
4031 Py_XDECREF(value);
4032 if (err != 0)
4033 break;
4034 }
4035 Py_DECREF(all);
4036 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004037}
4038
Guido van Rossumb209a111997-04-29 18:18:01 +00004039static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004040build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004041{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004042 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004043
4044 if (PyDict_Check(methods))
4045 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004046 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004047 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004048 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4049 base = PyTuple_GET_ITEM(bases, 0);
4050 metaclass = PyObject_GetAttrString(base, "__class__");
4051 if (metaclass == NULL) {
4052 PyErr_Clear();
4053 metaclass = (PyObject *)base->ob_type;
4054 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004055 }
4056 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004057 else {
4058 PyObject *g = PyEval_GetGlobals();
4059 if (g != NULL && PyDict_Check(g))
4060 metaclass = PyDict_GetItemString(g, "__metaclass__");
4061 if (metaclass == NULL)
4062 metaclass = (PyObject *) &PyClass_Type;
4063 Py_INCREF(metaclass);
4064 }
4065 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4066 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004067 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4068 /* A type error here likely means that the user passed
4069 in a base that was not a class (such the random module
4070 instead of the random.random type). Help them out with
4071 a more informative error message */
4072 PyErr_SetString(PyExc_TypeError,
4073 "Error when calling the metaclass.\n" \
4074 "Make sure the base arguments are valid.");
4075 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004076 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004077}
4078
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004079static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004080exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4081 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004082{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004083 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004084 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004085 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004086
Guido van Rossumb209a111997-04-29 18:18:01 +00004087 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4088 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004089 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004090 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004091 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004092 locals = PyTuple_GetItem(prog, 2);
4093 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004094 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004095 if (globals == Py_None) {
4096 globals = PyEval_GetGlobals();
4097 if (locals == Py_None) {
4098 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004099 plain = 1;
4100 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004101 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004102 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004103 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004104 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004105 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004106 !PyCode_Check(prog) &&
4107 !PyFile_Check(prog)) {
4108 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004109 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004110 return -1;
4111 }
Fred Drake661ea262000-10-24 19:57:45 +00004112 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004113 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004114 "exec: arg 2 must be a dictionary or None");
4115 return -1;
4116 }
4117 if (!PyDict_Check(locals)) {
4118 PyErr_SetString(PyExc_TypeError,
4119 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004120 return -1;
4121 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004122 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004123 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004124 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004125 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4126 PyErr_SetString(PyExc_TypeError,
4127 "code object passed to exec may not contain free variables");
4128 return -1;
4129 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004130 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004131 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004132 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004133 FILE *fp = PyFile_AsFile(prog);
4134 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004135 PyCompilerFlags cf;
4136 cf.cf_flags = 0;
4137 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004138 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004139 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004140 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004141 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004142 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004143 }
4144 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004145 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004146 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004147 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004148 cf.cf_flags = 0;
4149#ifdef Py_USING_UNICODE
4150 if (PyUnicode_Check(prog)) {
4151 tmp = PyUnicode_AsUTF8String(prog);
4152 if (tmp == NULL)
4153 return -1;
4154 prog = tmp;
4155 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4156 }
4157#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004158 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004159 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004160 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004161 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004162 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004163 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004164 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004165 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004166 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004167 if (plain)
4168 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004169 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004170 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004171 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004172 return 0;
4173}
Guido van Rossum24c13741995-02-14 09:42:43 +00004174
Guido van Rossumac7be682001-01-17 15:42:30 +00004175static void
Paul Prescode68140d2000-08-30 20:25:01 +00004176format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4177{
4178 char *obj_str;
4179
4180 if (!obj)
4181 return;
4182
4183 obj_str = PyString_AsString(obj);
4184 if (!obj_str)
4185 return;
4186
4187 PyErr_Format(exc, format_str, obj_str);
4188}
Guido van Rossum950361c1997-01-24 13:49:28 +00004189
4190#ifdef DYNAMIC_EXECUTION_PROFILE
4191
Skip Montanarof118cb12001-10-15 20:51:38 +00004192static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004193getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004194{
4195 int i;
4196 PyObject *l = PyList_New(256);
4197 if (l == NULL) return NULL;
4198 for (i = 0; i < 256; i++) {
4199 PyObject *x = PyInt_FromLong(a[i]);
4200 if (x == NULL) {
4201 Py_DECREF(l);
4202 return NULL;
4203 }
4204 PyList_SetItem(l, i, x);
4205 }
4206 for (i = 0; i < 256; i++)
4207 a[i] = 0;
4208 return l;
4209}
4210
4211PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004212_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004213{
4214#ifndef DXPAIRS
4215 return getarray(dxp);
4216#else
4217 int i;
4218 PyObject *l = PyList_New(257);
4219 if (l == NULL) return NULL;
4220 for (i = 0; i < 257; i++) {
4221 PyObject *x = getarray(dxpairs[i]);
4222 if (x == NULL) {
4223 Py_DECREF(l);
4224 return NULL;
4225 }
4226 PyList_SetItem(l, i, x);
4227 }
4228 return l;
4229#endif
4230}
4231
4232#endif