blob: 4c9bdeda1369e267f825be7025a83607058b3529 [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"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Guido van Rossumc6004111993-11-05 10:22:19 +000017#include <ctype.h>
18
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000019#ifdef WITH_TSC
20#include <asm/msr.h>
21
22typedef unsigned long long uint64;
23
24void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
25 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
26{
27 uint64 intr, inst, loop;
28 PyThreadState *tstate = PyThreadState_Get();
29 if (!tstate->interp->tscdump)
30 return;
31 intr = intr1 - intr0;
32 inst = inst1 - inst0 - intr;
33 loop = loop1 - loop0 - intr;
34 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
35 opcode, ticked, inst, loop);
36}
37#endif
38
Guido van Rossum04691fc1992-08-12 15:35:34 +000039/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000040/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000041
Guido van Rossum408027e1996-12-30 16:17:54 +000042#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000043/* For debugging the interpreter: */
44#define LLTRACE 1 /* Low-level trace feature */
45#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000046#endif
47
Jeremy Hylton52820442001-01-03 23:52:36 +000048typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000049
Guido van Rossum374a9221991-04-04 10:40:29 +000050/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000051#ifdef WITH_TSC
52static PyObject *call_function(PyObject ***, int, uint64*, uint64*);
53#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +000054static PyObject *call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000055#endif
Jeremy Hylton52820442001-01-03 23:52:36 +000056static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000057static PyObject *do_call(PyObject *, PyObject ***, int, int);
58static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000059static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000060static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000061static PyObject *load_args(PyObject ***, int);
62#define CALL_FLAG_VAR 1
63#define CALL_FLAG_KW 2
64
Guido van Rossum0a066c01992-03-27 17:29:15 +000065#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000067#endif
Fred Drake5755ce62001-06-27 19:19:46 +000068static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
69 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000070static void call_trace_protected(Py_tracefunc, PyObject *,
71 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +000072static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000073static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +000074 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000075
Tim Petersdbd9ba62000-07-09 03:09:57 +000076static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
77static int assign_slice(PyObject *, PyObject *,
78 PyObject *, PyObject *);
79static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000080static PyObject *import_from(PyObject *, PyObject *);
81static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000082static PyObject *build_class(PyObject *, PyObject *, PyObject *);
83static int exec_statement(PyFrameObject *,
84 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000085static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
86static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +000087static void format_exc_check_arg(PyObject *, char *, PyObject *);
Raymond Hettinger52a21b82004-08-06 18:43:09 +000088static PyObject *string_concatenate(PyObject *, PyObject *,
89 PyFrameObject *, unsigned char *);
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{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000323 static volatile 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
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000461PyObject *
462PyEval_EvalFrame(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])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000555#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000556#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000557#define JUMPBY(x) (next_instr += (x))
558
Raymond Hettingerf606f872003-03-16 03:11:04 +0000559/* OpCode prediction macros
560 Some opcodes tend to come in pairs thus making it possible to predict
561 the second code when the first is run. For example, COMPARE_OP is often
562 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
563 followed by a POP_TOP.
564
565 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000566 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000567 processor has a high likelihood of making its own successful branch
568 prediction which results in a nearly zero overhead transition to the
569 next opcode.
570
571 A successful prediction saves a trip through the eval-loop including
572 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000573
Tim Peters8a5c3c72004-04-05 19:36:21 +0000574 If collecting opcode statistics, turn off prediction so that
575 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000576 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000577*/
578
Raymond Hettingera7216982004-02-08 19:59:27 +0000579#ifdef DYNAMIC_EXECUTION_PROFILE
580#define PREDICT(op) if (0) goto PRED_##op
581#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000582#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000583#endif
584
Raymond Hettingerf606f872003-03-16 03:11:04 +0000585#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000586#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); 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 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001071 else if (PyString_CheckExact(v) &&
1072 PyString_CheckExact(w)) {
1073 x = string_concatenate(v, w, f, next_instr);
1074 /* string_concatenate consumed the ref to v */
1075 goto skip_decref_vx;
1076 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001077 else {
1078 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001079 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001080 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001081 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001082 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001083 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001084 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001085 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001086 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001087
Guido van Rossum374a9221991-04-04 10:40:29 +00001088 case BINARY_SUBTRACT:
1089 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001090 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001091 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001092 /* INLINE: int - int */
1093 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001094 a = PyInt_AS_LONG(v);
1095 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001096 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001097 if ((i^a) < 0 && (i^~b) < 0)
1098 goto slow_sub;
1099 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001100 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001101 else {
1102 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001103 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001104 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001105 Py_DECREF(v);
1106 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001107 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001108 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001109 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001110
Guido van Rossum374a9221991-04-04 10:40:29 +00001111 case BINARY_SUBSCR:
1112 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001113 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001114 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001115 /* INLINE: list[int] */
1116 long i = PyInt_AsLong(w);
1117 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001118 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001119 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001120 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001121 Py_INCREF(x);
1122 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001123 else
1124 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001125 }
1126 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001127 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001128 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001129 Py_DECREF(v);
1130 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001131 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001132 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001133 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001134
Guido van Rossum7928cd71991-10-24 14:59:31 +00001135 case BINARY_LSHIFT:
1136 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001137 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001138 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001139 Py_DECREF(v);
1140 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001141 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001142 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001143 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001144
Guido van Rossum7928cd71991-10-24 14:59:31 +00001145 case BINARY_RSHIFT:
1146 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001147 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001148 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001149 Py_DECREF(v);
1150 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001151 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001152 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001153 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Guido van Rossum7928cd71991-10-24 14:59:31 +00001155 case BINARY_AND:
1156 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001157 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001158 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001159 Py_DECREF(v);
1160 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001161 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001162 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001163 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001164
Guido van Rossum7928cd71991-10-24 14:59:31 +00001165 case BINARY_XOR:
1166 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001167 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001168 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001169 Py_DECREF(v);
1170 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001171 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001172 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001173 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001174
Guido van Rossum7928cd71991-10-24 14:59:31 +00001175 case BINARY_OR:
1176 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001177 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001178 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001179 Py_DECREF(v);
1180 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001182 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001183 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001184
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001185 case LIST_APPEND:
1186 w = POP();
1187 v = POP();
1188 err = PyList_Append(v, w);
1189 Py_DECREF(v);
1190 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001191 if (err == 0) {
1192 PREDICT(JUMP_ABSOLUTE);
1193 continue;
1194 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001195 break;
1196
Thomas Wouters434d0822000-08-24 20:11:32 +00001197 case INPLACE_POWER:
1198 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001199 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001200 x = PyNumber_InPlacePower(v, w, Py_None);
1201 Py_DECREF(v);
1202 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001203 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001204 if (x != NULL) continue;
1205 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Thomas Wouters434d0822000-08-24 20:11:32 +00001207 case INPLACE_MULTIPLY:
1208 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001209 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001210 x = PyNumber_InPlaceMultiply(v, w);
1211 Py_DECREF(v);
1212 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001213 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001214 if (x != NULL) continue;
1215 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001216
Thomas Wouters434d0822000-08-24 20:11:32 +00001217 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001218 if (!_Py_QnewFlag) {
1219 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001221 x = PyNumber_InPlaceDivide(v, w);
1222 Py_DECREF(v);
1223 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001224 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001225 if (x != NULL) continue;
1226 break;
1227 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001228 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001229 INPLACE_TRUE_DIVIDE */
1230 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001231 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001232 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001233 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001234 Py_DECREF(v);
1235 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001236 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001237 if (x != NULL) continue;
1238 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Guido van Rossum4668b002001-08-08 05:00:18 +00001240 case INPLACE_FLOOR_DIVIDE:
1241 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001242 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001243 x = PyNumber_InPlaceFloorDivide(v, w);
1244 Py_DECREF(v);
1245 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001246 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001247 if (x != NULL) continue;
1248 break;
1249
Thomas Wouters434d0822000-08-24 20:11:32 +00001250 case INPLACE_MODULO:
1251 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001253 x = PyNumber_InPlaceRemainder(v, w);
1254 Py_DECREF(v);
1255 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001256 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001257 if (x != NULL) continue;
1258 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001259
Thomas Wouters434d0822000-08-24 20:11:32 +00001260 case INPLACE_ADD:
1261 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001263 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001264 /* INLINE: int + int */
1265 register long a, b, i;
1266 a = PyInt_AS_LONG(v);
1267 b = PyInt_AS_LONG(w);
1268 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001269 if ((i^a) < 0 && (i^b) < 0)
1270 goto slow_iadd;
1271 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001272 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001273 else if (PyString_CheckExact(v) &&
1274 PyString_CheckExact(w)) {
1275 x = string_concatenate(v, w, f, next_instr);
1276 /* string_concatenate consumed the ref to v */
1277 goto skip_decref_v;
1278 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001279 else {
1280 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001281 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001282 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001284 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001285 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001286 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001287 if (x != NULL) continue;
1288 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001289
Thomas Wouters434d0822000-08-24 20:11:32 +00001290 case INPLACE_SUBTRACT:
1291 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001292 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001293 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 /* INLINE: int - int */
1295 register long a, b, i;
1296 a = PyInt_AS_LONG(v);
1297 b = PyInt_AS_LONG(w);
1298 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001299 if ((i^a) < 0 && (i^~b) < 0)
1300 goto slow_isub;
1301 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001303 else {
1304 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001305 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001306 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001307 Py_DECREF(v);
1308 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001309 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001310 if (x != NULL) continue;
1311 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001312
Thomas Wouters434d0822000-08-24 20:11:32 +00001313 case INPLACE_LSHIFT:
1314 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001315 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 x = PyNumber_InPlaceLshift(v, w);
1317 Py_DECREF(v);
1318 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001319 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 if (x != NULL) continue;
1321 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001322
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 case INPLACE_RSHIFT:
1324 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001325 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 x = PyNumber_InPlaceRshift(v, w);
1327 Py_DECREF(v);
1328 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001329 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001330 if (x != NULL) continue;
1331 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 case INPLACE_AND:
1334 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001335 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001336 x = PyNumber_InPlaceAnd(v, w);
1337 Py_DECREF(v);
1338 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001339 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 if (x != NULL) continue;
1341 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001342
Thomas Wouters434d0822000-08-24 20:11:32 +00001343 case INPLACE_XOR:
1344 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 x = PyNumber_InPlaceXor(v, w);
1347 Py_DECREF(v);
1348 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001349 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 if (x != NULL) continue;
1351 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001352
Thomas Wouters434d0822000-08-24 20:11:32 +00001353 case INPLACE_OR:
1354 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001355 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001356 x = PyNumber_InPlaceOr(v, w);
1357 Py_DECREF(v);
1358 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001359 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001360 if (x != NULL) continue;
1361 break;
1362
Guido van Rossum374a9221991-04-04 10:40:29 +00001363 case SLICE+0:
1364 case SLICE+1:
1365 case SLICE+2:
1366 case SLICE+3:
1367 if ((opcode-SLICE) & 2)
1368 w = POP();
1369 else
1370 w = NULL;
1371 if ((opcode-SLICE) & 1)
1372 v = POP();
1373 else
1374 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001376 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001377 Py_DECREF(u);
1378 Py_XDECREF(v);
1379 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001380 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001381 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001382 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Guido van Rossum374a9221991-04-04 10:40:29 +00001384 case STORE_SLICE+0:
1385 case STORE_SLICE+1:
1386 case STORE_SLICE+2:
1387 case STORE_SLICE+3:
1388 if ((opcode-STORE_SLICE) & 2)
1389 w = POP();
1390 else
1391 w = NULL;
1392 if ((opcode-STORE_SLICE) & 1)
1393 v = POP();
1394 else
1395 v = NULL;
1396 u = POP();
1397 t = POP();
1398 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001399 Py_DECREF(t);
1400 Py_DECREF(u);
1401 Py_XDECREF(v);
1402 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001403 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001404 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Guido van Rossum374a9221991-04-04 10:40:29 +00001406 case DELETE_SLICE+0:
1407 case DELETE_SLICE+1:
1408 case DELETE_SLICE+2:
1409 case DELETE_SLICE+3:
1410 if ((opcode-DELETE_SLICE) & 2)
1411 w = POP();
1412 else
1413 w = NULL;
1414 if ((opcode-DELETE_SLICE) & 1)
1415 v = POP();
1416 else
1417 v = NULL;
1418 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001419 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001420 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001421 Py_DECREF(u);
1422 Py_XDECREF(v);
1423 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001424 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001425 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Guido van Rossum374a9221991-04-04 10:40:29 +00001427 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001428 w = TOP();
1429 v = SECOND();
1430 u = THIRD();
1431 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001432 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001433 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001434 Py_DECREF(u);
1435 Py_DECREF(v);
1436 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001437 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001438 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001439
Guido van Rossum374a9221991-04-04 10:40:29 +00001440 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001441 w = TOP();
1442 v = SECOND();
1443 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001444 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001445 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001446 Py_DECREF(v);
1447 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001448 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001449 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001450
Guido van Rossum374a9221991-04-04 10:40:29 +00001451 case PRINT_EXPR:
1452 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001453 w = PySys_GetObject("displayhook");
1454 if (w == NULL) {
1455 PyErr_SetString(PyExc_RuntimeError,
1456 "lost sys.displayhook");
1457 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001458 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001459 }
1460 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001461 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001462 if (x == NULL)
1463 err = -1;
1464 }
1465 if (err == 0) {
1466 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001467 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001468 if (w == NULL)
1469 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001470 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001471 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001472 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001474
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001475 case PRINT_ITEM_TO:
1476 w = stream = POP();
1477 /* fall through to PRINT_ITEM */
1478
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 case PRINT_ITEM:
1480 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001481 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001482 w = PySys_GetObject("stdout");
1483 if (w == NULL) {
1484 PyErr_SetString(PyExc_RuntimeError,
1485 "lost sys.stdout");
1486 err = -1;
1487 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001488 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001489 /* PyFile_SoftSpace() can exececute arbitrary code
1490 if sys.stdout is an instance with a __getattr__.
1491 If __getattr__ raises an exception, w will
1492 be freed, so we need to prevent that temporarily. */
1493 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001494 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001495 err = PyFile_WriteString(" ", w);
1496 if (err == 0)
1497 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001498 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001499 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001500 if (PyString_Check(v)) {
1501 char *s = PyString_AS_STRING(v);
1502 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001503 if (len == 0 ||
1504 !isspace(Py_CHARMASK(s[len-1])) ||
1505 s[len-1] == ' ')
1506 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001507 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001508#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001509 else if (PyUnicode_Check(v)) {
1510 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1511 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001512 if (len == 0 ||
1513 !Py_UNICODE_ISSPACE(s[len-1]) ||
1514 s[len-1] == ' ')
1515 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001516 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001517#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001518 else
1519 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001521 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001522 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001523 Py_XDECREF(stream);
1524 stream = NULL;
1525 if (err == 0)
1526 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001527 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001529 case PRINT_NEWLINE_TO:
1530 w = stream = POP();
1531 /* fall through to PRINT_NEWLINE */
1532
Guido van Rossum374a9221991-04-04 10:40:29 +00001533 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001534 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001535 w = PySys_GetObject("stdout");
1536 if (w == NULL)
1537 PyErr_SetString(PyExc_RuntimeError,
1538 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001539 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001540 if (w != NULL) {
1541 err = PyFile_WriteString("\n", w);
1542 if (err == 0)
1543 PyFile_SoftSpace(w, 0);
1544 }
1545 Py_XDECREF(stream);
1546 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001547 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Thomas Wouters434d0822000-08-24 20:11:32 +00001549
1550#ifdef CASE_TOO_BIG
1551 default: switch (opcode) {
1552#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001553 case RAISE_VARARGS:
1554 u = v = w = NULL;
1555 switch (oparg) {
1556 case 3:
1557 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001558 /* Fallthrough */
1559 case 2:
1560 v = POP(); /* value */
1561 /* Fallthrough */
1562 case 1:
1563 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001564 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001565 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001566 break;
1567 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001568 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001569 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001570 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001571 break;
1572 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001573 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001574
Guido van Rossum374a9221991-04-04 10:40:29 +00001575 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001576 if ((x = f->f_locals) != NULL) {
1577 Py_INCREF(x);
1578 PUSH(x);
1579 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001580 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001581 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 case RETURN_VALUE:
1585 retval = POP();
1586 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001587 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001588
Tim Peters5ca576e2001-06-18 22:08:13 +00001589 case YIELD_VALUE:
1590 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001591 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001592 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001593 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001594
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001595 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001596 w = TOP();
1597 v = SECOND();
1598 u = THIRD();
1599 STACKADJ(-3);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001600#ifdef WITH_TSC
1601 rdtscll(intr0);
1602#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001603 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001604#ifdef WITH_TSC
1605 rdtscll(intr1);
1606#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001607 Py_DECREF(u);
1608 Py_DECREF(v);
1609 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001610 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001611
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 case POP_BLOCK:
1613 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001614 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001615 while (STACK_LEVEL() > b->b_level) {
1616 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001617 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 }
1619 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001620 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Guido van Rossum374a9221991-04-04 10:40:29 +00001622 case END_FINALLY:
1623 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001624 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001625 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001626 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001627 if (why == WHY_RETURN ||
1628 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 retval = POP();
1630 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001631 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001633 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001634 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001635 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001636 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001637 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001638 else if (v != Py_None) {
1639 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001640 "'finally' pops bad exception");
1641 why = WHY_EXCEPTION;
1642 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001643 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001645
Guido van Rossum374a9221991-04-04 10:40:29 +00001646 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001647 u = TOP();
1648 v = SECOND();
1649 w = THIRD();
1650 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001651 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001652 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001653 Py_DECREF(u);
1654 Py_DECREF(v);
1655 Py_DECREF(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 STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001659 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001661 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001662 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001663 err = PyDict_SetItem(x, w, v);
1664 else
1665 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001666 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001667 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001668 break;
1669 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001670 PyErr_Format(PyExc_SystemError,
1671 "no locals found when storing %s",
1672 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001676 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001677 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001678 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001679 format_exc_check_arg(PyExc_NameError,
1680 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001681 break;
1682 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001683 PyErr_Format(PyExc_SystemError,
1684 "no locals when deleting %s",
1685 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001687
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001688 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001689 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001691 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1692 PyObject **items = ((PyTupleObject *)v)->ob_item;
1693 while (oparg--) {
1694 w = items[oparg];
1695 Py_INCREF(w);
1696 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001697 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001698 Py_DECREF(v);
1699 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001700 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1701 PyObject **items = ((PyListObject *)v)->ob_item;
1702 while (oparg--) {
1703 w = items[oparg];
1704 Py_INCREF(w);
1705 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001706 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001707 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001708 stack_pointer + oparg))
1709 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001710 else {
1711 if (PyErr_ExceptionMatches(PyExc_TypeError))
1712 PyErr_SetString(PyExc_TypeError,
1713 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001714 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001715 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001716 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001718
Guido van Rossum374a9221991-04-04 10:40:29 +00001719 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001720 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001721 v = TOP();
1722 u = SECOND();
1723 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001724 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1725 Py_DECREF(v);
1726 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001727 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001731 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001732 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001733 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1734 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001735 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001736 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001737
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001738 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001739 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001740 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001741 err = PyDict_SetItem(f->f_globals, w, v);
1742 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001743 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001744 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001746 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001747 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001748 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001749 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001750 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001751 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001752
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001754 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001755 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001756 PyErr_Format(PyExc_SystemError,
1757 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001758 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001759 break;
1760 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001761 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001762 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001763 Py_XINCREF(x);
1764 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001765 else {
1766 x = PyObject_GetItem(v, w);
1767 if (x == NULL && PyErr_Occurred()) {
1768 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1769 break;
1770 PyErr_Clear();
1771 }
1772 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001774 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001775 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001776 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001777 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001778 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001779 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001780 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001781 break;
1782 }
1783 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001784 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001786 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001787 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001790 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001791 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001792 /* Inline the PyDict_GetItem() calls.
1793 WARNING: this is an extreme speed hack.
1794 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001795 long hash = ((PyStringObject *)w)->ob_shash;
1796 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001797 PyDictObject *d;
1798 d = (PyDictObject *)(f->f_globals);
1799 x = d->ma_lookup(d, w, hash)->me_value;
1800 if (x != NULL) {
1801 Py_INCREF(x);
1802 PUSH(x);
1803 continue;
1804 }
1805 d = (PyDictObject *)(f->f_builtins);
1806 x = d->ma_lookup(d, w, hash)->me_value;
1807 if (x != NULL) {
1808 Py_INCREF(x);
1809 PUSH(x);
1810 continue;
1811 }
1812 goto load_global_error;
1813 }
1814 }
1815 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001816 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001818 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001819 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001820 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001821 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001822 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001823 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001824 break;
1825 }
1826 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001827 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001828 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001829 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001830
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001831 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001832 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001833 if (x != NULL) {
1834 SETLOCAL(oparg, NULL);
1835 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001836 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001837 format_exc_check_arg(
1838 PyExc_UnboundLocalError,
1839 UNBOUNDLOCAL_ERROR_MSG,
1840 PyTuple_GetItem(co->co_varnames, oparg)
1841 );
1842 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001843
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001844 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001845 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001846 Py_INCREF(x);
1847 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001848 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001849 break;
1850
1851 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001852 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001853 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001854 if (w != NULL) {
1855 PUSH(w);
1856 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001857 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001858 err = -1;
1859 /* Don't stomp existing exception */
1860 if (PyErr_Occurred())
1861 break;
1862 if (oparg < f->f_ncells) {
1863 v = PyTuple_GetItem(co->co_cellvars,
1864 oparg);
1865 format_exc_check_arg(
1866 PyExc_UnboundLocalError,
1867 UNBOUNDLOCAL_ERROR_MSG,
1868 v);
1869 } else {
1870 v = PyTuple_GetItem(
1871 co->co_freevars,
1872 oparg - f->f_ncells);
1873 format_exc_check_arg(
1874 PyExc_NameError,
1875 UNBOUNDFREE_ERROR_MSG,
1876 v);
1877 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001878 break;
1879
1880 case STORE_DEREF:
1881 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001882 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001883 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001884 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001885 continue;
1886
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001888 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001889 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001890 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001892 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 }
1894 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001895 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001896 }
1897 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001898
Guido van Rossum374a9221991-04-04 10:40:29 +00001899 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001900 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001901 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001902 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001903 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001904 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001905 }
1906 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001907 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 }
1909 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001910
Guido van Rossum374a9221991-04-04 10:40:29 +00001911 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001912 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001913 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001914 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001916
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001918 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001919 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001920 x = PyObject_GetAttr(v, w);
1921 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001922 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001923 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001925
Guido van Rossum374a9221991-04-04 10:40:29 +00001926 case COMPARE_OP:
1927 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001928 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001929 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001930 /* INLINE: cmp(int, int) */
1931 register long a, b;
1932 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001933 a = PyInt_AS_LONG(v);
1934 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001935 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001936 case PyCmp_LT: res = a < b; break;
1937 case PyCmp_LE: res = a <= b; break;
1938 case PyCmp_EQ: res = a == b; break;
1939 case PyCmp_NE: res = a != b; break;
1940 case PyCmp_GT: res = a > b; break;
1941 case PyCmp_GE: res = a >= b; break;
1942 case PyCmp_IS: res = v == w; break;
1943 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001944 default: goto slow_compare;
1945 }
1946 x = res ? Py_True : Py_False;
1947 Py_INCREF(x);
1948 }
1949 else {
1950 slow_compare:
1951 x = cmp_outcome(oparg, v, w);
1952 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001953 Py_DECREF(v);
1954 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001955 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001956 if (x == NULL) break;
1957 PREDICT(JUMP_IF_FALSE);
1958 PREDICT(JUMP_IF_TRUE);
1959 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Guido van Rossum374a9221991-04-04 10:40:29 +00001961 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001962 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001963 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001964 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001965 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001966 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001967 break;
1968 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001969 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001970 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001971 w,
1972 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001973 f->f_locals == NULL ?
1974 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001975 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001976 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001977 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001978 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001979 x = NULL;
1980 break;
1981 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001982#ifdef WITH_TSC
1983 rdtscll(intr0);
1984#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001985 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001986#ifdef WITH_TSC
1987 rdtscll(intr1);
1988#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001989 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001990 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001991 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001992 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001993
Thomas Wouters52152252000-08-17 22:55:00 +00001994 case IMPORT_STAR:
1995 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001996 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001997 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001998 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001999 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002000 break;
2001 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002002#ifdef WITH_TSC
2003 rdtscll(intr0);
2004#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002005 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002006#ifdef WITH_TSC
2007 rdtscll(intr1);
2008#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00002009 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002010 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002011 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002012 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002013
Thomas Wouters52152252000-08-17 22:55:00 +00002014 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002015 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002016 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002017#ifdef WITH_TSC
2018 rdtscll(intr0);
2019#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002020 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002021#ifdef WITH_TSC
2022 rdtscll(intr1);
2023#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002024 PUSH(x);
2025 if (x != NULL) continue;
2026 break;
2027
Guido van Rossum374a9221991-04-04 10:40:29 +00002028 case JUMP_FORWARD:
2029 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002030 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002031
Raymond Hettingerf606f872003-03-16 03:11:04 +00002032 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002033 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002034 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002035 if (w == Py_True) {
2036 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002037 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002038 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002039 if (w == Py_False) {
2040 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002041 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002042 }
2043 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002044 if (err > 0)
2045 err = 0;
2046 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002048 else
2049 break;
2050 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002051
Raymond Hettingerf606f872003-03-16 03:11:04 +00002052 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002053 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002054 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002055 if (w == Py_False) {
2056 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002057 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002058 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002059 if (w == Py_True) {
2060 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002061 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002062 }
2063 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002064 if (err > 0) {
2065 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002066 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002067 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002068 else if (err == 0)
2069 ;
2070 else
2071 break;
2072 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002073
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002074 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002075 case JUMP_ABSOLUTE:
2076 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002077 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002078
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002079 case GET_ITER:
2080 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002081 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002082 x = PyObject_GetIter(v);
2083 Py_DECREF(v);
2084 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002085 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002086 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002087 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002088 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002089 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002090 break;
2091
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002092 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002093 case FOR_ITER:
2094 /* before: [iter]; after: [iter, iter()] *or* [] */
2095 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002096 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002097 if (x != NULL) {
2098 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002099 PREDICT(STORE_FAST);
2100 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002101 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002102 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002103 if (PyErr_Occurred()) {
2104 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2105 break;
2106 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002107 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002108 /* iterator ended normally */
2109 x = v = POP();
2110 Py_DECREF(v);
2111 JUMPBY(oparg);
2112 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002113
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002114 case BREAK_LOOP:
2115 why = WHY_BREAK;
2116 goto fast_block_end;
2117
2118 case CONTINUE_LOOP:
2119 retval = PyInt_FromLong(oparg);
2120 why = WHY_CONTINUE;
2121 goto fast_block_end;
2122
Guido van Rossum374a9221991-04-04 10:40:29 +00002123 case SETUP_LOOP:
2124 case SETUP_EXCEPT:
2125 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002126 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002127 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002128 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002129
Guido van Rossumf10570b1995-07-07 22:53:21 +00002130 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002131 {
2132 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002133 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002134 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002135#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002136 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002137#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002138 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002139#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002140 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002141 PUSH(x);
2142 if (x != NULL)
2143 continue;
2144 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002145 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002146
Jeremy Hylton76901512000-03-28 23:49:17 +00002147 case CALL_FUNCTION_VAR:
2148 case CALL_FUNCTION_KW:
2149 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002150 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002151 int na = oparg & 0xff;
2152 int nk = (oparg>>8) & 0xff;
2153 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002154 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002155 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002156 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002157 if (flags & CALL_FLAG_VAR)
2158 n++;
2159 if (flags & CALL_FLAG_KW)
2160 n++;
2161 pfunc = stack_pointer - n - 1;
2162 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002163
Guido van Rossumac7be682001-01-17 15:42:30 +00002164 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002165 && PyMethod_GET_SELF(func) != NULL) {
2166 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002167 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002168 func = PyMethod_GET_FUNCTION(func);
2169 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002170 Py_DECREF(*pfunc);
2171 *pfunc = self;
2172 na++;
2173 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002174 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002175 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002176 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002177#ifdef WITH_TSC
2178 rdtscll(intr0);
2179#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002180 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002181#ifdef WITH_TSC
2182 rdtscll(intr1);
2183#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002184 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002185 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002186
Jeremy Hylton76901512000-03-28 23:49:17 +00002187 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002188 w = POP();
2189 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002190 }
2191 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002192 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002193 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002194 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002195 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002196
Guido van Rossum681d79a1995-07-18 14:51:37 +00002197 case MAKE_FUNCTION:
2198 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002199 x = PyFunction_New(v, f->f_globals);
2200 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002201 /* XXX Maybe this should be a separate opcode? */
2202 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002203 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002204 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002205 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002206 x = NULL;
2207 break;
2208 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002209 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002210 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002211 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002212 }
2213 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002214 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002215 }
2216 PUSH(x);
2217 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002218
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002219 case MAKE_CLOSURE:
2220 {
2221 int nfree;
2222 v = POP(); /* code object */
2223 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002224 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002225 Py_DECREF(v);
2226 /* XXX Maybe this should be a separate opcode? */
2227 if (x != NULL && nfree > 0) {
2228 v = PyTuple_New(nfree);
2229 if (v == NULL) {
2230 Py_DECREF(x);
2231 x = NULL;
2232 break;
2233 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002234 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002235 w = POP();
2236 PyTuple_SET_ITEM(v, nfree, w);
2237 }
2238 err = PyFunction_SetClosure(x, v);
2239 Py_DECREF(v);
2240 }
2241 if (x != NULL && oparg > 0) {
2242 v = PyTuple_New(oparg);
2243 if (v == NULL) {
2244 Py_DECREF(x);
2245 x = NULL;
2246 break;
2247 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002248 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002249 w = POP();
2250 PyTuple_SET_ITEM(v, oparg, w);
2251 }
2252 err = PyFunction_SetDefaults(x, v);
2253 Py_DECREF(v);
2254 }
2255 PUSH(x);
2256 break;
2257 }
2258
Guido van Rossum8861b741996-07-30 16:49:37 +00002259 case BUILD_SLICE:
2260 if (oparg == 3)
2261 w = POP();
2262 else
2263 w = NULL;
2264 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002265 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002266 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002267 Py_DECREF(u);
2268 Py_DECREF(v);
2269 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002270 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002271 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002272 break;
2273
Fred Drakeef8ace32000-08-24 00:32:09 +00002274 case EXTENDED_ARG:
2275 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002276 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002277 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002278
Guido van Rossum374a9221991-04-04 10:40:29 +00002279 default:
2280 fprintf(stderr,
2281 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002282 PyCode_Addr2Line(f->f_code, f->f_lasti),
2283 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002284 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002285 why = WHY_EXCEPTION;
2286 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002287
2288#ifdef CASE_TOO_BIG
2289 }
2290#endif
2291
Guido van Rossum374a9221991-04-04 10:40:29 +00002292 } /* switch */
2293
2294 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002296#ifdef WITH_TSC
2297 rdtscll(inst1);
2298#endif
2299
Guido van Rossum374a9221991-04-04 10:40:29 +00002300 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Guido van Rossum374a9221991-04-04 10:40:29 +00002302 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002303 if (err == 0 && x != NULL) {
2304#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002305 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002306 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002307 fprintf(stderr,
2308 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002309 else {
2310#endif
2311#ifdef WITH_TSC
2312 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002313#endif
2314 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002315#ifdef CHECKEXC
2316 }
2317#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002318 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002319 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002320 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002321 err = 0;
2322 }
2323
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002326 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002327 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002328 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002329 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002330 why = WHY_EXCEPTION;
2331 }
2332 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002333#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002334 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002335 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002336 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002337 char buf[1024];
2338 sprintf(buf, "Stack unwind with exception "
2339 "set and why=%d", why);
2340 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002341 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002342 }
2343#endif
2344
2345 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002346
Guido van Rossum374a9221991-04-04 10:40:29 +00002347 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002348 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002349
Fred Drake8f51f542001-10-04 14:48:42 +00002350 if (tstate->c_tracefunc != NULL)
2351 call_exc_trace(tstate->c_tracefunc,
2352 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002353 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002354
Guido van Rossum374a9221991-04-04 10:40:29 +00002355 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002356
Guido van Rossum374a9221991-04-04 10:40:29 +00002357 if (why == WHY_RERAISE)
2358 why = WHY_EXCEPTION;
2359
2360 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002361
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002362fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002363 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002364 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002365
Tim Peters8a5c3c72004-04-05 19:36:21 +00002366 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002367 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2368 /* For a continue inside a try block,
2369 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002370 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2371 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002372 why = WHY_NOT;
2373 JUMPTO(PyInt_AS_LONG(retval));
2374 Py_DECREF(retval);
2375 break;
2376 }
2377
Guido van Rossum374a9221991-04-04 10:40:29 +00002378 while (STACK_LEVEL() > b->b_level) {
2379 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002380 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002381 }
2382 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2383 why = WHY_NOT;
2384 JUMPTO(b->b_handler);
2385 break;
2386 }
2387 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002388 (b->b_type == SETUP_EXCEPT &&
2389 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002390 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002391 PyObject *exc, *val, *tb;
2392 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002393 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002394 val = Py_None;
2395 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002396 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002397 /* Make the raw exception data
2398 available to the handler,
2399 so a program can emulate the
2400 Python main loop. Don't do
2401 this for 'finally'. */
2402 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002403 PyErr_NormalizeException(
2404 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002405 set_exc_info(tstate,
2406 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002407 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002408 if (tb == NULL) {
2409 Py_INCREF(Py_None);
2410 PUSH(Py_None);
2411 } else
2412 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002413 PUSH(val);
2414 PUSH(exc);
2415 }
2416 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002417 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002418 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002419 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002420 PUSH(v);
2421 }
2422 why = WHY_NOT;
2423 JUMPTO(b->b_handler);
2424 break;
2425 }
2426 } /* unwind stack */
2427
2428 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002429
Guido van Rossum374a9221991-04-04 10:40:29 +00002430 if (why != WHY_NOT)
2431 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002432#ifdef WITH_TSC
2433 rdtscll(loop1);
2434#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002435
Guido van Rossum374a9221991-04-04 10:40:29 +00002436 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002437
Tim Peters8a5c3c72004-04-05 19:36:21 +00002438 assert(why != WHY_YIELD);
2439 /* Pop remaining stack entries. */
2440 while (!EMPTY()) {
2441 v = POP();
2442 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002443 }
2444
Tim Peters8a5c3c72004-04-05 19:36:21 +00002445 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002446 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002447
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002448fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002449 if (tstate->use_tracing) {
2450 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002451 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002452 if (call_trace(tstate->c_tracefunc,
2453 tstate->c_traceobj, f,
2454 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002455 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002456 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002457 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002458 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002459 }
Fred Drake8f51f542001-10-04 14:48:42 +00002460 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002461 if (why == WHY_EXCEPTION)
2462 call_trace_protected(tstate->c_profilefunc,
2463 tstate->c_profileobj, f,
2464 PyTrace_RETURN);
2465 else if (call_trace(tstate->c_profilefunc,
2466 tstate->c_profileobj, f,
2467 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002468 Py_XDECREF(retval);
2469 retval = NULL;
2470 why = WHY_EXCEPTION;
2471 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002472 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002473 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002474
Guido van Rossuma027efa1997-05-05 20:56:21 +00002475 reset_exc_info(tstate);
2476
Tim Peters5ca576e2001-06-18 22:08:13 +00002477 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002478 exit_eval_frame:
2479 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002480 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002481
Guido van Rossum96a42c81992-01-12 02:29:51 +00002482 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002483}
2484
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002485/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002486 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2487 the test in the if statement in Misc/gdbinit:ppystack */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002488
Tim Peters6d6c1a32001-08-02 04:15:00 +00002489PyObject *
2490PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002491 PyObject **args, int argcount, PyObject **kws, int kwcount,
2492 PyObject **defs, int defcount, PyObject *closure)
2493{
2494 register PyFrameObject *f;
2495 register PyObject *retval = NULL;
2496 register PyObject **fastlocals, **freevars;
2497 PyThreadState *tstate = PyThreadState_GET();
2498 PyObject *x, *u;
2499
2500 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002501 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002502 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002503 return NULL;
2504 }
2505
Jeremy Hylton985eba52003-02-05 23:13:00 +00002506 assert(globals != NULL);
2507 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002508 if (f == NULL)
2509 return NULL;
2510
2511 fastlocals = f->f_localsplus;
2512 freevars = f->f_localsplus + f->f_nlocals;
2513
2514 if (co->co_argcount > 0 ||
2515 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2516 int i;
2517 int n = argcount;
2518 PyObject *kwdict = NULL;
2519 if (co->co_flags & CO_VARKEYWORDS) {
2520 kwdict = PyDict_New();
2521 if (kwdict == NULL)
2522 goto fail;
2523 i = co->co_argcount;
2524 if (co->co_flags & CO_VARARGS)
2525 i++;
2526 SETLOCAL(i, kwdict);
2527 }
2528 if (argcount > co->co_argcount) {
2529 if (!(co->co_flags & CO_VARARGS)) {
2530 PyErr_Format(PyExc_TypeError,
2531 "%.200s() takes %s %d "
2532 "%sargument%s (%d given)",
2533 PyString_AsString(co->co_name),
2534 defcount ? "at most" : "exactly",
2535 co->co_argcount,
2536 kwcount ? "non-keyword " : "",
2537 co->co_argcount == 1 ? "" : "s",
2538 argcount);
2539 goto fail;
2540 }
2541 n = co->co_argcount;
2542 }
2543 for (i = 0; i < n; i++) {
2544 x = args[i];
2545 Py_INCREF(x);
2546 SETLOCAL(i, x);
2547 }
2548 if (co->co_flags & CO_VARARGS) {
2549 u = PyTuple_New(argcount - n);
2550 if (u == NULL)
2551 goto fail;
2552 SETLOCAL(co->co_argcount, u);
2553 for (i = n; i < argcount; i++) {
2554 x = args[i];
2555 Py_INCREF(x);
2556 PyTuple_SET_ITEM(u, i-n, x);
2557 }
2558 }
2559 for (i = 0; i < kwcount; i++) {
2560 PyObject *keyword = kws[2*i];
2561 PyObject *value = kws[2*i + 1];
2562 int j;
2563 if (keyword == NULL || !PyString_Check(keyword)) {
2564 PyErr_Format(PyExc_TypeError,
2565 "%.200s() keywords must be strings",
2566 PyString_AsString(co->co_name));
2567 goto fail;
2568 }
2569 /* XXX slow -- speed up using dictionary? */
2570 for (j = 0; j < co->co_argcount; j++) {
2571 PyObject *nm = PyTuple_GET_ITEM(
2572 co->co_varnames, j);
2573 int cmp = PyObject_RichCompareBool(
2574 keyword, nm, Py_EQ);
2575 if (cmp > 0)
2576 break;
2577 else if (cmp < 0)
2578 goto fail;
2579 }
2580 /* Check errors from Compare */
2581 if (PyErr_Occurred())
2582 goto fail;
2583 if (j >= co->co_argcount) {
2584 if (kwdict == NULL) {
2585 PyErr_Format(PyExc_TypeError,
2586 "%.200s() got an unexpected "
2587 "keyword argument '%.400s'",
2588 PyString_AsString(co->co_name),
2589 PyString_AsString(keyword));
2590 goto fail;
2591 }
2592 PyDict_SetItem(kwdict, keyword, value);
2593 }
2594 else {
2595 if (GETLOCAL(j) != NULL) {
2596 PyErr_Format(PyExc_TypeError,
2597 "%.200s() got multiple "
2598 "values for keyword "
2599 "argument '%.400s'",
2600 PyString_AsString(co->co_name),
2601 PyString_AsString(keyword));
2602 goto fail;
2603 }
2604 Py_INCREF(value);
2605 SETLOCAL(j, value);
2606 }
2607 }
2608 if (argcount < co->co_argcount) {
2609 int m = co->co_argcount - defcount;
2610 for (i = argcount; i < m; i++) {
2611 if (GETLOCAL(i) == NULL) {
2612 PyErr_Format(PyExc_TypeError,
2613 "%.200s() takes %s %d "
2614 "%sargument%s (%d given)",
2615 PyString_AsString(co->co_name),
2616 ((co->co_flags & CO_VARARGS) ||
2617 defcount) ? "at least"
2618 : "exactly",
2619 m, kwcount ? "non-keyword " : "",
2620 m == 1 ? "" : "s", i);
2621 goto fail;
2622 }
2623 }
2624 if (n > m)
2625 i = n - m;
2626 else
2627 i = 0;
2628 for (; i < defcount; i++) {
2629 if (GETLOCAL(m+i) == NULL) {
2630 PyObject *def = defs[i];
2631 Py_INCREF(def);
2632 SETLOCAL(m+i, def);
2633 }
2634 }
2635 }
2636 }
2637 else {
2638 if (argcount > 0 || kwcount > 0) {
2639 PyErr_Format(PyExc_TypeError,
2640 "%.200s() takes no arguments (%d given)",
2641 PyString_AsString(co->co_name),
2642 argcount + kwcount);
2643 goto fail;
2644 }
2645 }
2646 /* Allocate and initialize storage for cell vars, and copy free
2647 vars into frame. This isn't too efficient right now. */
2648 if (f->f_ncells) {
2649 int i = 0, j = 0, nargs, found;
2650 char *cellname, *argname;
2651 PyObject *c;
2652
2653 nargs = co->co_argcount;
2654 if (co->co_flags & CO_VARARGS)
2655 nargs++;
2656 if (co->co_flags & CO_VARKEYWORDS)
2657 nargs++;
2658
2659 /* Check for cells that shadow args */
2660 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2661 cellname = PyString_AS_STRING(
2662 PyTuple_GET_ITEM(co->co_cellvars, i));
2663 found = 0;
2664 while (j < nargs) {
2665 argname = PyString_AS_STRING(
2666 PyTuple_GET_ITEM(co->co_varnames, j));
2667 if (strcmp(cellname, argname) == 0) {
2668 c = PyCell_New(GETLOCAL(j));
2669 if (c == NULL)
2670 goto fail;
2671 GETLOCAL(f->f_nlocals + i) = c;
2672 found = 1;
2673 break;
2674 }
2675 j++;
2676 }
2677 if (found == 0) {
2678 c = PyCell_New(NULL);
2679 if (c == NULL)
2680 goto fail;
2681 SETLOCAL(f->f_nlocals + i, c);
2682 }
2683 }
2684 /* Initialize any that are left */
2685 while (i < f->f_ncells) {
2686 c = PyCell_New(NULL);
2687 if (c == NULL)
2688 goto fail;
2689 SETLOCAL(f->f_nlocals + i, c);
2690 i++;
2691 }
2692 }
2693 if (f->f_nfreevars) {
2694 int i;
2695 for (i = 0; i < f->f_nfreevars; ++i) {
2696 PyObject *o = PyTuple_GET_ITEM(closure, i);
2697 Py_INCREF(o);
2698 freevars[f->f_ncells + i] = o;
2699 }
2700 }
2701
Tim Peters5ca576e2001-06-18 22:08:13 +00002702 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002703 /* Don't need to keep the reference to f_back, it will be set
2704 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002705 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002706 f->f_back = NULL;
2707
Jeremy Hylton985eba52003-02-05 23:13:00 +00002708 PCALL(PCALL_GENERATOR);
2709
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002710 /* Create a new generator that owns the ready to run frame
2711 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002712 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002713 }
2714
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002715 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002716
2717 fail: /* Jump here from prelude on failure */
2718
Tim Petersb13680b2001-11-27 23:29:29 +00002719 /* decref'ing the frame can cause __del__ methods to get invoked,
2720 which can call back into Python. While we're done with the
2721 current Python frame (f), the associated C stack is still in use,
2722 so recursion_depth must be boosted for the duration.
2723 */
2724 assert(tstate != NULL);
2725 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002726 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002727 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002728 return retval;
2729}
2730
2731
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002732/* Implementation notes for set_exc_info() and reset_exc_info():
2733
2734- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2735 'exc_traceback'. These always travel together.
2736
2737- tstate->curexc_ZZZ is the "hot" exception that is set by
2738 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2739
2740- Once an exception is caught by an except clause, it is transferred
2741 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2742 can pick it up. This is the primary task of set_exc_info().
2743
2744- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2745
2746 Long ago, when none of this existed, there were just a few globals:
2747 one set corresponding to the "hot" exception, and one set
2748 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2749 globals; they were simply stored as sys.exc_ZZZ. For backwards
2750 compatibility, they still are!) The problem was that in code like
2751 this:
2752
2753 try:
2754 "something that may fail"
2755 except "some exception":
2756 "do something else first"
2757 "print the exception from sys.exc_ZZZ."
2758
2759 if "do something else first" invoked something that raised and caught
2760 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2761 cause of subtle bugs. I fixed this by changing the semantics as
2762 follows:
2763
2764 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2765 *in that frame*.
2766
2767 - But initially, and as long as no exception is caught in a given
2768 frame, sys.exc_ZZZ will hold the last exception caught in the
2769 previous frame (or the frame before that, etc.).
2770
2771 The first bullet fixed the bug in the above example. The second
2772 bullet was for backwards compatibility: it was (and is) common to
2773 have a function that is called when an exception is caught, and to
2774 have that function access the caught exception via sys.exc_ZZZ.
2775 (Example: traceback.print_exc()).
2776
2777 At the same time I fixed the problem that sys.exc_ZZZ weren't
2778 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2779 but that's really a separate improvement.
2780
2781 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2782 variables to what they were before the current frame was called. The
2783 set_exc_info() function saves them on the frame so that
2784 reset_exc_info() can restore them. The invariant is that
2785 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2786 exception (where "catching" an exception applies only to successful
2787 except clauses); and if the current frame ever caught an exception,
2788 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2789 at the start of the current frame.
2790
2791*/
2792
Guido van Rossuma027efa1997-05-05 20:56:21 +00002793static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002794set_exc_info(PyThreadState *tstate,
2795 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002796{
2797 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002798 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002799
Guido van Rossuma027efa1997-05-05 20:56:21 +00002800 frame = tstate->frame;
2801 if (frame->f_exc_type == NULL) {
2802 /* This frame didn't catch an exception before */
2803 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002804 if (tstate->exc_type == NULL) {
2805 Py_INCREF(Py_None);
2806 tstate->exc_type = Py_None;
2807 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002808 tmp_type = frame->f_exc_type;
2809 tmp_value = frame->f_exc_value;
2810 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002811 Py_XINCREF(tstate->exc_type);
2812 Py_XINCREF(tstate->exc_value);
2813 Py_XINCREF(tstate->exc_traceback);
2814 frame->f_exc_type = tstate->exc_type;
2815 frame->f_exc_value = tstate->exc_value;
2816 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002817 Py_XDECREF(tmp_type);
2818 Py_XDECREF(tmp_value);
2819 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002820 }
2821 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002822 tmp_type = tstate->exc_type;
2823 tmp_value = tstate->exc_value;
2824 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002825 Py_XINCREF(type);
2826 Py_XINCREF(value);
2827 Py_XINCREF(tb);
2828 tstate->exc_type = type;
2829 tstate->exc_value = value;
2830 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002831 Py_XDECREF(tmp_type);
2832 Py_XDECREF(tmp_value);
2833 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002834 /* For b/w compatibility */
2835 PySys_SetObject("exc_type", type);
2836 PySys_SetObject("exc_value", value);
2837 PySys_SetObject("exc_traceback", tb);
2838}
2839
2840static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002841reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002842{
2843 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002844 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002845 frame = tstate->frame;
2846 if (frame->f_exc_type != NULL) {
2847 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002848 tmp_type = tstate->exc_type;
2849 tmp_value = tstate->exc_value;
2850 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002851 Py_XINCREF(frame->f_exc_type);
2852 Py_XINCREF(frame->f_exc_value);
2853 Py_XINCREF(frame->f_exc_traceback);
2854 tstate->exc_type = frame->f_exc_type;
2855 tstate->exc_value = frame->f_exc_value;
2856 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002857 Py_XDECREF(tmp_type);
2858 Py_XDECREF(tmp_value);
2859 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002860 /* For b/w compatibility */
2861 PySys_SetObject("exc_type", frame->f_exc_type);
2862 PySys_SetObject("exc_value", frame->f_exc_value);
2863 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2864 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002865 tmp_type = frame->f_exc_type;
2866 tmp_value = frame->f_exc_value;
2867 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002868 frame->f_exc_type = NULL;
2869 frame->f_exc_value = NULL;
2870 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002871 Py_XDECREF(tmp_type);
2872 Py_XDECREF(tmp_value);
2873 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002874}
2875
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002876/* Logic for the raise statement (too complicated for inlining).
2877 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002878static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002879do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002880{
Guido van Rossumd295f121998-04-09 21:39:57 +00002881 if (type == NULL) {
2882 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002883 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002884 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2885 value = tstate->exc_value;
2886 tb = tstate->exc_traceback;
2887 Py_XINCREF(type);
2888 Py_XINCREF(value);
2889 Py_XINCREF(tb);
2890 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002891
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002892 /* We support the following forms of raise:
2893 raise <class>, <classinstance>
2894 raise <class>, <argument tuple>
2895 raise <class>, None
2896 raise <class>, <argument>
2897 raise <classinstance>, None
2898 raise <string>, <object>
2899 raise <string>, None
2900
2901 An omitted second argument is the same as None.
2902
2903 In addition, raise <tuple>, <anything> is the same as
2904 raising the tuple's first item (and it better have one!);
2905 this rule is applied recursively.
2906
2907 Finally, an optional third argument can be supplied, which
2908 gives the traceback to be substituted (useful when
2909 re-raising an exception after examining it). */
2910
2911 /* First, check the traceback argument, replacing None with
2912 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002913 if (tb == Py_None) {
2914 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002915 tb = NULL;
2916 }
2917 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002918 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002919 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002920 goto raise_error;
2921 }
2922
2923 /* Next, replace a missing value with None */
2924 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002925 value = Py_None;
2926 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002927 }
2928
2929 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002930 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2931 PyObject *tmp = type;
2932 type = PyTuple_GET_ITEM(type, 0);
2933 Py_INCREF(type);
2934 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002935 }
2936
Tim Petersafb2c802002-04-18 18:06:20 +00002937 if (PyString_CheckExact(type))
2938 /* Raising builtin string is deprecated but still allowed --
2939 * do nothing. Raising an instance of a new-style str
2940 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002941 PyErr_Warn(PyExc_PendingDeprecationWarning,
2942 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002943
2944 else if (PyClass_Check(type))
2945 PyErr_NormalizeException(&type, &value, &tb);
2946
Guido van Rossumb209a111997-04-29 18:18:01 +00002947 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002948 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002949 if (value != Py_None) {
2950 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002951 "instance exception may not have a separate value");
2952 goto raise_error;
2953 }
2954 else {
2955 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002956 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002957 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002958 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2959 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002960 }
2961 }
2962 else {
2963 /* Not something you can raise. You get an exception
2964 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002965 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002966 "exceptions must be classes, instances, or "
2967 "strings (deprecated), not %s",
2968 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002969 goto raise_error;
2970 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002971 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002972 if (tb == NULL)
2973 return WHY_EXCEPTION;
2974 else
2975 return WHY_RERAISE;
2976 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002977 Py_XDECREF(value);
2978 Py_XDECREF(type);
2979 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002980 return WHY_EXCEPTION;
2981}
2982
Tim Petersd6d010b2001-06-21 02:49:55 +00002983/* Iterate v argcnt times and store the results on the stack (via decreasing
2984 sp). Return 1 for success, 0 if error. */
2985
Barry Warsawe42b18f1997-08-25 22:13:04 +00002986static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002987unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002988{
Tim Petersd6d010b2001-06-21 02:49:55 +00002989 int i = 0;
2990 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002991 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002992
Tim Petersd6d010b2001-06-21 02:49:55 +00002993 assert(v != NULL);
2994
2995 it = PyObject_GetIter(v);
2996 if (it == NULL)
2997 goto Error;
2998
2999 for (; i < argcnt; i++) {
3000 w = PyIter_Next(it);
3001 if (w == NULL) {
3002 /* Iterator done, via error or exhaustion. */
3003 if (!PyErr_Occurred()) {
3004 PyErr_Format(PyExc_ValueError,
3005 "need more than %d value%s to unpack",
3006 i, i == 1 ? "" : "s");
3007 }
3008 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003009 }
3010 *--sp = w;
3011 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003012
3013 /* We better have exhausted the iterator now. */
3014 w = PyIter_Next(it);
3015 if (w == NULL) {
3016 if (PyErr_Occurred())
3017 goto Error;
3018 Py_DECREF(it);
3019 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003020 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003021 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003022 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003023 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003024Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003025 for (; i > 0; i--, sp++)
3026 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003027 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003028 return 0;
3029}
3030
3031
Guido van Rossum96a42c81992-01-12 02:29:51 +00003032#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003033static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003034prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003035{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003036 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003037 if (PyObject_Print(v, stdout, 0) != 0)
3038 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003039 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003040 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003041}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003042#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003043
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003044static void
Fred Drake5755ce62001-06-27 19:19:46 +00003045call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003046{
Guido van Rossumb209a111997-04-29 18:18:01 +00003047 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003048 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003049 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003050 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003051 value = Py_None;
3052 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003053 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003054 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003055 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003056 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003057 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003058 }
Fred Drake5755ce62001-06-27 19:19:46 +00003059 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003060 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003061 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003062 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003063 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003064 Py_XDECREF(type);
3065 Py_XDECREF(value);
3066 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003067 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003068}
3069
Fred Drake4ec5d562001-10-04 19:26:43 +00003070static void
3071call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3072 int what)
3073{
3074 PyObject *type, *value, *traceback;
3075 int err;
3076 PyErr_Fetch(&type, &value, &traceback);
3077 err = call_trace(func, obj, frame, what, NULL);
3078 if (err == 0)
3079 PyErr_Restore(type, value, traceback);
3080 else {
3081 Py_XDECREF(type);
3082 Py_XDECREF(value);
3083 Py_XDECREF(traceback);
3084 }
3085}
3086
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003087static int
Fred Drake5755ce62001-06-27 19:19:46 +00003088call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3089 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003090{
Fred Drake5755ce62001-06-27 19:19:46 +00003091 register PyThreadState *tstate = frame->f_tstate;
3092 int result;
3093 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003094 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003095 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003096 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003097 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003098 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3099 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003100 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003101 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003102}
3103
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003104PyObject *
3105_PyEval_CallTracing(PyObject *func, PyObject *args)
3106{
3107 PyFrameObject *frame = PyEval_GetFrame();
3108 PyThreadState *tstate = frame->f_tstate;
3109 int save_tracing = tstate->tracing;
3110 int save_use_tracing = tstate->use_tracing;
3111 PyObject *result;
3112
3113 tstate->tracing = 0;
3114 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3115 || (tstate->c_profilefunc != NULL));
3116 result = PyObject_Call(func, args, NULL);
3117 tstate->tracing = save_tracing;
3118 tstate->use_tracing = save_use_tracing;
3119 return result;
3120}
3121
Michael W. Hudson006c7522002-11-08 13:08:46 +00003122static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003123maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003124 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3125 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003126{
3127 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003128
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003129 In a nutshell, we use the co_lnotab field of the code object
3130 to tell when execution has moved onto a different line.
3131
3132 As mentioned above, the basic idea is so set things up so
3133 that
3134
3135 *instr_lb <= frame->f_lasti < *instr_ub
3136
3137 is true so long as execution does not change lines.
3138
3139 This is all fairly simple. Digging the information out of
3140 co_lnotab takes some work, but is conceptually clear.
3141
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003142 Somewhat harder to explain is why we don't *always* call the
3143 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003144
3145 Consider this code:
3146
3147 1: def f(a):
3148 2: if a:
3149 3: print 1
3150 4: else:
3151 5: print 2
3152
3153 which compiles to this:
3154
3155 2 0 LOAD_FAST 0 (a)
3156 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003157 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003158
3159 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003160 10 PRINT_ITEM
3161 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003162 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003163 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003164
3165 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003166 19 PRINT_ITEM
3167 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003168 >> 21 LOAD_CONST 0 (None)
3169 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003171 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003172 15 and the co_lnotab will claim that execution has moved to
3173 line 3. This is at best misleading. In this case we could
3174 associate the POP_TOP with line 4, but that doesn't make
3175 sense in all cases (I think).
3176
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003177 What we do is only call the line trace function if the co_lnotab
3178 indicates we have jumped to the *start* of a line, i.e. if the
3179 current instruction offset matches the offset given for the
3180 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003181
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003182 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003183 Execution will jump from instruction offset 12 to offset 21.
3184 Then the co_lnotab would imply that execution has moved to line
3185 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003186
3187 Why do we set f_lineno when tracing? Well, consider the code
3188 above when 'a' is true. If stepping through this with 'n' in
3189 pdb, you would stop at line 1 with a "call" type event, then
3190 line events on lines 2 and 3, then a "return" type event -- but
3191 you would be shown line 5 during this event. This is a change
3192 from the behaviour in 2.2 and before, and I've found it
3193 confusing in practice. By setting and using f_lineno when
3194 tracing, one can report a line number different from that
3195 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003196 */
3197
Michael W. Hudson006c7522002-11-08 13:08:46 +00003198 int result = 0;
3199
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003200 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003201 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003202 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003203 unsigned char* p;
3204
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003205 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3206 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003207
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003208 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003209 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003210
3211 /* possible optimization: if f->f_lasti == instr_ub
3212 (likely to be a common case) then we already know
3213 instr_lb -- if we stored the matching value of p
3214 somwhere we could skip the first while loop. */
3215
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003216 /* see comments in compile.c for the description of
3217 co_lnotab. A point to remember: increments to p
3218 should come in pairs -- although we don't care about
3219 the line increments here, treating them as byte
3220 increments gets confusing, to say the least. */
3221
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003222 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003223 if (addr + *p > frame->f_lasti)
3224 break;
3225 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003226 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003227 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003228 --size;
3229 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003230
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003231 if (addr == frame->f_lasti) {
3232 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003233 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003234 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003235 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003236
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003237 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003238 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003239 addr += *p++;
3240 if (*p++)
3241 break;
3242 }
3243 *instr_ub = addr;
3244 }
3245 else {
3246 *instr_ub = INT_MAX;
3247 }
3248 }
Armin Rigobf57a142004-03-22 19:24:58 +00003249 else if (frame->f_lasti <= *instr_prev) {
3250 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003251 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003252 PyTrace_LINE, Py_None);
3253 }
3254 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003255 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003256}
3257
Fred Drake5755ce62001-06-27 19:19:46 +00003258void
3259PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003260{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003261 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003262 PyObject *temp = tstate->c_profileobj;
3263 Py_XINCREF(arg);
3264 tstate->c_profilefunc = NULL;
3265 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003266 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003267 Py_XDECREF(temp);
3268 tstate->c_profilefunc = func;
3269 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003270 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003271}
3272
3273void
3274PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3275{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003276 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003277 PyObject *temp = tstate->c_traceobj;
3278 Py_XINCREF(arg);
3279 tstate->c_tracefunc = NULL;
3280 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003281 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003282 Py_XDECREF(temp);
3283 tstate->c_tracefunc = func;
3284 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003285 tstate->use_tracing = ((func != NULL)
3286 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003287}
3288
Guido van Rossumb209a111997-04-29 18:18:01 +00003289PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003290PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003291{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003292 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003293 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003294 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003295 else
3296 return current_frame->f_builtins;
3297}
3298
Guido van Rossumb209a111997-04-29 18:18:01 +00003299PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003300PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003301{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003302 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003303 if (current_frame == NULL)
3304 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003305 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003306 return current_frame->f_locals;
3307}
3308
Guido van Rossumb209a111997-04-29 18:18:01 +00003309PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003310PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003311{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003312 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003313 if (current_frame == NULL)
3314 return NULL;
3315 else
3316 return current_frame->f_globals;
3317}
3318
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003319PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003320PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003321{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003322 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003323 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003324}
3325
Guido van Rossum6135a871995-01-09 17:53:26 +00003326int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003327PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003328{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003329 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003330 return current_frame == NULL ? 0 : current_frame->f_restricted;
3331}
3332
Guido van Rossumbe270261997-05-22 22:26:18 +00003333int
Tim Peters5ba58662001-07-16 02:29:45 +00003334PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003335{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003336 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003337 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003338
3339 if (current_frame != NULL) {
3340 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003341 const int compilerflags = codeflags & PyCF_MASK;
3342 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003343 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003344 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003345 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003346#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003347 if (codeflags & CO_GENERATOR_ALLOWED) {
3348 result = 1;
3349 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3350 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003351#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003352 }
3353 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003354}
3355
3356int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003357Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003358{
Guido van Rossumb209a111997-04-29 18:18:01 +00003359 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003360 if (f == NULL)
3361 return 0;
3362 if (!PyFile_SoftSpace(f, 0))
3363 return 0;
3364 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003365}
3366
Guido van Rossum3f5da241990-12-20 15:06:42 +00003367
Guido van Rossum681d79a1995-07-18 14:51:37 +00003368/* External interface to call any callable object.
3369 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003370
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003371#undef PyEval_CallObject
3372/* for backward compatibility: export this interface */
3373
Guido van Rossumb209a111997-04-29 18:18:01 +00003374PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003375PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003376{
Guido van Rossumb209a111997-04-29 18:18:01 +00003377 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003378}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003379#define PyEval_CallObject(func,arg) \
3380 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003381
Guido van Rossumb209a111997-04-29 18:18:01 +00003382PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003383PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003384{
Jeremy Hylton52820442001-01-03 23:52:36 +00003385 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003386
3387 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003388 arg = PyTuple_New(0);
3389 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003390 PyErr_SetString(PyExc_TypeError,
3391 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003392 return NULL;
3393 }
3394 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003395 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003396
Guido van Rossumb209a111997-04-29 18:18:01 +00003397 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003398 PyErr_SetString(PyExc_TypeError,
3399 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003400 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003401 return NULL;
3402 }
3403
Tim Peters6d6c1a32001-08-02 04:15:00 +00003404 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003405 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003406 return result;
3407}
3408
Tim Peters6d6c1a32001-08-02 04:15:00 +00003409char *
3410PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003411{
3412 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003413 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003414 else if (PyFunction_Check(func))
3415 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3416 else if (PyCFunction_Check(func))
3417 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3418 else if (PyClass_Check(func))
3419 return PyString_AsString(((PyClassObject*)func)->cl_name);
3420 else if (PyInstance_Check(func)) {
3421 return PyString_AsString(
3422 ((PyInstanceObject*)func)->in_class->cl_name);
3423 } else {
3424 return func->ob_type->tp_name;
3425 }
3426}
3427
Tim Peters6d6c1a32001-08-02 04:15:00 +00003428char *
3429PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003430{
3431 if (PyMethod_Check(func))
3432 return "()";
3433 else if (PyFunction_Check(func))
3434 return "()";
3435 else if (PyCFunction_Check(func))
3436 return "()";
3437 else if (PyClass_Check(func))
3438 return " constructor";
3439 else if (PyInstance_Check(func)) {
3440 return " instance";
3441 } else {
3442 return " object";
3443 }
3444}
3445
Jeremy Hylton52820442001-01-03 23:52:36 +00003446#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3447
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003448static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003449err_args(PyObject *func, int flags, int nargs)
3450{
3451 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003452 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003453 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003454 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003455 nargs);
3456 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003457 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003458 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003459 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003460 nargs);
3461}
3462
Nicholas Bastind858a772004-06-25 23:31:06 +00003463#define C_TRACE(call) \
3464if (tstate->use_tracing && tstate->c_profilefunc) { \
3465 if (call_trace(tstate->c_profilefunc, \
3466 tstate->c_profileobj, \
3467 tstate->frame, PyTrace_C_CALL, \
3468 func)) \
3469 { return NULL; } \
3470 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003471 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003472 if (x == NULL) { \
3473 if (call_trace (tstate->c_profilefunc, \
3474 tstate->c_profileobj, \
3475 tstate->frame, PyTrace_C_EXCEPTION, \
3476 func)) \
3477 { return NULL; } \
3478 } else { \
3479 if (call_trace(tstate->c_profilefunc, \
3480 tstate->c_profileobj, \
3481 tstate->frame, PyTrace_C_RETURN, \
3482 func)) \
3483 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003484 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003485 } \
3486} else { \
3487 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003488 }
3489
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003490static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003491call_function(PyObject ***pp_stack, int oparg
3492#ifdef WITH_TSC
3493 , uint64* pintr0, uint64* pintr1
3494#endif
3495 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003496{
3497 int na = oparg & 0xff;
3498 int nk = (oparg>>8) & 0xff;
3499 int n = na + 2 * nk;
3500 PyObject **pfunc = (*pp_stack) - n - 1;
3501 PyObject *func = *pfunc;
3502 PyObject *x, *w;
3503
Jeremy Hylton985eba52003-02-05 23:13:00 +00003504 /* Always dispatch PyCFunction first, because these are
3505 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003506 */
3507 if (PyCFunction_Check(func) && nk == 0) {
3508 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003509 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003510
3511 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003512 if (flags & (METH_NOARGS | METH_O)) {
3513 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3514 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003515 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003516 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003517 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003518 else if (flags & METH_O && na == 1) {
3519 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003520 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003521 Py_DECREF(arg);
3522 }
3523 else {
3524 err_args(func, flags, na);
3525 x = NULL;
3526 }
3527 }
3528 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003529 PyObject *callargs;
3530 callargs = load_args(pp_stack, na);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003531#ifdef WITH_TSC
3532 rdtscll(*pintr0);
3533#endif
Nicholas Bastind858a772004-06-25 23:31:06 +00003534 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003535#ifdef WITH_TSC
3536 rdtscll(*pintr1);
3537#endif
Tim Peters8a5c3c72004-04-05 19:36:21 +00003538 Py_XDECREF(callargs);
3539 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003540 } else {
3541 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3542 /* optimize access to bound methods */
3543 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003544 PCALL(PCALL_METHOD);
3545 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003546 Py_INCREF(self);
3547 func = PyMethod_GET_FUNCTION(func);
3548 Py_INCREF(func);
3549 Py_DECREF(*pfunc);
3550 *pfunc = self;
3551 na++;
3552 n++;
3553 } else
3554 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003555#ifdef WITH_TSC
3556 rdtscll(*pintr0);
3557#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003558 if (PyFunction_Check(func))
3559 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003560 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003561 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003562#ifdef WITH_TSC
3563 rdtscll(*pintr1);
3564#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003565 Py_DECREF(func);
3566 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003567
Jeremy Hylton985eba52003-02-05 23:13:00 +00003568 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003569 while ((*pp_stack) > pfunc) {
3570 w = EXT_POP(*pp_stack);
3571 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003572 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003573 }
3574 return x;
3575}
3576
Jeremy Hylton192690e2002-08-16 18:36:11 +00003577/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003578 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003579 For the simplest case -- a function that takes only positional
3580 arguments and is called with only positional arguments -- it
3581 inlines the most primitive frame setup code from
3582 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3583 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003584*/
3585
3586static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003587fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003588{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003589 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003590 PyObject *globals = PyFunction_GET_GLOBALS(func);
3591 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3592 PyObject **d = NULL;
3593 int nd = 0;
3594
Jeremy Hylton985eba52003-02-05 23:13:00 +00003595 PCALL(PCALL_FUNCTION);
3596 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003597 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003598 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3599 PyFrameObject *f;
3600 PyObject *retval = NULL;
3601 PyThreadState *tstate = PyThreadState_GET();
3602 PyObject **fastlocals, **stack;
3603 int i;
3604
3605 PCALL(PCALL_FASTER_FUNCTION);
3606 assert(globals != NULL);
3607 /* XXX Perhaps we should create a specialized
3608 PyFrame_New() that doesn't take locals, but does
3609 take builtins without sanity checking them.
3610 */
3611 f = PyFrame_New(tstate, co, globals, NULL);
3612 if (f == NULL)
3613 return NULL;
3614
3615 fastlocals = f->f_localsplus;
3616 stack = (*pp_stack) - n;
3617
3618 for (i = 0; i < n; i++) {
3619 Py_INCREF(*stack);
3620 fastlocals[i] = *stack++;
3621 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003622 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003623 assert(tstate != NULL);
3624 ++tstate->recursion_depth;
3625 Py_DECREF(f);
3626 --tstate->recursion_depth;
3627 return retval;
3628 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003629 if (argdefs != NULL) {
3630 d = &PyTuple_GET_ITEM(argdefs, 0);
3631 nd = ((PyTupleObject *)argdefs)->ob_size;
3632 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003633 return PyEval_EvalCodeEx(co, globals,
3634 (PyObject *)NULL, (*pp_stack)-n, na,
3635 (*pp_stack)-2*nk, nk, d, nd,
3636 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003637}
3638
3639static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003640update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3641 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003642{
3643 PyObject *kwdict = NULL;
3644 if (orig_kwdict == NULL)
3645 kwdict = PyDict_New();
3646 else {
3647 kwdict = PyDict_Copy(orig_kwdict);
3648 Py_DECREF(orig_kwdict);
3649 }
3650 if (kwdict == NULL)
3651 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003652 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003653 int err;
3654 PyObject *value = EXT_POP(*pp_stack);
3655 PyObject *key = EXT_POP(*pp_stack);
3656 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003657 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003658 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003659 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003660 PyEval_GetFuncName(func),
3661 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003662 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003663 Py_DECREF(key);
3664 Py_DECREF(value);
3665 Py_DECREF(kwdict);
3666 return NULL;
3667 }
3668 err = PyDict_SetItem(kwdict, key, value);
3669 Py_DECREF(key);
3670 Py_DECREF(value);
3671 if (err) {
3672 Py_DECREF(kwdict);
3673 return NULL;
3674 }
3675 }
3676 return kwdict;
3677}
3678
3679static PyObject *
3680update_star_args(int nstack, int nstar, PyObject *stararg,
3681 PyObject ***pp_stack)
3682{
3683 PyObject *callargs, *w;
3684
3685 callargs = PyTuple_New(nstack + nstar);
3686 if (callargs == NULL) {
3687 return NULL;
3688 }
3689 if (nstar) {
3690 int i;
3691 for (i = 0; i < nstar; i++) {
3692 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3693 Py_INCREF(a);
3694 PyTuple_SET_ITEM(callargs, nstack + i, a);
3695 }
3696 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003697 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003698 w = EXT_POP(*pp_stack);
3699 PyTuple_SET_ITEM(callargs, nstack, w);
3700 }
3701 return callargs;
3702}
3703
3704static PyObject *
3705load_args(PyObject ***pp_stack, int na)
3706{
3707 PyObject *args = PyTuple_New(na);
3708 PyObject *w;
3709
3710 if (args == NULL)
3711 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003712 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003713 w = EXT_POP(*pp_stack);
3714 PyTuple_SET_ITEM(args, na, w);
3715 }
3716 return args;
3717}
3718
3719static PyObject *
3720do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3721{
3722 PyObject *callargs = NULL;
3723 PyObject *kwdict = NULL;
3724 PyObject *result = NULL;
3725
3726 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003727 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003728 if (kwdict == NULL)
3729 goto call_fail;
3730 }
3731 callargs = load_args(pp_stack, na);
3732 if (callargs == NULL)
3733 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003734#ifdef CALL_PROFILE
3735 /* At this point, we have to look at the type of func to
3736 update the call stats properly. Do it here so as to avoid
3737 exposing the call stats machinery outside ceval.c
3738 */
3739 if (PyFunction_Check(func))
3740 PCALL(PCALL_FUNCTION);
3741 else if (PyMethod_Check(func))
3742 PCALL(PCALL_METHOD);
3743 else if (PyType_Check(func))
3744 PCALL(PCALL_TYPE);
3745 else
3746 PCALL(PCALL_OTHER);
3747#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003748 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003749 call_fail:
3750 Py_XDECREF(callargs);
3751 Py_XDECREF(kwdict);
3752 return result;
3753}
3754
3755static PyObject *
3756ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3757{
3758 int nstar = 0;
3759 PyObject *callargs = NULL;
3760 PyObject *stararg = NULL;
3761 PyObject *kwdict = NULL;
3762 PyObject *result = NULL;
3763
3764 if (flags & CALL_FLAG_KW) {
3765 kwdict = EXT_POP(*pp_stack);
3766 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003767 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003768 "%s%s argument after ** "
3769 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003770 PyEval_GetFuncName(func),
3771 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003772 goto ext_call_fail;
3773 }
3774 }
3775 if (flags & CALL_FLAG_VAR) {
3776 stararg = EXT_POP(*pp_stack);
3777 if (!PyTuple_Check(stararg)) {
3778 PyObject *t = NULL;
3779 t = PySequence_Tuple(stararg);
3780 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003781 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3782 PyErr_Format(PyExc_TypeError,
3783 "%s%s argument after * "
3784 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003785 PyEval_GetFuncName(func),
3786 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003787 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003788 goto ext_call_fail;
3789 }
3790 Py_DECREF(stararg);
3791 stararg = t;
3792 }
3793 nstar = PyTuple_GET_SIZE(stararg);
3794 }
3795 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003796 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003797 if (kwdict == NULL)
3798 goto ext_call_fail;
3799 }
3800 callargs = update_star_args(na, nstar, stararg, pp_stack);
3801 if (callargs == NULL)
3802 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003803#ifdef CALL_PROFILE
3804 /* At this point, we have to look at the type of func to
3805 update the call stats properly. Do it here so as to avoid
3806 exposing the call stats machinery outside ceval.c
3807 */
3808 if (PyFunction_Check(func))
3809 PCALL(PCALL_FUNCTION);
3810 else if (PyMethod_Check(func))
3811 PCALL(PCALL_METHOD);
3812 else if (PyType_Check(func))
3813 PCALL(PCALL_TYPE);
3814 else
3815 PCALL(PCALL_OTHER);
3816#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003817 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003818 ext_call_fail:
3819 Py_XDECREF(callargs);
3820 Py_XDECREF(kwdict);
3821 Py_XDECREF(stararg);
3822 return result;
3823}
3824
Tim Peterscb479e72001-12-16 19:11:44 +00003825/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3826 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3827 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3828*/
Tim Petersb5196382001-12-16 19:44:20 +00003829/* Note: If v is NULL, return success without storing into *pi. This
3830 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3831 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003832*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003833int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003834_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003835{
Tim Petersb5196382001-12-16 19:44:20 +00003836 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003837 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003838 if (PyInt_Check(v)) {
3839 x = PyInt_AsLong(v);
3840 } else if (PyLong_Check(v)) {
3841 x = PyLong_AsLong(v);
3842 if (x==-1 && PyErr_Occurred()) {
3843 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003844 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003845
Guido van Rossumac7be682001-01-17 15:42:30 +00003846 if (!PyErr_ExceptionMatches(
3847 PyExc_OverflowError)) {
3848 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003849 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003850 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003851 }
3852
Guido van Rossumac7be682001-01-17 15:42:30 +00003853 /* Clear the OverflowError */
3854 PyErr_Clear();
3855
3856 /* It's an overflow error, so we need to
3857 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003858 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003859 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003860
3861 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003862 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003863 if (long_zero == NULL)
3864 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003865
3866 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003867 cmp = PyObject_RichCompareBool(v, long_zero,
3868 Py_GT);
3869 Py_DECREF(long_zero);
3870 if (cmp < 0)
3871 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003872 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003873 x = INT_MAX;
3874 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003875 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003876 }
3877 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003878 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003879 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003880 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003881 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003882 /* Truncate -- very long indices are truncated anyway */
3883 if (x > INT_MAX)
3884 x = INT_MAX;
3885 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003886 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003887 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003888 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003889 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003890}
3891
Guido van Rossum50d756e2001-08-18 17:43:36 +00003892#undef ISINT
3893#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3894
Guido van Rossumb209a111997-04-29 18:18:01 +00003895static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003896apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003897{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003898 PyTypeObject *tp = u->ob_type;
3899 PySequenceMethods *sq = tp->tp_as_sequence;
3900
3901 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3902 int ilow = 0, ihigh = INT_MAX;
3903 if (!_PyEval_SliceIndex(v, &ilow))
3904 return NULL;
3905 if (!_PyEval_SliceIndex(w, &ihigh))
3906 return NULL;
3907 return PySequence_GetSlice(u, ilow, ihigh);
3908 }
3909 else {
3910 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003911 if (slice != NULL) {
3912 PyObject *res = PyObject_GetItem(u, slice);
3913 Py_DECREF(slice);
3914 return res;
3915 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003916 else
3917 return NULL;
3918 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003919}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003920
3921static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003922assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3923 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003924{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003925 PyTypeObject *tp = u->ob_type;
3926 PySequenceMethods *sq = tp->tp_as_sequence;
3927
3928 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3929 int ilow = 0, ihigh = INT_MAX;
3930 if (!_PyEval_SliceIndex(v, &ilow))
3931 return -1;
3932 if (!_PyEval_SliceIndex(w, &ihigh))
3933 return -1;
3934 if (x == NULL)
3935 return PySequence_DelSlice(u, ilow, ihigh);
3936 else
3937 return PySequence_SetSlice(u, ilow, ihigh, x);
3938 }
3939 else {
3940 PyObject *slice = PySlice_New(v, w, NULL);
3941 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003942 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003943 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003944 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003945 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003946 res = PyObject_DelItem(u, slice);
3947 Py_DECREF(slice);
3948 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003949 }
3950 else
3951 return -1;
3952 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003953}
3954
Guido van Rossumb209a111997-04-29 18:18:01 +00003955static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003956cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003957{
Guido van Rossumac7be682001-01-17 15:42:30 +00003958 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003959 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003960 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003961 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003962 break;
3963 case PyCmp_IS_NOT:
3964 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003965 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003966 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003967 res = PySequence_Contains(w, v);
3968 if (res < 0)
3969 return NULL;
3970 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003971 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003972 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003973 if (res < 0)
3974 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003975 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003976 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003977 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003978 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003979 break;
3980 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003981 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003982 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003983 v = res ? Py_True : Py_False;
3984 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003985 return v;
3986}
3987
Thomas Wouters52152252000-08-17 22:55:00 +00003988static PyObject *
3989import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003990{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003991 PyObject *x;
3992
3993 x = PyObject_GetAttr(v, name);
3994 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003995 PyErr_Format(PyExc_ImportError,
3996 "cannot import name %.230s",
3997 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003998 }
Thomas Wouters52152252000-08-17 22:55:00 +00003999 return x;
4000}
Guido van Rossumac7be682001-01-17 15:42:30 +00004001
Thomas Wouters52152252000-08-17 22:55:00 +00004002static int
4003import_all_from(PyObject *locals, PyObject *v)
4004{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004005 PyObject *all = PyObject_GetAttrString(v, "__all__");
4006 PyObject *dict, *name, *value;
4007 int skip_leading_underscores = 0;
4008 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004009
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004010 if (all == NULL) {
4011 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4012 return -1; /* Unexpected error */
4013 PyErr_Clear();
4014 dict = PyObject_GetAttrString(v, "__dict__");
4015 if (dict == NULL) {
4016 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4017 return -1;
4018 PyErr_SetString(PyExc_ImportError,
4019 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004020 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004021 }
4022 all = PyMapping_Keys(dict);
4023 Py_DECREF(dict);
4024 if (all == NULL)
4025 return -1;
4026 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004027 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004028
4029 for (pos = 0, err = 0; ; pos++) {
4030 name = PySequence_GetItem(all, pos);
4031 if (name == NULL) {
4032 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4033 err = -1;
4034 else
4035 PyErr_Clear();
4036 break;
4037 }
4038 if (skip_leading_underscores &&
4039 PyString_Check(name) &&
4040 PyString_AS_STRING(name)[0] == '_')
4041 {
4042 Py_DECREF(name);
4043 continue;
4044 }
4045 value = PyObject_GetAttr(v, name);
4046 if (value == NULL)
4047 err = -1;
4048 else
4049 err = PyDict_SetItem(locals, name, value);
4050 Py_DECREF(name);
4051 Py_XDECREF(value);
4052 if (err != 0)
4053 break;
4054 }
4055 Py_DECREF(all);
4056 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004057}
4058
Guido van Rossumb209a111997-04-29 18:18:01 +00004059static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004060build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004061{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004062 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004063
4064 if (PyDict_Check(methods))
4065 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004066 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004067 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004068 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4069 base = PyTuple_GET_ITEM(bases, 0);
4070 metaclass = PyObject_GetAttrString(base, "__class__");
4071 if (metaclass == NULL) {
4072 PyErr_Clear();
4073 metaclass = (PyObject *)base->ob_type;
4074 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004075 }
4076 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004077 else {
4078 PyObject *g = PyEval_GetGlobals();
4079 if (g != NULL && PyDict_Check(g))
4080 metaclass = PyDict_GetItemString(g, "__metaclass__");
4081 if (metaclass == NULL)
4082 metaclass = (PyObject *) &PyClass_Type;
4083 Py_INCREF(metaclass);
4084 }
4085 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4086 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004087 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4088 /* A type error here likely means that the user passed
4089 in a base that was not a class (such the random module
4090 instead of the random.random type). Help them out with
4091 a more informative error message */
4092 PyErr_SetString(PyExc_TypeError,
4093 "Error when calling the metaclass.\n" \
4094 "Make sure the base arguments are valid.");
4095 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004096 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004097}
4098
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004099static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004100exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4101 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004102{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004103 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004104 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004105 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004106
Guido van Rossumb209a111997-04-29 18:18:01 +00004107 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4108 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004109 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004110 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004111 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004112 locals = PyTuple_GetItem(prog, 2);
4113 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004114 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004115 if (globals == Py_None) {
4116 globals = PyEval_GetGlobals();
4117 if (locals == Py_None) {
4118 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004119 plain = 1;
4120 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004121 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004122 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004123 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004124 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004125 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004126 !PyCode_Check(prog) &&
4127 !PyFile_Check(prog)) {
4128 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004129 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004130 return -1;
4131 }
Fred Drake661ea262000-10-24 19:57:45 +00004132 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004133 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004134 "exec: arg 2 must be a dictionary or None");
4135 return -1;
4136 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004137 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004138 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004139 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004140 return -1;
4141 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004142 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004143 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004144 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004145 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4146 PyErr_SetString(PyExc_TypeError,
4147 "code object passed to exec may not contain free variables");
4148 return -1;
4149 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004150 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004151 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004152 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004153 FILE *fp = PyFile_AsFile(prog);
4154 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004155 PyCompilerFlags cf;
4156 cf.cf_flags = 0;
4157 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004158 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004159 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004160 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004161 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004162 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004163 }
4164 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004165 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004166 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004167 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004168 cf.cf_flags = 0;
4169#ifdef Py_USING_UNICODE
4170 if (PyUnicode_Check(prog)) {
4171 tmp = PyUnicode_AsUTF8String(prog);
4172 if (tmp == NULL)
4173 return -1;
4174 prog = tmp;
4175 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4176 }
4177#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004178 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004179 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004180 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004181 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004182 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004183 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004184 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004185 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004186 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004187 if (plain)
4188 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004189 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004190 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004191 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004192 return 0;
4193}
Guido van Rossum24c13741995-02-14 09:42:43 +00004194
Guido van Rossumac7be682001-01-17 15:42:30 +00004195static void
Paul Prescode68140d2000-08-30 20:25:01 +00004196format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4197{
4198 char *obj_str;
4199
4200 if (!obj)
4201 return;
4202
4203 obj_str = PyString_AsString(obj);
4204 if (!obj_str)
4205 return;
4206
4207 PyErr_Format(exc, format_str, obj_str);
4208}
Guido van Rossum950361c1997-01-24 13:49:28 +00004209
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004210static PyObject *
4211string_concatenate(PyObject *v, PyObject *w,
4212 PyFrameObject *f, unsigned char *next_instr)
4213{
4214 /* This function implements 'variable += expr' when both arguments
4215 are strings. */
4216
4217 if (v->ob_refcnt == 2) {
4218 /* In the common case, there are 2 references to the value
4219 * stored in 'variable' when the += is performed: one on the
4220 * value stack (in 'v') and one still stored in the 'variable'.
4221 * We try to delete the variable now to reduce the refcnt to 1.
4222 */
4223 switch (*next_instr) {
4224 case STORE_FAST:
4225 {
4226 int oparg = PEEKARG();
4227 PyObject **fastlocals = f->f_localsplus;
4228 if (GETLOCAL(oparg) == v)
4229 SETLOCAL(oparg, NULL);
4230 break;
4231 }
4232 case STORE_DEREF:
4233 {
4234 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4235 PyObject *c = freevars[PEEKARG()];
4236 if (PyCell_GET(c) == v)
4237 PyCell_Set(c, NULL);
4238 break;
4239 }
4240 case STORE_NAME:
4241 {
4242 PyObject *names = f->f_code->co_names;
4243 PyObject *name = GETITEM(names, PEEKARG());
4244 PyObject *locals = f->f_locals;
4245 if (PyDict_CheckExact(locals) &&
4246 PyDict_GetItem(locals, name) == v) {
4247 if (PyDict_DelItem(locals, name) != 0) {
4248 PyErr_Clear();
4249 }
4250 }
4251 break;
4252 }
4253 }
4254 }
4255
Armin Rigo618fbf52004-08-07 20:58:32 +00004256 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004257 /* Now we own the last reference to 'v', so we can resize it
4258 * in-place.
4259 */
4260 int v_len = PyString_GET_SIZE(v);
4261 int w_len = PyString_GET_SIZE(w);
4262 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4263 /* XXX if _PyString_Resize() fails, 'v' has been
4264 * deallocated so it cannot be put back into 'variable'.
4265 * The MemoryError is raised when there is no value in
4266 * 'variable', which might (very remotely) be a cause
4267 * of incompatibilities.
4268 */
4269 return NULL;
4270 }
4271 /* copy 'w' into the newly allocated area of 'v' */
4272 memcpy(PyString_AS_STRING(v) + v_len,
4273 PyString_AS_STRING(w), w_len);
4274 return v;
4275 }
4276 else {
4277 /* When in-place resizing is not an option. */
4278 PyString_Concat(&v, w);
4279 return v;
4280 }
4281}
4282
Guido van Rossum950361c1997-01-24 13:49:28 +00004283#ifdef DYNAMIC_EXECUTION_PROFILE
4284
Skip Montanarof118cb12001-10-15 20:51:38 +00004285static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004286getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004287{
4288 int i;
4289 PyObject *l = PyList_New(256);
4290 if (l == NULL) return NULL;
4291 for (i = 0; i < 256; i++) {
4292 PyObject *x = PyInt_FromLong(a[i]);
4293 if (x == NULL) {
4294 Py_DECREF(l);
4295 return NULL;
4296 }
4297 PyList_SetItem(l, i, x);
4298 }
4299 for (i = 0; i < 256; i++)
4300 a[i] = 0;
4301 return l;
4302}
4303
4304PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004305_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004306{
4307#ifndef DXPAIRS
4308 return getarray(dxp);
4309#else
4310 int i;
4311 PyObject *l = PyList_New(257);
4312 if (l == NULL) return NULL;
4313 for (i = 0; i < 257; i++) {
4314 PyObject *x = getarray(dxpairs[i]);
4315 if (x == NULL) {
4316 Py_DECREF(l);
4317 return NULL;
4318 }
4319 PyList_SetItem(l, i, x);
4320 }
4321 return l;
4322#endif
4323}
4324
4325#endif