blob: 059ed4a92dd258f1eb4384556f5fbea873416f96 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
31 section should work for GCC on any PowerPC platform,
32 irrespective of OS. POWER? Who knows :-) */
33
Michael W. Hudson75eabd22005-01-18 15:56:11 +000034#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000035
36static void
37ppc_getcounter(uint64 *v)
38{
39 register unsigned long tbu, tb, tbu2;
40
41 loop:
42 asm volatile ("mftbu %0" : "=r" (tbu) );
43 asm volatile ("mftb %0" : "=r" (tb) );
44 asm volatile ("mftbu %0" : "=r" (tbu2));
45 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
46
Thomas Wouters477c8d52006-05-27 19:21:47 +000047 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000048 compiled better by GCC than any other way I tried. */
49 ((long*)(v))[0] = tbu;
50 ((long*)(v))[1] = tb;
51}
52
Michael W. Hudson75eabd22005-01-18 15:56:11 +000053#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000054
Michael W. Hudson75eabd22005-01-18 15:56:11 +000055#define READ_TIMESTAMP(val) \
56 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
58#endif
59
Thomas Wouters477c8d52006-05-27 19:21:47 +000060void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000061 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
62{
63 uint64 intr, inst, loop;
64 PyThreadState *tstate = PyThreadState_Get();
65 if (!tstate->interp->tscdump)
66 return;
67 intr = intr1 - intr0;
68 inst = inst1 - inst0 - intr;
69 loop = loop1 - loop0 - intr;
70 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
71 opcode, ticked, inst, loop);
72}
Michael W. Hudson800ba232004-08-12 18:19:17 +000073
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000074#endif
75
Guido van Rossum04691fc1992-08-12 15:35:34 +000076/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000077/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000078
Guido van Rossum408027e1996-12-30 16:17:54 +000079#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000080/* For debugging the interpreter: */
81#define LLTRACE 1 /* Low-level trace feature */
82#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000083#endif
84
Jeremy Hylton52820442001-01-03 23:52:36 +000085typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000086
Guido van Rossum374a9221991-04-04 10:40:29 +000087/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000088#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +000089static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000090#else
Thomas Wouters477c8d52006-05-27 19:21:47 +000091static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000093static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
94static PyObject * do_call(PyObject *, PyObject ***, int, int);
95static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
96static PyObject * update_keyword_args(PyObject *, int, PyObject ***,PyObject *);
97static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
98static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +000099#define CALL_FLAG_VAR 1
100#define CALL_FLAG_KW 2
101
Guido van Rossum0a066c01992-03-27 17:29:15 +0000102#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000103static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000104static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000105#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000106static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
107 int, PyObject *);
Fred Drake4ec5d562001-10-04 19:26:43 +0000108static void call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000109 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000110static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000111static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000112 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000113
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000115static int assign_slice(PyObject *, PyObject *,
116 PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * cmp_outcome(int, PyObject *, PyObject *);
118static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000119static int import_all_from(PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000120static PyObject * build_class(PyObject *, PyObject *, PyObject *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000121static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
122static void reset_exc_info(PyThreadState *);
Paul Prescode68140d2000-08-30 20:25:01 +0000123static void format_exc_check_arg(PyObject *, char *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000125 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000126
Paul Prescode68140d2000-08-30 20:25:01 +0000127#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000128 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000129#define GLOBAL_NAME_ERROR_MSG \
130 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000131#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000132 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000133#define UNBOUNDFREE_ERROR_MSG \
134 "free variable '%.200s' referenced before assignment" \
135 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000136
Guido van Rossum950361c1997-01-24 13:49:28 +0000137/* Dynamic execution profile */
138#ifdef DYNAMIC_EXECUTION_PROFILE
139#ifdef DXPAIRS
140static long dxpairs[257][256];
141#define dxp dxpairs[256]
142#else
143static long dxp[256];
144#endif
145#endif
146
Jeremy Hylton985eba52003-02-05 23:13:00 +0000147/* Function call profile */
148#ifdef CALL_PROFILE
149#define PCALL_NUM 11
150static int pcall[PCALL_NUM];
151
152#define PCALL_ALL 0
153#define PCALL_FUNCTION 1
154#define PCALL_FAST_FUNCTION 2
155#define PCALL_FASTER_FUNCTION 3
156#define PCALL_METHOD 4
157#define PCALL_BOUND_METHOD 5
158#define PCALL_CFUNCTION 6
159#define PCALL_TYPE 7
160#define PCALL_GENERATOR 8
161#define PCALL_OTHER 9
162#define PCALL_POP 10
163
164/* Notes about the statistics
165
166 PCALL_FAST stats
167
168 FAST_FUNCTION means no argument tuple needs to be created.
169 FASTER_FUNCTION means that the fast-path frame setup code is used.
170
171 If there is a method call where the call can be optimized by changing
172 the argument tuple and calling the function directly, it gets recorded
173 twice.
174
175 As a result, the relationship among the statistics appears to be
176 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
177 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
178 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
179 PCALL_METHOD > PCALL_BOUND_METHOD
180*/
181
182#define PCALL(POS) pcall[POS]++
183
184PyObject *
185PyEval_GetCallStats(PyObject *self)
186{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000187 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000188 pcall[0], pcall[1], pcall[2], pcall[3],
189 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000191}
192#else
193#define PCALL(O)
194
195PyObject *
196PyEval_GetCallStats(PyObject *self)
197{
198 Py_INCREF(Py_None);
199 return Py_None;
200}
201#endif
202
Tim Peters5ca576e2001-06-18 22:08:13 +0000203
Guido van Rossume59214e1994-08-30 08:01:59 +0000204#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000205
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000206#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000207#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000208#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000209#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000210
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000211static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000212static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000213
Tim Peters7f468f22004-10-11 02:40:51 +0000214int
215PyEval_ThreadsInitialized(void)
216{
217 return interpreter_lock != 0;
218}
219
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000223 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000224 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000225 interpreter_lock = PyThread_allocate_lock();
226 PyThread_acquire_lock(interpreter_lock, 1);
227 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000229
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000230void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000231PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000233 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000234}
235
236void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000239 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240}
241
242void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000243PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000244{
245 if (tstate == NULL)
246 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000247 /* Check someone has called PyEval_InitThreads() to create the lock */
248 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000249 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000250 if (PyThreadState_Swap(tstate) != NULL)
251 Py_FatalError(
252 "PyEval_AcquireThread: non-NULL old thread state");
253}
254
255void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000256PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000257{
258 if (tstate == NULL)
259 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
260 if (PyThreadState_Swap(NULL) != tstate)
261 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000262 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000263}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000264
265/* This function is called from PyOS_AfterFork to ensure that newly
266 created child processes don't hold locks referring to threads which
267 are not running in the child process. (This could also be done using
268 pthread_atfork mechanism, at least for the pthreads implementation.) */
269
270void
271PyEval_ReInitThreads(void)
272{
273 if (!interpreter_lock)
274 return;
275 /*XXX Can't use PyThread_free_lock here because it does too
276 much error-checking. Doing this cleanly would require
277 adding a new function to each thread_*.h. Instead, just
278 create a new lock and waste a little bit of memory */
279 interpreter_lock = PyThread_allocate_lock();
280 PyThread_acquire_lock(interpreter_lock, 1);
281 main_thread = PyThread_get_thread_ident();
282}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283#endif
284
Guido van Rossumff4949e1992-08-05 19:58:53 +0000285/* Functions save_thread and restore_thread are always defined so
286 dynamically loaded modules needn't be compiled separately for use
287 with and without threads: */
288
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000289PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000290PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000291{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000292 PyThreadState *tstate = PyThreadState_Swap(NULL);
293 if (tstate == NULL)
294 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000295#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000296 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000297 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000298#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000299 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000300}
301
302void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000305 if (tstate == NULL)
306 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000307#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000309 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000310 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000312 }
313#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000314 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000315}
316
317
Guido van Rossuma9672091994-09-14 13:31:22 +0000318/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
319 signal handlers or Mac I/O completion routines) can schedule calls
320 to a function to be called synchronously.
321 The synchronous function is called with one void* argument.
322 It should return 0 for success or -1 for failure -- failure should
323 be accompanied by an exception.
324
325 If registry succeeds, the registry function returns 0; if it fails
326 (e.g. due to too many pending calls) it returns -1 (without setting
327 an exception condition).
328
329 Note that because registry may occur from within signal handlers,
330 or other asynchronous events, calling malloc() is unsafe!
331
332#ifdef WITH_THREAD
333 Any thread can schedule pending calls, but only the main thread
334 will execute them.
335#endif
336
337 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
338 There are two possible race conditions:
339 (1) nested asynchronous registry calls;
340 (2) registry calls made while pending calls are being processed.
341 While (1) is very unlikely, (2) is a real possibility.
342 The current code is safe against (2), but not against (1).
343 The safety against (2) is derived from the fact that only one
344 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000345
Guido van Rossuma027efa1997-05-05 20:56:21 +0000346 XXX Darn! With the advent of thread state, we should have an array
347 of pending calls per thread in the thread state! Later...
348*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000349
Guido van Rossuma9672091994-09-14 13:31:22 +0000350#define NPENDINGCALLS 32
351static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000352 int (*func)(void *);
353 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000354} pendingcalls[NPENDINGCALLS];
355static volatile int pendingfirst = 0;
356static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000357static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000358
359int
Thomas Wouters334fb892000-07-25 12:56:38 +0000360Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000361{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000362 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000363 int i, j;
364 /* XXX Begin critical section */
365 /* XXX If you want this to be safe against nested
366 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000367 if (busy)
368 return -1;
369 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000370 i = pendinglast;
371 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000372 if (j == pendingfirst) {
373 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000374 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000375 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000376 pendingcalls[i].func = func;
377 pendingcalls[i].arg = arg;
378 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000379
380 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000381 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000382 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000383 /* XXX End critical section */
384 return 0;
385}
386
Guido van Rossum180d7b41994-09-29 09:45:57 +0000387int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000388Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000389{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000390 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000391#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000392 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000393 return 0;
394#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000395 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000396 return 0;
397 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000398 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000399 for (;;) {
400 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000401 int (*func)(void *);
402 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000403 i = pendingfirst;
404 if (i == pendinglast)
405 break; /* Queue empty */
406 func = pendingcalls[i].func;
407 arg = pendingcalls[i].arg;
408 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000409 if (func(arg) < 0) {
410 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000411 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000412 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000413 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000414 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000415 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000416 return 0;
417}
418
419
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000420/* The interpreter's recursion limit */
421
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000422#ifndef Py_DEFAULT_RECURSION_LIMIT
423#define Py_DEFAULT_RECURSION_LIMIT 1000
424#endif
425static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
426int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000427
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000428int
429Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000430{
431 return recursion_limit;
432}
433
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000434void
435Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000436{
437 recursion_limit = new_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000438 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000439}
440
Armin Rigo2b3eb402003-10-28 12:05:48 +0000441/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
442 if the recursion_depth reaches _Py_CheckRecursionLimit.
443 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
444 to guarantee that _Py_CheckRecursiveCall() is regularly called.
445 Without USE_STACKCHECK, there is no need for this. */
446int
447_Py_CheckRecursiveCall(char *where)
448{
449 PyThreadState *tstate = PyThreadState_GET();
450
451#ifdef USE_STACKCHECK
452 if (PyOS_CheckStack()) {
453 --tstate->recursion_depth;
454 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
455 return -1;
456 }
457#endif
458 if (tstate->recursion_depth > recursion_limit) {
459 --tstate->recursion_depth;
460 PyErr_Format(PyExc_RuntimeError,
461 "maximum recursion depth exceeded%s",
462 where);
463 return -1;
464 }
465 _Py_CheckRecursionLimit = recursion_limit;
466 return 0;
467}
468
Guido van Rossum374a9221991-04-04 10:40:29 +0000469/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000470enum why_code {
471 WHY_NOT = 0x0001, /* No error */
472 WHY_EXCEPTION = 0x0002, /* Exception occurred */
473 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
474 WHY_RETURN = 0x0008, /* 'return' statement */
475 WHY_BREAK = 0x0010, /* 'break' statement */
476 WHY_CONTINUE = 0x0020, /* 'continue' statement */
477 WHY_YIELD = 0x0040 /* 'yield' operator */
478};
Guido van Rossum374a9221991-04-04 10:40:29 +0000479
Raymond Hettinger7c958652004-04-06 10:11:10 +0000480static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
Tim Petersd6d010b2001-06-21 02:49:55 +0000481static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000482
Skip Montanarod581d772002-09-03 20:10:45 +0000483/* for manipulating the thread switch and periodic "stuff" - used to be
484 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000485int _Py_CheckInterval = 100;
486volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000487
Guido van Rossumb209a111997-04-29 18:18:01 +0000488PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000489PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000490{
Jeremy Hylton985eba52003-02-05 23:13:00 +0000491 /* XXX raise SystemError if globals is NULL */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000492 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000493 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000494 (PyObject **)NULL, 0,
495 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000496 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000497 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000498}
499
500
501/* Interpreter main loop */
502
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000503PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000504PyEval_EvalFrame(PyFrameObject *f) {
505 /* This is for backward compatibility with extension modules that
506 used this API; core interpreter code should call PyEval_EvalFrameEx() */
507 return PyEval_EvalFrameEx(f, 0);
508}
509
510PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000512{
Guido van Rossum950361c1997-01-24 13:49:28 +0000513#ifdef DXPAIRS
514 int lastopcode = 0;
515#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +0000516 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000517 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000518 register int opcode; /* Current opcode */
519 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000520 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000521 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000522 register PyObject *x; /* Result object -- NULL if error */
523 register PyObject *v; /* Temporary objects popped off stack */
524 register PyObject *w;
525 register PyObject *u;
526 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000527 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000528 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000529 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000530 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000531 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000532
Tim Peters8a5c3c72004-04-05 19:36:21 +0000533 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000534
535 not (instr_lb <= current_bytecode_offset < instr_ub)
536
Tim Peters8a5c3c72004-04-05 19:36:21 +0000537 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000538 initial values are such as to make this false the first
539 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000540 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000541
Guido van Rossumd076c731998-10-07 19:42:25 +0000542 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000543 PyObject *names;
544 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000545#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000546 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000547 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000548#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000549
Neal Norwitza81d2202002-07-14 00:27:26 +0000550/* Tuple access macros */
551
552#ifndef Py_DEBUG
553#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
554#else
555#define GETITEM(v, i) PyTuple_GetItem((v), (i))
556#endif
557
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000558#ifdef WITH_TSC
559/* Use Pentium timestamp counter to mark certain events:
560 inst0 -- beginning of switch statement for opcode dispatch
561 inst1 -- end of switch statement (may be skipped)
562 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000563 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000564 (may be skipped)
565 intr1 -- beginning of long interruption
566 intr2 -- end of long interruption
567
568 Many opcodes call out to helper C functions. In some cases, the
569 time in those functions should be counted towards the time for the
570 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
571 calls another Python function; there's no point in charge all the
572 bytecode executed by the called function to the caller.
573
574 It's hard to make a useful judgement statically. In the presence
575 of operator overloading, it's impossible to tell if a call will
576 execute new Python code or not.
577
578 It's a case-by-case judgement. I'll use intr1 for the following
579 cases:
580
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000581 IMPORT_STAR
582 IMPORT_FROM
583 CALL_FUNCTION (and friends)
584
585 */
586 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
587 int ticked = 0;
588
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000589 READ_TIMESTAMP(inst0);
590 READ_TIMESTAMP(inst1);
591 READ_TIMESTAMP(loop0);
592 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000593
594 /* shut up the compiler */
595 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000596#endif
597
Guido van Rossum374a9221991-04-04 10:40:29 +0000598/* Code access macros */
599
Martin v. Löwis18e16552006-02-15 17:27:45 +0000600#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000601#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000602#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000603#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000604#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000605#define JUMPBY(x) (next_instr += (x))
606
Raymond Hettingerf606f872003-03-16 03:11:04 +0000607/* OpCode prediction macros
608 Some opcodes tend to come in pairs thus making it possible to predict
609 the second code when the first is run. For example, COMPARE_OP is often
610 followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And, those opcodes are often
611 followed by a POP_TOP.
612
613 Verifying the prediction costs a single high-speed test of register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000614 variable against a constant. If the pairing was good, then the
Raymond Hettingerf606f872003-03-16 03:11:04 +0000615 processor has a high likelihood of making its own successful branch
616 prediction which results in a nearly zero overhead transition to the
617 next opcode.
618
619 A successful prediction saves a trip through the eval-loop including
620 its two unpredictable branches, the HASARG test and the switch-case.
Raymond Hettingera7216982004-02-08 19:59:27 +0000621
Tim Peters8a5c3c72004-04-05 19:36:21 +0000622 If collecting opcode statistics, turn off prediction so that
623 statistics are accurately maintained (the predictions bypass
Raymond Hettingera7216982004-02-08 19:59:27 +0000624 the opcode frequency counter updates).
Raymond Hettingerf606f872003-03-16 03:11:04 +0000625*/
626
Raymond Hettingera7216982004-02-08 19:59:27 +0000627#ifdef DYNAMIC_EXECUTION_PROFILE
628#define PREDICT(op) if (0) goto PRED_##op
629#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000630#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000631#endif
632
Raymond Hettingerf606f872003-03-16 03:11:04 +0000633#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000634#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000635
Guido van Rossum374a9221991-04-04 10:40:29 +0000636/* Stack manipulation macros */
637
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638/* The stack can grow at most MAXINT deep, as co_nlocals and
639 co_stacksize are ints. */
640#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000641#define EMPTY() (STACK_LEVEL() == 0)
642#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000643#define SECOND() (stack_pointer[-2])
644#define THIRD() (stack_pointer[-3])
645#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000646#define SET_TOP(v) (stack_pointer[-1] = (v))
647#define SET_SECOND(v) (stack_pointer[-2] = (v))
648#define SET_THIRD(v) (stack_pointer[-3] = (v))
649#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000650#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000651#define BASIC_PUSH(v) (*stack_pointer++ = (v))
652#define BASIC_POP() (*--stack_pointer)
653
Guido van Rossum96a42c81992-01-12 02:29:51 +0000654#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000655#define PUSH(v) { (void)(BASIC_PUSH(v), \
656 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000657 assert(STACK_LEVEL() <= co->co_stacksize); }
Fred Drakede26cfc2001-10-13 06:11:28 +0000658#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000659#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
660 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000661 assert(STACK_LEVEL() <= co->co_stacksize); }
Guido van Rossumc2e20742006-02-27 22:32:47 +0000662#define EXT_POP(STACK_POINTER) (lltrace && prtrace(*(STACK_POINTER), "ext_pop"), *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000663#else
664#define PUSH(v) BASIC_PUSH(v)
665#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000666#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000667#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000668#endif
669
Guido van Rossum681d79a1995-07-18 14:51:37 +0000670/* Local variable macros */
671
672#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000673
674/* The SETLOCAL() macro must not DECREF the local variable in-place and
675 then store the new value; it must copy the old value to a temporary
676 value, then store the new value, and then DECREF the temporary value.
677 This is because it is possible that during the DECREF the frame is
678 accessed by other code (e.g. a __del__ method or gc.collect()) and the
679 variable would be pointing to already-freed memory. */
680#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
681 GETLOCAL(i) = value; \
682 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000683
Guido van Rossuma027efa1997-05-05 20:56:21 +0000684/* Start of code */
685
Tim Peters5ca576e2001-06-18 22:08:13 +0000686 if (f == NULL)
687 return NULL;
688
Armin Rigo1d313ab2003-10-25 14:33:09 +0000689 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000690 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000691 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000692
Tim Peters5ca576e2001-06-18 22:08:13 +0000693 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000694
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000695 if (tstate->use_tracing) {
696 if (tstate->c_tracefunc != NULL) {
697 /* tstate->c_tracefunc, if defined, is a
698 function that will be called on *every* entry
699 to a code block. Its return value, if not
700 None, is a function that will be called at
701 the start of each executed line of code.
702 (Actually, the function must return itself
703 in order to continue tracing.) The trace
704 functions are called with three arguments:
705 a pointer to the current frame, a string
706 indicating why the function is called, and
707 an argument which depends on the situation.
708 The global trace function is also called
709 whenever an exception is detected. */
710 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
711 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000712 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000713 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000714 }
715 }
716 if (tstate->c_profilefunc != NULL) {
717 /* Similar for c_profilefunc, except it needn't
718 return itself and isn't called for "line" events */
719 if (call_trace(tstate->c_profilefunc,
720 tstate->c_profileobj,
721 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000722 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000723 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000724 }
725 }
726 }
727
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000728 co = f->f_code;
729 names = co->co_names;
730 consts = co->co_consts;
731 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000732 freevars = f->f_localsplus + co->co_nlocals;
Brett Cannonc9371d42005-06-25 08:23:41 +0000733 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000734 /* An explanation is in order for the next line.
735
736 f->f_lasti now refers to the index of the last instruction
737 executed. You might think this was obvious from the name, but
738 this wasn't always true before 2.3! PyFrame_New now sets
739 f->f_lasti to -1 (i.e. the index *before* the first instruction)
740 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000741 does work. Promise.
742
743 When the PREDICT() macros are enabled, some opcode pairs follow in
744 direct succession without updating f->f_lasti. A successful
745 prediction effectively links the two codes together as if they
746 were a single new opcode; accordingly,f->f_lasti will point to
747 the first code in the pair (for instance, GET_ITER followed by
748 FOR_ITER is effectively a single opcode and f->f_lasti will point
749 at to the beginning of the combined pair.)
750 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000751 next_instr = first_instr + f->f_lasti + 1;
752 stack_pointer = f->f_stacktop;
753 assert(stack_pointer != NULL);
754 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
755
Tim Peters5ca576e2001-06-18 22:08:13 +0000756#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000758#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000759#if defined(Py_DEBUG) || defined(LLTRACE)
Tim Peters5ca576e2001-06-18 22:08:13 +0000760 filename = PyString_AsString(co->co_filename);
761#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000762
Guido van Rossum374a9221991-04-04 10:40:29 +0000763 why = WHY_NOT;
764 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000765 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000766 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000767
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000769 why = WHY_EXCEPTION;
770 goto on_error;
771 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000772
Guido van Rossum374a9221991-04-04 10:40:29 +0000773 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000774#ifdef WITH_TSC
775 if (inst1 == 0) {
776 /* Almost surely, the opcode executed a break
777 or a continue, preventing inst1 from being set
778 on the way out of the loop.
779 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000780 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000781 loop1 = inst1;
782 }
783 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
784 intr0, intr1);
785 ticked = 0;
786 inst1 = 0;
787 intr0 = 0;
788 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000789 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000790#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000791 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000792 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000793
Guido van Rossuma027efa1997-05-05 20:56:21 +0000794 /* Do periodic things. Doing this every time through
795 the loop would add too much overhead, so we do it
796 only every Nth instruction. We also do it if
797 ``things_to_do'' is set, i.e. when an asynchronous
798 event needs attention (e.g. a signal handler or
799 async I/O handler); see Py_AddPendingCall() and
800 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000801
Skip Montanarod581d772002-09-03 20:10:45 +0000802 if (--_Py_Ticker < 0) {
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000803 if (*next_instr == SETUP_FINALLY) {
804 /* Make the last opcode before
805 a try: finally: block uninterruptable. */
806 goto fast_next_opcode;
807 }
Skip Montanarod581d772002-09-03 20:10:45 +0000808 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000809 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000810#ifdef WITH_TSC
811 ticked = 1;
812#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000813 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000814 if (Py_MakePendingCalls() < 0) {
815 why = WHY_EXCEPTION;
816 goto on_error;
817 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000818 if (things_to_do)
819 /* MakePendingCalls() didn't succeed.
820 Force early re-execution of this
821 "periodic" code, possibly after
822 a thread switch */
823 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000824 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000825#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000826 if (interpreter_lock) {
827 /* Give another thread a chance */
828
Guido van Rossum25ce5661997-08-02 03:10:38 +0000829 if (PyThreadState_Swap(NULL) != tstate)
830 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000831 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000832
833 /* Other threads may run now */
834
Guido van Rossum65d5b571998-12-21 19:32:43 +0000835 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000836 if (PyThreadState_Swap(tstate) != NULL)
837 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000838
839 /* Check for thread interrupts */
840
841 if (tstate->async_exc != NULL) {
842 x = tstate->async_exc;
843 tstate->async_exc = NULL;
844 PyErr_SetNone(x);
845 Py_DECREF(x);
846 why = WHY_EXCEPTION;
847 goto on_error;
848 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000849 }
850#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000851 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000852
Neil Schemenauer63543862002-02-17 19:10:14 +0000853 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000854 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000855
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000856 /* line-by-line tracing support */
857
858 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
859 /* see maybe_call_line_trace
860 for expository comments */
861 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000862
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000863 err = maybe_call_line_trace(tstate->c_tracefunc,
864 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000865 f, &instr_lb, &instr_ub,
866 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000867 /* Reload possibly changed frame fields */
868 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000869 if (f->f_stacktop != NULL) {
870 stack_pointer = f->f_stacktop;
871 f->f_stacktop = NULL;
872 }
873 if (err) {
874 /* trace function raised an exception */
875 goto on_error;
876 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000877 }
878
879 /* Extract opcode and argument */
880
Guido van Rossum374a9221991-04-04 10:40:29 +0000881 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000882 oparg = 0; /* allows oparg to be stored in a register because
883 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000884 if (HAS_ARG(opcode))
885 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000886 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000887#ifdef DYNAMIC_EXECUTION_PROFILE
888#ifdef DXPAIRS
889 dxpairs[lastopcode][opcode]++;
890 lastopcode = opcode;
891#endif
892 dxp[opcode]++;
893#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000894
Guido van Rossum96a42c81992-01-12 02:29:51 +0000895#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000896 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000897
Guido van Rossum96a42c81992-01-12 02:29:51 +0000898 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000899 if (HAS_ARG(opcode)) {
900 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000901 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000902 }
903 else {
904 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000905 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000906 }
907 }
908#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000909
Guido van Rossum374a9221991-04-04 10:40:29 +0000910 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000911 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000912
Guido van Rossum374a9221991-04-04 10:40:29 +0000913 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000914
Guido van Rossum374a9221991-04-04 10:40:29 +0000915 /* BEWARE!
916 It is essential that any operation that fails sets either
917 x to NULL, err to nonzero, or why to anything but WHY_NOT,
918 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000919
Guido van Rossum374a9221991-04-04 10:40:29 +0000920 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000921
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000922 case NOP:
923 goto fast_next_opcode;
924
Neil Schemenauer63543862002-02-17 19:10:14 +0000925 case LOAD_FAST:
926 x = GETLOCAL(oparg);
927 if (x != NULL) {
928 Py_INCREF(x);
929 PUSH(x);
930 goto fast_next_opcode;
931 }
932 format_exc_check_arg(PyExc_UnboundLocalError,
933 UNBOUNDLOCAL_ERROR_MSG,
934 PyTuple_GetItem(co->co_varnames, oparg));
935 break;
936
937 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000938 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000939 Py_INCREF(x);
940 PUSH(x);
941 goto fast_next_opcode;
942
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000943 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000944 case STORE_FAST:
945 v = POP();
946 SETLOCAL(oparg, v);
947 goto fast_next_opcode;
948
Raymond Hettingerf606f872003-03-16 03:11:04 +0000949 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000950 case POP_TOP:
951 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000952 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000953 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000954
Guido van Rossum374a9221991-04-04 10:40:29 +0000955 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000956 v = TOP();
957 w = SECOND();
958 SET_TOP(w);
959 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000960 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000961
Guido van Rossum374a9221991-04-04 10:40:29 +0000962 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000963 v = TOP();
964 w = SECOND();
965 x = THIRD();
966 SET_TOP(w);
967 SET_SECOND(x);
968 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000969 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000970
Thomas Wouters434d0822000-08-24 20:11:32 +0000971 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000972 u = TOP();
973 v = SECOND();
974 w = THIRD();
975 x = FOURTH();
976 SET_TOP(v);
977 SET_SECOND(w);
978 SET_THIRD(x);
979 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000980 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000981
Guido van Rossum374a9221991-04-04 10:40:29 +0000982 case DUP_TOP:
983 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000984 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +0000985 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000986 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000987
Thomas Wouters434d0822000-08-24 20:11:32 +0000988 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000989 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000990 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +0000991 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000992 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +0000993 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 STACKADJ(2);
995 SET_TOP(x);
996 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +0000997 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +0000998 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +0000999 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001000 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001001 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001002 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001003 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001004 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005 STACKADJ(3);
1006 SET_TOP(x);
1007 SET_SECOND(w);
1008 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001009 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001010 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001011 Py_FatalError("invalid argument to DUP_TOPX"
1012 " (bytecode corruption?)");
Tim Peters35ba6892000-10-11 07:04:49 +00001013 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001014
Guido van Rossum374a9221991-04-04 10:40:29 +00001015 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001016 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001017 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001018 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001019 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001020 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001021 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001022
Guido van Rossum374a9221991-04-04 10:40:29 +00001023 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001024 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001025 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001026 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001027 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001028 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001029 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001030
Guido van Rossum374a9221991-04-04 10:40:29 +00001031 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001033 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001034 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001035 if (err == 0) {
1036 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001037 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001038 continue;
1039 }
1040 else if (err > 0) {
1041 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001042 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001043 err = 0;
1044 continue;
1045 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001046 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001047 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001048
Guido van Rossum7928cd71991-10-24 14:59:31 +00001049 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001050 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001051 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001052 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001053 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001054 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001055 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001056
Guido van Rossum50564e81996-01-12 01:13:16 +00001057 case BINARY_POWER:
1058 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001059 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001060 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001061 Py_DECREF(v);
1062 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001063 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001064 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001065 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001066
Guido van Rossum374a9221991-04-04 10:40:29 +00001067 case BINARY_MULTIPLY:
1068 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001069 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001070 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001071 Py_DECREF(v);
1072 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001073 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001074 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001075 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001076
Tim Peters3caca232001-12-06 06:23:26 +00001077 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001078 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001079 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001080 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001081 Py_DECREF(v);
1082 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001083 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001084 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001085 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001086
Guido van Rossum4668b002001-08-08 05:00:18 +00001087 case BINARY_FLOOR_DIVIDE:
1088 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001089 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001090 x = PyNumber_FloorDivide(v, w);
1091 Py_DECREF(v);
1092 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001093 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001094 if (x != NULL) continue;
1095 break;
1096
Guido van Rossum374a9221991-04-04 10:40:29 +00001097 case BINARY_MODULO:
1098 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001099 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001100 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001101 Py_DECREF(v);
1102 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001103 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001104 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001105 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001106
Guido van Rossum374a9221991-04-04 10:40:29 +00001107 case BINARY_ADD:
1108 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001109 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001110 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001111 /* INLINE: int + int */
1112 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001113 a = PyInt_AS_LONG(v);
1114 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001115 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001116 if ((i^a) < 0 && (i^b) < 0)
1117 goto slow_add;
1118 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001119 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001120 else if (PyString_CheckExact(v) &&
1121 PyString_CheckExact(w)) {
1122 x = string_concatenate(v, w, f, next_instr);
1123 /* string_concatenate consumed the ref to v */
1124 goto skip_decref_vx;
1125 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001126 else {
1127 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001128 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001129 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001130 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001131 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001132 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001133 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001134 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001135 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001136
Guido van Rossum374a9221991-04-04 10:40:29 +00001137 case BINARY_SUBTRACT:
1138 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001139 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001140 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001141 /* INLINE: int - int */
1142 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001143 a = PyInt_AS_LONG(v);
1144 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001145 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001146 if ((i^a) < 0 && (i^~b) < 0)
1147 goto slow_sub;
1148 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001149 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001150 else {
1151 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001152 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001153 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001154 Py_DECREF(v);
1155 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001156 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001157 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001158 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001159
Guido van Rossum374a9221991-04-04 10:40:29 +00001160 case BINARY_SUBSCR:
1161 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001162 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001163 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001164 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001165 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001166 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001167 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001168 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001169 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001170 Py_INCREF(x);
1171 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001172 else
1173 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001174 }
1175 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001176 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001177 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001178 Py_DECREF(v);
1179 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001180 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001181 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001182 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001183
Guido van Rossum7928cd71991-10-24 14:59:31 +00001184 case BINARY_LSHIFT:
1185 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001186 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001187 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001188 Py_DECREF(v);
1189 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001190 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001191 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001192 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001193
Guido van Rossum7928cd71991-10-24 14:59:31 +00001194 case BINARY_RSHIFT:
1195 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001196 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001197 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001198 Py_DECREF(v);
1199 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001200 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001201 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001202 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Guido van Rossum7928cd71991-10-24 14:59:31 +00001204 case BINARY_AND:
1205 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001207 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001208 Py_DECREF(v);
1209 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001210 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001211 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Guido van Rossum7928cd71991-10-24 14:59:31 +00001214 case BINARY_XOR:
1215 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001216 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001217 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001218 Py_DECREF(v);
1219 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001220 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001221 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001222 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Guido van Rossum7928cd71991-10-24 14:59:31 +00001224 case BINARY_OR:
1225 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001227 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001228 Py_DECREF(v);
1229 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001230 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001231 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001232 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001233
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001234 case LIST_APPEND:
1235 w = POP();
1236 v = POP();
1237 err = PyList_Append(v, w);
1238 Py_DECREF(v);
1239 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001240 if (err == 0) {
1241 PREDICT(JUMP_ABSOLUTE);
1242 continue;
1243 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001244 break;
1245
Thomas Wouters434d0822000-08-24 20:11:32 +00001246 case INPLACE_POWER:
1247 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001248 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001249 x = PyNumber_InPlacePower(v, w, Py_None);
1250 Py_DECREF(v);
1251 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001252 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001253 if (x != NULL) continue;
1254 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Thomas Wouters434d0822000-08-24 20:11:32 +00001256 case INPLACE_MULTIPLY:
1257 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001258 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001259 x = PyNumber_InPlaceMultiply(v, w);
1260 Py_DECREF(v);
1261 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001263 if (x != NULL) continue;
1264 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Tim Peters54b11912001-12-25 18:49:11 +00001266 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001267 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001268 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001269 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001270 Py_DECREF(v);
1271 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001273 if (x != NULL) continue;
1274 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001275
Guido van Rossum4668b002001-08-08 05:00:18 +00001276 case INPLACE_FLOOR_DIVIDE:
1277 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001278 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001279 x = PyNumber_InPlaceFloorDivide(v, w);
1280 Py_DECREF(v);
1281 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001282 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001283 if (x != NULL) continue;
1284 break;
1285
Thomas Wouters434d0822000-08-24 20:11:32 +00001286 case INPLACE_MODULO:
1287 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 x = PyNumber_InPlaceRemainder(v, w);
1290 Py_DECREF(v);
1291 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001292 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001293 if (x != NULL) continue;
1294 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001295
Thomas Wouters434d0822000-08-24 20:11:32 +00001296 case INPLACE_ADD:
1297 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001299 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001300 /* INLINE: int + int */
1301 register long a, b, i;
1302 a = PyInt_AS_LONG(v);
1303 b = PyInt_AS_LONG(w);
1304 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001305 if ((i^a) < 0 && (i^b) < 0)
1306 goto slow_iadd;
1307 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001308 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001309 else if (PyString_CheckExact(v) &&
1310 PyString_CheckExact(w)) {
1311 x = string_concatenate(v, w, f, next_instr);
1312 /* string_concatenate consumed the ref to v */
1313 goto skip_decref_v;
1314 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001315 else {
1316 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001317 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001318 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001320 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001321 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001323 if (x != NULL) continue;
1324 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Thomas Wouters434d0822000-08-24 20:11:32 +00001326 case INPLACE_SUBTRACT:
1327 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001329 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001330 /* INLINE: int - int */
1331 register long a, b, i;
1332 a = PyInt_AS_LONG(v);
1333 b = PyInt_AS_LONG(w);
1334 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001335 if ((i^a) < 0 && (i^~b) < 0)
1336 goto slow_isub;
1337 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001338 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001339 else {
1340 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001341 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001342 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001343 Py_DECREF(v);
1344 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001346 if (x != NULL) continue;
1347 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Thomas Wouters434d0822000-08-24 20:11:32 +00001349 case INPLACE_LSHIFT:
1350 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 x = PyNumber_InPlaceLshift(v, w);
1353 Py_DECREF(v);
1354 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001355 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001356 if (x != NULL) continue;
1357 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001358
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 case INPLACE_RSHIFT:
1360 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001362 x = PyNumber_InPlaceRshift(v, w);
1363 Py_DECREF(v);
1364 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 if (x != NULL) continue;
1367 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Thomas Wouters434d0822000-08-24 20:11:32 +00001369 case INPLACE_AND:
1370 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001372 x = PyNumber_InPlaceAnd(v, w);
1373 Py_DECREF(v);
1374 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001375 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001376 if (x != NULL) continue;
1377 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Thomas Wouters434d0822000-08-24 20:11:32 +00001379 case INPLACE_XOR:
1380 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 x = PyNumber_InPlaceXor(v, w);
1383 Py_DECREF(v);
1384 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001385 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001386 if (x != NULL) continue;
1387 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Thomas Wouters434d0822000-08-24 20:11:32 +00001389 case INPLACE_OR:
1390 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001391 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 x = PyNumber_InPlaceOr(v, w);
1393 Py_DECREF(v);
1394 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001395 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001396 if (x != NULL) continue;
1397 break;
1398
Guido van Rossum374a9221991-04-04 10:40:29 +00001399 case SLICE+0:
1400 case SLICE+1:
1401 case SLICE+2:
1402 case SLICE+3:
1403 if ((opcode-SLICE) & 2)
1404 w = POP();
1405 else
1406 w = NULL;
1407 if ((opcode-SLICE) & 1)
1408 v = POP();
1409 else
1410 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001411 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001412 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001413 Py_DECREF(u);
1414 Py_XDECREF(v);
1415 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001416 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001417 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001418 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001419
Guido van Rossum374a9221991-04-04 10:40:29 +00001420 case STORE_SLICE+0:
1421 case STORE_SLICE+1:
1422 case STORE_SLICE+2:
1423 case STORE_SLICE+3:
1424 if ((opcode-STORE_SLICE) & 2)
1425 w = POP();
1426 else
1427 w = NULL;
1428 if ((opcode-STORE_SLICE) & 1)
1429 v = POP();
1430 else
1431 v = NULL;
1432 u = POP();
1433 t = POP();
1434 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001435 Py_DECREF(t);
1436 Py_DECREF(u);
1437 Py_XDECREF(v);
1438 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001439 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001440 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001441
Guido van Rossum374a9221991-04-04 10:40:29 +00001442 case DELETE_SLICE+0:
1443 case DELETE_SLICE+1:
1444 case DELETE_SLICE+2:
1445 case DELETE_SLICE+3:
1446 if ((opcode-DELETE_SLICE) & 2)
1447 w = POP();
1448 else
1449 w = NULL;
1450 if ((opcode-DELETE_SLICE) & 1)
1451 v = POP();
1452 else
1453 v = NULL;
1454 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001455 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001456 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001457 Py_DECREF(u);
1458 Py_XDECREF(v);
1459 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001460 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001461 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001462
Guido van Rossum374a9221991-04-04 10:40:29 +00001463 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001464 w = TOP();
1465 v = SECOND();
1466 u = THIRD();
1467 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001468 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001469 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001470 Py_DECREF(u);
1471 Py_DECREF(v);
1472 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001473 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001474 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Guido van Rossum374a9221991-04-04 10:40:29 +00001476 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001477 w = TOP();
1478 v = SECOND();
1479 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001480 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001481 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001482 Py_DECREF(v);
1483 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001484 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001485 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001486
Guido van Rossum374a9221991-04-04 10:40:29 +00001487 case PRINT_EXPR:
1488 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001489 w = PySys_GetObject("displayhook");
1490 if (w == NULL) {
1491 PyErr_SetString(PyExc_RuntimeError,
1492 "lost sys.displayhook");
1493 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001494 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001495 }
1496 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001497 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001498 if (x == NULL)
1499 err = -1;
1500 }
1501 if (err == 0) {
1502 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001503 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001504 if (w == NULL)
1505 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001506 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001507 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001508 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001509 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001510
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001511 case PRINT_ITEM_TO:
1512 w = stream = POP();
1513 /* fall through to PRINT_ITEM */
1514
Guido van Rossum374a9221991-04-04 10:40:29 +00001515 case PRINT_ITEM:
1516 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001517 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001518 w = PySys_GetObject("stdout");
1519 if (w == NULL) {
1520 PyErr_SetString(PyExc_RuntimeError,
1521 "lost sys.stdout");
1522 err = -1;
1523 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001524 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001525 /* PyFile_SoftSpace() can exececute arbitrary code
1526 if sys.stdout is an instance with a __getattr__.
1527 If __getattr__ raises an exception, w will
1528 be freed, so we need to prevent that temporarily. */
1529 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001530 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001531 err = PyFile_WriteString(" ", w);
1532 if (err == 0)
1533 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001534 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001535 /* XXX move into writeobject() ? */
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001536 if (PyString_Check(v)) {
1537 char *s = PyString_AS_STRING(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001538 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001539 if (len == 0 ||
1540 !isspace(Py_CHARMASK(s[len-1])) ||
1541 s[len-1] == ' ')
1542 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001543 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001544#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001545 else if (PyUnicode_Check(v)) {
1546 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001547 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001548 if (len == 0 ||
1549 !Py_UNICODE_ISSPACE(s[len-1]) ||
1550 s[len-1] == ' ')
1551 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001552 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001553#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001554 else
1555 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001556 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001557 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001558 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001559 Py_XDECREF(stream);
1560 stream = NULL;
1561 if (err == 0)
1562 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001563 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001564
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001565 case PRINT_NEWLINE_TO:
1566 w = stream = POP();
1567 /* fall through to PRINT_NEWLINE */
1568
Guido van Rossum374a9221991-04-04 10:40:29 +00001569 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001570 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001571 w = PySys_GetObject("stdout");
1572 if (w == NULL)
1573 PyErr_SetString(PyExc_RuntimeError,
1574 "lost sys.stdout");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001575 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001576 if (w != NULL) {
1577 err = PyFile_WriteString("\n", w);
1578 if (err == 0)
1579 PyFile_SoftSpace(w, 0);
1580 }
1581 Py_XDECREF(stream);
1582 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001583 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001584
Thomas Wouters434d0822000-08-24 20:11:32 +00001585
1586#ifdef CASE_TOO_BIG
1587 default: switch (opcode) {
1588#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001589 case RAISE_VARARGS:
1590 u = v = w = NULL;
1591 switch (oparg) {
1592 case 3:
1593 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001594 /* Fallthrough */
1595 case 2:
1596 v = POP(); /* value */
1597 /* Fallthrough */
1598 case 1:
1599 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001600 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001601 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001602 break;
1603 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001604 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001605 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001606 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001607 break;
1608 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001609 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001610
Guido van Rossum374a9221991-04-04 10:40:29 +00001611 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001612 if ((x = f->f_locals) != NULL) {
1613 Py_INCREF(x);
1614 PUSH(x);
1615 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001616 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001617 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001618 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Guido van Rossum374a9221991-04-04 10:40:29 +00001620 case RETURN_VALUE:
1621 retval = POP();
1622 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001623 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001624
Tim Peters5ca576e2001-06-18 22:08:13 +00001625 case YIELD_VALUE:
1626 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001627 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001628 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001629 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001630
Guido van Rossum374a9221991-04-04 10:40:29 +00001631 case POP_BLOCK:
1632 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001633 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001634 while (STACK_LEVEL() > b->b_level) {
1635 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001636 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001637 }
1638 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001639 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Guido van Rossum374a9221991-04-04 10:40:29 +00001641 case END_FINALLY:
1642 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001643 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001644 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001645 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001646 if (why == WHY_RETURN ||
1647 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001648 retval = POP();
1649 }
Brett Cannonbf364092006-03-01 04:25:17 +00001650 else if (PyExceptionClass_Check(v) || PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001651 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001652 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001653 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001654 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001655 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001656 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001657 else if (v != Py_None) {
1658 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001659 "'finally' pops bad exception");
1660 why = WHY_EXCEPTION;
1661 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001662 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001663 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Guido van Rossum374a9221991-04-04 10:40:29 +00001665 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001666 u = TOP();
1667 v = SECOND();
1668 w = THIRD();
1669 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001670 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001671 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001672 Py_DECREF(u);
1673 Py_DECREF(v);
1674 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001675 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Guido van Rossum374a9221991-04-04 10:40:29 +00001677 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001678 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001679 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001680 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001681 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001682 err = PyDict_SetItem(x, w, v);
1683 else
1684 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001685 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001686 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001687 break;
1688 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001689 PyErr_Format(PyExc_SystemError,
1690 "no locals found when storing %s",
1691 PyObject_REPR(w));
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_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001695 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001696 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001697 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001698 format_exc_check_arg(PyExc_NameError,
1699 NAME_ERROR_MSG ,w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001700 break;
1701 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001702 PyErr_Format(PyExc_SystemError,
1703 "no locals when deleting %s",
1704 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001705 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001706
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001707 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001708 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001709 v = POP();
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001710 if (PyTuple_CheckExact(v) && PyTuple_GET_SIZE(v) == oparg) {
1711 PyObject **items = ((PyTupleObject *)v)->ob_item;
1712 while (oparg--) {
1713 w = items[oparg];
1714 Py_INCREF(w);
1715 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001716 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001717 Py_DECREF(v);
1718 continue;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001719 } else if (PyList_CheckExact(v) && PyList_GET_SIZE(v) == oparg) {
1720 PyObject **items = ((PyListObject *)v)->ob_item;
1721 while (oparg--) {
1722 w = items[oparg];
1723 Py_INCREF(w);
1724 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001725 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001726 } else if (unpack_iterable(v, oparg,
Tim Petersd6d010b2001-06-21 02:49:55 +00001727 stack_pointer + oparg))
1728 stack_pointer += oparg;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001729 else {
1730 if (PyErr_ExceptionMatches(PyExc_TypeError))
1731 PyErr_SetString(PyExc_TypeError,
1732 "unpack non-sequence");
Barry Warsawe42b18f1997-08-25 22:13:04 +00001733 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001734 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001735 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001736 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001737
Guido van Rossum374a9221991-04-04 10:40:29 +00001738 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001739 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001740 v = TOP();
1741 u = SECOND();
1742 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001743 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1744 Py_DECREF(v);
1745 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001746 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001747 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001748
Guido van Rossum374a9221991-04-04 10:40:29 +00001749 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001750 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001751 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001752 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1753 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001754 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001755 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001756
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001757 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001758 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001759 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001760 err = PyDict_SetItem(f->f_globals, w, v);
1761 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001762 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001763 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001765 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001766 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001767 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001768 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001769 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001770 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001771
Guido van Rossum374a9221991-04-04 10:40:29 +00001772 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001773 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001774 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001775 PyErr_Format(PyExc_SystemError,
1776 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001777 PyObject_REPR(w));
Guido van Rossum681d79a1995-07-18 14:51:37 +00001778 break;
1779 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001780 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001781 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001782 Py_XINCREF(x);
1783 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001784 else {
1785 x = PyObject_GetItem(v, w);
1786 if (x == NULL && PyErr_Occurred()) {
1787 if (!PyErr_ExceptionMatches(PyExc_KeyError))
1788 break;
1789 PyErr_Clear();
1790 }
1791 }
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_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001794 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001795 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001796 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001797 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001798 PyExc_NameError,
Paul Prescode68140d2000-08-30 20:25:01 +00001799 NAME_ERROR_MSG ,w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001800 break;
1801 }
1802 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001803 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001804 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001806 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001807
Guido van Rossum374a9221991-04-04 10:40:29 +00001808 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001809 w = GETITEM(names, oparg);
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001810 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001811 /* Inline the PyDict_GetItem() calls.
1812 WARNING: this is an extreme speed hack.
1813 Do not try this at home. */
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001814 long hash = ((PyStringObject *)w)->ob_shash;
1815 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001816 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001817 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001818 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001819 e = d->ma_lookup(d, w, hash);
1820 if (e == NULL) {
1821 x = NULL;
1822 break;
1823 }
1824 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001825 if (x != NULL) {
1826 Py_INCREF(x);
1827 PUSH(x);
1828 continue;
1829 }
1830 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001831 e = d->ma_lookup(d, w, hash);
1832 if (e == NULL) {
1833 x = NULL;
1834 break;
1835 }
1836 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001837 if (x != NULL) {
1838 Py_INCREF(x);
1839 PUSH(x);
1840 continue;
1841 }
1842 goto load_global_error;
1843 }
1844 }
1845 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001846 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001847 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001848 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001849 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001850 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001851 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001852 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001853 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001854 break;
1855 }
1856 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001857 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001859 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001860
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001861 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001862 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001863 if (x != NULL) {
1864 SETLOCAL(oparg, NULL);
1865 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001866 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001867 format_exc_check_arg(
1868 PyExc_UnboundLocalError,
1869 UNBOUNDLOCAL_ERROR_MSG,
1870 PyTuple_GetItem(co->co_varnames, oparg)
1871 );
1872 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001873
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001874 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001875 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001876 Py_INCREF(x);
1877 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001878 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001879 break;
1880
1881 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001882 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001883 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001884 if (w != NULL) {
1885 PUSH(w);
1886 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001887 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001888 err = -1;
1889 /* Don't stomp existing exception */
1890 if (PyErr_Occurred())
1891 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001892 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1893 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001894 oparg);
1895 format_exc_check_arg(
1896 PyExc_UnboundLocalError,
1897 UNBOUNDLOCAL_ERROR_MSG,
1898 v);
1899 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001900 v = PyTuple_GET_ITEM(
Raymond Hettinger467a6982004-04-07 11:39:21 +00001901 co->co_freevars,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001902 oparg - PyTuple_GET_SIZE(co->co_cellvars));
Raymond Hettinger467a6982004-04-07 11:39:21 +00001903 format_exc_check_arg(
1904 PyExc_NameError,
1905 UNBOUNDFREE_ERROR_MSG,
1906 v);
1907 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001908 break;
1909
1910 case STORE_DEREF:
1911 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001912 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001913 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001914 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001915 continue;
1916
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00001918 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001919 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001920 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001921 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001922 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 }
1924 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001925 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001926 }
1927 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001928
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00001930 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001931 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001932 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00001934 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001935 }
1936 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001937 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001938 }
1939 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001940
Guido van Rossum86e58e22006-08-28 15:27:34 +00001941 case BUILD_SET:
1942 x = PySet_New(NULL);
1943 if (x != NULL) {
1944 for (; --oparg >= 0;) {
1945 w = POP();
1946 if (err == 0)
1947 err = PySet_Add(x, w);
1948 Py_DECREF(w);
1949 }
1950 if (err != 0) {
1951 Py_DECREF(x);
1952 break;
1953 }
1954 PUSH(x);
1955 continue;
1956 }
1957 break;
1958
Guido van Rossum374a9221991-04-04 10:40:29 +00001959 case BUILD_MAP:
Guido van Rossumb209a111997-04-29 18:18:01 +00001960 x = PyDict_New();
Guido van Rossum374a9221991-04-04 10:40:29 +00001961 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001962 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001963 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001964
Guido van Rossum374a9221991-04-04 10:40:29 +00001965 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001966 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001967 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001968 x = PyObject_GetAttr(v, w);
1969 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001970 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001971 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001973
Guido van Rossum374a9221991-04-04 10:40:29 +00001974 case COMPARE_OP:
1975 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001976 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001977 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001978 /* INLINE: cmp(int, int) */
1979 register long a, b;
1980 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001981 a = PyInt_AS_LONG(v);
1982 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001983 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00001984 case PyCmp_LT: res = a < b; break;
1985 case PyCmp_LE: res = a <= b; break;
1986 case PyCmp_EQ: res = a == b; break;
1987 case PyCmp_NE: res = a != b; break;
1988 case PyCmp_GT: res = a > b; break;
1989 case PyCmp_GE: res = a >= b; break;
1990 case PyCmp_IS: res = v == w; break;
1991 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00001992 default: goto slow_compare;
1993 }
1994 x = res ? Py_True : Py_False;
1995 Py_INCREF(x);
1996 }
1997 else {
1998 slow_compare:
1999 x = cmp_outcome(oparg, v, w);
2000 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002001 Py_DECREF(v);
2002 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002003 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002004 if (x == NULL) break;
2005 PREDICT(JUMP_IF_FALSE);
2006 PREDICT(JUMP_IF_TRUE);
2007 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002008
Guido van Rossum374a9221991-04-04 10:40:29 +00002009 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002010 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002011 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002012 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002013 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002014 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002015 break;
2016 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002017 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002018 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002019 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2020 w = PyTuple_Pack(5,
2021 w,
2022 f->f_globals,
2023 f->f_locals == NULL ?
2024 Py_None : f->f_locals,
2025 v,
2026 u);
2027 else
2028 w = PyTuple_Pack(4,
2029 w,
2030 f->f_globals,
2031 f->f_locals == NULL ?
2032 Py_None : f->f_locals,
2033 v);
2034 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002035 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002036 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002037 u = POP();
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002038 x = NULL;
2039 break;
2040 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002041 READ_TIMESTAMP(intr0);
Guido van Rossumb209a111997-04-29 18:18:01 +00002042 x = PyEval_CallObject(x, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002043 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002044 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002045 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002046 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002048
Thomas Wouters52152252000-08-17 22:55:00 +00002049 case IMPORT_STAR:
2050 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002051 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002052 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002053 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002054 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002055 break;
2056 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002057 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002058 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002059 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002060 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002061 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002062 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002063 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002064
Thomas Wouters52152252000-08-17 22:55:00 +00002065 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002066 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002067 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002068 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002069 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002070 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002071 PUSH(x);
2072 if (x != NULL) continue;
2073 break;
2074
Guido van Rossum374a9221991-04-04 10:40:29 +00002075 case JUMP_FORWARD:
2076 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002077 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002078
Raymond Hettingerf606f872003-03-16 03:11:04 +00002079 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002080 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002081 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002082 if (w == Py_True) {
2083 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002084 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002085 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002086 if (w == Py_False) {
2087 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002088 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002089 }
2090 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002091 if (err > 0)
2092 err = 0;
2093 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002094 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002095 else
2096 break;
2097 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002098
Raymond Hettingerf606f872003-03-16 03:11:04 +00002099 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002100 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002101 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002102 if (w == Py_False) {
2103 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002104 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002105 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002106 if (w == Py_True) {
2107 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002108 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002109 }
2110 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002111 if (err > 0) {
2112 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002113 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002114 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002115 else if (err == 0)
2116 ;
2117 else
2118 break;
2119 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002120
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002121 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002122 case JUMP_ABSOLUTE:
2123 JUMPTO(oparg);
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002124 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002125
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002126 case GET_ITER:
2127 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002128 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002129 x = PyObject_GetIter(v);
2130 Py_DECREF(v);
2131 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002132 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002133 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002134 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002135 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002136 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002137 break;
2138
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002139 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002140 case FOR_ITER:
2141 /* before: [iter]; after: [iter, iter()] *or* [] */
2142 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002143 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002144 if (x != NULL) {
2145 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002146 PREDICT(STORE_FAST);
2147 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002148 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002149 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002150 if (PyErr_Occurred()) {
2151 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2152 break;
2153 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002154 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002155 /* iterator ended normally */
2156 x = v = POP();
2157 Py_DECREF(v);
2158 JUMPBY(oparg);
2159 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002160
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002161 case BREAK_LOOP:
2162 why = WHY_BREAK;
2163 goto fast_block_end;
2164
2165 case CONTINUE_LOOP:
2166 retval = PyInt_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002167 if (!retval) {
2168 x = NULL;
2169 break;
2170 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002171 why = WHY_CONTINUE;
2172 goto fast_block_end;
2173
Guido van Rossum374a9221991-04-04 10:40:29 +00002174 case SETUP_LOOP:
2175 case SETUP_EXCEPT:
2176 case SETUP_FINALLY:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00002177 /* NOTE: If you add any new block-setup opcodes that are
2178 not try/except/finally handlers, you may need to
2179 update the PyGen_NeedsFinalizing() function. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002180
Guido van Rossumb209a111997-04-29 18:18:01 +00002181 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002182 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002183 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002184
Guido van Rossumc2e20742006-02-27 22:32:47 +00002185 case WITH_CLEANUP:
2186 {
2187 /* TOP is the context.__exit__ bound method.
2188 Below that are 1-3 values indicating how/why
2189 we entered the finally clause:
2190 - SECOND = None
Guido van Rossumf6694362006-03-10 02:28:35 +00002191 - (SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval
Guido van Rossumc2e20742006-02-27 22:32:47 +00002192 - SECOND = WHY_*; no retval below it
2193 - (SECOND, THIRD, FOURTH) = exc_info()
2194 In the last case, we must call
2195 TOP(SECOND, THIRD, FOURTH)
2196 otherwise we must call
2197 TOP(None, None, None)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002198
2199 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002200 *and* the function call returns a 'true' value, we
2201 "zap" this information, to prevent END_FINALLY from
2202 re-raising the exception. (But non-local gotos
2203 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002204 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002205
Guido van Rossumc2e20742006-02-27 22:32:47 +00002206 x = TOP();
2207 u = SECOND();
2208 if (PyInt_Check(u) || u == Py_None) {
2209 u = v = w = Py_None;
2210 }
2211 else {
2212 v = THIRD();
2213 w = FOURTH();
2214 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002215 /* XXX Not the fastest way to call it... */
2216 x = PyObject_CallFunctionObjArgs(x, u, v, w, NULL);
2217 if (x == NULL)
2218 break; /* Go to error exit */
2219 if (u != Py_None && PyObject_IsTrue(x)) {
2220 /* There was an exception and a true return */
2221 Py_DECREF(x);
2222 x = TOP(); /* Again */
2223 STACKADJ(-3);
2224 Py_INCREF(Py_None);
2225 SET_TOP(Py_None);
2226 Py_DECREF(x);
2227 Py_DECREF(u);
2228 Py_DECREF(v);
2229 Py_DECREF(w);
2230 } else {
2231 /* Let END_FINALLY do its thing */
2232 Py_DECREF(x);
2233 x = POP();
2234 Py_DECREF(x);
2235 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002236 break;
2237 }
2238
Guido van Rossumf10570b1995-07-07 22:53:21 +00002239 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002240 {
2241 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002242 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002243 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002244#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002245 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002246#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002247 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002248#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002249 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002250 PUSH(x);
2251 if (x != NULL)
2252 continue;
2253 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002254 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002255
Jeremy Hylton76901512000-03-28 23:49:17 +00002256 case CALL_FUNCTION_VAR:
2257 case CALL_FUNCTION_KW:
2258 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002259 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002260 int na = oparg & 0xff;
2261 int nk = (oparg>>8) & 0xff;
2262 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002263 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002264 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002265 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002266 if (flags & CALL_FLAG_VAR)
2267 n++;
2268 if (flags & CALL_FLAG_KW)
2269 n++;
2270 pfunc = stack_pointer - n - 1;
2271 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002272
Guido van Rossumac7be682001-01-17 15:42:30 +00002273 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002274 && PyMethod_GET_SELF(func) != NULL) {
2275 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002276 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002277 func = PyMethod_GET_FUNCTION(func);
2278 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002279 Py_DECREF(*pfunc);
2280 *pfunc = self;
2281 na++;
2282 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002283 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002284 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002285 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002286 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002287 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002288 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002289 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002290 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002291
Jeremy Hylton76901512000-03-28 23:49:17 +00002292 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002293 w = POP();
2294 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002295 }
2296 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002297 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002298 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002299 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002300 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Guido van Rossum681d79a1995-07-18 14:51:37 +00002302 case MAKE_FUNCTION:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002303 {
2304 int posdefaults = oparg & 0xff;
2305 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002306 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002307
Guido van Rossum681d79a1995-07-18 14:51:37 +00002308 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002309 x = PyFunction_New(v, f->f_globals);
2310 Py_DECREF(v);
Neal Norwitzc1505362006-12-28 06:47:50 +00002311
2312 if (x != NULL && num_annotations > 0) {
2313 Py_ssize_t name_ix;
2314 u = POP(); /* names of args with annotations */
2315 v = PyDict_New();
2316 if (v == NULL) {
2317 Py_DECREF(x);
2318 x = NULL;
2319 break;
2320 }
2321 name_ix = PyTuple_Size(u);
2322 assert(num_annotations == name_ix+1);
2323 while (name_ix > 0) {
2324 --name_ix;
2325 t = PyTuple_GET_ITEM(u, name_ix);
2326 w = POP();
2327 /* XXX(nnorwitz): check for errors */
2328 PyDict_SetItem(v, t, w);
2329 Py_DECREF(w);
2330 }
2331
2332 err = PyFunction_SetAnnotations(x, v);
2333 Py_DECREF(v);
2334 Py_DECREF(u);
2335 }
2336
Guido van Rossum681d79a1995-07-18 14:51:37 +00002337 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002338 if (x != NULL && posdefaults > 0) {
2339 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002340 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002341 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002342 x = NULL;
2343 break;
2344 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002345 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002346 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002347 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002348 }
2349 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002350 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002351 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002352 if (x != NULL && kwdefaults > 0) {
2353 v = PyDict_New();
2354 if (v == NULL) {
2355 Py_DECREF(x);
2356 x = NULL;
2357 break;
2358 }
2359 while (--kwdefaults >= 0) {
2360 w = POP(); /* default value */
2361 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002362 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002363 PyDict_SetItem(v, u, w);
2364 }
2365 err = PyFunction_SetKwDefaults(x, v);
2366 Py_DECREF(v);
2367 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002368 PUSH(x);
2369 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002370 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002371
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002372 case MAKE_CLOSURE:
2373 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002374 v = POP(); /* code object */
2375 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002376 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 if (x != NULL) {
2378 v = POP();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002379 err = PyFunction_SetClosure(x, v);
2380 Py_DECREF(v);
2381 }
2382 if (x != NULL && oparg > 0) {
2383 v = PyTuple_New(oparg);
2384 if (v == NULL) {
2385 Py_DECREF(x);
2386 x = NULL;
2387 break;
2388 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002389 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002390 w = POP();
2391 PyTuple_SET_ITEM(v, oparg, w);
2392 }
2393 err = PyFunction_SetDefaults(x, v);
2394 Py_DECREF(v);
2395 }
2396 PUSH(x);
2397 break;
2398 }
2399
Guido van Rossum8861b741996-07-30 16:49:37 +00002400 case BUILD_SLICE:
2401 if (oparg == 3)
2402 w = POP();
2403 else
2404 w = NULL;
2405 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002406 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002407 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002408 Py_DECREF(u);
2409 Py_DECREF(v);
2410 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002411 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002412 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002413 break;
2414
Fred Drakeef8ace32000-08-24 00:32:09 +00002415 case EXTENDED_ARG:
2416 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002417 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002418 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002419
Guido van Rossum374a9221991-04-04 10:40:29 +00002420 default:
2421 fprintf(stderr,
2422 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002423 PyCode_Addr2Line(f->f_code, f->f_lasti),
2424 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002425 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002426 why = WHY_EXCEPTION;
2427 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002428
2429#ifdef CASE_TOO_BIG
2430 }
2431#endif
2432
Guido van Rossum374a9221991-04-04 10:40:29 +00002433 } /* switch */
2434
2435 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002436
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002437 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002438
Guido van Rossum374a9221991-04-04 10:40:29 +00002439 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002440
Guido van Rossum374a9221991-04-04 10:40:29 +00002441 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002442 if (err == 0 && x != NULL) {
2443#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002444 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002445 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002446 fprintf(stderr,
2447 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002448 else {
2449#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002450 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002451 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002452#ifdef CHECKEXC
2453 }
2454#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002455 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002456 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002457 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002458 err = 0;
2459 }
2460
Guido van Rossum374a9221991-04-04 10:40:29 +00002461 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002462
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002463 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002464 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002465 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002466 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002467 why = WHY_EXCEPTION;
2468 }
2469 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002470#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002471 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002472 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002473 if (PyErr_Occurred()) {
Jeremy Hylton904ed862003-11-05 17:29:35 +00002474 char buf[1024];
2475 sprintf(buf, "Stack unwind with exception "
2476 "set and why=%d", why);
2477 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002478 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002479 }
2480#endif
2481
2482 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002483
Guido van Rossum374a9221991-04-04 10:40:29 +00002484 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002485 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002486
Fred Drake8f51f542001-10-04 14:48:42 +00002487 if (tstate->c_tracefunc != NULL)
2488 call_exc_trace(tstate->c_tracefunc,
2489 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002490 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002491
Guido van Rossum374a9221991-04-04 10:40:29 +00002492 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002493
Guido van Rossum374a9221991-04-04 10:40:29 +00002494 if (why == WHY_RERAISE)
2495 why = WHY_EXCEPTION;
2496
2497 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002498
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002499fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002500 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002501 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002502
Tim Peters8a5c3c72004-04-05 19:36:21 +00002503 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002504 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2505 /* For a continue inside a try block,
2506 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002507 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2508 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002509 why = WHY_NOT;
2510 JUMPTO(PyInt_AS_LONG(retval));
2511 Py_DECREF(retval);
2512 break;
2513 }
2514
Guido van Rossum374a9221991-04-04 10:40:29 +00002515 while (STACK_LEVEL() > b->b_level) {
2516 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002517 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002518 }
2519 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2520 why = WHY_NOT;
2521 JUMPTO(b->b_handler);
2522 break;
2523 }
2524 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002525 (b->b_type == SETUP_EXCEPT &&
2526 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002527 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002528 PyObject *exc, *val, *tb;
2529 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002530 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002531 val = Py_None;
2532 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002533 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002534 /* Make the raw exception data
2535 available to the handler,
2536 so a program can emulate the
2537 Python main loop. Don't do
2538 this for 'finally'. */
2539 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002540 PyErr_NormalizeException(
2541 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002542 set_exc_info(tstate,
2543 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002544 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002545 if (tb == NULL) {
2546 Py_INCREF(Py_None);
2547 PUSH(Py_None);
2548 } else
2549 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002550 PUSH(val);
2551 PUSH(exc);
2552 }
2553 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002554 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002555 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002556 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002557 PUSH(v);
2558 }
2559 why = WHY_NOT;
2560 JUMPTO(b->b_handler);
2561 break;
2562 }
2563 } /* unwind stack */
2564
2565 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002566
Guido van Rossum374a9221991-04-04 10:40:29 +00002567 if (why != WHY_NOT)
2568 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002569 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002570
Guido van Rossum374a9221991-04-04 10:40:29 +00002571 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002572
Tim Peters8a5c3c72004-04-05 19:36:21 +00002573 assert(why != WHY_YIELD);
2574 /* Pop remaining stack entries. */
2575 while (!EMPTY()) {
2576 v = POP();
2577 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002578 }
2579
Tim Peters8a5c3c72004-04-05 19:36:21 +00002580 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002581 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002582
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002583fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002584 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002585 if (tstate->c_tracefunc) {
2586 if (why == WHY_RETURN || why == WHY_YIELD) {
2587 if (call_trace(tstate->c_tracefunc,
2588 tstate->c_traceobj, f,
2589 PyTrace_RETURN, retval)) {
2590 Py_XDECREF(retval);
2591 retval = NULL;
2592 why = WHY_EXCEPTION;
2593 }
2594 }
2595 else if (why == WHY_EXCEPTION) {
2596 call_trace_protected(tstate->c_tracefunc,
2597 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002598 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002599 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002600 }
Fred Drake8f51f542001-10-04 14:48:42 +00002601 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002602 if (why == WHY_EXCEPTION)
2603 call_trace_protected(tstate->c_profilefunc,
2604 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002605 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002606 else if (call_trace(tstate->c_profilefunc,
2607 tstate->c_profileobj, f,
2608 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002609 Py_XDECREF(retval);
2610 retval = NULL;
2611 why = WHY_EXCEPTION;
2612 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002613 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002614 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002615
Thomas Wouters477c8d52006-05-27 19:21:47 +00002616 if (tstate->frame->f_exc_type != NULL)
2617 reset_exc_info(tstate);
2618 else {
2619 assert(tstate->frame->f_exc_value == NULL);
2620 assert(tstate->frame->f_exc_traceback == NULL);
2621 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002622
Tim Peters5ca576e2001-06-18 22:08:13 +00002623 /* pop frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002624 exit_eval_frame:
2625 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002626 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002627
Guido van Rossum96a42c81992-01-12 02:29:51 +00002628 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002629}
2630
Guido van Rossumc2e20742006-02-27 22:32:47 +00002631/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002632 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002633 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002634
Tim Peters6d6c1a32001-08-02 04:15:00 +00002635PyObject *
2636PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002637 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002638 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002639{
2640 register PyFrameObject *f;
2641 register PyObject *retval = NULL;
2642 register PyObject **fastlocals, **freevars;
2643 PyThreadState *tstate = PyThreadState_GET();
2644 PyObject *x, *u;
2645
2646 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002647 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002648 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002649 return NULL;
2650 }
2651
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002652 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002653 assert(globals != NULL);
2654 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002655 if (f == NULL)
2656 return NULL;
2657
2658 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002659 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002660
2661 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002662 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002663 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2664 int i;
2665 int n = argcount;
2666 PyObject *kwdict = NULL;
2667 if (co->co_flags & CO_VARKEYWORDS) {
2668 kwdict = PyDict_New();
2669 if (kwdict == NULL)
2670 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002671 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002672 if (co->co_flags & CO_VARARGS)
2673 i++;
2674 SETLOCAL(i, kwdict);
2675 }
2676 if (argcount > co->co_argcount) {
2677 if (!(co->co_flags & CO_VARARGS)) {
2678 PyErr_Format(PyExc_TypeError,
2679 "%.200s() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002680 "%spositional argument%s (%d given)",
Tim Peters5ca576e2001-06-18 22:08:13 +00002681 PyString_AsString(co->co_name),
2682 defcount ? "at most" : "exactly",
2683 co->co_argcount,
2684 kwcount ? "non-keyword " : "",
2685 co->co_argcount == 1 ? "" : "s",
2686 argcount);
2687 goto fail;
2688 }
2689 n = co->co_argcount;
2690 }
2691 for (i = 0; i < n; i++) {
2692 x = args[i];
2693 Py_INCREF(x);
2694 SETLOCAL(i, x);
2695 }
2696 if (co->co_flags & CO_VARARGS) {
2697 u = PyTuple_New(argcount - n);
2698 if (u == NULL)
2699 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002700 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002701 for (i = n; i < argcount; i++) {
2702 x = args[i];
2703 Py_INCREF(x);
2704 PyTuple_SET_ITEM(u, i-n, x);
2705 }
2706 }
2707 for (i = 0; i < kwcount; i++) {
2708 PyObject *keyword = kws[2*i];
2709 PyObject *value = kws[2*i + 1];
2710 int j;
2711 if (keyword == NULL || !PyString_Check(keyword)) {
2712 PyErr_Format(PyExc_TypeError,
2713 "%.200s() keywords must be strings",
2714 PyString_AsString(co->co_name));
2715 goto fail;
2716 }
2717 /* XXX slow -- speed up using dictionary? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002718 for (j = 0;
2719 j < co->co_argcount + co->co_kwonlyargcount;
2720 j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002721 PyObject *nm = PyTuple_GET_ITEM(
2722 co->co_varnames, j);
2723 int cmp = PyObject_RichCompareBool(
2724 keyword, nm, Py_EQ);
2725 if (cmp > 0)
2726 break;
2727 else if (cmp < 0)
2728 goto fail;
2729 }
2730 /* Check errors from Compare */
2731 if (PyErr_Occurred())
2732 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002733 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002734 if (kwdict == NULL) {
2735 PyErr_Format(PyExc_TypeError,
2736 "%.200s() got an unexpected "
2737 "keyword argument '%.400s'",
2738 PyString_AsString(co->co_name),
2739 PyString_AsString(keyword));
2740 goto fail;
2741 }
2742 PyDict_SetItem(kwdict, keyword, value);
2743 }
2744 else {
2745 if (GETLOCAL(j) != NULL) {
2746 PyErr_Format(PyExc_TypeError,
2747 "%.200s() got multiple "
2748 "values for keyword "
2749 "argument '%.400s'",
2750 PyString_AsString(co->co_name),
2751 PyString_AsString(keyword));
2752 goto fail;
2753 }
2754 Py_INCREF(value);
2755 SETLOCAL(j, value);
2756 }
2757 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002758 if (co->co_kwonlyargcount > 0) {
2759 for (i = co->co_argcount;
2760 i < co->co_argcount + co->co_kwonlyargcount;
2761 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00002762 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002763 if (GETLOCAL(i) != NULL)
2764 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00002765 name = PyTuple_GET_ITEM(co->co_varnames, i);
2766 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002767 if (kwdefs != NULL)
2768 def = PyDict_GetItem(kwdefs, name);
2769 if (def != NULL) {
2770 Py_INCREF(def);
2771 SETLOCAL(i, def);
2772 continue;
2773 }
2774 PyErr_Format(PyExc_TypeError,
2775 "%.200s() needs "
Brett Cannone33a6112007-01-29 23:43:38 +00002776 "keyword-only argument %s",
Guido van Rossum4f72a782006-10-27 23:31:49 +00002777 PyString_AsString(co->co_name),
2778 PyString_AsString(name));
2779 goto fail;
2780 }
2781 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002782 if (argcount < co->co_argcount) {
2783 int m = co->co_argcount - defcount;
2784 for (i = argcount; i < m; i++) {
2785 if (GETLOCAL(i) == NULL) {
2786 PyErr_Format(PyExc_TypeError,
2787 "%.200s() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002788 "%spositional argument%s "
2789 "(%d given)",
Tim Peters5ca576e2001-06-18 22:08:13 +00002790 PyString_AsString(co->co_name),
2791 ((co->co_flags & CO_VARARGS) ||
2792 defcount) ? "at least"
2793 : "exactly",
2794 m, kwcount ? "non-keyword " : "",
2795 m == 1 ? "" : "s", i);
2796 goto fail;
2797 }
2798 }
2799 if (n > m)
2800 i = n - m;
2801 else
2802 i = 0;
2803 for (; i < defcount; i++) {
2804 if (GETLOCAL(m+i) == NULL) {
2805 PyObject *def = defs[i];
2806 Py_INCREF(def);
2807 SETLOCAL(m+i, def);
2808 }
2809 }
2810 }
2811 }
2812 else {
2813 if (argcount > 0 || kwcount > 0) {
2814 PyErr_Format(PyExc_TypeError,
2815 "%.200s() takes no arguments (%d given)",
2816 PyString_AsString(co->co_name),
2817 argcount + kwcount);
2818 goto fail;
2819 }
2820 }
2821 /* Allocate and initialize storage for cell vars, and copy free
2822 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002823 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002824 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002825 char *cellname, *argname;
2826 PyObject *c;
2827
2828 nargs = co->co_argcount;
2829 if (co->co_flags & CO_VARARGS)
2830 nargs++;
2831 if (co->co_flags & CO_VARKEYWORDS)
2832 nargs++;
2833
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 /* Initialize each cell var, taking into account
2835 cell vars that are initialized from arguments.
2836
2837 Should arrange for the compiler to put cellvars
2838 that are arguments at the beginning of the cellvars
2839 list so that we can march over it more efficiently?
2840 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002841 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002842 cellname = PyString_AS_STRING(
2843 PyTuple_GET_ITEM(co->co_cellvars, i));
2844 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 for (j = 0; j < nargs; j++) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002846 argname = PyString_AS_STRING(
2847 PyTuple_GET_ITEM(co->co_varnames, j));
2848 if (strcmp(cellname, argname) == 0) {
2849 c = PyCell_New(GETLOCAL(j));
2850 if (c == NULL)
2851 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002852 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002853 found = 1;
2854 break;
2855 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002856 }
2857 if (found == 0) {
2858 c = PyCell_New(NULL);
2859 if (c == NULL)
2860 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002861 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002862 }
2863 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002864 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002865 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002866 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002867 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002868 PyObject *o = PyTuple_GET_ITEM(closure, i);
2869 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002870 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002871 }
2872 }
2873
Tim Peters5ca576e2001-06-18 22:08:13 +00002874 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002875 /* Don't need to keep the reference to f_back, it will be set
2876 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002877 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002878 f->f_back = NULL;
2879
Jeremy Hylton985eba52003-02-05 23:13:00 +00002880 PCALL(PCALL_GENERATOR);
2881
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002882 /* Create a new generator that owns the ready to run frame
2883 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002884 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002885 }
2886
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00002887 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002888
2889 fail: /* Jump here from prelude on failure */
2890
Tim Petersb13680b2001-11-27 23:29:29 +00002891 /* decref'ing the frame can cause __del__ methods to get invoked,
2892 which can call back into Python. While we're done with the
2893 current Python frame (f), the associated C stack is still in use,
2894 so recursion_depth must be boosted for the duration.
2895 */
2896 assert(tstate != NULL);
2897 ++tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002898 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002899 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002900 return retval;
2901}
2902
2903
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002904/* Implementation notes for set_exc_info() and reset_exc_info():
2905
2906- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2907 'exc_traceback'. These always travel together.
2908
2909- tstate->curexc_ZZZ is the "hot" exception that is set by
2910 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2911
2912- Once an exception is caught by an except clause, it is transferred
2913 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2914 can pick it up. This is the primary task of set_exc_info().
Thomas Wouters477c8d52006-05-27 19:21:47 +00002915 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002916
2917- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2918
2919 Long ago, when none of this existed, there were just a few globals:
2920 one set corresponding to the "hot" exception, and one set
2921 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2922 globals; they were simply stored as sys.exc_ZZZ. For backwards
2923 compatibility, they still are!) The problem was that in code like
2924 this:
2925
2926 try:
2927 "something that may fail"
2928 except "some exception":
2929 "do something else first"
2930 "print the exception from sys.exc_ZZZ."
2931
2932 if "do something else first" invoked something that raised and caught
2933 an exception, sys.exc_ZZZ were overwritten. That was a frequent
2934 cause of subtle bugs. I fixed this by changing the semantics as
2935 follows:
2936
2937 - Within one frame, sys.exc_ZZZ will hold the last exception caught
2938 *in that frame*.
2939
2940 - But initially, and as long as no exception is caught in a given
2941 frame, sys.exc_ZZZ will hold the last exception caught in the
2942 previous frame (or the frame before that, etc.).
2943
2944 The first bullet fixed the bug in the above example. The second
2945 bullet was for backwards compatibility: it was (and is) common to
2946 have a function that is called when an exception is caught, and to
2947 have that function access the caught exception via sys.exc_ZZZ.
2948 (Example: traceback.print_exc()).
2949
2950 At the same time I fixed the problem that sys.exc_ZZZ weren't
2951 thread-safe, by introducing sys.exc_info() which gets it from tstate;
2952 but that's really a separate improvement.
2953
2954 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
2955 variables to what they were before the current frame was called. The
2956 set_exc_info() function saves them on the frame so that
2957 reset_exc_info() can restore them. The invariant is that
2958 frame->f_exc_ZZZ is NULL iff the current frame never caught an
2959 exception (where "catching" an exception applies only to successful
2960 except clauses); and if the current frame ever caught an exception,
2961 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
2962 at the start of the current frame.
2963
2964*/
2965
Guido van Rossuma027efa1997-05-05 20:56:21 +00002966static void
Guido van Rossumac7be682001-01-17 15:42:30 +00002967set_exc_info(PyThreadState *tstate,
2968 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00002969{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002970 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002971 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00002972
Thomas Wouters477c8d52006-05-27 19:21:47 +00002973 assert(type != NULL);
2974 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002975 if (frame->f_exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002976 assert(frame->f_exc_value == NULL);
2977 assert(frame->f_exc_traceback == NULL);
2978 /* This frame didn't catch an exception before. */
2979 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002980 if (tstate->exc_type == NULL) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002981 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00002982 Py_INCREF(Py_None);
2983 tstate->exc_type = Py_None;
2984 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002985 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002986 Py_XINCREF(tstate->exc_value);
2987 Py_XINCREF(tstate->exc_traceback);
2988 frame->f_exc_type = tstate->exc_type;
2989 frame->f_exc_value = tstate->exc_value;
2990 frame->f_exc_traceback = tstate->exc_traceback;
2991 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002992 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00002993 tmp_type = tstate->exc_type;
2994 tmp_value = tstate->exc_value;
2995 tmp_tb = tstate->exc_traceback;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002996 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002997 Py_XINCREF(value);
2998 Py_XINCREF(tb);
2999 tstate->exc_type = type;
3000 tstate->exc_value = value;
3001 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003002 Py_XDECREF(tmp_type);
3003 Py_XDECREF(tmp_value);
3004 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003005 /* For b/w compatibility */
3006 PySys_SetObject("exc_type", type);
3007 PySys_SetObject("exc_value", value);
3008 PySys_SetObject("exc_traceback", tb);
3009}
3010
3011static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003012reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003013{
3014 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003015 PyObject *tmp_type, *tmp_value, *tmp_tb;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003016
3017 /* It's a precondition that the thread state's frame caught an
3018 * exception -- verify in a debug build.
3019 */
3020 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003021 frame = tstate->frame;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003022 assert(frame != NULL);
3023 assert(frame->f_exc_type != NULL);
3024
3025 /* Copy the frame's exception info back to the thread state. */
3026 tmp_type = tstate->exc_type;
3027 tmp_value = tstate->exc_value;
3028 tmp_tb = tstate->exc_traceback;
3029 Py_INCREF(frame->f_exc_type);
3030 Py_XINCREF(frame->f_exc_value);
3031 Py_XINCREF(frame->f_exc_traceback);
3032 tstate->exc_type = frame->f_exc_type;
3033 tstate->exc_value = frame->f_exc_value;
3034 tstate->exc_traceback = frame->f_exc_traceback;
3035 Py_XDECREF(tmp_type);
3036 Py_XDECREF(tmp_value);
3037 Py_XDECREF(tmp_tb);
3038
3039 /* For b/w compatibility */
3040 PySys_SetObject("exc_type", frame->f_exc_type);
3041 PySys_SetObject("exc_value", frame->f_exc_value);
3042 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3043
3044 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003045 tmp_type = frame->f_exc_type;
3046 tmp_value = frame->f_exc_value;
3047 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003048 frame->f_exc_type = NULL;
3049 frame->f_exc_value = NULL;
3050 frame->f_exc_traceback = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003051 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003052 Py_XDECREF(tmp_value);
3053 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003054}
3055
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003056/* Logic for the raise statement (too complicated for inlining).
3057 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003058static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003059do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003060{
Guido van Rossumd295f121998-04-09 21:39:57 +00003061 if (type == NULL) {
3062 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003063 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003064 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3065 value = tstate->exc_value;
3066 tb = tstate->exc_traceback;
3067 Py_XINCREF(type);
3068 Py_XINCREF(value);
3069 Py_XINCREF(tb);
3070 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003071
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003072 /* We support the following forms of raise:
3073 raise <class>, <classinstance>
3074 raise <class>, <argument tuple>
3075 raise <class>, None
3076 raise <class>, <argument>
3077 raise <classinstance>, None
3078 raise <string>, <object>
3079 raise <string>, None
3080
3081 An omitted second argument is the same as None.
3082
3083 In addition, raise <tuple>, <anything> is the same as
3084 raising the tuple's first item (and it better have one!);
3085 this rule is applied recursively.
3086
3087 Finally, an optional third argument can be supplied, which
3088 gives the traceback to be substituted (useful when
3089 re-raising an exception after examining it). */
3090
3091 /* First, check the traceback argument, replacing None with
3092 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003093 if (tb == Py_None) {
3094 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003095 tb = NULL;
3096 }
3097 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003098 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003099 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003100 goto raise_error;
3101 }
3102
3103 /* Next, replace a missing value with None */
3104 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003105 value = Py_None;
3106 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003107 }
3108
3109 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003110 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3111 PyObject *tmp = type;
3112 type = PyTuple_GET_ITEM(type, 0);
3113 Py_INCREF(type);
3114 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003115 }
3116
Guido van Rossum45aecf42006-03-15 04:58:47 +00003117 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003118 PyErr_NormalizeException(&type, &value, &tb);
3119
Brett Cannonbf364092006-03-01 04:25:17 +00003120 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003121 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003122 if (value != Py_None) {
3123 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003124 "instance exception may not have a separate value");
3125 goto raise_error;
3126 }
3127 else {
3128 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003129 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003130 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003131 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003132 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003133 }
3134 }
3135 else {
3136 /* Not something you can raise. You get an exception
3137 anyway, just not what you specified :-) */
Guido van Rossum45aecf42006-03-15 04:58:47 +00003138 PyErr_SetString(PyExc_TypeError,
3139 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003140 goto raise_error;
3141 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003142 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003143 if (tb == NULL)
3144 return WHY_EXCEPTION;
3145 else
3146 return WHY_RERAISE;
3147 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003148 Py_XDECREF(value);
3149 Py_XDECREF(type);
3150 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003151 return WHY_EXCEPTION;
3152}
3153
Tim Petersd6d010b2001-06-21 02:49:55 +00003154/* Iterate v argcnt times and store the results on the stack (via decreasing
3155 sp). Return 1 for success, 0 if error. */
3156
Barry Warsawe42b18f1997-08-25 22:13:04 +00003157static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003158unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003159{
Tim Petersd6d010b2001-06-21 02:49:55 +00003160 int i = 0;
3161 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003162 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003163
Tim Petersd6d010b2001-06-21 02:49:55 +00003164 assert(v != NULL);
3165
3166 it = PyObject_GetIter(v);
3167 if (it == NULL)
3168 goto Error;
3169
3170 for (; i < argcnt; i++) {
3171 w = PyIter_Next(it);
3172 if (w == NULL) {
3173 /* Iterator done, via error or exhaustion. */
3174 if (!PyErr_Occurred()) {
3175 PyErr_Format(PyExc_ValueError,
3176 "need more than %d value%s to unpack",
3177 i, i == 1 ? "" : "s");
3178 }
3179 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003180 }
3181 *--sp = w;
3182 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003183
3184 /* We better have exhausted the iterator now. */
3185 w = PyIter_Next(it);
3186 if (w == NULL) {
3187 if (PyErr_Occurred())
3188 goto Error;
3189 Py_DECREF(it);
3190 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003191 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003192 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003193 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003194 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003195Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003196 for (; i > 0; i--, sp++)
3197 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003198 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003199 return 0;
3200}
3201
3202
Guido van Rossum96a42c81992-01-12 02:29:51 +00003203#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003204static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003205prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003206{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003207 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003208 if (PyObject_Print(v, stdout, 0) != 0)
3209 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003210 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003211 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003212}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003213#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003214
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003215static void
Fred Drake5755ce62001-06-27 19:19:46 +00003216call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003217{
Guido van Rossumb209a111997-04-29 18:18:01 +00003218 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003219 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003220 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003221 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003222 value = Py_None;
3223 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003224 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003225 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003226 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003227 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003228 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003229 }
Fred Drake5755ce62001-06-27 19:19:46 +00003230 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003231 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003232 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003233 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003234 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003235 Py_XDECREF(type);
3236 Py_XDECREF(value);
3237 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003238 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003239}
3240
Fred Drake4ec5d562001-10-04 19:26:43 +00003241static void
3242call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003243 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003244{
3245 PyObject *type, *value, *traceback;
3246 int err;
3247 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003248 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003249 if (err == 0)
3250 PyErr_Restore(type, value, traceback);
3251 else {
3252 Py_XDECREF(type);
3253 Py_XDECREF(value);
3254 Py_XDECREF(traceback);
3255 }
3256}
3257
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003258static int
Fred Drake5755ce62001-06-27 19:19:46 +00003259call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3260 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003261{
Fred Drake5755ce62001-06-27 19:19:46 +00003262 register PyThreadState *tstate = frame->f_tstate;
3263 int result;
3264 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003265 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003266 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003267 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003268 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003269 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3270 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003271 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003272 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003273}
3274
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003275PyObject *
3276_PyEval_CallTracing(PyObject *func, PyObject *args)
3277{
3278 PyFrameObject *frame = PyEval_GetFrame();
3279 PyThreadState *tstate = frame->f_tstate;
3280 int save_tracing = tstate->tracing;
3281 int save_use_tracing = tstate->use_tracing;
3282 PyObject *result;
3283
3284 tstate->tracing = 0;
3285 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3286 || (tstate->c_profilefunc != NULL));
3287 result = PyObject_Call(func, args, NULL);
3288 tstate->tracing = save_tracing;
3289 tstate->use_tracing = save_use_tracing;
3290 return result;
3291}
3292
Michael W. Hudson006c7522002-11-08 13:08:46 +00003293static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003294maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003295 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3296 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003297{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003298 int result = 0;
3299
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003300 /* If the last instruction executed isn't in the current
3301 instruction window, reset the window. If the last
3302 instruction happens to fall at the start of a line or if it
3303 represents a jump backwards, call the trace function.
3304 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003305 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003306 int line;
3307 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003308
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003309 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3310 &bounds);
3311 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003312 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003313 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003314 PyTrace_LINE, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003315 }
3316 *instr_lb = bounds.ap_lower;
3317 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003318 }
Armin Rigobf57a142004-03-22 19:24:58 +00003319 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003320 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003321 }
3322 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003323 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003324}
3325
Fred Drake5755ce62001-06-27 19:19:46 +00003326void
3327PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003328{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003329 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003330 PyObject *temp = tstate->c_profileobj;
3331 Py_XINCREF(arg);
3332 tstate->c_profilefunc = NULL;
3333 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003334 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003335 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003336 Py_XDECREF(temp);
3337 tstate->c_profilefunc = func;
3338 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003339 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003340 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003341}
3342
3343void
3344PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3345{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003346 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003347 PyObject *temp = tstate->c_traceobj;
3348 Py_XINCREF(arg);
3349 tstate->c_tracefunc = NULL;
3350 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003351 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003352 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003353 Py_XDECREF(temp);
3354 tstate->c_tracefunc = func;
3355 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003356 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003357 tstate->use_tracing = ((func != NULL)
3358 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003359}
3360
Guido van Rossumb209a111997-04-29 18:18:01 +00003361PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003362PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003363{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003364 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003365 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003366 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003367 else
3368 return current_frame->f_builtins;
3369}
3370
Guido van Rossumb209a111997-04-29 18:18:01 +00003371PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003372PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003373{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003374 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003375 if (current_frame == NULL)
3376 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003377 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003378 return current_frame->f_locals;
3379}
3380
Guido van Rossumb209a111997-04-29 18:18:01 +00003381PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003382PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003383{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003384 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003385 if (current_frame == NULL)
3386 return NULL;
3387 else
3388 return current_frame->f_globals;
3389}
3390
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003391PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003392PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003393{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003394 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003395 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003396}
3397
Guido van Rossum6135a871995-01-09 17:53:26 +00003398int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003399PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003400{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003401 PyFrameObject *current_frame = PyEval_GetFrame();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003402 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003403}
3404
Guido van Rossumbe270261997-05-22 22:26:18 +00003405int
Tim Peters5ba58662001-07-16 02:29:45 +00003406PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003407{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003408 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003409 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003410
3411 if (current_frame != NULL) {
3412 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003413 const int compilerflags = codeflags & PyCF_MASK;
3414 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003415 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003416 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003417 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003418#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003419 if (codeflags & CO_GENERATOR_ALLOWED) {
3420 result = 1;
3421 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3422 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003423#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003424 }
3425 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003426}
3427
3428int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003429Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003430{
Guido van Rossumb209a111997-04-29 18:18:01 +00003431 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003432 if (f == NULL)
3433 return 0;
3434 if (!PyFile_SoftSpace(f, 0))
3435 return 0;
3436 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003437}
3438
Guido van Rossum3f5da241990-12-20 15:06:42 +00003439
Guido van Rossum681d79a1995-07-18 14:51:37 +00003440/* External interface to call any callable object.
3441 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003442
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003443#undef PyEval_CallObject
3444/* for backward compatibility: export this interface */
3445
Guido van Rossumb209a111997-04-29 18:18:01 +00003446PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003447PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003448{
Guido van Rossumb209a111997-04-29 18:18:01 +00003449 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003450}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003451#define PyEval_CallObject(func,arg) \
3452 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003453
Guido van Rossumb209a111997-04-29 18:18:01 +00003454PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003455PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003456{
Jeremy Hylton52820442001-01-03 23:52:36 +00003457 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003458
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003459 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003460 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003461 if (arg == NULL)
3462 return NULL;
3463 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003464 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003465 PyErr_SetString(PyExc_TypeError,
3466 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003467 return NULL;
3468 }
3469 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003470 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003471
Guido van Rossumb209a111997-04-29 18:18:01 +00003472 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003473 PyErr_SetString(PyExc_TypeError,
3474 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003475 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003476 return NULL;
3477 }
3478
Tim Peters6d6c1a32001-08-02 04:15:00 +00003479 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003480 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003481 return result;
3482}
3483
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003484const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003485PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003486{
3487 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003488 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003489 else if (PyFunction_Check(func))
3490 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3491 else if (PyCFunction_Check(func))
3492 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003493 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003494 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003495}
3496
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003497const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003498PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003499{
3500 if (PyMethod_Check(func))
3501 return "()";
3502 else if (PyFunction_Check(func))
3503 return "()";
3504 else if (PyCFunction_Check(func))
3505 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003506 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003507 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003508}
3509
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003510static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003511err_args(PyObject *func, int flags, int nargs)
3512{
3513 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003514 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003515 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003516 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003517 nargs);
3518 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003519 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003520 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003521 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003522 nargs);
3523}
3524
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003525#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003526if (tstate->use_tracing && tstate->c_profilefunc) { \
3527 if (call_trace(tstate->c_profilefunc, \
3528 tstate->c_profileobj, \
3529 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003530 func)) { \
3531 x = NULL; \
3532 } \
3533 else { \
3534 x = call; \
3535 if (tstate->c_profilefunc != NULL) { \
3536 if (x == NULL) { \
3537 call_trace_protected(tstate->c_profilefunc, \
3538 tstate->c_profileobj, \
3539 tstate->frame, PyTrace_C_EXCEPTION, \
3540 func); \
3541 /* XXX should pass (type, value, tb) */ \
3542 } else { \
3543 if (call_trace(tstate->c_profilefunc, \
3544 tstate->c_profileobj, \
3545 tstate->frame, PyTrace_C_RETURN, \
3546 func)) { \
3547 Py_DECREF(x); \
3548 x = NULL; \
3549 } \
3550 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003551 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003552 } \
3553} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003554 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003555 }
3556
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003557static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003558call_function(PyObject ***pp_stack, int oparg
3559#ifdef WITH_TSC
3560 , uint64* pintr0, uint64* pintr1
3561#endif
3562 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003563{
3564 int na = oparg & 0xff;
3565 int nk = (oparg>>8) & 0xff;
3566 int n = na + 2 * nk;
3567 PyObject **pfunc = (*pp_stack) - n - 1;
3568 PyObject *func = *pfunc;
3569 PyObject *x, *w;
3570
Jeremy Hylton985eba52003-02-05 23:13:00 +00003571 /* Always dispatch PyCFunction first, because these are
3572 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003573 */
3574 if (PyCFunction_Check(func) && nk == 0) {
3575 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003576 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003577
3578 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003579 if (flags & (METH_NOARGS | METH_O)) {
3580 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3581 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003582 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003583 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003584 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003585 else if (flags & METH_O && na == 1) {
3586 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003587 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003588 Py_DECREF(arg);
3589 }
3590 else {
3591 err_args(func, flags, na);
3592 x = NULL;
3593 }
3594 }
3595 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003596 PyObject *callargs;
3597 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003598 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003599 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003600 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003601 Py_XDECREF(callargs);
3602 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003603 } else {
3604 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3605 /* optimize access to bound methods */
3606 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003607 PCALL(PCALL_METHOD);
3608 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003609 Py_INCREF(self);
3610 func = PyMethod_GET_FUNCTION(func);
3611 Py_INCREF(func);
3612 Py_DECREF(*pfunc);
3613 *pfunc = self;
3614 na++;
3615 n++;
3616 } else
3617 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003618 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003619 if (PyFunction_Check(func))
3620 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003621 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003622 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003623 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003624 Py_DECREF(func);
3625 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003626
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003627 /* Clear the stack of the function object. Also removes
3628 the arguments in case they weren't consumed already
3629 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003630 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003631 while ((*pp_stack) > pfunc) {
3632 w = EXT_POP(*pp_stack);
3633 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003634 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003635 }
3636 return x;
3637}
3638
Jeremy Hylton192690e2002-08-16 18:36:11 +00003639/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003640 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003641 For the simplest case -- a function that takes only positional
3642 arguments and is called with only positional arguments -- it
3643 inlines the most primitive frame setup code from
3644 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3645 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003646*/
3647
3648static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003649fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003650{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003651 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003652 PyObject *globals = PyFunction_GET_GLOBALS(func);
3653 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003654 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003655 PyObject **d = NULL;
3656 int nd = 0;
3657
Jeremy Hylton985eba52003-02-05 23:13:00 +00003658 PCALL(PCALL_FUNCTION);
3659 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003660 if (argdefs == NULL && co->co_argcount == n &&
3661 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003662 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3663 PyFrameObject *f;
3664 PyObject *retval = NULL;
3665 PyThreadState *tstate = PyThreadState_GET();
3666 PyObject **fastlocals, **stack;
3667 int i;
3668
3669 PCALL(PCALL_FASTER_FUNCTION);
3670 assert(globals != NULL);
3671 /* XXX Perhaps we should create a specialized
3672 PyFrame_New() that doesn't take locals, but does
3673 take builtins without sanity checking them.
3674 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003675 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003676 f = PyFrame_New(tstate, co, globals, NULL);
3677 if (f == NULL)
3678 return NULL;
3679
3680 fastlocals = f->f_localsplus;
3681 stack = (*pp_stack) - n;
3682
3683 for (i = 0; i < n; i++) {
3684 Py_INCREF(*stack);
3685 fastlocals[i] = *stack++;
3686 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003687 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003688 ++tstate->recursion_depth;
3689 Py_DECREF(f);
3690 --tstate->recursion_depth;
3691 return retval;
3692 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003693 if (argdefs != NULL) {
3694 d = &PyTuple_GET_ITEM(argdefs, 0);
3695 nd = ((PyTupleObject *)argdefs)->ob_size;
3696 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003697 return PyEval_EvalCodeEx(co, globals,
3698 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003699 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003700 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003701}
3702
3703static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003704update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3705 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003706{
3707 PyObject *kwdict = NULL;
3708 if (orig_kwdict == NULL)
3709 kwdict = PyDict_New();
3710 else {
3711 kwdict = PyDict_Copy(orig_kwdict);
3712 Py_DECREF(orig_kwdict);
3713 }
3714 if (kwdict == NULL)
3715 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003716 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003717 int err;
3718 PyObject *value = EXT_POP(*pp_stack);
3719 PyObject *key = EXT_POP(*pp_stack);
3720 if (PyDict_GetItem(kwdict, key) != NULL) {
Guido van Rossumac7be682001-01-17 15:42:30 +00003721 PyErr_Format(PyExc_TypeError,
Ka-Ping Yee20579702001-01-15 22:14:16 +00003722 "%.200s%s got multiple values "
Jeremy Hylton512a2372001-04-11 13:52:29 +00003723 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003724 PyEval_GetFuncName(func),
3725 PyEval_GetFuncDesc(func),
Jeremy Hylton512a2372001-04-11 13:52:29 +00003726 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003727 Py_DECREF(key);
3728 Py_DECREF(value);
3729 Py_DECREF(kwdict);
3730 return NULL;
3731 }
3732 err = PyDict_SetItem(kwdict, key, value);
3733 Py_DECREF(key);
3734 Py_DECREF(value);
3735 if (err) {
3736 Py_DECREF(kwdict);
3737 return NULL;
3738 }
3739 }
3740 return kwdict;
3741}
3742
3743static PyObject *
3744update_star_args(int nstack, int nstar, PyObject *stararg,
3745 PyObject ***pp_stack)
3746{
3747 PyObject *callargs, *w;
3748
3749 callargs = PyTuple_New(nstack + nstar);
3750 if (callargs == NULL) {
3751 return NULL;
3752 }
3753 if (nstar) {
3754 int i;
3755 for (i = 0; i < nstar; i++) {
3756 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3757 Py_INCREF(a);
3758 PyTuple_SET_ITEM(callargs, nstack + i, a);
3759 }
3760 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003761 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003762 w = EXT_POP(*pp_stack);
3763 PyTuple_SET_ITEM(callargs, nstack, w);
3764 }
3765 return callargs;
3766}
3767
3768static PyObject *
3769load_args(PyObject ***pp_stack, int na)
3770{
3771 PyObject *args = PyTuple_New(na);
3772 PyObject *w;
3773
3774 if (args == NULL)
3775 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003776 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003777 w = EXT_POP(*pp_stack);
3778 PyTuple_SET_ITEM(args, na, w);
3779 }
3780 return args;
3781}
3782
3783static PyObject *
3784do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3785{
3786 PyObject *callargs = NULL;
3787 PyObject *kwdict = NULL;
3788 PyObject *result = NULL;
3789
3790 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003791 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003792 if (kwdict == NULL)
3793 goto call_fail;
3794 }
3795 callargs = load_args(pp_stack, na);
3796 if (callargs == NULL)
3797 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003798#ifdef CALL_PROFILE
3799 /* At this point, we have to look at the type of func to
3800 update the call stats properly. Do it here so as to avoid
3801 exposing the call stats machinery outside ceval.c
3802 */
3803 if (PyFunction_Check(func))
3804 PCALL(PCALL_FUNCTION);
3805 else if (PyMethod_Check(func))
3806 PCALL(PCALL_METHOD);
3807 else if (PyType_Check(func))
3808 PCALL(PCALL_TYPE);
3809 else
3810 PCALL(PCALL_OTHER);
3811#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003812 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003813 call_fail:
3814 Py_XDECREF(callargs);
3815 Py_XDECREF(kwdict);
3816 return result;
3817}
3818
3819static PyObject *
3820ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3821{
3822 int nstar = 0;
3823 PyObject *callargs = NULL;
3824 PyObject *stararg = NULL;
3825 PyObject *kwdict = NULL;
3826 PyObject *result = NULL;
3827
3828 if (flags & CALL_FLAG_KW) {
3829 kwdict = EXT_POP(*pp_stack);
3830 if (!(kwdict && PyDict_Check(kwdict))) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003831 PyErr_Format(PyExc_TypeError,
Jeremy Hylton512a2372001-04-11 13:52:29 +00003832 "%s%s argument after ** "
3833 "must be a dictionary",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003834 PyEval_GetFuncName(func),
3835 PyEval_GetFuncDesc(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003836 goto ext_call_fail;
3837 }
3838 }
3839 if (flags & CALL_FLAG_VAR) {
3840 stararg = EXT_POP(*pp_stack);
3841 if (!PyTuple_Check(stararg)) {
3842 PyObject *t = NULL;
3843 t = PySequence_Tuple(stararg);
3844 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003845 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3846 PyErr_Format(PyExc_TypeError,
3847 "%s%s argument after * "
3848 "must be a sequence",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003849 PyEval_GetFuncName(func),
3850 PyEval_GetFuncDesc(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003851 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003852 goto ext_call_fail;
3853 }
3854 Py_DECREF(stararg);
3855 stararg = t;
3856 }
3857 nstar = PyTuple_GET_SIZE(stararg);
3858 }
3859 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003860 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003861 if (kwdict == NULL)
3862 goto ext_call_fail;
3863 }
3864 callargs = update_star_args(na, nstar, stararg, pp_stack);
3865 if (callargs == NULL)
3866 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003867#ifdef CALL_PROFILE
3868 /* At this point, we have to look at the type of func to
3869 update the call stats properly. Do it here so as to avoid
3870 exposing the call stats machinery outside ceval.c
3871 */
3872 if (PyFunction_Check(func))
3873 PCALL(PCALL_FUNCTION);
3874 else if (PyMethod_Check(func))
3875 PCALL(PCALL_METHOD);
3876 else if (PyType_Check(func))
3877 PCALL(PCALL_TYPE);
3878 else
3879 PCALL(PCALL_OTHER);
3880#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003881 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003882 ext_call_fail:
3883 Py_XDECREF(callargs);
3884 Py_XDECREF(kwdict);
3885 Py_XDECREF(stararg);
3886 return result;
3887}
3888
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003889/* Extract a slice index from a PyInt or PyLong or an object with the
3890 nb_index slot defined, and store in *pi.
3891 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3892 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 +00003893 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003894*/
Tim Petersb5196382001-12-16 19:44:20 +00003895/* Note: If v is NULL, return success without storing into *pi. This
3896 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3897 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003898*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003899int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003900_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003901{
Tim Petersb5196382001-12-16 19:44:20 +00003902 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003903 Py_ssize_t x;
Guido van Rossumddefaf32007-01-14 03:31:43 +00003904 if (PyInt_CheckExact(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003905 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
3906 however, it looks like it should be AsSsize_t.
3907 There should be a comment here explaining why.
3908 */
3909 x = PyInt_AS_LONG(v);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003910 }
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003911 else if (PyIndex_Check(v)) {
3912 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003913 if (x == -1 && PyErr_Occurred())
3914 return 0;
3915 }
3916 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003917 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003918 "slice indices must be integers or "
3919 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003920 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003921 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003922 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003923 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003924 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003925}
3926
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003927#undef ISINDEX
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003928#define ISINDEX(x) ((x) == NULL || \
3929 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00003930
Guido van Rossumb209a111997-04-29 18:18:01 +00003931static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003932apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003933{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003934 PyTypeObject *tp = u->ob_type;
3935 PySequenceMethods *sq = tp->tp_as_sequence;
3936
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003937 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003938 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003939 if (!_PyEval_SliceIndex(v, &ilow))
3940 return NULL;
3941 if (!_PyEval_SliceIndex(w, &ihigh))
3942 return NULL;
3943 return PySequence_GetSlice(u, ilow, ihigh);
3944 }
3945 else {
3946 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00003947 if (slice != NULL) {
3948 PyObject *res = PyObject_GetItem(u, slice);
3949 Py_DECREF(slice);
3950 return res;
3951 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00003952 else
3953 return NULL;
3954 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003955}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003956
3957static int
Guido van Rossumac7be682001-01-17 15:42:30 +00003958assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
3959 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003960{
Guido van Rossum50d756e2001-08-18 17:43:36 +00003961 PyTypeObject *tp = u->ob_type;
3962 PySequenceMethods *sq = tp->tp_as_sequence;
3963
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003964 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003965 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003966 if (!_PyEval_SliceIndex(v, &ilow))
3967 return -1;
3968 if (!_PyEval_SliceIndex(w, &ihigh))
3969 return -1;
3970 if (x == NULL)
3971 return PySequence_DelSlice(u, ilow, ihigh);
3972 else
3973 return PySequence_SetSlice(u, ilow, ihigh, x);
3974 }
3975 else {
3976 PyObject *slice = PySlice_New(v, w, NULL);
3977 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00003978 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003979 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00003980 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00003981 else
Guido van Rossum354797c2001-12-03 19:45:06 +00003982 res = PyObject_DelItem(u, slice);
3983 Py_DECREF(slice);
3984 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00003985 }
3986 else
3987 return -1;
3988 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003989}
3990
Guido van Rossumb209a111997-04-29 18:18:01 +00003991static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003992cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003993{
Guido van Rossumac7be682001-01-17 15:42:30 +00003994 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003995 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003996 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003997 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003998 break;
3999 case PyCmp_IS_NOT:
4000 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004001 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004002 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004003 res = PySequence_Contains(w, v);
4004 if (res < 0)
4005 return NULL;
4006 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004007 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004008 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004009 if (res < 0)
4010 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004011 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004012 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004013 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004014 if (PyTuple_Check(w)) {
4015 Py_ssize_t i, length;
4016 length = PyTuple_Size(w);
4017 for (i = 0; i < length; i += 1) {
4018 PyObject *exc = PyTuple_GET_ITEM(w, i);
4019 if (PyString_Check(exc)) {
4020 int ret_val;
4021 ret_val = PyErr_WarnEx(
4022 PyExc_DeprecationWarning,
4023 "catching of string "
4024 "exceptions is "
4025 "deprecated", 1);
4026 if (ret_val == -1)
4027 return NULL;
4028 }
4029 }
4030 }
4031 else {
4032 if (PyString_Check(w)) {
4033 int ret_val;
4034 ret_val = PyErr_WarnEx(
4035 PyExc_DeprecationWarning,
4036 "catching of string "
4037 "exceptions is deprecated",
4038 1);
4039 if (ret_val == -1)
4040 return NULL;
4041 }
4042 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004043 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004044 break;
4045 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004046 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004047 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004048 v = res ? Py_True : Py_False;
4049 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004050 return v;
4051}
4052
Thomas Wouters52152252000-08-17 22:55:00 +00004053static PyObject *
4054import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004055{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004056 PyObject *x;
4057
4058 x = PyObject_GetAttr(v, name);
4059 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004060 PyErr_Format(PyExc_ImportError,
4061 "cannot import name %.230s",
4062 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004063 }
Thomas Wouters52152252000-08-17 22:55:00 +00004064 return x;
4065}
Guido van Rossumac7be682001-01-17 15:42:30 +00004066
Thomas Wouters52152252000-08-17 22:55:00 +00004067static int
4068import_all_from(PyObject *locals, PyObject *v)
4069{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004070 PyObject *all = PyObject_GetAttrString(v, "__all__");
4071 PyObject *dict, *name, *value;
4072 int skip_leading_underscores = 0;
4073 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004074
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004075 if (all == NULL) {
4076 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4077 return -1; /* Unexpected error */
4078 PyErr_Clear();
4079 dict = PyObject_GetAttrString(v, "__dict__");
4080 if (dict == NULL) {
4081 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4082 return -1;
4083 PyErr_SetString(PyExc_ImportError,
4084 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004085 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004086 }
4087 all = PyMapping_Keys(dict);
4088 Py_DECREF(dict);
4089 if (all == NULL)
4090 return -1;
4091 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004092 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004093
4094 for (pos = 0, err = 0; ; pos++) {
4095 name = PySequence_GetItem(all, pos);
4096 if (name == NULL) {
4097 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4098 err = -1;
4099 else
4100 PyErr_Clear();
4101 break;
4102 }
4103 if (skip_leading_underscores &&
4104 PyString_Check(name) &&
4105 PyString_AS_STRING(name)[0] == '_')
4106 {
4107 Py_DECREF(name);
4108 continue;
4109 }
4110 value = PyObject_GetAttr(v, name);
4111 if (value == NULL)
4112 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004113 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004114 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004115 else
4116 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004117 Py_DECREF(name);
4118 Py_XDECREF(value);
4119 if (err != 0)
4120 break;
4121 }
4122 Py_DECREF(all);
4123 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004124}
4125
Guido van Rossumb209a111997-04-29 18:18:01 +00004126static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004127build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004128{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004129 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004130
4131 if (PyDict_Check(methods))
4132 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004133 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004134 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004135 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4136 base = PyTuple_GET_ITEM(bases, 0);
4137 metaclass = PyObject_GetAttrString(base, "__class__");
4138 if (metaclass == NULL) {
4139 PyErr_Clear();
4140 metaclass = (PyObject *)base->ob_type;
4141 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004142 }
4143 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004144 else {
4145 PyObject *g = PyEval_GetGlobals();
4146 if (g != NULL && PyDict_Check(g))
4147 metaclass = PyDict_GetItemString(g, "__metaclass__");
4148 if (metaclass == NULL)
Guido van Rossum45aecf42006-03-15 04:58:47 +00004149 metaclass = (PyObject *) &PyType_Type;
Guido van Rossum7851eea2001-09-12 19:19:18 +00004150 Py_INCREF(metaclass);
4151 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004152 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods, NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004153 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004154 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004155 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004156 in a base that was not a class (such the random module
4157 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004158 by augmenting the error message with more information.*/
4159
4160 PyObject *ptype, *pvalue, *ptraceback;
4161
4162 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4163 if (PyString_Check(pvalue)) {
4164 PyObject *newmsg;
4165 newmsg = PyString_FromFormat(
4166 "Error when calling the metaclass bases\n %s",
4167 PyString_AS_STRING(pvalue));
4168 if (newmsg != NULL) {
4169 Py_DECREF(pvalue);
4170 pvalue = newmsg;
4171 }
4172 }
4173 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004174 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004175 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004176}
4177
Guido van Rossumac7be682001-01-17 15:42:30 +00004178static void
Paul Prescode68140d2000-08-30 20:25:01 +00004179format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4180{
4181 char *obj_str;
4182
4183 if (!obj)
4184 return;
4185
4186 obj_str = PyString_AsString(obj);
4187 if (!obj_str)
4188 return;
4189
4190 PyErr_Format(exc, format_str, obj_str);
4191}
Guido van Rossum950361c1997-01-24 13:49:28 +00004192
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004193static PyObject *
4194string_concatenate(PyObject *v, PyObject *w,
4195 PyFrameObject *f, unsigned char *next_instr)
4196{
4197 /* This function implements 'variable += expr' when both arguments
4198 are strings. */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004199 Py_ssize_t v_len = PyString_GET_SIZE(v);
4200 Py_ssize_t w_len = PyString_GET_SIZE(w);
4201 Py_ssize_t new_len = v_len + w_len;
4202 if (new_len < 0) {
4203 PyErr_SetString(PyExc_OverflowError,
4204 "strings are too large to concat");
4205 return NULL;
4206 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004207
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004208 if (v->ob_refcnt == 2) {
4209 /* In the common case, there are 2 references to the value
4210 * stored in 'variable' when the += is performed: one on the
4211 * value stack (in 'v') and one still stored in the 'variable'.
4212 * We try to delete the variable now to reduce the refcnt to 1.
4213 */
4214 switch (*next_instr) {
4215 case STORE_FAST:
4216 {
4217 int oparg = PEEKARG();
4218 PyObject **fastlocals = f->f_localsplus;
4219 if (GETLOCAL(oparg) == v)
4220 SETLOCAL(oparg, NULL);
4221 break;
4222 }
4223 case STORE_DEREF:
4224 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00004225 PyObject **freevars = f->f_localsplus + f->f_code->co_nlocals;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004226 PyObject *c = freevars[PEEKARG()];
4227 if (PyCell_GET(c) == v)
4228 PyCell_Set(c, NULL);
4229 break;
4230 }
4231 case STORE_NAME:
4232 {
4233 PyObject *names = f->f_code->co_names;
4234 PyObject *name = GETITEM(names, PEEKARG());
4235 PyObject *locals = f->f_locals;
4236 if (PyDict_CheckExact(locals) &&
4237 PyDict_GetItem(locals, name) == v) {
4238 if (PyDict_DelItem(locals, name) != 0) {
4239 PyErr_Clear();
4240 }
4241 }
4242 break;
4243 }
4244 }
4245 }
4246
Armin Rigo618fbf52004-08-07 20:58:32 +00004247 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004248 /* Now we own the last reference to 'v', so we can resize it
4249 * in-place.
4250 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004251 if (_PyString_Resize(&v, new_len) != 0) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004252 /* XXX if _PyString_Resize() fails, 'v' has been
4253 * deallocated so it cannot be put back into 'variable'.
4254 * The MemoryError is raised when there is no value in
4255 * 'variable', which might (very remotely) be a cause
4256 * of incompatibilities.
4257 */
4258 return NULL;
4259 }
4260 /* copy 'w' into the newly allocated area of 'v' */
4261 memcpy(PyString_AS_STRING(v) + v_len,
4262 PyString_AS_STRING(w), w_len);
4263 return v;
4264 }
4265 else {
4266 /* When in-place resizing is not an option. */
4267 PyString_Concat(&v, w);
4268 return v;
4269 }
4270}
4271
Guido van Rossum950361c1997-01-24 13:49:28 +00004272#ifdef DYNAMIC_EXECUTION_PROFILE
4273
Skip Montanarof118cb12001-10-15 20:51:38 +00004274static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004275getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004276{
4277 int i;
4278 PyObject *l = PyList_New(256);
4279 if (l == NULL) return NULL;
4280 for (i = 0; i < 256; i++) {
4281 PyObject *x = PyInt_FromLong(a[i]);
4282 if (x == NULL) {
4283 Py_DECREF(l);
4284 return NULL;
4285 }
4286 PyList_SetItem(l, i, x);
4287 }
4288 for (i = 0; i < 256; i++)
4289 a[i] = 0;
4290 return l;
4291}
4292
4293PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004294_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004295{
4296#ifndef DXPAIRS
4297 return getarray(dxp);
4298#else
4299 int i;
4300 PyObject *l = PyList_New(257);
4301 if (l == NULL) return NULL;
4302 for (i = 0; i < 257; i++) {
4303 PyObject *x = getarray(dxpairs[i]);
4304 if (x == NULL) {
4305 Py_DECREF(l);
4306 return NULL;
4307 }
4308 PyList_SetItem(l, i, x);
4309 }
4310 return l;
4311#endif
4312}
4313
4314#endif