blob: 749291d577032a3ec25810647aaaf11281946e38 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Guido van Rossumb209a111997-04-29 18:18:01 +00009#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000010
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000012#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000013#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000015#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Guido van Rossumc6004111993-11-05 10:22:19 +000017#include <ctype.h>
18
Martin v. Löwis87fa7852004-08-29 15:51:52 +000019#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000020
21#define READ_TIMESTAMP(var)
22
23#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000024
25typedef unsigned long long uint64;
26
Michael W. Hudson800ba232004-08-12 18:19:17 +000027#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
28 section should work for GCC on any PowerPC platform,
29 irrespective of OS. POWER? Who knows :-) */
30
Michael W. Hudson75eabd22005-01-18 15:56:11 +000031#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000032
33static void
34ppc_getcounter(uint64 *v)
35{
36 register unsigned long tbu, tb, tbu2;
37
38 loop:
39 asm volatile ("mftbu %0" : "=r" (tbu) );
40 asm volatile ("mftb %0" : "=r" (tb) );
41 asm volatile ("mftbu %0" : "=r" (tbu2));
42 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
43
44 /* The slightly peculiar way of writing the next lines is
45 compiled better by GCC than any other way I tried. */
46 ((long*)(v))[0] = tbu;
47 ((long*)(v))[1] = tb;
48}
49
Michael W. Hudson75eabd22005-01-18 15:56:11 +000050#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000051
Michael W. Hudson75eabd22005-01-18 15:56:11 +000052#define READ_TIMESTAMP(val) \
53 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000054
55#endif
56
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000057void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
58 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
59{
60 uint64 intr, inst, loop;
61 PyThreadState *tstate = PyThreadState_Get();
62 if (!tstate->interp->tscdump)
63 return;
64 intr = intr1 - intr0;
65 inst = inst1 - inst0 - intr;
66 loop = loop1 - loop0 - intr;
67 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
68 opcode, ticked, inst, loop);
69}
Michael W. Hudson800ba232004-08-12 18:19:17 +000070
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000071#endif
72
Guido van Rossum04691fc1992-08-12 15:35:34 +000073/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000074/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000075
Guido van Rossum408027e1996-12-30 16:17:54 +000076#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000077/* For debugging the interpreter: */
78#define LLTRACE 1 /* Low-level trace feature */
79#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000080#endif
81
Jeremy Hylton52820442001-01-03 23:52:36 +000082typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000083
Guido van Rossum374a9221991-04-04 10:40:29 +000084/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000085#ifdef WITH_TSC
86static PyObject *call_function(PyObject ***, int, uint64*, uint64*);
87#else
Jeremy Hyltone8c04322002-08-16 17:47:26 +000088static PyObject *call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#endif
Jeremy Hylton52820442001-01-03 23:52:36 +000090static PyObject *fast_function(PyObject *, PyObject ***, int, int, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000091static PyObject *do_call(PyObject *, PyObject ***, int, int);
92static PyObject *ext_do_call(PyObject *, PyObject ***, int, int, int);
Guido van Rossumac7be682001-01-17 15:42:30 +000093static PyObject *update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
Ka-Ping Yee20579702001-01-15 22:14:16 +000094static PyObject *update_star_args(int, int, PyObject *, PyObject ***);
Jeremy Hylton52820442001-01-03 23:52:36 +000095static PyObject *load_args(PyObject ***, int);
96#define CALL_FLAG_VAR 1
97#define CALL_FLAG_KW 2
98
Guido van Rossum0a066c01992-03-27 17:29:15 +000099#ifdef LLTRACE
Tim Petersdbd9ba62000-07-09 03:09:57 +0000100static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000101#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000102static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
103 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +0000104static void call_trace_protected(Py_tracefunc, PyObject *,
105 PyFrameObject *, int);
Fred Drake5755ce62001-06-27 19:19:46 +0000106static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000107static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000108 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000109
Tim Petersdbd9ba62000-07-09 03:09:57 +0000110static PyObject *apply_slice(PyObject *, PyObject *, PyObject *);
111static int assign_slice(PyObject *, PyObject *,
112 PyObject *, PyObject *);
113static PyObject *cmp_outcome(int, PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000114static PyObject *import_from(PyObject *, PyObject *);
115static int import_all_from(PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000116static PyObject *build_class(PyObject *, PyObject *, PyObject *);
117static int exec_statement(PyFrameObject *,
118 PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000119static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
120static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000121static void format_exc_check_arg(PyObject *, char *, PyObject *);
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000122static PyObject *string_concatenate(PyObject *, PyObject *,
123 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000124
Paul Prescode68140d2000-08-30 20:25:01 +0000125#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000126 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000127#define GLOBAL_NAME_ERROR_MSG \
128 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000129#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000130 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000131#define UNBOUNDFREE_ERROR_MSG \
132 "free variable '%.200s' referenced before assignment" \
133 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000134
Guido van Rossum950361c1997-01-24 13:49:28 +0000135/* Dynamic execution profile */
136#ifdef DYNAMIC_EXECUTION_PROFILE
137#ifdef DXPAIRS
138static long dxpairs[257][256];
139#define dxp dxpairs[256]
140#else
141static long dxp[256];
142#endif
143#endif
144
Jeremy Hylton985eba52003-02-05 23:13:00 +0000145/* Function call profile */
146#ifdef CALL_PROFILE
147#define PCALL_NUM 11
148static int pcall[PCALL_NUM];
149
150#define PCALL_ALL 0
151#define PCALL_FUNCTION 1
152#define PCALL_FAST_FUNCTION 2
153#define PCALL_FASTER_FUNCTION 3
154#define PCALL_METHOD 4
155#define PCALL_BOUND_METHOD 5
156#define PCALL_CFUNCTION 6
157#define PCALL_TYPE 7
158#define PCALL_GENERATOR 8
159#define PCALL_OTHER 9
160#define PCALL_POP 10
161
162/* Notes about the statistics
163
164 PCALL_FAST stats
165
166 FAST_FUNCTION means no argument tuple needs to be created.
167 FASTER_FUNCTION means that the fast-path frame setup code is used.
168
169 If there is a method call where the call can be optimized by changing
170 the argument tuple and calling the function directly, it gets recorded
171 twice.
172
173 As a result, the relationship among the statistics appears to be
174 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
175 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
176 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
177 PCALL_METHOD > PCALL_BOUND_METHOD
178*/
179
180#define PCALL(POS) pcall[POS]++
181
182PyObject *
183PyEval_GetCallStats(PyObject *self)
184{
Tim Peters8a5c3c72004-04-05 19:36:21 +0000185 return Py_BuildValue("iiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000186 pcall[0], pcall[1], pcall[2], pcall[3],
187 pcall[4], pcall[5], pcall[6], pcall[7],
188 pcall[8], pcall[9]);
189}
190#else
191#define PCALL(O)
192
193PyObject *
194PyEval_GetCallStats(PyObject *self)
195{
196 Py_INCREF(Py_None);
197 return Py_None;
198}
199#endif
200
Tim Peters5ca576e2001-06-18 22:08:13 +0000201
Guido van Rossume59214e1994-08-30 08:01:59 +0000202#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000203
Guido van Rossum2571cc81999-04-07 16:07:23 +0000204#ifndef DONT_HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000205#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000206#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000207#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000208
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000209static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000210static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000211
Tim Peters7f468f22004-10-11 02:40:51 +0000212int
213PyEval_ThreadsInitialized(void)
214{
215 return interpreter_lock != 0;
216}
217
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000221 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000222 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000223 interpreter_lock = PyThread_allocate_lock();
224 PyThread_acquire_lock(interpreter_lock, 1);
225 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000227
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232}
233
234void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000237 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238}
239
240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000242{
243 if (tstate == NULL)
244 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000245 /* Check someone has called PyEval_InitThreads() to create the lock */
246 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000248 if (PyThreadState_Swap(tstate) != NULL)
249 Py_FatalError(
250 "PyEval_AcquireThread: non-NULL old thread state");
251}
252
253void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000255{
256 if (tstate == NULL)
257 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
258 if (PyThreadState_Swap(NULL) != tstate)
259 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000260 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000261}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000262
263/* This function is called from PyOS_AfterFork to ensure that newly
264 created child processes don't hold locks referring to threads which
265 are not running in the child process. (This could also be done using
266 pthread_atfork mechanism, at least for the pthreads implementation.) */
267
268void
269PyEval_ReInitThreads(void)
270{
271 if (!interpreter_lock)
272 return;
273 /*XXX Can't use PyThread_free_lock here because it does too
274 much error-checking. Doing this cleanly would require
275 adding a new function to each thread_*.h. Instead, just
276 create a new lock and waste a little bit of memory */
277 interpreter_lock = PyThread_allocate_lock();
278 PyThread_acquire_lock(interpreter_lock, 1);
279 main_thread = PyThread_get_thread_ident();
280}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281#endif
282
Guido van Rossumff4949e1992-08-05 19:58:53 +0000283/* Functions save_thread and restore_thread are always defined so
284 dynamically loaded modules needn't be compiled separately for use
285 with and without threads: */
286
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000287PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000288PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000289{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000290 PyThreadState *tstate = PyThreadState_Swap(NULL);
291 if (tstate == NULL)
292 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000293#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000294 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000295 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000297 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298}
299
300void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000301PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000303 if (tstate == NULL)
304 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000305#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000307 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000308 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310 }
311#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000312 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
314
315
Guido van Rossuma9672091994-09-14 13:31:22 +0000316/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
317 signal handlers or Mac I/O completion routines) can schedule calls
318 to a function to be called synchronously.
319 The synchronous function is called with one void* argument.
320 It should return 0 for success or -1 for failure -- failure should
321 be accompanied by an exception.
322
323 If registry succeeds, the registry function returns 0; if it fails
324 (e.g. due to too many pending calls) it returns -1 (without setting
325 an exception condition).
326
327 Note that because registry may occur from within signal handlers,
328 or other asynchronous events, calling malloc() is unsafe!
329
330#ifdef WITH_THREAD
331 Any thread can schedule pending calls, but only the main thread
332 will execute them.
333#endif
334
335 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
336 There are two possible race conditions:
337 (1) nested asynchronous registry calls;
338 (2) registry calls made while pending calls are being processed.
339 While (1) is very unlikely, (2) is a real possibility.
340 The current code is safe against (2), but not against (1).
341 The safety against (2) is derived from the fact that only one
342 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000343
Guido van Rossuma027efa1997-05-05 20:56:21 +0000344 XXX Darn! With the advent of thread state, we should have an array
345 of pending calls per thread in the thread state! Later...
346*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000347
Guido van Rossuma9672091994-09-14 13:31:22 +0000348#define NPENDINGCALLS 32
349static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000350 int (*func)(void *);
351 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000352} pendingcalls[NPENDINGCALLS];
353static volatile int pendingfirst = 0;
354static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000355static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000356
357int
Thomas Wouters334fb892000-07-25 12:56:38 +0000358Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000359{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000360 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000361 int i, j;
362 /* XXX Begin critical section */
363 /* XXX If you want this to be safe against nested
364 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000365 if (busy)
366 return -1;
367 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000368 i = pendinglast;
369 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000370 if (j == pendingfirst) {
371 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000372 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000373 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000374 pendingcalls[i].func = func;
375 pendingcalls[i].arg = arg;
376 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000377
378 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000379 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000380 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000381 /* XXX End critical section */
382 return 0;
383}
384
Guido van Rossum180d7b41994-09-29 09:45:57 +0000385int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000386Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000387{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000388 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000389#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000390 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000391 return 0;
392#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000393 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000394 return 0;
395 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000396 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000397 for (;;) {
398 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000399 int (*func)(void *);
400 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000401 i = pendingfirst;
402 if (i == pendinglast)
403 break; /* Queue empty */
404 func = pendingcalls[i].func;
405 arg = pendingcalls[i].arg;
406 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000407 if (func(arg) < 0) {
408 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000409 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000410 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000411 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000412 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000413 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000414 return 0;
415}
416
417
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000418/* The interpreter's recursion limit */
419
Guido van Rossum349ff6f2000-09-01 01:52:08 +0000420static int recursion_limit = 1000;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000421int _Py_CheckRecursionLimit = 1000;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000422
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000423int
424Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000425{
426 return recursion_limit;
427}
428
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000429void
430Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000431{
432 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000433 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000434}
435
Armin Rigo2b3eb402003-10-28 12:05:48 +0000436/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
437 if the recursion_depth reaches _Py_CheckRecursionLimit.
438 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
439 to guarantee that _Py_CheckRecursiveCall() is regularly called.
440 Without USE_STACKCHECK, there is no need for this. */
441int
442_Py_CheckRecursiveCall(char *where)
443{
444 PyThreadState *tstate = PyThreadState_GET();
445
446#ifdef USE_STACKCHECK
447 if (PyOS_CheckStack()) {
448 --tstate->recursion_depth;
449 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
450 return -1;
451 }
452#endif
453 if (tstate->recursion_depth > recursion_limit) {
454 --tstate->recursion_depth;
455 PyErr_Format(PyExc_RuntimeError,
456 "maximum recursion depth exceeded%s",
457 where);
458 return -1;
459 }
460 _Py_CheckRecursionLimit = recursion_limit;
461 return 0;
462}
463
Guido van Rossum374a9221991-04-04 10:40:29 +0000464/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000465enum why_code {
466 WHY_NOT = 0x0001, /* No error */
467 WHY_EXCEPTION = 0x0002, /* Exception occurred */
468 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
469 WHY_RETURN = 0x0008, /* 'return' statement */
470 WHY_BREAK = 0x0010, /* 'break' statement */
471 WHY_CONTINUE = 0x0020, /* 'continue' statement */
472 WHY_YIELD = 0x0040 /* 'yield' operator */
473};
Guido van Rossum374a9221991-04-04 10:40:29 +0000474
Raymond Hettinger7c958652004-04-06 10:11:10 +0000475static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000476static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000477
Skip Montanarod581d772002-09-03 20:10:45 +0000478/* for manipulating the thread switch and periodic "stuff" - used to be
479 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000480int _Py_CheckInterval = 100;
481volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000482
Guido van Rossumb209a111997-04-29 18:18:01 +0000483PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000484PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000485{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000486 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000487 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000488 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000489 (PyObject **)NULL, 0,
490 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000491 (PyObject **)NULL, 0,
492 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000493}
494
495
496/* Interpreter main loop */
497
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000498PyObject *
499PyEval_EvalFrame(PyFrameObject *f)
Guido van Rossum374a9221991-04-04 10:40:29 +0000500{
Guido van Rossum950361c1997-01-24 13:49:28 +0000501#ifdef DXPAIRS
502 int lastopcode = 0;
503#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000504 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000505 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000506 register int opcode; /* Current opcode */
507 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000508 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000509 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000510 register PyObject *x; /* Result object -- NULL if error */
511 register PyObject *v; /* Temporary objects popped off stack */
512 register PyObject *w;
513 register PyObject *u;
514 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000515 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000516 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000517 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000518 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000519 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000520
Tim Peters8a5c3c72004-04-05 19:36:21 +0000521 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000522
523 not (instr_lb <= current_bytecode_offset < instr_ub)
524
Tim Peters8a5c3c72004-04-05 19:36:21 +0000525 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000526 initial values are such as to make this false the first
527 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000528 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000529
Guido van Rossumd076c731998-10-07 19:42:25 +0000530 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000531 PyObject *names;
532 PyObject *consts;
Guido van Rossum96a42c81992-01-12 02:29:51 +0000533#ifdef LLTRACE
Guido van Rossumacbe8da1993-04-15 15:33:52 +0000534 int lltrace;
Guido van Rossum374a9221991-04-04 10:40:29 +0000535#endif
Guido van Rossum408027e1996-12-30 16:17:54 +0000536#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000537 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000538 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000539#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000540
Neal Norwitza81d2202002-07-14 00:27:26 +0000541/* Tuple access macros */
542
543#ifndef Py_DEBUG
544#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
545#else
546#define GETITEM(v, i) PyTuple_GetItem((v), (i))
547#endif
548
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000549#ifdef WITH_TSC
550/* Use Pentium timestamp counter to mark certain events:
551 inst0 -- beginning of switch statement for opcode dispatch
552 inst1 -- end of switch statement (may be skipped)
553 loop0 -- the top of the mainloop
554 loop1 -- place where control returns again to top of mainloop
555 (may be skipped)
556 intr1 -- beginning of long interruption
557 intr2 -- end of long interruption
558
559 Many opcodes call out to helper C functions. In some cases, the
560 time in those functions should be counted towards the time for the
561 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
562 calls another Python function; there's no point in charge all the
563 bytecode executed by the called function to the caller.
564
565 It's hard to make a useful judgement statically. In the presence
566 of operator overloading, it's impossible to tell if a call will
567 execute new Python code or not.
568
569 It's a case-by-case judgement. I'll use intr1 for the following
570 cases:
571
572 EXEC_STMT
573 IMPORT_STAR
574 IMPORT_FROM
575 CALL_FUNCTION (and friends)
576
577 */
578 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
579 int ticked = 0;
580
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000581 READ_TIMESTAMP(inst0);
582 READ_TIMESTAMP(inst1);
583 READ_TIMESTAMP(loop0);
584 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000585
586 /* shut up the compiler */
587 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000588#endif
589
Guido van Rossum374a9221991-04-04 10:40:29 +0000590/* Code access macros */
591
Guido van Rossumd076c731998-10-07 19:42:25 +0000592#define INSTR_OFFSET() (next_instr - first_instr)
Guido van Rossum374a9221991-04-04 10:40:29 +0000593#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000594#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000595#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000596#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000597#define JUMPBY(x) (next_instr += (x))
598
Raymond Hettingerf606f872003-03-16 03:11:04 +0000599/* OpCode prediction macros
600 Some opcodes tend to come in pairs thus making it possible to predict
601 the second code when the first is run. For example, COMPARE_OP is often
602 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
603 followed by a POP_TOP.
604
605 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000606 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000607 processor has a high likelihood of making its own successful branch
608 prediction which results in a nearly zero overhead transition to the
609 next opcode.
610
611 A successful prediction saves a trip through the eval-loop including
612 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000613
Tim Peters8a5c3c72004-04-05 19:36:21 +0000614 If collecting opcode statistics, turn off prediction so that
615 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000616 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000617*/
618
Raymond Hettingera7216982004-02-08 19:59:27 +0000619#ifdef DYNAMIC_EXECUTION_PROFILE
620#define PREDICT(op) if (0) goto PRED_##op
621#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000622#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000623#endif
624
Raymond Hettingerf606f872003-03-16 03:11:04 +0000625#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000626#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000627
Guido van Rossum374a9221991-04-04 10:40:29 +0000628/* Stack manipulation macros */
629
630#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
631#define EMPTY() (STACK_LEVEL() == 0)
632#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000633#define SECOND() (stack_pointer[-2])
634#define THIRD() (stack_pointer[-3])
635#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000636#define SET_TOP(v) (stack_pointer[-1] = (v))
637#define SET_SECOND(v) (stack_pointer[-2] = (v))
638#define SET_THIRD(v) (stack_pointer[-3] = (v))
639#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000640#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000641#define BASIC_PUSH(v) (*stack_pointer++ = (v))
642#define BASIC_POP() (*--stack_pointer)
643
Guido van Rossum96a42c81992-01-12 02:29:51 +0000644#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000645#define PUSH(v) { (void)(BASIC_PUSH(v), \
646 lltrace && prtrace(TOP(), "push")); \
647 assert(STACK_LEVEL() <= f->f_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000648#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000649#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
650 lltrace && prtrace(TOP(), "stackadj")); \
651 assert(STACK_LEVEL() <= f->f_stacksize); }
Guido van Rossum374a9221991-04-04 10:40:29 +0000652#else
653#define PUSH(v) BASIC_PUSH(v)
654#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000655#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000656#endif
657
Guido van Rossum681d79a1995-07-18 14:51:37 +0000658/* Local variable macros */
659
660#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000661
662/* The SETLOCAL() macro must not DECREF the local variable in-place and
663 then store the new value; it must copy the old value to a temporary
664 value, then store the new value, and then DECREF the temporary value.
665 This is because it is possible that during the DECREF the frame is
666 accessed by other code (e.g. a __del__ method or gc.collect()) and the
667 variable would be pointing to already-freed memory. */
668#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
669 GETLOCAL(i) = value; \
670 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000671
Guido van Rossuma027efa1997-05-05 20:56:21 +0000672/* Start of code */
673
Tim Peters5ca576e2001-06-18 22:08:13 +0000674 if (f == NULL)
675 return NULL;
676
Armin Rigo1d313ab2003-10-25 14:33:09 +0000677 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000678 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000679 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000680
Tim Peters5ca576e2001-06-18 22:08:13 +0000681 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000682
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000683 if (tstate->use_tracing) {
684 if (tstate->c_tracefunc != NULL) {
685 /* tstate->c_tracefunc, if defined, is a
686 function that will be called on *every* entry
687 to a code block. Its return value, if not
688 None, is a function that will be called at
689 the start of each executed line of code.
690 (Actually, the function must return itself
691 in order to continue tracing.) The trace
692 functions are called with three arguments:
693 a pointer to the current frame, a string
694 indicating why the function is called, and
695 an argument which depends on the situation.
696 The global trace function is also called
697 whenever an exception is detected. */
698 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
699 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000700 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000701 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000702 }
703 }
704 if (tstate->c_profilefunc != NULL) {
705 /* Similar for c_profilefunc, except it needn't
706 return itself and isn't called for "line" events */
707 if (call_trace(tstate->c_profilefunc,
708 tstate->c_profileobj,
709 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000710 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 }
713 }
714 }
715
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000716 co = f->f_code;
717 names = co->co_names;
718 consts = co->co_consts;
719 fastlocals = f->f_localsplus;
720 freevars = f->f_localsplus + f->f_nlocals;
Michael W. Hudsonecfeb7f2004-02-12 15:28:27 +0000721 first_instr = PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000722 /* An explanation is in order for the next line.
723
724 f->f_lasti now refers to the index of the last instruction
725 executed. You might think this was obvious from the name, but
726 this wasn't always true before 2.3! PyFrame_New now sets
727 f->f_lasti to -1 (i.e. the index *before* the first instruction)
728 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
729 does work. Promise. */
730 next_instr = first_instr + f->f_lasti + 1;
731 stack_pointer = f->f_stacktop;
732 assert(stack_pointer != NULL);
733 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
734
Tim Peters5ca576e2001-06-18 22:08:13 +0000735#ifdef LLTRACE
736 lltrace = PyDict_GetItemString(f->f_globals,"__lltrace__") != NULL;
737#endif
738#if defined(Py_DEBUG) || defined(LLTRACE)
739 filename = PyString_AsString(co->co_filename);
740#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000741
Guido van Rossum374a9221991-04-04 10:40:29 +0000742 why = WHY_NOT;
743 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000744 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000745 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000746
Guido van Rossum374a9221991-04-04 10:40:29 +0000747 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000748#ifdef WITH_TSC
749 if (inst1 == 0) {
750 /* Almost surely, the opcode executed a break
751 or a continue, preventing inst1 from being set
752 on the way out of the loop.
753 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000754 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000755 loop1 = inst1;
756 }
757 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
758 intr0, intr1);
759 ticked = 0;
760 inst1 = 0;
761 intr0 = 0;
762 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000763 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000764#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000765 assert(stack_pointer >= f->f_valuestack); /* else underflow */
766 assert(STACK_LEVEL() <= f->f_stacksize); /* else overflow */
767
Guido van Rossuma027efa1997-05-05 20:56:21 +0000768 /* Do periodic things. Doing this every time through
769 the loop would add too much overhead, so we do it
770 only every Nth instruction. We also do it if
771 ``things_to_do'' is set, i.e. when an asynchronous
772 event needs attention (e.g. a signal handler or
773 async I/O handler); see Py_AddPendingCall() and
774 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000775
Skip Montanarod581d772002-09-03 20:10:45 +0000776 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000777 if (*next_instr == SETUP_FINALLY) {
778 /* Make the last opcode before
779 a try: finally: block uninterruptable. */
780 goto fast_next_opcode;
781 }
Skip Montanarod581d772002-09-03 20:10:45 +0000782 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000783 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000784#ifdef WITH_TSC
785 ticked = 1;
786#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000787 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000788 if (Py_MakePendingCalls() < 0) {
789 why = WHY_EXCEPTION;
790 goto on_error;
791 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000792 if (things_to_do)
793 /* MakePendingCalls() didn't succeed.
794 Force early re-execution of this
795 "periodic" code, possibly after
796 a thread switch */
797 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000798 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000799#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000800 if (interpreter_lock) {
801 /* Give another thread a chance */
802
Guido van Rossum25ce5661997-08-02 03:10:38 +0000803 if (PyThreadState_Swap(NULL) != tstate)
804 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000805 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000806
807 /* Other threads may run now */
808
Guido van Rossum65d5b571998-12-21 19:32:43 +0000809 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000810 if (PyThreadState_Swap(tstate) != NULL)
811 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000812
813 /* Check for thread interrupts */
814
815 if (tstate->async_exc != NULL) {
816 x = tstate->async_exc;
817 tstate->async_exc = NULL;
818 PyErr_SetNone(x);
819 Py_DECREF(x);
820 why = WHY_EXCEPTION;
821 goto on_error;
822 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000823 }
824#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000825 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000826
Neil Schemenauer63543862002-02-17 19:10:14 +0000827 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000828 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000829
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000830 /* line-by-line tracing support */
831
832 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
833 /* see maybe_call_line_trace
834 for expository comments */
835 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000836
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000837 err = maybe_call_line_trace(tstate->c_tracefunc,
838 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000839 f, &instr_lb, &instr_ub,
840 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000841 /* Reload possibly changed frame fields */
842 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000843 if (f->f_stacktop != NULL) {
844 stack_pointer = f->f_stacktop;
845 f->f_stacktop = NULL;
846 }
847 if (err) {
848 /* trace function raised an exception */
849 goto on_error;
850 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000851 }
852
853 /* Extract opcode and argument */
854
Guido van Rossum374a9221991-04-04 10:40:29 +0000855 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000856 oparg = 0; /* allows oparg to be stored in a register because
857 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000858 if (HAS_ARG(opcode))
859 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000860 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000861#ifdef DYNAMIC_EXECUTION_PROFILE
862#ifdef DXPAIRS
863 dxpairs[lastopcode][opcode]++;
864 lastopcode = opcode;
865#endif
866 dxp[opcode]++;
867#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000868
Guido van Rossum96a42c81992-01-12 02:29:51 +0000869#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000870 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000871
Guido van Rossum96a42c81992-01-12 02:29:51 +0000872 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000873 if (HAS_ARG(opcode)) {
874 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000875 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000876 }
877 else {
878 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000879 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000880 }
881 }
882#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000883
Guido van Rossum374a9221991-04-04 10:40:29 +0000884 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000885 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000886
Guido van Rossum374a9221991-04-04 10:40:29 +0000887 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000888
Guido van Rossum374a9221991-04-04 10:40:29 +0000889 /* BEWARE!
890 It is essential that any operation that fails sets either
891 x to NULL, err to nonzero, or why to anything but WHY_NOT,
892 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000893
Guido van Rossum374a9221991-04-04 10:40:29 +0000894 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000895
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000896 case NOP:
897 goto fast_next_opcode;
898
Neil Schemenauer63543862002-02-17 19:10:14 +0000899 case LOAD_FAST:
900 x = GETLOCAL(oparg);
901 if (x != NULL) {
902 Py_INCREF(x);
903 PUSH(x);
904 goto fast_next_opcode;
905 }
906 format_exc_check_arg(PyExc_UnboundLocalError,
907 UNBOUNDLOCAL_ERROR_MSG,
908 PyTuple_GetItem(co->co_varnames, oparg));
909 break;
910
911 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000912 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000913 Py_INCREF(x);
914 PUSH(x);
915 goto fast_next_opcode;
916
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000917 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000918 case STORE_FAST:
919 v = POP();
920 SETLOCAL(oparg, v);
921 goto fast_next_opcode;
922
Raymond Hettingerf606f872003-03-16 03:11:04 +0000923 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000924 case POP_TOP:
925 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000926 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000927 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000928
Guido van Rossum374a9221991-04-04 10:40:29 +0000929 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000930 v = TOP();
931 w = SECOND();
932 SET_TOP(w);
933 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000934 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000935
Guido van Rossum374a9221991-04-04 10:40:29 +0000936 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000937 v = TOP();
938 w = SECOND();
939 x = THIRD();
940 SET_TOP(w);
941 SET_SECOND(x);
942 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000943 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000944
Thomas Wouters434d0822000-08-24 20:11:32 +0000945 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000946 u = TOP();
947 v = SECOND();
948 w = THIRD();
949 x = FOURTH();
950 SET_TOP(v);
951 SET_SECOND(w);
952 SET_THIRD(x);
953 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000954 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000955
Guido van Rossum374a9221991-04-04 10:40:29 +0000956 case DUP_TOP:
957 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000958 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000959 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000960 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000961
Thomas Wouters434d0822000-08-24 20:11:32 +0000962 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000963 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000964 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000965 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000966 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000967 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000968 STACKADJ(2);
969 SET_TOP(x);
970 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000971 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000972 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000973 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000974 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000975 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000976 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000977 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000978 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000979 STACKADJ(3);
980 SET_TOP(x);
981 SET_SECOND(w);
982 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000983 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +0000984 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000985 Py_FatalError("invalid argument to DUP_TOPX"
986 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +0000987 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000988
Guido van Rossum374a9221991-04-04 10:40:29 +0000989 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000990 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000991 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +0000992 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +0000994 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +0000995 break;
Guido van Rossumac7be682001-01-17 15:42:30 +0000996
Guido van Rossum374a9221991-04-04 10:40:29 +0000997 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000998 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +0000999 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001000 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001001 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001002 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001003 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001004
Guido van Rossum374a9221991-04-04 10:40:29 +00001005 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001006 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001007 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001008 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001009 if (err == 0) {
1010 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001011 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001012 continue;
1013 }
1014 else if (err > 0) {
1015 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001017 err = 0;
1018 continue;
1019 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001020 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001021 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001022
Guido van Rossum374a9221991-04-04 10:40:29 +00001023 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001024 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001025 x = PyObject_Repr(v);
1026 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001027 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001028 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001029 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001030
Guido van Rossum7928cd71991-10-24 14:59:31 +00001031 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001033 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001034 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001035 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001036 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001038
Guido van Rossum50564e81996-01-12 01:13:16 +00001039 case BINARY_POWER:
1040 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001041 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001042 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001043 Py_DECREF(v);
1044 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001045 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001046 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001047 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001048
Guido van Rossum374a9221991-04-04 10:40:29 +00001049 case BINARY_MULTIPLY:
1050 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001051 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001052 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001053 Py_DECREF(v);
1054 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001055 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001056 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001057 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001058
Guido van Rossum374a9221991-04-04 10:40:29 +00001059 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001060 if (!_Py_QnewFlag) {
1061 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001062 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001063 x = PyNumber_Divide(v, w);
1064 Py_DECREF(v);
1065 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001066 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001067 if (x != NULL) continue;
1068 break;
1069 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001070 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001071 BINARY_TRUE_DIVIDE */
1072 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001073 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001075 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001076 Py_DECREF(v);
1077 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001078 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001079 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001080 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001081
Guido van Rossum4668b002001-08-08 05:00:18 +00001082 case BINARY_FLOOR_DIVIDE:
1083 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001084 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001085 x = PyNumber_FloorDivide(v, w);
1086 Py_DECREF(v);
1087 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001088 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001089 if (x != NULL) continue;
1090 break;
1091
Guido van Rossum374a9221991-04-04 10:40:29 +00001092 case BINARY_MODULO:
1093 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001094 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001095 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001096 Py_DECREF(v);
1097 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001099 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001100 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001101
Guido van Rossum374a9221991-04-04 10:40:29 +00001102 case BINARY_ADD:
1103 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001104 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001105 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001106 /* INLINE: int + int */
1107 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001108 a = PyInt_AS_LONG(v);
1109 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001110 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001111 if ((i^a) < 0 && (i^b) < 0)
1112 goto slow_add;
1113 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001114 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001115 else if (PyString_CheckExact(v) &&
1116 PyString_CheckExact(w)) {
1117 x = string_concatenate(v, w, f, next_instr);
1118 /* string_concatenate consumed the ref to v */
1119 goto skip_decref_vx;
1120 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001121 else {
1122 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001123 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001124 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001125 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001126 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001127 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001128 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001129 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001130 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001131
Guido van Rossum374a9221991-04-04 10:40:29 +00001132 case BINARY_SUBTRACT:
1133 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001134 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001135 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001136 /* INLINE: int - int */
1137 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001138 a = PyInt_AS_LONG(v);
1139 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001140 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001141 if ((i^a) < 0 && (i^~b) < 0)
1142 goto slow_sub;
1143 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001144 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001145 else {
1146 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001147 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001148 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001149 Py_DECREF(v);
1150 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001151 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001152 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001153 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001154
Guido van Rossum374a9221991-04-04 10:40:29 +00001155 case BINARY_SUBSCR:
1156 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001157 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001158 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001159 /* INLINE: list[int] */
1160 long i = PyInt_AsLong(w);
1161 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001162 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001163 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001164 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001165 Py_INCREF(x);
1166 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001167 else
1168 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001169 }
1170 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001171 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001172 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001173 Py_DECREF(v);
1174 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001175 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001176 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001177 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001178
Guido van Rossum7928cd71991-10-24 14:59:31 +00001179 case BINARY_LSHIFT:
1180 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001182 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 Py_DECREF(v);
1184 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001186 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001187 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001188
Guido van Rossum7928cd71991-10-24 14:59:31 +00001189 case BINARY_RSHIFT:
1190 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001192 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001193 Py_DECREF(v);
1194 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001195 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001196 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001197 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001198
Guido van Rossum7928cd71991-10-24 14:59:31 +00001199 case BINARY_AND:
1200 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001202 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001203 Py_DECREF(v);
1204 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001205 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001206 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001207 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Guido van Rossum7928cd71991-10-24 14:59:31 +00001209 case BINARY_XOR:
1210 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001212 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001213 Py_DECREF(v);
1214 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001216 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001217 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Guido van Rossum7928cd71991-10-24 14:59:31 +00001219 case BINARY_OR:
1220 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001222 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001223 Py_DECREF(v);
1224 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001226 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001227 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001228
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001229 case LIST_APPEND:
1230 w = POP();
1231 v = POP();
1232 err = PyList_Append(v, w);
1233 Py_DECREF(v);
1234 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001235 if (err == 0) {
1236 PREDICT(JUMP_ABSOLUTE);
1237 continue;
1238 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001239 break;
1240
Thomas Wouters434d0822000-08-24 20:11:32 +00001241 case INPLACE_POWER:
1242 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001243 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 x = PyNumber_InPlacePower(v, w, Py_None);
1245 Py_DECREF(v);
1246 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001247 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001248 if (x != NULL) continue;
1249 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001250
Thomas Wouters434d0822000-08-24 20:11:32 +00001251 case INPLACE_MULTIPLY:
1252 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001254 x = PyNumber_InPlaceMultiply(v, w);
1255 Py_DECREF(v);
1256 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 if (x != NULL) continue;
1259 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001260
Thomas Wouters434d0822000-08-24 20:11:32 +00001261 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001262 if (!_Py_QnewFlag) {
1263 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001264 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001265 x = PyNumber_InPlaceDivide(v, w);
1266 Py_DECREF(v);
1267 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001269 if (x != NULL) continue;
1270 break;
1271 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001273 INPLACE_TRUE_DIVIDE */
1274 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001275 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001276 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001277 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001278 Py_DECREF(v);
1279 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001280 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001281 if (x != NULL) continue;
1282 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001283
Guido van Rossum4668b002001-08-08 05:00:18 +00001284 case INPLACE_FLOOR_DIVIDE:
1285 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001286 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001287 x = PyNumber_InPlaceFloorDivide(v, w);
1288 Py_DECREF(v);
1289 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001290 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001291 if (x != NULL) continue;
1292 break;
1293
Thomas Wouters434d0822000-08-24 20:11:32 +00001294 case INPLACE_MODULO:
1295 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001296 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001297 x = PyNumber_InPlaceRemainder(v, w);
1298 Py_DECREF(v);
1299 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001300 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001301 if (x != NULL) continue;
1302 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001303
Thomas Wouters434d0822000-08-24 20:11:32 +00001304 case INPLACE_ADD:
1305 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001306 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001307 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001308 /* INLINE: int + int */
1309 register long a, b, i;
1310 a = PyInt_AS_LONG(v);
1311 b = PyInt_AS_LONG(w);
1312 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001313 if ((i^a) < 0 && (i^b) < 0)
1314 goto slow_iadd;
1315 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001317 else if (PyString_CheckExact(v) &&
1318 PyString_CheckExact(w)) {
1319 x = string_concatenate(v, w, f, next_instr);
1320 /* string_concatenate consumed the ref to v */
1321 goto skip_decref_v;
1322 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001323 else {
1324 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001326 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001327 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001328 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001330 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 if (x != NULL) continue;
1332 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001333
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 case INPLACE_SUBTRACT:
1335 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001336 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001337 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001338 /* INLINE: int - int */
1339 register long a, b, i;
1340 a = PyInt_AS_LONG(v);
1341 b = PyInt_AS_LONG(w);
1342 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001343 if ((i^a) < 0 && (i^~b) < 0)
1344 goto slow_isub;
1345 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001347 else {
1348 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001350 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 Py_DECREF(v);
1352 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001353 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001354 if (x != NULL) continue;
1355 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Thomas Wouters434d0822000-08-24 20:11:32 +00001357 case INPLACE_LSHIFT:
1358 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001359 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001360 x = PyNumber_InPlaceLshift(v, w);
1361 Py_DECREF(v);
1362 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001363 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001364 if (x != NULL) continue;
1365 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001366
Thomas Wouters434d0822000-08-24 20:11:32 +00001367 case INPLACE_RSHIFT:
1368 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001369 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001370 x = PyNumber_InPlaceRshift(v, w);
1371 Py_DECREF(v);
1372 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001373 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001374 if (x != NULL) continue;
1375 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001376
Thomas Wouters434d0822000-08-24 20:11:32 +00001377 case INPLACE_AND:
1378 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001379 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001380 x = PyNumber_InPlaceAnd(v, w);
1381 Py_DECREF(v);
1382 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001383 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001384 if (x != NULL) continue;
1385 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Thomas Wouters434d0822000-08-24 20:11:32 +00001387 case INPLACE_XOR:
1388 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001389 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001390 x = PyNumber_InPlaceXor(v, w);
1391 Py_DECREF(v);
1392 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001393 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001394 if (x != NULL) continue;
1395 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Thomas Wouters434d0822000-08-24 20:11:32 +00001397 case INPLACE_OR:
1398 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001399 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001400 x = PyNumber_InPlaceOr(v, w);
1401 Py_DECREF(v);
1402 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001403 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001404 if (x != NULL) continue;
1405 break;
1406
Guido van Rossum374a9221991-04-04 10:40:29 +00001407 case SLICE+0:
1408 case SLICE+1:
1409 case SLICE+2:
1410 case SLICE+3:
1411 if ((opcode-SLICE) & 2)
1412 w = POP();
1413 else
1414 w = NULL;
1415 if ((opcode-SLICE) & 1)
1416 v = POP();
1417 else
1418 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001419 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001420 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001421 Py_DECREF(u);
1422 Py_XDECREF(v);
1423 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001424 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001425 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001426 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Guido van Rossum374a9221991-04-04 10:40:29 +00001428 case STORE_SLICE+0:
1429 case STORE_SLICE+1:
1430 case STORE_SLICE+2:
1431 case STORE_SLICE+3:
1432 if ((opcode-STORE_SLICE) & 2)
1433 w = POP();
1434 else
1435 w = NULL;
1436 if ((opcode-STORE_SLICE) & 1)
1437 v = POP();
1438 else
1439 v = NULL;
1440 u = POP();
1441 t = POP();
1442 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001443 Py_DECREF(t);
1444 Py_DECREF(u);
1445 Py_XDECREF(v);
1446 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001447 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001448 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001449
Guido van Rossum374a9221991-04-04 10:40:29 +00001450 case DELETE_SLICE+0:
1451 case DELETE_SLICE+1:
1452 case DELETE_SLICE+2:
1453 case DELETE_SLICE+3:
1454 if ((opcode-DELETE_SLICE) & 2)
1455 w = POP();
1456 else
1457 w = NULL;
1458 if ((opcode-DELETE_SLICE) & 1)
1459 v = POP();
1460 else
1461 v = NULL;
1462 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001463 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001464 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001465 Py_DECREF(u);
1466 Py_XDECREF(v);
1467 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001468 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001469 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001472 w = TOP();
1473 v = SECOND();
1474 u = THIRD();
1475 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001476 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001477 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001478 Py_DECREF(u);
1479 Py_DECREF(v);
1480 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001481 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001482 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001483
Guido van Rossum374a9221991-04-04 10:40:29 +00001484 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001485 w = TOP();
1486 v = SECOND();
1487 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001488 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001489 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001490 Py_DECREF(v);
1491 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001492 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001493 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001494
Guido van Rossum374a9221991-04-04 10:40:29 +00001495 case PRINT_EXPR:
1496 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001497 w = PySys_GetObject("displayhook");
1498 if (w == NULL) {
1499 PyErr_SetString(PyExc_RuntimeError,
1500 "lost sys.displayhook");
1501 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001502 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001503 }
1504 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001505 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001506 if (x == NULL)
1507 err = -1;
1508 }
1509 if (err == 0) {
1510 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001511 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001512 if (w == NULL)
1513 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001515 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001516 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001517 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001518
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001519 case PRINT_ITEM_TO:
1520 w = stream = POP();
1521 /* fall through to PRINT_ITEM */
1522
Guido van Rossum374a9221991-04-04 10:40:29 +00001523 case PRINT_ITEM:
1524 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001525 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001526 w = PySys_GetObject("stdout");
1527 if (w == NULL) {
1528 PyErr_SetString(PyExc_RuntimeError,
1529 "lost sys.stdout");
1530 err = -1;
1531 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001532 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001533 /* PyFile_SoftSpace() can exececute arbitrary code
1534 if sys.stdout is an instance with a __getattr__.
1535 If __getattr__ raises an exception, w will
1536 be freed, so we need to prevent that temporarily. */
1537 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001538 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001539 err = PyFile_WriteString(" ", w);
1540 if (err == 0)
1541 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001542 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001543 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001544 if (PyString_Check(v)) {
1545 char *s = PyString_AS_STRING(v);
1546 int len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001547 if (len == 0 ||
1548 !isspace(Py_CHARMASK(s[len-1])) ||
1549 s[len-1] == ' ')
1550 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001551 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001552#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001553 else if (PyUnicode_Check(v)) {
1554 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1555 int len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001556 if (len == 0 ||
1557 !Py_UNICODE_ISSPACE(s[len-1]) ||
1558 s[len-1] == ' ')
1559 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001560 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001561#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001562 else
1563 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001564 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001565 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001566 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001567 Py_XDECREF(stream);
1568 stream = NULL;
1569 if (err == 0)
1570 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001571 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001573 case PRINT_NEWLINE_TO:
1574 w = stream = POP();
1575 /* fall through to PRINT_NEWLINE */
1576
Guido van Rossum374a9221991-04-04 10:40:29 +00001577 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001578 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001579 w = PySys_GetObject("stdout");
1580 if (w == NULL)
1581 PyErr_SetString(PyExc_RuntimeError,
1582 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001583 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001584 if (w != NULL) {
1585 err = PyFile_WriteString("\n", w);
1586 if (err == 0)
1587 PyFile_SoftSpace(w, 0);
1588 }
1589 Py_XDECREF(stream);
1590 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001591 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Thomas Wouters434d0822000-08-24 20:11:32 +00001593
1594#ifdef CASE_TOO_BIG
1595 default: switch (opcode) {
1596#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001597 case RAISE_VARARGS:
1598 u = v = w = NULL;
1599 switch (oparg) {
1600 case 3:
1601 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001602 /* Fallthrough */
1603 case 2:
1604 v = POP(); /* value */
1605 /* Fallthrough */
1606 case 1:
1607 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001608 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001609 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001610 break;
1611 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001612 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001613 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001614 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001615 break;
1616 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001617 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Guido van Rossum374a9221991-04-04 10:40:29 +00001619 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001620 if ((x = f->f_locals) != NULL) {
1621 Py_INCREF(x);
1622 PUSH(x);
1623 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001624 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001625 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001626 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001627
Guido van Rossum374a9221991-04-04 10:40:29 +00001628 case RETURN_VALUE:
1629 retval = POP();
1630 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001631 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001632
Tim Peters5ca576e2001-06-18 22:08:13 +00001633 case YIELD_VALUE:
1634 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001635 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001636 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001637 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001638
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001639 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001640 w = TOP();
1641 v = SECOND();
1642 u = THIRD();
1643 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001644 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001645 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001646 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001647 Py_DECREF(u);
1648 Py_DECREF(v);
1649 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001650 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001651
Guido van Rossum374a9221991-04-04 10:40:29 +00001652 case POP_BLOCK:
1653 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001654 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 while (STACK_LEVEL() > b->b_level) {
1656 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001657 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 }
1659 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001660 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 case END_FINALLY:
1663 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001664 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001665 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001666 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001667 if (why == WHY_RETURN ||
1668 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 retval = POP();
1670 }
Raymond Hettingerd3b836d2004-04-07 13:17:27 +00001671 else if (PyClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001672 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001673 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001674 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001676 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001678 else if (v != Py_None) {
1679 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001680 "'finally' pops bad exception");
1681 why = WHY_EXCEPTION;
1682 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001683 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001684 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001687 u = TOP();
1688 v = SECOND();
1689 w = THIRD();
1690 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001691 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001692 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001693 Py_DECREF(u);
1694 Py_DECREF(v);
1695 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001696 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Guido van Rossum374a9221991-04-04 10:40:29 +00001698 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001699 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001701 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001702 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001703 err = PyDict_SetItem(x, w, v);
1704 else
1705 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001706 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001707 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001708 break;
1709 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001710 PyErr_Format(PyExc_SystemError,
1711 "no locals found when storing %s",
1712 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001714
Guido van Rossum374a9221991-04-04 10:40:29 +00001715 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001716 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001717 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001718 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001719 format_exc_check_arg(PyExc_NameError,
1720 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001721 break;
1722 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001723 PyErr_Format(PyExc_SystemError,
1724 "no locals when deleting %s",
1725 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001726 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001727
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001728 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001729 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001730 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001731 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1732 PyObject **items = ((PyTupleObject *)v)->ob_item;
1733 while (oparg--) {
1734 w = items[oparg];
1735 Py_INCREF(w);
1736 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001737 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001738 Py_DECREF(v);
1739 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001740 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1741 PyObject **items = ((PyListObject *)v)->ob_item;
1742 while (oparg--) {
1743 w = items[oparg];
1744 Py_INCREF(w);
1745 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001746 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001747 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001748 stack_pointer + oparg))
1749 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001750 else {
1751 if (PyErr_ExceptionMatches(PyExc_TypeError))
1752 PyErr_SetString(PyExc_TypeError,
1753 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001754 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001755 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001756 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001757 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001758
Guido van Rossum374a9221991-04-04 10:40:29 +00001759 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001760 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001761 v = TOP();
1762 u = SECOND();
1763 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001764 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1765 Py_DECREF(v);
1766 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001767 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001768 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001769
Guido van Rossum374a9221991-04-04 10:40:29 +00001770 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001771 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001773 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1774 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001775 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001778 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001779 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001780 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001781 err = PyDict_SetItem(f->f_globals, w, v);
1782 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001783 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001784 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001785
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001786 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001787 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001788 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001789 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001790 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001791 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Guido van Rossum374a9221991-04-04 10:40:29 +00001793 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001794 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001795 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001796 PyErr_Format(PyExc_SystemError,
1797 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001798 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001799 break;
1800 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001801 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001802 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001803 Py_XINCREF(x);
1804 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001805 else {
1806 x = PyObject_GetItem(v, w);
1807 if (x == NULL && PyErr_Occurred()) {
1808 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1809 break;
1810 PyErr_Clear();
1811 }
1812 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001813 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001814 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001815 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001816 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001817 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001818 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001819 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001820 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001821 break;
1822 }
1823 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001824 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001825 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001826 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001827 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001828
Guido van Rossum374a9221991-04-04 10:40:29 +00001829 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001830 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001831 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001832 /* Inline the PyDict_GetItem() calls.
1833 WARNING: this is an extreme speed hack.
1834 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001835 long hash = ((PyStringObject *)w)->ob_shash;
1836 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001837 PyDictObject *d;
1838 d = (PyDictObject *)(f->f_globals);
1839 x = d->ma_lookup(d, w, hash)->me_value;
1840 if (x != NULL) {
1841 Py_INCREF(x);
1842 PUSH(x);
1843 continue;
1844 }
1845 d = (PyDictObject *)(f->f_builtins);
1846 x = d->ma_lookup(d, w, hash)->me_value;
1847 if (x != NULL) {
1848 Py_INCREF(x);
1849 PUSH(x);
1850 continue;
1851 }
1852 goto load_global_error;
1853 }
1854 }
1855 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001856 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001858 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001859 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001860 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001861 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001862 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001863 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001864 break;
1865 }
1866 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001867 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001868 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001869 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001870
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001871 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001872 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001873 if (x != NULL) {
1874 SETLOCAL(oparg, NULL);
1875 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001876 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001877 format_exc_check_arg(
1878 PyExc_UnboundLocalError,
1879 UNBOUNDLOCAL_ERROR_MSG,
1880 PyTuple_GetItem(co->co_varnames, oparg)
1881 );
1882 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001883
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001884 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001885 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001886 Py_INCREF(x);
1887 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001888 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001889 break;
1890
1891 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001892 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001893 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001894 if (w != NULL) {
1895 PUSH(w);
1896 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001897 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001898 err = -1;
1899 /* Don't stomp existing exception */
1900 if (PyErr_Occurred())
1901 break;
1902 if (oparg < f->f_ncells) {
1903 v = PyTuple_GetItem(co->co_cellvars,
1904 oparg);
1905 format_exc_check_arg(
1906 PyExc_UnboundLocalError,
1907 UNBOUNDLOCAL_ERROR_MSG,
1908 v);
1909 } else {
1910 v = PyTuple_GetItem(
1911 co->co_freevars,
1912 oparg - f->f_ncells);
1913 format_exc_check_arg(
1914 PyExc_NameError,
1915 UNBOUNDFREE_ERROR_MSG,
1916 v);
1917 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001918 break;
1919
1920 case STORE_DEREF:
1921 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001922 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001923 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001924 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001925 continue;
1926
Guido van Rossum374a9221991-04-04 10:40:29 +00001927 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001928 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001930 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001931 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001932 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 }
1934 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001935 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001936 }
1937 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001938
Guido van Rossum374a9221991-04-04 10:40:29 +00001939 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001940 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001941 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001942 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001943 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001944 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 }
1946 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001947 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001948 }
1949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Guido van Rossum374a9221991-04-04 10:40:29 +00001951 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001952 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001953 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001954 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001955 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001956
Guido van Rossum374a9221991-04-04 10:40:29 +00001957 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001958 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001959 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001960 x = PyObject_GetAttr(v, w);
1961 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001962 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001963 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001964 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001965
Guido van Rossum374a9221991-04-04 10:40:29 +00001966 case COMPARE_OP:
1967 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001968 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001969 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001970 /* INLINE: cmp(int, int) */
1971 register long a, b;
1972 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001973 a = PyInt_AS_LONG(v);
1974 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001975 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001976 case PyCmp_LT: res = a < b; break;
1977 case PyCmp_LE: res = a <= b; break;
1978 case PyCmp_EQ: res = a == b; break;
1979 case PyCmp_NE: res = a != b; break;
1980 case PyCmp_GT: res = a > b; break;
1981 case PyCmp_GE: res = a >= b; break;
1982 case PyCmp_IS: res = v == w; break;
1983 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001984 default: goto slow_compare;
1985 }
1986 x = res ? Py_True : Py_False;
1987 Py_INCREF(x);
1988 }
1989 else {
1990 slow_compare:
1991 x = cmp_outcome(oparg, v, w);
1992 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001993 Py_DECREF(v);
1994 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001995 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001996 if (x == NULL) break;
1997 PREDICT(JUMP_IF_FALSE);
1998 PREDICT(JUMP_IF_TRUE);
1999 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002000
Guido van Rossum374a9221991-04-04 10:40:29 +00002001 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002002 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002003 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002004 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002005 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002006 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002007 break;
2008 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00002009 u = TOP();
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002010 w = PyTuple_Pack(4,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002011 w,
2012 f->f_globals,
Guido van Rossuma027efa1997-05-05 20:56:21 +00002013 f->f_locals == NULL ?
2014 Py_None : f->f_locals,
Guido van Rossum681d79a1995-07-18 14:51:37 +00002015 u);
Guido van Rossumb209a111997-04-29 18:18:01 +00002016 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002017 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002018 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002019 x = NULL;
2020 break;
2021 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002022 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002023 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002024 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002025 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002026 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002027 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002028 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002029
Thomas Wouters52152252000-08-17 22:55:00 +00002030 case IMPORT_STAR:
2031 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002032 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002033 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002034 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002035 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002036 break;
2037 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002038 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002039 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002040 READ_TIMESTAMP(intr1);
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();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002049 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002050 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002051 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002052 PUSH(x);
2053 if (x != NULL) continue;
2054 break;
2055
Guido van Rossum374a9221991-04-04 10:40:29 +00002056 case JUMP_FORWARD:
2057 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002058 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002059
Raymond Hettingerf606f872003-03-16 03:11:04 +00002060 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002061 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002062 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002063 if (w == Py_True) {
2064 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002065 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002066 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002067 if (w == Py_False) {
2068 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002069 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002070 }
2071 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002072 if (err > 0)
2073 err = 0;
2074 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002075 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002076 else
2077 break;
2078 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002079
Raymond Hettingerf606f872003-03-16 03:11:04 +00002080 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002081 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002082 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002083 if (w == Py_False) {
2084 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002085 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002086 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002087 if (w == Py_True) {
2088 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002089 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002090 }
2091 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002092 if (err > 0) {
2093 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002094 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002095 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002096 else if (err == 0)
2097 ;
2098 else
2099 break;
2100 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002101
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002102 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002103 case JUMP_ABSOLUTE:
2104 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002105 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002106
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107 case GET_ITER:
2108 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002109 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002110 x = PyObject_GetIter(v);
2111 Py_DECREF(v);
2112 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002113 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002114 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002115 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002116 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002117 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002118 break;
2119
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002120 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002121 case FOR_ITER:
2122 /* before: [iter]; after: [iter, iter()] *or* [] */
2123 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002124 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002125 if (x != NULL) {
2126 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002127 PREDICT(STORE_FAST);
2128 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002129 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002130 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002131 if (PyErr_Occurred()) {
2132 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2133 break;
2134 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002135 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002136 /* iterator ended normally */
2137 x = v = POP();
2138 Py_DECREF(v);
2139 JUMPBY(oparg);
2140 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002141
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002142 case BREAK_LOOP:
2143 why = WHY_BREAK;
2144 goto fast_block_end;
2145
2146 case CONTINUE_LOOP:
2147 retval = PyInt_FromLong(oparg);
2148 why = WHY_CONTINUE;
2149 goto fast_block_end;
2150
Guido van Rossum374a9221991-04-04 10:40:29 +00002151 case SETUP_LOOP:
2152 case SETUP_EXCEPT:
2153 case SETUP_FINALLY:
Guido van Rossumb209a111997-04-29 18:18:01 +00002154 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002155 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002156 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002157
Guido van Rossumf10570b1995-07-07 22:53:21 +00002158 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002159 {
2160 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002161 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002162 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002163#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002164 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002165#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002166 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002167#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002168 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002169 PUSH(x);
2170 if (x != NULL)
2171 continue;
2172 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002173 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002174
Jeremy Hylton76901512000-03-28 23:49:17 +00002175 case CALL_FUNCTION_VAR:
2176 case CALL_FUNCTION_KW:
2177 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002178 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002179 int na = oparg & 0xff;
2180 int nk = (oparg>>8) & 0xff;
2181 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002182 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002183 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002184 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002185 if (flags & CALL_FLAG_VAR)
2186 n++;
2187 if (flags & CALL_FLAG_KW)
2188 n++;
2189 pfunc = stack_pointer - n - 1;
2190 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002191
Guido van Rossumac7be682001-01-17 15:42:30 +00002192 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002193 && PyMethod_GET_SELF(func) != NULL) {
2194 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002195 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002196 func = PyMethod_GET_FUNCTION(func);
2197 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002198 Py_DECREF(*pfunc);
2199 *pfunc = self;
2200 na++;
2201 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002202 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002203 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002204 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002205 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002206 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002207 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002208 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002209 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002210
Jeremy Hylton76901512000-03-28 23:49:17 +00002211 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002212 w = POP();
2213 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002214 }
2215 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002216 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002217 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002218 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002219 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002220
Guido van Rossum681d79a1995-07-18 14:51:37 +00002221 case MAKE_FUNCTION:
2222 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002223 x = PyFunction_New(v, f->f_globals);
2224 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002225 /* XXX Maybe this should be a separate opcode? */
2226 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002227 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002228 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002229 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002230 x = NULL;
2231 break;
2232 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002233 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002234 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002235 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002236 }
2237 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002238 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002239 }
2240 PUSH(x);
2241 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002242
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002243 case MAKE_CLOSURE:
2244 {
2245 int nfree;
2246 v = POP(); /* code object */
2247 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002248 nfree = PyCode_GetNumFree((PyCodeObject *)v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002249 Py_DECREF(v);
2250 /* XXX Maybe this should be a separate opcode? */
2251 if (x != NULL && nfree > 0) {
2252 v = PyTuple_New(nfree);
2253 if (v == NULL) {
2254 Py_DECREF(x);
2255 x = NULL;
2256 break;
2257 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002258 while (--nfree >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002259 w = POP();
2260 PyTuple_SET_ITEM(v, nfree, w);
2261 }
2262 err = PyFunction_SetClosure(x, v);
2263 Py_DECREF(v);
2264 }
2265 if (x != NULL && oparg > 0) {
2266 v = PyTuple_New(oparg);
2267 if (v == NULL) {
2268 Py_DECREF(x);
2269 x = NULL;
2270 break;
2271 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002272 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002273 w = POP();
2274 PyTuple_SET_ITEM(v, oparg, w);
2275 }
2276 err = PyFunction_SetDefaults(x, v);
2277 Py_DECREF(v);
2278 }
2279 PUSH(x);
2280 break;
2281 }
2282
Guido van Rossum8861b741996-07-30 16:49:37 +00002283 case BUILD_SLICE:
2284 if (oparg == 3)
2285 w = POP();
2286 else
2287 w = NULL;
2288 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002289 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002290 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002291 Py_DECREF(u);
2292 Py_DECREF(v);
2293 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002294 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002295 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002296 break;
2297
Fred Drakeef8ace32000-08-24 00:32:09 +00002298 case EXTENDED_ARG:
2299 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002300 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002301 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002302
Guido van Rossum374a9221991-04-04 10:40:29 +00002303 default:
2304 fprintf(stderr,
2305 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002306 PyCode_Addr2Line(f->f_code, f->f_lasti),
2307 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002308 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002309 why = WHY_EXCEPTION;
2310 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002311
2312#ifdef CASE_TOO_BIG
2313 }
2314#endif
2315
Guido van Rossum374a9221991-04-04 10:40:29 +00002316 } /* switch */
2317
2318 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002319
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002320 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002321
Guido van Rossum374a9221991-04-04 10:40:29 +00002322 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Guido van Rossum374a9221991-04-04 10:40:29 +00002324 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002325 if (err == 0 && x != NULL) {
2326#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002327 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002328 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002329 fprintf(stderr,
2330 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002331 else {
2332#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002333 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002334 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002335#ifdef CHECKEXC
2336 }
2337#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002338 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002339 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002340 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002341 err = 0;
2342 }
2343
Guido van Rossum374a9221991-04-04 10:40:29 +00002344 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002345
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002346 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002347 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002348 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002349 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002350 why = WHY_EXCEPTION;
2351 }
2352 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002353#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002354 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002355 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002356 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002357 char buf[1024];
2358 sprintf(buf, "Stack unwind with exception "
2359 "set and why=%d", why);
2360 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002361 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002362 }
2363#endif
2364
2365 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002366
Guido van Rossum374a9221991-04-04 10:40:29 +00002367 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002368 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002369
Fred Drake8f51f542001-10-04 14:48:42 +00002370 if (tstate->c_tracefunc != NULL)
2371 call_exc_trace(tstate->c_tracefunc,
2372 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002373 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002374
Guido van Rossum374a9221991-04-04 10:40:29 +00002375 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002376
Guido van Rossum374a9221991-04-04 10:40:29 +00002377 if (why == WHY_RERAISE)
2378 why = WHY_EXCEPTION;
2379
2380 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002381
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002382fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002383 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002384 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002385
Tim Peters8a5c3c72004-04-05 19:36:21 +00002386 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002387 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2388 /* For a continue inside a try block,
2389 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002390 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2391 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002392 why = WHY_NOT;
2393 JUMPTO(PyInt_AS_LONG(retval));
2394 Py_DECREF(retval);
2395 break;
2396 }
2397
Guido van Rossum374a9221991-04-04 10:40:29 +00002398 while (STACK_LEVEL() > b->b_level) {
2399 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002400 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002401 }
2402 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2403 why = WHY_NOT;
2404 JUMPTO(b->b_handler);
2405 break;
2406 }
2407 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002408 (b->b_type == SETUP_EXCEPT &&
2409 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002410 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002411 PyObject *exc, *val, *tb;
2412 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002413 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002414 val = Py_None;
2415 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002416 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002417 /* Make the raw exception data
2418 available to the handler,
2419 so a program can emulate the
2420 Python main loop. Don't do
2421 this for 'finally'. */
2422 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002423 PyErr_NormalizeException(
2424 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002425 set_exc_info(tstate,
2426 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002427 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002428 if (tb == NULL) {
2429 Py_INCREF(Py_None);
2430 PUSH(Py_None);
2431 } else
2432 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002433 PUSH(val);
2434 PUSH(exc);
2435 }
2436 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002437 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002438 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002439 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002440 PUSH(v);
2441 }
2442 why = WHY_NOT;
2443 JUMPTO(b->b_handler);
2444 break;
2445 }
2446 } /* unwind stack */
2447
2448 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002449
Guido van Rossum374a9221991-04-04 10:40:29 +00002450 if (why != WHY_NOT)
2451 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002452 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002453
Guido van Rossum374a9221991-04-04 10:40:29 +00002454 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002455
Tim Peters8a5c3c72004-04-05 19:36:21 +00002456 assert(why != WHY_YIELD);
2457 /* Pop remaining stack entries. */
2458 while (!EMPTY()) {
2459 v = POP();
2460 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002461 }
2462
Tim Peters8a5c3c72004-04-05 19:36:21 +00002463 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002464 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002465
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002466fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002467 if (tstate->use_tracing) {
2468 if (tstate->c_tracefunc
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002469 && (why == WHY_RETURN || why == WHY_YIELD)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002470 if (call_trace(tstate->c_tracefunc,
2471 tstate->c_traceobj, f,
2472 PyTrace_RETURN, retval)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002473 Py_XDECREF(retval);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002474 retval = NULL;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002475 why = WHY_EXCEPTION;
Guido van Rossum96a42c81992-01-12 02:29:51 +00002476 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002477 }
Fred Drake8f51f542001-10-04 14:48:42 +00002478 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002479 if (why == WHY_EXCEPTION)
2480 call_trace_protected(tstate->c_profilefunc,
2481 tstate->c_profileobj, f,
2482 PyTrace_RETURN);
2483 else if (call_trace(tstate->c_profilefunc,
2484 tstate->c_profileobj, f,
2485 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002486 Py_XDECREF(retval);
2487 retval = NULL;
2488 why = WHY_EXCEPTION;
2489 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002490 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002491 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002492
Guido van Rossuma027efa1997-05-05 20:56:21 +00002493 reset_exc_info(tstate);
2494
Tim Peters5ca576e2001-06-18 22:08:13 +00002495 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002496 exit_eval_frame:
2497 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002498 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002499
Guido van Rossum96a42c81992-01-12 02:29:51 +00002500 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002501}
2502
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002503/* this is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002504 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Skip Montanaroc9a47622005-01-08 21:58:58 +00002505 the test in the if statement in Misc/gdbinit:pystack* */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002506
Tim Peters6d6c1a32001-08-02 04:15:00 +00002507PyObject *
2508PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002509 PyObject **args, int argcount, PyObject **kws, int kwcount,
2510 PyObject **defs, int defcount, PyObject *closure)
2511{
2512 register PyFrameObject *f;
2513 register PyObject *retval = NULL;
2514 register PyObject **fastlocals, **freevars;
2515 PyThreadState *tstate = PyThreadState_GET();
2516 PyObject *x, *u;
2517
2518 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002519 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002520 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002521 return NULL;
2522 }
2523
Jeremy Hylton985eba52003-02-05 23:13:00 +00002524 assert(globals != NULL);
2525 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002526 if (f == NULL)
2527 return NULL;
2528
2529 fastlocals = f->f_localsplus;
2530 freevars = f->f_localsplus + f->f_nlocals;
2531
2532 if (co->co_argcount > 0 ||
2533 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2534 int i;
2535 int n = argcount;
2536 PyObject *kwdict = NULL;
2537 if (co->co_flags & CO_VARKEYWORDS) {
2538 kwdict = PyDict_New();
2539 if (kwdict == NULL)
2540 goto fail;
2541 i = co->co_argcount;
2542 if (co->co_flags & CO_VARARGS)
2543 i++;
2544 SETLOCAL(i, kwdict);
2545 }
2546 if (argcount > co->co_argcount) {
2547 if (!(co->co_flags & CO_VARARGS)) {
2548 PyErr_Format(PyExc_TypeError,
2549 "%.200s() takes %s %d "
2550 "%sargument%s (%d given)",
2551 PyString_AsString(co->co_name),
2552 defcount ? "at most" : "exactly",
2553 co->co_argcount,
2554 kwcount ? "non-keyword " : "",
2555 co->co_argcount == 1 ? "" : "s",
2556 argcount);
2557 goto fail;
2558 }
2559 n = co->co_argcount;
2560 }
2561 for (i = 0; i < n; i++) {
2562 x = args[i];
2563 Py_INCREF(x);
2564 SETLOCAL(i, x);
2565 }
2566 if (co->co_flags & CO_VARARGS) {
2567 u = PyTuple_New(argcount - n);
2568 if (u == NULL)
2569 goto fail;
2570 SETLOCAL(co->co_argcount, u);
2571 for (i = n; i < argcount; i++) {
2572 x = args[i];
2573 Py_INCREF(x);
2574 PyTuple_SET_ITEM(u, i-n, x);
2575 }
2576 }
2577 for (i = 0; i < kwcount; i++) {
2578 PyObject *keyword = kws[2*i];
2579 PyObject *value = kws[2*i + 1];
2580 int j;
2581 if (keyword == NULL || !PyString_Check(keyword)) {
2582 PyErr_Format(PyExc_TypeError,
2583 "%.200s() keywords must be strings",
2584 PyString_AsString(co->co_name));
2585 goto fail;
2586 }
2587 /* XXX slow -- speed up using dictionary? */
2588 for (j = 0; j < co->co_argcount; j++) {
2589 PyObject *nm = PyTuple_GET_ITEM(
2590 co->co_varnames, j);
2591 int cmp = PyObject_RichCompareBool(
2592 keyword, nm, Py_EQ);
2593 if (cmp > 0)
2594 break;
2595 else if (cmp < 0)
2596 goto fail;
2597 }
2598 /* Check errors from Compare */
2599 if (PyErr_Occurred())
2600 goto fail;
2601 if (j >= co->co_argcount) {
2602 if (kwdict == NULL) {
2603 PyErr_Format(PyExc_TypeError,
2604 "%.200s() got an unexpected "
2605 "keyword argument '%.400s'",
2606 PyString_AsString(co->co_name),
2607 PyString_AsString(keyword));
2608 goto fail;
2609 }
2610 PyDict_SetItem(kwdict, keyword, value);
2611 }
2612 else {
2613 if (GETLOCAL(j) != NULL) {
2614 PyErr_Format(PyExc_TypeError,
2615 "%.200s() got multiple "
2616 "values for keyword "
2617 "argument '%.400s'",
2618 PyString_AsString(co->co_name),
2619 PyString_AsString(keyword));
2620 goto fail;
2621 }
2622 Py_INCREF(value);
2623 SETLOCAL(j, value);
2624 }
2625 }
2626 if (argcount < co->co_argcount) {
2627 int m = co->co_argcount - defcount;
2628 for (i = argcount; i < m; i++) {
2629 if (GETLOCAL(i) == NULL) {
2630 PyErr_Format(PyExc_TypeError,
2631 "%.200s() takes %s %d "
2632 "%sargument%s (%d given)",
2633 PyString_AsString(co->co_name),
2634 ((co->co_flags & CO_VARARGS) ||
2635 defcount) ? "at least"
2636 : "exactly",
2637 m, kwcount ? "non-keyword " : "",
2638 m == 1 ? "" : "s", i);
2639 goto fail;
2640 }
2641 }
2642 if (n > m)
2643 i = n - m;
2644 else
2645 i = 0;
2646 for (; i < defcount; i++) {
2647 if (GETLOCAL(m+i) == NULL) {
2648 PyObject *def = defs[i];
2649 Py_INCREF(def);
2650 SETLOCAL(m+i, def);
2651 }
2652 }
2653 }
2654 }
2655 else {
2656 if (argcount > 0 || kwcount > 0) {
2657 PyErr_Format(PyExc_TypeError,
2658 "%.200s() takes no arguments (%d given)",
2659 PyString_AsString(co->co_name),
2660 argcount + kwcount);
2661 goto fail;
2662 }
2663 }
2664 /* Allocate and initialize storage for cell vars, and copy free
2665 vars into frame. This isn't too efficient right now. */
2666 if (f->f_ncells) {
2667 int i = 0, j = 0, nargs, found;
2668 char *cellname, *argname;
2669 PyObject *c;
2670
2671 nargs = co->co_argcount;
2672 if (co->co_flags & CO_VARARGS)
2673 nargs++;
2674 if (co->co_flags & CO_VARKEYWORDS)
2675 nargs++;
2676
2677 /* Check for cells that shadow args */
2678 for (i = 0; i < f->f_ncells && j < nargs; ++i) {
2679 cellname = PyString_AS_STRING(
2680 PyTuple_GET_ITEM(co->co_cellvars, i));
2681 found = 0;
2682 while (j < nargs) {
2683 argname = PyString_AS_STRING(
2684 PyTuple_GET_ITEM(co->co_varnames, j));
2685 if (strcmp(cellname, argname) == 0) {
2686 c = PyCell_New(GETLOCAL(j));
2687 if (c == NULL)
2688 goto fail;
2689 GETLOCAL(f->f_nlocals + i) = c;
2690 found = 1;
2691 break;
2692 }
2693 j++;
2694 }
2695 if (found == 0) {
2696 c = PyCell_New(NULL);
2697 if (c == NULL)
2698 goto fail;
2699 SETLOCAL(f->f_nlocals + i, c);
2700 }
2701 }
2702 /* Initialize any that are left */
2703 while (i < f->f_ncells) {
2704 c = PyCell_New(NULL);
2705 if (c == NULL)
2706 goto fail;
2707 SETLOCAL(f->f_nlocals + i, c);
2708 i++;
2709 }
2710 }
2711 if (f->f_nfreevars) {
2712 int i;
2713 for (i = 0; i < f->f_nfreevars; ++i) {
2714 PyObject *o = PyTuple_GET_ITEM(closure, i);
2715 Py_INCREF(o);
2716 freevars[f->f_ncells + i] = o;
2717 }
2718 }
2719
Tim Peters5ca576e2001-06-18 22:08:13 +00002720 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002721 /* Don't need to keep the reference to f_back, it will be set
2722 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002723 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002724 f->f_back = NULL;
2725
Jeremy Hylton985eba52003-02-05 23:13:00 +00002726 PCALL(PCALL_GENERATOR);
2727
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002728 /* Create a new generator that owns the ready to run frame
2729 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002730 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002731 }
2732
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002733 retval = PyEval_EvalFrame(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002734
2735 fail: /* Jump here from prelude on failure */
2736
Tim Petersb13680b2001-11-27 23:29:29 +00002737 /* decref'ing the frame can cause __del__ methods to get invoked,
2738 which can call back into Python. While we're done with the
2739 current Python frame (f), the associated C stack is still in use,
2740 so recursion_depth must be boosted for the duration.
2741 */
2742 assert(tstate != NULL);
2743 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002744 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002745 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002746 return retval;
2747}
2748
2749
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002750/* Implementation notes for set_exc_info() and reset_exc_info():
2751
2752- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2753 'exc_traceback'. These always travel together.
2754
2755- tstate->curexc_ZZZ is the "hot" exception that is set by
2756 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2757
2758- Once an exception is caught by an except clause, it is transferred
2759 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2760 can pick it up. This is the primary task of set_exc_info().
2761
2762- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2763
2764 Long ago, when none of this existed, there were just a few globals:
2765 one set corresponding to the "hot" exception, and one set
2766 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2767 globals; they were simply stored as sys.exc_ZZZ. For backwards
2768 compatibility, they still are!) The problem was that in code like
2769 this:
2770
2771 try:
2772 "something that may fail"
2773 except "some exception":
2774 "do something else first"
2775 "print the exception from sys.exc_ZZZ."
2776
2777 if "do something else first" invoked something that raised and caught
2778 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2779 cause of subtle bugs. I fixed this by changing the semantics as
2780 follows:
2781
2782 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2783 *in that frame*.
2784
2785 - But initially, and as long as no exception is caught in a given
2786 frame, sys.exc_ZZZ will hold the last exception caught in the
2787 previous frame (or the frame before that, etc.).
2788
2789 The first bullet fixed the bug in the above example. The second
2790 bullet was for backwards compatibility: it was (and is) common to
2791 have a function that is called when an exception is caught, and to
2792 have that function access the caught exception via sys.exc_ZZZ.
2793 (Example: traceback.print_exc()).
2794
2795 At the same time I fixed the problem that sys.exc_ZZZ weren't
2796 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2797 but that's really a separate improvement.
2798
2799 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2800 variables to what they were before the current frame was called. The
2801 set_exc_info() function saves them on the frame so that
2802 reset_exc_info() can restore them. The invariant is that
2803 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2804 exception (where "catching" an exception applies only to successful
2805 except clauses); and if the current frame ever caught an exception,
2806 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2807 at the start of the current frame.
2808
2809*/
2810
Guido van Rossuma027efa1997-05-05 20:56:21 +00002811static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002812set_exc_info(PyThreadState *tstate,
2813 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002814{
2815 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002816 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002817
Guido van Rossuma027efa1997-05-05 20:56:21 +00002818 frame = tstate->frame;
2819 if (frame->f_exc_type == NULL) {
2820 /* This frame didn't catch an exception before */
2821 /* Save previous exception of this thread in this frame */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002822 if (tstate->exc_type == NULL) {
2823 Py_INCREF(Py_None);
2824 tstate->exc_type = Py_None;
2825 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002826 tmp_type = frame->f_exc_type;
2827 tmp_value = frame->f_exc_value;
2828 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002829 Py_XINCREF(tstate->exc_type);
2830 Py_XINCREF(tstate->exc_value);
2831 Py_XINCREF(tstate->exc_traceback);
2832 frame->f_exc_type = tstate->exc_type;
2833 frame->f_exc_value = tstate->exc_value;
2834 frame->f_exc_traceback = tstate->exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002835 Py_XDECREF(tmp_type);
2836 Py_XDECREF(tmp_value);
2837 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002838 }
2839 /* Set new exception for this thread */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002840 tmp_type = tstate->exc_type;
2841 tmp_value = tstate->exc_value;
2842 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002843 Py_XINCREF(type);
2844 Py_XINCREF(value);
2845 Py_XINCREF(tb);
2846 tstate->exc_type = type;
2847 tstate->exc_value = value;
2848 tstate->exc_traceback = tb;
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 /* For b/w compatibility */
2853 PySys_SetObject("exc_type", type);
2854 PySys_SetObject("exc_value", value);
2855 PySys_SetObject("exc_traceback", tb);
2856}
2857
2858static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002859reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002860{
2861 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002862 PyObject *tmp_type, *tmp_value, *tmp_tb;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002863 frame = tstate->frame;
2864 if (frame->f_exc_type != NULL) {
2865 /* This frame caught an exception */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002866 tmp_type = tstate->exc_type;
2867 tmp_value = tstate->exc_value;
2868 tmp_tb = tstate->exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002869 Py_XINCREF(frame->f_exc_type);
2870 Py_XINCREF(frame->f_exc_value);
2871 Py_XINCREF(frame->f_exc_traceback);
2872 tstate->exc_type = frame->f_exc_type;
2873 tstate->exc_value = frame->f_exc_value;
2874 tstate->exc_traceback = frame->f_exc_traceback;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002875 Py_XDECREF(tmp_type);
2876 Py_XDECREF(tmp_value);
2877 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002878 /* For b/w compatibility */
2879 PySys_SetObject("exc_type", frame->f_exc_type);
2880 PySys_SetObject("exc_value", frame->f_exc_value);
2881 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2882 }
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002883 tmp_type = frame->f_exc_type;
2884 tmp_value = frame->f_exc_value;
2885 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002886 frame->f_exc_type = NULL;
2887 frame->f_exc_value = NULL;
2888 frame->f_exc_traceback = NULL;
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}
2893
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002894/* Logic for the raise statement (too complicated for inlining).
2895 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002896static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002897do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002898{
Guido van Rossumd295f121998-04-09 21:39:57 +00002899 if (type == NULL) {
2900 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002901 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002902 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2903 value = tstate->exc_value;
2904 tb = tstate->exc_traceback;
2905 Py_XINCREF(type);
2906 Py_XINCREF(value);
2907 Py_XINCREF(tb);
2908 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002909
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002910 /* We support the following forms of raise:
2911 raise <class>, <classinstance>
2912 raise <class>, <argument tuple>
2913 raise <class>, None
2914 raise <class>, <argument>
2915 raise <classinstance>, None
2916 raise <string>, <object>
2917 raise <string>, None
2918
2919 An omitted second argument is the same as None.
2920
2921 In addition, raise <tuple>, <anything> is the same as
2922 raising the tuple's first item (and it better have one!);
2923 this rule is applied recursively.
2924
2925 Finally, an optional third argument can be supplied, which
2926 gives the traceback to be substituted (useful when
2927 re-raising an exception after examining it). */
2928
2929 /* First, check the traceback argument, replacing None with
2930 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002931 if (tb == Py_None) {
2932 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002933 tb = NULL;
2934 }
2935 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002936 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00002937 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002938 goto raise_error;
2939 }
2940
2941 /* Next, replace a missing value with None */
2942 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002943 value = Py_None;
2944 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002945 }
2946
2947 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00002948 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
2949 PyObject *tmp = type;
2950 type = PyTuple_GET_ITEM(type, 0);
2951 Py_INCREF(type);
2952 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002953 }
2954
Tim Petersafb2c802002-04-18 18:06:20 +00002955 if (PyString_CheckExact(type))
2956 /* Raising builtin string is deprecated but still allowed --
2957 * do nothing. Raising an instance of a new-style str
2958 * subclass is right out. */
Neal Norwitz37aa0662003-01-10 15:31:15 +00002959 PyErr_Warn(PyExc_PendingDeprecationWarning,
2960 "raising a string exception is deprecated");
Barry Warsaw4249f541997-08-22 21:26:19 +00002961
2962 else if (PyClass_Check(type))
2963 PyErr_NormalizeException(&type, &value, &tb);
2964
Guido van Rossumb209a111997-04-29 18:18:01 +00002965 else if (PyInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002966 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00002967 if (value != Py_None) {
2968 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002969 "instance exception may not have a separate value");
2970 goto raise_error;
2971 }
2972 else {
2973 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00002974 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002975 value = type;
Guido van Rossumb209a111997-04-29 18:18:01 +00002976 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
2977 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002978 }
2979 }
2980 else {
2981 /* Not something you can raise. You get an exception
2982 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00002983 PyErr_Format(PyExc_TypeError,
Neal Norwitz37aa0662003-01-10 15:31:15 +00002984 "exceptions must be classes, instances, or "
2985 "strings (deprecated), not %s",
2986 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002987 goto raise_error;
2988 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002989 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002990 if (tb == NULL)
2991 return WHY_EXCEPTION;
2992 else
2993 return WHY_RERAISE;
2994 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00002995 Py_XDECREF(value);
2996 Py_XDECREF(type);
2997 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002998 return WHY_EXCEPTION;
2999}
3000
Tim Petersd6d010b2001-06-21 02:49:55 +00003001/* Iterate v argcnt times and store the results on the stack (via decreasing
3002 sp). Return 1 for success, 0 if error. */
3003
Barry Warsawe42b18f1997-08-25 22:13:04 +00003004static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003005unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003006{
Tim Petersd6d010b2001-06-21 02:49:55 +00003007 int i = 0;
3008 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003009 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003010
Tim Petersd6d010b2001-06-21 02:49:55 +00003011 assert(v != NULL);
3012
3013 it = PyObject_GetIter(v);
3014 if (it == NULL)
3015 goto Error;
3016
3017 for (; i < argcnt; i++) {
3018 w = PyIter_Next(it);
3019 if (w == NULL) {
3020 /* Iterator done, via error or exhaustion. */
3021 if (!PyErr_Occurred()) {
3022 PyErr_Format(PyExc_ValueError,
3023 "need more than %d value%s to unpack",
3024 i, i == 1 ? "" : "s");
3025 }
3026 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003027 }
3028 *--sp = w;
3029 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003030
3031 /* We better have exhausted the iterator now. */
3032 w = PyIter_Next(it);
3033 if (w == NULL) {
3034 if (PyErr_Occurred())
3035 goto Error;
3036 Py_DECREF(it);
3037 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003038 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003039 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003040 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003041 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003042Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003043 for (; i > 0; i--, sp++)
3044 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003045 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003046 return 0;
3047}
3048
3049
Guido van Rossum96a42c81992-01-12 02:29:51 +00003050#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003051static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003052prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003053{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003054 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003055 if (PyObject_Print(v, stdout, 0) != 0)
3056 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003057 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003058 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003059}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003060#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003061
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003062static void
Fred Drake5755ce62001-06-27 19:19:46 +00003063call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003064{
Guido van Rossumb209a111997-04-29 18:18:01 +00003065 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003066 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003067 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003068 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003069 value = Py_None;
3070 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003071 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003072 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003073 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003074 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003075 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003076 }
Fred Drake5755ce62001-06-27 19:19:46 +00003077 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003078 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003079 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003080 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003081 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003082 Py_XDECREF(type);
3083 Py_XDECREF(value);
3084 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003085 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003086}
3087
Fred Drake4ec5d562001-10-04 19:26:43 +00003088static void
3089call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3090 int what)
3091{
3092 PyObject *type, *value, *traceback;
3093 int err;
3094 PyErr_Fetch(&type, &value, &traceback);
3095 err = call_trace(func, obj, frame, what, NULL);
3096 if (err == 0)
3097 PyErr_Restore(type, value, traceback);
3098 else {
3099 Py_XDECREF(type);
3100 Py_XDECREF(value);
3101 Py_XDECREF(traceback);
3102 }
3103}
3104
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003105static int
Fred Drake5755ce62001-06-27 19:19:46 +00003106call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3107 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003108{
Fred Drake5755ce62001-06-27 19:19:46 +00003109 register PyThreadState *tstate = frame->f_tstate;
3110 int result;
3111 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003112 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003113 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003114 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003115 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003116 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3117 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003118 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003119 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003120}
3121
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003122PyObject *
3123_PyEval_CallTracing(PyObject *func, PyObject *args)
3124{
3125 PyFrameObject *frame = PyEval_GetFrame();
3126 PyThreadState *tstate = frame->f_tstate;
3127 int save_tracing = tstate->tracing;
3128 int save_use_tracing = tstate->use_tracing;
3129 PyObject *result;
3130
3131 tstate->tracing = 0;
3132 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3133 || (tstate->c_profilefunc != NULL));
3134 result = PyObject_Call(func, args, NULL);
3135 tstate->tracing = save_tracing;
3136 tstate->use_tracing = save_use_tracing;
3137 return result;
3138}
3139
Michael W. Hudson006c7522002-11-08 13:08:46 +00003140static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003141maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003142 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3143 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003144{
3145 /* The theory of SET_LINENO-less tracing.
Tim Peters8a5c3c72004-04-05 19:36:21 +00003146
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003147 In a nutshell, we use the co_lnotab field of the code object
3148 to tell when execution has moved onto a different line.
3149
3150 As mentioned above, the basic idea is so set things up so
3151 that
3152
3153 *instr_lb <= frame->f_lasti < *instr_ub
3154
3155 is true so long as execution does not change lines.
3156
3157 This is all fairly simple. Digging the information out of
3158 co_lnotab takes some work, but is conceptually clear.
3159
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003160 Somewhat harder to explain is why we don't *always* call the
3161 line trace function when the above test fails.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003162
3163 Consider this code:
3164
3165 1: def f(a):
3166 2: if a:
3167 3: print 1
3168 4: else:
3169 5: print 2
3170
3171 which compiles to this:
3172
3173 2 0 LOAD_FAST 0 (a)
3174 3 JUMP_IF_FALSE 9 (to 15)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003175 6 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003176
3177 3 7 LOAD_CONST 1 (1)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003178 10 PRINT_ITEM
3179 11 PRINT_NEWLINE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003180 12 JUMP_FORWARD 6 (to 21)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003181 >> 15 POP_TOP
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003182
3183 5 16 LOAD_CONST 2 (2)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003184 19 PRINT_ITEM
3185 20 PRINT_NEWLINE
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003186 >> 21 LOAD_CONST 0 (None)
3187 24 RETURN_VALUE
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003188
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003189 If 'a' is false, execution will jump to instruction at offset
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003190 15 and the co_lnotab will claim that execution has moved to
3191 line 3. This is at best misleading. In this case we could
3192 associate the POP_TOP with line 4, but that doesn't make
3193 sense in all cases (I think).
3194
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003195 What we do is only call the line trace function if the co_lnotab
3196 indicates we have jumped to the *start* of a line, i.e. if the
3197 current instruction offset matches the offset given for the
3198 start of a line by the co_lnotab.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003199
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003200 This also takes care of the situation where 'a' is true.
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003201 Execution will jump from instruction offset 12 to offset 21.
3202 Then the co_lnotab would imply that execution has moved to line
3203 5, which is again misleading.
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003204
3205 Why do we set f_lineno when tracing? Well, consider the code
3206 above when 'a' is true. If stepping through this with 'n' in
3207 pdb, you would stop at line 1 with a "call" type event, then
3208 line events on lines 2 and 3, then a "return" type event -- but
3209 you would be shown line 5 during this event. This is a change
3210 from the behaviour in 2.2 and before, and I've found it
3211 confusing in practice. By setting and using f_lineno when
3212 tracing, one can report a line number different from that
3213 suggested by f_lasti on this one occasion where it's desirable.
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003214 */
3215
Michael W. Hudson006c7522002-11-08 13:08:46 +00003216 int result = 0;
3217
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003218 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003219 PyCodeObject* co = frame->f_code;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003220 int size, addr, line;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003221 unsigned char* p;
3222
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003223 size = PyString_GET_SIZE(co->co_lnotab) / 2;
3224 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003225
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003226 addr = 0;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003227 line = co->co_firstlineno;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003228
3229 /* possible optimization: if f->f_lasti == instr_ub
3230 (likely to be a common case) then we already know
3231 instr_lb -- if we stored the matching value of p
3232 somwhere we could skip the first while loop. */
3233
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003234 /* see comments in compile.c for the description of
3235 co_lnotab. A point to remember: increments to p
3236 should come in pairs -- although we don't care about
3237 the line increments here, treating them as byte
3238 increments gets confusing, to say the least. */
3239
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003240 while (size > 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003241 if (addr + *p > frame->f_lasti)
3242 break;
3243 addr += *p++;
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003244 if (*p) *instr_lb = addr;
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003245 line += *p++;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003246 --size;
3247 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003248
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003249 if (addr == frame->f_lasti) {
3250 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003251 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003252 PyTrace_LINE, Py_None);
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003253 }
Michael W. Hudsonca803a02002-10-03 09:53:11 +00003254
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003255 if (size > 0) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003256 while (--size >= 0) {
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003257 addr += *p++;
3258 if (*p++)
3259 break;
3260 }
3261 *instr_ub = addr;
3262 }
3263 else {
3264 *instr_ub = INT_MAX;
3265 }
3266 }
Armin Rigobf57a142004-03-22 19:24:58 +00003267 else if (frame->f_lasti <= *instr_prev) {
3268 /* jumping back in the same line forces a trace event */
Tim Peters8a5c3c72004-04-05 19:36:21 +00003269 result = call_trace(func, obj, frame,
Armin Rigobf57a142004-03-22 19:24:58 +00003270 PyTrace_LINE, Py_None);
3271 }
3272 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003273 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003274}
3275
Fred Drake5755ce62001-06-27 19:19:46 +00003276void
3277PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003278{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003279 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003280 PyObject *temp = tstate->c_profileobj;
3281 Py_XINCREF(arg);
3282 tstate->c_profilefunc = NULL;
3283 tstate->c_profileobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003284 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003285 Py_XDECREF(temp);
3286 tstate->c_profilefunc = func;
3287 tstate->c_profileobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003288 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003289}
3290
3291void
3292PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3293{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003294 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003295 PyObject *temp = tstate->c_traceobj;
3296 Py_XINCREF(arg);
3297 tstate->c_tracefunc = NULL;
3298 tstate->c_traceobj = NULL;
Fred Drake9e3ad782001-07-03 23:39:52 +00003299 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003300 Py_XDECREF(temp);
3301 tstate->c_tracefunc = func;
3302 tstate->c_traceobj = arg;
Fred Drake9e3ad782001-07-03 23:39:52 +00003303 tstate->use_tracing = ((func != NULL)
3304 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003305}
3306
Guido van Rossumb209a111997-04-29 18:18:01 +00003307PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003308PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003309{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003310 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003311 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003312 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003313 else
3314 return current_frame->f_builtins;
3315}
3316
Guido van Rossumb209a111997-04-29 18:18:01 +00003317PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003318PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003319{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003320 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003321 if (current_frame == NULL)
3322 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003323 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003324 return current_frame->f_locals;
3325}
3326
Guido van Rossumb209a111997-04-29 18:18:01 +00003327PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003328PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003329{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003330 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003331 if (current_frame == NULL)
3332 return NULL;
3333 else
3334 return current_frame->f_globals;
3335}
3336
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003337PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003338PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003339{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003340 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003341 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003342}
3343
Guido van Rossum6135a871995-01-09 17:53:26 +00003344int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003345PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003346{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003347 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003348 return current_frame == NULL ? 0 : current_frame->f_restricted;
3349}
3350
Guido van Rossumbe270261997-05-22 22:26:18 +00003351int
Tim Peters5ba58662001-07-16 02:29:45 +00003352PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003353{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003354 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003355 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003356
3357 if (current_frame != NULL) {
3358 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003359 const int compilerflags = codeflags & PyCF_MASK;
3360 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003361 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003362 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003363 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003364#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003365 if (codeflags & CO_GENERATOR_ALLOWED) {
3366 result = 1;
3367 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3368 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003369#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003370 }
3371 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003372}
3373
3374int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003375Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003376{
Guido van Rossumb209a111997-04-29 18:18:01 +00003377 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003378 if (f == NULL)
3379 return 0;
3380 if (!PyFile_SoftSpace(f, 0))
3381 return 0;
3382 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003383}
3384
Guido van Rossum3f5da241990-12-20 15:06:42 +00003385
Guido van Rossum681d79a1995-07-18 14:51:37 +00003386/* External interface to call any callable object.
3387 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003388
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003389#undef PyEval_CallObject
3390/* for backward compatibility: export this interface */
3391
Guido van Rossumb209a111997-04-29 18:18:01 +00003392PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003393PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003394{
Guido van Rossumb209a111997-04-29 18:18:01 +00003395 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003396}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003397#define PyEval_CallObject(func,arg) \
3398 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003399
Guido van Rossumb209a111997-04-29 18:18:01 +00003400PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003401PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003402{
Jeremy Hylton52820442001-01-03 23:52:36 +00003403 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003404
3405 if (arg == NULL)
Guido van Rossumb209a111997-04-29 18:18:01 +00003406 arg = PyTuple_New(0);
3407 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003408 PyErr_SetString(PyExc_TypeError,
3409 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003410 return NULL;
3411 }
3412 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003413 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003414
Guido van Rossumb209a111997-04-29 18:18:01 +00003415 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003416 PyErr_SetString(PyExc_TypeError,
3417 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003418 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003419 return NULL;
3420 }
3421
Tim Peters6d6c1a32001-08-02 04:15:00 +00003422 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003423 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003424 return result;
3425}
3426
Tim Peters6d6c1a32001-08-02 04:15:00 +00003427char *
3428PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003429{
3430 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003431 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003432 else if (PyFunction_Check(func))
3433 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3434 else if (PyCFunction_Check(func))
3435 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3436 else if (PyClass_Check(func))
3437 return PyString_AsString(((PyClassObject*)func)->cl_name);
3438 else if (PyInstance_Check(func)) {
3439 return PyString_AsString(
3440 ((PyInstanceObject*)func)->in_class->cl_name);
3441 } else {
3442 return func->ob_type->tp_name;
3443 }
3444}
3445
Tim Peters6d6c1a32001-08-02 04:15:00 +00003446char *
3447PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003448{
3449 if (PyMethod_Check(func))
3450 return "()";
3451 else if (PyFunction_Check(func))
3452 return "()";
3453 else if (PyCFunction_Check(func))
3454 return "()";
3455 else if (PyClass_Check(func))
3456 return " constructor";
3457 else if (PyInstance_Check(func)) {
3458 return " instance";
3459 } else {
3460 return " object";
3461 }
3462}
3463
Jeremy Hylton52820442001-01-03 23:52:36 +00003464#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
3465
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003466static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003467err_args(PyObject *func, int flags, int nargs)
3468{
3469 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003470 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003471 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003472 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003473 nargs);
3474 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003475 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003476 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003477 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003478 nargs);
3479}
3480
Nicholas Bastind858a772004-06-25 23:31:06 +00003481#define C_TRACE(call) \
3482if (tstate->use_tracing && tstate->c_profilefunc) { \
3483 if (call_trace(tstate->c_profilefunc, \
3484 tstate->c_profileobj, \
3485 tstate->frame, PyTrace_C_CALL, \
3486 func)) \
3487 { return NULL; } \
3488 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003489 if (tstate->c_profilefunc != NULL) { \
Nicholas Bastind858a772004-06-25 23:31:06 +00003490 if (x == NULL) { \
3491 if (call_trace (tstate->c_profilefunc, \
3492 tstate->c_profileobj, \
3493 tstate->frame, PyTrace_C_EXCEPTION, \
3494 func)) \
3495 { return NULL; } \
3496 } else { \
3497 if (call_trace(tstate->c_profilefunc, \
3498 tstate->c_profileobj, \
3499 tstate->frame, PyTrace_C_RETURN, \
3500 func)) \
3501 { return NULL; } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003502 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003503 } \
3504} else { \
3505 call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003506 }
3507
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003508static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003509call_function(PyObject ***pp_stack, int oparg
3510#ifdef WITH_TSC
3511 , uint64* pintr0, uint64* pintr1
3512#endif
3513 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003514{
3515 int na = oparg & 0xff;
3516 int nk = (oparg>>8) & 0xff;
3517 int n = na + 2 * nk;
3518 PyObject **pfunc = (*pp_stack) - n - 1;
3519 PyObject *func = *pfunc;
3520 PyObject *x, *w;
3521
Jeremy Hylton985eba52003-02-05 23:13:00 +00003522 /* Always dispatch PyCFunction first, because these are
3523 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003524 */
3525 if (PyCFunction_Check(func) && nk == 0) {
3526 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003527 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003528
3529 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003530 if (flags & (METH_NOARGS | METH_O)) {
3531 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3532 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003533 if (flags & METH_NOARGS && na == 0) {
Nicholas Bastind858a772004-06-25 23:31:06 +00003534 C_TRACE(x=(*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003535 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003536 else if (flags & METH_O && na == 1) {
3537 PyObject *arg = EXT_POP(*pp_stack);
Nicholas Bastind858a772004-06-25 23:31:06 +00003538 C_TRACE(x=(*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003539 Py_DECREF(arg);
3540 }
3541 else {
3542 err_args(func, flags, na);
3543 x = NULL;
3544 }
3545 }
3546 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003547 PyObject *callargs;
3548 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003549 READ_TIMESTAMP(*pintr0);
Nicholas Bastind858a772004-06-25 23:31:06 +00003550 C_TRACE(x=PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003551 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003552 Py_XDECREF(callargs);
3553 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003554 } else {
3555 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3556 /* optimize access to bound methods */
3557 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003558 PCALL(PCALL_METHOD);
3559 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003560 Py_INCREF(self);
3561 func = PyMethod_GET_FUNCTION(func);
3562 Py_INCREF(func);
3563 Py_DECREF(*pfunc);
3564 *pfunc = self;
3565 na++;
3566 n++;
3567 } else
3568 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003569 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003570 if (PyFunction_Check(func))
3571 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003572 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003573 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003574 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003575 Py_DECREF(func);
3576 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003577
Jeremy Hylton985eba52003-02-05 23:13:00 +00003578 /* What does this do? */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003579 while ((*pp_stack) > pfunc) {
3580 w = EXT_POP(*pp_stack);
3581 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003582 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003583 }
3584 return x;
3585}
3586
Jeremy Hylton192690e2002-08-16 18:36:11 +00003587/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003588 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003589 For the simplest case -- a function that takes only positional
3590 arguments and is called with only positional arguments -- it
3591 inlines the most primitive frame setup code from
3592 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3593 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003594*/
3595
3596static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003597fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003598{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003599 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003600 PyObject *globals = PyFunction_GET_GLOBALS(func);
3601 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3602 PyObject **d = NULL;
3603 int nd = 0;
3604
Jeremy Hylton985eba52003-02-05 23:13:00 +00003605 PCALL(PCALL_FUNCTION);
3606 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003607 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003608 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3609 PyFrameObject *f;
3610 PyObject *retval = NULL;
3611 PyThreadState *tstate = PyThreadState_GET();
3612 PyObject **fastlocals, **stack;
3613 int i;
3614
3615 PCALL(PCALL_FASTER_FUNCTION);
3616 assert(globals != NULL);
3617 /* XXX Perhaps we should create a specialized
3618 PyFrame_New() that doesn't take locals, but does
3619 take builtins without sanity checking them.
3620 */
3621 f = PyFrame_New(tstate, co, globals, NULL);
3622 if (f == NULL)
3623 return NULL;
3624
3625 fastlocals = f->f_localsplus;
3626 stack = (*pp_stack) - n;
3627
3628 for (i = 0; i < n; i++) {
3629 Py_INCREF(*stack);
3630 fastlocals[i] = *stack++;
3631 }
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003632 retval = PyEval_EvalFrame(f);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003633 assert(tstate != NULL);
3634 ++tstate->recursion_depth;
3635 Py_DECREF(f);
3636 --tstate->recursion_depth;
3637 return retval;
3638 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003639 if (argdefs != NULL) {
3640 d = &PyTuple_GET_ITEM(argdefs, 0);
3641 nd = ((PyTupleObject *)argdefs)->ob_size;
3642 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003643 return PyEval_EvalCodeEx(co, globals,
3644 (PyObject *)NULL, (*pp_stack)-n, na,
3645 (*pp_stack)-2*nk, nk, d, nd,
3646 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003647}
3648
3649static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003650update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3651 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003652{
3653 PyObject *kwdict = NULL;
3654 if (orig_kwdict == NULL)
3655 kwdict = PyDict_New();
3656 else {
3657 kwdict = PyDict_Copy(orig_kwdict);
3658 Py_DECREF(orig_kwdict);
3659 }
3660 if (kwdict == NULL)
3661 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003662 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003663 int err;
3664 PyObject *value = EXT_POP(*pp_stack);
3665 PyObject *key = EXT_POP(*pp_stack);
3666 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003667 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003668 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003669 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003670 PyEval_GetFuncName(func),
3671 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003672 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003673 Py_DECREF(key);
3674 Py_DECREF(value);
3675 Py_DECREF(kwdict);
3676 return NULL;
3677 }
3678 err = PyDict_SetItem(kwdict, key, value);
3679 Py_DECREF(key);
3680 Py_DECREF(value);
3681 if (err) {
3682 Py_DECREF(kwdict);
3683 return NULL;
3684 }
3685 }
3686 return kwdict;
3687}
3688
3689static PyObject *
3690update_star_args(int nstack, int nstar, PyObject *stararg,
3691 PyObject ***pp_stack)
3692{
3693 PyObject *callargs, *w;
3694
3695 callargs = PyTuple_New(nstack + nstar);
3696 if (callargs == NULL) {
3697 return NULL;
3698 }
3699 if (nstar) {
3700 int i;
3701 for (i = 0; i < nstar; i++) {
3702 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3703 Py_INCREF(a);
3704 PyTuple_SET_ITEM(callargs, nstack + i, a);
3705 }
3706 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003707 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003708 w = EXT_POP(*pp_stack);
3709 PyTuple_SET_ITEM(callargs, nstack, w);
3710 }
3711 return callargs;
3712}
3713
3714static PyObject *
3715load_args(PyObject ***pp_stack, int na)
3716{
3717 PyObject *args = PyTuple_New(na);
3718 PyObject *w;
3719
3720 if (args == NULL)
3721 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003722 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003723 w = EXT_POP(*pp_stack);
3724 PyTuple_SET_ITEM(args, na, w);
3725 }
3726 return args;
3727}
3728
3729static PyObject *
3730do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3731{
3732 PyObject *callargs = NULL;
3733 PyObject *kwdict = NULL;
3734 PyObject *result = NULL;
3735
3736 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003737 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003738 if (kwdict == NULL)
3739 goto call_fail;
3740 }
3741 callargs = load_args(pp_stack, na);
3742 if (callargs == NULL)
3743 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003744#ifdef CALL_PROFILE
3745 /* At this point, we have to look at the type of func to
3746 update the call stats properly. Do it here so as to avoid
3747 exposing the call stats machinery outside ceval.c
3748 */
3749 if (PyFunction_Check(func))
3750 PCALL(PCALL_FUNCTION);
3751 else if (PyMethod_Check(func))
3752 PCALL(PCALL_METHOD);
3753 else if (PyType_Check(func))
3754 PCALL(PCALL_TYPE);
3755 else
3756 PCALL(PCALL_OTHER);
3757#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003758 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003759 call_fail:
3760 Py_XDECREF(callargs);
3761 Py_XDECREF(kwdict);
3762 return result;
3763}
3764
3765static PyObject *
3766ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3767{
3768 int nstar = 0;
3769 PyObject *callargs = NULL;
3770 PyObject *stararg = NULL;
3771 PyObject *kwdict = NULL;
3772 PyObject *result = NULL;
3773
3774 if (flags & CALL_FLAG_KW) {
3775 kwdict = EXT_POP(*pp_stack);
3776 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003777 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003778 "%s%s argument after ** "
3779 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003780 PyEval_GetFuncName(func),
3781 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003782 goto ext_call_fail;
3783 }
3784 }
3785 if (flags & CALL_FLAG_VAR) {
3786 stararg = EXT_POP(*pp_stack);
3787 if (!PyTuple_Check(stararg)) {
3788 PyObject *t = NULL;
3789 t = PySequence_Tuple(stararg);
3790 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003791 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3792 PyErr_Format(PyExc_TypeError,
3793 "%s%s argument after * "
3794 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003795 PyEval_GetFuncName(func),
3796 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003797 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003798 goto ext_call_fail;
3799 }
3800 Py_DECREF(stararg);
3801 stararg = t;
3802 }
3803 nstar = PyTuple_GET_SIZE(stararg);
3804 }
3805 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003806 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003807 if (kwdict == NULL)
3808 goto ext_call_fail;
3809 }
3810 callargs = update_star_args(na, nstar, stararg, pp_stack);
3811 if (callargs == NULL)
3812 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003813#ifdef CALL_PROFILE
3814 /* At this point, we have to look at the type of func to
3815 update the call stats properly. Do it here so as to avoid
3816 exposing the call stats machinery outside ceval.c
3817 */
3818 if (PyFunction_Check(func))
3819 PCALL(PCALL_FUNCTION);
3820 else if (PyMethod_Check(func))
3821 PCALL(PCALL_METHOD);
3822 else if (PyType_Check(func))
3823 PCALL(PCALL_TYPE);
3824 else
3825 PCALL(PCALL_OTHER);
3826#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003827 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003828 ext_call_fail:
3829 Py_XDECREF(callargs);
3830 Py_XDECREF(kwdict);
3831 Py_XDECREF(stararg);
3832 return result;
3833}
3834
Tim Peterscb479e72001-12-16 19:11:44 +00003835/* Extract a slice index from a PyInt or PyLong, and store in *pi.
3836 Silently reduce values larger than INT_MAX to INT_MAX, and silently
3837 boost values less than -INT_MAX to 0. Return 0 on error, 1 on success.
3838*/
Tim Petersb5196382001-12-16 19:44:20 +00003839/* Note: If v is NULL, return success without storing into *pi. This
3840 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3841 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003842*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003843int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003844_PyEval_SliceIndex(PyObject *v, int *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003845{
Tim Petersb5196382001-12-16 19:44:20 +00003846 if (v != NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003847 long x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003848 if (PyInt_Check(v)) {
3849 x = PyInt_AsLong(v);
3850 } else if (PyLong_Check(v)) {
3851 x = PyLong_AsLong(v);
3852 if (x==-1 && PyErr_Occurred()) {
3853 PyObject *long_zero;
Guido van Rossumac7be682001-01-17 15:42:30 +00003854 int cmp;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003855
Guido van Rossumac7be682001-01-17 15:42:30 +00003856 if (!PyErr_ExceptionMatches(
3857 PyExc_OverflowError)) {
3858 /* It's not an overflow error, so just
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003859 signal an error */
Guido van Rossum20c6add2000-05-08 14:06:50 +00003860 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003861 }
3862
Guido van Rossumac7be682001-01-17 15:42:30 +00003863 /* Clear the OverflowError */
3864 PyErr_Clear();
3865
3866 /* It's an overflow error, so we need to
3867 check the sign of the long integer,
Tim Peters8a5c3c72004-04-05 19:36:21 +00003868 set the value to INT_MAX or -INT_MAX,
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003869 and clear the error. */
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003870
3871 /* Create a long integer with a value of 0 */
Guido van Rossumac7be682001-01-17 15:42:30 +00003872 long_zero = PyLong_FromLong(0L);
Tim Peterscb479e72001-12-16 19:11:44 +00003873 if (long_zero == NULL)
3874 return 0;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003875
3876 /* Check sign */
Guido van Rossumac7be682001-01-17 15:42:30 +00003877 cmp = PyObject_RichCompareBool(v, long_zero,
3878 Py_GT);
3879 Py_DECREF(long_zero);
3880 if (cmp < 0)
3881 return 0;
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003882 else if (cmp)
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003883 x = INT_MAX;
3884 else
Michael W. Hudsone46d1552003-02-27 14:50:34 +00003885 x = -INT_MAX;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003886 }
3887 } else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003888 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003889 "slice indices must be integers");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003890 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003892 /* Truncate -- very long indices are truncated anyway */
3893 if (x > INT_MAX)
3894 x = INT_MAX;
3895 else if (x < -INT_MAX)
Michael W. Hudsoncbd6fb92002-11-06 15:17:32 +00003896 x = -INT_MAX;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003897 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003898 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003899 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003900}
3901
Guido van Rossum50d756e2001-08-18 17:43:36 +00003902#undef ISINT
3903#define ISINT(x) ((x) == NULL || PyInt_Check(x) || PyLong_Check(x))
3904
Guido van Rossumb209a111997-04-29 18:18:01 +00003905static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003906apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003907{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003908 PyTypeObject *tp = u->ob_type;
3909 PySequenceMethods *sq = tp->tp_as_sequence;
3910
3911 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3912 int ilow = 0, ihigh = INT_MAX;
3913 if (!_PyEval_SliceIndex(v, &ilow))
3914 return NULL;
3915 if (!_PyEval_SliceIndex(w, &ihigh))
3916 return NULL;
3917 return PySequence_GetSlice(u, ilow, ihigh);
3918 }
3919 else {
3920 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003921 if (slice != NULL) {
3922 PyObject *res = PyObject_GetItem(u, slice);
3923 Py_DECREF(slice);
3924 return res;
3925 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003926 else
3927 return NULL;
3928 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003929}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003930
3931static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003932assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3933 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003934{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003935 PyTypeObject *tp = u->ob_type;
3936 PySequenceMethods *sq = tp->tp_as_sequence;
3937
3938 if (sq && sq->sq_slice && ISINT(v) && ISINT(w)) {
3939 int ilow = 0, ihigh = INT_MAX;
3940 if (!_PyEval_SliceIndex(v, &ilow))
3941 return -1;
3942 if (!_PyEval_SliceIndex(w, &ihigh))
3943 return -1;
3944 if (x == NULL)
3945 return PySequence_DelSlice(u, ilow, ihigh);
3946 else
3947 return PySequence_SetSlice(u, ilow, ihigh, x);
3948 }
3949 else {
3950 PyObject *slice = PySlice_New(v, w, NULL);
3951 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003952 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003953 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003954 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003955 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003956 res = PyObject_DelItem(u, slice);
3957 Py_DECREF(slice);
3958 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003959 }
3960 else
3961 return -1;
3962 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003963}
3964
Guido van Rossumb209a111997-04-29 18:18:01 +00003965static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003966cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003967{
Guido van Rossumac7be682001-01-17 15:42:30 +00003968 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003969 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003970 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003971 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003972 break;
3973 case PyCmp_IS_NOT:
3974 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003975 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003976 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003977 res = PySequence_Contains(w, v);
3978 if (res < 0)
3979 return NULL;
3980 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003981 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003982 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003983 if (res < 0)
3984 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003985 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003986 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003987 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00003988 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003989 break;
3990 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003991 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003992 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003993 v = res ? Py_True : Py_False;
3994 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003995 return v;
3996}
3997
Thomas Wouters52152252000-08-17 22:55:00 +00003998static PyObject *
3999import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004000{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004001 PyObject *x;
4002
4003 x = PyObject_GetAttr(v, name);
4004 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004005 PyErr_Format(PyExc_ImportError,
4006 "cannot import name %.230s",
4007 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004008 }
Thomas Wouters52152252000-08-17 22:55:00 +00004009 return x;
4010}
Guido van Rossumac7be682001-01-17 15:42:30 +00004011
Thomas Wouters52152252000-08-17 22:55:00 +00004012static int
4013import_all_from(PyObject *locals, PyObject *v)
4014{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004015 PyObject *all = PyObject_GetAttrString(v, "__all__");
4016 PyObject *dict, *name, *value;
4017 int skip_leading_underscores = 0;
4018 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004019
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004020 if (all == NULL) {
4021 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4022 return -1; /* Unexpected error */
4023 PyErr_Clear();
4024 dict = PyObject_GetAttrString(v, "__dict__");
4025 if (dict == NULL) {
4026 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4027 return -1;
4028 PyErr_SetString(PyExc_ImportError,
4029 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004030 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004031 }
4032 all = PyMapping_Keys(dict);
4033 Py_DECREF(dict);
4034 if (all == NULL)
4035 return -1;
4036 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004037 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004038
4039 for (pos = 0, err = 0; ; pos++) {
4040 name = PySequence_GetItem(all, pos);
4041 if (name == NULL) {
4042 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4043 err = -1;
4044 else
4045 PyErr_Clear();
4046 break;
4047 }
4048 if (skip_leading_underscores &&
4049 PyString_Check(name) &&
4050 PyString_AS_STRING(name)[0] == '_')
4051 {
4052 Py_DECREF(name);
4053 continue;
4054 }
4055 value = PyObject_GetAttr(v, name);
4056 if (value == NULL)
4057 err = -1;
4058 else
4059 err = PyDict_SetItem(locals, name, value);
4060 Py_DECREF(name);
4061 Py_XDECREF(value);
4062 if (err != 0)
4063 break;
4064 }
4065 Py_DECREF(all);
4066 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004067}
4068
Guido van Rossumb209a111997-04-29 18:18:01 +00004069static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004070build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004071{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004072 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004073
4074 if (PyDict_Check(methods))
4075 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004076 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004077 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004078 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4079 base = PyTuple_GET_ITEM(bases, 0);
4080 metaclass = PyObject_GetAttrString(base, "__class__");
4081 if (metaclass == NULL) {
4082 PyErr_Clear();
4083 metaclass = (PyObject *)base->ob_type;
4084 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004085 }
4086 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004087 else {
4088 PyObject *g = PyEval_GetGlobals();
4089 if (g != NULL && PyDict_Check(g))
4090 metaclass = PyDict_GetItemString(g, "__metaclass__");
4091 if (metaclass == NULL)
4092 metaclass = (PyObject *) &PyClass_Type;
4093 Py_INCREF(metaclass);
4094 }
4095 result = PyObject_CallFunction(metaclass, "OOO", name, bases, methods);
4096 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004097 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4098 /* A type error here likely means that the user passed
4099 in a base that was not a class (such the random module
4100 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004101 by augmenting the error message with more information.*/
4102
4103 PyObject *ptype, *pvalue, *ptraceback;
4104
4105 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4106 if (PyString_Check(pvalue)) {
4107 PyObject *newmsg;
4108 newmsg = PyString_FromFormat(
4109 "Error when calling the metaclass bases\n %s",
4110 PyString_AS_STRING(pvalue));
4111 if (newmsg != NULL) {
4112 Py_DECREF(pvalue);
4113 pvalue = newmsg;
4114 }
4115 }
4116 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004117 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004118 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004119}
4120
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004121static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004122exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4123 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004124{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004125 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004126 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004127 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004128
Guido van Rossumb209a111997-04-29 18:18:01 +00004129 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4130 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004131 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004132 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004133 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004134 locals = PyTuple_GetItem(prog, 2);
4135 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004136 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004137 if (globals == Py_None) {
4138 globals = PyEval_GetGlobals();
4139 if (locals == Py_None) {
4140 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004141 plain = 1;
4142 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004143 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004144 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004145 locals = globals;
Guido van Rossumb209a111997-04-29 18:18:01 +00004146 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004147 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004148 !PyCode_Check(prog) &&
4149 !PyFile_Check(prog)) {
4150 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004151 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004152 return -1;
4153 }
Fred Drake661ea262000-10-24 19:57:45 +00004154 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004155 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004156 "exec: arg 2 must be a dictionary or None");
4157 return -1;
4158 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004159 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004160 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004161 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004162 return -1;
4163 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004164 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004165 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004166 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004167 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4168 PyErr_SetString(PyExc_TypeError,
4169 "code object passed to exec may not contain free variables");
4170 return -1;
4171 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004172 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004173 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004174 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004175 FILE *fp = PyFile_AsFile(prog);
4176 char *name = PyString_AsString(PyFile_Name(prog));
Tim Peters5ba58662001-07-16 02:29:45 +00004177 PyCompilerFlags cf;
4178 cf.cf_flags = 0;
4179 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004180 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004181 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004182 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004183 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004184 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004185 }
4186 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004187 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004188 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004189 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004190 cf.cf_flags = 0;
4191#ifdef Py_USING_UNICODE
4192 if (PyUnicode_Check(prog)) {
4193 tmp = PyUnicode_AsUTF8String(prog);
4194 if (tmp == NULL)
4195 return -1;
4196 prog = tmp;
4197 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4198 }
4199#endif
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004200 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004201 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004202 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004203 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004204 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004205 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004206 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004207 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004208 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004209 if (plain)
4210 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004211 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004212 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004213 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004214 return 0;
4215}
Guido van Rossum24c13741995-02-14 09:42:43 +00004216
Guido van Rossumac7be682001-01-17 15:42:30 +00004217static void
Paul Prescode68140d2000-08-30 20:25:01 +00004218format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4219{
4220 char *obj_str;
4221
4222 if (!obj)
4223 return;
4224
4225 obj_str = PyString_AsString(obj);
4226 if (!obj_str)
4227 return;
4228
4229 PyErr_Format(exc, format_str, obj_str);
4230}
Guido van Rossum950361c1997-01-24 13:49:28 +00004231
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004232static PyObject *
4233string_concatenate(PyObject *v, PyObject *w,
4234 PyFrameObject *f, unsigned char *next_instr)
4235{
4236 /* This function implements 'variable += expr' when both arguments
4237 are strings. */
4238
4239 if (v->ob_refcnt == 2) {
4240 /* In the common case, there are 2 references to the value
4241 * stored in 'variable' when the += is performed: one on the
4242 * value stack (in 'v') and one still stored in the 'variable'.
4243 * We try to delete the variable now to reduce the refcnt to 1.
4244 */
4245 switch (*next_instr) {
4246 case STORE_FAST:
4247 {
4248 int oparg = PEEKARG();
4249 PyObject **fastlocals = f->f_localsplus;
4250 if (GETLOCAL(oparg) == v)
4251 SETLOCAL(oparg, NULL);
4252 break;
4253 }
4254 case STORE_DEREF:
4255 {
4256 PyObject **freevars = f->f_localsplus + f->f_nlocals;
4257 PyObject *c = freevars[PEEKARG()];
4258 if (PyCell_GET(c) == v)
4259 PyCell_Set(c, NULL);
4260 break;
4261 }
4262 case STORE_NAME:
4263 {
4264 PyObject *names = f->f_code->co_names;
4265 PyObject *name = GETITEM(names, PEEKARG());
4266 PyObject *locals = f->f_locals;
4267 if (PyDict_CheckExact(locals) &&
4268 PyDict_GetItem(locals, name) == v) {
4269 if (PyDict_DelItem(locals, name) != 0) {
4270 PyErr_Clear();
4271 }
4272 }
4273 break;
4274 }
4275 }
4276 }
4277
Armin Rigo618fbf52004-08-07 20:58:32 +00004278 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004279 /* Now we own the last reference to 'v', so we can resize it
4280 * in-place.
4281 */
4282 int v_len = PyString_GET_SIZE(v);
4283 int w_len = PyString_GET_SIZE(w);
4284 if (_PyString_Resize(&v, v_len + w_len) != 0) {
4285 /* XXX if _PyString_Resize() fails, 'v' has been
4286 * deallocated so it cannot be put back into 'variable'.
4287 * The MemoryError is raised when there is no value in
4288 * 'variable', which might (very remotely) be a cause
4289 * of incompatibilities.
4290 */
4291 return NULL;
4292 }
4293 /* copy 'w' into the newly allocated area of 'v' */
4294 memcpy(PyString_AS_STRING(v) + v_len,
4295 PyString_AS_STRING(w), w_len);
4296 return v;
4297 }
4298 else {
4299 /* When in-place resizing is not an option. */
4300 PyString_Concat(&v, w);
4301 return v;
4302 }
4303}
4304
Guido van Rossum950361c1997-01-24 13:49:28 +00004305#ifdef DYNAMIC_EXECUTION_PROFILE
4306
Skip Montanarof118cb12001-10-15 20:51:38 +00004307static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004308getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004309{
4310 int i;
4311 PyObject *l = PyList_New(256);
4312 if (l == NULL) return NULL;
4313 for (i = 0; i < 256; i++) {
4314 PyObject *x = PyInt_FromLong(a[i]);
4315 if (x == NULL) {
4316 Py_DECREF(l);
4317 return NULL;
4318 }
4319 PyList_SetItem(l, i, x);
4320 }
4321 for (i = 0; i < 256; i++)
4322 a[i] = 0;
4323 return l;
4324}
4325
4326PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004327_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004328{
4329#ifndef DXPAIRS
4330 return getarray(dxp);
4331#else
4332 int i;
4333 PyObject *l = PyList_New(257);
4334 if (l == NULL) return NULL;
4335 for (i = 0; i < 257; i++) {
4336 PyObject *x = getarray(dxpairs[i]);
4337 if (x == NULL) {
4338 Py_DECREF(l);
4339 return NULL;
4340 }
4341 PyList_SetItem(l, i, x);
4342 }
4343 return l;
4344#endif
4345}
4346
4347#endif