blob: 5bac65fd0dc743c2e70851b4c7a7149e4eccc44f [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
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#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
Tim Peters7df5e7f2006-05-26 23:14:37 +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
Thomas Wouterse2176022007-09-20 17:35:10 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Fredrik Lundh7a830892006-05-27 10:39:48 +000037static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000038ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Tim Peters7df5e7f2006-05-26 23:14:37 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Michael W. Hudson75eabd22005-01-18 15:56:11 +000054#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000055
Michael W. Hudson75eabd22005-01-18 15:56:11 +000056#define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
59#endif
60
Tim Peters7df5e7f2006-05-26 23:14:37 +000061void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63{
64 uint64 intr, inst, loop;
65 PyThreadState *tstate = PyThreadState_Get();
66 if (!tstate->interp->tscdump)
67 return;
68 intr = intr1 - intr0;
69 inst = inst1 - inst0 - intr;
70 loop = loop1 - loop0 - intr;
71 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode, ticked, inst, loop);
73}
Michael W. Hudson800ba232004-08-12 18:19:17 +000074
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000075#endif
76
Guido van Rossum04691fc1992-08-12 15:35:34 +000077/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000079
Guido van Rossum408027e1996-12-30 16:17:54 +000080#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000081/* For debugging the interpreter: */
82#define LLTRACE 1 /* Low-level trace feature */
83#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084#endif
85
Jeremy Hylton52820442001-01-03 23:52:36 +000086typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000087
Guido van Rossum374a9221991-04-04 10:40:29 +000088/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Fredrik Lundh7a830892006-05-27 10:39:48 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +000094static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
95static PyObject * do_call(PyObject *, PyObject ***, int, int);
96static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +000099static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
100static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000101#define CALL_FLAG_VAR 1
102#define CALL_FLAG_KW 2
103
Guido van Rossum0a066c01992-03-27 17:29:15 +0000104#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000105static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Fred Drake5755ce62001-06-27 19:19:46 +0000109 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
113static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000114 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000115
Fredrik Lundh7a830892006-05-27 10:39:48 +0000116static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
117static int assign_slice(PyObject *, PyObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000118 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000119static PyObject * cmp_outcome(int, PyObject *, PyObject *);
120static PyObject * import_from(PyObject *, PyObject *);
121static int import_all_from(PyObject *, PyObject *);
122static PyObject * build_class(PyObject *, PyObject *, PyObject *);
123static int exec_statement(PyFrameObject *,
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000125static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
126static void reset_exc_info(PyThreadState *);
127static void format_exc_check_arg(PyObject *, char *, PyObject *);
128static PyObject * string_concatenate(PyObject *, PyObject *,
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000129 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000130
Paul Prescode68140d2000-08-30 20:25:01 +0000131#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000132 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000133#define GLOBAL_NAME_ERROR_MSG \
134 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000135#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000136 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000137#define UNBOUNDFREE_ERROR_MSG \
138 "free variable '%.200s' referenced before assignment" \
139 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000140
Guido van Rossum950361c1997-01-24 13:49:28 +0000141/* Dynamic execution profile */
142#ifdef DYNAMIC_EXECUTION_PROFILE
143#ifdef DXPAIRS
144static long dxpairs[257][256];
145#define dxp dxpairs[256]
146#else
147static long dxp[256];
148#endif
149#endif
150
Jeremy Hylton985eba52003-02-05 23:13:00 +0000151/* Function call profile */
152#ifdef CALL_PROFILE
153#define PCALL_NUM 11
154static int pcall[PCALL_NUM];
155
156#define PCALL_ALL 0
157#define PCALL_FUNCTION 1
158#define PCALL_FAST_FUNCTION 2
159#define PCALL_FASTER_FUNCTION 3
160#define PCALL_METHOD 4
161#define PCALL_BOUND_METHOD 5
162#define PCALL_CFUNCTION 6
163#define PCALL_TYPE 7
164#define PCALL_GENERATOR 8
165#define PCALL_OTHER 9
166#define PCALL_POP 10
167
168/* Notes about the statistics
169
170 PCALL_FAST stats
171
172 FAST_FUNCTION means no argument tuple needs to be created.
173 FASTER_FUNCTION means that the fast-path frame setup code is used.
174
175 If there is a method call where the call can be optimized by changing
176 the argument tuple and calling the function directly, it gets recorded
177 twice.
178
179 As a result, the relationship among the statistics appears to be
180 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
181 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
182 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
183 PCALL_METHOD > PCALL_BOUND_METHOD
184*/
185
186#define PCALL(POS) pcall[POS]++
187
188PyObject *
189PyEval_GetCallStats(PyObject *self)
190{
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000191 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000192 pcall[0], pcall[1], pcall[2], pcall[3],
193 pcall[4], pcall[5], pcall[6], pcall[7],
Andrew M. Kuchling1f3ebe02006-10-27 13:22:46 +0000194 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000195}
196#else
197#define PCALL(O)
198
199PyObject *
200PyEval_GetCallStats(PyObject *self)
201{
202 Py_INCREF(Py_None);
203 return Py_None;
204}
205#endif
206
Tim Peters5ca576e2001-06-18 22:08:13 +0000207
Guido van Rossume59214e1994-08-30 08:01:59 +0000208#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000209
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000210#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000211#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000212#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000213#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000214
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000215static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Guido van Rossuma9672091994-09-14 13:31:22 +0000216static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217
Tim Peters7f468f22004-10-11 02:40:51 +0000218int
219PyEval_ThreadsInitialized(void)
220{
221 return interpreter_lock != 0;
222}
223
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000224void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000225PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000227 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000228 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000229 interpreter_lock = PyThread_allocate_lock();
230 PyThread_acquire_lock(interpreter_lock, 1);
231 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000232}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000233
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000234void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000237 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238}
239
240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000243 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000244}
245
246void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000248{
249 if (tstate == NULL)
250 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000251 /* Check someone has called PyEval_InitThreads() to create the lock */
252 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000253 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000254 if (PyThreadState_Swap(tstate) != NULL)
255 Py_FatalError(
256 "PyEval_AcquireThread: non-NULL old thread state");
257}
258
259void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000260PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000261{
262 if (tstate == NULL)
263 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
264 if (PyThreadState_Swap(NULL) != tstate)
265 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000266 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000267}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000268
269/* This function is called from PyOS_AfterFork to ensure that newly
270 created child processes don't hold locks referring to threads which
271 are not running in the child process. (This could also be done using
272 pthread_atfork mechanism, at least for the pthreads implementation.) */
273
274void
275PyEval_ReInitThreads(void)
276{
Jesse Noller5e62ca42008-07-16 20:03:47 +0000277 PyObject *threading, *result;
278 PyThreadState *tstate;
279
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000280 if (!interpreter_lock)
281 return;
282 /*XXX Can't use PyThread_free_lock here because it does too
283 much error-checking. Doing this cleanly would require
284 adding a new function to each thread_*.h. Instead, just
285 create a new lock and waste a little bit of memory */
286 interpreter_lock = PyThread_allocate_lock();
287 PyThread_acquire_lock(interpreter_lock, 1);
288 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000289
290 /* Update the threading module with the new state.
291 */
292 tstate = PyThreadState_GET();
293 threading = PyMapping_GetItemString(tstate->interp->modules,
294 "threading");
295 if (threading == NULL) {
296 /* threading not imported */
297 PyErr_Clear();
298 return;
299 }
300 result = PyObject_CallMethod(threading, "_after_fork", NULL);
301 if (result == NULL)
302 PyErr_WriteUnraisable(threading);
303 else
304 Py_DECREF(result);
305 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000306}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000307#endif
308
Guido van Rossumff4949e1992-08-05 19:58:53 +0000309/* Functions save_thread and restore_thread are always defined so
310 dynamically loaded modules needn't be compiled separately for use
311 with and without threads: */
312
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000313PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000315{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000316 PyThreadState *tstate = PyThreadState_Swap(NULL);
317 if (tstate == NULL)
318 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000319#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000320 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000321 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000323 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000324}
325
326void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000328{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000329 if (tstate == NULL)
330 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000331#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000332 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000333 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000334 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000335 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000336 }
337#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000338 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000339}
340
341
Guido van Rossuma9672091994-09-14 13:31:22 +0000342/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
343 signal handlers or Mac I/O completion routines) can schedule calls
344 to a function to be called synchronously.
345 The synchronous function is called with one void* argument.
346 It should return 0 for success or -1 for failure -- failure should
347 be accompanied by an exception.
348
349 If registry succeeds, the registry function returns 0; if it fails
350 (e.g. due to too many pending calls) it returns -1 (without setting
351 an exception condition).
352
353 Note that because registry may occur from within signal handlers,
354 or other asynchronous events, calling malloc() is unsafe!
355
356#ifdef WITH_THREAD
357 Any thread can schedule pending calls, but only the main thread
358 will execute them.
359#endif
360
361 XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
362 There are two possible race conditions:
363 (1) nested asynchronous registry calls;
364 (2) registry calls made while pending calls are being processed.
365 While (1) is very unlikely, (2) is a real possibility.
366 The current code is safe against (2), but not against (1).
367 The safety against (2) is derived from the fact that only one
368 thread (the main thread) ever takes things out of the queue.
Guido van Rossuma9672091994-09-14 13:31:22 +0000369
Guido van Rossuma027efa1997-05-05 20:56:21 +0000370 XXX Darn! With the advent of thread state, we should have an array
371 of pending calls per thread in the thread state! Later...
372*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000373
Guido van Rossuma9672091994-09-14 13:31:22 +0000374#define NPENDINGCALLS 32
375static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000376 int (*func)(void *);
377 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000378} pendingcalls[NPENDINGCALLS];
379static volatile int pendingfirst = 0;
380static volatile int pendinglast = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000381static volatile int things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000382
383int
Thomas Wouters334fb892000-07-25 12:56:38 +0000384Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000385{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000386 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000387 int i, j;
388 /* XXX Begin critical section */
389 /* XXX If you want this to be safe against nested
390 XXX asynchronous calls, you'll have to work harder! */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000391 if (busy)
392 return -1;
393 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000394 i = pendinglast;
395 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000396 if (j == pendingfirst) {
397 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000398 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000399 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000400 pendingcalls[i].func = func;
401 pendingcalls[i].arg = arg;
402 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000403
404 _Py_Ticker = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000405 things_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000406 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000407 /* XXX End critical section */
408 return 0;
409}
410
Guido van Rossum180d7b41994-09-29 09:45:57 +0000411int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000412Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000413{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000414 static int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000415#ifdef WITH_THREAD
Guido van Rossum65d5b571998-12-21 19:32:43 +0000416 if (main_thread && PyThread_get_thread_ident() != main_thread)
Guido van Rossuma9672091994-09-14 13:31:22 +0000417 return 0;
418#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000419 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000420 return 0;
421 busy = 1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000422 things_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000423 for (;;) {
424 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000425 int (*func)(void *);
426 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000427 i = pendingfirst;
428 if (i == pendinglast)
429 break; /* Queue empty */
430 func = pendingcalls[i].func;
431 arg = pendingcalls[i].arg;
432 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000433 if (func(arg) < 0) {
434 busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000435 things_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000436 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000437 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000438 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000439 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000440 return 0;
441}
442
443
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000444/* The interpreter's recursion limit */
445
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000446#ifndef Py_DEFAULT_RECURSION_LIMIT
447#define Py_DEFAULT_RECURSION_LIMIT 1000
448#endif
449static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
450int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000451
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000452int
453Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000454{
455 return recursion_limit;
456}
457
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000458void
459Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000460{
461 recursion_limit = new_limit;
Thomas Woutersae406c62007-09-19 17:27:43 +0000462 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000463}
464
Armin Rigo2b3eb402003-10-28 12:05:48 +0000465/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
466 if the recursion_depth reaches _Py_CheckRecursionLimit.
467 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
468 to guarantee that _Py_CheckRecursiveCall() is regularly called.
469 Without USE_STACKCHECK, there is no need for this. */
470int
471_Py_CheckRecursiveCall(char *where)
472{
473 PyThreadState *tstate = PyThreadState_GET();
474
475#ifdef USE_STACKCHECK
476 if (PyOS_CheckStack()) {
477 --tstate->recursion_depth;
478 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
479 return -1;
480 }
481#endif
482 if (tstate->recursion_depth > recursion_limit) {
483 --tstate->recursion_depth;
484 PyErr_Format(PyExc_RuntimeError,
485 "maximum recursion depth exceeded%s",
486 where);
487 return -1;
488 }
Thomas Woutersae406c62007-09-19 17:27:43 +0000489 _Py_CheckRecursionLimit = recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000490 return 0;
491}
492
Guido van Rossum374a9221991-04-04 10:40:29 +0000493/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000494enum why_code {
495 WHY_NOT = 0x0001, /* No error */
496 WHY_EXCEPTION = 0x0002, /* Exception occurred */
497 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
498 WHY_RETURN = 0x0008, /* 'return' statement */
499 WHY_BREAK = 0x0010, /* 'break' statement */
500 WHY_CONTINUE = 0x0020, /* 'continue' statement */
501 WHY_YIELD = 0x0040 /* 'yield' operator */
502};
Guido van Rossum374a9221991-04-04 10:40:29 +0000503
Fredrik Lundh7a830892006-05-27 10:39:48 +0000504static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
505static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000506
Skip Montanarod581d772002-09-03 20:10:45 +0000507/* for manipulating the thread switch and periodic "stuff" - used to be
508 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000509int _Py_CheckInterval = 100;
510volatile int _Py_Ticker = 100;
Guido van Rossum374a9221991-04-04 10:40:29 +0000511
Guido van Rossumb209a111997-04-29 18:18:01 +0000512PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000513PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000514{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000515 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000516 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000517 (PyObject **)NULL, 0,
518 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000519 (PyObject **)NULL, 0,
520 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000521}
522
523
524/* Interpreter main loop */
525
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000526PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000527PyEval_EvalFrame(PyFrameObject *f) {
528 /* This is for backward compatibility with extension modules that
Thomas Wouterse2176022007-09-20 17:35:10 +0000529 used this API; core interpreter code should call
530 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000531 return PyEval_EvalFrameEx(f, 0);
532}
533
534PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000535PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000536{
Guido van Rossum950361c1997-01-24 13:49:28 +0000537#ifdef DXPAIRS
538 int lastopcode = 0;
539#endif
Thomas Wouterse2176022007-09-20 17:35:10 +0000540 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000541 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000542 register int opcode; /* Current opcode */
543 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000544 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000545 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000546 register PyObject *x; /* Result object -- NULL if error */
547 register PyObject *v; /* Temporary objects popped off stack */
548 register PyObject *w;
549 register PyObject *u;
550 register PyObject *t;
Barry Warsaw23c9ec82000-08-21 15:44:01 +0000551 register PyObject *stream = NULL; /* for PRINT opcodes */
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000552 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000553 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000554 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000555 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000556
Tim Peters8a5c3c72004-04-05 19:36:21 +0000557 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000558
559 not (instr_lb <= current_bytecode_offset < instr_ub)
560
Tim Peters8a5c3c72004-04-05 19:36:21 +0000561 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000562 initial values are such as to make this false the first
563 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000564 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000565
Guido van Rossumd076c731998-10-07 19:42:25 +0000566 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000567 PyObject *names;
568 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000569#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000570 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000571 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000572#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000573
Neal Norwitza81d2202002-07-14 00:27:26 +0000574/* Tuple access macros */
575
576#ifndef Py_DEBUG
577#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
578#else
579#define GETITEM(v, i) PyTuple_GetItem((v), (i))
580#endif
581
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000582#ifdef WITH_TSC
583/* Use Pentium timestamp counter to mark certain events:
584 inst0 -- beginning of switch statement for opcode dispatch
585 inst1 -- end of switch statement (may be skipped)
586 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000587 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000588 (may be skipped)
589 intr1 -- beginning of long interruption
590 intr2 -- end of long interruption
591
592 Many opcodes call out to helper C functions. In some cases, the
593 time in those functions should be counted towards the time for the
594 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
595 calls another Python function; there's no point in charge all the
596 bytecode executed by the called function to the caller.
597
598 It's hard to make a useful judgement statically. In the presence
599 of operator overloading, it's impossible to tell if a call will
600 execute new Python code or not.
601
602 It's a case-by-case judgement. I'll use intr1 for the following
603 cases:
604
605 EXEC_STMT
606 IMPORT_STAR
607 IMPORT_FROM
608 CALL_FUNCTION (and friends)
609
610 */
611 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
612 int ticked = 0;
613
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000614 READ_TIMESTAMP(inst0);
615 READ_TIMESTAMP(inst1);
616 READ_TIMESTAMP(loop0);
617 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000618
619 /* shut up the compiler */
620 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000621#endif
622
Guido van Rossum374a9221991-04-04 10:40:29 +0000623/* Code access macros */
624
Martin v. Löwis18e16552006-02-15 17:27:45 +0000625#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000626#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000627#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000628#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000629#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000630#define JUMPBY(x) (next_instr += (x))
631
Raymond Hettingerf606f872003-03-16 03:11:04 +0000632/* OpCode prediction macros
Thomas Wouterse2176022007-09-20 17:35:10 +0000633 Some opcodes tend to come in pairs thus making it possible to
634 predict the second code when the first is run. For example,
635 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
636 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000637
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000638 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000639 variable against a constant. If the pairing was good, then the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000640 processor's own internal branch predication has a high likelihood of
641 success, resulting in a nearly zero-overhead transition to the
642 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000643 including its two unpredictable branches, the HAS_ARG test and the
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000644 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000645 a successful PREDICT has the effect of making the two opcodes run as if
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000646 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000647
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000648 If collecting opcode statistics, your choices are to either keep the
649 predictions turned-on and interpret the results as if some opcodes
650 had been combined or turn-off predictions so that the opcode frequency
651 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000652*/
653
Raymond Hettingera7216982004-02-08 19:59:27 +0000654#ifdef DYNAMIC_EXECUTION_PROFILE
655#define PREDICT(op) if (0) goto PRED_##op
656#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000657#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000658#endif
659
Raymond Hettingerf606f872003-03-16 03:11:04 +0000660#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000661#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000662
Guido van Rossum374a9221991-04-04 10:40:29 +0000663/* Stack manipulation macros */
664
Martin v. Löwis18e16552006-02-15 17:27:45 +0000665/* The stack can grow at most MAXINT deep, as co_nlocals and
666 co_stacksize are ints. */
667#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000668#define EMPTY() (STACK_LEVEL() == 0)
669#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000670#define SECOND() (stack_pointer[-2])
671#define THIRD() (stack_pointer[-3])
672#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000673#define SET_TOP(v) (stack_pointer[-1] = (v))
674#define SET_SECOND(v) (stack_pointer[-2] = (v))
675#define SET_THIRD(v) (stack_pointer[-3] = (v))
676#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000677#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000678#define BASIC_PUSH(v) (*stack_pointer++ = (v))
679#define BASIC_POP() (*--stack_pointer)
680
Guido van Rossum96a42c81992-01-12 02:29:51 +0000681#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000682#define PUSH(v) { (void)(BASIC_PUSH(v), \
683 lltrace && prtrace(TOP(), "push")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000684 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouterse2176022007-09-20 17:35:10 +0000685#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
686 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000687#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
688 lltrace && prtrace(TOP(), "stackadj")); \
Richard Jonescebbefc2006-05-23 18:28:17 +0000689 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000690#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
691 prtrace((STACK_POINTER)[-1], "ext_pop")), \
692 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000693#else
694#define PUSH(v) BASIC_PUSH(v)
695#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000696#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000697#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000698#endif
699
Guido van Rossum681d79a1995-07-18 14:51:37 +0000700/* Local variable macros */
701
702#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000703
704/* The SETLOCAL() macro must not DECREF the local variable in-place and
705 then store the new value; it must copy the old value to a temporary
706 value, then store the new value, and then DECREF the temporary value.
707 This is because it is possible that during the DECREF the frame is
708 accessed by other code (e.g. a __del__ method or gc.collect()) and the
709 variable would be pointing to already-freed memory. */
710#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
711 GETLOCAL(i) = value; \
712 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000713
Guido van Rossuma027efa1997-05-05 20:56:21 +0000714/* Start of code */
715
Tim Peters5ca576e2001-06-18 22:08:13 +0000716 if (f == NULL)
717 return NULL;
718
Armin Rigo1d313ab2003-10-25 14:33:09 +0000719 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000720 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000721 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000722
Tim Peters5ca576e2001-06-18 22:08:13 +0000723 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000724
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000725 if (tstate->use_tracing) {
726 if (tstate->c_tracefunc != NULL) {
727 /* tstate->c_tracefunc, if defined, is a
728 function that will be called on *every* entry
729 to a code block. Its return value, if not
730 None, is a function that will be called at
731 the start of each executed line of code.
732 (Actually, the function must return itself
733 in order to continue tracing.) The trace
734 functions are called with three arguments:
735 a pointer to the current frame, a string
736 indicating why the function is called, and
737 an argument which depends on the situation.
738 The global trace function is also called
739 whenever an exception is detected. */
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000740 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000741 tstate->c_traceobj,
742 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000743 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000744 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000745 }
746 }
747 if (tstate->c_profilefunc != NULL) {
748 /* Similar for c_profilefunc, except it needn't
749 return itself and isn't called for "line" events */
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000750 if (call_trace_protected(tstate->c_profilefunc,
751 tstate->c_profileobj,
752 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000753 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000754 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000755 }
756 }
757 }
758
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000759 co = f->f_code;
760 names = co->co_names;
761 consts = co->co_consts;
762 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +0000763 freevars = f->f_localsplus + co->co_nlocals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000764 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000765 /* An explanation is in order for the next line.
766
767 f->f_lasti now refers to the index of the last instruction
768 executed. You might think this was obvious from the name, but
769 this wasn't always true before 2.3! PyFrame_New now sets
770 f->f_lasti to -1 (i.e. the index *before* the first instruction)
771 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000772 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000773
774 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +0000775 direct succession without updating f->f_lasti. A successful
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000776 prediction effectively links the two codes together as if they
777 were a single new opcode; accordingly,f->f_lasti will point to
778 the first code in the pair (for instance, GET_ITER followed by
779 FOR_ITER is effectively a single opcode and f->f_lasti will point
780 at to the beginning of the combined pair.)
781 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000782 next_instr = first_instr + f->f_lasti + 1;
783 stack_pointer = f->f_stacktop;
784 assert(stack_pointer != NULL);
785 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
786
Tim Peters5ca576e2001-06-18 22:08:13 +0000787#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000789#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000790#if defined(Py_DEBUG) || defined(LLTRACE)
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000791 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000792#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000793
Guido van Rossum374a9221991-04-04 10:40:29 +0000794 why = WHY_NOT;
795 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000796 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000797 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000798
Anthony Baxtera863d332006-04-11 07:43:46 +0000799 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000800 why = WHY_EXCEPTION;
801 goto on_error;
802 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000803
Guido van Rossum374a9221991-04-04 10:40:29 +0000804 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000805#ifdef WITH_TSC
806 if (inst1 == 0) {
807 /* Almost surely, the opcode executed a break
808 or a continue, preventing inst1 from being set
809 on the way out of the loop.
810 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000811 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000812 loop1 = inst1;
813 }
814 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
815 intr0, intr1);
816 ticked = 0;
817 inst1 = 0;
818 intr0 = 0;
819 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000820 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000821#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000822 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Richard Jonescebbefc2006-05-23 18:28:17 +0000823 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000824
Guido van Rossuma027efa1997-05-05 20:56:21 +0000825 /* Do periodic things. Doing this every time through
826 the loop would add too much overhead, so we do it
827 only every Nth instruction. We also do it if
828 ``things_to_do'' is set, i.e. when an asynchronous
829 event needs attention (e.g. a signal handler or
830 async I/O handler); see Py_AddPendingCall() and
831 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000832
Skip Montanarod581d772002-09-03 20:10:45 +0000833 if (--_Py_Ticker < 0) {
Thomas Woutersae406c62007-09-19 17:27:43 +0000834 if (*next_instr == SETUP_FINALLY) {
835 /* Make the last opcode before
836 a try: finally: block uninterruptable. */
837 goto fast_next_opcode;
838 }
Skip Montanarod581d772002-09-03 20:10:45 +0000839 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000840 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000841#ifdef WITH_TSC
842 ticked = 1;
843#endif
Guido van Rossuma027efa1997-05-05 20:56:21 +0000844 if (things_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +0000845 if (Py_MakePendingCalls() < 0) {
846 why = WHY_EXCEPTION;
847 goto on_error;
848 }
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +0000849 if (things_to_do)
850 /* MakePendingCalls() didn't succeed.
851 Force early re-execution of this
852 "periodic" code, possibly after
853 a thread switch */
854 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +0000855 }
Guido van Rossume59214e1994-08-30 08:01:59 +0000856#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000857 if (interpreter_lock) {
858 /* Give another thread a chance */
859
Guido van Rossum25ce5661997-08-02 03:10:38 +0000860 if (PyThreadState_Swap(NULL) != tstate)
861 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000862 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000863
864 /* Other threads may run now */
865
Guido van Rossum65d5b571998-12-21 19:32:43 +0000866 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000867 if (PyThreadState_Swap(tstate) != NULL)
868 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000869
870 /* Check for thread interrupts */
871
872 if (tstate->async_exc != NULL) {
873 x = tstate->async_exc;
874 tstate->async_exc = NULL;
875 PyErr_SetNone(x);
876 Py_DECREF(x);
877 why = WHY_EXCEPTION;
878 goto on_error;
879 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000880 }
881#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000882 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000883
Neil Schemenauer63543862002-02-17 19:10:14 +0000884 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +0000885 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +0000886
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000887 /* line-by-line tracing support */
888
889 if (tstate->c_tracefunc != NULL && !tstate->tracing) {
890 /* see maybe_call_line_trace
891 for expository comments */
892 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +0000893
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000894 err = maybe_call_line_trace(tstate->c_tracefunc,
895 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +0000896 f, &instr_lb, &instr_ub,
897 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000898 /* Reload possibly changed frame fields */
899 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +0000900 if (f->f_stacktop != NULL) {
901 stack_pointer = f->f_stacktop;
902 f->f_stacktop = NULL;
903 }
904 if (err) {
905 /* trace function raised an exception */
906 goto on_error;
907 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000908 }
909
910 /* Extract opcode and argument */
911
Guido van Rossum374a9221991-04-04 10:40:29 +0000912 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +0000913 oparg = 0; /* allows oparg to be stored in a register because
914 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000915 if (HAS_ARG(opcode))
916 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +0000917 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +0000918#ifdef DYNAMIC_EXECUTION_PROFILE
919#ifdef DXPAIRS
920 dxpairs[lastopcode][opcode]++;
921 lastopcode = opcode;
922#endif
923 dxp[opcode]++;
924#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000925
Guido van Rossum96a42c81992-01-12 02:29:51 +0000926#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +0000927 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +0000928
Guido van Rossum96a42c81992-01-12 02:29:51 +0000929 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +0000930 if (HAS_ARG(opcode)) {
931 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000932 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +0000933 }
934 else {
935 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000936 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +0000937 }
938 }
939#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000940
Guido van Rossum374a9221991-04-04 10:40:29 +0000941 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000942 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +0000943
Guido van Rossum374a9221991-04-04 10:40:29 +0000944 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +0000945
Guido van Rossum374a9221991-04-04 10:40:29 +0000946 /* BEWARE!
947 It is essential that any operation that fails sets either
948 x to NULL, err to nonzero, or why to anything but WHY_NOT,
949 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000950
Guido van Rossum374a9221991-04-04 10:40:29 +0000951 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +0000952
Raymond Hettinger9c18e812004-06-21 16:31:15 +0000953 case NOP:
954 goto fast_next_opcode;
955
Neil Schemenauer63543862002-02-17 19:10:14 +0000956 case LOAD_FAST:
957 x = GETLOCAL(oparg);
958 if (x != NULL) {
959 Py_INCREF(x);
960 PUSH(x);
961 goto fast_next_opcode;
962 }
963 format_exc_check_arg(PyExc_UnboundLocalError,
964 UNBOUNDLOCAL_ERROR_MSG,
965 PyTuple_GetItem(co->co_varnames, oparg));
966 break;
967
968 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +0000969 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +0000970 Py_INCREF(x);
971 PUSH(x);
972 goto fast_next_opcode;
973
Raymond Hettinger7dc52212003-03-16 20:14:44 +0000974 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +0000975 case STORE_FAST:
976 v = POP();
977 SETLOCAL(oparg, v);
978 goto fast_next_opcode;
979
Raymond Hettingerf606f872003-03-16 03:11:04 +0000980 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +0000981 case POP_TOP:
982 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +0000983 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +0000984 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000985
Guido van Rossum374a9221991-04-04 10:40:29 +0000986 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000987 v = TOP();
988 w = SECOND();
989 SET_TOP(w);
990 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +0000991 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +0000992
Guido van Rossum374a9221991-04-04 10:40:29 +0000993 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +0000994 v = TOP();
995 w = SECOND();
996 x = THIRD();
997 SET_TOP(w);
998 SET_SECOND(x);
999 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001000 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001001
Thomas Wouters434d0822000-08-24 20:11:32 +00001002 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001003 u = TOP();
1004 v = SECOND();
1005 w = THIRD();
1006 x = FOURTH();
1007 SET_TOP(v);
1008 SET_SECOND(w);
1009 SET_THIRD(x);
1010 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001011 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001012
Guido van Rossum374a9221991-04-04 10:40:29 +00001013 case DUP_TOP:
1014 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001015 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001016 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001017 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001018
Thomas Wouters434d0822000-08-24 20:11:32 +00001019 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001020 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001021 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001022 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001023 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001024 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001025 STACKADJ(2);
1026 SET_TOP(x);
1027 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001028 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001029 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001030 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001031 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001032 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001033 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001034 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001035 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001036 STACKADJ(3);
1037 SET_TOP(x);
1038 SET_SECOND(w);
1039 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001040 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001041 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001042 Py_FatalError("invalid argument to DUP_TOPX"
1043 " (bytecode corruption?)");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001044 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001045 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001046
Guido van Rossum374a9221991-04-04 10:40:29 +00001047 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001048 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001049 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001050 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001051 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001052 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001053 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001054
Guido van Rossum374a9221991-04-04 10:40:29 +00001055 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001056 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001057 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001058 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001059 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001060 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001061 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001062
Guido van Rossum374a9221991-04-04 10:40:29 +00001063 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001064 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001065 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001066 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001067 if (err == 0) {
1068 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001069 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001070 continue;
1071 }
1072 else if (err > 0) {
1073 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001074 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001075 err = 0;
1076 continue;
1077 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001078 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001079 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001080
Guido van Rossum374a9221991-04-04 10:40:29 +00001081 case UNARY_CONVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001082 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001083 x = PyObject_Repr(v);
1084 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001085 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001086 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001087 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001088
Guido van Rossum7928cd71991-10-24 14:59:31 +00001089 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001090 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001091 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001092 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001093 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001094 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001095 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001096
Guido van Rossum50564e81996-01-12 01:13:16 +00001097 case BINARY_POWER:
1098 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001099 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001100 x = PyNumber_Power(v, w, Py_None);
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 Rossum50564e81996-01-12 01:13:16 +00001105 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001106
Guido van Rossum374a9221991-04-04 10:40:29 +00001107 case BINARY_MULTIPLY:
1108 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001109 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001110 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001111 Py_DECREF(v);
1112 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001113 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001114 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001115 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001116
Guido van Rossum374a9221991-04-04 10:40:29 +00001117 case BINARY_DIVIDE:
Tim Peters3caca232001-12-06 06:23:26 +00001118 if (!_Py_QnewFlag) {
1119 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001120 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001121 x = PyNumber_Divide(v, w);
1122 Py_DECREF(v);
1123 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001124 SET_TOP(x);
Tim Peters3caca232001-12-06 06:23:26 +00001125 if (x != NULL) continue;
1126 break;
1127 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001128 /* -Qnew is in effect: fall through to
Tim Peters3caca232001-12-06 06:23:26 +00001129 BINARY_TRUE_DIVIDE */
1130 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001131 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001132 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001133 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001134 Py_DECREF(v);
1135 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001136 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001137 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001138 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001139
Guido van Rossum4668b002001-08-08 05:00:18 +00001140 case BINARY_FLOOR_DIVIDE:
1141 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001142 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001143 x = PyNumber_FloorDivide(v, w);
1144 Py_DECREF(v);
1145 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001146 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001147 if (x != NULL) continue;
1148 break;
1149
Guido van Rossum374a9221991-04-04 10:40:29 +00001150 case BINARY_MODULO:
1151 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001152 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001153 x = PyNumber_Remainder(v, w);
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_ADD:
1161 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001162 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001163 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001164 /* INLINE: int + int */
1165 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001166 a = PyInt_AS_LONG(v);
1167 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001168 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001169 if ((i^a) < 0 && (i^b) < 0)
1170 goto slow_add;
1171 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001172 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001173 else if (PyString_CheckExact(v) &&
1174 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001175 x = string_concatenate(v, w, f, next_instr);
1176 /* string_concatenate consumed the ref to v */
1177 goto skip_decref_vx;
1178 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001179 else {
1180 slow_add:
Guido van Rossumc12da691997-07-17 23:12:42 +00001181 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001182 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001184 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001185 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001186 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001187 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001188 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001189
Guido van Rossum374a9221991-04-04 10:40:29 +00001190 case BINARY_SUBTRACT:
1191 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001192 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001193 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001194 /* INLINE: int - int */
1195 register long a, b, i;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00001196 a = PyInt_AS_LONG(v);
1197 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001198 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001199 if ((i^a) < 0 && (i^~b) < 0)
1200 goto slow_sub;
1201 x = PyInt_FromLong(i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001202 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001203 else {
1204 slow_sub:
Guido van Rossumc12da691997-07-17 23:12:42 +00001205 x = PyNumber_Subtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001206 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001207 Py_DECREF(v);
1208 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001209 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001210 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001211 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001212
Guido van Rossum374a9221991-04-04 10:40:29 +00001213 case BINARY_SUBSCR:
1214 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001215 v = TOP();
Tim Petersb1c46982001-10-05 20:41:38 +00001216 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00001217 /* INLINE: list[int] */
Neal Norwitz814e9382006-03-02 07:54:28 +00001218 Py_ssize_t i = PyInt_AsSsize_t(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00001219 if (i < 0)
Guido van Rossumfa00e951998-07-08 15:02:37 +00001220 i += PyList_GET_SIZE(v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001221 if (i >= 0 && i < PyList_GET_SIZE(v)) {
Guido van Rossumfa00e951998-07-08 15:02:37 +00001222 x = PyList_GET_ITEM(v, i);
Guido van Rossumc12da691997-07-17 23:12:42 +00001223 Py_INCREF(x);
1224 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001225 else
1226 goto slow_get;
Guido van Rossumc12da691997-07-17 23:12:42 +00001227 }
1228 else
Raymond Hettinger467a6982004-04-07 11:39:21 +00001229 slow_get:
Guido van Rossumc12da691997-07-17 23:12:42 +00001230 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001231 Py_DECREF(v);
1232 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001233 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001234 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001235 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001236
Guido van Rossum7928cd71991-10-24 14:59:31 +00001237 case BINARY_LSHIFT:
1238 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001239 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001240 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001241 Py_DECREF(v);
1242 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001243 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001244 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001245 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001246
Guido van Rossum7928cd71991-10-24 14:59:31 +00001247 case BINARY_RSHIFT:
1248 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001249 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001250 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001251 Py_DECREF(v);
1252 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001253 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001254 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001255 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001256
Guido van Rossum7928cd71991-10-24 14:59:31 +00001257 case BINARY_AND:
1258 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001259 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001260 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001261 Py_DECREF(v);
1262 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001263 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001264 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001265 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001266
Guido van Rossum7928cd71991-10-24 14:59:31 +00001267 case BINARY_XOR:
1268 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001269 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001270 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001271 Py_DECREF(v);
1272 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001273 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001274 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001275 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001276
Guido van Rossum7928cd71991-10-24 14:59:31 +00001277 case BINARY_OR:
1278 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001279 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001280 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001281 Py_DECREF(v);
1282 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001283 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001284 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001285 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001286
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001287 case LIST_APPEND:
1288 w = POP();
1289 v = POP();
1290 err = PyList_Append(v, w);
1291 Py_DECREF(v);
1292 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001293 if (err == 0) {
1294 PREDICT(JUMP_ABSOLUTE);
1295 continue;
1296 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001297 break;
1298
Thomas Wouters434d0822000-08-24 20:11:32 +00001299 case INPLACE_POWER:
1300 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001301 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001302 x = PyNumber_InPlacePower(v, w, Py_None);
1303 Py_DECREF(v);
1304 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 if (x != NULL) continue;
1307 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001308
Thomas Wouters434d0822000-08-24 20:11:32 +00001309 case INPLACE_MULTIPLY:
1310 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001311 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001312 x = PyNumber_InPlaceMultiply(v, w);
1313 Py_DECREF(v);
1314 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001315 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001316 if (x != NULL) continue;
1317 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Thomas Wouters434d0822000-08-24 20:11:32 +00001319 case INPLACE_DIVIDE:
Tim Peters54b11912001-12-25 18:49:11 +00001320 if (!_Py_QnewFlag) {
1321 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001323 x = PyNumber_InPlaceDivide(v, w);
1324 Py_DECREF(v);
1325 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001326 SET_TOP(x);
Tim Peters54b11912001-12-25 18:49:11 +00001327 if (x != NULL) continue;
1328 break;
1329 }
Raymond Hettinger663004b2003-01-09 15:24:30 +00001330 /* -Qnew is in effect: fall through to
Tim Peters54b11912001-12-25 18:49:11 +00001331 INPLACE_TRUE_DIVIDE */
1332 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001333 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001334 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001335 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001336 Py_DECREF(v);
1337 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001339 if (x != NULL) continue;
1340 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001341
Guido van Rossum4668b002001-08-08 05:00:18 +00001342 case INPLACE_FLOOR_DIVIDE:
1343 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001345 x = PyNumber_InPlaceFloorDivide(v, w);
1346 Py_DECREF(v);
1347 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001349 if (x != NULL) continue;
1350 break;
1351
Thomas Wouters434d0822000-08-24 20:11:32 +00001352 case INPLACE_MODULO:
1353 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001355 x = PyNumber_InPlaceRemainder(v, w);
1356 Py_DECREF(v);
1357 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001358 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 if (x != NULL) continue;
1360 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001361
Thomas Wouters434d0822000-08-24 20:11:32 +00001362 case INPLACE_ADD:
1363 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001364 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001365 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001366 /* INLINE: int + int */
1367 register long a, b, i;
1368 a = PyInt_AS_LONG(v);
1369 b = PyInt_AS_LONG(w);
1370 i = a + b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001371 if ((i^a) < 0 && (i^b) < 0)
1372 goto slow_iadd;
1373 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001374 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001375 else if (PyString_CheckExact(v) &&
1376 PyString_CheckExact(w)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001377 x = string_concatenate(v, w, f, next_instr);
1378 /* string_concatenate consumed the ref to v */
1379 goto skip_decref_v;
1380 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001381 else {
1382 slow_iadd:
Thomas Wouters434d0822000-08-24 20:11:32 +00001383 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001384 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001385 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001386 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001387 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001388 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001389 if (x != NULL) continue;
1390 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001391
Thomas Wouters434d0822000-08-24 20:11:32 +00001392 case INPLACE_SUBTRACT:
1393 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 v = TOP();
Tim Petersc1e6d962001-10-05 20:21:03 +00001395 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001396 /* INLINE: int - int */
1397 register long a, b, i;
1398 a = PyInt_AS_LONG(v);
1399 b = PyInt_AS_LONG(w);
1400 i = a - b;
Guido van Rossum87780df2001-08-23 02:58:07 +00001401 if ((i^a) < 0 && (i^~b) < 0)
1402 goto slow_isub;
1403 x = PyInt_FromLong(i);
Thomas Wouters434d0822000-08-24 20:11:32 +00001404 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001405 else {
1406 slow_isub:
Thomas Wouters434d0822000-08-24 20:11:32 +00001407 x = PyNumber_InPlaceSubtract(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001408 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001409 Py_DECREF(v);
1410 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001411 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001412 if (x != NULL) continue;
1413 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001414
Thomas Wouters434d0822000-08-24 20:11:32 +00001415 case INPLACE_LSHIFT:
1416 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001417 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001418 x = PyNumber_InPlaceLshift(v, w);
1419 Py_DECREF(v);
1420 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001421 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001422 if (x != NULL) continue;
1423 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Thomas Wouters434d0822000-08-24 20:11:32 +00001425 case INPLACE_RSHIFT:
1426 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001427 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001428 x = PyNumber_InPlaceRshift(v, w);
1429 Py_DECREF(v);
1430 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001431 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001432 if (x != NULL) continue;
1433 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Thomas Wouters434d0822000-08-24 20:11:32 +00001435 case INPLACE_AND:
1436 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001437 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001438 x = PyNumber_InPlaceAnd(v, w);
1439 Py_DECREF(v);
1440 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001441 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001442 if (x != NULL) continue;
1443 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001444
Thomas Wouters434d0822000-08-24 20:11:32 +00001445 case INPLACE_XOR:
1446 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001447 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001448 x = PyNumber_InPlaceXor(v, w);
1449 Py_DECREF(v);
1450 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001451 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001452 if (x != NULL) continue;
1453 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001454
Thomas Wouters434d0822000-08-24 20:11:32 +00001455 case INPLACE_OR:
1456 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001457 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001458 x = PyNumber_InPlaceOr(v, w);
1459 Py_DECREF(v);
1460 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001461 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001462 if (x != NULL) continue;
1463 break;
1464
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 case SLICE+0:
1466 case SLICE+1:
1467 case SLICE+2:
1468 case SLICE+3:
1469 if ((opcode-SLICE) & 2)
1470 w = POP();
1471 else
1472 w = NULL;
1473 if ((opcode-SLICE) & 1)
1474 v = POP();
1475 else
1476 v = NULL;
Raymond Hettinger663004b2003-01-09 15:24:30 +00001477 u = TOP();
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 x = apply_slice(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001479 Py_DECREF(u);
1480 Py_XDECREF(v);
1481 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001482 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001483 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001484 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 case STORE_SLICE+0:
1487 case STORE_SLICE+1:
1488 case STORE_SLICE+2:
1489 case STORE_SLICE+3:
1490 if ((opcode-STORE_SLICE) & 2)
1491 w = POP();
1492 else
1493 w = NULL;
1494 if ((opcode-STORE_SLICE) & 1)
1495 v = POP();
1496 else
1497 v = NULL;
1498 u = POP();
1499 t = POP();
1500 err = assign_slice(u, v, w, t); /* u[v:w] = t */
Guido van Rossumb209a111997-04-29 18:18:01 +00001501 Py_DECREF(t);
1502 Py_DECREF(u);
1503 Py_XDECREF(v);
1504 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001505 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001506 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001507
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 case DELETE_SLICE+0:
1509 case DELETE_SLICE+1:
1510 case DELETE_SLICE+2:
1511 case DELETE_SLICE+3:
1512 if ((opcode-DELETE_SLICE) & 2)
1513 w = POP();
1514 else
1515 w = NULL;
1516 if ((opcode-DELETE_SLICE) & 1)
1517 v = POP();
1518 else
1519 v = NULL;
1520 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001521 err = assign_slice(u, v, w, (PyObject *)NULL);
Guido van Rossum374a9221991-04-04 10:40:29 +00001522 /* del u[v:w] */
Guido van Rossumb209a111997-04-29 18:18:01 +00001523 Py_DECREF(u);
1524 Py_XDECREF(v);
1525 Py_XDECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001526 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001527 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Guido van Rossum374a9221991-04-04 10:40:29 +00001529 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001530 w = TOP();
1531 v = SECOND();
1532 u = THIRD();
1533 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001534 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001535 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001536 Py_DECREF(u);
1537 Py_DECREF(v);
1538 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001539 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001540 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Guido van Rossum374a9221991-04-04 10:40:29 +00001542 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001543 w = TOP();
1544 v = SECOND();
1545 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001546 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001547 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001548 Py_DECREF(v);
1549 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001550 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001551 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001552
Guido van Rossum374a9221991-04-04 10:40:29 +00001553 case PRINT_EXPR:
1554 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001555 w = PySys_GetObject("displayhook");
1556 if (w == NULL) {
1557 PyErr_SetString(PyExc_RuntimeError,
1558 "lost sys.displayhook");
1559 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001560 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001561 }
1562 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001563 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001564 if (x == NULL)
1565 err = -1;
1566 }
1567 if (err == 0) {
1568 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001569 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001570 if (w == NULL)
1571 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001572 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001573 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001574 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001575 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001576
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001577 case PRINT_ITEM_TO:
1578 w = stream = POP();
1579 /* fall through to PRINT_ITEM */
1580
Guido van Rossum374a9221991-04-04 10:40:29 +00001581 case PRINT_ITEM:
1582 v = POP();
Barry Warsaw093abe02000-08-29 04:56:13 +00001583 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001584 w = PySys_GetObject("stdout");
1585 if (w == NULL) {
1586 PyErr_SetString(PyExc_RuntimeError,
1587 "lost sys.stdout");
1588 err = -1;
1589 }
Guido van Rossum8f183201997-12-31 05:53:15 +00001590 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001591 /* PyFile_SoftSpace() can exececute arbitrary code
1592 if sys.stdout is an instance with a __getattr__.
1593 If __getattr__ raises an exception, w will
1594 be freed, so we need to prevent that temporarily. */
1595 Py_XINCREF(w);
Tim Peters8e5fd532002-03-24 19:25:00 +00001596 if (w != NULL && PyFile_SoftSpace(w, 0))
Guido van Rossumbe270261997-05-22 22:26:18 +00001597 err = PyFile_WriteString(" ", w);
1598 if (err == 0)
1599 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001600 if (err == 0) {
Tim Peters8e5fd532002-03-24 19:25:00 +00001601 /* XXX move into writeobject() ? */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001602 if (PyString_Check(v)) {
1603 char *s = PyString_AS_STRING(v);
1604 Py_ssize_t len = PyString_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001605 if (len == 0 ||
1606 !isspace(Py_CHARMASK(s[len-1])) ||
1607 s[len-1] == ' ')
1608 PyFile_SoftSpace(w, 1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001609 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001610#ifdef Py_USING_UNICODE
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001611 else if (PyUnicode_Check(v)) {
1612 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
Martin v. Löwis66851282006-04-22 11:40:03 +00001613 Py_ssize_t len = PyUnicode_GET_SIZE(v);
Tim Peters8e5fd532002-03-24 19:25:00 +00001614 if (len == 0 ||
1615 !Py_UNICODE_ISSPACE(s[len-1]) ||
1616 s[len-1] == ' ')
1617 PyFile_SoftSpace(w, 1);
Marc-André Lemburg0c4d8d02001-11-20 15:17:25 +00001618 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001619#endif
Tim Peters8e5fd532002-03-24 19:25:00 +00001620 else
1621 PyFile_SoftSpace(w, 1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001622 }
Neal Norwitzc5131bc2003-06-29 14:48:32 +00001623 Py_XDECREF(w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001624 Py_DECREF(v);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001625 Py_XDECREF(stream);
1626 stream = NULL;
1627 if (err == 0)
1628 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001629 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001630
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001631 case PRINT_NEWLINE_TO:
1632 w = stream = POP();
1633 /* fall through to PRINT_NEWLINE */
1634
Guido van Rossum374a9221991-04-04 10:40:29 +00001635 case PRINT_NEWLINE:
Barry Warsaw093abe02000-08-29 04:56:13 +00001636 if (stream == NULL || stream == Py_None) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001637 w = PySys_GetObject("stdout");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001638 if (w == NULL) {
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001639 PyErr_SetString(PyExc_RuntimeError,
1640 "lost sys.stdout");
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001641 why = WHY_EXCEPTION;
1642 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001643 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001644 if (w != NULL) {
Georg Brandlaa76d772008-07-01 20:56:03 +00001645 /* w.write() may replace sys.stdout, so we
1646 * have to keep our reference to it */
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001647 Py_INCREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001648 err = PyFile_WriteString("\n", w);
1649 if (err == 0)
1650 PyFile_SoftSpace(w, 0);
Amaury Forgeot d'Arcbdd941f2008-07-01 20:38:04 +00001651 Py_DECREF(w);
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001652 }
1653 Py_XDECREF(stream);
1654 stream = NULL;
Guido van Rossum374a9221991-04-04 10:40:29 +00001655 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Thomas Wouters434d0822000-08-24 20:11:32 +00001657
1658#ifdef CASE_TOO_BIG
1659 default: switch (opcode) {
1660#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001661 case RAISE_VARARGS:
1662 u = v = w = NULL;
1663 switch (oparg) {
1664 case 3:
1665 u = POP(); /* traceback */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001666 /* Fallthrough */
1667 case 2:
1668 v = POP(); /* value */
1669 /* Fallthrough */
1670 case 1:
1671 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001672 case 0: /* Fallthrough */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001673 why = do_raise(w, v, u);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001674 break;
1675 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001676 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001677 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001678 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001679 break;
1680 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001681 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001682
Guido van Rossum374a9221991-04-04 10:40:29 +00001683 case LOAD_LOCALS:
Raymond Hettinger467a6982004-04-07 11:39:21 +00001684 if ((x = f->f_locals) != NULL) {
1685 Py_INCREF(x);
1686 PUSH(x);
1687 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001688 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001689 PyErr_SetString(PyExc_SystemError, "no locals");
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001691
Guido van Rossum374a9221991-04-04 10:40:29 +00001692 case RETURN_VALUE:
1693 retval = POP();
1694 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001695 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001696
Tim Peters5ca576e2001-06-18 22:08:13 +00001697 case YIELD_VALUE:
1698 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001699 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001700 why = WHY_YIELD;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001701 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001702
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001703 case EXEC_STMT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001704 w = TOP();
1705 v = SECOND();
1706 u = THIRD();
1707 STACKADJ(-3);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001708 READ_TIMESTAMP(intr0);
Guido van Rossuma027efa1997-05-05 20:56:21 +00001709 err = exec_statement(f, u, v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001710 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00001711 Py_DECREF(u);
1712 Py_DECREF(v);
1713 Py_DECREF(w);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001714 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001715
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 case POP_BLOCK:
1717 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001718 PyTryBlock *b = PyFrame_BlockPop(f);
Guido van Rossum374a9221991-04-04 10:40:29 +00001719 while (STACK_LEVEL() > b->b_level) {
1720 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001721 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 }
1723 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001724 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001725
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00001726 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001727 case END_FINALLY:
1728 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001729 if (PyInt_Check(v)) {
Raymond Hettinger7c958652004-04-06 10:11:10 +00001730 why = (enum why_code) PyInt_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001731 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001732 if (why == WHY_RETURN ||
1733 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001734 retval = POP();
1735 }
Thomas Wouterse2176022007-09-20 17:35:10 +00001736 else if (PyExceptionClass_Check(v) ||
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001737 PyString_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001738 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001739 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001740 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001741 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001742 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 else if (v != Py_None) {
1745 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001746 "'finally' pops bad exception");
1747 why = WHY_EXCEPTION;
1748 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001749 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001750 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Guido van Rossum374a9221991-04-04 10:40:29 +00001752 case BUILD_CLASS:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001753 u = TOP();
1754 v = SECOND();
1755 w = THIRD();
1756 STACKADJ(-2);
Guido van Rossum25831651993-05-19 14:50:45 +00001757 x = build_class(u, v, w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001758 SET_TOP(x);
Guido van Rossumb209a111997-04-29 18:18:01 +00001759 Py_DECREF(u);
1760 Py_DECREF(v);
1761 Py_DECREF(w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001762 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001765 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001767 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001768 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001769 err = PyDict_SetItem(x, w, v);
1770 else
1771 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001772 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001773 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001774 break;
1775 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001776 PyErr_Format(PyExc_SystemError,
1777 "no locals found when storing %s",
1778 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001779 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001780
Guido van Rossum374a9221991-04-04 10:40:29 +00001781 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001782 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001783 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001784 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001785 format_exc_check_arg(PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001786 NAME_ERROR_MSG,
1787 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001788 break;
1789 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001790 PyErr_Format(PyExc_SystemError,
1791 "no locals when deleting %s",
1792 PyObject_REPR(w));
Guido van Rossum374a9221991-04-04 10:40:29 +00001793 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001794
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001795 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001796 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001797 v = POP();
Thomas Wouterse2176022007-09-20 17:35:10 +00001798 if (PyTuple_CheckExact(v) &&
1799 PyTuple_GET_SIZE(v) == oparg) {
1800 PyObject **items = \
1801 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001802 while (oparg--) {
1803 w = items[oparg];
1804 Py_INCREF(w);
1805 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001806 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001807 Py_DECREF(v);
1808 continue;
Thomas Wouterse2176022007-09-20 17:35:10 +00001809 } else if (PyList_CheckExact(v) &&
1810 PyList_GET_SIZE(v) == oparg) {
1811 PyObject **items = \
1812 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001813 while (oparg--) {
1814 w = items[oparg];
1815 Py_INCREF(w);
1816 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001817 }
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001818 } else if (unpack_iterable(v, oparg,
Thomas Wouterse2176022007-09-20 17:35:10 +00001819 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001820 stack_pointer += oparg;
Georg Brandl5cb76c12007-03-21 09:00:39 +00001821 } else {
1822 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001823 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001824 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001825 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001826 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Guido van Rossum374a9221991-04-04 10:40:29 +00001828 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001829 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001830 v = TOP();
1831 u = SECOND();
1832 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001833 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1834 Py_DECREF(v);
1835 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001836 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001838
Guido van Rossum374a9221991-04-04 10:40:29 +00001839 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001840 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001841 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001842 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1843 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001844 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001845 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001846
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001847 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001848 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001849 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001850 err = PyDict_SetItem(f->f_globals, w, v);
1851 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001852 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001853 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001854
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001855 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001856 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001857 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001858 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001859 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001860 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001861
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001863 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001864 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001865 PyErr_Format(PyExc_SystemError,
1866 "no locals when loading %s",
Jeremy Hylton483638c2001-02-01 20:20:45 +00001867 PyObject_REPR(w));
Jeffrey Yasskin69614982008-12-11 05:21:18 +00001868 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001869 break;
1870 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001871 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001872 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001873 Py_XINCREF(x);
1874 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001875 else {
1876 x = PyObject_GetItem(v, w);
1877 if (x == NULL && PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00001878 if (!PyErr_ExceptionMatches(
1879 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001880 break;
1881 PyErr_Clear();
1882 }
1883 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001884 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001885 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001886 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001887 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001888 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001889 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001890 PyExc_NameError,
Thomas Wouterse2176022007-09-20 17:35:10 +00001891 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 break;
1893 }
1894 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001895 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001896 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001898 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001899
Guido van Rossum374a9221991-04-04 10:40:29 +00001900 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001901 w = GETITEM(names, oparg);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001902 if (PyString_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001903 /* Inline the PyDict_GetItem() calls.
1904 WARNING: this is an extreme speed hack.
1905 Do not try this at home. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001906 long hash = ((PyStringObject *)w)->ob_shash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001907 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001908 PyDictObject *d;
Armin Rigo35f6d362006-06-01 13:19:12 +00001909 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001910 d = (PyDictObject *)(f->f_globals);
Armin Rigo35f6d362006-06-01 13:19:12 +00001911 e = d->ma_lookup(d, w, hash);
1912 if (e == NULL) {
1913 x = NULL;
1914 break;
1915 }
1916 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001917 if (x != NULL) {
1918 Py_INCREF(x);
1919 PUSH(x);
1920 continue;
1921 }
1922 d = (PyDictObject *)(f->f_builtins);
Armin Rigo35f6d362006-06-01 13:19:12 +00001923 e = d->ma_lookup(d, w, hash);
1924 if (e == NULL) {
1925 x = NULL;
1926 break;
1927 }
1928 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001929 if (x != NULL) {
1930 Py_INCREF(x);
1931 PUSH(x);
1932 continue;
1933 }
1934 goto load_global_error;
1935 }
1936 }
1937 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001938 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001939 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001940 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001941 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001942 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001943 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001944 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001945 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001946 break;
1947 }
1948 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001949 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001950 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001951 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001952
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001953 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001954 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001955 if (x != NULL) {
1956 SETLOCAL(oparg, NULL);
1957 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001958 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001959 format_exc_check_arg(
1960 PyExc_UnboundLocalError,
1961 UNBOUNDLOCAL_ERROR_MSG,
1962 PyTuple_GetItem(co->co_varnames, oparg)
1963 );
1964 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001965
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001966 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001967 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001968 Py_INCREF(x);
1969 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001970 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001971 break;
1972
1973 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001974 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001975 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001976 if (w != NULL) {
1977 PUSH(w);
1978 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001979 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001980 err = -1;
1981 /* Don't stomp existing exception */
1982 if (PyErr_Occurred())
1983 break;
Richard Jonescebbefc2006-05-23 18:28:17 +00001984 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1985 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001986 oparg);
1987 format_exc_check_arg(
1988 PyExc_UnboundLocalError,
1989 UNBOUNDLOCAL_ERROR_MSG,
1990 v);
1991 } else {
Thomas Wouterse2176022007-09-20 17:35:10 +00001992 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
1993 PyTuple_GET_SIZE(co->co_cellvars));
1994 format_exc_check_arg(PyExc_NameError,
1995 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001996 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001997 break;
1998
1999 case STORE_DEREF:
2000 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002001 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002002 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002003 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002004 continue;
2005
Guido van Rossum374a9221991-04-04 10:40:29 +00002006 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00002007 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002008 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002009 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002010 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002011 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002012 }
2013 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002014 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002015 }
2016 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002017
Guido van Rossum374a9221991-04-04 10:40:29 +00002018 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00002019 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002020 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002021 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002022 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002023 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002024 }
2025 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002026 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002027 }
2028 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002029
Guido van Rossum374a9221991-04-04 10:40:29 +00002030 case BUILD_MAP:
Raymond Hettingerfd7ed402007-12-18 21:24:09 +00002031 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002032 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002033 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002034 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002035
Raymond Hettingereffde122007-12-18 18:26:18 +00002036 case STORE_MAP:
2037 w = TOP(); /* key */
2038 u = SECOND(); /* value */
2039 v = THIRD(); /* dict */
2040 STACKADJ(-2);
2041 assert (PyDict_CheckExact(v));
2042 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2043 Py_DECREF(u);
2044 Py_DECREF(w);
2045 if (err == 0) continue;
2046 break;
2047
Guido van Rossum374a9221991-04-04 10:40:29 +00002048 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002049 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002050 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002051 x = PyObject_GetAttr(v, w);
2052 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002053 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002054 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002055 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002056
Guido van Rossum374a9221991-04-04 10:40:29 +00002057 case COMPARE_OP:
2058 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002059 v = TOP();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00002060 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
Guido van Rossumc12da691997-07-17 23:12:42 +00002061 /* INLINE: cmp(int, int) */
2062 register long a, b;
2063 register int res;
Guido van Rossumcf183ac1998-12-04 18:51:36 +00002064 a = PyInt_AS_LONG(v);
2065 b = PyInt_AS_LONG(w);
Guido van Rossumc12da691997-07-17 23:12:42 +00002066 switch (oparg) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002067 case PyCmp_LT: res = a < b; break;
2068 case PyCmp_LE: res = a <= b; break;
2069 case PyCmp_EQ: res = a == b; break;
2070 case PyCmp_NE: res = a != b; break;
2071 case PyCmp_GT: res = a > b; break;
2072 case PyCmp_GE: res = a >= b; break;
2073 case PyCmp_IS: res = v == w; break;
2074 case PyCmp_IS_NOT: res = v != w; break;
Guido van Rossumc12da691997-07-17 23:12:42 +00002075 default: goto slow_compare;
2076 }
2077 x = res ? Py_True : Py_False;
2078 Py_INCREF(x);
2079 }
2080 else {
2081 slow_compare:
2082 x = cmp_outcome(oparg, v, w);
2083 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002084 Py_DECREF(v);
2085 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002086 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002087 if (x == NULL) break;
2088 PREDICT(JUMP_IF_FALSE);
2089 PREDICT(JUMP_IF_TRUE);
2090 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002091
Guido van Rossum374a9221991-04-04 10:40:29 +00002092 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002093 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002094 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002095 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002096 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002097 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002098 break;
2099 }
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002100 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002101 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002102 u = TOP();
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002103 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2104 w = PyTuple_Pack(5,
2105 w,
2106 f->f_globals,
2107 f->f_locals == NULL ?
2108 Py_None : f->f_locals,
2109 v,
2110 u);
2111 else
2112 w = PyTuple_Pack(4,
2113 w,
2114 f->f_globals,
2115 f->f_locals == NULL ?
2116 Py_None : f->f_locals,
2117 v);
2118 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002119 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002120 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002121 u = POP();
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002122 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002123 x = NULL;
2124 break;
2125 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002126 READ_TIMESTAMP(intr0);
Guido van Rossum1d9a9ea2008-01-23 20:19:01 +00002127 v = x;
2128 x = PyEval_CallObject(v, w);
2129 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002130 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002131 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002132 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002133 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002134 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002135
Thomas Wouters52152252000-08-17 22:55:00 +00002136 case IMPORT_STAR:
2137 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002138 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002139 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002140 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002141 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002142 break;
2143 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002144 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002145 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002146 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002147 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002148 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002149 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002150 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002151
Thomas Wouters52152252000-08-17 22:55:00 +00002152 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002153 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002154 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002155 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002156 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002157 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002158 PUSH(x);
2159 if (x != NULL) continue;
2160 break;
2161
Guido van Rossum374a9221991-04-04 10:40:29 +00002162 case JUMP_FORWARD:
2163 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002164 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002165
Raymond Hettingerf606f872003-03-16 03:11:04 +00002166 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002167 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002168 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002169 if (w == Py_True) {
2170 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002171 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002172 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002173 if (w == Py_False) {
2174 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002175 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002176 }
2177 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002178 if (err > 0)
2179 err = 0;
2180 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002181 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002182 else
2183 break;
2184 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002185
Raymond Hettingerf606f872003-03-16 03:11:04 +00002186 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002187 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002188 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002189 if (w == Py_False) {
2190 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002191 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002192 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002193 if (w == Py_True) {
2194 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002195 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002196 }
2197 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002198 if (err > 0) {
2199 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002200 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002201 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002202 else if (err == 0)
2203 ;
2204 else
2205 break;
2206 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002207
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002208 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002209 case JUMP_ABSOLUTE:
2210 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002211#if FAST_LOOPS
2212 /* Enabling this path speeds-up all while and for-loops by bypassing
2213 the per-loop checks for signals. By default, this should be turned-off
2214 because it prevents detection of a control-break in tight loops like
2215 "while 1: pass". Compile with this option turned-on when you need
2216 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002217 that contain only instructions ending with goto fast_next_opcode).
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002218 */
2219 goto fast_next_opcode;
2220#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002221 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002222#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002223
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002224 case GET_ITER:
2225 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002226 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002227 x = PyObject_GetIter(v);
2228 Py_DECREF(v);
2229 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002230 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002231 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002232 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002233 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002234 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002235 break;
2236
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002237 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002238 case FOR_ITER:
2239 /* before: [iter]; after: [iter, iter()] *or* [] */
2240 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002241 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002242 if (x != NULL) {
2243 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002244 PREDICT(STORE_FAST);
2245 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002246 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002247 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002248 if (PyErr_Occurred()) {
Thomas Wouterse2176022007-09-20 17:35:10 +00002249 if (!PyErr_ExceptionMatches(
2250 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002251 break;
2252 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002253 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002254 /* iterator ended normally */
2255 x = v = POP();
2256 Py_DECREF(v);
2257 JUMPBY(oparg);
2258 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002259
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002260 case BREAK_LOOP:
2261 why = WHY_BREAK;
2262 goto fast_block_end;
2263
2264 case CONTINUE_LOOP:
2265 retval = PyInt_FromLong(oparg);
Neal Norwitz02104df2006-05-19 06:31:23 +00002266 if (!retval) {
2267 x = NULL;
2268 break;
2269 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002270 why = WHY_CONTINUE;
2271 goto fast_block_end;
2272
Guido van Rossum374a9221991-04-04 10:40:29 +00002273 case SETUP_LOOP:
2274 case SETUP_EXCEPT:
2275 case SETUP_FINALLY:
Thomas Wouterse2176022007-09-20 17:35:10 +00002276 /* NOTE: If you add any new block-setup opcodes that
2277 are not try/except/finally handlers, you may need
2278 to update the PyGen_NeedsFinalizing() function.
2279 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002280
Guido van Rossumb209a111997-04-29 18:18:01 +00002281 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002282 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002283 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002284
Guido van Rossumc2e20742006-02-27 22:32:47 +00002285 case WITH_CLEANUP:
2286 {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002287 /* At the top of the stack are 1-3 values indicating
2288 how/why we entered the finally clause:
2289 - TOP = None
2290 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2291 - TOP = WHY_*; no retval below it
2292 - (TOP, SECOND, THIRD) = exc_info()
2293 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002294 In the last case, we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002295 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002296 otherwise we must call
Nick Coghlan7af53be2008-03-07 14:13:28 +00002297 EXIT(None, None, None)
2298
2299 In all cases, we remove EXIT from the stack, leaving
2300 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002301
2302 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002303 *and* the function call returns a 'true' value, we
2304 "zap" this information, to prevent END_FINALLY from
2305 re-raising the exception. (But non-local gotos
2306 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002307 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002308
Nick Coghlan7af53be2008-03-07 14:13:28 +00002309 PyObject *exit_func;
2310
2311 u = POP();
2312 if (u == Py_None) {
2313 exit_func = TOP();
2314 SET_TOP(u);
2315 v = w = Py_None;
2316 }
2317 else if (PyInt_Check(u)) {
2318 switch(PyInt_AS_LONG(u)) {
2319 case WHY_RETURN:
2320 case WHY_CONTINUE:
2321 /* Retval in TOP. */
2322 exit_func = SECOND();
2323 SET_SECOND(TOP());
2324 SET_TOP(u);
2325 break;
2326 default:
2327 exit_func = TOP();
2328 SET_TOP(u);
2329 break;
2330 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002331 u = v = w = Py_None;
2332 }
2333 else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002334 v = TOP();
2335 w = SECOND();
2336 exit_func = THIRD();
2337 SET_TOP(u);
2338 SET_SECOND(v);
2339 SET_THIRD(w);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002340 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002341 /* XXX Not the fastest way to call it... */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002342 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2343 NULL);
Amaury Forgeot d'Arc79c9f762008-12-10 23:56:33 +00002344 Py_DECREF(exit_func);
2345 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002346 break; /* Go to error exit */
Amaury Forgeot d'Arc79c9f762008-12-10 23:56:33 +00002347
2348 if (u != Py_None)
2349 err = PyObject_IsTrue(x);
2350 else
2351 err = 0;
2352 Py_DECREF(x);
2353
2354 if (err < 0)
2355 break; /* Go to error exit */
2356 else if (err > 0) {
2357 err = 0;
Guido van Rossumf6694362006-03-10 02:28:35 +00002358 /* There was an exception and a true return */
Nick Coghlan7af53be2008-03-07 14:13:28 +00002359 STACKADJ(-2);
Guido van Rossumf6694362006-03-10 02:28:35 +00002360 Py_INCREF(Py_None);
2361 SET_TOP(Py_None);
Guido van Rossumf6694362006-03-10 02:28:35 +00002362 Py_DECREF(u);
2363 Py_DECREF(v);
2364 Py_DECREF(w);
2365 } else {
Nick Coghlan7af53be2008-03-07 14:13:28 +00002366 /* The stack was rearranged to remove EXIT
2367 above. Let END_FINALLY do its thing */
Guido van Rossumf6694362006-03-10 02:28:35 +00002368 }
Jeffrey Yasskin9063a992008-03-03 01:27:03 +00002369 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002370 break;
2371 }
2372
Guido van Rossumf10570b1995-07-07 22:53:21 +00002373 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002374 {
2375 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002376 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002377 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002378#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002379 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002380#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002381 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002382#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002383 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002384 PUSH(x);
2385 if (x != NULL)
2386 continue;
2387 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002388 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002389
Jeremy Hylton76901512000-03-28 23:49:17 +00002390 case CALL_FUNCTION_VAR:
2391 case CALL_FUNCTION_KW:
2392 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002393 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002394 int na = oparg & 0xff;
2395 int nk = (oparg>>8) & 0xff;
2396 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002397 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002398 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002399 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002400 if (flags & CALL_FLAG_VAR)
2401 n++;
2402 if (flags & CALL_FLAG_KW)
2403 n++;
2404 pfunc = stack_pointer - n - 1;
2405 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002406
Guido van Rossumac7be682001-01-17 15:42:30 +00002407 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002408 && PyMethod_GET_SELF(func) != NULL) {
2409 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002410 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002411 func = PyMethod_GET_FUNCTION(func);
2412 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002413 Py_DECREF(*pfunc);
2414 *pfunc = self;
2415 na++;
2416 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002417 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002418 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002419 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002420 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002421 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002422 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002423 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002424 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002425
Jeremy Hylton76901512000-03-28 23:49:17 +00002426 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002427 w = POP();
2428 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002429 }
2430 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002431 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002432 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002433 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002434 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002435
Guido van Rossum681d79a1995-07-18 14:51:37 +00002436 case MAKE_FUNCTION:
2437 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002438 x = PyFunction_New(v, f->f_globals);
2439 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002440 /* XXX Maybe this should be a separate opcode? */
2441 if (x != NULL && oparg > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002442 v = PyTuple_New(oparg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002443 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002444 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002445 x = NULL;
2446 break;
2447 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002448 while (--oparg >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002449 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002450 PyTuple_SET_ITEM(v, oparg, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002451 }
2452 err = PyFunction_SetDefaults(x, v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002453 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002454 }
2455 PUSH(x);
2456 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002457
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002458 case MAKE_CLOSURE:
2459 {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002460 v = POP(); /* code object */
2461 x = PyFunction_New(v, f->f_globals);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002462 Py_DECREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463 if (x != NULL) {
2464 v = POP();
Jeffrey Yasskin69614982008-12-11 05:21:18 +00002465 if (PyFunction_SetClosure(x, v) != 0) {
2466 /* Can't happen unless bytecode is corrupt. */
2467 why = WHY_EXCEPTION;
2468 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002469 Py_DECREF(v);
2470 }
2471 if (x != NULL && oparg > 0) {
2472 v = PyTuple_New(oparg);
2473 if (v == NULL) {
2474 Py_DECREF(x);
2475 x = NULL;
2476 break;
2477 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002478 while (--oparg >= 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002479 w = POP();
2480 PyTuple_SET_ITEM(v, oparg, w);
2481 }
Jeffrey Yasskin69614982008-12-11 05:21:18 +00002482 if (PyFunction_SetDefaults(x, v) != 0) {
2483 /* Can't happen unless
2484 PyFunction_SetDefaults changes. */
2485 why = WHY_EXCEPTION;
2486 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002487 Py_DECREF(v);
2488 }
2489 PUSH(x);
2490 break;
2491 }
2492
Guido van Rossum8861b741996-07-30 16:49:37 +00002493 case BUILD_SLICE:
2494 if (oparg == 3)
2495 w = POP();
2496 else
2497 w = NULL;
2498 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002499 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002500 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002501 Py_DECREF(u);
2502 Py_DECREF(v);
2503 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002504 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002505 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002506 break;
2507
Fred Drakeef8ace32000-08-24 00:32:09 +00002508 case EXTENDED_ARG:
2509 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002510 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002511 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002512
Guido van Rossum374a9221991-04-04 10:40:29 +00002513 default:
2514 fprintf(stderr,
2515 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002516 PyCode_Addr2Line(f->f_code, f->f_lasti),
2517 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002518 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002519 why = WHY_EXCEPTION;
2520 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002521
2522#ifdef CASE_TOO_BIG
2523 }
2524#endif
2525
Guido van Rossum374a9221991-04-04 10:40:29 +00002526 } /* switch */
2527
2528 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002529
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002530 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002531
Guido van Rossum374a9221991-04-04 10:40:29 +00002532 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002533
Guido van Rossum374a9221991-04-04 10:40:29 +00002534 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002535 if (err == 0 && x != NULL) {
2536#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002537 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002538 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002539 fprintf(stderr,
2540 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002541 else {
2542#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002543 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002544 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002545#ifdef CHECKEXC
2546 }
2547#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002548 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002549 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002550 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002551 err = 0;
2552 }
2553
Guido van Rossum374a9221991-04-04 10:40:29 +00002554 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002555
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002556 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002557 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002558 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002559 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002560 why = WHY_EXCEPTION;
2561 }
2562 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002563#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002564 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002565 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002566 if (PyErr_Occurred()) {
Neal Norwitz8250fbe2008-01-27 17:12:15 +00002567 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002568 sprintf(buf, "Stack unwind with exception "
2569 "set and why=%d", why);
2570 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002571 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002572 }
2573#endif
2574
2575 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002576
Guido van Rossum374a9221991-04-04 10:40:29 +00002577 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002578 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002579
Fred Drake8f51f542001-10-04 14:48:42 +00002580 if (tstate->c_tracefunc != NULL)
2581 call_exc_trace(tstate->c_tracefunc,
2582 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002583 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002584
Guido van Rossum374a9221991-04-04 10:40:29 +00002585 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002586
Guido van Rossum374a9221991-04-04 10:40:29 +00002587 if (why == WHY_RERAISE)
2588 why = WHY_EXCEPTION;
2589
2590 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002591
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002592fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002593 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002594 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002595
Tim Peters8a5c3c72004-04-05 19:36:21 +00002596 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002597 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2598 /* For a continue inside a try block,
2599 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002600 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2601 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002602 why = WHY_NOT;
2603 JUMPTO(PyInt_AS_LONG(retval));
2604 Py_DECREF(retval);
2605 break;
2606 }
2607
Guido van Rossum374a9221991-04-04 10:40:29 +00002608 while (STACK_LEVEL() > b->b_level) {
2609 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002610 Py_XDECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002611 }
2612 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2613 why = WHY_NOT;
2614 JUMPTO(b->b_handler);
2615 break;
2616 }
2617 if (b->b_type == SETUP_FINALLY ||
Guido van Rossum150b2df1996-12-05 23:17:11 +00002618 (b->b_type == SETUP_EXCEPT &&
2619 why == WHY_EXCEPTION)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002620 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002621 PyObject *exc, *val, *tb;
2622 PyErr_Fetch(&exc, &val, &tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002623 if (val == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002624 val = Py_None;
2625 Py_INCREF(val);
Guido van Rossum374a9221991-04-04 10:40:29 +00002626 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002627 /* Make the raw exception data
2628 available to the handler,
2629 so a program can emulate the
2630 Python main loop. Don't do
2631 this for 'finally'. */
2632 if (b->b_type == SETUP_EXCEPT) {
Barry Warsaweaedc7c1997-08-28 22:36:40 +00002633 PyErr_NormalizeException(
2634 &exc, &val, &tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00002635 set_exc_info(tstate,
2636 exc, val, tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002637 }
Jeremy Hyltonc6314892001-09-26 19:24:45 +00002638 if (tb == NULL) {
2639 Py_INCREF(Py_None);
2640 PUSH(Py_None);
2641 } else
2642 PUSH(tb);
Guido van Rossum374a9221991-04-04 10:40:29 +00002643 PUSH(val);
2644 PUSH(exc);
2645 }
2646 else {
Raymond Hettinger06032cb2004-04-06 09:37:35 +00002647 if (why & (WHY_RETURN | WHY_CONTINUE))
Guido van Rossum374a9221991-04-04 10:40:29 +00002648 PUSH(retval);
Guido van Rossumb209a111997-04-29 18:18:01 +00002649 v = PyInt_FromLong((long)why);
Guido van Rossum374a9221991-04-04 10:40:29 +00002650 PUSH(v);
2651 }
2652 why = WHY_NOT;
2653 JUMPTO(b->b_handler);
2654 break;
2655 }
2656 } /* unwind stack */
2657
2658 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002659
Guido van Rossum374a9221991-04-04 10:40:29 +00002660 if (why != WHY_NOT)
2661 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002662 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002663
Guido van Rossum374a9221991-04-04 10:40:29 +00002664 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002665
Tim Peters8a5c3c72004-04-05 19:36:21 +00002666 assert(why != WHY_YIELD);
2667 /* Pop remaining stack entries. */
2668 while (!EMPTY()) {
2669 v = POP();
2670 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002671 }
2672
Tim Peters8a5c3c72004-04-05 19:36:21 +00002673 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002674 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002675
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002676fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002677 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002678 if (tstate->c_tracefunc) {
2679 if (why == WHY_RETURN || why == WHY_YIELD) {
2680 if (call_trace(tstate->c_tracefunc,
2681 tstate->c_traceobj, f,
2682 PyTrace_RETURN, retval)) {
2683 Py_XDECREF(retval);
2684 retval = NULL;
2685 why = WHY_EXCEPTION;
2686 }
2687 }
2688 else if (why == WHY_EXCEPTION) {
2689 call_trace_protected(tstate->c_tracefunc,
2690 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002691 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002692 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002693 }
Fred Drake8f51f542001-10-04 14:48:42 +00002694 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002695 if (why == WHY_EXCEPTION)
2696 call_trace_protected(tstate->c_profilefunc,
2697 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002698 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002699 else if (call_trace(tstate->c_profilefunc,
2700 tstate->c_profileobj, f,
2701 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002702 Py_XDECREF(retval);
2703 retval = NULL;
2704 why = WHY_EXCEPTION;
2705 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002706 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002707 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002708
Tim Peters7df5e7f2006-05-26 23:14:37 +00002709 if (tstate->frame->f_exc_type != NULL)
2710 reset_exc_info(tstate);
2711 else {
2712 assert(tstate->frame->f_exc_value == NULL);
2713 assert(tstate->frame->f_exc_traceback == NULL);
2714 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00002715
Tim Peters5ca576e2001-06-18 22:08:13 +00002716 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00002717exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002718 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002719 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002720
Guido van Rossum96a42c81992-01-12 02:29:51 +00002721 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002722}
2723
Guido van Rossumc2e20742006-02-27 22:32:47 +00002724/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002725 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002726 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002727
Tim Peters6d6c1a32001-08-02 04:15:00 +00002728PyObject *
2729PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002730 PyObject **args, int argcount, PyObject **kws, int kwcount,
2731 PyObject **defs, int defcount, PyObject *closure)
2732{
2733 register PyFrameObject *f;
2734 register PyObject *retval = NULL;
2735 register PyObject **fastlocals, **freevars;
2736 PyThreadState *tstate = PyThreadState_GET();
2737 PyObject *x, *u;
2738
2739 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002740 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002741 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002742 return NULL;
2743 }
2744
Neal Norwitzdf6a6492006-08-13 18:10:10 +00002745 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002746 assert(globals != NULL);
2747 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002748 if (f == NULL)
2749 return NULL;
2750
2751 fastlocals = f->f_localsplus;
Richard Jonescebbefc2006-05-23 18:28:17 +00002752 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002753
2754 if (co->co_argcount > 0 ||
2755 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2756 int i;
2757 int n = argcount;
2758 PyObject *kwdict = NULL;
2759 if (co->co_flags & CO_VARKEYWORDS) {
2760 kwdict = PyDict_New();
2761 if (kwdict == NULL)
2762 goto fail;
2763 i = co->co_argcount;
2764 if (co->co_flags & CO_VARARGS)
2765 i++;
2766 SETLOCAL(i, kwdict);
2767 }
2768 if (argcount > co->co_argcount) {
2769 if (!(co->co_flags & CO_VARARGS)) {
2770 PyErr_Format(PyExc_TypeError,
2771 "%.200s() takes %s %d "
2772 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002773 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002774 defcount ? "at most" : "exactly",
2775 co->co_argcount,
2776 kwcount ? "non-keyword " : "",
2777 co->co_argcount == 1 ? "" : "s",
2778 argcount);
2779 goto fail;
2780 }
2781 n = co->co_argcount;
2782 }
2783 for (i = 0; i < n; i++) {
2784 x = args[i];
2785 Py_INCREF(x);
2786 SETLOCAL(i, x);
2787 }
2788 if (co->co_flags & CO_VARARGS) {
2789 u = PyTuple_New(argcount - n);
2790 if (u == NULL)
2791 goto fail;
2792 SETLOCAL(co->co_argcount, u);
2793 for (i = n; i < argcount; i++) {
2794 x = args[i];
2795 Py_INCREF(x);
2796 PyTuple_SET_ITEM(u, i-n, x);
2797 }
2798 }
2799 for (i = 0; i < kwcount; i++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002800 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002801 PyObject *keyword = kws[2*i];
2802 PyObject *value = kws[2*i + 1];
2803 int j;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002804 if (keyword == NULL || !PyString_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002805 PyErr_Format(PyExc_TypeError,
2806 "%.200s() keywords must be strings",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002807 PyString_AsString(co->co_name));
Tim Peters5ca576e2001-06-18 22:08:13 +00002808 goto fail;
2809 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002810 /* Speed hack: do raw pointer compares. As names are
2811 normally interned this should almost always hit. */
2812 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Tim Peters5ca576e2001-06-18 22:08:13 +00002813 for (j = 0; j < co->co_argcount; j++) {
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002814 PyObject *nm = co_varnames[j];
2815 if (nm == keyword)
2816 goto kw_found;
2817 }
2818 /* Slow fallback, just in case */
2819 for (j = 0; j < co->co_argcount; j++) {
2820 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00002821 int cmp = PyObject_RichCompareBool(
2822 keyword, nm, Py_EQ);
2823 if (cmp > 0)
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002824 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002825 else if (cmp < 0)
2826 goto fail;
2827 }
2828 /* Check errors from Compare */
2829 if (PyErr_Occurred())
2830 goto fail;
2831 if (j >= co->co_argcount) {
2832 if (kwdict == NULL) {
2833 PyErr_Format(PyExc_TypeError,
2834 "%.200s() got an unexpected "
2835 "keyword argument '%.400s'",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002836 PyString_AsString(co->co_name),
2837 PyString_AsString(keyword));
Tim Peters5ca576e2001-06-18 22:08:13 +00002838 goto fail;
2839 }
2840 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002841 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00002842 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002843kw_found:
2844 if (GETLOCAL(j) != NULL) {
2845 PyErr_Format(PyExc_TypeError,
2846 "%.200s() got multiple "
2847 "values for keyword "
2848 "argument '%.400s'",
2849 PyString_AsString(co->co_name),
2850 PyString_AsString(keyword));
2851 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00002852 }
Antoine Pitrouc2cc80c2008-07-25 22:13:52 +00002853 Py_INCREF(value);
2854 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00002855 }
2856 if (argcount < co->co_argcount) {
2857 int m = co->co_argcount - defcount;
2858 for (i = argcount; i < m; i++) {
2859 if (GETLOCAL(i) == NULL) {
2860 PyErr_Format(PyExc_TypeError,
2861 "%.200s() takes %s %d "
2862 "%sargument%s (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002863 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002864 ((co->co_flags & CO_VARARGS) ||
2865 defcount) ? "at least"
2866 : "exactly",
2867 m, kwcount ? "non-keyword " : "",
2868 m == 1 ? "" : "s", i);
2869 goto fail;
2870 }
2871 }
2872 if (n > m)
2873 i = n - m;
2874 else
2875 i = 0;
2876 for (; i < defcount; i++) {
2877 if (GETLOCAL(m+i) == NULL) {
2878 PyObject *def = defs[i];
2879 Py_INCREF(def);
2880 SETLOCAL(m+i, def);
2881 }
2882 }
2883 }
2884 }
2885 else {
2886 if (argcount > 0 || kwcount > 0) {
2887 PyErr_Format(PyExc_TypeError,
2888 "%.200s() takes no arguments (%d given)",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002889 PyString_AsString(co->co_name),
Tim Peters5ca576e2001-06-18 22:08:13 +00002890 argcount + kwcount);
2891 goto fail;
2892 }
2893 }
2894 /* Allocate and initialize storage for cell vars, and copy free
2895 vars into frame. This isn't too efficient right now. */
Richard Jonescebbefc2006-05-23 18:28:17 +00002896 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Neal Norwitz245ce8d2006-06-12 02:16:10 +00002897 int i, j, nargs, found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002898 char *cellname, *argname;
2899 PyObject *c;
2900
2901 nargs = co->co_argcount;
2902 if (co->co_flags & CO_VARARGS)
2903 nargs++;
2904 if (co->co_flags & CO_VARKEYWORDS)
2905 nargs++;
2906
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 /* Initialize each cell var, taking into account
2908 cell vars that are initialized from arguments.
2909
2910 Should arrange for the compiler to put cellvars
2911 that are arguments at the beginning of the cellvars
2912 list so that we can march over it more efficiently?
2913 */
Richard Jonescebbefc2006-05-23 18:28:17 +00002914 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002915 cellname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00002916 PyTuple_GET_ITEM(co->co_cellvars, i));
2917 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918 for (j = 0; j < nargs; j++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002919 argname = PyString_AS_STRING(
Tim Peters5ca576e2001-06-18 22:08:13 +00002920 PyTuple_GET_ITEM(co->co_varnames, j));
2921 if (strcmp(cellname, argname) == 0) {
2922 c = PyCell_New(GETLOCAL(j));
2923 if (c == NULL)
2924 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002925 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002926 found = 1;
2927 break;
2928 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002929 }
2930 if (found == 0) {
2931 c = PyCell_New(NULL);
2932 if (c == NULL)
2933 goto fail;
Richard Jonescebbefc2006-05-23 18:28:17 +00002934 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002935 }
2936 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002937 }
Richard Jonescebbefc2006-05-23 18:28:17 +00002938 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002939 int i;
Richard Jonescebbefc2006-05-23 18:28:17 +00002940 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002941 PyObject *o = PyTuple_GET_ITEM(closure, i);
2942 Py_INCREF(o);
Richard Jonescebbefc2006-05-23 18:28:17 +00002943 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002944 }
2945 }
2946
Tim Peters5ca576e2001-06-18 22:08:13 +00002947 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002948 /* Don't need to keep the reference to f_back, it will be set
2949 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002950 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002951 f->f_back = NULL;
2952
Jeremy Hylton985eba52003-02-05 23:13:00 +00002953 PCALL(PCALL_GENERATOR);
2954
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002955 /* Create a new generator that owns the ready to run frame
2956 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00002957 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00002958 }
2959
Thomas Woutersae406c62007-09-19 17:27:43 +00002960 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00002961
Thomas Woutersae406c62007-09-19 17:27:43 +00002962fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00002963
Tim Petersb13680b2001-11-27 23:29:29 +00002964 /* decref'ing the frame can cause __del__ methods to get invoked,
2965 which can call back into Python. While we're done with the
2966 current Python frame (f), the associated C stack is still in use,
2967 so recursion_depth must be boosted for the duration.
2968 */
2969 assert(tstate != NULL);
2970 ++tstate->recursion_depth;
Thomas Woutersae406c62007-09-19 17:27:43 +00002971 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00002972 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00002973 return retval;
2974}
2975
2976
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002977/* Implementation notes for set_exc_info() and reset_exc_info():
2978
2979- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
2980 'exc_traceback'. These always travel together.
2981
2982- tstate->curexc_ZZZ is the "hot" exception that is set by
2983 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
2984
2985- Once an exception is caught by an except clause, it is transferred
2986 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
2987 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00002988 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00002989
2990- Now let me explain the complicated dance with frame->f_exc_ZZZ.
2991
2992 Long ago, when none of this existed, there were just a few globals:
2993 one set corresponding to the "hot" exception, and one set
2994 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
2995 globals; they were simply stored as sys.exc_ZZZ. For backwards
2996 compatibility, they still are!) The problem was that in code like
2997 this:
2998
2999 try:
3000 "something that may fail"
3001 except "some exception":
3002 "do something else first"
3003 "print the exception from sys.exc_ZZZ."
3004
3005 if "do something else first" invoked something that raised and caught
3006 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3007 cause of subtle bugs. I fixed this by changing the semantics as
3008 follows:
3009
3010 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3011 *in that frame*.
3012
3013 - But initially, and as long as no exception is caught in a given
3014 frame, sys.exc_ZZZ will hold the last exception caught in the
3015 previous frame (or the frame before that, etc.).
3016
3017 The first bullet fixed the bug in the above example. The second
3018 bullet was for backwards compatibility: it was (and is) common to
3019 have a function that is called when an exception is caught, and to
3020 have that function access the caught exception via sys.exc_ZZZ.
3021 (Example: traceback.print_exc()).
3022
3023 At the same time I fixed the problem that sys.exc_ZZZ weren't
3024 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3025 but that's really a separate improvement.
3026
3027 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3028 variables to what they were before the current frame was called. The
3029 set_exc_info() function saves them on the frame so that
3030 reset_exc_info() can restore them. The invariant is that
3031 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3032 exception (where "catching" an exception applies only to successful
3033 except clauses); and if the current frame ever caught an exception,
3034 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3035 at the start of the current frame.
3036
3037*/
3038
Fredrik Lundh7a830892006-05-27 10:39:48 +00003039static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003040set_exc_info(PyThreadState *tstate,
3041 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003042{
Tim Peters7df5e7f2006-05-26 23:14:37 +00003043 PyFrameObject *frame = tstate->frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003044 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003045
Tim Peters7df5e7f2006-05-26 23:14:37 +00003046 assert(type != NULL);
3047 assert(frame != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003048 if (frame->f_exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003049 assert(frame->f_exc_value == NULL);
3050 assert(frame->f_exc_traceback == NULL);
3051 /* This frame didn't catch an exception before. */
3052 /* Save previous exception of this thread in this frame. */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003053 if (tstate->exc_type == NULL) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00003054 /* XXX Why is this set to Py_None? */
Guido van Rossuma027efa1997-05-05 20:56:21 +00003055 Py_INCREF(Py_None);
3056 tstate->exc_type = Py_None;
3057 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003058 Py_INCREF(tstate->exc_type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003059 Py_XINCREF(tstate->exc_value);
3060 Py_XINCREF(tstate->exc_traceback);
3061 frame->f_exc_type = tstate->exc_type;
3062 frame->f_exc_value = tstate->exc_value;
3063 frame->f_exc_traceback = tstate->exc_traceback;
3064 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00003065 /* Set new exception for this thread. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003066 tmp_type = tstate->exc_type;
3067 tmp_value = tstate->exc_value;
3068 tmp_tb = tstate->exc_traceback;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003069 Py_INCREF(type);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003070 Py_XINCREF(value);
3071 Py_XINCREF(tb);
3072 tstate->exc_type = type;
3073 tstate->exc_value = value;
3074 tstate->exc_traceback = tb;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003075 Py_XDECREF(tmp_type);
3076 Py_XDECREF(tmp_value);
3077 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003078 /* For b/w compatibility */
3079 PySys_SetObject("exc_type", type);
3080 PySys_SetObject("exc_value", value);
3081 PySys_SetObject("exc_traceback", tb);
3082}
3083
Fredrik Lundh7a830892006-05-27 10:39:48 +00003084static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003085reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003086{
3087 PyFrameObject *frame;
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003088 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003089
3090 /* It's a precondition that the thread state's frame caught an
3091 * exception -- verify in a debug build.
3092 */
3093 assert(tstate != NULL);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003094 frame = tstate->frame;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003095 assert(frame != NULL);
3096 assert(frame->f_exc_type != NULL);
3097
3098 /* Copy the frame's exception info back to the thread state. */
3099 tmp_type = tstate->exc_type;
3100 tmp_value = tstate->exc_value;
3101 tmp_tb = tstate->exc_traceback;
3102 Py_INCREF(frame->f_exc_type);
3103 Py_XINCREF(frame->f_exc_value);
3104 Py_XINCREF(frame->f_exc_traceback);
3105 tstate->exc_type = frame->f_exc_type;
3106 tstate->exc_value = frame->f_exc_value;
3107 tstate->exc_traceback = frame->f_exc_traceback;
3108 Py_XDECREF(tmp_type);
3109 Py_XDECREF(tmp_value);
3110 Py_XDECREF(tmp_tb);
3111
3112 /* For b/w compatibility */
3113 PySys_SetObject("exc_type", frame->f_exc_type);
3114 PySys_SetObject("exc_value", frame->f_exc_value);
3115 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3116
3117 /* Clear the frame's exception info. */
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003118 tmp_type = frame->f_exc_type;
3119 tmp_value = frame->f_exc_value;
3120 tmp_tb = frame->f_exc_traceback;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003121 frame->f_exc_type = NULL;
3122 frame->f_exc_value = NULL;
3123 frame->f_exc_traceback = NULL;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003124 Py_DECREF(tmp_type);
Guido van Rossumdf4c3081997-05-20 17:06:11 +00003125 Py_XDECREF(tmp_value);
3126 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003127}
3128
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003129/* Logic for the raise statement (too complicated for inlining).
3130 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003131static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003132do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003133{
Guido van Rossumd295f121998-04-09 21:39:57 +00003134 if (type == NULL) {
3135 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003136 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumd295f121998-04-09 21:39:57 +00003137 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3138 value = tstate->exc_value;
3139 tb = tstate->exc_traceback;
3140 Py_XINCREF(type);
3141 Py_XINCREF(value);
3142 Py_XINCREF(tb);
3143 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003144
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003145 /* We support the following forms of raise:
3146 raise <class>, <classinstance>
3147 raise <class>, <argument tuple>
3148 raise <class>, None
3149 raise <class>, <argument>
3150 raise <classinstance>, None
3151 raise <string>, <object>
3152 raise <string>, None
3153
3154 An omitted second argument is the same as None.
3155
3156 In addition, raise <tuple>, <anything> is the same as
3157 raising the tuple's first item (and it better have one!);
3158 this rule is applied recursively.
3159
3160 Finally, an optional third argument can be supplied, which
3161 gives the traceback to be substituted (useful when
3162 re-raising an exception after examining it). */
3163
3164 /* First, check the traceback argument, replacing None with
3165 NULL. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003166 if (tb == Py_None) {
3167 Py_DECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003168 tb = NULL;
3169 }
3170 else if (tb != NULL && !PyTraceBack_Check(tb)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003171 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00003172 "raise: arg 3 must be a traceback or None");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003173 goto raise_error;
3174 }
3175
3176 /* Next, replace a missing value with None */
3177 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003178 value = Py_None;
3179 Py_INCREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003180 }
3181
3182 /* Next, repeatedly, replace a tuple exception with its first item */
Guido van Rossumb209a111997-04-29 18:18:01 +00003183 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3184 PyObject *tmp = type;
3185 type = PyTuple_GET_ITEM(type, 0);
3186 Py_INCREF(type);
3187 Py_DECREF(tmp);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003188 }
3189
Brett Cannon129bd522007-01-30 21:34:36 +00003190 if (PyExceptionClass_Check(type))
Barry Warsaw4249f541997-08-22 21:26:19 +00003191 PyErr_NormalizeException(&type, &value, &tb);
3192
Brett Cannonbf364092006-03-01 04:25:17 +00003193 else if (PyExceptionInstance_Check(type)) {
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003194 /* Raising an instance. The value should be a dummy. */
Guido van Rossumb209a111997-04-29 18:18:01 +00003195 if (value != Py_None) {
3196 PyErr_SetString(PyExc_TypeError,
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003197 "instance exception may not have a separate value");
3198 goto raise_error;
3199 }
3200 else {
3201 /* Normalize to raise <class>, <instance> */
Guido van Rossumb209a111997-04-29 18:18:01 +00003202 Py_DECREF(value);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003203 value = type;
Brett Cannonbf364092006-03-01 04:25:17 +00003204 type = PyExceptionInstance_Class(type);
Guido van Rossumb209a111997-04-29 18:18:01 +00003205 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003206 }
3207 }
3208 else {
3209 /* Not something you can raise. You get an exception
3210 anyway, just not what you specified :-) */
Jeremy Hylton960d9482001-04-27 02:25:33 +00003211 PyErr_Format(PyExc_TypeError,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003212 "exceptions must be classes or instances, not %s",
3213 type->ob_type->tp_name);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003214 goto raise_error;
3215 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003216
3217 assert(PyExceptionClass_Check(type));
3218 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00003219 if (PyErr_WarnEx(PyExc_DeprecationWarning,
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00003220 "exceptions must derive from BaseException "
3221 "in 3.x", 1) < 0)
Guido van Rossum504153d2008-03-18 04:26:48 +00003222 goto raise_error;
3223 }
3224
Guido van Rossumb209a111997-04-29 18:18:01 +00003225 PyErr_Restore(type, value, tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003226 if (tb == NULL)
3227 return WHY_EXCEPTION;
3228 else
3229 return WHY_RERAISE;
3230 raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003231 Py_XDECREF(value);
3232 Py_XDECREF(type);
3233 Py_XDECREF(tb);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003234 return WHY_EXCEPTION;
3235}
3236
Tim Petersd6d010b2001-06-21 02:49:55 +00003237/* Iterate v argcnt times and store the results on the stack (via decreasing
3238 sp). Return 1 for success, 0 if error. */
3239
Fredrik Lundh7a830892006-05-27 10:39:48 +00003240static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003241unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003242{
Tim Petersd6d010b2001-06-21 02:49:55 +00003243 int i = 0;
3244 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003245 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003246
Tim Petersd6d010b2001-06-21 02:49:55 +00003247 assert(v != NULL);
3248
3249 it = PyObject_GetIter(v);
3250 if (it == NULL)
3251 goto Error;
3252
3253 for (; i < argcnt; i++) {
3254 w = PyIter_Next(it);
3255 if (w == NULL) {
3256 /* Iterator done, via error or exhaustion. */
3257 if (!PyErr_Occurred()) {
3258 PyErr_Format(PyExc_ValueError,
3259 "need more than %d value%s to unpack",
3260 i, i == 1 ? "" : "s");
3261 }
3262 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003263 }
3264 *--sp = w;
3265 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003266
3267 /* We better have exhausted the iterator now. */
3268 w = PyIter_Next(it);
3269 if (w == NULL) {
3270 if (PyErr_Occurred())
3271 goto Error;
3272 Py_DECREF(it);
3273 return 1;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003274 }
Guido van Rossumbb8f59a2001-12-03 19:33:25 +00003275 Py_DECREF(w);
Tim Petersd6d010b2001-06-21 02:49:55 +00003276 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
Barry Warsawe42b18f1997-08-25 22:13:04 +00003277 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003278Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003279 for (; i > 0; i--, sp++)
3280 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003281 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003282 return 0;
3283}
3284
3285
Guido van Rossum96a42c81992-01-12 02:29:51 +00003286#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003287static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003288prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003289{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003290 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003291 if (PyObject_Print(v, stdout, 0) != 0)
3292 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003293 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003294 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003295}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003296#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003297
Fredrik Lundh7a830892006-05-27 10:39:48 +00003298static void
Fred Drake5755ce62001-06-27 19:19:46 +00003299call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003300{
Guido van Rossumb209a111997-04-29 18:18:01 +00003301 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003302 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003303 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003304 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003305 value = Py_None;
3306 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003307 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003308 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003309 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003310 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003311 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003312 }
Fred Drake5755ce62001-06-27 19:19:46 +00003313 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003314 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003315 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003316 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003317 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003318 Py_XDECREF(type);
3319 Py_XDECREF(value);
3320 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003321 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003322}
3323
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003324static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003325call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003326 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003327{
3328 PyObject *type, *value, *traceback;
3329 int err;
3330 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003331 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003332 if (err == 0)
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003333 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003334 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003335 return 0;
3336 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003337 else {
3338 Py_XDECREF(type);
3339 Py_XDECREF(value);
3340 Py_XDECREF(traceback);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003341 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003342 }
3343}
3344
Fredrik Lundh7a830892006-05-27 10:39:48 +00003345static int
Fred Drake5755ce62001-06-27 19:19:46 +00003346call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3347 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003348{
Fred Drake5755ce62001-06-27 19:19:46 +00003349 register PyThreadState *tstate = frame->f_tstate;
3350 int result;
3351 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003352 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003353 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003354 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003355 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003356 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3357 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003358 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003359 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003360}
3361
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003362PyObject *
3363_PyEval_CallTracing(PyObject *func, PyObject *args)
3364{
3365 PyFrameObject *frame = PyEval_GetFrame();
3366 PyThreadState *tstate = frame->f_tstate;
3367 int save_tracing = tstate->tracing;
3368 int save_use_tracing = tstate->use_tracing;
3369 PyObject *result;
3370
3371 tstate->tracing = 0;
3372 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3373 || (tstate->c_profilefunc != NULL));
3374 result = PyObject_Call(func, args, NULL);
3375 tstate->tracing = save_tracing;
3376 tstate->use_tracing = save_use_tracing;
3377 return result;
3378}
3379
Fredrik Lundh7a830892006-05-27 10:39:48 +00003380static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003381maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003382 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3383 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003384{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003385 int result = 0;
3386
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003387 /* If the last instruction executed isn't in the current
3388 instruction window, reset the window. If the last
3389 instruction happens to fall at the start of a line or if it
3390 represents a jump backwards, call the trace function.
3391 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003392 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003393 int line;
3394 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003395
Thomas Woutersae406c62007-09-19 17:27:43 +00003396 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3397 &bounds);
3398 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003399 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003400 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003401 PyTrace_LINE, Py_None);
Thomas Woutersae406c62007-09-19 17:27:43 +00003402 }
3403 *instr_lb = bounds.ap_lower;
3404 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003405 }
Armin Rigobf57a142004-03-22 19:24:58 +00003406 else if (frame->f_lasti <= *instr_prev) {
Jeremy Hyltona4ebc132006-04-18 14:47:00 +00003407 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003408 }
3409 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003410 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003411}
3412
Fred Drake5755ce62001-06-27 19:19:46 +00003413void
3414PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003415{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003416 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003417 PyObject *temp = tstate->c_profileobj;
3418 Py_XINCREF(arg);
3419 tstate->c_profilefunc = NULL;
3420 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003421 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003422 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003423 Py_XDECREF(temp);
3424 tstate->c_profilefunc = func;
3425 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003426 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003427 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003428}
3429
3430void
3431PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3432{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003433 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003434 PyObject *temp = tstate->c_traceobj;
3435 Py_XINCREF(arg);
3436 tstate->c_tracefunc = NULL;
3437 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003438 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003439 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003440 Py_XDECREF(temp);
3441 tstate->c_tracefunc = func;
3442 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003443 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003444 tstate->use_tracing = ((func != NULL)
3445 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003446}
3447
Guido van Rossumb209a111997-04-29 18:18:01 +00003448PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003449PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003450{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003451 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003452 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003453 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003454 else
3455 return current_frame->f_builtins;
3456}
3457
Guido van Rossumb209a111997-04-29 18:18:01 +00003458PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003459PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003460{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003461 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003462 if (current_frame == NULL)
3463 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003464 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003465 return current_frame->f_locals;
3466}
3467
Guido van Rossumb209a111997-04-29 18:18:01 +00003468PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003469PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003470{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003471 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003472 if (current_frame == NULL)
3473 return NULL;
3474 else
3475 return current_frame->f_globals;
3476}
3477
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003478PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003479PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003480{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003481 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003482 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003483}
3484
Guido van Rossum6135a871995-01-09 17:53:26 +00003485int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003486PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003487{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003488 PyFrameObject *current_frame = PyEval_GetFrame();
Neal Norwitzb9845e72006-06-12 02:11:18 +00003489 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003490}
3491
Guido van Rossumbe270261997-05-22 22:26:18 +00003492int
Tim Peters5ba58662001-07-16 02:29:45 +00003493PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003494{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003495 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003496 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003497
3498 if (current_frame != NULL) {
3499 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003500 const int compilerflags = codeflags & PyCF_MASK;
3501 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003502 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003503 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003504 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003505#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003506 if (codeflags & CO_GENERATOR_ALLOWED) {
3507 result = 1;
3508 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3509 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003510#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003511 }
3512 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003513}
3514
3515int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003516Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003517{
Guido van Rossumb209a111997-04-29 18:18:01 +00003518 PyObject *f = PySys_GetObject("stdout");
Guido van Rossumbe270261997-05-22 22:26:18 +00003519 if (f == NULL)
3520 return 0;
3521 if (!PyFile_SoftSpace(f, 0))
3522 return 0;
3523 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003524}
3525
Guido van Rossum3f5da241990-12-20 15:06:42 +00003526
Guido van Rossum681d79a1995-07-18 14:51:37 +00003527/* External interface to call any callable object.
3528 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003529
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003530#undef PyEval_CallObject
3531/* for backward compatibility: export this interface */
3532
Guido van Rossumb209a111997-04-29 18:18:01 +00003533PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003534PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003535{
Guido van Rossumb209a111997-04-29 18:18:01 +00003536 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003537}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003538#define PyEval_CallObject(func,arg) \
3539 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003540
Guido van Rossumb209a111997-04-29 18:18:01 +00003541PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003542PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003543{
Jeremy Hylton52820442001-01-03 23:52:36 +00003544 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003545
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003546 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003547 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003548 if (arg == NULL)
3549 return NULL;
3550 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003551 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003552 PyErr_SetString(PyExc_TypeError,
3553 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003554 return NULL;
3555 }
3556 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003557 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003558
Guido van Rossumb209a111997-04-29 18:18:01 +00003559 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003560 PyErr_SetString(PyExc_TypeError,
3561 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003562 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003563 return NULL;
3564 }
3565
Tim Peters6d6c1a32001-08-02 04:15:00 +00003566 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003567 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003568 return result;
3569}
3570
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003571const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003572PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003573{
3574 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003575 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003576 else if (PyFunction_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003577 return PyString_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003578 else if (PyCFunction_Check(func))
3579 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3580 else if (PyClass_Check(func))
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003581 return PyString_AsString(((PyClassObject*)func)->cl_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003582 else if (PyInstance_Check(func)) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003583 return PyString_AsString(
Jeremy Hylton512a2372001-04-11 13:52:29 +00003584 ((PyInstanceObject*)func)->in_class->cl_name);
3585 } else {
3586 return func->ob_type->tp_name;
3587 }
3588}
3589
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003590const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003591PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003592{
3593 if (PyMethod_Check(func))
3594 return "()";
3595 else if (PyFunction_Check(func))
3596 return "()";
3597 else if (PyCFunction_Check(func))
3598 return "()";
3599 else if (PyClass_Check(func))
3600 return " constructor";
3601 else if (PyInstance_Check(func)) {
3602 return " instance";
3603 } else {
3604 return " object";
3605 }
3606}
3607
Fredrik Lundh7a830892006-05-27 10:39:48 +00003608static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003609err_args(PyObject *func, int flags, int nargs)
3610{
3611 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003612 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003613 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003614 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003615 nargs);
3616 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003617 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003618 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003619 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003620 nargs);
3621}
3622
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003623#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003624if (tstate->use_tracing && tstate->c_profilefunc) { \
3625 if (call_trace(tstate->c_profilefunc, \
3626 tstate->c_profileobj, \
3627 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003628 func)) { \
3629 x = NULL; \
3630 } \
3631 else { \
3632 x = call; \
3633 if (tstate->c_profilefunc != NULL) { \
3634 if (x == NULL) { \
3635 call_trace_protected(tstate->c_profilefunc, \
3636 tstate->c_profileobj, \
3637 tstate->frame, PyTrace_C_EXCEPTION, \
3638 func); \
3639 /* XXX should pass (type, value, tb) */ \
3640 } else { \
3641 if (call_trace(tstate->c_profilefunc, \
3642 tstate->c_profileobj, \
3643 tstate->frame, PyTrace_C_RETURN, \
3644 func)) { \
3645 Py_DECREF(x); \
3646 x = NULL; \
3647 } \
3648 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003649 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003650 } \
3651} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003652 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003653 }
3654
Fredrik Lundh7a830892006-05-27 10:39:48 +00003655static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003656call_function(PyObject ***pp_stack, int oparg
3657#ifdef WITH_TSC
3658 , uint64* pintr0, uint64* pintr1
3659#endif
3660 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003661{
3662 int na = oparg & 0xff;
3663 int nk = (oparg>>8) & 0xff;
3664 int n = na + 2 * nk;
3665 PyObject **pfunc = (*pp_stack) - n - 1;
3666 PyObject *func = *pfunc;
3667 PyObject *x, *w;
3668
Jeremy Hylton985eba52003-02-05 23:13:00 +00003669 /* Always dispatch PyCFunction first, because these are
3670 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003671 */
3672 if (PyCFunction_Check(func) && nk == 0) {
3673 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003674 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003675
3676 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003677 if (flags & (METH_NOARGS | METH_O)) {
3678 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3679 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003680 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003681 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003682 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003683 else if (flags & METH_O && na == 1) {
3684 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003685 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003686 Py_DECREF(arg);
3687 }
3688 else {
3689 err_args(func, flags, na);
3690 x = NULL;
3691 }
3692 }
3693 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003694 PyObject *callargs;
3695 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003696 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003697 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003698 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003699 Py_XDECREF(callargs);
3700 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003701 } else {
3702 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3703 /* optimize access to bound methods */
3704 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003705 PCALL(PCALL_METHOD);
3706 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003707 Py_INCREF(self);
3708 func = PyMethod_GET_FUNCTION(func);
3709 Py_INCREF(func);
3710 Py_DECREF(*pfunc);
3711 *pfunc = self;
3712 na++;
3713 n++;
3714 } else
3715 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003716 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003717 if (PyFunction_Check(func))
3718 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003719 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003720 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003721 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003722 Py_DECREF(func);
3723 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003724
Armin Rigod34fa522006-03-28 19:10:40 +00003725 /* Clear the stack of the function object. Also removes
3726 the arguments in case they weren't consumed already
3727 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003728 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003729 while ((*pp_stack) > pfunc) {
3730 w = EXT_POP(*pp_stack);
3731 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003732 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003733 }
3734 return x;
3735}
3736
Jeremy Hylton192690e2002-08-16 18:36:11 +00003737/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003738 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003739 For the simplest case -- a function that takes only positional
3740 arguments and is called with only positional arguments -- it
3741 inlines the most primitive frame setup code from
3742 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3743 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003744*/
3745
Fredrik Lundh7a830892006-05-27 10:39:48 +00003746static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003747fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003748{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003749 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003750 PyObject *globals = PyFunction_GET_GLOBALS(func);
3751 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3752 PyObject **d = NULL;
3753 int nd = 0;
3754
Jeremy Hylton985eba52003-02-05 23:13:00 +00003755 PCALL(PCALL_FUNCTION);
3756 PCALL(PCALL_FAST_FUNCTION);
Raymond Hettinger40174c32003-05-31 07:04:16 +00003757 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003758 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3759 PyFrameObject *f;
3760 PyObject *retval = NULL;
3761 PyThreadState *tstate = PyThreadState_GET();
3762 PyObject **fastlocals, **stack;
3763 int i;
3764
3765 PCALL(PCALL_FASTER_FUNCTION);
3766 assert(globals != NULL);
3767 /* XXX Perhaps we should create a specialized
3768 PyFrame_New() that doesn't take locals, but does
3769 take builtins without sanity checking them.
3770 */
Neal Norwitzdf6a6492006-08-13 18:10:10 +00003771 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003772 f = PyFrame_New(tstate, co, globals, NULL);
3773 if (f == NULL)
3774 return NULL;
3775
3776 fastlocals = f->f_localsplus;
3777 stack = (*pp_stack) - n;
3778
3779 for (i = 0; i < n; i++) {
3780 Py_INCREF(*stack);
3781 fastlocals[i] = *stack++;
3782 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003783 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003784 ++tstate->recursion_depth;
3785 Py_DECREF(f);
3786 --tstate->recursion_depth;
3787 return retval;
3788 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003789 if (argdefs != NULL) {
3790 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimese93237d2007-12-19 02:37:44 +00003791 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003792 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003793 return PyEval_EvalCodeEx(co, globals,
3794 (PyObject *)NULL, (*pp_stack)-n, na,
3795 (*pp_stack)-2*nk, nk, d, nd,
3796 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003797}
3798
Fredrik Lundh7a830892006-05-27 10:39:48 +00003799static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003800update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3801 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003802{
3803 PyObject *kwdict = NULL;
3804 if (orig_kwdict == NULL)
3805 kwdict = PyDict_New();
3806 else {
3807 kwdict = PyDict_Copy(orig_kwdict);
3808 Py_DECREF(orig_kwdict);
3809 }
3810 if (kwdict == NULL)
3811 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003812 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003813 int err;
3814 PyObject *value = EXT_POP(*pp_stack);
3815 PyObject *key = EXT_POP(*pp_stack);
3816 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersae406c62007-09-19 17:27:43 +00003817 PyErr_Format(PyExc_TypeError,
3818 "%.200s%s got multiple values "
3819 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003820 PyEval_GetFuncName(func),
3821 PyEval_GetFuncDesc(func),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003822 PyString_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003823 Py_DECREF(key);
3824 Py_DECREF(value);
3825 Py_DECREF(kwdict);
3826 return NULL;
3827 }
3828 err = PyDict_SetItem(kwdict, key, value);
3829 Py_DECREF(key);
3830 Py_DECREF(value);
3831 if (err) {
3832 Py_DECREF(kwdict);
3833 return NULL;
3834 }
3835 }
3836 return kwdict;
3837}
3838
Fredrik Lundh7a830892006-05-27 10:39:48 +00003839static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003840update_star_args(int nstack, int nstar, PyObject *stararg,
3841 PyObject ***pp_stack)
3842{
3843 PyObject *callargs, *w;
3844
3845 callargs = PyTuple_New(nstack + nstar);
3846 if (callargs == NULL) {
3847 return NULL;
3848 }
3849 if (nstar) {
3850 int i;
3851 for (i = 0; i < nstar; i++) {
3852 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3853 Py_INCREF(a);
3854 PyTuple_SET_ITEM(callargs, nstack + i, a);
3855 }
3856 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003857 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003858 w = EXT_POP(*pp_stack);
3859 PyTuple_SET_ITEM(callargs, nstack, w);
3860 }
3861 return callargs;
3862}
3863
Fredrik Lundh7a830892006-05-27 10:39:48 +00003864static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003865load_args(PyObject ***pp_stack, int na)
3866{
3867 PyObject *args = PyTuple_New(na);
3868 PyObject *w;
3869
3870 if (args == NULL)
3871 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003872 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003873 w = EXT_POP(*pp_stack);
3874 PyTuple_SET_ITEM(args, na, w);
3875 }
3876 return args;
3877}
3878
Fredrik Lundh7a830892006-05-27 10:39:48 +00003879static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003880do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3881{
3882 PyObject *callargs = NULL;
3883 PyObject *kwdict = NULL;
3884 PyObject *result = NULL;
3885
3886 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003887 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003888 if (kwdict == NULL)
3889 goto call_fail;
3890 }
3891 callargs = load_args(pp_stack, na);
3892 if (callargs == NULL)
3893 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003894#ifdef CALL_PROFILE
3895 /* At this point, we have to look at the type of func to
3896 update the call stats properly. Do it here so as to avoid
3897 exposing the call stats machinery outside ceval.c
3898 */
3899 if (PyFunction_Check(func))
3900 PCALL(PCALL_FUNCTION);
3901 else if (PyMethod_Check(func))
3902 PCALL(PCALL_METHOD);
3903 else if (PyType_Check(func))
3904 PCALL(PCALL_TYPE);
3905 else
3906 PCALL(PCALL_OTHER);
3907#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003908 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00003909 call_fail:
3910 Py_XDECREF(callargs);
3911 Py_XDECREF(kwdict);
3912 return result;
3913}
3914
Fredrik Lundh7a830892006-05-27 10:39:48 +00003915static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00003916ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3917{
3918 int nstar = 0;
3919 PyObject *callargs = NULL;
3920 PyObject *stararg = NULL;
3921 PyObject *kwdict = NULL;
3922 PyObject *result = NULL;
3923
3924 if (flags & CALL_FLAG_KW) {
3925 kwdict = EXT_POP(*pp_stack);
Georg Brandl2134e752007-05-21 20:34:16 +00003926 if (!PyDict_Check(kwdict)) {
3927 PyObject *d;
3928 d = PyDict_New();
3929 if (d == NULL)
3930 goto ext_call_fail;
3931 if (PyDict_Update(d, kwdict) != 0) {
3932 Py_DECREF(d);
3933 /* PyDict_Update raises attribute
3934 * error (percolated from an attempt
3935 * to get 'keys' attribute) instead of
3936 * a type error if its second argument
3937 * is not a mapping.
3938 */
3939 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3940 PyErr_Format(PyExc_TypeError,
3941 "%.200s%.200s argument after ** "
3942 "must be a mapping, not %.200s",
3943 PyEval_GetFuncName(func),
3944 PyEval_GetFuncDesc(func),
3945 kwdict->ob_type->tp_name);
3946 }
3947 goto ext_call_fail;
3948 }
3949 Py_DECREF(kwdict);
3950 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00003951 }
3952 }
3953 if (flags & CALL_FLAG_VAR) {
3954 stararg = EXT_POP(*pp_stack);
3955 if (!PyTuple_Check(stararg)) {
3956 PyObject *t = NULL;
3957 t = PySequence_Tuple(stararg);
3958 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003959 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3960 PyErr_Format(PyExc_TypeError,
Georg Brandl2134e752007-05-21 20:34:16 +00003961 "%.200s%.200s argument after * "
3962 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003963 PyEval_GetFuncName(func),
Georg Brandl2134e752007-05-21 20:34:16 +00003964 PyEval_GetFuncDesc(func),
3965 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003966 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003967 goto ext_call_fail;
3968 }
3969 Py_DECREF(stararg);
3970 stararg = t;
3971 }
3972 nstar = PyTuple_GET_SIZE(stararg);
3973 }
3974 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003975 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003976 if (kwdict == NULL)
3977 goto ext_call_fail;
3978 }
3979 callargs = update_star_args(na, nstar, stararg, pp_stack);
3980 if (callargs == NULL)
3981 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003982#ifdef CALL_PROFILE
3983 /* At this point, we have to look at the type of func to
3984 update the call stats properly. Do it here so as to avoid
3985 exposing the call stats machinery outside ceval.c
3986 */
3987 if (PyFunction_Check(func))
3988 PCALL(PCALL_FUNCTION);
3989 else if (PyMethod_Check(func))
3990 PCALL(PCALL_METHOD);
3991 else if (PyType_Check(func))
3992 PCALL(PCALL_TYPE);
3993 else
3994 PCALL(PCALL_OTHER);
3995#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003996 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00003997ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003998 Py_XDECREF(callargs);
3999 Py_XDECREF(kwdict);
4000 Py_XDECREF(stararg);
4001 return result;
4002}
4003
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004004/* Extract a slice index from a PyInt or PyLong or an object with the
4005 nb_index slot defined, and store in *pi.
4006 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4007 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 +00004008 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004009*/
Tim Petersb5196382001-12-16 19:44:20 +00004010/* Note: If v is NULL, return success without storing into *pi. This
4011 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4012 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004013*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004014int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004015_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004016{
Tim Petersb5196382001-12-16 19:44:20 +00004017 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004018 Py_ssize_t x;
Andrew M. Kuchling2194b162000-02-23 22:18:48 +00004019 if (PyInt_Check(v)) {
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004020 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4021 however, it looks like it should be AsSsize_t.
4022 There should be a comment here explaining why.
4023 */
4024 x = PyInt_AS_LONG(v);
Tim Peters7df5e7f2006-05-26 23:14:37 +00004025 }
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004026 else if (PyIndex_Check(v)) {
4027 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004028 if (x == -1 && PyErr_Occurred())
4029 return 0;
4030 }
4031 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004032 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004033 "slice indices must be integers or "
4034 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004035 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004036 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004037 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004038 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004039 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004040}
4041
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004042#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004043#define ISINDEX(x) ((x) == NULL || \
4044 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004045
Fredrik Lundh7a830892006-05-27 10:39:48 +00004046static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004047apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004048{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004049 PyTypeObject *tp = u->ob_type;
4050 PySequenceMethods *sq = tp->tp_as_sequence;
4051
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004052 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004053 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004054 if (!_PyEval_SliceIndex(v, &ilow))
4055 return NULL;
4056 if (!_PyEval_SliceIndex(w, &ihigh))
4057 return NULL;
4058 return PySequence_GetSlice(u, ilow, ihigh);
4059 }
4060 else {
4061 PyObject *slice = PySlice_New(v, w, NULL);
Guido van Rossum354797c2001-12-03 19:45:06 +00004062 if (slice != NULL) {
4063 PyObject *res = PyObject_GetItem(u, slice);
4064 Py_DECREF(slice);
4065 return res;
4066 }
Guido van Rossum50d756e2001-08-18 17:43:36 +00004067 else
4068 return NULL;
4069 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004070}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004071
Fredrik Lundh7a830892006-05-27 10:39:48 +00004072static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004073assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4074 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004075{
Guido van Rossum50d756e2001-08-18 17:43:36 +00004076 PyTypeObject *tp = u->ob_type;
4077 PySequenceMethods *sq = tp->tp_as_sequence;
4078
Georg Brandl0fca97a2007-03-05 22:28:08 +00004079 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004080 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004081 if (!_PyEval_SliceIndex(v, &ilow))
4082 return -1;
4083 if (!_PyEval_SliceIndex(w, &ihigh))
4084 return -1;
4085 if (x == NULL)
4086 return PySequence_DelSlice(u, ilow, ihigh);
4087 else
4088 return PySequence_SetSlice(u, ilow, ihigh, x);
4089 }
4090 else {
4091 PyObject *slice = PySlice_New(v, w, NULL);
4092 if (slice != NULL) {
Guido van Rossum354797c2001-12-03 19:45:06 +00004093 int res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004094 if (x != NULL)
Guido van Rossum354797c2001-12-03 19:45:06 +00004095 res = PyObject_SetItem(u, slice, x);
Guido van Rossum50d756e2001-08-18 17:43:36 +00004096 else
Guido van Rossum354797c2001-12-03 19:45:06 +00004097 res = PyObject_DelItem(u, slice);
4098 Py_DECREF(slice);
4099 return res;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004100 }
4101 else
4102 return -1;
4103 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004104}
4105
Guido van Rossum04edb522008-03-18 02:49:46 +00004106#define Py3kExceptionClass_Check(x) \
4107 (PyType_Check((x)) && \
4108 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4109
4110#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Georg Brandld5b635f2008-03-25 08:29:14 +00004111 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004112
Fredrik Lundh7a830892006-05-27 10:39:48 +00004113static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004114cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004115{
Guido van Rossumac7be682001-01-17 15:42:30 +00004116 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004117 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004118 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004119 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004120 break;
4121 case PyCmp_IS_NOT:
4122 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004123 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004124 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004125 res = PySequence_Contains(w, v);
4126 if (res < 0)
4127 return NULL;
4128 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004129 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004130 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004131 if (res < 0)
4132 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004133 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004134 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004135 case PyCmp_EXC_MATCH:
Brett Cannon129bd522007-01-30 21:34:36 +00004136 if (PyTuple_Check(w)) {
4137 Py_ssize_t i, length;
4138 length = PyTuple_Size(w);
4139 for (i = 0; i < length; i += 1) {
4140 PyObject *exc = PyTuple_GET_ITEM(w, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004141 if (PyString_Check(exc)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004142 int ret_val;
4143 ret_val = PyErr_WarnEx(
Thomas Wouterse2176022007-09-20 17:35:10 +00004144 PyExc_DeprecationWarning,
4145 "catching of string "
4146 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004147 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004148 return NULL;
4149 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004150 else if (Py_Py3kWarningFlag &&
4151 !PyTuple_Check(exc) &&
4152 !Py3kExceptionClass_Check(exc))
Guido van Rossum04edb522008-03-18 02:49:46 +00004153 {
4154 int ret_val;
4155 ret_val = PyErr_WarnEx(
4156 PyExc_DeprecationWarning,
4157 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004158 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004159 return NULL;
4160 }
Brett Cannon129bd522007-01-30 21:34:36 +00004161 }
4162 }
4163 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004164 if (PyString_Check(w)) {
Brett Cannon129bd522007-01-30 21:34:36 +00004165 int ret_val;
4166 ret_val = PyErr_WarnEx(
4167 PyExc_DeprecationWarning,
4168 "catching of string "
Thomas Wouterse2176022007-09-20 17:35:10 +00004169 "exceptions is deprecated", 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004170 if (ret_val < 0)
Brett Cannon129bd522007-01-30 21:34:36 +00004171 return NULL;
4172 }
Guido van Rossum20bda582008-03-18 03:15:05 +00004173 else if (Py_Py3kWarningFlag &&
4174 !PyTuple_Check(w) &&
4175 !Py3kExceptionClass_Check(w))
Guido van Rossum04edb522008-03-18 02:49:46 +00004176 {
4177 int ret_val;
4178 ret_val = PyErr_WarnEx(
4179 PyExc_DeprecationWarning,
4180 CANNOT_CATCH_MSG, 1);
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00004181 if (ret_val < 0)
Guido van Rossum04edb522008-03-18 02:49:46 +00004182 return NULL;
4183 }
Brett Cannon129bd522007-01-30 21:34:36 +00004184 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004185 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004186 break;
4187 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004188 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004189 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004190 v = res ? Py_True : Py_False;
4191 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004192 return v;
4193}
4194
Fredrik Lundh7a830892006-05-27 10:39:48 +00004195static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004196import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004197{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004198 PyObject *x;
4199
4200 x = PyObject_GetAttr(v, name);
4201 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Thomas Wouters52152252000-08-17 22:55:00 +00004202 PyErr_Format(PyExc_ImportError,
4203 "cannot import name %.230s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004204 PyString_AsString(name));
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004205 }
Thomas Wouters52152252000-08-17 22:55:00 +00004206 return x;
4207}
Guido van Rossumac7be682001-01-17 15:42:30 +00004208
Fredrik Lundh7a830892006-05-27 10:39:48 +00004209static int
Thomas Wouters52152252000-08-17 22:55:00 +00004210import_all_from(PyObject *locals, PyObject *v)
4211{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004212 PyObject *all = PyObject_GetAttrString(v, "__all__");
4213 PyObject *dict, *name, *value;
4214 int skip_leading_underscores = 0;
4215 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004216
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004217 if (all == NULL) {
4218 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4219 return -1; /* Unexpected error */
4220 PyErr_Clear();
4221 dict = PyObject_GetAttrString(v, "__dict__");
4222 if (dict == NULL) {
4223 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4224 return -1;
4225 PyErr_SetString(PyExc_ImportError,
4226 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004227 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004228 }
4229 all = PyMapping_Keys(dict);
4230 Py_DECREF(dict);
4231 if (all == NULL)
4232 return -1;
4233 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004234 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004235
4236 for (pos = 0, err = 0; ; pos++) {
4237 name = PySequence_GetItem(all, pos);
4238 if (name == NULL) {
4239 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4240 err = -1;
4241 else
4242 PyErr_Clear();
4243 break;
4244 }
4245 if (skip_leading_underscores &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004246 PyString_Check(name) &&
4247 PyString_AS_STRING(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004248 {
4249 Py_DECREF(name);
4250 continue;
4251 }
4252 value = PyObject_GetAttr(v, name);
4253 if (value == NULL)
4254 err = -1;
Armin Rigo70370852006-11-29 21:59:22 +00004255 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004256 err = PyDict_SetItem(locals, name, value);
Armin Rigo70370852006-11-29 21:59:22 +00004257 else
4258 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004259 Py_DECREF(name);
4260 Py_XDECREF(value);
4261 if (err != 0)
4262 break;
4263 }
4264 Py_DECREF(all);
4265 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004266}
4267
Fredrik Lundh7a830892006-05-27 10:39:48 +00004268static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004269build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004270{
Guido van Rossum7851eea2001-09-12 19:19:18 +00004271 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004272
4273 if (PyDict_Check(methods))
4274 metaclass = PyDict_GetItemString(methods, "__metaclass__");
Guido van Rossum7851eea2001-09-12 19:19:18 +00004275 if (metaclass != NULL)
Guido van Rossum2556f2e2001-12-06 14:09:56 +00004276 Py_INCREF(metaclass);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004277 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4278 base = PyTuple_GET_ITEM(bases, 0);
4279 metaclass = PyObject_GetAttrString(base, "__class__");
4280 if (metaclass == NULL) {
4281 PyErr_Clear();
4282 metaclass = (PyObject *)base->ob_type;
4283 Py_INCREF(metaclass);
Guido van Rossum25831651993-05-19 14:50:45 +00004284 }
4285 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004286 else {
4287 PyObject *g = PyEval_GetGlobals();
4288 if (g != NULL && PyDict_Check(g))
4289 metaclass = PyDict_GetItemString(g, "__metaclass__");
4290 if (metaclass == NULL)
4291 metaclass = (PyObject *) &PyClass_Type;
4292 Py_INCREF(metaclass);
4293 }
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004294 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
Thomas Woutersae406c62007-09-19 17:27:43 +00004295 NULL);
Guido van Rossum7851eea2001-09-12 19:19:18 +00004296 Py_DECREF(metaclass);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004297 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
Tim Peters7df5e7f2006-05-26 23:14:37 +00004298 /* A type error here likely means that the user passed
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004299 in a base that was not a class (such the random module
4300 instead of the random.random type). Help them out with
Raymond Hettingercfc31922004-09-16 16:41:57 +00004301 by augmenting the error message with more information.*/
4302
4303 PyObject *ptype, *pvalue, *ptraceback;
4304
4305 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004306 if (PyString_Check(pvalue)) {
Raymond Hettingercfc31922004-09-16 16:41:57 +00004307 PyObject *newmsg;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004308 newmsg = PyString_FromFormat(
Jeremy Hylton7c1e3472007-02-26 16:14:51 +00004309 "Error when calling the metaclass bases\n"
Thomas Woutersae406c62007-09-19 17:27:43 +00004310 " %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004311 PyString_AS_STRING(pvalue));
Raymond Hettingercfc31922004-09-16 16:41:57 +00004312 if (newmsg != NULL) {
4313 Py_DECREF(pvalue);
4314 pvalue = newmsg;
4315 }
4316 }
4317 PyErr_Restore(ptype, pvalue, ptraceback);
Raymond Hettingerf2c08302004-06-05 06:16:22 +00004318 }
Guido van Rossum7851eea2001-09-12 19:19:18 +00004319 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004320}
4321
Fredrik Lundh7a830892006-05-27 10:39:48 +00004322static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004323exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
4324 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004325{
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004326 int n;
Guido van Rossumb209a111997-04-29 18:18:01 +00004327 PyObject *v;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004328 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004329
Guido van Rossumb209a111997-04-29 18:18:01 +00004330 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4331 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004332 /* Backward compatibility hack */
Guido van Rossumb209a111997-04-29 18:18:01 +00004333 globals = PyTuple_GetItem(prog, 1);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004334 if (n == 3)
Guido van Rossumb209a111997-04-29 18:18:01 +00004335 locals = PyTuple_GetItem(prog, 2);
4336 prog = PyTuple_GetItem(prog, 0);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004337 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004338 if (globals == Py_None) {
4339 globals = PyEval_GetGlobals();
4340 if (locals == Py_None) {
4341 locals = PyEval_GetLocals();
Guido van Rossum681d79a1995-07-18 14:51:37 +00004342 plain = 1;
4343 }
Neal Norwitzdf6a6492006-08-13 18:10:10 +00004344 if (!globals || !locals) {
4345 PyErr_SetString(PyExc_SystemError,
4346 "globals and locals cannot be NULL");
4347 return -1;
4348 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004349 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004350 else if (locals == Py_None)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004351 locals = globals;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004352 if (!PyString_Check(prog) &&
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004353 !PyUnicode_Check(prog) &&
Guido van Rossumb209a111997-04-29 18:18:01 +00004354 !PyCode_Check(prog) &&
4355 !PyFile_Check(prog)) {
4356 PyErr_SetString(PyExc_TypeError,
Guido van Rossumac7be682001-01-17 15:42:30 +00004357 "exec: arg 1 must be a string, file, or code object");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004358 return -1;
4359 }
Fred Drake661ea262000-10-24 19:57:45 +00004360 if (!PyDict_Check(globals)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004361 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +00004362 "exec: arg 2 must be a dictionary or None");
4363 return -1;
4364 }
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004365 if (!PyMapping_Check(locals)) {
Fred Drake661ea262000-10-24 19:57:45 +00004366 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger66bd2332004-08-02 08:30:07 +00004367 "exec: arg 3 must be a mapping or None");
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004368 return -1;
4369 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004370 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
Guido van Rossuma027efa1997-05-05 20:56:21 +00004371 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
Guido van Rossumb209a111997-04-29 18:18:01 +00004372 if (PyCode_Check(prog)) {
Jeremy Hylton733c8932001-12-13 19:51:56 +00004373 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4374 PyErr_SetString(PyExc_TypeError,
4375 "code object passed to exec may not contain free variables");
4376 return -1;
4377 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004378 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004379 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004380 else if (PyFile_Check(prog)) {
Guido van Rossumb209a111997-04-29 18:18:01 +00004381 FILE *fp = PyFile_AsFile(prog);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004382 char *name = PyString_AsString(PyFile_Name(prog));
Jeremy Hylton714b1122007-02-25 16:01:58 +00004383 PyCompilerFlags cf;
Thomas Woutersae406c62007-09-19 17:27:43 +00004384 if (name == NULL)
4385 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004386 cf.cf_flags = 0;
4387 if (PyEval_MergeCompilerFlags(&cf))
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004388 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004389 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004390 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004391 v = PyRun_File(fp, name, Py_file_input, globals,
Tim Peters8a5c3c72004-04-05 19:36:21 +00004392 locals);
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004393 }
4394 else {
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004395 PyObject *tmp = NULL;
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +00004396 char *str;
Tim Peters5ba58662001-07-16 02:29:45 +00004397 PyCompilerFlags cf;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004398 cf.cf_flags = 0;
4399#ifdef Py_USING_UNICODE
4400 if (PyUnicode_Check(prog)) {
4401 tmp = PyUnicode_AsUTF8String(prog);
4402 if (tmp == NULL)
4403 return -1;
4404 prog = tmp;
4405 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4406 }
4407#endif
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004408 if (PyString_AsStringAndSize(prog, &str, NULL))
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004409 return -1;
Tim Peters5ba58662001-07-16 02:29:45 +00004410 if (PyEval_MergeCompilerFlags(&cf))
Tim Peters8a5c3c72004-04-05 19:36:21 +00004411 v = PyRun_StringFlags(str, Py_file_input, globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004412 locals, &cf);
Tim Peters5ba58662001-07-16 02:29:45 +00004413 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +00004414 v = PyRun_String(str, Py_file_input, globals, locals);
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004415 Py_XDECREF(tmp);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004416 }
Guido van Rossuma400d8a2000-01-12 22:45:54 +00004417 if (plain)
4418 PyFrame_LocalsToFast(f, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004419 if (v == NULL)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004420 return -1;
Guido van Rossumb209a111997-04-29 18:18:01 +00004421 Py_DECREF(v);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004422 return 0;
4423}
Guido van Rossum24c13741995-02-14 09:42:43 +00004424
Fredrik Lundh7a830892006-05-27 10:39:48 +00004425static void
Paul Prescode68140d2000-08-30 20:25:01 +00004426format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4427{
4428 char *obj_str;
4429
4430 if (!obj)
4431 return;
4432
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004433 obj_str = PyString_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004434 if (!obj_str)
4435 return;
4436
4437 PyErr_Format(exc, format_str, obj_str);
4438}
Guido van Rossum950361c1997-01-24 13:49:28 +00004439
Fredrik Lundh7a830892006-05-27 10:39:48 +00004440static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004441string_concatenate(PyObject *v, PyObject *w,
4442 PyFrameObject *f, unsigned char *next_instr)
4443{
4444 /* This function implements 'variable += expr' when both arguments
4445 are strings. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004446 Py_ssize_t v_len = PyString_GET_SIZE(v);
4447 Py_ssize_t w_len = PyString_GET_SIZE(w);
Armin Rigo97ff0472006-08-09 15:37:26 +00004448 Py_ssize_t new_len = v_len + w_len;
4449 if (new_len < 0) {
4450 PyErr_SetString(PyExc_OverflowError,
4451 "strings are too large to concat");
4452 return NULL;
4453 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004454
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004455 if (v->ob_refcnt == 2) {
4456 /* In the common case, there are 2 references to the value
4457 * stored in 'variable' when the += is performed: one on the
Thomas Wouterse2176022007-09-20 17:35:10 +00004458 * value stack (in 'v') and one still stored in the
4459 * 'variable'. We try to delete the variable now to reduce
4460 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004461 */
4462 switch (*next_instr) {
4463 case STORE_FAST:
4464 {
4465 int oparg = PEEKARG();
4466 PyObject **fastlocals = f->f_localsplus;
4467 if (GETLOCAL(oparg) == v)
4468 SETLOCAL(oparg, NULL);
4469 break;
4470 }
4471 case STORE_DEREF:
4472 {
Thomas Wouterse2176022007-09-20 17:35:10 +00004473 PyObject **freevars = (f->f_localsplus +
4474 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004475 PyObject *c = freevars[PEEKARG()];
4476 if (PyCell_GET(c) == v)
4477 PyCell_Set(c, NULL);
4478 break;
4479 }
4480 case STORE_NAME:
4481 {
4482 PyObject *names = f->f_code->co_names;
4483 PyObject *name = GETITEM(names, PEEKARG());
4484 PyObject *locals = f->f_locals;
4485 if (PyDict_CheckExact(locals) &&
4486 PyDict_GetItem(locals, name) == v) {
4487 if (PyDict_DelItem(locals, name) != 0) {
4488 PyErr_Clear();
4489 }
4490 }
4491 break;
4492 }
4493 }
4494 }
4495
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004496 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004497 /* Now we own the last reference to 'v', so we can resize it
4498 * in-place.
4499 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004500 if (_PyString_Resize(&v, new_len) != 0) {
4501 /* XXX if _PyString_Resize() fails, 'v' has been
Thomas Wouterse2176022007-09-20 17:35:10 +00004502 * deallocated so it cannot be put back into
4503 * 'variable'. The MemoryError is raised when there
4504 * is no value in 'variable', which might (very
4505 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004506 */
4507 return NULL;
4508 }
4509 /* copy 'w' into the newly allocated area of 'v' */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004510 memcpy(PyString_AS_STRING(v) + v_len,
4511 PyString_AS_STRING(w), w_len);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004512 return v;
4513 }
4514 else {
4515 /* When in-place resizing is not an option. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004516 PyString_Concat(&v, w);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004517 return v;
4518 }
4519}
4520
Guido van Rossum950361c1997-01-24 13:49:28 +00004521#ifdef DYNAMIC_EXECUTION_PROFILE
4522
Fredrik Lundh7a830892006-05-27 10:39:48 +00004523static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004524getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004525{
4526 int i;
4527 PyObject *l = PyList_New(256);
4528 if (l == NULL) return NULL;
4529 for (i = 0; i < 256; i++) {
4530 PyObject *x = PyInt_FromLong(a[i]);
4531 if (x == NULL) {
4532 Py_DECREF(l);
4533 return NULL;
4534 }
4535 PyList_SetItem(l, i, x);
4536 }
4537 for (i = 0; i < 256; i++)
4538 a[i] = 0;
4539 return l;
4540}
4541
4542PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004543_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004544{
4545#ifndef DXPAIRS
4546 return getarray(dxp);
4547#else
4548 int i;
4549 PyObject *l = PyList_New(257);
4550 if (l == NULL) return NULL;
4551 for (i = 0; i < 257; i++) {
4552 PyObject *x = getarray(dxpairs[i]);
4553 if (x == NULL) {
4554 Py_DECREF(l);
4555 return NULL;
4556 }
4557 PyList_SetItem(l, i, x);
4558 }
4559 return l;
4560#endif
4561}
4562
4563#endif