blob: fcf8efb65d2136bee60fa0724aa30aa510904b2d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouters8ce81f72007-09-20 18:22:40 +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
37static void
38ppc_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
Thomas Wouters477c8d52006-05-27 19:21:47 +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
Thomas Wouters477c8d52006-05-27 19:21:47 +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
Thomas Wouters477c8d52006-05-27 19:21:47 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Thomas Wouters477c8d52006-05-27 19:21:47 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +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 Wouters8ce81f72007-09-20 18:22:40 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +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
Guido van Rossumc2e20742006-02-27 22:32:47 +0000105static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
109 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000113static 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
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * cmp_outcome(int, PyObject *, PyObject *);
117static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000118static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000119static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000120static PyObject * unicode_concatenate(PyObject *, PyObject *,
121 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000122
Paul Prescode68140d2000-08-30 20:25:01 +0000123#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000124 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000125#define GLOBAL_NAME_ERROR_MSG \
126 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000127#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000128 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000129#define UNBOUNDFREE_ERROR_MSG \
130 "free variable '%.200s' referenced before assignment" \
131 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000132
Guido van Rossum950361c1997-01-24 13:49:28 +0000133/* Dynamic execution profile */
134#ifdef DYNAMIC_EXECUTION_PROFILE
135#ifdef DXPAIRS
136static long dxpairs[257][256];
137#define dxp dxpairs[256]
138#else
139static long dxp[256];
140#endif
141#endif
142
Jeremy Hylton985eba52003-02-05 23:13:00 +0000143/* Function call profile */
144#ifdef CALL_PROFILE
145#define PCALL_NUM 11
146static int pcall[PCALL_NUM];
147
148#define PCALL_ALL 0
149#define PCALL_FUNCTION 1
150#define PCALL_FAST_FUNCTION 2
151#define PCALL_FASTER_FUNCTION 3
152#define PCALL_METHOD 4
153#define PCALL_BOUND_METHOD 5
154#define PCALL_CFUNCTION 6
155#define PCALL_TYPE 7
156#define PCALL_GENERATOR 8
157#define PCALL_OTHER 9
158#define PCALL_POP 10
159
160/* Notes about the statistics
161
162 PCALL_FAST stats
163
164 FAST_FUNCTION means no argument tuple needs to be created.
165 FASTER_FUNCTION means that the fast-path frame setup code is used.
166
167 If there is a method call where the call can be optimized by changing
168 the argument tuple and calling the function directly, it gets recorded
169 twice.
170
171 As a result, the relationship among the statistics appears to be
172 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
173 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
174 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
175 PCALL_METHOD > PCALL_BOUND_METHOD
176*/
177
178#define PCALL(POS) pcall[POS]++
179
180PyObject *
181PyEval_GetCallStats(PyObject *self)
182{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000183 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000184 pcall[0], pcall[1], pcall[2], pcall[3],
185 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000187}
188#else
189#define PCALL(O)
190
191PyObject *
192PyEval_GetCallStats(PyObject *self)
193{
194 Py_INCREF(Py_None);
195 return Py_None;
196}
197#endif
198
Tim Peters5ca576e2001-06-18 22:08:13 +0000199
Guido van Rossume59214e1994-08-30 08:01:59 +0000200#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000201
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000202#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000203#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000204#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000205#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000206
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000207static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000208static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000209static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000210
Tim Peters7f468f22004-10-11 02:40:51 +0000211int
212PyEval_ThreadsInitialized(void)
213{
214 return interpreter_lock != 0;
215}
216
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000218PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000221 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000222 interpreter_lock = PyThread_allocate_lock();
223 PyThread_acquire_lock(interpreter_lock, 1);
224 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000225}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000226
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000230 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231}
232
233void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000234PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000236 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237}
238
239void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000241{
242 if (tstate == NULL)
243 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000244 /* Check someone has called PyEval_InitThreads() to create the lock */
245 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000247 if (PyThreadState_Swap(tstate) != NULL)
248 Py_FatalError(
249 "PyEval_AcquireThread: non-NULL old thread state");
250}
251
252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000254{
255 if (tstate == NULL)
256 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
257 if (PyThreadState_Swap(NULL) != tstate)
258 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000259 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000260}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000261
262/* This function is called from PyOS_AfterFork to ensure that newly
263 created child processes don't hold locks referring to threads which
264 are not running in the child process. (This could also be done using
265 pthread_atfork mechanism, at least for the pthreads implementation.) */
266
267void
268PyEval_ReInitThreads(void)
269{
Jesse Nollera8513972008-07-17 16:49:17 +0000270 PyObject *threading, *result;
271 PyThreadState *tstate;
272
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000273 if (!interpreter_lock)
274 return;
275 /*XXX Can't use PyThread_free_lock here because it does too
276 much error-checking. Doing this cleanly would require
277 adding a new function to each thread_*.h. Instead, just
278 create a new lock and waste a little bit of memory */
279 interpreter_lock = PyThread_allocate_lock();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000280 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000281 PyThread_acquire_lock(interpreter_lock, 1);
282 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000283
284 /* Update the threading module with the new state.
285 */
286 tstate = PyThreadState_GET();
287 threading = PyMapping_GetItemString(tstate->interp->modules,
288 "threading");
289 if (threading == NULL) {
290 /* threading not imported */
291 PyErr_Clear();
292 return;
293 }
294 result = PyObject_CallMethod(threading, "_after_fork", NULL);
295 if (result == NULL)
296 PyErr_WriteUnraisable(threading);
297 else
298 Py_DECREF(result);
299 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000300}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301#endif
302
Guido van Rossumff4949e1992-08-05 19:58:53 +0000303/* Functions save_thread and restore_thread are always defined so
304 dynamically loaded modules needn't be compiled separately for use
305 with and without threads: */
306
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000307PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000308PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000310 PyThreadState *tstate = PyThreadState_Swap(NULL);
311 if (tstate == NULL)
312 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000313#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000314 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000315 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000316#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000317 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318}
319
320void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000321PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000323 if (tstate == NULL)
324 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000325#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000326 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000327 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000328 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000329 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330 }
331#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000332 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000333}
334
335
Guido van Rossuma9672091994-09-14 13:31:22 +0000336/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
337 signal handlers or Mac I/O completion routines) can schedule calls
338 to a function to be called synchronously.
339 The synchronous function is called with one void* argument.
340 It should return 0 for success or -1 for failure -- failure should
341 be accompanied by an exception.
342
343 If registry succeeds, the registry function returns 0; if it fails
344 (e.g. due to too many pending calls) it returns -1 (without setting
345 an exception condition).
346
347 Note that because registry may occur from within signal handlers,
348 or other asynchronous events, calling malloc() is unsafe!
349
350#ifdef WITH_THREAD
351 Any thread can schedule pending calls, but only the main thread
352 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000353 There is no facility to schedule calls to a particular thread, but
354 that should be easy to change, should that ever be required. In
355 that case, the static variables here should go into the python
356 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000357#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000358*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000359
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000360#ifdef WITH_THREAD
361
362/* The WITH_THREAD implementation is thread-safe. It allows
363 scheduling to be made from any thread, and even from an executing
364 callback.
365 */
366
367#define NPENDINGCALLS 32
368static struct {
369 int (*func)(void *);
370 void *arg;
371} pendingcalls[NPENDINGCALLS];
372static int pendingfirst = 0;
373static int pendinglast = 0;
374static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
375static char pendingbusy = 0;
376
377int
378Py_AddPendingCall(int (*func)(void *), void *arg)
379{
380 int i, j, result=0;
381 PyThread_type_lock lock = pending_lock;
382
383 /* try a few times for the lock. Since this mechanism is used
384 * for signal handling (on the main thread), there is a (slim)
385 * chance that a signal is delivered on the same thread while we
386 * hold the lock during the Py_MakePendingCalls() function.
387 * This avoids a deadlock in that case.
388 * Note that signals can be delivered on any thread. In particular,
389 * on Windows, a SIGINT is delivered on a system-created worker
390 * thread.
391 * We also check for lock being NULL, in the unlikely case that
392 * this function is called before any bytecode evaluation takes place.
393 */
394 if (lock != NULL) {
395 for (i = 0; i<100; i++) {
396 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
397 break;
398 }
399 if (i == 100)
400 return -1;
401 }
402
403 i = pendinglast;
404 j = (i + 1) % NPENDINGCALLS;
405 if (j == pendingfirst) {
406 result = -1; /* Queue full */
407 } else {
408 pendingcalls[i].func = func;
409 pendingcalls[i].arg = arg;
410 pendinglast = j;
411 }
412 /* signal main loop */
413 _Py_Ticker = 0;
414 pendingcalls_to_do = 1;
415 if (lock != NULL)
416 PyThread_release_lock(lock);
417 return result;
418}
419
420int
421Py_MakePendingCalls(void)
422{
423 int i;
424 int r = 0;
425
426 if (!pending_lock) {
427 /* initial allocation of the lock */
428 pending_lock = PyThread_allocate_lock();
429 if (pending_lock == NULL)
430 return -1;
431 }
432
433 /* only service pending calls on main thread */
434 if (main_thread && PyThread_get_thread_ident() != main_thread)
435 return 0;
436 /* don't perform recursive pending calls */
437 if (pendingbusy)
438 return 0;
439 pendingbusy = 1;
440 /* perform a bounded number of calls, in case of recursion */
441 for (i=0; i<NPENDINGCALLS; i++) {
442 int j;
443 int (*func)(void *);
Benjamin Petersonb4ddfa42009-01-17 23:46:54 +0000444 void *arg = NULL;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000445
446 /* pop one item off the queue while holding the lock */
447 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
448 j = pendingfirst;
449 if (j == pendinglast) {
450 func = NULL; /* Queue empty */
451 } else {
452 func = pendingcalls[j].func;
453 arg = pendingcalls[j].arg;
454 pendingfirst = (j + 1) % NPENDINGCALLS;
455 }
456 pendingcalls_to_do = pendingfirst != pendinglast;
457 PyThread_release_lock(pending_lock);
458 /* having released the lock, perform the callback */
459 if (func == NULL)
460 break;
461 r = func(arg);
462 if (r)
463 break;
464 }
465 pendingbusy = 0;
466 return r;
467}
468
469#else /* if ! defined WITH_THREAD */
470
471/*
472 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
473 This code is used for signal handling in python that isn't built
474 with WITH_THREAD.
475 Don't use this implementation when Py_AddPendingCalls() can happen
476 on a different thread!
477
Guido van Rossuma9672091994-09-14 13:31:22 +0000478 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479 (1) nested asynchronous calls to Py_AddPendingCall()
480 (2) AddPendingCall() calls made while pending calls are being processed.
481
482 (1) is very unlikely because typically signal delivery
483 is blocked during signal handling. So it should be impossible.
484 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000485 The current code is safe against (2), but not against (1).
486 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000487 thread is present, interrupted by signals, and that the critical
488 section is protected with the "busy" variable. On Windows, which
489 delivers SIGINT on a system thread, this does not hold and therefore
490 Windows really shouldn't use this version.
491 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000492*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000493
Guido van Rossuma9672091994-09-14 13:31:22 +0000494#define NPENDINGCALLS 32
495static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000496 int (*func)(void *);
497 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000498} pendingcalls[NPENDINGCALLS];
499static volatile int pendingfirst = 0;
500static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000502
503int
Thomas Wouters334fb892000-07-25 12:56:38 +0000504Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000505{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000506 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000507 int i, j;
508 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000509 if (busy)
510 return -1;
511 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000512 i = pendinglast;
513 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000514 if (j == pendingfirst) {
515 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000516 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000517 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000518 pendingcalls[i].func = func;
519 pendingcalls[i].arg = arg;
520 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000521
522 _Py_Ticker = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000523 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000524 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000525 /* XXX End critical section */
526 return 0;
527}
528
Guido van Rossum180d7b41994-09-29 09:45:57 +0000529int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000530Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000531{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000532 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000533 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000534 return 0;
535 busy = 1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000537 for (;;) {
538 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000539 int (*func)(void *);
540 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000541 i = pendingfirst;
542 if (i == pendinglast)
543 break; /* Queue empty */
544 func = pendingcalls[i].func;
545 arg = pendingcalls[i].arg;
546 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000547 if (func(arg) < 0) {
548 busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000550 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000551 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000552 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000553 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000554 return 0;
555}
556
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000557#endif /* WITH_THREAD */
558
Guido van Rossuma9672091994-09-14 13:31:22 +0000559
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000560/* The interpreter's recursion limit */
561
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000562#ifndef Py_DEFAULT_RECURSION_LIMIT
563#define Py_DEFAULT_RECURSION_LIMIT 1000
564#endif
565static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
566int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000567
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000568int
569Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000570{
571 return recursion_limit;
572}
573
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000574void
575Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000576{
577 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000578 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000579}
580
Armin Rigo2b3eb402003-10-28 12:05:48 +0000581/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
582 if the recursion_depth reaches _Py_CheckRecursionLimit.
583 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
584 to guarantee that _Py_CheckRecursiveCall() is regularly called.
585 Without USE_STACKCHECK, there is no need for this. */
586int
587_Py_CheckRecursiveCall(char *where)
588{
589 PyThreadState *tstate = PyThreadState_GET();
590
591#ifdef USE_STACKCHECK
592 if (PyOS_CheckStack()) {
593 --tstate->recursion_depth;
594 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
595 return -1;
596 }
597#endif
Antoine Pitrou658fad82008-09-03 18:34:34 +0000598 _Py_CheckRecursionLimit = recursion_limit;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000599 if (tstate->recursion_critical)
600 /* Somebody asked that we don't check for recursion. */
601 return 0;
602 if (tstate->overflowed) {
603 if (tstate->recursion_depth > recursion_limit + 50) {
604 /* Overflowing while handling an overflow. Give up. */
605 Py_FatalError("Cannot recover from stack overflow.");
606 }
607 return 0;
608 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000609 if (tstate->recursion_depth > recursion_limit) {
610 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000611 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000612 PyErr_Format(PyExc_RuntimeError,
613 "maximum recursion depth exceeded%s",
614 where);
615 return -1;
616 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000617 return 0;
618}
619
Guido van Rossum374a9221991-04-04 10:40:29 +0000620/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000621enum why_code {
622 WHY_NOT = 0x0001, /* No error */
623 WHY_EXCEPTION = 0x0002, /* Exception occurred */
624 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
625 WHY_RETURN = 0x0008, /* 'return' statement */
626 WHY_BREAK = 0x0010, /* 'break' statement */
627 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000628 WHY_YIELD = 0x0040, /* 'yield' operator */
629 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000630};
Guido van Rossum374a9221991-04-04 10:40:29 +0000631
Collin Winter828f04a2007-08-31 00:04:24 +0000632static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000633static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000634
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000635/* Records whether tracing is on for any thread. Counts the number of
636 threads for which tstate->c_tracefunc is non-NULL, so if the value
637 is 0, we know we don't have to check this thread's c_tracefunc.
638 This speeds up the if statement in PyEval_EvalFrameEx() after
639 fast_next_opcode*/
640static int _Py_TracingPossible = 0;
641
Skip Montanarod581d772002-09-03 20:10:45 +0000642/* for manipulating the thread switch and periodic "stuff" - used to be
643 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000644int _Py_CheckInterval = 100;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000645volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000646
Guido van Rossumb209a111997-04-29 18:18:01 +0000647PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000648PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000649{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000650 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000651 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000652 (PyObject **)NULL, 0,
653 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000654 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000655 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000656}
657
658
659/* Interpreter main loop */
660
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000661PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000662PyEval_EvalFrame(PyFrameObject *f) {
663 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000664 used this API; core interpreter code should call
665 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000666 return PyEval_EvalFrameEx(f, 0);
667}
668
669PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000671{
Guido van Rossum950361c1997-01-24 13:49:28 +0000672#ifdef DXPAIRS
673 int lastopcode = 0;
674#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000675 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000676 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000677 register int opcode; /* Current opcode */
678 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000679 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000680 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000681 register PyObject *x; /* Result object -- NULL if error */
682 register PyObject *v; /* Temporary objects popped off stack */
683 register PyObject *w;
684 register PyObject *u;
685 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000686 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000687 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000688 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000689 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000690
Tim Peters8a5c3c72004-04-05 19:36:21 +0000691 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000692
693 not (instr_lb <= current_bytecode_offset < instr_ub)
694
Tim Peters8a5c3c72004-04-05 19:36:21 +0000695 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000696 initial values are such as to make this false the first
697 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000698 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000699
Guido van Rossumd076c731998-10-07 19:42:25 +0000700 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000701 PyObject *names;
702 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000703#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000704 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000705 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000706#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000707
Neal Norwitza81d2202002-07-14 00:27:26 +0000708/* Tuple access macros */
709
710#ifndef Py_DEBUG
711#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
712#else
713#define GETITEM(v, i) PyTuple_GetItem((v), (i))
714#endif
715
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000716#ifdef WITH_TSC
717/* Use Pentium timestamp counter to mark certain events:
718 inst0 -- beginning of switch statement for opcode dispatch
719 inst1 -- end of switch statement (may be skipped)
720 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000721 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000722 (may be skipped)
723 intr1 -- beginning of long interruption
724 intr2 -- end of long interruption
725
726 Many opcodes call out to helper C functions. In some cases, the
727 time in those functions should be counted towards the time for the
728 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
729 calls another Python function; there's no point in charge all the
730 bytecode executed by the called function to the caller.
731
732 It's hard to make a useful judgement statically. In the presence
733 of operator overloading, it's impossible to tell if a call will
734 execute new Python code or not.
735
736 It's a case-by-case judgement. I'll use intr1 for the following
737 cases:
738
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000739 IMPORT_STAR
740 IMPORT_FROM
741 CALL_FUNCTION (and friends)
742
743 */
744 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
745 int ticked = 0;
746
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000747 READ_TIMESTAMP(inst0);
748 READ_TIMESTAMP(inst1);
749 READ_TIMESTAMP(loop0);
750 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000751
752 /* shut up the compiler */
753 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000754#endif
755
Guido van Rossum374a9221991-04-04 10:40:29 +0000756/* Code access macros */
757
Martin v. Löwis18e16552006-02-15 17:27:45 +0000758#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000759#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000760#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000761#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000762#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000763#define JUMPBY(x) (next_instr += (x))
764
Raymond Hettingerf606f872003-03-16 03:11:04 +0000765/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000766 Some opcodes tend to come in pairs thus making it possible to
767 predict the second code when the first is run. For example,
768 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
769 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000770
Georg Brandl86b2fb92008-07-16 03:43:04 +0000771 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000772 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000773 processor's own internal branch predication has a high likelihood of
774 success, resulting in a nearly zero-overhead transition to the
775 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000776 including its two unpredictable branches, the HAS_ARG test and the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000777 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000778 a successful PREDICT has the effect of making the two opcodes run as if
Georg Brandl86b2fb92008-07-16 03:43:04 +0000779 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000780
Georg Brandl86b2fb92008-07-16 03:43:04 +0000781 If collecting opcode statistics, your choices are to either keep the
782 predictions turned-on and interpret the results as if some opcodes
783 had been combined or turn-off predictions so that the opcode frequency
784 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000785*/
786
Raymond Hettingera7216982004-02-08 19:59:27 +0000787#ifdef DYNAMIC_EXECUTION_PROFILE
788#define PREDICT(op) if (0) goto PRED_##op
789#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000790#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000791#endif
792
Raymond Hettingerf606f872003-03-16 03:11:04 +0000793#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000794#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000795
Guido van Rossum374a9221991-04-04 10:40:29 +0000796/* Stack manipulation macros */
797
Martin v. Löwis18e16552006-02-15 17:27:45 +0000798/* The stack can grow at most MAXINT deep, as co_nlocals and
799 co_stacksize are ints. */
800#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000801#define EMPTY() (STACK_LEVEL() == 0)
802#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000803#define SECOND() (stack_pointer[-2])
804#define THIRD() (stack_pointer[-3])
805#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000806#define SET_TOP(v) (stack_pointer[-1] = (v))
807#define SET_SECOND(v) (stack_pointer[-2] = (v))
808#define SET_THIRD(v) (stack_pointer[-3] = (v))
809#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000810#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000811#define BASIC_PUSH(v) (*stack_pointer++ = (v))
812#define BASIC_POP() (*--stack_pointer)
813
Guido van Rossum96a42c81992-01-12 02:29:51 +0000814#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000815#define PUSH(v) { (void)(BASIC_PUSH(v), \
816 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000817 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000818#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
819 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000820#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
821 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000822 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000823#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
824 prtrace((STACK_POINTER)[-1], "ext_pop")), \
825 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000826#else
827#define PUSH(v) BASIC_PUSH(v)
828#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000829#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000830#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000831#endif
832
Guido van Rossum681d79a1995-07-18 14:51:37 +0000833/* Local variable macros */
834
835#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000836
837/* The SETLOCAL() macro must not DECREF the local variable in-place and
838 then store the new value; it must copy the old value to a temporary
839 value, then store the new value, and then DECREF the temporary value.
840 This is because it is possible that during the DECREF the frame is
841 accessed by other code (e.g. a __del__ method or gc.collect()) and the
842 variable would be pointing to already-freed memory. */
843#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
844 GETLOCAL(i) = value; \
845 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000846
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000847
848#define UNWIND_BLOCK(b) \
849 while (STACK_LEVEL() > (b)->b_level) { \
850 PyObject *v = POP(); \
851 Py_XDECREF(v); \
852 }
853
854#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000855 { \
856 PyObject *type, *value, *traceback; \
857 assert(STACK_LEVEL() >= (b)->b_level + 3); \
858 while (STACK_LEVEL() > (b)->b_level + 3) { \
859 value = POP(); \
860 Py_XDECREF(value); \
861 } \
862 type = tstate->exc_type; \
863 value = tstate->exc_value; \
864 traceback = tstate->exc_traceback; \
865 tstate->exc_type = POP(); \
866 tstate->exc_value = POP(); \
867 tstate->exc_traceback = POP(); \
868 Py_XDECREF(type); \
869 Py_XDECREF(value); \
870 Py_XDECREF(traceback); \
871 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000872
873#define SAVE_EXC_STATE() \
874 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000875 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000876 Py_XINCREF(tstate->exc_type); \
877 Py_XINCREF(tstate->exc_value); \
878 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000879 type = f->f_exc_type; \
880 value = f->f_exc_value; \
881 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000882 f->f_exc_type = tstate->exc_type; \
883 f->f_exc_value = tstate->exc_value; \
884 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000885 Py_XDECREF(type); \
886 Py_XDECREF(value); \
887 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000888 }
889
890#define SWAP_EXC_STATE() \
891 { \
892 PyObject *tmp; \
893 tmp = tstate->exc_type; \
894 tstate->exc_type = f->f_exc_type; \
895 f->f_exc_type = tmp; \
896 tmp = tstate->exc_value; \
897 tstate->exc_value = f->f_exc_value; \
898 f->f_exc_value = tmp; \
899 tmp = tstate->exc_traceback; \
900 tstate->exc_traceback = f->f_exc_traceback; \
901 f->f_exc_traceback = tmp; \
902 }
903
Guido van Rossuma027efa1997-05-05 20:56:21 +0000904/* Start of code */
905
Tim Peters5ca576e2001-06-18 22:08:13 +0000906 if (f == NULL)
907 return NULL;
908
Armin Rigo1d313ab2003-10-25 14:33:09 +0000909 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000910 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +0000911 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000912
Tim Peters5ca576e2001-06-18 22:08:13 +0000913 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000914
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000915 if (tstate->use_tracing) {
916 if (tstate->c_tracefunc != NULL) {
917 /* tstate->c_tracefunc, if defined, is a
918 function that will be called on *every* entry
919 to a code block. Its return value, if not
920 None, is a function that will be called at
921 the start of each executed line of code.
922 (Actually, the function must return itself
923 in order to continue tracing.) The trace
924 functions are called with three arguments:
925 a pointer to the current frame, a string
926 indicating why the function is called, and
927 an argument which depends on the situation.
928 The global trace function is also called
929 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000930 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000931 tstate->c_traceobj,
932 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000933 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000934 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000935 }
936 }
937 if (tstate->c_profilefunc != NULL) {
938 /* Similar for c_profilefunc, except it needn't
939 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000940 if (call_trace_protected(tstate->c_profilefunc,
941 tstate->c_profileobj,
942 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000943 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +0000944 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000945 }
946 }
947 }
948
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000949 co = f->f_code;
950 names = co->co_names;
951 consts = co->co_consts;
952 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +0000954 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000955 /* An explanation is in order for the next line.
956
957 f->f_lasti now refers to the index of the last instruction
958 executed. You might think this was obvious from the name, but
959 this wasn't always true before 2.3! PyFrame_New now sets
960 f->f_lasti to -1 (i.e. the index *before* the first instruction)
961 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000962 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000963
964 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000965 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000966 prediction effectively links the two codes together as if they
967 were a single new opcode; accordingly,f->f_lasti will point to
968 the first code in the pair (for instance, GET_ITER followed by
969 FOR_ITER is effectively a single opcode and f->f_lasti will point
970 at to the beginning of the combined pair.)
971 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000972 next_instr = first_instr + f->f_lasti + 1;
973 stack_pointer = f->f_stacktop;
974 assert(stack_pointer != NULL);
975 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
976
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000977 if (f->f_code->co_flags & CO_GENERATOR) {
978 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
979 /* We were in an except handler when we left,
980 restore the exception state which was put aside
981 (see YIELD_VALUE). */
982 SWAP_EXC_STATE();
983 }
984 else {
985 SAVE_EXC_STATE();
986 }
987 }
988
Tim Peters5ca576e2001-06-18 22:08:13 +0000989#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000991#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000992#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000993 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000994#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000995
Guido van Rossum374a9221991-04-04 10:40:29 +0000996 why = WHY_NOT;
997 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +0000998 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +0000999 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001000
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001001 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001002 why = WHY_EXCEPTION;
1003 goto on_error;
1004 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005
Guido van Rossum374a9221991-04-04 10:40:29 +00001006 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001007#ifdef WITH_TSC
1008 if (inst1 == 0) {
1009 /* Almost surely, the opcode executed a break
1010 or a continue, preventing inst1 from being set
1011 on the way out of the loop.
1012 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001013 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001014 loop1 = inst1;
1015 }
1016 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1017 intr0, intr1);
1018 ticked = 0;
1019 inst1 = 0;
1020 intr0 = 0;
1021 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001022 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001023#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001024 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001025 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001026
Guido van Rossuma027efa1997-05-05 20:56:21 +00001027 /* Do periodic things. Doing this every time through
1028 the loop would add too much overhead, so we do it
1029 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001030 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001031 event needs attention (e.g. a signal handler or
1032 async I/O handler); see Py_AddPendingCall() and
1033 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001034
Skip Montanarod581d772002-09-03 20:10:45 +00001035 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001036 if (*next_instr == SETUP_FINALLY) {
1037 /* Make the last opcode before
1038 a try: finally: block uninterruptable. */
1039 goto fast_next_opcode;
1040 }
Skip Montanarod581d772002-09-03 20:10:45 +00001041 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001042 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001043#ifdef WITH_TSC
1044 ticked = 1;
1045#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001046 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001047 if (Py_MakePendingCalls() < 0) {
1048 why = WHY_EXCEPTION;
1049 goto on_error;
1050 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001051 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +00001052 /* MakePendingCalls() didn't succeed.
1053 Force early re-execution of this
1054 "periodic" code, possibly after
1055 a thread switch */
1056 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +00001057 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001058#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001059 if (interpreter_lock) {
1060 /* Give another thread a chance */
1061
Guido van Rossum25ce5661997-08-02 03:10:38 +00001062 if (PyThreadState_Swap(NULL) != tstate)
1063 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +00001064 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001065
1066 /* Other threads may run now */
1067
Guido van Rossum65d5b571998-12-21 19:32:43 +00001068 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001069 if (PyThreadState_Swap(tstate) != NULL)
1070 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001071
1072 /* Check for thread interrupts */
1073
1074 if (tstate->async_exc != NULL) {
1075 x = tstate->async_exc;
1076 tstate->async_exc = NULL;
1077 PyErr_SetNone(x);
1078 Py_DECREF(x);
1079 why = WHY_EXCEPTION;
1080 goto on_error;
1081 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001082 }
1083#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001084 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001085
Neil Schemenauer63543862002-02-17 19:10:14 +00001086 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001087 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001088
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001089 /* line-by-line tracing support */
1090
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001091 if (_Py_TracingPossible &&
1092 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001093 /* see maybe_call_line_trace
1094 for expository comments */
1095 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001096
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001097 err = maybe_call_line_trace(tstate->c_tracefunc,
1098 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001099 f, &instr_lb, &instr_ub,
1100 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001101 /* Reload possibly changed frame fields */
1102 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001103 if (f->f_stacktop != NULL) {
1104 stack_pointer = f->f_stacktop;
1105 f->f_stacktop = NULL;
1106 }
1107 if (err) {
1108 /* trace function raised an exception */
1109 goto on_error;
1110 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001111 }
1112
1113 /* Extract opcode and argument */
1114
Guido van Rossum374a9221991-04-04 10:40:29 +00001115 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001116 oparg = 0; /* allows oparg to be stored in a register because
1117 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001118 if (HAS_ARG(opcode))
1119 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001120 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001121#ifdef DYNAMIC_EXECUTION_PROFILE
1122#ifdef DXPAIRS
1123 dxpairs[lastopcode][opcode]++;
1124 lastopcode = opcode;
1125#endif
1126 dxp[opcode]++;
1127#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001128
Guido van Rossum96a42c81992-01-12 02:29:51 +00001129#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001130 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001131
Guido van Rossum96a42c81992-01-12 02:29:51 +00001132 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001133 if (HAS_ARG(opcode)) {
1134 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001135 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001136 }
1137 else {
1138 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001139 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001140 }
1141 }
1142#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001143
Guido van Rossum374a9221991-04-04 10:40:29 +00001144 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001145 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001146
Guido van Rossum374a9221991-04-04 10:40:29 +00001147 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001148
Guido van Rossum374a9221991-04-04 10:40:29 +00001149 /* BEWARE!
1150 It is essential that any operation that fails sets either
1151 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1152 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001153
Guido van Rossum374a9221991-04-04 10:40:29 +00001154 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001155
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001156 case NOP:
1157 goto fast_next_opcode;
1158
Neil Schemenauer63543862002-02-17 19:10:14 +00001159 case LOAD_FAST:
1160 x = GETLOCAL(oparg);
1161 if (x != NULL) {
1162 Py_INCREF(x);
1163 PUSH(x);
1164 goto fast_next_opcode;
1165 }
1166 format_exc_check_arg(PyExc_UnboundLocalError,
1167 UNBOUNDLOCAL_ERROR_MSG,
1168 PyTuple_GetItem(co->co_varnames, oparg));
1169 break;
1170
1171 case LOAD_CONST:
Skip Montanaro04d80f82002-08-04 21:03:35 +00001172 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001173 Py_INCREF(x);
1174 PUSH(x);
1175 goto fast_next_opcode;
1176
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001177 PREDICTED_WITH_ARG(STORE_FAST);
Neil Schemenauer63543862002-02-17 19:10:14 +00001178 case STORE_FAST:
1179 v = POP();
1180 SETLOCAL(oparg, v);
1181 goto fast_next_opcode;
1182
Raymond Hettingerf606f872003-03-16 03:11:04 +00001183 PREDICTED(POP_TOP);
Guido van Rossum374a9221991-04-04 10:40:29 +00001184 case POP_TOP:
1185 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001186 Py_DECREF(v);
Neil Schemenauer63543862002-02-17 19:10:14 +00001187 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001188
Guido van Rossum374a9221991-04-04 10:40:29 +00001189 case ROT_TWO:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001190 v = TOP();
1191 w = SECOND();
1192 SET_TOP(w);
1193 SET_SECOND(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001194 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001195
Guido van Rossum374a9221991-04-04 10:40:29 +00001196 case ROT_THREE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001197 v = TOP();
1198 w = SECOND();
1199 x = THIRD();
1200 SET_TOP(w);
1201 SET_SECOND(x);
1202 SET_THIRD(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001203 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001204
Thomas Wouters434d0822000-08-24 20:11:32 +00001205 case ROT_FOUR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001206 u = TOP();
1207 v = SECOND();
1208 w = THIRD();
1209 x = FOURTH();
1210 SET_TOP(v);
1211 SET_SECOND(w);
1212 SET_THIRD(x);
1213 SET_FOURTH(u);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001214 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Guido van Rossum374a9221991-04-04 10:40:29 +00001216 case DUP_TOP:
1217 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001218 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001219 PUSH(v);
Raymond Hettinger080cb322003-03-14 01:37:42 +00001220 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Thomas Wouters434d0822000-08-24 20:11:32 +00001222 case DUP_TOPX:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001223 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001224 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001225 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001226 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001227 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001228 STACKADJ(2);
1229 SET_TOP(x);
1230 SET_SECOND(w);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001231 goto fast_next_opcode;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001232 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001233 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001234 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001235 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001236 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001237 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001238 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001239 STACKADJ(3);
1240 SET_TOP(x);
1241 SET_SECOND(w);
1242 SET_THIRD(v);
Raymond Hettingerf606f872003-03-16 03:11:04 +00001243 goto fast_next_opcode;
Thomas Wouters434d0822000-08-24 20:11:32 +00001244 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001245 Py_FatalError("invalid argument to DUP_TOPX"
1246 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001247 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001248 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001249
Guido van Rossum374a9221991-04-04 10:40:29 +00001250 case UNARY_POSITIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001251 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001252 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001253 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001254 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001255 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001256 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001257
Guido van Rossum374a9221991-04-04 10:40:29 +00001258 case UNARY_NEGATIVE:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001259 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001260 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001261 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001262 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001263 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001264 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Guido van Rossum374a9221991-04-04 10:40:29 +00001266 case UNARY_NOT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001267 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001268 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001269 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001270 if (err == 0) {
1271 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001272 SET_TOP(Py_True);
Guido van Rossumfc490731997-05-06 15:06:49 +00001273 continue;
1274 }
1275 else if (err > 0) {
1276 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001277 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001278 err = 0;
1279 continue;
1280 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001281 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001282 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001283
Guido van Rossum7928cd71991-10-24 14:59:31 +00001284 case UNARY_INVERT:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001285 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001286 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001287 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001288 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001289 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001290 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001291
Guido van Rossum50564e81996-01-12 01:13:16 +00001292 case BINARY_POWER:
1293 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001294 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001295 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001296 Py_DECREF(v);
1297 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001298 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001299 if (x != NULL) continue;
Guido van Rossum50564e81996-01-12 01:13:16 +00001300 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001301
Guido van Rossum374a9221991-04-04 10:40:29 +00001302 case BINARY_MULTIPLY:
1303 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001304 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001305 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001306 Py_DECREF(v);
1307 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001308 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001309 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001310 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001311
Tim Peters3caca232001-12-06 06:23:26 +00001312 case BINARY_TRUE_DIVIDE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001313 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001314 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001315 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001316 Py_DECREF(v);
1317 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001318 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001319 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001320 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Guido van Rossum4668b002001-08-08 05:00:18 +00001322 case BINARY_FLOOR_DIVIDE:
1323 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001324 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001325 x = PyNumber_FloorDivide(v, w);
1326 Py_DECREF(v);
1327 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001328 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001329 if (x != NULL) continue;
1330 break;
1331
Guido van Rossum374a9221991-04-04 10:40:29 +00001332 case BINARY_MODULO:
1333 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001334 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001335 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001336 Py_DECREF(v);
1337 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001339 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001340 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001341
Guido van Rossum374a9221991-04-04 10:40:29 +00001342 case BINARY_ADD:
1343 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001344 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001345 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001346 PyUnicode_CheckExact(w)) {
1347 x = unicode_concatenate(v, w, f, next_instr);
1348 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001349 goto skip_decref_vx;
1350 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001351 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001352 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001353 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001354 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001355 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001356 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001357 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001358 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001359 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Guido van Rossum374a9221991-04-04 10:40:29 +00001361 case BINARY_SUBTRACT:
1362 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001363 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001364 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001365 Py_DECREF(v);
1366 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001367 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001368 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001369 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Guido van Rossum374a9221991-04-04 10:40:29 +00001371 case BINARY_SUBSCR:
1372 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001373 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001374 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001375 Py_DECREF(v);
1376 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001378 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001379 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Guido van Rossum7928cd71991-10-24 14:59:31 +00001381 case BINARY_LSHIFT:
1382 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001383 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001384 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001385 Py_DECREF(v);
1386 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001387 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001388 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001389 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001390
Guido van Rossum7928cd71991-10-24 14:59:31 +00001391 case BINARY_RSHIFT:
1392 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001393 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001394 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001395 Py_DECREF(v);
1396 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001397 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001398 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001399 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Guido van Rossum7928cd71991-10-24 14:59:31 +00001401 case BINARY_AND:
1402 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001403 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001404 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001405 Py_DECREF(v);
1406 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001407 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001408 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001409 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Guido van Rossum7928cd71991-10-24 14:59:31 +00001411 case BINARY_XOR:
1412 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001413 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001414 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001415 Py_DECREF(v);
1416 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001417 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001418 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001419 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Guido van Rossum7928cd71991-10-24 14:59:31 +00001421 case BINARY_OR:
1422 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001423 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001424 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001425 Py_DECREF(v);
1426 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001427 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001428 if (x != NULL) continue;
Guido van Rossum7928cd71991-10-24 14:59:31 +00001429 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001430
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001431 case LIST_APPEND:
1432 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001433 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001434 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001435 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001436 if (err == 0) {
1437 PREDICT(JUMP_ABSOLUTE);
1438 continue;
1439 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001440 break;
1441
Nick Coghlan650f0d02007-04-15 12:05:43 +00001442 case SET_ADD:
1443 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001444 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001445 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001446 Py_DECREF(w);
1447 if (err == 0) {
1448 PREDICT(JUMP_ABSOLUTE);
1449 continue;
1450 }
1451 break;
1452
Thomas Wouters434d0822000-08-24 20:11:32 +00001453 case INPLACE_POWER:
1454 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001455 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001456 x = PyNumber_InPlacePower(v, w, Py_None);
1457 Py_DECREF(v);
1458 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001459 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001460 if (x != NULL) continue;
1461 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001462
Thomas Wouters434d0822000-08-24 20:11:32 +00001463 case INPLACE_MULTIPLY:
1464 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001465 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001466 x = PyNumber_InPlaceMultiply(v, w);
1467 Py_DECREF(v);
1468 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001469 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001470 if (x != NULL) continue;
1471 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Tim Peters54b11912001-12-25 18:49:11 +00001473 case INPLACE_TRUE_DIVIDE:
Thomas Wouters434d0822000-08-24 20:11:32 +00001474 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001475 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001476 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001477 Py_DECREF(v);
1478 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001479 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001480 if (x != NULL) continue;
1481 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Guido van Rossum4668b002001-08-08 05:00:18 +00001483 case INPLACE_FLOOR_DIVIDE:
1484 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001485 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001486 x = PyNumber_InPlaceFloorDivide(v, w);
1487 Py_DECREF(v);
1488 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001489 SET_TOP(x);
Guido van Rossum4668b002001-08-08 05:00:18 +00001490 if (x != NULL) continue;
1491 break;
1492
Thomas Wouters434d0822000-08-24 20:11:32 +00001493 case INPLACE_MODULO:
1494 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001495 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001496 x = PyNumber_InPlaceRemainder(v, w);
1497 Py_DECREF(v);
1498 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001499 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001500 if (x != NULL) continue;
1501 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001502
Thomas Wouters434d0822000-08-24 20:11:32 +00001503 case INPLACE_ADD:
1504 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001505 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001506 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001507 PyUnicode_CheckExact(w)) {
1508 x = unicode_concatenate(v, w, f, next_instr);
1509 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001510 goto skip_decref_v;
1511 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001512 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001513 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001514 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001515 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001516 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001517 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001518 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001519 if (x != NULL) continue;
1520 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001521
Thomas Wouters434d0822000-08-24 20:11:32 +00001522 case INPLACE_SUBTRACT:
1523 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001524 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001525 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001526 Py_DECREF(v);
1527 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001528 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001529 if (x != NULL) continue;
1530 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001531
Thomas Wouters434d0822000-08-24 20:11:32 +00001532 case INPLACE_LSHIFT:
1533 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001534 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001535 x = PyNumber_InPlaceLshift(v, w);
1536 Py_DECREF(v);
1537 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001538 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001539 if (x != NULL) continue;
1540 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001541
Thomas Wouters434d0822000-08-24 20:11:32 +00001542 case INPLACE_RSHIFT:
1543 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001544 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001545 x = PyNumber_InPlaceRshift(v, w);
1546 Py_DECREF(v);
1547 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001548 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001549 if (x != NULL) continue;
1550 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001551
Thomas Wouters434d0822000-08-24 20:11:32 +00001552 case INPLACE_AND:
1553 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001554 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001555 x = PyNumber_InPlaceAnd(v, w);
1556 Py_DECREF(v);
1557 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001558 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001559 if (x != NULL) continue;
1560 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001561
Thomas Wouters434d0822000-08-24 20:11:32 +00001562 case INPLACE_XOR:
1563 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001564 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001565 x = PyNumber_InPlaceXor(v, w);
1566 Py_DECREF(v);
1567 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001568 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001569 if (x != NULL) continue;
1570 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Thomas Wouters434d0822000-08-24 20:11:32 +00001572 case INPLACE_OR:
1573 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001574 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001575 x = PyNumber_InPlaceOr(v, w);
1576 Py_DECREF(v);
1577 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001578 SET_TOP(x);
Thomas Wouters434d0822000-08-24 20:11:32 +00001579 if (x != NULL) continue;
1580 break;
1581
Guido van Rossum374a9221991-04-04 10:40:29 +00001582 case STORE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001583 w = TOP();
1584 v = SECOND();
1585 u = THIRD();
1586 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001587 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001588 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001589 Py_DECREF(u);
1590 Py_DECREF(v);
1591 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001592 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001593 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001594
Guido van Rossum374a9221991-04-04 10:40:29 +00001595 case DELETE_SUBSCR:
Raymond Hettinger663004b2003-01-09 15:24:30 +00001596 w = TOP();
1597 v = SECOND();
1598 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001599 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001600 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001601 Py_DECREF(v);
1602 Py_DECREF(w);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00001603 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001604 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001605
Guido van Rossum374a9221991-04-04 10:40:29 +00001606 case PRINT_EXPR:
1607 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001608 w = PySys_GetObject("displayhook");
1609 if (w == NULL) {
1610 PyErr_SetString(PyExc_RuntimeError,
1611 "lost sys.displayhook");
1612 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001613 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001614 }
1615 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001616 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001617 if (x == NULL)
1618 err = -1;
1619 }
1620 if (err == 0) {
1621 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001622 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001623 if (w == NULL)
1624 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001625 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001626 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001627 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001628 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001629
Thomas Wouters434d0822000-08-24 20:11:32 +00001630#ifdef CASE_TOO_BIG
1631 default: switch (opcode) {
1632#endif
Guido van Rossumf10570b1995-07-07 22:53:21 +00001633 case RAISE_VARARGS:
Collin Winter828f04a2007-08-31 00:04:24 +00001634 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001635 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001636 case 2:
1637 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001638 case 1:
1639 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001640 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001641 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001642 break;
1643 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001644 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001645 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001646 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001647 break;
1648 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001649 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001651 case STORE_LOCALS:
1652 x = POP();
1653 v = f->f_locals;
1654 Py_XDECREF(v);
1655 f->f_locals = x;
1656 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001657
Guido van Rossum374a9221991-04-04 10:40:29 +00001658 case RETURN_VALUE:
1659 retval = POP();
1660 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001661 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001662
Tim Peters5ca576e2001-06-18 22:08:13 +00001663 case YIELD_VALUE:
1664 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001665 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001666 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001667 /* Put aside the current exception state and restore
1668 that of the calling frame. This only serves when
1669 "yield" is used inside an except handler. */
1670 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001671 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001672
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001673 case POP_EXCEPT:
1674 {
1675 PyTryBlock *b = PyFrame_BlockPop(f);
1676 if (b->b_type != EXCEPT_HANDLER) {
1677 PyErr_SetString(PyExc_SystemError,
1678 "popped block is not an except handler");
1679 why = WHY_EXCEPTION;
1680 break;
1681 }
1682 UNWIND_EXCEPT_HANDLER(b);
1683 }
1684 continue;
1685
Guido van Rossum374a9221991-04-04 10:40:29 +00001686 case POP_BLOCK:
1687 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001688 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001689 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001690 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001691 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001692
Christian Heimes180510d2008-03-03 19:15:45 +00001693 PREDICTED(END_FINALLY);
Guido van Rossum374a9221991-04-04 10:40:29 +00001694 case END_FINALLY:
1695 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001696 if (PyLong_Check(v)) {
1697 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001698 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001699 if (why == WHY_RETURN ||
1700 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001702 if (why == WHY_SILENCED) {
1703 /* An exception was silenced by 'with', we must
1704 manually unwind the EXCEPT_HANDLER block which was
1705 created when the exception was caught, otherwise
1706 the stack will be in an inconsistent state. */
1707 PyTryBlock *b = PyFrame_BlockPop(f);
1708 if (b->b_type != EXCEPT_HANDLER) {
1709 PyErr_SetString(PyExc_SystemError,
1710 "popped block is not an except handler");
1711 why = WHY_EXCEPTION;
1712 }
1713 else {
1714 UNWIND_EXCEPT_HANDLER(b);
1715 why = WHY_NOT;
1716 }
1717 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001719 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001720 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001721 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001722 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001723 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001724 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001726 else if (v != Py_None) {
1727 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001728 "'finally' pops bad exception");
1729 why = WHY_EXCEPTION;
1730 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001731 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001732 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001733
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001734 case LOAD_BUILD_CLASS:
1735 x = PyDict_GetItemString(f->f_builtins,
1736 "__build_class__");
1737 if (x == NULL) {
1738 PyErr_SetString(PyExc_ImportError,
1739 "__build_class__ not found");
1740 break;
1741 }
1742 Py_INCREF(x);
1743 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001744 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Guido van Rossum374a9221991-04-04 10:40:29 +00001746 case STORE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001747 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001748 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001749 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001750 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001751 err = PyDict_SetItem(x, w, v);
1752 else
1753 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001754 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001755 if (err == 0) continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001756 break;
1757 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001758 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001759 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001760 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Guido van Rossum374a9221991-04-04 10:40:29 +00001762 case DELETE_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001763 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001764 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001765 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001766 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001767 NAME_ERROR_MSG,
1768 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001769 break;
1770 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001771 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001772 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001774
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001775 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Thomas Wouters0be5aab2000-08-11 22:15:52 +00001776 case UNPACK_SEQUENCE:
Guido van Rossum374a9221991-04-04 10:40:29 +00001777 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001778 if (PyTuple_CheckExact(v) &&
1779 PyTuple_GET_SIZE(v) == oparg) {
1780 PyObject **items = \
1781 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001782 while (oparg--) {
1783 w = items[oparg];
1784 Py_INCREF(w);
1785 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001786 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001787 Py_DECREF(v);
1788 continue;
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001789 } else if (PyList_CheckExact(v) &&
1790 PyList_GET_SIZE(v) == oparg) {
1791 PyObject **items = \
1792 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001793 while (oparg--) {
1794 w = items[oparg];
1795 Py_INCREF(w);
1796 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001797 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001798 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001799 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001800 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001801 } else {
1802 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001803 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001804 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001805 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001806 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001807
Guido van Rossum0368b722007-05-11 16:50:42 +00001808 case UNPACK_EX:
1809 {
1810 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1811 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001812
Guido van Rossum0368b722007-05-11 16:50:42 +00001813 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1814 stack_pointer + totalargs)) {
1815 stack_pointer += totalargs;
1816 } else {
1817 why = WHY_EXCEPTION;
1818 }
1819 Py_DECREF(v);
1820 break;
1821 }
1822
Guido van Rossum374a9221991-04-04 10:40:29 +00001823 case STORE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001824 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001825 v = TOP();
1826 u = SECOND();
1827 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001828 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1829 Py_DECREF(v);
1830 Py_DECREF(u);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001831 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001833
Guido van Rossum374a9221991-04-04 10:40:29 +00001834 case DELETE_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00001835 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001836 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001837 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1838 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001839 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001841
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001842 case STORE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001843 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001844 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001845 err = PyDict_SetItem(f->f_globals, w, v);
1846 Py_DECREF(v);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001847 if (err == 0) continue;
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001848 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001849
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001850 case DELETE_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001851 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001852 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001853 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001854 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001855 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001856
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 case LOAD_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00001858 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001859 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001860 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001861 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001862 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001863 break;
1864 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001865 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001866 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001867 Py_XINCREF(x);
1868 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001869 else {
1870 x = PyObject_GetItem(v, w);
1871 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001872 if (!PyErr_ExceptionMatches(
1873 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001874 break;
1875 PyErr_Clear();
1876 }
1877 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001879 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001881 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001882 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001883 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001884 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001885 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001886 break;
1887 }
1888 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001889 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 PUSH(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001892 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001893
Guido van Rossum374a9221991-04-04 10:40:29 +00001894 case LOAD_GLOBAL:
Skip Montanaro496e6582002-08-06 17:47:40 +00001895 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00001896 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00001897 /* Inline the PyDict_GetItem() calls.
1898 WARNING: this is an extreme speed hack.
1899 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00001900 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001901 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001902 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001903 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001904 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001905 e = d->ma_lookup(d, w, hash);
1906 if (e == NULL) {
1907 x = NULL;
1908 break;
1909 }
1910 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001911 if (x != NULL) {
1912 Py_INCREF(x);
1913 PUSH(x);
1914 continue;
1915 }
1916 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001917 e = d->ma_lookup(d, w, hash);
1918 if (e == NULL) {
1919 x = NULL;
1920 break;
1921 }
1922 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001923 if (x != NULL) {
1924 Py_INCREF(x);
1925 PUSH(x);
1926 continue;
1927 }
1928 goto load_global_error;
1929 }
1930 }
1931 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00001932 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001933 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001934 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001935 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001936 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00001937 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001938 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00001939 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001940 break;
1941 }
1942 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001943 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001944 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001945 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001946
Guido van Rossum8b17d6b1993-03-30 13:18:41 +00001947 case DELETE_FAST:
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001948 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001949 if (x != NULL) {
1950 SETLOCAL(oparg, NULL);
1951 continue;
Guido van Rossum2e4c8991998-05-12 20:27:36 +00001952 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001953 format_exc_check_arg(
1954 PyExc_UnboundLocalError,
1955 UNBOUNDLOCAL_ERROR_MSG,
1956 PyTuple_GetItem(co->co_varnames, oparg)
1957 );
1958 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001960 case LOAD_CLOSURE:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001961 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001962 Py_INCREF(x);
1963 PUSH(x);
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001964 if (x != NULL) continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001965 break;
1966
1967 case LOAD_DEREF:
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001968 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001969 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001970 if (w != NULL) {
1971 PUSH(w);
1972 continue;
Jeremy Hylton2524d692001-02-05 17:23:16 +00001973 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001974 err = -1;
1975 /* Don't stomp existing exception */
1976 if (PyErr_Occurred())
1977 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001978 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
1979 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00001980 oparg);
1981 format_exc_check_arg(
1982 PyExc_UnboundLocalError,
1983 UNBOUNDLOCAL_ERROR_MSG,
1984 v);
1985 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001986 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
1987 PyTuple_GET_SIZE(co->co_cellvars));
1988 format_exc_check_arg(PyExc_NameError,
1989 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001990 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001991 break;
1992
1993 case STORE_DEREF:
1994 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00001995 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001996 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00001997 Py_DECREF(w);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001998 continue;
1999
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 case BUILD_TUPLE:
Guido van Rossumb209a111997-04-29 18:18:01 +00002001 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002002 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002003 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002005 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002006 }
2007 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002008 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002009 }
2010 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002011
Guido van Rossum374a9221991-04-04 10:40:29 +00002012 case BUILD_LIST:
Guido van Rossumb209a111997-04-29 18:18:01 +00002013 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002014 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002015 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002016 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002017 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002018 }
2019 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002020 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002021 }
2022 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002023
Guido van Rossum86e58e22006-08-28 15:27:34 +00002024 case BUILD_SET:
2025 x = PySet_New(NULL);
2026 if (x != NULL) {
2027 for (; --oparg >= 0;) {
2028 w = POP();
2029 if (err == 0)
2030 err = PySet_Add(x, w);
2031 Py_DECREF(w);
2032 }
2033 if (err != 0) {
2034 Py_DECREF(x);
2035 break;
2036 }
2037 PUSH(x);
2038 continue;
2039 }
2040 break;
2041
Guido van Rossum374a9221991-04-04 10:40:29 +00002042 case BUILD_MAP:
Christian Heimes99170a52007-12-19 02:07:34 +00002043 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002044 PUSH(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002045 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002046 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002047
Christian Heimes99170a52007-12-19 02:07:34 +00002048 case STORE_MAP:
2049 w = TOP(); /* key */
2050 u = SECOND(); /* value */
2051 v = THIRD(); /* dict */
2052 STACKADJ(-2);
2053 assert (PyDict_CheckExact(v));
2054 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2055 Py_DECREF(u);
2056 Py_DECREF(w);
2057 if (err == 0) continue;
2058 break;
2059
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002060 case MAP_ADD:
2061 w = TOP(); /* key */
2062 u = SECOND(); /* value */
2063 STACKADJ(-2);
2064 v = stack_pointer[-oparg]; /* dict */
2065 assert (PyDict_CheckExact(v));
2066 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2067 Py_DECREF(u);
2068 Py_DECREF(w);
2069 if (err == 0) {
2070 PREDICT(JUMP_ABSOLUTE);
2071 continue;
2072 }
2073 break;
2074
Guido van Rossum374a9221991-04-04 10:40:29 +00002075 case LOAD_ATTR:
Skip Montanaro496e6582002-08-06 17:47:40 +00002076 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002077 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002078 x = PyObject_GetAttr(v, w);
2079 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002080 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002081 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002082 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002083
Guido van Rossum374a9221991-04-04 10:40:29 +00002084 case COMPARE_OP:
2085 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002086 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002087 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002088 Py_DECREF(v);
2089 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002090 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002091 if (x == NULL) break;
2092 PREDICT(JUMP_IF_FALSE);
2093 PREDICT(JUMP_IF_TRUE);
2094 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002095
Guido van Rossum374a9221991-04-04 10:40:29 +00002096 case IMPORT_NAME:
Skip Montanaro496e6582002-08-06 17:47:40 +00002097 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002098 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002099 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002100 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002101 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002102 break;
2103 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002104 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002105 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002106 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002107 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002108 w = PyTuple_Pack(5,
2109 w,
2110 f->f_globals,
2111 f->f_locals == NULL ?
2112 Py_None : f->f_locals,
2113 v,
2114 u);
2115 else
2116 w = PyTuple_Pack(4,
2117 w,
2118 f->f_globals,
2119 f->f_locals == NULL ?
2120 Py_None : f->f_locals,
2121 v);
2122 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002123 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002124 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002125 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002126 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002127 x = NULL;
2128 break;
2129 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002130 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002131 v = x;
2132 x = PyEval_CallObject(v, w);
2133 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002134 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002135 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002136 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002137 if (x != NULL) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002138 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002139
Thomas Wouters52152252000-08-17 22:55:00 +00002140 case IMPORT_STAR:
2141 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002142 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002143 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002144 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002145 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002146 break;
2147 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002148 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002149 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002150 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002151 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002152 Py_DECREF(v);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002153 if (err == 0) continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002154 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002155
Thomas Wouters52152252000-08-17 22:55:00 +00002156 case IMPORT_FROM:
Skip Montanaro496e6582002-08-06 17:47:40 +00002157 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002158 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002159 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002160 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002161 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002162 PUSH(x);
2163 if (x != NULL) continue;
2164 break;
2165
Guido van Rossum374a9221991-04-04 10:40:29 +00002166 case JUMP_FORWARD:
2167 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002168 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002169
Raymond Hettingerf606f872003-03-16 03:11:04 +00002170 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002171 case JUMP_IF_FALSE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002172 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002173 if (w == Py_True) {
2174 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002175 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002176 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002177 if (w == Py_False) {
2178 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002179 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002180 }
2181 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002182 if (err > 0)
2183 err = 0;
2184 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002185 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002186 else
2187 break;
2188 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002189
Raymond Hettingerf606f872003-03-16 03:11:04 +00002190 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002191 case JUMP_IF_TRUE:
Raymond Hettinger21012b82003-02-26 18:11:50 +00002192 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002193 if (w == Py_False) {
2194 PREDICT(POP_TOP);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002195 goto fast_next_opcode;
Raymond Hettingerf606f872003-03-16 03:11:04 +00002196 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002197 if (w == Py_True) {
2198 JUMPBY(oparg);
Neil Schemenauerc4b570f2003-06-01 19:21:12 +00002199 goto fast_next_opcode;
Raymond Hettinger21012b82003-02-26 18:11:50 +00002200 }
2201 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002202 if (err > 0) {
2203 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002204 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002205 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002206 else if (err == 0)
2207 ;
2208 else
2209 break;
2210 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002211
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002212 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Guido van Rossum374a9221991-04-04 10:40:29 +00002213 case JUMP_ABSOLUTE:
2214 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002215#if FAST_LOOPS
2216 /* Enabling this path speeds-up all while and for-loops by bypassing
2217 the per-loop checks for signals. By default, this should be turned-off
2218 because it prevents detection of a control-break in tight loops like
2219 "while 1: pass". Compile with this option turned-on when you need
2220 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002221 that contain only instructions ending with goto fast_next_opcode).
Guido van Rossum58da9312007-11-10 23:39:45 +00002222 */
2223 goto fast_next_opcode;
2224#else
Neil Schemenauerca2a2f12003-05-30 23:59:44 +00002225 continue;
Guido van Rossum58da9312007-11-10 23:39:45 +00002226#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002228 case GET_ITER:
2229 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002230 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002231 x = PyObject_GetIter(v);
2232 Py_DECREF(v);
2233 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002234 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002235 PREDICT(FOR_ITER);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002236 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002237 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002238 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002239 break;
2240
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002241 PREDICTED_WITH_ARG(FOR_ITER);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002242 case FOR_ITER:
2243 /* before: [iter]; after: [iter, iter()] *or* [] */
2244 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002245 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002246 if (x != NULL) {
2247 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002248 PREDICT(STORE_FAST);
2249 PREDICT(UNPACK_SEQUENCE);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002250 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002251 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002252 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002253 if (!PyErr_ExceptionMatches(
2254 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002255 break;
2256 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002257 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002258 /* iterator ended normally */
2259 x = v = POP();
2260 Py_DECREF(v);
2261 JUMPBY(oparg);
2262 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002263
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002264 case BREAK_LOOP:
2265 why = WHY_BREAK;
2266 goto fast_block_end;
2267
2268 case CONTINUE_LOOP:
Christian Heimes217cfd12007-12-02 14:31:20 +00002269 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002270 if (!retval) {
2271 x = NULL;
2272 break;
2273 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002274 why = WHY_CONTINUE;
2275 goto fast_block_end;
2276
Guido van Rossum374a9221991-04-04 10:40:29 +00002277 case SETUP_LOOP:
2278 case SETUP_EXCEPT:
2279 case SETUP_FINALLY:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002280 /* NOTE: If you add any new block-setup opcodes that
2281 are not try/except/finally handlers, you may need
2282 to update the PyGen_NeedsFinalizing() function.
2283 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002284
Guido van Rossumb209a111997-04-29 18:18:01 +00002285 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002286 STACK_LEVEL());
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002287 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002288
Guido van Rossumc2e20742006-02-27 22:32:47 +00002289 case WITH_CLEANUP:
2290 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002291 /* At the top of the stack are 1-3 values indicating
2292 how/why we entered the finally clause:
2293 - TOP = None
2294 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2295 - TOP = WHY_*; no retval below it
2296 - (TOP, SECOND, THIRD) = exc_info()
2297 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002298 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002299 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002300 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002301 EXIT(None, None, None)
2302
2303 In all cases, we remove EXIT from the stack, leaving
2304 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002305
2306 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002307 *and* the function call returns a 'true' value, we
2308 "zap" this information, to prevent END_FINALLY from
2309 re-raising the exception. (But non-local gotos
2310 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002311 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002312
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002313 PyObject *exit_func = POP();
2314 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002315 if (u == Py_None) {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002316 v = w = Py_None;
2317 }
2318 else if (PyLong_Check(u)) {
Guido van Rossumc2e20742006-02-27 22:32:47 +00002319 u = v = w = Py_None;
2320 }
2321 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002322 v = SECOND();
2323 w = THIRD();
Guido van Rossumc2e20742006-02-27 22:32:47 +00002324 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002325 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002326 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2327 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002328 Py_DECREF(exit_func);
2329 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002330 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002331
2332 if (u != Py_None)
2333 err = PyObject_IsTrue(x);
2334 else
2335 err = 0;
2336 Py_DECREF(x);
2337
2338 if (err < 0)
2339 break; /* Go to error exit */
2340 else if (err > 0) {
2341 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002342 /* There was an exception and a True return */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002343 STACKADJ(-2);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002344 SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002345 Py_DECREF(u);
2346 Py_DECREF(v);
2347 Py_DECREF(w);
Guido van Rossumf6694362006-03-10 02:28:35 +00002348 }
Christian Heimes180510d2008-03-03 19:15:45 +00002349 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002350 break;
2351 }
2352
Guido van Rossumf10570b1995-07-07 22:53:21 +00002353 case CALL_FUNCTION:
Armin Rigo8817fcd2004-06-17 10:22:40 +00002354 {
2355 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002356 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002357 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002358#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002359 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002360#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002361 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002362#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002363 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002364 PUSH(x);
2365 if (x != NULL)
2366 continue;
2367 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002368 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002369
Jeremy Hylton76901512000-03-28 23:49:17 +00002370 case CALL_FUNCTION_VAR:
2371 case CALL_FUNCTION_KW:
2372 case CALL_FUNCTION_VAR_KW:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002373 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002374 int na = oparg & 0xff;
2375 int nk = (oparg>>8) & 0xff;
2376 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002377 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002378 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002379 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002380 if (flags & CALL_FLAG_VAR)
2381 n++;
2382 if (flags & CALL_FLAG_KW)
2383 n++;
2384 pfunc = stack_pointer - n - 1;
2385 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002386
Guido van Rossumac7be682001-01-17 15:42:30 +00002387 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002388 && PyMethod_GET_SELF(func) != NULL) {
2389 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002390 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002391 func = PyMethod_GET_FUNCTION(func);
2392 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002393 Py_DECREF(*pfunc);
2394 *pfunc = self;
2395 na++;
2396 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002397 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002398 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002399 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002400 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002401 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002402 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002403 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002404 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002405
Jeremy Hylton76901512000-03-28 23:49:17 +00002406 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002407 w = POP();
2408 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002409 }
2410 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002411 if (x != NULL)
Jeremy Hylton52820442001-01-03 23:52:36 +00002412 continue;
Jeremy Hylton76901512000-03-28 23:49:17 +00002413 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002414 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002415
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002416 case MAKE_CLOSURE:
Guido van Rossum681d79a1995-07-18 14:51:37 +00002417 case MAKE_FUNCTION:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002418 {
2419 int posdefaults = oparg & 0xff;
2420 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002421 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002422
Guido van Rossum681d79a1995-07-18 14:51:37 +00002423 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002424 x = PyFunction_New(v, f->f_globals);
2425 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002426
Guido van Rossum0240b922007-02-26 21:23:50 +00002427 if (x != NULL && opcode == MAKE_CLOSURE) {
2428 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002429 if (PyFunction_SetClosure(x, v) != 0) {
2430 /* Can't happen unless bytecode is corrupt. */
2431 why = WHY_EXCEPTION;
2432 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002433 Py_DECREF(v);
2434 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002435
2436 if (x != NULL && num_annotations > 0) {
2437 Py_ssize_t name_ix;
2438 u = POP(); /* names of args with annotations */
2439 v = PyDict_New();
2440 if (v == NULL) {
2441 Py_DECREF(x);
2442 x = NULL;
2443 break;
2444 }
2445 name_ix = PyTuple_Size(u);
2446 assert(num_annotations == name_ix+1);
2447 while (name_ix > 0) {
2448 --name_ix;
2449 t = PyTuple_GET_ITEM(u, name_ix);
2450 w = POP();
2451 /* XXX(nnorwitz): check for errors */
2452 PyDict_SetItem(v, t, w);
2453 Py_DECREF(w);
2454 }
2455
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002456 if (PyFunction_SetAnnotations(x, v) != 0) {
2457 /* Can't happen unless
2458 PyFunction_SetAnnotations changes. */
2459 why = WHY_EXCEPTION;
2460 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002461 Py_DECREF(v);
2462 Py_DECREF(u);
2463 }
2464
Guido van Rossum681d79a1995-07-18 14:51:37 +00002465 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002466 if (x != NULL && posdefaults > 0) {
2467 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002468 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002469 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002470 x = NULL;
2471 break;
2472 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002473 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002474 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002475 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002476 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002477 if (PyFunction_SetDefaults(x, v) != 0) {
2478 /* Can't happen unless
2479 PyFunction_SetDefaults changes. */
2480 why = WHY_EXCEPTION;
2481 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002482 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002483 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002484 if (x != NULL && kwdefaults > 0) {
2485 v = PyDict_New();
2486 if (v == NULL) {
2487 Py_DECREF(x);
2488 x = NULL;
2489 break;
2490 }
2491 while (--kwdefaults >= 0) {
2492 w = POP(); /* default value */
2493 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002494 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002495 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002496 Py_DECREF(w);
2497 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002498 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002499 if (PyFunction_SetKwDefaults(x, v) != 0) {
2500 /* Can't happen unless
2501 PyFunction_SetKwDefaults changes. */
2502 why = WHY_EXCEPTION;
2503 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002504 Py_DECREF(v);
2505 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002506 PUSH(x);
2507 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002508 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002509
2510 case BUILD_SLICE:
2511 if (oparg == 3)
2512 w = POP();
2513 else
2514 w = NULL;
2515 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002516 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002517 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002518 Py_DECREF(u);
2519 Py_DECREF(v);
2520 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002521 SET_TOP(x);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002522 if (x != NULL) continue;
Guido van Rossum8861b741996-07-30 16:49:37 +00002523 break;
2524
Fred Drakeef8ace32000-08-24 00:32:09 +00002525 case EXTENDED_ARG:
2526 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002527 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002528 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002529
Guido van Rossum374a9221991-04-04 10:40:29 +00002530 default:
2531 fprintf(stderr,
2532 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002533 PyCode_Addr2Line(f->f_code, f->f_lasti),
2534 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002535 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002536 why = WHY_EXCEPTION;
2537 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002538
2539#ifdef CASE_TOO_BIG
2540 }
2541#endif
2542
Guido van Rossum374a9221991-04-04 10:40:29 +00002543 } /* switch */
2544
2545 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002546
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002547 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002548
Guido van Rossum374a9221991-04-04 10:40:29 +00002549 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002550
Guido van Rossum374a9221991-04-04 10:40:29 +00002551 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002552 if (err == 0 && x != NULL) {
2553#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002554 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002555 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002556 fprintf(stderr,
2557 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002558 else {
2559#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002560 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002561 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002562#ifdef CHECKEXC
2563 }
2564#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002565 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002566 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002567 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002568 err = 0;
2569 }
2570
Guido van Rossum374a9221991-04-04 10:40:29 +00002571 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002572
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002573 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002574 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002575 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002576 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002577 why = WHY_EXCEPTION;
2578 }
2579 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002580#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002581 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002582 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002583 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002584 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002585 sprintf(buf, "Stack unwind with exception "
2586 "set and why=%d", why);
2587 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002588 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002589 }
2590#endif
2591
2592 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002593
Guido van Rossum374a9221991-04-04 10:40:29 +00002594 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002595 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002596
Fred Drake8f51f542001-10-04 14:48:42 +00002597 if (tstate->c_tracefunc != NULL)
2598 call_exc_trace(tstate->c_tracefunc,
2599 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002600 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002601
Guido van Rossum374a9221991-04-04 10:40:29 +00002602 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002603
Guido van Rossum374a9221991-04-04 10:40:29 +00002604 if (why == WHY_RERAISE)
2605 why = WHY_EXCEPTION;
2606
2607 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002608
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002609fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002610 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002611 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002612
Tim Peters8a5c3c72004-04-05 19:36:21 +00002613 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002614 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2615 /* For a continue inside a try block,
2616 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002617 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2618 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002619 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002620 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002621 Py_DECREF(retval);
2622 break;
2623 }
2624
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002625 if (b->b_type == EXCEPT_HANDLER) {
2626 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002627 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002628 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002629 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002630 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2631 why = WHY_NOT;
2632 JUMPTO(b->b_handler);
2633 break;
2634 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002635 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2636 || b->b_type == SETUP_FINALLY)) {
2637 PyObject *exc, *val, *tb;
2638 int handler = b->b_handler;
2639 /* Beware, this invalidates all b->b_* fields */
2640 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2641 PUSH(tstate->exc_traceback);
2642 PUSH(tstate->exc_value);
2643 if (tstate->exc_type != NULL) {
2644 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002645 }
2646 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002647 Py_INCREF(Py_None);
2648 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002649 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002650 PyErr_Fetch(&exc, &val, &tb);
2651 /* Make the raw exception data
2652 available to the handler,
2653 so a program can emulate the
2654 Python main loop. */
2655 PyErr_NormalizeException(
2656 &exc, &val, &tb);
2657 PyException_SetTraceback(val, tb);
2658 Py_INCREF(exc);
2659 tstate->exc_type = exc;
2660 Py_INCREF(val);
2661 tstate->exc_value = val;
2662 tstate->exc_traceback = tb;
2663 if (tb == NULL)
2664 tb = Py_None;
2665 Py_INCREF(tb);
2666 PUSH(tb);
2667 PUSH(val);
2668 PUSH(exc);
2669 why = WHY_NOT;
2670 JUMPTO(handler);
2671 break;
2672 }
2673 if (b->b_type == SETUP_FINALLY) {
2674 if (why & (WHY_RETURN | WHY_CONTINUE))
2675 PUSH(retval);
2676 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002677 why = WHY_NOT;
2678 JUMPTO(b->b_handler);
2679 break;
2680 }
2681 } /* unwind stack */
2682
2683 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002684
Guido van Rossum374a9221991-04-04 10:40:29 +00002685 if (why != WHY_NOT)
2686 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002687 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002688
Guido van Rossum374a9221991-04-04 10:40:29 +00002689 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002690
Tim Peters8a5c3c72004-04-05 19:36:21 +00002691 assert(why != WHY_YIELD);
2692 /* Pop remaining stack entries. */
2693 while (!EMPTY()) {
2694 v = POP();
2695 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002696 }
2697
Tim Peters8a5c3c72004-04-05 19:36:21 +00002698 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002699 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002700
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002701fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002702 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002703 if (tstate->c_tracefunc) {
2704 if (why == WHY_RETURN || why == WHY_YIELD) {
2705 if (call_trace(tstate->c_tracefunc,
2706 tstate->c_traceobj, f,
2707 PyTrace_RETURN, retval)) {
2708 Py_XDECREF(retval);
2709 retval = NULL;
2710 why = WHY_EXCEPTION;
2711 }
2712 }
2713 else if (why == WHY_EXCEPTION) {
2714 call_trace_protected(tstate->c_tracefunc,
2715 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002716 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002717 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002718 }
Fred Drake8f51f542001-10-04 14:48:42 +00002719 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002720 if (why == WHY_EXCEPTION)
2721 call_trace_protected(tstate->c_profilefunc,
2722 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002723 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002724 else if (call_trace(tstate->c_profilefunc,
2725 tstate->c_profileobj, f,
2726 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002727 Py_XDECREF(retval);
2728 retval = NULL;
2729 why = WHY_EXCEPTION;
2730 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002731 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002732 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002733
Tim Peters5ca576e2001-06-18 22:08:13 +00002734 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002735exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002736 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002737 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002738
Guido van Rossum96a42c81992-01-12 02:29:51 +00002739 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002740}
2741
Guido van Rossumc2e20742006-02-27 22:32:47 +00002742/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002743 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002744 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002745
Tim Peters6d6c1a32001-08-02 04:15:00 +00002746PyObject *
2747PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002748 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002749 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002750{
2751 register PyFrameObject *f;
2752 register PyObject *retval = NULL;
2753 register PyObject **fastlocals, **freevars;
2754 PyThreadState *tstate = PyThreadState_GET();
2755 PyObject *x, *u;
2756
2757 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002758 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002759 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002760 return NULL;
2761 }
2762
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002763 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002764 assert(globals != NULL);
2765 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002766 if (f == NULL)
2767 return NULL;
2768
2769 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002770 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002771
2772 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002773 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002774 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2775 int i;
2776 int n = argcount;
2777 PyObject *kwdict = NULL;
2778 if (co->co_flags & CO_VARKEYWORDS) {
2779 kwdict = PyDict_New();
2780 if (kwdict == NULL)
2781 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002782 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002783 if (co->co_flags & CO_VARARGS)
2784 i++;
2785 SETLOCAL(i, kwdict);
2786 }
2787 if (argcount > co->co_argcount) {
2788 if (!(co->co_flags & CO_VARARGS)) {
2789 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002790 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002791 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002792 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002793 defcount ? "at most" : "exactly",
2794 co->co_argcount,
2795 kwcount ? "non-keyword " : "",
2796 co->co_argcount == 1 ? "" : "s",
2797 argcount);
2798 goto fail;
2799 }
2800 n = co->co_argcount;
2801 }
2802 for (i = 0; i < n; i++) {
2803 x = args[i];
2804 Py_INCREF(x);
2805 SETLOCAL(i, x);
2806 }
2807 if (co->co_flags & CO_VARARGS) {
2808 u = PyTuple_New(argcount - n);
2809 if (u == NULL)
2810 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002811 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002812 for (i = n; i < argcount; i++) {
2813 x = args[i];
2814 Py_INCREF(x);
2815 PyTuple_SET_ITEM(u, i-n, x);
2816 }
2817 }
2818 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002819 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002820 PyObject *keyword = kws[2*i];
2821 PyObject *value = kws[2*i + 1];
2822 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00002823 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002824 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002825 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002826 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00002827 goto fail;
2828 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002829 /* Speed hack: do raw pointer compares. As names are
2830 normally interned this should almost always hit. */
2831 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002832 for (j = 0;
2833 j < co->co_argcount + co->co_kwonlyargcount;
2834 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002835 PyObject *nm = co_varnames[j];
2836 if (nm == keyword)
2837 goto kw_found;
2838 }
2839 /* Slow fallback, just in case */
2840 for (j = 0;
2841 j < co->co_argcount + co->co_kwonlyargcount;
2842 j++) {
2843 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00002844 int cmp = PyObject_RichCompareBool(
2845 keyword, nm, Py_EQ);
2846 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002847 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002848 else if (cmp < 0)
2849 goto fail;
2850 }
2851 /* Check errors from Compare */
2852 if (PyErr_Occurred())
2853 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002854 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002855 if (kwdict == NULL) {
2856 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002857 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00002858 "keyword argument '%S'",
2859 co->co_name,
2860 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00002861 goto fail;
2862 }
2863 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002864 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00002865 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002866kw_found:
2867 if (GETLOCAL(j) != NULL) {
2868 PyErr_Format(PyExc_TypeError,
2869 "%U() got multiple "
2870 "values for keyword "
2871 "argument '%S'",
2872 co->co_name,
2873 keyword);
2874 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00002875 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002876 Py_INCREF(value);
2877 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00002878 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002879 if (co->co_kwonlyargcount > 0) {
2880 for (i = co->co_argcount;
2881 i < co->co_argcount + co->co_kwonlyargcount;
2882 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00002883 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002884 if (GETLOCAL(i) != NULL)
2885 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00002886 name = PyTuple_GET_ITEM(co->co_varnames, i);
2887 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002888 if (kwdefs != NULL)
2889 def = PyDict_GetItem(kwdefs, name);
2890 if (def != NULL) {
2891 Py_INCREF(def);
2892 SETLOCAL(i, def);
2893 continue;
2894 }
2895 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002896 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002897 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002898 goto fail;
2899 }
2900 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002901 if (argcount < co->co_argcount) {
2902 int m = co->co_argcount - defcount;
2903 for (i = argcount; i < m; i++) {
2904 if (GETLOCAL(i) == NULL) {
2905 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002906 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002907 "%spositional argument%s "
2908 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002909 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002910 ((co->co_flags & CO_VARARGS) ||
2911 defcount) ? "at least"
2912 : "exactly",
2913 m, kwcount ? "non-keyword " : "",
2914 m == 1 ? "" : "s", i);
2915 goto fail;
2916 }
2917 }
2918 if (n > m)
2919 i = n - m;
2920 else
2921 i = 0;
2922 for (; i < defcount; i++) {
2923 if (GETLOCAL(m+i) == NULL) {
2924 PyObject *def = defs[i];
2925 Py_INCREF(def);
2926 SETLOCAL(m+i, def);
2927 }
2928 }
2929 }
2930 }
2931 else {
2932 if (argcount > 0 || kwcount > 0) {
2933 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002934 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002935 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002936 argcount + kwcount);
2937 goto fail;
2938 }
2939 }
2940 /* Allocate and initialize storage for cell vars, and copy free
2941 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002942 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002943 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002944 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00002945 PyObject *c;
2946
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00002947 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002948 if (co->co_flags & CO_VARARGS)
2949 nargs++;
2950 if (co->co_flags & CO_VARKEYWORDS)
2951 nargs++;
2952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 /* Initialize each cell var, taking into account
2954 cell vars that are initialized from arguments.
2955
2956 Should arrange for the compiler to put cellvars
2957 that are arguments at the beginning of the cellvars
2958 list so that we can march over it more efficiently?
2959 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002960 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002961 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00002962 PyTuple_GET_ITEM(co->co_cellvars, i));
2963 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002965 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00002966 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00002967 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002968 c = PyCell_New(GETLOCAL(j));
2969 if (c == NULL)
2970 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002971 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00002972 found = 1;
2973 break;
2974 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002975 }
2976 if (found == 0) {
2977 c = PyCell_New(NULL);
2978 if (c == NULL)
2979 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002980 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00002981 }
2982 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002983 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002984 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002985 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002986 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002987 PyObject *o = PyTuple_GET_ITEM(closure, i);
2988 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002989 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00002990 }
2991 }
2992
Tim Peters5ca576e2001-06-18 22:08:13 +00002993 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002994 /* Don't need to keep the reference to f_back, it will be set
2995 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00002996 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00002997 f->f_back = NULL;
2998
Jeremy Hylton985eba52003-02-05 23:13:00 +00002999 PCALL(PCALL_GENERATOR);
3000
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003001 /* Create a new generator that owns the ready to run frame
3002 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003003 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003004 }
3005
Thomas Woutersce272b62007-09-19 21:19:28 +00003006 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003007
Thomas Woutersce272b62007-09-19 21:19:28 +00003008fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003009
Tim Petersb13680b2001-11-27 23:29:29 +00003010 /* decref'ing the frame can cause __del__ methods to get invoked,
3011 which can call back into Python. While we're done with the
3012 current Python frame (f), the associated C stack is still in use,
3013 so recursion_depth must be boosted for the duration.
3014 */
3015 assert(tstate != NULL);
3016 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003017 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003018 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003019 return retval;
3020}
3021
3022
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003023/* Logic for the raise statement (too complicated for inlining).
3024 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003025static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003026do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003027{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003028 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003029
3030 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003031 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003032 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003033 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003034 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003035 value = tstate->exc_value;
3036 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003037 if (type == Py_None) {
3038 PyErr_SetString(PyExc_RuntimeError,
3039 "No active exception to reraise");
3040 return WHY_EXCEPTION;
3041 }
3042 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003043 Py_XINCREF(value);
3044 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003045 PyErr_Restore(type, value, tb);
3046 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003047 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003048
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003049 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003050 raise
3051 raise <instance>
3052 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003053
Collin Winter828f04a2007-08-31 00:04:24 +00003054 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003055 type = exc;
3056 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003057 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003058 goto raise_error;
3059 }
Collin Winter828f04a2007-08-31 00:04:24 +00003060 else if (PyExceptionInstance_Check(exc)) {
3061 value = exc;
3062 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003063 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003064 }
3065 else {
3066 /* Not something you can raise. You get an exception
3067 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003068 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003069 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003070 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003071 goto raise_error;
3072 }
Collin Winter828f04a2007-08-31 00:04:24 +00003073
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003074 if (cause) {
3075 PyObject *fixed_cause;
3076 if (PyExceptionClass_Check(cause)) {
3077 fixed_cause = PyObject_CallObject(cause, NULL);
3078 if (fixed_cause == NULL)
3079 goto raise_error;
3080 Py_DECREF(cause);
3081 }
3082 else if (PyExceptionInstance_Check(cause)) {
3083 fixed_cause = cause;
3084 }
3085 else {
3086 PyErr_SetString(PyExc_TypeError,
3087 "exception causes must derive from "
3088 "BaseException");
3089 goto raise_error;
3090 }
3091 PyException_SetCause(value, fixed_cause);
3092 }
Collin Winter828f04a2007-08-31 00:04:24 +00003093
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003094 PyErr_SetObject(type, value);
3095 /* PyErr_SetObject incref's its arguments */
3096 Py_XDECREF(value);
3097 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003098 return WHY_EXCEPTION;
3099
3100raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003101 Py_XDECREF(value);
3102 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003103 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003104 return WHY_EXCEPTION;
3105}
3106
Tim Petersd6d010b2001-06-21 02:49:55 +00003107/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003108 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003109
Guido van Rossum0368b722007-05-11 16:50:42 +00003110 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3111 with a variable target.
3112*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003113
Barry Warsawe42b18f1997-08-25 22:13:04 +00003114static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003115unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003116{
Guido van Rossum0368b722007-05-11 16:50:42 +00003117 int i = 0, j = 0;
3118 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003119 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003120 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003121 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003122
Tim Petersd6d010b2001-06-21 02:49:55 +00003123 assert(v != NULL);
3124
3125 it = PyObject_GetIter(v);
3126 if (it == NULL)
3127 goto Error;
3128
3129 for (; i < argcnt; i++) {
3130 w = PyIter_Next(it);
3131 if (w == NULL) {
3132 /* Iterator done, via error or exhaustion. */
3133 if (!PyErr_Occurred()) {
3134 PyErr_Format(PyExc_ValueError,
3135 "need more than %d value%s to unpack",
3136 i, i == 1 ? "" : "s");
3137 }
3138 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003139 }
3140 *--sp = w;
3141 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003142
Guido van Rossum0368b722007-05-11 16:50:42 +00003143 if (argcntafter == -1) {
3144 /* We better have exhausted the iterator now. */
3145 w = PyIter_Next(it);
3146 if (w == NULL) {
3147 if (PyErr_Occurred())
3148 goto Error;
3149 Py_DECREF(it);
3150 return 1;
3151 }
3152 Py_DECREF(w);
3153 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3154 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003155 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003156
3157 l = PySequence_List(it);
3158 if (l == NULL)
3159 goto Error;
3160 *--sp = l;
3161 i++;
3162
3163 ll = PyList_GET_SIZE(l);
3164 if (ll < argcntafter) {
3165 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3166 argcnt + ll);
3167 goto Error;
3168 }
3169
3170 /* Pop the "after-variable" args off the list. */
3171 for (j = argcntafter; j > 0; j--, i++) {
3172 *--sp = PyList_GET_ITEM(l, ll - j);
3173 }
3174 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003175 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003176 Py_DECREF(it);
3177 return 1;
3178
Tim Petersd6d010b2001-06-21 02:49:55 +00003179Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003180 for (; i > 0; i--, sp++)
3181 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003182 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003183 return 0;
3184}
3185
3186
Guido van Rossum96a42c81992-01-12 02:29:51 +00003187#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003188static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003189prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003190{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003191 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003192 if (PyObject_Print(v, stdout, 0) != 0)
3193 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003194 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003195 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003196}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003197#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003198
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003199static void
Fred Drake5755ce62001-06-27 19:19:46 +00003200call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003201{
Guido van Rossumb209a111997-04-29 18:18:01 +00003202 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003203 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003204 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003205 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003206 value = Py_None;
3207 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003208 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003209 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003210 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003211 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003212 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003213 }
Fred Drake5755ce62001-06-27 19:19:46 +00003214 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003215 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003216 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003217 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003218 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003219 Py_XDECREF(type);
3220 Py_XDECREF(value);
3221 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003222 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003223}
3224
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003225static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003226call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003227 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003228{
3229 PyObject *type, *value, *traceback;
3230 int err;
3231 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003232 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003233 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003234 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003235 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003236 return 0;
3237 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003238 else {
3239 Py_XDECREF(type);
3240 Py_XDECREF(value);
3241 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003242 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003243 }
3244}
3245
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003246static int
Fred Drake5755ce62001-06-27 19:19:46 +00003247call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3248 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003249{
Fred Drake5755ce62001-06-27 19:19:46 +00003250 register PyThreadState *tstate = frame->f_tstate;
3251 int result;
3252 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003253 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003254 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003255 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003256 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003257 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3258 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003259 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003260 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003261}
3262
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003263PyObject *
3264_PyEval_CallTracing(PyObject *func, PyObject *args)
3265{
3266 PyFrameObject *frame = PyEval_GetFrame();
3267 PyThreadState *tstate = frame->f_tstate;
3268 int save_tracing = tstate->tracing;
3269 int save_use_tracing = tstate->use_tracing;
3270 PyObject *result;
3271
3272 tstate->tracing = 0;
3273 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3274 || (tstate->c_profilefunc != NULL));
3275 result = PyObject_Call(func, args, NULL);
3276 tstate->tracing = save_tracing;
3277 tstate->use_tracing = save_use_tracing;
3278 return result;
3279}
3280
Michael W. Hudson006c7522002-11-08 13:08:46 +00003281static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003282maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003283 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3284 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003285{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003286 int result = 0;
3287
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003288 /* If the last instruction executed isn't in the current
3289 instruction window, reset the window. If the last
3290 instruction happens to fall at the start of a line or if it
3291 represents a jump backwards, call the trace function.
3292 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003293 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003294 int line;
3295 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003296
Thomas Woutersce272b62007-09-19 21:19:28 +00003297 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3298 &bounds);
3299 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003300 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003301 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003302 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003303 }
3304 *instr_lb = bounds.ap_lower;
3305 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003306 }
Armin Rigobf57a142004-03-22 19:24:58 +00003307 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003308 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003309 }
3310 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003311 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003312}
3313
Fred Drake5755ce62001-06-27 19:19:46 +00003314void
3315PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003316{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003317 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003318 PyObject *temp = tstate->c_profileobj;
3319 Py_XINCREF(arg);
3320 tstate->c_profilefunc = NULL;
3321 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003322 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003323 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003324 Py_XDECREF(temp);
3325 tstate->c_profilefunc = func;
3326 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003327 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003328 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003329}
3330
3331void
3332PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3333{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003334 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003335 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003336 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003337 Py_XINCREF(arg);
3338 tstate->c_tracefunc = NULL;
3339 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003340 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003341 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003342 Py_XDECREF(temp);
3343 tstate->c_tracefunc = func;
3344 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003345 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003346 tstate->use_tracing = ((func != NULL)
3347 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003348}
3349
Guido van Rossumb209a111997-04-29 18:18:01 +00003350PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003351PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003352{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003353 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003354 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003355 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003356 else
3357 return current_frame->f_builtins;
3358}
3359
Guido van Rossumb209a111997-04-29 18:18:01 +00003360PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003361PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003362{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003363 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003364 if (current_frame == NULL)
3365 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003366 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003367 return current_frame->f_locals;
3368}
3369
Guido van Rossumb209a111997-04-29 18:18:01 +00003370PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003371PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003372{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003373 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003374 if (current_frame == NULL)
3375 return NULL;
3376 else
3377 return current_frame->f_globals;
3378}
3379
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003380PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003381PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003382{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003383 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003384 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003385}
3386
Guido van Rossum6135a871995-01-09 17:53:26 +00003387int
Tim Peters5ba58662001-07-16 02:29:45 +00003388PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003389{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003390 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003391 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003392
3393 if (current_frame != NULL) {
3394 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003395 const int compilerflags = codeflags & PyCF_MASK;
3396 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003397 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003398 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003399 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003400#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003401 if (codeflags & CO_GENERATOR_ALLOWED) {
3402 result = 1;
3403 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3404 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003405#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003406 }
3407 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003408}
3409
Guido van Rossum3f5da241990-12-20 15:06:42 +00003410
Guido van Rossum681d79a1995-07-18 14:51:37 +00003411/* External interface to call any callable object.
3412 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003413
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003414#undef PyEval_CallObject
3415/* for backward compatibility: export this interface */
3416
Guido van Rossumb209a111997-04-29 18:18:01 +00003417PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003418PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003419{
Guido van Rossumb209a111997-04-29 18:18:01 +00003420 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003421}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003422#define PyEval_CallObject(func,arg) \
3423 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003424
Guido van Rossumb209a111997-04-29 18:18:01 +00003425PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003426PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003427{
Jeremy Hylton52820442001-01-03 23:52:36 +00003428 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003429
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003430 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003431 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003432 if (arg == NULL)
3433 return NULL;
3434 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003435 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003436 PyErr_SetString(PyExc_TypeError,
3437 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003438 return NULL;
3439 }
3440 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003441 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003442
Guido van Rossumb209a111997-04-29 18:18:01 +00003443 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003444 PyErr_SetString(PyExc_TypeError,
3445 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003446 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003447 return NULL;
3448 }
3449
Tim Peters6d6c1a32001-08-02 04:15:00 +00003450 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003451 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003452 return result;
3453}
3454
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003455const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003456PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003457{
3458 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003459 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003460 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003461 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003462 else if (PyCFunction_Check(func))
3463 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003464 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003465 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003466}
3467
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003468const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003469PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003470{
3471 if (PyMethod_Check(func))
3472 return "()";
3473 else if (PyFunction_Check(func))
3474 return "()";
3475 else if (PyCFunction_Check(func))
3476 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003477 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003478 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003479}
3480
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003481static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003482err_args(PyObject *func, int flags, int nargs)
3483{
3484 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003485 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003486 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003487 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003488 nargs);
3489 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003490 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003491 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003492 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003493 nargs);
3494}
3495
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003496#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003497if (tstate->use_tracing && tstate->c_profilefunc) { \
3498 if (call_trace(tstate->c_profilefunc, \
3499 tstate->c_profileobj, \
3500 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003501 func)) { \
3502 x = NULL; \
3503 } \
3504 else { \
3505 x = call; \
3506 if (tstate->c_profilefunc != NULL) { \
3507 if (x == NULL) { \
3508 call_trace_protected(tstate->c_profilefunc, \
3509 tstate->c_profileobj, \
3510 tstate->frame, PyTrace_C_EXCEPTION, \
3511 func); \
3512 /* XXX should pass (type, value, tb) */ \
3513 } else { \
3514 if (call_trace(tstate->c_profilefunc, \
3515 tstate->c_profileobj, \
3516 tstate->frame, PyTrace_C_RETURN, \
3517 func)) { \
3518 Py_DECREF(x); \
3519 x = NULL; \
3520 } \
3521 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003522 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003523 } \
3524} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003525 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003526 }
3527
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003528static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003529call_function(PyObject ***pp_stack, int oparg
3530#ifdef WITH_TSC
3531 , uint64* pintr0, uint64* pintr1
3532#endif
3533 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003534{
3535 int na = oparg & 0xff;
3536 int nk = (oparg>>8) & 0xff;
3537 int n = na + 2 * nk;
3538 PyObject **pfunc = (*pp_stack) - n - 1;
3539 PyObject *func = *pfunc;
3540 PyObject *x, *w;
3541
Jeremy Hylton985eba52003-02-05 23:13:00 +00003542 /* Always dispatch PyCFunction first, because these are
3543 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003544 */
3545 if (PyCFunction_Check(func) && nk == 0) {
3546 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003547 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003548
3549 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003550 if (flags & (METH_NOARGS | METH_O)) {
3551 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3552 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003553 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003554 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003555 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003556 else if (flags & METH_O && na == 1) {
3557 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003558 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003559 Py_DECREF(arg);
3560 }
3561 else {
3562 err_args(func, flags, na);
3563 x = NULL;
3564 }
3565 }
3566 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003567 PyObject *callargs;
3568 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003569 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003570 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003571 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003572 Py_XDECREF(callargs);
3573 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003574 } else {
3575 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3576 /* optimize access to bound methods */
3577 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003578 PCALL(PCALL_METHOD);
3579 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003580 Py_INCREF(self);
3581 func = PyMethod_GET_FUNCTION(func);
3582 Py_INCREF(func);
3583 Py_DECREF(*pfunc);
3584 *pfunc = self;
3585 na++;
3586 n++;
3587 } else
3588 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003589 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003590 if (PyFunction_Check(func))
3591 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003592 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003593 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003594 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003595 Py_DECREF(func);
3596 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003597
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003598 /* Clear the stack of the function object. Also removes
3599 the arguments in case they weren't consumed already
3600 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003601 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003602 while ((*pp_stack) > pfunc) {
3603 w = EXT_POP(*pp_stack);
3604 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003605 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003606 }
3607 return x;
3608}
3609
Jeremy Hylton192690e2002-08-16 18:36:11 +00003610/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003611 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003612 For the simplest case -- a function that takes only positional
3613 arguments and is called with only positional arguments -- it
3614 inlines the most primitive frame setup code from
3615 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3616 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003617*/
3618
3619static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003620fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003621{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003622 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003623 PyObject *globals = PyFunction_GET_GLOBALS(func);
3624 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003625 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003626 PyObject **d = NULL;
3627 int nd = 0;
3628
Jeremy Hylton985eba52003-02-05 23:13:00 +00003629 PCALL(PCALL_FUNCTION);
3630 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003631 if (argdefs == NULL && co->co_argcount == n &&
3632 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003633 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3634 PyFrameObject *f;
3635 PyObject *retval = NULL;
3636 PyThreadState *tstate = PyThreadState_GET();
3637 PyObject **fastlocals, **stack;
3638 int i;
3639
3640 PCALL(PCALL_FASTER_FUNCTION);
3641 assert(globals != NULL);
3642 /* XXX Perhaps we should create a specialized
3643 PyFrame_New() that doesn't take locals, but does
3644 take builtins without sanity checking them.
3645 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003646 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003647 f = PyFrame_New(tstate, co, globals, NULL);
3648 if (f == NULL)
3649 return NULL;
3650
3651 fastlocals = f->f_localsplus;
3652 stack = (*pp_stack) - n;
3653
3654 for (i = 0; i < n; i++) {
3655 Py_INCREF(*stack);
3656 fastlocals[i] = *stack++;
3657 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003658 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003659 ++tstate->recursion_depth;
3660 Py_DECREF(f);
3661 --tstate->recursion_depth;
3662 return retval;
3663 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003664 if (argdefs != NULL) {
3665 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003666 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003667 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003668 return PyEval_EvalCodeEx(co, globals,
3669 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003670 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003671 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003672}
3673
3674static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003675update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3676 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003677{
3678 PyObject *kwdict = NULL;
3679 if (orig_kwdict == NULL)
3680 kwdict = PyDict_New();
3681 else {
3682 kwdict = PyDict_Copy(orig_kwdict);
3683 Py_DECREF(orig_kwdict);
3684 }
3685 if (kwdict == NULL)
3686 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003687 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003688 int err;
3689 PyObject *value = EXT_POP(*pp_stack);
3690 PyObject *key = EXT_POP(*pp_stack);
3691 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003692 PyErr_Format(PyExc_TypeError,
3693 "%.200s%s got multiple values "
3694 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003695 PyEval_GetFuncName(func),
3696 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003697 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003698 Py_DECREF(key);
3699 Py_DECREF(value);
3700 Py_DECREF(kwdict);
3701 return NULL;
3702 }
3703 err = PyDict_SetItem(kwdict, key, value);
3704 Py_DECREF(key);
3705 Py_DECREF(value);
3706 if (err) {
3707 Py_DECREF(kwdict);
3708 return NULL;
3709 }
3710 }
3711 return kwdict;
3712}
3713
3714static PyObject *
3715update_star_args(int nstack, int nstar, PyObject *stararg,
3716 PyObject ***pp_stack)
3717{
3718 PyObject *callargs, *w;
3719
3720 callargs = PyTuple_New(nstack + nstar);
3721 if (callargs == NULL) {
3722 return NULL;
3723 }
3724 if (nstar) {
3725 int i;
3726 for (i = 0; i < nstar; i++) {
3727 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3728 Py_INCREF(a);
3729 PyTuple_SET_ITEM(callargs, nstack + i, a);
3730 }
3731 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003732 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003733 w = EXT_POP(*pp_stack);
3734 PyTuple_SET_ITEM(callargs, nstack, w);
3735 }
3736 return callargs;
3737}
3738
3739static PyObject *
3740load_args(PyObject ***pp_stack, int na)
3741{
3742 PyObject *args = PyTuple_New(na);
3743 PyObject *w;
3744
3745 if (args == NULL)
3746 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003747 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003748 w = EXT_POP(*pp_stack);
3749 PyTuple_SET_ITEM(args, na, w);
3750 }
3751 return args;
3752}
3753
3754static PyObject *
3755do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3756{
3757 PyObject *callargs = NULL;
3758 PyObject *kwdict = NULL;
3759 PyObject *result = NULL;
3760
3761 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003762 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003763 if (kwdict == NULL)
3764 goto call_fail;
3765 }
3766 callargs = load_args(pp_stack, na);
3767 if (callargs == NULL)
3768 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003769#ifdef CALL_PROFILE
3770 /* At this point, we have to look at the type of func to
3771 update the call stats properly. Do it here so as to avoid
3772 exposing the call stats machinery outside ceval.c
3773 */
3774 if (PyFunction_Check(func))
3775 PCALL(PCALL_FUNCTION);
3776 else if (PyMethod_Check(func))
3777 PCALL(PCALL_METHOD);
3778 else if (PyType_Check(func))
3779 PCALL(PCALL_TYPE);
3780 else
3781 PCALL(PCALL_OTHER);
3782#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003783 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003784call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003785 Py_XDECREF(callargs);
3786 Py_XDECREF(kwdict);
3787 return result;
3788}
3789
3790static PyObject *
3791ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3792{
3793 int nstar = 0;
3794 PyObject *callargs = NULL;
3795 PyObject *stararg = NULL;
3796 PyObject *kwdict = NULL;
3797 PyObject *result = NULL;
3798
3799 if (flags & CALL_FLAG_KW) {
3800 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003801 if (!PyDict_Check(kwdict)) {
3802 PyObject *d;
3803 d = PyDict_New();
3804 if (d == NULL)
3805 goto ext_call_fail;
3806 if (PyDict_Update(d, kwdict) != 0) {
3807 Py_DECREF(d);
3808 /* PyDict_Update raises attribute
3809 * error (percolated from an attempt
3810 * to get 'keys' attribute) instead of
3811 * a type error if its second argument
3812 * is not a mapping.
3813 */
3814 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3815 PyErr_Format(PyExc_TypeError,
3816 "%.200s%.200s argument after ** "
3817 "must be a mapping, not %.200s",
3818 PyEval_GetFuncName(func),
3819 PyEval_GetFuncDesc(func),
3820 kwdict->ob_type->tp_name);
3821 }
3822 goto ext_call_fail;
3823 }
3824 Py_DECREF(kwdict);
3825 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00003826 }
3827 }
3828 if (flags & CALL_FLAG_VAR) {
3829 stararg = EXT_POP(*pp_stack);
3830 if (!PyTuple_Check(stararg)) {
3831 PyObject *t = NULL;
3832 t = PySequence_Tuple(stararg);
3833 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003834 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3835 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003836 "%.200s%.200s argument after * "
3837 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003838 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003839 PyEval_GetFuncDesc(func),
3840 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003841 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003842 goto ext_call_fail;
3843 }
3844 Py_DECREF(stararg);
3845 stararg = t;
3846 }
3847 nstar = PyTuple_GET_SIZE(stararg);
3848 }
3849 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003850 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003851 if (kwdict == NULL)
3852 goto ext_call_fail;
3853 }
3854 callargs = update_star_args(na, nstar, stararg, pp_stack);
3855 if (callargs == NULL)
3856 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003857#ifdef CALL_PROFILE
3858 /* At this point, we have to look at the type of func to
3859 update the call stats properly. Do it here so as to avoid
3860 exposing the call stats machinery outside ceval.c
3861 */
3862 if (PyFunction_Check(func))
3863 PCALL(PCALL_FUNCTION);
3864 else if (PyMethod_Check(func))
3865 PCALL(PCALL_METHOD);
3866 else if (PyType_Check(func))
3867 PCALL(PCALL_TYPE);
3868 else
3869 PCALL(PCALL_OTHER);
3870#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003871 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00003872ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003873 Py_XDECREF(callargs);
3874 Py_XDECREF(kwdict);
3875 Py_XDECREF(stararg);
3876 return result;
3877}
3878
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003879/* Extract a slice index from a PyInt or PyLong or an object with the
3880 nb_index slot defined, and store in *pi.
3881 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
3882 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 +00003883 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00003884*/
Tim Petersb5196382001-12-16 19:44:20 +00003885/* Note: If v is NULL, return success without storing into *pi. This
3886 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
3887 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00003888*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00003889int
Martin v. Löwis18e16552006-02-15 17:27:45 +00003890_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891{
Tim Petersb5196382001-12-16 19:44:20 +00003892 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00003893 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00003894 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003895 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003896 if (x == -1 && PyErr_Occurred())
3897 return 0;
3898 }
3899 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003900 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00003901 "slice indices must be integers or "
3902 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00003903 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003904 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003905 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003906 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00003907 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003908}
3909
Guido van Rossum486364b2007-06-30 05:01:58 +00003910#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00003911 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00003912
Guido van Rossumb209a111997-04-29 18:18:01 +00003913static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003914cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003915{
Guido van Rossumac7be682001-01-17 15:42:30 +00003916 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003917 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00003918 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003919 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003920 break;
3921 case PyCmp_IS_NOT:
3922 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003923 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003924 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003925 res = PySequence_Contains(w, v);
3926 if (res < 0)
3927 return NULL;
3928 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003929 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00003930 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003931 if (res < 0)
3932 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00003933 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003934 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00003935 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003936 if (PyTuple_Check(w)) {
3937 Py_ssize_t i, length;
3938 length = PyTuple_Size(w);
3939 for (i = 0; i < length; i += 1) {
3940 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00003941 if (!PyExceptionClass_Check(exc)) {
3942 PyErr_SetString(PyExc_TypeError,
3943 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00003944 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003945 }
3946 }
3947 }
3948 else {
Brett Cannon39590462007-02-26 22:01:14 +00003949 if (!PyExceptionClass_Check(w)) {
3950 PyErr_SetString(PyExc_TypeError,
3951 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00003952 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00003953 }
3954 }
Barry Warsaw4249f541997-08-22 21:26:19 +00003955 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003956 break;
3957 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00003958 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003959 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003960 v = res ? Py_True : Py_False;
3961 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003962 return v;
3963}
3964
Thomas Wouters52152252000-08-17 22:55:00 +00003965static PyObject *
3966import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00003967{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003968 PyObject *x;
3969
3970 x = PyObject_GetAttr(v, name);
3971 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00003972 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003973 }
Thomas Wouters52152252000-08-17 22:55:00 +00003974 return x;
3975}
Guido van Rossumac7be682001-01-17 15:42:30 +00003976
Thomas Wouters52152252000-08-17 22:55:00 +00003977static int
3978import_all_from(PyObject *locals, PyObject *v)
3979{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003980 PyObject *all = PyObject_GetAttrString(v, "__all__");
3981 PyObject *dict, *name, *value;
3982 int skip_leading_underscores = 0;
3983 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00003984
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003985 if (all == NULL) {
3986 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3987 return -1; /* Unexpected error */
3988 PyErr_Clear();
3989 dict = PyObject_GetAttrString(v, "__dict__");
3990 if (dict == NULL) {
3991 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3992 return -1;
3993 PyErr_SetString(PyExc_ImportError,
3994 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003995 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00003996 }
3997 all = PyMapping_Keys(dict);
3998 Py_DECREF(dict);
3999 if (all == NULL)
4000 return -1;
4001 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004002 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004003
4004 for (pos = 0, err = 0; ; pos++) {
4005 name = PySequence_GetItem(all, pos);
4006 if (name == NULL) {
4007 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4008 err = -1;
4009 else
4010 PyErr_Clear();
4011 break;
4012 }
4013 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004014 PyUnicode_Check(name) &&
4015 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004016 {
4017 Py_DECREF(name);
4018 continue;
4019 }
4020 value = PyObject_GetAttr(v, name);
4021 if (value == NULL)
4022 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004023 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004024 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004025 else
4026 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004027 Py_DECREF(name);
4028 Py_XDECREF(value);
4029 if (err != 0)
4030 break;
4031 }
4032 Py_DECREF(all);
4033 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004034}
4035
Guido van Rossumac7be682001-01-17 15:42:30 +00004036static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004037format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004038{
Neal Norwitzda059e32007-08-26 05:33:45 +00004039 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004040
4041 if (!obj)
4042 return;
4043
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004044 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004045 if (!obj_str)
4046 return;
4047
4048 PyErr_Format(exc, format_str, obj_str);
4049}
Guido van Rossum950361c1997-01-24 13:49:28 +00004050
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004051static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004052unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004053 PyFrameObject *f, unsigned char *next_instr)
4054{
4055 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004056 are (Unicode) strings. */
4057 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4058 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004059 Py_ssize_t new_len = v_len + w_len;
4060 if (new_len < 0) {
4061 PyErr_SetString(PyExc_OverflowError,
4062 "strings are too large to concat");
4063 return NULL;
4064 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004065
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004066 if (v->ob_refcnt == 2) {
4067 /* In the common case, there are 2 references to the value
4068 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004069 * value stack (in 'v') and one still stored in the
4070 * 'variable'. We try to delete the variable now to reduce
4071 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004072 */
4073 switch (*next_instr) {
4074 case STORE_FAST:
4075 {
4076 int oparg = PEEKARG();
4077 PyObject **fastlocals = f->f_localsplus;
4078 if (GETLOCAL(oparg) == v)
4079 SETLOCAL(oparg, NULL);
4080 break;
4081 }
4082 case STORE_DEREF:
4083 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004084 PyObject **freevars = (f->f_localsplus +
4085 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004086 PyObject *c = freevars[PEEKARG()];
4087 if (PyCell_GET(c) == v)
4088 PyCell_Set(c, NULL);
4089 break;
4090 }
4091 case STORE_NAME:
4092 {
4093 PyObject *names = f->f_code->co_names;
4094 PyObject *name = GETITEM(names, PEEKARG());
4095 PyObject *locals = f->f_locals;
4096 if (PyDict_CheckExact(locals) &&
4097 PyDict_GetItem(locals, name) == v) {
4098 if (PyDict_DelItem(locals, name) != 0) {
4099 PyErr_Clear();
4100 }
4101 }
4102 break;
4103 }
4104 }
4105 }
4106
Guido van Rossum98297ee2007-11-06 21:34:58 +00004107 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004108 /* Now we own the last reference to 'v', so we can resize it
4109 * in-place.
4110 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004111 if (PyUnicode_Resize(&v, new_len) != 0) {
4112 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004113 * deallocated so it cannot be put back into
4114 * 'variable'. The MemoryError is raised when there
4115 * is no value in 'variable', which might (very
4116 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004117 */
4118 return NULL;
4119 }
4120 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004121 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4122 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004123 return v;
4124 }
4125 else {
4126 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004127 w = PyUnicode_Concat(v, w);
4128 Py_DECREF(v);
4129 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004130 }
4131}
4132
Guido van Rossum950361c1997-01-24 13:49:28 +00004133#ifdef DYNAMIC_EXECUTION_PROFILE
4134
Skip Montanarof118cb12001-10-15 20:51:38 +00004135static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004136getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004137{
4138 int i;
4139 PyObject *l = PyList_New(256);
4140 if (l == NULL) return NULL;
4141 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004142 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004143 if (x == NULL) {
4144 Py_DECREF(l);
4145 return NULL;
4146 }
4147 PyList_SetItem(l, i, x);
4148 }
4149 for (i = 0; i < 256; i++)
4150 a[i] = 0;
4151 return l;
4152}
4153
4154PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004155_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004156{
4157#ifndef DXPAIRS
4158 return getarray(dxp);
4159#else
4160 int i;
4161 PyObject *l = PyList_New(257);
4162 if (l == NULL) return NULL;
4163 for (i = 0; i < 257; i++) {
4164 PyObject *x = getarray(dxpairs[i]);
4165 if (x == NULL) {
4166 Py_DECREF(l);
4167 return NULL;
4168 }
4169 PyList_SetItem(l, i, x);
4170 }
4171 return l;
4172#endif
4173}
4174
4175#endif