blob: fdee13d30a553c3bfeaacd56cb3e9dfb012ba71e [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 *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000120static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
121static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000122static void format_exc_check_arg(PyObject *, char *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000123static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000124 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000125
Paul Prescode68140d2000-08-30 20:25:01 +0000126#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000127 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000128#define GLOBAL_NAME_ERROR_MSG \
129 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000130#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000131 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000132#define UNBOUNDFREE_ERROR_MSG \
133 "free variable '%.200s' referenced before assignment" \
134 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000135
Guido van Rossum950361c1997-01-24 13:49:28 +0000136/* Dynamic execution profile */
137#ifdef DYNAMIC_EXECUTION_PROFILE
138#ifdef DXPAIRS
139static long dxpairs[257][256];
140#define dxp dxpairs[256]
141#else
142static long dxp[256];
143#endif
144#endif
145
Jeremy Hylton985eba52003-02-05 23:13:00 +0000146/* Function call profile */
147#ifdef CALL_PROFILE
148#define PCALL_NUM 11
149static int pcall[PCALL_NUM];
150
151#define PCALL_ALL 0
152#define PCALL_FUNCTION 1
153#define PCALL_FAST_FUNCTION 2
154#define PCALL_FASTER_FUNCTION 3
155#define PCALL_METHOD 4
156#define PCALL_BOUND_METHOD 5
157#define PCALL_CFUNCTION 6
158#define PCALL_TYPE 7
159#define PCALL_GENERATOR 8
160#define PCALL_OTHER 9
161#define PCALL_POP 10
162
163/* Notes about the statistics
164
165 PCALL_FAST stats
166
167 FAST_FUNCTION means no argument tuple needs to be created.
168 FASTER_FUNCTION means that the fast-path frame setup code is used.
169
170 If there is a method call where the call can be optimized by changing
171 the argument tuple and calling the function directly, it gets recorded
172 twice.
173
174 As a result, the relationship among the statistics appears to be
175 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
176 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
177 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
178 PCALL_METHOD > PCALL_BOUND_METHOD
179*/
180
181#define PCALL(POS) pcall[POS]++
182
183PyObject *
184PyEval_GetCallStats(PyObject *self)
185{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000187 pcall[0], pcall[1], pcall[2], pcall[3],
188 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000189 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000190}
191#else
192#define PCALL(O)
193
194PyObject *
195PyEval_GetCallStats(PyObject *self)
196{
197 Py_INCREF(Py_None);
198 return Py_None;
199}
200#endif
201
Tim Peters5ca576e2001-06-18 22:08:13 +0000202
Guido van Rossume59214e1994-08-30 08:01:59 +0000203#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000204
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000205#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000206#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000207#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000208#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000209
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000210static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000211static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000212
Tim Peters7f468f22004-10-11 02:40:51 +0000213int
214PyEval_ThreadsInitialized(void)
215{
216 return interpreter_lock != 0;
217}
218
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000220PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000221{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000223 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000224 interpreter_lock = PyThread_allocate_lock();
225 PyThread_acquire_lock(interpreter_lock, 1);
226 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000227}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000228
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000229void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000230PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000232 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000233}
234
235void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000238 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000239}
240
241void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000242PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000243{
244 if (tstate == NULL)
245 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000246 /* Check someone has called PyEval_InitThreads() to create the lock */
247 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000248 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000249 if (PyThreadState_Swap(tstate) != NULL)
250 Py_FatalError(
251 "PyEval_AcquireThread: non-NULL old thread state");
252}
253
254void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000255PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000256{
257 if (tstate == NULL)
258 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
259 if (PyThreadState_Swap(NULL) != tstate)
260 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000261 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000262}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000263
264/* This function is called from PyOS_AfterFork to ensure that newly
265 created child processes don't hold locks referring to threads which
266 are not running in the child process. (This could also be done using
267 pthread_atfork mechanism, at least for the pthreads implementation.) */
268
269void
270PyEval_ReInitThreads(void)
271{
272 if (!interpreter_lock)
273 return;
274 /*XXX Can't use PyThread_free_lock here because it does too
275 much error-checking. Doing this cleanly would require
276 adding a new function to each thread_*.h. Instead, just
277 create a new lock and waste a little bit of memory */
278 interpreter_lock = PyThread_allocate_lock();
279 PyThread_acquire_lock(interpreter_lock, 1);
280 main_thread = PyThread_get_thread_ident();
281}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000282#endif
283
Guido van Rossumff4949e1992-08-05 19:58:53 +0000284/* Functions save_thread and restore_thread are always defined so
285 dynamically loaded modules needn't be compiled separately for use
286 with and without threads: */
287
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000288PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000289PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000290{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000291 PyThreadState *tstate = PyThreadState_Swap(NULL);
292 if (tstate == NULL)
293 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000294#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000295 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000296 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000297#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000298 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299}
300
301void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000302PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000304 if (tstate == NULL)
305 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000306#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000308 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000309 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311 }
312#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000313 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000314}
315
316
Guido van Rossuma9672091994-09-14 13:31:22 +0000317/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
318 signal handlers or Mac I/O completion routines) can schedule calls
319 to a function to be called synchronously.
320 The synchronous function is called with one void* argument.
321 It should return 0 for success or -1 for failure -- failure should
322 be accompanied by an exception.
323
324 If registry succeeds, the registry function returns 0; if it fails
325 (e.g. due to too many pending calls) it returns -1 (without setting
326 an exception condition).
327
328 Note that because registry may occur from within signal handlers,
329 or other asynchronous events, calling malloc() is unsafe!
330
331#ifdef WITH_THREAD
332 Any thread can schedule pending calls, but only the main thread
333 will execute them.
334#endif
335
336 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
337 There are two possible race conditions:
338 (1) nested asynchronous registry calls;
339 (2) registry calls made while pending calls are being processed.
340 While (1) is very unlikely, (2) is a real possibility.
341 The current code is safe against (2), but not against (1).
342 The safety against (2) is derived from the fact that only one
343 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000344
Guido van Rossuma027efa1997-05-05 20:56:21 +0000345 XXX Darn! With the advent of thread state, we should have an array
346 of pending calls per thread in the thread state! Later...
347*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000348
Guido van Rossuma9672091994-09-14 13:31:22 +0000349#define NPENDINGCALLS 32
350static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000351 int (*func)(void *);
352 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000353} pendingcalls[NPENDINGCALLS];
354static volatile int pendingfirst = 0;
355static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000356static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000357
358int
Thomas Wouters334fb892000-07-25 12:56:38 +0000359Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000360{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000361 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000362 int i, j;
363 /* XXX Begin critical section */
364 /* XXX If you want this to be safe against nested
365 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000366 if (busy)
367 return -1;
368 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000369 i = pendinglast;
370 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000371 if (j == pendingfirst) {
372 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000373 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000374 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000375 pendingcalls[i].func = func;
376 pendingcalls[i].arg = arg;
377 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000378
379 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000380 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000381 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000382 /* XXX End critical section */
383 return 0;
384}
385
Guido van Rossum180d7b41994-09-29 09:45:57 +0000386int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000387Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000388{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000389 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000390#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000391 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000392 return 0;
393#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000394 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000395 return 0;
396 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000397 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000398 for (;;) {
399 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000400 int (*func)(void *);
401 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000402 i = pendingfirst;
403 if (i == pendinglast)
404 break; /* Queue empty */
405 func = pendingcalls[i].func;
406 arg = pendingcalls[i].arg;
407 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000408 if (func(arg) < 0) {
409 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000410 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000411 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000412 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000413 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000414 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000415 return 0;
416}
417
418
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000419/* The interpreter's recursion limit */
420
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000421#ifndef Py_DEFAULT_RECURSION_LIMIT
422#define Py_DEFAULT_RECURSION_LIMIT 1000
423#endif
424static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
425int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000426
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000427int
428Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000429{
430 return recursion_limit;
431}
432
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000433void
434Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000435{
436 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000437 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000438}
439
Armin Rigo2b3eb402003-10-28 12:05:48 +0000440/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
441 if the recursion_depth reaches _Py_CheckRecursionLimit.
442 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
443 to guarantee that _Py_CheckRecursiveCall() is regularly called.
444 Without USE_STACKCHECK, there is no need for this. */
445int
446_Py_CheckRecursiveCall(char *where)
447{
448 PyThreadState *tstate = PyThreadState_GET();
449
450#ifdef USE_STACKCHECK
451 if (PyOS_CheckStack()) {
452 --tstate->recursion_depth;
453 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
454 return -1;
455 }
456#endif
457 if (tstate->recursion_depth > recursion_limit) {
458 --tstate->recursion_depth;
459 PyErr_Format(PyExc_RuntimeError,
460 "maximum recursion depth exceeded%s",
461 where);
462 return -1;
463 }
464 _Py_CheckRecursionLimit = recursion_limit;
465 return 0;
466}
467
Guido van Rossum374a9221991-04-04 10:40:29 +0000468/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000469enum why_code {
470 WHY_NOT = 0x0001, /* No error */
471 WHY_EXCEPTION = 0x0002, /* Exception occurred */
472 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
473 WHY_RETURN = 0x0008, /* 'return' statement */
474 WHY_BREAK = 0x0010, /* 'break' statement */
475 WHY_CONTINUE = 0x0020, /* 'continue' statement */
476 WHY_YIELD = 0x0040 /* 'yield' operator */
477};
Guido van Rossum374a9221991-04-04 10:40:29 +0000478
Raymond Hettinger7c958652004-04-06 10:11:10 +0000479static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000480static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000481
Skip Montanarod581d772002-09-03 20:10:45 +0000482/* for manipulating the thread switch and periodic "stuff" - used to be
483 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000484int _Py_CheckInterval = 100;
485volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000486
Guido van Rossumb209a111997-04-29 18:18:01 +0000487PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000488PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000489{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000490 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000491 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000492 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000493 (PyObject **)NULL, 0,
494 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000495 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000496 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000497}
498
499
500/* Interpreter main loop */
501
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000502PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000503PyEval_EvalFrame(PyFrameObject *f) {
504 /* This is for backward compatibility with extension modules that
505 used this API; core interpreter code should call PyEval_EvalFrameEx() */
506 return PyEval_EvalFrameEx(f, 0);
507}
508
509PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000510PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000511{
Guido van Rossum950361c1997-01-24 13:49:28 +0000512#ifdef DXPAIRS
513 int lastopcode = 0;
514#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000515 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000516 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000517 register int opcode; /* Current opcode */
518 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000519 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000520 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000521 register PyObject *x; /* Result object -- NULL if error */
522 register PyObject *v; /* Temporary objects popped off stack */
523 register PyObject *w;
524 register PyObject *u;
525 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000526 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000527 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000528 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000529 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000530
Tim Peters8a5c3c72004-04-05 19:36:21 +0000531 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000532
533 not (instr_lb <= current_bytecode_offset < instr_ub)
534
Tim Peters8a5c3c72004-04-05 19:36:21 +0000535 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000536 initial values are such as to make this false the first
537 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000538 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000539
Guido van Rossumd076c731998-10-07 19:42:25 +0000540 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000541 PyObject *names;
542 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000543#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000544 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000545 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000546#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000547
Neal Norwitza81d2202002-07-14 00:27:26 +0000548/* Tuple access macros */
549
550#ifndef Py_DEBUG
551#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
552#else
553#define GETITEM(v, i) PyTuple_GetItem((v), (i))
554#endif
555
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000556#ifdef WITH_TSC
557/* Use Pentium timestamp counter to mark certain events:
558 inst0 -- beginning of switch statement for opcode dispatch
559 inst1 -- end of switch statement (may be skipped)
560 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000561 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000562 (may be skipped)
563 intr1 -- beginning of long interruption
564 intr2 -- end of long interruption
565
566 Many opcodes call out to helper C functions. In some cases, the
567 time in those functions should be counted towards the time for the
568 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
569 calls another Python function; there's no point in charge all the
570 bytecode executed by the called function to the caller.
571
572 It's hard to make a useful judgement statically. In the presence
573 of operator overloading, it's impossible to tell if a call will
574 execute new Python code or not.
575
576 It's a case-by-case judgement. I'll use intr1 for the following
577 cases:
578
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000579 IMPORT_STAR
580 IMPORT_FROM
581 CALL_FUNCTION (and friends)
582
583 */
584 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
585 int ticked = 0;
586
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000587 READ_TIMESTAMP(inst0);
588 READ_TIMESTAMP(inst1);
589 READ_TIMESTAMP(loop0);
590 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000591
592 /* shut up the compiler */
593 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000594#endif
595
Guido van Rossum374a9221991-04-04 10:40:29 +0000596/* Code access macros */
597
Martin v. Löwis18e16552006-02-15 17:27:45 +0000598#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000599#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000600#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000601#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000602#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000603#define JUMPBY(x) (next_instr += (x))
604
Raymond Hettingerf606f872003-03-16 03:11:04 +0000605/* OpCode prediction macros
606 Some opcodes tend to come in pairs thus making it possible to predict
607 the second code when the first is run. For example, COMPARE_OP is often
608 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
609 followed by a POP_TOP.
610
611 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000612 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000613 processor has a high likelihood of making its own successful branch
614 prediction which results in a nearly zero overhead transition to the
615 next opcode.
616
617 A successful prediction saves a trip through the eval-loop including
618 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000619
Tim Peters8a5c3c72004-04-05 19:36:21 +0000620 If collecting opcode statistics, turn off prediction so that
621 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000622 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000623*/
624
Raymond Hettingera7216982004-02-08 19:59:27 +0000625#ifdef DYNAMIC_EXECUTION_PROFILE
626#define PREDICT(op) if (0) goto PRED_##op
627#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000628#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000629#endif
630
Raymond Hettingerf606f872003-03-16 03:11:04 +0000631#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000632#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000633
Guido van Rossum374a9221991-04-04 10:40:29 +0000634/* Stack manipulation macros */
635
Martin v. Löwis18e16552006-02-15 17:27:45 +0000636/* The stack can grow at most MAXINT deep, as co_nlocals and
637 co_stacksize are ints. */
638#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000639#define EMPTY() (STACK_LEVEL() == 0)
640#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000641#define SECOND() (stack_pointer[-2])
642#define THIRD() (stack_pointer[-3])
643#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000644#define SET_TOP(v) (stack_pointer[-1] = (v))
645#define SET_SECOND(v) (stack_pointer[-2] = (v))
646#define SET_THIRD(v) (stack_pointer[-3] = (v))
647#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000648#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000649#define BASIC_PUSH(v) (*stack_pointer++ = (v))
650#define BASIC_POP() (*--stack_pointer)
651
Guido van Rossum96a42c81992-01-12 02:29:51 +0000652#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000653#define PUSH(v) { (void)(BASIC_PUSH(v), \
654 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000655 assert(STACK_LEVEL() <= co->co_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000656#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000657#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
658 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659 assert(STACK_LEVEL() <= co->co_stacksize); }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000660#define EXT_POP(STACK_POINTER) (lltrace && prtrace((STACK_POINTER)[-1], "ext_pop"), *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000661#else
662#define PUSH(v) BASIC_PUSH(v)
663#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000664#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000665#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000666#endif
667
Guido van Rossum681d79a1995-07-18 14:51:37 +0000668/* Local variable macros */
669
670#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000671
672/* The SETLOCAL() macro must not DECREF the local variable in-place and
673 then store the new value; it must copy the old value to a temporary
674 value, then store the new value, and then DECREF the temporary value.
675 This is because it is possible that during the DECREF the frame is
676 accessed by other code (e.g. a __del__ method or gc.collect()) and the
677 variable would be pointing to already-freed memory. */
678#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
679 GETLOCAL(i) = value; \
680 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000681
Guido van Rossuma027efa1997-05-05 20:56:21 +0000682/* Start of code */
683
Tim Peters5ca576e2001-06-18 22:08:13 +0000684 if (f == NULL)
685 return NULL;
686
Armin Rigo1d313ab2003-10-25 14:33:09 +0000687 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000688 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000689 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000690
Tim Peters5ca576e2001-06-18 22:08:13 +0000691 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000692
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000693 if (tstate->use_tracing) {
694 if (tstate->c_tracefunc != NULL) {
695 /* tstate->c_tracefunc, if defined, is a
696 function that will be called on *every* entry
697 to a code block. Its return value, if not
698 None, is a function that will be called at
699 the start of each executed line of code.
700 (Actually, the function must return itself
701 in order to continue tracing.) The trace
702 functions are called with three arguments:
703 a pointer to the current frame, a string
704 indicating why the function is called, and
705 an argument which depends on the situation.
706 The global trace function is also called
707 whenever an exception is detected. */
708 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
709 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000710 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 }
713 }
714 if (tstate->c_profilefunc != NULL) {
715 /* Similar for c_profilefunc, except it needn't
716 return itself and isn't called for "line" events */
717 if (call_trace(tstate->c_profilefunc,
718 tstate->c_profileobj,
719 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000720 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000721 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000722 }
723 }
724 }
725
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000726 co = f->f_code;
727 names = co->co_names;
728 consts = co->co_consts;
729 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000730 freevars = f->f_localsplus + co->co_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000731 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000732 /* An explanation is in order for the next line.
733
734 f->f_lasti now refers to the index of the last instruction
735 executed. You might think this was obvious from the name, but
736 this wasn't always true before 2.3! PyFrame_New now sets
737 f->f_lasti to -1 (i.e. the index *before* the first instruction)
738 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000739 does work. Promise.
740
741 When the PREDICT() macros are enabled, some opcode pairs follow in
742 direct succession without updating f->f_lasti. A successful
743 prediction effectively links the two codes together as if they
744 were a single new opcode; accordingly,f->f_lasti will point to
745 the first code in the pair (for instance, GET_ITER followed by
746 FOR_ITER is effectively a single opcode and f->f_lasti will point
747 at to the beginning of the combined pair.)
748 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000749 next_instr = first_instr + f->f_lasti + 1;
750 stack_pointer = f->f_stacktop;
751 assert(stack_pointer != NULL);
752 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
753
Tim Peters5ca576e2001-06-18 22:08:13 +0000754#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000756#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000757#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000758 filename = PyString_AsString(co->co_filename);
759#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000760
Guido van Rossum374a9221991-04-04 10:40:29 +0000761 why = WHY_NOT;
762 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000763 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000764 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000765
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000767 why = WHY_EXCEPTION;
768 goto on_error;
769 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000770
Guido van Rossum374a9221991-04-04 10:40:29 +0000771 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000772#ifdef WITH_TSC
773 if (inst1 == 0) {
774 /* Almost surely, the opcode executed a break
775 or a continue, preventing inst1 from being set
776 on the way out of the loop.
777 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000778 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000779 loop1 = inst1;
780 }
781 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
782 intr0, intr1);
783 ticked = 0;
784 inst1 = 0;
785 intr0 = 0;
786 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000787 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000788#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000789 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000790 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000791
Guido van Rossuma027efa1997-05-05 20:56:21 +0000792 /* Do periodic things. Doing this every time through
793 the loop would add too much overhead, so we do it
794 only every Nth instruction. We also do it if
795 ``things_to_do'' is set, i.e. when an asynchronous
796 event needs attention (e.g. a signal handler or
797 async I/O handler); see Py_AddPendingCall() and
798 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000799
Skip Montanarod581d772002-09-03 20:10:45 +0000800 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000801 if (*next_instr == SETUP_FINALLY) {
802 /* Make the last opcode before
803 a try: finally: block uninterruptable. */
804 goto fast_next_opcode;
805 }
Skip Montanarod581d772002-09-03 20:10:45 +0000806 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000807 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000808#ifdef WITH_TSC
809 ticked = 1;
810#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000811 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000812 if (Py_MakePendingCalls() < 0) {
813 why = WHY_EXCEPTION;
814 goto on_error;
815 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000816 if (things_to_do)
817 /* MakePendingCalls() didn't succeed.
818 Force early re-execution of this
819 "periodic" code, possibly after
820 a thread switch */
821 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000822 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000823#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000824 if (interpreter_lock) {
825 /* Give another thread a chance */
826
Guido van Rossum25ce5661997-08-02 03:10:38 +0000827 if (PyThreadState_Swap(NULL) != tstate)
828 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000829 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000830
831 /* Other threads may run now */
832
Guido van Rossum65d5b571998-12-21 19:32:43 +0000833 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000834 if (PyThreadState_Swap(tstate) != NULL)
835 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000836
837 /* Check for thread interrupts */
838
839 if (tstate->async_exc != NULL) {
840 x = tstate->async_exc;
841 tstate->async_exc = NULL;
842 PyErr_SetNone(x);
843 Py_DECREF(x);
844 why = WHY_EXCEPTION;
845 goto on_error;
846 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000847 }
848#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000849 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000850
Neil Schemenauer63543862002-02-17 19:10:14 +0000851 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000852 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000853
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000854 /* line-by-line tracing support */
855
856 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
857 /* see maybe_call_line_trace
858 for expository comments */
859 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000860
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000861 err = maybe_call_line_trace(tstate->c_tracefunc,
862 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000863 f, &instr_lb, &instr_ub,
864 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000865 /* Reload possibly changed frame fields */
866 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000867 if (f->f_stacktop != NULL) {
868 stack_pointer = f->f_stacktop;
869 f->f_stacktop = NULL;
870 }
871 if (err) {
872 /* trace function raised an exception */
873 goto on_error;
874 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000875 }
876
877 /* Extract opcode and argument */
878
Guido van Rossum374a9221991-04-04 10:40:29 +0000879 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000880 oparg = 0; /* allows oparg to be stored in a register because
881 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000882 if (HAS_ARG(opcode))
883 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000884 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000885#ifdef DYNAMIC_EXECUTION_PROFILE
886#ifdef DXPAIRS
887 dxpairs[lastopcode][opcode]++;
888 lastopcode = opcode;
889#endif
890 dxp[opcode]++;
891#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000892
Guido van Rossum96a42c81992-01-12 02:29:51 +0000893#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000894 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000895
Guido van Rossum96a42c81992-01-12 02:29:51 +0000896 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000897 if (HAS_ARG(opcode)) {
898 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000899 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000900 }
901 else {
902 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000903 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000904 }
905 }
906#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000907
Guido van Rossum374a9221991-04-04 10:40:29 +0000908 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000909 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000910
Guido van Rossum374a9221991-04-04 10:40:29 +0000911 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000912
Guido van Rossum374a9221991-04-04 10:40:29 +0000913 /* BEWARE!
914 It is essential that any operation that fails sets either
915 x to NULL, err to nonzero, or why to anything but WHY_NOT,
916 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000917
Guido van Rossum374a9221991-04-04 10:40:29 +0000918 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000919
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000920 case NOP:
921 goto fast_next_opcode;
922
Neil Schemenauer63543862002-02-17 19:10:14 +0000923 case LOAD_FAST:
924 x = GETLOCAL(oparg);
925 if (x != NULL) {
926 Py_INCREF(x);
927 PUSH(x);
928 goto fast_next_opcode;
929 }
930 format_exc_check_arg(PyExc_UnboundLocalError,
931 UNBOUNDLOCAL_ERROR_MSG,
932 PyTuple_GetItem(co->co_varnames, oparg));
933 break;
934
935 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000936 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000937 Py_INCREF(x);
938 PUSH(x);
939 goto fast_next_opcode;
940
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000941 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000942 case STORE_FAST:
943 v = POP();
944 SETLOCAL(oparg, v);
945 goto fast_next_opcode;
946
Raymond Hettingerf606f872003-03-16 03:11:04 +0000947 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000948 case POP_TOP:
949 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000950 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000951 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000952
Guido van Rossum374a9221991-04-04 10:40:29 +0000953 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000954 v = TOP();
955 w = SECOND();
956 SET_TOP(w);
957 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000958 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000959
Guido van Rossum374a9221991-04-04 10:40:29 +0000960 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000961 v = TOP();
962 w = SECOND();
963 x = THIRD();
964 SET_TOP(w);
965 SET_SECOND(x);
966 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000967 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000968
Thomas Wouters434d0822000-08-24 20:11:32 +0000969 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000970 u = TOP();
971 v = SECOND();
972 w = THIRD();
973 x = FOURTH();
974 SET_TOP(v);
975 SET_SECOND(w);
976 SET_THIRD(x);
977 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000978 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000979
Guido van Rossum374a9221991-04-04 10:40:29 +0000980 case DUP_TOP:
981 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000982 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000983 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000984 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000985
Thomas Wouters434d0822000-08-24 20:11:32 +0000986 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000987 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000988 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000989 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000990 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000991 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 STACKADJ(2);
993 SET_TOP(x);
994 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000995 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000996 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000997 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000998 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000999 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001000 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001001 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001002 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001003 STACKADJ(3);
1004 SET_TOP(x);
1005 SET_SECOND(w);
1006 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001007 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001008 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001009 Py_FatalError("invalid argument to DUP_TOPX"
1010 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001011 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001012
Guido van Rossum374a9221991-04-04 10:40:29 +00001013 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001014 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001015 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001016 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001017 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001018 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001019 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001020
Guido van Rossum374a9221991-04-04 10:40:29 +00001021 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001022 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001023 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001024 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001025 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001026 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001027 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001028
Guido van Rossum374a9221991-04-04 10:40:29 +00001029 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001030 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001031 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001032 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001033 if (err == 0) {
1034 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001035 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001036 continue;
1037 }
1038 else if (err > 0) {
1039 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001040 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001041 err = 0;
1042 continue;
1043 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001044 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001045 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001046
Guido van Rossum7928cd71991-10-24 14:59:31 +00001047 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001048 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001049 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001050 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001051 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001052 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001053 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001054
Guido van Rossum50564e81996-01-12 01:13:16 +00001055 case BINARY_POWER:
1056 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001057 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001058 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001059 Py_DECREF(v);
1060 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001061 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001062 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001063 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001064
Guido van Rossum374a9221991-04-04 10:40:29 +00001065 case BINARY_MULTIPLY:
1066 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001067 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001068 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001069 Py_DECREF(v);
1070 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001071 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001072 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001073 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001074
Tim Peters3caca232001-12-06 06:23:26 +00001075 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001076 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001077 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001078 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001079 Py_DECREF(v);
1080 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001081 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001082 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001083 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001084
Guido van Rossum4668b002001-08-08 05:00:18 +00001085 case BINARY_FLOOR_DIVIDE:
1086 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001087 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001088 x = PyNumber_FloorDivide(v, w);
1089 Py_DECREF(v);
1090 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001091 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001092 if (x != NULL) continue;
1093 break;
1094
Guido van Rossum374a9221991-04-04 10:40:29 +00001095 case BINARY_MODULO:
1096 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001097 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001098 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001099 Py_DECREF(v);
1100 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001101 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001102 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001103 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001104
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 case BINARY_ADD:
1106 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001107 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001108 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001109 /* INLINE: int + int */
1110 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001111 a = PyInt_AS_LONG(v);
1112 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001113 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001114 if ((i^a) < 0 && (i^b) < 0)
1115 goto slow_add;
1116 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001117 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001118 else if (PyString_CheckExact(v) &&
1119 PyString_CheckExact(w)) {
1120 x = string_concatenate(v, w, f, next_instr);
1121 /* string_concatenate consumed the ref to v */
1122 goto skip_decref_vx;
1123 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001124 else {
1125 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001126 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001127 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001128 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001129 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001130 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001131 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001132 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001133 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001134
Guido van Rossum374a9221991-04-04 10:40:29 +00001135 case BINARY_SUBTRACT:
1136 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001137 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001138 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001139 /* INLINE: int - int */
1140 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001141 a = PyInt_AS_LONG(v);
1142 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001143 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001144 if ((i^a) < 0 && (i^~b) < 0)
1145 goto slow_sub;
1146 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001147 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001148 else {
1149 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001150 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001151 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001152 Py_DECREF(v);
1153 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001154 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001155 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001156 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001157
Guido van Rossum374a9221991-04-04 10:40:29 +00001158 case BINARY_SUBSCR:
1159 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001160 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001161 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001162 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001163 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001164 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001165 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001166 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001167 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001168 Py_INCREF(x);
1169 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001170 else
1171 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001172 }
1173 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001174 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001175 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001176 Py_DECREF(v);
1177 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001178 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001179 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001180 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001181
Guido van Rossum7928cd71991-10-24 14:59:31 +00001182 case BINARY_LSHIFT:
1183 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001184 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001185 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001186 Py_DECREF(v);
1187 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001188 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001189 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001190 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001191
Guido van Rossum7928cd71991-10-24 14:59:31 +00001192 case BINARY_RSHIFT:
1193 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001194 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001195 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001196 Py_DECREF(v);
1197 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001198 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001199 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001200 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001201
Guido van Rossum7928cd71991-10-24 14:59:31 +00001202 case BINARY_AND:
1203 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001204 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001205 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001206 Py_DECREF(v);
1207 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001208 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001209 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001210 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Guido van Rossum7928cd71991-10-24 14:59:31 +00001212 case BINARY_XOR:
1213 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001214 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001215 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001216 Py_DECREF(v);
1217 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001218 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001219 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001220 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 case BINARY_OR:
1223 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001224 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001225 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001226 Py_DECREF(v);
1227 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001228 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001229 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001230 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001231
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001232 case LIST_APPEND:
1233 w = POP();
1234 v = POP();
1235 err = PyList_Append(v, w);
1236 Py_DECREF(v);
1237 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001238 if (err == 0) {
1239 PREDICT(JUMP_ABSOLUTE);
1240 continue;
1241 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001242 break;
1243
Nick Coghlan650f0d02007-04-15 12:05:43 +00001244 case SET_ADD:
1245 w = POP();
1246 v = POP();
1247 err = PySet_Add(v, w);
1248 Py_DECREF(v);
1249 Py_DECREF(w);
1250 if (err == 0) {
1251 PREDICT(JUMP_ABSOLUTE);
1252 continue;
1253 }
1254 break;
1255
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 case INPLACE_POWER:
1257 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001258 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001259 x = PyNumber_InPlacePower(v, w, Py_None);
1260 Py_DECREF(v);
1261 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 if (x != NULL) continue;
1264 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Thomas Wouters434d0822000-08-24 20:11:32 +00001266 case INPLACE_MULTIPLY:
1267 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001269 x = PyNumber_InPlaceMultiply(v, w);
1270 Py_DECREF(v);
1271 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001273 if (x != NULL) continue;
1274 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001275
Tim Peters54b11912001-12-25 18:49:11 +00001276 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001277 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001278 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001279 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001280 Py_DECREF(v);
1281 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001282 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001283 if (x != NULL) continue;
1284 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001285
Guido van Rossum4668b002001-08-08 05:00:18 +00001286 case INPLACE_FLOOR_DIVIDE:
1287 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001289 x = PyNumber_InPlaceFloorDivide(v, w);
1290 Py_DECREF(v);
1291 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001292 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001293 if (x != NULL) continue;
1294 break;
1295
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 case INPLACE_MODULO:
1297 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 x = PyNumber_InPlaceRemainder(v, w);
1300 Py_DECREF(v);
1301 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001302 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001303 if (x != NULL) continue;
1304 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001305
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 case INPLACE_ADD:
1307 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001308 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001309 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001310 /* INLINE: int + int */
1311 register long a, b, i;
1312 a = PyInt_AS_LONG(v);
1313 b = PyInt_AS_LONG(w);
1314 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001315 if ((i^a) < 0 && (i^b) < 0)
1316 goto slow_iadd;
1317 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001318 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001319 else if (PyString_CheckExact(v) &&
1320 PyString_CheckExact(w)) {
1321 x = string_concatenate(v, w, f, next_instr);
1322 /* string_concatenate consumed the ref to v */
1323 goto skip_decref_v;
1324 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001325 else {
1326 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001327 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001328 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001329 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001330 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001331 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001332 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 if (x != NULL) continue;
1334 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001335
Thomas Wouters434d0822000-08-24 20:11:32 +00001336 case INPLACE_SUBTRACT:
1337 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001339 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001340 /* INLINE: int - int */
1341 register long a, b, i;
1342 a = PyInt_AS_LONG(v);
1343 b = PyInt_AS_LONG(w);
1344 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001345 if ((i^a) < 0 && (i^~b) < 0)
1346 goto slow_isub;
1347 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001348 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001349 else {
1350 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001351 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001352 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001353 Py_DECREF(v);
1354 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001355 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001356 if (x != NULL) continue;
1357 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001358
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 case INPLACE_LSHIFT:
1360 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001362 x = PyNumber_InPlaceLshift(v, w);
1363 Py_DECREF(v);
1364 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 if (x != NULL) continue;
1367 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Thomas Wouters434d0822000-08-24 20:11:32 +00001369 case INPLACE_RSHIFT:
1370 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 x = PyNumber_InPlaceRshift(v, w);
1373 Py_DECREF(v);
1374 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001376 if (x != NULL) continue;
1377 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Thomas Wouters434d0822000-08-24 20:11:32 +00001379 case INPLACE_AND:
1380 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 x = PyNumber_InPlaceAnd(v, w);
1383 Py_DECREF(v);
1384 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001385 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001386 if (x != NULL) continue;
1387 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Thomas Wouters434d0822000-08-24 20:11:32 +00001389 case INPLACE_XOR:
1390 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001391 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 x = PyNumber_InPlaceXor(v, w);
1393 Py_DECREF(v);
1394 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001396 if (x != NULL) continue;
1397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Thomas Wouters434d0822000-08-24 20:11:32 +00001399 case INPLACE_OR:
1400 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001401 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001402 x = PyNumber_InPlaceOr(v, w);
1403 Py_DECREF(v);
1404 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001405 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001406 if (x != NULL) continue;
1407 break;
1408
Guido van Rossum374a9221991-04-04 10:40:29 +00001409 case SLICE+0:
1410 case SLICE+1:
1411 case SLICE+2:
1412 case SLICE+3:
1413 if ((opcode-SLICE) & 2)
1414 w = POP();
1415 else
1416 w = NULL;
1417 if ((opcode-SLICE) & 1)
1418 v = POP();
1419 else
1420 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001421 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001422 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001423 Py_DECREF(u);
1424 Py_XDECREF(v);
1425 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001426 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001427 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001428 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001429
Guido van Rossum374a9221991-04-04 10:40:29 +00001430 case STORE_SLICE+0:
1431 case STORE_SLICE+1:
1432 case STORE_SLICE+2:
1433 case STORE_SLICE+3:
1434 if ((opcode-STORE_SLICE) & 2)
1435 w = POP();
1436 else
1437 w = NULL;
1438 if ((opcode-STORE_SLICE) & 1)
1439 v = POP();
1440 else
1441 v = NULL;
1442 u = POP();
1443 t = POP();
1444 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001445 Py_DECREF(t);
1446 Py_DECREF(u);
1447 Py_XDECREF(v);
1448 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001449 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001450 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001451
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 case DELETE_SLICE+0:
1453 case DELETE_SLICE+1:
1454 case DELETE_SLICE+2:
1455 case DELETE_SLICE+3:
1456 if ((opcode-DELETE_SLICE) & 2)
1457 w = POP();
1458 else
1459 w = NULL;
1460 if ((opcode-DELETE_SLICE) & 1)
1461 v = POP();
1462 else
1463 v = NULL;
1464 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001465 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001466 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001467 Py_DECREF(u);
1468 Py_XDECREF(v);
1469 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001470 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001471 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001474 w = TOP();
1475 v = SECOND();
1476 u = THIRD();
1477 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001479 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001480 Py_DECREF(u);
1481 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;
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001487 w = TOP();
1488 v = SECOND();
1489 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001490 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001491 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001492 Py_DECREF(v);
1493 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001494 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001495 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001496
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 case PRINT_EXPR:
1498 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001499 w = PySys_GetObject("displayhook");
1500 if (w == NULL) {
1501 PyErr_SetString(PyExc_RuntimeError,
1502 "lost sys.displayhook");
1503 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001504 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001505 }
1506 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001507 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001508 if (x == NULL)
1509 err = -1;
1510 }
1511 if (err == 0) {
1512 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001513 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001514 if (w == NULL)
1515 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001516 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001517 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001518 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001519 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001520
Thomas Wouters434d0822000-08-24 20:11:32 +00001521#ifdef CASE_TOO_BIG
1522 default: switch (opcode) {
1523#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001524 case RAISE_VARARGS:
1525 u = v = w = NULL;
1526 switch (oparg) {
1527 case 3:
1528 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001529 /* Fallthrough */
1530 case 2:
1531 v = POP(); /* value */
1532 /* Fallthrough */
1533 case 1:
1534 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001535 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001536 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001537 break;
1538 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001539 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001540 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001541 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001542 break;
1543 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001544 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001545
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001546 case STORE_LOCALS:
1547 x = POP();
1548 v = f->f_locals;
1549 Py_XDECREF(v);
1550 f->f_locals = x;
1551 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001552
Guido van Rossum374a9221991-04-04 10:40:29 +00001553 case RETURN_VALUE:
1554 retval = POP();
1555 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001556 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001557
Tim Peters5ca576e2001-06-18 22:08:13 +00001558 case YIELD_VALUE:
1559 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001560 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001561 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001562 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001563
Guido van Rossum374a9221991-04-04 10:40:29 +00001564 case POP_BLOCK:
1565 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001566 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001567 while (STACK_LEVEL() > b->b_level) {
1568 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001569 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001570 }
1571 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001572 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001573
Guido van Rossum374a9221991-04-04 10:40:29 +00001574 case END_FINALLY:
1575 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001576 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001577 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001578 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001579 if (why == WHY_RETURN ||
1580 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001581 retval = POP();
1582 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001583 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001584 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001585 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001586 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001587 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001588 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001589 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001590 else if (v != Py_None) {
1591 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001592 "'finally' pops bad exception");
1593 why = WHY_EXCEPTION;
1594 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001595 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001596 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001598 case LOAD_BUILD_CLASS:
1599 x = PyDict_GetItemString(f->f_builtins,
1600 "__build_class__");
1601 if (x == NULL) {
1602 PyErr_SetString(PyExc_ImportError,
1603 "__build_class__ not found");
1604 break;
1605 }
1606 Py_INCREF(x);
1607 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001608 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001609
Guido van Rossum374a9221991-04-04 10:40:29 +00001610 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001611 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001612 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001613 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001614 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001615 err = PyDict_SetItem(x, w, v);
1616 else
1617 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001618 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001619 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001620 break;
1621 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001622 PyErr_Format(PyExc_SystemError,
1623 "no locals found when storing %s",
1624 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001625 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001626
Guido van Rossum374a9221991-04-04 10:40:29 +00001627 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001628 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001629 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001630 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001631 format_exc_check_arg(PyExc_NameError,
1632 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001633 break;
1634 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001635 PyErr_Format(PyExc_SystemError,
1636 "no locals when deleting %s",
1637 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001638 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001639
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001640 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001641 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001642 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001643 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1644 PyObject **items = ((PyTupleObject *)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 Hettinger7eddd782004-04-07 14:38:08 +00001650 Py_DECREF(v);
1651 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001652 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1653 PyObject **items = ((PyListObject *)v)->ob_item;
1654 while (oparg--) {
1655 w = items[oparg];
1656 Py_INCREF(w);
1657 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001658 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001659 } else if (unpack_iterable(v, oparg,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001660 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001661 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001662 } else {
1663 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001664 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001665 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001666 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001667 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001668
Guido van Rossum374a9221991-04-04 10:40:29 +00001669 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001670 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001671 v = TOP();
1672 u = SECOND();
1673 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001674 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1675 Py_DECREF(v);
1676 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001677 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001678 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001679
Guido van Rossum374a9221991-04-04 10:40:29 +00001680 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001681 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001682 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001683 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1684 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001685 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001687
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001688 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001689 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001690 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001691 err = PyDict_SetItem(f->f_globals, w, v);
1692 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001693 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001694 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001695
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001696 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001697 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001698 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001699 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001700 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001701 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Guido van Rossum374a9221991-04-04 10:40:29 +00001703 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001704 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001705 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001706 PyErr_Format(PyExc_SystemError,
1707 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001708 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001709 break;
1710 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001711 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001712 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001713 Py_XINCREF(x);
1714 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001715 else {
1716 x = PyObject_GetItem(v, w);
1717 if (x == NULL && PyErr_Occurred()) {
1718 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1719 break;
1720 PyErr_Clear();
1721 }
1722 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001724 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001726 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001727 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001728 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001729 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001730 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001731 break;
1732 }
1733 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001734 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001735 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001736 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001737 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001738
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001740 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001741 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001742 /* Inline the PyDict_GetItem() calls.
1743 WARNING: this is an extreme speed hack.
1744 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001745 long hash = ((PyStringObject *)w)->ob_shash;
1746 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001747 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001748 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001749 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001750 e = d->ma_lookup(d, w, hash);
1751 if (e == NULL) {
1752 x = NULL;
1753 break;
1754 }
1755 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001756 if (x != NULL) {
1757 Py_INCREF(x);
1758 PUSH(x);
1759 continue;
1760 }
1761 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001762 e = d->ma_lookup(d, w, hash);
1763 if (e == NULL) {
1764 x = NULL;
1765 break;
1766 }
1767 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001768 if (x != NULL) {
1769 Py_INCREF(x);
1770 PUSH(x);
1771 continue;
1772 }
1773 goto load_global_error;
1774 }
1775 }
1776 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001777 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001778 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001779 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001780 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001781 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001782 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001783 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001784 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 break;
1786 }
1787 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001788 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001789 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001790 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001791
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001792 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001793 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001794 if (x != NULL) {
1795 SETLOCAL(oparg, NULL);
1796 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001797 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001798 format_exc_check_arg(
1799 PyExc_UnboundLocalError,
1800 UNBOUNDLOCAL_ERROR_MSG,
1801 PyTuple_GetItem(co->co_varnames, oparg)
1802 );
1803 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001804
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001805 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001806 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001807 Py_INCREF(x);
1808 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001809 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001810 break;
1811
1812 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001813 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001814 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001815 if (w != NULL) {
1816 PUSH(w);
1817 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001818 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001819 err = -1;
1820 /* Don't stomp existing exception */
1821 if (PyErr_Occurred())
1822 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001823 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1824 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001825 oparg);
1826 format_exc_check_arg(
1827 PyExc_UnboundLocalError,
1828 UNBOUNDLOCAL_ERROR_MSG,
1829 v);
1830 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001831 v = PyTuple_GET_ITEM(
Raymond Hettinger467a6982004-04-07 11:39:21 +00001832 co->co_freevars,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001833 oparg - PyTuple_GET_SIZE(co->co_cellvars));
Raymond Hettinger467a6982004-04-07 11:39:21 +00001834 format_exc_check_arg(
1835 PyExc_NameError,
1836 UNBOUNDFREE_ERROR_MSG,
1837 v);
1838 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001839 break;
1840
1841 case STORE_DEREF:
1842 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001843 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001844 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001845 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001846 continue;
1847
Guido van Rossum374a9221991-04-04 10:40:29 +00001848 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001849 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001851 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001853 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 }
1855 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001856 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 }
1858 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001859
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001861 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001863 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001864 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001865 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 }
1867 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001868 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001869 }
1870 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001871
Guido van Rossum86e58e22006-08-28 15:27:34 +00001872 case BUILD_SET:
1873 x = PySet_New(NULL);
1874 if (x != NULL) {
1875 for (; --oparg >= 0;) {
1876 w = POP();
1877 if (err == 0)
1878 err = PySet_Add(x, w);
1879 Py_DECREF(w);
1880 }
1881 if (err != 0) {
1882 Py_DECREF(x);
1883 break;
1884 }
1885 PUSH(x);
1886 continue;
1887 }
1888 break;
1889
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001891 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001893 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001894 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001895
1896 case MAKE_BYTES:
1897 w = POP();
1898 if (PyString_Check(w))
1899 x = PyBytes_FromStringAndSize(
1900 PyString_AS_STRING(w),
1901 PyString_GET_SIZE(w));
1902 else
1903 x = NULL;
1904 Py_DECREF(w);
1905 PUSH(x);
1906 if (x != NULL) continue;
1907 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001908
Guido van Rossum374a9221991-04-04 10:40:29 +00001909 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001910 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001911 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001912 x = PyObject_GetAttr(v, w);
1913 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001914 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001915 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001917
Guido van Rossum374a9221991-04-04 10:40:29 +00001918 case COMPARE_OP:
1919 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001920 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001921 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001922 /* INLINE: cmp(int, int) */
1923 register long a, b;
1924 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001925 a = PyInt_AS_LONG(v);
1926 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001927 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001928 case PyCmp_LT: res = a < b; break;
1929 case PyCmp_LE: res = a <= b; break;
1930 case PyCmp_EQ: res = a == b; break;
1931 case PyCmp_NE: res = a != b; break;
1932 case PyCmp_GT: res = a > b; break;
1933 case PyCmp_GE: res = a >= b; break;
1934 case PyCmp_IS: res = v == w; break;
1935 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001936 default: goto slow_compare;
1937 }
1938 x = res ? Py_True : Py_False;
1939 Py_INCREF(x);
1940 }
1941 else {
1942 slow_compare:
1943 x = cmp_outcome(oparg, v, w);
1944 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001945 Py_DECREF(v);
1946 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001947 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001948 if (x == NULL) break;
1949 PREDICT(JUMP_IF_FALSE);
1950 PREDICT(JUMP_IF_TRUE);
1951 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001952
Guido van Rossum374a9221991-04-04 10:40:29 +00001953 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001954 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001955 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001956 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001958 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001959 break;
1960 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001961 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001962 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001963 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
1964 w = PyTuple_Pack(5,
1965 w,
1966 f->f_globals,
1967 f->f_locals == NULL ?
1968 Py_None : f->f_locals,
1969 v,
1970 u);
1971 else
1972 w = PyTuple_Pack(4,
1973 w,
1974 f->f_globals,
1975 f->f_locals == NULL ?
1976 Py_None : f->f_locals,
1977 v);
1978 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001979 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001980 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001981 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001982 x = NULL;
1983 break;
1984 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001985 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00001986 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001987 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001988 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001989 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001990 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001991 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001992
Thomas Wouters52152252000-08-17 22:55:00 +00001993 case IMPORT_STAR:
1994 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001995 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001996 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00001997 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001998 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00001999 break;
2000 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002001 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002002 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002003 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002004 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002005 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002006 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002007 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002008
Thomas Wouters52152252000-08-17 22:55:00 +00002009 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002010 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002011 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002012 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002013 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002014 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002015 PUSH(x);
2016 if (x != NULL) continue;
2017 break;
2018
Guido van Rossum374a9221991-04-04 10:40:29 +00002019 case JUMP_FORWARD:
2020 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002021 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Raymond Hettingerf606f872003-03-16 03:11:04 +00002023 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002024 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002025 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002026 if (w == Py_True) {
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_False) {
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;
2037 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002038 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002039 else
2040 break;
2041 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002042
Raymond Hettingerf606f872003-03-16 03:11:04 +00002043 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002044 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002045 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002046 if (w == Py_False) {
2047 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002048 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002049 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002050 if (w == Py_True) {
2051 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002052 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002053 }
2054 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002055 if (err > 0) {
2056 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002057 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002058 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002059 else if (err == 0)
2060 ;
2061 else
2062 break;
2063 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002064
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002065 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002066 case JUMP_ABSOLUTE:
2067 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002068 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002069
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002070 case GET_ITER:
2071 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002072 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002073 x = PyObject_GetIter(v);
2074 Py_DECREF(v);
2075 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002076 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002077 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002078 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002079 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002080 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002081 break;
2082
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002083 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084 case FOR_ITER:
2085 /* before: [iter]; after: [iter, iter()] *or* [] */
2086 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002087 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002088 if (x != NULL) {
2089 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002090 PREDICT(STORE_FAST);
2091 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002092 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002093 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002094 if (PyErr_Occurred()) {
2095 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2096 break;
2097 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002098 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002099 /* iterator ended normally */
2100 x = v = POP();
2101 Py_DECREF(v);
2102 JUMPBY(oparg);
2103 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002104
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002105 case BREAK_LOOP:
2106 why = WHY_BREAK;
2107 goto fast_block_end;
2108
2109 case CONTINUE_LOOP:
2110 retval = PyInt_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002111 if (!retval) {
2112 x = NULL;
2113 break;
2114 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002115 why = WHY_CONTINUE;
2116 goto fast_block_end;
2117
Guido van Rossum374a9221991-04-04 10:40:29 +00002118 case SETUP_LOOP:
2119 case SETUP_EXCEPT:
2120 case SETUP_FINALLY:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00002121 /* NOTE: If you add any new block-setup opcodes that are
2122 not try/except/finally handlers, you may need to
2123 update the PyGen_NeedsFinalizing() function. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002124
Guido van Rossumb209a111997-04-29 18:18:01 +00002125 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002126 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002127 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002128
Guido van Rossumc2e20742006-02-27 22:32:47 +00002129 case WITH_CLEANUP:
2130 {
2131 /* TOP is the context.__exit__ bound method.
2132 Below that are 1-3 values indicating how/why
2133 we entered the finally clause:
2134 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002135 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002136 - SECOND = WHY_*; no retval below it
2137 - (SECOND, THIRD, FOURTH) = exc_info()
2138 In the last case, we must call
2139 TOP(SECOND, THIRD, FOURTH)
2140 otherwise we must call
2141 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002142
2143 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002144 *and* the function call returns a 'true' value, we
2145 "zap" this information, to prevent END_FINALLY from
2146 re-raising the exception. (But non-local gotos
2147 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002148 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002149
Guido van Rossumc2e20742006-02-27 22:32:47 +00002150 x = TOP();
2151 u = SECOND();
2152 if (PyInt_Check(u) || u == Py_None) {
2153 u = v = w = Py_None;
2154 }
2155 else {
2156 v = THIRD();
2157 w = FOURTH();
2158 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002159 /* XXX Not the fastest way to call it... */
2160 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2161 if (x == NULL)
2162 break; /* Go to error exit */
2163 if (u != Py_None && PyObject_IsTrue(x)) {
2164 /* There was an exception and a true return */
2165 Py_DECREF(x);
2166 x = TOP(); /* Again */
2167 STACKADJ(-3);
2168 Py_INCREF(Py_None);
2169 SET_TOP(Py_None);
2170 Py_DECREF(x);
2171 Py_DECREF(u);
2172 Py_DECREF(v);
2173 Py_DECREF(w);
2174 } else {
2175 /* Let END_FINALLY do its thing */
2176 Py_DECREF(x);
2177 x = POP();
2178 Py_DECREF(x);
2179 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002180 break;
2181 }
2182
Guido van Rossumf10570b1995-07-07 22:53:21 +00002183 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002184 {
2185 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002186 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002187 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002188#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002189 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002190#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002191 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002192#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002193 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002194 PUSH(x);
2195 if (x != NULL)
2196 continue;
2197 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002198 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002199
Jeremy Hylton76901512000-03-28 23:49:17 +00002200 case CALL_FUNCTION_VAR:
2201 case CALL_FUNCTION_KW:
2202 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002203 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002204 int na = oparg & 0xff;
2205 int nk = (oparg>>8) & 0xff;
2206 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002207 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002208 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002209 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002210 if (flags & CALL_FLAG_VAR)
2211 n++;
2212 if (flags & CALL_FLAG_KW)
2213 n++;
2214 pfunc = stack_pointer - n - 1;
2215 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002216
Guido van Rossumac7be682001-01-17 15:42:30 +00002217 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002218 && PyMethod_GET_SELF(func) != NULL) {
2219 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002220 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002221 func = PyMethod_GET_FUNCTION(func);
2222 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002223 Py_DECREF(*pfunc);
2224 *pfunc = self;
2225 na++;
2226 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002227 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002228 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002229 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002230 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002231 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002232 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002233 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002234 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002235
Jeremy Hylton76901512000-03-28 23:49:17 +00002236 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002237 w = POP();
2238 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002239 }
2240 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002241 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002242 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002243 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002244 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002245
Guido van Rossum0240b922007-02-26 21:23:50 +00002246 case MAKE_CLOSURE:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002247 case MAKE_FUNCTION:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002248 {
2249 int posdefaults = oparg & 0xff;
2250 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002251 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002252
Guido van Rossum681d79a1995-07-18 14:51:37 +00002253 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002254 x = PyFunction_New(v, f->f_globals);
2255 Py_DECREF(v);
Guido van Rossum0240b922007-02-26 21:23:50 +00002256
2257 if (x != NULL && opcode == MAKE_CLOSURE) {
2258 v = POP();
2259 err = PyFunction_SetClosure(x, v);
2260 Py_DECREF(v);
2261 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002262
2263 if (x != NULL && num_annotations > 0) {
2264 Py_ssize_t name_ix;
2265 u = POP(); /* names of args with annotations */
2266 v = PyDict_New();
2267 if (v == NULL) {
2268 Py_DECREF(x);
2269 x = NULL;
2270 break;
2271 }
2272 name_ix = PyTuple_Size(u);
2273 assert(num_annotations == name_ix+1);
2274 while (name_ix > 0) {
2275 --name_ix;
2276 t = PyTuple_GET_ITEM(u, name_ix);
2277 w = POP();
2278 /* XXX(nnorwitz): check for errors */
2279 PyDict_SetItem(v, t, w);
2280 Py_DECREF(w);
2281 }
2282
2283 err = PyFunction_SetAnnotations(x, v);
2284 Py_DECREF(v);
2285 Py_DECREF(u);
2286 }
2287
Guido van Rossum681d79a1995-07-18 14:51:37 +00002288 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002289 if (x != NULL && posdefaults > 0) {
2290 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002291 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002292 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002293 x = NULL;
2294 break;
2295 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002296 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002297 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002298 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002299 }
2300 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002301 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002302 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002303 if (x != NULL && kwdefaults > 0) {
2304 v = PyDict_New();
2305 if (v == NULL) {
2306 Py_DECREF(x);
2307 x = NULL;
2308 break;
2309 }
2310 while (--kwdefaults >= 0) {
2311 w = POP(); /* default value */
2312 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002313 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002314 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002315 Py_DECREF(w);
2316 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002317 }
2318 err = PyFunction_SetKwDefaults(x, v);
2319 Py_DECREF(v);
2320 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002321 PUSH(x);
2322 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002323 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002324
2325 case BUILD_SLICE:
2326 if (oparg == 3)
2327 w = POP();
2328 else
2329 w = NULL;
2330 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002331 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002332 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002333 Py_DECREF(u);
2334 Py_DECREF(v);
2335 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002336 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002337 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002338 break;
2339
Fred Drakeef8ace32000-08-24 00:32:09 +00002340 case EXTENDED_ARG:
2341 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002342 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002343 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002344
Guido van Rossum374a9221991-04-04 10:40:29 +00002345 default:
2346 fprintf(stderr,
2347 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002348 PyCode_Addr2Line(f->f_code, f->f_lasti),
2349 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002350 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 why = WHY_EXCEPTION;
2352 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002353
2354#ifdef CASE_TOO_BIG
2355 }
2356#endif
2357
Guido van Rossum374a9221991-04-04 10:40:29 +00002358 } /* switch */
2359
2360 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002361
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002362 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002363
Guido van Rossum374a9221991-04-04 10:40:29 +00002364 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Guido van Rossum374a9221991-04-04 10:40:29 +00002366 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002367 if (err == 0 && x != NULL) {
2368#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002369 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002370 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002371 fprintf(stderr,
2372 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002373 else {
2374#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002375 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002376 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002377#ifdef CHECKEXC
2378 }
2379#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002380 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002381 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002382 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002383 err = 0;
2384 }
2385
Guido van Rossum374a9221991-04-04 10:40:29 +00002386 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002387
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002388 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002389 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002390 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002391 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002392 why = WHY_EXCEPTION;
2393 }
2394 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002395#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002396 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002397 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002398 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002399 char buf[1024];
2400 sprintf(buf, "Stack unwind with exception "
2401 "set and why=%d", why);
2402 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002403 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002404 }
2405#endif
2406
2407 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002408
Guido van Rossum374a9221991-04-04 10:40:29 +00002409 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002410 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002411
Fred Drake8f51f542001-10-04 14:48:42 +00002412 if (tstate->c_tracefunc != NULL)
2413 call_exc_trace(tstate->c_tracefunc,
2414 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002415 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002416
Guido van Rossum374a9221991-04-04 10:40:29 +00002417 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002418
Guido van Rossum374a9221991-04-04 10:40:29 +00002419 if (why == WHY_RERAISE)
2420 why = WHY_EXCEPTION;
2421
2422 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002423
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002424fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002425 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002426 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002427
Tim Peters8a5c3c72004-04-05 19:36:21 +00002428 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002429 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2430 /* For a continue inside a try block,
2431 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002432 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2433 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002434 why = WHY_NOT;
2435 JUMPTO(PyInt_AS_LONG(retval));
2436 Py_DECREF(retval);
2437 break;
2438 }
2439
Guido van Rossum374a9221991-04-04 10:40:29 +00002440 while (STACK_LEVEL() > b->b_level) {
2441 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002442 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002443 }
2444 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2445 why = WHY_NOT;
2446 JUMPTO(b->b_handler);
2447 break;
2448 }
2449 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002450 (b->b_type == SETUP_EXCEPT &&
2451 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002452 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002453 PyObject *exc, *val, *tb;
2454 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002455 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002456 val = Py_None;
2457 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002458 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002459 /* Make the raw exception data
2460 available to the handler,
2461 so a program can emulate the
2462 Python main loop. Don't do
2463 this for 'finally'. */
2464 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002465 PyErr_NormalizeException(
2466 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002467 set_exc_info(tstate,
2468 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002469 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002470 if (tb == NULL) {
2471 Py_INCREF(Py_None);
2472 PUSH(Py_None);
2473 } else
2474 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002475 PUSH(val);
2476 PUSH(exc);
2477 }
2478 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002479 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002480 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002481 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002482 PUSH(v);
2483 }
2484 why = WHY_NOT;
2485 JUMPTO(b->b_handler);
2486 break;
2487 }
2488 } /* unwind stack */
2489
2490 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002491
Guido van Rossum374a9221991-04-04 10:40:29 +00002492 if (why != WHY_NOT)
2493 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002494 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002495
Guido van Rossum374a9221991-04-04 10:40:29 +00002496 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002497
Tim Peters8a5c3c72004-04-05 19:36:21 +00002498 assert(why != WHY_YIELD);
2499 /* Pop remaining stack entries. */
2500 while (!EMPTY()) {
2501 v = POP();
2502 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002503 }
2504
Tim Peters8a5c3c72004-04-05 19:36:21 +00002505 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002506 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002507
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002508fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002509 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002510 if (tstate->c_tracefunc) {
2511 if (why == WHY_RETURN || why == WHY_YIELD) {
2512 if (call_trace(tstate->c_tracefunc,
2513 tstate->c_traceobj, f,
2514 PyTrace_RETURN, retval)) {
2515 Py_XDECREF(retval);
2516 retval = NULL;
2517 why = WHY_EXCEPTION;
2518 }
2519 }
2520 else if (why == WHY_EXCEPTION) {
2521 call_trace_protected(tstate->c_tracefunc,
2522 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002523 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002524 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002525 }
Fred Drake8f51f542001-10-04 14:48:42 +00002526 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002527 if (why == WHY_EXCEPTION)
2528 call_trace_protected(tstate->c_profilefunc,
2529 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002530 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002531 else if (call_trace(tstate->c_profilefunc,
2532 tstate->c_profileobj, f,
2533 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002534 Py_XDECREF(retval);
2535 retval = NULL;
2536 why = WHY_EXCEPTION;
2537 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002538 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002539 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002540
Thomas Wouters477c8d52006-05-27 19:21:47 +00002541 if (tstate->frame->f_exc_type != NULL)
2542 reset_exc_info(tstate);
2543 else {
2544 assert(tstate->frame->f_exc_value == NULL);
2545 assert(tstate->frame->f_exc_traceback == NULL);
2546 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002547
Tim Peters5ca576e2001-06-18 22:08:13 +00002548 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002549 exit_eval_frame:
2550 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002551 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002552
Guido van Rossum96a42c81992-01-12 02:29:51 +00002553 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002554}
2555
Guido van Rossumc2e20742006-02-27 22:32:47 +00002556/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002557 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002558 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002559
Tim Peters6d6c1a32001-08-02 04:15:00 +00002560PyObject *
2561PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002562 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002563 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002564{
2565 register PyFrameObject *f;
2566 register PyObject *retval = NULL;
2567 register PyObject **fastlocals, **freevars;
2568 PyThreadState *tstate = PyThreadState_GET();
2569 PyObject *x, *u;
2570
2571 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002572 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002573 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002574 return NULL;
2575 }
2576
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002577 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002578 assert(globals != NULL);
2579 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002580 if (f == NULL)
2581 return NULL;
2582
2583 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002584 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002585
2586 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002587 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002588 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2589 int i;
2590 int n = argcount;
2591 PyObject *kwdict = NULL;
2592 if (co->co_flags & CO_VARKEYWORDS) {
2593 kwdict = PyDict_New();
2594 if (kwdict == NULL)
2595 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002596 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002597 if (co->co_flags & CO_VARARGS)
2598 i++;
2599 SETLOCAL(i, kwdict);
2600 }
2601 if (argcount > co->co_argcount) {
2602 if (!(co->co_flags & CO_VARARGS)) {
2603 PyErr_Format(PyExc_TypeError,
2604 "%.200s() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002605 "%spositional argument%s (%d given)",
Tim Peters5ca576e2001-06-18 22:08:13 +00002606 PyString_AsString(co->co_name),
2607 defcount ? "at most" : "exactly",
2608 co->co_argcount,
2609 kwcount ? "non-keyword " : "",
2610 co->co_argcount == 1 ? "" : "s",
2611 argcount);
2612 goto fail;
2613 }
2614 n = co->co_argcount;
2615 }
2616 for (i = 0; i < n; i++) {
2617 x = args[i];
2618 Py_INCREF(x);
2619 SETLOCAL(i, x);
2620 }
2621 if (co->co_flags & CO_VARARGS) {
2622 u = PyTuple_New(argcount - n);
2623 if (u == NULL)
2624 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002625 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002626 for (i = n; i < argcount; i++) {
2627 x = args[i];
2628 Py_INCREF(x);
2629 PyTuple_SET_ITEM(u, i-n, x);
2630 }
2631 }
2632 for (i = 0; i < kwcount; i++) {
2633 PyObject *keyword = kws[2*i];
2634 PyObject *value = kws[2*i + 1];
2635 int j;
2636 if (keyword == NULL || !PyString_Check(keyword)) {
2637 PyErr_Format(PyExc_TypeError,
2638 "%.200s() keywords must be strings",
2639 PyString_AsString(co->co_name));
2640 goto fail;
2641 }
2642 /* XXX slow -- speed up using dictionary? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002643 for (j = 0;
2644 j < co->co_argcount + co->co_kwonlyargcount;
2645 j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002646 PyObject *nm = PyTuple_GET_ITEM(
2647 co->co_varnames, j);
2648 int cmp = PyObject_RichCompareBool(
2649 keyword, nm, Py_EQ);
2650 if (cmp > 0)
2651 break;
2652 else if (cmp < 0)
2653 goto fail;
2654 }
2655 /* Check errors from Compare */
2656 if (PyErr_Occurred())
2657 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002658 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002659 if (kwdict == NULL) {
2660 PyErr_Format(PyExc_TypeError,
2661 "%.200s() got an unexpected "
2662 "keyword argument '%.400s'",
2663 PyString_AsString(co->co_name),
2664 PyString_AsString(keyword));
2665 goto fail;
2666 }
2667 PyDict_SetItem(kwdict, keyword, value);
2668 }
2669 else {
2670 if (GETLOCAL(j) != NULL) {
2671 PyErr_Format(PyExc_TypeError,
2672 "%.200s() got multiple "
2673 "values for keyword "
2674 "argument '%.400s'",
2675 PyString_AsString(co->co_name),
2676 PyString_AsString(keyword));
2677 goto fail;
2678 }
2679 Py_INCREF(value);
2680 SETLOCAL(j, value);
2681 }
2682 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002683 if (co->co_kwonlyargcount > 0) {
2684 for (i = co->co_argcount;
2685 i < co->co_argcount + co->co_kwonlyargcount;
2686 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00002687 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002688 if (GETLOCAL(i) != NULL)
2689 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00002690 name = PyTuple_GET_ITEM(co->co_varnames, i);
2691 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002692 if (kwdefs != NULL)
2693 def = PyDict_GetItem(kwdefs, name);
2694 if (def != NULL) {
2695 Py_INCREF(def);
2696 SETLOCAL(i, def);
2697 continue;
2698 }
2699 PyErr_Format(PyExc_TypeError,
2700 "%.200s() needs "
Brett Cannone33a6112007-01-29 23:43:38 +00002701 "keyword-only argument %s",
Guido van Rossum4f72a782006-10-27 23:31:49 +00002702 PyString_AsString(co->co_name),
2703 PyString_AsString(name));
2704 goto fail;
2705 }
2706 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002707 if (argcount < co->co_argcount) {
2708 int m = co->co_argcount - defcount;
2709 for (i = argcount; i < m; i++) {
2710 if (GETLOCAL(i) == NULL) {
2711 PyErr_Format(PyExc_TypeError,
2712 "%.200s() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002713 "%spositional argument%s "
2714 "(%d given)",
Tim Peters5ca576e2001-06-18 22:08:13 +00002715 PyString_AsString(co->co_name),
2716 ((co->co_flags & CO_VARARGS) ||
2717 defcount) ? "at least"
2718 : "exactly",
2719 m, kwcount ? "non-keyword " : "",
2720 m == 1 ? "" : "s", i);
2721 goto fail;
2722 }
2723 }
2724 if (n > m)
2725 i = n - m;
2726 else
2727 i = 0;
2728 for (; i < defcount; i++) {
2729 if (GETLOCAL(m+i) == NULL) {
2730 PyObject *def = defs[i];
2731 Py_INCREF(def);
2732 SETLOCAL(m+i, def);
2733 }
2734 }
2735 }
2736 }
2737 else {
2738 if (argcount > 0 || kwcount > 0) {
2739 PyErr_Format(PyExc_TypeError,
2740 "%.200s() takes no arguments (%d given)",
2741 PyString_AsString(co->co_name),
2742 argcount + kwcount);
2743 goto fail;
2744 }
2745 }
2746 /* Allocate and initialize storage for cell vars, and copy free
2747 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002748 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002749 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002750 char *cellname, *argname;
2751 PyObject *c;
2752
2753 nargs = co->co_argcount;
2754 if (co->co_flags & CO_VARARGS)
2755 nargs++;
2756 if (co->co_flags & CO_VARKEYWORDS)
2757 nargs++;
2758
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 /* Initialize each cell var, taking into account
2760 cell vars that are initialized from arguments.
2761
2762 Should arrange for the compiler to put cellvars
2763 that are arguments at the beginning of the cellvars
2764 list so that we can march over it more efficiently?
2765 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002766 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002767 cellname = PyString_AS_STRING(
2768 PyTuple_GET_ITEM(co->co_cellvars, i));
2769 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002771 argname = PyString_AS_STRING(
2772 PyTuple_GET_ITEM(co->co_varnames, j));
2773 if (strcmp(cellname, argname) == 0) {
2774 c = PyCell_New(GETLOCAL(j));
2775 if (c == NULL)
2776 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002777 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002778 found = 1;
2779 break;
2780 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002781 }
2782 if (found == 0) {
2783 c = PyCell_New(NULL);
2784 if (c == NULL)
2785 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002786 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002787 }
2788 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002789 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002790 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002791 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002792 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002793 PyObject *o = PyTuple_GET_ITEM(closure, i);
2794 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002795 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002796 }
2797 }
2798
Tim Peters5ca576e2001-06-18 22:08:13 +00002799 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002800 /* Don't need to keep the reference to f_back, it will be set
2801 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002802 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002803 f->f_back = NULL;
2804
Jeremy Hylton985eba52003-02-05 23:13:00 +00002805 PCALL(PCALL_GENERATOR);
2806
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002807 /* Create a new generator that owns the ready to run frame
2808 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002809 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002810 }
2811
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002812 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002813
2814 fail: /* Jump here from prelude on failure */
2815
Tim Petersb13680b2001-11-27 23:29:29 +00002816 /* decref'ing the frame can cause __del__ methods to get invoked,
2817 which can call back into Python. While we're done with the
2818 current Python frame (f), the associated C stack is still in use,
2819 so recursion_depth must be boosted for the duration.
2820 */
2821 assert(tstate != NULL);
2822 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002823 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002824 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002825 return retval;
2826}
2827
2828
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002829/* Implementation notes for set_exc_info() and reset_exc_info():
2830
2831- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2832 'exc_traceback'. These always travel together.
2833
2834- tstate->curexc_ZZZ is the "hot" exception that is set by
2835 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2836
2837- Once an exception is caught by an except clause, it is transferred
2838 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2839 can pick it up. This is the primary task of set_exc_info().
Thomas Wouters477c8d52006-05-27 19:21:47 +00002840 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002841
2842- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2843
2844 Long ago, when none of this existed, there were just a few globals:
2845 one set corresponding to the "hot" exception, and one set
2846 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2847 globals; they were simply stored as sys.exc_ZZZ. For backwards
2848 compatibility, they still are!) The problem was that in code like
2849 this:
2850
2851 try:
2852 "something that may fail"
2853 except "some exception":
2854 "do something else first"
2855 "print the exception from sys.exc_ZZZ."
2856
2857 if "do something else first" invoked something that raised and caught
2858 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2859 cause of subtle bugs. I fixed this by changing the semantics as
2860 follows:
2861
2862 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2863 *in that frame*.
2864
2865 - But initially, and as long as no exception is caught in a given
2866 frame, sys.exc_ZZZ will hold the last exception caught in the
2867 previous frame (or the frame before that, etc.).
2868
2869 The first bullet fixed the bug in the above example. The second
2870 bullet was for backwards compatibility: it was (and is) common to
2871 have a function that is called when an exception is caught, and to
2872 have that function access the caught exception via sys.exc_ZZZ.
2873 (Example: traceback.print_exc()).
2874
2875 At the same time I fixed the problem that sys.exc_ZZZ weren't
2876 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2877 but that's really a separate improvement.
2878
2879 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2880 variables to what they were before the current frame was called. The
2881 set_exc_info() function saves them on the frame so that
2882 reset_exc_info() can restore them. The invariant is that
2883 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2884 exception (where "catching" an exception applies only to successful
2885 except clauses); and if the current frame ever caught an exception,
2886 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2887 at the start of the current frame.
2888
2889*/
2890
Guido van Rossuma027efa1997-05-05 20:56:21 +00002891static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002892set_exc_info(PyThreadState *tstate,
2893 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002894{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002895 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002896 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002897
Thomas Wouters477c8d52006-05-27 19:21:47 +00002898 assert(type != NULL);
2899 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002900 if (frame->f_exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002901 assert(frame->f_exc_value == NULL);
2902 assert(frame->f_exc_traceback == NULL);
2903 /* This frame didn't catch an exception before. */
2904 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002905 if (tstate->exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002906 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002907 Py_INCREF(Py_None);
2908 tstate->exc_type = Py_None;
2909 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002910 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002911 Py_XINCREF(tstate->exc_value);
2912 Py_XINCREF(tstate->exc_traceback);
2913 frame->f_exc_type = tstate->exc_type;
2914 frame->f_exc_value = tstate->exc_value;
2915 frame->f_exc_traceback = tstate->exc_traceback;
2916 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002917 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002918 tmp_type = tstate->exc_type;
2919 tmp_value = tstate->exc_value;
2920 tmp_tb = tstate->exc_traceback;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002921 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002922 Py_XINCREF(value);
2923 Py_XINCREF(tb);
2924 tstate->exc_type = type;
2925 tstate->exc_value = value;
2926 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002927 Py_XDECREF(tmp_type);
2928 Py_XDECREF(tmp_value);
2929 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002930 /* For b/w compatibility */
2931 PySys_SetObject("exc_type", type);
2932 PySys_SetObject("exc_value", value);
2933 PySys_SetObject("exc_traceback", tb);
2934}
2935
2936static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002937reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002938{
2939 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002940 PyObject *tmp_type, *tmp_value, *tmp_tb;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002941
2942 /* It's a precondition that the thread state's frame caught an
2943 * exception -- verify in a debug build.
2944 */
2945 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002946 frame = tstate->frame;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002947 assert(frame != NULL);
2948 assert(frame->f_exc_type != NULL);
2949
2950 /* Copy the frame's exception info back to the thread state. */
2951 tmp_type = tstate->exc_type;
2952 tmp_value = tstate->exc_value;
2953 tmp_tb = tstate->exc_traceback;
2954 Py_INCREF(frame->f_exc_type);
2955 Py_XINCREF(frame->f_exc_value);
2956 Py_XINCREF(frame->f_exc_traceback);
2957 tstate->exc_type = frame->f_exc_type;
2958 tstate->exc_value = frame->f_exc_value;
2959 tstate->exc_traceback = frame->f_exc_traceback;
2960 Py_XDECREF(tmp_type);
2961 Py_XDECREF(tmp_value);
2962 Py_XDECREF(tmp_tb);
2963
2964 /* For b/w compatibility */
2965 PySys_SetObject("exc_type", frame->f_exc_type);
2966 PySys_SetObject("exc_value", frame->f_exc_value);
2967 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2968
2969 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002970 tmp_type = frame->f_exc_type;
2971 tmp_value = frame->f_exc_value;
2972 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002973 frame->f_exc_type = NULL;
2974 frame->f_exc_value = NULL;
2975 frame->f_exc_traceback = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002976 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002977 Py_XDECREF(tmp_value);
2978 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002979}
2980
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002981/* Logic for the raise statement (too complicated for inlining).
2982 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002983static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002984do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002985{
Guido van Rossumd295f121998-04-09 21:39:57 +00002986 if (type == NULL) {
2987 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00002988 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00002989 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
2990 value = tstate->exc_value;
2991 tb = tstate->exc_traceback;
2992 Py_XINCREF(type);
2993 Py_XINCREF(value);
2994 Py_XINCREF(tb);
2995 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002996
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002997 /* We support the following forms of raise:
2998 raise <class>, <classinstance>
2999 raise <class>, <argument tuple>
3000 raise <class>, None
3001 raise <class>, <argument>
3002 raise <classinstance>, None
3003 raise <string>, <object>
3004 raise <string>, None
3005
3006 An omitted second argument is the same as None.
3007
3008 In addition, raise <tuple>, <anything> is the same as
3009 raising the tuple's first item (and it better have one!);
3010 this rule is applied recursively.
3011
3012 Finally, an optional third argument can be supplied, which
3013 gives the traceback to be substituted (useful when
3014 re-raising an exception after examining it). */
3015
3016 /* First, check the traceback argument, replacing None with
3017 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003018 if (tb == Py_None) {
3019 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003020 tb = NULL;
3021 }
3022 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003023 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003024 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003025 goto raise_error;
3026 }
3027
3028 /* Next, replace a missing value with None */
3029 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003030 value = Py_None;
3031 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003032 }
3033
3034 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003035 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3036 PyObject *tmp = type;
3037 type = PyTuple_GET_ITEM(type, 0);
3038 Py_INCREF(type);
3039 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003040 }
3041
Guido van Rossum45aecf42006-03-15 04:58:47 +00003042 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003043 PyErr_NormalizeException(&type, &value, &tb);
3044
Brett Cannonbf364092006-03-01 04:25:17 +00003045 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003046 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003047 if (value != Py_None) {
3048 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003049 "instance exception may not have a separate value");
3050 goto raise_error;
3051 }
3052 else {
3053 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003054 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003055 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003056 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003057 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003058 }
3059 }
3060 else {
3061 /* Not something you can raise. You get an exception
3062 anyway, just not what you specified :-) */
Guido van Rossum45aecf42006-03-15 04:58:47 +00003063 PyErr_SetString(PyExc_TypeError,
3064 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003065 goto raise_error;
3066 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003067 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003068 if (tb == NULL)
3069 return WHY_EXCEPTION;
3070 else
3071 return WHY_RERAISE;
3072 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003073 Py_XDECREF(value);
3074 Py_XDECREF(type);
3075 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003076 return WHY_EXCEPTION;
3077}
3078
Tim Petersd6d010b2001-06-21 02:49:55 +00003079/* Iterate v argcnt times and store the results on the stack (via decreasing
3080 sp). Return 1 for success, 0 if error. */
3081
Barry Warsawe42b18f1997-08-25 22:13:04 +00003082static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003083unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003084{
Tim Petersd6d010b2001-06-21 02:49:55 +00003085 int i = 0;
3086 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003087 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003088
Tim Petersd6d010b2001-06-21 02:49:55 +00003089 assert(v != NULL);
3090
3091 it = PyObject_GetIter(v);
3092 if (it == NULL)
3093 goto Error;
3094
3095 for (; i < argcnt; i++) {
3096 w = PyIter_Next(it);
3097 if (w == NULL) {
3098 /* Iterator done, via error or exhaustion. */
3099 if (!PyErr_Occurred()) {
3100 PyErr_Format(PyExc_ValueError,
3101 "need more than %d value%s to unpack",
3102 i, i == 1 ? "" : "s");
3103 }
3104 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003105 }
3106 *--sp = w;
3107 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003108
3109 /* We better have exhausted the iterator now. */
3110 w = PyIter_Next(it);
3111 if (w == NULL) {
3112 if (PyErr_Occurred())
3113 goto Error;
3114 Py_DECREF(it);
3115 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003116 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003117 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003118 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003119 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003120Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003121 for (; i > 0; i--, sp++)
3122 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003123 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003124 return 0;
3125}
3126
3127
Guido van Rossum96a42c81992-01-12 02:29:51 +00003128#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003129static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003130prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003131{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003132 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003133 if (PyObject_Print(v, stdout, 0) != 0)
3134 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003135 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003136 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003137}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003138#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003139
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003140static void
Fred Drake5755ce62001-06-27 19:19:46 +00003141call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003142{
Guido van Rossumb209a111997-04-29 18:18:01 +00003143 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003144 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003145 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003146 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003147 value = Py_None;
3148 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003149 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003150 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003151 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003152 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003153 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003154 }
Fred Drake5755ce62001-06-27 19:19:46 +00003155 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003156 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003157 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003158 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003159 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003160 Py_XDECREF(type);
3161 Py_XDECREF(value);
3162 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003163 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003164}
3165
Fred Drake4ec5d562001-10-04 19:26:43 +00003166static void
3167call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003168 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003169{
3170 PyObject *type, *value, *traceback;
3171 int err;
3172 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003173 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003174 if (err == 0)
3175 PyErr_Restore(type, value, traceback);
3176 else {
3177 Py_XDECREF(type);
3178 Py_XDECREF(value);
3179 Py_XDECREF(traceback);
3180 }
3181}
3182
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003183static int
Fred Drake5755ce62001-06-27 19:19:46 +00003184call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3185 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003186{
Fred Drake5755ce62001-06-27 19:19:46 +00003187 register PyThreadState *tstate = frame->f_tstate;
3188 int result;
3189 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003190 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003191 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003192 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003193 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003194 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3195 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003196 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003197 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003198}
3199
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003200PyObject *
3201_PyEval_CallTracing(PyObject *func, PyObject *args)
3202{
3203 PyFrameObject *frame = PyEval_GetFrame();
3204 PyThreadState *tstate = frame->f_tstate;
3205 int save_tracing = tstate->tracing;
3206 int save_use_tracing = tstate->use_tracing;
3207 PyObject *result;
3208
3209 tstate->tracing = 0;
3210 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3211 || (tstate->c_profilefunc != NULL));
3212 result = PyObject_Call(func, args, NULL);
3213 tstate->tracing = save_tracing;
3214 tstate->use_tracing = save_use_tracing;
3215 return result;
3216}
3217
Michael W. Hudson006c7522002-11-08 13:08:46 +00003218static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003219maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003220 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3221 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003222{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003223 int result = 0;
3224
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003225 /* If the last instruction executed isn't in the current
3226 instruction window, reset the window. If the last
3227 instruction happens to fall at the start of a line or if it
3228 represents a jump backwards, call the trace function.
3229 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003230 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003231 int line;
3232 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003233
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003234 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3235 &bounds);
3236 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003237 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003238 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003239 PyTrace_LINE, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003240 }
3241 *instr_lb = bounds.ap_lower;
3242 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003243 }
Armin Rigobf57a142004-03-22 19:24:58 +00003244 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003245 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003246 }
3247 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003248 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003249}
3250
Fred Drake5755ce62001-06-27 19:19:46 +00003251void
3252PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003253{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003254 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003255 PyObject *temp = tstate->c_profileobj;
3256 Py_XINCREF(arg);
3257 tstate->c_profilefunc = NULL;
3258 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003259 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003260 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003261 Py_XDECREF(temp);
3262 tstate->c_profilefunc = func;
3263 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003264 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003265 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003266}
3267
3268void
3269PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3270{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003271 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003272 PyObject *temp = tstate->c_traceobj;
3273 Py_XINCREF(arg);
3274 tstate->c_tracefunc = NULL;
3275 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003276 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003277 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003278 Py_XDECREF(temp);
3279 tstate->c_tracefunc = func;
3280 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003281 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003282 tstate->use_tracing = ((func != NULL)
3283 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003284}
3285
Guido van Rossumb209a111997-04-29 18:18:01 +00003286PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003287PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003288{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003289 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003290 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003291 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003292 else
3293 return current_frame->f_builtins;
3294}
3295
Guido van Rossumb209a111997-04-29 18:18:01 +00003296PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003297PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003298{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003299 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003300 if (current_frame == NULL)
3301 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003302 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003303 return current_frame->f_locals;
3304}
3305
Guido van Rossumb209a111997-04-29 18:18:01 +00003306PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003307PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003308{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003309 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003310 if (current_frame == NULL)
3311 return NULL;
3312 else
3313 return current_frame->f_globals;
3314}
3315
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003316PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003317PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003318{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003319 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003320 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003321}
3322
Guido van Rossum6135a871995-01-09 17:53:26 +00003323int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003324PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003325{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003326 PyFrameObject *current_frame = PyEval_GetFrame();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003327 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003328}
3329
Guido van Rossumbe270261997-05-22 22:26:18 +00003330int
Tim Peters5ba58662001-07-16 02:29:45 +00003331PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003332{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003333 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003334 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003335
3336 if (current_frame != NULL) {
3337 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003338 const int compilerflags = codeflags & PyCF_MASK;
3339 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003340 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003341 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003342 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003343#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003344 if (codeflags & CO_GENERATOR_ALLOWED) {
3345 result = 1;
3346 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3347 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003348#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003349 }
3350 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003351}
3352
Guido van Rossum3f5da241990-12-20 15:06:42 +00003353
Guido van Rossum681d79a1995-07-18 14:51:37 +00003354/* External interface to call any callable object.
3355 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003356
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003357#undef PyEval_CallObject
3358/* for backward compatibility: export this interface */
3359
Guido van Rossumb209a111997-04-29 18:18:01 +00003360PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003361PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003362{
Guido van Rossumb209a111997-04-29 18:18:01 +00003363 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003364}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003365#define PyEval_CallObject(func,arg) \
3366 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003367
Guido van Rossumb209a111997-04-29 18:18:01 +00003368PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003369PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003370{
Jeremy Hylton52820442001-01-03 23:52:36 +00003371 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003372
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003373 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003374 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003375 if (arg == NULL)
3376 return NULL;
3377 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003378 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003379 PyErr_SetString(PyExc_TypeError,
3380 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003381 return NULL;
3382 }
3383 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003384 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003385
Guido van Rossumb209a111997-04-29 18:18:01 +00003386 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003387 PyErr_SetString(PyExc_TypeError,
3388 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003389 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003390 return NULL;
3391 }
3392
Tim Peters6d6c1a32001-08-02 04:15:00 +00003393 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003394 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003395 return result;
3396}
3397
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003398const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003399PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003400{
3401 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003402 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003403 else if (PyFunction_Check(func))
3404 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3405 else if (PyCFunction_Check(func))
3406 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003407 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003408 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003409}
3410
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003411const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003412PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003413{
3414 if (PyMethod_Check(func))
3415 return "()";
3416 else if (PyFunction_Check(func))
3417 return "()";
3418 else if (PyCFunction_Check(func))
3419 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003420 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003421 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003422}
3423
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003424static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003425err_args(PyObject *func, int flags, int nargs)
3426{
3427 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003428 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003429 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003430 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003431 nargs);
3432 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003433 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003434 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003435 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003436 nargs);
3437}
3438
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003439#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003440if (tstate->use_tracing && tstate->c_profilefunc) { \
3441 if (call_trace(tstate->c_profilefunc, \
3442 tstate->c_profileobj, \
3443 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003444 func)) { \
3445 x = NULL; \
3446 } \
3447 else { \
3448 x = call; \
3449 if (tstate->c_profilefunc != NULL) { \
3450 if (x == NULL) { \
3451 call_trace_protected(tstate->c_profilefunc, \
3452 tstate->c_profileobj, \
3453 tstate->frame, PyTrace_C_EXCEPTION, \
3454 func); \
3455 /* XXX should pass (type, value, tb) */ \
3456 } else { \
3457 if (call_trace(tstate->c_profilefunc, \
3458 tstate->c_profileobj, \
3459 tstate->frame, PyTrace_C_RETURN, \
3460 func)) { \
3461 Py_DECREF(x); \
3462 x = NULL; \
3463 } \
3464 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003465 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003466 } \
3467} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003468 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003469 }
3470
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003471static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003472call_function(PyObject ***pp_stack, int oparg
3473#ifdef WITH_TSC
3474 , uint64* pintr0, uint64* pintr1
3475#endif
3476 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003477{
3478 int na = oparg & 0xff;
3479 int nk = (oparg>>8) & 0xff;
3480 int n = na + 2 * nk;
3481 PyObject **pfunc = (*pp_stack) - n - 1;
3482 PyObject *func = *pfunc;
3483 PyObject *x, *w;
3484
Jeremy Hylton985eba52003-02-05 23:13:00 +00003485 /* Always dispatch PyCFunction first, because these are
3486 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003487 */
3488 if (PyCFunction_Check(func) && nk == 0) {
3489 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003490 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003491
3492 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003493 if (flags & (METH_NOARGS | METH_O)) {
3494 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3495 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003496 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003497 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003498 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003499 else if (flags & METH_O && na == 1) {
3500 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003501 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003502 Py_DECREF(arg);
3503 }
3504 else {
3505 err_args(func, flags, na);
3506 x = NULL;
3507 }
3508 }
3509 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003510 PyObject *callargs;
3511 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003512 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003513 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003514 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003515 Py_XDECREF(callargs);
3516 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003517 } else {
3518 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3519 /* optimize access to bound methods */
3520 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003521 PCALL(PCALL_METHOD);
3522 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003523 Py_INCREF(self);
3524 func = PyMethod_GET_FUNCTION(func);
3525 Py_INCREF(func);
3526 Py_DECREF(*pfunc);
3527 *pfunc = self;
3528 na++;
3529 n++;
3530 } else
3531 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003532 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003533 if (PyFunction_Check(func))
3534 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003535 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003536 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003537 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003538 Py_DECREF(func);
3539 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003540
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003541 /* Clear the stack of the function object. Also removes
3542 the arguments in case they weren't consumed already
3543 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003544 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003545 while ((*pp_stack) > pfunc) {
3546 w = EXT_POP(*pp_stack);
3547 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003548 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003549 }
3550 return x;
3551}
3552
Jeremy Hylton192690e2002-08-16 18:36:11 +00003553/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003554 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003555 For the simplest case -- a function that takes only positional
3556 arguments and is called with only positional arguments -- it
3557 inlines the most primitive frame setup code from
3558 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3559 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003560*/
3561
3562static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003563fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003564{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003565 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003566 PyObject *globals = PyFunction_GET_GLOBALS(func);
3567 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003568 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003569 PyObject **d = NULL;
3570 int nd = 0;
3571
Jeremy Hylton985eba52003-02-05 23:13:00 +00003572 PCALL(PCALL_FUNCTION);
3573 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003574 if (argdefs == NULL && co->co_argcount == n &&
3575 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003576 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3577 PyFrameObject *f;
3578 PyObject *retval = NULL;
3579 PyThreadState *tstate = PyThreadState_GET();
3580 PyObject **fastlocals, **stack;
3581 int i;
3582
3583 PCALL(PCALL_FASTER_FUNCTION);
3584 assert(globals != NULL);
3585 /* XXX Perhaps we should create a specialized
3586 PyFrame_New() that doesn't take locals, but does
3587 take builtins without sanity checking them.
3588 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003589 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003590 f = PyFrame_New(tstate, co, globals, NULL);
3591 if (f == NULL)
3592 return NULL;
3593
3594 fastlocals = f->f_localsplus;
3595 stack = (*pp_stack) - n;
3596
3597 for (i = 0; i < n; i++) {
3598 Py_INCREF(*stack);
3599 fastlocals[i] = *stack++;
3600 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003601 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003602 ++tstate->recursion_depth;
3603 Py_DECREF(f);
3604 --tstate->recursion_depth;
3605 return retval;
3606 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003607 if (argdefs != NULL) {
3608 d = &PyTuple_GET_ITEM(argdefs, 0);
3609 nd = ((PyTupleObject *)argdefs)->ob_size;
3610 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003611 return PyEval_EvalCodeEx(co, globals,
3612 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003613 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003614 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003615}
3616
3617static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003618update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3619 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003620{
3621 PyObject *kwdict = NULL;
3622 if (orig_kwdict == NULL)
3623 kwdict = PyDict_New();
3624 else {
3625 kwdict = PyDict_Copy(orig_kwdict);
3626 Py_DECREF(orig_kwdict);
3627 }
3628 if (kwdict == NULL)
3629 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003630 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003631 int err;
3632 PyObject *value = EXT_POP(*pp_stack);
3633 PyObject *key = EXT_POP(*pp_stack);
3634 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003635 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003636 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003637 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003638 PyEval_GetFuncName(func),
3639 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003640 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003641 Py_DECREF(key);
3642 Py_DECREF(value);
3643 Py_DECREF(kwdict);
3644 return NULL;
3645 }
3646 err = PyDict_SetItem(kwdict, key, value);
3647 Py_DECREF(key);
3648 Py_DECREF(value);
3649 if (err) {
3650 Py_DECREF(kwdict);
3651 return NULL;
3652 }
3653 }
3654 return kwdict;
3655}
3656
3657static PyObject *
3658update_star_args(int nstack, int nstar, PyObject *stararg,
3659 PyObject ***pp_stack)
3660{
3661 PyObject *callargs, *w;
3662
3663 callargs = PyTuple_New(nstack + nstar);
3664 if (callargs == NULL) {
3665 return NULL;
3666 }
3667 if (nstar) {
3668 int i;
3669 for (i = 0; i < nstar; i++) {
3670 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3671 Py_INCREF(a);
3672 PyTuple_SET_ITEM(callargs, nstack + i, a);
3673 }
3674 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003675 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003676 w = EXT_POP(*pp_stack);
3677 PyTuple_SET_ITEM(callargs, nstack, w);
3678 }
3679 return callargs;
3680}
3681
3682static PyObject *
3683load_args(PyObject ***pp_stack, int na)
3684{
3685 PyObject *args = PyTuple_New(na);
3686 PyObject *w;
3687
3688 if (args == NULL)
3689 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003690 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003691 w = EXT_POP(*pp_stack);
3692 PyTuple_SET_ITEM(args, na, w);
3693 }
3694 return args;
3695}
3696
3697static PyObject *
3698do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3699{
3700 PyObject *callargs = NULL;
3701 PyObject *kwdict = NULL;
3702 PyObject *result = NULL;
3703
3704 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003705 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003706 if (kwdict == NULL)
3707 goto call_fail;
3708 }
3709 callargs = load_args(pp_stack, na);
3710 if (callargs == NULL)
3711 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003712#ifdef CALL_PROFILE
3713 /* At this point, we have to look at the type of func to
3714 update the call stats properly. Do it here so as to avoid
3715 exposing the call stats machinery outside ceval.c
3716 */
3717 if (PyFunction_Check(func))
3718 PCALL(PCALL_FUNCTION);
3719 else if (PyMethod_Check(func))
3720 PCALL(PCALL_METHOD);
3721 else if (PyType_Check(func))
3722 PCALL(PCALL_TYPE);
3723 else
3724 PCALL(PCALL_OTHER);
3725#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003726 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003727 call_fail:
3728 Py_XDECREF(callargs);
3729 Py_XDECREF(kwdict);
3730 return result;
3731}
3732
3733static PyObject *
3734ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3735{
3736 int nstar = 0;
3737 PyObject *callargs = NULL;
3738 PyObject *stararg = NULL;
3739 PyObject *kwdict = NULL;
3740 PyObject *result = NULL;
3741
3742 if (flags & CALL_FLAG_KW) {
3743 kwdict = EXT_POP(*pp_stack);
3744 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003745 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003746 "%s%s argument after ** "
3747 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003748 PyEval_GetFuncName(func),
3749 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003750 goto ext_call_fail;
3751 }
3752 }
3753 if (flags & CALL_FLAG_VAR) {
3754 stararg = EXT_POP(*pp_stack);
3755 if (!PyTuple_Check(stararg)) {
3756 PyObject *t = NULL;
3757 t = PySequence_Tuple(stararg);
3758 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003759 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3760 PyErr_Format(PyExc_TypeError,
3761 "%s%s argument after * "
3762 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003763 PyEval_GetFuncName(func),
3764 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003765 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003766 goto ext_call_fail;
3767 }
3768 Py_DECREF(stararg);
3769 stararg = t;
3770 }
3771 nstar = PyTuple_GET_SIZE(stararg);
3772 }
3773 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003774 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003775 if (kwdict == NULL)
3776 goto ext_call_fail;
3777 }
3778 callargs = update_star_args(na, nstar, stararg, pp_stack);
3779 if (callargs == NULL)
3780 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003781#ifdef CALL_PROFILE
3782 /* At this point, we have to look at the type of func to
3783 update the call stats properly. Do it here so as to avoid
3784 exposing the call stats machinery outside ceval.c
3785 */
3786 if (PyFunction_Check(func))
3787 PCALL(PCALL_FUNCTION);
3788 else if (PyMethod_Check(func))
3789 PCALL(PCALL_METHOD);
3790 else if (PyType_Check(func))
3791 PCALL(PCALL_TYPE);
3792 else
3793 PCALL(PCALL_OTHER);
3794#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003795 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003796 ext_call_fail:
3797 Py_XDECREF(callargs);
3798 Py_XDECREF(kwdict);
3799 Py_XDECREF(stararg);
3800 return result;
3801}
3802
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003803/* Extract a slice index from a PyInt or PyLong or an object with the
3804 nb_index slot defined, and store in *pi.
3805 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3806 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 +00003807 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003808*/
Tim Petersb5196382001-12-16 19:44:20 +00003809/* Note: If v is NULL, return success without storing into *pi. This
3810 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3811 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003812*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003813int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003814_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003815{
Tim Petersb5196382001-12-16 19:44:20 +00003816 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003817 Py_ssize_t x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003818 if (PyInt_CheckExact(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003819 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3820 however, it looks like it should be AsSsize_t.
3821 There should be a comment here explaining why.
3822 */
3823 x = PyInt_AS_LONG(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003824 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003825 else if (PyIndex_Check(v)) {
3826 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003827 if (x == -1 && PyErr_Occurred())
3828 return 0;
3829 }
3830 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003831 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003832 "slice indices must be integers or "
3833 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003834 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003835 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003836 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003837 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003838 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003839}
3840
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003841#undef ISINDEX
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003842#define ISINDEX(x) ((x) == NULL || \
3843 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003844
Guido van Rossumb209a111997-04-29 18:18:01 +00003845static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003846apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003847{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003848 PyTypeObject *tp = u->ob_type;
3849 PySequenceMethods *sq = tp->tp_as_sequence;
3850
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003851 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003852 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003853 if (!_PyEval_SliceIndex(v, &ilow))
3854 return NULL;
3855 if (!_PyEval_SliceIndex(w, &ihigh))
3856 return NULL;
3857 return PySequence_GetSlice(u, ilow, ihigh);
3858 }
3859 else {
3860 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003861 if (slice != NULL) {
3862 PyObject *res = PyObject_GetItem(u, slice);
3863 Py_DECREF(slice);
3864 return res;
3865 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003866 else
3867 return NULL;
3868 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003869}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003870
3871static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003872assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3873 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003874{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003875 PyTypeObject *tp = u->ob_type;
3876 PySequenceMethods *sq = tp->tp_as_sequence;
3877
Guido van Rossumd8faa362007-04-27 19:54:29 +00003878 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003879 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003880 if (!_PyEval_SliceIndex(v, &ilow))
3881 return -1;
3882 if (!_PyEval_SliceIndex(w, &ihigh))
3883 return -1;
3884 if (x == NULL)
3885 return PySequence_DelSlice(u, ilow, ihigh);
3886 else
3887 return PySequence_SetSlice(u, ilow, ihigh, x);
3888 }
3889 else {
3890 PyObject *slice = PySlice_New(v, w, NULL);
3891 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003892 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003893 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003894 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003895 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003896 res = PyObject_DelItem(u, slice);
3897 Py_DECREF(slice);
3898 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003899 }
3900 else
3901 return -1;
3902 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003903}
3904
Brett Cannon39590462007-02-26 22:01:14 +00003905#define CANNOT_CATCH_MSG "catching classes that do not inherit from"\
3906 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00003907
Guido van Rossumb209a111997-04-29 18:18:01 +00003908static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003909cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003910{
Guido van Rossumac7be682001-01-17 15:42:30 +00003911 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003912 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003913 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003914 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003915 break;
3916 case PyCmp_IS_NOT:
3917 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003918 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003919 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003920 res = PySequence_Contains(w, v);
3921 if (res < 0)
3922 return NULL;
3923 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003924 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003925 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003926 if (res < 0)
3927 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003928 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003929 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003930 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003931 if (PyTuple_Check(w)) {
3932 Py_ssize_t i, length;
3933 length = PyTuple_Size(w);
3934 for (i = 0; i < length; i += 1) {
3935 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00003936 if (!PyExceptionClass_Check(exc)) {
3937 PyErr_SetString(PyExc_TypeError,
3938 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00003939 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003940 }
3941 }
3942 }
3943 else {
Brett Cannon39590462007-02-26 22:01:14 +00003944 if (!PyExceptionClass_Check(w)) {
3945 PyErr_SetString(PyExc_TypeError,
3946 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00003947 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003948 }
3949 }
Barry Warsaw4249f541997-08-22 21:26:19 +00003950 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003951 break;
3952 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003953 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003954 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003955 v = res ? Py_True : Py_False;
3956 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003957 return v;
3958}
3959
Thomas Wouters52152252000-08-17 22:55:00 +00003960static PyObject *
3961import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003962{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003963 PyObject *x;
3964
3965 x = PyObject_GetAttr(v, name);
3966 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00003967 PyErr_Format(PyExc_ImportError,
3968 "cannot import name %.230s",
3969 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003970 }
Thomas Wouters52152252000-08-17 22:55:00 +00003971 return x;
3972}
Guido van Rossumac7be682001-01-17 15:42:30 +00003973
Thomas Wouters52152252000-08-17 22:55:00 +00003974static int
3975import_all_from(PyObject *locals, PyObject *v)
3976{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003977 PyObject *all = PyObject_GetAttrString(v, "__all__");
3978 PyObject *dict, *name, *value;
3979 int skip_leading_underscores = 0;
3980 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003981
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003982 if (all == NULL) {
3983 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3984 return -1; /* Unexpected error */
3985 PyErr_Clear();
3986 dict = PyObject_GetAttrString(v, "__dict__");
3987 if (dict == NULL) {
3988 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3989 return -1;
3990 PyErr_SetString(PyExc_ImportError,
3991 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003992 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003993 }
3994 all = PyMapping_Keys(dict);
3995 Py_DECREF(dict);
3996 if (all == NULL)
3997 return -1;
3998 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00003999 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004000
4001 for (pos = 0, err = 0; ; pos++) {
4002 name = PySequence_GetItem(all, pos);
4003 if (name == NULL) {
4004 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4005 err = -1;
4006 else
4007 PyErr_Clear();
4008 break;
4009 }
4010 if (skip_leading_underscores &&
4011 PyString_Check(name) &&
4012 PyString_AS_STRING(name)[0] == '_')
4013 {
4014 Py_DECREF(name);
4015 continue;
4016 }
4017 value = PyObject_GetAttr(v, name);
4018 if (value == NULL)
4019 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004020 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004021 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004022 else
4023 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004024 Py_DECREF(name);
4025 Py_XDECREF(value);
4026 if (err != 0)
4027 break;
4028 }
4029 Py_DECREF(all);
4030 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004031}
4032
Guido van Rossumac7be682001-01-17 15:42:30 +00004033static void
Paul Prescode68140d2000-08-30 20:25:01 +00004034format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4035{
4036 char *obj_str;
4037
4038 if (!obj)
4039 return;
4040
4041 obj_str = PyString_AsString(obj);
4042 if (!obj_str)
4043 return;
4044
4045 PyErr_Format(exc, format_str, obj_str);
4046}
Guido van Rossum950361c1997-01-24 13:49:28 +00004047
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004048static PyObject *
4049string_concatenate(PyObject *v, PyObject *w,
4050 PyFrameObject *f, unsigned char *next_instr)
4051{
4052 /* This function implements 'variable += expr' when both arguments
4053 are strings. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004054 Py_ssize_t v_len = PyString_GET_SIZE(v);
4055 Py_ssize_t w_len = PyString_GET_SIZE(w);
4056 Py_ssize_t new_len = v_len + w_len;
4057 if (new_len < 0) {
4058 PyErr_SetString(PyExc_OverflowError,
4059 "strings are too large to concat");
4060 return NULL;
4061 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004062
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004063 if (v->ob_refcnt == 2) {
4064 /* In the common case, there are 2 references to the value
4065 * stored in 'variable' when the += is performed: one on the
4066 * value stack (in 'v') and one still stored in the 'variable'.
4067 * We try to delete the variable now to reduce the refcnt to 1.
4068 */
4069 switch (*next_instr) {
4070 case STORE_FAST:
4071 {
4072 int oparg = PEEKARG();
4073 PyObject **fastlocals = f->f_localsplus;
4074 if (GETLOCAL(oparg) == v)
4075 SETLOCAL(oparg, NULL);
4076 break;
4077 }
4078 case STORE_DEREF:
4079 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004080 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004081 PyObject *c = freevars[PEEKARG()];
4082 if (PyCell_GET(c) == v)
4083 PyCell_Set(c, NULL);
4084 break;
4085 }
4086 case STORE_NAME:
4087 {
4088 PyObject *names = f->f_code->co_names;
4089 PyObject *name = GETITEM(names, PEEKARG());
4090 PyObject *locals = f->f_locals;
4091 if (PyDict_CheckExact(locals) &&
4092 PyDict_GetItem(locals, name) == v) {
4093 if (PyDict_DelItem(locals, name) != 0) {
4094 PyErr_Clear();
4095 }
4096 }
4097 break;
4098 }
4099 }
4100 }
4101
Armin Rigo618fbf52004-08-07 20:58:32 +00004102 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004103 /* Now we own the last reference to 'v', so we can resize it
4104 * in-place.
4105 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004106 if (_PyString_Resize(&v, new_len) != 0) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004107 /* XXX if _PyString_Resize() fails, 'v' has been
4108 * deallocated so it cannot be put back into 'variable'.
4109 * The MemoryError is raised when there is no value in
4110 * 'variable', which might (very remotely) be a cause
4111 * of incompatibilities.
4112 */
4113 return NULL;
4114 }
4115 /* copy 'w' into the newly allocated area of 'v' */
4116 memcpy(PyString_AS_STRING(v) + v_len,
4117 PyString_AS_STRING(w), w_len);
4118 return v;
4119 }
4120 else {
4121 /* When in-place resizing is not an option. */
4122 PyString_Concat(&v, w);
4123 return v;
4124 }
4125}
4126
Guido van Rossum950361c1997-01-24 13:49:28 +00004127#ifdef DYNAMIC_EXECUTION_PROFILE
4128
Skip Montanarof118cb12001-10-15 20:51:38 +00004129static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004130getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004131{
4132 int i;
4133 PyObject *l = PyList_New(256);
4134 if (l == NULL) return NULL;
4135 for (i = 0; i < 256; i++) {
4136 PyObject *x = PyInt_FromLong(a[i]);
4137 if (x == NULL) {
4138 Py_DECREF(l);
4139 return NULL;
4140 }
4141 PyList_SetItem(l, i, x);
4142 }
4143 for (i = 0; i < 256; i++)
4144 a[i] = 0;
4145 return l;
4146}
4147
4148PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004149_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004150{
4151#ifndef DXPAIRS
4152 return getarray(dxp);
4153#else
4154 int i;
4155 PyObject *l = PyList_New(257);
4156 if (l == NULL) return NULL;
4157 for (i = 0; i < 257; i++) {
4158 PyObject *x = getarray(dxpairs[i]);
4159 if (x == NULL) {
4160 Py_DECREF(l);
4161 return NULL;
4162 }
4163 PyList_SetItem(l, i, x);
4164 }
4165 return l;
4166#endif
4167}
4168
4169#endif