blob: 186bc82122dab5f5222d7d1895bbbad3d11ea3b0 [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 Rossuma027efa1997-05-05 20:56:21 +0000206extern int _PyThread_Started; /* Flag for Py_Exit */
207
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000208static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000209static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000210
211void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000212PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000213{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000214 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000215 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000216 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000217 interpreter_lock = PyThread_allocate_lock();
218 PyThread_acquire_lock(interpreter_lock, 1);
219 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000221
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000222void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000223PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000224{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000225 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000226}
227
228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232}
233
234void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000236{
237 if (tstate == NULL)
238 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000239 /* Check someone has called PyEval_InitThreads() to create the lock */
240 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000241 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000242 if (PyThreadState_Swap(tstate) != NULL)
243 Py_FatalError(
244 "PyEval_AcquireThread: non-NULL old thread state");
245}
246
247void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000248PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000249{
250 if (tstate == NULL)
251 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
252 if (PyThreadState_Swap(NULL) != tstate)
253 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000254 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000255}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000256
257/* This function is called from PyOS_AfterFork to ensure that newly
258 created child processes don't hold locks referring to threads which
259 are not running in the child process. (This could also be done using
260 pthread_atfork mechanism, at least for the pthreads implementation.) */
261
262void
263PyEval_ReInitThreads(void)
264{
265 if (!interpreter_lock)
266 return;
267 /*XXX Can't use PyThread_free_lock here because it does too
268 much error-checking. Doing this cleanly would require
269 adding a new function to each thread_*.h. Instead, just
270 create a new lock and waste a little bit of memory */
271 interpreter_lock = PyThread_allocate_lock();
272 PyThread_acquire_lock(interpreter_lock, 1);
273 main_thread = PyThread_get_thread_ident();
274}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000275#endif
276
Guido van Rossumff4949e1992-08-05 19:58:53 +0000277/* Functions save_thread and restore_thread are always defined so
278 dynamically loaded modules needn't be compiled separately for use
279 with and without threads: */
280
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000281PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000282PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000284 PyThreadState *tstate = PyThreadState_Swap(NULL);
285 if (tstate == NULL)
286 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000287#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000288 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000289 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000290#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000291 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000292}
293
294void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000295PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000297 if (tstate == NULL)
298 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000299#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000300 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000301 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000302 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304 }
305#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000306 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307}
308
309
Guido van Rossuma9672091994-09-14 13:31:22 +0000310/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
311 signal handlers or Mac I/O completion routines) can schedule calls
312 to a function to be called synchronously.
313 The synchronous function is called with one void* argument.
314 It should return 0 for success or -1 for failure -- failure should
315 be accompanied by an exception.
316
317 If registry succeeds, the registry function returns 0; if it fails
318 (e.g. due to too many pending calls) it returns -1 (without setting
319 an exception condition).
320
321 Note that because registry may occur from within signal handlers,
322 or other asynchronous events, calling malloc() is unsafe!
323
324#ifdef WITH_THREAD
325 Any thread can schedule pending calls, but only the main thread
326 will execute them.
327#endif
328
329 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
330 There are two possible race conditions:
331 (1) nested asynchronous registry calls;
332 (2) registry calls made while pending calls are being processed.
333 While (1) is very unlikely, (2) is a real possibility.
334 The current code is safe against (2), but not against (1).
335 The safety against (2) is derived from the fact that only one
336 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000337
Guido van Rossuma027efa1997-05-05 20:56:21 +0000338 XXX Darn! With the advent of thread state, we should have an array
339 of pending calls per thread in the thread state! Later...
340*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000341
Guido van Rossuma9672091994-09-14 13:31:22 +0000342#define NPENDINGCALLS 32
343static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000344 int (*func)(void *);
345 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000346} pendingcalls[NPENDINGCALLS];
347static volatile int pendingfirst = 0;
348static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000349static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000350
351int
Thomas Wouters334fb892000-07-25 12:56:38 +0000352Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000353{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000354 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000355 int i, j;
356 /* XXX Begin critical section */
357 /* XXX If you want this to be safe against nested
358 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000359 if (busy)
360 return -1;
361 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000362 i = pendinglast;
363 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000364 if (j == pendingfirst) {
365 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000366 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000367 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000368 pendingcalls[i].func = func;
369 pendingcalls[i].arg = arg;
370 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000371
372 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000373 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000374 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000375 /* XXX End critical section */
376 return 0;
377}
378
Guido van Rossum180d7b41994-09-29 09:45:57 +0000379int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000381{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000382 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000383#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000384 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000385 return 0;
386#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000387 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000388 return 0;
389 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000390 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000391 for (;;) {
392 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000393 int (*func)(void *);
394 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000395 i = pendingfirst;
396 if (i == pendinglast)
397 break; /* Queue empty */
398 func = pendingcalls[i].func;
399 arg = pendingcalls[i].arg;
400 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000401 if (func(arg) < 0) {
402 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000403 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000404 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000405 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000406 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000407 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000408 return 0;
409}
410
411
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000412/* The interpreter's recursion limit */
413
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000414static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000415int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000416
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000417int
418Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000419{
420 return recursion_limit;
421}
422
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000423void
424Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000425{
426 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000427 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000428}
429
Armin Rigo2b3eb402003-10-28 12:05:48 +0000430/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
431 if the recursion_depth reaches _Py_CheckRecursionLimit.
432 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
433 to guarantee that _Py_CheckRecursiveCall() is regularly called.
434 Without USE_STACKCHECK, there is no need for this. */
435int
436_Py_CheckRecursiveCall(char *where)
437{
438 PyThreadState *tstate = PyThreadState_GET();
439
440#ifdef USE_STACKCHECK
441 if (PyOS_CheckStack()) {
442 --tstate->recursion_depth;
443 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
444 return -1;
445 }
446#endif
447 if (tstate->recursion_depth > recursion_limit) {
448 --tstate->recursion_depth;
449 PyErr_Format(PyExc_RuntimeError,
450 "maximum recursion depth exceeded%s",
451 where);
452 return -1;
453 }
454 _Py_CheckRecursionLimit = recursion_limit;
455 return 0;
456}
457
Guido van Rossum374a9221991-04-04 10:40:29 +0000458/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000459enum why_code {
460 WHY_NOT = 0x0001, /* No error */
461 WHY_EXCEPTION = 0x0002, /* Exception occurred */
462 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
463 WHY_RETURN = 0x0008, /* 'return' statement */
464 WHY_BREAK = 0x0010, /* 'break' statement */
465 WHY_CONTINUE = 0x0020, /* 'continue' statement */
466 WHY_YIELD = 0x0040 /* 'yield' operator */
467};
Guido van Rossum374a9221991-04-04 10:40:29 +0000468
Raymond Hettinger7c958652004-04-06 10:11:10 +0000469static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000470static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000471
Skip Montanarod581d772002-09-03 20:10:45 +0000472/* for manipulating the thread switch and periodic "stuff" - used to be
473 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000474int _Py_CheckInterval = 100;
475volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000476
Guido van Rossumb209a111997-04-29 18:18:01 +0000477PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000478PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000479{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000480 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000481 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000482 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000483 (PyObject **)NULL, 0,
484 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000485 (PyObject **)NULL, 0,
486 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000487}
488
489
490/* Interpreter main loop */
491
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000492PyObject *
493PyEval_EvalFrame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000494{
Guido van Rossum950361c1997-01-24 13:49:28 +0000495#ifdef DXPAIRS
496 int lastopcode = 0;
497#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000498 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000499 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000500 register int opcode; /* Current opcode */
501 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000502 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000503 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000504 register PyObject *x; /* Result object -- NULL if error */
505 register PyObject *v; /* Temporary objects popped off stack */
506 register PyObject *w;
507 register PyObject *u;
508 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000509 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000510 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000511 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000512 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000513 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000514
Tim Peters8a5c3c72004-04-05 19:36:21 +0000515 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000516
517 not (instr_lb <= current_bytecode_offset < instr_ub)
518
Tim Peters8a5c3c72004-04-05 19:36:21 +0000519 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000520 initial values are such as to make this false the first
521 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000522 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000523
Guido van Rossumd076c731998-10-07 19:42:25 +0000524 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000525 PyObject *names;
526 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000527#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000528 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000529#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000530#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000531 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000532 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000533#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000534
Neal Norwitza81d2202002-07-14 00:27:26 +0000535/* Tuple access macros */
536
537#ifndef Py_DEBUG
538#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
539#else
540#define GETITEM(v, i) PyTuple_GetItem((v), (i))
541#endif
542
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000543#ifdef WITH_TSC
544/* Use Pentium timestamp counter to mark certain events:
545 inst0 -- beginning of switch statement for opcode dispatch
546 inst1 -- end of switch statement (may be skipped)
547 loop0 -- the top of the mainloop
548 loop1 -- place where control returns again to top of mainloop
549 (may be skipped)
550 intr1 -- beginning of long interruption
551 intr2 -- end of long interruption
552
553 Many opcodes call out to helper C functions. In some cases, the
554 time in those functions should be counted towards the time for the
555 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
556 calls another Python function; there's no point in charge all the
557 bytecode executed by the called function to the caller.
558
559 It's hard to make a useful judgement statically. In the presence
560 of operator overloading, it's impossible to tell if a call will
561 execute new Python code or not.
562
563 It's a case-by-case judgement. I'll use intr1 for the following
564 cases:
565
566 EXEC_STMT
567 IMPORT_STAR
568 IMPORT_FROM
569 CALL_FUNCTION (and friends)
570
571 */
572 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
573 int ticked = 0;
574
575 rdtscll(inst0);
576 rdtscll(inst1);
577 rdtscll(loop0);
578 rdtscll(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000579
580 /* shut up the compiler */
581 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000582#endif
583
Guido van Rossum374a9221991-04-04 10:40:29 +0000584/* Code access macros */
585
Guido van Rossumd076c731998-10-07 19:42:25 +0000586#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000587#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000588#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000589#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000590#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000591#define JUMPBY(x) (next_instr += (x))
592
Raymond Hettingerf606f872003-03-16 03:11:04 +0000593/* OpCode prediction macros
594 Some opcodes tend to come in pairs thus making it possible to predict
595 the second code when the first is run. For example, COMPARE_OP is often
596 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
597 followed by a POP_TOP.
598
599 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000600 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000601 processor has a high likelihood of making its own successful branch
602 prediction which results in a nearly zero overhead transition to the
603 next opcode.
604
605 A successful prediction saves a trip through the eval-loop including
606 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000607
Tim Peters8a5c3c72004-04-05 19:36:21 +0000608 If collecting opcode statistics, turn off prediction so that
609 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000610 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000611*/
612
Raymond Hettingera7216982004-02-08 19:59:27 +0000613#ifdef DYNAMIC_EXECUTION_PROFILE
614#define PREDICT(op) if (0) goto PRED_##op
615#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000616#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000617#endif
618
Raymond Hettingerf606f872003-03-16 03:11:04 +0000619#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000620#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000621
Guido van Rossum374a9221991-04-04 10:40:29 +0000622/* Stack manipulation macros */
623
624#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
625#define EMPTY() (STACK_LEVEL() == 0)
626#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000627#define SECOND() (stack_pointer[-2])
628#define THIRD() (stack_pointer[-3])
629#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000630#define SET_TOP(v) (stack_pointer[-1] = (v))
631#define SET_SECOND(v) (stack_pointer[-2] = (v))
632#define SET_THIRD(v) (stack_pointer[-3] = (v))
633#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000634#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000635#define BASIC_PUSH(v) (*stack_pointer++ = (v))
636#define BASIC_POP() (*--stack_pointer)
637
Guido van Rossum96a42c81992-01-12 02:29:51 +0000638#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000639#define PUSH(v) { (void)(BASIC_PUSH(v), \
640 lltrace && prtrace(TOP(), "push")); \
641 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000642#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000643#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
644 lltrace && prtrace(TOP(), "stackadj")); \
645 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000646#else
647#define PUSH(v) BASIC_PUSH(v)
648#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000649#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000650#endif
651
Guido van Rossum681d79a1995-07-18 14:51:37 +0000652/* Local variable macros */
653
654#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000655
656/* The SETLOCAL() macro must not DECREF the local variable in-place and
657 then store the new value; it must copy the old value to a temporary
658 value, then store the new value, and then DECREF the temporary value.
659 This is because it is possible that during the DECREF the frame is
660 accessed by other code (e.g. a __del__ method or gc.collect()) and the
661 variable would be pointing to already-freed memory. */
662#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
663 GETLOCAL(i) = value; \
664 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000665
Guido van Rossuma027efa1997-05-05 20:56:21 +0000666/* Start of code */
667
Tim Peters5ca576e2001-06-18 22:08:13 +0000668 if (f == NULL)
669 return NULL;
670
Armin Rigo1d313ab2003-10-25 14:33:09 +0000671 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000672 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000673 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000674
Tim Peters5ca576e2001-06-18 22:08:13 +0000675 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000676
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000677 if (tstate->use_tracing) {
678 if (tstate->c_tracefunc != NULL) {
679 /* tstate->c_tracefunc, if defined, is a
680 function that will be called on *every* entry
681 to a code block. Its return value, if not
682 None, is a function that will be called at
683 the start of each executed line of code.
684 (Actually, the function must return itself
685 in order to continue tracing.) The trace
686 functions are called with three arguments:
687 a pointer to the current frame, a string
688 indicating why the function is called, and
689 an argument which depends on the situation.
690 The global trace function is also called
691 whenever an exception is detected. */
692 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
693 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000694 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000695 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000696 }
697 }
698 if (tstate->c_profilefunc != NULL) {
699 /* Similar for c_profilefunc, except it needn't
700 return itself and isn't called for "line" events */
701 if (call_trace(tstate->c_profilefunc,
702 tstate->c_profileobj,
703 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000704 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000705 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000706 }
707 }
708 }
709
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000710 co = f->f_code;
711 names = co->co_names;
712 consts = co->co_consts;
713 fastlocals = f->f_localsplus;
714 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000715 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000716 /* An explanation is in order for the next line.
717
718 f->f_lasti now refers to the index of the last instruction
719 executed. You might think this was obvious from the name, but
720 this wasn't always true before 2.3! PyFrame_New now sets
721 f->f_lasti to -1 (i.e. the index *before* the first instruction)
722 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
723 does work. Promise. */
724 next_instr = first_instr + f->f_lasti + 1;
725 stack_pointer = f->f_stacktop;
726 assert(stack_pointer != NULL);
727 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
728
Tim Peters5ca576e2001-06-18 22:08:13 +0000729#ifdef LLTRACE
730 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
731#endif
732#if defined(Py_DEBUG) || defined(LLTRACE)
733 filename = PyString_AsString(co->co_filename);
734#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000735
Guido van Rossum374a9221991-04-04 10:40:29 +0000736 why = WHY_NOT;
737 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000738 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000739 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000740
Guido van Rossum374a9221991-04-04 10:40:29 +0000741 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000742#ifdef WITH_TSC
743 if (inst1 == 0) {
744 /* Almost surely, the opcode executed a break
745 or a continue, preventing inst1 from being set
746 on the way out of the loop.
747 */
748 rdtscll(inst1);
749 loop1 = inst1;
750 }
751 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
752 intr0, intr1);
753 ticked = 0;
754 inst1 = 0;
755 intr0 = 0;
756 intr1 = 0;
757 rdtscll(loop0);
758#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000759 assert(stack_pointer >= f->f_valuestack); /* else underflow */
760 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
761
Guido van Rossuma027efa1997-05-05 20:56:21 +0000762 /* Do periodic things. Doing this every time through
763 the loop would add too much overhead, so we do it
764 only every Nth instruction. We also do it if
765 ``things_to_do'' is set, i.e. when an asynchronous
766 event needs attention (e.g. a signal handler or
767 async I/O handler); see Py_AddPendingCall() and
768 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000769
Skip Montanarod581d772002-09-03 20:10:45 +0000770 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000771 if (*next_instr == SETUP_FINALLY) {
772 /* Make the last opcode before
773 a try: finally: block uninterruptable. */
774 goto fast_next_opcode;
775 }
Skip Montanarod581d772002-09-03 20:10:45 +0000776 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000777 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000778#ifdef WITH_TSC
779 ticked = 1;
780#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000781 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000782 if (Py_MakePendingCalls() < 0) {
783 why = WHY_EXCEPTION;
784 goto on_error;
785 }
786 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000787#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000788 if (interpreter_lock) {
789 /* Give another thread a chance */
790
Guido van Rossum25ce5661997-08-02 03:10:38 +0000791 if (PyThreadState_Swap(NULL) != tstate)
792 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000793 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000794
795 /* Other threads may run now */
796
Guido van Rossum65d5b571998-12-21 19:32:43 +0000797 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000798 if (PyThreadState_Swap(tstate) != NULL)
799 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000800
801 /* Check for thread interrupts */
802
803 if (tstate->async_exc != NULL) {
804 x = tstate->async_exc;
805 tstate->async_exc = NULL;
806 PyErr_SetNone(x);
807 Py_DECREF(x);
808 why = WHY_EXCEPTION;
809 goto on_error;
810 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000811 }
812#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000813 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000814
Neil Schemenauer63543862002-02-17 19:10:14 +0000815 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000816 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000817
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000818 /* line-by-line tracing support */
819
820 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
821 /* see maybe_call_line_trace
822 for expository comments */
823 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000824
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000825 err = maybe_call_line_trace(tstate->c_tracefunc,
826 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000827 f, &instr_lb, &instr_ub,
828 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000829 /* Reload possibly changed frame fields */
830 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000831 if (f->f_stacktop != NULL) {
832 stack_pointer = f->f_stacktop;
833 f->f_stacktop = NULL;
834 }
835 if (err) {
836 /* trace function raised an exception */
837 goto on_error;
838 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000839 }
840
841 /* Extract opcode and argument */
842
Guido van Rossum374a9221991-04-04 10:40:29 +0000843 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000844 oparg = 0; /* allows oparg to be stored in a register because
845 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000846 if (HAS_ARG(opcode))
847 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000848 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000849#ifdef DYNAMIC_EXECUTION_PROFILE
850#ifdef DXPAIRS
851 dxpairs[lastopcode][opcode]++;
852 lastopcode = opcode;
853#endif
854 dxp[opcode]++;
855#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000856
Guido van Rossum96a42c81992-01-12 02:29:51 +0000857#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000858 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000859
Guido van Rossum96a42c81992-01-12 02:29:51 +0000860 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000861 if (HAS_ARG(opcode)) {
862 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000863 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000864 }
865 else {
866 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000867 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000868 }
869 }
870#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000871
Guido van Rossum374a9221991-04-04 10:40:29 +0000872 /* Main switch on opcode */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000873 rdtscll(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000874
Guido van Rossum374a9221991-04-04 10:40:29 +0000875 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000876
Guido van Rossum374a9221991-04-04 10:40:29 +0000877 /* BEWARE!
878 It is essential that any operation that fails sets either
879 x to NULL, err to nonzero, or why to anything but WHY_NOT,
880 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000881
Guido van Rossum374a9221991-04-04 10:40:29 +0000882 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000883
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000884 case NOP:
885 goto fast_next_opcode;
886
Neil Schemenauer63543862002-02-17 19:10:14 +0000887 case LOAD_FAST:
888 x = GETLOCAL(oparg);
889 if (x != NULL) {
890 Py_INCREF(x);
891 PUSH(x);
892 goto fast_next_opcode;
893 }
894 format_exc_check_arg(PyExc_UnboundLocalError,
895 UNBOUNDLOCAL_ERROR_MSG,
896 PyTuple_GetItem(co->co_varnames, oparg));
897 break;
898
899 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000900 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000901 Py_INCREF(x);
902 PUSH(x);
903 goto fast_next_opcode;
904
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000905 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000906 case STORE_FAST:
907 v = POP();
908 SETLOCAL(oparg, v);
909 goto fast_next_opcode;
910
Raymond Hettingerf606f872003-03-16 03:11:04 +0000911 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000912 case POP_TOP:
913 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000914 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000915 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000916
Guido van Rossum374a9221991-04-04 10:40:29 +0000917 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000918 v = TOP();
919 w = SECOND();
920 SET_TOP(w);
921 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000922 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000923
Guido van Rossum374a9221991-04-04 10:40:29 +0000924 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000925 v = TOP();
926 w = SECOND();
927 x = THIRD();
928 SET_TOP(w);
929 SET_SECOND(x);
930 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000931 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000932
Thomas Wouters434d0822000-08-24 20:11:32 +0000933 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000934 u = TOP();
935 v = SECOND();
936 w = THIRD();
937 x = FOURTH();
938 SET_TOP(v);
939 SET_SECOND(w);
940 SET_THIRD(x);
941 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000942 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000943
Guido van Rossum374a9221991-04-04 10:40:29 +0000944 case DUP_TOP:
945 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000946 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000947 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000948 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000949
Thomas Wouters434d0822000-08-24 20:11:32 +0000950 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000951 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000952 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000953 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000954 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000955 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000956 STACKADJ(2);
957 SET_TOP(x);
958 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000959 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000960 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000961 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000962 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000963 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000964 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000965 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000966 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000967 STACKADJ(3);
968 SET_TOP(x);
969 SET_SECOND(w);
970 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000971 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000972 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000973 Py_FatalError("invalid argument to DUP_TOPX"
974 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000975 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000976
Guido van Rossum374a9221991-04-04 10:40:29 +0000977 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000978 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000979 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000980 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000982 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000983 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000984
Guido van Rossum374a9221991-04-04 10:40:29 +0000985 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000986 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000987 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000988 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000989 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000990 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000991 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000992
Guido van Rossum374a9221991-04-04 10:40:29 +0000993 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000995 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000996 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +0000997 if (err == 0) {
998 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000999 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001000 continue;
1001 }
1002 else if (err > 0) {
1003 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001004 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001005 err = 0;
1006 continue;
1007 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001008 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001009 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001010
Guido van Rossum374a9221991-04-04 10:40:29 +00001011 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001012 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001013 x = PyObject_Repr(v);
1014 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001016 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001017 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001018
Guido van Rossum7928cd71991-10-24 14:59:31 +00001019 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001021 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001022 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001023 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001024 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001025 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001026
Guido van Rossum50564e81996-01-12 01:13:16 +00001027 case BINARY_POWER:
1028 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001030 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001031 Py_DECREF(v);
1032 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001033 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001034 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001035 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001036
Guido van Rossum374a9221991-04-04 10:40:29 +00001037 case BINARY_MULTIPLY:
1038 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001040 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001041 Py_DECREF(v);
1042 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001043 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001044 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001045 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001046
Guido van Rossum374a9221991-04-04 10:40:29 +00001047 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001048 if (!_Py_QnewFlag) {
1049 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001051 x = PyNumber_Divide(v, w);
1052 Py_DECREF(v);
1053 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001054 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001055 if (x != NULL) continue;
1056 break;
1057 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001058 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001059 BINARY_TRUE_DIVIDE */
1060 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001061 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001062 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001063 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001064 Py_DECREF(v);
1065 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001066 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001067 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001068 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001069
Guido van Rossum4668b002001-08-08 05:00:18 +00001070 case BINARY_FLOOR_DIVIDE:
1071 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001072 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001073 x = PyNumber_FloorDivide(v, w);
1074 Py_DECREF(v);
1075 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001076 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001077 if (x != NULL) continue;
1078 break;
1079
Guido van Rossum374a9221991-04-04 10:40:29 +00001080 case BINARY_MODULO:
1081 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001083 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001084 Py_DECREF(v);
1085 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001086 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001087 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001088 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001089
Guido van Rossum374a9221991-04-04 10:40:29 +00001090 case BINARY_ADD:
1091 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001092 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001093 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001094 /* INLINE: int + int */
1095 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001096 a = PyInt_AS_LONG(v);
1097 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001098 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001099 if ((i^a) < 0 && (i^b) < 0)
1100 goto slow_add;
1101 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001102 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001103 else if (PyString_CheckExact(v) &&
1104 PyString_CheckExact(w)) {
1105 x = string_concatenate(v, w, f, next_instr);
1106 /* string_concatenate consumed the ref to v */
1107 goto skip_decref_vx;
1108 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001109 else {
1110 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001112 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001113 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001114 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001115 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001116 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001117 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001118 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001119
Guido van Rossum374a9221991-04-04 10:40:29 +00001120 case BINARY_SUBTRACT:
1121 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001122 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001123 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001124 /* INLINE: int - int */
1125 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001126 a = PyInt_AS_LONG(v);
1127 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001128 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001129 if ((i^a) < 0 && (i^~b) < 0)
1130 goto slow_sub;
1131 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001132 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001133 else {
1134 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001135 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001136 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001137 Py_DECREF(v);
1138 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001139 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001140 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001141 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001142
Guido van Rossum374a9221991-04-04 10:40:29 +00001143 case BINARY_SUBSCR:
1144 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001145 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001146 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001147 /* INLINE: list[int] */
1148 long i = PyInt_AsLong(w);
1149 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001150 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001151 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001152 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001153 Py_INCREF(x);
1154 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001155 else
1156 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001157 }
1158 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001159 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001160 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001161 Py_DECREF(v);
1162 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001163 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001164 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001165 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Guido van Rossum7928cd71991-10-24 14:59:31 +00001167 case BINARY_LSHIFT:
1168 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001169 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001170 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001171 Py_DECREF(v);
1172 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001173 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001174 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001175 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001176
Guido van Rossum7928cd71991-10-24 14:59:31 +00001177 case BINARY_RSHIFT:
1178 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001179 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001180 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001181 Py_DECREF(v);
1182 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001183 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001184 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001185 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001186
Guido van Rossum7928cd71991-10-24 14:59:31 +00001187 case BINARY_AND:
1188 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001190 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001191 Py_DECREF(v);
1192 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001193 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001194 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001195 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Guido van Rossum7928cd71991-10-24 14:59:31 +00001197 case BINARY_XOR:
1198 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001199 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001200 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001201 Py_DECREF(v);
1202 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001203 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001204 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001205 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Guido van Rossum7928cd71991-10-24 14:59:31 +00001207 case BINARY_OR:
1208 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001209 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001210 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001211 Py_DECREF(v);
1212 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001213 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001214 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001215 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001216
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001217 case LIST_APPEND:
1218 w = POP();
1219 v = POP();
1220 err = PyList_Append(v, w);
1221 Py_DECREF(v);
1222 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001223 if (err == 0) {
1224 PREDICT(JUMP_ABSOLUTE);
1225 continue;
1226 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001227 break;
1228
Thomas Wouters434d0822000-08-24 20:11:32 +00001229 case INPLACE_POWER:
1230 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001231 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001232 x = PyNumber_InPlacePower(v, w, Py_None);
1233 Py_DECREF(v);
1234 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001236 if (x != NULL) continue;
1237 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001238
Thomas Wouters434d0822000-08-24 20:11:32 +00001239 case INPLACE_MULTIPLY:
1240 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001241 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001242 x = PyNumber_InPlaceMultiply(v, w);
1243 Py_DECREF(v);
1244 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001245 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001246 if (x != NULL) continue;
1247 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001248
Thomas Wouters434d0822000-08-24 20:11:32 +00001249 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001250 if (!_Py_QnewFlag) {
1251 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001253 x = PyNumber_InPlaceDivide(v, w);
1254 Py_DECREF(v);
1255 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001256 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001257 if (x != NULL) continue;
1258 break;
1259 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001260 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001261 INPLACE_TRUE_DIVIDE */
1262 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001265 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 Py_DECREF(v);
1267 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001269 if (x != NULL) continue;
1270 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001271
Guido van Rossum4668b002001-08-08 05:00:18 +00001272 case INPLACE_FLOOR_DIVIDE:
1273 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001274 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001275 x = PyNumber_InPlaceFloorDivide(v, w);
1276 Py_DECREF(v);
1277 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001278 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001279 if (x != NULL) continue;
1280 break;
1281
Thomas Wouters434d0822000-08-24 20:11:32 +00001282 case INPLACE_MODULO:
1283 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001284 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001285 x = PyNumber_InPlaceRemainder(v, w);
1286 Py_DECREF(v);
1287 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 if (x != NULL) continue;
1290 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001291
Thomas Wouters434d0822000-08-24 20:11:32 +00001292 case INPLACE_ADD:
1293 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001294 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001295 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 /* INLINE: int + int */
1297 register long a, b, i;
1298 a = PyInt_AS_LONG(v);
1299 b = PyInt_AS_LONG(w);
1300 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001301 if ((i^a) < 0 && (i^b) < 0)
1302 goto slow_iadd;
1303 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001304 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001305 else if (PyString_CheckExact(v) &&
1306 PyString_CheckExact(w)) {
1307 x = string_concatenate(v, w, f, next_instr);
1308 /* string_concatenate consumed the ref to v */
1309 goto skip_decref_v;
1310 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001311 else {
1312 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001313 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001314 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001315 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001316 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 if (x != NULL) continue;
1320 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 case INPLACE_SUBTRACT:
1323 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001325 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 /* INLINE: int - int */
1327 register long a, b, i;
1328 a = PyInt_AS_LONG(v);
1329 b = PyInt_AS_LONG(w);
1330 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001331 if ((i^a) < 0 && (i^~b) < 0)
1332 goto slow_isub;
1333 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001335 else {
1336 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001338 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 Py_DECREF(v);
1340 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 if (x != NULL) continue;
1343 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 case INPLACE_LSHIFT:
1346 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 x = PyNumber_InPlaceLshift(v, w);
1349 Py_DECREF(v);
1350 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 if (x != NULL) continue;
1353 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 case INPLACE_RSHIFT:
1356 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001357 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 x = PyNumber_InPlaceRshift(v, w);
1359 Py_DECREF(v);
1360 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001362 if (x != NULL) continue;
1363 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001364
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 case INPLACE_AND:
1366 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001367 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 x = PyNumber_InPlaceAnd(v, w);
1369 Py_DECREF(v);
1370 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 if (x != NULL) continue;
1373 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001374
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 case INPLACE_XOR:
1376 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 x = PyNumber_InPlaceXor(v, w);
1379 Py_DECREF(v);
1380 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 if (x != NULL) continue;
1383 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001384
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 case INPLACE_OR:
1386 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001387 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 x = PyNumber_InPlaceOr(v, w);
1389 Py_DECREF(v);
1390 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001391 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 if (x != NULL) continue;
1393 break;
1394
Guido van Rossum374a9221991-04-04 10:40:29 +00001395 case SLICE+0:
1396 case SLICE+1:
1397 case SLICE+2:
1398 case SLICE+3:
1399 if ((opcode-SLICE) & 2)
1400 w = POP();
1401 else
1402 w = NULL;
1403 if ((opcode-SLICE) & 1)
1404 v = POP();
1405 else
1406 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001407 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001408 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001409 Py_DECREF(u);
1410 Py_XDECREF(v);
1411 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001412 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001413 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001414 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Guido van Rossum374a9221991-04-04 10:40:29 +00001416 case STORE_SLICE+0:
1417 case STORE_SLICE+1:
1418 case STORE_SLICE+2:
1419 case STORE_SLICE+3:
1420 if ((opcode-STORE_SLICE) & 2)
1421 w = POP();
1422 else
1423 w = NULL;
1424 if ((opcode-STORE_SLICE) & 1)
1425 v = POP();
1426 else
1427 v = NULL;
1428 u = POP();
1429 t = POP();
1430 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001431 Py_DECREF(t);
1432 Py_DECREF(u);
1433 Py_XDECREF(v);
1434 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001435 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001436 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Guido van Rossum374a9221991-04-04 10:40:29 +00001438 case DELETE_SLICE+0:
1439 case DELETE_SLICE+1:
1440 case DELETE_SLICE+2:
1441 case DELETE_SLICE+3:
1442 if ((opcode-DELETE_SLICE) & 2)
1443 w = POP();
1444 else
1445 w = NULL;
1446 if ((opcode-DELETE_SLICE) & 1)
1447 v = POP();
1448 else
1449 v = NULL;
1450 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001451 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001453 Py_DECREF(u);
1454 Py_XDECREF(v);
1455 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001456 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Guido van Rossum374a9221991-04-04 10:40:29 +00001459 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001460 w = TOP();
1461 v = SECOND();
1462 u = THIRD();
1463 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001464 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001465 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001466 Py_DECREF(u);
1467 Py_DECREF(v);
1468 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001469 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001470 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001471
Guido van Rossum374a9221991-04-04 10:40:29 +00001472 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001473 w = TOP();
1474 v = SECOND();
1475 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001476 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001477 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001478 Py_DECREF(v);
1479 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001480 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001481 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001482
Guido van Rossum374a9221991-04-04 10:40:29 +00001483 case PRINT_EXPR:
1484 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001485 w = PySys_GetObject("displayhook");
1486 if (w == NULL) {
1487 PyErr_SetString(PyExc_RuntimeError,
1488 "lost sys.displayhook");
1489 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001490 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001491 }
1492 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001493 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001494 if (x == NULL)
1495 err = -1;
1496 }
1497 if (err == 0) {
1498 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001499 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001500 if (w == NULL)
1501 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001502 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001503 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001504 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001506
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001507 case PRINT_ITEM_TO:
1508 w = stream = POP();
1509 /* fall through to PRINT_ITEM */
1510
Guido van Rossum374a9221991-04-04 10:40:29 +00001511 case PRINT_ITEM:
1512 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001513 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001514 w = PySys_GetObject("stdout");
1515 if (w == NULL) {
1516 PyErr_SetString(PyExc_RuntimeError,
1517 "lost sys.stdout");
1518 err = -1;
1519 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001520 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001521 /* PyFile_SoftSpace() can exececute arbitrary code
1522 if sys.stdout is an instance with a __getattr__.
1523 If __getattr__ raises an exception, w will
1524 be freed, so we need to prevent that temporarily. */
1525 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001526 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001527 err = PyFile_WriteString(" ", w);
1528 if (err == 0)
1529 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001530 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001531 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001532 if (PyString_Check(v)) {
1533 char *s = PyString_AS_STRING(v);
1534 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001535 if (len == 0 ||
1536 !isspace(Py_CHARMASK(s[len-1])) ||
1537 s[len-1] == ' ')
1538 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001539 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001540#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001541 else if (PyUnicode_Check(v)) {
1542 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1543 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001544 if (len == 0 ||
1545 !Py_UNICODE_ISSPACE(s[len-1]) ||
1546 s[len-1] == ' ')
1547 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001548 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001549#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001550 else
1551 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001552 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001553 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001554 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001555 Py_XDECREF(stream);
1556 stream = NULL;
1557 if (err == 0)
1558 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001559 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001561 case PRINT_NEWLINE_TO:
1562 w = stream = POP();
1563 /* fall through to PRINT_NEWLINE */
1564
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001566 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001567 w = PySys_GetObject("stdout");
1568 if (w == NULL)
1569 PyErr_SetString(PyExc_RuntimeError,
1570 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001571 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001572 if (w != NULL) {
1573 err = PyFile_WriteString("\n", w);
1574 if (err == 0)
1575 PyFile_SoftSpace(w, 0);
1576 }
1577 Py_XDECREF(stream);
1578 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001579 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Thomas Wouters434d0822000-08-24 20:11:32 +00001581
1582#ifdef CASE_TOO_BIG
1583 default: switch (opcode) {
1584#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001585 case RAISE_VARARGS:
1586 u = v = w = NULL;
1587 switch (oparg) {
1588 case 3:
1589 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001590 /* Fallthrough */
1591 case 2:
1592 v = POP(); /* value */
1593 /* Fallthrough */
1594 case 1:
1595 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001596 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001597 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001598 break;
1599 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001600 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001601 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001602 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001603 break;
1604 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001605 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Guido van Rossum374a9221991-04-04 10:40:29 +00001607 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001608 if ((x = f->f_locals) != NULL) {
1609 Py_INCREF(x);
1610 PUSH(x);
1611 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001612 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001613 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001614 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 case RETURN_VALUE:
1617 retval = POP();
1618 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001619 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001620
Tim Peters5ca576e2001-06-18 22:08:13 +00001621 case YIELD_VALUE:
1622 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001623 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001624 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001625 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001626
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001627 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001628 w = TOP();
1629 v = SECOND();
1630 u = THIRD();
1631 STACKADJ(-3);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001632 rdtscll(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001633 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001634 rdtscll(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001635 Py_DECREF(u);
1636 Py_DECREF(v);
1637 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001638 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001639
Guido van Rossum374a9221991-04-04 10:40:29 +00001640 case POP_BLOCK:
1641 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001642 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001643 while (STACK_LEVEL() > b->b_level) {
1644 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001645 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001646 }
1647 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001648 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001649
Guido van Rossum374a9221991-04-04 10:40:29 +00001650 case END_FINALLY:
1651 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001652 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001653 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001654 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001655 if (why == WHY_RETURN ||
1656 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001657 retval = POP();
1658 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001659 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001661 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001662 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001663 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001664 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001666 else if (v != Py_None) {
1667 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001668 "'finally' pops bad exception");
1669 why = WHY_EXCEPTION;
1670 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001671 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Guido van Rossum374a9221991-04-04 10:40:29 +00001674 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001675 u = TOP();
1676 v = SECOND();
1677 w = THIRD();
1678 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001679 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001680 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001681 Py_DECREF(u);
1682 Py_DECREF(v);
1683 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001687 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001688 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001689 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001690 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001691 err = PyDict_SetItem(x, w, v);
1692 else
1693 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001694 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001695 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001696 break;
1697 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001698 PyErr_Format(PyExc_SystemError,
1699 "no locals found when storing %s",
1700 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Guido van Rossum374a9221991-04-04 10:40:29 +00001703 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001704 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001705 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001706 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001707 format_exc_check_arg(PyExc_NameError,
1708 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001709 break;
1710 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001711 PyErr_Format(PyExc_SystemError,
1712 "no locals when deleting %s",
1713 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001715
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001716 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001717 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001719 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1720 PyObject **items = ((PyTupleObject *)v)->ob_item;
1721 while (oparg--) {
1722 w = items[oparg];
1723 Py_INCREF(w);
1724 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001725 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001726 Py_DECREF(v);
1727 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001728 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1729 PyObject **items = ((PyListObject *)v)->ob_item;
1730 while (oparg--) {
1731 w = items[oparg];
1732 Py_INCREF(w);
1733 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001734 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001735 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001736 stack_pointer + oparg))
1737 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001738 else {
1739 if (PyErr_ExceptionMatches(PyExc_TypeError))
1740 PyErr_SetString(PyExc_TypeError,
1741 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001742 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001743 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001748 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001749 v = TOP();
1750 u = SECOND();
1751 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001752 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1753 Py_DECREF(v);
1754 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001755 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001756 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001757
Guido van Rossum374a9221991-04-04 10:40:29 +00001758 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001759 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001760 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001761 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1762 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001763 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001765
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001766 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001767 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001768 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001769 err = PyDict_SetItem(f->f_globals, w, v);
1770 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001771 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001772 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001773
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001774 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001775 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001776 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001777 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001778 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001779 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001780
Guido van Rossum374a9221991-04-04 10:40:29 +00001781 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001782 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001783 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001784 PyErr_Format(PyExc_SystemError,
1785 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001786 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001787 break;
1788 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001789 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001790 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001791 Py_XINCREF(x);
1792 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001793 else {
1794 x = PyObject_GetItem(v, w);
1795 if (x == NULL && PyErr_Occurred()) {
1796 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1797 break;
1798 PyErr_Clear();
1799 }
1800 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001801 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001802 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001803 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001804 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001806 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001807 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001808 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001809 break;
1810 }
1811 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001812 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001813 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001814 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001815 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001816
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001818 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001819 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001820 /* Inline the PyDict_GetItem() calls.
1821 WARNING: this is an extreme speed hack.
1822 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001823 long hash = ((PyStringObject *)w)->ob_shash;
1824 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001825 PyDictObject *d;
1826 d = (PyDictObject *)(f->f_globals);
1827 x = d->ma_lookup(d, w, hash)->me_value;
1828 if (x != NULL) {
1829 Py_INCREF(x);
1830 PUSH(x);
1831 continue;
1832 }
1833 d = (PyDictObject *)(f->f_builtins);
1834 x = d->ma_lookup(d, w, hash)->me_value;
1835 if (x != NULL) {
1836 Py_INCREF(x);
1837 PUSH(x);
1838 continue;
1839 }
1840 goto load_global_error;
1841 }
1842 }
1843 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001844 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001845 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001846 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001847 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001848 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001849 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001850 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001851 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 break;
1853 }
1854 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001855 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001856 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001857 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001858
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001859 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001860 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001861 if (x != NULL) {
1862 SETLOCAL(oparg, NULL);
1863 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001864 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001865 format_exc_check_arg(
1866 PyExc_UnboundLocalError,
1867 UNBOUNDLOCAL_ERROR_MSG,
1868 PyTuple_GetItem(co->co_varnames, oparg)
1869 );
1870 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001871
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001872 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001873 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001874 Py_INCREF(x);
1875 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001876 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001877 break;
1878
1879 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001880 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001881 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001882 if (w != NULL) {
1883 PUSH(w);
1884 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001885 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001886 err = -1;
1887 /* Don't stomp existing exception */
1888 if (PyErr_Occurred())
1889 break;
1890 if (oparg < f->f_ncells) {
1891 v = PyTuple_GetItem(co->co_cellvars,
1892 oparg);
1893 format_exc_check_arg(
1894 PyExc_UnboundLocalError,
1895 UNBOUNDLOCAL_ERROR_MSG,
1896 v);
1897 } else {
1898 v = PyTuple_GetItem(
1899 co->co_freevars,
1900 oparg - f->f_ncells);
1901 format_exc_check_arg(
1902 PyExc_NameError,
1903 UNBOUNDFREE_ERROR_MSG,
1904 v);
1905 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001906 break;
1907
1908 case STORE_DEREF:
1909 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001910 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001911 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001912 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001913 continue;
1914
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001916 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001918 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001919 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001920 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001921 }
1922 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001923 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 }
1925 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001926
Guido van Rossum374a9221991-04-04 10:40:29 +00001927 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001928 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001930 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001931 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001932 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 }
1934 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001935 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001936 }
1937 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001938
Guido van Rossum374a9221991-04-04 10:40:29 +00001939 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001940 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001941 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001942 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001943 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001944
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001946 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001947 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001948 x = PyObject_GetAttr(v, w);
1949 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001950 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001951 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001952 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001953
Guido van Rossum374a9221991-04-04 10:40:29 +00001954 case COMPARE_OP:
1955 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001956 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001957 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001958 /* INLINE: cmp(int, int) */
1959 register long a, b;
1960 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001961 a = PyInt_AS_LONG(v);
1962 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001963 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001964 case PyCmp_LT: res = a < b; break;
1965 case PyCmp_LE: res = a <= b; break;
1966 case PyCmp_EQ: res = a == b; break;
1967 case PyCmp_NE: res = a != b; break;
1968 case PyCmp_GT: res = a > b; break;
1969 case PyCmp_GE: res = a >= b; break;
1970 case PyCmp_IS: res = v == w; break;
1971 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001972 default: goto slow_compare;
1973 }
1974 x = res ? Py_True : Py_False;
1975 Py_INCREF(x);
1976 }
1977 else {
1978 slow_compare:
1979 x = cmp_outcome(oparg, v, w);
1980 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001981 Py_DECREF(v);
1982 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001983 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001984 if (x == NULL) break;
1985 PREDICT(JUMP_IF_FALSE);
1986 PREDICT(JUMP_IF_TRUE);
1987 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001988
Guido van Rossum374a9221991-04-04 10:40:29 +00001989 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001990 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001991 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001992 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001993 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001994 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001995 break;
1996 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001997 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001998 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00001999 w,
2000 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002001 f->f_locals == NULL ?
2002 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002003 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002004 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002005 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002006 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002007 x = NULL;
2008 break;
2009 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002010 rdtscll(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002011 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002012 rdtscll(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002013 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002014 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002015 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002016 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002017
Thomas Wouters52152252000-08-17 22:55:00 +00002018 case IMPORT_STAR:
2019 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002020 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002021 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002022 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002023 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002024 break;
2025 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002026 rdtscll(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002027 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002028 rdtscll(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002029 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002030 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002031 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002032 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002033
Thomas Wouters52152252000-08-17 22:55:00 +00002034 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002035 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002036 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002037 rdtscll(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002038 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002039 rdtscll(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002040 PUSH(x);
2041 if (x != NULL) continue;
2042 break;
2043
Guido van Rossum374a9221991-04-04 10:40:29 +00002044 case JUMP_FORWARD:
2045 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002046 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002047
Raymond Hettingerf606f872003-03-16 03:11:04 +00002048 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002049 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002050 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002051 if (w == Py_True) {
2052 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002053 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002054 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002055 if (w == Py_False) {
2056 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002057 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002058 }
2059 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002060 if (err > 0)
2061 err = 0;
2062 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002063 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002064 else
2065 break;
2066 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002067
Raymond Hettingerf606f872003-03-16 03:11:04 +00002068 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002069 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002070 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002071 if (w == Py_False) {
2072 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002073 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002074 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002075 if (w == Py_True) {
2076 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002077 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002078 }
2079 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002080 if (err > 0) {
2081 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002082 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002083 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002084 else if (err == 0)
2085 ;
2086 else
2087 break;
2088 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002089
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002090 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002091 case JUMP_ABSOLUTE:
2092 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002093 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002094
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002095 case GET_ITER:
2096 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002097 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002098 x = PyObject_GetIter(v);
2099 Py_DECREF(v);
2100 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002101 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002102 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002103 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002104 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002105 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002106 break;
2107
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002108 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002109 case FOR_ITER:
2110 /* before: [iter]; after: [iter, iter()] *or* [] */
2111 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002112 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002113 if (x != NULL) {
2114 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002115 PREDICT(STORE_FAST);
2116 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002117 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002118 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002119 if (PyErr_Occurred()) {
2120 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2121 break;
2122 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002123 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002124 /* iterator ended normally */
2125 x = v = POP();
2126 Py_DECREF(v);
2127 JUMPBY(oparg);
2128 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002129
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002130 case BREAK_LOOP:
2131 why = WHY_BREAK;
2132 goto fast_block_end;
2133
2134 case CONTINUE_LOOP:
2135 retval = PyInt_FromLong(oparg);
2136 why = WHY_CONTINUE;
2137 goto fast_block_end;
2138
Guido van Rossum374a9221991-04-04 10:40:29 +00002139 case SETUP_LOOP:
2140 case SETUP_EXCEPT:
2141 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002142 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002143 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002144 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002145
Guido van Rossumf10570b1995-07-07 22:53:21 +00002146 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002147 {
2148 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002149 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002150 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002151#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002152 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002153#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002154 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002155#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002156 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002157 PUSH(x);
2158 if (x != NULL)
2159 continue;
2160 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002161 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002162
Jeremy Hylton76901512000-03-28 23:49:17 +00002163 case CALL_FUNCTION_VAR:
2164 case CALL_FUNCTION_KW:
2165 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002166 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002167 int na = oparg & 0xff;
2168 int nk = (oparg>>8) & 0xff;
2169 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002170 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002171 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002172 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002173 if (flags & CALL_FLAG_VAR)
2174 n++;
2175 if (flags & CALL_FLAG_KW)
2176 n++;
2177 pfunc = stack_pointer - n - 1;
2178 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002179
Guido van Rossumac7be682001-01-17 15:42:30 +00002180 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002181 && PyMethod_GET_SELF(func) != NULL) {
2182 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002183 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002184 func = PyMethod_GET_FUNCTION(func);
2185 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002186 Py_DECREF(*pfunc);
2187 *pfunc = self;
2188 na++;
2189 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002190 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002191 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002192 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002193 rdtscll(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002194 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002195 rdtscll(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002196 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002197 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002198
Jeremy Hylton76901512000-03-28 23:49:17 +00002199 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002200 w = POP();
2201 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002202 }
2203 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002204 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002205 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002206 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Guido van Rossum681d79a1995-07-18 14:51:37 +00002209 case MAKE_FUNCTION:
2210 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002211 x = PyFunction_New(v, f->f_globals);
2212 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002213 /* XXX Maybe this should be a separate opcode? */
2214 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002215 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002216 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002217 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002218 x = NULL;
2219 break;
2220 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002221 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002222 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002223 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002224 }
2225 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002226 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002227 }
2228 PUSH(x);
2229 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002230
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002231 case MAKE_CLOSURE:
2232 {
2233 int nfree;
2234 v = POP(); /* code object */
2235 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002236 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002237 Py_DECREF(v);
2238 /* XXX Maybe this should be a separate opcode? */
2239 if (x != NULL && nfree > 0) {
2240 v = PyTuple_New(nfree);
2241 if (v == NULL) {
2242 Py_DECREF(x);
2243 x = NULL;
2244 break;
2245 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002246 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002247 w = POP();
2248 PyTuple_SET_ITEM(v, nfree, w);
2249 }
2250 err = PyFunction_SetClosure(x, v);
2251 Py_DECREF(v);
2252 }
2253 if (x != NULL && oparg > 0) {
2254 v = PyTuple_New(oparg);
2255 if (v == NULL) {
2256 Py_DECREF(x);
2257 x = NULL;
2258 break;
2259 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002260 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002261 w = POP();
2262 PyTuple_SET_ITEM(v, oparg, w);
2263 }
2264 err = PyFunction_SetDefaults(x, v);
2265 Py_DECREF(v);
2266 }
2267 PUSH(x);
2268 break;
2269 }
2270
Guido van Rossum8861b741996-07-30 16:49:37 +00002271 case BUILD_SLICE:
2272 if (oparg == 3)
2273 w = POP();
2274 else
2275 w = NULL;
2276 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002277 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002278 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002279 Py_DECREF(u);
2280 Py_DECREF(v);
2281 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002282 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002283 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002284 break;
2285
Fred Drakeef8ace32000-08-24 00:32:09 +00002286 case EXTENDED_ARG:
2287 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002288 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002289 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002290
Guido van Rossum374a9221991-04-04 10:40:29 +00002291 default:
2292 fprintf(stderr,
2293 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002294 PyCode_Addr2Line(f->f_code, f->f_lasti),
2295 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002296 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002297 why = WHY_EXCEPTION;
2298 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002299
2300#ifdef CASE_TOO_BIG
2301 }
2302#endif
2303
Guido van Rossum374a9221991-04-04 10:40:29 +00002304 } /* switch */
2305
2306 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002307
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002308 rdtscll(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002309
Guido van Rossum374a9221991-04-04 10:40:29 +00002310 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002311
Guido van Rossum374a9221991-04-04 10:40:29 +00002312 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002313 if (err == 0 && x != NULL) {
2314#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002315 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002316 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002317 fprintf(stderr,
2318 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002319 else {
2320#endif
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002321 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002322 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002323#ifdef CHECKEXC
2324 }
2325#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002326 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002327 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002328 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002329 err = 0;
2330 }
2331
Guido van Rossum374a9221991-04-04 10:40:29 +00002332 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002334 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002335 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002336 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002337 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002338 why = WHY_EXCEPTION;
2339 }
2340 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002341#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002342 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002343 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002344 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002345 char buf[1024];
2346 sprintf(buf, "Stack unwind with exception "
2347 "set and why=%d", why);
2348 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002349 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002350 }
2351#endif
2352
2353 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002354
Guido van Rossum374a9221991-04-04 10:40:29 +00002355 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002356 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002357
Fred Drake8f51f542001-10-04 14:48:42 +00002358 if (tstate->c_tracefunc != NULL)
2359 call_exc_trace(tstate->c_tracefunc,
2360 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002361 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002362
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002364
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 if (why == WHY_RERAISE)
2366 why = WHY_EXCEPTION;
2367
2368 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002369
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002370fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002371 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002372 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002373
Tim Peters8a5c3c72004-04-05 19:36:21 +00002374 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002375 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2376 /* For a continue inside a try block,
2377 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002378 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2379 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002380 why = WHY_NOT;
2381 JUMPTO(PyInt_AS_LONG(retval));
2382 Py_DECREF(retval);
2383 break;
2384 }
2385
Guido van Rossum374a9221991-04-04 10:40:29 +00002386 while (STACK_LEVEL() > b->b_level) {
2387 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002388 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002389 }
2390 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2391 why = WHY_NOT;
2392 JUMPTO(b->b_handler);
2393 break;
2394 }
2395 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002396 (b->b_type == SETUP_EXCEPT &&
2397 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002398 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002399 PyObject *exc, *val, *tb;
2400 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002401 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002402 val = Py_None;
2403 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002404 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002405 /* Make the raw exception data
2406 available to the handler,
2407 so a program can emulate the
2408 Python main loop. Don't do
2409 this for 'finally'. */
2410 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002411 PyErr_NormalizeException(
2412 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002413 set_exc_info(tstate,
2414 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002415 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002416 if (tb == NULL) {
2417 Py_INCREF(Py_None);
2418 PUSH(Py_None);
2419 } else
2420 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002421 PUSH(val);
2422 PUSH(exc);
2423 }
2424 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002425 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002426 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002427 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002428 PUSH(v);
2429 }
2430 why = WHY_NOT;
2431 JUMPTO(b->b_handler);
2432 break;
2433 }
2434 } /* unwind stack */
2435
2436 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002437
Guido van Rossum374a9221991-04-04 10:40:29 +00002438 if (why != WHY_NOT)
2439 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002440 rdtscll(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002441
Guido van Rossum374a9221991-04-04 10:40:29 +00002442 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002443
Tim Peters8a5c3c72004-04-05 19:36:21 +00002444 assert(why != WHY_YIELD);
2445 /* Pop remaining stack entries. */
2446 while (!EMPTY()) {
2447 v = POP();
2448 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002449 }
2450
Tim Peters8a5c3c72004-04-05 19:36:21 +00002451 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002452 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002453
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002454fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002455 if (tstate->use_tracing) {
2456 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002457 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002458 if (call_trace(tstate->c_tracefunc,
2459 tstate->c_traceobj, f,
2460 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002461 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002462 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002463 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002464 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002465 }
Fred Drake8f51f542001-10-04 14:48:42 +00002466 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002467 if (why == WHY_EXCEPTION)
2468 call_trace_protected(tstate->c_profilefunc,
2469 tstate->c_profileobj, f,
2470 PyTrace_RETURN);
2471 else if (call_trace(tstate->c_profilefunc,
2472 tstate->c_profileobj, f,
2473 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002474 Py_XDECREF(retval);
2475 retval = NULL;
2476 why = WHY_EXCEPTION;
2477 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002478 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002479 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002480
Guido van Rossuma027efa1997-05-05 20:56:21 +00002481 reset_exc_info(tstate);
2482
Tim Peters5ca576e2001-06-18 22:08:13 +00002483 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002484 exit_eval_frame:
2485 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002486 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002487
Guido van Rossum96a42c81992-01-12 02:29:51 +00002488 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002489}
2490
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002491/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002492 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2493 the test in the if statement in Misc/gdbinit:ppystack */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002494
Tim Peters6d6c1a32001-08-02 04:15:00 +00002495PyObject *
2496PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002497 PyObject **args, int argcount, PyObject **kws, int kwcount,
2498 PyObject **defs, int defcount, PyObject *closure)
2499{
2500 register PyFrameObject *f;
2501 register PyObject *retval = NULL;
2502 register PyObject **fastlocals, **freevars;
2503 PyThreadState *tstate = PyThreadState_GET();
2504 PyObject *x, *u;
2505
2506 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002507 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002508 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002509 return NULL;
2510 }
2511
Jeremy Hylton985eba52003-02-05 23:13:00 +00002512 assert(globals != NULL);
2513 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002514 if (f == NULL)
2515 return NULL;
2516
2517 fastlocals = f->f_localsplus;
2518 freevars = f->f_localsplus + f->f_nlocals;
2519
2520 if (co->co_argcount > 0 ||
2521 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2522 int i;
2523 int n = argcount;
2524 PyObject *kwdict = NULL;
2525 if (co->co_flags & CO_VARKEYWORDS) {
2526 kwdict = PyDict_New();
2527 if (kwdict == NULL)
2528 goto fail;
2529 i = co->co_argcount;
2530 if (co->co_flags & CO_VARARGS)
2531 i++;
2532 SETLOCAL(i, kwdict);
2533 }
2534 if (argcount > co->co_argcount) {
2535 if (!(co->co_flags & CO_VARARGS)) {
2536 PyErr_Format(PyExc_TypeError,
2537 "%.200s() takes %s %d "
2538 "%sargument%s (%d given)",
2539 PyString_AsString(co->co_name),
2540 defcount ? "at most" : "exactly",
2541 co->co_argcount,
2542 kwcount ? "non-keyword " : "",
2543 co->co_argcount == 1 ? "" : "s",
2544 argcount);
2545 goto fail;
2546 }
2547 n = co->co_argcount;
2548 }
2549 for (i = 0; i < n; i++) {
2550 x = args[i];
2551 Py_INCREF(x);
2552 SETLOCAL(i, x);
2553 }
2554 if (co->co_flags & CO_VARARGS) {
2555 u = PyTuple_New(argcount - n);
2556 if (u == NULL)
2557 goto fail;
2558 SETLOCAL(co->co_argcount, u);
2559 for (i = n; i < argcount; i++) {
2560 x = args[i];
2561 Py_INCREF(x);
2562 PyTuple_SET_ITEM(u, i-n, x);
2563 }
2564 }
2565 for (i = 0; i < kwcount; i++) {
2566 PyObject *keyword = kws[2*i];
2567 PyObject *value = kws[2*i + 1];
2568 int j;
2569 if (keyword == NULL || !PyString_Check(keyword)) {
2570 PyErr_Format(PyExc_TypeError,
2571 "%.200s() keywords must be strings",
2572 PyString_AsString(co->co_name));
2573 goto fail;
2574 }
2575 /* XXX slow -- speed up using dictionary? */
2576 for (j = 0; j < co->co_argcount; j++) {
2577 PyObject *nm = PyTuple_GET_ITEM(
2578 co->co_varnames, j);
2579 int cmp = PyObject_RichCompareBool(
2580 keyword, nm, Py_EQ);
2581 if (cmp > 0)
2582 break;
2583 else if (cmp < 0)
2584 goto fail;
2585 }
2586 /* Check errors from Compare */
2587 if (PyErr_Occurred())
2588 goto fail;
2589 if (j >= co->co_argcount) {
2590 if (kwdict == NULL) {
2591 PyErr_Format(PyExc_TypeError,
2592 "%.200s() got an unexpected "
2593 "keyword argument '%.400s'",
2594 PyString_AsString(co->co_name),
2595 PyString_AsString(keyword));
2596 goto fail;
2597 }
2598 PyDict_SetItem(kwdict, keyword, value);
2599 }
2600 else {
2601 if (GETLOCAL(j) != NULL) {
2602 PyErr_Format(PyExc_TypeError,
2603 "%.200s() got multiple "
2604 "values for keyword "
2605 "argument '%.400s'",
2606 PyString_AsString(co->co_name),
2607 PyString_AsString(keyword));
2608 goto fail;
2609 }
2610 Py_INCREF(value);
2611 SETLOCAL(j, value);
2612 }
2613 }
2614 if (argcount < co->co_argcount) {
2615 int m = co->co_argcount - defcount;
2616 for (i = argcount; i < m; i++) {
2617 if (GETLOCAL(i) == NULL) {
2618 PyErr_Format(PyExc_TypeError,
2619 "%.200s() takes %s %d "
2620 "%sargument%s (%d given)",
2621 PyString_AsString(co->co_name),
2622 ((co->co_flags & CO_VARARGS) ||
2623 defcount) ? "at least"
2624 : "exactly",
2625 m, kwcount ? "non-keyword " : "",
2626 m == 1 ? "" : "s", i);
2627 goto fail;
2628 }
2629 }
2630 if (n > m)
2631 i = n - m;
2632 else
2633 i = 0;
2634 for (; i < defcount; i++) {
2635 if (GETLOCAL(m+i) == NULL) {
2636 PyObject *def = defs[i];
2637 Py_INCREF(def);
2638 SETLOCAL(m+i, def);
2639 }
2640 }
2641 }
2642 }
2643 else {
2644 if (argcount > 0 || kwcount > 0) {
2645 PyErr_Format(PyExc_TypeError,
2646 "%.200s() takes no arguments (%d given)",
2647 PyString_AsString(co->co_name),
2648 argcount + kwcount);
2649 goto fail;
2650 }
2651 }
2652 /* Allocate and initialize storage for cell vars, and copy free
2653 vars into frame. This isn't too efficient right now. */
2654 if (f->f_ncells) {
2655 int i = 0, j = 0, nargs, found;
2656 char *cellname, *argname;
2657 PyObject *c;
2658
2659 nargs = co->co_argcount;
2660 if (co->co_flags & CO_VARARGS)
2661 nargs++;
2662 if (co->co_flags & CO_VARKEYWORDS)
2663 nargs++;
2664
2665 /* Check for cells that shadow args */
2666 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2667 cellname = PyString_AS_STRING(
2668 PyTuple_GET_ITEM(co->co_cellvars, i));
2669 found = 0;
2670 while (j < nargs) {
2671 argname = PyString_AS_STRING(
2672 PyTuple_GET_ITEM(co->co_varnames, j));
2673 if (strcmp(cellname, argname) == 0) {
2674 c = PyCell_New(GETLOCAL(j));
2675 if (c == NULL)
2676 goto fail;
2677 GETLOCAL(f->f_nlocals + i) = c;
2678 found = 1;
2679 break;
2680 }
2681 j++;
2682 }
2683 if (found == 0) {
2684 c = PyCell_New(NULL);
2685 if (c == NULL)
2686 goto fail;
2687 SETLOCAL(f->f_nlocals + i, c);
2688 }
2689 }
2690 /* Initialize any that are left */
2691 while (i < f->f_ncells) {
2692 c = PyCell_New(NULL);
2693 if (c == NULL)
2694 goto fail;
2695 SETLOCAL(f->f_nlocals + i, c);
2696 i++;
2697 }
2698 }
2699 if (f->f_nfreevars) {
2700 int i;
2701 for (i = 0; i < f->f_nfreevars; ++i) {
2702 PyObject *o = PyTuple_GET_ITEM(closure, i);
2703 Py_INCREF(o);
2704 freevars[f->f_ncells + i] = o;
2705 }
2706 }
2707
Tim Peters5ca576e2001-06-18 22:08:13 +00002708 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002709 /* Don't need to keep the reference to f_back, it will be set
2710 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002711 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002712 f->f_back = NULL;
2713
Jeremy Hylton985eba52003-02-05 23:13:00 +00002714 PCALL(PCALL_GENERATOR);
2715
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002716 /* Create a new generator that owns the ready to run frame
2717 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002718 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002719 }
2720
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002721 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002722
2723 fail: /* Jump here from prelude on failure */
2724
Tim Petersb13680b2001-11-27 23:29:29 +00002725 /* decref'ing the frame can cause __del__ methods to get invoked,
2726 which can call back into Python. While we're done with the
2727 current Python frame (f), the associated C stack is still in use,
2728 so recursion_depth must be boosted for the duration.
2729 */
2730 assert(tstate != NULL);
2731 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002732 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002733 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002734 return retval;
2735}
2736
2737
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002738/* Implementation notes for set_exc_info() and reset_exc_info():
2739
2740- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2741 'exc_traceback'. These always travel together.
2742
2743- tstate->curexc_ZZZ is the "hot" exception that is set by
2744 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2745
2746- Once an exception is caught by an except clause, it is transferred
2747 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2748 can pick it up. This is the primary task of set_exc_info().
2749
2750- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2751
2752 Long ago, when none of this existed, there were just a few globals:
2753 one set corresponding to the "hot" exception, and one set
2754 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2755 globals; they were simply stored as sys.exc_ZZZ. For backwards
2756 compatibility, they still are!) The problem was that in code like
2757 this:
2758
2759 try:
2760 "something that may fail"
2761 except "some exception":
2762 "do something else first"
2763 "print the exception from sys.exc_ZZZ."
2764
2765 if "do something else first" invoked something that raised and caught
2766 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2767 cause of subtle bugs. I fixed this by changing the semantics as
2768 follows:
2769
2770 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2771 *in that frame*.
2772
2773 - But initially, and as long as no exception is caught in a given
2774 frame, sys.exc_ZZZ will hold the last exception caught in the
2775 previous frame (or the frame before that, etc.).
2776
2777 The first bullet fixed the bug in the above example. The second
2778 bullet was for backwards compatibility: it was (and is) common to
2779 have a function that is called when an exception is caught, and to
2780 have that function access the caught exception via sys.exc_ZZZ.
2781 (Example: traceback.print_exc()).
2782
2783 At the same time I fixed the problem that sys.exc_ZZZ weren't
2784 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2785 but that's really a separate improvement.
2786
2787 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2788 variables to what they were before the current frame was called. The
2789 set_exc_info() function saves them on the frame so that
2790 reset_exc_info() can restore them. The invariant is that
2791 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2792 exception (where "catching" an exception applies only to successful
2793 except clauses); and if the current frame ever caught an exception,
2794 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2795 at the start of the current frame.
2796
2797*/
2798
Guido van Rossuma027efa1997-05-05 20:56:21 +00002799static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002800set_exc_info(PyThreadState *tstate,
2801 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002802{
2803 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002804 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002805
Guido van Rossuma027efa1997-05-05 20:56:21 +00002806 frame = tstate->frame;
2807 if (frame->f_exc_type == NULL) {
2808 /* This frame didn't catch an exception before */
2809 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002810 if (tstate->exc_type == NULL) {
2811 Py_INCREF(Py_None);
2812 tstate->exc_type = Py_None;
2813 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002814 tmp_type = frame->f_exc_type;
2815 tmp_value = frame->f_exc_value;
2816 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002817 Py_XINCREF(tstate->exc_type);
2818 Py_XINCREF(tstate->exc_value);
2819 Py_XINCREF(tstate->exc_traceback);
2820 frame->f_exc_type = tstate->exc_type;
2821 frame->f_exc_value = tstate->exc_value;
2822 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002823 Py_XDECREF(tmp_type);
2824 Py_XDECREF(tmp_value);
2825 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002826 }
2827 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002828 tmp_type = tstate->exc_type;
2829 tmp_value = tstate->exc_value;
2830 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002831 Py_XINCREF(type);
2832 Py_XINCREF(value);
2833 Py_XINCREF(tb);
2834 tstate->exc_type = type;
2835 tstate->exc_value = value;
2836 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002837 Py_XDECREF(tmp_type);
2838 Py_XDECREF(tmp_value);
2839 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002840 /* For b/w compatibility */
2841 PySys_SetObject("exc_type", type);
2842 PySys_SetObject("exc_value", value);
2843 PySys_SetObject("exc_traceback", tb);
2844}
2845
2846static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002847reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002848{
2849 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002850 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002851 frame = tstate->frame;
2852 if (frame->f_exc_type != NULL) {
2853 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002854 tmp_type = tstate->exc_type;
2855 tmp_value = tstate->exc_value;
2856 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002857 Py_XINCREF(frame->f_exc_type);
2858 Py_XINCREF(frame->f_exc_value);
2859 Py_XINCREF(frame->f_exc_traceback);
2860 tstate->exc_type = frame->f_exc_type;
2861 tstate->exc_value = frame->f_exc_value;
2862 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002863 Py_XDECREF(tmp_type);
2864 Py_XDECREF(tmp_value);
2865 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002866 /* For b/w compatibility */
2867 PySys_SetObject("exc_type", frame->f_exc_type);
2868 PySys_SetObject("exc_value", frame->f_exc_value);
2869 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2870 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002871 tmp_type = frame->f_exc_type;
2872 tmp_value = frame->f_exc_value;
2873 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002874 frame->f_exc_type = NULL;
2875 frame->f_exc_value = NULL;
2876 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002877 Py_XDECREF(tmp_type);
2878 Py_XDECREF(tmp_value);
2879 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002880}
2881
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002882/* Logic for the raise statement (too complicated for inlining).
2883 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002884static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002885do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002886{
Guido van Rossumd295f121998-04-09 21:39:57 +00002887 if (type == NULL) {
2888 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002889 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002890 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2891 value = tstate->exc_value;
2892 tb = tstate->exc_traceback;
2893 Py_XINCREF(type);
2894 Py_XINCREF(value);
2895 Py_XINCREF(tb);
2896 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002897
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002898 /* We support the following forms of raise:
2899 raise <class>, <classinstance>
2900 raise <class>, <argument tuple>
2901 raise <class>, None
2902 raise <class>, <argument>
2903 raise <classinstance>, None
2904 raise <string>, <object>
2905 raise <string>, None
2906
2907 An omitted second argument is the same as None.
2908
2909 In addition, raise <tuple>, <anything> is the same as
2910 raising the tuple's first item (and it better have one!);
2911 this rule is applied recursively.
2912
2913 Finally, an optional third argument can be supplied, which
2914 gives the traceback to be substituted (useful when
2915 re-raising an exception after examining it). */
2916
2917 /* First, check the traceback argument, replacing None with
2918 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002919 if (tb == Py_None) {
2920 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002921 tb = NULL;
2922 }
2923 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002924 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002925 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002926 goto raise_error;
2927 }
2928
2929 /* Next, replace a missing value with None */
2930 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002931 value = Py_None;
2932 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002933 }
2934
2935 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002936 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2937 PyObject *tmp = type;
2938 type = PyTuple_GET_ITEM(type, 0);
2939 Py_INCREF(type);
2940 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002941 }
2942
Tim Petersafb2c802002-04-18 18:06:20 +00002943 if (PyString_CheckExact(type))
2944 /* Raising builtin string is deprecated but still allowed --
2945 * do nothing. Raising an instance of a new-style str
2946 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002947 PyErr_Warn(PyExc_PendingDeprecationWarning,
2948 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002949
2950 else if (PyClass_Check(type))
2951 PyErr_NormalizeException(&type, &value, &tb);
2952
Guido van Rossumb209a111997-04-29 18:18:01 +00002953 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002954 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002955 if (value != Py_None) {
2956 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002957 "instance exception may not have a separate value");
2958 goto raise_error;
2959 }
2960 else {
2961 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002962 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002963 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002964 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2965 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002966 }
2967 }
2968 else {
2969 /* Not something you can raise. You get an exception
2970 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002971 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002972 "exceptions must be classes, instances, or "
2973 "strings (deprecated), not %s",
2974 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002975 goto raise_error;
2976 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002977 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002978 if (tb == NULL)
2979 return WHY_EXCEPTION;
2980 else
2981 return WHY_RERAISE;
2982 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002983 Py_XDECREF(value);
2984 Py_XDECREF(type);
2985 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002986 return WHY_EXCEPTION;
2987}
2988
Tim Petersd6d010b2001-06-21 02:49:55 +00002989/* Iterate v argcnt times and store the results on the stack (via decreasing
2990 sp). Return 1 for success, 0 if error. */
2991
Barry Warsawe42b18f1997-08-25 22:13:04 +00002992static int
Tim Petersd6d010b2001-06-21 02:49:55 +00002993unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00002994{
Tim Petersd6d010b2001-06-21 02:49:55 +00002995 int i = 0;
2996 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002997 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00002998
Tim Petersd6d010b2001-06-21 02:49:55 +00002999 assert(v != NULL);
3000
3001 it = PyObject_GetIter(v);
3002 if (it == NULL)
3003 goto Error;
3004
3005 for (; i < argcnt; i++) {
3006 w = PyIter_Next(it);
3007 if (w == NULL) {
3008 /* Iterator done, via error or exhaustion. */
3009 if (!PyErr_Occurred()) {
3010 PyErr_Format(PyExc_ValueError,
3011 "need more than %d value%s to unpack",
3012 i, i == 1 ? "" : "s");
3013 }
3014 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003015 }
3016 *--sp = w;
3017 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003018
3019 /* We better have exhausted the iterator now. */
3020 w = PyIter_Next(it);
3021 if (w == NULL) {
3022 if (PyErr_Occurred())
3023 goto Error;
3024 Py_DECREF(it);
3025 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003026 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003027 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003028 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003029 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003030Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003031 for (; i > 0; i--, sp++)
3032 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003033 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003034 return 0;
3035}
3036
3037
Guido van Rossum96a42c81992-01-12 02:29:51 +00003038#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003039static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003040prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003041{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003042 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003043 if (PyObject_Print(v, stdout, 0) != 0)
3044 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003045 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003046 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003047}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003048#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003049
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003050static void
Fred Drake5755ce62001-06-27 19:19:46 +00003051call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003052{
Guido van Rossumb209a111997-04-29 18:18:01 +00003053 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003054 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003055 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003056 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003057 value = Py_None;
3058 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003059 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003060 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003061 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003062 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003063 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003064 }
Fred Drake5755ce62001-06-27 19:19:46 +00003065 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003066 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003067 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003068 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003069 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003070 Py_XDECREF(type);
3071 Py_XDECREF(value);
3072 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003073 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003074}
3075
Fred Drake4ec5d562001-10-04 19:26:43 +00003076static void
3077call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3078 int what)
3079{
3080 PyObject *type, *value, *traceback;
3081 int err;
3082 PyErr_Fetch(&type, &value, &traceback);
3083 err = call_trace(func, obj, frame, what, NULL);
3084 if (err == 0)
3085 PyErr_Restore(type, value, traceback);
3086 else {
3087 Py_XDECREF(type);
3088 Py_XDECREF(value);
3089 Py_XDECREF(traceback);
3090 }
3091}
3092
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003093static int
Fred Drake5755ce62001-06-27 19:19:46 +00003094call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3095 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003096{
Fred Drake5755ce62001-06-27 19:19:46 +00003097 register PyThreadState *tstate = frame->f_tstate;
3098 int result;
3099 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003100 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003101 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003102 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003103 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003104 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3105 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003106 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003107 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003108}
3109
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003110PyObject *
3111_PyEval_CallTracing(PyObject *func, PyObject *args)
3112{
3113 PyFrameObject *frame = PyEval_GetFrame();
3114 PyThreadState *tstate = frame->f_tstate;
3115 int save_tracing = tstate->tracing;
3116 int save_use_tracing = tstate->use_tracing;
3117 PyObject *result;
3118
3119 tstate->tracing = 0;
3120 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3121 || (tstate->c_profilefunc != NULL));
3122 result = PyObject_Call(func, args, NULL);
3123 tstate->tracing = save_tracing;
3124 tstate->use_tracing = save_use_tracing;
3125 return result;
3126}
3127
Michael W. Hudson006c7522002-11-08 13:08:46 +00003128static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003129maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003130 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3131 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003132{
3133 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003134
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003135 In a nutshell, we use the co_lnotab field of the code object
3136 to tell when execution has moved onto a different line.
3137
3138 As mentioned above, the basic idea is so set things up so
3139 that
3140
3141 *instr_lb <= frame->f_lasti < *instr_ub
3142
3143 is true so long as execution does not change lines.
3144
3145 This is all fairly simple. Digging the information out of
3146 co_lnotab takes some work, but is conceptually clear.
3147
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003148 Somewhat harder to explain is why we don't *always* call the
3149 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003150
3151 Consider this code:
3152
3153 1: def f(a):
3154 2: if a:
3155 3: print 1
3156 4: else:
3157 5: print 2
3158
3159 which compiles to this:
3160
3161 2 0 LOAD_FAST 0 (a)
3162 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003163 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003164
3165 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003166 10 PRINT_ITEM
3167 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003168 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003169 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003170
3171 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003172 19 PRINT_ITEM
3173 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003174 >> 21 LOAD_CONST 0 (None)
3175 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003176
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003177 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003178 15 and the co_lnotab will claim that execution has moved to
3179 line 3. This is at best misleading. In this case we could
3180 associate the POP_TOP with line 4, but that doesn't make
3181 sense in all cases (I think).
3182
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003183 What we do is only call the line trace function if the co_lnotab
3184 indicates we have jumped to the *start* of a line, i.e. if the
3185 current instruction offset matches the offset given for the
3186 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003187
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003188 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003189 Execution will jump from instruction offset 12 to offset 21.
3190 Then the co_lnotab would imply that execution has moved to line
3191 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003192
3193 Why do we set f_lineno when tracing? Well, consider the code
3194 above when 'a' is true. If stepping through this with 'n' in
3195 pdb, you would stop at line 1 with a "call" type event, then
3196 line events on lines 2 and 3, then a "return" type event -- but
3197 you would be shown line 5 during this event. This is a change
3198 from the behaviour in 2.2 and before, and I've found it
3199 confusing in practice. By setting and using f_lineno when
3200 tracing, one can report a line number different from that
3201 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003202 */
3203
Michael W. Hudson006c7522002-11-08 13:08:46 +00003204 int result = 0;
3205
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003206 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003207 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003208 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003209 unsigned char* p;
3210
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003211 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3212 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003213
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003214 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003215 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003216
3217 /* possible optimization: if f->f_lasti == instr_ub
3218 (likely to be a common case) then we already know
3219 instr_lb -- if we stored the matching value of p
3220 somwhere we could skip the first while loop. */
3221
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003222 /* see comments in compile.c for the description of
3223 co_lnotab. A point to remember: increments to p
3224 should come in pairs -- although we don't care about
3225 the line increments here, treating them as byte
3226 increments gets confusing, to say the least. */
3227
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003228 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003229 if (addr + *p > frame->f_lasti)
3230 break;
3231 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003232 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003233 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003234 --size;
3235 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003236
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003237 if (addr == frame->f_lasti) {
3238 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003239 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003240 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003241 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003242
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003243 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003244 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003245 addr += *p++;
3246 if (*p++)
3247 break;
3248 }
3249 *instr_ub = addr;
3250 }
3251 else {
3252 *instr_ub = INT_MAX;
3253 }
3254 }
Armin Rigobf57a142004-03-22 19:24:58 +00003255 else if (frame->f_lasti <= *instr_prev) {
3256 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003257 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003258 PyTrace_LINE, Py_None);
3259 }
3260 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003261 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003262}
3263
Fred Drake5755ce62001-06-27 19:19:46 +00003264void
3265PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003266{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003267 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003268 PyObject *temp = tstate->c_profileobj;
3269 Py_XINCREF(arg);
3270 tstate->c_profilefunc = NULL;
3271 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003272 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003273 Py_XDECREF(temp);
3274 tstate->c_profilefunc = func;
3275 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003276 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003277}
3278
3279void
3280PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3281{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003282 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003283 PyObject *temp = tstate->c_traceobj;
3284 Py_XINCREF(arg);
3285 tstate->c_tracefunc = NULL;
3286 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003287 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003288 Py_XDECREF(temp);
3289 tstate->c_tracefunc = func;
3290 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003291 tstate->use_tracing = ((func != NULL)
3292 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003293}
3294
Guido van Rossumb209a111997-04-29 18:18:01 +00003295PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003296PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003297{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003298 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003299 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003300 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003301 else
3302 return current_frame->f_builtins;
3303}
3304
Guido van Rossumb209a111997-04-29 18:18:01 +00003305PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003306PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003307{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003308 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003309 if (current_frame == NULL)
3310 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003311 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003312 return current_frame->f_locals;
3313}
3314
Guido van Rossumb209a111997-04-29 18:18:01 +00003315PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003316PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003317{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003318 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003319 if (current_frame == NULL)
3320 return NULL;
3321 else
3322 return current_frame->f_globals;
3323}
3324
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003325PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003326PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003327{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003328 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003329 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003330}
3331
Guido van Rossum6135a871995-01-09 17:53:26 +00003332int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003333PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003334{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003335 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003336 return current_frame == NULL ? 0 : current_frame->f_restricted;
3337}
3338
Guido van Rossumbe270261997-05-22 22:26:18 +00003339int
Tim Peters5ba58662001-07-16 02:29:45 +00003340PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003341{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003342 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003343 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003344
3345 if (current_frame != NULL) {
3346 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003347 const int compilerflags = codeflags & PyCF_MASK;
3348 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003349 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003350 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003351 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003352#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003353 if (codeflags & CO_GENERATOR_ALLOWED) {
3354 result = 1;
3355 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3356 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003357#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003358 }
3359 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003360}
3361
3362int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003363Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003364{
Guido van Rossumb209a111997-04-29 18:18:01 +00003365 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003366 if (f == NULL)
3367 return 0;
3368 if (!PyFile_SoftSpace(f, 0))
3369 return 0;
3370 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003371}
3372
Guido van Rossum3f5da241990-12-20 15:06:42 +00003373
Guido van Rossum681d79a1995-07-18 14:51:37 +00003374/* External interface to call any callable object.
3375 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003376
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003377#undef PyEval_CallObject
3378/* for backward compatibility: export this interface */
3379
Guido van Rossumb209a111997-04-29 18:18:01 +00003380PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003381PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003382{
Guido van Rossumb209a111997-04-29 18:18:01 +00003383 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003384}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003385#define PyEval_CallObject(func,arg) \
3386 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003387
Guido van Rossumb209a111997-04-29 18:18:01 +00003388PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003389PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003390{
Jeremy Hylton52820442001-01-03 23:52:36 +00003391 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003392
3393 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003394 arg = PyTuple_New(0);
3395 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003396 PyErr_SetString(PyExc_TypeError,
3397 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003398 return NULL;
3399 }
3400 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003401 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003402
Guido van Rossumb209a111997-04-29 18:18:01 +00003403 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003404 PyErr_SetString(PyExc_TypeError,
3405 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003406 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003407 return NULL;
3408 }
3409
Tim Peters6d6c1a32001-08-02 04:15:00 +00003410 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003411 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003412 return result;
3413}
3414
Tim Peters6d6c1a32001-08-02 04:15:00 +00003415char *
3416PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003417{
3418 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003419 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003420 else if (PyFunction_Check(func))
3421 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3422 else if (PyCFunction_Check(func))
3423 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3424 else if (PyClass_Check(func))
3425 return PyString_AsString(((PyClassObject*)func)->cl_name);
3426 else if (PyInstance_Check(func)) {
3427 return PyString_AsString(
3428 ((PyInstanceObject*)func)->in_class->cl_name);
3429 } else {
3430 return func->ob_type->tp_name;
3431 }
3432}
3433
Tim Peters6d6c1a32001-08-02 04:15:00 +00003434char *
3435PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003436{
3437 if (PyMethod_Check(func))
3438 return "()";
3439 else if (PyFunction_Check(func))
3440 return "()";
3441 else if (PyCFunction_Check(func))
3442 return "()";
3443 else if (PyClass_Check(func))
3444 return " constructor";
3445 else if (PyInstance_Check(func)) {
3446 return " instance";
3447 } else {
3448 return " object";
3449 }
3450}
3451
Jeremy Hylton52820442001-01-03 23:52:36 +00003452#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3453
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003454static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003455err_args(PyObject *func, int flags, int nargs)
3456{
3457 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003458 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003459 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003460 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003461 nargs);
3462 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003463 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003464 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003465 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003466 nargs);
3467}
3468
Nicholas Bastind858a772004-06-25 23:31:06 +00003469#define C_TRACE(call) \
3470if (tstate->use_tracing && tstate->c_profilefunc) { \
3471 if (call_trace(tstate->c_profilefunc, \
3472 tstate->c_profileobj, \
3473 tstate->frame, PyTrace_C_CALL, \
3474 func)) \
3475 { return NULL; } \
3476 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003477 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003478 if (x == NULL) { \
3479 if (call_trace (tstate->c_profilefunc, \
3480 tstate->c_profileobj, \
3481 tstate->frame, PyTrace_C_EXCEPTION, \
3482 func)) \
3483 { return NULL; } \
3484 } else { \
3485 if (call_trace(tstate->c_profilefunc, \
3486 tstate->c_profileobj, \
3487 tstate->frame, PyTrace_C_RETURN, \
3488 func)) \
3489 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003490 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003491 } \
3492} else { \
3493 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003494 }
3495
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003496static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003497call_function(PyObject ***pp_stack, int oparg
3498#ifdef WITH_TSC
3499 , uint64* pintr0, uint64* pintr1
3500#endif
3501 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003502{
3503 int na = oparg & 0xff;
3504 int nk = (oparg>>8) & 0xff;
3505 int n = na + 2 * nk;
3506 PyObject **pfunc = (*pp_stack) - n - 1;
3507 PyObject *func = *pfunc;
3508 PyObject *x, *w;
3509
Jeremy Hylton985eba52003-02-05 23:13:00 +00003510 /* Always dispatch PyCFunction first, because these are
3511 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003512 */
3513 if (PyCFunction_Check(func) && nk == 0) {
3514 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003515 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003516
3517 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003518 if (flags & (METH_NOARGS | METH_O)) {
3519 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3520 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003521 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003522 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003523 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003524 else if (flags & METH_O && na == 1) {
3525 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003526 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003527 Py_DECREF(arg);
3528 }
3529 else {
3530 err_args(func, flags, na);
3531 x = NULL;
3532 }
3533 }
3534 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003535 PyObject *callargs;
3536 callargs = load_args(pp_stack, na);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003537 rdtscll(*pintr0);
Nicholas Bastind858a772004-06-25 23:31:06 +00003538 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003539 rdtscll(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003540 Py_XDECREF(callargs);
3541 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003542 } else {
3543 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3544 /* optimize access to bound methods */
3545 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003546 PCALL(PCALL_METHOD);
3547 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003548 Py_INCREF(self);
3549 func = PyMethod_GET_FUNCTION(func);
3550 Py_INCREF(func);
3551 Py_DECREF(*pfunc);
3552 *pfunc = self;
3553 na++;
3554 n++;
3555 } else
3556 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003557 rdtscll(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003558 if (PyFunction_Check(func))
3559 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003560 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003561 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003562 rdtscll(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003563 Py_DECREF(func);
3564 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003565
Jeremy Hylton985eba52003-02-05 23:13:00 +00003566 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003567 while ((*pp_stack) > pfunc) {
3568 w = EXT_POP(*pp_stack);
3569 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003570 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003571 }
3572 return x;
3573}
3574
Jeremy Hylton192690e2002-08-16 18:36:11 +00003575/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003576 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003577 For the simplest case -- a function that takes only positional
3578 arguments and is called with only positional arguments -- it
3579 inlines the most primitive frame setup code from
3580 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3581 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003582*/
3583
3584static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003585fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003586{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003587 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003588 PyObject *globals = PyFunction_GET_GLOBALS(func);
3589 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3590 PyObject **d = NULL;
3591 int nd = 0;
3592
Jeremy Hylton985eba52003-02-05 23:13:00 +00003593 PCALL(PCALL_FUNCTION);
3594 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003595 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003596 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3597 PyFrameObject *f;
3598 PyObject *retval = NULL;
3599 PyThreadState *tstate = PyThreadState_GET();
3600 PyObject **fastlocals, **stack;
3601 int i;
3602
3603 PCALL(PCALL_FASTER_FUNCTION);
3604 assert(globals != NULL);
3605 /* XXX Perhaps we should create a specialized
3606 PyFrame_New() that doesn't take locals, but does
3607 take builtins without sanity checking them.
3608 */
3609 f = PyFrame_New(tstate, co, globals, NULL);
3610 if (f == NULL)
3611 return NULL;
3612
3613 fastlocals = f->f_localsplus;
3614 stack = (*pp_stack) - n;
3615
3616 for (i = 0; i < n; i++) {
3617 Py_INCREF(*stack);
3618 fastlocals[i] = *stack++;
3619 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003620 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003621 assert(tstate != NULL);
3622 ++tstate->recursion_depth;
3623 Py_DECREF(f);
3624 --tstate->recursion_depth;
3625 return retval;
3626 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003627 if (argdefs != NULL) {
3628 d = &PyTuple_GET_ITEM(argdefs, 0);
3629 nd = ((PyTupleObject *)argdefs)->ob_size;
3630 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003631 return PyEval_EvalCodeEx(co, globals,
3632 (PyObject *)NULL, (*pp_stack)-n, na,
3633 (*pp_stack)-2*nk, nk, d, nd,
3634 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003635}
3636
3637static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003638update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3639 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003640{
3641 PyObject *kwdict = NULL;
3642 if (orig_kwdict == NULL)
3643 kwdict = PyDict_New();
3644 else {
3645 kwdict = PyDict_Copy(orig_kwdict);
3646 Py_DECREF(orig_kwdict);
3647 }
3648 if (kwdict == NULL)
3649 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003650 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003651 int err;
3652 PyObject *value = EXT_POP(*pp_stack);
3653 PyObject *key = EXT_POP(*pp_stack);
3654 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003655 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003656 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003657 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003658 PyEval_GetFuncName(func),
3659 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003660 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003661 Py_DECREF(key);
3662 Py_DECREF(value);
3663 Py_DECREF(kwdict);
3664 return NULL;
3665 }
3666 err = PyDict_SetItem(kwdict, key, value);
3667 Py_DECREF(key);
3668 Py_DECREF(value);
3669 if (err) {
3670 Py_DECREF(kwdict);
3671 return NULL;
3672 }
3673 }
3674 return kwdict;
3675}
3676
3677static PyObject *
3678update_star_args(int nstack, int nstar, PyObject *stararg,
3679 PyObject ***pp_stack)
3680{
3681 PyObject *callargs, *w;
3682
3683 callargs = PyTuple_New(nstack + nstar);
3684 if (callargs == NULL) {
3685 return NULL;
3686 }
3687 if (nstar) {
3688 int i;
3689 for (i = 0; i < nstar; i++) {
3690 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3691 Py_INCREF(a);
3692 PyTuple_SET_ITEM(callargs, nstack + i, a);
3693 }
3694 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003695 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003696 w = EXT_POP(*pp_stack);
3697 PyTuple_SET_ITEM(callargs, nstack, w);
3698 }
3699 return callargs;
3700}
3701
3702static PyObject *
3703load_args(PyObject ***pp_stack, int na)
3704{
3705 PyObject *args = PyTuple_New(na);
3706 PyObject *w;
3707
3708 if (args == NULL)
3709 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003710 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003711 w = EXT_POP(*pp_stack);
3712 PyTuple_SET_ITEM(args, na, w);
3713 }
3714 return args;
3715}
3716
3717static PyObject *
3718do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3719{
3720 PyObject *callargs = NULL;
3721 PyObject *kwdict = NULL;
3722 PyObject *result = NULL;
3723
3724 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003725 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003726 if (kwdict == NULL)
3727 goto call_fail;
3728 }
3729 callargs = load_args(pp_stack, na);
3730 if (callargs == NULL)
3731 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003732#ifdef CALL_PROFILE
3733 /* At this point, we have to look at the type of func to
3734 update the call stats properly. Do it here so as to avoid
3735 exposing the call stats machinery outside ceval.c
3736 */
3737 if (PyFunction_Check(func))
3738 PCALL(PCALL_FUNCTION);
3739 else if (PyMethod_Check(func))
3740 PCALL(PCALL_METHOD);
3741 else if (PyType_Check(func))
3742 PCALL(PCALL_TYPE);
3743 else
3744 PCALL(PCALL_OTHER);
3745#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003746 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003747 call_fail:
3748 Py_XDECREF(callargs);
3749 Py_XDECREF(kwdict);
3750 return result;
3751}
3752
3753static PyObject *
3754ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3755{
3756 int nstar = 0;
3757 PyObject *callargs = NULL;
3758 PyObject *stararg = NULL;
3759 PyObject *kwdict = NULL;
3760 PyObject *result = NULL;
3761
3762 if (flags & CALL_FLAG_KW) {
3763 kwdict = EXT_POP(*pp_stack);
3764 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003765 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003766 "%s%s argument after ** "
3767 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003768 PyEval_GetFuncName(func),
3769 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003770 goto ext_call_fail;
3771 }
3772 }
3773 if (flags & CALL_FLAG_VAR) {
3774 stararg = EXT_POP(*pp_stack);
3775 if (!PyTuple_Check(stararg)) {
3776 PyObject *t = NULL;
3777 t = PySequence_Tuple(stararg);
3778 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003779 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3780 PyErr_Format(PyExc_TypeError,
3781 "%s%s argument after * "
3782 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003783 PyEval_GetFuncName(func),
3784 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003785 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003786 goto ext_call_fail;
3787 }
3788 Py_DECREF(stararg);
3789 stararg = t;
3790 }
3791 nstar = PyTuple_GET_SIZE(stararg);
3792 }
3793 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003794 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003795 if (kwdict == NULL)
3796 goto ext_call_fail;
3797 }
3798 callargs = update_star_args(na, nstar, stararg, pp_stack);
3799 if (callargs == NULL)
3800 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003801#ifdef CALL_PROFILE
3802 /* At this point, we have to look at the type of func to
3803 update the call stats properly. Do it here so as to avoid
3804 exposing the call stats machinery outside ceval.c
3805 */
3806 if (PyFunction_Check(func))
3807 PCALL(PCALL_FUNCTION);
3808 else if (PyMethod_Check(func))
3809 PCALL(PCALL_METHOD);
3810 else if (PyType_Check(func))
3811 PCALL(PCALL_TYPE);
3812 else
3813 PCALL(PCALL_OTHER);
3814#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003815 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003816 ext_call_fail:
3817 Py_XDECREF(callargs);
3818 Py_XDECREF(kwdict);
3819 Py_XDECREF(stararg);
3820 return result;
3821}
3822
Tim Peterscb479e72001-12-16 19:11:44 +00003823/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3824 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3825 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3826*/
Tim Petersb5196382001-12-16 19:44:20 +00003827/* Note: If v is NULL, return success without storing into *pi. This
3828 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3829 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003830*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003831int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003832_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003833{
Tim Petersb5196382001-12-16 19:44:20 +00003834 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003835 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003836 if (PyInt_Check(v)) {
3837 x = PyInt_AsLong(v);
3838 } else if (PyLong_Check(v)) {
3839 x = PyLong_AsLong(v);
3840 if (x==-1 && PyErr_Occurred()) {
3841 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003842 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003843
Guido van Rossumac7be682001-01-17 15:42:30 +00003844 if (!PyErr_ExceptionMatches(
3845 PyExc_OverflowError)) {
3846 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003847 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003848 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003849 }
3850
Guido van Rossumac7be682001-01-17 15:42:30 +00003851 /* Clear the OverflowError */
3852 PyErr_Clear();
3853
3854 /* It's an overflow error, so we need to
3855 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003856 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003857 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003858
3859 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003860 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003861 if (long_zero == NULL)
3862 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003863
3864 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003865 cmp = PyObject_RichCompareBool(v, long_zero,
3866 Py_GT);
3867 Py_DECREF(long_zero);
3868 if (cmp < 0)
3869 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003870 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003871 x = INT_MAX;
3872 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003873 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003874 }
3875 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003876 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003877 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003878 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003879 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003880 /* Truncate -- very long indices are truncated anyway */
3881 if (x > INT_MAX)
3882 x = INT_MAX;
3883 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003884 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003885 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003886 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003887 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003888}
3889
Guido van Rossum50d756e2001-08-18 17:43:36 +00003890#undef ISINT
3891#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3892
Guido van Rossumb209a111997-04-29 18:18:01 +00003893static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003894apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003895{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003896 PyTypeObject *tp = u->ob_type;
3897 PySequenceMethods *sq = tp->tp_as_sequence;
3898
3899 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3900 int ilow = 0, ihigh = INT_MAX;
3901 if (!_PyEval_SliceIndex(v, &ilow))
3902 return NULL;
3903 if (!_PyEval_SliceIndex(w, &ihigh))
3904 return NULL;
3905 return PySequence_GetSlice(u, ilow, ihigh);
3906 }
3907 else {
3908 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003909 if (slice != NULL) {
3910 PyObject *res = PyObject_GetItem(u, slice);
3911 Py_DECREF(slice);
3912 return res;
3913 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003914 else
3915 return NULL;
3916 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003917}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003918
3919static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003920assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3921 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003922{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003923 PyTypeObject *tp = u->ob_type;
3924 PySequenceMethods *sq = tp->tp_as_sequence;
3925
3926 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3927 int ilow = 0, ihigh = INT_MAX;
3928 if (!_PyEval_SliceIndex(v, &ilow))
3929 return -1;
3930 if (!_PyEval_SliceIndex(w, &ihigh))
3931 return -1;
3932 if (x == NULL)
3933 return PySequence_DelSlice(u, ilow, ihigh);
3934 else
3935 return PySequence_SetSlice(u, ilow, ihigh, x);
3936 }
3937 else {
3938 PyObject *slice = PySlice_New(v, w, NULL);
3939 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003940 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003941 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003942 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003943 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003944 res = PyObject_DelItem(u, slice);
3945 Py_DECREF(slice);
3946 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003947 }
3948 else
3949 return -1;
3950 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003951}
3952
Guido van Rossumb209a111997-04-29 18:18:01 +00003953static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003954cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003955{
Guido van Rossumac7be682001-01-17 15:42:30 +00003956 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003957 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003958 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003959 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003960 break;
3961 case PyCmp_IS_NOT:
3962 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003963 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003964 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003965 res = PySequence_Contains(w, v);
3966 if (res < 0)
3967 return NULL;
3968 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003969 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003970 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003971 if (res < 0)
3972 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003973 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003974 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003975 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003976 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003977 break;
3978 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003979 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003980 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003981 v = res ? Py_True : Py_False;
3982 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003983 return v;
3984}
3985
Thomas Wouters52152252000-08-17 22:55:00 +00003986static PyObject *
3987import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003988{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003989 PyObject *x;
3990
3991 x = PyObject_GetAttr(v, name);
3992 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003993 PyErr_Format(PyExc_ImportError,
3994 "cannot import name %.230s",
3995 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003996 }
Thomas Wouters52152252000-08-17 22:55:00 +00003997 return x;
3998}
Guido van Rossumac7be682001-01-17 15:42:30 +00003999
Thomas Wouters52152252000-08-17 22:55:00 +00004000static int
4001import_all_from(PyObject *locals, PyObject *v)
4002{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004003 PyObject *all = PyObject_GetAttrString(v, "__all__");
4004 PyObject *dict, *name, *value;
4005 int skip_leading_underscores = 0;
4006 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004007
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004008 if (all == NULL) {
4009 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4010 return -1; /* Unexpected error */
4011 PyErr_Clear();
4012 dict = PyObject_GetAttrString(v, "__dict__");
4013 if (dict == NULL) {
4014 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4015 return -1;
4016 PyErr_SetString(PyExc_ImportError,
4017 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004018 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004019 }
4020 all = PyMapping_Keys(dict);
4021 Py_DECREF(dict);
4022 if (all == NULL)
4023 return -1;
4024 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004025 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004026
4027 for (pos = 0, err = 0; ; pos++) {
4028 name = PySequence_GetItem(all, pos);
4029 if (name == NULL) {
4030 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4031 err = -1;
4032 else
4033 PyErr_Clear();
4034 break;
4035 }
4036 if (skip_leading_underscores &&
4037 PyString_Check(name) &&
4038 PyString_AS_STRING(name)[0] == '_')
4039 {
4040 Py_DECREF(name);
4041 continue;
4042 }
4043 value = PyObject_GetAttr(v, name);
4044 if (value == NULL)
4045 err = -1;
4046 else
4047 err = PyDict_SetItem(locals, name, value);
4048 Py_DECREF(name);
4049 Py_XDECREF(value);
4050 if (err != 0)
4051 break;
4052 }
4053 Py_DECREF(all);
4054 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004055}
4056
Guido van Rossumb209a111997-04-29 18:18:01 +00004057static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004058build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004059{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004060 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004061
4062 if (PyDict_Check(methods))
4063 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004064 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004065 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004066 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4067 base = PyTuple_GET_ITEM(bases, 0);
4068 metaclass = PyObject_GetAttrString(base, "__class__");
4069 if (metaclass == NULL) {
4070 PyErr_Clear();
4071 metaclass = (PyObject *)base->ob_type;
4072 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004073 }
4074 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004075 else {
4076 PyObject *g = PyEval_GetGlobals();
4077 if (g != NULL && PyDict_Check(g))
4078 metaclass = PyDict_GetItemString(g, "__metaclass__");
4079 if (metaclass == NULL)
4080 metaclass = (PyObject *) &PyClass_Type;
4081 Py_INCREF(metaclass);
4082 }
4083 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4084 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004085 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4086 /* A type error here likely means that the user passed
4087 in a base that was not a class (such the random module
4088 instead of the random.random type). Help them out with
4089 a more informative error message */
4090 PyErr_SetString(PyExc_TypeError,
4091 "Error when calling the metaclass.\n" \
4092 "Make sure the base arguments are valid.");
4093 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004094 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004095}
4096
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004097static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004098exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4099 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004100{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004101 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004102 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004103 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004104
Guido van Rossumb209a111997-04-29 18:18:01 +00004105 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4106 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004107 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004108 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004109 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004110 locals = PyTuple_GetItem(prog, 2);
4111 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004112 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004113 if (globals == Py_None) {
4114 globals = PyEval_GetGlobals();
4115 if (locals == Py_None) {
4116 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004117 plain = 1;
4118 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004119 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004120 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004121 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004122 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004123 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004124 !PyCode_Check(prog) &&
4125 !PyFile_Check(prog)) {
4126 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004127 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004128 return -1;
4129 }
Fred Drake661ea262000-10-24 19:57:45 +00004130 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004131 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004132 "exec: arg 2 must be a dictionary or None");
4133 return -1;
4134 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004135 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004136 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004137 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004138 return -1;
4139 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004140 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004141 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004142 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004143 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4144 PyErr_SetString(PyExc_TypeError,
4145 "code object passed to exec may not contain free variables");
4146 return -1;
4147 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004148 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004149 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004150 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004151 FILE *fp = PyFile_AsFile(prog);
4152 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004153 PyCompilerFlags cf;
4154 cf.cf_flags = 0;
4155 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004156 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004157 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004158 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004159 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004160 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004161 }
4162 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004163 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004164 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004165 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004166 cf.cf_flags = 0;
4167#ifdef Py_USING_UNICODE
4168 if (PyUnicode_Check(prog)) {
4169 tmp = PyUnicode_AsUTF8String(prog);
4170 if (tmp == NULL)
4171 return -1;
4172 prog = tmp;
4173 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4174 }
4175#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004176 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004177 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004178 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004179 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004180 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004181 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004182 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004183 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004184 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004185 if (plain)
4186 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004187 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004188 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004189 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004190 return 0;
4191}
Guido van Rossum24c13741995-02-14 09:42:43 +00004192
Guido van Rossumac7be682001-01-17 15:42:30 +00004193static void
Paul Prescode68140d2000-08-30 20:25:01 +00004194format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4195{
4196 char *obj_str;
4197
4198 if (!obj)
4199 return;
4200
4201 obj_str = PyString_AsString(obj);
4202 if (!obj_str)
4203 return;
4204
4205 PyErr_Format(exc, format_str, obj_str);
4206}
Guido van Rossum950361c1997-01-24 13:49:28 +00004207
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004208static PyObject *
4209string_concatenate(PyObject *v, PyObject *w,
4210 PyFrameObject *f, unsigned char *next_instr)
4211{
4212 /* This function implements 'variable += expr' when both arguments
4213 are strings. */
4214
4215 if (v->ob_refcnt == 2) {
4216 /* In the common case, there are 2 references to the value
4217 * stored in 'variable' when the += is performed: one on the
4218 * value stack (in 'v') and one still stored in the 'variable'.
4219 * We try to delete the variable now to reduce the refcnt to 1.
4220 */
4221 switch (*next_instr) {
4222 case STORE_FAST:
4223 {
4224 int oparg = PEEKARG();
4225 PyObject **fastlocals = f->f_localsplus;
4226 if (GETLOCAL(oparg) == v)
4227 SETLOCAL(oparg, NULL);
4228 break;
4229 }
4230 case STORE_DEREF:
4231 {
4232 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4233 PyObject *c = freevars[PEEKARG()];
4234 if (PyCell_GET(c) == v)
4235 PyCell_Set(c, NULL);
4236 break;
4237 }
4238 case STORE_NAME:
4239 {
4240 PyObject *names = f->f_code->co_names;
4241 PyObject *name = GETITEM(names, PEEKARG());
4242 PyObject *locals = f->f_locals;
4243 if (PyDict_CheckExact(locals) &&
4244 PyDict_GetItem(locals, name) == v) {
4245 if (PyDict_DelItem(locals, name) != 0) {
4246 PyErr_Clear();
4247 }
4248 }
4249 break;
4250 }
4251 }
4252 }
4253
Armin Rigo618fbf52004-08-07 20:58:32 +00004254 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004255 /* Now we own the last reference to 'v', so we can resize it
4256 * in-place.
4257 */
4258 int v_len = PyString_GET_SIZE(v);
4259 int w_len = PyString_GET_SIZE(w);
4260 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4261 /* XXX if _PyString_Resize() fails, 'v' has been
4262 * deallocated so it cannot be put back into 'variable'.
4263 * The MemoryError is raised when there is no value in
4264 * 'variable', which might (very remotely) be a cause
4265 * of incompatibilities.
4266 */
4267 return NULL;
4268 }
4269 /* copy 'w' into the newly allocated area of 'v' */
4270 memcpy(PyString_AS_STRING(v) + v_len,
4271 PyString_AS_STRING(w), w_len);
4272 return v;
4273 }
4274 else {
4275 /* When in-place resizing is not an option. */
4276 PyString_Concat(&v, w);
4277 return v;
4278 }
4279}
4280
Guido van Rossum950361c1997-01-24 13:49:28 +00004281#ifdef DYNAMIC_EXECUTION_PROFILE
4282
Skip Montanarof118cb12001-10-15 20:51:38 +00004283static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004284getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004285{
4286 int i;
4287 PyObject *l = PyList_New(256);
4288 if (l == NULL) return NULL;
4289 for (i = 0; i < 256; i++) {
4290 PyObject *x = PyInt_FromLong(a[i]);
4291 if (x == NULL) {
4292 Py_DECREF(l);
4293 return NULL;
4294 }
4295 PyList_SetItem(l, i, x);
4296 }
4297 for (i = 0; i < 256; i++)
4298 a[i] = 0;
4299 return l;
4300}
4301
4302PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004303_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004304{
4305#ifndef DXPAIRS
4306 return getarray(dxp);
4307#else
4308 int i;
4309 PyObject *l = PyList_New(257);
4310 if (l == NULL) return NULL;
4311 for (i = 0; i < 257; i++) {
4312 PyObject *x = getarray(dxpairs[i]);
4313 if (x == NULL) {
4314 Py_DECREF(l);
4315 return NULL;
4316 }
4317 PyList_SetItem(l, i, x);
4318 }
4319 return l;
4320#endif
4321}
4322
4323#endif