blob: 152b9421283ebbd84f93f221d8247decc6878be9 [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 *);
Guido van Rossum374a9221991-04-04 10:40:29 +000088
Paul Prescode68140d2000-08-30 20:25:01 +000089#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000090 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +000091#define GLOBAL_NAME_ERROR_MSG \
92 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000093#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +000094 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000095#define UNBOUNDFREE_ERROR_MSG \
96 "free variable '%.200s' referenced before assignment" \
97 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000098
Guido van Rossum950361c1997-01-24 13:49:28 +000099/* Dynamic execution profile */
100#ifdef DYNAMIC_EXECUTION_PROFILE
101#ifdef DXPAIRS
102static long dxpairs[257][256];
103#define dxp dxpairs[256]
104#else
105static long dxp[256];
106#endif
107#endif
108
Jeremy Hylton985eba52003-02-05 23:13:00 +0000109/* Function call profile */
110#ifdef CALL_PROFILE
111#define PCALL_NUM 11
112static int pcall[PCALL_NUM];
113
114#define PCALL_ALL 0
115#define PCALL_FUNCTION 1
116#define PCALL_FAST_FUNCTION 2
117#define PCALL_FASTER_FUNCTION 3
118#define PCALL_METHOD 4
119#define PCALL_BOUND_METHOD 5
120#define PCALL_CFUNCTION 6
121#define PCALL_TYPE 7
122#define PCALL_GENERATOR 8
123#define PCALL_OTHER 9
124#define PCALL_POP 10
125
126/* Notes about the statistics
127
128 PCALL_FAST stats
129
130 FAST_FUNCTION means no argument tuple needs to be created.
131 FASTER_FUNCTION means that the fast-path frame setup code is used.
132
133 If there is a method call where the call can be optimized by changing
134 the argument tuple and calling the function directly, it gets recorded
135 twice.
136
137 As a result, the relationship among the statistics appears to be
138 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
139 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
140 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
141 PCALL_METHOD > PCALL_BOUND_METHOD
142*/
143
144#define PCALL(POS) pcall[POS]++
145
146PyObject *
147PyEval_GetCallStats(PyObject *self)
148{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000149 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000150 pcall[0], pcall[1], pcall[2], pcall[3],
151 pcall[4], pcall[5], pcall[6], pcall[7],
152 pcall[8], pcall[9]);
153}
154#else
155#define PCALL(O)
156
157PyObject *
158PyEval_GetCallStats(PyObject *self)
159{
160 Py_INCREF(Py_None);
161 return Py_None;
162}
163#endif
164
Tim Peters5ca576e2001-06-18 22:08:13 +0000165
Guido van Rossume59214e1994-08-30 08:01:59 +0000166#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000167
Guido van Rossum2571cc81999-04-07 16:07:23 +0000168#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000169#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000170#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000171#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000172
Guido van Rossuma027efa1997-05-05 20:56:21 +0000173extern int _PyThread_Started; /* Flag for Py_Exit */
174
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000175static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000176static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000177
178void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000179PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000180{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000181 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000182 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000183 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000184 interpreter_lock = PyThread_allocate_lock();
185 PyThread_acquire_lock(interpreter_lock, 1);
186 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000187}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000188
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000189void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000190PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000191{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000192 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193}
194
195void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000196PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000197{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000198 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199}
200
201void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000202PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000203{
204 if (tstate == NULL)
205 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000206 /* Check someone has called PyEval_InitThreads() to create the lock */
207 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000208 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000209 if (PyThreadState_Swap(tstate) != NULL)
210 Py_FatalError(
211 "PyEval_AcquireThread: non-NULL old thread state");
212}
213
214void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000215PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000216{
217 if (tstate == NULL)
218 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
219 if (PyThreadState_Swap(NULL) != tstate)
220 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000221 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000222}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000223
224/* This function is called from PyOS_AfterFork to ensure that newly
225 created child processes don't hold locks referring to threads which
226 are not running in the child process. (This could also be done using
227 pthread_atfork mechanism, at least for the pthreads implementation.) */
228
229void
230PyEval_ReInitThreads(void)
231{
232 if (!interpreter_lock)
233 return;
234 /*XXX Can't use PyThread_free_lock here because it does too
235 much error-checking. Doing this cleanly would require
236 adding a new function to each thread_*.h. Instead, just
237 create a new lock and waste a little bit of memory */
238 interpreter_lock = PyThread_allocate_lock();
239 PyThread_acquire_lock(interpreter_lock, 1);
240 main_thread = PyThread_get_thread_ident();
241}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000242#endif
243
Guido van Rossumff4949e1992-08-05 19:58:53 +0000244/* Functions save_thread and restore_thread are always defined so
245 dynamically loaded modules needn't be compiled separately for use
246 with and without threads: */
247
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000248PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000250{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000251 PyThreadState *tstate = PyThreadState_Swap(NULL);
252 if (tstate == NULL)
253 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000254#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000255 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000256 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000257#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000258 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000259}
260
261void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000262PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000263{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000264 if (tstate == NULL)
265 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000266#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000268 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000269 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000270 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000271 }
272#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000273 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000274}
275
276
Guido van Rossuma9672091994-09-14 13:31:22 +0000277/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
278 signal handlers or Mac I/O completion routines) can schedule calls
279 to a function to be called synchronously.
280 The synchronous function is called with one void* argument.
281 It should return 0 for success or -1 for failure -- failure should
282 be accompanied by an exception.
283
284 If registry succeeds, the registry function returns 0; if it fails
285 (e.g. due to too many pending calls) it returns -1 (without setting
286 an exception condition).
287
288 Note that because registry may occur from within signal handlers,
289 or other asynchronous events, calling malloc() is unsafe!
290
291#ifdef WITH_THREAD
292 Any thread can schedule pending calls, but only the main thread
293 will execute them.
294#endif
295
296 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
297 There are two possible race conditions:
298 (1) nested asynchronous registry calls;
299 (2) registry calls made while pending calls are being processed.
300 While (1) is very unlikely, (2) is a real possibility.
301 The current code is safe against (2), but not against (1).
302 The safety against (2) is derived from the fact that only one
303 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000304
Guido van Rossuma027efa1997-05-05 20:56:21 +0000305 XXX Darn! With the advent of thread state, we should have an array
306 of pending calls per thread in the thread state! Later...
307*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000308
Guido van Rossuma9672091994-09-14 13:31:22 +0000309#define NPENDINGCALLS 32
310static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000311 int (*func)(void *);
312 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000313} pendingcalls[NPENDINGCALLS];
314static volatile int pendingfirst = 0;
315static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000316static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000317
318int
Thomas Wouters334fb892000-07-25 12:56:38 +0000319Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000320{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000321 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000322 int i, j;
323 /* XXX Begin critical section */
324 /* XXX If you want this to be safe against nested
325 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000326 if (busy)
327 return -1;
328 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000329 i = pendinglast;
330 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000331 if (j == pendingfirst) {
332 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000333 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000334 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000335 pendingcalls[i].func = func;
336 pendingcalls[i].arg = arg;
337 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000338
339 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000340 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000341 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000342 /* XXX End critical section */
343 return 0;
344}
345
Guido van Rossum180d7b41994-09-29 09:45:57 +0000346int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000347Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000348{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000349 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000350#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000351 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000352 return 0;
353#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000354 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000355 return 0;
356 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000357 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000358 for (;;) {
359 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000360 int (*func)(void *);
361 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000362 i = pendingfirst;
363 if (i == pendinglast)
364 break; /* Queue empty */
365 func = pendingcalls[i].func;
366 arg = pendingcalls[i].arg;
367 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000368 if (func(arg) < 0) {
369 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000370 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000371 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000372 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000373 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000374 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000375 return 0;
376}
377
378
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000379/* The interpreter's recursion limit */
380
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000381static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000382int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000383
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000384int
385Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000386{
387 return recursion_limit;
388}
389
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000390void
391Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000392{
393 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000394 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000395}
396
Armin Rigo2b3eb402003-10-28 12:05:48 +0000397/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
398 if the recursion_depth reaches _Py_CheckRecursionLimit.
399 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
400 to guarantee that _Py_CheckRecursiveCall() is regularly called.
401 Without USE_STACKCHECK, there is no need for this. */
402int
403_Py_CheckRecursiveCall(char *where)
404{
405 PyThreadState *tstate = PyThreadState_GET();
406
407#ifdef USE_STACKCHECK
408 if (PyOS_CheckStack()) {
409 --tstate->recursion_depth;
410 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
411 return -1;
412 }
413#endif
414 if (tstate->recursion_depth > recursion_limit) {
415 --tstate->recursion_depth;
416 PyErr_Format(PyExc_RuntimeError,
417 "maximum recursion depth exceeded%s",
418 where);
419 return -1;
420 }
421 _Py_CheckRecursionLimit = recursion_limit;
422 return 0;
423}
424
Guido van Rossum374a9221991-04-04 10:40:29 +0000425/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000426enum why_code {
427 WHY_NOT = 0x0001, /* No error */
428 WHY_EXCEPTION = 0x0002, /* Exception occurred */
429 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
430 WHY_RETURN = 0x0008, /* 'return' statement */
431 WHY_BREAK = 0x0010, /* 'break' statement */
432 WHY_CONTINUE = 0x0020, /* 'continue' statement */
433 WHY_YIELD = 0x0040 /* 'yield' operator */
434};
Guido van Rossum374a9221991-04-04 10:40:29 +0000435
Raymond Hettinger7c958652004-04-06 10:11:10 +0000436static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000437static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000438
Skip Montanarod581d772002-09-03 20:10:45 +0000439/* for manipulating the thread switch and periodic "stuff" - used to be
440 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000441int _Py_CheckInterval = 100;
442volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000443
Guido van Rossumb209a111997-04-29 18:18:01 +0000444PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000445PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000446{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000447 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000448 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000449 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000450 (PyObject **)NULL, 0,
451 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000452 (PyObject **)NULL, 0,
453 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000454}
455
456
457/* Interpreter main loop */
458
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000459PyObject *
460PyEval_EvalFrame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000461{
Guido van Rossum950361c1997-01-24 13:49:28 +0000462#ifdef DXPAIRS
463 int lastopcode = 0;
464#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000465 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000466 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000467 register int opcode; /* Current opcode */
468 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000469 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000470 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000471 register PyObject *x; /* Result object -- NULL if error */
472 register PyObject *v; /* Temporary objects popped off stack */
473 register PyObject *w;
474 register PyObject *u;
475 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000476 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000477 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000478 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000479 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000480 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000481
Tim Peters8a5c3c72004-04-05 19:36:21 +0000482 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000483
484 not (instr_lb <= current_bytecode_offset < instr_ub)
485
Tim Peters8a5c3c72004-04-05 19:36:21 +0000486 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000487 initial values are such as to make this false the first
488 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000489 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000490
Guido van Rossumd076c731998-10-07 19:42:25 +0000491 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000492 PyObject *names;
493 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000494#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000495 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000496#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000497#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000498 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000499 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000500#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000501
Neal Norwitza81d2202002-07-14 00:27:26 +0000502/* Tuple access macros */
503
504#ifndef Py_DEBUG
505#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
506#else
507#define GETITEM(v, i) PyTuple_GetItem((v), (i))
508#endif
509
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000510#ifdef WITH_TSC
511/* Use Pentium timestamp counter to mark certain events:
512 inst0 -- beginning of switch statement for opcode dispatch
513 inst1 -- end of switch statement (may be skipped)
514 loop0 -- the top of the mainloop
515 loop1 -- place where control returns again to top of mainloop
516 (may be skipped)
517 intr1 -- beginning of long interruption
518 intr2 -- end of long interruption
519
520 Many opcodes call out to helper C functions. In some cases, the
521 time in those functions should be counted towards the time for the
522 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
523 calls another Python function; there's no point in charge all the
524 bytecode executed by the called function to the caller.
525
526 It's hard to make a useful judgement statically. In the presence
527 of operator overloading, it's impossible to tell if a call will
528 execute new Python code or not.
529
530 It's a case-by-case judgement. I'll use intr1 for the following
531 cases:
532
533 EXEC_STMT
534 IMPORT_STAR
535 IMPORT_FROM
536 CALL_FUNCTION (and friends)
537
538 */
539 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
540 int ticked = 0;
541
542 rdtscll(inst0);
543 rdtscll(inst1);
544 rdtscll(loop0);
545 rdtscll(loop1);
546#endif
547
Guido van Rossum374a9221991-04-04 10:40:29 +0000548/* Code access macros */
549
Guido van Rossumd076c731998-10-07 19:42:25 +0000550#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000551#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000552#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Guido van Rossumd076c731998-10-07 19:42:25 +0000553#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000554#define JUMPBY(x) (next_instr += (x))
555
Raymond Hettingerf606f872003-03-16 03:11:04 +0000556/* OpCode prediction macros
557 Some opcodes tend to come in pairs thus making it possible to predict
558 the second code when the first is run. For example, COMPARE_OP is often
559 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
560 followed by a POP_TOP.
561
562 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000563 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000564 processor has a high likelihood of making its own successful branch
565 prediction which results in a nearly zero overhead transition to the
566 next opcode.
567
568 A successful prediction saves a trip through the eval-loop including
569 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000570
Tim Peters8a5c3c72004-04-05 19:36:21 +0000571 If collecting opcode statistics, turn off prediction so that
572 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000573 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000574*/
575
Raymond Hettingera7216982004-02-08 19:59:27 +0000576#ifdef DYNAMIC_EXECUTION_PROFILE
577#define PREDICT(op) if (0) goto PRED_##op
578#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000579#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000580#endif
581
Raymond Hettingerf606f872003-03-16 03:11:04 +0000582#define PREDICTED(op) PRED_##op: next_instr++
Armin Rigo9dbf9082004-03-20 21:50:13 +0000583#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = (next_instr[2]<<8) + \
584 next_instr[1]; next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000585
Guido van Rossum374a9221991-04-04 10:40:29 +0000586/* Stack manipulation macros */
587
588#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
589#define EMPTY() (STACK_LEVEL() == 0)
590#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000591#define SECOND() (stack_pointer[-2])
592#define THIRD() (stack_pointer[-3])
593#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000594#define SET_TOP(v) (stack_pointer[-1] = (v))
595#define SET_SECOND(v) (stack_pointer[-2] = (v))
596#define SET_THIRD(v) (stack_pointer[-3] = (v))
597#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000598#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000599#define BASIC_PUSH(v) (*stack_pointer++ = (v))
600#define BASIC_POP() (*--stack_pointer)
601
Guido van Rossum96a42c81992-01-12 02:29:51 +0000602#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000603#define PUSH(v) { (void)(BASIC_PUSH(v), \
604 lltrace && prtrace(TOP(), "push")); \
605 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000606#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000607#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
608 lltrace && prtrace(TOP(), "stackadj")); \
609 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000610#else
611#define PUSH(v) BASIC_PUSH(v)
612#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000613#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000614#endif
615
Guido van Rossum681d79a1995-07-18 14:51:37 +0000616/* Local variable macros */
617
618#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000619
620/* The SETLOCAL() macro must not DECREF the local variable in-place and
621 then store the new value; it must copy the old value to a temporary
622 value, then store the new value, and then DECREF the temporary value.
623 This is because it is possible that during the DECREF the frame is
624 accessed by other code (e.g. a __del__ method or gc.collect()) and the
625 variable would be pointing to already-freed memory. */
626#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
627 GETLOCAL(i) = value; \
628 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000629
Guido van Rossuma027efa1997-05-05 20:56:21 +0000630/* Start of code */
631
Tim Peters5ca576e2001-06-18 22:08:13 +0000632 if (f == NULL)
633 return NULL;
634
Armin Rigo1d313ab2003-10-25 14:33:09 +0000635 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000636 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000637 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000638
Tim Peters5ca576e2001-06-18 22:08:13 +0000639 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000640
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000641 if (tstate->use_tracing) {
642 if (tstate->c_tracefunc != NULL) {
643 /* tstate->c_tracefunc, if defined, is a
644 function that will be called on *every* entry
645 to a code block. Its return value, if not
646 None, is a function that will be called at
647 the start of each executed line of code.
648 (Actually, the function must return itself
649 in order to continue tracing.) The trace
650 functions are called with three arguments:
651 a pointer to the current frame, a string
652 indicating why the function is called, and
653 an argument which depends on the situation.
654 The global trace function is also called
655 whenever an exception is detected. */
656 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
657 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000658 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000659 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000660 }
661 }
662 if (tstate->c_profilefunc != NULL) {
663 /* Similar for c_profilefunc, except it needn't
664 return itself and isn't called for "line" events */
665 if (call_trace(tstate->c_profilefunc,
666 tstate->c_profileobj,
667 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000668 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000669 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000670 }
671 }
672 }
673
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000674 co = f->f_code;
675 names = co->co_names;
676 consts = co->co_consts;
677 fastlocals = f->f_localsplus;
678 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000679 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000680 /* An explanation is in order for the next line.
681
682 f->f_lasti now refers to the index of the last instruction
683 executed. You might think this was obvious from the name, but
684 this wasn't always true before 2.3! PyFrame_New now sets
685 f->f_lasti to -1 (i.e. the index *before* the first instruction)
686 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
687 does work. Promise. */
688 next_instr = first_instr + f->f_lasti + 1;
689 stack_pointer = f->f_stacktop;
690 assert(stack_pointer != NULL);
691 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
692
Tim Peters5ca576e2001-06-18 22:08:13 +0000693#ifdef LLTRACE
694 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
695#endif
696#if defined(Py_DEBUG) || defined(LLTRACE)
697 filename = PyString_AsString(co->co_filename);
698#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000699
Guido van Rossum374a9221991-04-04 10:40:29 +0000700 why = WHY_NOT;
701 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000702 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000703 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000704
Guido van Rossum374a9221991-04-04 10:40:29 +0000705 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000706#ifdef WITH_TSC
707 if (inst1 == 0) {
708 /* Almost surely, the opcode executed a break
709 or a continue, preventing inst1 from being set
710 on the way out of the loop.
711 */
712 rdtscll(inst1);
713 loop1 = inst1;
714 }
715 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
716 intr0, intr1);
717 ticked = 0;
718 inst1 = 0;
719 intr0 = 0;
720 intr1 = 0;
721 rdtscll(loop0);
722#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000723 assert(stack_pointer >= f->f_valuestack); /* else underflow */
724 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
725
Guido van Rossuma027efa1997-05-05 20:56:21 +0000726 /* Do periodic things. Doing this every time through
727 the loop would add too much overhead, so we do it
728 only every Nth instruction. We also do it if
729 ``things_to_do'' is set, i.e. when an asynchronous
730 event needs attention (e.g. a signal handler or
731 async I/O handler); see Py_AddPendingCall() and
732 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000733
Skip Montanarod581d772002-09-03 20:10:45 +0000734 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000735 if (*next_instr == SETUP_FINALLY) {
736 /* Make the last opcode before
737 a try: finally: block uninterruptable. */
738 goto fast_next_opcode;
739 }
Skip Montanarod581d772002-09-03 20:10:45 +0000740 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000741 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000742#ifdef WITH_TSC
743 ticked = 1;
744#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000745 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000746 if (Py_MakePendingCalls() < 0) {
747 why = WHY_EXCEPTION;
748 goto on_error;
749 }
750 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000751#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000752 if (interpreter_lock) {
753 /* Give another thread a chance */
754
Guido van Rossum25ce5661997-08-02 03:10:38 +0000755 if (PyThreadState_Swap(NULL) != tstate)
756 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000757 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000758
759 /* Other threads may run now */
760
Guido van Rossum65d5b571998-12-21 19:32:43 +0000761 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000762 if (PyThreadState_Swap(tstate) != NULL)
763 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000764
765 /* Check for thread interrupts */
766
767 if (tstate->async_exc != NULL) {
768 x = tstate->async_exc;
769 tstate->async_exc = NULL;
770 PyErr_SetNone(x);
771 Py_DECREF(x);
772 why = WHY_EXCEPTION;
773 goto on_error;
774 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000775 }
776#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000777 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000778
Neil Schemenauer63543862002-02-17 19:10:14 +0000779 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000780 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000781
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000782 /* line-by-line tracing support */
783
784 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
785 /* see maybe_call_line_trace
786 for expository comments */
787 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000788
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000789 err = maybe_call_line_trace(tstate->c_tracefunc,
790 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000791 f, &instr_lb, &instr_ub,
792 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000793 /* Reload possibly changed frame fields */
794 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000795 if (f->f_stacktop != NULL) {
796 stack_pointer = f->f_stacktop;
797 f->f_stacktop = NULL;
798 }
799 if (err) {
800 /* trace function raised an exception */
801 goto on_error;
802 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000803 }
804
805 /* Extract opcode and argument */
806
Guido van Rossum374a9221991-04-04 10:40:29 +0000807 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000808 oparg = 0; /* allows oparg to be stored in a register because
809 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000810 if (HAS_ARG(opcode))
811 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000812 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000813#ifdef DYNAMIC_EXECUTION_PROFILE
814#ifdef DXPAIRS
815 dxpairs[lastopcode][opcode]++;
816 lastopcode = opcode;
817#endif
818 dxp[opcode]++;
819#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000820
Guido van Rossum96a42c81992-01-12 02:29:51 +0000821#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000822 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000823
Guido van Rossum96a42c81992-01-12 02:29:51 +0000824 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000825 if (HAS_ARG(opcode)) {
826 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000827 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000828 }
829 else {
830 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000831 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000832 }
833 }
834#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000835
Guido van Rossum374a9221991-04-04 10:40:29 +0000836 /* Main switch on opcode */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000837#ifdef WITH_TSC
838 rdtscll(inst0);
839#endif
Jeremy Hylton52820442001-01-03 23:52:36 +0000840
Guido van Rossum374a9221991-04-04 10:40:29 +0000841 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000842
Guido van Rossum374a9221991-04-04 10:40:29 +0000843 /* BEWARE!
844 It is essential that any operation that fails sets either
845 x to NULL, err to nonzero, or why to anything but WHY_NOT,
846 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000847
Guido van Rossum374a9221991-04-04 10:40:29 +0000848 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000849
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000850 case NOP:
851 goto fast_next_opcode;
852
Neil Schemenauer63543862002-02-17 19:10:14 +0000853 case LOAD_FAST:
854 x = GETLOCAL(oparg);
855 if (x != NULL) {
856 Py_INCREF(x);
857 PUSH(x);
858 goto fast_next_opcode;
859 }
860 format_exc_check_arg(PyExc_UnboundLocalError,
861 UNBOUNDLOCAL_ERROR_MSG,
862 PyTuple_GetItem(co->co_varnames, oparg));
863 break;
864
865 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000866 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000867 Py_INCREF(x);
868 PUSH(x);
869 goto fast_next_opcode;
870
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000871 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000872 case STORE_FAST:
873 v = POP();
874 SETLOCAL(oparg, v);
875 goto fast_next_opcode;
876
Raymond Hettingerf606f872003-03-16 03:11:04 +0000877 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000878 case POP_TOP:
879 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000880 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000881 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000882
Guido van Rossum374a9221991-04-04 10:40:29 +0000883 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000884 v = TOP();
885 w = SECOND();
886 SET_TOP(w);
887 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000888 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000889
Guido van Rossum374a9221991-04-04 10:40:29 +0000890 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000891 v = TOP();
892 w = SECOND();
893 x = THIRD();
894 SET_TOP(w);
895 SET_SECOND(x);
896 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000897 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000898
Thomas Wouters434d0822000-08-24 20:11:32 +0000899 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000900 u = TOP();
901 v = SECOND();
902 w = THIRD();
903 x = FOURTH();
904 SET_TOP(v);
905 SET_SECOND(w);
906 SET_THIRD(x);
907 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000908 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000909
Guido van Rossum374a9221991-04-04 10:40:29 +0000910 case DUP_TOP:
911 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000912 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000913 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000914 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000915
Thomas Wouters434d0822000-08-24 20:11:32 +0000916 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000917 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000918 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000919 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000920 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000921 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000922 STACKADJ(2);
923 SET_TOP(x);
924 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000925 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000926 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000927 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000928 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000929 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000930 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000931 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000932 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000933 STACKADJ(3);
934 SET_TOP(x);
935 SET_SECOND(w);
936 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000937 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000938 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000939 Py_FatalError("invalid argument to DUP_TOPX"
940 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000941 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000942
Guido van Rossum374a9221991-04-04 10:40:29 +0000943 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000944 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000945 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000946 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000947 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000948 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000950
Guido van Rossum374a9221991-04-04 10:40:29 +0000951 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000952 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000953 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000954 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000955 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000956 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000957 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000958
Guido van Rossum374a9221991-04-04 10:40:29 +0000959 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000960 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000961 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000962 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000963 if (err == 0) {
964 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000965 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +0000966 continue;
967 }
968 else if (err > 0) {
969 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +0000971 err = 0;
972 continue;
973 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +0000974 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +0000975 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000976
Guido van Rossum374a9221991-04-04 10:40:29 +0000977 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000979 x = PyObject_Repr(v);
980 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000982 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000983 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000984
Guido van Rossum7928cd71991-10-24 14:59:31 +0000985 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000986 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000987 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000988 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000989 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000990 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000991 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000992
Guido van Rossum50564e81996-01-12 01:13:16 +0000993 case BINARY_POWER:
994 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000996 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +0000997 Py_DECREF(v);
998 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000999 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001000 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001001 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001002
Guido van Rossum374a9221991-04-04 10:40:29 +00001003 case BINARY_MULTIPLY:
1004 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001006 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001007 Py_DECREF(v);
1008 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001009 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001010 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001011 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001012
Guido van Rossum374a9221991-04-04 10:40:29 +00001013 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001014 if (!_Py_QnewFlag) {
1015 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001017 x = PyNumber_Divide(v, w);
1018 Py_DECREF(v);
1019 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001021 if (x != NULL) continue;
1022 break;
1023 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001024 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001025 BINARY_TRUE_DIVIDE */
1026 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001027 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001028 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001029 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001030 Py_DECREF(v);
1031 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001033 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001034 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001035
Guido van Rossum4668b002001-08-08 05:00:18 +00001036 case BINARY_FLOOR_DIVIDE:
1037 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001038 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001039 x = PyNumber_FloorDivide(v, w);
1040 Py_DECREF(v);
1041 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001043 if (x != NULL) continue;
1044 break;
1045
Guido van Rossum374a9221991-04-04 10:40:29 +00001046 case BINARY_MODULO:
1047 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001048 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001049 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001050 Py_DECREF(v);
1051 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001052 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001053 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001054 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001055
Guido van Rossum374a9221991-04-04 10:40:29 +00001056 case BINARY_ADD:
1057 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001058 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001059 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001060 /* INLINE: int + int */
1061 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001062 a = PyInt_AS_LONG(v);
1063 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001064 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001065 if ((i^a) < 0 && (i^b) < 0)
1066 goto slow_add;
1067 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001068 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001069 else {
1070 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001071 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001072 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001073 Py_DECREF(v);
1074 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001076 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001078
Guido van Rossum374a9221991-04-04 10:40:29 +00001079 case BINARY_SUBTRACT:
1080 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001082 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001083 /* INLINE: int - int */
1084 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001085 a = PyInt_AS_LONG(v);
1086 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001087 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001088 if ((i^a) < 0 && (i^~b) < 0)
1089 goto slow_sub;
1090 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001091 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001092 else {
1093 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001094 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001095 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001096 Py_DECREF(v);
1097 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001099 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001100 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001101
Guido van Rossum374a9221991-04-04 10:40:29 +00001102 case BINARY_SUBSCR:
1103 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001104 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001105 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001106 /* INLINE: list[int] */
1107 long i = PyInt_AsLong(w);
1108 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001109 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001110 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001111 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001112 Py_INCREF(x);
1113 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001114 else
1115 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001116 }
1117 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001118 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001119 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001120 Py_DECREF(v);
1121 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001122 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001123 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001124 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001125
Guido van Rossum7928cd71991-10-24 14:59:31 +00001126 case BINARY_LSHIFT:
1127 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001128 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001129 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001130 Py_DECREF(v);
1131 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001132 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001133 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001134 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001135
Guido van Rossum7928cd71991-10-24 14:59:31 +00001136 case BINARY_RSHIFT:
1137 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001138 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001139 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001140 Py_DECREF(v);
1141 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001142 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001143 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001144 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001145
Guido van Rossum7928cd71991-10-24 14:59:31 +00001146 case BINARY_AND:
1147 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001148 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001149 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001150 Py_DECREF(v);
1151 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001152 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001153 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001154 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001155
Guido van Rossum7928cd71991-10-24 14:59:31 +00001156 case BINARY_XOR:
1157 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001158 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001159 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001160 Py_DECREF(v);
1161 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001162 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001163 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001164 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001165
Guido van Rossum7928cd71991-10-24 14:59:31 +00001166 case BINARY_OR:
1167 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001168 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001169 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001170 Py_DECREF(v);
1171 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001172 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001173 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001174 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001175
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001176 case LIST_APPEND:
1177 w = POP();
1178 v = POP();
1179 err = PyList_Append(v, w);
1180 Py_DECREF(v);
1181 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001182 if (err == 0) {
1183 PREDICT(JUMP_ABSOLUTE);
1184 continue;
1185 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001186 break;
1187
Thomas Wouters434d0822000-08-24 20:11:32 +00001188 case INPLACE_POWER:
1189 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001190 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001191 x = PyNumber_InPlacePower(v, w, Py_None);
1192 Py_DECREF(v);
1193 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001194 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001195 if (x != NULL) continue;
1196 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001197
Thomas Wouters434d0822000-08-24 20:11:32 +00001198 case INPLACE_MULTIPLY:
1199 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001201 x = PyNumber_InPlaceMultiply(v, w);
1202 Py_DECREF(v);
1203 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001204 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001205 if (x != NULL) continue;
1206 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001207
Thomas Wouters434d0822000-08-24 20:11:32 +00001208 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001209 if (!_Py_QnewFlag) {
1210 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001212 x = PyNumber_InPlaceDivide(v, w);
1213 Py_DECREF(v);
1214 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001216 if (x != NULL) continue;
1217 break;
1218 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001219 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001220 INPLACE_TRUE_DIVIDE */
1221 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001222 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001223 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001224 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001225 Py_DECREF(v);
1226 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001227 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001228 if (x != NULL) continue;
1229 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001230
Guido van Rossum4668b002001-08-08 05:00:18 +00001231 case INPLACE_FLOOR_DIVIDE:
1232 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001233 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001234 x = PyNumber_InPlaceFloorDivide(v, w);
1235 Py_DECREF(v);
1236 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001237 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001238 if (x != NULL) continue;
1239 break;
1240
Thomas Wouters434d0822000-08-24 20:11:32 +00001241 case INPLACE_MODULO:
1242 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001243 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 x = PyNumber_InPlaceRemainder(v, w);
1245 Py_DECREF(v);
1246 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001247 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001248 if (x != NULL) continue;
1249 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001250
Thomas Wouters434d0822000-08-24 20:11:32 +00001251 case INPLACE_ADD:
1252 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001254 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 /* INLINE: int + int */
1256 register long a, b, i;
1257 a = PyInt_AS_LONG(v);
1258 b = PyInt_AS_LONG(w);
1259 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001260 if ((i^a) < 0 && (i^b) < 0)
1261 goto slow_iadd;
1262 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001264 else {
1265 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001267 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001268 Py_DECREF(v);
1269 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001270 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001271 if (x != NULL) continue;
1272 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001273
Thomas Wouters434d0822000-08-24 20:11:32 +00001274 case INPLACE_SUBTRACT:
1275 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001276 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001277 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 /* INLINE: int - int */
1279 register long a, b, i;
1280 a = PyInt_AS_LONG(v);
1281 b = PyInt_AS_LONG(w);
1282 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001283 if ((i^a) < 0 && (i^~b) < 0)
1284 goto slow_isub;
1285 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001287 else {
1288 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001290 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001291 Py_DECREF(v);
1292 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001293 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 if (x != NULL) continue;
1295 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001296
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 case INPLACE_LSHIFT:
1298 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001299 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001300 x = PyNumber_InPlaceLshift(v, w);
1301 Py_DECREF(v);
1302 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001303 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001304 if (x != NULL) continue;
1305 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Thomas Wouters434d0822000-08-24 20:11:32 +00001307 case INPLACE_RSHIFT:
1308 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001309 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001310 x = PyNumber_InPlaceRshift(v, w);
1311 Py_DECREF(v);
1312 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001313 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 if (x != NULL) continue;
1315 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001316
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 case INPLACE_AND:
1318 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001319 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 x = PyNumber_InPlaceAnd(v, w);
1321 Py_DECREF(v);
1322 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001323 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001324 if (x != NULL) continue;
1325 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001326
Thomas Wouters434d0822000-08-24 20:11:32 +00001327 case INPLACE_XOR:
1328 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001329 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001330 x = PyNumber_InPlaceXor(v, w);
1331 Py_DECREF(v);
1332 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001333 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 if (x != NULL) continue;
1335 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 case INPLACE_OR:
1338 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001339 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 x = PyNumber_InPlaceOr(v, w);
1341 Py_DECREF(v);
1342 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001343 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001344 if (x != NULL) continue;
1345 break;
1346
Guido van Rossum374a9221991-04-04 10:40:29 +00001347 case SLICE+0:
1348 case SLICE+1:
1349 case SLICE+2:
1350 case SLICE+3:
1351 if ((opcode-SLICE) & 2)
1352 w = POP();
1353 else
1354 w = NULL;
1355 if ((opcode-SLICE) & 1)
1356 v = POP();
1357 else
1358 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001359 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001360 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001361 Py_DECREF(u);
1362 Py_XDECREF(v);
1363 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001365 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001366 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Guido van Rossum374a9221991-04-04 10:40:29 +00001368 case STORE_SLICE+0:
1369 case STORE_SLICE+1:
1370 case STORE_SLICE+2:
1371 case STORE_SLICE+3:
1372 if ((opcode-STORE_SLICE) & 2)
1373 w = POP();
1374 else
1375 w = NULL;
1376 if ((opcode-STORE_SLICE) & 1)
1377 v = POP();
1378 else
1379 v = NULL;
1380 u = POP();
1381 t = POP();
1382 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001383 Py_DECREF(t);
1384 Py_DECREF(u);
1385 Py_XDECREF(v);
1386 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001387 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001388 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001389
Guido van Rossum374a9221991-04-04 10:40:29 +00001390 case DELETE_SLICE+0:
1391 case DELETE_SLICE+1:
1392 case DELETE_SLICE+2:
1393 case DELETE_SLICE+3:
1394 if ((opcode-DELETE_SLICE) & 2)
1395 w = POP();
1396 else
1397 w = NULL;
1398 if ((opcode-DELETE_SLICE) & 1)
1399 v = POP();
1400 else
1401 v = NULL;
1402 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001403 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001404 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001405 Py_DECREF(u);
1406 Py_XDECREF(v);
1407 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001408 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001409 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001412 w = TOP();
1413 v = SECOND();
1414 u = THIRD();
1415 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001416 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001417 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001418 Py_DECREF(u);
1419 Py_DECREF(v);
1420 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001421 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001425 w = TOP();
1426 v = SECOND();
1427 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001428 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001429 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001430 Py_DECREF(v);
1431 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001432 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001433 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001434
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 case PRINT_EXPR:
1436 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001437 w = PySys_GetObject("displayhook");
1438 if (w == NULL) {
1439 PyErr_SetString(PyExc_RuntimeError,
1440 "lost sys.displayhook");
1441 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001442 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001443 }
1444 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001445 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001446 if (x == NULL)
1447 err = -1;
1448 }
1449 if (err == 0) {
1450 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001451 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001452 if (w == NULL)
1453 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001454 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001455 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001456 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001458
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001459 case PRINT_ITEM_TO:
1460 w = stream = POP();
1461 /* fall through to PRINT_ITEM */
1462
Guido van Rossum374a9221991-04-04 10:40:29 +00001463 case PRINT_ITEM:
1464 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001465 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001466 w = PySys_GetObject("stdout");
1467 if (w == NULL) {
1468 PyErr_SetString(PyExc_RuntimeError,
1469 "lost sys.stdout");
1470 err = -1;
1471 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001472 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001473 /* PyFile_SoftSpace() can exececute arbitrary code
1474 if sys.stdout is an instance with a __getattr__.
1475 If __getattr__ raises an exception, w will
1476 be freed, so we need to prevent that temporarily. */
1477 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001478 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001479 err = PyFile_WriteString(" ", w);
1480 if (err == 0)
1481 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001482 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001483 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001484 if (PyString_Check(v)) {
1485 char *s = PyString_AS_STRING(v);
1486 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001487 if (len == 0 ||
1488 !isspace(Py_CHARMASK(s[len-1])) ||
1489 s[len-1] == ' ')
1490 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001491 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001492#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001493 else if (PyUnicode_Check(v)) {
1494 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1495 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001496 if (len == 0 ||
1497 !Py_UNICODE_ISSPACE(s[len-1]) ||
1498 s[len-1] == ' ')
1499 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001500 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001501#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001502 else
1503 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001504 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001505 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001506 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001507 Py_XDECREF(stream);
1508 stream = NULL;
1509 if (err == 0)
1510 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001513 case PRINT_NEWLINE_TO:
1514 w = stream = POP();
1515 /* fall through to PRINT_NEWLINE */
1516
Guido van Rossum374a9221991-04-04 10:40:29 +00001517 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001518 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001519 w = PySys_GetObject("stdout");
1520 if (w == NULL)
1521 PyErr_SetString(PyExc_RuntimeError,
1522 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001523 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001524 if (w != NULL) {
1525 err = PyFile_WriteString("\n", w);
1526 if (err == 0)
1527 PyFile_SoftSpace(w, 0);
1528 }
1529 Py_XDECREF(stream);
1530 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001531 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Thomas Wouters434d0822000-08-24 20:11:32 +00001533
1534#ifdef CASE_TOO_BIG
1535 default: switch (opcode) {
1536#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001537 case RAISE_VARARGS:
1538 u = v = w = NULL;
1539 switch (oparg) {
1540 case 3:
1541 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001542 /* Fallthrough */
1543 case 2:
1544 v = POP(); /* value */
1545 /* Fallthrough */
1546 case 1:
1547 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001548 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001549 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001550 break;
1551 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001552 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001553 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001554 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001555 break;
1556 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001557 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001560 if ((x = f->f_locals) != NULL) {
1561 Py_INCREF(x);
1562 PUSH(x);
1563 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001564 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001565 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001566 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 case RETURN_VALUE:
1569 retval = POP();
1570 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001571 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001572
Tim Peters5ca576e2001-06-18 22:08:13 +00001573 case YIELD_VALUE:
1574 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001575 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001576 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001577 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001578
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001579 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001580 w = TOP();
1581 v = SECOND();
1582 u = THIRD();
1583 STACKADJ(-3);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001584#ifdef WITH_TSC
1585 rdtscll(intr0);
1586#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001587 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001588#ifdef WITH_TSC
1589 rdtscll(intr1);
1590#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001591 Py_DECREF(u);
1592 Py_DECREF(v);
1593 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001594 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001595
Guido van Rossum374a9221991-04-04 10:40:29 +00001596 case POP_BLOCK:
1597 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001598 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001599 while (STACK_LEVEL() > b->b_level) {
1600 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001601 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001602 }
1603 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001604 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001605
Guido van Rossum374a9221991-04-04 10:40:29 +00001606 case END_FINALLY:
1607 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001608 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001609 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001610 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001611 if (why == WHY_RETURN ||
1612 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001613 retval = POP();
1614 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001615 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001617 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001618 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001620 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001621 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001622 else if (v != Py_None) {
1623 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001624 "'finally' pops bad exception");
1625 why = WHY_EXCEPTION;
1626 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001627 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001628 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Guido van Rossum374a9221991-04-04 10:40:29 +00001630 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001631 u = TOP();
1632 v = SECOND();
1633 w = THIRD();
1634 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001635 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001636 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001637 Py_DECREF(u);
1638 Py_DECREF(v);
1639 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001640 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001643 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001645 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001646 if (PyDict_CheckExact(v))
1647 err = PyDict_SetItem(x, w, v);
1648 else
1649 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001650 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001651 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001652 break;
1653 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001654 PyErr_Format(PyExc_SystemError,
1655 "no locals found when storing %s",
1656 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001658
Guido van Rossum374a9221991-04-04 10:40:29 +00001659 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001660 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001661 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001662 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001663 format_exc_check_arg(PyExc_NameError,
1664 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001665 break;
1666 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001667 PyErr_Format(PyExc_SystemError,
1668 "no locals when deleting %s",
1669 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001671
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001672 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001673 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001675 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1676 PyObject **items = ((PyTupleObject *)v)->ob_item;
1677 while (oparg--) {
1678 w = items[oparg];
1679 Py_INCREF(w);
1680 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001681 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001682 Py_DECREF(v);
1683 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001684 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1685 PyObject **items = ((PyListObject *)v)->ob_item;
1686 while (oparg--) {
1687 w = items[oparg];
1688 Py_INCREF(w);
1689 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001690 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001691 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001692 stack_pointer + oparg))
1693 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001694 else {
1695 if (PyErr_ExceptionMatches(PyExc_TypeError))
1696 PyErr_SetString(PyExc_TypeError,
1697 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001698 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001699 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001700 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Guido van Rossum374a9221991-04-04 10:40:29 +00001703 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001704 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001705 v = TOP();
1706 u = SECOND();
1707 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001708 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1709 Py_DECREF(v);
1710 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001711 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001712 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001715 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001717 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1718 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001719 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001721
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001722 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001723 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001724 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001725 err = PyDict_SetItem(f->f_globals, w, v);
1726 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001727 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001730 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001731 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001732 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001733 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001734 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001735 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001736
Guido van Rossum374a9221991-04-04 10:40:29 +00001737 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001738 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001739 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001740 PyErr_Format(PyExc_SystemError,
1741 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001742 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001743 break;
1744 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001745 if (PyDict_CheckExact(v))
1746 x = PyDict_GetItem(v, w);
1747 else {
1748 x = PyObject_GetItem(v, w);
1749 if (x == NULL && PyErr_Occurred()) {
1750 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1751 break;
1752 PyErr_Clear();
1753 }
1754 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001756 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001758 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001760 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001761 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001762 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 break;
1764 }
1765 }
1766 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001767 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001768 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001769 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001770
Guido van Rossum374a9221991-04-04 10:40:29 +00001771 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001772 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001773 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001774 /* Inline the PyDict_GetItem() calls.
1775 WARNING: this is an extreme speed hack.
1776 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001777 long hash = ((PyStringObject *)w)->ob_shash;
1778 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001779 PyDictObject *d;
1780 d = (PyDictObject *)(f->f_globals);
1781 x = d->ma_lookup(d, w, hash)->me_value;
1782 if (x != NULL) {
1783 Py_INCREF(x);
1784 PUSH(x);
1785 continue;
1786 }
1787 d = (PyDictObject *)(f->f_builtins);
1788 x = d->ma_lookup(d, w, hash)->me_value;
1789 if (x != NULL) {
1790 Py_INCREF(x);
1791 PUSH(x);
1792 continue;
1793 }
1794 goto load_global_error;
1795 }
1796 }
1797 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001798 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001799 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001800 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001801 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001802 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001803 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001804 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001805 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001806 break;
1807 }
1808 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001809 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001810 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001811 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001812
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001813 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001814 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001815 if (x != NULL) {
1816 SETLOCAL(oparg, NULL);
1817 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001818 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001819 format_exc_check_arg(
1820 PyExc_UnboundLocalError,
1821 UNBOUNDLOCAL_ERROR_MSG,
1822 PyTuple_GetItem(co->co_varnames, oparg)
1823 );
1824 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001825
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001826 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001827 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001828 Py_INCREF(x);
1829 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001830 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001831 break;
1832
1833 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001834 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001835 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001836 if (w != NULL) {
1837 PUSH(w);
1838 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001839 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001840 err = -1;
1841 /* Don't stomp existing exception */
1842 if (PyErr_Occurred())
1843 break;
1844 if (oparg < f->f_ncells) {
1845 v = PyTuple_GetItem(co->co_cellvars,
1846 oparg);
1847 format_exc_check_arg(
1848 PyExc_UnboundLocalError,
1849 UNBOUNDLOCAL_ERROR_MSG,
1850 v);
1851 } else {
1852 v = PyTuple_GetItem(
1853 co->co_freevars,
1854 oparg - f->f_ncells);
1855 format_exc_check_arg(
1856 PyExc_NameError,
1857 UNBOUNDFREE_ERROR_MSG,
1858 v);
1859 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001860 break;
1861
1862 case STORE_DEREF:
1863 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001864 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001865 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001866 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001867 continue;
1868
Guido van Rossum374a9221991-04-04 10:40:29 +00001869 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001870 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001871 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001872 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001873 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001874 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001875 }
1876 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001877 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 }
1879 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001880
Guido van Rossum374a9221991-04-04 10:40:29 +00001881 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001882 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001884 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001886 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 }
1888 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001889 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 }
1891 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001892
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001894 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001896 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001898
Guido van Rossum374a9221991-04-04 10:40:29 +00001899 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001900 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001901 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001902 x = PyObject_GetAttr(v, w);
1903 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001904 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001905 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001906 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001907
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 case COMPARE_OP:
1909 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001910 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001911 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001912 /* INLINE: cmp(int, int) */
1913 register long a, b;
1914 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001915 a = PyInt_AS_LONG(v);
1916 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001917 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001918 case PyCmp_LT: res = a < b; break;
1919 case PyCmp_LE: res = a <= b; break;
1920 case PyCmp_EQ: res = a == b; break;
1921 case PyCmp_NE: res = a != b; break;
1922 case PyCmp_GT: res = a > b; break;
1923 case PyCmp_GE: res = a >= b; break;
1924 case PyCmp_IS: res = v == w; break;
1925 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001926 default: goto slow_compare;
1927 }
1928 x = res ? Py_True : Py_False;
1929 Py_INCREF(x);
1930 }
1931 else {
1932 slow_compare:
1933 x = cmp_outcome(oparg, v, w);
1934 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001935 Py_DECREF(v);
1936 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001937 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001938 if (x == NULL) break;
1939 PREDICT(JUMP_IF_FALSE);
1940 PREDICT(JUMP_IF_TRUE);
1941 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001942
Guido van Rossum374a9221991-04-04 10:40:29 +00001943 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001944 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001945 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001946 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001947 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001948 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001949 break;
1950 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001951 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001952 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001953 w,
1954 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001955 f->f_locals == NULL ?
1956 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001957 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001958 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001959 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001960 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001961 x = NULL;
1962 break;
1963 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001964#ifdef WITH_TSC
1965 rdtscll(intr0);
1966#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001967 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001968#ifdef WITH_TSC
1969 rdtscll(intr1);
1970#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001971 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001972 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001973 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001974 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001975
Thomas Wouters52152252000-08-17 22:55:00 +00001976 case IMPORT_STAR:
1977 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001978 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001979 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001980 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001981 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001982 break;
1983 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001984#ifdef WITH_TSC
1985 rdtscll(intr0);
1986#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001987 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001988#ifdef WITH_TSC
1989 rdtscll(intr1);
1990#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001991 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001992 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001993 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001994 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001995
Thomas Wouters52152252000-08-17 22:55:00 +00001996 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001997 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001998 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001999#ifdef WITH_TSC
2000 rdtscll(intr0);
2001#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002002 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002003#ifdef WITH_TSC
2004 rdtscll(intr1);
2005#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002006 PUSH(x);
2007 if (x != NULL) continue;
2008 break;
2009
Guido van Rossum374a9221991-04-04 10:40:29 +00002010 case JUMP_FORWARD:
2011 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002012 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002013
Raymond Hettingerf606f872003-03-16 03:11:04 +00002014 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002015 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002016 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002017 if (w == Py_True) {
2018 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002019 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002020 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002021 if (w == Py_False) {
2022 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002023 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002024 }
2025 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002026 if (err > 0)
2027 err = 0;
2028 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002029 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002030 else
2031 break;
2032 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002033
Raymond Hettingerf606f872003-03-16 03:11:04 +00002034 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002035 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002036 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002037 if (w == Py_False) {
2038 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002039 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002040 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002041 if (w == Py_True) {
2042 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002043 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002044 }
2045 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002046 if (err > 0) {
2047 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002048 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002049 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002050 else if (err == 0)
2051 ;
2052 else
2053 break;
2054 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002055
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002056 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002057 case JUMP_ABSOLUTE:
2058 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002059 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002060
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002061 case GET_ITER:
2062 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002063 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002064 x = PyObject_GetIter(v);
2065 Py_DECREF(v);
2066 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002067 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002068 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002069 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002070 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002071 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002072 break;
2073
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002074 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002075 case FOR_ITER:
2076 /* before: [iter]; after: [iter, iter()] *or* [] */
2077 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002078 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002079 if (x != NULL) {
2080 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002081 PREDICT(STORE_FAST);
2082 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002083 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002085 if (PyErr_Occurred()) {
2086 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2087 break;
2088 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002089 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002090 /* iterator ended normally */
2091 x = v = POP();
2092 Py_DECREF(v);
2093 JUMPBY(oparg);
2094 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002095
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002096 case BREAK_LOOP:
2097 why = WHY_BREAK;
2098 goto fast_block_end;
2099
2100 case CONTINUE_LOOP:
2101 retval = PyInt_FromLong(oparg);
2102 why = WHY_CONTINUE;
2103 goto fast_block_end;
2104
Guido van Rossum374a9221991-04-04 10:40:29 +00002105 case SETUP_LOOP:
2106 case SETUP_EXCEPT:
2107 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002108 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002109 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002110 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002111
Guido van Rossumf10570b1995-07-07 22:53:21 +00002112 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002113 {
2114 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002115 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002116 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002117#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002118 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002119#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002120 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002121#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002122 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002123 PUSH(x);
2124 if (x != NULL)
2125 continue;
2126 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002127 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002128
Jeremy Hylton76901512000-03-28 23:49:17 +00002129 case CALL_FUNCTION_VAR:
2130 case CALL_FUNCTION_KW:
2131 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002132 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002133 int na = oparg & 0xff;
2134 int nk = (oparg>>8) & 0xff;
2135 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002136 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002137 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002138 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002139 if (flags & CALL_FLAG_VAR)
2140 n++;
2141 if (flags & CALL_FLAG_KW)
2142 n++;
2143 pfunc = stack_pointer - n - 1;
2144 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002145
Guido van Rossumac7be682001-01-17 15:42:30 +00002146 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002147 && PyMethod_GET_SELF(func) != NULL) {
2148 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002149 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002150 func = PyMethod_GET_FUNCTION(func);
2151 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002152 Py_DECREF(*pfunc);
2153 *pfunc = self;
2154 na++;
2155 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002156 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002157 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002158 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002159#ifdef WITH_TSC
2160 rdtscll(intr0);
2161#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002162 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002163#ifdef WITH_TSC
2164 rdtscll(intr1);
2165#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002166 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002167 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002168
Jeremy Hylton76901512000-03-28 23:49:17 +00002169 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002170 w = POP();
2171 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002172 }
2173 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002174 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002175 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002176 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002177 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002178
Guido van Rossum681d79a1995-07-18 14:51:37 +00002179 case MAKE_FUNCTION:
2180 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002181 x = PyFunction_New(v, f->f_globals);
2182 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002183 /* XXX Maybe this should be a separate opcode? */
2184 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002185 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002186 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002187 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002188 x = NULL;
2189 break;
2190 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002191 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002192 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002193 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002194 }
2195 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002196 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002197 }
2198 PUSH(x);
2199 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002200
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002201 case MAKE_CLOSURE:
2202 {
2203 int nfree;
2204 v = POP(); /* code object */
2205 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002206 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002207 Py_DECREF(v);
2208 /* XXX Maybe this should be a separate opcode? */
2209 if (x != NULL && nfree > 0) {
2210 v = PyTuple_New(nfree);
2211 if (v == NULL) {
2212 Py_DECREF(x);
2213 x = NULL;
2214 break;
2215 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002216 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002217 w = POP();
2218 PyTuple_SET_ITEM(v, nfree, w);
2219 }
2220 err = PyFunction_SetClosure(x, v);
2221 Py_DECREF(v);
2222 }
2223 if (x != NULL && oparg > 0) {
2224 v = PyTuple_New(oparg);
2225 if (v == NULL) {
2226 Py_DECREF(x);
2227 x = NULL;
2228 break;
2229 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002230 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002231 w = POP();
2232 PyTuple_SET_ITEM(v, oparg, w);
2233 }
2234 err = PyFunction_SetDefaults(x, v);
2235 Py_DECREF(v);
2236 }
2237 PUSH(x);
2238 break;
2239 }
2240
Guido van Rossum8861b741996-07-30 16:49:37 +00002241 case BUILD_SLICE:
2242 if (oparg == 3)
2243 w = POP();
2244 else
2245 w = NULL;
2246 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002247 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002248 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002249 Py_DECREF(u);
2250 Py_DECREF(v);
2251 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002252 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002253 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002254 break;
2255
Fred Drakeef8ace32000-08-24 00:32:09 +00002256 case EXTENDED_ARG:
2257 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002258 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002259 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002260
Guido van Rossum374a9221991-04-04 10:40:29 +00002261 default:
2262 fprintf(stderr,
2263 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002264 PyCode_Addr2Line(f->f_code, f->f_lasti),
2265 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002266 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002267 why = WHY_EXCEPTION;
2268 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002269
2270#ifdef CASE_TOO_BIG
2271 }
2272#endif
2273
Guido van Rossum374a9221991-04-04 10:40:29 +00002274 } /* switch */
2275
2276 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002277
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002278#ifdef WITH_TSC
2279 rdtscll(inst1);
2280#endif
2281
Guido van Rossum374a9221991-04-04 10:40:29 +00002282 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Guido van Rossum374a9221991-04-04 10:40:29 +00002284 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002285 if (err == 0 && x != NULL) {
2286#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002287 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002288 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002289 fprintf(stderr,
2290 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002291 else {
2292#endif
2293#ifdef WITH_TSC
2294 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002295#endif
2296 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002297#ifdef CHECKEXC
2298 }
2299#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002300 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002301 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002302 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002303 err = 0;
2304 }
2305
Guido van Rossum374a9221991-04-04 10:40:29 +00002306 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002307
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002308 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002309 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002310 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002311 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002312 why = WHY_EXCEPTION;
2313 }
2314 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002315#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002316 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002317 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002318 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002319 char buf[1024];
2320 sprintf(buf, "Stack unwind with exception "
2321 "set and why=%d", why);
2322 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002323 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 }
2325#endif
2326
2327 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002328
Guido van Rossum374a9221991-04-04 10:40:29 +00002329 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002330 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002331
Fred Drake8f51f542001-10-04 14:48:42 +00002332 if (tstate->c_tracefunc != NULL)
2333 call_exc_trace(tstate->c_tracefunc,
2334 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002335 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002336
Guido van Rossum374a9221991-04-04 10:40:29 +00002337 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002338
Guido van Rossum374a9221991-04-04 10:40:29 +00002339 if (why == WHY_RERAISE)
2340 why = WHY_EXCEPTION;
2341
2342 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002343
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002344fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002345 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002346 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002347
Tim Peters8a5c3c72004-04-05 19:36:21 +00002348 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002349 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2350 /* For a continue inside a try block,
2351 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002352 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2353 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002354 why = WHY_NOT;
2355 JUMPTO(PyInt_AS_LONG(retval));
2356 Py_DECREF(retval);
2357 break;
2358 }
2359
Guido van Rossum374a9221991-04-04 10:40:29 +00002360 while (STACK_LEVEL() > b->b_level) {
2361 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002362 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 }
2364 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2365 why = WHY_NOT;
2366 JUMPTO(b->b_handler);
2367 break;
2368 }
2369 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002370 (b->b_type == SETUP_EXCEPT &&
2371 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002372 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002373 PyObject *exc, *val, *tb;
2374 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002375 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002376 val = Py_None;
2377 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002378 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002379 /* Make the raw exception data
2380 available to the handler,
2381 so a program can emulate the
2382 Python main loop. Don't do
2383 this for 'finally'. */
2384 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002385 PyErr_NormalizeException(
2386 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002387 set_exc_info(tstate,
2388 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002389 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002390 if (tb == NULL) {
2391 Py_INCREF(Py_None);
2392 PUSH(Py_None);
2393 } else
2394 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002395 PUSH(val);
2396 PUSH(exc);
2397 }
2398 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002399 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002400 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002401 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002402 PUSH(v);
2403 }
2404 why = WHY_NOT;
2405 JUMPTO(b->b_handler);
2406 break;
2407 }
2408 } /* unwind stack */
2409
2410 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002411
Guido van Rossum374a9221991-04-04 10:40:29 +00002412 if (why != WHY_NOT)
2413 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002414#ifdef WITH_TSC
2415 rdtscll(loop1);
2416#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002417
Guido van Rossum374a9221991-04-04 10:40:29 +00002418 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002419
Tim Peters8a5c3c72004-04-05 19:36:21 +00002420 assert(why != WHY_YIELD);
2421 /* Pop remaining stack entries. */
2422 while (!EMPTY()) {
2423 v = POP();
2424 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002425 }
2426
Tim Peters8a5c3c72004-04-05 19:36:21 +00002427 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002428 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002429
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002430fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002431 if (tstate->use_tracing) {
2432 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002433 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002434 if (call_trace(tstate->c_tracefunc,
2435 tstate->c_traceobj, f,
2436 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002437 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002438 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002439 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002440 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002441 }
Fred Drake8f51f542001-10-04 14:48:42 +00002442 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002443 if (why == WHY_EXCEPTION)
2444 call_trace_protected(tstate->c_profilefunc,
2445 tstate->c_profileobj, f,
2446 PyTrace_RETURN);
2447 else if (call_trace(tstate->c_profilefunc,
2448 tstate->c_profileobj, f,
2449 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002450 Py_XDECREF(retval);
2451 retval = NULL;
2452 why = WHY_EXCEPTION;
2453 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002454 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002455 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002456
Guido van Rossuma027efa1997-05-05 20:56:21 +00002457 reset_exc_info(tstate);
2458
Tim Peters5ca576e2001-06-18 22:08:13 +00002459 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002460 exit_eval_frame:
2461 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002462 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002463
Guido van Rossum96a42c81992-01-12 02:29:51 +00002464 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002465}
2466
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002467/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002468 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2469 the test in the if statement in Misc/gdbinit:ppystack */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002470
Tim Peters6d6c1a32001-08-02 04:15:00 +00002471PyObject *
2472PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002473 PyObject **args, int argcount, PyObject **kws, int kwcount,
2474 PyObject **defs, int defcount, PyObject *closure)
2475{
2476 register PyFrameObject *f;
2477 register PyObject *retval = NULL;
2478 register PyObject **fastlocals, **freevars;
2479 PyThreadState *tstate = PyThreadState_GET();
2480 PyObject *x, *u;
2481
2482 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002483 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002484 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002485 return NULL;
2486 }
2487
Jeremy Hylton985eba52003-02-05 23:13:00 +00002488 assert(globals != NULL);
2489 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002490 if (f == NULL)
2491 return NULL;
2492
2493 fastlocals = f->f_localsplus;
2494 freevars = f->f_localsplus + f->f_nlocals;
2495
2496 if (co->co_argcount > 0 ||
2497 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2498 int i;
2499 int n = argcount;
2500 PyObject *kwdict = NULL;
2501 if (co->co_flags & CO_VARKEYWORDS) {
2502 kwdict = PyDict_New();
2503 if (kwdict == NULL)
2504 goto fail;
2505 i = co->co_argcount;
2506 if (co->co_flags & CO_VARARGS)
2507 i++;
2508 SETLOCAL(i, kwdict);
2509 }
2510 if (argcount > co->co_argcount) {
2511 if (!(co->co_flags & CO_VARARGS)) {
2512 PyErr_Format(PyExc_TypeError,
2513 "%.200s() takes %s %d "
2514 "%sargument%s (%d given)",
2515 PyString_AsString(co->co_name),
2516 defcount ? "at most" : "exactly",
2517 co->co_argcount,
2518 kwcount ? "non-keyword " : "",
2519 co->co_argcount == 1 ? "" : "s",
2520 argcount);
2521 goto fail;
2522 }
2523 n = co->co_argcount;
2524 }
2525 for (i = 0; i < n; i++) {
2526 x = args[i];
2527 Py_INCREF(x);
2528 SETLOCAL(i, x);
2529 }
2530 if (co->co_flags & CO_VARARGS) {
2531 u = PyTuple_New(argcount - n);
2532 if (u == NULL)
2533 goto fail;
2534 SETLOCAL(co->co_argcount, u);
2535 for (i = n; i < argcount; i++) {
2536 x = args[i];
2537 Py_INCREF(x);
2538 PyTuple_SET_ITEM(u, i-n, x);
2539 }
2540 }
2541 for (i = 0; i < kwcount; i++) {
2542 PyObject *keyword = kws[2*i];
2543 PyObject *value = kws[2*i + 1];
2544 int j;
2545 if (keyword == NULL || !PyString_Check(keyword)) {
2546 PyErr_Format(PyExc_TypeError,
2547 "%.200s() keywords must be strings",
2548 PyString_AsString(co->co_name));
2549 goto fail;
2550 }
2551 /* XXX slow -- speed up using dictionary? */
2552 for (j = 0; j < co->co_argcount; j++) {
2553 PyObject *nm = PyTuple_GET_ITEM(
2554 co->co_varnames, j);
2555 int cmp = PyObject_RichCompareBool(
2556 keyword, nm, Py_EQ);
2557 if (cmp > 0)
2558 break;
2559 else if (cmp < 0)
2560 goto fail;
2561 }
2562 /* Check errors from Compare */
2563 if (PyErr_Occurred())
2564 goto fail;
2565 if (j >= co->co_argcount) {
2566 if (kwdict == NULL) {
2567 PyErr_Format(PyExc_TypeError,
2568 "%.200s() got an unexpected "
2569 "keyword argument '%.400s'",
2570 PyString_AsString(co->co_name),
2571 PyString_AsString(keyword));
2572 goto fail;
2573 }
2574 PyDict_SetItem(kwdict, keyword, value);
2575 }
2576 else {
2577 if (GETLOCAL(j) != NULL) {
2578 PyErr_Format(PyExc_TypeError,
2579 "%.200s() got multiple "
2580 "values for keyword "
2581 "argument '%.400s'",
2582 PyString_AsString(co->co_name),
2583 PyString_AsString(keyword));
2584 goto fail;
2585 }
2586 Py_INCREF(value);
2587 SETLOCAL(j, value);
2588 }
2589 }
2590 if (argcount < co->co_argcount) {
2591 int m = co->co_argcount - defcount;
2592 for (i = argcount; i < m; i++) {
2593 if (GETLOCAL(i) == NULL) {
2594 PyErr_Format(PyExc_TypeError,
2595 "%.200s() takes %s %d "
2596 "%sargument%s (%d given)",
2597 PyString_AsString(co->co_name),
2598 ((co->co_flags & CO_VARARGS) ||
2599 defcount) ? "at least"
2600 : "exactly",
2601 m, kwcount ? "non-keyword " : "",
2602 m == 1 ? "" : "s", i);
2603 goto fail;
2604 }
2605 }
2606 if (n > m)
2607 i = n - m;
2608 else
2609 i = 0;
2610 for (; i < defcount; i++) {
2611 if (GETLOCAL(m+i) == NULL) {
2612 PyObject *def = defs[i];
2613 Py_INCREF(def);
2614 SETLOCAL(m+i, def);
2615 }
2616 }
2617 }
2618 }
2619 else {
2620 if (argcount > 0 || kwcount > 0) {
2621 PyErr_Format(PyExc_TypeError,
2622 "%.200s() takes no arguments (%d given)",
2623 PyString_AsString(co->co_name),
2624 argcount + kwcount);
2625 goto fail;
2626 }
2627 }
2628 /* Allocate and initialize storage for cell vars, and copy free
2629 vars into frame. This isn't too efficient right now. */
2630 if (f->f_ncells) {
2631 int i = 0, j = 0, nargs, found;
2632 char *cellname, *argname;
2633 PyObject *c;
2634
2635 nargs = co->co_argcount;
2636 if (co->co_flags & CO_VARARGS)
2637 nargs++;
2638 if (co->co_flags & CO_VARKEYWORDS)
2639 nargs++;
2640
2641 /* Check for cells that shadow args */
2642 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2643 cellname = PyString_AS_STRING(
2644 PyTuple_GET_ITEM(co->co_cellvars, i));
2645 found = 0;
2646 while (j < nargs) {
2647 argname = PyString_AS_STRING(
2648 PyTuple_GET_ITEM(co->co_varnames, j));
2649 if (strcmp(cellname, argname) == 0) {
2650 c = PyCell_New(GETLOCAL(j));
2651 if (c == NULL)
2652 goto fail;
2653 GETLOCAL(f->f_nlocals + i) = c;
2654 found = 1;
2655 break;
2656 }
2657 j++;
2658 }
2659 if (found == 0) {
2660 c = PyCell_New(NULL);
2661 if (c == NULL)
2662 goto fail;
2663 SETLOCAL(f->f_nlocals + i, c);
2664 }
2665 }
2666 /* Initialize any that are left */
2667 while (i < f->f_ncells) {
2668 c = PyCell_New(NULL);
2669 if (c == NULL)
2670 goto fail;
2671 SETLOCAL(f->f_nlocals + i, c);
2672 i++;
2673 }
2674 }
2675 if (f->f_nfreevars) {
2676 int i;
2677 for (i = 0; i < f->f_nfreevars; ++i) {
2678 PyObject *o = PyTuple_GET_ITEM(closure, i);
2679 Py_INCREF(o);
2680 freevars[f->f_ncells + i] = o;
2681 }
2682 }
2683
Tim Peters5ca576e2001-06-18 22:08:13 +00002684 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002685 /* Don't need to keep the reference to f_back, it will be set
2686 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002687 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002688 f->f_back = NULL;
2689
Jeremy Hylton985eba52003-02-05 23:13:00 +00002690 PCALL(PCALL_GENERATOR);
2691
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002692 /* Create a new generator that owns the ready to run frame
2693 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002694 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002695 }
2696
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002697 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002698
2699 fail: /* Jump here from prelude on failure */
2700
Tim Petersb13680b2001-11-27 23:29:29 +00002701 /* decref'ing the frame can cause __del__ methods to get invoked,
2702 which can call back into Python. While we're done with the
2703 current Python frame (f), the associated C stack is still in use,
2704 so recursion_depth must be boosted for the duration.
2705 */
2706 assert(tstate != NULL);
2707 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002708 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002709 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002710 return retval;
2711}
2712
2713
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002714/* Implementation notes for set_exc_info() and reset_exc_info():
2715
2716- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2717 'exc_traceback'. These always travel together.
2718
2719- tstate->curexc_ZZZ is the "hot" exception that is set by
2720 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2721
2722- Once an exception is caught by an except clause, it is transferred
2723 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2724 can pick it up. This is the primary task of set_exc_info().
2725
2726- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2727
2728 Long ago, when none of this existed, there were just a few globals:
2729 one set corresponding to the "hot" exception, and one set
2730 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2731 globals; they were simply stored as sys.exc_ZZZ. For backwards
2732 compatibility, they still are!) The problem was that in code like
2733 this:
2734
2735 try:
2736 "something that may fail"
2737 except "some exception":
2738 "do something else first"
2739 "print the exception from sys.exc_ZZZ."
2740
2741 if "do something else first" invoked something that raised and caught
2742 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2743 cause of subtle bugs. I fixed this by changing the semantics as
2744 follows:
2745
2746 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2747 *in that frame*.
2748
2749 - But initially, and as long as no exception is caught in a given
2750 frame, sys.exc_ZZZ will hold the last exception caught in the
2751 previous frame (or the frame before that, etc.).
2752
2753 The first bullet fixed the bug in the above example. The second
2754 bullet was for backwards compatibility: it was (and is) common to
2755 have a function that is called when an exception is caught, and to
2756 have that function access the caught exception via sys.exc_ZZZ.
2757 (Example: traceback.print_exc()).
2758
2759 At the same time I fixed the problem that sys.exc_ZZZ weren't
2760 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2761 but that's really a separate improvement.
2762
2763 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2764 variables to what they were before the current frame was called. The
2765 set_exc_info() function saves them on the frame so that
2766 reset_exc_info() can restore them. The invariant is that
2767 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2768 exception (where "catching" an exception applies only to successful
2769 except clauses); and if the current frame ever caught an exception,
2770 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2771 at the start of the current frame.
2772
2773*/
2774
Guido van Rossuma027efa1997-05-05 20:56:21 +00002775static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002776set_exc_info(PyThreadState *tstate,
2777 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002778{
2779 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002780 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002781
Guido van Rossuma027efa1997-05-05 20:56:21 +00002782 frame = tstate->frame;
2783 if (frame->f_exc_type == NULL) {
2784 /* This frame didn't catch an exception before */
2785 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002786 if (tstate->exc_type == NULL) {
2787 Py_INCREF(Py_None);
2788 tstate->exc_type = Py_None;
2789 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002790 tmp_type = frame->f_exc_type;
2791 tmp_value = frame->f_exc_value;
2792 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002793 Py_XINCREF(tstate->exc_type);
2794 Py_XINCREF(tstate->exc_value);
2795 Py_XINCREF(tstate->exc_traceback);
2796 frame->f_exc_type = tstate->exc_type;
2797 frame->f_exc_value = tstate->exc_value;
2798 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002799 Py_XDECREF(tmp_type);
2800 Py_XDECREF(tmp_value);
2801 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002802 }
2803 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002804 tmp_type = tstate->exc_type;
2805 tmp_value = tstate->exc_value;
2806 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002807 Py_XINCREF(type);
2808 Py_XINCREF(value);
2809 Py_XINCREF(tb);
2810 tstate->exc_type = type;
2811 tstate->exc_value = value;
2812 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002813 Py_XDECREF(tmp_type);
2814 Py_XDECREF(tmp_value);
2815 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002816 /* For b/w compatibility */
2817 PySys_SetObject("exc_type", type);
2818 PySys_SetObject("exc_value", value);
2819 PySys_SetObject("exc_traceback", tb);
2820}
2821
2822static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002823reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002824{
2825 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002826 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002827 frame = tstate->frame;
2828 if (frame->f_exc_type != NULL) {
2829 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002830 tmp_type = tstate->exc_type;
2831 tmp_value = tstate->exc_value;
2832 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002833 Py_XINCREF(frame->f_exc_type);
2834 Py_XINCREF(frame->f_exc_value);
2835 Py_XINCREF(frame->f_exc_traceback);
2836 tstate->exc_type = frame->f_exc_type;
2837 tstate->exc_value = frame->f_exc_value;
2838 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002839 Py_XDECREF(tmp_type);
2840 Py_XDECREF(tmp_value);
2841 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002842 /* For b/w compatibility */
2843 PySys_SetObject("exc_type", frame->f_exc_type);
2844 PySys_SetObject("exc_value", frame->f_exc_value);
2845 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2846 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002847 tmp_type = frame->f_exc_type;
2848 tmp_value = frame->f_exc_value;
2849 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002850 frame->f_exc_type = NULL;
2851 frame->f_exc_value = NULL;
2852 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002853 Py_XDECREF(tmp_type);
2854 Py_XDECREF(tmp_value);
2855 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002856}
2857
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002858/* Logic for the raise statement (too complicated for inlining).
2859 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002860static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002861do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002862{
Guido van Rossumd295f121998-04-09 21:39:57 +00002863 if (type == NULL) {
2864 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002865 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002866 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2867 value = tstate->exc_value;
2868 tb = tstate->exc_traceback;
2869 Py_XINCREF(type);
2870 Py_XINCREF(value);
2871 Py_XINCREF(tb);
2872 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002873
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002874 /* We support the following forms of raise:
2875 raise <class>, <classinstance>
2876 raise <class>, <argument tuple>
2877 raise <class>, None
2878 raise <class>, <argument>
2879 raise <classinstance>, None
2880 raise <string>, <object>
2881 raise <string>, None
2882
2883 An omitted second argument is the same as None.
2884
2885 In addition, raise <tuple>, <anything> is the same as
2886 raising the tuple's first item (and it better have one!);
2887 this rule is applied recursively.
2888
2889 Finally, an optional third argument can be supplied, which
2890 gives the traceback to be substituted (useful when
2891 re-raising an exception after examining it). */
2892
2893 /* First, check the traceback argument, replacing None with
2894 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002895 if (tb == Py_None) {
2896 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002897 tb = NULL;
2898 }
2899 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002900 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002901 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002902 goto raise_error;
2903 }
2904
2905 /* Next, replace a missing value with None */
2906 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002907 value = Py_None;
2908 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002909 }
2910
2911 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002912 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2913 PyObject *tmp = type;
2914 type = PyTuple_GET_ITEM(type, 0);
2915 Py_INCREF(type);
2916 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002917 }
2918
Tim Petersafb2c802002-04-18 18:06:20 +00002919 if (PyString_CheckExact(type))
2920 /* Raising builtin string is deprecated but still allowed --
2921 * do nothing. Raising an instance of a new-style str
2922 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002923 PyErr_Warn(PyExc_PendingDeprecationWarning,
2924 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002925
2926 else if (PyClass_Check(type))
2927 PyErr_NormalizeException(&type, &value, &tb);
2928
Guido van Rossumb209a111997-04-29 18:18:01 +00002929 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002930 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002931 if (value != Py_None) {
2932 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002933 "instance exception may not have a separate value");
2934 goto raise_error;
2935 }
2936 else {
2937 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002938 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002939 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002940 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2941 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002942 }
2943 }
2944 else {
2945 /* Not something you can raise. You get an exception
2946 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002947 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002948 "exceptions must be classes, instances, or "
2949 "strings (deprecated), not %s",
2950 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002951 goto raise_error;
2952 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002953 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002954 if (tb == NULL)
2955 return WHY_EXCEPTION;
2956 else
2957 return WHY_RERAISE;
2958 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002959 Py_XDECREF(value);
2960 Py_XDECREF(type);
2961 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002962 return WHY_EXCEPTION;
2963}
2964
Tim Petersd6d010b2001-06-21 02:49:55 +00002965/* Iterate v argcnt times and store the results on the stack (via decreasing
2966 sp). Return 1 for success, 0 if error. */
2967
Barry Warsawe42b18f1997-08-25 22:13:04 +00002968static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002969unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002970{
Tim Petersd6d010b2001-06-21 02:49:55 +00002971 int i = 0;
2972 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002973 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002974
Tim Petersd6d010b2001-06-21 02:49:55 +00002975 assert(v != NULL);
2976
2977 it = PyObject_GetIter(v);
2978 if (it == NULL)
2979 goto Error;
2980
2981 for (; i < argcnt; i++) {
2982 w = PyIter_Next(it);
2983 if (w == NULL) {
2984 /* Iterator done, via error or exhaustion. */
2985 if (!PyErr_Occurred()) {
2986 PyErr_Format(PyExc_ValueError,
2987 "need more than %d value%s to unpack",
2988 i, i == 1 ? "" : "s");
2989 }
2990 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002991 }
2992 *--sp = w;
2993 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002994
2995 /* We better have exhausted the iterator now. */
2996 w = PyIter_Next(it);
2997 if (w == NULL) {
2998 if (PyErr_Occurred())
2999 goto Error;
3000 Py_DECREF(it);
3001 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003002 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003003 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003004 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003005 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003006Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003007 for (; i > 0; i--, sp++)
3008 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003009 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003010 return 0;
3011}
3012
3013
Guido van Rossum96a42c81992-01-12 02:29:51 +00003014#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003015static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003016prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003017{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003018 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003019 if (PyObject_Print(v, stdout, 0) != 0)
3020 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003021 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003022 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003023}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003024#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003025
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003026static void
Fred Drake5755ce62001-06-27 19:19:46 +00003027call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003028{
Guido van Rossumb209a111997-04-29 18:18:01 +00003029 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003030 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003031 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003032 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003033 value = Py_None;
3034 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003035 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003036 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003037 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003038 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003039 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003040 }
Fred Drake5755ce62001-06-27 19:19:46 +00003041 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003042 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003043 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003044 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003045 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003046 Py_XDECREF(type);
3047 Py_XDECREF(value);
3048 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003049 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003050}
3051
Fred Drake4ec5d562001-10-04 19:26:43 +00003052static void
3053call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3054 int what)
3055{
3056 PyObject *type, *value, *traceback;
3057 int err;
3058 PyErr_Fetch(&type, &value, &traceback);
3059 err = call_trace(func, obj, frame, what, NULL);
3060 if (err == 0)
3061 PyErr_Restore(type, value, traceback);
3062 else {
3063 Py_XDECREF(type);
3064 Py_XDECREF(value);
3065 Py_XDECREF(traceback);
3066 }
3067}
3068
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003069static int
Fred Drake5755ce62001-06-27 19:19:46 +00003070call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3071 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003072{
Fred Drake5755ce62001-06-27 19:19:46 +00003073 register PyThreadState *tstate = frame->f_tstate;
3074 int result;
3075 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003076 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003077 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003078 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003079 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003080 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3081 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003082 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003083 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003084}
3085
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003086PyObject *
3087_PyEval_CallTracing(PyObject *func, PyObject *args)
3088{
3089 PyFrameObject *frame = PyEval_GetFrame();
3090 PyThreadState *tstate = frame->f_tstate;
3091 int save_tracing = tstate->tracing;
3092 int save_use_tracing = tstate->use_tracing;
3093 PyObject *result;
3094
3095 tstate->tracing = 0;
3096 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3097 || (tstate->c_profilefunc != NULL));
3098 result = PyObject_Call(func, args, NULL);
3099 tstate->tracing = save_tracing;
3100 tstate->use_tracing = save_use_tracing;
3101 return result;
3102}
3103
Michael W. Hudson006c7522002-11-08 13:08:46 +00003104static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003105maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003106 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3107 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003108{
3109 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003110
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003111 In a nutshell, we use the co_lnotab field of the code object
3112 to tell when execution has moved onto a different line.
3113
3114 As mentioned above, the basic idea is so set things up so
3115 that
3116
3117 *instr_lb <= frame->f_lasti < *instr_ub
3118
3119 is true so long as execution does not change lines.
3120
3121 This is all fairly simple. Digging the information out of
3122 co_lnotab takes some work, but is conceptually clear.
3123
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003124 Somewhat harder to explain is why we don't *always* call the
3125 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003126
3127 Consider this code:
3128
3129 1: def f(a):
3130 2: if a:
3131 3: print 1
3132 4: else:
3133 5: print 2
3134
3135 which compiles to this:
3136
3137 2 0 LOAD_FAST 0 (a)
3138 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003139 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003140
3141 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003142 10 PRINT_ITEM
3143 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003144 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003145 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003146
3147 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003148 19 PRINT_ITEM
3149 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003150 >> 21 LOAD_CONST 0 (None)
3151 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003152
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003153 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003154 15 and the co_lnotab will claim that execution has moved to
3155 line 3. This is at best misleading. In this case we could
3156 associate the POP_TOP with line 4, but that doesn't make
3157 sense in all cases (I think).
3158
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003159 What we do is only call the line trace function if the co_lnotab
3160 indicates we have jumped to the *start* of a line, i.e. if the
3161 current instruction offset matches the offset given for the
3162 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003163
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003164 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003165 Execution will jump from instruction offset 12 to offset 21.
3166 Then the co_lnotab would imply that execution has moved to line
3167 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003168
3169 Why do we set f_lineno when tracing? Well, consider the code
3170 above when 'a' is true. If stepping through this with 'n' in
3171 pdb, you would stop at line 1 with a "call" type event, then
3172 line events on lines 2 and 3, then a "return" type event -- but
3173 you would be shown line 5 during this event. This is a change
3174 from the behaviour in 2.2 and before, and I've found it
3175 confusing in practice. By setting and using f_lineno when
3176 tracing, one can report a line number different from that
3177 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003178 */
3179
Michael W. Hudson006c7522002-11-08 13:08:46 +00003180 int result = 0;
3181
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003182 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003183 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003184 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003185 unsigned char* p;
3186
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003187 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3188 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003189
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003190 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003191 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003192
3193 /* possible optimization: if f->f_lasti == instr_ub
3194 (likely to be a common case) then we already know
3195 instr_lb -- if we stored the matching value of p
3196 somwhere we could skip the first while loop. */
3197
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003198 /* see comments in compile.c for the description of
3199 co_lnotab. A point to remember: increments to p
3200 should come in pairs -- although we don't care about
3201 the line increments here, treating them as byte
3202 increments gets confusing, to say the least. */
3203
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003204 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003205 if (addr + *p > frame->f_lasti)
3206 break;
3207 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003208 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003209 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003210 --size;
3211 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003212
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003213 if (addr == frame->f_lasti) {
3214 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003215 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003216 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003217 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003218
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003219 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003220 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003221 addr += *p++;
3222 if (*p++)
3223 break;
3224 }
3225 *instr_ub = addr;
3226 }
3227 else {
3228 *instr_ub = INT_MAX;
3229 }
3230 }
Armin Rigobf57a142004-03-22 19:24:58 +00003231 else if (frame->f_lasti <= *instr_prev) {
3232 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003233 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003234 PyTrace_LINE, Py_None);
3235 }
3236 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003237 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003238}
3239
Fred Drake5755ce62001-06-27 19:19:46 +00003240void
3241PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003242{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003243 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003244 PyObject *temp = tstate->c_profileobj;
3245 Py_XINCREF(arg);
3246 tstate->c_profilefunc = NULL;
3247 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003248 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003249 Py_XDECREF(temp);
3250 tstate->c_profilefunc = func;
3251 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003252 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003253}
3254
3255void
3256PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3257{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003258 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003259 PyObject *temp = tstate->c_traceobj;
3260 Py_XINCREF(arg);
3261 tstate->c_tracefunc = NULL;
3262 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003263 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003264 Py_XDECREF(temp);
3265 tstate->c_tracefunc = func;
3266 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003267 tstate->use_tracing = ((func != NULL)
3268 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003269}
3270
Guido van Rossumb209a111997-04-29 18:18:01 +00003271PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003272PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003273{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003274 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003275 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003276 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003277 else
3278 return current_frame->f_builtins;
3279}
3280
Guido van Rossumb209a111997-04-29 18:18:01 +00003281PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003282PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003283{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003284 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003285 if (current_frame == NULL)
3286 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003287 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003288 return current_frame->f_locals;
3289}
3290
Guido van Rossumb209a111997-04-29 18:18:01 +00003291PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003292PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003293{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003294 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003295 if (current_frame == NULL)
3296 return NULL;
3297 else
3298 return current_frame->f_globals;
3299}
3300
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003301PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003302PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003303{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003304 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003305 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003306}
3307
Guido van Rossum6135a871995-01-09 17:53:26 +00003308int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003309PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003310{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003311 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003312 return current_frame == NULL ? 0 : current_frame->f_restricted;
3313}
3314
Guido van Rossumbe270261997-05-22 22:26:18 +00003315int
Tim Peters5ba58662001-07-16 02:29:45 +00003316PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003317{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003318 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003319 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003320
3321 if (current_frame != NULL) {
3322 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003323 const int compilerflags = codeflags & PyCF_MASK;
3324 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003325 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003326 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003327 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003328#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003329 if (codeflags & CO_GENERATOR_ALLOWED) {
3330 result = 1;
3331 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3332 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003333#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003334 }
3335 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003336}
3337
3338int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003339Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003340{
Guido van Rossumb209a111997-04-29 18:18:01 +00003341 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003342 if (f == NULL)
3343 return 0;
3344 if (!PyFile_SoftSpace(f, 0))
3345 return 0;
3346 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003347}
3348
Guido van Rossum3f5da241990-12-20 15:06:42 +00003349
Guido van Rossum681d79a1995-07-18 14:51:37 +00003350/* External interface to call any callable object.
3351 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003352
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003353#undef PyEval_CallObject
3354/* for backward compatibility: export this interface */
3355
Guido van Rossumb209a111997-04-29 18:18:01 +00003356PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003357PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003358{
Guido van Rossumb209a111997-04-29 18:18:01 +00003359 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003360}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003361#define PyEval_CallObject(func,arg) \
3362 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003363
Guido van Rossumb209a111997-04-29 18:18:01 +00003364PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003365PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003366{
Jeremy Hylton52820442001-01-03 23:52:36 +00003367 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003368
3369 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003370 arg = PyTuple_New(0);
3371 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003372 PyErr_SetString(PyExc_TypeError,
3373 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003374 return NULL;
3375 }
3376 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003377 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003378
Guido van Rossumb209a111997-04-29 18:18:01 +00003379 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003380 PyErr_SetString(PyExc_TypeError,
3381 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003382 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003383 return NULL;
3384 }
3385
Tim Peters6d6c1a32001-08-02 04:15:00 +00003386 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003387 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003388 return result;
3389}
3390
Tim Peters6d6c1a32001-08-02 04:15:00 +00003391char *
3392PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003393{
3394 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003395 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003396 else if (PyFunction_Check(func))
3397 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3398 else if (PyCFunction_Check(func))
3399 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3400 else if (PyClass_Check(func))
3401 return PyString_AsString(((PyClassObject*)func)->cl_name);
3402 else if (PyInstance_Check(func)) {
3403 return PyString_AsString(
3404 ((PyInstanceObject*)func)->in_class->cl_name);
3405 } else {
3406 return func->ob_type->tp_name;
3407 }
3408}
3409
Tim Peters6d6c1a32001-08-02 04:15:00 +00003410char *
3411PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003412{
3413 if (PyMethod_Check(func))
3414 return "()";
3415 else if (PyFunction_Check(func))
3416 return "()";
3417 else if (PyCFunction_Check(func))
3418 return "()";
3419 else if (PyClass_Check(func))
3420 return " constructor";
3421 else if (PyInstance_Check(func)) {
3422 return " instance";
3423 } else {
3424 return " object";
3425 }
3426}
3427
Jeremy Hylton52820442001-01-03 23:52:36 +00003428#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3429
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003430static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003431err_args(PyObject *func, int flags, int nargs)
3432{
3433 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003434 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003435 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003436 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003437 nargs);
3438 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003439 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003440 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003441 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003442 nargs);
3443}
3444
Nicholas Bastind858a772004-06-25 23:31:06 +00003445#define C_TRACE(call) \
3446if (tstate->use_tracing && tstate->c_profilefunc) { \
3447 if (call_trace(tstate->c_profilefunc, \
3448 tstate->c_profileobj, \
3449 tstate->frame, PyTrace_C_CALL, \
3450 func)) \
3451 { return NULL; } \
3452 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003453 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003454 if (x == NULL) { \
3455 if (call_trace (tstate->c_profilefunc, \
3456 tstate->c_profileobj, \
3457 tstate->frame, PyTrace_C_EXCEPTION, \
3458 func)) \
3459 { return NULL; } \
3460 } else { \
3461 if (call_trace(tstate->c_profilefunc, \
3462 tstate->c_profileobj, \
3463 tstate->frame, PyTrace_C_RETURN, \
3464 func)) \
3465 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003466 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003467 } \
3468} else { \
3469 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003470 }
3471
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003472static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003473call_function(PyObject ***pp_stack, int oparg
3474#ifdef WITH_TSC
3475 , uint64* pintr0, uint64* pintr1
3476#endif
3477 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003478{
3479 int na = oparg & 0xff;
3480 int nk = (oparg>>8) & 0xff;
3481 int n = na + 2 * nk;
3482 PyObject **pfunc = (*pp_stack) - n - 1;
3483 PyObject *func = *pfunc;
3484 PyObject *x, *w;
3485
Jeremy Hylton985eba52003-02-05 23:13:00 +00003486 /* Always dispatch PyCFunction first, because these are
3487 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003488 */
3489 if (PyCFunction_Check(func) && nk == 0) {
3490 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003491 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003492
3493 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003494 if (flags & (METH_NOARGS | METH_O)) {
3495 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3496 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003497 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003498 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003499 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003500 else if (flags & METH_O && na == 1) {
3501 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003502 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003503 Py_DECREF(arg);
3504 }
3505 else {
3506 err_args(func, flags, na);
3507 x = NULL;
3508 }
3509 }
3510 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003511 PyObject *callargs;
3512 callargs = load_args(pp_stack, na);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003513#ifdef WITH_TSC
3514 rdtscll(*pintr0);
3515#endif
Nicholas Bastind858a772004-06-25 23:31:06 +00003516 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003517#ifdef WITH_TSC
3518 rdtscll(*pintr1);
3519#endif
Tim Peters8a5c3c72004-04-05 19:36:21 +00003520 Py_XDECREF(callargs);
3521 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003522 } else {
3523 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3524 /* optimize access to bound methods */
3525 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003526 PCALL(PCALL_METHOD);
3527 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003528 Py_INCREF(self);
3529 func = PyMethod_GET_FUNCTION(func);
3530 Py_INCREF(func);
3531 Py_DECREF(*pfunc);
3532 *pfunc = self;
3533 na++;
3534 n++;
3535 } else
3536 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003537#ifdef WITH_TSC
3538 rdtscll(*pintr0);
3539#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003540 if (PyFunction_Check(func))
3541 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003542 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003543 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003544#ifdef WITH_TSC
3545 rdtscll(*pintr1);
3546#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003547 Py_DECREF(func);
3548 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003549
Jeremy Hylton985eba52003-02-05 23:13:00 +00003550 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003551 while ((*pp_stack) > pfunc) {
3552 w = EXT_POP(*pp_stack);
3553 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003554 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003555 }
3556 return x;
3557}
3558
Jeremy Hylton192690e2002-08-16 18:36:11 +00003559/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003560 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003561 For the simplest case -- a function that takes only positional
3562 arguments and is called with only positional arguments -- it
3563 inlines the most primitive frame setup code from
3564 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3565 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003566*/
3567
3568static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003569fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003570{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003571 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003572 PyObject *globals = PyFunction_GET_GLOBALS(func);
3573 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3574 PyObject **d = NULL;
3575 int nd = 0;
3576
Jeremy Hylton985eba52003-02-05 23:13:00 +00003577 PCALL(PCALL_FUNCTION);
3578 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003579 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003580 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3581 PyFrameObject *f;
3582 PyObject *retval = NULL;
3583 PyThreadState *tstate = PyThreadState_GET();
3584 PyObject **fastlocals, **stack;
3585 int i;
3586
3587 PCALL(PCALL_FASTER_FUNCTION);
3588 assert(globals != NULL);
3589 /* XXX Perhaps we should create a specialized
3590 PyFrame_New() that doesn't take locals, but does
3591 take builtins without sanity checking them.
3592 */
3593 f = PyFrame_New(tstate, co, globals, NULL);
3594 if (f == NULL)
3595 return NULL;
3596
3597 fastlocals = f->f_localsplus;
3598 stack = (*pp_stack) - n;
3599
3600 for (i = 0; i < n; i++) {
3601 Py_INCREF(*stack);
3602 fastlocals[i] = *stack++;
3603 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003604 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003605 assert(tstate != NULL);
3606 ++tstate->recursion_depth;
3607 Py_DECREF(f);
3608 --tstate->recursion_depth;
3609 return retval;
3610 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003611 if (argdefs != NULL) {
3612 d = &PyTuple_GET_ITEM(argdefs, 0);
3613 nd = ((PyTupleObject *)argdefs)->ob_size;
3614 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003615 return PyEval_EvalCodeEx(co, globals,
3616 (PyObject *)NULL, (*pp_stack)-n, na,
3617 (*pp_stack)-2*nk, nk, d, nd,
3618 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003619}
3620
3621static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003622update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3623 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003624{
3625 PyObject *kwdict = NULL;
3626 if (orig_kwdict == NULL)
3627 kwdict = PyDict_New();
3628 else {
3629 kwdict = PyDict_Copy(orig_kwdict);
3630 Py_DECREF(orig_kwdict);
3631 }
3632 if (kwdict == NULL)
3633 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003634 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003635 int err;
3636 PyObject *value = EXT_POP(*pp_stack);
3637 PyObject *key = EXT_POP(*pp_stack);
3638 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003639 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003640 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003641 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003642 PyEval_GetFuncName(func),
3643 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003644 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003645 Py_DECREF(key);
3646 Py_DECREF(value);
3647 Py_DECREF(kwdict);
3648 return NULL;
3649 }
3650 err = PyDict_SetItem(kwdict, key, value);
3651 Py_DECREF(key);
3652 Py_DECREF(value);
3653 if (err) {
3654 Py_DECREF(kwdict);
3655 return NULL;
3656 }
3657 }
3658 return kwdict;
3659}
3660
3661static PyObject *
3662update_star_args(int nstack, int nstar, PyObject *stararg,
3663 PyObject ***pp_stack)
3664{
3665 PyObject *callargs, *w;
3666
3667 callargs = PyTuple_New(nstack + nstar);
3668 if (callargs == NULL) {
3669 return NULL;
3670 }
3671 if (nstar) {
3672 int i;
3673 for (i = 0; i < nstar; i++) {
3674 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3675 Py_INCREF(a);
3676 PyTuple_SET_ITEM(callargs, nstack + i, a);
3677 }
3678 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003679 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003680 w = EXT_POP(*pp_stack);
3681 PyTuple_SET_ITEM(callargs, nstack, w);
3682 }
3683 return callargs;
3684}
3685
3686static PyObject *
3687load_args(PyObject ***pp_stack, int na)
3688{
3689 PyObject *args = PyTuple_New(na);
3690 PyObject *w;
3691
3692 if (args == NULL)
3693 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003694 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003695 w = EXT_POP(*pp_stack);
3696 PyTuple_SET_ITEM(args, na, w);
3697 }
3698 return args;
3699}
3700
3701static PyObject *
3702do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3703{
3704 PyObject *callargs = NULL;
3705 PyObject *kwdict = NULL;
3706 PyObject *result = NULL;
3707
3708 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003709 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003710 if (kwdict == NULL)
3711 goto call_fail;
3712 }
3713 callargs = load_args(pp_stack, na);
3714 if (callargs == NULL)
3715 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003716#ifdef CALL_PROFILE
3717 /* At this point, we have to look at the type of func to
3718 update the call stats properly. Do it here so as to avoid
3719 exposing the call stats machinery outside ceval.c
3720 */
3721 if (PyFunction_Check(func))
3722 PCALL(PCALL_FUNCTION);
3723 else if (PyMethod_Check(func))
3724 PCALL(PCALL_METHOD);
3725 else if (PyType_Check(func))
3726 PCALL(PCALL_TYPE);
3727 else
3728 PCALL(PCALL_OTHER);
3729#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003730 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003731 call_fail:
3732 Py_XDECREF(callargs);
3733 Py_XDECREF(kwdict);
3734 return result;
3735}
3736
3737static PyObject *
3738ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3739{
3740 int nstar = 0;
3741 PyObject *callargs = NULL;
3742 PyObject *stararg = NULL;
3743 PyObject *kwdict = NULL;
3744 PyObject *result = NULL;
3745
3746 if (flags & CALL_FLAG_KW) {
3747 kwdict = EXT_POP(*pp_stack);
3748 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003749 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003750 "%s%s argument after ** "
3751 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003752 PyEval_GetFuncName(func),
3753 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003754 goto ext_call_fail;
3755 }
3756 }
3757 if (flags & CALL_FLAG_VAR) {
3758 stararg = EXT_POP(*pp_stack);
3759 if (!PyTuple_Check(stararg)) {
3760 PyObject *t = NULL;
3761 t = PySequence_Tuple(stararg);
3762 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003763 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3764 PyErr_Format(PyExc_TypeError,
3765 "%s%s argument after * "
3766 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003767 PyEval_GetFuncName(func),
3768 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003769 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003770 goto ext_call_fail;
3771 }
3772 Py_DECREF(stararg);
3773 stararg = t;
3774 }
3775 nstar = PyTuple_GET_SIZE(stararg);
3776 }
3777 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003778 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003779 if (kwdict == NULL)
3780 goto ext_call_fail;
3781 }
3782 callargs = update_star_args(na, nstar, stararg, pp_stack);
3783 if (callargs == NULL)
3784 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003785#ifdef CALL_PROFILE
3786 /* At this point, we have to look at the type of func to
3787 update the call stats properly. Do it here so as to avoid
3788 exposing the call stats machinery outside ceval.c
3789 */
3790 if (PyFunction_Check(func))
3791 PCALL(PCALL_FUNCTION);
3792 else if (PyMethod_Check(func))
3793 PCALL(PCALL_METHOD);
3794 else if (PyType_Check(func))
3795 PCALL(PCALL_TYPE);
3796 else
3797 PCALL(PCALL_OTHER);
3798#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003799 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003800 ext_call_fail:
3801 Py_XDECREF(callargs);
3802 Py_XDECREF(kwdict);
3803 Py_XDECREF(stararg);
3804 return result;
3805}
3806
Tim Peterscb479e72001-12-16 19:11:44 +00003807/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3808 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3809 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3810*/
Tim Petersb5196382001-12-16 19:44:20 +00003811/* Note: If v is NULL, return success without storing into *pi. This
3812 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3813 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003814*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003815int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003816_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003817{
Tim Petersb5196382001-12-16 19:44:20 +00003818 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003819 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003820 if (PyInt_Check(v)) {
3821 x = PyInt_AsLong(v);
3822 } else if (PyLong_Check(v)) {
3823 x = PyLong_AsLong(v);
3824 if (x==-1 && PyErr_Occurred()) {
3825 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003826 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003827
Guido van Rossumac7be682001-01-17 15:42:30 +00003828 if (!PyErr_ExceptionMatches(
3829 PyExc_OverflowError)) {
3830 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003831 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003832 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003833 }
3834
Guido van Rossumac7be682001-01-17 15:42:30 +00003835 /* Clear the OverflowError */
3836 PyErr_Clear();
3837
3838 /* It's an overflow error, so we need to
3839 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003840 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003841 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003842
3843 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003844 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003845 if (long_zero == NULL)
3846 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003847
3848 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003849 cmp = PyObject_RichCompareBool(v, long_zero,
3850 Py_GT);
3851 Py_DECREF(long_zero);
3852 if (cmp < 0)
3853 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003854 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003855 x = INT_MAX;
3856 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003857 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003858 }
3859 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003860 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003861 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003862 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003863 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003864 /* Truncate -- very long indices are truncated anyway */
3865 if (x > INT_MAX)
3866 x = INT_MAX;
3867 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003868 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003869 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003870 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003871 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003872}
3873
Guido van Rossum50d756e2001-08-18 17:43:36 +00003874#undef ISINT
3875#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3876
Guido van Rossumb209a111997-04-29 18:18:01 +00003877static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003878apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003879{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003880 PyTypeObject *tp = u->ob_type;
3881 PySequenceMethods *sq = tp->tp_as_sequence;
3882
3883 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3884 int ilow = 0, ihigh = INT_MAX;
3885 if (!_PyEval_SliceIndex(v, &ilow))
3886 return NULL;
3887 if (!_PyEval_SliceIndex(w, &ihigh))
3888 return NULL;
3889 return PySequence_GetSlice(u, ilow, ihigh);
3890 }
3891 else {
3892 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003893 if (slice != NULL) {
3894 PyObject *res = PyObject_GetItem(u, slice);
3895 Py_DECREF(slice);
3896 return res;
3897 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003898 else
3899 return NULL;
3900 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003901}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003902
3903static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003904assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3905 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003906{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003907 PyTypeObject *tp = u->ob_type;
3908 PySequenceMethods *sq = tp->tp_as_sequence;
3909
3910 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3911 int ilow = 0, ihigh = INT_MAX;
3912 if (!_PyEval_SliceIndex(v, &ilow))
3913 return -1;
3914 if (!_PyEval_SliceIndex(w, &ihigh))
3915 return -1;
3916 if (x == NULL)
3917 return PySequence_DelSlice(u, ilow, ihigh);
3918 else
3919 return PySequence_SetSlice(u, ilow, ihigh, x);
3920 }
3921 else {
3922 PyObject *slice = PySlice_New(v, w, NULL);
3923 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003924 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003925 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003926 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003927 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003928 res = PyObject_DelItem(u, slice);
3929 Py_DECREF(slice);
3930 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003931 }
3932 else
3933 return -1;
3934 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003935}
3936
Guido van Rossumb209a111997-04-29 18:18:01 +00003937static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003938cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003939{
Guido van Rossumac7be682001-01-17 15:42:30 +00003940 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003941 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003942 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003943 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003944 break;
3945 case PyCmp_IS_NOT:
3946 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003947 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003948 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003949 res = PySequence_Contains(w, v);
3950 if (res < 0)
3951 return NULL;
3952 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003953 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003954 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003955 if (res < 0)
3956 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003957 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003958 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003959 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003960 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003961 break;
3962 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003963 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003964 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003965 v = res ? Py_True : Py_False;
3966 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003967 return v;
3968}
3969
Thomas Wouters52152252000-08-17 22:55:00 +00003970static PyObject *
3971import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003972{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003973 PyObject *x;
3974
3975 x = PyObject_GetAttr(v, name);
3976 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003977 PyErr_Format(PyExc_ImportError,
3978 "cannot import name %.230s",
3979 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003980 }
Thomas Wouters52152252000-08-17 22:55:00 +00003981 return x;
3982}
Guido van Rossumac7be682001-01-17 15:42:30 +00003983
Thomas Wouters52152252000-08-17 22:55:00 +00003984static int
3985import_all_from(PyObject *locals, PyObject *v)
3986{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003987 PyObject *all = PyObject_GetAttrString(v, "__all__");
3988 PyObject *dict, *name, *value;
3989 int skip_leading_underscores = 0;
3990 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003991
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003992 if (all == NULL) {
3993 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3994 return -1; /* Unexpected error */
3995 PyErr_Clear();
3996 dict = PyObject_GetAttrString(v, "__dict__");
3997 if (dict == NULL) {
3998 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3999 return -1;
4000 PyErr_SetString(PyExc_ImportError,
4001 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004002 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004003 }
4004 all = PyMapping_Keys(dict);
4005 Py_DECREF(dict);
4006 if (all == NULL)
4007 return -1;
4008 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004009 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004010
4011 for (pos = 0, err = 0; ; pos++) {
4012 name = PySequence_GetItem(all, pos);
4013 if (name == NULL) {
4014 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4015 err = -1;
4016 else
4017 PyErr_Clear();
4018 break;
4019 }
4020 if (skip_leading_underscores &&
4021 PyString_Check(name) &&
4022 PyString_AS_STRING(name)[0] == '_')
4023 {
4024 Py_DECREF(name);
4025 continue;
4026 }
4027 value = PyObject_GetAttr(v, name);
4028 if (value == NULL)
4029 err = -1;
4030 else
4031 err = PyDict_SetItem(locals, name, value);
4032 Py_DECREF(name);
4033 Py_XDECREF(value);
4034 if (err != 0)
4035 break;
4036 }
4037 Py_DECREF(all);
4038 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004039}
4040
Guido van Rossumb209a111997-04-29 18:18:01 +00004041static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004042build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004043{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004044 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004045
4046 if (PyDict_Check(methods))
4047 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004048 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004049 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004050 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4051 base = PyTuple_GET_ITEM(bases, 0);
4052 metaclass = PyObject_GetAttrString(base, "__class__");
4053 if (metaclass == NULL) {
4054 PyErr_Clear();
4055 metaclass = (PyObject *)base->ob_type;
4056 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004057 }
4058 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004059 else {
4060 PyObject *g = PyEval_GetGlobals();
4061 if (g != NULL && PyDict_Check(g))
4062 metaclass = PyDict_GetItemString(g, "__metaclass__");
4063 if (metaclass == NULL)
4064 metaclass = (PyObject *) &PyClass_Type;
4065 Py_INCREF(metaclass);
4066 }
4067 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4068 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004069 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4070 /* A type error here likely means that the user passed
4071 in a base that was not a class (such the random module
4072 instead of the random.random type). Help them out with
4073 a more informative error message */
4074 PyErr_SetString(PyExc_TypeError,
4075 "Error when calling the metaclass.\n" \
4076 "Make sure the base arguments are valid.");
4077 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004078 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004079}
4080
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004081static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004082exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4083 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004084{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004085 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004086 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004087 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004088
Guido van Rossumb209a111997-04-29 18:18:01 +00004089 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4090 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004091 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004092 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004093 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004094 locals = PyTuple_GetItem(prog, 2);
4095 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004096 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004097 if (globals == Py_None) {
4098 globals = PyEval_GetGlobals();
4099 if (locals == Py_None) {
4100 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004101 plain = 1;
4102 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004103 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004104 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004105 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004106 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004107 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004108 !PyCode_Check(prog) &&
4109 !PyFile_Check(prog)) {
4110 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004111 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004112 return -1;
4113 }
Fred Drake661ea262000-10-24 19:57:45 +00004114 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004115 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004116 "exec: arg 2 must be a dictionary or None");
4117 return -1;
4118 }
4119 if (!PyDict_Check(locals)) {
4120 PyErr_SetString(PyExc_TypeError,
4121 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004122 return -1;
4123 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004124 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004125 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004126 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004127 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4128 PyErr_SetString(PyExc_TypeError,
4129 "code object passed to exec may not contain free variables");
4130 return -1;
4131 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004132 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004133 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004134 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004135 FILE *fp = PyFile_AsFile(prog);
4136 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004137 PyCompilerFlags cf;
4138 cf.cf_flags = 0;
4139 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004140 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004141 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004142 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004143 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004144 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004145 }
4146 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004147 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004148 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004149 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004150 cf.cf_flags = 0;
4151#ifdef Py_USING_UNICODE
4152 if (PyUnicode_Check(prog)) {
4153 tmp = PyUnicode_AsUTF8String(prog);
4154 if (tmp == NULL)
4155 return -1;
4156 prog = tmp;
4157 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4158 }
4159#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004160 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004161 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004162 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004163 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004164 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004165 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004166 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004167 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004168 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004169 if (plain)
4170 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004171 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004172 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004173 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004174 return 0;
4175}
Guido van Rossum24c13741995-02-14 09:42:43 +00004176
Guido van Rossumac7be682001-01-17 15:42:30 +00004177static void
Paul Prescode68140d2000-08-30 20:25:01 +00004178format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4179{
4180 char *obj_str;
4181
4182 if (!obj)
4183 return;
4184
4185 obj_str = PyString_AsString(obj);
4186 if (!obj_str)
4187 return;
4188
4189 PyErr_Format(exc, format_str, obj_str);
4190}
Guido van Rossum950361c1997-01-24 13:49:28 +00004191
4192#ifdef DYNAMIC_EXECUTION_PROFILE
4193
Skip Montanarof118cb12001-10-15 20:51:38 +00004194static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004195getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004196{
4197 int i;
4198 PyObject *l = PyList_New(256);
4199 if (l == NULL) return NULL;
4200 for (i = 0; i < 256; i++) {
4201 PyObject *x = PyInt_FromLong(a[i]);
4202 if (x == NULL) {
4203 Py_DECREF(l);
4204 return NULL;
4205 }
4206 PyList_SetItem(l, i, x);
4207 }
4208 for (i = 0; i < 256; i++)
4209 a[i] = 0;
4210 return l;
4211}
4212
4213PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004214_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004215{
4216#ifndef DXPAIRS
4217 return getarray(dxp);
4218#else
4219 int i;
4220 PyObject *l = PyList_New(257);
4221 if (l == NULL) return NULL;
4222 for (i = 0; i < 257; i++) {
4223 PyObject *x = getarray(dxpairs[i]);
4224 if (x == NULL) {
4225 Py_DECREF(l);
4226 return NULL;
4227 }
4228 PyList_SetItem(l, i, x);
4229 }
4230 return l;
4231#endif
4232}
4233
4234#endif