blob: 37659f8f72abfa8dc2a58e9f0bce73d6e020b2d4 [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 *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000480static int unpack_iterable(PyObject *, int, 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{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000490 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000491 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000492 (PyObject **)NULL, 0,
493 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000494 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000495 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000496}
497
498
499/* Interpreter main loop */
500
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000501PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000502PyEval_EvalFrame(PyFrameObject *f) {
503 /* This is for backward compatibility with extension modules that
504 used this API; core interpreter code should call PyEval_EvalFrameEx() */
505 return PyEval_EvalFrameEx(f, 0);
506}
507
508PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000510{
Guido van Rossum950361c1997-01-24 13:49:28 +0000511#ifdef DXPAIRS
512 int lastopcode = 0;
513#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000514 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000515 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000516 register int opcode; /* Current opcode */
517 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000518 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000519 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000520 register PyObject *x; /* Result object -- NULL if error */
521 register PyObject *v; /* Temporary objects popped off stack */
522 register PyObject *w;
523 register PyObject *u;
524 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000525 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000526 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000527 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000528 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000529
Tim Peters8a5c3c72004-04-05 19:36:21 +0000530 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000531
532 not (instr_lb <= current_bytecode_offset < instr_ub)
533
Tim Peters8a5c3c72004-04-05 19:36:21 +0000534 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000535 initial values are such as to make this false the first
536 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000537 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000538
Guido van Rossumd076c731998-10-07 19:42:25 +0000539 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000540 PyObject *names;
541 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000542#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000543 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000544 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000545#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000546
Neal Norwitza81d2202002-07-14 00:27:26 +0000547/* Tuple access macros */
548
549#ifndef Py_DEBUG
550#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
551#else
552#define GETITEM(v, i) PyTuple_GetItem((v), (i))
553#endif
554
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000555#ifdef WITH_TSC
556/* Use Pentium timestamp counter to mark certain events:
557 inst0 -- beginning of switch statement for opcode dispatch
558 inst1 -- end of switch statement (may be skipped)
559 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000560 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000561 (may be skipped)
562 intr1 -- beginning of long interruption
563 intr2 -- end of long interruption
564
565 Many opcodes call out to helper C functions. In some cases, the
566 time in those functions should be counted towards the time for the
567 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
568 calls another Python function; there's no point in charge all the
569 bytecode executed by the called function to the caller.
570
571 It's hard to make a useful judgement statically. In the presence
572 of operator overloading, it's impossible to tell if a call will
573 execute new Python code or not.
574
575 It's a case-by-case judgement. I'll use intr1 for the following
576 cases:
577
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000578 IMPORT_STAR
579 IMPORT_FROM
580 CALL_FUNCTION (and friends)
581
582 */
583 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
584 int ticked = 0;
585
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000586 READ_TIMESTAMP(inst0);
587 READ_TIMESTAMP(inst1);
588 READ_TIMESTAMP(loop0);
589 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000590
591 /* shut up the compiler */
592 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000593#endif
594
Guido van Rossum374a9221991-04-04 10:40:29 +0000595/* Code access macros */
596
Martin v. Löwis18e16552006-02-15 17:27:45 +0000597#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000598#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000599#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000600#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000601#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000602#define JUMPBY(x) (next_instr += (x))
603
Raymond Hettingerf606f872003-03-16 03:11:04 +0000604/* OpCode prediction macros
605 Some opcodes tend to come in pairs thus making it possible to predict
606 the second code when the first is run. For example, COMPARE_OP is often
607 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
608 followed by a POP_TOP.
609
610 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000611 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000612 processor has a high likelihood of making its own successful branch
613 prediction which results in a nearly zero overhead transition to the
614 next opcode.
615
616 A successful prediction saves a trip through the eval-loop including
617 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000618
Tim Peters8a5c3c72004-04-05 19:36:21 +0000619 If collecting opcode statistics, turn off prediction so that
620 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000621 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000622*/
623
Raymond Hettingera7216982004-02-08 19:59:27 +0000624#ifdef DYNAMIC_EXECUTION_PROFILE
625#define PREDICT(op) if (0) goto PRED_##op
626#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000627#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000628#endif
629
Raymond Hettingerf606f872003-03-16 03:11:04 +0000630#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000631#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000632
Guido van Rossum374a9221991-04-04 10:40:29 +0000633/* Stack manipulation macros */
634
Martin v. Löwis18e16552006-02-15 17:27:45 +0000635/* The stack can grow at most MAXINT deep, as co_nlocals and
636 co_stacksize are ints. */
637#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000638#define EMPTY() (STACK_LEVEL() == 0)
639#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000640#define SECOND() (stack_pointer[-2])
641#define THIRD() (stack_pointer[-3])
642#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000643#define SET_TOP(v) (stack_pointer[-1] = (v))
644#define SET_SECOND(v) (stack_pointer[-2] = (v))
645#define SET_THIRD(v) (stack_pointer[-3] = (v))
646#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000647#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000648#define BASIC_PUSH(v) (*stack_pointer++ = (v))
649#define BASIC_POP() (*--stack_pointer)
650
Guido van Rossum96a42c81992-01-12 02:29:51 +0000651#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000652#define PUSH(v) { (void)(BASIC_PUSH(v), \
653 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 assert(STACK_LEVEL() <= co->co_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000655#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000656#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
657 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 assert(STACK_LEVEL() <= co->co_stacksize); }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000659#define EXT_POP(STACK_POINTER) (lltrace && prtrace((STACK_POINTER)[-1], "ext_pop"), *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000660#else
661#define PUSH(v) BASIC_PUSH(v)
662#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000663#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000664#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000665#endif
666
Guido van Rossum681d79a1995-07-18 14:51:37 +0000667/* Local variable macros */
668
669#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000670
671/* The SETLOCAL() macro must not DECREF the local variable in-place and
672 then store the new value; it must copy the old value to a temporary
673 value, then store the new value, and then DECREF the temporary value.
674 This is because it is possible that during the DECREF the frame is
675 accessed by other code (e.g. a __del__ method or gc.collect()) and the
676 variable would be pointing to already-freed memory. */
677#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
678 GETLOCAL(i) = value; \
679 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000680
Guido van Rossuma027efa1997-05-05 20:56:21 +0000681/* Start of code */
682
Tim Peters5ca576e2001-06-18 22:08:13 +0000683 if (f == NULL)
684 return NULL;
685
Armin Rigo1d313ab2003-10-25 14:33:09 +0000686 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000687 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000688 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000689
Tim Peters5ca576e2001-06-18 22:08:13 +0000690 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000691
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000692 if (tstate->use_tracing) {
693 if (tstate->c_tracefunc != NULL) {
694 /* tstate->c_tracefunc, if defined, is a
695 function that will be called on *every* entry
696 to a code block. Its return value, if not
697 None, is a function that will be called at
698 the start of each executed line of code.
699 (Actually, the function must return itself
700 in order to continue tracing.) The trace
701 functions are called with three arguments:
702 a pointer to the current frame, a string
703 indicating why the function is called, and
704 an argument which depends on the situation.
705 The global trace function is also called
706 whenever an exception is detected. */
707 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
708 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000709 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000710 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000711 }
712 }
713 if (tstate->c_profilefunc != NULL) {
714 /* Similar for c_profilefunc, except it needn't
715 return itself and isn't called for "line" events */
716 if (call_trace(tstate->c_profilefunc,
717 tstate->c_profileobj,
718 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000719 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000720 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000721 }
722 }
723 }
724
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000725 co = f->f_code;
726 names = co->co_names;
727 consts = co->co_consts;
728 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729 freevars = f->f_localsplus + co->co_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000730 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000731 /* An explanation is in order for the next line.
732
733 f->f_lasti now refers to the index of the last instruction
734 executed. You might think this was obvious from the name, but
735 this wasn't always true before 2.3! PyFrame_New now sets
736 f->f_lasti to -1 (i.e. the index *before* the first instruction)
737 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000738 does work. Promise.
739
740 When the PREDICT() macros are enabled, some opcode pairs follow in
741 direct succession without updating f->f_lasti. A successful
742 prediction effectively links the two codes together as if they
743 were a single new opcode; accordingly,f->f_lasti will point to
744 the first code in the pair (for instance, GET_ITER followed by
745 FOR_ITER is effectively a single opcode and f->f_lasti will point
746 at to the beginning of the combined pair.)
747 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000748 next_instr = first_instr + f->f_lasti + 1;
749 stack_pointer = f->f_stacktop;
750 assert(stack_pointer != NULL);
751 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
752
Tim Peters5ca576e2001-06-18 22:08:13 +0000753#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000755#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000756#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000757 filename = PyString_AsString(co->co_filename);
758#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000759
Guido van Rossum374a9221991-04-04 10:40:29 +0000760 why = WHY_NOT;
761 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000762 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000763 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000764
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000765 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000766 why = WHY_EXCEPTION;
767 goto on_error;
768 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000769
Guido van Rossum374a9221991-04-04 10:40:29 +0000770 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000771#ifdef WITH_TSC
772 if (inst1 == 0) {
773 /* Almost surely, the opcode executed a break
774 or a continue, preventing inst1 from being set
775 on the way out of the loop.
776 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000777 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000778 loop1 = inst1;
779 }
780 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
781 intr0, intr1);
782 ticked = 0;
783 inst1 = 0;
784 intr0 = 0;
785 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000786 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000787#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000788 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000789 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000790
Guido van Rossuma027efa1997-05-05 20:56:21 +0000791 /* Do periodic things. Doing this every time through
792 the loop would add too much overhead, so we do it
793 only every Nth instruction. We also do it if
794 ``things_to_do'' is set, i.e. when an asynchronous
795 event needs attention (e.g. a signal handler or
796 async I/O handler); see Py_AddPendingCall() and
797 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000798
Skip Montanarod581d772002-09-03 20:10:45 +0000799 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000800 if (*next_instr == SETUP_FINALLY) {
801 /* Make the last opcode before
802 a try: finally: block uninterruptable. */
803 goto fast_next_opcode;
804 }
Skip Montanarod581d772002-09-03 20:10:45 +0000805 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000806 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000807#ifdef WITH_TSC
808 ticked = 1;
809#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000810 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000811 if (Py_MakePendingCalls() < 0) {
812 why = WHY_EXCEPTION;
813 goto on_error;
814 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000815 if (things_to_do)
816 /* MakePendingCalls() didn't succeed.
817 Force early re-execution of this
818 "periodic" code, possibly after
819 a thread switch */
820 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000821 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000822#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000823 if (interpreter_lock) {
824 /* Give another thread a chance */
825
Guido van Rossum25ce5661997-08-02 03:10:38 +0000826 if (PyThreadState_Swap(NULL) != tstate)
827 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000828 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000829
830 /* Other threads may run now */
831
Guido van Rossum65d5b571998-12-21 19:32:43 +0000832 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000833 if (PyThreadState_Swap(tstate) != NULL)
834 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000835
836 /* Check for thread interrupts */
837
838 if (tstate->async_exc != NULL) {
839 x = tstate->async_exc;
840 tstate->async_exc = NULL;
841 PyErr_SetNone(x);
842 Py_DECREF(x);
843 why = WHY_EXCEPTION;
844 goto on_error;
845 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000846 }
847#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000848 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000849
Neil Schemenauer63543862002-02-17 19:10:14 +0000850 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000851 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000852
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000853 /* line-by-line tracing support */
854
855 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
856 /* see maybe_call_line_trace
857 for expository comments */
858 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000859
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000860 err = maybe_call_line_trace(tstate->c_tracefunc,
861 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000862 f, &instr_lb, &instr_ub,
863 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000864 /* Reload possibly changed frame fields */
865 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000866 if (f->f_stacktop != NULL) {
867 stack_pointer = f->f_stacktop;
868 f->f_stacktop = NULL;
869 }
870 if (err) {
871 /* trace function raised an exception */
872 goto on_error;
873 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000874 }
875
876 /* Extract opcode and argument */
877
Guido van Rossum374a9221991-04-04 10:40:29 +0000878 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000879 oparg = 0; /* allows oparg to be stored in a register because
880 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000881 if (HAS_ARG(opcode))
882 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000883 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000884#ifdef DYNAMIC_EXECUTION_PROFILE
885#ifdef DXPAIRS
886 dxpairs[lastopcode][opcode]++;
887 lastopcode = opcode;
888#endif
889 dxp[opcode]++;
890#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000891
Guido van Rossum96a42c81992-01-12 02:29:51 +0000892#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000893 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000894
Guido van Rossum96a42c81992-01-12 02:29:51 +0000895 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000896 if (HAS_ARG(opcode)) {
897 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000898 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000899 }
900 else {
901 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000902 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000903 }
904 }
905#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000906
Guido van Rossum374a9221991-04-04 10:40:29 +0000907 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000908 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000909
Guido van Rossum374a9221991-04-04 10:40:29 +0000910 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000911
Guido van Rossum374a9221991-04-04 10:40:29 +0000912 /* BEWARE!
913 It is essential that any operation that fails sets either
914 x to NULL, err to nonzero, or why to anything but WHY_NOT,
915 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000916
Guido van Rossum374a9221991-04-04 10:40:29 +0000917 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000918
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000919 case NOP:
920 goto fast_next_opcode;
921
Neil Schemenauer63543862002-02-17 19:10:14 +0000922 case LOAD_FAST:
923 x = GETLOCAL(oparg);
924 if (x != NULL) {
925 Py_INCREF(x);
926 PUSH(x);
927 goto fast_next_opcode;
928 }
929 format_exc_check_arg(PyExc_UnboundLocalError,
930 UNBOUNDLOCAL_ERROR_MSG,
931 PyTuple_GetItem(co->co_varnames, oparg));
932 break;
933
934 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000935 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000936 Py_INCREF(x);
937 PUSH(x);
938 goto fast_next_opcode;
939
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000940 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000941 case STORE_FAST:
942 v = POP();
943 SETLOCAL(oparg, v);
944 goto fast_next_opcode;
945
Raymond Hettingerf606f872003-03-16 03:11:04 +0000946 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000947 case POP_TOP:
948 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000949 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000950 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000951
Guido van Rossum374a9221991-04-04 10:40:29 +0000952 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000953 v = TOP();
954 w = SECOND();
955 SET_TOP(w);
956 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000957 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000958
Guido van Rossum374a9221991-04-04 10:40:29 +0000959 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000960 v = TOP();
961 w = SECOND();
962 x = THIRD();
963 SET_TOP(w);
964 SET_SECOND(x);
965 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000966 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000967
Thomas Wouters434d0822000-08-24 20:11:32 +0000968 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000969 u = TOP();
970 v = SECOND();
971 w = THIRD();
972 x = FOURTH();
973 SET_TOP(v);
974 SET_SECOND(w);
975 SET_THIRD(x);
976 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000977 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000978
Guido van Rossum374a9221991-04-04 10:40:29 +0000979 case DUP_TOP:
980 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000981 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000982 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000983 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000984
Thomas Wouters434d0822000-08-24 20:11:32 +0000985 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000986 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000988 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000989 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000990 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000991 STACKADJ(2);
992 SET_TOP(x);
993 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000994 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000995 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000996 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000997 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000998 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000999 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001000 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001001 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001002 STACKADJ(3);
1003 SET_TOP(x);
1004 SET_SECOND(w);
1005 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001006 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001007 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001008 Py_FatalError("invalid argument to DUP_TOPX"
1009 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001010 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001011
Guido van Rossum374a9221991-04-04 10:40:29 +00001012 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001013 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001014 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001015 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001017 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001018 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001019
Guido van Rossum374a9221991-04-04 10:40:29 +00001020 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001022 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001023 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001024 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001025 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001026 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001027
Guido van Rossum374a9221991-04-04 10:40:29 +00001028 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001029 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001030 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001031 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001032 if (err == 0) {
1033 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001034 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001035 continue;
1036 }
1037 else if (err > 0) {
1038 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001040 err = 0;
1041 continue;
1042 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001043 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001045
Guido van Rossum7928cd71991-10-24 14:59:31 +00001046 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001047 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001048 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001049 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001051 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001053
Guido van Rossum50564e81996-01-12 01:13:16 +00001054 case BINARY_POWER:
1055 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001057 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001058 Py_DECREF(v);
1059 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001060 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001061 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001062 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001063
Guido van Rossum374a9221991-04-04 10:40:29 +00001064 case BINARY_MULTIPLY:
1065 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001066 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001067 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001068 Py_DECREF(v);
1069 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001070 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001071 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001072 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001073
Tim Peters3caca232001-12-06 06:23:26 +00001074 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001075 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001076 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001077 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001078 Py_DECREF(v);
1079 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001080 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001081 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001082 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001083
Guido van Rossum4668b002001-08-08 05:00:18 +00001084 case BINARY_FLOOR_DIVIDE:
1085 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001086 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001087 x = PyNumber_FloorDivide(v, w);
1088 Py_DECREF(v);
1089 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001090 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001091 if (x != NULL) continue;
1092 break;
1093
Guido van Rossum374a9221991-04-04 10:40:29 +00001094 case BINARY_MODULO:
1095 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001096 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001097 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001098 Py_DECREF(v);
1099 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001100 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001101 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001102 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Guido van Rossum374a9221991-04-04 10:40:29 +00001104 case BINARY_ADD:
1105 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001106 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001107 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001108 /* INLINE: int + int */
1109 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001110 a = PyInt_AS_LONG(v);
1111 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001112 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001113 if ((i^a) < 0 && (i^b) < 0)
1114 goto slow_add;
1115 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001116 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001117 else if (PyString_CheckExact(v) &&
1118 PyString_CheckExact(w)) {
1119 x = string_concatenate(v, w, f, next_instr);
1120 /* string_concatenate consumed the ref to v */
1121 goto skip_decref_vx;
1122 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001123 else {
1124 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001125 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001126 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001127 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001128 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001129 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001130 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001131 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001132 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001133
Guido van Rossum374a9221991-04-04 10:40:29 +00001134 case BINARY_SUBTRACT:
1135 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001136 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001137 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001138 /* INLINE: int - int */
1139 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001140 a = PyInt_AS_LONG(v);
1141 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001142 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001143 if ((i^a) < 0 && (i^~b) < 0)
1144 goto slow_sub;
1145 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001146 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001147 else {
1148 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001149 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001150 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001151 Py_DECREF(v);
1152 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001153 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001154 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001155 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Guido van Rossum374a9221991-04-04 10:40:29 +00001157 case BINARY_SUBSCR:
1158 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001159 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001160 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001161 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001162 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001163 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001164 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001165 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001166 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001167 Py_INCREF(x);
1168 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001169 else
1170 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001171 }
1172 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001173 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001174 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001175 Py_DECREF(v);
1176 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001177 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001178 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001179 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001180
Guido van Rossum7928cd71991-10-24 14:59:31 +00001181 case BINARY_LSHIFT:
1182 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001183 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001184 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001185 Py_DECREF(v);
1186 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001187 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001188 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001189 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001190
Guido van Rossum7928cd71991-10-24 14:59:31 +00001191 case BINARY_RSHIFT:
1192 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001193 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001194 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001195 Py_DECREF(v);
1196 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001197 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001198 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001199 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001200
Guido van Rossum7928cd71991-10-24 14:59:31 +00001201 case BINARY_AND:
1202 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001203 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001204 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001205 Py_DECREF(v);
1206 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001207 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001208 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001209 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001210
Guido van Rossum7928cd71991-10-24 14:59:31 +00001211 case BINARY_XOR:
1212 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001213 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001214 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001215 Py_DECREF(v);
1216 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001217 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001218 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001219 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001220
Guido van Rossum7928cd71991-10-24 14:59:31 +00001221 case BINARY_OR:
1222 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001223 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001224 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001225 Py_DECREF(v);
1226 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001227 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001228 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001229 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001230
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001231 case LIST_APPEND:
1232 w = POP();
1233 v = POP();
1234 err = PyList_Append(v, w);
1235 Py_DECREF(v);
1236 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001237 if (err == 0) {
1238 PREDICT(JUMP_ABSOLUTE);
1239 continue;
1240 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001241 break;
1242
Nick Coghlan650f0d02007-04-15 12:05:43 +00001243 case SET_ADD:
1244 w = POP();
1245 v = POP();
1246 err = PySet_Add(v, w);
1247 Py_DECREF(v);
1248 Py_DECREF(w);
1249 if (err == 0) {
1250 PREDICT(JUMP_ABSOLUTE);
1251 continue;
1252 }
1253 break;
1254
Thomas Wouters434d0822000-08-24 20:11:32 +00001255 case INPLACE_POWER:
1256 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001257 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001258 x = PyNumber_InPlacePower(v, w, Py_None);
1259 Py_DECREF(v);
1260 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001261 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001262 if (x != NULL) continue;
1263 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Thomas Wouters434d0822000-08-24 20:11:32 +00001265 case INPLACE_MULTIPLY:
1266 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001267 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001268 x = PyNumber_InPlaceMultiply(v, w);
1269 Py_DECREF(v);
1270 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001271 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001272 if (x != NULL) continue;
1273 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Tim Peters54b11912001-12-25 18:49:11 +00001275 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001276 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001277 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001278 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001279 Py_DECREF(v);
1280 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001281 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001282 if (x != NULL) continue;
1283 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001284
Guido van Rossum4668b002001-08-08 05:00:18 +00001285 case INPLACE_FLOOR_DIVIDE:
1286 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001287 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001288 x = PyNumber_InPlaceFloorDivide(v, w);
1289 Py_DECREF(v);
1290 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001291 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001292 if (x != NULL) continue;
1293 break;
1294
Thomas Wouters434d0822000-08-24 20:11:32 +00001295 case INPLACE_MODULO:
1296 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001297 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001298 x = PyNumber_InPlaceRemainder(v, w);
1299 Py_DECREF(v);
1300 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001301 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 if (x != NULL) continue;
1303 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001304
Thomas Wouters434d0822000-08-24 20:11:32 +00001305 case INPLACE_ADD:
1306 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001307 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001308 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 /* INLINE: int + int */
1310 register long a, b, i;
1311 a = PyInt_AS_LONG(v);
1312 b = PyInt_AS_LONG(w);
1313 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001314 if ((i^a) < 0 && (i^b) < 0)
1315 goto slow_iadd;
1316 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001318 else if (PyString_CheckExact(v) &&
1319 PyString_CheckExact(w)) {
1320 x = string_concatenate(v, w, f, next_instr);
1321 /* string_concatenate consumed the ref to v */
1322 goto skip_decref_v;
1323 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001324 else {
1325 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001327 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001328 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001329 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001330 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001331 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001332 if (x != NULL) continue;
1333 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001334
Thomas Wouters434d0822000-08-24 20:11:32 +00001335 case INPLACE_SUBTRACT:
1336 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001337 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001338 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 /* INLINE: int - int */
1340 register long a, b, i;
1341 a = PyInt_AS_LONG(v);
1342 b = PyInt_AS_LONG(w);
1343 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001344 if ((i^a) < 0 && (i^~b) < 0)
1345 goto slow_isub;
1346 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001347 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001348 else {
1349 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001350 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001351 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 Py_DECREF(v);
1353 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 if (x != NULL) continue;
1356 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001357
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 case INPLACE_LSHIFT:
1359 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 x = PyNumber_InPlaceLshift(v, w);
1362 Py_DECREF(v);
1363 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001365 if (x != NULL) continue;
1366 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Thomas Wouters434d0822000-08-24 20:11:32 +00001368 case INPLACE_RSHIFT:
1369 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001371 x = PyNumber_InPlaceRshift(v, w);
1372 Py_DECREF(v);
1373 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001375 if (x != NULL) continue;
1376 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Thomas Wouters434d0822000-08-24 20:11:32 +00001378 case INPLACE_AND:
1379 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001380 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001381 x = PyNumber_InPlaceAnd(v, w);
1382 Py_DECREF(v);
1383 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 if (x != NULL) continue;
1386 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001387
Thomas Wouters434d0822000-08-24 20:11:32 +00001388 case INPLACE_XOR:
1389 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001391 x = PyNumber_InPlaceXor(v, w);
1392 Py_DECREF(v);
1393 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001395 if (x != NULL) continue;
1396 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Thomas Wouters434d0822000-08-24 20:11:32 +00001398 case INPLACE_OR:
1399 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001400 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001401 x = PyNumber_InPlaceOr(v, w);
1402 Py_DECREF(v);
1403 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001404 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001405 if (x != NULL) continue;
1406 break;
1407
Guido van Rossum374a9221991-04-04 10:40:29 +00001408 case SLICE+0:
1409 case SLICE+1:
1410 case SLICE+2:
1411 case SLICE+3:
1412 if ((opcode-SLICE) & 2)
1413 w = POP();
1414 else
1415 w = NULL;
1416 if ((opcode-SLICE) & 1)
1417 v = POP();
1418 else
1419 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001420 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001421 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001422 Py_DECREF(u);
1423 Py_XDECREF(v);
1424 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001425 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001426 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001427 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001428
Guido van Rossum374a9221991-04-04 10:40:29 +00001429 case STORE_SLICE+0:
1430 case STORE_SLICE+1:
1431 case STORE_SLICE+2:
1432 case STORE_SLICE+3:
1433 if ((opcode-STORE_SLICE) & 2)
1434 w = POP();
1435 else
1436 w = NULL;
1437 if ((opcode-STORE_SLICE) & 1)
1438 v = POP();
1439 else
1440 v = NULL;
1441 u = POP();
1442 t = POP();
1443 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001444 Py_DECREF(t);
1445 Py_DECREF(u);
1446 Py_XDECREF(v);
1447 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001448 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001449 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001450
Guido van Rossum374a9221991-04-04 10:40:29 +00001451 case DELETE_SLICE+0:
1452 case DELETE_SLICE+1:
1453 case DELETE_SLICE+2:
1454 case DELETE_SLICE+3:
1455 if ((opcode-DELETE_SLICE) & 2)
1456 w = POP();
1457 else
1458 w = NULL;
1459 if ((opcode-DELETE_SLICE) & 1)
1460 v = POP();
1461 else
1462 v = NULL;
1463 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001464 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001466 Py_DECREF(u);
1467 Py_XDECREF(v);
1468 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001469 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001470 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001471
Guido van Rossum374a9221991-04-04 10:40:29 +00001472 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001473 w = TOP();
1474 v = SECOND();
1475 u = THIRD();
1476 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001478 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001479 Py_DECREF(u);
1480 Py_DECREF(v);
1481 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001482 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001483 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001484
Guido van Rossum374a9221991-04-04 10:40:29 +00001485 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001486 w = TOP();
1487 v = SECOND();
1488 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001489 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001490 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001491 Py_DECREF(v);
1492 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001493 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001494 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001495
Guido van Rossum374a9221991-04-04 10:40:29 +00001496 case PRINT_EXPR:
1497 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001498 w = PySys_GetObject("displayhook");
1499 if (w == NULL) {
1500 PyErr_SetString(PyExc_RuntimeError,
1501 "lost sys.displayhook");
1502 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001503 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001504 }
1505 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001506 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001507 if (x == NULL)
1508 err = -1;
1509 }
1510 if (err == 0) {
1511 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001512 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001513 if (w == NULL)
1514 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001515 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001516 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001517 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001518 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001519
Thomas Wouters434d0822000-08-24 20:11:32 +00001520#ifdef CASE_TOO_BIG
1521 default: switch (opcode) {
1522#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001523 case RAISE_VARARGS:
1524 u = v = w = NULL;
1525 switch (oparg) {
1526 case 3:
1527 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001528 /* Fallthrough */
1529 case 2:
1530 v = POP(); /* value */
1531 /* Fallthrough */
1532 case 1:
1533 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001534 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001535 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001536 break;
1537 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001538 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001539 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001540 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001541 break;
1542 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001543 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001545 case STORE_LOCALS:
1546 x = POP();
1547 v = f->f_locals;
1548 Py_XDECREF(v);
1549 f->f_locals = x;
1550 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001551
Guido van Rossum374a9221991-04-04 10:40:29 +00001552 case RETURN_VALUE:
1553 retval = POP();
1554 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001555 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001556
Tim Peters5ca576e2001-06-18 22:08:13 +00001557 case YIELD_VALUE:
1558 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001559 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001560 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001561 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001562
Guido van Rossum374a9221991-04-04 10:40:29 +00001563 case POP_BLOCK:
1564 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001565 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001566 while (STACK_LEVEL() > b->b_level) {
1567 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001568 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001569 }
1570 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001571 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Guido van Rossum374a9221991-04-04 10:40:29 +00001573 case END_FINALLY:
1574 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001575 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001576 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001577 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001578 if (why == WHY_RETURN ||
1579 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001580 retval = POP();
1581 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001582 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001583 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001584 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001585 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001586 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001587 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001588 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001589 else if (v != Py_None) {
1590 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001591 "'finally' pops bad exception");
1592 why = WHY_EXCEPTION;
1593 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001594 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001595 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001596
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001597 case LOAD_BUILD_CLASS:
1598 x = PyDict_GetItemString(f->f_builtins,
1599 "__build_class__");
1600 if (x == NULL) {
1601 PyErr_SetString(PyExc_ImportError,
1602 "__build_class__ not found");
1603 break;
1604 }
1605 Py_INCREF(x);
1606 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001607 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Guido van Rossum374a9221991-04-04 10:40:29 +00001609 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001610 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001611 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001612 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001613 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001614 err = PyDict_SetItem(x, w, v);
1615 else
1616 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001617 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001618 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001619 break;
1620 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001621 PyErr_Format(PyExc_SystemError,
1622 "no locals found when storing %s",
1623 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001624 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001625
Guido van Rossum374a9221991-04-04 10:40:29 +00001626 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001627 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001628 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001629 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001630 format_exc_check_arg(PyExc_NameError,
1631 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001632 break;
1633 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001634 PyErr_Format(PyExc_SystemError,
1635 "no locals when deleting %s",
1636 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001637 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001638
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001639 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001640 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001642 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1643 PyObject **items = ((PyTupleObject *)v)->ob_item;
1644 while (oparg--) {
1645 w = items[oparg];
1646 Py_INCREF(w);
1647 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001648 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001649 Py_DECREF(v);
1650 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001651 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1652 PyObject **items = ((PyListObject *)v)->ob_item;
1653 while (oparg--) {
1654 w = items[oparg];
1655 Py_INCREF(w);
1656 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001657 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001658 } else if (unpack_iterable(v, oparg, -1,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001659 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001660 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001661 } else {
1662 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001663 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001664 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001665 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001666 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001667
Guido van Rossum0368b722007-05-11 16:50:42 +00001668 case UNPACK_EX:
1669 {
1670 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1671 v = POP();
1672
1673 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1674 stack_pointer + totalargs)) {
1675 stack_pointer += totalargs;
1676 } else {
1677 why = WHY_EXCEPTION;
1678 }
1679 Py_DECREF(v);
1680 break;
1681 }
1682
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001684 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001685 v = TOP();
1686 u = SECOND();
1687 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001688 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1689 Py_DECREF(v);
1690 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001691 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001692 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001693
Guido van Rossum374a9221991-04-04 10:40:29 +00001694 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001695 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001696 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001697 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1698 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001699 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001700 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001702 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001703 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001704 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001705 err = PyDict_SetItem(f->f_globals, w, v);
1706 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001707 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001708 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001710 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001711 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001712 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001713 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001714 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001715 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001716
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001718 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001719 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001720 PyErr_Format(PyExc_SystemError,
1721 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001722 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001723 break;
1724 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001725 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001726 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001727 Py_XINCREF(x);
1728 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001729 else {
1730 x = PyObject_GetItem(v, w);
1731 if (x == NULL && PyErr_Occurred()) {
1732 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1733 break;
1734 PyErr_Clear();
1735 }
1736 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001737 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001738 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001740 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001742 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001743 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001744 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 break;
1746 }
1747 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001748 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001749 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001750 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001751 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001752
Guido van Rossum374a9221991-04-04 10:40:29 +00001753 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001754 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001755 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001756 /* Inline the PyDict_GetItem() calls.
1757 WARNING: this is an extreme speed hack.
1758 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001759 long hash = ((PyStringObject *)w)->ob_shash;
1760 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001761 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001762 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001763 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001764 e = d->ma_lookup(d, w, hash);
1765 if (e == NULL) {
1766 x = NULL;
1767 break;
1768 }
1769 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001770 if (x != NULL) {
1771 Py_INCREF(x);
1772 PUSH(x);
1773 continue;
1774 }
1775 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001776 e = d->ma_lookup(d, w, hash);
1777 if (e == NULL) {
1778 x = NULL;
1779 break;
1780 }
1781 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001782 if (x != NULL) {
1783 Py_INCREF(x);
1784 PUSH(x);
1785 continue;
1786 }
1787 goto load_global_error;
1788 }
1789 }
1790 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001791 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001792 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001793 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001794 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001795 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001796 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001797 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001798 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001799 break;
1800 }
1801 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001802 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001803 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001804 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001805
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001806 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001807 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001808 if (x != NULL) {
1809 SETLOCAL(oparg, NULL);
1810 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001811 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001812 format_exc_check_arg(
1813 PyExc_UnboundLocalError,
1814 UNBOUNDLOCAL_ERROR_MSG,
1815 PyTuple_GetItem(co->co_varnames, oparg)
1816 );
1817 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001819 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001820 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001821 Py_INCREF(x);
1822 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001823 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001824 break;
1825
1826 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001827 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001828 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001829 if (w != NULL) {
1830 PUSH(w);
1831 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001832 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001833 err = -1;
1834 /* Don't stomp existing exception */
1835 if (PyErr_Occurred())
1836 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001837 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1838 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001839 oparg);
1840 format_exc_check_arg(
1841 PyExc_UnboundLocalError,
1842 UNBOUNDLOCAL_ERROR_MSG,
1843 v);
1844 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001845 v = PyTuple_GET_ITEM(
Raymond Hettinger467a6982004-04-07 11:39:21 +00001846 co->co_freevars,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001847 oparg - PyTuple_GET_SIZE(co->co_cellvars));
Raymond Hettinger467a6982004-04-07 11:39:21 +00001848 format_exc_check_arg(
1849 PyExc_NameError,
1850 UNBOUNDFREE_ERROR_MSG,
1851 v);
1852 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001853 break;
1854
1855 case STORE_DEREF:
1856 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001857 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001858 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001859 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001860 continue;
1861
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001863 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001864 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001865 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001867 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001868 }
1869 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001870 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001871 }
1872 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001873
Guido van Rossum374a9221991-04-04 10:40:29 +00001874 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001875 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001877 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001879 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 }
1881 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001882 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 }
1884 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001885
Guido van Rossum86e58e22006-08-28 15:27:34 +00001886 case BUILD_SET:
1887 x = PySet_New(NULL);
1888 if (x != NULL) {
1889 for (; --oparg >= 0;) {
1890 w = POP();
1891 if (err == 0)
1892 err = PySet_Add(x, w);
1893 Py_DECREF(w);
1894 }
1895 if (err != 0) {
1896 Py_DECREF(x);
1897 break;
1898 }
1899 PUSH(x);
1900 continue;
1901 }
1902 break;
1903
Guido van Rossum374a9221991-04-04 10:40:29 +00001904 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001905 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001906 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001907 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001909
1910 case MAKE_BYTES:
1911 w = POP();
1912 if (PyString_Check(w))
1913 x = PyBytes_FromStringAndSize(
1914 PyString_AS_STRING(w),
1915 PyString_GET_SIZE(w));
1916 else
1917 x = NULL;
1918 Py_DECREF(w);
1919 PUSH(x);
1920 if (x != NULL) continue;
1921 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001922
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001924 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001925 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001926 x = PyObject_GetAttr(v, w);
1927 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001928 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001929 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001930 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001931
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 case COMPARE_OP:
1933 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001934 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001935 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001936 /* INLINE: cmp(int, int) */
1937 register long a, b;
1938 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001939 a = PyInt_AS_LONG(v);
1940 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001941 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001942 case PyCmp_LT: res = a < b; break;
1943 case PyCmp_LE: res = a <= b; break;
1944 case PyCmp_EQ: res = a == b; break;
1945 case PyCmp_NE: res = a != b; break;
1946 case PyCmp_GT: res = a > b; break;
1947 case PyCmp_GE: res = a >= b; break;
1948 case PyCmp_IS: res = v == w; break;
1949 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001950 default: goto slow_compare;
1951 }
1952 x = res ? Py_True : Py_False;
1953 Py_INCREF(x);
1954 }
1955 else {
1956 slow_compare:
1957 x = cmp_outcome(oparg, v, w);
1958 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 Py_DECREF(v);
1960 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001961 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001962 if (x == NULL) break;
1963 PREDICT(JUMP_IF_FALSE);
1964 PREDICT(JUMP_IF_TRUE);
1965 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001966
Guido van Rossum374a9221991-04-04 10:40:29 +00001967 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001968 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001969 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001970 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001971 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00001972 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001973 break;
1974 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001975 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001976 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001977 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
1978 w = PyTuple_Pack(5,
1979 w,
1980 f->f_globals,
1981 f->f_locals == NULL ?
1982 Py_None : f->f_locals,
1983 v,
1984 u);
1985 else
1986 w = PyTuple_Pack(4,
1987 w,
1988 f->f_globals,
1989 f->f_locals == NULL ?
1990 Py_None : f->f_locals,
1991 v);
1992 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001993 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001994 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001995 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001996 x = NULL;
1997 break;
1998 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001999 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002000 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002001 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002002 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002003 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002004 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002005 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002006
Thomas Wouters52152252000-08-17 22:55:00 +00002007 case IMPORT_STAR:
2008 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002009 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002010 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002011 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002012 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002013 break;
2014 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002015 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002016 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002017 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002018 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002019 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002020 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002021 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002022
Thomas Wouters52152252000-08-17 22:55:00 +00002023 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002024 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002025 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002026 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002027 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002028 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002029 PUSH(x);
2030 if (x != NULL) continue;
2031 break;
2032
Guido van Rossum374a9221991-04-04 10:40:29 +00002033 case JUMP_FORWARD:
2034 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002035 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002036
Raymond Hettingerf606f872003-03-16 03:11:04 +00002037 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002038 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002039 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002040 if (w == Py_True) {
2041 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002042 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002043 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002044 if (w == Py_False) {
2045 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002046 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002047 }
2048 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002049 if (err > 0)
2050 err = 0;
2051 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002052 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002053 else
2054 break;
2055 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002056
Raymond Hettingerf606f872003-03-16 03:11:04 +00002057 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002059 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002060 if (w == Py_False) {
2061 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002062 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002063 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002064 if (w == Py_True) {
2065 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002066 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002067 }
2068 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002069 if (err > 0) {
2070 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002071 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002072 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002073 else if (err == 0)
2074 ;
2075 else
2076 break;
2077 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002078
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002079 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002080 case JUMP_ABSOLUTE:
2081 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002082 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002083
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002084 case GET_ITER:
2085 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002086 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002087 x = PyObject_GetIter(v);
2088 Py_DECREF(v);
2089 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002090 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002091 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002092 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002093 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002094 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002095 break;
2096
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002097 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002098 case FOR_ITER:
2099 /* before: [iter]; after: [iter, iter()] *or* [] */
2100 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002101 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002102 if (x != NULL) {
2103 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002104 PREDICT(STORE_FAST);
2105 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002106 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002108 if (PyErr_Occurred()) {
2109 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2110 break;
2111 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002112 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002113 /* iterator ended normally */
2114 x = v = POP();
2115 Py_DECREF(v);
2116 JUMPBY(oparg);
2117 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002118
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002119 case BREAK_LOOP:
2120 why = WHY_BREAK;
2121 goto fast_block_end;
2122
2123 case CONTINUE_LOOP:
2124 retval = PyInt_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002125 if (!retval) {
2126 x = NULL;
2127 break;
2128 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002129 why = WHY_CONTINUE;
2130 goto fast_block_end;
2131
Guido van Rossum374a9221991-04-04 10:40:29 +00002132 case SETUP_LOOP:
2133 case SETUP_EXCEPT:
2134 case SETUP_FINALLY:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00002135 /* NOTE: If you add any new block-setup opcodes that are
2136 not try/except/finally handlers, you may need to
2137 update the PyGen_NeedsFinalizing() function. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002138
Guido van Rossumb209a111997-04-29 18:18:01 +00002139 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002140 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002141 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002142
Guido van Rossumc2e20742006-02-27 22:32:47 +00002143 case WITH_CLEANUP:
2144 {
2145 /* TOP is the context.__exit__ bound method.
2146 Below that are 1-3 values indicating how/why
2147 we entered the finally clause:
2148 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002149 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002150 - SECOND = WHY_*; no retval below it
2151 - (SECOND, THIRD, FOURTH) = exc_info()
2152 In the last case, we must call
2153 TOP(SECOND, THIRD, FOURTH)
2154 otherwise we must call
2155 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002156
2157 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002158 *and* the function call returns a 'true' value, we
2159 "zap" this information, to prevent END_FINALLY from
2160 re-raising the exception. (But non-local gotos
2161 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002162 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002163
Guido van Rossumc2e20742006-02-27 22:32:47 +00002164 x = TOP();
2165 u = SECOND();
2166 if (PyInt_Check(u) || u == Py_None) {
2167 u = v = w = Py_None;
2168 }
2169 else {
2170 v = THIRD();
2171 w = FOURTH();
2172 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002173 /* XXX Not the fastest way to call it... */
2174 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2175 if (x == NULL)
2176 break; /* Go to error exit */
2177 if (u != Py_None && PyObject_IsTrue(x)) {
2178 /* There was an exception and a true return */
2179 Py_DECREF(x);
2180 x = TOP(); /* Again */
2181 STACKADJ(-3);
2182 Py_INCREF(Py_None);
2183 SET_TOP(Py_None);
2184 Py_DECREF(x);
2185 Py_DECREF(u);
2186 Py_DECREF(v);
2187 Py_DECREF(w);
2188 } else {
2189 /* Let END_FINALLY do its thing */
2190 Py_DECREF(x);
2191 x = POP();
2192 Py_DECREF(x);
2193 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002194 break;
2195 }
2196
Guido van Rossumf10570b1995-07-07 22:53:21 +00002197 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002198 {
2199 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002200 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002201 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002202#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002203 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002204#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002205 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002206#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002207 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002208 PUSH(x);
2209 if (x != NULL)
2210 continue;
2211 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002212 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002213
Jeremy Hylton76901512000-03-28 23:49:17 +00002214 case CALL_FUNCTION_VAR:
2215 case CALL_FUNCTION_KW:
2216 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002217 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002218 int na = oparg & 0xff;
2219 int nk = (oparg>>8) & 0xff;
2220 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002221 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002222 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002223 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002224 if (flags & CALL_FLAG_VAR)
2225 n++;
2226 if (flags & CALL_FLAG_KW)
2227 n++;
2228 pfunc = stack_pointer - n - 1;
2229 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002230
Guido van Rossumac7be682001-01-17 15:42:30 +00002231 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002232 && PyMethod_GET_SELF(func) != NULL) {
2233 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002234 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002235 func = PyMethod_GET_FUNCTION(func);
2236 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002237 Py_DECREF(*pfunc);
2238 *pfunc = self;
2239 na++;
2240 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002241 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002242 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002243 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002244 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002245 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002246 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002247 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002248 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002249
Jeremy Hylton76901512000-03-28 23:49:17 +00002250 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002251 w = POP();
2252 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002253 }
2254 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002255 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002256 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002257 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002259
Guido van Rossum0240b922007-02-26 21:23:50 +00002260 case MAKE_CLOSURE:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002261 case MAKE_FUNCTION:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002262 {
2263 int posdefaults = oparg & 0xff;
2264 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002265 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002266
Guido van Rossum681d79a1995-07-18 14:51:37 +00002267 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002268 x = PyFunction_New(v, f->f_globals);
2269 Py_DECREF(v);
Guido van Rossum0240b922007-02-26 21:23:50 +00002270
2271 if (x != NULL && opcode == MAKE_CLOSURE) {
2272 v = POP();
2273 err = PyFunction_SetClosure(x, v);
2274 Py_DECREF(v);
2275 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002276
2277 if (x != NULL && num_annotations > 0) {
2278 Py_ssize_t name_ix;
2279 u = POP(); /* names of args with annotations */
2280 v = PyDict_New();
2281 if (v == NULL) {
2282 Py_DECREF(x);
2283 x = NULL;
2284 break;
2285 }
2286 name_ix = PyTuple_Size(u);
2287 assert(num_annotations == name_ix+1);
2288 while (name_ix > 0) {
2289 --name_ix;
2290 t = PyTuple_GET_ITEM(u, name_ix);
2291 w = POP();
2292 /* XXX(nnorwitz): check for errors */
2293 PyDict_SetItem(v, t, w);
2294 Py_DECREF(w);
2295 }
2296
2297 err = PyFunction_SetAnnotations(x, v);
2298 Py_DECREF(v);
2299 Py_DECREF(u);
2300 }
2301
Guido van Rossum681d79a1995-07-18 14:51:37 +00002302 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002303 if (x != NULL && posdefaults > 0) {
2304 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002305 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002306 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002307 x = NULL;
2308 break;
2309 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002310 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002311 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002312 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002313 }
2314 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002315 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002316 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002317 if (x != NULL && kwdefaults > 0) {
2318 v = PyDict_New();
2319 if (v == NULL) {
2320 Py_DECREF(x);
2321 x = NULL;
2322 break;
2323 }
2324 while (--kwdefaults >= 0) {
2325 w = POP(); /* default value */
2326 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002327 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002328 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002329 Py_DECREF(w);
2330 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002331 }
2332 err = PyFunction_SetKwDefaults(x, v);
2333 Py_DECREF(v);
2334 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002335 PUSH(x);
2336 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002337 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002338
2339 case BUILD_SLICE:
2340 if (oparg == 3)
2341 w = POP();
2342 else
2343 w = NULL;
2344 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002345 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002346 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002347 Py_DECREF(u);
2348 Py_DECREF(v);
2349 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002350 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002351 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002352 break;
2353
Fred Drakeef8ace32000-08-24 00:32:09 +00002354 case EXTENDED_ARG:
2355 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002356 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002357 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002358
Guido van Rossum374a9221991-04-04 10:40:29 +00002359 default:
2360 fprintf(stderr,
2361 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002362 PyCode_Addr2Line(f->f_code, f->f_lasti),
2363 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002364 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002365 why = WHY_EXCEPTION;
2366 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002367
2368#ifdef CASE_TOO_BIG
2369 }
2370#endif
2371
Guido van Rossum374a9221991-04-04 10:40:29 +00002372 } /* switch */
2373
2374 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002375
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002376 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002377
Guido van Rossum374a9221991-04-04 10:40:29 +00002378 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Guido van Rossum374a9221991-04-04 10:40:29 +00002380 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002381 if (err == 0 && x != NULL) {
2382#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002383 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002384 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002385 fprintf(stderr,
2386 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002387 else {
2388#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002389 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002390 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002391#ifdef CHECKEXC
2392 }
2393#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002394 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002395 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002396 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002397 err = 0;
2398 }
2399
Guido van Rossum374a9221991-04-04 10:40:29 +00002400 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002401
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002402 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002403 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002404 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002405 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002406 why = WHY_EXCEPTION;
2407 }
2408 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002409#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002410 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002411 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002412 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002413 char buf[1024];
2414 sprintf(buf, "Stack unwind with exception "
2415 "set and why=%d", why);
2416 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002417 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002418 }
2419#endif
2420
2421 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002422
Guido van Rossum374a9221991-04-04 10:40:29 +00002423 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002424 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002425
Fred Drake8f51f542001-10-04 14:48:42 +00002426 if (tstate->c_tracefunc != NULL)
2427 call_exc_trace(tstate->c_tracefunc,
2428 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002429 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002430
Guido van Rossum374a9221991-04-04 10:40:29 +00002431 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002432
Guido van Rossum374a9221991-04-04 10:40:29 +00002433 if (why == WHY_RERAISE)
2434 why = WHY_EXCEPTION;
2435
2436 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002437
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002438fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002439 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002440 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002441
Tim Peters8a5c3c72004-04-05 19:36:21 +00002442 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002443 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2444 /* For a continue inside a try block,
2445 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002446 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2447 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002448 why = WHY_NOT;
2449 JUMPTO(PyInt_AS_LONG(retval));
2450 Py_DECREF(retval);
2451 break;
2452 }
2453
Guido van Rossum374a9221991-04-04 10:40:29 +00002454 while (STACK_LEVEL() > b->b_level) {
2455 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002456 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002457 }
2458 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2459 why = WHY_NOT;
2460 JUMPTO(b->b_handler);
2461 break;
2462 }
2463 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002464 (b->b_type == SETUP_EXCEPT &&
2465 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002466 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002467 PyObject *exc, *val, *tb;
2468 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002469 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002470 val = Py_None;
2471 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002472 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002473 /* Make the raw exception data
2474 available to the handler,
2475 so a program can emulate the
2476 Python main loop. Don't do
2477 this for 'finally'. */
2478 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002479 PyErr_NormalizeException(
2480 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002481 set_exc_info(tstate,
2482 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002483 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002484 if (tb == NULL) {
2485 Py_INCREF(Py_None);
2486 PUSH(Py_None);
2487 } else
2488 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002489 PUSH(val);
2490 PUSH(exc);
2491 }
2492 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002493 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002494 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002495 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002496 PUSH(v);
2497 }
2498 why = WHY_NOT;
2499 JUMPTO(b->b_handler);
2500 break;
2501 }
2502 } /* unwind stack */
2503
2504 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002505
Guido van Rossum374a9221991-04-04 10:40:29 +00002506 if (why != WHY_NOT)
2507 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002508 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002509
Guido van Rossum374a9221991-04-04 10:40:29 +00002510 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002511
Tim Peters8a5c3c72004-04-05 19:36:21 +00002512 assert(why != WHY_YIELD);
2513 /* Pop remaining stack entries. */
2514 while (!EMPTY()) {
2515 v = POP();
2516 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002517 }
2518
Tim Peters8a5c3c72004-04-05 19:36:21 +00002519 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002520 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002521
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002522fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002523 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002524 if (tstate->c_tracefunc) {
2525 if (why == WHY_RETURN || why == WHY_YIELD) {
2526 if (call_trace(tstate->c_tracefunc,
2527 tstate->c_traceobj, f,
2528 PyTrace_RETURN, retval)) {
2529 Py_XDECREF(retval);
2530 retval = NULL;
2531 why = WHY_EXCEPTION;
2532 }
2533 }
2534 else if (why == WHY_EXCEPTION) {
2535 call_trace_protected(tstate->c_tracefunc,
2536 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002537 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002538 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002539 }
Fred Drake8f51f542001-10-04 14:48:42 +00002540 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002541 if (why == WHY_EXCEPTION)
2542 call_trace_protected(tstate->c_profilefunc,
2543 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002544 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002545 else if (call_trace(tstate->c_profilefunc,
2546 tstate->c_profileobj, f,
2547 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002548 Py_XDECREF(retval);
2549 retval = NULL;
2550 why = WHY_EXCEPTION;
2551 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002552 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002553 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002554
Thomas Wouters477c8d52006-05-27 19:21:47 +00002555 if (tstate->frame->f_exc_type != NULL)
2556 reset_exc_info(tstate);
2557 else {
2558 assert(tstate->frame->f_exc_value == NULL);
2559 assert(tstate->frame->f_exc_traceback == NULL);
2560 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002561
Tim Peters5ca576e2001-06-18 22:08:13 +00002562 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002563 exit_eval_frame:
2564 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002565 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002566
Guido van Rossum96a42c81992-01-12 02:29:51 +00002567 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002568}
2569
Guido van Rossumc2e20742006-02-27 22:32:47 +00002570/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002571 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002572 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002573
Tim Peters6d6c1a32001-08-02 04:15:00 +00002574PyObject *
2575PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002576 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002577 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002578{
2579 register PyFrameObject *f;
2580 register PyObject *retval = NULL;
2581 register PyObject **fastlocals, **freevars;
2582 PyThreadState *tstate = PyThreadState_GET();
2583 PyObject *x, *u;
2584
2585 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002586 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002587 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002588 return NULL;
2589 }
2590
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002591 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002592 assert(globals != NULL);
2593 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002594 if (f == NULL)
2595 return NULL;
2596
2597 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002598 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002599
2600 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002601 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002602 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2603 int i;
2604 int n = argcount;
2605 PyObject *kwdict = NULL;
2606 if (co->co_flags & CO_VARKEYWORDS) {
2607 kwdict = PyDict_New();
2608 if (kwdict == NULL)
2609 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002610 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002611 if (co->co_flags & CO_VARARGS)
2612 i++;
2613 SETLOCAL(i, kwdict);
2614 }
2615 if (argcount > co->co_argcount) {
2616 if (!(co->co_flags & CO_VARARGS)) {
2617 PyErr_Format(PyExc_TypeError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002618 "%S() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002619 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002620 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002621 defcount ? "at most" : "exactly",
2622 co->co_argcount,
2623 kwcount ? "non-keyword " : "",
2624 co->co_argcount == 1 ? "" : "s",
2625 argcount);
2626 goto fail;
2627 }
2628 n = co->co_argcount;
2629 }
2630 for (i = 0; i < n; i++) {
2631 x = args[i];
2632 Py_INCREF(x);
2633 SETLOCAL(i, x);
2634 }
2635 if (co->co_flags & CO_VARARGS) {
2636 u = PyTuple_New(argcount - n);
2637 if (u == NULL)
2638 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002639 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002640 for (i = n; i < argcount; i++) {
2641 x = args[i];
2642 Py_INCREF(x);
2643 PyTuple_SET_ITEM(u, i-n, x);
2644 }
2645 }
2646 for (i = 0; i < kwcount; i++) {
2647 PyObject *keyword = kws[2*i];
2648 PyObject *value = kws[2*i + 1];
2649 int j;
Guido van Rossum572dbf82007-04-27 23:53:51 +00002650 if (keyword == NULL || !(PyString_Check(keyword) || PyUnicode_Check(keyword))) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002651 PyErr_Format(PyExc_TypeError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002652 "%S() keywords must be strings",
2653 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00002654 goto fail;
2655 }
2656 /* XXX slow -- speed up using dictionary? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002657 for (j = 0;
2658 j < co->co_argcount + co->co_kwonlyargcount;
2659 j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002660 PyObject *nm = PyTuple_GET_ITEM(
2661 co->co_varnames, j);
2662 int cmp = PyObject_RichCompareBool(
2663 keyword, nm, Py_EQ);
2664 if (cmp > 0)
2665 break;
2666 else if (cmp < 0)
2667 goto fail;
2668 }
2669 /* Check errors from Compare */
2670 if (PyErr_Occurred())
2671 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002672 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002673 if (kwdict == NULL) {
2674 PyErr_Format(PyExc_TypeError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002675 "%S() got an unexpected "
2676 "keyword argument '%S'",
2677 co->co_name,
2678 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00002679 goto fail;
2680 }
2681 PyDict_SetItem(kwdict, keyword, value);
2682 }
2683 else {
2684 if (GETLOCAL(j) != NULL) {
2685 PyErr_Format(PyExc_TypeError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002686 "%S() got multiple "
Tim Peters5ca576e2001-06-18 22:08:13 +00002687 "values for keyword "
Walter Dörwald573c08c2007-05-25 15:46:59 +00002688 "argument '%S'",
2689 co->co_name,
2690 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00002691 goto fail;
2692 }
2693 Py_INCREF(value);
2694 SETLOCAL(j, value);
2695 }
2696 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002697 if (co->co_kwonlyargcount > 0) {
2698 for (i = co->co_argcount;
2699 i < co->co_argcount + co->co_kwonlyargcount;
2700 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00002701 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002702 if (GETLOCAL(i) != NULL)
2703 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00002704 name = PyTuple_GET_ITEM(co->co_varnames, i);
2705 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002706 if (kwdefs != NULL)
2707 def = PyDict_GetItem(kwdefs, name);
2708 if (def != NULL) {
2709 Py_INCREF(def);
2710 SETLOCAL(i, def);
2711 continue;
2712 }
2713 PyErr_Format(PyExc_TypeError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002714 "%S() needs keyword-only argument %S",
2715 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002716 goto fail;
2717 }
2718 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002719 if (argcount < co->co_argcount) {
2720 int m = co->co_argcount - defcount;
2721 for (i = argcount; i < m; i++) {
2722 if (GETLOCAL(i) == NULL) {
2723 PyErr_Format(PyExc_TypeError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002724 "%S() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002725 "%spositional argument%s "
2726 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002727 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002728 ((co->co_flags & CO_VARARGS) ||
2729 defcount) ? "at least"
2730 : "exactly",
2731 m, kwcount ? "non-keyword " : "",
2732 m == 1 ? "" : "s", i);
2733 goto fail;
2734 }
2735 }
2736 if (n > m)
2737 i = n - m;
2738 else
2739 i = 0;
2740 for (; i < defcount; i++) {
2741 if (GETLOCAL(m+i) == NULL) {
2742 PyObject *def = defs[i];
2743 Py_INCREF(def);
2744 SETLOCAL(m+i, def);
2745 }
2746 }
2747 }
2748 }
2749 else {
2750 if (argcount > 0 || kwcount > 0) {
2751 PyErr_Format(PyExc_TypeError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002752 "%S() takes no arguments (%d given)",
2753 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002754 argcount + kwcount);
2755 goto fail;
2756 }
2757 }
2758 /* Allocate and initialize storage for cell vars, and copy free
2759 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002760 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002761 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002762 char *cellname, *argname;
2763 PyObject *c;
2764
2765 nargs = co->co_argcount;
2766 if (co->co_flags & CO_VARARGS)
2767 nargs++;
2768 if (co->co_flags & CO_VARKEYWORDS)
2769 nargs++;
2770
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771 /* Initialize each cell var, taking into account
2772 cell vars that are initialized from arguments.
2773
2774 Should arrange for the compiler to put cellvars
2775 that are arguments at the beginning of the cellvars
2776 list so that we can march over it more efficiently?
2777 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002778 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002779 cellname = PyString_AS_STRING(
2780 PyTuple_GET_ITEM(co->co_cellvars, i));
2781 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002783 argname = PyString_AS_STRING(
2784 PyTuple_GET_ITEM(co->co_varnames, j));
2785 if (strcmp(cellname, argname) == 0) {
2786 c = PyCell_New(GETLOCAL(j));
2787 if (c == NULL)
2788 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002789 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002790 found = 1;
2791 break;
2792 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002793 }
2794 if (found == 0) {
2795 c = PyCell_New(NULL);
2796 if (c == NULL)
2797 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002798 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002799 }
2800 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002801 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002802 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002803 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002804 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002805 PyObject *o = PyTuple_GET_ITEM(closure, i);
2806 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002807 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002808 }
2809 }
2810
Tim Peters5ca576e2001-06-18 22:08:13 +00002811 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002812 /* Don't need to keep the reference to f_back, it will be set
2813 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002814 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002815 f->f_back = NULL;
2816
Jeremy Hylton985eba52003-02-05 23:13:00 +00002817 PCALL(PCALL_GENERATOR);
2818
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002819 /* Create a new generator that owns the ready to run frame
2820 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002821 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002822 }
2823
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002824 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002825
2826 fail: /* Jump here from prelude on failure */
2827
Tim Petersb13680b2001-11-27 23:29:29 +00002828 /* decref'ing the frame can cause __del__ methods to get invoked,
2829 which can call back into Python. While we're done with the
2830 current Python frame (f), the associated C stack is still in use,
2831 so recursion_depth must be boosted for the duration.
2832 */
2833 assert(tstate != NULL);
2834 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002835 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002836 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002837 return retval;
2838}
2839
2840
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002841/* Implementation notes for set_exc_info() and reset_exc_info():
2842
2843- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2844 'exc_traceback'. These always travel together.
2845
2846- tstate->curexc_ZZZ is the "hot" exception that is set by
2847 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2848
2849- Once an exception is caught by an except clause, it is transferred
2850 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2851 can pick it up. This is the primary task of set_exc_info().
Thomas Wouters477c8d52006-05-27 19:21:47 +00002852 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002853
2854- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2855
2856 Long ago, when none of this existed, there were just a few globals:
2857 one set corresponding to the "hot" exception, and one set
2858 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2859 globals; they were simply stored as sys.exc_ZZZ. For backwards
2860 compatibility, they still are!) The problem was that in code like
2861 this:
2862
2863 try:
2864 "something that may fail"
2865 except "some exception":
2866 "do something else first"
2867 "print the exception from sys.exc_ZZZ."
2868
2869 if "do something else first" invoked something that raised and caught
2870 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2871 cause of subtle bugs. I fixed this by changing the semantics as
2872 follows:
2873
2874 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2875 *in that frame*.
2876
2877 - But initially, and as long as no exception is caught in a given
2878 frame, sys.exc_ZZZ will hold the last exception caught in the
2879 previous frame (or the frame before that, etc.).
2880
2881 The first bullet fixed the bug in the above example. The second
2882 bullet was for backwards compatibility: it was (and is) common to
2883 have a function that is called when an exception is caught, and to
2884 have that function access the caught exception via sys.exc_ZZZ.
2885 (Example: traceback.print_exc()).
2886
2887 At the same time I fixed the problem that sys.exc_ZZZ weren't
2888 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2889 but that's really a separate improvement.
2890
2891 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2892 variables to what they were before the current frame was called. The
2893 set_exc_info() function saves them on the frame so that
2894 reset_exc_info() can restore them. The invariant is that
2895 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2896 exception (where "catching" an exception applies only to successful
2897 except clauses); and if the current frame ever caught an exception,
2898 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2899 at the start of the current frame.
2900
2901*/
2902
Guido van Rossuma027efa1997-05-05 20:56:21 +00002903static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002904set_exc_info(PyThreadState *tstate,
2905 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002906{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002907 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002908 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002909
Thomas Wouters477c8d52006-05-27 19:21:47 +00002910 assert(type != NULL);
2911 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002912 if (frame->f_exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002913 assert(frame->f_exc_value == NULL);
2914 assert(frame->f_exc_traceback == NULL);
2915 /* This frame didn't catch an exception before. */
2916 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002917 if (tstate->exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002918 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002919 Py_INCREF(Py_None);
2920 tstate->exc_type = Py_None;
2921 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002922 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002923 Py_XINCREF(tstate->exc_value);
2924 Py_XINCREF(tstate->exc_traceback);
2925 frame->f_exc_type = tstate->exc_type;
2926 frame->f_exc_value = tstate->exc_value;
2927 frame->f_exc_traceback = tstate->exc_traceback;
2928 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002929 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002930 tmp_type = tstate->exc_type;
2931 tmp_value = tstate->exc_value;
2932 tmp_tb = tstate->exc_traceback;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002933 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002934 Py_XINCREF(value);
2935 Py_XINCREF(tb);
2936 tstate->exc_type = type;
2937 tstate->exc_value = value;
2938 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002939 Py_XDECREF(tmp_type);
2940 Py_XDECREF(tmp_value);
2941 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002942 /* For b/w compatibility */
2943 PySys_SetObject("exc_type", type);
2944 PySys_SetObject("exc_value", value);
2945 PySys_SetObject("exc_traceback", tb);
2946}
2947
2948static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002949reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002950{
2951 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002952 PyObject *tmp_type, *tmp_value, *tmp_tb;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002953
2954 /* It's a precondition that the thread state's frame caught an
2955 * exception -- verify in a debug build.
2956 */
2957 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002958 frame = tstate->frame;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002959 assert(frame != NULL);
2960 assert(frame->f_exc_type != NULL);
2961
2962 /* Copy the frame's exception info back to the thread state. */
2963 tmp_type = tstate->exc_type;
2964 tmp_value = tstate->exc_value;
2965 tmp_tb = tstate->exc_traceback;
2966 Py_INCREF(frame->f_exc_type);
2967 Py_XINCREF(frame->f_exc_value);
2968 Py_XINCREF(frame->f_exc_traceback);
2969 tstate->exc_type = frame->f_exc_type;
2970 tstate->exc_value = frame->f_exc_value;
2971 tstate->exc_traceback = frame->f_exc_traceback;
2972 Py_XDECREF(tmp_type);
2973 Py_XDECREF(tmp_value);
2974 Py_XDECREF(tmp_tb);
2975
2976 /* For b/w compatibility */
2977 PySys_SetObject("exc_type", frame->f_exc_type);
2978 PySys_SetObject("exc_value", frame->f_exc_value);
2979 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
2980
2981 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002982 tmp_type = frame->f_exc_type;
2983 tmp_value = frame->f_exc_value;
2984 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00002985 frame->f_exc_type = NULL;
2986 frame->f_exc_value = NULL;
2987 frame->f_exc_traceback = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002988 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002989 Py_XDECREF(tmp_value);
2990 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002991}
2992
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002993/* Logic for the raise statement (too complicated for inlining).
2994 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00002995static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002996do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00002997{
Guido van Rossumd295f121998-04-09 21:39:57 +00002998 if (type == NULL) {
2999 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003000 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003001 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3002 value = tstate->exc_value;
3003 tb = tstate->exc_traceback;
3004 Py_XINCREF(type);
3005 Py_XINCREF(value);
3006 Py_XINCREF(tb);
3007 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003008
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003009 /* We support the following forms of raise:
3010 raise <class>, <classinstance>
3011 raise <class>, <argument tuple>
3012 raise <class>, None
3013 raise <class>, <argument>
3014 raise <classinstance>, None
3015 raise <string>, <object>
3016 raise <string>, None
3017
3018 An omitted second argument is the same as None.
3019
3020 In addition, raise <tuple>, <anything> is the same as
3021 raising the tuple's first item (and it better have one!);
3022 this rule is applied recursively.
3023
3024 Finally, an optional third argument can be supplied, which
3025 gives the traceback to be substituted (useful when
3026 re-raising an exception after examining it). */
3027
3028 /* First, check the traceback argument, replacing None with
3029 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003030 if (tb == Py_None) {
3031 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003032 tb = NULL;
3033 }
3034 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003035 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003036 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003037 goto raise_error;
3038 }
3039
3040 /* Next, replace a missing value with None */
3041 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003042 value = Py_None;
3043 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003044 }
3045
3046 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003047 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3048 PyObject *tmp = type;
3049 type = PyTuple_GET_ITEM(type, 0);
3050 Py_INCREF(type);
3051 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003052 }
3053
Guido van Rossum45aecf42006-03-15 04:58:47 +00003054 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003055 PyErr_NormalizeException(&type, &value, &tb);
3056
Brett Cannonbf364092006-03-01 04:25:17 +00003057 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003058 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003059 if (value != Py_None) {
3060 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003061 "instance exception may not have a separate value");
3062 goto raise_error;
3063 }
3064 else {
3065 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003066 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003067 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003068 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003069 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003070 }
3071 }
3072 else {
3073 /* Not something you can raise. You get an exception
3074 anyway, just not what you specified :-) */
Guido van Rossum45aecf42006-03-15 04:58:47 +00003075 PyErr_SetString(PyExc_TypeError,
3076 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003077 goto raise_error;
3078 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003079 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003080 if (tb == NULL)
3081 return WHY_EXCEPTION;
3082 else
3083 return WHY_RERAISE;
3084 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003085 Py_XDECREF(value);
3086 Py_XDECREF(type);
3087 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003088 return WHY_EXCEPTION;
3089}
3090
Tim Petersd6d010b2001-06-21 02:49:55 +00003091/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003092 sp). Return 1 for success, 0 if error.
3093
3094 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3095 with a variable target.
3096*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003097
Barry Warsawe42b18f1997-08-25 22:13:04 +00003098static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003099unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003100{
Guido van Rossum0368b722007-05-11 16:50:42 +00003101 int i = 0, j = 0;
3102 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003103 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003104 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003105 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003106
Tim Petersd6d010b2001-06-21 02:49:55 +00003107 assert(v != NULL);
3108
3109 it = PyObject_GetIter(v);
3110 if (it == NULL)
3111 goto Error;
3112
3113 for (; i < argcnt; i++) {
3114 w = PyIter_Next(it);
3115 if (w == NULL) {
3116 /* Iterator done, via error or exhaustion. */
3117 if (!PyErr_Occurred()) {
3118 PyErr_Format(PyExc_ValueError,
3119 "need more than %d value%s to unpack",
3120 i, i == 1 ? "" : "s");
3121 }
3122 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003123 }
3124 *--sp = w;
3125 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003126
Guido van Rossum0368b722007-05-11 16:50:42 +00003127 if (argcntafter == -1) {
3128 /* We better have exhausted the iterator now. */
3129 w = PyIter_Next(it);
3130 if (w == NULL) {
3131 if (PyErr_Occurred())
3132 goto Error;
3133 Py_DECREF(it);
3134 return 1;
3135 }
3136 Py_DECREF(w);
3137 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3138 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003139 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003140
3141 l = PySequence_List(it);
3142 if (l == NULL)
3143 goto Error;
3144 *--sp = l;
3145 i++;
3146
3147 ll = PyList_GET_SIZE(l);
3148 if (ll < argcntafter) {
3149 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3150 argcnt + ll);
3151 goto Error;
3152 }
3153
3154 /* Pop the "after-variable" args off the list. */
3155 for (j = argcntafter; j > 0; j--, i++) {
3156 *--sp = PyList_GET_ITEM(l, ll - j);
3157 }
3158 /* Resize the list. */
3159 ((PyListObject *)l)->ob_size = ll - argcntafter;
3160 Py_DECREF(it);
3161 return 1;
3162
Tim Petersd6d010b2001-06-21 02:49:55 +00003163Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003164 for (; i > 0; i--, sp++)
3165 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003166 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003167 return 0;
3168}
3169
3170
Guido van Rossum96a42c81992-01-12 02:29:51 +00003171#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003172static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003173prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003174{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003175 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003176 if (PyObject_Print(v, stdout, 0) != 0)
3177 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003178 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003179 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003180}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003181#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003182
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003183static void
Fred Drake5755ce62001-06-27 19:19:46 +00003184call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003185{
Guido van Rossumb209a111997-04-29 18:18:01 +00003186 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003187 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003188 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003189 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003190 value = Py_None;
3191 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003192 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003193 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003194 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003195 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003196 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003197 }
Fred Drake5755ce62001-06-27 19:19:46 +00003198 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003199 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003200 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003201 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003202 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003203 Py_XDECREF(type);
3204 Py_XDECREF(value);
3205 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003206 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003207}
3208
Fred Drake4ec5d562001-10-04 19:26:43 +00003209static void
3210call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003211 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003212{
3213 PyObject *type, *value, *traceback;
3214 int err;
3215 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003216 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003217 if (err == 0)
3218 PyErr_Restore(type, value, traceback);
3219 else {
3220 Py_XDECREF(type);
3221 Py_XDECREF(value);
3222 Py_XDECREF(traceback);
3223 }
3224}
3225
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003226static int
Fred Drake5755ce62001-06-27 19:19:46 +00003227call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3228 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003229{
Fred Drake5755ce62001-06-27 19:19:46 +00003230 register PyThreadState *tstate = frame->f_tstate;
3231 int result;
3232 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003233 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003234 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003235 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003236 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003237 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3238 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003239 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003240 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003241}
3242
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003243PyObject *
3244_PyEval_CallTracing(PyObject *func, PyObject *args)
3245{
3246 PyFrameObject *frame = PyEval_GetFrame();
3247 PyThreadState *tstate = frame->f_tstate;
3248 int save_tracing = tstate->tracing;
3249 int save_use_tracing = tstate->use_tracing;
3250 PyObject *result;
3251
3252 tstate->tracing = 0;
3253 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3254 || (tstate->c_profilefunc != NULL));
3255 result = PyObject_Call(func, args, NULL);
3256 tstate->tracing = save_tracing;
3257 tstate->use_tracing = save_use_tracing;
3258 return result;
3259}
3260
Michael W. Hudson006c7522002-11-08 13:08:46 +00003261static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003262maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003263 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3264 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003265{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003266 int result = 0;
3267
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003268 /* If the last instruction executed isn't in the current
3269 instruction window, reset the window. If the last
3270 instruction happens to fall at the start of a line or if it
3271 represents a jump backwards, call the trace function.
3272 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003273 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003274 int line;
3275 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003276
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003277 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3278 &bounds);
3279 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003280 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003281 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003282 PyTrace_LINE, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003283 }
3284 *instr_lb = bounds.ap_lower;
3285 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003286 }
Armin Rigobf57a142004-03-22 19:24:58 +00003287 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003288 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003289 }
3290 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003291 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003292}
3293
Fred Drake5755ce62001-06-27 19:19:46 +00003294void
3295PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003296{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003297 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003298 PyObject *temp = tstate->c_profileobj;
3299 Py_XINCREF(arg);
3300 tstate->c_profilefunc = NULL;
3301 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003302 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003303 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003304 Py_XDECREF(temp);
3305 tstate->c_profilefunc = func;
3306 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003307 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003308 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003309}
3310
3311void
3312PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3313{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003314 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003315 PyObject *temp = tstate->c_traceobj;
3316 Py_XINCREF(arg);
3317 tstate->c_tracefunc = NULL;
3318 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003319 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003320 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003321 Py_XDECREF(temp);
3322 tstate->c_tracefunc = func;
3323 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003324 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003325 tstate->use_tracing = ((func != NULL)
3326 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003327}
3328
Guido van Rossumb209a111997-04-29 18:18:01 +00003329PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003330PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003331{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003332 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003333 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003334 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003335 else
3336 return current_frame->f_builtins;
3337}
3338
Guido van Rossumb209a111997-04-29 18:18:01 +00003339PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003340PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003341{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003342 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003343 if (current_frame == NULL)
3344 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003345 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003346 return current_frame->f_locals;
3347}
3348
Guido van Rossumb209a111997-04-29 18:18:01 +00003349PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003350PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003351{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003352 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003353 if (current_frame == NULL)
3354 return NULL;
3355 else
3356 return current_frame->f_globals;
3357}
3358
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003359PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003360PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003361{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003362 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003363 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003364}
3365
Guido van Rossum6135a871995-01-09 17:53:26 +00003366int
Tim Peters5ba58662001-07-16 02:29:45 +00003367PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003368{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003369 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003370 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003371
3372 if (current_frame != NULL) {
3373 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003374 const int compilerflags = codeflags & PyCF_MASK;
3375 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003376 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003377 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003378 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003379#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003380 if (codeflags & CO_GENERATOR_ALLOWED) {
3381 result = 1;
3382 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3383 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003384#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003385 }
3386 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003387}
3388
Guido van Rossum3f5da241990-12-20 15:06:42 +00003389
Guido van Rossum681d79a1995-07-18 14:51:37 +00003390/* External interface to call any callable object.
3391 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003392
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003393#undef PyEval_CallObject
3394/* for backward compatibility: export this interface */
3395
Guido van Rossumb209a111997-04-29 18:18:01 +00003396PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003397PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003398{
Guido van Rossumb209a111997-04-29 18:18:01 +00003399 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003400}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003401#define PyEval_CallObject(func,arg) \
3402 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003403
Guido van Rossumb209a111997-04-29 18:18:01 +00003404PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003405PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003406{
Jeremy Hylton52820442001-01-03 23:52:36 +00003407 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003408
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003409 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003410 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003411 if (arg == NULL)
3412 return NULL;
3413 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003414 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003415 PyErr_SetString(PyExc_TypeError,
3416 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003417 return NULL;
3418 }
3419 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003420 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003421
Guido van Rossumb209a111997-04-29 18:18:01 +00003422 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003423 PyErr_SetString(PyExc_TypeError,
3424 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003425 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003426 return NULL;
3427 }
3428
Tim Peters6d6c1a32001-08-02 04:15:00 +00003429 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003430 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003431 return result;
3432}
3433
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003434const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003435PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003436{
3437 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003438 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003439 else if (PyFunction_Check(func))
3440 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3441 else if (PyCFunction_Check(func))
3442 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003443 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003444 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003445}
3446
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003447const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003448PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003449{
3450 if (PyMethod_Check(func))
3451 return "()";
3452 else if (PyFunction_Check(func))
3453 return "()";
3454 else if (PyCFunction_Check(func))
3455 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003456 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003457 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003458}
3459
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003460static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003461err_args(PyObject *func, int flags, int nargs)
3462{
3463 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003464 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003465 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003466 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003467 nargs);
3468 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003469 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003470 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003471 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003472 nargs);
3473}
3474
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003475#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003476if (tstate->use_tracing && tstate->c_profilefunc) { \
3477 if (call_trace(tstate->c_profilefunc, \
3478 tstate->c_profileobj, \
3479 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003480 func)) { \
3481 x = NULL; \
3482 } \
3483 else { \
3484 x = call; \
3485 if (tstate->c_profilefunc != NULL) { \
3486 if (x == NULL) { \
3487 call_trace_protected(tstate->c_profilefunc, \
3488 tstate->c_profileobj, \
3489 tstate->frame, PyTrace_C_EXCEPTION, \
3490 func); \
3491 /* XXX should pass (type, value, tb) */ \
3492 } else { \
3493 if (call_trace(tstate->c_profilefunc, \
3494 tstate->c_profileobj, \
3495 tstate->frame, PyTrace_C_RETURN, \
3496 func)) { \
3497 Py_DECREF(x); \
3498 x = NULL; \
3499 } \
3500 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003501 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003502 } \
3503} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003504 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003505 }
3506
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003507static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003508call_function(PyObject ***pp_stack, int oparg
3509#ifdef WITH_TSC
3510 , uint64* pintr0, uint64* pintr1
3511#endif
3512 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003513{
3514 int na = oparg & 0xff;
3515 int nk = (oparg>>8) & 0xff;
3516 int n = na + 2 * nk;
3517 PyObject **pfunc = (*pp_stack) - n - 1;
3518 PyObject *func = *pfunc;
3519 PyObject *x, *w;
3520
Jeremy Hylton985eba52003-02-05 23:13:00 +00003521 /* Always dispatch PyCFunction first, because these are
3522 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003523 */
3524 if (PyCFunction_Check(func) && nk == 0) {
3525 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003526 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003527
3528 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003529 if (flags & (METH_NOARGS | METH_O)) {
3530 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3531 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003532 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003533 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003534 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003535 else if (flags & METH_O && na == 1) {
3536 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003537 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003538 Py_DECREF(arg);
3539 }
3540 else {
3541 err_args(func, flags, na);
3542 x = NULL;
3543 }
3544 }
3545 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003546 PyObject *callargs;
3547 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003548 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003549 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003550 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003551 Py_XDECREF(callargs);
3552 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003553 } else {
3554 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3555 /* optimize access to bound methods */
3556 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003557 PCALL(PCALL_METHOD);
3558 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003559 Py_INCREF(self);
3560 func = PyMethod_GET_FUNCTION(func);
3561 Py_INCREF(func);
3562 Py_DECREF(*pfunc);
3563 *pfunc = self;
3564 na++;
3565 n++;
3566 } else
3567 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003568 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003569 if (PyFunction_Check(func))
3570 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003571 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003572 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003573 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003574 Py_DECREF(func);
3575 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003576
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003577 /* Clear the stack of the function object. Also removes
3578 the arguments in case they weren't consumed already
3579 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003580 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003581 while ((*pp_stack) > pfunc) {
3582 w = EXT_POP(*pp_stack);
3583 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003584 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003585 }
3586 return x;
3587}
3588
Jeremy Hylton192690e2002-08-16 18:36:11 +00003589/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003590 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003591 For the simplest case -- a function that takes only positional
3592 arguments and is called with only positional arguments -- it
3593 inlines the most primitive frame setup code from
3594 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3595 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003596*/
3597
3598static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003599fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003600{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003601 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003602 PyObject *globals = PyFunction_GET_GLOBALS(func);
3603 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003604 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003605 PyObject **d = NULL;
3606 int nd = 0;
3607
Jeremy Hylton985eba52003-02-05 23:13:00 +00003608 PCALL(PCALL_FUNCTION);
3609 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003610 if (argdefs == NULL && co->co_argcount == n &&
3611 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003612 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3613 PyFrameObject *f;
3614 PyObject *retval = NULL;
3615 PyThreadState *tstate = PyThreadState_GET();
3616 PyObject **fastlocals, **stack;
3617 int i;
3618
3619 PCALL(PCALL_FASTER_FUNCTION);
3620 assert(globals != NULL);
3621 /* XXX Perhaps we should create a specialized
3622 PyFrame_New() that doesn't take locals, but does
3623 take builtins without sanity checking them.
3624 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003625 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003626 f = PyFrame_New(tstate, co, globals, NULL);
3627 if (f == NULL)
3628 return NULL;
3629
3630 fastlocals = f->f_localsplus;
3631 stack = (*pp_stack) - n;
3632
3633 for (i = 0; i < n; i++) {
3634 Py_INCREF(*stack);
3635 fastlocals[i] = *stack++;
3636 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003637 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003638 ++tstate->recursion_depth;
3639 Py_DECREF(f);
3640 --tstate->recursion_depth;
3641 return retval;
3642 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003643 if (argdefs != NULL) {
3644 d = &PyTuple_GET_ITEM(argdefs, 0);
3645 nd = ((PyTupleObject *)argdefs)->ob_size;
3646 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003647 return PyEval_EvalCodeEx(co, globals,
3648 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003649 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003650 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003651}
3652
3653static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003654update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3655 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003656{
3657 PyObject *kwdict = NULL;
3658 if (orig_kwdict == NULL)
3659 kwdict = PyDict_New();
3660 else {
3661 kwdict = PyDict_Copy(orig_kwdict);
3662 Py_DECREF(orig_kwdict);
3663 }
3664 if (kwdict == NULL)
3665 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003666 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003667 int err;
3668 PyObject *value = EXT_POP(*pp_stack);
3669 PyObject *key = EXT_POP(*pp_stack);
3670 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003671 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003672 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003673 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003674 PyEval_GetFuncName(func),
3675 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003676 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003677 Py_DECREF(key);
3678 Py_DECREF(value);
3679 Py_DECREF(kwdict);
3680 return NULL;
3681 }
3682 err = PyDict_SetItem(kwdict, key, value);
3683 Py_DECREF(key);
3684 Py_DECREF(value);
3685 if (err) {
3686 Py_DECREF(kwdict);
3687 return NULL;
3688 }
3689 }
3690 return kwdict;
3691}
3692
3693static PyObject *
3694update_star_args(int nstack, int nstar, PyObject *stararg,
3695 PyObject ***pp_stack)
3696{
3697 PyObject *callargs, *w;
3698
3699 callargs = PyTuple_New(nstack + nstar);
3700 if (callargs == NULL) {
3701 return NULL;
3702 }
3703 if (nstar) {
3704 int i;
3705 for (i = 0; i < nstar; i++) {
3706 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3707 Py_INCREF(a);
3708 PyTuple_SET_ITEM(callargs, nstack + i, a);
3709 }
3710 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003711 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003712 w = EXT_POP(*pp_stack);
3713 PyTuple_SET_ITEM(callargs, nstack, w);
3714 }
3715 return callargs;
3716}
3717
3718static PyObject *
3719load_args(PyObject ***pp_stack, int na)
3720{
3721 PyObject *args = PyTuple_New(na);
3722 PyObject *w;
3723
3724 if (args == NULL)
3725 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003726 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003727 w = EXT_POP(*pp_stack);
3728 PyTuple_SET_ITEM(args, na, w);
3729 }
3730 return args;
3731}
3732
3733static PyObject *
3734do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3735{
3736 PyObject *callargs = NULL;
3737 PyObject *kwdict = NULL;
3738 PyObject *result = NULL;
3739
3740 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003741 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003742 if (kwdict == NULL)
3743 goto call_fail;
3744 }
3745 callargs = load_args(pp_stack, na);
3746 if (callargs == NULL)
3747 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003748#ifdef CALL_PROFILE
3749 /* At this point, we have to look at the type of func to
3750 update the call stats properly. Do it here so as to avoid
3751 exposing the call stats machinery outside ceval.c
3752 */
3753 if (PyFunction_Check(func))
3754 PCALL(PCALL_FUNCTION);
3755 else if (PyMethod_Check(func))
3756 PCALL(PCALL_METHOD);
3757 else if (PyType_Check(func))
3758 PCALL(PCALL_TYPE);
3759 else
3760 PCALL(PCALL_OTHER);
3761#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003762 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003763 call_fail:
3764 Py_XDECREF(callargs);
3765 Py_XDECREF(kwdict);
3766 return result;
3767}
3768
3769static PyObject *
3770ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3771{
3772 int nstar = 0;
3773 PyObject *callargs = NULL;
3774 PyObject *stararg = NULL;
3775 PyObject *kwdict = NULL;
3776 PyObject *result = NULL;
3777
3778 if (flags & CALL_FLAG_KW) {
3779 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003780 if (!PyDict_Check(kwdict)) {
3781 PyObject *d;
3782 d = PyDict_New();
3783 if (d == NULL)
3784 goto ext_call_fail;
3785 if (PyDict_Update(d, kwdict) != 0) {
3786 Py_DECREF(d);
3787 /* PyDict_Update raises attribute
3788 * error (percolated from an attempt
3789 * to get 'keys' attribute) instead of
3790 * a type error if its second argument
3791 * is not a mapping.
3792 */
3793 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3794 PyErr_Format(PyExc_TypeError,
3795 "%.200s%.200s argument after ** "
3796 "must be a mapping, not %.200s",
3797 PyEval_GetFuncName(func),
3798 PyEval_GetFuncDesc(func),
3799 kwdict->ob_type->tp_name);
3800 }
3801 goto ext_call_fail;
3802 }
3803 Py_DECREF(kwdict);
3804 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00003805 }
3806 }
3807 if (flags & CALL_FLAG_VAR) {
3808 stararg = EXT_POP(*pp_stack);
3809 if (!PyTuple_Check(stararg)) {
3810 PyObject *t = NULL;
3811 t = PySequence_Tuple(stararg);
3812 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003813 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3814 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003815 "%.200s%.200s argument after * "
3816 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003817 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003818 PyEval_GetFuncDesc(func),
3819 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003820 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003821 goto ext_call_fail;
3822 }
3823 Py_DECREF(stararg);
3824 stararg = t;
3825 }
3826 nstar = PyTuple_GET_SIZE(stararg);
3827 }
3828 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003829 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003830 if (kwdict == NULL)
3831 goto ext_call_fail;
3832 }
3833 callargs = update_star_args(na, nstar, stararg, pp_stack);
3834 if (callargs == NULL)
3835 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003836#ifdef CALL_PROFILE
3837 /* At this point, we have to look at the type of func to
3838 update the call stats properly. Do it here so as to avoid
3839 exposing the call stats machinery outside ceval.c
3840 */
3841 if (PyFunction_Check(func))
3842 PCALL(PCALL_FUNCTION);
3843 else if (PyMethod_Check(func))
3844 PCALL(PCALL_METHOD);
3845 else if (PyType_Check(func))
3846 PCALL(PCALL_TYPE);
3847 else
3848 PCALL(PCALL_OTHER);
3849#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003850 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003851 ext_call_fail:
3852 Py_XDECREF(callargs);
3853 Py_XDECREF(kwdict);
3854 Py_XDECREF(stararg);
3855 return result;
3856}
3857
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003858/* Extract a slice index from a PyInt or PyLong or an object with the
3859 nb_index slot defined, and store in *pi.
3860 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3861 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 +00003862 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003863*/
Tim Petersb5196382001-12-16 19:44:20 +00003864/* Note: If v is NULL, return success without storing into *pi. This
3865 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3866 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003867*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003868int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003869_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003870{
Tim Petersb5196382001-12-16 19:44:20 +00003871 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003872 Py_ssize_t x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003873 if (PyInt_CheckExact(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003874 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3875 however, it looks like it should be AsSsize_t.
3876 There should be a comment here explaining why.
3877 */
3878 x = PyInt_AS_LONG(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003879 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003880 else if (PyIndex_Check(v)) {
3881 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003882 if (x == -1 && PyErr_Occurred())
3883 return 0;
3884 }
3885 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003886 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003887 "slice indices must be integers or "
3888 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003889 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003890 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003891 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003892 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003893 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003894}
3895
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003896#undef ISINDEX
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003897#define ISINDEX(x) ((x) == NULL || \
3898 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003899
Guido van Rossumb209a111997-04-29 18:18:01 +00003900static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003901apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003902{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003903 PyTypeObject *tp = u->ob_type;
3904 PySequenceMethods *sq = tp->tp_as_sequence;
3905
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003906 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003907 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003908 if (!_PyEval_SliceIndex(v, &ilow))
3909 return NULL;
3910 if (!_PyEval_SliceIndex(w, &ihigh))
3911 return NULL;
3912 return PySequence_GetSlice(u, ilow, ihigh);
3913 }
3914 else {
3915 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003916 if (slice != NULL) {
3917 PyObject *res = PyObject_GetItem(u, slice);
3918 Py_DECREF(slice);
3919 return res;
3920 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003921 else
3922 return NULL;
3923 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003924}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003925
3926static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003927assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3928 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003929{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003930 PyTypeObject *tp = u->ob_type;
3931 PySequenceMethods *sq = tp->tp_as_sequence;
3932
Guido van Rossumd8faa362007-04-27 19:54:29 +00003933 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003934 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003935 if (!_PyEval_SliceIndex(v, &ilow))
3936 return -1;
3937 if (!_PyEval_SliceIndex(w, &ihigh))
3938 return -1;
3939 if (x == NULL)
3940 return PySequence_DelSlice(u, ilow, ihigh);
3941 else
3942 return PySequence_SetSlice(u, ilow, ihigh, x);
3943 }
3944 else {
3945 PyObject *slice = PySlice_New(v, w, NULL);
3946 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003947 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003948 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003949 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003950 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003951 res = PyObject_DelItem(u, slice);
3952 Py_DECREF(slice);
3953 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003954 }
3955 else
3956 return -1;
3957 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003958}
3959
Brett Cannon39590462007-02-26 22:01:14 +00003960#define CANNOT_CATCH_MSG "catching classes that do not inherit from"\
3961 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00003962
Guido van Rossumb209a111997-04-29 18:18:01 +00003963static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003964cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003965{
Guido van Rossumac7be682001-01-17 15:42:30 +00003966 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003967 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003968 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003969 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003970 break;
3971 case PyCmp_IS_NOT:
3972 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003973 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003974 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003975 res = PySequence_Contains(w, v);
3976 if (res < 0)
3977 return NULL;
3978 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003979 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003980 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003981 if (res < 0)
3982 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003983 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003984 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003985 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003986 if (PyTuple_Check(w)) {
3987 Py_ssize_t i, length;
3988 length = PyTuple_Size(w);
3989 for (i = 0; i < length; i += 1) {
3990 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00003991 if (!PyExceptionClass_Check(exc)) {
3992 PyErr_SetString(PyExc_TypeError,
3993 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00003994 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003995 }
3996 }
3997 }
3998 else {
Brett Cannon39590462007-02-26 22:01:14 +00003999 if (!PyExceptionClass_Check(w)) {
4000 PyErr_SetString(PyExc_TypeError,
4001 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004002 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004003 }
4004 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004005 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004006 break;
4007 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004008 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004009 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004010 v = res ? Py_True : Py_False;
4011 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004012 return v;
4013}
4014
Thomas Wouters52152252000-08-17 22:55:00 +00004015static PyObject *
4016import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004017{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004018 PyObject *x;
4019
4020 x = PyObject_GetAttr(v, name);
4021 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004022 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004023 }
Thomas Wouters52152252000-08-17 22:55:00 +00004024 return x;
4025}
Guido van Rossumac7be682001-01-17 15:42:30 +00004026
Thomas Wouters52152252000-08-17 22:55:00 +00004027static int
4028import_all_from(PyObject *locals, PyObject *v)
4029{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004030 PyObject *all = PyObject_GetAttrString(v, "__all__");
4031 PyObject *dict, *name, *value;
4032 int skip_leading_underscores = 0;
4033 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004034
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004035 if (all == NULL) {
4036 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4037 return -1; /* Unexpected error */
4038 PyErr_Clear();
4039 dict = PyObject_GetAttrString(v, "__dict__");
4040 if (dict == NULL) {
4041 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4042 return -1;
4043 PyErr_SetString(PyExc_ImportError,
4044 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004045 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004046 }
4047 all = PyMapping_Keys(dict);
4048 Py_DECREF(dict);
4049 if (all == NULL)
4050 return -1;
4051 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004052 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004053
4054 for (pos = 0, err = 0; ; pos++) {
4055 name = PySequence_GetItem(all, pos);
4056 if (name == NULL) {
4057 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4058 err = -1;
4059 else
4060 PyErr_Clear();
4061 break;
4062 }
4063 if (skip_leading_underscores &&
4064 PyString_Check(name) &&
4065 PyString_AS_STRING(name)[0] == '_')
4066 {
4067 Py_DECREF(name);
4068 continue;
4069 }
4070 value = PyObject_GetAttr(v, name);
4071 if (value == NULL)
4072 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004073 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004074 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004075 else
4076 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004077 Py_DECREF(name);
4078 Py_XDECREF(value);
4079 if (err != 0)
4080 break;
4081 }
4082 Py_DECREF(all);
4083 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004084}
4085
Guido van Rossumac7be682001-01-17 15:42:30 +00004086static void
Paul Prescode68140d2000-08-30 20:25:01 +00004087format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4088{
4089 char *obj_str;
4090
4091 if (!obj)
4092 return;
4093
4094 obj_str = PyString_AsString(obj);
4095 if (!obj_str)
4096 return;
4097
4098 PyErr_Format(exc, format_str, obj_str);
4099}
Guido van Rossum950361c1997-01-24 13:49:28 +00004100
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004101static PyObject *
4102string_concatenate(PyObject *v, PyObject *w,
4103 PyFrameObject *f, unsigned char *next_instr)
4104{
4105 /* This function implements 'variable += expr' when both arguments
4106 are strings. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004107 Py_ssize_t v_len = PyString_GET_SIZE(v);
4108 Py_ssize_t w_len = PyString_GET_SIZE(w);
4109 Py_ssize_t new_len = v_len + w_len;
4110 if (new_len < 0) {
4111 PyErr_SetString(PyExc_OverflowError,
4112 "strings are too large to concat");
4113 return NULL;
4114 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004115
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004116 if (v->ob_refcnt == 2) {
4117 /* In the common case, there are 2 references to the value
4118 * stored in 'variable' when the += is performed: one on the
4119 * value stack (in 'v') and one still stored in the 'variable'.
4120 * We try to delete the variable now to reduce the refcnt to 1.
4121 */
4122 switch (*next_instr) {
4123 case STORE_FAST:
4124 {
4125 int oparg = PEEKARG();
4126 PyObject **fastlocals = f->f_localsplus;
4127 if (GETLOCAL(oparg) == v)
4128 SETLOCAL(oparg, NULL);
4129 break;
4130 }
4131 case STORE_DEREF:
4132 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004133 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004134 PyObject *c = freevars[PEEKARG()];
4135 if (PyCell_GET(c) == v)
4136 PyCell_Set(c, NULL);
4137 break;
4138 }
4139 case STORE_NAME:
4140 {
4141 PyObject *names = f->f_code->co_names;
4142 PyObject *name = GETITEM(names, PEEKARG());
4143 PyObject *locals = f->f_locals;
4144 if (PyDict_CheckExact(locals) &&
4145 PyDict_GetItem(locals, name) == v) {
4146 if (PyDict_DelItem(locals, name) != 0) {
4147 PyErr_Clear();
4148 }
4149 }
4150 break;
4151 }
4152 }
4153 }
4154
Armin Rigo618fbf52004-08-07 20:58:32 +00004155 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004156 /* Now we own the last reference to 'v', so we can resize it
4157 * in-place.
4158 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004159 if (_PyString_Resize(&v, new_len) != 0) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004160 /* XXX if _PyString_Resize() fails, 'v' has been
4161 * deallocated so it cannot be put back into 'variable'.
4162 * The MemoryError is raised when there is no value in
4163 * 'variable', which might (very remotely) be a cause
4164 * of incompatibilities.
4165 */
4166 return NULL;
4167 }
4168 /* copy 'w' into the newly allocated area of 'v' */
4169 memcpy(PyString_AS_STRING(v) + v_len,
4170 PyString_AS_STRING(w), w_len);
4171 return v;
4172 }
4173 else {
4174 /* When in-place resizing is not an option. */
4175 PyString_Concat(&v, w);
4176 return v;
4177 }
4178}
4179
Guido van Rossum950361c1997-01-24 13:49:28 +00004180#ifdef DYNAMIC_EXECUTION_PROFILE
4181
Skip Montanarof118cb12001-10-15 20:51:38 +00004182static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004183getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004184{
4185 int i;
4186 PyObject *l = PyList_New(256);
4187 if (l == NULL) return NULL;
4188 for (i = 0; i < 256; i++) {
4189 PyObject *x = PyInt_FromLong(a[i]);
4190 if (x == NULL) {
4191 Py_DECREF(l);
4192 return NULL;
4193 }
4194 PyList_SetItem(l, i, x);
4195 }
4196 for (i = 0; i < 256; i++)
4197 a[i] = 0;
4198 return l;
4199}
4200
4201PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004202_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004203{
4204#ifndef DXPAIRS
4205 return getarray(dxp);
4206#else
4207 int i;
4208 PyObject *l = PyList_New(257);
4209 if (l == NULL) return NULL;
4210 for (i = 0; i < 257; i++) {
4211 PyObject *x = getarray(dxpairs[i]);
4212 if (x == NULL) {
4213 Py_DECREF(l);
4214 return NULL;
4215 }
4216 PyList_SetItem(l, i, x);
4217 }
4218 return l;
4219#endif
4220}
4221
4222#endif