blob: ca7cea8a425edbbd0d22a7f92b91fce78bf24b68 [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{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000321 static 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) {
1646 err = PyDict_SetItem(x, w, v);
1647 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001648 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001649 break;
1650 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001651 PyErr_Format(PyExc_SystemError,
1652 "no locals found when storing %s",
1653 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001657 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001658 if ((x = f->f_locals) != NULL) {
1659 if ((err = PyDict_DelItem(x, w)) != 0)
1660 format_exc_check_arg(PyExc_NameError,
1661 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001662 break;
1663 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001664 PyErr_Format(PyExc_SystemError,
1665 "no locals when deleting %s",
1666 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001668
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001669 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001670 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001672 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1673 PyObject **items = ((PyTupleObject *)v)->ob_item;
1674 while (oparg--) {
1675 w = items[oparg];
1676 Py_INCREF(w);
1677 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001678 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001679 Py_DECREF(v);
1680 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001681 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1682 PyObject **items = ((PyListObject *)v)->ob_item;
1683 while (oparg--) {
1684 w = items[oparg];
1685 Py_INCREF(w);
1686 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001687 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001688 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001689 stack_pointer + oparg))
1690 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001691 else {
1692 if (PyErr_ExceptionMatches(PyExc_TypeError))
1693 PyErr_SetString(PyExc_TypeError,
1694 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001695 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001696 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001697 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001701 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001702 v = TOP();
1703 u = SECOND();
1704 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001705 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1706 Py_DECREF(v);
1707 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001708 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001709 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Guido van Rossum374a9221991-04-04 10:40:29 +00001711 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001712 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001714 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1715 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001716 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001718
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001719 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001720 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001721 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001722 err = PyDict_SetItem(f->f_globals, w, v);
1723 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001724 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001725 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001726
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001727 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001728 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001729 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001730 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001731 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001732 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001733
Guido van Rossum374a9221991-04-04 10:40:29 +00001734 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001735 w = GETITEM(names, oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001736 if ((x = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001737 PyErr_Format(PyExc_SystemError,
1738 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001739 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001740 break;
1741 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001742 x = PyDict_GetItem(x, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001746 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001748 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001749 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001750 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 break;
1752 }
1753 }
1754 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001755 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001756 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001757 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001758
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001760 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001761 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001762 /* Inline the PyDict_GetItem() calls.
1763 WARNING: this is an extreme speed hack.
1764 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001765 long hash = ((PyStringObject *)w)->ob_shash;
1766 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001767 PyDictObject *d;
1768 d = (PyDictObject *)(f->f_globals);
1769 x = d->ma_lookup(d, w, hash)->me_value;
1770 if (x != NULL) {
1771 Py_INCREF(x);
1772 PUSH(x);
1773 continue;
1774 }
1775 d = (PyDictObject *)(f->f_builtins);
1776 x = d->ma_lookup(d, w, hash)->me_value;
1777 if (x != NULL) {
1778 Py_INCREF(x);
1779 PUSH(x);
1780 continue;
1781 }
1782 goto load_global_error;
1783 }
1784 }
1785 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001786 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001788 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001790 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001791 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001792 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001793 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001794 break;
1795 }
1796 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001797 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001798 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001799 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001800
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001801 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001802 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001803 if (x != NULL) {
1804 SETLOCAL(oparg, NULL);
1805 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001806 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001807 format_exc_check_arg(
1808 PyExc_UnboundLocalError,
1809 UNBOUNDLOCAL_ERROR_MSG,
1810 PyTuple_GetItem(co->co_varnames, oparg)
1811 );
1812 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001813
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001814 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001815 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001816 Py_INCREF(x);
1817 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001818 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001819 break;
1820
1821 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001822 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001823 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001824 if (w != NULL) {
1825 PUSH(w);
1826 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001827 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001828 err = -1;
1829 /* Don't stomp existing exception */
1830 if (PyErr_Occurred())
1831 break;
1832 if (oparg < f->f_ncells) {
1833 v = PyTuple_GetItem(co->co_cellvars,
1834 oparg);
1835 format_exc_check_arg(
1836 PyExc_UnboundLocalError,
1837 UNBOUNDLOCAL_ERROR_MSG,
1838 v);
1839 } else {
1840 v = PyTuple_GetItem(
1841 co->co_freevars,
1842 oparg - f->f_ncells);
1843 format_exc_check_arg(
1844 PyExc_NameError,
1845 UNBOUNDFREE_ERROR_MSG,
1846 v);
1847 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001848 break;
1849
1850 case STORE_DEREF:
1851 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001852 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001853 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001854 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001855 continue;
1856
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001858 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001859 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001860 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001861 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001862 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001863 }
1864 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001865 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 }
1867 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001868
Guido van Rossum374a9221991-04-04 10:40:29 +00001869 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001870 x = PyList_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 Rossum5053efc1998-08-04 15:27:50 +00001874 PyList_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_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001882 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001884 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001886
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001888 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001889 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001890 x = PyObject_GetAttr(v, w);
1891 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001892 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001893 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001894 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001895
Guido van Rossum374a9221991-04-04 10:40:29 +00001896 case COMPARE_OP:
1897 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001898 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001899 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001900 /* INLINE: cmp(int, int) */
1901 register long a, b;
1902 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001903 a = PyInt_AS_LONG(v);
1904 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001905 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001906 case PyCmp_LT: res = a < b; break;
1907 case PyCmp_LE: res = a <= b; break;
1908 case PyCmp_EQ: res = a == b; break;
1909 case PyCmp_NE: res = a != b; break;
1910 case PyCmp_GT: res = a > b; break;
1911 case PyCmp_GE: res = a >= b; break;
1912 case PyCmp_IS: res = v == w; break;
1913 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001914 default: goto slow_compare;
1915 }
1916 x = res ? Py_True : Py_False;
1917 Py_INCREF(x);
1918 }
1919 else {
1920 slow_compare:
1921 x = cmp_outcome(oparg, v, w);
1922 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001923 Py_DECREF(v);
1924 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001925 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001926 if (x == NULL) break;
1927 PREDICT(JUMP_IF_FALSE);
1928 PREDICT(JUMP_IF_TRUE);
1929 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Guido van Rossum374a9221991-04-04 10:40:29 +00001931 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001932 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001933 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001934 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001935 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001936 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001937 break;
1938 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001939 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001940 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001941 w,
1942 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00001943 f->f_locals == NULL ?
1944 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001945 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001946 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001947 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001948 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001949 x = NULL;
1950 break;
1951 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001952#ifdef WITH_TSC
1953 rdtscll(intr0);
1954#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001955 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001956#ifdef WITH_TSC
1957 rdtscll(intr1);
1958#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001960 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001961 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001962 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001963
Thomas Wouters52152252000-08-17 22:55:00 +00001964 case IMPORT_STAR:
1965 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001966 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001967 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001968 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001969 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001970 break;
1971 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001972#ifdef WITH_TSC
1973 rdtscll(intr0);
1974#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001975 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001976#ifdef WITH_TSC
1977 rdtscll(intr1);
1978#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001979 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001980 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001981 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001982 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001983
Thomas Wouters52152252000-08-17 22:55:00 +00001984 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001985 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001986 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001987#ifdef WITH_TSC
1988 rdtscll(intr0);
1989#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001990 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001991#ifdef WITH_TSC
1992 rdtscll(intr1);
1993#endif
Thomas Wouters52152252000-08-17 22:55:00 +00001994 PUSH(x);
1995 if (x != NULL) continue;
1996 break;
1997
Guido van Rossum374a9221991-04-04 10:40:29 +00001998 case JUMP_FORWARD:
1999 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002000 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002001
Raymond Hettingerf606f872003-03-16 03:11:04 +00002002 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002003 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002004 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002005 if (w == Py_True) {
2006 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002007 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002008 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002009 if (w == Py_False) {
2010 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002011 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002012 }
2013 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002014 if (err > 0)
2015 err = 0;
2016 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002017 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002018 else
2019 break;
2020 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002021
Raymond Hettingerf606f872003-03-16 03:11:04 +00002022 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002023 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002024 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002025 if (w == Py_False) {
2026 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002027 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002028 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002029 if (w == Py_True) {
2030 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002031 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002032 }
2033 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002034 if (err > 0) {
2035 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002036 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002037 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002038 else if (err == 0)
2039 ;
2040 else
2041 break;
2042 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002043
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002044 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002045 case JUMP_ABSOLUTE:
2046 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002047 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002048
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002049 case GET_ITER:
2050 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002051 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002052 x = PyObject_GetIter(v);
2053 Py_DECREF(v);
2054 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002055 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002056 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002057 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002058 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002059 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002060 break;
2061
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002062 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002063 case FOR_ITER:
2064 /* before: [iter]; after: [iter, iter()] *or* [] */
2065 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002066 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002067 if (x != NULL) {
2068 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002069 PREDICT(STORE_FAST);
2070 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002071 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002072 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002073 if (PyErr_Occurred()) {
2074 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2075 break;
2076 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002077 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002078 /* iterator ended normally */
2079 x = v = POP();
2080 Py_DECREF(v);
2081 JUMPBY(oparg);
2082 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002083
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002084 case BREAK_LOOP:
2085 why = WHY_BREAK;
2086 goto fast_block_end;
2087
2088 case CONTINUE_LOOP:
2089 retval = PyInt_FromLong(oparg);
2090 why = WHY_CONTINUE;
2091 goto fast_block_end;
2092
Guido van Rossum374a9221991-04-04 10:40:29 +00002093 case SETUP_LOOP:
2094 case SETUP_EXCEPT:
2095 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002096 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002097 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002098 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002099
Guido van Rossumf10570b1995-07-07 22:53:21 +00002100 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002101 {
2102 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002103 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002104 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002105#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002106 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002107#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002108 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002109#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002110 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002111 PUSH(x);
2112 if (x != NULL)
2113 continue;
2114 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002115 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002116
Jeremy Hylton76901512000-03-28 23:49:17 +00002117 case CALL_FUNCTION_VAR:
2118 case CALL_FUNCTION_KW:
2119 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002120 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002121 int na = oparg & 0xff;
2122 int nk = (oparg>>8) & 0xff;
2123 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002124 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002125 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002126 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002127 if (flags & CALL_FLAG_VAR)
2128 n++;
2129 if (flags & CALL_FLAG_KW)
2130 n++;
2131 pfunc = stack_pointer - n - 1;
2132 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002133
Guido van Rossumac7be682001-01-17 15:42:30 +00002134 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002135 && PyMethod_GET_SELF(func) != NULL) {
2136 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002137 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002138 func = PyMethod_GET_FUNCTION(func);
2139 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002140 Py_DECREF(*pfunc);
2141 *pfunc = self;
2142 na++;
2143 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002144 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002145 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002146 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002147#ifdef WITH_TSC
2148 rdtscll(intr0);
2149#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002150 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002151#ifdef WITH_TSC
2152 rdtscll(intr1);
2153#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002154 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002155 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002156
Jeremy Hylton76901512000-03-28 23:49:17 +00002157 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002158 w = POP();
2159 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002160 }
2161 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002162 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002163 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002164 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002165 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002166
Guido van Rossum681d79a1995-07-18 14:51:37 +00002167 case MAKE_FUNCTION:
2168 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002169 x = PyFunction_New(v, f->f_globals);
2170 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002171 /* XXX Maybe this should be a separate opcode? */
2172 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002173 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002174 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002175 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002176 x = NULL;
2177 break;
2178 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002179 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002180 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002181 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002182 }
2183 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002184 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002185 }
2186 PUSH(x);
2187 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002188
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002189 case MAKE_CLOSURE:
2190 {
2191 int nfree;
2192 v = POP(); /* code object */
2193 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002194 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002195 Py_DECREF(v);
2196 /* XXX Maybe this should be a separate opcode? */
2197 if (x != NULL && nfree > 0) {
2198 v = PyTuple_New(nfree);
2199 if (v == NULL) {
2200 Py_DECREF(x);
2201 x = NULL;
2202 break;
2203 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002204 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002205 w = POP();
2206 PyTuple_SET_ITEM(v, nfree, w);
2207 }
2208 err = PyFunction_SetClosure(x, v);
2209 Py_DECREF(v);
2210 }
2211 if (x != NULL && oparg > 0) {
2212 v = PyTuple_New(oparg);
2213 if (v == NULL) {
2214 Py_DECREF(x);
2215 x = NULL;
2216 break;
2217 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002218 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002219 w = POP();
2220 PyTuple_SET_ITEM(v, oparg, w);
2221 }
2222 err = PyFunction_SetDefaults(x, v);
2223 Py_DECREF(v);
2224 }
2225 PUSH(x);
2226 break;
2227 }
2228
Guido van Rossum8861b741996-07-30 16:49:37 +00002229 case BUILD_SLICE:
2230 if (oparg == 3)
2231 w = POP();
2232 else
2233 w = NULL;
2234 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002235 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002236 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002237 Py_DECREF(u);
2238 Py_DECREF(v);
2239 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002240 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002241 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002242 break;
2243
Fred Drakeef8ace32000-08-24 00:32:09 +00002244 case EXTENDED_ARG:
2245 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002246 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002247 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002248
Guido van Rossum374a9221991-04-04 10:40:29 +00002249 default:
2250 fprintf(stderr,
2251 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002252 PyCode_Addr2Line(f->f_code, f->f_lasti),
2253 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002254 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002255 why = WHY_EXCEPTION;
2256 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002257
2258#ifdef CASE_TOO_BIG
2259 }
2260#endif
2261
Guido van Rossum374a9221991-04-04 10:40:29 +00002262 } /* switch */
2263
2264 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002265
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002266#ifdef WITH_TSC
2267 rdtscll(inst1);
2268#endif
2269
Guido van Rossum374a9221991-04-04 10:40:29 +00002270 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002271
Guido van Rossum374a9221991-04-04 10:40:29 +00002272 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002273 if (err == 0 && x != NULL) {
2274#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002275 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002276 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002277 fprintf(stderr,
2278 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002279 else {
2280#endif
2281#ifdef WITH_TSC
2282 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002283#endif
2284 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002285#ifdef CHECKEXC
2286 }
2287#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002288 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002289 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002290 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 err = 0;
2292 }
2293
Guido van Rossum374a9221991-04-04 10:40:29 +00002294 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002296 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002297 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002298 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002299 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002300 why = WHY_EXCEPTION;
2301 }
2302 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002303#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002304 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002305 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002306 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002307 char buf[1024];
2308 sprintf(buf, "Stack unwind with exception "
2309 "set and why=%d", why);
2310 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002311 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002312 }
2313#endif
2314
2315 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002316
Guido van Rossum374a9221991-04-04 10:40:29 +00002317 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002318 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002319
Fred Drake8f51f542001-10-04 14:48:42 +00002320 if (tstate->c_tracefunc != NULL)
2321 call_exc_trace(tstate->c_tracefunc,
2322 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002323 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002324
Guido van Rossum374a9221991-04-04 10:40:29 +00002325 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 if (why == WHY_RERAISE)
2328 why = WHY_EXCEPTION;
2329
2330 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002331
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002332fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002333 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002334 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002335
Tim Peters8a5c3c72004-04-05 19:36:21 +00002336 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002337 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2338 /* For a continue inside a try block,
2339 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002340 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2341 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002342 why = WHY_NOT;
2343 JUMPTO(PyInt_AS_LONG(retval));
2344 Py_DECREF(retval);
2345 break;
2346 }
2347
Guido van Rossum374a9221991-04-04 10:40:29 +00002348 while (STACK_LEVEL() > b->b_level) {
2349 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002350 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 }
2352 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2353 why = WHY_NOT;
2354 JUMPTO(b->b_handler);
2355 break;
2356 }
2357 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002358 (b->b_type == SETUP_EXCEPT &&
2359 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002360 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002361 PyObject *exc, *val, *tb;
2362 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002364 val = Py_None;
2365 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002367 /* Make the raw exception data
2368 available to the handler,
2369 so a program can emulate the
2370 Python main loop. Don't do
2371 this for 'finally'. */
2372 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002373 PyErr_NormalizeException(
2374 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002375 set_exc_info(tstate,
2376 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002377 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002378 if (tb == NULL) {
2379 Py_INCREF(Py_None);
2380 PUSH(Py_None);
2381 } else
2382 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002383 PUSH(val);
2384 PUSH(exc);
2385 }
2386 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002387 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002388 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002389 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002390 PUSH(v);
2391 }
2392 why = WHY_NOT;
2393 JUMPTO(b->b_handler);
2394 break;
2395 }
2396 } /* unwind stack */
2397
2398 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002399
Guido van Rossum374a9221991-04-04 10:40:29 +00002400 if (why != WHY_NOT)
2401 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002402#ifdef WITH_TSC
2403 rdtscll(loop1);
2404#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002405
Guido van Rossum374a9221991-04-04 10:40:29 +00002406 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002407
Tim Peters8a5c3c72004-04-05 19:36:21 +00002408 assert(why != WHY_YIELD);
2409 /* Pop remaining stack entries. */
2410 while (!EMPTY()) {
2411 v = POP();
2412 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002413 }
2414
Tim Peters8a5c3c72004-04-05 19:36:21 +00002415 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002416 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002417
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002418fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002419 if (tstate->use_tracing) {
2420 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002421 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002422 if (call_trace(tstate->c_tracefunc,
2423 tstate->c_traceobj, f,
2424 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002425 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002426 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002427 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002428 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002429 }
Fred Drake8f51f542001-10-04 14:48:42 +00002430 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002431 if (why == WHY_EXCEPTION)
2432 call_trace_protected(tstate->c_profilefunc,
2433 tstate->c_profileobj, f,
2434 PyTrace_RETURN);
2435 else if (call_trace(tstate->c_profilefunc,
2436 tstate->c_profileobj, f,
2437 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002438 Py_XDECREF(retval);
2439 retval = NULL;
2440 why = WHY_EXCEPTION;
2441 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002442 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002443 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002444
Guido van Rossuma027efa1997-05-05 20:56:21 +00002445 reset_exc_info(tstate);
2446
Tim Peters5ca576e2001-06-18 22:08:13 +00002447 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002448 exit_eval_frame:
2449 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002450 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002451
Guido van Rossum96a42c81992-01-12 02:29:51 +00002452 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002453}
2454
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002455/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002456 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2457 the test in the if statement in Misc/gdbinit:ppystack */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002458
Tim Peters6d6c1a32001-08-02 04:15:00 +00002459PyObject *
2460PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002461 PyObject **args, int argcount, PyObject **kws, int kwcount,
2462 PyObject **defs, int defcount, PyObject *closure)
2463{
2464 register PyFrameObject *f;
2465 register PyObject *retval = NULL;
2466 register PyObject **fastlocals, **freevars;
2467 PyThreadState *tstate = PyThreadState_GET();
2468 PyObject *x, *u;
2469
2470 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002471 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002472 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002473 return NULL;
2474 }
2475
Jeremy Hylton985eba52003-02-05 23:13:00 +00002476 assert(globals != NULL);
2477 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002478 if (f == NULL)
2479 return NULL;
2480
2481 fastlocals = f->f_localsplus;
2482 freevars = f->f_localsplus + f->f_nlocals;
2483
2484 if (co->co_argcount > 0 ||
2485 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2486 int i;
2487 int n = argcount;
2488 PyObject *kwdict = NULL;
2489 if (co->co_flags & CO_VARKEYWORDS) {
2490 kwdict = PyDict_New();
2491 if (kwdict == NULL)
2492 goto fail;
2493 i = co->co_argcount;
2494 if (co->co_flags & CO_VARARGS)
2495 i++;
2496 SETLOCAL(i, kwdict);
2497 }
2498 if (argcount > co->co_argcount) {
2499 if (!(co->co_flags & CO_VARARGS)) {
2500 PyErr_Format(PyExc_TypeError,
2501 "%.200s() takes %s %d "
2502 "%sargument%s (%d given)",
2503 PyString_AsString(co->co_name),
2504 defcount ? "at most" : "exactly",
2505 co->co_argcount,
2506 kwcount ? "non-keyword " : "",
2507 co->co_argcount == 1 ? "" : "s",
2508 argcount);
2509 goto fail;
2510 }
2511 n = co->co_argcount;
2512 }
2513 for (i = 0; i < n; i++) {
2514 x = args[i];
2515 Py_INCREF(x);
2516 SETLOCAL(i, x);
2517 }
2518 if (co->co_flags & CO_VARARGS) {
2519 u = PyTuple_New(argcount - n);
2520 if (u == NULL)
2521 goto fail;
2522 SETLOCAL(co->co_argcount, u);
2523 for (i = n; i < argcount; i++) {
2524 x = args[i];
2525 Py_INCREF(x);
2526 PyTuple_SET_ITEM(u, i-n, x);
2527 }
2528 }
2529 for (i = 0; i < kwcount; i++) {
2530 PyObject *keyword = kws[2*i];
2531 PyObject *value = kws[2*i + 1];
2532 int j;
2533 if (keyword == NULL || !PyString_Check(keyword)) {
2534 PyErr_Format(PyExc_TypeError,
2535 "%.200s() keywords must be strings",
2536 PyString_AsString(co->co_name));
2537 goto fail;
2538 }
2539 /* XXX slow -- speed up using dictionary? */
2540 for (j = 0; j < co->co_argcount; j++) {
2541 PyObject *nm = PyTuple_GET_ITEM(
2542 co->co_varnames, j);
2543 int cmp = PyObject_RichCompareBool(
2544 keyword, nm, Py_EQ);
2545 if (cmp > 0)
2546 break;
2547 else if (cmp < 0)
2548 goto fail;
2549 }
2550 /* Check errors from Compare */
2551 if (PyErr_Occurred())
2552 goto fail;
2553 if (j >= co->co_argcount) {
2554 if (kwdict == NULL) {
2555 PyErr_Format(PyExc_TypeError,
2556 "%.200s() got an unexpected "
2557 "keyword argument '%.400s'",
2558 PyString_AsString(co->co_name),
2559 PyString_AsString(keyword));
2560 goto fail;
2561 }
2562 PyDict_SetItem(kwdict, keyword, value);
2563 }
2564 else {
2565 if (GETLOCAL(j) != NULL) {
2566 PyErr_Format(PyExc_TypeError,
2567 "%.200s() got multiple "
2568 "values for keyword "
2569 "argument '%.400s'",
2570 PyString_AsString(co->co_name),
2571 PyString_AsString(keyword));
2572 goto fail;
2573 }
2574 Py_INCREF(value);
2575 SETLOCAL(j, value);
2576 }
2577 }
2578 if (argcount < co->co_argcount) {
2579 int m = co->co_argcount - defcount;
2580 for (i = argcount; i < m; i++) {
2581 if (GETLOCAL(i) == NULL) {
2582 PyErr_Format(PyExc_TypeError,
2583 "%.200s() takes %s %d "
2584 "%sargument%s (%d given)",
2585 PyString_AsString(co->co_name),
2586 ((co->co_flags & CO_VARARGS) ||
2587 defcount) ? "at least"
2588 : "exactly",
2589 m, kwcount ? "non-keyword " : "",
2590 m == 1 ? "" : "s", i);
2591 goto fail;
2592 }
2593 }
2594 if (n > m)
2595 i = n - m;
2596 else
2597 i = 0;
2598 for (; i < defcount; i++) {
2599 if (GETLOCAL(m+i) == NULL) {
2600 PyObject *def = defs[i];
2601 Py_INCREF(def);
2602 SETLOCAL(m+i, def);
2603 }
2604 }
2605 }
2606 }
2607 else {
2608 if (argcount > 0 || kwcount > 0) {
2609 PyErr_Format(PyExc_TypeError,
2610 "%.200s() takes no arguments (%d given)",
2611 PyString_AsString(co->co_name),
2612 argcount + kwcount);
2613 goto fail;
2614 }
2615 }
2616 /* Allocate and initialize storage for cell vars, and copy free
2617 vars into frame. This isn't too efficient right now. */
2618 if (f->f_ncells) {
2619 int i = 0, j = 0, nargs, found;
2620 char *cellname, *argname;
2621 PyObject *c;
2622
2623 nargs = co->co_argcount;
2624 if (co->co_flags & CO_VARARGS)
2625 nargs++;
2626 if (co->co_flags & CO_VARKEYWORDS)
2627 nargs++;
2628
2629 /* Check for cells that shadow args */
2630 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2631 cellname = PyString_AS_STRING(
2632 PyTuple_GET_ITEM(co->co_cellvars, i));
2633 found = 0;
2634 while (j < nargs) {
2635 argname = PyString_AS_STRING(
2636 PyTuple_GET_ITEM(co->co_varnames, j));
2637 if (strcmp(cellname, argname) == 0) {
2638 c = PyCell_New(GETLOCAL(j));
2639 if (c == NULL)
2640 goto fail;
2641 GETLOCAL(f->f_nlocals + i) = c;
2642 found = 1;
2643 break;
2644 }
2645 j++;
2646 }
2647 if (found == 0) {
2648 c = PyCell_New(NULL);
2649 if (c == NULL)
2650 goto fail;
2651 SETLOCAL(f->f_nlocals + i, c);
2652 }
2653 }
2654 /* Initialize any that are left */
2655 while (i < f->f_ncells) {
2656 c = PyCell_New(NULL);
2657 if (c == NULL)
2658 goto fail;
2659 SETLOCAL(f->f_nlocals + i, c);
2660 i++;
2661 }
2662 }
2663 if (f->f_nfreevars) {
2664 int i;
2665 for (i = 0; i < f->f_nfreevars; ++i) {
2666 PyObject *o = PyTuple_GET_ITEM(closure, i);
2667 Py_INCREF(o);
2668 freevars[f->f_ncells + i] = o;
2669 }
2670 }
2671
Tim Peters5ca576e2001-06-18 22:08:13 +00002672 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002673 /* Don't need to keep the reference to f_back, it will be set
2674 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002675 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002676 f->f_back = NULL;
2677
Jeremy Hylton985eba52003-02-05 23:13:00 +00002678 PCALL(PCALL_GENERATOR);
2679
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002680 /* Create a new generator that owns the ready to run frame
2681 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002682 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002683 }
2684
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002685 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002686
2687 fail: /* Jump here from prelude on failure */
2688
Tim Petersb13680b2001-11-27 23:29:29 +00002689 /* decref'ing the frame can cause __del__ methods to get invoked,
2690 which can call back into Python. While we're done with the
2691 current Python frame (f), the associated C stack is still in use,
2692 so recursion_depth must be boosted for the duration.
2693 */
2694 assert(tstate != NULL);
2695 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002696 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002697 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002698 return retval;
2699}
2700
2701
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002702/* Implementation notes for set_exc_info() and reset_exc_info():
2703
2704- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2705 'exc_traceback'. These always travel together.
2706
2707- tstate->curexc_ZZZ is the "hot" exception that is set by
2708 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2709
2710- Once an exception is caught by an except clause, it is transferred
2711 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2712 can pick it up. This is the primary task of set_exc_info().
2713
2714- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2715
2716 Long ago, when none of this existed, there were just a few globals:
2717 one set corresponding to the "hot" exception, and one set
2718 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2719 globals; they were simply stored as sys.exc_ZZZ. For backwards
2720 compatibility, they still are!) The problem was that in code like
2721 this:
2722
2723 try:
2724 "something that may fail"
2725 except "some exception":
2726 "do something else first"
2727 "print the exception from sys.exc_ZZZ."
2728
2729 if "do something else first" invoked something that raised and caught
2730 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2731 cause of subtle bugs. I fixed this by changing the semantics as
2732 follows:
2733
2734 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2735 *in that frame*.
2736
2737 - But initially, and as long as no exception is caught in a given
2738 frame, sys.exc_ZZZ will hold the last exception caught in the
2739 previous frame (or the frame before that, etc.).
2740
2741 The first bullet fixed the bug in the above example. The second
2742 bullet was for backwards compatibility: it was (and is) common to
2743 have a function that is called when an exception is caught, and to
2744 have that function access the caught exception via sys.exc_ZZZ.
2745 (Example: traceback.print_exc()).
2746
2747 At the same time I fixed the problem that sys.exc_ZZZ weren't
2748 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2749 but that's really a separate improvement.
2750
2751 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2752 variables to what they were before the current frame was called. The
2753 set_exc_info() function saves them on the frame so that
2754 reset_exc_info() can restore them. The invariant is that
2755 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2756 exception (where "catching" an exception applies only to successful
2757 except clauses); and if the current frame ever caught an exception,
2758 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2759 at the start of the current frame.
2760
2761*/
2762
Guido van Rossuma027efa1997-05-05 20:56:21 +00002763static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002764set_exc_info(PyThreadState *tstate,
2765 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002766{
2767 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002768 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002769
Guido van Rossuma027efa1997-05-05 20:56:21 +00002770 frame = tstate->frame;
2771 if (frame->f_exc_type == NULL) {
2772 /* This frame didn't catch an exception before */
2773 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002774 if (tstate->exc_type == NULL) {
2775 Py_INCREF(Py_None);
2776 tstate->exc_type = Py_None;
2777 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002778 tmp_type = frame->f_exc_type;
2779 tmp_value = frame->f_exc_value;
2780 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002781 Py_XINCREF(tstate->exc_type);
2782 Py_XINCREF(tstate->exc_value);
2783 Py_XINCREF(tstate->exc_traceback);
2784 frame->f_exc_type = tstate->exc_type;
2785 frame->f_exc_value = tstate->exc_value;
2786 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002787 Py_XDECREF(tmp_type);
2788 Py_XDECREF(tmp_value);
2789 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002790 }
2791 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002792 tmp_type = tstate->exc_type;
2793 tmp_value = tstate->exc_value;
2794 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002795 Py_XINCREF(type);
2796 Py_XINCREF(value);
2797 Py_XINCREF(tb);
2798 tstate->exc_type = type;
2799 tstate->exc_value = value;
2800 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002801 Py_XDECREF(tmp_type);
2802 Py_XDECREF(tmp_value);
2803 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002804 /* For b/w compatibility */
2805 PySys_SetObject("exc_type", type);
2806 PySys_SetObject("exc_value", value);
2807 PySys_SetObject("exc_traceback", tb);
2808}
2809
2810static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002811reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002812{
2813 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002814 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002815 frame = tstate->frame;
2816 if (frame->f_exc_type != NULL) {
2817 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002818 tmp_type = tstate->exc_type;
2819 tmp_value = tstate->exc_value;
2820 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002821 Py_XINCREF(frame->f_exc_type);
2822 Py_XINCREF(frame->f_exc_value);
2823 Py_XINCREF(frame->f_exc_traceback);
2824 tstate->exc_type = frame->f_exc_type;
2825 tstate->exc_value = frame->f_exc_value;
2826 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002827 Py_XDECREF(tmp_type);
2828 Py_XDECREF(tmp_value);
2829 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002830 /* For b/w compatibility */
2831 PySys_SetObject("exc_type", frame->f_exc_type);
2832 PySys_SetObject("exc_value", frame->f_exc_value);
2833 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2834 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002835 tmp_type = frame->f_exc_type;
2836 tmp_value = frame->f_exc_value;
2837 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002838 frame->f_exc_type = NULL;
2839 frame->f_exc_value = NULL;
2840 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002841 Py_XDECREF(tmp_type);
2842 Py_XDECREF(tmp_value);
2843 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002844}
2845
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002846/* Logic for the raise statement (too complicated for inlining).
2847 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002848static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002849do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002850{
Guido van Rossumd295f121998-04-09 21:39:57 +00002851 if (type == NULL) {
2852 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002853 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002854 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2855 value = tstate->exc_value;
2856 tb = tstate->exc_traceback;
2857 Py_XINCREF(type);
2858 Py_XINCREF(value);
2859 Py_XINCREF(tb);
2860 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002861
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002862 /* We support the following forms of raise:
2863 raise <class>, <classinstance>
2864 raise <class>, <argument tuple>
2865 raise <class>, None
2866 raise <class>, <argument>
2867 raise <classinstance>, None
2868 raise <string>, <object>
2869 raise <string>, None
2870
2871 An omitted second argument is the same as None.
2872
2873 In addition, raise <tuple>, <anything> is the same as
2874 raising the tuple's first item (and it better have one!);
2875 this rule is applied recursively.
2876
2877 Finally, an optional third argument can be supplied, which
2878 gives the traceback to be substituted (useful when
2879 re-raising an exception after examining it). */
2880
2881 /* First, check the traceback argument, replacing None with
2882 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002883 if (tb == Py_None) {
2884 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002885 tb = NULL;
2886 }
2887 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002888 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002889 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002890 goto raise_error;
2891 }
2892
2893 /* Next, replace a missing value with None */
2894 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002895 value = Py_None;
2896 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002897 }
2898
2899 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002900 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2901 PyObject *tmp = type;
2902 type = PyTuple_GET_ITEM(type, 0);
2903 Py_INCREF(type);
2904 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002905 }
2906
Tim Petersafb2c802002-04-18 18:06:20 +00002907 if (PyString_CheckExact(type))
2908 /* Raising builtin string is deprecated but still allowed --
2909 * do nothing. Raising an instance of a new-style str
2910 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002911 PyErr_Warn(PyExc_PendingDeprecationWarning,
2912 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002913
2914 else if (PyClass_Check(type))
2915 PyErr_NormalizeException(&type, &value, &tb);
2916
Guido van Rossumb209a111997-04-29 18:18:01 +00002917 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002918 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002919 if (value != Py_None) {
2920 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002921 "instance exception may not have a separate value");
2922 goto raise_error;
2923 }
2924 else {
2925 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002926 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002927 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002928 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2929 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002930 }
2931 }
2932 else {
2933 /* Not something you can raise. You get an exception
2934 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002935 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002936 "exceptions must be classes, instances, or "
2937 "strings (deprecated), not %s",
2938 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002939 goto raise_error;
2940 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002941 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002942 if (tb == NULL)
2943 return WHY_EXCEPTION;
2944 else
2945 return WHY_RERAISE;
2946 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002947 Py_XDECREF(value);
2948 Py_XDECREF(type);
2949 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002950 return WHY_EXCEPTION;
2951}
2952
Tim Petersd6d010b2001-06-21 02:49:55 +00002953/* Iterate v argcnt times and store the results on the stack (via decreasing
2954 sp). Return 1 for success, 0 if error. */
2955
Barry Warsawe42b18f1997-08-25 22:13:04 +00002956static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002957unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002958{
Tim Petersd6d010b2001-06-21 02:49:55 +00002959 int i = 0;
2960 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002961 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002962
Tim Petersd6d010b2001-06-21 02:49:55 +00002963 assert(v != NULL);
2964
2965 it = PyObject_GetIter(v);
2966 if (it == NULL)
2967 goto Error;
2968
2969 for (; i < argcnt; i++) {
2970 w = PyIter_Next(it);
2971 if (w == NULL) {
2972 /* Iterator done, via error or exhaustion. */
2973 if (!PyErr_Occurred()) {
2974 PyErr_Format(PyExc_ValueError,
2975 "need more than %d value%s to unpack",
2976 i, i == 1 ? "" : "s");
2977 }
2978 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002979 }
2980 *--sp = w;
2981 }
Tim Petersd6d010b2001-06-21 02:49:55 +00002982
2983 /* We better have exhausted the iterator now. */
2984 w = PyIter_Next(it);
2985 if (w == NULL) {
2986 if (PyErr_Occurred())
2987 goto Error;
2988 Py_DECREF(it);
2989 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00002990 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00002991 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00002992 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00002993 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00002994Error:
Barry Warsaw91010551997-08-25 22:30:51 +00002995 for (; i > 0; i--, sp++)
2996 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00002997 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00002998 return 0;
2999}
3000
3001
Guido van Rossum96a42c81992-01-12 02:29:51 +00003002#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003003static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003004prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003005{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003006 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003007 if (PyObject_Print(v, stdout, 0) != 0)
3008 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003009 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003010 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003011}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003012#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003013
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003014static void
Fred Drake5755ce62001-06-27 19:19:46 +00003015call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003016{
Guido van Rossumb209a111997-04-29 18:18:01 +00003017 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003018 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003019 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003020 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003021 value = Py_None;
3022 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003023 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003024 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003025 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003026 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003027 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003028 }
Fred Drake5755ce62001-06-27 19:19:46 +00003029 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003030 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003031 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003032 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003033 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003034 Py_XDECREF(type);
3035 Py_XDECREF(value);
3036 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003037 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003038}
3039
Fred Drake4ec5d562001-10-04 19:26:43 +00003040static void
3041call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3042 int what)
3043{
3044 PyObject *type, *value, *traceback;
3045 int err;
3046 PyErr_Fetch(&type, &value, &traceback);
3047 err = call_trace(func, obj, frame, what, NULL);
3048 if (err == 0)
3049 PyErr_Restore(type, value, traceback);
3050 else {
3051 Py_XDECREF(type);
3052 Py_XDECREF(value);
3053 Py_XDECREF(traceback);
3054 }
3055}
3056
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003057static int
Fred Drake5755ce62001-06-27 19:19:46 +00003058call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3059 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003060{
Fred Drake5755ce62001-06-27 19:19:46 +00003061 register PyThreadState *tstate = frame->f_tstate;
3062 int result;
3063 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003064 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003065 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003066 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003067 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003068 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3069 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003070 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003071 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003072}
3073
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003074PyObject *
3075_PyEval_CallTracing(PyObject *func, PyObject *args)
3076{
3077 PyFrameObject *frame = PyEval_GetFrame();
3078 PyThreadState *tstate = frame->f_tstate;
3079 int save_tracing = tstate->tracing;
3080 int save_use_tracing = tstate->use_tracing;
3081 PyObject *result;
3082
3083 tstate->tracing = 0;
3084 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3085 || (tstate->c_profilefunc != NULL));
3086 result = PyObject_Call(func, args, NULL);
3087 tstate->tracing = save_tracing;
3088 tstate->use_tracing = save_use_tracing;
3089 return result;
3090}
3091
Michael W. Hudson006c7522002-11-08 13:08:46 +00003092static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003093maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003094 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3095 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003096{
3097 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003098
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003099 In a nutshell, we use the co_lnotab field of the code object
3100 to tell when execution has moved onto a different line.
3101
3102 As mentioned above, the basic idea is so set things up so
3103 that
3104
3105 *instr_lb <= frame->f_lasti < *instr_ub
3106
3107 is true so long as execution does not change lines.
3108
3109 This is all fairly simple. Digging the information out of
3110 co_lnotab takes some work, but is conceptually clear.
3111
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003112 Somewhat harder to explain is why we don't *always* call the
3113 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003114
3115 Consider this code:
3116
3117 1: def f(a):
3118 2: if a:
3119 3: print 1
3120 4: else:
3121 5: print 2
3122
3123 which compiles to this:
3124
3125 2 0 LOAD_FAST 0 (a)
3126 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003127 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003128
3129 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003130 10 PRINT_ITEM
3131 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003132 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003133 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003134
3135 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003136 19 PRINT_ITEM
3137 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003138 >> 21 LOAD_CONST 0 (None)
3139 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003140
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003141 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003142 15 and the co_lnotab will claim that execution has moved to
3143 line 3. This is at best misleading. In this case we could
3144 associate the POP_TOP with line 4, but that doesn't make
3145 sense in all cases (I think).
3146
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003147 What we do is only call the line trace function if the co_lnotab
3148 indicates we have jumped to the *start* of a line, i.e. if the
3149 current instruction offset matches the offset given for the
3150 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003151
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003152 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003153 Execution will jump from instruction offset 12 to offset 21.
3154 Then the co_lnotab would imply that execution has moved to line
3155 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003156
3157 Why do we set f_lineno when tracing? Well, consider the code
3158 above when 'a' is true. If stepping through this with 'n' in
3159 pdb, you would stop at line 1 with a "call" type event, then
3160 line events on lines 2 and 3, then a "return" type event -- but
3161 you would be shown line 5 during this event. This is a change
3162 from the behaviour in 2.2 and before, and I've found it
3163 confusing in practice. By setting and using f_lineno when
3164 tracing, one can report a line number different from that
3165 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003166 */
3167
Michael W. Hudson006c7522002-11-08 13:08:46 +00003168 int result = 0;
3169
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003170 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003171 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003172 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003173 unsigned char* p;
3174
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003175 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3176 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003177
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003178 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003179 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003180
3181 /* possible optimization: if f->f_lasti == instr_ub
3182 (likely to be a common case) then we already know
3183 instr_lb -- if we stored the matching value of p
3184 somwhere we could skip the first while loop. */
3185
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003186 /* see comments in compile.c for the description of
3187 co_lnotab. A point to remember: increments to p
3188 should come in pairs -- although we don't care about
3189 the line increments here, treating them as byte
3190 increments gets confusing, to say the least. */
3191
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003192 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003193 if (addr + *p > frame->f_lasti)
3194 break;
3195 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003196 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003197 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003198 --size;
3199 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003200
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003201 if (addr == frame->f_lasti) {
3202 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003203 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003204 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003205 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003206
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003207 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003208 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003209 addr += *p++;
3210 if (*p++)
3211 break;
3212 }
3213 *instr_ub = addr;
3214 }
3215 else {
3216 *instr_ub = INT_MAX;
3217 }
3218 }
Armin Rigobf57a142004-03-22 19:24:58 +00003219 else if (frame->f_lasti <= *instr_prev) {
3220 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003221 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003222 PyTrace_LINE, Py_None);
3223 }
3224 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003225 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003226}
3227
Fred Drake5755ce62001-06-27 19:19:46 +00003228void
3229PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003230{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003231 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003232 PyObject *temp = tstate->c_profileobj;
3233 Py_XINCREF(arg);
3234 tstate->c_profilefunc = NULL;
3235 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003236 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003237 Py_XDECREF(temp);
3238 tstate->c_profilefunc = func;
3239 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003240 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003241}
3242
3243void
3244PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3245{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003246 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003247 PyObject *temp = tstate->c_traceobj;
3248 Py_XINCREF(arg);
3249 tstate->c_tracefunc = NULL;
3250 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003251 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003252 Py_XDECREF(temp);
3253 tstate->c_tracefunc = func;
3254 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003255 tstate->use_tracing = ((func != NULL)
3256 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003257}
3258
Guido van Rossumb209a111997-04-29 18:18:01 +00003259PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003260PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003261{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003262 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003263 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003264 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003265 else
3266 return current_frame->f_builtins;
3267}
3268
Guido van Rossumb209a111997-04-29 18:18:01 +00003269PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003270PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003271{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003272 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003273 if (current_frame == NULL)
3274 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003275 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003276 return current_frame->f_locals;
3277}
3278
Guido van Rossumb209a111997-04-29 18:18:01 +00003279PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003280PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003281{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003282 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003283 if (current_frame == NULL)
3284 return NULL;
3285 else
3286 return current_frame->f_globals;
3287}
3288
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003289PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003290PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003291{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003292 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003293 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003294}
3295
Guido van Rossum6135a871995-01-09 17:53:26 +00003296int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003297PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003298{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003299 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003300 return current_frame == NULL ? 0 : current_frame->f_restricted;
3301}
3302
Guido van Rossumbe270261997-05-22 22:26:18 +00003303int
Tim Peters5ba58662001-07-16 02:29:45 +00003304PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003305{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003306 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003307 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003308
3309 if (current_frame != NULL) {
3310 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003311 const int compilerflags = codeflags & PyCF_MASK;
3312 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003313 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003314 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003315 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003316#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003317 if (codeflags & CO_GENERATOR_ALLOWED) {
3318 result = 1;
3319 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3320 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003321#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003322 }
3323 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003324}
3325
3326int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003327Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003328{
Guido van Rossumb209a111997-04-29 18:18:01 +00003329 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003330 if (f == NULL)
3331 return 0;
3332 if (!PyFile_SoftSpace(f, 0))
3333 return 0;
3334 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003335}
3336
Guido van Rossum3f5da241990-12-20 15:06:42 +00003337
Guido van Rossum681d79a1995-07-18 14:51:37 +00003338/* External interface to call any callable object.
3339 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003340
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003341#undef PyEval_CallObject
3342/* for backward compatibility: export this interface */
3343
Guido van Rossumb209a111997-04-29 18:18:01 +00003344PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003345PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003346{
Guido van Rossumb209a111997-04-29 18:18:01 +00003347 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003348}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003349#define PyEval_CallObject(func,arg) \
3350 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003351
Guido van Rossumb209a111997-04-29 18:18:01 +00003352PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003353PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003354{
Jeremy Hylton52820442001-01-03 23:52:36 +00003355 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003356
3357 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003358 arg = PyTuple_New(0);
3359 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003360 PyErr_SetString(PyExc_TypeError,
3361 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003362 return NULL;
3363 }
3364 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003365 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003366
Guido van Rossumb209a111997-04-29 18:18:01 +00003367 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003368 PyErr_SetString(PyExc_TypeError,
3369 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003370 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003371 return NULL;
3372 }
3373
Tim Peters6d6c1a32001-08-02 04:15:00 +00003374 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003375 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003376 return result;
3377}
3378
Tim Peters6d6c1a32001-08-02 04:15:00 +00003379char *
3380PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003381{
3382 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003383 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003384 else if (PyFunction_Check(func))
3385 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3386 else if (PyCFunction_Check(func))
3387 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3388 else if (PyClass_Check(func))
3389 return PyString_AsString(((PyClassObject*)func)->cl_name);
3390 else if (PyInstance_Check(func)) {
3391 return PyString_AsString(
3392 ((PyInstanceObject*)func)->in_class->cl_name);
3393 } else {
3394 return func->ob_type->tp_name;
3395 }
3396}
3397
Tim Peters6d6c1a32001-08-02 04:15:00 +00003398char *
3399PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003400{
3401 if (PyMethod_Check(func))
3402 return "()";
3403 else if (PyFunction_Check(func))
3404 return "()";
3405 else if (PyCFunction_Check(func))
3406 return "()";
3407 else if (PyClass_Check(func))
3408 return " constructor";
3409 else if (PyInstance_Check(func)) {
3410 return " instance";
3411 } else {
3412 return " object";
3413 }
3414}
3415
Jeremy Hylton52820442001-01-03 23:52:36 +00003416#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3417
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003418static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003419err_args(PyObject *func, int flags, int nargs)
3420{
3421 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003422 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003423 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003424 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003425 nargs);
3426 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003427 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003428 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003429 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003430 nargs);
3431}
3432
Nicholas Bastind858a772004-06-25 23:31:06 +00003433#define C_TRACE(call) \
3434if (tstate->use_tracing && tstate->c_profilefunc) { \
3435 if (call_trace(tstate->c_profilefunc, \
3436 tstate->c_profileobj, \
3437 tstate->frame, PyTrace_C_CALL, \
3438 func)) \
3439 { return NULL; } \
3440 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003441 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003442 if (x == NULL) { \
3443 if (call_trace (tstate->c_profilefunc, \
3444 tstate->c_profileobj, \
3445 tstate->frame, PyTrace_C_EXCEPTION, \
3446 func)) \
3447 { return NULL; } \
3448 } else { \
3449 if (call_trace(tstate->c_profilefunc, \
3450 tstate->c_profileobj, \
3451 tstate->frame, PyTrace_C_RETURN, \
3452 func)) \
3453 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003454 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003455 } \
3456} else { \
3457 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003458 }
3459
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003460static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003461call_function(PyObject ***pp_stack, int oparg
3462#ifdef WITH_TSC
3463 , uint64* pintr0, uint64* pintr1
3464#endif
3465 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003466{
3467 int na = oparg & 0xff;
3468 int nk = (oparg>>8) & 0xff;
3469 int n = na + 2 * nk;
3470 PyObject **pfunc = (*pp_stack) - n - 1;
3471 PyObject *func = *pfunc;
3472 PyObject *x, *w;
3473
Jeremy Hylton985eba52003-02-05 23:13:00 +00003474 /* Always dispatch PyCFunction first, because these are
3475 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003476 */
3477 if (PyCFunction_Check(func) && nk == 0) {
3478 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003479 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003480
3481 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003482 if (flags & (METH_NOARGS | METH_O)) {
3483 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3484 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003485 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003486 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003487 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003488 else if (flags & METH_O && na == 1) {
3489 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003490 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003491 Py_DECREF(arg);
3492 }
3493 else {
3494 err_args(func, flags, na);
3495 x = NULL;
3496 }
3497 }
3498 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003499 PyObject *callargs;
3500 callargs = load_args(pp_stack, na);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003501#ifdef WITH_TSC
3502 rdtscll(*pintr0);
3503#endif
Nicholas Bastind858a772004-06-25 23:31:06 +00003504 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003505#ifdef WITH_TSC
3506 rdtscll(*pintr1);
3507#endif
Tim Peters8a5c3c72004-04-05 19:36:21 +00003508 Py_XDECREF(callargs);
3509 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003510 } else {
3511 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3512 /* optimize access to bound methods */
3513 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003514 PCALL(PCALL_METHOD);
3515 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003516 Py_INCREF(self);
3517 func = PyMethod_GET_FUNCTION(func);
3518 Py_INCREF(func);
3519 Py_DECREF(*pfunc);
3520 *pfunc = self;
3521 na++;
3522 n++;
3523 } else
3524 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003525#ifdef WITH_TSC
3526 rdtscll(*pintr0);
3527#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003528 if (PyFunction_Check(func))
3529 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003530 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003531 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003532#ifdef WITH_TSC
3533 rdtscll(*pintr1);
3534#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003535 Py_DECREF(func);
3536 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003537
Jeremy Hylton985eba52003-02-05 23:13:00 +00003538 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003539 while ((*pp_stack) > pfunc) {
3540 w = EXT_POP(*pp_stack);
3541 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003542 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003543 }
3544 return x;
3545}
3546
Jeremy Hylton192690e2002-08-16 18:36:11 +00003547/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003548 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003549 For the simplest case -- a function that takes only positional
3550 arguments and is called with only positional arguments -- it
3551 inlines the most primitive frame setup code from
3552 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3553 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003554*/
3555
3556static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003557fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003558{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003559 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003560 PyObject *globals = PyFunction_GET_GLOBALS(func);
3561 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3562 PyObject **d = NULL;
3563 int nd = 0;
3564
Jeremy Hylton985eba52003-02-05 23:13:00 +00003565 PCALL(PCALL_FUNCTION);
3566 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003567 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003568 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3569 PyFrameObject *f;
3570 PyObject *retval = NULL;
3571 PyThreadState *tstate = PyThreadState_GET();
3572 PyObject **fastlocals, **stack;
3573 int i;
3574
3575 PCALL(PCALL_FASTER_FUNCTION);
3576 assert(globals != NULL);
3577 /* XXX Perhaps we should create a specialized
3578 PyFrame_New() that doesn't take locals, but does
3579 take builtins without sanity checking them.
3580 */
3581 f = PyFrame_New(tstate, co, globals, NULL);
3582 if (f == NULL)
3583 return NULL;
3584
3585 fastlocals = f->f_localsplus;
3586 stack = (*pp_stack) - n;
3587
3588 for (i = 0; i < n; i++) {
3589 Py_INCREF(*stack);
3590 fastlocals[i] = *stack++;
3591 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003592 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003593 assert(tstate != NULL);
3594 ++tstate->recursion_depth;
3595 Py_DECREF(f);
3596 --tstate->recursion_depth;
3597 return retval;
3598 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003599 if (argdefs != NULL) {
3600 d = &PyTuple_GET_ITEM(argdefs, 0);
3601 nd = ((PyTupleObject *)argdefs)->ob_size;
3602 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003603 return PyEval_EvalCodeEx(co, globals,
3604 (PyObject *)NULL, (*pp_stack)-n, na,
3605 (*pp_stack)-2*nk, nk, d, nd,
3606 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003607}
3608
3609static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003610update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3611 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003612{
3613 PyObject *kwdict = NULL;
3614 if (orig_kwdict == NULL)
3615 kwdict = PyDict_New();
3616 else {
3617 kwdict = PyDict_Copy(orig_kwdict);
3618 Py_DECREF(orig_kwdict);
3619 }
3620 if (kwdict == NULL)
3621 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003622 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003623 int err;
3624 PyObject *value = EXT_POP(*pp_stack);
3625 PyObject *key = EXT_POP(*pp_stack);
3626 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003627 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003628 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003629 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003630 PyEval_GetFuncName(func),
3631 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003632 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003633 Py_DECREF(key);
3634 Py_DECREF(value);
3635 Py_DECREF(kwdict);
3636 return NULL;
3637 }
3638 err = PyDict_SetItem(kwdict, key, value);
3639 Py_DECREF(key);
3640 Py_DECREF(value);
3641 if (err) {
3642 Py_DECREF(kwdict);
3643 return NULL;
3644 }
3645 }
3646 return kwdict;
3647}
3648
3649static PyObject *
3650update_star_args(int nstack, int nstar, PyObject *stararg,
3651 PyObject ***pp_stack)
3652{
3653 PyObject *callargs, *w;
3654
3655 callargs = PyTuple_New(nstack + nstar);
3656 if (callargs == NULL) {
3657 return NULL;
3658 }
3659 if (nstar) {
3660 int i;
3661 for (i = 0; i < nstar; i++) {
3662 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3663 Py_INCREF(a);
3664 PyTuple_SET_ITEM(callargs, nstack + i, a);
3665 }
3666 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003667 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003668 w = EXT_POP(*pp_stack);
3669 PyTuple_SET_ITEM(callargs, nstack, w);
3670 }
3671 return callargs;
3672}
3673
3674static PyObject *
3675load_args(PyObject ***pp_stack, int na)
3676{
3677 PyObject *args = PyTuple_New(na);
3678 PyObject *w;
3679
3680 if (args == NULL)
3681 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003682 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003683 w = EXT_POP(*pp_stack);
3684 PyTuple_SET_ITEM(args, na, w);
3685 }
3686 return args;
3687}
3688
3689static PyObject *
3690do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3691{
3692 PyObject *callargs = NULL;
3693 PyObject *kwdict = NULL;
3694 PyObject *result = NULL;
3695
3696 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003697 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003698 if (kwdict == NULL)
3699 goto call_fail;
3700 }
3701 callargs = load_args(pp_stack, na);
3702 if (callargs == NULL)
3703 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003704#ifdef CALL_PROFILE
3705 /* At this point, we have to look at the type of func to
3706 update the call stats properly. Do it here so as to avoid
3707 exposing the call stats machinery outside ceval.c
3708 */
3709 if (PyFunction_Check(func))
3710 PCALL(PCALL_FUNCTION);
3711 else if (PyMethod_Check(func))
3712 PCALL(PCALL_METHOD);
3713 else if (PyType_Check(func))
3714 PCALL(PCALL_TYPE);
3715 else
3716 PCALL(PCALL_OTHER);
3717#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003718 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003719 call_fail:
3720 Py_XDECREF(callargs);
3721 Py_XDECREF(kwdict);
3722 return result;
3723}
3724
3725static PyObject *
3726ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3727{
3728 int nstar = 0;
3729 PyObject *callargs = NULL;
3730 PyObject *stararg = NULL;
3731 PyObject *kwdict = NULL;
3732 PyObject *result = NULL;
3733
3734 if (flags & CALL_FLAG_KW) {
3735 kwdict = EXT_POP(*pp_stack);
3736 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003737 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003738 "%s%s argument after ** "
3739 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003740 PyEval_GetFuncName(func),
3741 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003742 goto ext_call_fail;
3743 }
3744 }
3745 if (flags & CALL_FLAG_VAR) {
3746 stararg = EXT_POP(*pp_stack);
3747 if (!PyTuple_Check(stararg)) {
3748 PyObject *t = NULL;
3749 t = PySequence_Tuple(stararg);
3750 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003751 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3752 PyErr_Format(PyExc_TypeError,
3753 "%s%s argument after * "
3754 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003755 PyEval_GetFuncName(func),
3756 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003757 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003758 goto ext_call_fail;
3759 }
3760 Py_DECREF(stararg);
3761 stararg = t;
3762 }
3763 nstar = PyTuple_GET_SIZE(stararg);
3764 }
3765 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003766 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003767 if (kwdict == NULL)
3768 goto ext_call_fail;
3769 }
3770 callargs = update_star_args(na, nstar, stararg, pp_stack);
3771 if (callargs == NULL)
3772 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003773#ifdef CALL_PROFILE
3774 /* At this point, we have to look at the type of func to
3775 update the call stats properly. Do it here so as to avoid
3776 exposing the call stats machinery outside ceval.c
3777 */
3778 if (PyFunction_Check(func))
3779 PCALL(PCALL_FUNCTION);
3780 else if (PyMethod_Check(func))
3781 PCALL(PCALL_METHOD);
3782 else if (PyType_Check(func))
3783 PCALL(PCALL_TYPE);
3784 else
3785 PCALL(PCALL_OTHER);
3786#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003787 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003788 ext_call_fail:
3789 Py_XDECREF(callargs);
3790 Py_XDECREF(kwdict);
3791 Py_XDECREF(stararg);
3792 return result;
3793}
3794
Guido van Rossum3b9c6671996-07-30 18:40:29 +00003795#define SLICE_ERROR_MSG \
3796 "standard sequence type does not support step size other than one"
3797
Tim Peterscb479e72001-12-16 19:11:44 +00003798/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3799 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3800 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3801*/
Tim Petersb5196382001-12-16 19:44:20 +00003802/* Note: If v is NULL, return success without storing into *pi. This
3803 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3804 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003805*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003806int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003807_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003808{
Tim Petersb5196382001-12-16 19:44:20 +00003809 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003810 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003811 if (PyInt_Check(v)) {
3812 x = PyInt_AsLong(v);
3813 } else if (PyLong_Check(v)) {
3814 x = PyLong_AsLong(v);
3815 if (x==-1 && PyErr_Occurred()) {
3816 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003817 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003818
Guido van Rossumac7be682001-01-17 15:42:30 +00003819 if (!PyErr_ExceptionMatches(
3820 PyExc_OverflowError)) {
3821 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003822 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003823 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003824 }
3825
Guido van Rossumac7be682001-01-17 15:42:30 +00003826 /* Clear the OverflowError */
3827 PyErr_Clear();
3828
3829 /* It's an overflow error, so we need to
3830 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003831 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003832 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003833
3834 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003835 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003836 if (long_zero == NULL)
3837 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003838
3839 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003840 cmp = PyObject_RichCompareBool(v, long_zero,
3841 Py_GT);
3842 Py_DECREF(long_zero);
3843 if (cmp < 0)
3844 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003845 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003846 x = INT_MAX;
3847 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003848 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003849 }
3850 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003851 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003852 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003853 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003854 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003855 /* Truncate -- very long indices are truncated anyway */
3856 if (x > INT_MAX)
3857 x = INT_MAX;
3858 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003859 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003860 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003861 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003862 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003863}
3864
Guido van Rossum50d756e2001-08-18 17:43:36 +00003865#undef ISINT
3866#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3867
Guido van Rossumb209a111997-04-29 18:18:01 +00003868static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003869apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003870{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003871 PyTypeObject *tp = u->ob_type;
3872 PySequenceMethods *sq = tp->tp_as_sequence;
3873
3874 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3875 int ilow = 0, ihigh = INT_MAX;
3876 if (!_PyEval_SliceIndex(v, &ilow))
3877 return NULL;
3878 if (!_PyEval_SliceIndex(w, &ihigh))
3879 return NULL;
3880 return PySequence_GetSlice(u, ilow, ihigh);
3881 }
3882 else {
3883 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003884 if (slice != NULL) {
3885 PyObject *res = PyObject_GetItem(u, slice);
3886 Py_DECREF(slice);
3887 return res;
3888 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003889 else
3890 return NULL;
3891 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003892}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003893
3894static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003895assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3896 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003897{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003898 PyTypeObject *tp = u->ob_type;
3899 PySequenceMethods *sq = tp->tp_as_sequence;
3900
3901 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3902 int ilow = 0, ihigh = INT_MAX;
3903 if (!_PyEval_SliceIndex(v, &ilow))
3904 return -1;
3905 if (!_PyEval_SliceIndex(w, &ihigh))
3906 return -1;
3907 if (x == NULL)
3908 return PySequence_DelSlice(u, ilow, ihigh);
3909 else
3910 return PySequence_SetSlice(u, ilow, ihigh, x);
3911 }
3912 else {
3913 PyObject *slice = PySlice_New(v, w, NULL);
3914 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003915 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003916 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003917 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003918 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003919 res = PyObject_DelItem(u, slice);
3920 Py_DECREF(slice);
3921 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003922 }
3923 else
3924 return -1;
3925 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003926}
3927
Guido van Rossumb209a111997-04-29 18:18:01 +00003928static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003929cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003930{
Guido van Rossumac7be682001-01-17 15:42:30 +00003931 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003932 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003933 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003934 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003935 break;
3936 case PyCmp_IS_NOT:
3937 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003938 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003939 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003940 res = PySequence_Contains(w, v);
3941 if (res < 0)
3942 return NULL;
3943 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003944 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003945 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003946 if (res < 0)
3947 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003948 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003949 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003950 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003951 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003952 break;
3953 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003954 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003955 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003956 v = res ? Py_True : Py_False;
3957 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003958 return v;
3959}
3960
Thomas Wouters52152252000-08-17 22:55:00 +00003961static PyObject *
3962import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003963{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003964 PyObject *x;
3965
3966 x = PyObject_GetAttr(v, name);
3967 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003968 PyErr_Format(PyExc_ImportError,
3969 "cannot import name %.230s",
3970 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003971 }
Thomas Wouters52152252000-08-17 22:55:00 +00003972 return x;
3973}
Guido van Rossumac7be682001-01-17 15:42:30 +00003974
Thomas Wouters52152252000-08-17 22:55:00 +00003975static int
3976import_all_from(PyObject *locals, PyObject *v)
3977{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003978 PyObject *all = PyObject_GetAttrString(v, "__all__");
3979 PyObject *dict, *name, *value;
3980 int skip_leading_underscores = 0;
3981 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003982
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003983 if (all == NULL) {
3984 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3985 return -1; /* Unexpected error */
3986 PyErr_Clear();
3987 dict = PyObject_GetAttrString(v, "__dict__");
3988 if (dict == NULL) {
3989 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3990 return -1;
3991 PyErr_SetString(PyExc_ImportError,
3992 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003993 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003994 }
3995 all = PyMapping_Keys(dict);
3996 Py_DECREF(dict);
3997 if (all == NULL)
3998 return -1;
3999 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004000 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004001
4002 for (pos = 0, err = 0; ; pos++) {
4003 name = PySequence_GetItem(all, pos);
4004 if (name == NULL) {
4005 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4006 err = -1;
4007 else
4008 PyErr_Clear();
4009 break;
4010 }
4011 if (skip_leading_underscores &&
4012 PyString_Check(name) &&
4013 PyString_AS_STRING(name)[0] == '_')
4014 {
4015 Py_DECREF(name);
4016 continue;
4017 }
4018 value = PyObject_GetAttr(v, name);
4019 if (value == NULL)
4020 err = -1;
4021 else
4022 err = PyDict_SetItem(locals, name, value);
4023 Py_DECREF(name);
4024 Py_XDECREF(value);
4025 if (err != 0)
4026 break;
4027 }
4028 Py_DECREF(all);
4029 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004030}
4031
Guido van Rossumb209a111997-04-29 18:18:01 +00004032static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004033build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004034{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004035 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004036
4037 if (PyDict_Check(methods))
4038 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004039 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004040 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004041 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4042 base = PyTuple_GET_ITEM(bases, 0);
4043 metaclass = PyObject_GetAttrString(base, "__class__");
4044 if (metaclass == NULL) {
4045 PyErr_Clear();
4046 metaclass = (PyObject *)base->ob_type;
4047 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004048 }
4049 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004050 else {
4051 PyObject *g = PyEval_GetGlobals();
4052 if (g != NULL && PyDict_Check(g))
4053 metaclass = PyDict_GetItemString(g, "__metaclass__");
4054 if (metaclass == NULL)
4055 metaclass = (PyObject *) &PyClass_Type;
4056 Py_INCREF(metaclass);
4057 }
4058 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4059 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004060 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4061 /* A type error here likely means that the user passed
4062 in a base that was not a class (such the random module
4063 instead of the random.random type). Help them out with
4064 a more informative error message */
4065 PyErr_SetString(PyExc_TypeError,
4066 "Error when calling the metaclass.\n" \
4067 "Make sure the base arguments are valid.");
4068 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004069 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004070}
4071
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004072static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004073exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4074 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004075{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004076 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004077 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004078 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004079
Guido van Rossumb209a111997-04-29 18:18:01 +00004080 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4081 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004082 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004083 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004084 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004085 locals = PyTuple_GetItem(prog, 2);
4086 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004087 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004088 if (globals == Py_None) {
4089 globals = PyEval_GetGlobals();
4090 if (locals == Py_None) {
4091 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004092 plain = 1;
4093 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004094 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004095 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004096 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004097 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004098 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004099 !PyCode_Check(prog) &&
4100 !PyFile_Check(prog)) {
4101 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004102 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004103 return -1;
4104 }
Fred Drake661ea262000-10-24 19:57:45 +00004105 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004106 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004107 "exec: arg 2 must be a dictionary or None");
4108 return -1;
4109 }
4110 if (!PyDict_Check(locals)) {
4111 PyErr_SetString(PyExc_TypeError,
4112 "exec: arg 3 must be a dictionary or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004113 return -1;
4114 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004115 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004116 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004117 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004118 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4119 PyErr_SetString(PyExc_TypeError,
4120 "code object passed to exec may not contain free variables");
4121 return -1;
4122 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004123 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004124 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004125 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004126 FILE *fp = PyFile_AsFile(prog);
4127 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004128 PyCompilerFlags cf;
4129 cf.cf_flags = 0;
4130 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004131 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004132 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004133 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004134 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004135 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004136 }
4137 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004138 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004139 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004140 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004141 cf.cf_flags = 0;
4142#ifdef Py_USING_UNICODE
4143 if (PyUnicode_Check(prog)) {
4144 tmp = PyUnicode_AsUTF8String(prog);
4145 if (tmp == NULL)
4146 return -1;
4147 prog = tmp;
4148 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4149 }
4150#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004151 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004152 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004153 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004154 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004155 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004156 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004157 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004158 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004159 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004160 if (plain)
4161 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004162 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004163 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004164 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004165 return 0;
4166}
Guido van Rossum24c13741995-02-14 09:42:43 +00004167
Guido van Rossumac7be682001-01-17 15:42:30 +00004168static void
Paul Prescode68140d2000-08-30 20:25:01 +00004169format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4170{
4171 char *obj_str;
4172
4173 if (!obj)
4174 return;
4175
4176 obj_str = PyString_AsString(obj);
4177 if (!obj_str)
4178 return;
4179
4180 PyErr_Format(exc, format_str, obj_str);
4181}
Guido van Rossum950361c1997-01-24 13:49:28 +00004182
4183#ifdef DYNAMIC_EXECUTION_PROFILE
4184
Skip Montanarof118cb12001-10-15 20:51:38 +00004185static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004186getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004187{
4188 int i;
4189 PyObject *l = PyList_New(256);
4190 if (l == NULL) return NULL;
4191 for (i = 0; i < 256; i++) {
4192 PyObject *x = PyInt_FromLong(a[i]);
4193 if (x == NULL) {
4194 Py_DECREF(l);
4195 return NULL;
4196 }
4197 PyList_SetItem(l, i, x);
4198 }
4199 for (i = 0; i < 256; i++)
4200 a[i] = 0;
4201 return l;
4202}
4203
4204PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004205_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004206{
4207#ifndef DXPAIRS
4208 return getarray(dxp);
4209#else
4210 int i;
4211 PyObject *l = PyList_New(257);
4212 if (l == NULL) return NULL;
4213 for (i = 0; i < 257; i++) {
4214 PyObject *x = getarray(dxpairs[i]);
4215 if (x == NULL) {
4216 Py_DECREF(l);
4217 return NULL;
4218 }
4219 PyList_SetItem(l, i, x);
4220 }
4221 return l;
4222#endif
4223}
4224
4225#endif