blob: f5ebb8eded165b043928fe9eda7fc68005138ee9 [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
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
31 section should work for GCC on any PowerPC platform,
32 irrespective of OS. POWER? Who knows :-) */
33
Michael W. Hudson75eabd22005-01-18 15:56:11 +000034#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000035
36static void
37ppc_getcounter(uint64 *v)
38{
39 register unsigned long tbu, tb, tbu2;
40
41 loop:
42 asm volatile ("mftbu %0" : "=r" (tbu) );
43 asm volatile ("mftb %0" : "=r" (tb) );
44 asm volatile ("mftbu %0" : "=r" (tbu2));
45 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
46
Thomas Wouters477c8d52006-05-27 19:21:47 +000047 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000048 compiled better by GCC than any other way I tried. */
49 ((long*)(v))[0] = tbu;
50 ((long*)(v))[1] = tb;
51}
52
Michael W. Hudson75eabd22005-01-18 15:56:11 +000053#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000054
Michael W. Hudson75eabd22005-01-18 15:56:11 +000055#define READ_TIMESTAMP(val) \
56 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
58#endif
59
Thomas Wouters477c8d52006-05-27 19:21:47 +000060void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000061 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
62{
63 uint64 intr, inst, loop;
64 PyThreadState *tstate = PyThreadState_Get();
65 if (!tstate->interp->tscdump)
66 return;
67 intr = intr1 - intr0;
68 inst = inst1 - inst0 - intr;
69 loop = loop1 - loop0 - intr;
70 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
71 opcode, ticked, inst, loop);
72}
Michael W. Hudson800ba232004-08-12 18:19:17 +000073
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000074#endif
75
Guido van Rossum04691fc1992-08-12 15:35:34 +000076/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000077/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000078
Guido van Rossum408027e1996-12-30 16:17:54 +000079#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000080/* For debugging the interpreter: */
81#define LLTRACE 1 /* Low-level trace feature */
82#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000083#endif
84
Jeremy Hylton52820442001-01-03 23:52:36 +000085typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000086
Guido van Rossum374a9221991-04-04 10:40:29 +000087/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000088#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +000089static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000090#else
Thomas Wouters477c8d52006-05-27 19:21:47 +000091static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000093static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
94static PyObject * do_call(PyObject *, PyObject ***, int, int);
95static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
96static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
97static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
98static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000099#define CALL_FLAG_VAR 1
100#define CALL_FLAG_KW 2
101
Guido van Rossum0a066c01992-03-27 17:29:15 +0000102#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000103static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000104static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000105#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000106static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
107 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +0000108static void call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000109 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000110static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000111static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000112 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000113
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000115static int assign_slice(PyObject *, PyObject *,
116 PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * cmp_outcome(int, PyObject *, PyObject *);
118static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000119static int import_all_from(PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000120static PyObject * build_class(PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000121static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
122static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000123static void format_exc_check_arg(PyObject *, char *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000125 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000126
Paul Prescode68140d2000-08-30 20:25:01 +0000127#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000128 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000129#define GLOBAL_NAME_ERROR_MSG \
130 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000131#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000132 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000133#define UNBOUNDFREE_ERROR_MSG \
134 "free variable '%.200s' referenced before assignment" \
135 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000136
Guido van Rossum950361c1997-01-24 13:49:28 +0000137/* Dynamic execution profile */
138#ifdef DYNAMIC_EXECUTION_PROFILE
139#ifdef DXPAIRS
140static long dxpairs[257][256];
141#define dxp dxpairs[256]
142#else
143static long dxp[256];
144#endif
145#endif
146
Jeremy Hylton985eba52003-02-05 23:13:00 +0000147/* Function call profile */
148#ifdef CALL_PROFILE
149#define PCALL_NUM 11
150static int pcall[PCALL_NUM];
151
152#define PCALL_ALL 0
153#define PCALL_FUNCTION 1
154#define PCALL_FAST_FUNCTION 2
155#define PCALL_FASTER_FUNCTION 3
156#define PCALL_METHOD 4
157#define PCALL_BOUND_METHOD 5
158#define PCALL_CFUNCTION 6
159#define PCALL_TYPE 7
160#define PCALL_GENERATOR 8
161#define PCALL_OTHER 9
162#define PCALL_POP 10
163
164/* Notes about the statistics
165
166 PCALL_FAST stats
167
168 FAST_FUNCTION means no argument tuple needs to be created.
169 FASTER_FUNCTION means that the fast-path frame setup code is used.
170
171 If there is a method call where the call can be optimized by changing
172 the argument tuple and calling the function directly, it gets recorded
173 twice.
174
175 As a result, the relationship among the statistics appears to be
176 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
177 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
178 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
179 PCALL_METHOD > PCALL_BOUND_METHOD
180*/
181
182#define PCALL(POS) pcall[POS]++
183
184PyObject *
185PyEval_GetCallStats(PyObject *self)
186{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000187 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000188 pcall[0], pcall[1], pcall[2], pcall[3],
189 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000191}
192#else
193#define PCALL(O)
194
195PyObject *
196PyEval_GetCallStats(PyObject *self)
197{
198 Py_INCREF(Py_None);
199 return Py_None;
200}
201#endif
202
Tim Peters5ca576e2001-06-18 22:08:13 +0000203
Guido van Rossume59214e1994-08-30 08:01:59 +0000204#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000205
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000206#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000207#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000208#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000209#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000210
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000211static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000212static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000213
Tim Peters7f468f22004-10-11 02:40:51 +0000214int
215PyEval_ThreadsInitialized(void)
216{
217 return interpreter_lock != 0;
218}
219
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000223 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000224 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000225 interpreter_lock = PyThread_allocate_lock();
226 PyThread_acquire_lock(interpreter_lock, 1);
227 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000229
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000230void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000231PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000233 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234}
235
236void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000239 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240}
241
242void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000244{
245 if (tstate == NULL)
246 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000247 /* Check someone has called PyEval_InitThreads() to create the lock */
248 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000249 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000250 if (PyThreadState_Swap(tstate) != NULL)
251 Py_FatalError(
252 "PyEval_AcquireThread: non-NULL old thread state");
253}
254
255void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000256PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000257{
258 if (tstate == NULL)
259 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
260 if (PyThreadState_Swap(NULL) != tstate)
261 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000262 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000263}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000264
265/* This function is called from PyOS_AfterFork to ensure that newly
266 created child processes don't hold locks referring to threads which
267 are not running in the child process. (This could also be done using
268 pthread_atfork mechanism, at least for the pthreads implementation.) */
269
270void
271PyEval_ReInitThreads(void)
272{
273 if (!interpreter_lock)
274 return;
275 /*XXX Can't use PyThread_free_lock here because it does too
276 much error-checking. Doing this cleanly would require
277 adding a new function to each thread_*.h. Instead, just
278 create a new lock and waste a little bit of memory */
279 interpreter_lock = PyThread_allocate_lock();
280 PyThread_acquire_lock(interpreter_lock, 1);
281 main_thread = PyThread_get_thread_ident();
282}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283#endif
284
Guido van Rossumff4949e1992-08-05 19:58:53 +0000285/* Functions save_thread and restore_thread are always defined so
286 dynamically loaded modules needn't be compiled separately for use
287 with and without threads: */
288
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000289PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000290PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000291{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000292 PyThreadState *tstate = PyThreadState_Swap(NULL);
293 if (tstate == NULL)
294 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000295#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000296 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000297 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000299 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000300}
301
302void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000305 if (tstate == NULL)
306 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000307#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000309 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000310 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000312 }
313#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000314 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000315}
316
317
Guido van Rossuma9672091994-09-14 13:31:22 +0000318/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
319 signal handlers or Mac I/O completion routines) can schedule calls
320 to a function to be called synchronously.
321 The synchronous function is called with one void* argument.
322 It should return 0 for success or -1 for failure -- failure should
323 be accompanied by an exception.
324
325 If registry succeeds, the registry function returns 0; if it fails
326 (e.g. due to too many pending calls) it returns -1 (without setting
327 an exception condition).
328
329 Note that because registry may occur from within signal handlers,
330 or other asynchronous events, calling malloc() is unsafe!
331
332#ifdef WITH_THREAD
333 Any thread can schedule pending calls, but only the main thread
334 will execute them.
335#endif
336
337 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
338 There are two possible race conditions:
339 (1) nested asynchronous registry calls;
340 (2) registry calls made while pending calls are being processed.
341 While (1) is very unlikely, (2) is a real possibility.
342 The current code is safe against (2), but not against (1).
343 The safety against (2) is derived from the fact that only one
344 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000345
Guido van Rossuma027efa1997-05-05 20:56:21 +0000346 XXX Darn! With the advent of thread state, we should have an array
347 of pending calls per thread in the thread state! Later...
348*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000349
Guido van Rossuma9672091994-09-14 13:31:22 +0000350#define NPENDINGCALLS 32
351static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000352 int (*func)(void *);
353 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000354} pendingcalls[NPENDINGCALLS];
355static volatile int pendingfirst = 0;
356static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000357static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000358
359int
Thomas Wouters334fb892000-07-25 12:56:38 +0000360Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000361{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000362 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000363 int i, j;
364 /* XXX Begin critical section */
365 /* XXX If you want this to be safe against nested
366 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000367 if (busy)
368 return -1;
369 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000370 i = pendinglast;
371 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000372 if (j == pendingfirst) {
373 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000374 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000375 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000376 pendingcalls[i].func = func;
377 pendingcalls[i].arg = arg;
378 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000379
380 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000381 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000382 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000383 /* XXX End critical section */
384 return 0;
385}
386
Guido van Rossum180d7b41994-09-29 09:45:57 +0000387int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000388Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000389{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000390 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000391#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000392 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000393 return 0;
394#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000395 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000396 return 0;
397 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000398 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000399 for (;;) {
400 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000401 int (*func)(void *);
402 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000403 i = pendingfirst;
404 if (i == pendinglast)
405 break; /* Queue empty */
406 func = pendingcalls[i].func;
407 arg = pendingcalls[i].arg;
408 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000409 if (func(arg) < 0) {
410 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000411 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000412 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000413 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000414 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000415 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000416 return 0;
417}
418
419
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000420/* The interpreter's recursion limit */
421
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000422#ifndef Py_DEFAULT_RECURSION_LIMIT
423#define Py_DEFAULT_RECURSION_LIMIT 1000
424#endif
425static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
426int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000427
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000428int
429Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000430{
431 return recursion_limit;
432}
433
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000434void
435Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000436{
437 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000438 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000439}
440
Armin Rigo2b3eb402003-10-28 12:05:48 +0000441/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
442 if the recursion_depth reaches _Py_CheckRecursionLimit.
443 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
444 to guarantee that _Py_CheckRecursiveCall() is regularly called.
445 Without USE_STACKCHECK, there is no need for this. */
446int
447_Py_CheckRecursiveCall(char *where)
448{
449 PyThreadState *tstate = PyThreadState_GET();
450
451#ifdef USE_STACKCHECK
452 if (PyOS_CheckStack()) {
453 --tstate->recursion_depth;
454 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
455 return -1;
456 }
457#endif
458 if (tstate->recursion_depth > recursion_limit) {
459 --tstate->recursion_depth;
460 PyErr_Format(PyExc_RuntimeError,
461 "maximum recursion depth exceeded%s",
462 where);
463 return -1;
464 }
465 _Py_CheckRecursionLimit = recursion_limit;
466 return 0;
467}
468
Guido van Rossum374a9221991-04-04 10:40:29 +0000469/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000470enum why_code {
471 WHY_NOT = 0x0001, /* No error */
472 WHY_EXCEPTION = 0x0002, /* Exception occurred */
473 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
474 WHY_RETURN = 0x0008, /* 'return' statement */
475 WHY_BREAK = 0x0010, /* 'break' statement */
476 WHY_CONTINUE = 0x0020, /* 'continue' statement */
477 WHY_YIELD = 0x0040 /* 'yield' operator */
478};
Guido van Rossum374a9221991-04-04 10:40:29 +0000479
Raymond Hettinger7c958652004-04-06 10:11:10 +0000480static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000481static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000482
Skip Montanarod581d772002-09-03 20:10:45 +0000483/* for manipulating the thread switch and periodic "stuff" - used to be
484 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000485int _Py_CheckInterval = 100;
486volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000487
Guido van Rossumb209a111997-04-29 18:18:01 +0000488PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000489PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000490{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000491 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000492 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000493 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000494 (PyObject **)NULL, 0,
495 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000496 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000497 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000498}
499
500
501/* Interpreter main loop */
502
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000503PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000504PyEval_EvalFrame(PyFrameObject *f) {
505 /* This is for backward compatibility with extension modules that
506 used this API; core interpreter code should call PyEval_EvalFrameEx() */
507 return PyEval_EvalFrameEx(f, 0);
508}
509
510PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000512{
Guido van Rossum950361c1997-01-24 13:49:28 +0000513#ifdef DXPAIRS
514 int lastopcode = 0;
515#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000516 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000517 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000518 register int opcode; /* Current opcode */
519 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000520 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000521 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000522 register PyObject *x; /* Result object -- NULL if error */
523 register PyObject *v; /* Temporary objects popped off stack */
524 register PyObject *w;
525 register PyObject *u;
526 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000527 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000528 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000529 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000530 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000531 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000532
Tim Peters8a5c3c72004-04-05 19:36:21 +0000533 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000534
535 not (instr_lb <= current_bytecode_offset < instr_ub)
536
Tim Peters8a5c3c72004-04-05 19:36:21 +0000537 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000538 initial values are such as to make this false the first
539 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000540 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000541
Guido van Rossumd076c731998-10-07 19:42:25 +0000542 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000543 PyObject *names;
544 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000545#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000546 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000547 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000548#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000549
Neal Norwitza81d2202002-07-14 00:27:26 +0000550/* Tuple access macros */
551
552#ifndef Py_DEBUG
553#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
554#else
555#define GETITEM(v, i) PyTuple_GetItem((v), (i))
556#endif
557
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000558#ifdef WITH_TSC
559/* Use Pentium timestamp counter to mark certain events:
560 inst0 -- beginning of switch statement for opcode dispatch
561 inst1 -- end of switch statement (may be skipped)
562 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000563 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000564 (may be skipped)
565 intr1 -- beginning of long interruption
566 intr2 -- end of long interruption
567
568 Many opcodes call out to helper C functions. In some cases, the
569 time in those functions should be counted towards the time for the
570 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
571 calls another Python function; there's no point in charge all the
572 bytecode executed by the called function to the caller.
573
574 It's hard to make a useful judgement statically. In the presence
575 of operator overloading, it's impossible to tell if a call will
576 execute new Python code or not.
577
578 It's a case-by-case judgement. I'll use intr1 for the following
579 cases:
580
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000581 IMPORT_STAR
582 IMPORT_FROM
583 CALL_FUNCTION (and friends)
584
585 */
586 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
587 int ticked = 0;
588
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000589 READ_TIMESTAMP(inst0);
590 READ_TIMESTAMP(inst1);
591 READ_TIMESTAMP(loop0);
592 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000593
594 /* shut up the compiler */
595 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000596#endif
597
Guido van Rossum374a9221991-04-04 10:40:29 +0000598/* Code access macros */
599
Martin v. Löwis18e16552006-02-15 17:27:45 +0000600#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000601#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000602#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000603#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000604#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000605#define JUMPBY(x) (next_instr += (x))
606
Raymond Hettingerf606f872003-03-16 03:11:04 +0000607/* OpCode prediction macros
608 Some opcodes tend to come in pairs thus making it possible to predict
609 the second code when the first is run. For example, COMPARE_OP is often
610 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
611 followed by a POP_TOP.
612
613 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000614 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000615 processor has a high likelihood of making its own successful branch
616 prediction which results in a nearly zero overhead transition to the
617 next opcode.
618
619 A successful prediction saves a trip through the eval-loop including
620 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000621
Tim Peters8a5c3c72004-04-05 19:36:21 +0000622 If collecting opcode statistics, turn off prediction so that
623 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000624 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000625*/
626
Raymond Hettingera7216982004-02-08 19:59:27 +0000627#ifdef DYNAMIC_EXECUTION_PROFILE
628#define PREDICT(op) if (0) goto PRED_##op
629#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000630#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000631#endif
632
Raymond Hettingerf606f872003-03-16 03:11:04 +0000633#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000634#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000635
Guido van Rossum374a9221991-04-04 10:40:29 +0000636/* Stack manipulation macros */
637
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638/* The stack can grow at most MAXINT deep, as co_nlocals and
639 co_stacksize are ints. */
640#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000641#define EMPTY() (STACK_LEVEL() == 0)
642#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000643#define SECOND() (stack_pointer[-2])
644#define THIRD() (stack_pointer[-3])
645#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000646#define SET_TOP(v) (stack_pointer[-1] = (v))
647#define SET_SECOND(v) (stack_pointer[-2] = (v))
648#define SET_THIRD(v) (stack_pointer[-3] = (v))
649#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000650#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000651#define BASIC_PUSH(v) (*stack_pointer++ = (v))
652#define BASIC_POP() (*--stack_pointer)
653
Guido van Rossum96a42c81992-01-12 02:29:51 +0000654#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000655#define PUSH(v) { (void)(BASIC_PUSH(v), \
656 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000657 assert(STACK_LEVEL() <= co->co_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000658#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000659#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
660 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000661 assert(STACK_LEVEL() <= co->co_stacksize); }
Guido van Rossumc2e20742006-02-27 22:32:47 +0000662#define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000663#else
664#define PUSH(v) BASIC_PUSH(v)
665#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000666#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000667#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000668#endif
669
Guido van Rossum681d79a1995-07-18 14:51:37 +0000670/* Local variable macros */
671
672#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000673
674/* The SETLOCAL() macro must not DECREF the local variable in-place and
675 then store the new value; it must copy the old value to a temporary
676 value, then store the new value, and then DECREF the temporary value.
677 This is because it is possible that during the DECREF the frame is
678 accessed by other code (e.g. a __del__ method or gc.collect()) and the
679 variable would be pointing to already-freed memory. */
680#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
681 GETLOCAL(i) = value; \
682 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000683
Guido van Rossuma027efa1997-05-05 20:56:21 +0000684/* Start of code */
685
Tim Peters5ca576e2001-06-18 22:08:13 +0000686 if (f == NULL)
687 return NULL;
688
Armin Rigo1d313ab2003-10-25 14:33:09 +0000689 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000690 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000691 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000692
Tim Peters5ca576e2001-06-18 22:08:13 +0000693 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000694
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000695 if (tstate->use_tracing) {
696 if (tstate->c_tracefunc != NULL) {
697 /* tstate->c_tracefunc, if defined, is a
698 function that will be called on *every* entry
699 to a code block. Its return value, if not
700 None, is a function that will be called at
701 the start of each executed line of code.
702 (Actually, the function must return itself
703 in order to continue tracing.) The trace
704 functions are called with three arguments:
705 a pointer to the current frame, a string
706 indicating why the function is called, and
707 an argument which depends on the situation.
708 The global trace function is also called
709 whenever an exception is detected. */
710 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
711 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000713 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000714 }
715 }
716 if (tstate->c_profilefunc != NULL) {
717 /* Similar for c_profilefunc, except it needn't
718 return itself and isn't called for "line" events */
719 if (call_trace(tstate->c_profilefunc,
720 tstate->c_profileobj,
721 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000722 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000723 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000724 }
725 }
726 }
727
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000728 co = f->f_code;
729 names = co->co_names;
730 consts = co->co_consts;
731 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000732 freevars = f->f_localsplus + co->co_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000733 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000734 /* An explanation is in order for the next line.
735
736 f->f_lasti now refers to the index of the last instruction
737 executed. You might think this was obvious from the name, but
738 this wasn't always true before 2.3! PyFrame_New now sets
739 f->f_lasti to -1 (i.e. the index *before* the first instruction)
740 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
741 does work. Promise. */
742 next_instr = first_instr + f->f_lasti + 1;
743 stack_pointer = f->f_stacktop;
744 assert(stack_pointer != NULL);
745 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
746
Tim Peters5ca576e2001-06-18 22:08:13 +0000747#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000749#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000750#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000751 filename = PyString_AsString(co->co_filename);
752#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000753
Guido van Rossum374a9221991-04-04 10:40:29 +0000754 why = WHY_NOT;
755 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000756 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000757 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000758
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000759 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000760 why = WHY_EXCEPTION;
761 goto on_error;
762 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000763
Guido van Rossum374a9221991-04-04 10:40:29 +0000764 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000765#ifdef WITH_TSC
766 if (inst1 == 0) {
767 /* Almost surely, the opcode executed a break
768 or a continue, preventing inst1 from being set
769 on the way out of the loop.
770 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000771 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000772 loop1 = inst1;
773 }
774 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
775 intr0, intr1);
776 ticked = 0;
777 inst1 = 0;
778 intr0 = 0;
779 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000780 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000781#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000782 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000783 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000784
Guido van Rossuma027efa1997-05-05 20:56:21 +0000785 /* Do periodic things. Doing this every time through
786 the loop would add too much overhead, so we do it
787 only every Nth instruction. We also do it if
788 ``things_to_do'' is set, i.e. when an asynchronous
789 event needs attention (e.g. a signal handler or
790 async I/O handler); see Py_AddPendingCall() and
791 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000792
Skip Montanarod581d772002-09-03 20:10:45 +0000793 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000794 if (*next_instr == SETUP_FINALLY) {
795 /* Make the last opcode before
796 a try: finally: block uninterruptable. */
797 goto fast_next_opcode;
798 }
Skip Montanarod581d772002-09-03 20:10:45 +0000799 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000800 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000801#ifdef WITH_TSC
802 ticked = 1;
803#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000804 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000805 if (Py_MakePendingCalls() < 0) {
806 why = WHY_EXCEPTION;
807 goto on_error;
808 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000809 if (things_to_do)
810 /* MakePendingCalls() didn't succeed.
811 Force early re-execution of this
812 "periodic" code, possibly after
813 a thread switch */
814 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000815 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000816#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000817 if (interpreter_lock) {
818 /* Give another thread a chance */
819
Guido van Rossum25ce5661997-08-02 03:10:38 +0000820 if (PyThreadState_Swap(NULL) != tstate)
821 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000822 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000823
824 /* Other threads may run now */
825
Guido van Rossum65d5b571998-12-21 19:32:43 +0000826 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000827 if (PyThreadState_Swap(tstate) != NULL)
828 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000829
830 /* Check for thread interrupts */
831
832 if (tstate->async_exc != NULL) {
833 x = tstate->async_exc;
834 tstate->async_exc = NULL;
835 PyErr_SetNone(x);
836 Py_DECREF(x);
837 why = WHY_EXCEPTION;
838 goto on_error;
839 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000840 }
841#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000842 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000843
Neil Schemenauer63543862002-02-17 19:10:14 +0000844 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000845 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000846
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000847 /* line-by-line tracing support */
848
849 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
850 /* see maybe_call_line_trace
851 for expository comments */
852 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000853
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000854 err = maybe_call_line_trace(tstate->c_tracefunc,
855 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000856 f, &instr_lb, &instr_ub,
857 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000858 /* Reload possibly changed frame fields */
859 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000860 if (f->f_stacktop != NULL) {
861 stack_pointer = f->f_stacktop;
862 f->f_stacktop = NULL;
863 }
864 if (err) {
865 /* trace function raised an exception */
866 goto on_error;
867 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000868 }
869
870 /* Extract opcode and argument */
871
Guido van Rossum374a9221991-04-04 10:40:29 +0000872 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000873 oparg = 0; /* allows oparg to be stored in a register because
874 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000875 if (HAS_ARG(opcode))
876 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000877 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000878#ifdef DYNAMIC_EXECUTION_PROFILE
879#ifdef DXPAIRS
880 dxpairs[lastopcode][opcode]++;
881 lastopcode = opcode;
882#endif
883 dxp[opcode]++;
884#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000885
Guido van Rossum96a42c81992-01-12 02:29:51 +0000886#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000887 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000888
Guido van Rossum96a42c81992-01-12 02:29:51 +0000889 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000890 if (HAS_ARG(opcode)) {
891 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000892 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000893 }
894 else {
895 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000896 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000897 }
898 }
899#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000900
Guido van Rossum374a9221991-04-04 10:40:29 +0000901 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000902 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000903
Guido van Rossum374a9221991-04-04 10:40:29 +0000904 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000905
Guido van Rossum374a9221991-04-04 10:40:29 +0000906 /* BEWARE!
907 It is essential that any operation that fails sets either
908 x to NULL, err to nonzero, or why to anything but WHY_NOT,
909 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000910
Guido van Rossum374a9221991-04-04 10:40:29 +0000911 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000912
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000913 case NOP:
914 goto fast_next_opcode;
915
Neil Schemenauer63543862002-02-17 19:10:14 +0000916 case LOAD_FAST:
917 x = GETLOCAL(oparg);
918 if (x != NULL) {
919 Py_INCREF(x);
920 PUSH(x);
921 goto fast_next_opcode;
922 }
923 format_exc_check_arg(PyExc_UnboundLocalError,
924 UNBOUNDLOCAL_ERROR_MSG,
925 PyTuple_GetItem(co->co_varnames, oparg));
926 break;
927
928 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000929 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000930 Py_INCREF(x);
931 PUSH(x);
932 goto fast_next_opcode;
933
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000934 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000935 case STORE_FAST:
936 v = POP();
937 SETLOCAL(oparg, v);
938 goto fast_next_opcode;
939
Raymond Hettingerf606f872003-03-16 03:11:04 +0000940 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000941 case POP_TOP:
942 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000943 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000944 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000945
Guido van Rossum374a9221991-04-04 10:40:29 +0000946 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000947 v = TOP();
948 w = SECOND();
949 SET_TOP(w);
950 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000951 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000952
Guido van Rossum374a9221991-04-04 10:40:29 +0000953 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000954 v = TOP();
955 w = SECOND();
956 x = THIRD();
957 SET_TOP(w);
958 SET_SECOND(x);
959 SET_THIRD(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 ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000963 u = TOP();
964 v = SECOND();
965 w = THIRD();
966 x = FOURTH();
967 SET_TOP(v);
968 SET_SECOND(w);
969 SET_THIRD(x);
970 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000971 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000972
Guido van Rossum374a9221991-04-04 10:40:29 +0000973 case DUP_TOP:
974 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000975 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000976 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000977 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000978
Thomas Wouters434d0822000-08-24 20:11:32 +0000979 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000980 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000981 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000982 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000983 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000984 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000985 STACKADJ(2);
986 SET_TOP(x);
987 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000988 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000989 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000990 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000991 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000993 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +0000995 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000996 STACKADJ(3);
997 SET_TOP(x);
998 SET_SECOND(w);
999 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001000 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001001 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001002 Py_FatalError("invalid argument to DUP_TOPX"
1003 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001004 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001005
Guido van Rossum374a9221991-04-04 10:40:29 +00001006 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001007 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001008 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001009 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001010 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001011 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001013
Guido van Rossum374a9221991-04-04 10:40:29 +00001014 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001016 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001017 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001018 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001019 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001021
Guido van Rossum374a9221991-04-04 10:40:29 +00001022 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001023 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001024 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001025 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001026 if (err == 0) {
1027 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001028 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001029 continue;
1030 }
1031 else if (err > 0) {
1032 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001033 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001034 err = 0;
1035 continue;
1036 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001037 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001038 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001039
Guido van Rossum7928cd71991-10-24 14:59:31 +00001040 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001041 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001042 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001043 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001044 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001045 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001046 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001047
Guido van Rossum50564e81996-01-12 01:13:16 +00001048 case BINARY_POWER:
1049 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001051 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001052 Py_DECREF(v);
1053 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001054 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001055 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001056 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Guido van Rossum374a9221991-04-04 10:40:29 +00001058 case BINARY_MULTIPLY:
1059 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001061 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001062 Py_DECREF(v);
1063 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001064 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001065 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001066 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001067
Tim Peters3caca232001-12-06 06:23:26 +00001068 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001069 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001070 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001071 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001072 Py_DECREF(v);
1073 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001075 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001076 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001077
Guido van Rossum4668b002001-08-08 05:00:18 +00001078 case BINARY_FLOOR_DIVIDE:
1079 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001080 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001081 x = PyNumber_FloorDivide(v, w);
1082 Py_DECREF(v);
1083 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001084 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001085 if (x != NULL) continue;
1086 break;
1087
Guido van Rossum374a9221991-04-04 10:40:29 +00001088 case BINARY_MODULO:
1089 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001090 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001091 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001092 Py_DECREF(v);
1093 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001094 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001095 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001096 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001097
Guido van Rossum374a9221991-04-04 10:40:29 +00001098 case BINARY_ADD:
1099 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001100 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001101 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001102 /* INLINE: int + int */
1103 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001104 a = PyInt_AS_LONG(v);
1105 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001106 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001107 if ((i^a) < 0 && (i^b) < 0)
1108 goto slow_add;
1109 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001110 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001111 else if (PyString_CheckExact(v) &&
1112 PyString_CheckExact(w)) {
1113 x = string_concatenate(v, w, f, next_instr);
1114 /* string_concatenate consumed the ref to v */
1115 goto skip_decref_vx;
1116 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001117 else {
1118 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001119 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001120 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001121 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001122 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001123 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001124 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001125 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001126 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001127
Guido van Rossum374a9221991-04-04 10:40:29 +00001128 case BINARY_SUBTRACT:
1129 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001130 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001131 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001132 /* INLINE: int - int */
1133 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001134 a = PyInt_AS_LONG(v);
1135 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001136 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001137 if ((i^a) < 0 && (i^~b) < 0)
1138 goto slow_sub;
1139 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001140 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001141 else {
1142 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001143 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001144 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001145 Py_DECREF(v);
1146 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001147 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001148 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001150
Guido van Rossum374a9221991-04-04 10:40:29 +00001151 case BINARY_SUBSCR:
1152 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001153 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001154 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001155 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001156 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001157 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001158 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001159 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001160 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001161 Py_INCREF(x);
1162 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001163 else
1164 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001165 }
1166 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001167 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001168 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001169 Py_DECREF(v);
1170 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001171 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001172 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001173 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001174
Guido van Rossum7928cd71991-10-24 14:59:31 +00001175 case BINARY_LSHIFT:
1176 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001177 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001178 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001179 Py_DECREF(v);
1180 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001181 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001182 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001183 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001184
Guido van Rossum7928cd71991-10-24 14:59:31 +00001185 case BINARY_RSHIFT:
1186 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001187 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001188 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001189 Py_DECREF(v);
1190 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001191 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001192 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001193 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Guido van Rossum7928cd71991-10-24 14:59:31 +00001195 case BINARY_AND:
1196 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001197 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001198 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001199 Py_DECREF(v);
1200 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001201 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001202 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001203 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001204
Guido van Rossum7928cd71991-10-24 14:59:31 +00001205 case BINARY_XOR:
1206 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001207 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001208 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001209 Py_DECREF(v);
1210 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001211 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001212 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001213 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001214
Guido van Rossum7928cd71991-10-24 14:59:31 +00001215 case BINARY_OR:
1216 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001217 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001218 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001219 Py_DECREF(v);
1220 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001221 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001222 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001223 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001224
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001225 case LIST_APPEND:
1226 w = POP();
1227 v = POP();
1228 err = PyList_Append(v, w);
1229 Py_DECREF(v);
1230 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001231 if (err == 0) {
1232 PREDICT(JUMP_ABSOLUTE);
1233 continue;
1234 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001235 break;
1236
Thomas Wouters434d0822000-08-24 20:11:32 +00001237 case INPLACE_POWER:
1238 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001239 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001240 x = PyNumber_InPlacePower(v, w, Py_None);
1241 Py_DECREF(v);
1242 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001243 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 if (x != NULL) continue;
1245 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001246
Thomas Wouters434d0822000-08-24 20:11:32 +00001247 case INPLACE_MULTIPLY:
1248 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001249 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001250 x = PyNumber_InPlaceMultiply(v, w);
1251 Py_DECREF(v);
1252 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001254 if (x != NULL) continue;
1255 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001256
Tim Peters54b11912001-12-25 18:49:11 +00001257 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001259 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001260 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001261 Py_DECREF(v);
1262 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001263 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001264 if (x != NULL) continue;
1265 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001266
Guido van Rossum4668b002001-08-08 05:00:18 +00001267 case INPLACE_FLOOR_DIVIDE:
1268 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001270 x = PyNumber_InPlaceFloorDivide(v, w);
1271 Py_DECREF(v);
1272 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001273 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001274 if (x != NULL) continue;
1275 break;
1276
Thomas Wouters434d0822000-08-24 20:11:32 +00001277 case INPLACE_MODULO:
1278 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001280 x = PyNumber_InPlaceRemainder(v, w);
1281 Py_DECREF(v);
1282 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001283 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001284 if (x != NULL) continue;
1285 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001286
Thomas Wouters434d0822000-08-24 20:11:32 +00001287 case INPLACE_ADD:
1288 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001289 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001290 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001291 /* INLINE: int + int */
1292 register long a, b, i;
1293 a = PyInt_AS_LONG(v);
1294 b = PyInt_AS_LONG(w);
1295 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001296 if ((i^a) < 0 && (i^b) < 0)
1297 goto slow_iadd;
1298 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001300 else if (PyString_CheckExact(v) &&
1301 PyString_CheckExact(w)) {
1302 x = string_concatenate(v, w, f, next_instr);
1303 /* string_concatenate consumed the ref to v */
1304 goto skip_decref_v;
1305 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001306 else {
1307 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001308 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001309 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001310 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001311 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001313 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001314 if (x != NULL) continue;
1315 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001316
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 case INPLACE_SUBTRACT:
1318 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001319 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001320 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001321 /* INLINE: int - int */
1322 register long a, b, i;
1323 a = PyInt_AS_LONG(v);
1324 b = PyInt_AS_LONG(w);
1325 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001326 if ((i^a) < 0 && (i^~b) < 0)
1327 goto slow_isub;
1328 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001330 else {
1331 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001333 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001334 Py_DECREF(v);
1335 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001336 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 if (x != NULL) continue;
1338 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 case INPLACE_LSHIFT:
1341 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001342 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001343 x = PyNumber_InPlaceLshift(v, w);
1344 Py_DECREF(v);
1345 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001346 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001347 if (x != NULL) continue;
1348 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 case INPLACE_RSHIFT:
1351 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001352 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001353 x = PyNumber_InPlaceRshift(v, w);
1354 Py_DECREF(v);
1355 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001357 if (x != NULL) continue;
1358 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001359
Thomas Wouters434d0822000-08-24 20:11:32 +00001360 case INPLACE_AND:
1361 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001362 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001363 x = PyNumber_InPlaceAnd(v, w);
1364 Py_DECREF(v);
1365 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001366 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001367 if (x != NULL) continue;
1368 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001369
Thomas Wouters434d0822000-08-24 20:11:32 +00001370 case INPLACE_XOR:
1371 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001372 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001373 x = PyNumber_InPlaceXor(v, w);
1374 Py_DECREF(v);
1375 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001377 if (x != NULL) continue;
1378 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Thomas Wouters434d0822000-08-24 20:11:32 +00001380 case INPLACE_OR:
1381 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001382 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001383 x = PyNumber_InPlaceOr(v, w);
1384 Py_DECREF(v);
1385 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001386 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001387 if (x != NULL) continue;
1388 break;
1389
Guido van Rossum374a9221991-04-04 10:40:29 +00001390 case SLICE+0:
1391 case SLICE+1:
1392 case SLICE+2:
1393 case SLICE+3:
1394 if ((opcode-SLICE) & 2)
1395 w = POP();
1396 else
1397 w = NULL;
1398 if ((opcode-SLICE) & 1)
1399 v = POP();
1400 else
1401 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001402 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001403 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001404 Py_DECREF(u);
1405 Py_XDECREF(v);
1406 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001407 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001408 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001409 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 case STORE_SLICE+0:
1412 case STORE_SLICE+1:
1413 case STORE_SLICE+2:
1414 case STORE_SLICE+3:
1415 if ((opcode-STORE_SLICE) & 2)
1416 w = POP();
1417 else
1418 w = NULL;
1419 if ((opcode-STORE_SLICE) & 1)
1420 v = POP();
1421 else
1422 v = NULL;
1423 u = POP();
1424 t = POP();
1425 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001426 Py_DECREF(t);
1427 Py_DECREF(u);
1428 Py_XDECREF(v);
1429 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001430 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001431 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Guido van Rossum374a9221991-04-04 10:40:29 +00001433 case DELETE_SLICE+0:
1434 case DELETE_SLICE+1:
1435 case DELETE_SLICE+2:
1436 case DELETE_SLICE+3:
1437 if ((opcode-DELETE_SLICE) & 2)
1438 w = POP();
1439 else
1440 w = NULL;
1441 if ((opcode-DELETE_SLICE) & 1)
1442 v = POP();
1443 else
1444 v = NULL;
1445 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001446 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001448 Py_DECREF(u);
1449 Py_XDECREF(v);
1450 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001451 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Guido van Rossum374a9221991-04-04 10:40:29 +00001454 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001455 w = TOP();
1456 v = SECOND();
1457 u = THIRD();
1458 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001459 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001460 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001461 Py_DECREF(u);
1462 Py_DECREF(v);
1463 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001464 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001466
Guido van Rossum374a9221991-04-04 10:40:29 +00001467 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001468 w = TOP();
1469 v = SECOND();
1470 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001472 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001473 Py_DECREF(v);
1474 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001475 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001476 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001477
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 case PRINT_EXPR:
1479 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001480 w = PySys_GetObject("displayhook");
1481 if (w == NULL) {
1482 PyErr_SetString(PyExc_RuntimeError,
1483 "lost sys.displayhook");
1484 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001485 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001486 }
1487 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001488 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001489 if (x == NULL)
1490 err = -1;
1491 }
1492 if (err == 0) {
1493 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001494 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001495 if (w == NULL)
1496 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001498 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001499 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001500 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001501
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001502 case PRINT_ITEM_TO:
1503 w = stream = POP();
1504 /* fall through to PRINT_ITEM */
1505
Guido van Rossum374a9221991-04-04 10:40:29 +00001506 case PRINT_ITEM:
1507 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001508 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001509 w = PySys_GetObject("stdout");
1510 if (w == NULL) {
1511 PyErr_SetString(PyExc_RuntimeError,
1512 "lost sys.stdout");
1513 err = -1;
1514 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001515 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001516 /* PyFile_SoftSpace() can exececute arbitrary code
1517 if sys.stdout is an instance with a __getattr__.
1518 If __getattr__ raises an exception, w will
1519 be freed, so we need to prevent that temporarily. */
1520 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001521 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001522 err = PyFile_WriteString(" ", w);
1523 if (err == 0)
1524 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001525 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001526 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001527 if (PyString_Check(v)) {
1528 char *s = PyString_AS_STRING(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001529 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001530 if (len == 0 ||
1531 !isspace(Py_CHARMASK(s[len-1])) ||
1532 s[len-1] == ' ')
1533 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001534 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001535#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001536 else if (PyUnicode_Check(v)) {
1537 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001538 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001539 if (len == 0 ||
1540 !Py_UNICODE_ISSPACE(s[len-1]) ||
1541 s[len-1] == ' ')
1542 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001543 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001544#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001545 else
1546 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001547 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001548 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001549 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001550 Py_XDECREF(stream);
1551 stream = NULL;
1552 if (err == 0)
1553 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001554 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001556 case PRINT_NEWLINE_TO:
1557 w = stream = POP();
1558 /* fall through to PRINT_NEWLINE */
1559
Guido van Rossum374a9221991-04-04 10:40:29 +00001560 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001561 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001562 w = PySys_GetObject("stdout");
1563 if (w == NULL)
1564 PyErr_SetString(PyExc_RuntimeError,
1565 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001566 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001567 if (w != NULL) {
1568 err = PyFile_WriteString("\n", w);
1569 if (err == 0)
1570 PyFile_SoftSpace(w, 0);
1571 }
1572 Py_XDECREF(stream);
1573 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001574 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Thomas Wouters434d0822000-08-24 20:11:32 +00001576
1577#ifdef CASE_TOO_BIG
1578 default: switch (opcode) {
1579#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001580 case RAISE_VARARGS:
1581 u = v = w = NULL;
1582 switch (oparg) {
1583 case 3:
1584 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001585 /* Fallthrough */
1586 case 2:
1587 v = POP(); /* value */
1588 /* Fallthrough */
1589 case 1:
1590 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001591 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001592 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001593 break;
1594 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001595 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001596 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001597 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001598 break;
1599 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001600 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001601
Guido van Rossum374a9221991-04-04 10:40:29 +00001602 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001603 if ((x = f->f_locals) != NULL) {
1604 Py_INCREF(x);
1605 PUSH(x);
1606 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001607 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001608 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001609 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001610
Guido van Rossum374a9221991-04-04 10:40:29 +00001611 case RETURN_VALUE:
1612 retval = POP();
1613 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001614 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001615
Tim Peters5ca576e2001-06-18 22:08:13 +00001616 case YIELD_VALUE:
1617 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001618 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001619 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001620 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001621
Guido van Rossum374a9221991-04-04 10:40:29 +00001622 case POP_BLOCK:
1623 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001624 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001625 while (STACK_LEVEL() > b->b_level) {
1626 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001627 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001628 }
1629 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001630 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001631
Guido van Rossum374a9221991-04-04 10:40:29 +00001632 case END_FINALLY:
1633 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001634 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001635 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001636 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001637 if (why == WHY_RETURN ||
1638 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001639 retval = POP();
1640 }
Brett Cannonbf364092006-03-01 04:25:17 +00001641 else if (PyExceptionClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001643 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001644 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001645 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001646 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001647 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001648 else if (v != Py_None) {
1649 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001650 "'finally' pops bad exception");
1651 why = WHY_EXCEPTION;
1652 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001653 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001657 u = TOP();
1658 v = SECOND();
1659 w = THIRD();
1660 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001661 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001662 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001663 Py_DECREF(u);
1664 Py_DECREF(v);
1665 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001667
Guido van Rossum374a9221991-04-04 10:40:29 +00001668 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001669 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001670 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001671 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001672 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001673 err = PyDict_SetItem(x, w, v);
1674 else
1675 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001676 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001677 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001678 break;
1679 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001680 PyErr_Format(PyExc_SystemError,
1681 "no locals found when storing %s",
1682 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Guido van Rossum374a9221991-04-04 10:40:29 +00001685 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001686 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001687 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001688 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001689 format_exc_check_arg(PyExc_NameError,
1690 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001691 break;
1692 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001693 PyErr_Format(PyExc_SystemError,
1694 "no locals when deleting %s",
1695 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001696 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001697
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001698 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001699 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001701 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1702 PyObject **items = ((PyTupleObject *)v)->ob_item;
1703 while (oparg--) {
1704 w = items[oparg];
1705 Py_INCREF(w);
1706 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001707 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001708 Py_DECREF(v);
1709 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001710 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1711 PyObject **items = ((PyListObject *)v)->ob_item;
1712 while (oparg--) {
1713 w = items[oparg];
1714 Py_INCREF(w);
1715 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001716 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001717 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001718 stack_pointer + oparg))
1719 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001720 else {
1721 if (PyErr_ExceptionMatches(PyExc_TypeError))
1722 PyErr_SetString(PyExc_TypeError,
1723 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001724 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001725 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001726 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001727 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001728
Guido van Rossum374a9221991-04-04 10:40:29 +00001729 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001730 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001731 v = TOP();
1732 u = SECOND();
1733 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001734 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1735 Py_DECREF(v);
1736 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001737 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001738 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Guido van Rossum374a9221991-04-04 10:40:29 +00001740 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001741 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001743 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1744 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001745 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001746 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001747
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001748 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001749 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001750 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001751 err = PyDict_SetItem(f->f_globals, w, v);
1752 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001753 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001754 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001755
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001756 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001757 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001758 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001759 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001760 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001761 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001764 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001765 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001766 PyErr_Format(PyExc_SystemError,
1767 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001768 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001769 break;
1770 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001771 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001772 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001773 Py_XINCREF(x);
1774 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001775 else {
1776 x = PyObject_GetItem(v, w);
1777 if (x == NULL && PyErr_Occurred()) {
1778 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1779 break;
1780 PyErr_Clear();
1781 }
1782 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001783 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001784 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001786 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001788 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001789 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001790 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001791 break;
1792 }
1793 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001794 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001795 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001796 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001797 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001798
Guido van Rossum374a9221991-04-04 10:40:29 +00001799 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001800 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001801 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001802 /* Inline the PyDict_GetItem() calls.
1803 WARNING: this is an extreme speed hack.
1804 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001805 long hash = ((PyStringObject *)w)->ob_shash;
1806 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001807 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001808 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001809 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001810 e = d->ma_lookup(d, w, hash);
1811 if (e == NULL) {
1812 x = NULL;
1813 break;
1814 }
1815 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001816 if (x != NULL) {
1817 Py_INCREF(x);
1818 PUSH(x);
1819 continue;
1820 }
1821 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001822 e = d->ma_lookup(d, w, hash);
1823 if (e == NULL) {
1824 x = NULL;
1825 break;
1826 }
1827 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001828 if (x != NULL) {
1829 Py_INCREF(x);
1830 PUSH(x);
1831 continue;
1832 }
1833 goto load_global_error;
1834 }
1835 }
1836 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001837 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001838 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001839 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001841 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001842 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001843 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001844 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001845 break;
1846 }
1847 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001848 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001849 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001850 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001851
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001852 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001853 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001854 if (x != NULL) {
1855 SETLOCAL(oparg, NULL);
1856 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001857 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001858 format_exc_check_arg(
1859 PyExc_UnboundLocalError,
1860 UNBOUNDLOCAL_ERROR_MSG,
1861 PyTuple_GetItem(co->co_varnames, oparg)
1862 );
1863 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001864
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001865 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001866 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001867 Py_INCREF(x);
1868 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001869 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001870 break;
1871
1872 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001873 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001874 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001875 if (w != NULL) {
1876 PUSH(w);
1877 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001878 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001879 err = -1;
1880 /* Don't stomp existing exception */
1881 if (PyErr_Occurred())
1882 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001883 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1884 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001885 oparg);
1886 format_exc_check_arg(
1887 PyExc_UnboundLocalError,
1888 UNBOUNDLOCAL_ERROR_MSG,
1889 v);
1890 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001891 v = PyTuple_GET_ITEM(
Raymond Hettinger467a6982004-04-07 11:39:21 +00001892 co->co_freevars,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001893 oparg - PyTuple_GET_SIZE(co->co_cellvars));
Raymond Hettinger467a6982004-04-07 11:39:21 +00001894 format_exc_check_arg(
1895 PyExc_NameError,
1896 UNBOUNDFREE_ERROR_MSG,
1897 v);
1898 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001899 break;
1900
1901 case STORE_DEREF:
1902 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001903 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001904 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001905 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001906 continue;
1907
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001909 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001910 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001911 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001912 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001913 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001914 }
1915 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001916 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 }
1918 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001919
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001921 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001923 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001925 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001926 }
1927 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001928 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 }
1930 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001931
Guido van Rossum86e58e22006-08-28 15:27:34 +00001932 case BUILD_SET:
1933 x = PySet_New(NULL);
1934 if (x != NULL) {
1935 for (; --oparg >= 0;) {
1936 w = POP();
1937 if (err == 0)
1938 err = PySet_Add(x, w);
1939 Py_DECREF(w);
1940 }
1941 if (err != 0) {
1942 Py_DECREF(x);
1943 break;
1944 }
1945 PUSH(x);
1946 continue;
1947 }
1948 break;
1949
Guido van Rossum374a9221991-04-04 10:40:29 +00001950 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001951 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001952 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001953 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001954 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001955
Guido van Rossum374a9221991-04-04 10:40:29 +00001956 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001957 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001958 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 x = PyObject_GetAttr(v, w);
1960 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001961 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001962 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001964
Guido van Rossum374a9221991-04-04 10:40:29 +00001965 case COMPARE_OP:
1966 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001967 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001968 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001969 /* INLINE: cmp(int, int) */
1970 register long a, b;
1971 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001972 a = PyInt_AS_LONG(v);
1973 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001974 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001975 case PyCmp_LT: res = a < b; break;
1976 case PyCmp_LE: res = a <= b; break;
1977 case PyCmp_EQ: res = a == b; break;
1978 case PyCmp_NE: res = a != b; break;
1979 case PyCmp_GT: res = a > b; break;
1980 case PyCmp_GE: res = a >= b; break;
1981 case PyCmp_IS: res = v == w; break;
1982 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001983 default: goto slow_compare;
1984 }
1985 x = res ? Py_True : Py_False;
1986 Py_INCREF(x);
1987 }
1988 else {
1989 slow_compare:
1990 x = cmp_outcome(oparg, v, w);
1991 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001992 Py_DECREF(v);
1993 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001994 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001995 if (x == NULL) break;
1996 PREDICT(JUMP_IF_FALSE);
1997 PREDICT(JUMP_IF_TRUE);
1998 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001999
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002001 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002002 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002003 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002004 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002005 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002006 break;
2007 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002008 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002009 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002010 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2011 w = PyTuple_Pack(5,
2012 w,
2013 f->f_globals,
2014 f->f_locals == NULL ?
2015 Py_None : f->f_locals,
2016 v,
2017 u);
2018 else
2019 w = PyTuple_Pack(4,
2020 w,
2021 f->f_globals,
2022 f->f_locals == NULL ?
2023 Py_None : f->f_locals,
2024 v);
2025 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002026 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002027 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002028 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002029 x = NULL;
2030 break;
2031 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002032 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002033 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002034 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002035 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002036 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002037 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002038 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002039
Thomas Wouters52152252000-08-17 22:55:00 +00002040 case IMPORT_STAR:
2041 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002042 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002043 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002044 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002045 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002046 break;
2047 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002048 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002049 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002050 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002051 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002052 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002053 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002054 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002055
Thomas Wouters52152252000-08-17 22:55:00 +00002056 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002057 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002058 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002059 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002060 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002061 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002062 PUSH(x);
2063 if (x != NULL) continue;
2064 break;
2065
Guido van Rossum374a9221991-04-04 10:40:29 +00002066 case JUMP_FORWARD:
2067 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002068 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002069
Raymond Hettingerf606f872003-03-16 03:11:04 +00002070 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002071 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002072 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002073 if (w == Py_True) {
2074 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002075 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002076 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002077 if (w == Py_False) {
2078 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002079 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002080 }
2081 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002082 if (err > 0)
2083 err = 0;
2084 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002085 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002086 else
2087 break;
2088 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002089
Raymond Hettingerf606f872003-03-16 03:11:04 +00002090 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002091 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002092 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002093 if (w == Py_False) {
2094 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002095 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002096 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002097 if (w == Py_True) {
2098 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002099 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002100 }
2101 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002102 if (err > 0) {
2103 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002104 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002105 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002106 else if (err == 0)
2107 ;
2108 else
2109 break;
2110 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002111
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002112 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002113 case JUMP_ABSOLUTE:
2114 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002115 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002116
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002117 case GET_ITER:
2118 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002119 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002120 x = PyObject_GetIter(v);
2121 Py_DECREF(v);
2122 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002123 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002124 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002125 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002126 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002127 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002128 break;
2129
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002130 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002131 case FOR_ITER:
2132 /* before: [iter]; after: [iter, iter()] *or* [] */
2133 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002134 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002135 if (x != NULL) {
2136 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002137 PREDICT(STORE_FAST);
2138 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002139 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002140 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002141 if (PyErr_Occurred()) {
2142 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2143 break;
2144 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002145 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002146 /* iterator ended normally */
2147 x = v = POP();
2148 Py_DECREF(v);
2149 JUMPBY(oparg);
2150 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002151
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002152 case BREAK_LOOP:
2153 why = WHY_BREAK;
2154 goto fast_block_end;
2155
2156 case CONTINUE_LOOP:
2157 retval = PyInt_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002158 if (!retval) {
2159 x = NULL;
2160 break;
2161 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002162 why = WHY_CONTINUE;
2163 goto fast_block_end;
2164
Guido van Rossum374a9221991-04-04 10:40:29 +00002165 case SETUP_LOOP:
2166 case SETUP_EXCEPT:
2167 case SETUP_FINALLY:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002168 /* NOTE: If you add any new block-setup opcodes that are not try/except/finally
2169 handlers, you may need to update the PyGen_NeedsFinalizing() function. */
2170
Guido van Rossumb209a111997-04-29 18:18:01 +00002171 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002172 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002173 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002174
Guido van Rossumc2e20742006-02-27 22:32:47 +00002175 case WITH_CLEANUP:
2176 {
2177 /* TOP is the context.__exit__ bound method.
2178 Below that are 1-3 values indicating how/why
2179 we entered the finally clause:
2180 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002181 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002182 - SECOND = WHY_*; no retval below it
2183 - (SECOND, THIRD, FOURTH) = exc_info()
2184 In the last case, we must call
2185 TOP(SECOND, THIRD, FOURTH)
2186 otherwise we must call
2187 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002188
2189 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002190 *and* the function call returns a 'true' value, we
2191 "zap" this information, to prevent END_FINALLY from
2192 re-raising the exception. (But non-local gotos
2193 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002194 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002195
Guido van Rossumc2e20742006-02-27 22:32:47 +00002196 x = TOP();
2197 u = SECOND();
2198 if (PyInt_Check(u) || u == Py_None) {
2199 u = v = w = Py_None;
2200 }
2201 else {
2202 v = THIRD();
2203 w = FOURTH();
2204 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002205 /* XXX Not the fastest way to call it... */
2206 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2207 if (x == NULL)
2208 break; /* Go to error exit */
2209 if (u != Py_None && PyObject_IsTrue(x)) {
2210 /* There was an exception and a true return */
2211 Py_DECREF(x);
2212 x = TOP(); /* Again */
2213 STACKADJ(-3);
2214 Py_INCREF(Py_None);
2215 SET_TOP(Py_None);
2216 Py_DECREF(x);
2217 Py_DECREF(u);
2218 Py_DECREF(v);
2219 Py_DECREF(w);
2220 } else {
2221 /* Let END_FINALLY do its thing */
2222 Py_DECREF(x);
2223 x = POP();
2224 Py_DECREF(x);
2225 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002226 break;
2227 }
2228
Guido van Rossumf10570b1995-07-07 22:53:21 +00002229 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002230 {
2231 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002232 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002233 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002234#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002235 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002236#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002237 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002238#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002239 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002240 PUSH(x);
2241 if (x != NULL)
2242 continue;
2243 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002244 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002245
Jeremy Hylton76901512000-03-28 23:49:17 +00002246 case CALL_FUNCTION_VAR:
2247 case CALL_FUNCTION_KW:
2248 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002249 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002250 int na = oparg & 0xff;
2251 int nk = (oparg>>8) & 0xff;
2252 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002253 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002254 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002255 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002256 if (flags & CALL_FLAG_VAR)
2257 n++;
2258 if (flags & CALL_FLAG_KW)
2259 n++;
2260 pfunc = stack_pointer - n - 1;
2261 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002262
Guido van Rossumac7be682001-01-17 15:42:30 +00002263 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002264 && PyMethod_GET_SELF(func) != NULL) {
2265 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002266 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002267 func = PyMethod_GET_FUNCTION(func);
2268 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002269 Py_DECREF(*pfunc);
2270 *pfunc = self;
2271 na++;
2272 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002273 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002274 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002275 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002276 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002277 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002278 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002279 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002280 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002281
Jeremy Hylton76901512000-03-28 23:49:17 +00002282 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002283 w = POP();
2284 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002285 }
2286 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002287 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002288 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002289 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002290 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002291
Guido van Rossum681d79a1995-07-18 14:51:37 +00002292 case MAKE_FUNCTION:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002293 {
2294 int posdefaults = oparg & 0xff;
2295 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002296 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002297
Guido van Rossum681d79a1995-07-18 14:51:37 +00002298 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002299 x = PyFunction_New(v, f->f_globals);
2300 Py_DECREF(v);
Neal Norwitzc1505362006-12-28 06:47:50 +00002301
2302 if (x != NULL && num_annotations > 0) {
2303 Py_ssize_t name_ix;
2304 u = POP(); /* names of args with annotations */
2305 v = PyDict_New();
2306 if (v == NULL) {
2307 Py_DECREF(x);
2308 x = NULL;
2309 break;
2310 }
2311 name_ix = PyTuple_Size(u);
2312 assert(num_annotations == name_ix+1);
2313 while (name_ix > 0) {
2314 --name_ix;
2315 t = PyTuple_GET_ITEM(u, name_ix);
2316 w = POP();
2317 /* XXX(nnorwitz): check for errors */
2318 PyDict_SetItem(v, t, w);
2319 Py_DECREF(w);
2320 }
2321
2322 err = PyFunction_SetAnnotations(x, v);
2323 Py_DECREF(v);
2324 Py_DECREF(u);
2325 }
2326
Guido van Rossum681d79a1995-07-18 14:51:37 +00002327 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002328 if (x != NULL && posdefaults > 0) {
2329 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002330 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002331 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002332 x = NULL;
2333 break;
2334 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002335 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002336 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002337 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002338 }
2339 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002340 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002341 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002342 if (x != NULL && kwdefaults > 0) {
2343 v = PyDict_New();
2344 if (v == NULL) {
2345 Py_DECREF(x);
2346 x = NULL;
2347 break;
2348 }
2349 while (--kwdefaults >= 0) {
2350 w = POP(); /* default value */
2351 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002352 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002353 PyDict_SetItem(v, u, w);
2354 }
2355 err = PyFunction_SetKwDefaults(x, v);
2356 Py_DECREF(v);
2357 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002358 PUSH(x);
2359 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002360 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002361
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002362 case MAKE_CLOSURE:
2363 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002364 v = POP(); /* code object */
2365 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002366 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002367 if (x != NULL) {
2368 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002369 err = PyFunction_SetClosure(x, v);
2370 Py_DECREF(v);
2371 }
2372 if (x != NULL && oparg > 0) {
2373 v = PyTuple_New(oparg);
2374 if (v == NULL) {
2375 Py_DECREF(x);
2376 x = NULL;
2377 break;
2378 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002379 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002380 w = POP();
2381 PyTuple_SET_ITEM(v, oparg, w);
2382 }
2383 err = PyFunction_SetDefaults(x, v);
2384 Py_DECREF(v);
2385 }
2386 PUSH(x);
2387 break;
2388 }
2389
Guido van Rossum8861b741996-07-30 16:49:37 +00002390 case BUILD_SLICE:
2391 if (oparg == 3)
2392 w = POP();
2393 else
2394 w = NULL;
2395 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002396 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002397 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002398 Py_DECREF(u);
2399 Py_DECREF(v);
2400 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002401 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002402 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002403 break;
2404
Fred Drakeef8ace32000-08-24 00:32:09 +00002405 case EXTENDED_ARG:
2406 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002407 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002408 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002409
Guido van Rossum374a9221991-04-04 10:40:29 +00002410 default:
2411 fprintf(stderr,
2412 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002413 PyCode_Addr2Line(f->f_code, f->f_lasti),
2414 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002415 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002416 why = WHY_EXCEPTION;
2417 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002418
2419#ifdef CASE_TOO_BIG
2420 }
2421#endif
2422
Guido van Rossum374a9221991-04-04 10:40:29 +00002423 } /* switch */
2424
2425 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002426
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002427 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002428
Guido van Rossum374a9221991-04-04 10:40:29 +00002429 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002430
Guido van Rossum374a9221991-04-04 10:40:29 +00002431 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002432 if (err == 0 && x != NULL) {
2433#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002434 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002435 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002436 fprintf(stderr,
2437 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002438 else {
2439#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002440 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002441 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002442#ifdef CHECKEXC
2443 }
2444#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002445 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002446 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002447 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002448 err = 0;
2449 }
2450
Guido van Rossum374a9221991-04-04 10:40:29 +00002451 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002453 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002454 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002455 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002456 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002457 why = WHY_EXCEPTION;
2458 }
2459 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002460#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002461 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002462 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002463 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002464 char buf[1024];
2465 sprintf(buf, "Stack unwind with exception "
2466 "set and why=%d", why);
2467 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002468 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002469 }
2470#endif
2471
2472 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002473
Guido van Rossum374a9221991-04-04 10:40:29 +00002474 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002475 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002476
Fred Drake8f51f542001-10-04 14:48:42 +00002477 if (tstate->c_tracefunc != NULL)
2478 call_exc_trace(tstate->c_tracefunc,
2479 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002480 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002481
Guido van Rossum374a9221991-04-04 10:40:29 +00002482 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002483
Guido van Rossum374a9221991-04-04 10:40:29 +00002484 if (why == WHY_RERAISE)
2485 why = WHY_EXCEPTION;
2486
2487 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002488
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002489fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002490 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002491 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002492
Tim Peters8a5c3c72004-04-05 19:36:21 +00002493 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002494 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2495 /* For a continue inside a try block,
2496 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002497 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2498 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002499 why = WHY_NOT;
2500 JUMPTO(PyInt_AS_LONG(retval));
2501 Py_DECREF(retval);
2502 break;
2503 }
2504
Guido van Rossum374a9221991-04-04 10:40:29 +00002505 while (STACK_LEVEL() > b->b_level) {
2506 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002507 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002508 }
2509 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2510 why = WHY_NOT;
2511 JUMPTO(b->b_handler);
2512 break;
2513 }
2514 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002515 (b->b_type == SETUP_EXCEPT &&
2516 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002517 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002518 PyObject *exc, *val, *tb;
2519 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002520 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002521 val = Py_None;
2522 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002523 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002524 /* Make the raw exception data
2525 available to the handler,
2526 so a program can emulate the
2527 Python main loop. Don't do
2528 this for 'finally'. */
2529 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002530 PyErr_NormalizeException(
2531 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002532 set_exc_info(tstate,
2533 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002534 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002535 if (tb == NULL) {
2536 Py_INCREF(Py_None);
2537 PUSH(Py_None);
2538 } else
2539 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002540 PUSH(val);
2541 PUSH(exc);
2542 }
2543 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002544 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002545 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002546 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002547 PUSH(v);
2548 }
2549 why = WHY_NOT;
2550 JUMPTO(b->b_handler);
2551 break;
2552 }
2553 } /* unwind stack */
2554
2555 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002556
Guido van Rossum374a9221991-04-04 10:40:29 +00002557 if (why != WHY_NOT)
2558 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002559 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002560
Guido van Rossum374a9221991-04-04 10:40:29 +00002561 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002562
Tim Peters8a5c3c72004-04-05 19:36:21 +00002563 assert(why != WHY_YIELD);
2564 /* Pop remaining stack entries. */
2565 while (!EMPTY()) {
2566 v = POP();
2567 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002568 }
2569
Tim Peters8a5c3c72004-04-05 19:36:21 +00002570 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002571 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002572
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002573fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002574 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002575 if (tstate->c_tracefunc) {
2576 if (why == WHY_RETURN || why == WHY_YIELD) {
2577 if (call_trace(tstate->c_tracefunc,
2578 tstate->c_traceobj, f,
2579 PyTrace_RETURN, retval)) {
2580 Py_XDECREF(retval);
2581 retval = NULL;
2582 why = WHY_EXCEPTION;
2583 }
2584 }
2585 else if (why == WHY_EXCEPTION) {
2586 call_trace_protected(tstate->c_tracefunc,
2587 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002588 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002589 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002590 }
Fred Drake8f51f542001-10-04 14:48:42 +00002591 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002592 if (why == WHY_EXCEPTION)
2593 call_trace_protected(tstate->c_profilefunc,
2594 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002595 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002596 else if (call_trace(tstate->c_profilefunc,
2597 tstate->c_profileobj, f,
2598 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002599 Py_XDECREF(retval);
2600 retval = NULL;
2601 why = WHY_EXCEPTION;
2602 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002603 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002604 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002605
Thomas Wouters477c8d52006-05-27 19:21:47 +00002606 if (tstate->frame->f_exc_type != NULL)
2607 reset_exc_info(tstate);
2608 else {
2609 assert(tstate->frame->f_exc_value == NULL);
2610 assert(tstate->frame->f_exc_traceback == NULL);
2611 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002612
Tim Peters5ca576e2001-06-18 22:08:13 +00002613 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002614 exit_eval_frame:
2615 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002616 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002617
Guido van Rossum96a42c81992-01-12 02:29:51 +00002618 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002619}
2620
Guido van Rossumc2e20742006-02-27 22:32:47 +00002621/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002622 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002623 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002624
Tim Peters6d6c1a32001-08-02 04:15:00 +00002625PyObject *
2626PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002627 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002628 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002629{
2630 register PyFrameObject *f;
2631 register PyObject *retval = NULL;
2632 register PyObject **fastlocals, **freevars;
2633 PyThreadState *tstate = PyThreadState_GET();
2634 PyObject *x, *u;
2635
2636 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002637 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002638 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002639 return NULL;
2640 }
2641
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002642 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002643 assert(globals != NULL);
2644 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002645 if (f == NULL)
2646 return NULL;
2647
2648 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002649 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002650
2651 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002652 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002653 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2654 int i;
2655 int n = argcount;
2656 PyObject *kwdict = NULL;
2657 if (co->co_flags & CO_VARKEYWORDS) {
2658 kwdict = PyDict_New();
2659 if (kwdict == NULL)
2660 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002661 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002662 if (co->co_flags & CO_VARARGS)
2663 i++;
2664 SETLOCAL(i, kwdict);
2665 }
2666 if (argcount > co->co_argcount) {
2667 if (!(co->co_flags & CO_VARARGS)) {
2668 PyErr_Format(PyExc_TypeError,
2669 "%.200s() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002670 "%spositional argument%s (%d given)",
Tim Peters5ca576e2001-06-18 22:08:13 +00002671 PyString_AsString(co->co_name),
2672 defcount ? "at most" : "exactly",
2673 co->co_argcount,
2674 kwcount ? "non-keyword " : "",
2675 co->co_argcount == 1 ? "" : "s",
2676 argcount);
2677 goto fail;
2678 }
2679 n = co->co_argcount;
2680 }
2681 for (i = 0; i < n; i++) {
2682 x = args[i];
2683 Py_INCREF(x);
2684 SETLOCAL(i, x);
2685 }
2686 if (co->co_flags & CO_VARARGS) {
2687 u = PyTuple_New(argcount - n);
2688 if (u == NULL)
2689 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002690 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002691 for (i = n; i < argcount; i++) {
2692 x = args[i];
2693 Py_INCREF(x);
2694 PyTuple_SET_ITEM(u, i-n, x);
2695 }
2696 }
2697 for (i = 0; i < kwcount; i++) {
2698 PyObject *keyword = kws[2*i];
2699 PyObject *value = kws[2*i + 1];
2700 int j;
2701 if (keyword == NULL || !PyString_Check(keyword)) {
2702 PyErr_Format(PyExc_TypeError,
2703 "%.200s() keywords must be strings",
2704 PyString_AsString(co->co_name));
2705 goto fail;
2706 }
2707 /* XXX slow -- speed up using dictionary? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002708 for (j = 0;
2709 j < co->co_argcount + co->co_kwonlyargcount;
2710 j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002711 PyObject *nm = PyTuple_GET_ITEM(
2712 co->co_varnames, j);
2713 int cmp = PyObject_RichCompareBool(
2714 keyword, nm, Py_EQ);
2715 if (cmp > 0)
2716 break;
2717 else if (cmp < 0)
2718 goto fail;
2719 }
2720 /* Check errors from Compare */
2721 if (PyErr_Occurred())
2722 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002723 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002724 if (kwdict == NULL) {
2725 PyErr_Format(PyExc_TypeError,
2726 "%.200s() got an unexpected "
2727 "keyword argument '%.400s'",
2728 PyString_AsString(co->co_name),
2729 PyString_AsString(keyword));
2730 goto fail;
2731 }
2732 PyDict_SetItem(kwdict, keyword, value);
2733 }
2734 else {
2735 if (GETLOCAL(j) != NULL) {
2736 PyErr_Format(PyExc_TypeError,
2737 "%.200s() got multiple "
2738 "values for keyword "
2739 "argument '%.400s'",
2740 PyString_AsString(co->co_name),
2741 PyString_AsString(keyword));
2742 goto fail;
2743 }
2744 Py_INCREF(value);
2745 SETLOCAL(j, value);
2746 }
2747 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002748 if (co->co_kwonlyargcount > 0) {
2749 for (i = co->co_argcount;
2750 i < co->co_argcount + co->co_kwonlyargcount;
2751 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00002752 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002753 if (GETLOCAL(i) != NULL)
2754 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00002755 name = PyTuple_GET_ITEM(co->co_varnames, i);
2756 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002757 if (kwdefs != NULL)
2758 def = PyDict_GetItem(kwdefs, name);
2759 if (def != NULL) {
2760 Py_INCREF(def);
2761 SETLOCAL(i, def);
2762 continue;
2763 }
2764 PyErr_Format(PyExc_TypeError,
2765 "%.200s() needs "
2766 "keyword only argument %s",
2767 PyString_AsString(co->co_name),
2768 PyString_AsString(name));
2769 goto fail;
2770 }
2771 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002772 if (argcount < co->co_argcount) {
2773 int m = co->co_argcount - defcount;
2774 for (i = argcount; i < m; i++) {
2775 if (GETLOCAL(i) == NULL) {
2776 PyErr_Format(PyExc_TypeError,
2777 "%.200s() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002778 "%spositional argument%s "
2779 "(%d given)",
Tim Peters5ca576e2001-06-18 22:08:13 +00002780 PyString_AsString(co->co_name),
2781 ((co->co_flags & CO_VARARGS) ||
2782 defcount) ? "at least"
2783 : "exactly",
2784 m, kwcount ? "non-keyword " : "",
2785 m == 1 ? "" : "s", i);
2786 goto fail;
2787 }
2788 }
2789 if (n > m)
2790 i = n - m;
2791 else
2792 i = 0;
2793 for (; i < defcount; i++) {
2794 if (GETLOCAL(m+i) == NULL) {
2795 PyObject *def = defs[i];
2796 Py_INCREF(def);
2797 SETLOCAL(m+i, def);
2798 }
2799 }
2800 }
2801 }
2802 else {
2803 if (argcount > 0 || kwcount > 0) {
2804 PyErr_Format(PyExc_TypeError,
2805 "%.200s() takes no arguments (%d given)",
2806 PyString_AsString(co->co_name),
2807 argcount + kwcount);
2808 goto fail;
2809 }
2810 }
2811 /* Allocate and initialize storage for cell vars, and copy free
2812 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002813 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002814 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002815 char *cellname, *argname;
2816 PyObject *c;
2817
2818 nargs = co->co_argcount;
2819 if (co->co_flags & CO_VARARGS)
2820 nargs++;
2821 if (co->co_flags & CO_VARKEYWORDS)
2822 nargs++;
2823
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 /* Initialize each cell var, taking into account
2825 cell vars that are initialized from arguments.
2826
2827 Should arrange for the compiler to put cellvars
2828 that are arguments at the beginning of the cellvars
2829 list so that we can march over it more efficiently?
2830 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002831 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002832 cellname = PyString_AS_STRING(
2833 PyTuple_GET_ITEM(co->co_cellvars, i));
2834 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002836 argname = PyString_AS_STRING(
2837 PyTuple_GET_ITEM(co->co_varnames, j));
2838 if (strcmp(cellname, argname) == 0) {
2839 c = PyCell_New(GETLOCAL(j));
2840 if (c == NULL)
2841 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002842 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002843 found = 1;
2844 break;
2845 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002846 }
2847 if (found == 0) {
2848 c = PyCell_New(NULL);
2849 if (c == NULL)
2850 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002851 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002852 }
2853 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002854 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002855 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002856 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002857 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002858 PyObject *o = PyTuple_GET_ITEM(closure, i);
2859 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002860 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002861 }
2862 }
2863
Tim Peters5ca576e2001-06-18 22:08:13 +00002864 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002865 /* Don't need to keep the reference to f_back, it will be set
2866 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002867 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002868 f->f_back = NULL;
2869
Jeremy Hylton985eba52003-02-05 23:13:00 +00002870 PCALL(PCALL_GENERATOR);
2871
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002872 /* Create a new generator that owns the ready to run frame
2873 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002874 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002875 }
2876
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002877 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002878
2879 fail: /* Jump here from prelude on failure */
2880
Tim Petersb13680b2001-11-27 23:29:29 +00002881 /* decref'ing the frame can cause __del__ methods to get invoked,
2882 which can call back into Python. While we're done with the
2883 current Python frame (f), the associated C stack is still in use,
2884 so recursion_depth must be boosted for the duration.
2885 */
2886 assert(tstate != NULL);
2887 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002888 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002889 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002890 return retval;
2891}
2892
2893
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002894/* Implementation notes for set_exc_info() and reset_exc_info():
2895
2896- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2897 'exc_traceback'. These always travel together.
2898
2899- tstate->curexc_ZZZ is the "hot" exception that is set by
2900 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2901
2902- Once an exception is caught by an except clause, it is transferred
2903 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2904 can pick it up. This is the primary task of set_exc_info().
Thomas Wouters477c8d52006-05-27 19:21:47 +00002905 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002906
2907- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2908
2909 Long ago, when none of this existed, there were just a few globals:
2910 one set corresponding to the "hot" exception, and one set
2911 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2912 globals; they were simply stored as sys.exc_ZZZ. For backwards
2913 compatibility, they still are!) The problem was that in code like
2914 this:
2915
2916 try:
2917 "something that may fail"
2918 except "some exception":
2919 "do something else first"
2920 "print the exception from sys.exc_ZZZ."
2921
2922 if "do something else first" invoked something that raised and caught
2923 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2924 cause of subtle bugs. I fixed this by changing the semantics as
2925 follows:
2926
2927 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2928 *in that frame*.
2929
2930 - But initially, and as long as no exception is caught in a given
2931 frame, sys.exc_ZZZ will hold the last exception caught in the
2932 previous frame (or the frame before that, etc.).
2933
2934 The first bullet fixed the bug in the above example. The second
2935 bullet was for backwards compatibility: it was (and is) common to
2936 have a function that is called when an exception is caught, and to
2937 have that function access the caught exception via sys.exc_ZZZ.
2938 (Example: traceback.print_exc()).
2939
2940 At the same time I fixed the problem that sys.exc_ZZZ weren't
2941 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2942 but that's really a separate improvement.
2943
2944 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2945 variables to what they were before the current frame was called. The
2946 set_exc_info() function saves them on the frame so that
2947 reset_exc_info() can restore them. The invariant is that
2948 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2949 exception (where "catching" an exception applies only to successful
2950 except clauses); and if the current frame ever caught an exception,
2951 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2952 at the start of the current frame.
2953
2954*/
2955
Guido van Rossuma027efa1997-05-05 20:56:21 +00002956static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002957set_exc_info(PyThreadState *tstate,
2958 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002959{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002960 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002961 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002962
Thomas Wouters477c8d52006-05-27 19:21:47 +00002963 assert(type != NULL);
2964 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002965 if (frame->f_exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002966 assert(frame->f_exc_value == NULL);
2967 assert(frame->f_exc_traceback == NULL);
2968 /* This frame didn't catch an exception before. */
2969 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002970 if (tstate->exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002971 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002972 Py_INCREF(Py_None);
2973 tstate->exc_type = Py_None;
2974 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002975 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002976 Py_XINCREF(tstate->exc_value);
2977 Py_XINCREF(tstate->exc_traceback);
2978 frame->f_exc_type = tstate->exc_type;
2979 frame->f_exc_value = tstate->exc_value;
2980 frame->f_exc_traceback = tstate->exc_traceback;
2981 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002982 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002983 tmp_type = tstate->exc_type;
2984 tmp_value = tstate->exc_value;
2985 tmp_tb = tstate->exc_traceback;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002986 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002987 Py_XINCREF(value);
2988 Py_XINCREF(tb);
2989 tstate->exc_type = type;
2990 tstate->exc_value = value;
2991 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002992 Py_XDECREF(tmp_type);
2993 Py_XDECREF(tmp_value);
2994 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002995 /* For b/w compatibility */
2996 PySys_SetObject("exc_type", type);
2997 PySys_SetObject("exc_value", value);
2998 PySys_SetObject("exc_traceback", tb);
2999}
3000
3001static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003002reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003003{
3004 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003005 PyObject *tmp_type, *tmp_value, *tmp_tb;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003006
3007 /* It's a precondition that the thread state's frame caught an
3008 * exception -- verify in a debug build.
3009 */
3010 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003011 frame = tstate->frame;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003012 assert(frame != NULL);
3013 assert(frame->f_exc_type != NULL);
3014
3015 /* Copy the frame's exception info back to the thread state. */
3016 tmp_type = tstate->exc_type;
3017 tmp_value = tstate->exc_value;
3018 tmp_tb = tstate->exc_traceback;
3019 Py_INCREF(frame->f_exc_type);
3020 Py_XINCREF(frame->f_exc_value);
3021 Py_XINCREF(frame->f_exc_traceback);
3022 tstate->exc_type = frame->f_exc_type;
3023 tstate->exc_value = frame->f_exc_value;
3024 tstate->exc_traceback = frame->f_exc_traceback;
3025 Py_XDECREF(tmp_type);
3026 Py_XDECREF(tmp_value);
3027 Py_XDECREF(tmp_tb);
3028
3029 /* For b/w compatibility */
3030 PySys_SetObject("exc_type", frame->f_exc_type);
3031 PySys_SetObject("exc_value", frame->f_exc_value);
3032 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3033
3034 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003035 tmp_type = frame->f_exc_type;
3036 tmp_value = frame->f_exc_value;
3037 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003038 frame->f_exc_type = NULL;
3039 frame->f_exc_value = NULL;
3040 frame->f_exc_traceback = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003041 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003042 Py_XDECREF(tmp_value);
3043 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003044}
3045
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003046/* Logic for the raise statement (too complicated for inlining).
3047 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003048static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003049do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003050{
Guido van Rossumd295f121998-04-09 21:39:57 +00003051 if (type == NULL) {
3052 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003053 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003054 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3055 value = tstate->exc_value;
3056 tb = tstate->exc_traceback;
3057 Py_XINCREF(type);
3058 Py_XINCREF(value);
3059 Py_XINCREF(tb);
3060 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003061
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003062 /* We support the following forms of raise:
3063 raise <class>, <classinstance>
3064 raise <class>, <argument tuple>
3065 raise <class>, None
3066 raise <class>, <argument>
3067 raise <classinstance>, None
3068 raise <string>, <object>
3069 raise <string>, None
3070
3071 An omitted second argument is the same as None.
3072
3073 In addition, raise <tuple>, <anything> is the same as
3074 raising the tuple's first item (and it better have one!);
3075 this rule is applied recursively.
3076
3077 Finally, an optional third argument can be supplied, which
3078 gives the traceback to be substituted (useful when
3079 re-raising an exception after examining it). */
3080
3081 /* First, check the traceback argument, replacing None with
3082 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003083 if (tb == Py_None) {
3084 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003085 tb = NULL;
3086 }
3087 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003088 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003089 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003090 goto raise_error;
3091 }
3092
3093 /* Next, replace a missing value with None */
3094 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003095 value = Py_None;
3096 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003097 }
3098
3099 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003100 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3101 PyObject *tmp = type;
3102 type = PyTuple_GET_ITEM(type, 0);
3103 Py_INCREF(type);
3104 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003105 }
3106
Guido van Rossum45aecf42006-03-15 04:58:47 +00003107 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003108 PyErr_NormalizeException(&type, &value, &tb);
3109
Brett Cannonbf364092006-03-01 04:25:17 +00003110 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003111 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003112 if (value != Py_None) {
3113 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003114 "instance exception may not have a separate value");
3115 goto raise_error;
3116 }
3117 else {
3118 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003119 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003120 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003121 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003122 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003123 }
3124 }
3125 else {
3126 /* Not something you can raise. You get an exception
3127 anyway, just not what you specified :-) */
Guido van Rossum45aecf42006-03-15 04:58:47 +00003128 PyErr_SetString(PyExc_TypeError,
3129 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003130 goto raise_error;
3131 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003132 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003133 if (tb == NULL)
3134 return WHY_EXCEPTION;
3135 else
3136 return WHY_RERAISE;
3137 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003138 Py_XDECREF(value);
3139 Py_XDECREF(type);
3140 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003141 return WHY_EXCEPTION;
3142}
3143
Tim Petersd6d010b2001-06-21 02:49:55 +00003144/* Iterate v argcnt times and store the results on the stack (via decreasing
3145 sp). Return 1 for success, 0 if error. */
3146
Barry Warsawe42b18f1997-08-25 22:13:04 +00003147static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003148unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003149{
Tim Petersd6d010b2001-06-21 02:49:55 +00003150 int i = 0;
3151 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003152 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003153
Tim Petersd6d010b2001-06-21 02:49:55 +00003154 assert(v != NULL);
3155
3156 it = PyObject_GetIter(v);
3157 if (it == NULL)
3158 goto Error;
3159
3160 for (; i < argcnt; i++) {
3161 w = PyIter_Next(it);
3162 if (w == NULL) {
3163 /* Iterator done, via error or exhaustion. */
3164 if (!PyErr_Occurred()) {
3165 PyErr_Format(PyExc_ValueError,
3166 "need more than %d value%s to unpack",
3167 i, i == 1 ? "" : "s");
3168 }
3169 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003170 }
3171 *--sp = w;
3172 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003173
3174 /* We better have exhausted the iterator now. */
3175 w = PyIter_Next(it);
3176 if (w == NULL) {
3177 if (PyErr_Occurred())
3178 goto Error;
3179 Py_DECREF(it);
3180 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003181 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003182 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003183 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003184 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003185Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003186 for (; i > 0; i--, sp++)
3187 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003188 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003189 return 0;
3190}
3191
3192
Guido van Rossum96a42c81992-01-12 02:29:51 +00003193#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003194static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003195prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003196{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003197 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003198 if (PyObject_Print(v, stdout, 0) != 0)
3199 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003200 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003201 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003202}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003203#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003204
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003205static void
Fred Drake5755ce62001-06-27 19:19:46 +00003206call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003207{
Guido van Rossumb209a111997-04-29 18:18:01 +00003208 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003209 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003210 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003211 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003212 value = Py_None;
3213 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003214 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003215 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003216 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003217 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003218 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003219 }
Fred Drake5755ce62001-06-27 19:19:46 +00003220 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003221 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003222 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003223 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003224 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003225 Py_XDECREF(type);
3226 Py_XDECREF(value);
3227 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003228 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003229}
3230
Fred Drake4ec5d562001-10-04 19:26:43 +00003231static void
3232call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003233 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003234{
3235 PyObject *type, *value, *traceback;
3236 int err;
3237 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003238 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003239 if (err == 0)
3240 PyErr_Restore(type, value, traceback);
3241 else {
3242 Py_XDECREF(type);
3243 Py_XDECREF(value);
3244 Py_XDECREF(traceback);
3245 }
3246}
3247
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003248static int
Fred Drake5755ce62001-06-27 19:19:46 +00003249call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3250 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003251{
Fred Drake5755ce62001-06-27 19:19:46 +00003252 register PyThreadState *tstate = frame->f_tstate;
3253 int result;
3254 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003255 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003256 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003257 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003258 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003259 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3260 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003261 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003262 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003263}
3264
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003265PyObject *
3266_PyEval_CallTracing(PyObject *func, PyObject *args)
3267{
3268 PyFrameObject *frame = PyEval_GetFrame();
3269 PyThreadState *tstate = frame->f_tstate;
3270 int save_tracing = tstate->tracing;
3271 int save_use_tracing = tstate->use_tracing;
3272 PyObject *result;
3273
3274 tstate->tracing = 0;
3275 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3276 || (tstate->c_profilefunc != NULL));
3277 result = PyObject_Call(func, args, NULL);
3278 tstate->tracing = save_tracing;
3279 tstate->use_tracing = save_use_tracing;
3280 return result;
3281}
3282
Michael W. Hudson006c7522002-11-08 13:08:46 +00003283static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003284maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003285 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3286 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003287{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003288 int result = 0;
3289
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003290 /* If the last instruction executed isn't in the current
3291 instruction window, reset the window. If the last
3292 instruction happens to fall at the start of a line or if it
3293 represents a jump backwards, call the trace function.
3294 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003295 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003296 int line;
3297 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003298
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003299 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3300 &bounds);
3301 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003302 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003303 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003304 PyTrace_LINE, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003305 }
3306 *instr_lb = bounds.ap_lower;
3307 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003308 }
Armin Rigobf57a142004-03-22 19:24:58 +00003309 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003310 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003311 }
3312 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003313 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003314}
3315
Fred Drake5755ce62001-06-27 19:19:46 +00003316void
3317PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003318{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003319 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003320 PyObject *temp = tstate->c_profileobj;
3321 Py_XINCREF(arg);
3322 tstate->c_profilefunc = NULL;
3323 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003324 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003325 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003326 Py_XDECREF(temp);
3327 tstate->c_profilefunc = func;
3328 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003329 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003330 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003331}
3332
3333void
3334PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3335{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003336 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003337 PyObject *temp = tstate->c_traceobj;
3338 Py_XINCREF(arg);
3339 tstate->c_tracefunc = NULL;
3340 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003341 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003342 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003343 Py_XDECREF(temp);
3344 tstate->c_tracefunc = func;
3345 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003346 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003347 tstate->use_tracing = ((func != NULL)
3348 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003349}
3350
Guido van Rossumb209a111997-04-29 18:18:01 +00003351PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003352PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003353{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003354 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003355 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003356 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003357 else
3358 return current_frame->f_builtins;
3359}
3360
Guido van Rossumb209a111997-04-29 18:18:01 +00003361PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003362PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003363{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003364 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003365 if (current_frame == NULL)
3366 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003367 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003368 return current_frame->f_locals;
3369}
3370
Guido van Rossumb209a111997-04-29 18:18:01 +00003371PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003372PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003373{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003374 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003375 if (current_frame == NULL)
3376 return NULL;
3377 else
3378 return current_frame->f_globals;
3379}
3380
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003381PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003382PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003383{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003384 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003385 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003386}
3387
Guido van Rossum6135a871995-01-09 17:53:26 +00003388int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003389PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003390{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003391 PyFrameObject *current_frame = PyEval_GetFrame();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003392 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003393}
3394
Guido van Rossumbe270261997-05-22 22:26:18 +00003395int
Tim Peters5ba58662001-07-16 02:29:45 +00003396PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003397{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003398 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003399 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003400
3401 if (current_frame != NULL) {
3402 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003403 const int compilerflags = codeflags & PyCF_MASK;
3404 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003405 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003406 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003407 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003408#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003409 if (codeflags & CO_GENERATOR_ALLOWED) {
3410 result = 1;
3411 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3412 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003413#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003414 }
3415 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003416}
3417
3418int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003419Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003420{
Guido van Rossumb209a111997-04-29 18:18:01 +00003421 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003422 if (f == NULL)
3423 return 0;
3424 if (!PyFile_SoftSpace(f, 0))
3425 return 0;
3426 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003427}
3428
Guido van Rossum3f5da241990-12-20 15:06:42 +00003429
Guido van Rossum681d79a1995-07-18 14:51:37 +00003430/* External interface to call any callable object.
3431 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003432
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003433#undef PyEval_CallObject
3434/* for backward compatibility: export this interface */
3435
Guido van Rossumb209a111997-04-29 18:18:01 +00003436PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003437PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003438{
Guido van Rossumb209a111997-04-29 18:18:01 +00003439 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003440}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003441#define PyEval_CallObject(func,arg) \
3442 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003443
Guido van Rossumb209a111997-04-29 18:18:01 +00003444PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003445PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003446{
Jeremy Hylton52820442001-01-03 23:52:36 +00003447 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003448
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003449 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003450 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003451 if (arg == NULL)
3452 return NULL;
3453 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003454 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003455 PyErr_SetString(PyExc_TypeError,
3456 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003457 return NULL;
3458 }
3459 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003460 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003461
Guido van Rossumb209a111997-04-29 18:18:01 +00003462 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003463 PyErr_SetString(PyExc_TypeError,
3464 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003465 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003466 return NULL;
3467 }
3468
Tim Peters6d6c1a32001-08-02 04:15:00 +00003469 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003470 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003471 return result;
3472}
3473
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003474const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003475PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003476{
3477 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003478 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003479 else if (PyFunction_Check(func))
3480 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3481 else if (PyCFunction_Check(func))
3482 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003483 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003484 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003485}
3486
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003487const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003488PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003489{
3490 if (PyMethod_Check(func))
3491 return "()";
3492 else if (PyFunction_Check(func))
3493 return "()";
3494 else if (PyCFunction_Check(func))
3495 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003496 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003497 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003498}
3499
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003500static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003501err_args(PyObject *func, int flags, int nargs)
3502{
3503 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003504 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003505 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003506 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003507 nargs);
3508 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003509 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003510 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003511 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003512 nargs);
3513}
3514
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003515#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003516if (tstate->use_tracing && tstate->c_profilefunc) { \
3517 if (call_trace(tstate->c_profilefunc, \
3518 tstate->c_profileobj, \
3519 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003520 func)) { \
3521 x = NULL; \
3522 } \
3523 else { \
3524 x = call; \
3525 if (tstate->c_profilefunc != NULL) { \
3526 if (x == NULL) { \
3527 call_trace_protected(tstate->c_profilefunc, \
3528 tstate->c_profileobj, \
3529 tstate->frame, PyTrace_C_EXCEPTION, \
3530 func); \
3531 /* XXX should pass (type, value, tb) */ \
3532 } else { \
3533 if (call_trace(tstate->c_profilefunc, \
3534 tstate->c_profileobj, \
3535 tstate->frame, PyTrace_C_RETURN, \
3536 func)) { \
3537 Py_DECREF(x); \
3538 x = NULL; \
3539 } \
3540 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003541 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003542 } \
3543} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003544 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003545 }
3546
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003547static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003548call_function(PyObject ***pp_stack, int oparg
3549#ifdef WITH_TSC
3550 , uint64* pintr0, uint64* pintr1
3551#endif
3552 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003553{
3554 int na = oparg & 0xff;
3555 int nk = (oparg>>8) & 0xff;
3556 int n = na + 2 * nk;
3557 PyObject **pfunc = (*pp_stack) - n - 1;
3558 PyObject *func = *pfunc;
3559 PyObject *x, *w;
3560
Jeremy Hylton985eba52003-02-05 23:13:00 +00003561 /* Always dispatch PyCFunction first, because these are
3562 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003563 */
3564 if (PyCFunction_Check(func) && nk == 0) {
3565 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003566 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003567
3568 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003569 if (flags & (METH_NOARGS | METH_O)) {
3570 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3571 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003572 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003573 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003574 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003575 else if (flags & METH_O && na == 1) {
3576 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003577 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003578 Py_DECREF(arg);
3579 }
3580 else {
3581 err_args(func, flags, na);
3582 x = NULL;
3583 }
3584 }
3585 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003586 PyObject *callargs;
3587 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003588 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003589 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003590 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003591 Py_XDECREF(callargs);
3592 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003593 } else {
3594 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3595 /* optimize access to bound methods */
3596 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003597 PCALL(PCALL_METHOD);
3598 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003599 Py_INCREF(self);
3600 func = PyMethod_GET_FUNCTION(func);
3601 Py_INCREF(func);
3602 Py_DECREF(*pfunc);
3603 *pfunc = self;
3604 na++;
3605 n++;
3606 } else
3607 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003608 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003609 if (PyFunction_Check(func))
3610 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003611 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003612 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003613 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003614 Py_DECREF(func);
3615 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003616
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003617 /* Clear the stack of the function object. Also removes
3618 the arguments in case they weren't consumed already
3619 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003620 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003621 while ((*pp_stack) > pfunc) {
3622 w = EXT_POP(*pp_stack);
3623 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003624 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003625 }
3626 return x;
3627}
3628
Jeremy Hylton192690e2002-08-16 18:36:11 +00003629/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003630 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003631 For the simplest case -- a function that takes only positional
3632 arguments and is called with only positional arguments -- it
3633 inlines the most primitive frame setup code from
3634 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3635 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003636*/
3637
3638static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003639fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003640{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003641 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003642 PyObject *globals = PyFunction_GET_GLOBALS(func);
3643 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003644 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003645 PyObject **d = NULL;
3646 int nd = 0;
3647
Jeremy Hylton985eba52003-02-05 23:13:00 +00003648 PCALL(PCALL_FUNCTION);
3649 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003650 if (argdefs == NULL && co->co_argcount == n &&
3651 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003652 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3653 PyFrameObject *f;
3654 PyObject *retval = NULL;
3655 PyThreadState *tstate = PyThreadState_GET();
3656 PyObject **fastlocals, **stack;
3657 int i;
3658
3659 PCALL(PCALL_FASTER_FUNCTION);
3660 assert(globals != NULL);
3661 /* XXX Perhaps we should create a specialized
3662 PyFrame_New() that doesn't take locals, but does
3663 take builtins without sanity checking them.
3664 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003665 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003666 f = PyFrame_New(tstate, co, globals, NULL);
3667 if (f == NULL)
3668 return NULL;
3669
3670 fastlocals = f->f_localsplus;
3671 stack = (*pp_stack) - n;
3672
3673 for (i = 0; i < n; i++) {
3674 Py_INCREF(*stack);
3675 fastlocals[i] = *stack++;
3676 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003677 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003678 ++tstate->recursion_depth;
3679 Py_DECREF(f);
3680 --tstate->recursion_depth;
3681 return retval;
3682 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003683 if (argdefs != NULL) {
3684 d = &PyTuple_GET_ITEM(argdefs, 0);
3685 nd = ((PyTupleObject *)argdefs)->ob_size;
3686 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003687 return PyEval_EvalCodeEx(co, globals,
3688 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003689 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003690 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003691}
3692
3693static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003694update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3695 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003696{
3697 PyObject *kwdict = NULL;
3698 if (orig_kwdict == NULL)
3699 kwdict = PyDict_New();
3700 else {
3701 kwdict = PyDict_Copy(orig_kwdict);
3702 Py_DECREF(orig_kwdict);
3703 }
3704 if (kwdict == NULL)
3705 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003706 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003707 int err;
3708 PyObject *value = EXT_POP(*pp_stack);
3709 PyObject *key = EXT_POP(*pp_stack);
3710 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003711 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003712 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003713 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003714 PyEval_GetFuncName(func),
3715 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003716 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003717 Py_DECREF(key);
3718 Py_DECREF(value);
3719 Py_DECREF(kwdict);
3720 return NULL;
3721 }
3722 err = PyDict_SetItem(kwdict, key, value);
3723 Py_DECREF(key);
3724 Py_DECREF(value);
3725 if (err) {
3726 Py_DECREF(kwdict);
3727 return NULL;
3728 }
3729 }
3730 return kwdict;
3731}
3732
3733static PyObject *
3734update_star_args(int nstack, int nstar, PyObject *stararg,
3735 PyObject ***pp_stack)
3736{
3737 PyObject *callargs, *w;
3738
3739 callargs = PyTuple_New(nstack + nstar);
3740 if (callargs == NULL) {
3741 return NULL;
3742 }
3743 if (nstar) {
3744 int i;
3745 for (i = 0; i < nstar; i++) {
3746 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3747 Py_INCREF(a);
3748 PyTuple_SET_ITEM(callargs, nstack + i, a);
3749 }
3750 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003751 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003752 w = EXT_POP(*pp_stack);
3753 PyTuple_SET_ITEM(callargs, nstack, w);
3754 }
3755 return callargs;
3756}
3757
3758static PyObject *
3759load_args(PyObject ***pp_stack, int na)
3760{
3761 PyObject *args = PyTuple_New(na);
3762 PyObject *w;
3763
3764 if (args == NULL)
3765 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003766 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003767 w = EXT_POP(*pp_stack);
3768 PyTuple_SET_ITEM(args, na, w);
3769 }
3770 return args;
3771}
3772
3773static PyObject *
3774do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3775{
3776 PyObject *callargs = NULL;
3777 PyObject *kwdict = NULL;
3778 PyObject *result = NULL;
3779
3780 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003781 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003782 if (kwdict == NULL)
3783 goto call_fail;
3784 }
3785 callargs = load_args(pp_stack, na);
3786 if (callargs == NULL)
3787 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003788#ifdef CALL_PROFILE
3789 /* At this point, we have to look at the type of func to
3790 update the call stats properly. Do it here so as to avoid
3791 exposing the call stats machinery outside ceval.c
3792 */
3793 if (PyFunction_Check(func))
3794 PCALL(PCALL_FUNCTION);
3795 else if (PyMethod_Check(func))
3796 PCALL(PCALL_METHOD);
3797 else if (PyType_Check(func))
3798 PCALL(PCALL_TYPE);
3799 else
3800 PCALL(PCALL_OTHER);
3801#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003802 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003803 call_fail:
3804 Py_XDECREF(callargs);
3805 Py_XDECREF(kwdict);
3806 return result;
3807}
3808
3809static PyObject *
3810ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3811{
3812 int nstar = 0;
3813 PyObject *callargs = NULL;
3814 PyObject *stararg = NULL;
3815 PyObject *kwdict = NULL;
3816 PyObject *result = NULL;
3817
3818 if (flags & CALL_FLAG_KW) {
3819 kwdict = EXT_POP(*pp_stack);
3820 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003821 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003822 "%s%s argument after ** "
3823 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003824 PyEval_GetFuncName(func),
3825 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003826 goto ext_call_fail;
3827 }
3828 }
3829 if (flags & CALL_FLAG_VAR) {
3830 stararg = EXT_POP(*pp_stack);
3831 if (!PyTuple_Check(stararg)) {
3832 PyObject *t = NULL;
3833 t = PySequence_Tuple(stararg);
3834 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003835 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3836 PyErr_Format(PyExc_TypeError,
3837 "%s%s argument after * "
3838 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003839 PyEval_GetFuncName(func),
3840 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003841 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003842 goto ext_call_fail;
3843 }
3844 Py_DECREF(stararg);
3845 stararg = t;
3846 }
3847 nstar = PyTuple_GET_SIZE(stararg);
3848 }
3849 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003850 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003851 if (kwdict == NULL)
3852 goto ext_call_fail;
3853 }
3854 callargs = update_star_args(na, nstar, stararg, pp_stack);
3855 if (callargs == NULL)
3856 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003857#ifdef CALL_PROFILE
3858 /* At this point, we have to look at the type of func to
3859 update the call stats properly. Do it here so as to avoid
3860 exposing the call stats machinery outside ceval.c
3861 */
3862 if (PyFunction_Check(func))
3863 PCALL(PCALL_FUNCTION);
3864 else if (PyMethod_Check(func))
3865 PCALL(PCALL_METHOD);
3866 else if (PyType_Check(func))
3867 PCALL(PCALL_TYPE);
3868 else
3869 PCALL(PCALL_OTHER);
3870#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003871 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003872 ext_call_fail:
3873 Py_XDECREF(callargs);
3874 Py_XDECREF(kwdict);
3875 Py_XDECREF(stararg);
3876 return result;
3877}
3878
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003879/* Extract a slice index from a PyInt or PyLong or an object with the
3880 nb_index slot defined, and store in *pi.
3881 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3882 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003883 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003884*/
Tim Petersb5196382001-12-16 19:44:20 +00003885/* Note: If v is NULL, return success without storing into *pi. This
3886 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3887 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003888*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003889int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003890_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891{
Tim Petersb5196382001-12-16 19:44:20 +00003892 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003893 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00003894 if (PyInt_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003895 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3896 however, it looks like it should be AsSsize_t.
3897 There should be a comment here explaining why.
3898 */
3899 x = PyInt_AS_LONG(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003900 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003901 else if (PyIndex_Check(v)) {
3902 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003903 if (x == -1 && PyErr_Occurred())
3904 return 0;
3905 }
3906 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003907 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003908 "slice indices must be integers or "
3909 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003910 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003911 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003912 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003913 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003914 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003915}
3916
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003917#undef ISINDEX
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003918#define ISINDEX(x) ((x) == NULL || \
3919 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003920
Guido van Rossumb209a111997-04-29 18:18:01 +00003921static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003922apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003923{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003924 PyTypeObject *tp = u->ob_type;
3925 PySequenceMethods *sq = tp->tp_as_sequence;
3926
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003927 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003928 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003929 if (!_PyEval_SliceIndex(v, &ilow))
3930 return NULL;
3931 if (!_PyEval_SliceIndex(w, &ihigh))
3932 return NULL;
3933 return PySequence_GetSlice(u, ilow, ihigh);
3934 }
3935 else {
3936 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003937 if (slice != NULL) {
3938 PyObject *res = PyObject_GetItem(u, slice);
3939 Py_DECREF(slice);
3940 return res;
3941 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003942 else
3943 return NULL;
3944 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003945}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003946
3947static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003948assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3949 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003950{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003951 PyTypeObject *tp = u->ob_type;
3952 PySequenceMethods *sq = tp->tp_as_sequence;
3953
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003954 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003955 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003956 if (!_PyEval_SliceIndex(v, &ilow))
3957 return -1;
3958 if (!_PyEval_SliceIndex(w, &ihigh))
3959 return -1;
3960 if (x == NULL)
3961 return PySequence_DelSlice(u, ilow, ihigh);
3962 else
3963 return PySequence_SetSlice(u, ilow, ihigh, x);
3964 }
3965 else {
3966 PyObject *slice = PySlice_New(v, w, NULL);
3967 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003968 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003969 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003970 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003971 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003972 res = PyObject_DelItem(u, slice);
3973 Py_DECREF(slice);
3974 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003975 }
3976 else
3977 return -1;
3978 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003979}
3980
Guido van Rossumb209a111997-04-29 18:18:01 +00003981static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003982cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003983{
Guido van Rossumac7be682001-01-17 15:42:30 +00003984 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003985 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003986 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003987 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003988 break;
3989 case PyCmp_IS_NOT:
3990 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003991 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003992 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003993 res = PySequence_Contains(w, v);
3994 if (res < 0)
3995 return NULL;
3996 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003997 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003998 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003999 if (res < 0)
4000 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004001 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004002 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004003 case PyCmp_EXC_MATCH:
Barry Warsaw4249f541997-08-22 21:26:19 +00004004 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004005 break;
4006 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004007 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004008 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004009 v = res ? Py_True : Py_False;
4010 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004011 return v;
4012}
4013
Thomas Wouters52152252000-08-17 22:55:00 +00004014static PyObject *
4015import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004016{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004017 PyObject *x;
4018
4019 x = PyObject_GetAttr(v, name);
4020 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004021 PyErr_Format(PyExc_ImportError,
4022 "cannot import name %.230s",
4023 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004024 }
Thomas Wouters52152252000-08-17 22:55:00 +00004025 return x;
4026}
Guido van Rossumac7be682001-01-17 15:42:30 +00004027
Thomas Wouters52152252000-08-17 22:55:00 +00004028static int
4029import_all_from(PyObject *locals, PyObject *v)
4030{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004031 PyObject *all = PyObject_GetAttrString(v, "__all__");
4032 PyObject *dict, *name, *value;
4033 int skip_leading_underscores = 0;
4034 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004035
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004036 if (all == NULL) {
4037 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4038 return -1; /* Unexpected error */
4039 PyErr_Clear();
4040 dict = PyObject_GetAttrString(v, "__dict__");
4041 if (dict == NULL) {
4042 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4043 return -1;
4044 PyErr_SetString(PyExc_ImportError,
4045 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004046 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004047 }
4048 all = PyMapping_Keys(dict);
4049 Py_DECREF(dict);
4050 if (all == NULL)
4051 return -1;
4052 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004053 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004054
4055 for (pos = 0, err = 0; ; pos++) {
4056 name = PySequence_GetItem(all, pos);
4057 if (name == NULL) {
4058 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4059 err = -1;
4060 else
4061 PyErr_Clear();
4062 break;
4063 }
4064 if (skip_leading_underscores &&
4065 PyString_Check(name) &&
4066 PyString_AS_STRING(name)[0] == '_')
4067 {
4068 Py_DECREF(name);
4069 continue;
4070 }
4071 value = PyObject_GetAttr(v, name);
4072 if (value == NULL)
4073 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004074 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004075 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004076 else
4077 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004078 Py_DECREF(name);
4079 Py_XDECREF(value);
4080 if (err != 0)
4081 break;
4082 }
4083 Py_DECREF(all);
4084 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004085}
4086
Guido van Rossumb209a111997-04-29 18:18:01 +00004087static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004088build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004089{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004090 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004091
4092 if (PyDict_Check(methods))
4093 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004094 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004095 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004096 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4097 base = PyTuple_GET_ITEM(bases, 0);
4098 metaclass = PyObject_GetAttrString(base, "__class__");
4099 if (metaclass == NULL) {
4100 PyErr_Clear();
4101 metaclass = (PyObject *)base->ob_type;
4102 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004103 }
4104 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004105 else {
4106 PyObject *g = PyEval_GetGlobals();
4107 if (g != NULL && PyDict_Check(g))
4108 metaclass = PyDict_GetItemString(g, "__metaclass__");
4109 if (metaclass == NULL)
Guido van Rossum45aecf42006-03-15 04:58:47 +00004110 metaclass = (PyObject *) &PyType_Type;
Guido van Rossum7851eea2001-09-12 19:19:18 +00004111 Py_INCREF(metaclass);
4112 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004113 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004114 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004115 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004116 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004117 in a base that was not a class (such the random module
4118 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004119 by augmenting the error message with more information.*/
4120
4121 PyObject *ptype, *pvalue, *ptraceback;
4122
4123 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4124 if (PyString_Check(pvalue)) {
4125 PyObject *newmsg;
4126 newmsg = PyString_FromFormat(
4127 "Error when calling the metaclass bases\n %s",
4128 PyString_AS_STRING(pvalue));
4129 if (newmsg != NULL) {
4130 Py_DECREF(pvalue);
4131 pvalue = newmsg;
4132 }
4133 }
4134 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004135 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004136 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004137}
4138
Guido van Rossumac7be682001-01-17 15:42:30 +00004139static void
Paul Prescode68140d2000-08-30 20:25:01 +00004140format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4141{
4142 char *obj_str;
4143
4144 if (!obj)
4145 return;
4146
4147 obj_str = PyString_AsString(obj);
4148 if (!obj_str)
4149 return;
4150
4151 PyErr_Format(exc, format_str, obj_str);
4152}
Guido van Rossum950361c1997-01-24 13:49:28 +00004153
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004154static PyObject *
4155string_concatenate(PyObject *v, PyObject *w,
4156 PyFrameObject *f, unsigned char *next_instr)
4157{
4158 /* This function implements 'variable += expr' when both arguments
4159 are strings. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004160 Py_ssize_t v_len = PyString_GET_SIZE(v);
4161 Py_ssize_t w_len = PyString_GET_SIZE(w);
4162 Py_ssize_t new_len = v_len + w_len;
4163 if (new_len < 0) {
4164 PyErr_SetString(PyExc_OverflowError,
4165 "strings are too large to concat");
4166 return NULL;
4167 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004168
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004169 if (v->ob_refcnt == 2) {
4170 /* In the common case, there are 2 references to the value
4171 * stored in 'variable' when the += is performed: one on the
4172 * value stack (in 'v') and one still stored in the 'variable'.
4173 * We try to delete the variable now to reduce the refcnt to 1.
4174 */
4175 switch (*next_instr) {
4176 case STORE_FAST:
4177 {
4178 int oparg = PEEKARG();
4179 PyObject **fastlocals = f->f_localsplus;
4180 if (GETLOCAL(oparg) == v)
4181 SETLOCAL(oparg, NULL);
4182 break;
4183 }
4184 case STORE_DEREF:
4185 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004186 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004187 PyObject *c = freevars[PEEKARG()];
4188 if (PyCell_GET(c) == v)
4189 PyCell_Set(c, NULL);
4190 break;
4191 }
4192 case STORE_NAME:
4193 {
4194 PyObject *names = f->f_code->co_names;
4195 PyObject *name = GETITEM(names, PEEKARG());
4196 PyObject *locals = f->f_locals;
4197 if (PyDict_CheckExact(locals) &&
4198 PyDict_GetItem(locals, name) == v) {
4199 if (PyDict_DelItem(locals, name) != 0) {
4200 PyErr_Clear();
4201 }
4202 }
4203 break;
4204 }
4205 }
4206 }
4207
Armin Rigo618fbf52004-08-07 20:58:32 +00004208 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004209 /* Now we own the last reference to 'v', so we can resize it
4210 * in-place.
4211 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004212 if (_PyString_Resize(&v, new_len) != 0) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004213 /* XXX if _PyString_Resize() fails, 'v' has been
4214 * deallocated so it cannot be put back into 'variable'.
4215 * The MemoryError is raised when there is no value in
4216 * 'variable', which might (very remotely) be a cause
4217 * of incompatibilities.
4218 */
4219 return NULL;
4220 }
4221 /* copy 'w' into the newly allocated area of 'v' */
4222 memcpy(PyString_AS_STRING(v) + v_len,
4223 PyString_AS_STRING(w), w_len);
4224 return v;
4225 }
4226 else {
4227 /* When in-place resizing is not an option. */
4228 PyString_Concat(&v, w);
4229 return v;
4230 }
4231}
4232
Guido van Rossum950361c1997-01-24 13:49:28 +00004233#ifdef DYNAMIC_EXECUTION_PROFILE
4234
Skip Montanarof118cb12001-10-15 20:51:38 +00004235static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004236getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004237{
4238 int i;
4239 PyObject *l = PyList_New(256);
4240 if (l == NULL) return NULL;
4241 for (i = 0; i < 256; i++) {
4242 PyObject *x = PyInt_FromLong(a[i]);
4243 if (x == NULL) {
4244 Py_DECREF(l);
4245 return NULL;
4246 }
4247 PyList_SetItem(l, i, x);
4248 }
4249 for (i = 0; i < 256; i++)
4250 a[i] = 0;
4251 return l;
4252}
4253
4254PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004255_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004256{
4257#ifndef DXPAIRS
4258 return getarray(dxp);
4259#else
4260 int i;
4261 PyObject *l = PyList_New(257);
4262 if (l == NULL) return NULL;
4263 for (i = 0; i < 257; i++) {
4264 PyObject *x = getarray(dxpairs[i]);
4265 if (x == NULL) {
4266 Py_DECREF(l);
4267 return NULL;
4268 }
4269 PyList_SetItem(l, i, x);
4270 }
4271 return l;
4272#endif
4273}
4274
4275#endif