blob: ba452f9a16819351bfb6fd0ff55f6f156591d67b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Guido van Rossumc6004111993-11-05 10:22:19 +000017#include <ctype.h>
18
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000019#ifdef WITH_TSC
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000020
21typedef unsigned long long uint64;
22
Michael W. Hudson800ba232004-08-12 18:19:17 +000023#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
24 section should work for GCC on any PowerPC platform,
25 irrespective of OS. POWER? Who knows :-) */
26
27#define rdtscll(var) ppc_getcounter(&var)
28
29static void
30ppc_getcounter(uint64 *v)
31{
32 register unsigned long tbu, tb, tbu2;
33
34 loop:
35 asm volatile ("mftbu %0" : "=r" (tbu) );
36 asm volatile ("mftb %0" : "=r" (tb) );
37 asm volatile ("mftbu %0" : "=r" (tbu2));
38 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
39
40 /* The slightly peculiar way of writing the next lines is
41 compiled better by GCC than any other way I tried. */
42 ((long*)(v))[0] = tbu;
43 ((long*)(v))[1] = tb;
44}
45
46#else /* this section is for linux/x86 */
47
48#include <asm/msr.h>
49
50#endif
51
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000052void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
53 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
54{
55 uint64 intr, inst, loop;
56 PyThreadState *tstate = PyThreadState_Get();
57 if (!tstate->interp->tscdump)
58 return;
59 intr = intr1 - intr0;
60 inst = inst1 - inst0 - intr;
61 loop = loop1 - loop0 - intr;
62 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
63 opcode, ticked, inst, loop);
64}
Michael W. Hudson800ba232004-08-12 18:19:17 +000065
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000066#endif
67
Guido van Rossum04691fc1992-08-12 15:35:34 +000068/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000069/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000070
Guido van Rossum408027e1996-12-30 16:17:54 +000071#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000072/* For debugging the interpreter: */
73#define LLTRACE 1 /* Low-level trace feature */
74#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000075#endif
76
Jeremy Hylton52820442001-01-03 23:52:36 +000077typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000078
Guido van Rossum374a9221991-04-04 10:40:29 +000079/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000080#ifdef WITH_TSC
81static PyObject *call_function(PyObject ***, int, uint64*, uint64*);
82#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +000083static PyObject *call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000084#endif
Jeremy Hylton52820442001-01-03 23:52:36 +000085static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000086static PyObject *do_call(PyObject *, PyObject ***, int, int);
87static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000088static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000089static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000090static PyObject *load_args(PyObject ***, int);
91#define CALL_FLAG_VAR 1
92#define CALL_FLAG_KW 2
93
Guido van Rossum0a066c01992-03-27 17:29:15 +000094#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +000095static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000096#endif
Fred Drake5755ce62001-06-27 19:19:46 +000097static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
98 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +000099static void call_trace_protected(Py_tracefunc, PyObject *,
100 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +0000101static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000102static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000103 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000104
Tim Petersdbd9ba62000-07-09 03:09:57 +0000105static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
106static int assign_slice(PyObject *, PyObject *,
107 PyObject *, PyObject *);
108static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000109static PyObject *import_from(PyObject *, PyObject *);
110static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000111static PyObject *build_class(PyObject *, PyObject *, PyObject *);
112static int exec_statement(PyFrameObject *,
113 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000114static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
115static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000116static void format_exc_check_arg(PyObject *, char *, PyObject *);
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000117static PyObject *string_concatenate(PyObject *, PyObject *,
118 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000119
Paul Prescode68140d2000-08-30 20:25:01 +0000120#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000121 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000122#define GLOBAL_NAME_ERROR_MSG \
123 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000124#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000125 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000126#define UNBOUNDFREE_ERROR_MSG \
127 "free variable '%.200s' referenced before assignment" \
128 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000129
Guido van Rossum950361c1997-01-24 13:49:28 +0000130/* Dynamic execution profile */
131#ifdef DYNAMIC_EXECUTION_PROFILE
132#ifdef DXPAIRS
133static long dxpairs[257][256];
134#define dxp dxpairs[256]
135#else
136static long dxp[256];
137#endif
138#endif
139
Jeremy Hylton985eba52003-02-05 23:13:00 +0000140/* Function call profile */
141#ifdef CALL_PROFILE
142#define PCALL_NUM 11
143static int pcall[PCALL_NUM];
144
145#define PCALL_ALL 0
146#define PCALL_FUNCTION 1
147#define PCALL_FAST_FUNCTION 2
148#define PCALL_FASTER_FUNCTION 3
149#define PCALL_METHOD 4
150#define PCALL_BOUND_METHOD 5
151#define PCALL_CFUNCTION 6
152#define PCALL_TYPE 7
153#define PCALL_GENERATOR 8
154#define PCALL_OTHER 9
155#define PCALL_POP 10
156
157/* Notes about the statistics
158
159 PCALL_FAST stats
160
161 FAST_FUNCTION means no argument tuple needs to be created.
162 FASTER_FUNCTION means that the fast-path frame setup code is used.
163
164 If there is a method call where the call can be optimized by changing
165 the argument tuple and calling the function directly, it gets recorded
166 twice.
167
168 As a result, the relationship among the statistics appears to be
169 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
170 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
171 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
172 PCALL_METHOD > PCALL_BOUND_METHOD
173*/
174
175#define PCALL(POS) pcall[POS]++
176
177PyObject *
178PyEval_GetCallStats(PyObject *self)
179{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000180 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000181 pcall[0], pcall[1], pcall[2], pcall[3],
182 pcall[4], pcall[5], pcall[6], pcall[7],
183 pcall[8], pcall[9]);
184}
185#else
186#define PCALL(O)
187
188PyObject *
189PyEval_GetCallStats(PyObject *self)
190{
191 Py_INCREF(Py_None);
192 return Py_None;
193}
194#endif
195
Tim Peters5ca576e2001-06-18 22:08:13 +0000196
Guido van Rossume59214e1994-08-30 08:01:59 +0000197#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000198
Guido van Rossum2571cc81999-04-07 16:07:23 +0000199#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000200#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000201#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000202#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000203
Guido van Rossuma027efa1997-05-05 20:56:21 +0000204extern int _PyThread_Started; /* Flag for Py_Exit */
205
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000206static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000207static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000208
209void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000210PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000211{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000212 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000213 return;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000214 _PyThread_Started = 1;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000215 interpreter_lock = PyThread_allocate_lock();
216 PyThread_acquire_lock(interpreter_lock, 1);
217 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000219
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000220void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000222{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000223 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000224}
225
226void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000227PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000229 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230}
231
232void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000233PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000234{
235 if (tstate == NULL)
236 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000237 /* Check someone has called PyEval_InitThreads() to create the lock */
238 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000239 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000240 if (PyThreadState_Swap(tstate) != NULL)
241 Py_FatalError(
242 "PyEval_AcquireThread: non-NULL old thread state");
243}
244
245void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000246PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000247{
248 if (tstate == NULL)
249 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
250 if (PyThreadState_Swap(NULL) != tstate)
251 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000252 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000253}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000254
255/* This function is called from PyOS_AfterFork to ensure that newly
256 created child processes don't hold locks referring to threads which
257 are not running in the child process. (This could also be done using
258 pthread_atfork mechanism, at least for the pthreads implementation.) */
259
260void
261PyEval_ReInitThreads(void)
262{
263 if (!interpreter_lock)
264 return;
265 /*XXX Can't use PyThread_free_lock here because it does too
266 much error-checking. Doing this cleanly would require
267 adding a new function to each thread_*.h. Instead, just
268 create a new lock and waste a little bit of memory */
269 interpreter_lock = PyThread_allocate_lock();
270 PyThread_acquire_lock(interpreter_lock, 1);
271 main_thread = PyThread_get_thread_ident();
272}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000273#endif
274
Guido van Rossumff4949e1992-08-05 19:58:53 +0000275/* Functions save_thread and restore_thread are always defined so
276 dynamically loaded modules needn't be compiled separately for use
277 with and without threads: */
278
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000279PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000280PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000282 PyThreadState *tstate = PyThreadState_Swap(NULL);
283 if (tstate == NULL)
284 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000285#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000286 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000287 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000288#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000289 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000290}
291
292void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000293PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000295 if (tstate == NULL)
296 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000297#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000299 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000300 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302 }
303#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000304 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305}
306
307
Guido van Rossuma9672091994-09-14 13:31:22 +0000308/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
309 signal handlers or Mac I/O completion routines) can schedule calls
310 to a function to be called synchronously.
311 The synchronous function is called with one void* argument.
312 It should return 0 for success or -1 for failure -- failure should
313 be accompanied by an exception.
314
315 If registry succeeds, the registry function returns 0; if it fails
316 (e.g. due to too many pending calls) it returns -1 (without setting
317 an exception condition).
318
319 Note that because registry may occur from within signal handlers,
320 or other asynchronous events, calling malloc() is unsafe!
321
322#ifdef WITH_THREAD
323 Any thread can schedule pending calls, but only the main thread
324 will execute them.
325#endif
326
327 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
328 There are two possible race conditions:
329 (1) nested asynchronous registry calls;
330 (2) registry calls made while pending calls are being processed.
331 While (1) is very unlikely, (2) is a real possibility.
332 The current code is safe against (2), but not against (1).
333 The safety against (2) is derived from the fact that only one
334 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000335
Guido van Rossuma027efa1997-05-05 20:56:21 +0000336 XXX Darn! With the advent of thread state, we should have an array
337 of pending calls per thread in the thread state! Later...
338*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000339
Guido van Rossuma9672091994-09-14 13:31:22 +0000340#define NPENDINGCALLS 32
341static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000342 int (*func)(void *);
343 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000344} pendingcalls[NPENDINGCALLS];
345static volatile int pendingfirst = 0;
346static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000347static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000348
349int
Thomas Wouters334fb892000-07-25 12:56:38 +0000350Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000351{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000352 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000353 int i, j;
354 /* XXX Begin critical section */
355 /* XXX If you want this to be safe against nested
356 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000357 if (busy)
358 return -1;
359 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000360 i = pendinglast;
361 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000362 if (j == pendingfirst) {
363 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000364 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000365 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000366 pendingcalls[i].func = func;
367 pendingcalls[i].arg = arg;
368 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000369
370 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000371 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000372 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000373 /* XXX End critical section */
374 return 0;
375}
376
Guido van Rossum180d7b41994-09-29 09:45:57 +0000377int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000378Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000379{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000380 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000381#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000382 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000383 return 0;
384#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000385 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000386 return 0;
387 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000388 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000389 for (;;) {
390 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000391 int (*func)(void *);
392 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000393 i = pendingfirst;
394 if (i == pendinglast)
395 break; /* Queue empty */
396 func = pendingcalls[i].func;
397 arg = pendingcalls[i].arg;
398 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000399 if (func(arg) < 0) {
400 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000401 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000402 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000403 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000404 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000405 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000406 return 0;
407}
408
409
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000410/* The interpreter's recursion limit */
411
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000412static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000413int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000414
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000415int
416Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000417{
418 return recursion_limit;
419}
420
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000421void
422Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000423{
424 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000425 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000426}
427
Armin Rigo2b3eb402003-10-28 12:05:48 +0000428/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
429 if the recursion_depth reaches _Py_CheckRecursionLimit.
430 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
431 to guarantee that _Py_CheckRecursiveCall() is regularly called.
432 Without USE_STACKCHECK, there is no need for this. */
433int
434_Py_CheckRecursiveCall(char *where)
435{
436 PyThreadState *tstate = PyThreadState_GET();
437
438#ifdef USE_STACKCHECK
439 if (PyOS_CheckStack()) {
440 --tstate->recursion_depth;
441 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
442 return -1;
443 }
444#endif
445 if (tstate->recursion_depth > recursion_limit) {
446 --tstate->recursion_depth;
447 PyErr_Format(PyExc_RuntimeError,
448 "maximum recursion depth exceeded%s",
449 where);
450 return -1;
451 }
452 _Py_CheckRecursionLimit = recursion_limit;
453 return 0;
454}
455
Guido van Rossum374a9221991-04-04 10:40:29 +0000456/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000457enum why_code {
458 WHY_NOT = 0x0001, /* No error */
459 WHY_EXCEPTION = 0x0002, /* Exception occurred */
460 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
461 WHY_RETURN = 0x0008, /* 'return' statement */
462 WHY_BREAK = 0x0010, /* 'break' statement */
463 WHY_CONTINUE = 0x0020, /* 'continue' statement */
464 WHY_YIELD = 0x0040 /* 'yield' operator */
465};
Guido van Rossum374a9221991-04-04 10:40:29 +0000466
Raymond Hettinger7c958652004-04-06 10:11:10 +0000467static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000468static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000469
Skip Montanarod581d772002-09-03 20:10:45 +0000470/* for manipulating the thread switch and periodic "stuff" - used to be
471 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000472int _Py_CheckInterval = 100;
473volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000474
Guido van Rossumb209a111997-04-29 18:18:01 +0000475PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000476PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000477{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000478 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000479 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000480 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000481 (PyObject **)NULL, 0,
482 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000483 (PyObject **)NULL, 0,
484 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000485}
486
487
488/* Interpreter main loop */
489
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000490PyObject *
491PyEval_EvalFrame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000492{
Guido van Rossum950361c1997-01-24 13:49:28 +0000493#ifdef DXPAIRS
494 int lastopcode = 0;
495#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000496 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000497 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000498 register int opcode; /* Current opcode */
499 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000500 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000501 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000502 register PyObject *x; /* Result object -- NULL if error */
503 register PyObject *v; /* Temporary objects popped off stack */
504 register PyObject *w;
505 register PyObject *u;
506 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000507 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000508 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000509 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000510 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000511 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000512
Tim Peters8a5c3c72004-04-05 19:36:21 +0000513 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000514
515 not (instr_lb <= current_bytecode_offset < instr_ub)
516
Tim Peters8a5c3c72004-04-05 19:36:21 +0000517 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000518 initial values are such as to make this false the first
519 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000520 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000521
Guido van Rossumd076c731998-10-07 19:42:25 +0000522 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000523 PyObject *names;
524 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000525#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000526 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000527#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000528#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000529 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000530 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000531#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000532
Neal Norwitza81d2202002-07-14 00:27:26 +0000533/* Tuple access macros */
534
535#ifndef Py_DEBUG
536#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
537#else
538#define GETITEM(v, i) PyTuple_GetItem((v), (i))
539#endif
540
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000541#ifdef WITH_TSC
542/* Use Pentium timestamp counter to mark certain events:
543 inst0 -- beginning of switch statement for opcode dispatch
544 inst1 -- end of switch statement (may be skipped)
545 loop0 -- the top of the mainloop
546 loop1 -- place where control returns again to top of mainloop
547 (may be skipped)
548 intr1 -- beginning of long interruption
549 intr2 -- end of long interruption
550
551 Many opcodes call out to helper C functions. In some cases, the
552 time in those functions should be counted towards the time for the
553 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
554 calls another Python function; there's no point in charge all the
555 bytecode executed by the called function to the caller.
556
557 It's hard to make a useful judgement statically. In the presence
558 of operator overloading, it's impossible to tell if a call will
559 execute new Python code or not.
560
561 It's a case-by-case judgement. I'll use intr1 for the following
562 cases:
563
564 EXEC_STMT
565 IMPORT_STAR
566 IMPORT_FROM
567 CALL_FUNCTION (and friends)
568
569 */
570 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
571 int ticked = 0;
572
573 rdtscll(inst0);
574 rdtscll(inst1);
575 rdtscll(loop0);
576 rdtscll(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000577
578 /* shut up the compiler */
579 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000580#endif
581
Guido van Rossum374a9221991-04-04 10:40:29 +0000582/* Code access macros */
583
Guido van Rossumd076c731998-10-07 19:42:25 +0000584#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000585#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000586#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000587#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000588#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000589#define JUMPBY(x) (next_instr += (x))
590
Raymond Hettingerf606f872003-03-16 03:11:04 +0000591/* OpCode prediction macros
592 Some opcodes tend to come in pairs thus making it possible to predict
593 the second code when the first is run. For example, COMPARE_OP is often
594 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
595 followed by a POP_TOP.
596
597 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000598 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000599 processor has a high likelihood of making its own successful branch
600 prediction which results in a nearly zero overhead transition to the
601 next opcode.
602
603 A successful prediction saves a trip through the eval-loop including
604 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000605
Tim Peters8a5c3c72004-04-05 19:36:21 +0000606 If collecting opcode statistics, turn off prediction so that
607 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000608 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000609*/
610
Raymond Hettingera7216982004-02-08 19:59:27 +0000611#ifdef DYNAMIC_EXECUTION_PROFILE
612#define PREDICT(op) if (0) goto PRED_##op
613#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000614#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000615#endif
616
Raymond Hettingerf606f872003-03-16 03:11:04 +0000617#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000618#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000619
Guido van Rossum374a9221991-04-04 10:40:29 +0000620/* Stack manipulation macros */
621
622#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
623#define EMPTY() (STACK_LEVEL() == 0)
624#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000625#define SECOND() (stack_pointer[-2])
626#define THIRD() (stack_pointer[-3])
627#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000628#define SET_TOP(v) (stack_pointer[-1] = (v))
629#define SET_SECOND(v) (stack_pointer[-2] = (v))
630#define SET_THIRD(v) (stack_pointer[-3] = (v))
631#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000632#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000633#define BASIC_PUSH(v) (*stack_pointer++ = (v))
634#define BASIC_POP() (*--stack_pointer)
635
Guido van Rossum96a42c81992-01-12 02:29:51 +0000636#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000637#define PUSH(v) { (void)(BASIC_PUSH(v), \
638 lltrace && prtrace(TOP(), "push")); \
639 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000640#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000641#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
642 lltrace && prtrace(TOP(), "stackadj")); \
643 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000644#else
645#define PUSH(v) BASIC_PUSH(v)
646#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000647#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000648#endif
649
Guido van Rossum681d79a1995-07-18 14:51:37 +0000650/* Local variable macros */
651
652#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000653
654/* The SETLOCAL() macro must not DECREF the local variable in-place and
655 then store the new value; it must copy the old value to a temporary
656 value, then store the new value, and then DECREF the temporary value.
657 This is because it is possible that during the DECREF the frame is
658 accessed by other code (e.g. a __del__ method or gc.collect()) and the
659 variable would be pointing to already-freed memory. */
660#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
661 GETLOCAL(i) = value; \
662 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000663
Guido van Rossuma027efa1997-05-05 20:56:21 +0000664/* Start of code */
665
Tim Peters5ca576e2001-06-18 22:08:13 +0000666 if (f == NULL)
667 return NULL;
668
Armin Rigo1d313ab2003-10-25 14:33:09 +0000669 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000670 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000671 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000672
Tim Peters5ca576e2001-06-18 22:08:13 +0000673 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000674
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000675 if (tstate->use_tracing) {
676 if (tstate->c_tracefunc != NULL) {
677 /* tstate->c_tracefunc, if defined, is a
678 function that will be called on *every* entry
679 to a code block. Its return value, if not
680 None, is a function that will be called at
681 the start of each executed line of code.
682 (Actually, the function must return itself
683 in order to continue tracing.) The trace
684 functions are called with three arguments:
685 a pointer to the current frame, a string
686 indicating why the function is called, and
687 an argument which depends on the situation.
688 The global trace function is also called
689 whenever an exception is detected. */
690 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
691 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000692 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000693 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000694 }
695 }
696 if (tstate->c_profilefunc != NULL) {
697 /* Similar for c_profilefunc, except it needn't
698 return itself and isn't called for "line" events */
699 if (call_trace(tstate->c_profilefunc,
700 tstate->c_profileobj,
701 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000702 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000703 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000704 }
705 }
706 }
707
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000708 co = f->f_code;
709 names = co->co_names;
710 consts = co->co_consts;
711 fastlocals = f->f_localsplus;
712 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000713 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000714 /* An explanation is in order for the next line.
715
716 f->f_lasti now refers to the index of the last instruction
717 executed. You might think this was obvious from the name, but
718 this wasn't always true before 2.3! PyFrame_New now sets
719 f->f_lasti to -1 (i.e. the index *before* the first instruction)
720 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
721 does work. Promise. */
722 next_instr = first_instr + f->f_lasti + 1;
723 stack_pointer = f->f_stacktop;
724 assert(stack_pointer != NULL);
725 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
726
Tim Peters5ca576e2001-06-18 22:08:13 +0000727#ifdef LLTRACE
728 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
729#endif
730#if defined(Py_DEBUG) || defined(LLTRACE)
731 filename = PyString_AsString(co->co_filename);
732#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000733
Guido van Rossum374a9221991-04-04 10:40:29 +0000734 why = WHY_NOT;
735 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000736 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000737 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000738
Guido van Rossum374a9221991-04-04 10:40:29 +0000739 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000740#ifdef WITH_TSC
741 if (inst1 == 0) {
742 /* Almost surely, the opcode executed a break
743 or a continue, preventing inst1 from being set
744 on the way out of the loop.
745 */
746 rdtscll(inst1);
747 loop1 = inst1;
748 }
749 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
750 intr0, intr1);
751 ticked = 0;
752 inst1 = 0;
753 intr0 = 0;
754 intr1 = 0;
755 rdtscll(loop0);
756#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000757 assert(stack_pointer >= f->f_valuestack); /* else underflow */
758 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
759
Guido van Rossuma027efa1997-05-05 20:56:21 +0000760 /* Do periodic things. Doing this every time through
761 the loop would add too much overhead, so we do it
762 only every Nth instruction. We also do it if
763 ``things_to_do'' is set, i.e. when an asynchronous
764 event needs attention (e.g. a signal handler or
765 async I/O handler); see Py_AddPendingCall() and
766 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000767
Skip Montanarod581d772002-09-03 20:10:45 +0000768 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000769 if (*next_instr == SETUP_FINALLY) {
770 /* Make the last opcode before
771 a try: finally: block uninterruptable. */
772 goto fast_next_opcode;
773 }
Skip Montanarod581d772002-09-03 20:10:45 +0000774 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000775 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000776#ifdef WITH_TSC
777 ticked = 1;
778#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000779 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000780 if (Py_MakePendingCalls() < 0) {
781 why = WHY_EXCEPTION;
782 goto on_error;
783 }
784 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000785#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000786 if (interpreter_lock) {
787 /* Give another thread a chance */
788
Guido van Rossum25ce5661997-08-02 03:10:38 +0000789 if (PyThreadState_Swap(NULL) != tstate)
790 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000791 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000792
793 /* Other threads may run now */
794
Guido van Rossum65d5b571998-12-21 19:32:43 +0000795 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000796 if (PyThreadState_Swap(tstate) != NULL)
797 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000798
799 /* Check for thread interrupts */
800
801 if (tstate->async_exc != NULL) {
802 x = tstate->async_exc;
803 tstate->async_exc = NULL;
804 PyErr_SetNone(x);
805 Py_DECREF(x);
806 why = WHY_EXCEPTION;
807 goto on_error;
808 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000809 }
810#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000811 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000812
Neil Schemenauer63543862002-02-17 19:10:14 +0000813 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000814 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000815
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000816 /* line-by-line tracing support */
817
818 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
819 /* see maybe_call_line_trace
820 for expository comments */
821 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000822
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000823 err = maybe_call_line_trace(tstate->c_tracefunc,
824 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000825 f, &instr_lb, &instr_ub,
826 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000827 /* Reload possibly changed frame fields */
828 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000829 if (f->f_stacktop != NULL) {
830 stack_pointer = f->f_stacktop;
831 f->f_stacktop = NULL;
832 }
833 if (err) {
834 /* trace function raised an exception */
835 goto on_error;
836 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000837 }
838
839 /* Extract opcode and argument */
840
Guido van Rossum374a9221991-04-04 10:40:29 +0000841 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000842 oparg = 0; /* allows oparg to be stored in a register because
843 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000844 if (HAS_ARG(opcode))
845 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000846 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000847#ifdef DYNAMIC_EXECUTION_PROFILE
848#ifdef DXPAIRS
849 dxpairs[lastopcode][opcode]++;
850 lastopcode = opcode;
851#endif
852 dxp[opcode]++;
853#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000854
Guido van Rossum96a42c81992-01-12 02:29:51 +0000855#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000856 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000857
Guido van Rossum96a42c81992-01-12 02:29:51 +0000858 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000859 if (HAS_ARG(opcode)) {
860 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000861 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000862 }
863 else {
864 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000865 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000866 }
867 }
868#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000869
Guido van Rossum374a9221991-04-04 10:40:29 +0000870 /* Main switch on opcode */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000871#ifdef WITH_TSC
872 rdtscll(inst0);
873#endif
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#ifdef WITH_TSC
1633 rdtscll(intr0);
1634#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +00001635 err = exec_statement(f, u, v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001636#ifdef WITH_TSC
1637 rdtscll(intr1);
1638#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00001639 Py_DECREF(u);
1640 Py_DECREF(v);
1641 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001642 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001643
Guido van Rossum374a9221991-04-04 10:40:29 +00001644 case POP_BLOCK:
1645 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001646 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001647 while (STACK_LEVEL() > b->b_level) {
1648 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001649 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001650 }
1651 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001652 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001653
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 case END_FINALLY:
1655 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001656 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001657 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001658 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001659 if (why == WHY_RETURN ||
1660 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001661 retval = POP();
1662 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001663 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001664 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001665 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001666 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001668 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001670 else if (v != Py_None) {
1671 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 "'finally' pops bad exception");
1673 why = WHY_EXCEPTION;
1674 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001675 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001676 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Guido van Rossum374a9221991-04-04 10:40:29 +00001678 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001679 u = TOP();
1680 v = SECOND();
1681 w = THIRD();
1682 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001683 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001684 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001685 Py_DECREF(u);
1686 Py_DECREF(v);
1687 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001688 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001691 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001692 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001693 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001694 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001695 err = PyDict_SetItem(x, w, v);
1696 else
1697 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001698 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001699 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001700 break;
1701 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001702 PyErr_Format(PyExc_SystemError,
1703 "no locals found when storing %s",
1704 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001705 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001706
Guido van Rossum374a9221991-04-04 10:40:29 +00001707 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001708 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001709 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001710 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001711 format_exc_check_arg(PyExc_NameError,
1712 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001713 break;
1714 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001715 PyErr_Format(PyExc_SystemError,
1716 "no locals when deleting %s",
1717 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001719
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001720 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001721 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001723 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1724 PyObject **items = ((PyTupleObject *)v)->ob_item;
1725 while (oparg--) {
1726 w = items[oparg];
1727 Py_INCREF(w);
1728 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001729 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001730 Py_DECREF(v);
1731 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001732 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1733 PyObject **items = ((PyListObject *)v)->ob_item;
1734 while (oparg--) {
1735 w = items[oparg];
1736 Py_INCREF(w);
1737 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001738 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001739 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001740 stack_pointer + oparg))
1741 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001742 else {
1743 if (PyErr_ExceptionMatches(PyExc_TypeError))
1744 PyErr_SetString(PyExc_TypeError,
1745 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001746 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001747 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001748 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001749 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001750
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001752 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001753 v = TOP();
1754 u = SECOND();
1755 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001756 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1757 Py_DECREF(v);
1758 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001759 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001760 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Guido van Rossum374a9221991-04-04 10:40:29 +00001762 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001763 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001765 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1766 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001767 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001768 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001769
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001770 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001771 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001772 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001773 err = PyDict_SetItem(f->f_globals, w, v);
1774 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001775 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001776 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001778 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001779 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001780 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001781 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001782 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001783 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001784
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001786 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001787 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001788 PyErr_Format(PyExc_SystemError,
1789 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001790 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001791 break;
1792 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001793 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001794 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001795 Py_XINCREF(x);
1796 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001797 else {
1798 x = PyObject_GetItem(v, w);
1799 if (x == NULL && PyErr_Occurred()) {
1800 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1801 break;
1802 PyErr_Clear();
1803 }
1804 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001806 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001808 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001809 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001810 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001811 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001812 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001813 break;
1814 }
1815 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001816 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001818 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001819 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001820
Guido van Rossum374a9221991-04-04 10:40:29 +00001821 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001822 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001823 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001824 /* Inline the PyDict_GetItem() calls.
1825 WARNING: this is an extreme speed hack.
1826 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001827 long hash = ((PyStringObject *)w)->ob_shash;
1828 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001829 PyDictObject *d;
1830 d = (PyDictObject *)(f->f_globals);
1831 x = d->ma_lookup(d, w, hash)->me_value;
1832 if (x != NULL) {
1833 Py_INCREF(x);
1834 PUSH(x);
1835 continue;
1836 }
1837 d = (PyDictObject *)(f->f_builtins);
1838 x = d->ma_lookup(d, w, hash)->me_value;
1839 if (x != NULL) {
1840 Py_INCREF(x);
1841 PUSH(x);
1842 continue;
1843 }
1844 goto load_global_error;
1845 }
1846 }
1847 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001848 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001849 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001850 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001851 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001852 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001853 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001854 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001855 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001856 break;
1857 }
1858 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001859 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001861 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001862
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001863 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001864 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001865 if (x != NULL) {
1866 SETLOCAL(oparg, NULL);
1867 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001868 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001869 format_exc_check_arg(
1870 PyExc_UnboundLocalError,
1871 UNBOUNDLOCAL_ERROR_MSG,
1872 PyTuple_GetItem(co->co_varnames, oparg)
1873 );
1874 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001876 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001877 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001878 Py_INCREF(x);
1879 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001880 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001881 break;
1882
1883 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001884 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001885 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001886 if (w != NULL) {
1887 PUSH(w);
1888 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001889 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001890 err = -1;
1891 /* Don't stomp existing exception */
1892 if (PyErr_Occurred())
1893 break;
1894 if (oparg < f->f_ncells) {
1895 v = PyTuple_GetItem(co->co_cellvars,
1896 oparg);
1897 format_exc_check_arg(
1898 PyExc_UnboundLocalError,
1899 UNBOUNDLOCAL_ERROR_MSG,
1900 v);
1901 } else {
1902 v = PyTuple_GetItem(
1903 co->co_freevars,
1904 oparg - f->f_ncells);
1905 format_exc_check_arg(
1906 PyExc_NameError,
1907 UNBOUNDFREE_ERROR_MSG,
1908 v);
1909 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001910 break;
1911
1912 case STORE_DEREF:
1913 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001914 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001915 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001916 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001917 continue;
1918
Guido van Rossum374a9221991-04-04 10:40:29 +00001919 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001920 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001921 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001922 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001924 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001925 }
1926 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001927 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001928 }
1929 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Guido van Rossum374a9221991-04-04 10:40:29 +00001931 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001932 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001934 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001935 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001936 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001937 }
1938 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001939 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001940 }
1941 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001942
Guido van Rossum374a9221991-04-04 10:40:29 +00001943 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001944 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001946 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001947 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001948
Guido van Rossum374a9221991-04-04 10:40:29 +00001949 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001950 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001951 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001952 x = PyObject_GetAttr(v, w);
1953 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001954 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001955 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001956 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001957
Guido van Rossum374a9221991-04-04 10:40:29 +00001958 case COMPARE_OP:
1959 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001960 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001961 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001962 /* INLINE: cmp(int, int) */
1963 register long a, b;
1964 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001965 a = PyInt_AS_LONG(v);
1966 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001967 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001968 case PyCmp_LT: res = a < b; break;
1969 case PyCmp_LE: res = a <= b; break;
1970 case PyCmp_EQ: res = a == b; break;
1971 case PyCmp_NE: res = a != b; break;
1972 case PyCmp_GT: res = a > b; break;
1973 case PyCmp_GE: res = a >= b; break;
1974 case PyCmp_IS: res = v == w; break;
1975 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001976 default: goto slow_compare;
1977 }
1978 x = res ? Py_True : Py_False;
1979 Py_INCREF(x);
1980 }
1981 else {
1982 slow_compare:
1983 x = cmp_outcome(oparg, v, w);
1984 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001985 Py_DECREF(v);
1986 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001987 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001988 if (x == NULL) break;
1989 PREDICT(JUMP_IF_FALSE);
1990 PREDICT(JUMP_IF_TRUE);
1991 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001992
Guido van Rossum374a9221991-04-04 10:40:29 +00001993 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001994 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001995 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001996 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001997 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001998 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001999 break;
2000 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00002001 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002002 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002003 w,
2004 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002005 f->f_locals == NULL ?
2006 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002007 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002008 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002009 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002010 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002011 x = NULL;
2012 break;
2013 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002014#ifdef WITH_TSC
2015 rdtscll(intr0);
2016#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00002017 x = PyEval_CallObject(x, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002018#ifdef WITH_TSC
2019 rdtscll(intr1);
2020#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00002021 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002022 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002023 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002024 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002025
Thomas Wouters52152252000-08-17 22:55:00 +00002026 case IMPORT_STAR:
2027 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002028 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002029 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002030 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002031 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002032 break;
2033 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002034#ifdef WITH_TSC
2035 rdtscll(intr0);
2036#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002037 err = import_all_from(x, v);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002038#ifdef WITH_TSC
2039 rdtscll(intr1);
2040#endif
Guido van Rossumb209a111997-04-29 18:18:01 +00002041 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002042 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002043 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002044 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002045
Thomas Wouters52152252000-08-17 22:55:00 +00002046 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002047 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002048 v = TOP();
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002049#ifdef WITH_TSC
2050 rdtscll(intr0);
2051#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002052 x = import_from(v, w);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002053#ifdef WITH_TSC
2054 rdtscll(intr1);
2055#endif
Thomas Wouters52152252000-08-17 22:55:00 +00002056 PUSH(x);
2057 if (x != NULL) continue;
2058 break;
2059
Guido van Rossum374a9221991-04-04 10:40:29 +00002060 case JUMP_FORWARD:
2061 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002062 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002063
Raymond Hettingerf606f872003-03-16 03:11:04 +00002064 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002065 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002066 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002067 if (w == Py_True) {
2068 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002069 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002070 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002071 if (w == Py_False) {
2072 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002073 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002074 }
2075 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002076 if (err > 0)
2077 err = 0;
2078 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002079 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002080 else
2081 break;
2082 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002083
Raymond Hettingerf606f872003-03-16 03:11:04 +00002084 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002085 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002086 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002087 if (w == Py_False) {
2088 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002089 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002090 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002091 if (w == Py_True) {
2092 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002093 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002094 }
2095 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002096 if (err > 0) {
2097 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002098 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002099 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002100 else if (err == 0)
2101 ;
2102 else
2103 break;
2104 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002105
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002106 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002107 case JUMP_ABSOLUTE:
2108 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002109 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002110
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002111 case GET_ITER:
2112 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002113 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002114 x = PyObject_GetIter(v);
2115 Py_DECREF(v);
2116 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002117 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002118 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002119 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002120 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002121 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002122 break;
2123
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002124 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002125 case FOR_ITER:
2126 /* before: [iter]; after: [iter, iter()] *or* [] */
2127 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002128 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002129 if (x != NULL) {
2130 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002131 PREDICT(STORE_FAST);
2132 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002133 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002134 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002135 if (PyErr_Occurred()) {
2136 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2137 break;
2138 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002139 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002140 /* iterator ended normally */
2141 x = v = POP();
2142 Py_DECREF(v);
2143 JUMPBY(oparg);
2144 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002145
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002146 case BREAK_LOOP:
2147 why = WHY_BREAK;
2148 goto fast_block_end;
2149
2150 case CONTINUE_LOOP:
2151 retval = PyInt_FromLong(oparg);
2152 why = WHY_CONTINUE;
2153 goto fast_block_end;
2154
Guido van Rossum374a9221991-04-04 10:40:29 +00002155 case SETUP_LOOP:
2156 case SETUP_EXCEPT:
2157 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002158 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002159 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002160 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002161
Guido van Rossumf10570b1995-07-07 22:53:21 +00002162 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002163 {
2164 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002165 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002166 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002167#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002168 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002169#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002170 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002171#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002172 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002173 PUSH(x);
2174 if (x != NULL)
2175 continue;
2176 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002177 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002178
Jeremy Hylton76901512000-03-28 23:49:17 +00002179 case CALL_FUNCTION_VAR:
2180 case CALL_FUNCTION_KW:
2181 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002182 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002183 int na = oparg & 0xff;
2184 int nk = (oparg>>8) & 0xff;
2185 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002186 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002187 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002188 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002189 if (flags & CALL_FLAG_VAR)
2190 n++;
2191 if (flags & CALL_FLAG_KW)
2192 n++;
2193 pfunc = stack_pointer - n - 1;
2194 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002195
Guido van Rossumac7be682001-01-17 15:42:30 +00002196 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002197 && PyMethod_GET_SELF(func) != NULL) {
2198 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002199 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002200 func = PyMethod_GET_FUNCTION(func);
2201 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002202 Py_DECREF(*pfunc);
2203 *pfunc = self;
2204 na++;
2205 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002206 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002207 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002208 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002209#ifdef WITH_TSC
2210 rdtscll(intr0);
2211#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002212 x = ext_do_call(func, &sp, flags, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002213#ifdef WITH_TSC
2214 rdtscll(intr1);
2215#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002216 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002217 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002218
Jeremy Hylton76901512000-03-28 23:49:17 +00002219 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002220 w = POP();
2221 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002222 }
2223 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002224 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002225 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002226 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002227 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002228
Guido van Rossum681d79a1995-07-18 14:51:37 +00002229 case MAKE_FUNCTION:
2230 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002231 x = PyFunction_New(v, f->f_globals);
2232 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002233 /* XXX Maybe this should be a separate opcode? */
2234 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002235 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002236 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002237 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002238 x = NULL;
2239 break;
2240 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002241 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002242 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002243 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002244 }
2245 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002246 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002247 }
2248 PUSH(x);
2249 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002250
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002251 case MAKE_CLOSURE:
2252 {
2253 int nfree;
2254 v = POP(); /* code object */
2255 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002256 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002257 Py_DECREF(v);
2258 /* XXX Maybe this should be a separate opcode? */
2259 if (x != NULL && nfree > 0) {
2260 v = PyTuple_New(nfree);
2261 if (v == NULL) {
2262 Py_DECREF(x);
2263 x = NULL;
2264 break;
2265 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002266 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002267 w = POP();
2268 PyTuple_SET_ITEM(v, nfree, w);
2269 }
2270 err = PyFunction_SetClosure(x, v);
2271 Py_DECREF(v);
2272 }
2273 if (x != NULL && oparg > 0) {
2274 v = PyTuple_New(oparg);
2275 if (v == NULL) {
2276 Py_DECREF(x);
2277 x = NULL;
2278 break;
2279 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002280 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002281 w = POP();
2282 PyTuple_SET_ITEM(v, oparg, w);
2283 }
2284 err = PyFunction_SetDefaults(x, v);
2285 Py_DECREF(v);
2286 }
2287 PUSH(x);
2288 break;
2289 }
2290
Guido van Rossum8861b741996-07-30 16:49:37 +00002291 case BUILD_SLICE:
2292 if (oparg == 3)
2293 w = POP();
2294 else
2295 w = NULL;
2296 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002297 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002298 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002299 Py_DECREF(u);
2300 Py_DECREF(v);
2301 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002302 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002303 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002304 break;
2305
Fred Drakeef8ace32000-08-24 00:32:09 +00002306 case EXTENDED_ARG:
2307 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002308 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002309 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002310
Guido van Rossum374a9221991-04-04 10:40:29 +00002311 default:
2312 fprintf(stderr,
2313 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002314 PyCode_Addr2Line(f->f_code, f->f_lasti),
2315 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002316 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002317 why = WHY_EXCEPTION;
2318 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002319
2320#ifdef CASE_TOO_BIG
2321 }
2322#endif
2323
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 } /* switch */
2325
2326 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002327
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002328#ifdef WITH_TSC
2329 rdtscll(inst1);
2330#endif
2331
Guido van Rossum374a9221991-04-04 10:40:29 +00002332 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Guido van Rossum374a9221991-04-04 10:40:29 +00002334 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002335 if (err == 0 && x != NULL) {
2336#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002337 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002338 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002339 fprintf(stderr,
2340 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002341 else {
2342#endif
2343#ifdef WITH_TSC
2344 rdtscll(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002345#endif
2346 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002347#ifdef CHECKEXC
2348 }
2349#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002350 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002352 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002353 err = 0;
2354 }
2355
Guido van Rossum374a9221991-04-04 10:40:29 +00002356 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002357
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002358 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002359 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002360 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002361 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002362 why = WHY_EXCEPTION;
2363 }
2364 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002365#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002367 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002368 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002369 char buf[1024];
2370 sprintf(buf, "Stack unwind with exception "
2371 "set and why=%d", why);
2372 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002373 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002374 }
2375#endif
2376
2377 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002378
Guido van Rossum374a9221991-04-04 10:40:29 +00002379 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002380 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002381
Fred Drake8f51f542001-10-04 14:48:42 +00002382 if (tstate->c_tracefunc != NULL)
2383 call_exc_trace(tstate->c_tracefunc,
2384 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002385 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002386
Guido van Rossum374a9221991-04-04 10:40:29 +00002387 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002388
Guido van Rossum374a9221991-04-04 10:40:29 +00002389 if (why == WHY_RERAISE)
2390 why = WHY_EXCEPTION;
2391
2392 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002393
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002394fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002395 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002396 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002397
Tim Peters8a5c3c72004-04-05 19:36:21 +00002398 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002399 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2400 /* For a continue inside a try block,
2401 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002402 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2403 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002404 why = WHY_NOT;
2405 JUMPTO(PyInt_AS_LONG(retval));
2406 Py_DECREF(retval);
2407 break;
2408 }
2409
Guido van Rossum374a9221991-04-04 10:40:29 +00002410 while (STACK_LEVEL() > b->b_level) {
2411 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002412 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002413 }
2414 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2415 why = WHY_NOT;
2416 JUMPTO(b->b_handler);
2417 break;
2418 }
2419 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002420 (b->b_type == SETUP_EXCEPT &&
2421 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002422 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002423 PyObject *exc, *val, *tb;
2424 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002425 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002426 val = Py_None;
2427 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002428 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002429 /* Make the raw exception data
2430 available to the handler,
2431 so a program can emulate the
2432 Python main loop. Don't do
2433 this for 'finally'. */
2434 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002435 PyErr_NormalizeException(
2436 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002437 set_exc_info(tstate,
2438 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002439 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002440 if (tb == NULL) {
2441 Py_INCREF(Py_None);
2442 PUSH(Py_None);
2443 } else
2444 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002445 PUSH(val);
2446 PUSH(exc);
2447 }
2448 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002449 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002450 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002451 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002452 PUSH(v);
2453 }
2454 why = WHY_NOT;
2455 JUMPTO(b->b_handler);
2456 break;
2457 }
2458 } /* unwind stack */
2459
2460 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002461
Guido van Rossum374a9221991-04-04 10:40:29 +00002462 if (why != WHY_NOT)
2463 break;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002464#ifdef WITH_TSC
2465 rdtscll(loop1);
2466#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002467
Guido van Rossum374a9221991-04-04 10:40:29 +00002468 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002469
Tim Peters8a5c3c72004-04-05 19:36:21 +00002470 assert(why != WHY_YIELD);
2471 /* Pop remaining stack entries. */
2472 while (!EMPTY()) {
2473 v = POP();
2474 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002475 }
2476
Tim Peters8a5c3c72004-04-05 19:36:21 +00002477 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002478 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002479
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002480fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002481 if (tstate->use_tracing) {
2482 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002483 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002484 if (call_trace(tstate->c_tracefunc,
2485 tstate->c_traceobj, f,
2486 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002487 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002488 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002489 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002490 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002491 }
Fred Drake8f51f542001-10-04 14:48:42 +00002492 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002493 if (why == WHY_EXCEPTION)
2494 call_trace_protected(tstate->c_profilefunc,
2495 tstate->c_profileobj, f,
2496 PyTrace_RETURN);
2497 else if (call_trace(tstate->c_profilefunc,
2498 tstate->c_profileobj, f,
2499 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002500 Py_XDECREF(retval);
2501 retval = NULL;
2502 why = WHY_EXCEPTION;
2503 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002504 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002505 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002506
Guido van Rossuma027efa1997-05-05 20:56:21 +00002507 reset_exc_info(tstate);
2508
Tim Peters5ca576e2001-06-18 22:08:13 +00002509 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002510 exit_eval_frame:
2511 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002512 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002513
Guido van Rossum96a42c81992-01-12 02:29:51 +00002514 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002515}
2516
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002517/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002518 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
2519 the test in the if statement in Misc/gdbinit:ppystack */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002520
Tim Peters6d6c1a32001-08-02 04:15:00 +00002521PyObject *
2522PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002523 PyObject **args, int argcount, PyObject **kws, int kwcount,
2524 PyObject **defs, int defcount, PyObject *closure)
2525{
2526 register PyFrameObject *f;
2527 register PyObject *retval = NULL;
2528 register PyObject **fastlocals, **freevars;
2529 PyThreadState *tstate = PyThreadState_GET();
2530 PyObject *x, *u;
2531
2532 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002533 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002534 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002535 return NULL;
2536 }
2537
Jeremy Hylton985eba52003-02-05 23:13:00 +00002538 assert(globals != NULL);
2539 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002540 if (f == NULL)
2541 return NULL;
2542
2543 fastlocals = f->f_localsplus;
2544 freevars = f->f_localsplus + f->f_nlocals;
2545
2546 if (co->co_argcount > 0 ||
2547 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2548 int i;
2549 int n = argcount;
2550 PyObject *kwdict = NULL;
2551 if (co->co_flags & CO_VARKEYWORDS) {
2552 kwdict = PyDict_New();
2553 if (kwdict == NULL)
2554 goto fail;
2555 i = co->co_argcount;
2556 if (co->co_flags & CO_VARARGS)
2557 i++;
2558 SETLOCAL(i, kwdict);
2559 }
2560 if (argcount > co->co_argcount) {
2561 if (!(co->co_flags & CO_VARARGS)) {
2562 PyErr_Format(PyExc_TypeError,
2563 "%.200s() takes %s %d "
2564 "%sargument%s (%d given)",
2565 PyString_AsString(co->co_name),
2566 defcount ? "at most" : "exactly",
2567 co->co_argcount,
2568 kwcount ? "non-keyword " : "",
2569 co->co_argcount == 1 ? "" : "s",
2570 argcount);
2571 goto fail;
2572 }
2573 n = co->co_argcount;
2574 }
2575 for (i = 0; i < n; i++) {
2576 x = args[i];
2577 Py_INCREF(x);
2578 SETLOCAL(i, x);
2579 }
2580 if (co->co_flags & CO_VARARGS) {
2581 u = PyTuple_New(argcount - n);
2582 if (u == NULL)
2583 goto fail;
2584 SETLOCAL(co->co_argcount, u);
2585 for (i = n; i < argcount; i++) {
2586 x = args[i];
2587 Py_INCREF(x);
2588 PyTuple_SET_ITEM(u, i-n, x);
2589 }
2590 }
2591 for (i = 0; i < kwcount; i++) {
2592 PyObject *keyword = kws[2*i];
2593 PyObject *value = kws[2*i + 1];
2594 int j;
2595 if (keyword == NULL || !PyString_Check(keyword)) {
2596 PyErr_Format(PyExc_TypeError,
2597 "%.200s() keywords must be strings",
2598 PyString_AsString(co->co_name));
2599 goto fail;
2600 }
2601 /* XXX slow -- speed up using dictionary? */
2602 for (j = 0; j < co->co_argcount; j++) {
2603 PyObject *nm = PyTuple_GET_ITEM(
2604 co->co_varnames, j);
2605 int cmp = PyObject_RichCompareBool(
2606 keyword, nm, Py_EQ);
2607 if (cmp > 0)
2608 break;
2609 else if (cmp < 0)
2610 goto fail;
2611 }
2612 /* Check errors from Compare */
2613 if (PyErr_Occurred())
2614 goto fail;
2615 if (j >= co->co_argcount) {
2616 if (kwdict == NULL) {
2617 PyErr_Format(PyExc_TypeError,
2618 "%.200s() got an unexpected "
2619 "keyword argument '%.400s'",
2620 PyString_AsString(co->co_name),
2621 PyString_AsString(keyword));
2622 goto fail;
2623 }
2624 PyDict_SetItem(kwdict, keyword, value);
2625 }
2626 else {
2627 if (GETLOCAL(j) != NULL) {
2628 PyErr_Format(PyExc_TypeError,
2629 "%.200s() got multiple "
2630 "values for keyword "
2631 "argument '%.400s'",
2632 PyString_AsString(co->co_name),
2633 PyString_AsString(keyword));
2634 goto fail;
2635 }
2636 Py_INCREF(value);
2637 SETLOCAL(j, value);
2638 }
2639 }
2640 if (argcount < co->co_argcount) {
2641 int m = co->co_argcount - defcount;
2642 for (i = argcount; i < m; i++) {
2643 if (GETLOCAL(i) == NULL) {
2644 PyErr_Format(PyExc_TypeError,
2645 "%.200s() takes %s %d "
2646 "%sargument%s (%d given)",
2647 PyString_AsString(co->co_name),
2648 ((co->co_flags & CO_VARARGS) ||
2649 defcount) ? "at least"
2650 : "exactly",
2651 m, kwcount ? "non-keyword " : "",
2652 m == 1 ? "" : "s", i);
2653 goto fail;
2654 }
2655 }
2656 if (n > m)
2657 i = n - m;
2658 else
2659 i = 0;
2660 for (; i < defcount; i++) {
2661 if (GETLOCAL(m+i) == NULL) {
2662 PyObject *def = defs[i];
2663 Py_INCREF(def);
2664 SETLOCAL(m+i, def);
2665 }
2666 }
2667 }
2668 }
2669 else {
2670 if (argcount > 0 || kwcount > 0) {
2671 PyErr_Format(PyExc_TypeError,
2672 "%.200s() takes no arguments (%d given)",
2673 PyString_AsString(co->co_name),
2674 argcount + kwcount);
2675 goto fail;
2676 }
2677 }
2678 /* Allocate and initialize storage for cell vars, and copy free
2679 vars into frame. This isn't too efficient right now. */
2680 if (f->f_ncells) {
2681 int i = 0, j = 0, nargs, found;
2682 char *cellname, *argname;
2683 PyObject *c;
2684
2685 nargs = co->co_argcount;
2686 if (co->co_flags & CO_VARARGS)
2687 nargs++;
2688 if (co->co_flags & CO_VARKEYWORDS)
2689 nargs++;
2690
2691 /* Check for cells that shadow args */
2692 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2693 cellname = PyString_AS_STRING(
2694 PyTuple_GET_ITEM(co->co_cellvars, i));
2695 found = 0;
2696 while (j < nargs) {
2697 argname = PyString_AS_STRING(
2698 PyTuple_GET_ITEM(co->co_varnames, j));
2699 if (strcmp(cellname, argname) == 0) {
2700 c = PyCell_New(GETLOCAL(j));
2701 if (c == NULL)
2702 goto fail;
2703 GETLOCAL(f->f_nlocals + i) = c;
2704 found = 1;
2705 break;
2706 }
2707 j++;
2708 }
2709 if (found == 0) {
2710 c = PyCell_New(NULL);
2711 if (c == NULL)
2712 goto fail;
2713 SETLOCAL(f->f_nlocals + i, c);
2714 }
2715 }
2716 /* Initialize any that are left */
2717 while (i < f->f_ncells) {
2718 c = PyCell_New(NULL);
2719 if (c == NULL)
2720 goto fail;
2721 SETLOCAL(f->f_nlocals + i, c);
2722 i++;
2723 }
2724 }
2725 if (f->f_nfreevars) {
2726 int i;
2727 for (i = 0; i < f->f_nfreevars; ++i) {
2728 PyObject *o = PyTuple_GET_ITEM(closure, i);
2729 Py_INCREF(o);
2730 freevars[f->f_ncells + i] = o;
2731 }
2732 }
2733
Tim Peters5ca576e2001-06-18 22:08:13 +00002734 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002735 /* Don't need to keep the reference to f_back, it will be set
2736 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002737 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002738 f->f_back = NULL;
2739
Jeremy Hylton985eba52003-02-05 23:13:00 +00002740 PCALL(PCALL_GENERATOR);
2741
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002742 /* Create a new generator that owns the ready to run frame
2743 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002744 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002745 }
2746
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002747 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002748
2749 fail: /* Jump here from prelude on failure */
2750
Tim Petersb13680b2001-11-27 23:29:29 +00002751 /* decref'ing the frame can cause __del__ methods to get invoked,
2752 which can call back into Python. While we're done with the
2753 current Python frame (f), the associated C stack is still in use,
2754 so recursion_depth must be boosted for the duration.
2755 */
2756 assert(tstate != NULL);
2757 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002758 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002759 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002760 return retval;
2761}
2762
2763
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002764/* Implementation notes for set_exc_info() and reset_exc_info():
2765
2766- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2767 'exc_traceback'. These always travel together.
2768
2769- tstate->curexc_ZZZ is the "hot" exception that is set by
2770 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2771
2772- Once an exception is caught by an except clause, it is transferred
2773 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2774 can pick it up. This is the primary task of set_exc_info().
2775
2776- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2777
2778 Long ago, when none of this existed, there were just a few globals:
2779 one set corresponding to the "hot" exception, and one set
2780 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2781 globals; they were simply stored as sys.exc_ZZZ. For backwards
2782 compatibility, they still are!) The problem was that in code like
2783 this:
2784
2785 try:
2786 "something that may fail"
2787 except "some exception":
2788 "do something else first"
2789 "print the exception from sys.exc_ZZZ."
2790
2791 if "do something else first" invoked something that raised and caught
2792 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2793 cause of subtle bugs. I fixed this by changing the semantics as
2794 follows:
2795
2796 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2797 *in that frame*.
2798
2799 - But initially, and as long as no exception is caught in a given
2800 frame, sys.exc_ZZZ will hold the last exception caught in the
2801 previous frame (or the frame before that, etc.).
2802
2803 The first bullet fixed the bug in the above example. The second
2804 bullet was for backwards compatibility: it was (and is) common to
2805 have a function that is called when an exception is caught, and to
2806 have that function access the caught exception via sys.exc_ZZZ.
2807 (Example: traceback.print_exc()).
2808
2809 At the same time I fixed the problem that sys.exc_ZZZ weren't
2810 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2811 but that's really a separate improvement.
2812
2813 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2814 variables to what they were before the current frame was called. The
2815 set_exc_info() function saves them on the frame so that
2816 reset_exc_info() can restore them. The invariant is that
2817 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2818 exception (where "catching" an exception applies only to successful
2819 except clauses); and if the current frame ever caught an exception,
2820 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2821 at the start of the current frame.
2822
2823*/
2824
Guido van Rossuma027efa1997-05-05 20:56:21 +00002825static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002826set_exc_info(PyThreadState *tstate,
2827 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002828{
2829 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002830 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002831
Guido van Rossuma027efa1997-05-05 20:56:21 +00002832 frame = tstate->frame;
2833 if (frame->f_exc_type == NULL) {
2834 /* This frame didn't catch an exception before */
2835 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002836 if (tstate->exc_type == NULL) {
2837 Py_INCREF(Py_None);
2838 tstate->exc_type = Py_None;
2839 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002840 tmp_type = frame->f_exc_type;
2841 tmp_value = frame->f_exc_value;
2842 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002843 Py_XINCREF(tstate->exc_type);
2844 Py_XINCREF(tstate->exc_value);
2845 Py_XINCREF(tstate->exc_traceback);
2846 frame->f_exc_type = tstate->exc_type;
2847 frame->f_exc_value = tstate->exc_value;
2848 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002849 Py_XDECREF(tmp_type);
2850 Py_XDECREF(tmp_value);
2851 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002852 }
2853 /* Set new exception for this thread */
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(type);
2858 Py_XINCREF(value);
2859 Py_XINCREF(tb);
2860 tstate->exc_type = type;
2861 tstate->exc_value = value;
2862 tstate->exc_traceback = tb;
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", type);
2868 PySys_SetObject("exc_value", value);
2869 PySys_SetObject("exc_traceback", tb);
2870}
2871
2872static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002873reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002874{
2875 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002876 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002877 frame = tstate->frame;
2878 if (frame->f_exc_type != NULL) {
2879 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002880 tmp_type = tstate->exc_type;
2881 tmp_value = tstate->exc_value;
2882 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002883 Py_XINCREF(frame->f_exc_type);
2884 Py_XINCREF(frame->f_exc_value);
2885 Py_XINCREF(frame->f_exc_traceback);
2886 tstate->exc_type = frame->f_exc_type;
2887 tstate->exc_value = frame->f_exc_value;
2888 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002889 Py_XDECREF(tmp_type);
2890 Py_XDECREF(tmp_value);
2891 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002892 /* For b/w compatibility */
2893 PySys_SetObject("exc_type", frame->f_exc_type);
2894 PySys_SetObject("exc_value", frame->f_exc_value);
2895 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2896 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002897 tmp_type = frame->f_exc_type;
2898 tmp_value = frame->f_exc_value;
2899 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002900 frame->f_exc_type = NULL;
2901 frame->f_exc_value = NULL;
2902 frame->f_exc_traceback = NULL;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002903 Py_XDECREF(tmp_type);
2904 Py_XDECREF(tmp_value);
2905 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002906}
2907
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002908/* Logic for the raise statement (too complicated for inlining).
2909 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002910static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002911do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002912{
Guido van Rossumd295f121998-04-09 21:39:57 +00002913 if (type == NULL) {
2914 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002915 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002916 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2917 value = tstate->exc_value;
2918 tb = tstate->exc_traceback;
2919 Py_XINCREF(type);
2920 Py_XINCREF(value);
2921 Py_XINCREF(tb);
2922 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002923
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002924 /* We support the following forms of raise:
2925 raise <class>, <classinstance>
2926 raise <class>, <argument tuple>
2927 raise <class>, None
2928 raise <class>, <argument>
2929 raise <classinstance>, None
2930 raise <string>, <object>
2931 raise <string>, None
2932
2933 An omitted second argument is the same as None.
2934
2935 In addition, raise <tuple>, <anything> is the same as
2936 raising the tuple's first item (and it better have one!);
2937 this rule is applied recursively.
2938
2939 Finally, an optional third argument can be supplied, which
2940 gives the traceback to be substituted (useful when
2941 re-raising an exception after examining it). */
2942
2943 /* First, check the traceback argument, replacing None with
2944 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002945 if (tb == Py_None) {
2946 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002947 tb = NULL;
2948 }
2949 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002950 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002951 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002952 goto raise_error;
2953 }
2954
2955 /* Next, replace a missing value with None */
2956 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002957 value = Py_None;
2958 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002959 }
2960
2961 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002962 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2963 PyObject *tmp = type;
2964 type = PyTuple_GET_ITEM(type, 0);
2965 Py_INCREF(type);
2966 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002967 }
2968
Tim Petersafb2c802002-04-18 18:06:20 +00002969 if (PyString_CheckExact(type))
2970 /* Raising builtin string is deprecated but still allowed --
2971 * do nothing. Raising an instance of a new-style str
2972 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002973 PyErr_Warn(PyExc_PendingDeprecationWarning,
2974 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002975
2976 else if (PyClass_Check(type))
2977 PyErr_NormalizeException(&type, &value, &tb);
2978
Guido van Rossumb209a111997-04-29 18:18:01 +00002979 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002980 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002981 if (value != Py_None) {
2982 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002983 "instance exception may not have a separate value");
2984 goto raise_error;
2985 }
2986 else {
2987 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002988 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002989 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002990 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2991 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002992 }
2993 }
2994 else {
2995 /* Not something you can raise. You get an exception
2996 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002997 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002998 "exceptions must be classes, instances, or "
2999 "strings (deprecated), not %s",
3000 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003001 goto raise_error;
3002 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003003 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003004 if (tb == NULL)
3005 return WHY_EXCEPTION;
3006 else
3007 return WHY_RERAISE;
3008 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003009 Py_XDECREF(value);
3010 Py_XDECREF(type);
3011 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003012 return WHY_EXCEPTION;
3013}
3014
Tim Petersd6d010b2001-06-21 02:49:55 +00003015/* Iterate v argcnt times and store the results on the stack (via decreasing
3016 sp). Return 1 for success, 0 if error. */
3017
Barry Warsawe42b18f1997-08-25 22:13:04 +00003018static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003019unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003020{
Tim Petersd6d010b2001-06-21 02:49:55 +00003021 int i = 0;
3022 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003023 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003024
Tim Petersd6d010b2001-06-21 02:49:55 +00003025 assert(v != NULL);
3026
3027 it = PyObject_GetIter(v);
3028 if (it == NULL)
3029 goto Error;
3030
3031 for (; i < argcnt; i++) {
3032 w = PyIter_Next(it);
3033 if (w == NULL) {
3034 /* Iterator done, via error or exhaustion. */
3035 if (!PyErr_Occurred()) {
3036 PyErr_Format(PyExc_ValueError,
3037 "need more than %d value%s to unpack",
3038 i, i == 1 ? "" : "s");
3039 }
3040 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003041 }
3042 *--sp = w;
3043 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003044
3045 /* We better have exhausted the iterator now. */
3046 w = PyIter_Next(it);
3047 if (w == NULL) {
3048 if (PyErr_Occurred())
3049 goto Error;
3050 Py_DECREF(it);
3051 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003052 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003053 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003054 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003055 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003056Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003057 for (; i > 0; i--, sp++)
3058 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003059 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003060 return 0;
3061}
3062
3063
Guido van Rossum96a42c81992-01-12 02:29:51 +00003064#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003065static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003066prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003067{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003068 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003069 if (PyObject_Print(v, stdout, 0) != 0)
3070 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003071 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003072 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003073}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003074#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003075
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003076static void
Fred Drake5755ce62001-06-27 19:19:46 +00003077call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003078{
Guido van Rossumb209a111997-04-29 18:18:01 +00003079 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003080 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003081 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003082 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003083 value = Py_None;
3084 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003085 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003086 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003087 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003088 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003089 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003090 }
Fred Drake5755ce62001-06-27 19:19:46 +00003091 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003092 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003093 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003094 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003095 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003096 Py_XDECREF(type);
3097 Py_XDECREF(value);
3098 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003099 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003100}
3101
Fred Drake4ec5d562001-10-04 19:26:43 +00003102static void
3103call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3104 int what)
3105{
3106 PyObject *type, *value, *traceback;
3107 int err;
3108 PyErr_Fetch(&type, &value, &traceback);
3109 err = call_trace(func, obj, frame, what, NULL);
3110 if (err == 0)
3111 PyErr_Restore(type, value, traceback);
3112 else {
3113 Py_XDECREF(type);
3114 Py_XDECREF(value);
3115 Py_XDECREF(traceback);
3116 }
3117}
3118
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003119static int
Fred Drake5755ce62001-06-27 19:19:46 +00003120call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3121 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003122{
Fred Drake5755ce62001-06-27 19:19:46 +00003123 register PyThreadState *tstate = frame->f_tstate;
3124 int result;
3125 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003126 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003127 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003128 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003129 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003130 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3131 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003132 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003133 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003134}
3135
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003136PyObject *
3137_PyEval_CallTracing(PyObject *func, PyObject *args)
3138{
3139 PyFrameObject *frame = PyEval_GetFrame();
3140 PyThreadState *tstate = frame->f_tstate;
3141 int save_tracing = tstate->tracing;
3142 int save_use_tracing = tstate->use_tracing;
3143 PyObject *result;
3144
3145 tstate->tracing = 0;
3146 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3147 || (tstate->c_profilefunc != NULL));
3148 result = PyObject_Call(func, args, NULL);
3149 tstate->tracing = save_tracing;
3150 tstate->use_tracing = save_use_tracing;
3151 return result;
3152}
3153
Michael W. Hudson006c7522002-11-08 13:08:46 +00003154static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003155maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003156 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3157 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003158{
3159 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003160
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003161 In a nutshell, we use the co_lnotab field of the code object
3162 to tell when execution has moved onto a different line.
3163
3164 As mentioned above, the basic idea is so set things up so
3165 that
3166
3167 *instr_lb <= frame->f_lasti < *instr_ub
3168
3169 is true so long as execution does not change lines.
3170
3171 This is all fairly simple. Digging the information out of
3172 co_lnotab takes some work, but is conceptually clear.
3173
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003174 Somewhat harder to explain is why we don't *always* call the
3175 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003176
3177 Consider this code:
3178
3179 1: def f(a):
3180 2: if a:
3181 3: print 1
3182 4: else:
3183 5: print 2
3184
3185 which compiles to this:
3186
3187 2 0 LOAD_FAST 0 (a)
3188 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003189 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003190
3191 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003192 10 PRINT_ITEM
3193 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003194 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003195 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003196
3197 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003198 19 PRINT_ITEM
3199 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003200 >> 21 LOAD_CONST 0 (None)
3201 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003202
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003203 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003204 15 and the co_lnotab will claim that execution has moved to
3205 line 3. This is at best misleading. In this case we could
3206 associate the POP_TOP with line 4, but that doesn't make
3207 sense in all cases (I think).
3208
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003209 What we do is only call the line trace function if the co_lnotab
3210 indicates we have jumped to the *start* of a line, i.e. if the
3211 current instruction offset matches the offset given for the
3212 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003213
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003214 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003215 Execution will jump from instruction offset 12 to offset 21.
3216 Then the co_lnotab would imply that execution has moved to line
3217 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003218
3219 Why do we set f_lineno when tracing? Well, consider the code
3220 above when 'a' is true. If stepping through this with 'n' in
3221 pdb, you would stop at line 1 with a "call" type event, then
3222 line events on lines 2 and 3, then a "return" type event -- but
3223 you would be shown line 5 during this event. This is a change
3224 from the behaviour in 2.2 and before, and I've found it
3225 confusing in practice. By setting and using f_lineno when
3226 tracing, one can report a line number different from that
3227 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003228 */
3229
Michael W. Hudson006c7522002-11-08 13:08:46 +00003230 int result = 0;
3231
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003232 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003233 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003234 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003235 unsigned char* p;
3236
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003237 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3238 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003239
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003240 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003241 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003242
3243 /* possible optimization: if f->f_lasti == instr_ub
3244 (likely to be a common case) then we already know
3245 instr_lb -- if we stored the matching value of p
3246 somwhere we could skip the first while loop. */
3247
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003248 /* see comments in compile.c for the description of
3249 co_lnotab. A point to remember: increments to p
3250 should come in pairs -- although we don't care about
3251 the line increments here, treating them as byte
3252 increments gets confusing, to say the least. */
3253
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003254 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003255 if (addr + *p > frame->f_lasti)
3256 break;
3257 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003258 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003259 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003260 --size;
3261 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003262
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003263 if (addr == frame->f_lasti) {
3264 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003265 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003266 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003267 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003268
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003269 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003270 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003271 addr += *p++;
3272 if (*p++)
3273 break;
3274 }
3275 *instr_ub = addr;
3276 }
3277 else {
3278 *instr_ub = INT_MAX;
3279 }
3280 }
Armin Rigobf57a142004-03-22 19:24:58 +00003281 else if (frame->f_lasti <= *instr_prev) {
3282 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003283 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003284 PyTrace_LINE, Py_None);
3285 }
3286 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003287 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003288}
3289
Fred Drake5755ce62001-06-27 19:19:46 +00003290void
3291PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003292{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003293 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003294 PyObject *temp = tstate->c_profileobj;
3295 Py_XINCREF(arg);
3296 tstate->c_profilefunc = NULL;
3297 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003298 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003299 Py_XDECREF(temp);
3300 tstate->c_profilefunc = func;
3301 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003302 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003303}
3304
3305void
3306PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3307{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003308 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003309 PyObject *temp = tstate->c_traceobj;
3310 Py_XINCREF(arg);
3311 tstate->c_tracefunc = NULL;
3312 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003313 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003314 Py_XDECREF(temp);
3315 tstate->c_tracefunc = func;
3316 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003317 tstate->use_tracing = ((func != NULL)
3318 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003319}
3320
Guido van Rossumb209a111997-04-29 18:18:01 +00003321PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003322PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003323{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003324 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003325 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003326 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003327 else
3328 return current_frame->f_builtins;
3329}
3330
Guido van Rossumb209a111997-04-29 18:18:01 +00003331PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003332PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003333{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003334 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003335 if (current_frame == NULL)
3336 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003337 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003338 return current_frame->f_locals;
3339}
3340
Guido van Rossumb209a111997-04-29 18:18:01 +00003341PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003342PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003343{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003344 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003345 if (current_frame == NULL)
3346 return NULL;
3347 else
3348 return current_frame->f_globals;
3349}
3350
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003351PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003352PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003353{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003354 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003355 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003356}
3357
Guido van Rossum6135a871995-01-09 17:53:26 +00003358int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003359PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003360{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003361 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003362 return current_frame == NULL ? 0 : current_frame->f_restricted;
3363}
3364
Guido van Rossumbe270261997-05-22 22:26:18 +00003365int
Tim Peters5ba58662001-07-16 02:29:45 +00003366PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003367{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003368 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003369 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003370
3371 if (current_frame != NULL) {
3372 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003373 const int compilerflags = codeflags & PyCF_MASK;
3374 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003375 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003376 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003377 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003378#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003379 if (codeflags & CO_GENERATOR_ALLOWED) {
3380 result = 1;
3381 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3382 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003383#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003384 }
3385 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003386}
3387
3388int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003389Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003390{
Guido van Rossumb209a111997-04-29 18:18:01 +00003391 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003392 if (f == NULL)
3393 return 0;
3394 if (!PyFile_SoftSpace(f, 0))
3395 return 0;
3396 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003397}
3398
Guido van Rossum3f5da241990-12-20 15:06:42 +00003399
Guido van Rossum681d79a1995-07-18 14:51:37 +00003400/* External interface to call any callable object.
3401 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003402
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003403#undef PyEval_CallObject
3404/* for backward compatibility: export this interface */
3405
Guido van Rossumb209a111997-04-29 18:18:01 +00003406PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003407PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003408{
Guido van Rossumb209a111997-04-29 18:18:01 +00003409 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003410}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003411#define PyEval_CallObject(func,arg) \
3412 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003413
Guido van Rossumb209a111997-04-29 18:18:01 +00003414PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003415PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003416{
Jeremy Hylton52820442001-01-03 23:52:36 +00003417 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003418
3419 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003420 arg = PyTuple_New(0);
3421 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003422 PyErr_SetString(PyExc_TypeError,
3423 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003424 return NULL;
3425 }
3426 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003427 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003428
Guido van Rossumb209a111997-04-29 18:18:01 +00003429 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003430 PyErr_SetString(PyExc_TypeError,
3431 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003432 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003433 return NULL;
3434 }
3435
Tim Peters6d6c1a32001-08-02 04:15:00 +00003436 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003437 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003438 return result;
3439}
3440
Tim Peters6d6c1a32001-08-02 04:15:00 +00003441char *
3442PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003443{
3444 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003445 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003446 else if (PyFunction_Check(func))
3447 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3448 else if (PyCFunction_Check(func))
3449 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3450 else if (PyClass_Check(func))
3451 return PyString_AsString(((PyClassObject*)func)->cl_name);
3452 else if (PyInstance_Check(func)) {
3453 return PyString_AsString(
3454 ((PyInstanceObject*)func)->in_class->cl_name);
3455 } else {
3456 return func->ob_type->tp_name;
3457 }
3458}
3459
Tim Peters6d6c1a32001-08-02 04:15:00 +00003460char *
3461PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003462{
3463 if (PyMethod_Check(func))
3464 return "()";
3465 else if (PyFunction_Check(func))
3466 return "()";
3467 else if (PyCFunction_Check(func))
3468 return "()";
3469 else if (PyClass_Check(func))
3470 return " constructor";
3471 else if (PyInstance_Check(func)) {
3472 return " instance";
3473 } else {
3474 return " object";
3475 }
3476}
3477
Jeremy Hylton52820442001-01-03 23:52:36 +00003478#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3479
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003480static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003481err_args(PyObject *func, int flags, int nargs)
3482{
3483 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003484 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003485 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003486 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003487 nargs);
3488 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003489 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003490 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003491 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003492 nargs);
3493}
3494
Nicholas Bastind858a772004-06-25 23:31:06 +00003495#define C_TRACE(call) \
3496if (tstate->use_tracing && tstate->c_profilefunc) { \
3497 if (call_trace(tstate->c_profilefunc, \
3498 tstate->c_profileobj, \
3499 tstate->frame, PyTrace_C_CALL, \
3500 func)) \
3501 { return NULL; } \
3502 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003503 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003504 if (x == NULL) { \
3505 if (call_trace (tstate->c_profilefunc, \
3506 tstate->c_profileobj, \
3507 tstate->frame, PyTrace_C_EXCEPTION, \
3508 func)) \
3509 { return NULL; } \
3510 } else { \
3511 if (call_trace(tstate->c_profilefunc, \
3512 tstate->c_profileobj, \
3513 tstate->frame, PyTrace_C_RETURN, \
3514 func)) \
3515 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003516 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003517 } \
3518} else { \
3519 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003520 }
3521
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003522static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003523call_function(PyObject ***pp_stack, int oparg
3524#ifdef WITH_TSC
3525 , uint64* pintr0, uint64* pintr1
3526#endif
3527 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003528{
3529 int na = oparg & 0xff;
3530 int nk = (oparg>>8) & 0xff;
3531 int n = na + 2 * nk;
3532 PyObject **pfunc = (*pp_stack) - n - 1;
3533 PyObject *func = *pfunc;
3534 PyObject *x, *w;
3535
Jeremy Hylton985eba52003-02-05 23:13:00 +00003536 /* Always dispatch PyCFunction first, because these are
3537 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003538 */
3539 if (PyCFunction_Check(func) && nk == 0) {
3540 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003541 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003542
3543 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003544 if (flags & (METH_NOARGS | METH_O)) {
3545 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3546 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003547 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003548 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003549 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003550 else if (flags & METH_O && na == 1) {
3551 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003552 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003553 Py_DECREF(arg);
3554 }
3555 else {
3556 err_args(func, flags, na);
3557 x = NULL;
3558 }
3559 }
3560 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003561 PyObject *callargs;
3562 callargs = load_args(pp_stack, na);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003563#ifdef WITH_TSC
3564 rdtscll(*pintr0);
3565#endif
Nicholas Bastind858a772004-06-25 23:31:06 +00003566 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003567#ifdef WITH_TSC
3568 rdtscll(*pintr1);
3569#endif
Tim Peters8a5c3c72004-04-05 19:36:21 +00003570 Py_XDECREF(callargs);
3571 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003572 } else {
3573 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3574 /* optimize access to bound methods */
3575 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003576 PCALL(PCALL_METHOD);
3577 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003578 Py_INCREF(self);
3579 func = PyMethod_GET_FUNCTION(func);
3580 Py_INCREF(func);
3581 Py_DECREF(*pfunc);
3582 *pfunc = self;
3583 na++;
3584 n++;
3585 } else
3586 Py_INCREF(func);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003587#ifdef WITH_TSC
3588 rdtscll(*pintr0);
3589#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003590 if (PyFunction_Check(func))
3591 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003592 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003593 x = do_call(func, pp_stack, na, nk);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003594#ifdef WITH_TSC
3595 rdtscll(*pintr1);
3596#endif
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003597 Py_DECREF(func);
3598 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003599
Jeremy Hylton985eba52003-02-05 23:13:00 +00003600 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003601 while ((*pp_stack) > pfunc) {
3602 w = EXT_POP(*pp_stack);
3603 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003604 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003605 }
3606 return x;
3607}
3608
Jeremy Hylton192690e2002-08-16 18:36:11 +00003609/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003610 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003611 For the simplest case -- a function that takes only positional
3612 arguments and is called with only positional arguments -- it
3613 inlines the most primitive frame setup code from
3614 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3615 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003616*/
3617
3618static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003619fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003620{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003621 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003622 PyObject *globals = PyFunction_GET_GLOBALS(func);
3623 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3624 PyObject **d = NULL;
3625 int nd = 0;
3626
Jeremy Hylton985eba52003-02-05 23:13:00 +00003627 PCALL(PCALL_FUNCTION);
3628 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003629 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003630 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3631 PyFrameObject *f;
3632 PyObject *retval = NULL;
3633 PyThreadState *tstate = PyThreadState_GET();
3634 PyObject **fastlocals, **stack;
3635 int i;
3636
3637 PCALL(PCALL_FASTER_FUNCTION);
3638 assert(globals != NULL);
3639 /* XXX Perhaps we should create a specialized
3640 PyFrame_New() that doesn't take locals, but does
3641 take builtins without sanity checking them.
3642 */
3643 f = PyFrame_New(tstate, co, globals, NULL);
3644 if (f == NULL)
3645 return NULL;
3646
3647 fastlocals = f->f_localsplus;
3648 stack = (*pp_stack) - n;
3649
3650 for (i = 0; i < n; i++) {
3651 Py_INCREF(*stack);
3652 fastlocals[i] = *stack++;
3653 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003654 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003655 assert(tstate != NULL);
3656 ++tstate->recursion_depth;
3657 Py_DECREF(f);
3658 --tstate->recursion_depth;
3659 return retval;
3660 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003661 if (argdefs != NULL) {
3662 d = &PyTuple_GET_ITEM(argdefs, 0);
3663 nd = ((PyTupleObject *)argdefs)->ob_size;
3664 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003665 return PyEval_EvalCodeEx(co, globals,
3666 (PyObject *)NULL, (*pp_stack)-n, na,
3667 (*pp_stack)-2*nk, nk, d, nd,
3668 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003669}
3670
3671static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003672update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3673 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003674{
3675 PyObject *kwdict = NULL;
3676 if (orig_kwdict == NULL)
3677 kwdict = PyDict_New();
3678 else {
3679 kwdict = PyDict_Copy(orig_kwdict);
3680 Py_DECREF(orig_kwdict);
3681 }
3682 if (kwdict == NULL)
3683 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003684 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003685 int err;
3686 PyObject *value = EXT_POP(*pp_stack);
3687 PyObject *key = EXT_POP(*pp_stack);
3688 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003689 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003690 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003691 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003692 PyEval_GetFuncName(func),
3693 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003694 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003695 Py_DECREF(key);
3696 Py_DECREF(value);
3697 Py_DECREF(kwdict);
3698 return NULL;
3699 }
3700 err = PyDict_SetItem(kwdict, key, value);
3701 Py_DECREF(key);
3702 Py_DECREF(value);
3703 if (err) {
3704 Py_DECREF(kwdict);
3705 return NULL;
3706 }
3707 }
3708 return kwdict;
3709}
3710
3711static PyObject *
3712update_star_args(int nstack, int nstar, PyObject *stararg,
3713 PyObject ***pp_stack)
3714{
3715 PyObject *callargs, *w;
3716
3717 callargs = PyTuple_New(nstack + nstar);
3718 if (callargs == NULL) {
3719 return NULL;
3720 }
3721 if (nstar) {
3722 int i;
3723 for (i = 0; i < nstar; i++) {
3724 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3725 Py_INCREF(a);
3726 PyTuple_SET_ITEM(callargs, nstack + i, a);
3727 }
3728 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003729 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003730 w = EXT_POP(*pp_stack);
3731 PyTuple_SET_ITEM(callargs, nstack, w);
3732 }
3733 return callargs;
3734}
3735
3736static PyObject *
3737load_args(PyObject ***pp_stack, int na)
3738{
3739 PyObject *args = PyTuple_New(na);
3740 PyObject *w;
3741
3742 if (args == NULL)
3743 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003744 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003745 w = EXT_POP(*pp_stack);
3746 PyTuple_SET_ITEM(args, na, w);
3747 }
3748 return args;
3749}
3750
3751static PyObject *
3752do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3753{
3754 PyObject *callargs = NULL;
3755 PyObject *kwdict = NULL;
3756 PyObject *result = NULL;
3757
3758 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003759 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003760 if (kwdict == NULL)
3761 goto call_fail;
3762 }
3763 callargs = load_args(pp_stack, na);
3764 if (callargs == NULL)
3765 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003766#ifdef CALL_PROFILE
3767 /* At this point, we have to look at the type of func to
3768 update the call stats properly. Do it here so as to avoid
3769 exposing the call stats machinery outside ceval.c
3770 */
3771 if (PyFunction_Check(func))
3772 PCALL(PCALL_FUNCTION);
3773 else if (PyMethod_Check(func))
3774 PCALL(PCALL_METHOD);
3775 else if (PyType_Check(func))
3776 PCALL(PCALL_TYPE);
3777 else
3778 PCALL(PCALL_OTHER);
3779#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003780 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003781 call_fail:
3782 Py_XDECREF(callargs);
3783 Py_XDECREF(kwdict);
3784 return result;
3785}
3786
3787static PyObject *
3788ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3789{
3790 int nstar = 0;
3791 PyObject *callargs = NULL;
3792 PyObject *stararg = NULL;
3793 PyObject *kwdict = NULL;
3794 PyObject *result = NULL;
3795
3796 if (flags & CALL_FLAG_KW) {
3797 kwdict = EXT_POP(*pp_stack);
3798 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003799 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003800 "%s%s argument after ** "
3801 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003802 PyEval_GetFuncName(func),
3803 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003804 goto ext_call_fail;
3805 }
3806 }
3807 if (flags & CALL_FLAG_VAR) {
3808 stararg = EXT_POP(*pp_stack);
3809 if (!PyTuple_Check(stararg)) {
3810 PyObject *t = NULL;
3811 t = PySequence_Tuple(stararg);
3812 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003813 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3814 PyErr_Format(PyExc_TypeError,
3815 "%s%s argument after * "
3816 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003817 PyEval_GetFuncName(func),
3818 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003819 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003820 goto ext_call_fail;
3821 }
3822 Py_DECREF(stararg);
3823 stararg = t;
3824 }
3825 nstar = PyTuple_GET_SIZE(stararg);
3826 }
3827 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003828 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003829 if (kwdict == NULL)
3830 goto ext_call_fail;
3831 }
3832 callargs = update_star_args(na, nstar, stararg, pp_stack);
3833 if (callargs == NULL)
3834 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003835#ifdef CALL_PROFILE
3836 /* At this point, we have to look at the type of func to
3837 update the call stats properly. Do it here so as to avoid
3838 exposing the call stats machinery outside ceval.c
3839 */
3840 if (PyFunction_Check(func))
3841 PCALL(PCALL_FUNCTION);
3842 else if (PyMethod_Check(func))
3843 PCALL(PCALL_METHOD);
3844 else if (PyType_Check(func))
3845 PCALL(PCALL_TYPE);
3846 else
3847 PCALL(PCALL_OTHER);
3848#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003849 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003850 ext_call_fail:
3851 Py_XDECREF(callargs);
3852 Py_XDECREF(kwdict);
3853 Py_XDECREF(stararg);
3854 return result;
3855}
3856
Tim Peterscb479e72001-12-16 19:11:44 +00003857/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3858 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3859 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3860*/
Tim Petersb5196382001-12-16 19:44:20 +00003861/* Note: If v is NULL, return success without storing into *pi. This
3862 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3863 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003864*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003865int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003866_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003867{
Tim Petersb5196382001-12-16 19:44:20 +00003868 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003869 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003870 if (PyInt_Check(v)) {
3871 x = PyInt_AsLong(v);
3872 } else if (PyLong_Check(v)) {
3873 x = PyLong_AsLong(v);
3874 if (x==-1 && PyErr_Occurred()) {
3875 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003876 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003877
Guido van Rossumac7be682001-01-17 15:42:30 +00003878 if (!PyErr_ExceptionMatches(
3879 PyExc_OverflowError)) {
3880 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003881 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003882 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003883 }
3884
Guido van Rossumac7be682001-01-17 15:42:30 +00003885 /* Clear the OverflowError */
3886 PyErr_Clear();
3887
3888 /* It's an overflow error, so we need to
3889 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003890 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003891 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003892
3893 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003894 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003895 if (long_zero == NULL)
3896 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003897
3898 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003899 cmp = PyObject_RichCompareBool(v, long_zero,
3900 Py_GT);
3901 Py_DECREF(long_zero);
3902 if (cmp < 0)
3903 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003904 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003905 x = INT_MAX;
3906 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003907 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003908 }
3909 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003910 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003911 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003912 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003913 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003914 /* Truncate -- very long indices are truncated anyway */
3915 if (x > INT_MAX)
3916 x = INT_MAX;
3917 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003918 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003919 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003920 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003921 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003922}
3923
Guido van Rossum50d756e2001-08-18 17:43:36 +00003924#undef ISINT
3925#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3926
Guido van Rossumb209a111997-04-29 18:18:01 +00003927static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003928apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003929{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003930 PyTypeObject *tp = u->ob_type;
3931 PySequenceMethods *sq = tp->tp_as_sequence;
3932
3933 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3934 int ilow = 0, ihigh = INT_MAX;
3935 if (!_PyEval_SliceIndex(v, &ilow))
3936 return NULL;
3937 if (!_PyEval_SliceIndex(w, &ihigh))
3938 return NULL;
3939 return PySequence_GetSlice(u, ilow, ihigh);
3940 }
3941 else {
3942 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003943 if (slice != NULL) {
3944 PyObject *res = PyObject_GetItem(u, slice);
3945 Py_DECREF(slice);
3946 return res;
3947 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003948 else
3949 return NULL;
3950 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003951}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003952
3953static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003954assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3955 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003956{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003957 PyTypeObject *tp = u->ob_type;
3958 PySequenceMethods *sq = tp->tp_as_sequence;
3959
3960 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3961 int ilow = 0, ihigh = INT_MAX;
3962 if (!_PyEval_SliceIndex(v, &ilow))
3963 return -1;
3964 if (!_PyEval_SliceIndex(w, &ihigh))
3965 return -1;
3966 if (x == NULL)
3967 return PySequence_DelSlice(u, ilow, ihigh);
3968 else
3969 return PySequence_SetSlice(u, ilow, ihigh, x);
3970 }
3971 else {
3972 PyObject *slice = PySlice_New(v, w, NULL);
3973 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003974 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003975 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003976 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003977 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003978 res = PyObject_DelItem(u, slice);
3979 Py_DECREF(slice);
3980 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003981 }
3982 else
3983 return -1;
3984 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003985}
3986
Guido van Rossumb209a111997-04-29 18:18:01 +00003987static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003988cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003989{
Guido van Rossumac7be682001-01-17 15:42:30 +00003990 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003991 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003992 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003993 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003994 break;
3995 case PyCmp_IS_NOT:
3996 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003997 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003998 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003999 res = PySequence_Contains(w, v);
4000 if (res < 0)
4001 return NULL;
4002 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004003 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004004 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004005 if (res < 0)
4006 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004007 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004008 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004009 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004010 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004011 break;
4012 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004013 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004014 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004015 v = res ? Py_True : Py_False;
4016 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004017 return v;
4018}
4019
Thomas Wouters52152252000-08-17 22:55:00 +00004020static PyObject *
4021import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004022{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004023 PyObject *x;
4024
4025 x = PyObject_GetAttr(v, name);
4026 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004027 PyErr_Format(PyExc_ImportError,
4028 "cannot import name %.230s",
4029 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004030 }
Thomas Wouters52152252000-08-17 22:55:00 +00004031 return x;
4032}
Guido van Rossumac7be682001-01-17 15:42:30 +00004033
Thomas Wouters52152252000-08-17 22:55:00 +00004034static int
4035import_all_from(PyObject *locals, PyObject *v)
4036{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004037 PyObject *all = PyObject_GetAttrString(v, "__all__");
4038 PyObject *dict, *name, *value;
4039 int skip_leading_underscores = 0;
4040 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004041
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004042 if (all == NULL) {
4043 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4044 return -1; /* Unexpected error */
4045 PyErr_Clear();
4046 dict = PyObject_GetAttrString(v, "__dict__");
4047 if (dict == NULL) {
4048 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4049 return -1;
4050 PyErr_SetString(PyExc_ImportError,
4051 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004052 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004053 }
4054 all = PyMapping_Keys(dict);
4055 Py_DECREF(dict);
4056 if (all == NULL)
4057 return -1;
4058 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004059 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004060
4061 for (pos = 0, err = 0; ; pos++) {
4062 name = PySequence_GetItem(all, pos);
4063 if (name == NULL) {
4064 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4065 err = -1;
4066 else
4067 PyErr_Clear();
4068 break;
4069 }
4070 if (skip_leading_underscores &&
4071 PyString_Check(name) &&
4072 PyString_AS_STRING(name)[0] == '_')
4073 {
4074 Py_DECREF(name);
4075 continue;
4076 }
4077 value = PyObject_GetAttr(v, name);
4078 if (value == NULL)
4079 err = -1;
4080 else
4081 err = PyDict_SetItem(locals, name, value);
4082 Py_DECREF(name);
4083 Py_XDECREF(value);
4084 if (err != 0)
4085 break;
4086 }
4087 Py_DECREF(all);
4088 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004089}
4090
Guido van Rossumb209a111997-04-29 18:18:01 +00004091static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004092build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004093{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004094 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004095
4096 if (PyDict_Check(methods))
4097 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004098 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004099 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004100 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4101 base = PyTuple_GET_ITEM(bases, 0);
4102 metaclass = PyObject_GetAttrString(base, "__class__");
4103 if (metaclass == NULL) {
4104 PyErr_Clear();
4105 metaclass = (PyObject *)base->ob_type;
4106 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004107 }
4108 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004109 else {
4110 PyObject *g = PyEval_GetGlobals();
4111 if (g != NULL && PyDict_Check(g))
4112 metaclass = PyDict_GetItemString(g, "__metaclass__");
4113 if (metaclass == NULL)
4114 metaclass = (PyObject *) &PyClass_Type;
4115 Py_INCREF(metaclass);
4116 }
4117 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4118 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004119 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4120 /* A type error here likely means that the user passed
4121 in a base that was not a class (such the random module
4122 instead of the random.random type). Help them out with
4123 a more informative error message */
4124 PyErr_SetString(PyExc_TypeError,
4125 "Error when calling the metaclass.\n" \
4126 "Make sure the base arguments are valid.");
4127 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004128 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004129}
4130
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004131static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004132exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4133 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004134{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004135 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004136 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004137 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004138
Guido van Rossumb209a111997-04-29 18:18:01 +00004139 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4140 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004141 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004142 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004143 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004144 locals = PyTuple_GetItem(prog, 2);
4145 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004146 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004147 if (globals == Py_None) {
4148 globals = PyEval_GetGlobals();
4149 if (locals == Py_None) {
4150 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004151 plain = 1;
4152 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004153 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004154 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004155 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004156 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004157 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004158 !PyCode_Check(prog) &&
4159 !PyFile_Check(prog)) {
4160 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004161 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004162 return -1;
4163 }
Fred Drake661ea262000-10-24 19:57:45 +00004164 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004165 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004166 "exec: arg 2 must be a dictionary or None");
4167 return -1;
4168 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004169 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004170 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004171 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004172 return -1;
4173 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004174 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004175 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004176 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004177 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4178 PyErr_SetString(PyExc_TypeError,
4179 "code object passed to exec may not contain free variables");
4180 return -1;
4181 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004182 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004183 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004184 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004185 FILE *fp = PyFile_AsFile(prog);
4186 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004187 PyCompilerFlags cf;
4188 cf.cf_flags = 0;
4189 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004190 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004191 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004192 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004193 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004194 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004195 }
4196 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004197 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004198 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004199 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004200 cf.cf_flags = 0;
4201#ifdef Py_USING_UNICODE
4202 if (PyUnicode_Check(prog)) {
4203 tmp = PyUnicode_AsUTF8String(prog);
4204 if (tmp == NULL)
4205 return -1;
4206 prog = tmp;
4207 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4208 }
4209#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004210 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004211 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004212 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004213 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004214 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004215 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004216 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004217 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004218 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004219 if (plain)
4220 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004221 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004222 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004223 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004224 return 0;
4225}
Guido van Rossum24c13741995-02-14 09:42:43 +00004226
Guido van Rossumac7be682001-01-17 15:42:30 +00004227static void
Paul Prescode68140d2000-08-30 20:25:01 +00004228format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4229{
4230 char *obj_str;
4231
4232 if (!obj)
4233 return;
4234
4235 obj_str = PyString_AsString(obj);
4236 if (!obj_str)
4237 return;
4238
4239 PyErr_Format(exc, format_str, obj_str);
4240}
Guido van Rossum950361c1997-01-24 13:49:28 +00004241
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004242static PyObject *
4243string_concatenate(PyObject *v, PyObject *w,
4244 PyFrameObject *f, unsigned char *next_instr)
4245{
4246 /* This function implements 'variable += expr' when both arguments
4247 are strings. */
4248
4249 if (v->ob_refcnt == 2) {
4250 /* In the common case, there are 2 references to the value
4251 * stored in 'variable' when the += is performed: one on the
4252 * value stack (in 'v') and one still stored in the 'variable'.
4253 * We try to delete the variable now to reduce the refcnt to 1.
4254 */
4255 switch (*next_instr) {
4256 case STORE_FAST:
4257 {
4258 int oparg = PEEKARG();
4259 PyObject **fastlocals = f->f_localsplus;
4260 if (GETLOCAL(oparg) == v)
4261 SETLOCAL(oparg, NULL);
4262 break;
4263 }
4264 case STORE_DEREF:
4265 {
4266 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4267 PyObject *c = freevars[PEEKARG()];
4268 if (PyCell_GET(c) == v)
4269 PyCell_Set(c, NULL);
4270 break;
4271 }
4272 case STORE_NAME:
4273 {
4274 PyObject *names = f->f_code->co_names;
4275 PyObject *name = GETITEM(names, PEEKARG());
4276 PyObject *locals = f->f_locals;
4277 if (PyDict_CheckExact(locals) &&
4278 PyDict_GetItem(locals, name) == v) {
4279 if (PyDict_DelItem(locals, name) != 0) {
4280 PyErr_Clear();
4281 }
4282 }
4283 break;
4284 }
4285 }
4286 }
4287
Armin Rigo618fbf52004-08-07 20:58:32 +00004288 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004289 /* Now we own the last reference to 'v', so we can resize it
4290 * in-place.
4291 */
4292 int v_len = PyString_GET_SIZE(v);
4293 int w_len = PyString_GET_SIZE(w);
4294 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4295 /* XXX if _PyString_Resize() fails, 'v' has been
4296 * deallocated so it cannot be put back into 'variable'.
4297 * The MemoryError is raised when there is no value in
4298 * 'variable', which might (very remotely) be a cause
4299 * of incompatibilities.
4300 */
4301 return NULL;
4302 }
4303 /* copy 'w' into the newly allocated area of 'v' */
4304 memcpy(PyString_AS_STRING(v) + v_len,
4305 PyString_AS_STRING(w), w_len);
4306 return v;
4307 }
4308 else {
4309 /* When in-place resizing is not an option. */
4310 PyString_Concat(&v, w);
4311 return v;
4312 }
4313}
4314
Guido van Rossum950361c1997-01-24 13:49:28 +00004315#ifdef DYNAMIC_EXECUTION_PROFILE
4316
Skip Montanarof118cb12001-10-15 20:51:38 +00004317static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004318getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004319{
4320 int i;
4321 PyObject *l = PyList_New(256);
4322 if (l == NULL) return NULL;
4323 for (i = 0; i < 256; i++) {
4324 PyObject *x = PyInt_FromLong(a[i]);
4325 if (x == NULL) {
4326 Py_DECREF(l);
4327 return NULL;
4328 }
4329 PyList_SetItem(l, i, x);
4330 }
4331 for (i = 0; i < 256; i++)
4332 a[i] = 0;
4333 return l;
4334}
4335
4336PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004337_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004338{
4339#ifndef DXPAIRS
4340 return getarray(dxp);
4341#else
4342 int i;
4343 PyObject *l = PyList_New(257);
4344 if (l == NULL) return NULL;
4345 for (i = 0; i < 257; i++) {
4346 PyObject *x = getarray(dxpairs[i]);
4347 if (x == NULL) {
4348 Py_DECREF(l);
4349 return NULL;
4350 }
4351 PyList_SetItem(l, i, x);
4352 }
4353 return l;
4354#endif
4355}
4356
4357#endif