blob: 4d26a7a7359efad99a6952e7e1429a3207ac47fe [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öwis87fa7852004-08-29 15:51:52 +000019#ifndef WITH_TSC
20#define rdtscll(var)
21#else /*WITH_TSC defined*/
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000022
23typedef unsigned long long uint64;
24
Michael W. Hudson800ba232004-08-12 18:19:17 +000025#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
26 section should work for GCC on any PowerPC platform,
27 irrespective of OS. POWER? Who knows :-) */
28
29#define rdtscll(var) ppc_getcounter(&var)
30
31static void
32ppc_getcounter(uint64 *v)
33{
34 register unsigned long tbu, tb, tbu2;
35
36 loop:
37 asm volatile ("mftbu %0" : "=r" (tbu) );
38 asm volatile ("mftb %0" : "=r" (tb) );
39 asm volatile ("mftbu %0" : "=r" (tbu2));
40 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
41
42 /* The slightly peculiar way of writing the next lines is
43 compiled better by GCC than any other way I tried. */
44 ((long*)(v))[0] = tbu;
45 ((long*)(v))[1] = tb;
46}
47
48#else /* this section is for linux/x86 */
49
50#include <asm/msr.h>
51
52#endif
53
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000054void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
55 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
56{
57 uint64 intr, inst, loop;
58 PyThreadState *tstate = PyThreadState_Get();
59 if (!tstate->interp->tscdump)
60 return;
61 intr = intr1 - intr0;
62 inst = inst1 - inst0 - intr;
63 loop = loop1 - loop0 - intr;
64 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
65 opcode, ticked, inst, loop);
66}
Michael W. Hudson800ba232004-08-12 18:19:17 +000067
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000068#endif
69
Guido van Rossum04691fc1992-08-12 15:35:34 +000070/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000071/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000072
Guido van Rossum408027e1996-12-30 16:17:54 +000073#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000074/* For debugging the interpreter: */
75#define LLTRACE 1 /* Low-level trace feature */
76#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000077#endif
78
Jeremy Hylton52820442001-01-03 23:52:36 +000079typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000080
Guido van Rossum374a9221991-04-04 10:40:29 +000081/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000082#ifdef WITH_TSC
83static PyObject *call_function(PyObject ***, int, uint64*, uint64*);
84#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +000085static PyObject *call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000086#endif
Jeremy Hylton52820442001-01-03 23:52:36 +000087static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000088static PyObject *do_call(PyObject *, PyObject ***, int, int);
89static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000090static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000091static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000092static PyObject *load_args(PyObject ***, int);
93#define CALL_FLAG_VAR 1
94#define CALL_FLAG_KW 2
95
Guido van Rossum0a066c01992-03-27 17:29:15 +000096#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000097static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000098#endif
Fred Drake5755ce62001-06-27 19:19:46 +000099static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
100 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +0000101static void call_trace_protected(Py_tracefunc, PyObject *,
102 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +0000103static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000104static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000105 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000106
Tim Petersdbd9ba62000-07-09 03:09:57 +0000107static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
108static int assign_slice(PyObject *, PyObject *,
109 PyObject *, PyObject *);
110static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000111static PyObject *import_from(PyObject *, PyObject *);
112static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000113static PyObject *build_class(PyObject *, PyObject *, PyObject *);
114static int exec_statement(PyFrameObject *,
115 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000116static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
117static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000118static void format_exc_check_arg(PyObject *, char *, PyObject *);
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000119static PyObject *string_concatenate(PyObject *, PyObject *,
120 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000121
Paul Prescode68140d2000-08-30 20:25:01 +0000122#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000123 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000124#define GLOBAL_NAME_ERROR_MSG \
125 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000126#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000127 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000128#define UNBOUNDFREE_ERROR_MSG \
129 "free variable '%.200s' referenced before assignment" \
130 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000131
Guido van Rossum950361c1997-01-24 13:49:28 +0000132/* Dynamic execution profile */
133#ifdef DYNAMIC_EXECUTION_PROFILE
134#ifdef DXPAIRS
135static long dxpairs[257][256];
136#define dxp dxpairs[256]
137#else
138static long dxp[256];
139#endif
140#endif
141
Jeremy Hylton985eba52003-02-05 23:13:00 +0000142/* Function call profile */
143#ifdef CALL_PROFILE
144#define PCALL_NUM 11
145static int pcall[PCALL_NUM];
146
147#define PCALL_ALL 0
148#define PCALL_FUNCTION 1
149#define PCALL_FAST_FUNCTION 2
150#define PCALL_FASTER_FUNCTION 3
151#define PCALL_METHOD 4
152#define PCALL_BOUND_METHOD 5
153#define PCALL_CFUNCTION 6
154#define PCALL_TYPE 7
155#define PCALL_GENERATOR 8
156#define PCALL_OTHER 9
157#define PCALL_POP 10
158
159/* Notes about the statistics
160
161 PCALL_FAST stats
162
163 FAST_FUNCTION means no argument tuple needs to be created.
164 FASTER_FUNCTION means that the fast-path frame setup code is used.
165
166 If there is a method call where the call can be optimized by changing
167 the argument tuple and calling the function directly, it gets recorded
168 twice.
169
170 As a result, the relationship among the statistics appears to be
171 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
172 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
173 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
174 PCALL_METHOD > PCALL_BOUND_METHOD
175*/
176
177#define PCALL(POS) pcall[POS]++
178
179PyObject *
180PyEval_GetCallStats(PyObject *self)
181{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000182 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000183 pcall[0], pcall[1], pcall[2], pcall[3],
184 pcall[4], pcall[5], pcall[6], pcall[7],
185 pcall[8], pcall[9]);
186}
187#else
188#define PCALL(O)
189
190PyObject *
191PyEval_GetCallStats(PyObject *self)
192{
193 Py_INCREF(Py_None);
194 return Py_None;
195}
196#endif
197
Tim Peters5ca576e2001-06-18 22:08:13 +0000198
Guido van Rossume59214e1994-08-30 08:01:59 +0000199#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000200
Guido van Rossum2571cc81999-04-07 16:07:23 +0000201#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000202#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000203#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000204#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000205
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000206static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000207static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000208
Tim Peters7f468f22004-10-11 02:40:51 +0000209int
210PyEval_ThreadsInitialized(void)
211{
212 return interpreter_lock != 0;
213}
214
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000215void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000216PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000219 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000220 interpreter_lock = PyThread_allocate_lock();
221 PyThread_acquire_lock(interpreter_lock, 1);
222 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000223}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000224
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000225void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000226PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000227{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000228 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229}
230
231void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000232PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000233{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000234 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235}
236
237void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000238PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000239{
240 if (tstate == NULL)
241 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000242 /* Check someone has called PyEval_InitThreads() to create the lock */
243 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000244 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000245 if (PyThreadState_Swap(tstate) != NULL)
246 Py_FatalError(
247 "PyEval_AcquireThread: non-NULL old thread state");
248}
249
250void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000251PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000252{
253 if (tstate == NULL)
254 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
255 if (PyThreadState_Swap(NULL) != tstate)
256 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000257 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000258}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000259
260/* This function is called from PyOS_AfterFork to ensure that newly
261 created child processes don't hold locks referring to threads which
262 are not running in the child process. (This could also be done using
263 pthread_atfork mechanism, at least for the pthreads implementation.) */
264
265void
266PyEval_ReInitThreads(void)
267{
268 if (!interpreter_lock)
269 return;
270 /*XXX Can't use PyThread_free_lock here because it does too
271 much error-checking. Doing this cleanly would require
272 adding a new function to each thread_*.h. Instead, just
273 create a new lock and waste a little bit of memory */
274 interpreter_lock = PyThread_allocate_lock();
275 PyThread_acquire_lock(interpreter_lock, 1);
276 main_thread = PyThread_get_thread_ident();
277}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278#endif
279
Guido van Rossumff4949e1992-08-05 19:58:53 +0000280/* Functions save_thread and restore_thread are always defined so
281 dynamically loaded modules needn't be compiled separately for use
282 with and without threads: */
283
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000284PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000285PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000286{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000287 PyThreadState *tstate = PyThreadState_Swap(NULL);
288 if (tstate == NULL)
289 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000290#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000291 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000292 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000293#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000294 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295}
296
297void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000298PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000300 if (tstate == NULL)
301 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000302#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000304 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000305 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307 }
308#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000309 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310}
311
312
Guido van Rossuma9672091994-09-14 13:31:22 +0000313/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
314 signal handlers or Mac I/O completion routines) can schedule calls
315 to a function to be called synchronously.
316 The synchronous function is called with one void* argument.
317 It should return 0 for success or -1 for failure -- failure should
318 be accompanied by an exception.
319
320 If registry succeeds, the registry function returns 0; if it fails
321 (e.g. due to too many pending calls) it returns -1 (without setting
322 an exception condition).
323
324 Note that because registry may occur from within signal handlers,
325 or other asynchronous events, calling malloc() is unsafe!
326
327#ifdef WITH_THREAD
328 Any thread can schedule pending calls, but only the main thread
329 will execute them.
330#endif
331
332 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
333 There are two possible race conditions:
334 (1) nested asynchronous registry calls;
335 (2) registry calls made while pending calls are being processed.
336 While (1) is very unlikely, (2) is a real possibility.
337 The current code is safe against (2), but not against (1).
338 The safety against (2) is derived from the fact that only one
339 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000340
Guido van Rossuma027efa1997-05-05 20:56:21 +0000341 XXX Darn! With the advent of thread state, we should have an array
342 of pending calls per thread in the thread state! Later...
343*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000344
Guido van Rossuma9672091994-09-14 13:31:22 +0000345#define NPENDINGCALLS 32
346static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000347 int (*func)(void *);
348 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000349} pendingcalls[NPENDINGCALLS];
350static volatile int pendingfirst = 0;
351static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000352static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000353
354int
Thomas Wouters334fb892000-07-25 12:56:38 +0000355Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000356{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000357 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000358 int i, j;
359 /* XXX Begin critical section */
360 /* XXX If you want this to be safe against nested
361 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000362 if (busy)
363 return -1;
364 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000365 i = pendinglast;
366 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000367 if (j == pendingfirst) {
368 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000369 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000370 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000371 pendingcalls[i].func = func;
372 pendingcalls[i].arg = arg;
373 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000374
375 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000376 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000377 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000378 /* XXX End critical section */
379 return 0;
380}
381
Guido van Rossum180d7b41994-09-29 09:45:57 +0000382int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000384{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000385 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000386#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000387 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000388 return 0;
389#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000390 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000391 return 0;
392 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000393 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000394 for (;;) {
395 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000396 int (*func)(void *);
397 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000398 i = pendingfirst;
399 if (i == pendinglast)
400 break; /* Queue empty */
401 func = pendingcalls[i].func;
402 arg = pendingcalls[i].arg;
403 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000404 if (func(arg) < 0) {
405 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000406 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000407 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000408 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000409 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000410 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000411 return 0;
412}
413
414
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000415/* The interpreter's recursion limit */
416
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000417static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000418int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000419
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000420int
421Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000422{
423 return recursion_limit;
424}
425
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000426void
427Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000428{
429 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000430 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000431}
432
Armin Rigo2b3eb402003-10-28 12:05:48 +0000433/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
434 if the recursion_depth reaches _Py_CheckRecursionLimit.
435 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
436 to guarantee that _Py_CheckRecursiveCall() is regularly called.
437 Without USE_STACKCHECK, there is no need for this. */
438int
439_Py_CheckRecursiveCall(char *where)
440{
441 PyThreadState *tstate = PyThreadState_GET();
442
443#ifdef USE_STACKCHECK
444 if (PyOS_CheckStack()) {
445 --tstate->recursion_depth;
446 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
447 return -1;
448 }
449#endif
450 if (tstate->recursion_depth > recursion_limit) {
451 --tstate->recursion_depth;
452 PyErr_Format(PyExc_RuntimeError,
453 "maximum recursion depth exceeded%s",
454 where);
455 return -1;
456 }
457 _Py_CheckRecursionLimit = recursion_limit;
458 return 0;
459}
460
Guido van Rossum374a9221991-04-04 10:40:29 +0000461/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000462enum why_code {
463 WHY_NOT = 0x0001, /* No error */
464 WHY_EXCEPTION = 0x0002, /* Exception occurred */
465 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
466 WHY_RETURN = 0x0008, /* 'return' statement */
467 WHY_BREAK = 0x0010, /* 'break' statement */
468 WHY_CONTINUE = 0x0020, /* 'continue' statement */
469 WHY_YIELD = 0x0040 /* 'yield' operator */
470};
Guido van Rossum374a9221991-04-04 10:40:29 +0000471
Raymond Hettinger7c958652004-04-06 10:11:10 +0000472static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000473static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000474
Skip Montanarod581d772002-09-03 20:10:45 +0000475/* for manipulating the thread switch and periodic "stuff" - used to be
476 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000477int _Py_CheckInterval = 100;
478volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000479
Guido van Rossumb209a111997-04-29 18:18:01 +0000480PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000481PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000482{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000483 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000484 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000485 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000486 (PyObject **)NULL, 0,
487 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000488 (PyObject **)NULL, 0,
489 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000490}
491
492
493/* Interpreter main loop */
494
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000495PyObject *
496PyEval_EvalFrame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000497{
Guido van Rossum950361c1997-01-24 13:49:28 +0000498#ifdef DXPAIRS
499 int lastopcode = 0;
500#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000501 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000502 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000503 register int opcode; /* Current opcode */
504 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000505 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000506 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000507 register PyObject *x; /* Result object -- NULL if error */
508 register PyObject *v; /* Temporary objects popped off stack */
509 register PyObject *w;
510 register PyObject *u;
511 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000512 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000513 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000514 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000515 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000516 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000517
Tim Peters8a5c3c72004-04-05 19:36:21 +0000518 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000519
520 not (instr_lb <= current_bytecode_offset < instr_ub)
521
Tim Peters8a5c3c72004-04-05 19:36:21 +0000522 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000523 initial values are such as to make this false the first
524 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000525 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000526
Guido van Rossumd076c731998-10-07 19:42:25 +0000527 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000528 PyObject *names;
529 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000530#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000531 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000532#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000533#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000534 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000535 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000536#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000537
Neal Norwitza81d2202002-07-14 00:27:26 +0000538/* Tuple access macros */
539
540#ifndef Py_DEBUG
541#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
542#else
543#define GETITEM(v, i) PyTuple_GetItem((v), (i))
544#endif
545
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000546#ifdef WITH_TSC
547/* Use Pentium timestamp counter to mark certain events:
548 inst0 -- beginning of switch statement for opcode dispatch
549 inst1 -- end of switch statement (may be skipped)
550 loop0 -- the top of the mainloop
551 loop1 -- place where control returns again to top of mainloop
552 (may be skipped)
553 intr1 -- beginning of long interruption
554 intr2 -- end of long interruption
555
556 Many opcodes call out to helper C functions. In some cases, the
557 time in those functions should be counted towards the time for the
558 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
559 calls another Python function; there's no point in charge all the
560 bytecode executed by the called function to the caller.
561
562 It's hard to make a useful judgement statically. In the presence
563 of operator overloading, it's impossible to tell if a call will
564 execute new Python code or not.
565
566 It's a case-by-case judgement. I'll use intr1 for the following
567 cases:
568
569 EXEC_STMT
570 IMPORT_STAR
571 IMPORT_FROM
572 CALL_FUNCTION (and friends)
573
574 */
575 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
576 int ticked = 0;
577
578 rdtscll(inst0);
579 rdtscll(inst1);
580 rdtscll(loop0);
581 rdtscll(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000582
583 /* shut up the compiler */
584 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000585#endif
586
Guido van Rossum374a9221991-04-04 10:40:29 +0000587/* Code access macros */
588
Guido van Rossumd076c731998-10-07 19:42:25 +0000589#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000590#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000591#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000592#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000593#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000594#define JUMPBY(x) (next_instr += (x))
595
Raymond Hettingerf606f872003-03-16 03:11:04 +0000596/* OpCode prediction macros
597 Some opcodes tend to come in pairs thus making it possible to predict
598 the second code when the first is run. For example, COMPARE_OP is often
599 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
600 followed by a POP_TOP.
601
602 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000603 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000604 processor has a high likelihood of making its own successful branch
605 prediction which results in a nearly zero overhead transition to the
606 next opcode.
607
608 A successful prediction saves a trip through the eval-loop including
609 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000610
Tim Peters8a5c3c72004-04-05 19:36:21 +0000611 If collecting opcode statistics, turn off prediction so that
612 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000613 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000614*/
615
Raymond Hettingera7216982004-02-08 19:59:27 +0000616#ifdef DYNAMIC_EXECUTION_PROFILE
617#define PREDICT(op) if (0) goto PRED_##op
618#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000619#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000620#endif
621
Raymond Hettingerf606f872003-03-16 03:11:04 +0000622#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000623#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000624
Guido van Rossum374a9221991-04-04 10:40:29 +0000625/* Stack manipulation macros */
626
627#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
628#define EMPTY() (STACK_LEVEL() == 0)
629#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000630#define SECOND() (stack_pointer[-2])
631#define THIRD() (stack_pointer[-3])
632#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000633#define SET_TOP(v) (stack_pointer[-1] = (v))
634#define SET_SECOND(v) (stack_pointer[-2] = (v))
635#define SET_THIRD(v) (stack_pointer[-3] = (v))
636#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000637#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000638#define BASIC_PUSH(v) (*stack_pointer++ = (v))
639#define BASIC_POP() (*--stack_pointer)
640
Guido van Rossum96a42c81992-01-12 02:29:51 +0000641#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000642#define PUSH(v) { (void)(BASIC_PUSH(v), \
643 lltrace && prtrace(TOP(), "push")); \
644 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000645#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000646#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
647 lltrace && prtrace(TOP(), "stackadj")); \
648 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000649#else
650#define PUSH(v) BASIC_PUSH(v)
651#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000652#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000653#endif
654
Guido van Rossum681d79a1995-07-18 14:51:37 +0000655/* Local variable macros */
656
657#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000658
659/* The SETLOCAL() macro must not DECREF the local variable in-place and
660 then store the new value; it must copy the old value to a temporary
661 value, then store the new value, and then DECREF the temporary value.
662 This is because it is possible that during the DECREF the frame is
663 accessed by other code (e.g. a __del__ method or gc.collect()) and the
664 variable would be pointing to already-freed memory. */
665#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
666 GETLOCAL(i) = value; \
667 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000668
Guido van Rossuma027efa1997-05-05 20:56:21 +0000669/* Start of code */
670
Tim Peters5ca576e2001-06-18 22:08:13 +0000671 if (f == NULL)
672 return NULL;
673
Armin Rigo1d313ab2003-10-25 14:33:09 +0000674 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000675 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000676 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000677
Tim Peters5ca576e2001-06-18 22:08:13 +0000678 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000679
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000680 if (tstate->use_tracing) {
681 if (tstate->c_tracefunc != NULL) {
682 /* tstate->c_tracefunc, if defined, is a
683 function that will be called on *every* entry
684 to a code block. Its return value, if not
685 None, is a function that will be called at
686 the start of each executed line of code.
687 (Actually, the function must return itself
688 in order to continue tracing.) The trace
689 functions are called with three arguments:
690 a pointer to the current frame, a string
691 indicating why the function is called, and
692 an argument which depends on the situation.
693 The global trace function is also called
694 whenever an exception is detected. */
695 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
696 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000697 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000698 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000699 }
700 }
701 if (tstate->c_profilefunc != NULL) {
702 /* Similar for c_profilefunc, except it needn't
703 return itself and isn't called for "line" events */
704 if (call_trace(tstate->c_profilefunc,
705 tstate->c_profileobj,
706 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000707 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000708 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000709 }
710 }
711 }
712
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000713 co = f->f_code;
714 names = co->co_names;
715 consts = co->co_consts;
716 fastlocals = f->f_localsplus;
717 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000718 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000719 /* An explanation is in order for the next line.
720
721 f->f_lasti now refers to the index of the last instruction
722 executed. You might think this was obvious from the name, but
723 this wasn't always true before 2.3! PyFrame_New now sets
724 f->f_lasti to -1 (i.e. the index *before* the first instruction)
725 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
726 does work. Promise. */
727 next_instr = first_instr + f->f_lasti + 1;
728 stack_pointer = f->f_stacktop;
729 assert(stack_pointer != NULL);
730 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
731
Tim Peters5ca576e2001-06-18 22:08:13 +0000732#ifdef LLTRACE
733 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
734#endif
735#if defined(Py_DEBUG) || defined(LLTRACE)
736 filename = PyString_AsString(co->co_filename);
737#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000738
Guido van Rossum374a9221991-04-04 10:40:29 +0000739 why = WHY_NOT;
740 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000741 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000742 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000743
Guido van Rossum374a9221991-04-04 10:40:29 +0000744 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000745#ifdef WITH_TSC
746 if (inst1 == 0) {
747 /* Almost surely, the opcode executed a break
748 or a continue, preventing inst1 from being set
749 on the way out of the loop.
750 */
751 rdtscll(inst1);
752 loop1 = inst1;
753 }
754 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
755 intr0, intr1);
756 ticked = 0;
757 inst1 = 0;
758 intr0 = 0;
759 intr1 = 0;
760 rdtscll(loop0);
761#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000762 assert(stack_pointer >= f->f_valuestack); /* else underflow */
763 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
764
Guido van Rossuma027efa1997-05-05 20:56:21 +0000765 /* Do periodic things. Doing this every time through
766 the loop would add too much overhead, so we do it
767 only every Nth instruction. We also do it if
768 ``things_to_do'' is set, i.e. when an asynchronous
769 event needs attention (e.g. a signal handler or
770 async I/O handler); see Py_AddPendingCall() and
771 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000772
Skip Montanarod581d772002-09-03 20:10:45 +0000773 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000774 if (*next_instr == SETUP_FINALLY) {
775 /* Make the last opcode before
776 a try: finally: block uninterruptable. */
777 goto fast_next_opcode;
778 }
Skip Montanarod581d772002-09-03 20:10:45 +0000779 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000780 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000781#ifdef WITH_TSC
782 ticked = 1;
783#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000784 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000785 if (Py_MakePendingCalls() < 0) {
786 why = WHY_EXCEPTION;
787 goto on_error;
788 }
789 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000790#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000791 if (interpreter_lock) {
792 /* Give another thread a chance */
793
Guido van Rossum25ce5661997-08-02 03:10:38 +0000794 if (PyThreadState_Swap(NULL) != tstate)
795 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000796 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000797
798 /* Other threads may run now */
799
Guido van Rossum65d5b571998-12-21 19:32:43 +0000800 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000801 if (PyThreadState_Swap(tstate) != NULL)
802 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000803
804 /* Check for thread interrupts */
805
806 if (tstate->async_exc != NULL) {
807 x = tstate->async_exc;
808 tstate->async_exc = NULL;
809 PyErr_SetNone(x);
810 Py_DECREF(x);
811 why = WHY_EXCEPTION;
812 goto on_error;
813 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000814 }
815#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000816 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000817
Neil Schemenauer63543862002-02-17 19:10:14 +0000818 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000819 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000820
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000821 /* line-by-line tracing support */
822
823 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
824 /* see maybe_call_line_trace
825 for expository comments */
826 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000827
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000828 err = maybe_call_line_trace(tstate->c_tracefunc,
829 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000830 f, &instr_lb, &instr_ub,
831 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000832 /* Reload possibly changed frame fields */
833 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000834 if (f->f_stacktop != NULL) {
835 stack_pointer = f->f_stacktop;
836 f->f_stacktop = NULL;
837 }
838 if (err) {
839 /* trace function raised an exception */
840 goto on_error;
841 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000842 }
843
844 /* Extract opcode and argument */
845
Guido van Rossum374a9221991-04-04 10:40:29 +0000846 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000847 oparg = 0; /* allows oparg to be stored in a register because
848 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000849 if (HAS_ARG(opcode))
850 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000851 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000852#ifdef DYNAMIC_EXECUTION_PROFILE
853#ifdef DXPAIRS
854 dxpairs[lastopcode][opcode]++;
855 lastopcode = opcode;
856#endif
857 dxp[opcode]++;
858#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000859
Guido van Rossum96a42c81992-01-12 02:29:51 +0000860#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000861 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000862
Guido van Rossum96a42c81992-01-12 02:29:51 +0000863 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000864 if (HAS_ARG(opcode)) {
865 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000866 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000867 }
868 else {
869 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000870 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000871 }
872 }
873#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000874
Guido van Rossum374a9221991-04-04 10:40:29 +0000875 /* Main switch on opcode */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000876 rdtscll(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000877
Guido van Rossum374a9221991-04-04 10:40:29 +0000878 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000879
Guido van Rossum374a9221991-04-04 10:40:29 +0000880 /* BEWARE!
881 It is essential that any operation that fails sets either
882 x to NULL, err to nonzero, or why to anything but WHY_NOT,
883 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000884
Guido van Rossum374a9221991-04-04 10:40:29 +0000885 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000886
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000887 case NOP:
888 goto fast_next_opcode;
889
Neil Schemenauer63543862002-02-17 19:10:14 +0000890 case LOAD_FAST:
891 x = GETLOCAL(oparg);
892 if (x != NULL) {
893 Py_INCREF(x);
894 PUSH(x);
895 goto fast_next_opcode;
896 }
897 format_exc_check_arg(PyExc_UnboundLocalError,
898 UNBOUNDLOCAL_ERROR_MSG,
899 PyTuple_GetItem(co->co_varnames, oparg));
900 break;
901
902 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000903 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000904 Py_INCREF(x);
905 PUSH(x);
906 goto fast_next_opcode;
907
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000908 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000909 case STORE_FAST:
910 v = POP();
911 SETLOCAL(oparg, v);
912 goto fast_next_opcode;
913
Raymond Hettingerf606f872003-03-16 03:11:04 +0000914 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000915 case POP_TOP:
916 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000917 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000918 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000919
Guido van Rossum374a9221991-04-04 10:40:29 +0000920 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000921 v = TOP();
922 w = SECOND();
923 SET_TOP(w);
924 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000925 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000926
Guido van Rossum374a9221991-04-04 10:40:29 +0000927 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000928 v = TOP();
929 w = SECOND();
930 x = THIRD();
931 SET_TOP(w);
932 SET_SECOND(x);
933 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000934 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000935
Thomas Wouters434d0822000-08-24 20:11:32 +0000936 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000937 u = TOP();
938 v = SECOND();
939 w = THIRD();
940 x = FOURTH();
941 SET_TOP(v);
942 SET_SECOND(w);
943 SET_THIRD(x);
944 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000945 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000946
Guido van Rossum374a9221991-04-04 10:40:29 +0000947 case DUP_TOP:
948 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000949 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000950 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000951 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000952
Thomas Wouters434d0822000-08-24 20:11:32 +0000953 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000954 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000955 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000956 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000957 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000958 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000959 STACKADJ(2);
960 SET_TOP(x);
961 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000962 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000963 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000964 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000965 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000966 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000967 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000968 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000969 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 STACKADJ(3);
971 SET_TOP(x);
972 SET_SECOND(w);
973 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000974 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000975 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000976 Py_FatalError("invalid argument to DUP_TOPX"
977 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000978 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000979
Guido van Rossum374a9221991-04-04 10:40:29 +0000980 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000982 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000983 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000984 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000985 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000986 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000987
Guido van Rossum374a9221991-04-04 10:40:29 +0000988 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000989 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000990 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000991 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000993 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000994 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000995
Guido van Rossum374a9221991-04-04 10:40:29 +0000996 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000997 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000998 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000999 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001000 if (err == 0) {
1001 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001003 continue;
1004 }
1005 else if (err > 0) {
1006 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001008 err = 0;
1009 continue;
1010 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001011 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001013
Guido van Rossum374a9221991-04-04 10:40:29 +00001014 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001016 x = PyObject_Repr(v);
1017 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001019 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001021
Guido van Rossum7928cd71991-10-24 14:59:31 +00001022 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001023 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001024 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001025 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001027 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001028 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001029
Guido van Rossum50564e81996-01-12 01:13:16 +00001030 case BINARY_POWER:
1031 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001033 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001034 Py_DECREF(v);
1035 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001037 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001038 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001039
Guido van Rossum374a9221991-04-04 10:40:29 +00001040 case BINARY_MULTIPLY:
1041 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001043 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001044 Py_DECREF(v);
1045 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001046 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001047 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001048 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001049
Guido van Rossum374a9221991-04-04 10:40:29 +00001050 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001051 if (!_Py_QnewFlag) {
1052 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001053 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001054 x = PyNumber_Divide(v, w);
1055 Py_DECREF(v);
1056 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001057 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001058 if (x != NULL) continue;
1059 break;
1060 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001061 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001062 BINARY_TRUE_DIVIDE */
1063 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001065 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001066 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001067 Py_DECREF(v);
1068 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001069 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001070 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001071 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001072
Guido van Rossum4668b002001-08-08 05:00:18 +00001073 case BINARY_FLOOR_DIVIDE:
1074 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001075 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001076 x = PyNumber_FloorDivide(v, w);
1077 Py_DECREF(v);
1078 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001079 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001080 if (x != NULL) continue;
1081 break;
1082
Guido van Rossum374a9221991-04-04 10:40:29 +00001083 case BINARY_MODULO:
1084 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001085 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001086 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001087 Py_DECREF(v);
1088 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001089 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001090 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001091 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001092
Guido van Rossum374a9221991-04-04 10:40:29 +00001093 case BINARY_ADD:
1094 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001095 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001096 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001097 /* INLINE: int + int */
1098 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001099 a = PyInt_AS_LONG(v);
1100 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001101 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001102 if ((i^a) < 0 && (i^b) < 0)
1103 goto slow_add;
1104 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001105 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001106 else if (PyString_CheckExact(v) &&
1107 PyString_CheckExact(w)) {
1108 x = string_concatenate(v, w, f, next_instr);
1109 /* string_concatenate consumed the ref to v */
1110 goto skip_decref_vx;
1111 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001112 else {
1113 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001114 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001115 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001116 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001117 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001118 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001119 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001120 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001121 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001122
Guido van Rossum374a9221991-04-04 10:40:29 +00001123 case BINARY_SUBTRACT:
1124 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001125 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001126 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001127 /* INLINE: int - int */
1128 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001129 a = PyInt_AS_LONG(v);
1130 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001131 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001132 if ((i^a) < 0 && (i^~b) < 0)
1133 goto slow_sub;
1134 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001135 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001136 else {
1137 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001138 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001139 }
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 Rossum374a9221991-04-04 10:40:29 +00001144 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001145
Guido van Rossum374a9221991-04-04 10:40:29 +00001146 case BINARY_SUBSCR:
1147 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001148 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001149 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001150 /* INLINE: list[int] */
1151 long i = PyInt_AsLong(w);
1152 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001153 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001154 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001155 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001156 Py_INCREF(x);
1157 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001158 else
1159 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001160 }
1161 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001162 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001163 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001164 Py_DECREF(v);
1165 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001166 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001167 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001168 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001169
Guido van Rossum7928cd71991-10-24 14:59:31 +00001170 case BINARY_LSHIFT:
1171 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001172 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001173 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001174 Py_DECREF(v);
1175 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001176 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001177 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001178 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001179
Guido van Rossum7928cd71991-10-24 14:59:31 +00001180 case BINARY_RSHIFT:
1181 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001182 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001183 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001184 Py_DECREF(v);
1185 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001186 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001187 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001188 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001189
Guido van Rossum7928cd71991-10-24 14:59:31 +00001190 case BINARY_AND:
1191 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001193 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001194 Py_DECREF(v);
1195 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001197 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001198 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001199
Guido van Rossum7928cd71991-10-24 14:59:31 +00001200 case BINARY_XOR:
1201 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001202 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001203 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001204 Py_DECREF(v);
1205 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001207 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001208 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001209
Guido van Rossum7928cd71991-10-24 14:59:31 +00001210 case BINARY_OR:
1211 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001212 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001213 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001214 Py_DECREF(v);
1215 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001217 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001218 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001219
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001220 case LIST_APPEND:
1221 w = POP();
1222 v = POP();
1223 err = PyList_Append(v, w);
1224 Py_DECREF(v);
1225 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001226 if (err == 0) {
1227 PREDICT(JUMP_ABSOLUTE);
1228 continue;
1229 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001230 break;
1231
Thomas Wouters434d0822000-08-24 20:11:32 +00001232 case INPLACE_POWER:
1233 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001234 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001235 x = PyNumber_InPlacePower(v, w, Py_None);
1236 Py_DECREF(v);
1237 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001238 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 if (x != NULL) continue;
1240 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001241
Thomas Wouters434d0822000-08-24 20:11:32 +00001242 case INPLACE_MULTIPLY:
1243 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001244 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001245 x = PyNumber_InPlaceMultiply(v, w);
1246 Py_DECREF(v);
1247 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001248 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001249 if (x != NULL) continue;
1250 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001251
Thomas Wouters434d0822000-08-24 20:11:32 +00001252 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001253 if (!_Py_QnewFlag) {
1254 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001255 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001256 x = PyNumber_InPlaceDivide(v, w);
1257 Py_DECREF(v);
1258 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001259 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001260 if (x != NULL) continue;
1261 break;
1262 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001263 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001264 INPLACE_TRUE_DIVIDE */
1265 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001267 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001268 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001269 Py_DECREF(v);
1270 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001271 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001272 if (x != NULL) continue;
1273 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Guido van Rossum4668b002001-08-08 05:00:18 +00001275 case INPLACE_FLOOR_DIVIDE:
1276 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001277 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001278 x = PyNumber_InPlaceFloorDivide(v, w);
1279 Py_DECREF(v);
1280 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001281 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001282 if (x != NULL) continue;
1283 break;
1284
Thomas Wouters434d0822000-08-24 20:11:32 +00001285 case INPLACE_MODULO:
1286 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001287 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001288 x = PyNumber_InPlaceRemainder(v, w);
1289 Py_DECREF(v);
1290 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001291 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001292 if (x != NULL) continue;
1293 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001294
Thomas Wouters434d0822000-08-24 20:11:32 +00001295 case INPLACE_ADD:
1296 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001297 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001298 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 /* INLINE: int + int */
1300 register long a, b, i;
1301 a = PyInt_AS_LONG(v);
1302 b = PyInt_AS_LONG(w);
1303 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001304 if ((i^a) < 0 && (i^b) < 0)
1305 goto slow_iadd;
1306 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001307 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001308 else if (PyString_CheckExact(v) &&
1309 PyString_CheckExact(w)) {
1310 x = string_concatenate(v, w, f, next_instr);
1311 /* string_concatenate consumed the ref to v */
1312 goto skip_decref_v;
1313 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001314 else {
1315 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001317 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001318 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001319 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 if (x != NULL) continue;
1323 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001324
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 case INPLACE_SUBTRACT:
1326 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001327 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001328 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 /* INLINE: int - int */
1330 register long a, b, i;
1331 a = PyInt_AS_LONG(v);
1332 b = PyInt_AS_LONG(w);
1333 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001334 if ((i^a) < 0 && (i^~b) < 0)
1335 goto slow_isub;
1336 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001338 else {
1339 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001341 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 Py_DECREF(v);
1343 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 if (x != NULL) continue;
1346 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 case INPLACE_LSHIFT:
1349 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 x = PyNumber_InPlaceLshift(v, w);
1352 Py_DECREF(v);
1353 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 if (x != NULL) continue;
1356 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001357
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 case INPLACE_RSHIFT:
1359 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 x = PyNumber_InPlaceRshift(v, w);
1362 Py_DECREF(v);
1363 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 if (x != NULL) continue;
1366 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 case INPLACE_AND:
1369 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 x = PyNumber_InPlaceAnd(v, w);
1372 Py_DECREF(v);
1373 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 if (x != NULL) continue;
1376 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 case INPLACE_XOR:
1379 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001380 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 x = PyNumber_InPlaceXor(v, w);
1382 Py_DECREF(v);
1383 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 if (x != NULL) continue;
1386 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001387
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 case INPLACE_OR:
1389 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 x = PyNumber_InPlaceOr(v, w);
1392 Py_DECREF(v);
1393 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001395 if (x != NULL) continue;
1396 break;
1397
Guido van Rossum374a9221991-04-04 10:40:29 +00001398 case SLICE+0:
1399 case SLICE+1:
1400 case SLICE+2:
1401 case SLICE+3:
1402 if ((opcode-SLICE) & 2)
1403 w = POP();
1404 else
1405 w = NULL;
1406 if ((opcode-SLICE) & 1)
1407 v = POP();
1408 else
1409 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001410 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001412 Py_DECREF(u);
1413 Py_XDECREF(v);
1414 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001415 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001416 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001418
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 case STORE_SLICE+0:
1420 case STORE_SLICE+1:
1421 case STORE_SLICE+2:
1422 case STORE_SLICE+3:
1423 if ((opcode-STORE_SLICE) & 2)
1424 w = POP();
1425 else
1426 w = NULL;
1427 if ((opcode-STORE_SLICE) & 1)
1428 v = POP();
1429 else
1430 v = NULL;
1431 u = POP();
1432 t = POP();
1433 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001434 Py_DECREF(t);
1435 Py_DECREF(u);
1436 Py_XDECREF(v);
1437 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001438 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001439 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 case DELETE_SLICE+0:
1442 case DELETE_SLICE+1:
1443 case DELETE_SLICE+2:
1444 case DELETE_SLICE+3:
1445 if ((opcode-DELETE_SLICE) & 2)
1446 w = POP();
1447 else
1448 w = NULL;
1449 if ((opcode-DELETE_SLICE) & 1)
1450 v = POP();
1451 else
1452 v = NULL;
1453 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001454 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001455 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001456 Py_DECREF(u);
1457 Py_XDECREF(v);
1458 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001459 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001460 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001463 w = TOP();
1464 v = SECOND();
1465 u = THIRD();
1466 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001467 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001468 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001469 Py_DECREF(u);
1470 Py_DECREF(v);
1471 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001472 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001476 w = TOP();
1477 v = SECOND();
1478 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001480 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001481 Py_DECREF(v);
1482 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001483 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001484 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001485
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 case PRINT_EXPR:
1487 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001488 w = PySys_GetObject("displayhook");
1489 if (w == NULL) {
1490 PyErr_SetString(PyExc_RuntimeError,
1491 "lost sys.displayhook");
1492 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001493 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001494 }
1495 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001496 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001497 if (x == NULL)
1498 err = -1;
1499 }
1500 if (err == 0) {
1501 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001502 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001503 if (w == NULL)
1504 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001506 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001507 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001509
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001510 case PRINT_ITEM_TO:
1511 w = stream = POP();
1512 /* fall through to PRINT_ITEM */
1513
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 case PRINT_ITEM:
1515 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001516 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001517 w = PySys_GetObject("stdout");
1518 if (w == NULL) {
1519 PyErr_SetString(PyExc_RuntimeError,
1520 "lost sys.stdout");
1521 err = -1;
1522 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001523 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001524 /* PyFile_SoftSpace() can exececute arbitrary code
1525 if sys.stdout is an instance with a __getattr__.
1526 If __getattr__ raises an exception, w will
1527 be freed, so we need to prevent that temporarily. */
1528 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001529 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001530 err = PyFile_WriteString(" ", w);
1531 if (err == 0)
1532 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001533 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001534 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001535 if (PyString_Check(v)) {
1536 char *s = PyString_AS_STRING(v);
1537 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001538 if (len == 0 ||
1539 !isspace(Py_CHARMASK(s[len-1])) ||
1540 s[len-1] == ' ')
1541 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001542 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001543#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001544 else if (PyUnicode_Check(v)) {
1545 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1546 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001547 if (len == 0 ||
1548 !Py_UNICODE_ISSPACE(s[len-1]) ||
1549 s[len-1] == ' ')
1550 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001551 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001552#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001553 else
1554 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001555 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001556 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001557 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001558 Py_XDECREF(stream);
1559 stream = NULL;
1560 if (err == 0)
1561 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001562 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001563
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001564 case PRINT_NEWLINE_TO:
1565 w = stream = POP();
1566 /* fall through to PRINT_NEWLINE */
1567
Guido van Rossum374a9221991-04-04 10:40:29 +00001568 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001569 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001570 w = PySys_GetObject("stdout");
1571 if (w == NULL)
1572 PyErr_SetString(PyExc_RuntimeError,
1573 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001574 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001575 if (w != NULL) {
1576 err = PyFile_WriteString("\n", w);
1577 if (err == 0)
1578 PyFile_SoftSpace(w, 0);
1579 }
1580 Py_XDECREF(stream);
1581 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Thomas Wouters434d0822000-08-24 20:11:32 +00001584
1585#ifdef CASE_TOO_BIG
1586 default: switch (opcode) {
1587#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001588 case RAISE_VARARGS:
1589 u = v = w = NULL;
1590 switch (oparg) {
1591 case 3:
1592 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001593 /* Fallthrough */
1594 case 2:
1595 v = POP(); /* value */
1596 /* Fallthrough */
1597 case 1:
1598 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001599 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001600 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001601 break;
1602 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001603 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001604 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001605 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001606 break;
1607 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001608 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001609
Guido van Rossum374a9221991-04-04 10:40:29 +00001610 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001611 if ((x = f->f_locals) != NULL) {
1612 Py_INCREF(x);
1613 PUSH(x);
1614 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001615 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001616 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001617 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 case RETURN_VALUE:
1620 retval = POP();
1621 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001622 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001623
Tim Peters5ca576e2001-06-18 22:08:13 +00001624 case YIELD_VALUE:
1625 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001626 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001627 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001628 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001629
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001630 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001631 w = TOP();
1632 v = SECOND();
1633 u = THIRD();
1634 STACKADJ(-3);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001635 rdtscll(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001636 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001637 rdtscll(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001638 Py_DECREF(u);
1639 Py_DECREF(v);
1640 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001641 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 case POP_BLOCK:
1644 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001645 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001646 while (STACK_LEVEL() > b->b_level) {
1647 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001648 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001649 }
1650 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001651 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Guido van Rossum374a9221991-04-04 10:40:29 +00001653 case END_FINALLY:
1654 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001655 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001656 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001657 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001658 if (why == WHY_RETURN ||
1659 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 retval = POP();
1661 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001662 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001663 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001664 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001665 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001667 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001668 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001669 else if (v != Py_None) {
1670 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 "'finally' pops bad exception");
1672 why = WHY_EXCEPTION;
1673 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001674 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001678 u = TOP();
1679 v = SECOND();
1680 w = THIRD();
1681 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001682 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001683 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001684 Py_DECREF(u);
1685 Py_DECREF(v);
1686 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001687 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Guido van Rossum374a9221991-04-04 10:40:29 +00001689 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001690 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001691 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001692 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001693 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001694 err = PyDict_SetItem(x, w, v);
1695 else
1696 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001697 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001698 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001699 break;
1700 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001701 PyErr_Format(PyExc_SystemError,
1702 "no locals found when storing %s",
1703 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001704 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001705
Guido van Rossum374a9221991-04-04 10:40:29 +00001706 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001707 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001708 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001709 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001710 format_exc_check_arg(PyExc_NameError,
1711 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001712 break;
1713 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001714 PyErr_Format(PyExc_SystemError,
1715 "no locals when deleting %s",
1716 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001718
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001719 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001720 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001721 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001722 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1723 PyObject **items = ((PyTupleObject *)v)->ob_item;
1724 while (oparg--) {
1725 w = items[oparg];
1726 Py_INCREF(w);
1727 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001728 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001729 Py_DECREF(v);
1730 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001731 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1732 PyObject **items = ((PyListObject *)v)->ob_item;
1733 while (oparg--) {
1734 w = items[oparg];
1735 Py_INCREF(w);
1736 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001737 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001738 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001739 stack_pointer + oparg))
1740 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001741 else {
1742 if (PyErr_ExceptionMatches(PyExc_TypeError))
1743 PyErr_SetString(PyExc_TypeError,
1744 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001745 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001746 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001747 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001748 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001749
Guido van Rossum374a9221991-04-04 10:40:29 +00001750 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001751 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001752 v = TOP();
1753 u = SECOND();
1754 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001755 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1756 Py_DECREF(v);
1757 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001758 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001760
Guido van Rossum374a9221991-04-04 10:40:29 +00001761 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001762 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001764 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1765 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001766 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001767 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001769 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001770 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001771 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001772 err = PyDict_SetItem(f->f_globals, w, v);
1773 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001774 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001775 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001776
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001777 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001778 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001779 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001780 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001781 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001782 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001783
Guido van Rossum374a9221991-04-04 10:40:29 +00001784 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001785 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001786 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001787 PyErr_Format(PyExc_SystemError,
1788 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001789 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001790 break;
1791 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001792 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001793 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001794 Py_XINCREF(x);
1795 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001796 else {
1797 x = PyObject_GetItem(v, w);
1798 if (x == NULL && PyErr_Occurred()) {
1799 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1800 break;
1801 PyErr_Clear();
1802 }
1803 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001804 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001805 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001806 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001807 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001808 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001809 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001810 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001811 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 break;
1813 }
1814 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001815 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001816 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001818 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001819
Guido van Rossum374a9221991-04-04 10:40:29 +00001820 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001821 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001822 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001823 /* Inline the PyDict_GetItem() calls.
1824 WARNING: this is an extreme speed hack.
1825 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001826 long hash = ((PyStringObject *)w)->ob_shash;
1827 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001828 PyDictObject *d;
1829 d = (PyDictObject *)(f->f_globals);
1830 x = d->ma_lookup(d, w, hash)->me_value;
1831 if (x != NULL) {
1832 Py_INCREF(x);
1833 PUSH(x);
1834 continue;
1835 }
1836 d = (PyDictObject *)(f->f_builtins);
1837 x = d->ma_lookup(d, w, hash)->me_value;
1838 if (x != NULL) {
1839 Py_INCREF(x);
1840 PUSH(x);
1841 continue;
1842 }
1843 goto load_global_error;
1844 }
1845 }
1846 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001847 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001848 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001849 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001851 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001852 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001853 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001854 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001855 break;
1856 }
1857 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001858 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001859 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001860 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001861
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001862 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001863 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001864 if (x != NULL) {
1865 SETLOCAL(oparg, NULL);
1866 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001867 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001868 format_exc_check_arg(
1869 PyExc_UnboundLocalError,
1870 UNBOUNDLOCAL_ERROR_MSG,
1871 PyTuple_GetItem(co->co_varnames, oparg)
1872 );
1873 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001874
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001875 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001876 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001877 Py_INCREF(x);
1878 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001879 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001880 break;
1881
1882 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001883 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001884 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001885 if (w != NULL) {
1886 PUSH(w);
1887 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001888 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001889 err = -1;
1890 /* Don't stomp existing exception */
1891 if (PyErr_Occurred())
1892 break;
1893 if (oparg < f->f_ncells) {
1894 v = PyTuple_GetItem(co->co_cellvars,
1895 oparg);
1896 format_exc_check_arg(
1897 PyExc_UnboundLocalError,
1898 UNBOUNDLOCAL_ERROR_MSG,
1899 v);
1900 } else {
1901 v = PyTuple_GetItem(
1902 co->co_freevars,
1903 oparg - f->f_ncells);
1904 format_exc_check_arg(
1905 PyExc_NameError,
1906 UNBOUNDFREE_ERROR_MSG,
1907 v);
1908 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001909 break;
1910
1911 case STORE_DEREF:
1912 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001913 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001914 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001915 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001916 continue;
1917
Guido van Rossum374a9221991-04-04 10:40:29 +00001918 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001919 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001921 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001923 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 }
1925 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001926 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001927 }
1928 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001929
Guido van Rossum374a9221991-04-04 10:40:29 +00001930 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001931 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001933 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001934 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001935 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001936 }
1937 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001938 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001939 }
1940 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001941
Guido van Rossum374a9221991-04-04 10:40:29 +00001942 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001943 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001944 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001945 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001946 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Guido van Rossum374a9221991-04-04 10:40:29 +00001948 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001949 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001950 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001951 x = PyObject_GetAttr(v, w);
1952 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001953 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001954 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001955 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001956
Guido van Rossum374a9221991-04-04 10:40:29 +00001957 case COMPARE_OP:
1958 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001959 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001960 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001961 /* INLINE: cmp(int, int) */
1962 register long a, b;
1963 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001964 a = PyInt_AS_LONG(v);
1965 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001966 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001967 case PyCmp_LT: res = a < b; break;
1968 case PyCmp_LE: res = a <= b; break;
1969 case PyCmp_EQ: res = a == b; break;
1970 case PyCmp_NE: res = a != b; break;
1971 case PyCmp_GT: res = a > b; break;
1972 case PyCmp_GE: res = a >= b; break;
1973 case PyCmp_IS: res = v == w; break;
1974 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001975 default: goto slow_compare;
1976 }
1977 x = res ? Py_True : Py_False;
1978 Py_INCREF(x);
1979 }
1980 else {
1981 slow_compare:
1982 x = cmp_outcome(oparg, v, w);
1983 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 Py_DECREF(v);
1985 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001986 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001987 if (x == NULL) break;
1988 PREDICT(JUMP_IF_FALSE);
1989 PREDICT(JUMP_IF_TRUE);
1990 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001991
Guido van Rossum374a9221991-04-04 10:40:29 +00001992 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001993 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001994 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001995 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001996 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001997 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001998 break;
1999 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00002000 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002001 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002002 w,
2003 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002004 f->f_locals == NULL ?
2005 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002006 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002007 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002008 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002009 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002010 x = NULL;
2011 break;
2012 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002013 rdtscll(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002014 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002015 rdtscll(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002016 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002017 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002018 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002019 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002020
Thomas Wouters52152252000-08-17 22:55:00 +00002021 case IMPORT_STAR:
2022 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002023 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002024 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002025 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002026 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002027 break;
2028 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002029 rdtscll(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002030 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002031 rdtscll(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002032 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002033 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002034 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002035 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002036
Thomas Wouters52152252000-08-17 22:55:00 +00002037 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002038 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002039 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002040 rdtscll(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002041 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002042 rdtscll(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002043 PUSH(x);
2044 if (x != NULL) continue;
2045 break;
2046
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 case JUMP_FORWARD:
2048 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002049 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002050
Raymond Hettingerf606f872003-03-16 03:11:04 +00002051 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002052 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002053 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002054 if (w == Py_True) {
2055 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002056 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002057 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002058 if (w == Py_False) {
2059 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002060 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002061 }
2062 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002063 if (err > 0)
2064 err = 0;
2065 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002066 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002067 else
2068 break;
2069 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002070
Raymond Hettingerf606f872003-03-16 03:11:04 +00002071 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002072 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002073 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002074 if (w == Py_False) {
2075 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002076 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002077 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002078 if (w == Py_True) {
2079 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002080 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002081 }
2082 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002083 if (err > 0) {
2084 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002085 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002086 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002087 else if (err == 0)
2088 ;
2089 else
2090 break;
2091 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002092
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002093 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002094 case JUMP_ABSOLUTE:
2095 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002096 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002097
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002098 case GET_ITER:
2099 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002100 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002101 x = PyObject_GetIter(v);
2102 Py_DECREF(v);
2103 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002104 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002105 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002106 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002108 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002109 break;
2110
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002111 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002112 case FOR_ITER:
2113 /* before: [iter]; after: [iter, iter()] *or* [] */
2114 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002115 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002116 if (x != NULL) {
2117 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002118 PREDICT(STORE_FAST);
2119 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002120 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002121 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002122 if (PyErr_Occurred()) {
2123 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2124 break;
2125 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002126 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002127 /* iterator ended normally */
2128 x = v = POP();
2129 Py_DECREF(v);
2130 JUMPBY(oparg);
2131 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002132
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002133 case BREAK_LOOP:
2134 why = WHY_BREAK;
2135 goto fast_block_end;
2136
2137 case CONTINUE_LOOP:
2138 retval = PyInt_FromLong(oparg);
2139 why = WHY_CONTINUE;
2140 goto fast_block_end;
2141
Guido van Rossum374a9221991-04-04 10:40:29 +00002142 case SETUP_LOOP:
2143 case SETUP_EXCEPT:
2144 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002145 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002146 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002147 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002148
Guido van Rossumf10570b1995-07-07 22:53:21 +00002149 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002150 {
2151 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002152 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002153 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002154#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002155 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002156#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002157 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002158#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002159 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002160 PUSH(x);
2161 if (x != NULL)
2162 continue;
2163 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002164 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002165
Jeremy Hylton76901512000-03-28 23:49:17 +00002166 case CALL_FUNCTION_VAR:
2167 case CALL_FUNCTION_KW:
2168 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002169 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002170 int na = oparg & 0xff;
2171 int nk = (oparg>>8) & 0xff;
2172 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002173 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002174 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002175 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002176 if (flags & CALL_FLAG_VAR)
2177 n++;
2178 if (flags & CALL_FLAG_KW)
2179 n++;
2180 pfunc = stack_pointer - n - 1;
2181 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002182
Guido van Rossumac7be682001-01-17 15:42:30 +00002183 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002184 && PyMethod_GET_SELF(func) != NULL) {
2185 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002186 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002187 func = PyMethod_GET_FUNCTION(func);
2188 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002189 Py_DECREF(*pfunc);
2190 *pfunc = self;
2191 na++;
2192 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002193 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002194 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002195 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002196 rdtscll(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002197 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002198 rdtscll(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002199 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002200 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002201
Jeremy Hylton76901512000-03-28 23:49:17 +00002202 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002203 w = POP();
2204 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002205 }
2206 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002207 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002208 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002209 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002210 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002211
Guido van Rossum681d79a1995-07-18 14:51:37 +00002212 case MAKE_FUNCTION:
2213 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002214 x = PyFunction_New(v, f->f_globals);
2215 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002216 /* XXX Maybe this should be a separate opcode? */
2217 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002218 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002219 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002220 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002221 x = NULL;
2222 break;
2223 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002224 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002225 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002226 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002227 }
2228 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002229 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002230 }
2231 PUSH(x);
2232 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002233
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002234 case MAKE_CLOSURE:
2235 {
2236 int nfree;
2237 v = POP(); /* code object */
2238 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002239 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002240 Py_DECREF(v);
2241 /* XXX Maybe this should be a separate opcode? */
2242 if (x != NULL && nfree > 0) {
2243 v = PyTuple_New(nfree);
2244 if (v == NULL) {
2245 Py_DECREF(x);
2246 x = NULL;
2247 break;
2248 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002249 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002250 w = POP();
2251 PyTuple_SET_ITEM(v, nfree, w);
2252 }
2253 err = PyFunction_SetClosure(x, v);
2254 Py_DECREF(v);
2255 }
2256 if (x != NULL && oparg > 0) {
2257 v = PyTuple_New(oparg);
2258 if (v == NULL) {
2259 Py_DECREF(x);
2260 x = NULL;
2261 break;
2262 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002263 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002264 w = POP();
2265 PyTuple_SET_ITEM(v, oparg, w);
2266 }
2267 err = PyFunction_SetDefaults(x, v);
2268 Py_DECREF(v);
2269 }
2270 PUSH(x);
2271 break;
2272 }
2273
Guido van Rossum8861b741996-07-30 16:49:37 +00002274 case BUILD_SLICE:
2275 if (oparg == 3)
2276 w = POP();
2277 else
2278 w = NULL;
2279 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002280 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002281 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002282 Py_DECREF(u);
2283 Py_DECREF(v);
2284 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002285 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002286 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002287 break;
2288
Fred Drakeef8ace32000-08-24 00:32:09 +00002289 case EXTENDED_ARG:
2290 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002291 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002292 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002293
Guido van Rossum374a9221991-04-04 10:40:29 +00002294 default:
2295 fprintf(stderr,
2296 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002297 PyCode_Addr2Line(f->f_code, f->f_lasti),
2298 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002299 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002300 why = WHY_EXCEPTION;
2301 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002302
2303#ifdef CASE_TOO_BIG
2304 }
2305#endif
2306
Guido van Rossum374a9221991-04-04 10:40:29 +00002307 } /* switch */
2308
2309 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002310
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002311 rdtscll(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002312
Guido van Rossum374a9221991-04-04 10:40:29 +00002313 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002314
Guido van Rossum374a9221991-04-04 10:40:29 +00002315 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002316 if (err == 0 && x != NULL) {
2317#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002318 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002319 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002320 fprintf(stderr,
2321 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002322 else {
2323#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002324 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002325 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002326#ifdef CHECKEXC
2327 }
2328#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002329 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002330 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002331 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002332 err = 0;
2333 }
2334
Guido van Rossum374a9221991-04-04 10:40:29 +00002335 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002336
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002337 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002338 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002339 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002340 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002341 why = WHY_EXCEPTION;
2342 }
2343 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002344#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002345 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002346 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002347 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002348 char buf[1024];
2349 sprintf(buf, "Stack unwind with exception "
2350 "set and why=%d", why);
2351 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002352 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002353 }
2354#endif
2355
2356 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002357
Guido van Rossum374a9221991-04-04 10:40:29 +00002358 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002359 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002360
Fred Drake8f51f542001-10-04 14:48:42 +00002361 if (tstate->c_tracefunc != NULL)
2362 call_exc_trace(tstate->c_tracefunc,
2363 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002364 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002367
Guido van Rossum374a9221991-04-04 10:40:29 +00002368 if (why == WHY_RERAISE)
2369 why = WHY_EXCEPTION;
2370
2371 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002372
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002373fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002374 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002375 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002376
Tim Peters8a5c3c72004-04-05 19:36:21 +00002377 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002378 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2379 /* For a continue inside a try block,
2380 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002381 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2382 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002383 why = WHY_NOT;
2384 JUMPTO(PyInt_AS_LONG(retval));
2385 Py_DECREF(retval);
2386 break;
2387 }
2388
Guido van Rossum374a9221991-04-04 10:40:29 +00002389 while (STACK_LEVEL() > b->b_level) {
2390 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002391 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002392 }
2393 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2394 why = WHY_NOT;
2395 JUMPTO(b->b_handler);
2396 break;
2397 }
2398 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002399 (b->b_type == SETUP_EXCEPT &&
2400 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002401 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002402 PyObject *exc, *val, *tb;
2403 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002404 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002405 val = Py_None;
2406 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002407 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002408 /* Make the raw exception data
2409 available to the handler,
2410 so a program can emulate the
2411 Python main loop. Don't do
2412 this for 'finally'. */
2413 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002414 PyErr_NormalizeException(
2415 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002416 set_exc_info(tstate,
2417 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002418 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002419 if (tb == NULL) {
2420 Py_INCREF(Py_None);
2421 PUSH(Py_None);
2422 } else
2423 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002424 PUSH(val);
2425 PUSH(exc);
2426 }
2427 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002428 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002429 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002430 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002431 PUSH(v);
2432 }
2433 why = WHY_NOT;
2434 JUMPTO(b->b_handler);
2435 break;
2436 }
2437 } /* unwind stack */
2438
2439 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002440
Guido van Rossum374a9221991-04-04 10:40:29 +00002441 if (why != WHY_NOT)
2442 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002443 rdtscll(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002444
Guido van Rossum374a9221991-04-04 10:40:29 +00002445 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002446
Tim Peters8a5c3c72004-04-05 19:36:21 +00002447 assert(why != WHY_YIELD);
2448 /* Pop remaining stack entries. */
2449 while (!EMPTY()) {
2450 v = POP();
2451 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002452 }
2453
Tim Peters8a5c3c72004-04-05 19:36:21 +00002454 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002455 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002456
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002457fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002458 if (tstate->use_tracing) {
2459 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002460 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002461 if (call_trace(tstate->c_tracefunc,
2462 tstate->c_traceobj, f,
2463 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002464 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002465 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002466 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002467 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002468 }
Fred Drake8f51f542001-10-04 14:48:42 +00002469 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002470 if (why == WHY_EXCEPTION)
2471 call_trace_protected(tstate->c_profilefunc,
2472 tstate->c_profileobj, f,
2473 PyTrace_RETURN);
2474 else if (call_trace(tstate->c_profilefunc,
2475 tstate->c_profileobj, f,
2476 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002477 Py_XDECREF(retval);
2478 retval = NULL;
2479 why = WHY_EXCEPTION;
2480 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002481 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002482 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002483
Guido van Rossuma027efa1997-05-05 20:56:21 +00002484 reset_exc_info(tstate);
2485
Tim Peters5ca576e2001-06-18 22:08:13 +00002486 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002487 exit_eval_frame:
2488 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002489 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002490
Guido van Rossum96a42c81992-01-12 02:29:51 +00002491 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002492}
2493
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002494/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002495 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2496 the test in the if statement in Misc/gdbinit:ppystack */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002497
Tim Peters6d6c1a32001-08-02 04:15:00 +00002498PyObject *
2499PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002500 PyObject **args, int argcount, PyObject **kws, int kwcount,
2501 PyObject **defs, int defcount, PyObject *closure)
2502{
2503 register PyFrameObject *f;
2504 register PyObject *retval = NULL;
2505 register PyObject **fastlocals, **freevars;
2506 PyThreadState *tstate = PyThreadState_GET();
2507 PyObject *x, *u;
2508
2509 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002510 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002511 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002512 return NULL;
2513 }
2514
Jeremy Hylton985eba52003-02-05 23:13:00 +00002515 assert(globals != NULL);
2516 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002517 if (f == NULL)
2518 return NULL;
2519
2520 fastlocals = f->f_localsplus;
2521 freevars = f->f_localsplus + f->f_nlocals;
2522
2523 if (co->co_argcount > 0 ||
2524 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2525 int i;
2526 int n = argcount;
2527 PyObject *kwdict = NULL;
2528 if (co->co_flags & CO_VARKEYWORDS) {
2529 kwdict = PyDict_New();
2530 if (kwdict == NULL)
2531 goto fail;
2532 i = co->co_argcount;
2533 if (co->co_flags & CO_VARARGS)
2534 i++;
2535 SETLOCAL(i, kwdict);
2536 }
2537 if (argcount > co->co_argcount) {
2538 if (!(co->co_flags & CO_VARARGS)) {
2539 PyErr_Format(PyExc_TypeError,
2540 "%.200s() takes %s %d "
2541 "%sargument%s (%d given)",
2542 PyString_AsString(co->co_name),
2543 defcount ? "at most" : "exactly",
2544 co->co_argcount,
2545 kwcount ? "non-keyword " : "",
2546 co->co_argcount == 1 ? "" : "s",
2547 argcount);
2548 goto fail;
2549 }
2550 n = co->co_argcount;
2551 }
2552 for (i = 0; i < n; i++) {
2553 x = args[i];
2554 Py_INCREF(x);
2555 SETLOCAL(i, x);
2556 }
2557 if (co->co_flags & CO_VARARGS) {
2558 u = PyTuple_New(argcount - n);
2559 if (u == NULL)
2560 goto fail;
2561 SETLOCAL(co->co_argcount, u);
2562 for (i = n; i < argcount; i++) {
2563 x = args[i];
2564 Py_INCREF(x);
2565 PyTuple_SET_ITEM(u, i-n, x);
2566 }
2567 }
2568 for (i = 0; i < kwcount; i++) {
2569 PyObject *keyword = kws[2*i];
2570 PyObject *value = kws[2*i + 1];
2571 int j;
2572 if (keyword == NULL || !PyString_Check(keyword)) {
2573 PyErr_Format(PyExc_TypeError,
2574 "%.200s() keywords must be strings",
2575 PyString_AsString(co->co_name));
2576 goto fail;
2577 }
2578 /* XXX slow -- speed up using dictionary? */
2579 for (j = 0; j < co->co_argcount; j++) {
2580 PyObject *nm = PyTuple_GET_ITEM(
2581 co->co_varnames, j);
2582 int cmp = PyObject_RichCompareBool(
2583 keyword, nm, Py_EQ);
2584 if (cmp > 0)
2585 break;
2586 else if (cmp < 0)
2587 goto fail;
2588 }
2589 /* Check errors from Compare */
2590 if (PyErr_Occurred())
2591 goto fail;
2592 if (j >= co->co_argcount) {
2593 if (kwdict == NULL) {
2594 PyErr_Format(PyExc_TypeError,
2595 "%.200s() got an unexpected "
2596 "keyword argument '%.400s'",
2597 PyString_AsString(co->co_name),
2598 PyString_AsString(keyword));
2599 goto fail;
2600 }
2601 PyDict_SetItem(kwdict, keyword, value);
2602 }
2603 else {
2604 if (GETLOCAL(j) != NULL) {
2605 PyErr_Format(PyExc_TypeError,
2606 "%.200s() got multiple "
2607 "values for keyword "
2608 "argument '%.400s'",
2609 PyString_AsString(co->co_name),
2610 PyString_AsString(keyword));
2611 goto fail;
2612 }
2613 Py_INCREF(value);
2614 SETLOCAL(j, value);
2615 }
2616 }
2617 if (argcount < co->co_argcount) {
2618 int m = co->co_argcount - defcount;
2619 for (i = argcount; i < m; i++) {
2620 if (GETLOCAL(i) == NULL) {
2621 PyErr_Format(PyExc_TypeError,
2622 "%.200s() takes %s %d "
2623 "%sargument%s (%d given)",
2624 PyString_AsString(co->co_name),
2625 ((co->co_flags & CO_VARARGS) ||
2626 defcount) ? "at least"
2627 : "exactly",
2628 m, kwcount ? "non-keyword " : "",
2629 m == 1 ? "" : "s", i);
2630 goto fail;
2631 }
2632 }
2633 if (n > m)
2634 i = n - m;
2635 else
2636 i = 0;
2637 for (; i < defcount; i++) {
2638 if (GETLOCAL(m+i) == NULL) {
2639 PyObject *def = defs[i];
2640 Py_INCREF(def);
2641 SETLOCAL(m+i, def);
2642 }
2643 }
2644 }
2645 }
2646 else {
2647 if (argcount > 0 || kwcount > 0) {
2648 PyErr_Format(PyExc_TypeError,
2649 "%.200s() takes no arguments (%d given)",
2650 PyString_AsString(co->co_name),
2651 argcount + kwcount);
2652 goto fail;
2653 }
2654 }
2655 /* Allocate and initialize storage for cell vars, and copy free
2656 vars into frame. This isn't too efficient right now. */
2657 if (f->f_ncells) {
2658 int i = 0, j = 0, nargs, found;
2659 char *cellname, *argname;
2660 PyObject *c;
2661
2662 nargs = co->co_argcount;
2663 if (co->co_flags & CO_VARARGS)
2664 nargs++;
2665 if (co->co_flags & CO_VARKEYWORDS)
2666 nargs++;
2667
2668 /* Check for cells that shadow args */
2669 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2670 cellname = PyString_AS_STRING(
2671 PyTuple_GET_ITEM(co->co_cellvars, i));
2672 found = 0;
2673 while (j < nargs) {
2674 argname = PyString_AS_STRING(
2675 PyTuple_GET_ITEM(co->co_varnames, j));
2676 if (strcmp(cellname, argname) == 0) {
2677 c = PyCell_New(GETLOCAL(j));
2678 if (c == NULL)
2679 goto fail;
2680 GETLOCAL(f->f_nlocals + i) = c;
2681 found = 1;
2682 break;
2683 }
2684 j++;
2685 }
2686 if (found == 0) {
2687 c = PyCell_New(NULL);
2688 if (c == NULL)
2689 goto fail;
2690 SETLOCAL(f->f_nlocals + i, c);
2691 }
2692 }
2693 /* Initialize any that are left */
2694 while (i < f->f_ncells) {
2695 c = PyCell_New(NULL);
2696 if (c == NULL)
2697 goto fail;
2698 SETLOCAL(f->f_nlocals + i, c);
2699 i++;
2700 }
2701 }
2702 if (f->f_nfreevars) {
2703 int i;
2704 for (i = 0; i < f->f_nfreevars; ++i) {
2705 PyObject *o = PyTuple_GET_ITEM(closure, i);
2706 Py_INCREF(o);
2707 freevars[f->f_ncells + i] = o;
2708 }
2709 }
2710
Tim Peters5ca576e2001-06-18 22:08:13 +00002711 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002712 /* Don't need to keep the reference to f_back, it will be set
2713 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002714 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002715 f->f_back = NULL;
2716
Jeremy Hylton985eba52003-02-05 23:13:00 +00002717 PCALL(PCALL_GENERATOR);
2718
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002719 /* Create a new generator that owns the ready to run frame
2720 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002721 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002722 }
2723
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002724 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002725
2726 fail: /* Jump here from prelude on failure */
2727
Tim Petersb13680b2001-11-27 23:29:29 +00002728 /* decref'ing the frame can cause __del__ methods to get invoked,
2729 which can call back into Python. While we're done with the
2730 current Python frame (f), the associated C stack is still in use,
2731 so recursion_depth must be boosted for the duration.
2732 */
2733 assert(tstate != NULL);
2734 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002735 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002736 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002737 return retval;
2738}
2739
2740
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002741/* Implementation notes for set_exc_info() and reset_exc_info():
2742
2743- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2744 'exc_traceback'. These always travel together.
2745
2746- tstate->curexc_ZZZ is the "hot" exception that is set by
2747 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2748
2749- Once an exception is caught by an except clause, it is transferred
2750 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2751 can pick it up. This is the primary task of set_exc_info().
2752
2753- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2754
2755 Long ago, when none of this existed, there were just a few globals:
2756 one set corresponding to the "hot" exception, and one set
2757 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2758 globals; they were simply stored as sys.exc_ZZZ. For backwards
2759 compatibility, they still are!) The problem was that in code like
2760 this:
2761
2762 try:
2763 "something that may fail"
2764 except "some exception":
2765 "do something else first"
2766 "print the exception from sys.exc_ZZZ."
2767
2768 if "do something else first" invoked something that raised and caught
2769 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2770 cause of subtle bugs. I fixed this by changing the semantics as
2771 follows:
2772
2773 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2774 *in that frame*.
2775
2776 - But initially, and as long as no exception is caught in a given
2777 frame, sys.exc_ZZZ will hold the last exception caught in the
2778 previous frame (or the frame before that, etc.).
2779
2780 The first bullet fixed the bug in the above example. The second
2781 bullet was for backwards compatibility: it was (and is) common to
2782 have a function that is called when an exception is caught, and to
2783 have that function access the caught exception via sys.exc_ZZZ.
2784 (Example: traceback.print_exc()).
2785
2786 At the same time I fixed the problem that sys.exc_ZZZ weren't
2787 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2788 but that's really a separate improvement.
2789
2790 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2791 variables to what they were before the current frame was called. The
2792 set_exc_info() function saves them on the frame so that
2793 reset_exc_info() can restore them. The invariant is that
2794 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2795 exception (where "catching" an exception applies only to successful
2796 except clauses); and if the current frame ever caught an exception,
2797 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2798 at the start of the current frame.
2799
2800*/
2801
Guido van Rossuma027efa1997-05-05 20:56:21 +00002802static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002803set_exc_info(PyThreadState *tstate,
2804 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002805{
2806 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002807 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002808
Guido van Rossuma027efa1997-05-05 20:56:21 +00002809 frame = tstate->frame;
2810 if (frame->f_exc_type == NULL) {
2811 /* This frame didn't catch an exception before */
2812 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002813 if (tstate->exc_type == NULL) {
2814 Py_INCREF(Py_None);
2815 tstate->exc_type = Py_None;
2816 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002817 tmp_type = frame->f_exc_type;
2818 tmp_value = frame->f_exc_value;
2819 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002820 Py_XINCREF(tstate->exc_type);
2821 Py_XINCREF(tstate->exc_value);
2822 Py_XINCREF(tstate->exc_traceback);
2823 frame->f_exc_type = tstate->exc_type;
2824 frame->f_exc_value = tstate->exc_value;
2825 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002826 Py_XDECREF(tmp_type);
2827 Py_XDECREF(tmp_value);
2828 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002829 }
2830 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002831 tmp_type = tstate->exc_type;
2832 tmp_value = tstate->exc_value;
2833 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002834 Py_XINCREF(type);
2835 Py_XINCREF(value);
2836 Py_XINCREF(tb);
2837 tstate->exc_type = type;
2838 tstate->exc_value = value;
2839 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002840 Py_XDECREF(tmp_type);
2841 Py_XDECREF(tmp_value);
2842 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002843 /* For b/w compatibility */
2844 PySys_SetObject("exc_type", type);
2845 PySys_SetObject("exc_value", value);
2846 PySys_SetObject("exc_traceback", tb);
2847}
2848
2849static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002850reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002851{
2852 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002853 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002854 frame = tstate->frame;
2855 if (frame->f_exc_type != NULL) {
2856 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002857 tmp_type = tstate->exc_type;
2858 tmp_value = tstate->exc_value;
2859 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002860 Py_XINCREF(frame->f_exc_type);
2861 Py_XINCREF(frame->f_exc_value);
2862 Py_XINCREF(frame->f_exc_traceback);
2863 tstate->exc_type = frame->f_exc_type;
2864 tstate->exc_value = frame->f_exc_value;
2865 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002866 Py_XDECREF(tmp_type);
2867 Py_XDECREF(tmp_value);
2868 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002869 /* For b/w compatibility */
2870 PySys_SetObject("exc_type", frame->f_exc_type);
2871 PySys_SetObject("exc_value", frame->f_exc_value);
2872 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2873 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002874 tmp_type = frame->f_exc_type;
2875 tmp_value = frame->f_exc_value;
2876 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002877 frame->f_exc_type = NULL;
2878 frame->f_exc_value = NULL;
2879 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002880 Py_XDECREF(tmp_type);
2881 Py_XDECREF(tmp_value);
2882 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002883}
2884
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002885/* Logic for the raise statement (too complicated for inlining).
2886 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002887static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002888do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002889{
Guido van Rossumd295f121998-04-09 21:39:57 +00002890 if (type == NULL) {
2891 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002892 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002893 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2894 value = tstate->exc_value;
2895 tb = tstate->exc_traceback;
2896 Py_XINCREF(type);
2897 Py_XINCREF(value);
2898 Py_XINCREF(tb);
2899 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002900
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002901 /* We support the following forms of raise:
2902 raise <class>, <classinstance>
2903 raise <class>, <argument tuple>
2904 raise <class>, None
2905 raise <class>, <argument>
2906 raise <classinstance>, None
2907 raise <string>, <object>
2908 raise <string>, None
2909
2910 An omitted second argument is the same as None.
2911
2912 In addition, raise <tuple>, <anything> is the same as
2913 raising the tuple's first item (and it better have one!);
2914 this rule is applied recursively.
2915
2916 Finally, an optional third argument can be supplied, which
2917 gives the traceback to be substituted (useful when
2918 re-raising an exception after examining it). */
2919
2920 /* First, check the traceback argument, replacing None with
2921 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002922 if (tb == Py_None) {
2923 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002924 tb = NULL;
2925 }
2926 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002927 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002928 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002929 goto raise_error;
2930 }
2931
2932 /* Next, replace a missing value with None */
2933 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002934 value = Py_None;
2935 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002936 }
2937
2938 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002939 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2940 PyObject *tmp = type;
2941 type = PyTuple_GET_ITEM(type, 0);
2942 Py_INCREF(type);
2943 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002944 }
2945
Tim Petersafb2c802002-04-18 18:06:20 +00002946 if (PyString_CheckExact(type))
2947 /* Raising builtin string is deprecated but still allowed --
2948 * do nothing. Raising an instance of a new-style str
2949 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002950 PyErr_Warn(PyExc_PendingDeprecationWarning,
2951 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002952
2953 else if (PyClass_Check(type))
2954 PyErr_NormalizeException(&type, &value, &tb);
2955
Guido van Rossumb209a111997-04-29 18:18:01 +00002956 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002957 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002958 if (value != Py_None) {
2959 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002960 "instance exception may not have a separate value");
2961 goto raise_error;
2962 }
2963 else {
2964 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002965 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002966 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002967 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2968 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002969 }
2970 }
2971 else {
2972 /* Not something you can raise. You get an exception
2973 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002974 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002975 "exceptions must be classes, instances, or "
2976 "strings (deprecated), not %s",
2977 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002978 goto raise_error;
2979 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002980 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002981 if (tb == NULL)
2982 return WHY_EXCEPTION;
2983 else
2984 return WHY_RERAISE;
2985 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002986 Py_XDECREF(value);
2987 Py_XDECREF(type);
2988 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002989 return WHY_EXCEPTION;
2990}
2991
Tim Petersd6d010b2001-06-21 02:49:55 +00002992/* Iterate v argcnt times and store the results on the stack (via decreasing
2993 sp). Return 1 for success, 0 if error. */
2994
Barry Warsawe42b18f1997-08-25 22:13:04 +00002995static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002996unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002997{
Tim Petersd6d010b2001-06-21 02:49:55 +00002998 int i = 0;
2999 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003000 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003001
Tim Petersd6d010b2001-06-21 02:49:55 +00003002 assert(v != NULL);
3003
3004 it = PyObject_GetIter(v);
3005 if (it == NULL)
3006 goto Error;
3007
3008 for (; i < argcnt; i++) {
3009 w = PyIter_Next(it);
3010 if (w == NULL) {
3011 /* Iterator done, via error or exhaustion. */
3012 if (!PyErr_Occurred()) {
3013 PyErr_Format(PyExc_ValueError,
3014 "need more than %d value%s to unpack",
3015 i, i == 1 ? "" : "s");
3016 }
3017 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003018 }
3019 *--sp = w;
3020 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003021
3022 /* We better have exhausted the iterator now. */
3023 w = PyIter_Next(it);
3024 if (w == NULL) {
3025 if (PyErr_Occurred())
3026 goto Error;
3027 Py_DECREF(it);
3028 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003029 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003030 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003031 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003032 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003033Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003034 for (; i > 0; i--, sp++)
3035 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003036 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003037 return 0;
3038}
3039
3040
Guido van Rossum96a42c81992-01-12 02:29:51 +00003041#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003042static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003043prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003044{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003045 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003046 if (PyObject_Print(v, stdout, 0) != 0)
3047 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003048 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003049 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003050}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003051#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003052
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003053static void
Fred Drake5755ce62001-06-27 19:19:46 +00003054call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003055{
Guido van Rossumb209a111997-04-29 18:18:01 +00003056 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003057 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003058 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003059 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003060 value = Py_None;
3061 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003062 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003063 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003064 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003065 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003066 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003067 }
Fred Drake5755ce62001-06-27 19:19:46 +00003068 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003069 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003070 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003071 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003072 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003073 Py_XDECREF(type);
3074 Py_XDECREF(value);
3075 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003076 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003077}
3078
Fred Drake4ec5d562001-10-04 19:26:43 +00003079static void
3080call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3081 int what)
3082{
3083 PyObject *type, *value, *traceback;
3084 int err;
3085 PyErr_Fetch(&type, &value, &traceback);
3086 err = call_trace(func, obj, frame, what, NULL);
3087 if (err == 0)
3088 PyErr_Restore(type, value, traceback);
3089 else {
3090 Py_XDECREF(type);
3091 Py_XDECREF(value);
3092 Py_XDECREF(traceback);
3093 }
3094}
3095
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003096static int
Fred Drake5755ce62001-06-27 19:19:46 +00003097call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3098 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003099{
Fred Drake5755ce62001-06-27 19:19:46 +00003100 register PyThreadState *tstate = frame->f_tstate;
3101 int result;
3102 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003103 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003104 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003105 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003106 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003107 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3108 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003109 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003110 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003111}
3112
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003113PyObject *
3114_PyEval_CallTracing(PyObject *func, PyObject *args)
3115{
3116 PyFrameObject *frame = PyEval_GetFrame();
3117 PyThreadState *tstate = frame->f_tstate;
3118 int save_tracing = tstate->tracing;
3119 int save_use_tracing = tstate->use_tracing;
3120 PyObject *result;
3121
3122 tstate->tracing = 0;
3123 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3124 || (tstate->c_profilefunc != NULL));
3125 result = PyObject_Call(func, args, NULL);
3126 tstate->tracing = save_tracing;
3127 tstate->use_tracing = save_use_tracing;
3128 return result;
3129}
3130
Michael W. Hudson006c7522002-11-08 13:08:46 +00003131static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003132maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003133 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3134 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003135{
3136 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003137
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003138 In a nutshell, we use the co_lnotab field of the code object
3139 to tell when execution has moved onto a different line.
3140
3141 As mentioned above, the basic idea is so set things up so
3142 that
3143
3144 *instr_lb <= frame->f_lasti < *instr_ub
3145
3146 is true so long as execution does not change lines.
3147
3148 This is all fairly simple. Digging the information out of
3149 co_lnotab takes some work, but is conceptually clear.
3150
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003151 Somewhat harder to explain is why we don't *always* call the
3152 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003153
3154 Consider this code:
3155
3156 1: def f(a):
3157 2: if a:
3158 3: print 1
3159 4: else:
3160 5: print 2
3161
3162 which compiles to this:
3163
3164 2 0 LOAD_FAST 0 (a)
3165 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003166 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003167
3168 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003169 10 PRINT_ITEM
3170 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003171 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003172 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003173
3174 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003175 19 PRINT_ITEM
3176 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003177 >> 21 LOAD_CONST 0 (None)
3178 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003179
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003180 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003181 15 and the co_lnotab will claim that execution has moved to
3182 line 3. This is at best misleading. In this case we could
3183 associate the POP_TOP with line 4, but that doesn't make
3184 sense in all cases (I think).
3185
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003186 What we do is only call the line trace function if the co_lnotab
3187 indicates we have jumped to the *start* of a line, i.e. if the
3188 current instruction offset matches the offset given for the
3189 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003190
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003191 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003192 Execution will jump from instruction offset 12 to offset 21.
3193 Then the co_lnotab would imply that execution has moved to line
3194 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003195
3196 Why do we set f_lineno when tracing? Well, consider the code
3197 above when 'a' is true. If stepping through this with 'n' in
3198 pdb, you would stop at line 1 with a "call" type event, then
3199 line events on lines 2 and 3, then a "return" type event -- but
3200 you would be shown line 5 during this event. This is a change
3201 from the behaviour in 2.2 and before, and I've found it
3202 confusing in practice. By setting and using f_lineno when
3203 tracing, one can report a line number different from that
3204 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003205 */
3206
Michael W. Hudson006c7522002-11-08 13:08:46 +00003207 int result = 0;
3208
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003209 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003210 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003211 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003212 unsigned char* p;
3213
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003214 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3215 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003216
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003217 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003218 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003219
3220 /* possible optimization: if f->f_lasti == instr_ub
3221 (likely to be a common case) then we already know
3222 instr_lb -- if we stored the matching value of p
3223 somwhere we could skip the first while loop. */
3224
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003225 /* see comments in compile.c for the description of
3226 co_lnotab. A point to remember: increments to p
3227 should come in pairs -- although we don't care about
3228 the line increments here, treating them as byte
3229 increments gets confusing, to say the least. */
3230
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003231 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003232 if (addr + *p > frame->f_lasti)
3233 break;
3234 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003235 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003236 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003237 --size;
3238 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003239
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003240 if (addr == frame->f_lasti) {
3241 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003242 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003243 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003244 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003245
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003246 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003247 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003248 addr += *p++;
3249 if (*p++)
3250 break;
3251 }
3252 *instr_ub = addr;
3253 }
3254 else {
3255 *instr_ub = INT_MAX;
3256 }
3257 }
Armin Rigobf57a142004-03-22 19:24:58 +00003258 else if (frame->f_lasti <= *instr_prev) {
3259 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003260 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003261 PyTrace_LINE, Py_None);
3262 }
3263 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003264 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003265}
3266
Fred Drake5755ce62001-06-27 19:19:46 +00003267void
3268PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003269{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003270 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003271 PyObject *temp = tstate->c_profileobj;
3272 Py_XINCREF(arg);
3273 tstate->c_profilefunc = NULL;
3274 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003275 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003276 Py_XDECREF(temp);
3277 tstate->c_profilefunc = func;
3278 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003279 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003280}
3281
3282void
3283PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3284{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003285 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003286 PyObject *temp = tstate->c_traceobj;
3287 Py_XINCREF(arg);
3288 tstate->c_tracefunc = NULL;
3289 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003290 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003291 Py_XDECREF(temp);
3292 tstate->c_tracefunc = func;
3293 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003294 tstate->use_tracing = ((func != NULL)
3295 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003296}
3297
Guido van Rossumb209a111997-04-29 18:18:01 +00003298PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003299PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003300{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003301 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003302 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003303 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003304 else
3305 return current_frame->f_builtins;
3306}
3307
Guido van Rossumb209a111997-04-29 18:18:01 +00003308PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003309PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003310{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003311 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003312 if (current_frame == NULL)
3313 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003314 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003315 return current_frame->f_locals;
3316}
3317
Guido van Rossumb209a111997-04-29 18:18:01 +00003318PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003319PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003320{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003321 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003322 if (current_frame == NULL)
3323 return NULL;
3324 else
3325 return current_frame->f_globals;
3326}
3327
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003328PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003329PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003330{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003331 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003332 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003333}
3334
Guido van Rossum6135a871995-01-09 17:53:26 +00003335int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003336PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003337{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003338 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003339 return current_frame == NULL ? 0 : current_frame->f_restricted;
3340}
3341
Guido van Rossumbe270261997-05-22 22:26:18 +00003342int
Tim Peters5ba58662001-07-16 02:29:45 +00003343PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003344{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003345 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003346 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003347
3348 if (current_frame != NULL) {
3349 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003350 const int compilerflags = codeflags & PyCF_MASK;
3351 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003352 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003353 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003354 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003355#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003356 if (codeflags & CO_GENERATOR_ALLOWED) {
3357 result = 1;
3358 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3359 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003360#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003361 }
3362 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003363}
3364
3365int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003366Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003367{
Guido van Rossumb209a111997-04-29 18:18:01 +00003368 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003369 if (f == NULL)
3370 return 0;
3371 if (!PyFile_SoftSpace(f, 0))
3372 return 0;
3373 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003374}
3375
Guido van Rossum3f5da241990-12-20 15:06:42 +00003376
Guido van Rossum681d79a1995-07-18 14:51:37 +00003377/* External interface to call any callable object.
3378 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003379
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003380#undef PyEval_CallObject
3381/* for backward compatibility: export this interface */
3382
Guido van Rossumb209a111997-04-29 18:18:01 +00003383PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003384PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003385{
Guido van Rossumb209a111997-04-29 18:18:01 +00003386 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003387}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003388#define PyEval_CallObject(func,arg) \
3389 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003390
Guido van Rossumb209a111997-04-29 18:18:01 +00003391PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003392PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003393{
Jeremy Hylton52820442001-01-03 23:52:36 +00003394 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003395
3396 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003397 arg = PyTuple_New(0);
3398 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003399 PyErr_SetString(PyExc_TypeError,
3400 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003401 return NULL;
3402 }
3403 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003404 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003405
Guido van Rossumb209a111997-04-29 18:18:01 +00003406 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003407 PyErr_SetString(PyExc_TypeError,
3408 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003409 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003410 return NULL;
3411 }
3412
Tim Peters6d6c1a32001-08-02 04:15:00 +00003413 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003414 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003415 return result;
3416}
3417
Tim Peters6d6c1a32001-08-02 04:15:00 +00003418char *
3419PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003420{
3421 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003422 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003423 else if (PyFunction_Check(func))
3424 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3425 else if (PyCFunction_Check(func))
3426 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3427 else if (PyClass_Check(func))
3428 return PyString_AsString(((PyClassObject*)func)->cl_name);
3429 else if (PyInstance_Check(func)) {
3430 return PyString_AsString(
3431 ((PyInstanceObject*)func)->in_class->cl_name);
3432 } else {
3433 return func->ob_type->tp_name;
3434 }
3435}
3436
Tim Peters6d6c1a32001-08-02 04:15:00 +00003437char *
3438PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003439{
3440 if (PyMethod_Check(func))
3441 return "()";
3442 else if (PyFunction_Check(func))
3443 return "()";
3444 else if (PyCFunction_Check(func))
3445 return "()";
3446 else if (PyClass_Check(func))
3447 return " constructor";
3448 else if (PyInstance_Check(func)) {
3449 return " instance";
3450 } else {
3451 return " object";
3452 }
3453}
3454
Jeremy Hylton52820442001-01-03 23:52:36 +00003455#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3456
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003457static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003458err_args(PyObject *func, int flags, int nargs)
3459{
3460 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003461 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003462 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003463 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003464 nargs);
3465 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003466 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003467 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003468 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003469 nargs);
3470}
3471
Nicholas Bastind858a772004-06-25 23:31:06 +00003472#define C_TRACE(call) \
3473if (tstate->use_tracing && tstate->c_profilefunc) { \
3474 if (call_trace(tstate->c_profilefunc, \
3475 tstate->c_profileobj, \
3476 tstate->frame, PyTrace_C_CALL, \
3477 func)) \
3478 { return NULL; } \
3479 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003480 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003481 if (x == NULL) { \
3482 if (call_trace (tstate->c_profilefunc, \
3483 tstate->c_profileobj, \
3484 tstate->frame, PyTrace_C_EXCEPTION, \
3485 func)) \
3486 { return NULL; } \
3487 } else { \
3488 if (call_trace(tstate->c_profilefunc, \
3489 tstate->c_profileobj, \
3490 tstate->frame, PyTrace_C_RETURN, \
3491 func)) \
3492 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003493 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003494 } \
3495} else { \
3496 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003497 }
3498
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003499static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003500call_function(PyObject ***pp_stack, int oparg
3501#ifdef WITH_TSC
3502 , uint64* pintr0, uint64* pintr1
3503#endif
3504 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003505{
3506 int na = oparg & 0xff;
3507 int nk = (oparg>>8) & 0xff;
3508 int n = na + 2 * nk;
3509 PyObject **pfunc = (*pp_stack) - n - 1;
3510 PyObject *func = *pfunc;
3511 PyObject *x, *w;
3512
Jeremy Hylton985eba52003-02-05 23:13:00 +00003513 /* Always dispatch PyCFunction first, because these are
3514 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003515 */
3516 if (PyCFunction_Check(func) && nk == 0) {
3517 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003518 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003519
3520 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003521 if (flags & (METH_NOARGS | METH_O)) {
3522 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3523 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003524 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003525 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003526 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003527 else if (flags & METH_O && na == 1) {
3528 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003529 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003530 Py_DECREF(arg);
3531 }
3532 else {
3533 err_args(func, flags, na);
3534 x = NULL;
3535 }
3536 }
3537 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003538 PyObject *callargs;
3539 callargs = load_args(pp_stack, na);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003540 rdtscll(*pintr0);
Nicholas Bastind858a772004-06-25 23:31:06 +00003541 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003542 rdtscll(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003543 Py_XDECREF(callargs);
3544 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003545 } else {
3546 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3547 /* optimize access to bound methods */
3548 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003549 PCALL(PCALL_METHOD);
3550 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003551 Py_INCREF(self);
3552 func = PyMethod_GET_FUNCTION(func);
3553 Py_INCREF(func);
3554 Py_DECREF(*pfunc);
3555 *pfunc = self;
3556 na++;
3557 n++;
3558 } else
3559 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003560 rdtscll(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003561 if (PyFunction_Check(func))
3562 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003563 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003564 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003565 rdtscll(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003566 Py_DECREF(func);
3567 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003568
Jeremy Hylton985eba52003-02-05 23:13:00 +00003569 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003570 while ((*pp_stack) > pfunc) {
3571 w = EXT_POP(*pp_stack);
3572 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003573 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003574 }
3575 return x;
3576}
3577
Jeremy Hylton192690e2002-08-16 18:36:11 +00003578/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003579 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003580 For the simplest case -- a function that takes only positional
3581 arguments and is called with only positional arguments -- it
3582 inlines the most primitive frame setup code from
3583 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3584 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003585*/
3586
3587static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003588fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003589{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003590 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003591 PyObject *globals = PyFunction_GET_GLOBALS(func);
3592 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3593 PyObject **d = NULL;
3594 int nd = 0;
3595
Jeremy Hylton985eba52003-02-05 23:13:00 +00003596 PCALL(PCALL_FUNCTION);
3597 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003598 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003599 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3600 PyFrameObject *f;
3601 PyObject *retval = NULL;
3602 PyThreadState *tstate = PyThreadState_GET();
3603 PyObject **fastlocals, **stack;
3604 int i;
3605
3606 PCALL(PCALL_FASTER_FUNCTION);
3607 assert(globals != NULL);
3608 /* XXX Perhaps we should create a specialized
3609 PyFrame_New() that doesn't take locals, but does
3610 take builtins without sanity checking them.
3611 */
3612 f = PyFrame_New(tstate, co, globals, NULL);
3613 if (f == NULL)
3614 return NULL;
3615
3616 fastlocals = f->f_localsplus;
3617 stack = (*pp_stack) - n;
3618
3619 for (i = 0; i < n; i++) {
3620 Py_INCREF(*stack);
3621 fastlocals[i] = *stack++;
3622 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003623 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003624 assert(tstate != NULL);
3625 ++tstate->recursion_depth;
3626 Py_DECREF(f);
3627 --tstate->recursion_depth;
3628 return retval;
3629 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003630 if (argdefs != NULL) {
3631 d = &PyTuple_GET_ITEM(argdefs, 0);
3632 nd = ((PyTupleObject *)argdefs)->ob_size;
3633 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003634 return PyEval_EvalCodeEx(co, globals,
3635 (PyObject *)NULL, (*pp_stack)-n, na,
3636 (*pp_stack)-2*nk, nk, d, nd,
3637 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003638}
3639
3640static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003641update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3642 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003643{
3644 PyObject *kwdict = NULL;
3645 if (orig_kwdict == NULL)
3646 kwdict = PyDict_New();
3647 else {
3648 kwdict = PyDict_Copy(orig_kwdict);
3649 Py_DECREF(orig_kwdict);
3650 }
3651 if (kwdict == NULL)
3652 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003653 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003654 int err;
3655 PyObject *value = EXT_POP(*pp_stack);
3656 PyObject *key = EXT_POP(*pp_stack);
3657 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003658 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003659 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003660 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003661 PyEval_GetFuncName(func),
3662 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003663 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003664 Py_DECREF(key);
3665 Py_DECREF(value);
3666 Py_DECREF(kwdict);
3667 return NULL;
3668 }
3669 err = PyDict_SetItem(kwdict, key, value);
3670 Py_DECREF(key);
3671 Py_DECREF(value);
3672 if (err) {
3673 Py_DECREF(kwdict);
3674 return NULL;
3675 }
3676 }
3677 return kwdict;
3678}
3679
3680static PyObject *
3681update_star_args(int nstack, int nstar, PyObject *stararg,
3682 PyObject ***pp_stack)
3683{
3684 PyObject *callargs, *w;
3685
3686 callargs = PyTuple_New(nstack + nstar);
3687 if (callargs == NULL) {
3688 return NULL;
3689 }
3690 if (nstar) {
3691 int i;
3692 for (i = 0; i < nstar; i++) {
3693 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3694 Py_INCREF(a);
3695 PyTuple_SET_ITEM(callargs, nstack + i, a);
3696 }
3697 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003698 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003699 w = EXT_POP(*pp_stack);
3700 PyTuple_SET_ITEM(callargs, nstack, w);
3701 }
3702 return callargs;
3703}
3704
3705static PyObject *
3706load_args(PyObject ***pp_stack, int na)
3707{
3708 PyObject *args = PyTuple_New(na);
3709 PyObject *w;
3710
3711 if (args == NULL)
3712 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003713 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003714 w = EXT_POP(*pp_stack);
3715 PyTuple_SET_ITEM(args, na, w);
3716 }
3717 return args;
3718}
3719
3720static PyObject *
3721do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3722{
3723 PyObject *callargs = NULL;
3724 PyObject *kwdict = NULL;
3725 PyObject *result = NULL;
3726
3727 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003728 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003729 if (kwdict == NULL)
3730 goto call_fail;
3731 }
3732 callargs = load_args(pp_stack, na);
3733 if (callargs == NULL)
3734 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003735#ifdef CALL_PROFILE
3736 /* At this point, we have to look at the type of func to
3737 update the call stats properly. Do it here so as to avoid
3738 exposing the call stats machinery outside ceval.c
3739 */
3740 if (PyFunction_Check(func))
3741 PCALL(PCALL_FUNCTION);
3742 else if (PyMethod_Check(func))
3743 PCALL(PCALL_METHOD);
3744 else if (PyType_Check(func))
3745 PCALL(PCALL_TYPE);
3746 else
3747 PCALL(PCALL_OTHER);
3748#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003749 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003750 call_fail:
3751 Py_XDECREF(callargs);
3752 Py_XDECREF(kwdict);
3753 return result;
3754}
3755
3756static PyObject *
3757ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3758{
3759 int nstar = 0;
3760 PyObject *callargs = NULL;
3761 PyObject *stararg = NULL;
3762 PyObject *kwdict = NULL;
3763 PyObject *result = NULL;
3764
3765 if (flags & CALL_FLAG_KW) {
3766 kwdict = EXT_POP(*pp_stack);
3767 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003768 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003769 "%s%s argument after ** "
3770 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003771 PyEval_GetFuncName(func),
3772 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003773 goto ext_call_fail;
3774 }
3775 }
3776 if (flags & CALL_FLAG_VAR) {
3777 stararg = EXT_POP(*pp_stack);
3778 if (!PyTuple_Check(stararg)) {
3779 PyObject *t = NULL;
3780 t = PySequence_Tuple(stararg);
3781 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003782 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3783 PyErr_Format(PyExc_TypeError,
3784 "%s%s argument after * "
3785 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003786 PyEval_GetFuncName(func),
3787 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003788 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003789 goto ext_call_fail;
3790 }
3791 Py_DECREF(stararg);
3792 stararg = t;
3793 }
3794 nstar = PyTuple_GET_SIZE(stararg);
3795 }
3796 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003797 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003798 if (kwdict == NULL)
3799 goto ext_call_fail;
3800 }
3801 callargs = update_star_args(na, nstar, stararg, pp_stack);
3802 if (callargs == NULL)
3803 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003804#ifdef CALL_PROFILE
3805 /* At this point, we have to look at the type of func to
3806 update the call stats properly. Do it here so as to avoid
3807 exposing the call stats machinery outside ceval.c
3808 */
3809 if (PyFunction_Check(func))
3810 PCALL(PCALL_FUNCTION);
3811 else if (PyMethod_Check(func))
3812 PCALL(PCALL_METHOD);
3813 else if (PyType_Check(func))
3814 PCALL(PCALL_TYPE);
3815 else
3816 PCALL(PCALL_OTHER);
3817#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003818 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003819 ext_call_fail:
3820 Py_XDECREF(callargs);
3821 Py_XDECREF(kwdict);
3822 Py_XDECREF(stararg);
3823 return result;
3824}
3825
Tim Peterscb479e72001-12-16 19:11:44 +00003826/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3827 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3828 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3829*/
Tim Petersb5196382001-12-16 19:44:20 +00003830/* Note: If v is NULL, return success without storing into *pi. This
3831 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3832 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003833*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003834int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003835_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003836{
Tim Petersb5196382001-12-16 19:44:20 +00003837 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003838 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003839 if (PyInt_Check(v)) {
3840 x = PyInt_AsLong(v);
3841 } else if (PyLong_Check(v)) {
3842 x = PyLong_AsLong(v);
3843 if (x==-1 && PyErr_Occurred()) {
3844 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003845 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003846
Guido van Rossumac7be682001-01-17 15:42:30 +00003847 if (!PyErr_ExceptionMatches(
3848 PyExc_OverflowError)) {
3849 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003850 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003851 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003852 }
3853
Guido van Rossumac7be682001-01-17 15:42:30 +00003854 /* Clear the OverflowError */
3855 PyErr_Clear();
3856
3857 /* It's an overflow error, so we need to
3858 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003859 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003860 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003861
3862 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003863 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003864 if (long_zero == NULL)
3865 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003866
3867 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003868 cmp = PyObject_RichCompareBool(v, long_zero,
3869 Py_GT);
3870 Py_DECREF(long_zero);
3871 if (cmp < 0)
3872 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003873 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003874 x = INT_MAX;
3875 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003876 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003877 }
3878 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003879 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003880 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003881 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003882 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003883 /* Truncate -- very long indices are truncated anyway */
3884 if (x > INT_MAX)
3885 x = INT_MAX;
3886 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003887 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003888 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003889 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003890 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891}
3892
Guido van Rossum50d756e2001-08-18 17:43:36 +00003893#undef ISINT
3894#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3895
Guido van Rossumb209a111997-04-29 18:18:01 +00003896static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003897apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003898{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003899 PyTypeObject *tp = u->ob_type;
3900 PySequenceMethods *sq = tp->tp_as_sequence;
3901
3902 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3903 int ilow = 0, ihigh = INT_MAX;
3904 if (!_PyEval_SliceIndex(v, &ilow))
3905 return NULL;
3906 if (!_PyEval_SliceIndex(w, &ihigh))
3907 return NULL;
3908 return PySequence_GetSlice(u, ilow, ihigh);
3909 }
3910 else {
3911 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003912 if (slice != NULL) {
3913 PyObject *res = PyObject_GetItem(u, slice);
3914 Py_DECREF(slice);
3915 return res;
3916 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003917 else
3918 return NULL;
3919 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003920}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003921
3922static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003923assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3924 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003925{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003926 PyTypeObject *tp = u->ob_type;
3927 PySequenceMethods *sq = tp->tp_as_sequence;
3928
3929 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3930 int ilow = 0, ihigh = INT_MAX;
3931 if (!_PyEval_SliceIndex(v, &ilow))
3932 return -1;
3933 if (!_PyEval_SliceIndex(w, &ihigh))
3934 return -1;
3935 if (x == NULL)
3936 return PySequence_DelSlice(u, ilow, ihigh);
3937 else
3938 return PySequence_SetSlice(u, ilow, ihigh, x);
3939 }
3940 else {
3941 PyObject *slice = PySlice_New(v, w, NULL);
3942 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003943 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003944 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003945 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003946 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003947 res = PyObject_DelItem(u, slice);
3948 Py_DECREF(slice);
3949 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003950 }
3951 else
3952 return -1;
3953 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003954}
3955
Guido van Rossumb209a111997-04-29 18:18:01 +00003956static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003957cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003958{
Guido van Rossumac7be682001-01-17 15:42:30 +00003959 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003960 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003961 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003962 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003963 break;
3964 case PyCmp_IS_NOT:
3965 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003966 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003967 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003968 res = PySequence_Contains(w, v);
3969 if (res < 0)
3970 return NULL;
3971 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003972 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003973 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003974 if (res < 0)
3975 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003976 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003977 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003978 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003979 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003980 break;
3981 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003982 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003983 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003984 v = res ? Py_True : Py_False;
3985 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003986 return v;
3987}
3988
Thomas Wouters52152252000-08-17 22:55:00 +00003989static PyObject *
3990import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003991{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003992 PyObject *x;
3993
3994 x = PyObject_GetAttr(v, name);
3995 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003996 PyErr_Format(PyExc_ImportError,
3997 "cannot import name %.230s",
3998 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003999 }
Thomas Wouters52152252000-08-17 22:55:00 +00004000 return x;
4001}
Guido van Rossumac7be682001-01-17 15:42:30 +00004002
Thomas Wouters52152252000-08-17 22:55:00 +00004003static int
4004import_all_from(PyObject *locals, PyObject *v)
4005{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004006 PyObject *all = PyObject_GetAttrString(v, "__all__");
4007 PyObject *dict, *name, *value;
4008 int skip_leading_underscores = 0;
4009 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004010
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004011 if (all == NULL) {
4012 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4013 return -1; /* Unexpected error */
4014 PyErr_Clear();
4015 dict = PyObject_GetAttrString(v, "__dict__");
4016 if (dict == NULL) {
4017 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4018 return -1;
4019 PyErr_SetString(PyExc_ImportError,
4020 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004021 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004022 }
4023 all = PyMapping_Keys(dict);
4024 Py_DECREF(dict);
4025 if (all == NULL)
4026 return -1;
4027 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004028 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004029
4030 for (pos = 0, err = 0; ; pos++) {
4031 name = PySequence_GetItem(all, pos);
4032 if (name == NULL) {
4033 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4034 err = -1;
4035 else
4036 PyErr_Clear();
4037 break;
4038 }
4039 if (skip_leading_underscores &&
4040 PyString_Check(name) &&
4041 PyString_AS_STRING(name)[0] == '_')
4042 {
4043 Py_DECREF(name);
4044 continue;
4045 }
4046 value = PyObject_GetAttr(v, name);
4047 if (value == NULL)
4048 err = -1;
4049 else
4050 err = PyDict_SetItem(locals, name, value);
4051 Py_DECREF(name);
4052 Py_XDECREF(value);
4053 if (err != 0)
4054 break;
4055 }
4056 Py_DECREF(all);
4057 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004058}
4059
Guido van Rossumb209a111997-04-29 18:18:01 +00004060static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004061build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004062{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004063 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004064
4065 if (PyDict_Check(methods))
4066 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004067 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004068 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004069 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4070 base = PyTuple_GET_ITEM(bases, 0);
4071 metaclass = PyObject_GetAttrString(base, "__class__");
4072 if (metaclass == NULL) {
4073 PyErr_Clear();
4074 metaclass = (PyObject *)base->ob_type;
4075 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004076 }
4077 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004078 else {
4079 PyObject *g = PyEval_GetGlobals();
4080 if (g != NULL && PyDict_Check(g))
4081 metaclass = PyDict_GetItemString(g, "__metaclass__");
4082 if (metaclass == NULL)
4083 metaclass = (PyObject *) &PyClass_Type;
4084 Py_INCREF(metaclass);
4085 }
4086 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4087 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004088 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4089 /* A type error here likely means that the user passed
4090 in a base that was not a class (such the random module
4091 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004092 by augmenting the error message with more information.*/
4093
4094 PyObject *ptype, *pvalue, *ptraceback;
4095
4096 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4097 if (PyString_Check(pvalue)) {
4098 PyObject *newmsg;
4099 newmsg = PyString_FromFormat(
4100 "Error when calling the metaclass bases\n %s",
4101 PyString_AS_STRING(pvalue));
4102 if (newmsg != NULL) {
4103 Py_DECREF(pvalue);
4104 pvalue = newmsg;
4105 }
4106 }
4107 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004108 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004109 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004110}
4111
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004112static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004113exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4114 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004115{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004116 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004117 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004118 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004119
Guido van Rossumb209a111997-04-29 18:18:01 +00004120 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4121 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004122 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004123 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004124 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004125 locals = PyTuple_GetItem(prog, 2);
4126 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004127 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004128 if (globals == Py_None) {
4129 globals = PyEval_GetGlobals();
4130 if (locals == Py_None) {
4131 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004132 plain = 1;
4133 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004134 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004135 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004136 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004137 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004138 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004139 !PyCode_Check(prog) &&
4140 !PyFile_Check(prog)) {
4141 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004142 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004143 return -1;
4144 }
Fred Drake661ea262000-10-24 19:57:45 +00004145 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004146 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004147 "exec: arg 2 must be a dictionary or None");
4148 return -1;
4149 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004150 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004151 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004152 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004153 return -1;
4154 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004155 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004156 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004157 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004158 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4159 PyErr_SetString(PyExc_TypeError,
4160 "code object passed to exec may not contain free variables");
4161 return -1;
4162 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004163 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004164 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004165 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004166 FILE *fp = PyFile_AsFile(prog);
4167 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004168 PyCompilerFlags cf;
4169 cf.cf_flags = 0;
4170 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004171 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004172 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004173 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004174 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004175 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004176 }
4177 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004178 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004179 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004180 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004181 cf.cf_flags = 0;
4182#ifdef Py_USING_UNICODE
4183 if (PyUnicode_Check(prog)) {
4184 tmp = PyUnicode_AsUTF8String(prog);
4185 if (tmp == NULL)
4186 return -1;
4187 prog = tmp;
4188 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4189 }
4190#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004191 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004192 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004193 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004194 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004195 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004196 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004197 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004198 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004199 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004200 if (plain)
4201 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004202 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004203 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004204 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004205 return 0;
4206}
Guido van Rossum24c13741995-02-14 09:42:43 +00004207
Guido van Rossumac7be682001-01-17 15:42:30 +00004208static void
Paul Prescode68140d2000-08-30 20:25:01 +00004209format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4210{
4211 char *obj_str;
4212
4213 if (!obj)
4214 return;
4215
4216 obj_str = PyString_AsString(obj);
4217 if (!obj_str)
4218 return;
4219
4220 PyErr_Format(exc, format_str, obj_str);
4221}
Guido van Rossum950361c1997-01-24 13:49:28 +00004222
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004223static PyObject *
4224string_concatenate(PyObject *v, PyObject *w,
4225 PyFrameObject *f, unsigned char *next_instr)
4226{
4227 /* This function implements 'variable += expr' when both arguments
4228 are strings. */
4229
4230 if (v->ob_refcnt == 2) {
4231 /* In the common case, there are 2 references to the value
4232 * stored in 'variable' when the += is performed: one on the
4233 * value stack (in 'v') and one still stored in the 'variable'.
4234 * We try to delete the variable now to reduce the refcnt to 1.
4235 */
4236 switch (*next_instr) {
4237 case STORE_FAST:
4238 {
4239 int oparg = PEEKARG();
4240 PyObject **fastlocals = f->f_localsplus;
4241 if (GETLOCAL(oparg) == v)
4242 SETLOCAL(oparg, NULL);
4243 break;
4244 }
4245 case STORE_DEREF:
4246 {
4247 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4248 PyObject *c = freevars[PEEKARG()];
4249 if (PyCell_GET(c) == v)
4250 PyCell_Set(c, NULL);
4251 break;
4252 }
4253 case STORE_NAME:
4254 {
4255 PyObject *names = f->f_code->co_names;
4256 PyObject *name = GETITEM(names, PEEKARG());
4257 PyObject *locals = f->f_locals;
4258 if (PyDict_CheckExact(locals) &&
4259 PyDict_GetItem(locals, name) == v) {
4260 if (PyDict_DelItem(locals, name) != 0) {
4261 PyErr_Clear();
4262 }
4263 }
4264 break;
4265 }
4266 }
4267 }
4268
Armin Rigo618fbf52004-08-07 20:58:32 +00004269 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004270 /* Now we own the last reference to 'v', so we can resize it
4271 * in-place.
4272 */
4273 int v_len = PyString_GET_SIZE(v);
4274 int w_len = PyString_GET_SIZE(w);
4275 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4276 /* XXX if _PyString_Resize() fails, 'v' has been
4277 * deallocated so it cannot be put back into 'variable'.
4278 * The MemoryError is raised when there is no value in
4279 * 'variable', which might (very remotely) be a cause
4280 * of incompatibilities.
4281 */
4282 return NULL;
4283 }
4284 /* copy 'w' into the newly allocated area of 'v' */
4285 memcpy(PyString_AS_STRING(v) + v_len,
4286 PyString_AS_STRING(w), w_len);
4287 return v;
4288 }
4289 else {
4290 /* When in-place resizing is not an option. */
4291 PyString_Concat(&v, w);
4292 return v;
4293 }
4294}
4295
Guido van Rossum950361c1997-01-24 13:49:28 +00004296#ifdef DYNAMIC_EXECUTION_PROFILE
4297
Skip Montanarof118cb12001-10-15 20:51:38 +00004298static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004299getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004300{
4301 int i;
4302 PyObject *l = PyList_New(256);
4303 if (l == NULL) return NULL;
4304 for (i = 0; i < 256; i++) {
4305 PyObject *x = PyInt_FromLong(a[i]);
4306 if (x == NULL) {
4307 Py_DECREF(l);
4308 return NULL;
4309 }
4310 PyList_SetItem(l, i, x);
4311 }
4312 for (i = 0; i < 256; i++)
4313 a[i] = 0;
4314 return l;
4315}
4316
4317PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004318_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004319{
4320#ifndef DXPAIRS
4321 return getarray(dxp);
4322#else
4323 int i;
4324 PyObject *l = PyList_New(257);
4325 if (l == NULL) return NULL;
4326 for (i = 0; i < 257; i++) {
4327 PyObject *x = getarray(dxpairs[i]);
4328 if (x == NULL) {
4329 Py_DECREF(l);
4330 return NULL;
4331 }
4332 PyList_SetItem(l, i, x);
4333 }
4334 return l;
4335#endif
4336}
4337
4338#endif