blob: 7511bb6a5d8644dea76e7fa998ff11d6804ef6bb [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;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000527 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000528 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000529 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000530 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000531
Tim Peters8a5c3c72004-04-05 19:36:21 +0000532 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000533
534 not (instr_lb <= current_bytecode_offset < instr_ub)
535
Tim Peters8a5c3c72004-04-05 19:36:21 +0000536 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000537 initial values are such as to make this false the first
538 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000539 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000540
Guido van Rossumd076c731998-10-07 19:42:25 +0000541 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000542 PyObject *names;
543 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000544#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000545 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000546 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000547#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000548
Neal Norwitza81d2202002-07-14 00:27:26 +0000549/* Tuple access macros */
550
551#ifndef Py_DEBUG
552#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
553#else
554#define GETITEM(v, i) PyTuple_GetItem((v), (i))
555#endif
556
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000557#ifdef WITH_TSC
558/* Use Pentium timestamp counter to mark certain events:
559 inst0 -- beginning of switch statement for opcode dispatch
560 inst1 -- end of switch statement (may be skipped)
561 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000562 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000563 (may be skipped)
564 intr1 -- beginning of long interruption
565 intr2 -- end of long interruption
566
567 Many opcodes call out to helper C functions. In some cases, the
568 time in those functions should be counted towards the time for the
569 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
570 calls another Python function; there's no point in charge all the
571 bytecode executed by the called function to the caller.
572
573 It's hard to make a useful judgement statically. In the presence
574 of operator overloading, it's impossible to tell if a call will
575 execute new Python code or not.
576
577 It's a case-by-case judgement. I'll use intr1 for the following
578 cases:
579
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000580 IMPORT_STAR
581 IMPORT_FROM
582 CALL_FUNCTION (and friends)
583
584 */
585 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
586 int ticked = 0;
587
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000588 READ_TIMESTAMP(inst0);
589 READ_TIMESTAMP(inst1);
590 READ_TIMESTAMP(loop0);
591 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000592
593 /* shut up the compiler */
594 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000595#endif
596
Guido van Rossum374a9221991-04-04 10:40:29 +0000597/* Code access macros */
598
Martin v. Löwis18e16552006-02-15 17:27:45 +0000599#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000600#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000601#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000602#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000603#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000604#define JUMPBY(x) (next_instr += (x))
605
Raymond Hettingerf606f872003-03-16 03:11:04 +0000606/* OpCode prediction macros
607 Some opcodes tend to come in pairs thus making it possible to predict
608 the second code when the first is run. For example, COMPARE_OP is often
609 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
610 followed by a POP_TOP.
611
612 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000613 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000614 processor has a high likelihood of making its own successful branch
615 prediction which results in a nearly zero overhead transition to the
616 next opcode.
617
618 A successful prediction saves a trip through the eval-loop including
619 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000620
Tim Peters8a5c3c72004-04-05 19:36:21 +0000621 If collecting opcode statistics, turn off prediction so that
622 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000623 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000624*/
625
Raymond Hettingera7216982004-02-08 19:59:27 +0000626#ifdef DYNAMIC_EXECUTION_PROFILE
627#define PREDICT(op) if (0) goto PRED_##op
628#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000629#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000630#endif
631
Raymond Hettingerf606f872003-03-16 03:11:04 +0000632#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000633#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000634
Guido van Rossum374a9221991-04-04 10:40:29 +0000635/* Stack manipulation macros */
636
Martin v. Löwis18e16552006-02-15 17:27:45 +0000637/* The stack can grow at most MAXINT deep, as co_nlocals and
638 co_stacksize are ints. */
639#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000640#define EMPTY() (STACK_LEVEL() == 0)
641#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000642#define SECOND() (stack_pointer[-2])
643#define THIRD() (stack_pointer[-3])
644#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000645#define SET_TOP(v) (stack_pointer[-1] = (v))
646#define SET_SECOND(v) (stack_pointer[-2] = (v))
647#define SET_THIRD(v) (stack_pointer[-3] = (v))
648#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000649#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000650#define BASIC_PUSH(v) (*stack_pointer++ = (v))
651#define BASIC_POP() (*--stack_pointer)
652
Guido van Rossum96a42c81992-01-12 02:29:51 +0000653#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000654#define PUSH(v) { (void)(BASIC_PUSH(v), \
655 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656 assert(STACK_LEVEL() <= co->co_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000657#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000658#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
659 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000660 assert(STACK_LEVEL() <= co->co_stacksize); }
Guido van Rossumc2e20742006-02-27 22:32:47 +0000661#define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000662#else
663#define PUSH(v) BASIC_PUSH(v)
664#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000665#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000666#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000667#endif
668
Guido van Rossum681d79a1995-07-18 14:51:37 +0000669/* Local variable macros */
670
671#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000672
673/* The SETLOCAL() macro must not DECREF the local variable in-place and
674 then store the new value; it must copy the old value to a temporary
675 value, then store the new value, and then DECREF the temporary value.
676 This is because it is possible that during the DECREF the frame is
677 accessed by other code (e.g. a __del__ method or gc.collect()) and the
678 variable would be pointing to already-freed memory. */
679#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
680 GETLOCAL(i) = value; \
681 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000682
Guido van Rossuma027efa1997-05-05 20:56:21 +0000683/* Start of code */
684
Tim Peters5ca576e2001-06-18 22:08:13 +0000685 if (f == NULL)
686 return NULL;
687
Armin Rigo1d313ab2003-10-25 14:33:09 +0000688 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000689 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000690 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000691
Tim Peters5ca576e2001-06-18 22:08:13 +0000692 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000693
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000694 if (tstate->use_tracing) {
695 if (tstate->c_tracefunc != NULL) {
696 /* tstate->c_tracefunc, if defined, is a
697 function that will be called on *every* entry
698 to a code block. Its return value, if not
699 None, is a function that will be called at
700 the start of each executed line of code.
701 (Actually, the function must return itself
702 in order to continue tracing.) The trace
703 functions are called with three arguments:
704 a pointer to the current frame, a string
705 indicating why the function is called, and
706 an argument which depends on the situation.
707 The global trace function is also called
708 whenever an exception is detected. */
709 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
710 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000711 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000712 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000713 }
714 }
715 if (tstate->c_profilefunc != NULL) {
716 /* Similar for c_profilefunc, except it needn't
717 return itself and isn't called for "line" events */
718 if (call_trace(tstate->c_profilefunc,
719 tstate->c_profileobj,
720 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000721 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000722 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000723 }
724 }
725 }
726
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000727 co = f->f_code;
728 names = co->co_names;
729 consts = co->co_consts;
730 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000731 freevars = f->f_localsplus + co->co_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000732 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000733 /* An explanation is in order for the next line.
734
735 f->f_lasti now refers to the index of the last instruction
736 executed. You might think this was obvious from the name, but
737 this wasn't always true before 2.3! PyFrame_New now sets
738 f->f_lasti to -1 (i.e. the index *before* the first instruction)
739 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000740 does work. Promise.
741
742 When the PREDICT() macros are enabled, some opcode pairs follow in
743 direct succession without updating f->f_lasti. A successful
744 prediction effectively links the two codes together as if they
745 were a single new opcode; accordingly,f->f_lasti will point to
746 the first code in the pair (for instance, GET_ITER followed by
747 FOR_ITER is effectively a single opcode and f->f_lasti will point
748 at to the beginning of the combined pair.)
749 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000750 next_instr = first_instr + f->f_lasti + 1;
751 stack_pointer = f->f_stacktop;
752 assert(stack_pointer != NULL);
753 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
754
Tim Peters5ca576e2001-06-18 22:08:13 +0000755#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000757#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000758#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000759 filename = PyString_AsString(co->co_filename);
760#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000761
Guido van Rossum374a9221991-04-04 10:40:29 +0000762 why = WHY_NOT;
763 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000764 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000765 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000766
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000768 why = WHY_EXCEPTION;
769 goto on_error;
770 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000771
Guido van Rossum374a9221991-04-04 10:40:29 +0000772 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000773#ifdef WITH_TSC
774 if (inst1 == 0) {
775 /* Almost surely, the opcode executed a break
776 or a continue, preventing inst1 from being set
777 on the way out of the loop.
778 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000779 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000780 loop1 = inst1;
781 }
782 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
783 intr0, intr1);
784 ticked = 0;
785 inst1 = 0;
786 intr0 = 0;
787 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000788 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000789#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000790 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000791 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000792
Guido van Rossuma027efa1997-05-05 20:56:21 +0000793 /* Do periodic things. Doing this every time through
794 the loop would add too much overhead, so we do it
795 only every Nth instruction. We also do it if
796 ``things_to_do'' is set, i.e. when an asynchronous
797 event needs attention (e.g. a signal handler or
798 async I/O handler); see Py_AddPendingCall() and
799 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000800
Skip Montanarod581d772002-09-03 20:10:45 +0000801 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000802 if (*next_instr == SETUP_FINALLY) {
803 /* Make the last opcode before
804 a try: finally: block uninterruptable. */
805 goto fast_next_opcode;
806 }
Skip Montanarod581d772002-09-03 20:10:45 +0000807 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000808 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000809#ifdef WITH_TSC
810 ticked = 1;
811#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000812 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000813 if (Py_MakePendingCalls() < 0) {
814 why = WHY_EXCEPTION;
815 goto on_error;
816 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000817 if (things_to_do)
818 /* MakePendingCalls() didn't succeed.
819 Force early re-execution of this
820 "periodic" code, possibly after
821 a thread switch */
822 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000823 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000824#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000825 if (interpreter_lock) {
826 /* Give another thread a chance */
827
Guido van Rossum25ce5661997-08-02 03:10:38 +0000828 if (PyThreadState_Swap(NULL) != tstate)
829 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000830 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000831
832 /* Other threads may run now */
833
Guido van Rossum65d5b571998-12-21 19:32:43 +0000834 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000835 if (PyThreadState_Swap(tstate) != NULL)
836 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000837
838 /* Check for thread interrupts */
839
840 if (tstate->async_exc != NULL) {
841 x = tstate->async_exc;
842 tstate->async_exc = NULL;
843 PyErr_SetNone(x);
844 Py_DECREF(x);
845 why = WHY_EXCEPTION;
846 goto on_error;
847 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000848 }
849#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000850 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000851
Neil Schemenauer63543862002-02-17 19:10:14 +0000852 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000853 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000854
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000855 /* line-by-line tracing support */
856
857 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
858 /* see maybe_call_line_trace
859 for expository comments */
860 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000861
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000862 err = maybe_call_line_trace(tstate->c_tracefunc,
863 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000864 f, &instr_lb, &instr_ub,
865 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000866 /* Reload possibly changed frame fields */
867 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000868 if (f->f_stacktop != NULL) {
869 stack_pointer = f->f_stacktop;
870 f->f_stacktop = NULL;
871 }
872 if (err) {
873 /* trace function raised an exception */
874 goto on_error;
875 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000876 }
877
878 /* Extract opcode and argument */
879
Guido van Rossum374a9221991-04-04 10:40:29 +0000880 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000881 oparg = 0; /* allows oparg to be stored in a register because
882 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000883 if (HAS_ARG(opcode))
884 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000885 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000886#ifdef DYNAMIC_EXECUTION_PROFILE
887#ifdef DXPAIRS
888 dxpairs[lastopcode][opcode]++;
889 lastopcode = opcode;
890#endif
891 dxp[opcode]++;
892#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000893
Guido van Rossum96a42c81992-01-12 02:29:51 +0000894#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000895 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000896
Guido van Rossum96a42c81992-01-12 02:29:51 +0000897 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000898 if (HAS_ARG(opcode)) {
899 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000900 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000901 }
902 else {
903 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000904 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000905 }
906 }
907#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000908
Guido van Rossum374a9221991-04-04 10:40:29 +0000909 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000910 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000911
Guido van Rossum374a9221991-04-04 10:40:29 +0000912 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000913
Guido van Rossum374a9221991-04-04 10:40:29 +0000914 /* BEWARE!
915 It is essential that any operation that fails sets either
916 x to NULL, err to nonzero, or why to anything but WHY_NOT,
917 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000918
Guido van Rossum374a9221991-04-04 10:40:29 +0000919 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000920
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000921 case NOP:
922 goto fast_next_opcode;
923
Neil Schemenauer63543862002-02-17 19:10:14 +0000924 case LOAD_FAST:
925 x = GETLOCAL(oparg);
926 if (x != NULL) {
927 Py_INCREF(x);
928 PUSH(x);
929 goto fast_next_opcode;
930 }
931 format_exc_check_arg(PyExc_UnboundLocalError,
932 UNBOUNDLOCAL_ERROR_MSG,
933 PyTuple_GetItem(co->co_varnames, oparg));
934 break;
935
936 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000937 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000938 Py_INCREF(x);
939 PUSH(x);
940 goto fast_next_opcode;
941
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000942 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000943 case STORE_FAST:
944 v = POP();
945 SETLOCAL(oparg, v);
946 goto fast_next_opcode;
947
Raymond Hettingerf606f872003-03-16 03:11:04 +0000948 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000949 case POP_TOP:
950 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000951 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000952 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000953
Guido van Rossum374a9221991-04-04 10:40:29 +0000954 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000955 v = TOP();
956 w = SECOND();
957 SET_TOP(w);
958 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000959 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000960
Guido van Rossum374a9221991-04-04 10:40:29 +0000961 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000962 v = TOP();
963 w = SECOND();
964 x = THIRD();
965 SET_TOP(w);
966 SET_SECOND(x);
967 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000968 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000969
Thomas Wouters434d0822000-08-24 20:11:32 +0000970 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000971 u = TOP();
972 v = SECOND();
973 w = THIRD();
974 x = FOURTH();
975 SET_TOP(v);
976 SET_SECOND(w);
977 SET_THIRD(x);
978 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000979 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000980
Guido van Rossum374a9221991-04-04 10:40:29 +0000981 case DUP_TOP:
982 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000983 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000984 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000985 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000986
Thomas Wouters434d0822000-08-24 20:11:32 +0000987 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000988 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000989 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000990 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000991 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000992 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000993 STACKADJ(2);
994 SET_TOP(x);
995 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000996 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000997 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000998 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000999 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001000 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001001 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001003 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001004 STACKADJ(3);
1005 SET_TOP(x);
1006 SET_SECOND(w);
1007 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001008 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001009 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001010 Py_FatalError("invalid argument to DUP_TOPX"
1011 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001012 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001013
Guido van Rossum374a9221991-04-04 10:40:29 +00001014 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001016 x = PyNumber_Positive(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_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001023 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001024 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001025 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001026 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001027 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001028 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001029
Guido van Rossum374a9221991-04-04 10:40:29 +00001030 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001031 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001032 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001033 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001034 if (err == 0) {
1035 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001037 continue;
1038 }
1039 else if (err > 0) {
1040 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001041 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001042 err = 0;
1043 continue;
1044 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001045 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001046 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001047
Guido van Rossum7928cd71991-10-24 14:59:31 +00001048 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001049 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001050 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001051 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001052 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001053 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001054 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001055
Guido van Rossum50564e81996-01-12 01:13:16 +00001056 case BINARY_POWER:
1057 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001058 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001059 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001060 Py_DECREF(v);
1061 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001062 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001063 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001064 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001065
Guido van Rossum374a9221991-04-04 10:40:29 +00001066 case BINARY_MULTIPLY:
1067 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001068 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001069 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001070 Py_DECREF(v);
1071 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001072 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001073 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001074 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001075
Tim Peters3caca232001-12-06 06:23:26 +00001076 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001077 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001078 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001079 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001080 Py_DECREF(v);
1081 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001083 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001084 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001085
Guido van Rossum4668b002001-08-08 05:00:18 +00001086 case BINARY_FLOOR_DIVIDE:
1087 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001088 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001089 x = PyNumber_FloorDivide(v, w);
1090 Py_DECREF(v);
1091 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001092 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001093 if (x != NULL) continue;
1094 break;
1095
Guido van Rossum374a9221991-04-04 10:40:29 +00001096 case BINARY_MODULO:
1097 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001098 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001099 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001100 Py_DECREF(v);
1101 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001102 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001103 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001104 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001105
Guido van Rossum374a9221991-04-04 10:40:29 +00001106 case BINARY_ADD:
1107 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001108 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001109 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001110 /* INLINE: int + int */
1111 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001112 a = PyInt_AS_LONG(v);
1113 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001114 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001115 if ((i^a) < 0 && (i^b) < 0)
1116 goto slow_add;
1117 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001118 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001119 else if (PyString_CheckExact(v) &&
1120 PyString_CheckExact(w)) {
1121 x = string_concatenate(v, w, f, next_instr);
1122 /* string_concatenate consumed the ref to v */
1123 goto skip_decref_vx;
1124 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001125 else {
1126 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001127 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001128 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001129 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001130 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001131 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001132 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001133 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001134 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001135
Guido van Rossum374a9221991-04-04 10:40:29 +00001136 case BINARY_SUBTRACT:
1137 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001138 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001139 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001140 /* INLINE: int - int */
1141 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001142 a = PyInt_AS_LONG(v);
1143 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001144 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001145 if ((i^a) < 0 && (i^~b) < 0)
1146 goto slow_sub;
1147 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001148 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001149 else {
1150 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001151 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001152 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001153 Py_DECREF(v);
1154 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001155 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001156 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001157 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001158
Guido van Rossum374a9221991-04-04 10:40:29 +00001159 case BINARY_SUBSCR:
1160 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001161 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001162 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001163 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001164 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001165 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001166 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001167 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001168 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001169 Py_INCREF(x);
1170 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001171 else
1172 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001173 }
1174 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001175 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001176 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001177 Py_DECREF(v);
1178 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001179 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001180 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001181 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001182
Guido van Rossum7928cd71991-10-24 14:59:31 +00001183 case BINARY_LSHIFT:
1184 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001185 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001186 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001187 Py_DECREF(v);
1188 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001189 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001190 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001191 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001192
Guido van Rossum7928cd71991-10-24 14:59:31 +00001193 case BINARY_RSHIFT:
1194 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001195 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001196 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001197 Py_DECREF(v);
1198 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001199 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001200 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001201 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001202
Guido van Rossum7928cd71991-10-24 14:59:31 +00001203 case BINARY_AND:
1204 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001205 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001206 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001207 Py_DECREF(v);
1208 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001209 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001210 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001211 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001212
Guido van Rossum7928cd71991-10-24 14:59:31 +00001213 case BINARY_XOR:
1214 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001216 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001217 Py_DECREF(v);
1218 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001219 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001220 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001221 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001222
Guido van Rossum7928cd71991-10-24 14:59:31 +00001223 case BINARY_OR:
1224 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001225 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001226 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001227 Py_DECREF(v);
1228 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001229 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001230 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001231 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001232
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001233 case LIST_APPEND:
1234 w = POP();
1235 v = POP();
1236 err = PyList_Append(v, w);
1237 Py_DECREF(v);
1238 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001239 if (err == 0) {
1240 PREDICT(JUMP_ABSOLUTE);
1241 continue;
1242 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001243 break;
1244
Thomas Wouters434d0822000-08-24 20:11:32 +00001245 case INPLACE_POWER:
1246 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001247 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001248 x = PyNumber_InPlacePower(v, w, Py_None);
1249 Py_DECREF(v);
1250 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001251 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001252 if (x != NULL) continue;
1253 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001254
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 case INPLACE_MULTIPLY:
1256 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 x = PyNumber_InPlaceMultiply(v, w);
1259 Py_DECREF(v);
1260 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001262 if (x != NULL) continue;
1263 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Tim Peters54b11912001-12-25 18:49:11 +00001265 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001267 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001268 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001269 Py_DECREF(v);
1270 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001271 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001272 if (x != NULL) continue;
1273 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Guido van Rossum4668b002001-08-08 05:00:18 +00001275 case INPLACE_FLOOR_DIVIDE:
1276 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001277 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001278 x = PyNumber_InPlaceFloorDivide(v, w);
1279 Py_DECREF(v);
1280 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001281 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001282 if (x != NULL) continue;
1283 break;
1284
Thomas Wouters434d0822000-08-24 20:11:32 +00001285 case INPLACE_MODULO:
1286 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001287 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001288 x = PyNumber_InPlaceRemainder(v, w);
1289 Py_DECREF(v);
1290 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001291 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001292 if (x != NULL) continue;
1293 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001294
Thomas Wouters434d0822000-08-24 20:11:32 +00001295 case INPLACE_ADD:
1296 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001297 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001298 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 /* INLINE: int + int */
1300 register long a, b, i;
1301 a = PyInt_AS_LONG(v);
1302 b = PyInt_AS_LONG(w);
1303 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001304 if ((i^a) < 0 && (i^b) < 0)
1305 goto slow_iadd;
1306 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001307 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001308 else if (PyString_CheckExact(v) &&
1309 PyString_CheckExact(w)) {
1310 x = string_concatenate(v, w, f, next_instr);
1311 /* string_concatenate consumed the ref to v */
1312 goto skip_decref_v;
1313 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001314 else {
1315 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001317 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001318 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001319 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001320 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001322 if (x != NULL) continue;
1323 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001324
Thomas Wouters434d0822000-08-24 20:11:32 +00001325 case INPLACE_SUBTRACT:
1326 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001327 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001328 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 /* INLINE: int - int */
1330 register long a, b, i;
1331 a = PyInt_AS_LONG(v);
1332 b = PyInt_AS_LONG(w);
1333 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001334 if ((i^a) < 0 && (i^~b) < 0)
1335 goto slow_isub;
1336 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001337 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001338 else {
1339 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001341 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001342 Py_DECREF(v);
1343 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001345 if (x != NULL) continue;
1346 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 case INPLACE_LSHIFT:
1349 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 x = PyNumber_InPlaceLshift(v, w);
1352 Py_DECREF(v);
1353 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 if (x != NULL) continue;
1356 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001357
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 case INPLACE_RSHIFT:
1359 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 x = PyNumber_InPlaceRshift(v, w);
1362 Py_DECREF(v);
1363 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 if (x != NULL) continue;
1366 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 case INPLACE_AND:
1369 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 x = PyNumber_InPlaceAnd(v, w);
1372 Py_DECREF(v);
1373 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 if (x != NULL) continue;
1376 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 case INPLACE_XOR:
1379 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001380 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 x = PyNumber_InPlaceXor(v, w);
1382 Py_DECREF(v);
1383 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 if (x != NULL) continue;
1386 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001387
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 case INPLACE_OR:
1389 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 x = PyNumber_InPlaceOr(v, w);
1392 Py_DECREF(v);
1393 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001395 if (x != NULL) continue;
1396 break;
1397
Guido van Rossum374a9221991-04-04 10:40:29 +00001398 case SLICE+0:
1399 case SLICE+1:
1400 case SLICE+2:
1401 case SLICE+3:
1402 if ((opcode-SLICE) & 2)
1403 w = POP();
1404 else
1405 w = NULL;
1406 if ((opcode-SLICE) & 1)
1407 v = POP();
1408 else
1409 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001410 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001411 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001412 Py_DECREF(u);
1413 Py_XDECREF(v);
1414 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001415 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001416 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001418
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 case STORE_SLICE+0:
1420 case STORE_SLICE+1:
1421 case STORE_SLICE+2:
1422 case STORE_SLICE+3:
1423 if ((opcode-STORE_SLICE) & 2)
1424 w = POP();
1425 else
1426 w = NULL;
1427 if ((opcode-STORE_SLICE) & 1)
1428 v = POP();
1429 else
1430 v = NULL;
1431 u = POP();
1432 t = POP();
1433 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001434 Py_DECREF(t);
1435 Py_DECREF(u);
1436 Py_XDECREF(v);
1437 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001438 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001439 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Guido van Rossum374a9221991-04-04 10:40:29 +00001441 case DELETE_SLICE+0:
1442 case DELETE_SLICE+1:
1443 case DELETE_SLICE+2:
1444 case DELETE_SLICE+3:
1445 if ((opcode-DELETE_SLICE) & 2)
1446 w = POP();
1447 else
1448 w = NULL;
1449 if ((opcode-DELETE_SLICE) & 1)
1450 v = POP();
1451 else
1452 v = NULL;
1453 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001454 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001455 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001456 Py_DECREF(u);
1457 Py_XDECREF(v);
1458 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001459 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001460 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Guido van Rossum374a9221991-04-04 10:40:29 +00001462 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001463 w = TOP();
1464 v = SECOND();
1465 u = THIRD();
1466 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001467 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001468 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001469 Py_DECREF(u);
1470 Py_DECREF(v);
1471 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001472 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001476 w = TOP();
1477 v = SECOND();
1478 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001480 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001481 Py_DECREF(v);
1482 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001483 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001484 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001485
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 case PRINT_EXPR:
1487 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001488 w = PySys_GetObject("displayhook");
1489 if (w == NULL) {
1490 PyErr_SetString(PyExc_RuntimeError,
1491 "lost sys.displayhook");
1492 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001493 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001494 }
1495 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001496 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001497 if (x == NULL)
1498 err = -1;
1499 }
1500 if (err == 0) {
1501 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001502 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001503 if (w == NULL)
1504 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001505 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001506 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001507 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001509
Thomas Wouters434d0822000-08-24 20:11:32 +00001510#ifdef CASE_TOO_BIG
1511 default: switch (opcode) {
1512#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001513 case RAISE_VARARGS:
1514 u = v = w = NULL;
1515 switch (oparg) {
1516 case 3:
1517 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001518 /* Fallthrough */
1519 case 2:
1520 v = POP(); /* value */
1521 /* Fallthrough */
1522 case 1:
1523 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001524 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001525 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001526 break;
1527 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001528 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001529 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001530 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001531 break;
1532 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001533 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Guido van Rossum374a9221991-04-04 10:40:29 +00001535 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001536 if ((x = f->f_locals) != NULL) {
1537 Py_INCREF(x);
1538 PUSH(x);
1539 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001540 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001541 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001542 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001543
Guido van Rossum374a9221991-04-04 10:40:29 +00001544 case RETURN_VALUE:
1545 retval = POP();
1546 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001547 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001548
Tim Peters5ca576e2001-06-18 22:08:13 +00001549 case YIELD_VALUE:
1550 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001551 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001552 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001553 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001554
Guido van Rossum374a9221991-04-04 10:40:29 +00001555 case POP_BLOCK:
1556 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001557 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001558 while (STACK_LEVEL() > b->b_level) {
1559 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001560 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001561 }
1562 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001563 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001564
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 case END_FINALLY:
1566 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001567 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001568 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001569 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001570 if (why == WHY_RETURN ||
1571 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001572 retval = POP();
1573 }
Brett Cannonbf364092006-03-01 04:25:17 +00001574 else if (PyExceptionClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001575 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001576 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001577 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001578 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001579 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001580 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001581 else if (v != Py_None) {
1582 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001583 "'finally' pops bad exception");
1584 why = WHY_EXCEPTION;
1585 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001586 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001587 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Guido van Rossum374a9221991-04-04 10:40:29 +00001589 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001590 u = TOP();
1591 v = SECOND();
1592 w = THIRD();
1593 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001594 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001595 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001596 Py_DECREF(u);
1597 Py_DECREF(v);
1598 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001599 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001600
Guido van Rossum374a9221991-04-04 10:40:29 +00001601 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001602 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001603 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001604 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001605 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001606 err = PyDict_SetItem(x, w, v);
1607 else
1608 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001609 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001610 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001611 break;
1612 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001613 PyErr_Format(PyExc_SystemError,
1614 "no locals found when storing %s",
1615 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001616 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001619 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001620 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001621 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001622 format_exc_check_arg(PyExc_NameError,
1623 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001624 break;
1625 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001626 PyErr_Format(PyExc_SystemError,
1627 "no locals when deleting %s",
1628 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001630
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001631 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001632 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001633 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001634 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1635 PyObject **items = ((PyTupleObject *)v)->ob_item;
1636 while (oparg--) {
1637 w = items[oparg];
1638 Py_INCREF(w);
1639 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001640 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001641 Py_DECREF(v);
1642 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001643 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1644 PyObject **items = ((PyListObject *)v)->ob_item;
1645 while (oparg--) {
1646 w = items[oparg];
1647 Py_INCREF(w);
1648 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001649 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001650 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001651 stack_pointer + oparg))
1652 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001653 else {
1654 if (PyErr_ExceptionMatches(PyExc_TypeError))
1655 PyErr_SetString(PyExc_TypeError,
1656 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001657 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001658 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001659 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001660 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Guido van Rossum374a9221991-04-04 10:40:29 +00001662 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001663 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001664 v = TOP();
1665 u = SECOND();
1666 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001667 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1668 Py_DECREF(v);
1669 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001670 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001671 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Guido van Rossum374a9221991-04-04 10:40:29 +00001673 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001674 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001676 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1677 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001678 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001679 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001680
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001681 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001682 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001683 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001684 err = PyDict_SetItem(f->f_globals, w, v);
1685 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001686 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001687 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001689 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001690 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001691 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001692 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001693 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001694 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001695
Guido van Rossum374a9221991-04-04 10:40:29 +00001696 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001697 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001698 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001699 PyErr_Format(PyExc_SystemError,
1700 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001701 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001702 break;
1703 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001704 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001705 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001706 Py_XINCREF(x);
1707 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001708 else {
1709 x = PyObject_GetItem(v, w);
1710 if (x == NULL && PyErr_Occurred()) {
1711 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1712 break;
1713 PyErr_Clear();
1714 }
1715 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001717 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001719 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001721 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001722 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001723 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001724 break;
1725 }
1726 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001727 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001728 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001729 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001730 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001731
Guido van Rossum374a9221991-04-04 10:40:29 +00001732 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001733 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001734 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001735 /* Inline the PyDict_GetItem() calls.
1736 WARNING: this is an extreme speed hack.
1737 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001738 long hash = ((PyStringObject *)w)->ob_shash;
1739 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001740 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001741 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001742 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001743 e = d->ma_lookup(d, w, hash);
1744 if (e == NULL) {
1745 x = NULL;
1746 break;
1747 }
1748 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001749 if (x != NULL) {
1750 Py_INCREF(x);
1751 PUSH(x);
1752 continue;
1753 }
1754 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001755 e = d->ma_lookup(d, w, hash);
1756 if (e == NULL) {
1757 x = NULL;
1758 break;
1759 }
1760 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001761 if (x != NULL) {
1762 Py_INCREF(x);
1763 PUSH(x);
1764 continue;
1765 }
1766 goto load_global_error;
1767 }
1768 }
1769 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001770 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001771 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001772 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001774 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001775 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001776 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001777 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001778 break;
1779 }
1780 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001781 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001782 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001783 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001784
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001785 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001786 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001787 if (x != NULL) {
1788 SETLOCAL(oparg, NULL);
1789 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001790 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001791 format_exc_check_arg(
1792 PyExc_UnboundLocalError,
1793 UNBOUNDLOCAL_ERROR_MSG,
1794 PyTuple_GetItem(co->co_varnames, oparg)
1795 );
1796 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001798 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001799 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001800 Py_INCREF(x);
1801 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001802 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001803 break;
1804
1805 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001806 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001807 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001808 if (w != NULL) {
1809 PUSH(w);
1810 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001811 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001812 err = -1;
1813 /* Don't stomp existing exception */
1814 if (PyErr_Occurred())
1815 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001816 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1817 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001818 oparg);
1819 format_exc_check_arg(
1820 PyExc_UnboundLocalError,
1821 UNBOUNDLOCAL_ERROR_MSG,
1822 v);
1823 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001824 v = PyTuple_GET_ITEM(
Raymond Hettinger467a6982004-04-07 11:39:21 +00001825 co->co_freevars,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001826 oparg - PyTuple_GET_SIZE(co->co_cellvars));
Raymond Hettinger467a6982004-04-07 11:39:21 +00001827 format_exc_check_arg(
1828 PyExc_NameError,
1829 UNBOUNDFREE_ERROR_MSG,
1830 v);
1831 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001832 break;
1833
1834 case STORE_DEREF:
1835 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001836 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001837 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001838 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001839 continue;
1840
Guido van Rossum374a9221991-04-04 10:40:29 +00001841 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001842 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001843 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001844 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001845 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001846 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001847 }
1848 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001849 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 }
1851 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001852
Guido van Rossum374a9221991-04-04 10:40:29 +00001853 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001854 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001855 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001856 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001858 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001859 }
1860 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001861 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 }
1863 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001864
Guido van Rossum86e58e22006-08-28 15:27:34 +00001865 case BUILD_SET:
1866 x = PySet_New(NULL);
1867 if (x != NULL) {
1868 for (; --oparg >= 0;) {
1869 w = POP();
1870 if (err == 0)
1871 err = PySet_Add(x, w);
1872 Py_DECREF(w);
1873 }
1874 if (err != 0) {
1875 Py_DECREF(x);
1876 break;
1877 }
1878 PUSH(x);
1879 continue;
1880 }
1881 break;
1882
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001884 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001886 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001888
Guido van Rossum374a9221991-04-04 10:40:29 +00001889 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001890 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001891 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001892 x = PyObject_GetAttr(v, w);
1893 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001894 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001895 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001896 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001897
Guido van Rossum374a9221991-04-04 10:40:29 +00001898 case COMPARE_OP:
1899 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001900 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001901 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001902 /* INLINE: cmp(int, int) */
1903 register long a, b;
1904 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001905 a = PyInt_AS_LONG(v);
1906 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001907 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001908 case PyCmp_LT: res = a < b; break;
1909 case PyCmp_LE: res = a <= b; break;
1910 case PyCmp_EQ: res = a == b; break;
1911 case PyCmp_NE: res = a != b; break;
1912 case PyCmp_GT: res = a > b; break;
1913 case PyCmp_GE: res = a >= b; break;
1914 case PyCmp_IS: res = v == w; break;
1915 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001916 default: goto slow_compare;
1917 }
1918 x = res ? Py_True : Py_False;
1919 Py_INCREF(x);
1920 }
1921 else {
1922 slow_compare:
1923 x = cmp_outcome(oparg, v, w);
1924 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001925 Py_DECREF(v);
1926 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001927 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001928 if (x == NULL) break;
1929 PREDICT(JUMP_IF_FALSE);
1930 PREDICT(JUMP_IF_TRUE);
1931 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001932
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001934 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001935 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001936 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001937 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001938 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001939 break;
1940 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001941 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001942 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001943 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
1944 w = PyTuple_Pack(5,
1945 w,
1946 f->f_globals,
1947 f->f_locals == NULL ?
1948 Py_None : f->f_locals,
1949 v,
1950 u);
1951 else
1952 w = PyTuple_Pack(4,
1953 w,
1954 f->f_globals,
1955 f->f_locals == NULL ?
1956 Py_None : f->f_locals,
1957 v);
1958 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001960 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001961 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001962 x = NULL;
1963 break;
1964 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001965 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00001966 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001967 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001968 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001969 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001970 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001971 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001972
Thomas Wouters52152252000-08-17 22:55:00 +00001973 case IMPORT_STAR:
1974 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001975 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001976 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001977 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001978 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001979 break;
1980 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001981 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00001982 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001983 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00001985 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001986 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001987 break;
Guido van Rossum25831651993-05-19 14:50:45 +00001988
Thomas Wouters52152252000-08-17 22:55:00 +00001989 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00001990 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00001991 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001992 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00001993 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001994 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00001995 PUSH(x);
1996 if (x != NULL) continue;
1997 break;
1998
Guido van Rossum374a9221991-04-04 10:40:29 +00001999 case JUMP_FORWARD:
2000 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002001 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002002
Raymond Hettingerf606f872003-03-16 03:11:04 +00002003 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002005 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002006 if (w == Py_True) {
2007 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002008 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002009 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002010 if (w == Py_False) {
2011 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002012 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002013 }
2014 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002015 if (err > 0)
2016 err = 0;
2017 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002018 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002019 else
2020 break;
2021 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Raymond Hettingerf606f872003-03-16 03:11:04 +00002023 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002024 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002025 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002026 if (w == Py_False) {
2027 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002028 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002029 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002030 if (w == Py_True) {
2031 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002032 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002033 }
2034 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002035 if (err > 0) {
2036 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002037 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002038 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002039 else if (err == 0)
2040 ;
2041 else
2042 break;
2043 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002044
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002045 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002046 case JUMP_ABSOLUTE:
2047 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002048 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002049
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002050 case GET_ITER:
2051 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002052 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002053 x = PyObject_GetIter(v);
2054 Py_DECREF(v);
2055 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002056 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002057 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002058 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002059 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002060 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002061 break;
2062
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002063 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002064 case FOR_ITER:
2065 /* before: [iter]; after: [iter, iter()] *or* [] */
2066 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002067 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002068 if (x != NULL) {
2069 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002070 PREDICT(STORE_FAST);
2071 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002072 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002073 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002074 if (PyErr_Occurred()) {
2075 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2076 break;
2077 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002078 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002079 /* iterator ended normally */
2080 x = v = POP();
2081 Py_DECREF(v);
2082 JUMPBY(oparg);
2083 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002085 case BREAK_LOOP:
2086 why = WHY_BREAK;
2087 goto fast_block_end;
2088
2089 case CONTINUE_LOOP:
2090 retval = PyInt_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002091 if (!retval) {
2092 x = NULL;
2093 break;
2094 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002095 why = WHY_CONTINUE;
2096 goto fast_block_end;
2097
Guido van Rossum374a9221991-04-04 10:40:29 +00002098 case SETUP_LOOP:
2099 case SETUP_EXCEPT:
2100 case SETUP_FINALLY:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00002101 /* NOTE: If you add any new block-setup opcodes that are
2102 not try/except/finally handlers, you may need to
2103 update the PyGen_NeedsFinalizing() function. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002104
Guido van Rossumb209a111997-04-29 18:18:01 +00002105 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002106 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002107 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002108
Guido van Rossumc2e20742006-02-27 22:32:47 +00002109 case WITH_CLEANUP:
2110 {
2111 /* TOP is the context.__exit__ bound method.
2112 Below that are 1-3 values indicating how/why
2113 we entered the finally clause:
2114 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002115 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002116 - SECOND = WHY_*; no retval below it
2117 - (SECOND, THIRD, FOURTH) = exc_info()
2118 In the last case, we must call
2119 TOP(SECOND, THIRD, FOURTH)
2120 otherwise we must call
2121 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002122
2123 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002124 *and* the function call returns a 'true' value, we
2125 "zap" this information, to prevent END_FINALLY from
2126 re-raising the exception. (But non-local gotos
2127 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002128 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002129
Guido van Rossumc2e20742006-02-27 22:32:47 +00002130 x = TOP();
2131 u = SECOND();
2132 if (PyInt_Check(u) || u == Py_None) {
2133 u = v = w = Py_None;
2134 }
2135 else {
2136 v = THIRD();
2137 w = FOURTH();
2138 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002139 /* XXX Not the fastest way to call it... */
2140 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2141 if (x == NULL)
2142 break; /* Go to error exit */
2143 if (u != Py_None && PyObject_IsTrue(x)) {
2144 /* There was an exception and a true return */
2145 Py_DECREF(x);
2146 x = TOP(); /* Again */
2147 STACKADJ(-3);
2148 Py_INCREF(Py_None);
2149 SET_TOP(Py_None);
2150 Py_DECREF(x);
2151 Py_DECREF(u);
2152 Py_DECREF(v);
2153 Py_DECREF(w);
2154 } else {
2155 /* Let END_FINALLY do its thing */
2156 Py_DECREF(x);
2157 x = POP();
2158 Py_DECREF(x);
2159 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002160 break;
2161 }
2162
Guido van Rossumf10570b1995-07-07 22:53:21 +00002163 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002164 {
2165 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002166 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002167 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002168#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002169 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002170#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002171 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002172#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002173 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002174 PUSH(x);
2175 if (x != NULL)
2176 continue;
2177 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002178 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002179
Jeremy Hylton76901512000-03-28 23:49:17 +00002180 case CALL_FUNCTION_VAR:
2181 case CALL_FUNCTION_KW:
2182 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002183 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002184 int na = oparg & 0xff;
2185 int nk = (oparg>>8) & 0xff;
2186 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002187 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002188 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002189 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002190 if (flags & CALL_FLAG_VAR)
2191 n++;
2192 if (flags & CALL_FLAG_KW)
2193 n++;
2194 pfunc = stack_pointer - n - 1;
2195 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002196
Guido van Rossumac7be682001-01-17 15:42:30 +00002197 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002198 && PyMethod_GET_SELF(func) != NULL) {
2199 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002200 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002201 func = PyMethod_GET_FUNCTION(func);
2202 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002203 Py_DECREF(*pfunc);
2204 *pfunc = self;
2205 na++;
2206 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002207 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002208 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002209 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002210 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002211 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002212 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002213 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002214 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002215
Jeremy Hylton76901512000-03-28 23:49:17 +00002216 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002217 w = POP();
2218 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002219 }
2220 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002221 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002222 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002223 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002224 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002225
Guido van Rossum681d79a1995-07-18 14:51:37 +00002226 case MAKE_FUNCTION:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002227 {
2228 int posdefaults = oparg & 0xff;
2229 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002230 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002231
Guido van Rossum681d79a1995-07-18 14:51:37 +00002232 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002233 x = PyFunction_New(v, f->f_globals);
2234 Py_DECREF(v);
Neal Norwitzc1505362006-12-28 06:47:50 +00002235
2236 if (x != NULL && num_annotations > 0) {
2237 Py_ssize_t name_ix;
2238 u = POP(); /* names of args with annotations */
2239 v = PyDict_New();
2240 if (v == NULL) {
2241 Py_DECREF(x);
2242 x = NULL;
2243 break;
2244 }
2245 name_ix = PyTuple_Size(u);
2246 assert(num_annotations == name_ix+1);
2247 while (name_ix > 0) {
2248 --name_ix;
2249 t = PyTuple_GET_ITEM(u, name_ix);
2250 w = POP();
2251 /* XXX(nnorwitz): check for errors */
2252 PyDict_SetItem(v, t, w);
2253 Py_DECREF(w);
2254 }
2255
2256 err = PyFunction_SetAnnotations(x, v);
2257 Py_DECREF(v);
2258 Py_DECREF(u);
2259 }
2260
Guido van Rossum681d79a1995-07-18 14:51:37 +00002261 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002262 if (x != NULL && posdefaults > 0) {
2263 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002264 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002265 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002266 x = NULL;
2267 break;
2268 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002269 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002270 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002271 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002272 }
2273 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002274 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002275 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002276 if (x != NULL && kwdefaults > 0) {
2277 v = PyDict_New();
2278 if (v == NULL) {
2279 Py_DECREF(x);
2280 x = NULL;
2281 break;
2282 }
2283 while (--kwdefaults >= 0) {
2284 w = POP(); /* default value */
2285 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002286 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002287 PyDict_SetItem(v, u, w);
2288 }
2289 err = PyFunction_SetKwDefaults(x, v);
2290 Py_DECREF(v);
2291 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002292 PUSH(x);
2293 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002294 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002295
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002296 case MAKE_CLOSURE:
2297 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002298 v = POP(); /* code object */
2299 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002300 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 if (x != NULL) {
2302 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002303 err = PyFunction_SetClosure(x, v);
2304 Py_DECREF(v);
2305 }
2306 if (x != NULL && oparg > 0) {
2307 v = PyTuple_New(oparg);
2308 if (v == NULL) {
2309 Py_DECREF(x);
2310 x = NULL;
2311 break;
2312 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002313 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002314 w = POP();
2315 PyTuple_SET_ITEM(v, oparg, w);
2316 }
2317 err = PyFunction_SetDefaults(x, v);
2318 Py_DECREF(v);
2319 }
2320 PUSH(x);
2321 break;
2322 }
2323
Guido van Rossum8861b741996-07-30 16:49:37 +00002324 case BUILD_SLICE:
2325 if (oparg == 3)
2326 w = POP();
2327 else
2328 w = NULL;
2329 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002330 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002331 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002332 Py_DECREF(u);
2333 Py_DECREF(v);
2334 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002335 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002336 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002337 break;
2338
Fred Drakeef8ace32000-08-24 00:32:09 +00002339 case EXTENDED_ARG:
2340 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002341 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002342 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002343
Guido van Rossum374a9221991-04-04 10:40:29 +00002344 default:
2345 fprintf(stderr,
2346 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002347 PyCode_Addr2Line(f->f_code, f->f_lasti),
2348 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002349 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002350 why = WHY_EXCEPTION;
2351 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002352
2353#ifdef CASE_TOO_BIG
2354 }
2355#endif
2356
Guido van Rossum374a9221991-04-04 10:40:29 +00002357 } /* switch */
2358
2359 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002360
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002361 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002362
Guido van Rossum374a9221991-04-04 10:40:29 +00002363 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002364
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002366 if (err == 0 && x != NULL) {
2367#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002368 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002369 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002370 fprintf(stderr,
2371 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002372 else {
2373#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002374 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002375 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002376#ifdef CHECKEXC
2377 }
2378#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002379 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002381 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002382 err = 0;
2383 }
2384
Guido van Rossum374a9221991-04-04 10:40:29 +00002385 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002386
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002387 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002388 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002389 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002390 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002391 why = WHY_EXCEPTION;
2392 }
2393 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002394#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002395 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002396 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002397 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002398 char buf[1024];
2399 sprintf(buf, "Stack unwind with exception "
2400 "set and why=%d", why);
2401 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002402 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002403 }
2404#endif
2405
2406 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002407
Guido van Rossum374a9221991-04-04 10:40:29 +00002408 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002409 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002410
Fred Drake8f51f542001-10-04 14:48:42 +00002411 if (tstate->c_tracefunc != NULL)
2412 call_exc_trace(tstate->c_tracefunc,
2413 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002414 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002415
Guido van Rossum374a9221991-04-04 10:40:29 +00002416 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002417
Guido van Rossum374a9221991-04-04 10:40:29 +00002418 if (why == WHY_RERAISE)
2419 why = WHY_EXCEPTION;
2420
2421 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002422
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002423fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002424 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002425 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002426
Tim Peters8a5c3c72004-04-05 19:36:21 +00002427 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002428 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2429 /* For a continue inside a try block,
2430 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002431 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2432 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002433 why = WHY_NOT;
2434 JUMPTO(PyInt_AS_LONG(retval));
2435 Py_DECREF(retval);
2436 break;
2437 }
2438
Guido van Rossum374a9221991-04-04 10:40:29 +00002439 while (STACK_LEVEL() > b->b_level) {
2440 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002441 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002442 }
2443 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2444 why = WHY_NOT;
2445 JUMPTO(b->b_handler);
2446 break;
2447 }
2448 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002449 (b->b_type == SETUP_EXCEPT &&
2450 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002451 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002452 PyObject *exc, *val, *tb;
2453 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002454 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002455 val = Py_None;
2456 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002457 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002458 /* Make the raw exception data
2459 available to the handler,
2460 so a program can emulate the
2461 Python main loop. Don't do
2462 this for 'finally'. */
2463 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002464 PyErr_NormalizeException(
2465 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002466 set_exc_info(tstate,
2467 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002468 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002469 if (tb == NULL) {
2470 Py_INCREF(Py_None);
2471 PUSH(Py_None);
2472 } else
2473 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002474 PUSH(val);
2475 PUSH(exc);
2476 }
2477 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002478 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002479 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002480 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002481 PUSH(v);
2482 }
2483 why = WHY_NOT;
2484 JUMPTO(b->b_handler);
2485 break;
2486 }
2487 } /* unwind stack */
2488
2489 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002490
Guido van Rossum374a9221991-04-04 10:40:29 +00002491 if (why != WHY_NOT)
2492 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002493 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002494
Guido van Rossum374a9221991-04-04 10:40:29 +00002495 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002496
Tim Peters8a5c3c72004-04-05 19:36:21 +00002497 assert(why != WHY_YIELD);
2498 /* Pop remaining stack entries. */
2499 while (!EMPTY()) {
2500 v = POP();
2501 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002502 }
2503
Tim Peters8a5c3c72004-04-05 19:36:21 +00002504 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002505 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002506
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002507fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002508 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002509 if (tstate->c_tracefunc) {
2510 if (why == WHY_RETURN || why == WHY_YIELD) {
2511 if (call_trace(tstate->c_tracefunc,
2512 tstate->c_traceobj, f,
2513 PyTrace_RETURN, retval)) {
2514 Py_XDECREF(retval);
2515 retval = NULL;
2516 why = WHY_EXCEPTION;
2517 }
2518 }
2519 else if (why == WHY_EXCEPTION) {
2520 call_trace_protected(tstate->c_tracefunc,
2521 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002522 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002523 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002524 }
Fred Drake8f51f542001-10-04 14:48:42 +00002525 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002526 if (why == WHY_EXCEPTION)
2527 call_trace_protected(tstate->c_profilefunc,
2528 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002529 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002530 else if (call_trace(tstate->c_profilefunc,
2531 tstate->c_profileobj, f,
2532 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002533 Py_XDECREF(retval);
2534 retval = NULL;
2535 why = WHY_EXCEPTION;
2536 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002537 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002538 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002539
Thomas Wouters477c8d52006-05-27 19:21:47 +00002540 if (tstate->frame->f_exc_type != NULL)
2541 reset_exc_info(tstate);
2542 else {
2543 assert(tstate->frame->f_exc_value == NULL);
2544 assert(tstate->frame->f_exc_traceback == NULL);
2545 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002546
Tim Peters5ca576e2001-06-18 22:08:13 +00002547 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002548 exit_eval_frame:
2549 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002550 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002551
Guido van Rossum96a42c81992-01-12 02:29:51 +00002552 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002553}
2554
Guido van Rossumc2e20742006-02-27 22:32:47 +00002555/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002556 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002557 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002558
Tim Peters6d6c1a32001-08-02 04:15:00 +00002559PyObject *
2560PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002561 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002562 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002563{
2564 register PyFrameObject *f;
2565 register PyObject *retval = NULL;
2566 register PyObject **fastlocals, **freevars;
2567 PyThreadState *tstate = PyThreadState_GET();
2568 PyObject *x, *u;
2569
2570 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002571 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002572 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002573 return NULL;
2574 }
2575
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002576 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002577 assert(globals != NULL);
2578 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002579 if (f == NULL)
2580 return NULL;
2581
2582 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002583 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002584
2585 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002586 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002587 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2588 int i;
2589 int n = argcount;
2590 PyObject *kwdict = NULL;
2591 if (co->co_flags & CO_VARKEYWORDS) {
2592 kwdict = PyDict_New();
2593 if (kwdict == NULL)
2594 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002595 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002596 if (co->co_flags & CO_VARARGS)
2597 i++;
2598 SETLOCAL(i, kwdict);
2599 }
2600 if (argcount > co->co_argcount) {
2601 if (!(co->co_flags & CO_VARARGS)) {
2602 PyErr_Format(PyExc_TypeError,
2603 "%.200s() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002604 "%spositional argument%s (%d given)",
Tim Peters5ca576e2001-06-18 22:08:13 +00002605 PyString_AsString(co->co_name),
2606 defcount ? "at most" : "exactly",
2607 co->co_argcount,
2608 kwcount ? "non-keyword " : "",
2609 co->co_argcount == 1 ? "" : "s",
2610 argcount);
2611 goto fail;
2612 }
2613 n = co->co_argcount;
2614 }
2615 for (i = 0; i < n; i++) {
2616 x = args[i];
2617 Py_INCREF(x);
2618 SETLOCAL(i, x);
2619 }
2620 if (co->co_flags & CO_VARARGS) {
2621 u = PyTuple_New(argcount - n);
2622 if (u == NULL)
2623 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002624 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002625 for (i = n; i < argcount; i++) {
2626 x = args[i];
2627 Py_INCREF(x);
2628 PyTuple_SET_ITEM(u, i-n, x);
2629 }
2630 }
2631 for (i = 0; i < kwcount; i++) {
2632 PyObject *keyword = kws[2*i];
2633 PyObject *value = kws[2*i + 1];
2634 int j;
2635 if (keyword == NULL || !PyString_Check(keyword)) {
2636 PyErr_Format(PyExc_TypeError,
2637 "%.200s() keywords must be strings",
2638 PyString_AsString(co->co_name));
2639 goto fail;
2640 }
2641 /* XXX slow -- speed up using dictionary? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002642 for (j = 0;
2643 j < co->co_argcount + co->co_kwonlyargcount;
2644 j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002645 PyObject *nm = PyTuple_GET_ITEM(
2646 co->co_varnames, j);
2647 int cmp = PyObject_RichCompareBool(
2648 keyword, nm, Py_EQ);
2649 if (cmp > 0)
2650 break;
2651 else if (cmp < 0)
2652 goto fail;
2653 }
2654 /* Check errors from Compare */
2655 if (PyErr_Occurred())
2656 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002657 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002658 if (kwdict == NULL) {
2659 PyErr_Format(PyExc_TypeError,
2660 "%.200s() got an unexpected "
2661 "keyword argument '%.400s'",
2662 PyString_AsString(co->co_name),
2663 PyString_AsString(keyword));
2664 goto fail;
2665 }
2666 PyDict_SetItem(kwdict, keyword, value);
2667 }
2668 else {
2669 if (GETLOCAL(j) != NULL) {
2670 PyErr_Format(PyExc_TypeError,
2671 "%.200s() got multiple "
2672 "values for keyword "
2673 "argument '%.400s'",
2674 PyString_AsString(co->co_name),
2675 PyString_AsString(keyword));
2676 goto fail;
2677 }
2678 Py_INCREF(value);
2679 SETLOCAL(j, value);
2680 }
2681 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002682 if (co->co_kwonlyargcount > 0) {
2683 for (i = co->co_argcount;
2684 i < co->co_argcount + co->co_kwonlyargcount;
2685 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00002686 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002687 if (GETLOCAL(i) != NULL)
2688 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00002689 name = PyTuple_GET_ITEM(co->co_varnames, i);
2690 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002691 if (kwdefs != NULL)
2692 def = PyDict_GetItem(kwdefs, name);
2693 if (def != NULL) {
2694 Py_INCREF(def);
2695 SETLOCAL(i, def);
2696 continue;
2697 }
2698 PyErr_Format(PyExc_TypeError,
2699 "%.200s() needs "
Brett Cannone33a6112007-01-29 23:43:38 +00002700 "keyword-only argument %s",
Guido van Rossum4f72a782006-10-27 23:31:49 +00002701 PyString_AsString(co->co_name),
2702 PyString_AsString(name));
2703 goto fail;
2704 }
2705 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002706 if (argcount < co->co_argcount) {
2707 int m = co->co_argcount - defcount;
2708 for (i = argcount; i < m; i++) {
2709 if (GETLOCAL(i) == NULL) {
2710 PyErr_Format(PyExc_TypeError,
2711 "%.200s() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002712 "%spositional argument%s "
2713 "(%d given)",
Tim Peters5ca576e2001-06-18 22:08:13 +00002714 PyString_AsString(co->co_name),
2715 ((co->co_flags & CO_VARARGS) ||
2716 defcount) ? "at least"
2717 : "exactly",
2718 m, kwcount ? "non-keyword " : "",
2719 m == 1 ? "" : "s", i);
2720 goto fail;
2721 }
2722 }
2723 if (n > m)
2724 i = n - m;
2725 else
2726 i = 0;
2727 for (; i < defcount; i++) {
2728 if (GETLOCAL(m+i) == NULL) {
2729 PyObject *def = defs[i];
2730 Py_INCREF(def);
2731 SETLOCAL(m+i, def);
2732 }
2733 }
2734 }
2735 }
2736 else {
2737 if (argcount > 0 || kwcount > 0) {
2738 PyErr_Format(PyExc_TypeError,
2739 "%.200s() takes no arguments (%d given)",
2740 PyString_AsString(co->co_name),
2741 argcount + kwcount);
2742 goto fail;
2743 }
2744 }
2745 /* Allocate and initialize storage for cell vars, and copy free
2746 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002747 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002748 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002749 char *cellname, *argname;
2750 PyObject *c;
2751
2752 nargs = co->co_argcount;
2753 if (co->co_flags & CO_VARARGS)
2754 nargs++;
2755 if (co->co_flags & CO_VARKEYWORDS)
2756 nargs++;
2757
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 /* Initialize each cell var, taking into account
2759 cell vars that are initialized from arguments.
2760
2761 Should arrange for the compiler to put cellvars
2762 that are arguments at the beginning of the cellvars
2763 list so that we can march over it more efficiently?
2764 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002765 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002766 cellname = PyString_AS_STRING(
2767 PyTuple_GET_ITEM(co->co_cellvars, i));
2768 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002770 argname = PyString_AS_STRING(
2771 PyTuple_GET_ITEM(co->co_varnames, j));
2772 if (strcmp(cellname, argname) == 0) {
2773 c = PyCell_New(GETLOCAL(j));
2774 if (c == NULL)
2775 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002776 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002777 found = 1;
2778 break;
2779 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002780 }
2781 if (found == 0) {
2782 c = PyCell_New(NULL);
2783 if (c == NULL)
2784 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002785 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002786 }
2787 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002788 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002789 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002790 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002791 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002792 PyObject *o = PyTuple_GET_ITEM(closure, i);
2793 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002794 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002795 }
2796 }
2797
Tim Peters5ca576e2001-06-18 22:08:13 +00002798 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002799 /* Don't need to keep the reference to f_back, it will be set
2800 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002801 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002802 f->f_back = NULL;
2803
Jeremy Hylton985eba52003-02-05 23:13:00 +00002804 PCALL(PCALL_GENERATOR);
2805
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002806 /* Create a new generator that owns the ready to run frame
2807 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002808 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002809 }
2810
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002811 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002812
2813 fail: /* Jump here from prelude on failure */
2814
Tim Petersb13680b2001-11-27 23:29:29 +00002815 /* decref'ing the frame can cause __del__ methods to get invoked,
2816 which can call back into Python. While we're done with the
2817 current Python frame (f), the associated C stack is still in use,
2818 so recursion_depth must be boosted for the duration.
2819 */
2820 assert(tstate != NULL);
2821 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002822 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002823 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002824 return retval;
2825}
2826
2827
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002828/* Implementation notes for set_exc_info() and reset_exc_info():
2829
2830- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2831 'exc_traceback'. These always travel together.
2832
2833- tstate->curexc_ZZZ is the "hot" exception that is set by
2834 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2835
2836- Once an exception is caught by an except clause, it is transferred
2837 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2838 can pick it up. This is the primary task of set_exc_info().
Thomas Wouters477c8d52006-05-27 19:21:47 +00002839 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002840
2841- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2842
2843 Long ago, when none of this existed, there were just a few globals:
2844 one set corresponding to the "hot" exception, and one set
2845 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2846 globals; they were simply stored as sys.exc_ZZZ. For backwards
2847 compatibility, they still are!) The problem was that in code like
2848 this:
2849
2850 try:
2851 "something that may fail"
2852 except "some exception":
2853 "do something else first"
2854 "print the exception from sys.exc_ZZZ."
2855
2856 if "do something else first" invoked something that raised and caught
2857 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2858 cause of subtle bugs. I fixed this by changing the semantics as
2859 follows:
2860
2861 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2862 *in that frame*.
2863
2864 - But initially, and as long as no exception is caught in a given
2865 frame, sys.exc_ZZZ will hold the last exception caught in the
2866 previous frame (or the frame before that, etc.).
2867
2868 The first bullet fixed the bug in the above example. The second
2869 bullet was for backwards compatibility: it was (and is) common to
2870 have a function that is called when an exception is caught, and to
2871 have that function access the caught exception via sys.exc_ZZZ.
2872 (Example: traceback.print_exc()).
2873
2874 At the same time I fixed the problem that sys.exc_ZZZ weren't
2875 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2876 but that's really a separate improvement.
2877
2878 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2879 variables to what they were before the current frame was called. The
2880 set_exc_info() function saves them on the frame so that
2881 reset_exc_info() can restore them. The invariant is that
2882 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2883 exception (where "catching" an exception applies only to successful
2884 except clauses); and if the current frame ever caught an exception,
2885 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2886 at the start of the current frame.
2887
2888*/
2889
Guido van Rossuma027efa1997-05-05 20:56:21 +00002890static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002891set_exc_info(PyThreadState *tstate,
2892 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002893{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002894 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002895 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002896
Thomas Wouters477c8d52006-05-27 19:21:47 +00002897 assert(type != NULL);
2898 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002899 if (frame->f_exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002900 assert(frame->f_exc_value == NULL);
2901 assert(frame->f_exc_traceback == NULL);
2902 /* This frame didn't catch an exception before. */
2903 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002904 if (tstate->exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002905 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002906 Py_INCREF(Py_None);
2907 tstate->exc_type = Py_None;
2908 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002909 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002910 Py_XINCREF(tstate->exc_value);
2911 Py_XINCREF(tstate->exc_traceback);
2912 frame->f_exc_type = tstate->exc_type;
2913 frame->f_exc_value = tstate->exc_value;
2914 frame->f_exc_traceback = tstate->exc_traceback;
2915 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002916 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002917 tmp_type = tstate->exc_type;
2918 tmp_value = tstate->exc_value;
2919 tmp_tb = tstate->exc_traceback;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002920 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002921 Py_XINCREF(value);
2922 Py_XINCREF(tb);
2923 tstate->exc_type = type;
2924 tstate->exc_value = value;
2925 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002926 Py_XDECREF(tmp_type);
2927 Py_XDECREF(tmp_value);
2928 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002929 /* For b/w compatibility */
2930 PySys_SetObject("exc_type", type);
2931 PySys_SetObject("exc_value", value);
2932 PySys_SetObject("exc_traceback", tb);
2933}
2934
2935static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002936reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002937{
2938 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002939 PyObject *tmp_type, *tmp_value, *tmp_tb;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002940
2941 /* It's a precondition that the thread state's frame caught an
2942 * exception -- verify in a debug build.
2943 */
2944 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002945 frame = tstate->frame;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002946 assert(frame != NULL);
2947 assert(frame->f_exc_type != NULL);
2948
2949 /* Copy the frame's exception info back to the thread state. */
2950 tmp_type = tstate->exc_type;
2951 tmp_value = tstate->exc_value;
2952 tmp_tb = tstate->exc_traceback;
2953 Py_INCREF(frame->f_exc_type);
2954 Py_XINCREF(frame->f_exc_value);
2955 Py_XINCREF(frame->f_exc_traceback);
2956 tstate->exc_type = frame->f_exc_type;
2957 tstate->exc_value = frame->f_exc_value;
2958 tstate->exc_traceback = frame->f_exc_traceback;
2959 Py_XDECREF(tmp_type);
2960 Py_XDECREF(tmp_value);
2961 Py_XDECREF(tmp_tb);
2962
2963 /* For b/w compatibility */
2964 PySys_SetObject("exc_type", frame->f_exc_type);
2965 PySys_SetObject("exc_value", frame->f_exc_value);
2966 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2967
2968 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002969 tmp_type = frame->f_exc_type;
2970 tmp_value = frame->f_exc_value;
2971 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002972 frame->f_exc_type = NULL;
2973 frame->f_exc_value = NULL;
2974 frame->f_exc_traceback = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002975 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002976 Py_XDECREF(tmp_value);
2977 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002978}
2979
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002980/* Logic for the raise statement (too complicated for inlining).
2981 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002982static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002983do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002984{
Guido van Rossumd295f121998-04-09 21:39:57 +00002985 if (type == NULL) {
2986 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002987 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002988 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2989 value = tstate->exc_value;
2990 tb = tstate->exc_traceback;
2991 Py_XINCREF(type);
2992 Py_XINCREF(value);
2993 Py_XINCREF(tb);
2994 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002995
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002996 /* We support the following forms of raise:
2997 raise <class>, <classinstance>
2998 raise <class>, <argument tuple>
2999 raise <class>, None
3000 raise <class>, <argument>
3001 raise <classinstance>, None
3002 raise <string>, <object>
3003 raise <string>, None
3004
3005 An omitted second argument is the same as None.
3006
3007 In addition, raise <tuple>, <anything> is the same as
3008 raising the tuple's first item (and it better have one!);
3009 this rule is applied recursively.
3010
3011 Finally, an optional third argument can be supplied, which
3012 gives the traceback to be substituted (useful when
3013 re-raising an exception after examining it). */
3014
3015 /* First, check the traceback argument, replacing None with
3016 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003017 if (tb == Py_None) {
3018 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003019 tb = NULL;
3020 }
3021 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003022 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003023 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003024 goto raise_error;
3025 }
3026
3027 /* Next, replace a missing value with None */
3028 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003029 value = Py_None;
3030 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003031 }
3032
3033 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003034 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3035 PyObject *tmp = type;
3036 type = PyTuple_GET_ITEM(type, 0);
3037 Py_INCREF(type);
3038 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003039 }
3040
Guido van Rossum45aecf42006-03-15 04:58:47 +00003041 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003042 PyErr_NormalizeException(&type, &value, &tb);
3043
Brett Cannonbf364092006-03-01 04:25:17 +00003044 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003045 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003046 if (value != Py_None) {
3047 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003048 "instance exception may not have a separate value");
3049 goto raise_error;
3050 }
3051 else {
3052 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003053 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003054 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003055 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003056 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003057 }
3058 }
3059 else {
3060 /* Not something you can raise. You get an exception
3061 anyway, just not what you specified :-) */
Guido van Rossum45aecf42006-03-15 04:58:47 +00003062 PyErr_SetString(PyExc_TypeError,
3063 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003064 goto raise_error;
3065 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003066 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003067 if (tb == NULL)
3068 return WHY_EXCEPTION;
3069 else
3070 return WHY_RERAISE;
3071 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003072 Py_XDECREF(value);
3073 Py_XDECREF(type);
3074 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003075 return WHY_EXCEPTION;
3076}
3077
Tim Petersd6d010b2001-06-21 02:49:55 +00003078/* Iterate v argcnt times and store the results on the stack (via decreasing
3079 sp). Return 1 for success, 0 if error. */
3080
Barry Warsawe42b18f1997-08-25 22:13:04 +00003081static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003082unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003083{
Tim Petersd6d010b2001-06-21 02:49:55 +00003084 int i = 0;
3085 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003086 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003087
Tim Petersd6d010b2001-06-21 02:49:55 +00003088 assert(v != NULL);
3089
3090 it = PyObject_GetIter(v);
3091 if (it == NULL)
3092 goto Error;
3093
3094 for (; i < argcnt; i++) {
3095 w = PyIter_Next(it);
3096 if (w == NULL) {
3097 /* Iterator done, via error or exhaustion. */
3098 if (!PyErr_Occurred()) {
3099 PyErr_Format(PyExc_ValueError,
3100 "need more than %d value%s to unpack",
3101 i, i == 1 ? "" : "s");
3102 }
3103 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003104 }
3105 *--sp = w;
3106 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003107
3108 /* We better have exhausted the iterator now. */
3109 w = PyIter_Next(it);
3110 if (w == NULL) {
3111 if (PyErr_Occurred())
3112 goto Error;
3113 Py_DECREF(it);
3114 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003115 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003116 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003117 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003118 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003119Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003120 for (; i > 0; i--, sp++)
3121 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003122 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003123 return 0;
3124}
3125
3126
Guido van Rossum96a42c81992-01-12 02:29:51 +00003127#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003128static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003129prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003130{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003131 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003132 if (PyObject_Print(v, stdout, 0) != 0)
3133 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003134 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003135 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003136}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003137#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003138
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003139static void
Fred Drake5755ce62001-06-27 19:19:46 +00003140call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003141{
Guido van Rossumb209a111997-04-29 18:18:01 +00003142 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003143 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003144 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003145 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003146 value = Py_None;
3147 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003148 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003149 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003150 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003151 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003152 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003153 }
Fred Drake5755ce62001-06-27 19:19:46 +00003154 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003155 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003156 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003157 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003158 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003159 Py_XDECREF(type);
3160 Py_XDECREF(value);
3161 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003162 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003163}
3164
Fred Drake4ec5d562001-10-04 19:26:43 +00003165static void
3166call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003167 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003168{
3169 PyObject *type, *value, *traceback;
3170 int err;
3171 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003172 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003173 if (err == 0)
3174 PyErr_Restore(type, value, traceback);
3175 else {
3176 Py_XDECREF(type);
3177 Py_XDECREF(value);
3178 Py_XDECREF(traceback);
3179 }
3180}
3181
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003182static int
Fred Drake5755ce62001-06-27 19:19:46 +00003183call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3184 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003185{
Fred Drake5755ce62001-06-27 19:19:46 +00003186 register PyThreadState *tstate = frame->f_tstate;
3187 int result;
3188 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003189 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003190 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003191 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003192 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003193 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3194 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003195 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003196 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003197}
3198
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003199PyObject *
3200_PyEval_CallTracing(PyObject *func, PyObject *args)
3201{
3202 PyFrameObject *frame = PyEval_GetFrame();
3203 PyThreadState *tstate = frame->f_tstate;
3204 int save_tracing = tstate->tracing;
3205 int save_use_tracing = tstate->use_tracing;
3206 PyObject *result;
3207
3208 tstate->tracing = 0;
3209 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3210 || (tstate->c_profilefunc != NULL));
3211 result = PyObject_Call(func, args, NULL);
3212 tstate->tracing = save_tracing;
3213 tstate->use_tracing = save_use_tracing;
3214 return result;
3215}
3216
Michael W. Hudson006c7522002-11-08 13:08:46 +00003217static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003218maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003219 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3220 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003221{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003222 int result = 0;
3223
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003224 /* If the last instruction executed isn't in the current
3225 instruction window, reset the window. If the last
3226 instruction happens to fall at the start of a line or if it
3227 represents a jump backwards, call the trace function.
3228 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003229 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003230 int line;
3231 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003232
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003233 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3234 &bounds);
3235 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003236 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003237 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003238 PyTrace_LINE, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003239 }
3240 *instr_lb = bounds.ap_lower;
3241 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003242 }
Armin Rigobf57a142004-03-22 19:24:58 +00003243 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003244 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003245 }
3246 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003247 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003248}
3249
Fred Drake5755ce62001-06-27 19:19:46 +00003250void
3251PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003252{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003253 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003254 PyObject *temp = tstate->c_profileobj;
3255 Py_XINCREF(arg);
3256 tstate->c_profilefunc = NULL;
3257 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003258 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003259 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003260 Py_XDECREF(temp);
3261 tstate->c_profilefunc = func;
3262 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003263 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003264 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003265}
3266
3267void
3268PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3269{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003270 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003271 PyObject *temp = tstate->c_traceobj;
3272 Py_XINCREF(arg);
3273 tstate->c_tracefunc = NULL;
3274 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003275 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003276 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003277 Py_XDECREF(temp);
3278 tstate->c_tracefunc = func;
3279 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003280 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003281 tstate->use_tracing = ((func != NULL)
3282 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003283}
3284
Guido van Rossumb209a111997-04-29 18:18:01 +00003285PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003286PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003287{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003288 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003289 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003290 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003291 else
3292 return current_frame->f_builtins;
3293}
3294
Guido van Rossumb209a111997-04-29 18:18:01 +00003295PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003296PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003297{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003298 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003299 if (current_frame == NULL)
3300 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003301 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003302 return current_frame->f_locals;
3303}
3304
Guido van Rossumb209a111997-04-29 18:18:01 +00003305PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003306PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003307{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003308 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003309 if (current_frame == NULL)
3310 return NULL;
3311 else
3312 return current_frame->f_globals;
3313}
3314
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003315PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003316PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003317{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003318 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003319 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003320}
3321
Guido van Rossum6135a871995-01-09 17:53:26 +00003322int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003323PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003324{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003325 PyFrameObject *current_frame = PyEval_GetFrame();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003326 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003327}
3328
Guido van Rossumbe270261997-05-22 22:26:18 +00003329int
Tim Peters5ba58662001-07-16 02:29:45 +00003330PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003331{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003332 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003333 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003334
3335 if (current_frame != NULL) {
3336 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003337 const int compilerflags = codeflags & PyCF_MASK;
3338 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003339 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003340 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003341 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003342#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003343 if (codeflags & CO_GENERATOR_ALLOWED) {
3344 result = 1;
3345 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3346 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003347#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003348 }
3349 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003350}
3351
3352int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003353Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003354{
Guido van Rossumb209a111997-04-29 18:18:01 +00003355 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003356 if (f == NULL)
3357 return 0;
3358 if (!PyFile_SoftSpace(f, 0))
3359 return 0;
3360 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003361}
3362
Guido van Rossum3f5da241990-12-20 15:06:42 +00003363
Guido van Rossum681d79a1995-07-18 14:51:37 +00003364/* External interface to call any callable object.
3365 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003366
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003367#undef PyEval_CallObject
3368/* for backward compatibility: export this interface */
3369
Guido van Rossumb209a111997-04-29 18:18:01 +00003370PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003371PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003372{
Guido van Rossumb209a111997-04-29 18:18:01 +00003373 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003374}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003375#define PyEval_CallObject(func,arg) \
3376 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003377
Guido van Rossumb209a111997-04-29 18:18:01 +00003378PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003379PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003380{
Jeremy Hylton52820442001-01-03 23:52:36 +00003381 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003382
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003383 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003384 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003385 if (arg == NULL)
3386 return NULL;
3387 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003388 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003389 PyErr_SetString(PyExc_TypeError,
3390 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003391 return NULL;
3392 }
3393 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003394 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003395
Guido van Rossumb209a111997-04-29 18:18:01 +00003396 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003397 PyErr_SetString(PyExc_TypeError,
3398 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003399 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003400 return NULL;
3401 }
3402
Tim Peters6d6c1a32001-08-02 04:15:00 +00003403 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003404 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003405 return result;
3406}
3407
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003408const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003409PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003410{
3411 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003412 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003413 else if (PyFunction_Check(func))
3414 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3415 else if (PyCFunction_Check(func))
3416 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003417 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003418 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003419}
3420
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003421const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003422PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003423{
3424 if (PyMethod_Check(func))
3425 return "()";
3426 else if (PyFunction_Check(func))
3427 return "()";
3428 else if (PyCFunction_Check(func))
3429 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003430 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003431 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003432}
3433
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003434static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003435err_args(PyObject *func, int flags, int nargs)
3436{
3437 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003438 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003439 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003440 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003441 nargs);
3442 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003443 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003444 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003445 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003446 nargs);
3447}
3448
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003449#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003450if (tstate->use_tracing && tstate->c_profilefunc) { \
3451 if (call_trace(tstate->c_profilefunc, \
3452 tstate->c_profileobj, \
3453 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003454 func)) { \
3455 x = NULL; \
3456 } \
3457 else { \
3458 x = call; \
3459 if (tstate->c_profilefunc != NULL) { \
3460 if (x == NULL) { \
3461 call_trace_protected(tstate->c_profilefunc, \
3462 tstate->c_profileobj, \
3463 tstate->frame, PyTrace_C_EXCEPTION, \
3464 func); \
3465 /* XXX should pass (type, value, tb) */ \
3466 } else { \
3467 if (call_trace(tstate->c_profilefunc, \
3468 tstate->c_profileobj, \
3469 tstate->frame, PyTrace_C_RETURN, \
3470 func)) { \
3471 Py_DECREF(x); \
3472 x = NULL; \
3473 } \
3474 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003475 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003476 } \
3477} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003478 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003479 }
3480
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003481static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003482call_function(PyObject ***pp_stack, int oparg
3483#ifdef WITH_TSC
3484 , uint64* pintr0, uint64* pintr1
3485#endif
3486 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003487{
3488 int na = oparg & 0xff;
3489 int nk = (oparg>>8) & 0xff;
3490 int n = na + 2 * nk;
3491 PyObject **pfunc = (*pp_stack) - n - 1;
3492 PyObject *func = *pfunc;
3493 PyObject *x, *w;
3494
Jeremy Hylton985eba52003-02-05 23:13:00 +00003495 /* Always dispatch PyCFunction first, because these are
3496 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003497 */
3498 if (PyCFunction_Check(func) && nk == 0) {
3499 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003500 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003501
3502 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003503 if (flags & (METH_NOARGS | METH_O)) {
3504 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3505 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003506 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003507 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003508 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003509 else if (flags & METH_O && na == 1) {
3510 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003511 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003512 Py_DECREF(arg);
3513 }
3514 else {
3515 err_args(func, flags, na);
3516 x = NULL;
3517 }
3518 }
3519 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003520 PyObject *callargs;
3521 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003522 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003523 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003524 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003525 Py_XDECREF(callargs);
3526 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003527 } else {
3528 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3529 /* optimize access to bound methods */
3530 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003531 PCALL(PCALL_METHOD);
3532 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003533 Py_INCREF(self);
3534 func = PyMethod_GET_FUNCTION(func);
3535 Py_INCREF(func);
3536 Py_DECREF(*pfunc);
3537 *pfunc = self;
3538 na++;
3539 n++;
3540 } else
3541 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003542 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003543 if (PyFunction_Check(func))
3544 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003545 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003546 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003547 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003548 Py_DECREF(func);
3549 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003550
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003551 /* Clear the stack of the function object. Also removes
3552 the arguments in case they weren't consumed already
3553 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003554 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003555 while ((*pp_stack) > pfunc) {
3556 w = EXT_POP(*pp_stack);
3557 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003558 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003559 }
3560 return x;
3561}
3562
Jeremy Hylton192690e2002-08-16 18:36:11 +00003563/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003564 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003565 For the simplest case -- a function that takes only positional
3566 arguments and is called with only positional arguments -- it
3567 inlines the most primitive frame setup code from
3568 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3569 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003570*/
3571
3572static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003573fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003574{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003575 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003576 PyObject *globals = PyFunction_GET_GLOBALS(func);
3577 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003578 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003579 PyObject **d = NULL;
3580 int nd = 0;
3581
Jeremy Hylton985eba52003-02-05 23:13:00 +00003582 PCALL(PCALL_FUNCTION);
3583 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003584 if (argdefs == NULL && co->co_argcount == n &&
3585 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003586 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3587 PyFrameObject *f;
3588 PyObject *retval = NULL;
3589 PyThreadState *tstate = PyThreadState_GET();
3590 PyObject **fastlocals, **stack;
3591 int i;
3592
3593 PCALL(PCALL_FASTER_FUNCTION);
3594 assert(globals != NULL);
3595 /* XXX Perhaps we should create a specialized
3596 PyFrame_New() that doesn't take locals, but does
3597 take builtins without sanity checking them.
3598 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003599 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003600 f = PyFrame_New(tstate, co, globals, NULL);
3601 if (f == NULL)
3602 return NULL;
3603
3604 fastlocals = f->f_localsplus;
3605 stack = (*pp_stack) - n;
3606
3607 for (i = 0; i < n; i++) {
3608 Py_INCREF(*stack);
3609 fastlocals[i] = *stack++;
3610 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003611 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003612 ++tstate->recursion_depth;
3613 Py_DECREF(f);
3614 --tstate->recursion_depth;
3615 return retval;
3616 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003617 if (argdefs != NULL) {
3618 d = &PyTuple_GET_ITEM(argdefs, 0);
3619 nd = ((PyTupleObject *)argdefs)->ob_size;
3620 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003621 return PyEval_EvalCodeEx(co, globals,
3622 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003623 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003624 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003625}
3626
3627static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003628update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3629 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003630{
3631 PyObject *kwdict = NULL;
3632 if (orig_kwdict == NULL)
3633 kwdict = PyDict_New();
3634 else {
3635 kwdict = PyDict_Copy(orig_kwdict);
3636 Py_DECREF(orig_kwdict);
3637 }
3638 if (kwdict == NULL)
3639 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003640 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003641 int err;
3642 PyObject *value = EXT_POP(*pp_stack);
3643 PyObject *key = EXT_POP(*pp_stack);
3644 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003645 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003646 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003647 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003648 PyEval_GetFuncName(func),
3649 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003650 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003651 Py_DECREF(key);
3652 Py_DECREF(value);
3653 Py_DECREF(kwdict);
3654 return NULL;
3655 }
3656 err = PyDict_SetItem(kwdict, key, value);
3657 Py_DECREF(key);
3658 Py_DECREF(value);
3659 if (err) {
3660 Py_DECREF(kwdict);
3661 return NULL;
3662 }
3663 }
3664 return kwdict;
3665}
3666
3667static PyObject *
3668update_star_args(int nstack, int nstar, PyObject *stararg,
3669 PyObject ***pp_stack)
3670{
3671 PyObject *callargs, *w;
3672
3673 callargs = PyTuple_New(nstack + nstar);
3674 if (callargs == NULL) {
3675 return NULL;
3676 }
3677 if (nstar) {
3678 int i;
3679 for (i = 0; i < nstar; i++) {
3680 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3681 Py_INCREF(a);
3682 PyTuple_SET_ITEM(callargs, nstack + i, a);
3683 }
3684 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003685 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003686 w = EXT_POP(*pp_stack);
3687 PyTuple_SET_ITEM(callargs, nstack, w);
3688 }
3689 return callargs;
3690}
3691
3692static PyObject *
3693load_args(PyObject ***pp_stack, int na)
3694{
3695 PyObject *args = PyTuple_New(na);
3696 PyObject *w;
3697
3698 if (args == NULL)
3699 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003700 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003701 w = EXT_POP(*pp_stack);
3702 PyTuple_SET_ITEM(args, na, w);
3703 }
3704 return args;
3705}
3706
3707static PyObject *
3708do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3709{
3710 PyObject *callargs = NULL;
3711 PyObject *kwdict = NULL;
3712 PyObject *result = NULL;
3713
3714 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003715 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003716 if (kwdict == NULL)
3717 goto call_fail;
3718 }
3719 callargs = load_args(pp_stack, na);
3720 if (callargs == NULL)
3721 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003722#ifdef CALL_PROFILE
3723 /* At this point, we have to look at the type of func to
3724 update the call stats properly. Do it here so as to avoid
3725 exposing the call stats machinery outside ceval.c
3726 */
3727 if (PyFunction_Check(func))
3728 PCALL(PCALL_FUNCTION);
3729 else if (PyMethod_Check(func))
3730 PCALL(PCALL_METHOD);
3731 else if (PyType_Check(func))
3732 PCALL(PCALL_TYPE);
3733 else
3734 PCALL(PCALL_OTHER);
3735#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003736 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003737 call_fail:
3738 Py_XDECREF(callargs);
3739 Py_XDECREF(kwdict);
3740 return result;
3741}
3742
3743static PyObject *
3744ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3745{
3746 int nstar = 0;
3747 PyObject *callargs = NULL;
3748 PyObject *stararg = NULL;
3749 PyObject *kwdict = NULL;
3750 PyObject *result = NULL;
3751
3752 if (flags & CALL_FLAG_KW) {
3753 kwdict = EXT_POP(*pp_stack);
3754 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003755 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003756 "%s%s argument after ** "
3757 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003758 PyEval_GetFuncName(func),
3759 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003760 goto ext_call_fail;
3761 }
3762 }
3763 if (flags & CALL_FLAG_VAR) {
3764 stararg = EXT_POP(*pp_stack);
3765 if (!PyTuple_Check(stararg)) {
3766 PyObject *t = NULL;
3767 t = PySequence_Tuple(stararg);
3768 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003769 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3770 PyErr_Format(PyExc_TypeError,
3771 "%s%s argument after * "
3772 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003773 PyEval_GetFuncName(func),
3774 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003775 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003776 goto ext_call_fail;
3777 }
3778 Py_DECREF(stararg);
3779 stararg = t;
3780 }
3781 nstar = PyTuple_GET_SIZE(stararg);
3782 }
3783 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003784 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003785 if (kwdict == NULL)
3786 goto ext_call_fail;
3787 }
3788 callargs = update_star_args(na, nstar, stararg, pp_stack);
3789 if (callargs == NULL)
3790 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003791#ifdef CALL_PROFILE
3792 /* At this point, we have to look at the type of func to
3793 update the call stats properly. Do it here so as to avoid
3794 exposing the call stats machinery outside ceval.c
3795 */
3796 if (PyFunction_Check(func))
3797 PCALL(PCALL_FUNCTION);
3798 else if (PyMethod_Check(func))
3799 PCALL(PCALL_METHOD);
3800 else if (PyType_Check(func))
3801 PCALL(PCALL_TYPE);
3802 else
3803 PCALL(PCALL_OTHER);
3804#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003805 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003806 ext_call_fail:
3807 Py_XDECREF(callargs);
3808 Py_XDECREF(kwdict);
3809 Py_XDECREF(stararg);
3810 return result;
3811}
3812
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003813/* Extract a slice index from a PyInt or PyLong or an object with the
3814 nb_index slot defined, and store in *pi.
3815 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3816 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 +00003817 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003818*/
Tim Petersb5196382001-12-16 19:44:20 +00003819/* Note: If v is NULL, return success without storing into *pi. This
3820 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3821 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003822*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003823int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003824_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003825{
Tim Petersb5196382001-12-16 19:44:20 +00003826 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003827 Py_ssize_t x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003828 if (PyInt_CheckExact(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003829 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3830 however, it looks like it should be AsSsize_t.
3831 There should be a comment here explaining why.
3832 */
3833 x = PyInt_AS_LONG(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003834 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003835 else if (PyIndex_Check(v)) {
3836 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003837 if (x == -1 && PyErr_Occurred())
3838 return 0;
3839 }
3840 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003841 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003842 "slice indices must be integers or "
3843 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003844 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003845 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003846 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003847 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003848 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003849}
3850
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003851#undef ISINDEX
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003852#define ISINDEX(x) ((x) == NULL || \
3853 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003854
Guido van Rossumb209a111997-04-29 18:18:01 +00003855static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003856apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003857{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003858 PyTypeObject *tp = u->ob_type;
3859 PySequenceMethods *sq = tp->tp_as_sequence;
3860
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003861 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003862 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003863 if (!_PyEval_SliceIndex(v, &ilow))
3864 return NULL;
3865 if (!_PyEval_SliceIndex(w, &ihigh))
3866 return NULL;
3867 return PySequence_GetSlice(u, ilow, ihigh);
3868 }
3869 else {
3870 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003871 if (slice != NULL) {
3872 PyObject *res = PyObject_GetItem(u, slice);
3873 Py_DECREF(slice);
3874 return res;
3875 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003876 else
3877 return NULL;
3878 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003879}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003880
3881static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003882assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3883 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003884{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003885 PyTypeObject *tp = u->ob_type;
3886 PySequenceMethods *sq = tp->tp_as_sequence;
3887
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003888 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003889 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003890 if (!_PyEval_SliceIndex(v, &ilow))
3891 return -1;
3892 if (!_PyEval_SliceIndex(w, &ihigh))
3893 return -1;
3894 if (x == NULL)
3895 return PySequence_DelSlice(u, ilow, ihigh);
3896 else
3897 return PySequence_SetSlice(u, ilow, ihigh, x);
3898 }
3899 else {
3900 PyObject *slice = PySlice_New(v, w, NULL);
3901 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003902 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003903 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003904 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003905 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003906 res = PyObject_DelItem(u, slice);
3907 Py_DECREF(slice);
3908 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003909 }
3910 else
3911 return -1;
3912 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003913}
3914
Guido van Rossumb209a111997-04-29 18:18:01 +00003915static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003916cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003917{
Guido van Rossumac7be682001-01-17 15:42:30 +00003918 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003919 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003920 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003921 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003922 break;
3923 case PyCmp_IS_NOT:
3924 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003925 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003926 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003927 res = PySequence_Contains(w, v);
3928 if (res < 0)
3929 return NULL;
3930 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003931 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003932 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003933 if (res < 0)
3934 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003935 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003936 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003937 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003938 if (PyTuple_Check(w)) {
3939 Py_ssize_t i, length;
3940 length = PyTuple_Size(w);
3941 for (i = 0; i < length; i += 1) {
3942 PyObject *exc = PyTuple_GET_ITEM(w, i);
3943 if (PyString_Check(exc)) {
3944 int ret_val;
3945 ret_val = PyErr_WarnEx(
3946 PyExc_DeprecationWarning,
3947 "catching of string "
3948 "exceptions is "
3949 "deprecated", 1);
3950 if (ret_val == -1)
3951 return NULL;
3952 }
3953 }
3954 }
3955 else {
3956 if (PyString_Check(w)) {
3957 int ret_val;
3958 ret_val = PyErr_WarnEx(
3959 PyExc_DeprecationWarning,
3960 "catching of string "
3961 "exceptions is deprecated",
3962 1);
3963 if (ret_val == -1)
3964 return NULL;
3965 }
3966 }
Barry Warsaw4249f541997-08-22 21:26:19 +00003967 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003968 break;
3969 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003970 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003971 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003972 v = res ? Py_True : Py_False;
3973 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003974 return v;
3975}
3976
Thomas Wouters52152252000-08-17 22:55:00 +00003977static PyObject *
3978import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003979{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003980 PyObject *x;
3981
3982 x = PyObject_GetAttr(v, name);
3983 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003984 PyErr_Format(PyExc_ImportError,
3985 "cannot import name %.230s",
3986 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003987 }
Thomas Wouters52152252000-08-17 22:55:00 +00003988 return x;
3989}
Guido van Rossumac7be682001-01-17 15:42:30 +00003990
Thomas Wouters52152252000-08-17 22:55:00 +00003991static int
3992import_all_from(PyObject *locals, PyObject *v)
3993{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003994 PyObject *all = PyObject_GetAttrString(v, "__all__");
3995 PyObject *dict, *name, *value;
3996 int skip_leading_underscores = 0;
3997 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003998
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003999 if (all == NULL) {
4000 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4001 return -1; /* Unexpected error */
4002 PyErr_Clear();
4003 dict = PyObject_GetAttrString(v, "__dict__");
4004 if (dict == NULL) {
4005 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4006 return -1;
4007 PyErr_SetString(PyExc_ImportError,
4008 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004009 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004010 }
4011 all = PyMapping_Keys(dict);
4012 Py_DECREF(dict);
4013 if (all == NULL)
4014 return -1;
4015 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004016 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004017
4018 for (pos = 0, err = 0; ; pos++) {
4019 name = PySequence_GetItem(all, pos);
4020 if (name == NULL) {
4021 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4022 err = -1;
4023 else
4024 PyErr_Clear();
4025 break;
4026 }
4027 if (skip_leading_underscores &&
4028 PyString_Check(name) &&
4029 PyString_AS_STRING(name)[0] == '_')
4030 {
4031 Py_DECREF(name);
4032 continue;
4033 }
4034 value = PyObject_GetAttr(v, name);
4035 if (value == NULL)
4036 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004037 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004038 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004039 else
4040 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004041 Py_DECREF(name);
4042 Py_XDECREF(value);
4043 if (err != 0)
4044 break;
4045 }
4046 Py_DECREF(all);
4047 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004048}
4049
Guido van Rossumb209a111997-04-29 18:18:01 +00004050static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004051build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004052{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004053 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004054
4055 if (PyDict_Check(methods))
4056 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004057 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004058 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004059 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4060 base = PyTuple_GET_ITEM(bases, 0);
4061 metaclass = PyObject_GetAttrString(base, "__class__");
4062 if (metaclass == NULL) {
4063 PyErr_Clear();
4064 metaclass = (PyObject *)base->ob_type;
4065 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004066 }
4067 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004068 else {
4069 PyObject *g = PyEval_GetGlobals();
4070 if (g != NULL && PyDict_Check(g))
4071 metaclass = PyDict_GetItemString(g, "__metaclass__");
4072 if (metaclass == NULL)
Guido van Rossum45aecf42006-03-15 04:58:47 +00004073 metaclass = (PyObject *) &PyType_Type;
Guido van Rossum7851eea2001-09-12 19:19:18 +00004074 Py_INCREF(metaclass);
4075 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004076 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004077 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004078 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004079 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004080 in a base that was not a class (such the random module
4081 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004082 by augmenting the error message with more information.*/
4083
4084 PyObject *ptype, *pvalue, *ptraceback;
4085
4086 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4087 if (PyString_Check(pvalue)) {
4088 PyObject *newmsg;
4089 newmsg = PyString_FromFormat(
4090 "Error when calling the metaclass bases\n %s",
4091 PyString_AS_STRING(pvalue));
4092 if (newmsg != NULL) {
4093 Py_DECREF(pvalue);
4094 pvalue = newmsg;
4095 }
4096 }
4097 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004098 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004099 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004100}
4101
Guido van Rossumac7be682001-01-17 15:42:30 +00004102static void
Paul Prescode68140d2000-08-30 20:25:01 +00004103format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4104{
4105 char *obj_str;
4106
4107 if (!obj)
4108 return;
4109
4110 obj_str = PyString_AsString(obj);
4111 if (!obj_str)
4112 return;
4113
4114 PyErr_Format(exc, format_str, obj_str);
4115}
Guido van Rossum950361c1997-01-24 13:49:28 +00004116
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004117static PyObject *
4118string_concatenate(PyObject *v, PyObject *w,
4119 PyFrameObject *f, unsigned char *next_instr)
4120{
4121 /* This function implements 'variable += expr' when both arguments
4122 are strings. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004123 Py_ssize_t v_len = PyString_GET_SIZE(v);
4124 Py_ssize_t w_len = PyString_GET_SIZE(w);
4125 Py_ssize_t new_len = v_len + w_len;
4126 if (new_len < 0) {
4127 PyErr_SetString(PyExc_OverflowError,
4128 "strings are too large to concat");
4129 return NULL;
4130 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004131
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004132 if (v->ob_refcnt == 2) {
4133 /* In the common case, there are 2 references to the value
4134 * stored in 'variable' when the += is performed: one on the
4135 * value stack (in 'v') and one still stored in the 'variable'.
4136 * We try to delete the variable now to reduce the refcnt to 1.
4137 */
4138 switch (*next_instr) {
4139 case STORE_FAST:
4140 {
4141 int oparg = PEEKARG();
4142 PyObject **fastlocals = f->f_localsplus;
4143 if (GETLOCAL(oparg) == v)
4144 SETLOCAL(oparg, NULL);
4145 break;
4146 }
4147 case STORE_DEREF:
4148 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004149 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004150 PyObject *c = freevars[PEEKARG()];
4151 if (PyCell_GET(c) == v)
4152 PyCell_Set(c, NULL);
4153 break;
4154 }
4155 case STORE_NAME:
4156 {
4157 PyObject *names = f->f_code->co_names;
4158 PyObject *name = GETITEM(names, PEEKARG());
4159 PyObject *locals = f->f_locals;
4160 if (PyDict_CheckExact(locals) &&
4161 PyDict_GetItem(locals, name) == v) {
4162 if (PyDict_DelItem(locals, name) != 0) {
4163 PyErr_Clear();
4164 }
4165 }
4166 break;
4167 }
4168 }
4169 }
4170
Armin Rigo618fbf52004-08-07 20:58:32 +00004171 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004172 /* Now we own the last reference to 'v', so we can resize it
4173 * in-place.
4174 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004175 if (_PyString_Resize(&v, new_len) != 0) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004176 /* XXX if _PyString_Resize() fails, 'v' has been
4177 * deallocated so it cannot be put back into 'variable'.
4178 * The MemoryError is raised when there is no value in
4179 * 'variable', which might (very remotely) be a cause
4180 * of incompatibilities.
4181 */
4182 return NULL;
4183 }
4184 /* copy 'w' into the newly allocated area of 'v' */
4185 memcpy(PyString_AS_STRING(v) + v_len,
4186 PyString_AS_STRING(w), w_len);
4187 return v;
4188 }
4189 else {
4190 /* When in-place resizing is not an option. */
4191 PyString_Concat(&v, w);
4192 return v;
4193 }
4194}
4195
Guido van Rossum950361c1997-01-24 13:49:28 +00004196#ifdef DYNAMIC_EXECUTION_PROFILE
4197
Skip Montanarof118cb12001-10-15 20:51:38 +00004198static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004199getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004200{
4201 int i;
4202 PyObject *l = PyList_New(256);
4203 if (l == NULL) return NULL;
4204 for (i = 0; i < 256; i++) {
4205 PyObject *x = PyInt_FromLong(a[i]);
4206 if (x == NULL) {
4207 Py_DECREF(l);
4208 return NULL;
4209 }
4210 PyList_SetItem(l, i, x);
4211 }
4212 for (i = 0; i < 256; i++)
4213 a[i] = 0;
4214 return l;
4215}
4216
4217PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004218_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004219{
4220#ifndef DXPAIRS
4221 return getarray(dxp);
4222#else
4223 int i;
4224 PyObject *l = PyList_New(257);
4225 if (l == NULL) return NULL;
4226 for (i = 0; i < 257; i++) {
4227 PyObject *x = getarray(dxpairs[i]);
4228 if (x == NULL) {
4229 Py_DECREF(l);
4230 return NULL;
4231 }
4232 PyList_SetItem(l, i, x);
4233 }
4234 return l;
4235#endif
4236}
4237
4238#endif