blob: 3ca972a697acdd7d8b2a02c9fae26627a066561c [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 *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000122static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000123
Paul Prescode68140d2000-08-30 20:25:01 +0000124#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000125 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000126#define GLOBAL_NAME_ERROR_MSG \
127 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000128#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000129 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000130#define UNBOUNDFREE_ERROR_MSG \
131 "free variable '%.200s' referenced before assignment" \
132 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000133
Guido van Rossum950361c1997-01-24 13:49:28 +0000134/* Dynamic execution profile */
135#ifdef DYNAMIC_EXECUTION_PROFILE
136#ifdef DXPAIRS
137static long dxpairs[257][256];
138#define dxp dxpairs[256]
139#else
140static long dxp[256];
141#endif
142#endif
143
Jeremy Hylton985eba52003-02-05 23:13:00 +0000144/* Function call profile */
145#ifdef CALL_PROFILE
146#define PCALL_NUM 11
147static int pcall[PCALL_NUM];
148
149#define PCALL_ALL 0
150#define PCALL_FUNCTION 1
151#define PCALL_FAST_FUNCTION 2
152#define PCALL_FASTER_FUNCTION 3
153#define PCALL_METHOD 4
154#define PCALL_BOUND_METHOD 5
155#define PCALL_CFUNCTION 6
156#define PCALL_TYPE 7
157#define PCALL_GENERATOR 8
158#define PCALL_OTHER 9
159#define PCALL_POP 10
160
161/* Notes about the statistics
162
163 PCALL_FAST stats
164
165 FAST_FUNCTION means no argument tuple needs to be created.
166 FASTER_FUNCTION means that the fast-path frame setup code is used.
167
168 If there is a method call where the call can be optimized by changing
169 the argument tuple and calling the function directly, it gets recorded
170 twice.
171
172 As a result, the relationship among the statistics appears to be
173 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
174 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
175 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
176 PCALL_METHOD > PCALL_BOUND_METHOD
177*/
178
179#define PCALL(POS) pcall[POS]++
180
181PyObject *
182PyEval_GetCallStats(PyObject *self)
183{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000184 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000185 pcall[0], pcall[1], pcall[2], pcall[3],
186 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000187 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000188}
189#else
190#define PCALL(O)
191
192PyObject *
193PyEval_GetCallStats(PyObject *self)
194{
195 Py_INCREF(Py_None);
196 return Py_None;
197}
198#endif
199
Tim Peters5ca576e2001-06-18 22:08:13 +0000200
Guido van Rossume59214e1994-08-30 08:01:59 +0000201#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000202
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000203#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000204#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000205#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000206#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000207
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000208static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000209static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000210static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000211
Tim Peters7f468f22004-10-11 02:40:51 +0000212int
213PyEval_ThreadsInitialized(void)
214{
215 return interpreter_lock != 0;
216}
217
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000221 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000222 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000223 interpreter_lock = PyThread_allocate_lock();
224 PyThread_acquire_lock(interpreter_lock, 1);
225 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000227
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232}
233
234void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000237 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238}
239
240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000242{
243 if (tstate == NULL)
244 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000245 /* Check someone has called PyEval_InitThreads() to create the lock */
246 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000248 if (PyThreadState_Swap(tstate) != NULL)
249 Py_FatalError(
250 "PyEval_AcquireThread: non-NULL old thread state");
251}
252
253void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000255{
256 if (tstate == NULL)
257 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
258 if (PyThreadState_Swap(NULL) != tstate)
259 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000260 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000261}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000262
263/* This function is called from PyOS_AfterFork to ensure that newly
264 created child processes don't hold locks referring to threads which
265 are not running in the child process. (This could also be done using
266 pthread_atfork mechanism, at least for the pthreads implementation.) */
267
268void
269PyEval_ReInitThreads(void)
270{
Jesse Nollera8513972008-07-17 16:49:17 +0000271 PyObject *threading, *result;
272 PyThreadState *tstate;
273
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000274 if (!interpreter_lock)
275 return;
276 /*XXX Can't use PyThread_free_lock here because it does too
277 much error-checking. Doing this cleanly would require
278 adding a new function to each thread_*.h. Instead, just
279 create a new lock and waste a little bit of memory */
280 interpreter_lock = PyThread_allocate_lock();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000281 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000282 PyThread_acquire_lock(interpreter_lock, 1);
283 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000284
285 /* Update the threading module with the new state.
286 */
287 tstate = PyThreadState_GET();
288 threading = PyMapping_GetItemString(tstate->interp->modules,
289 "threading");
290 if (threading == NULL) {
291 /* threading not imported */
292 PyErr_Clear();
293 return;
294 }
295 result = PyObject_CallMethod(threading, "_after_fork", NULL);
296 if (result == NULL)
297 PyErr_WriteUnraisable(threading);
298 else
299 Py_DECREF(result);
300 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000301}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302#endif
303
Guido van Rossumff4949e1992-08-05 19:58:53 +0000304/* Functions save_thread and restore_thread are always defined so
305 dynamically loaded modules needn't be compiled separately for use
306 with and without threads: */
307
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000308PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000309PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000311 PyThreadState *tstate = PyThreadState_Swap(NULL);
312 if (tstate == NULL)
313 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000314#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000315 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000316 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000317#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000318 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000319}
320
321void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000323{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000324 if (tstate == NULL)
325 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000326#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000327 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000328 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000329 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000331 }
332#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000333 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000334}
335
336
Guido van Rossuma9672091994-09-14 13:31:22 +0000337/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
338 signal handlers or Mac I/O completion routines) can schedule calls
339 to a function to be called synchronously.
340 The synchronous function is called with one void* argument.
341 It should return 0 for success or -1 for failure -- failure should
342 be accompanied by an exception.
343
344 If registry succeeds, the registry function returns 0; if it fails
345 (e.g. due to too many pending calls) it returns -1 (without setting
346 an exception condition).
347
348 Note that because registry may occur from within signal handlers,
349 or other asynchronous events, calling malloc() is unsafe!
350
351#ifdef WITH_THREAD
352 Any thread can schedule pending calls, but only the main thread
353 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000354 There is no facility to schedule calls to a particular thread, but
355 that should be easy to change, should that ever be required. In
356 that case, the static variables here should go into the python
357 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000358#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000359*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000360
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000361#ifdef WITH_THREAD
362
363/* The WITH_THREAD implementation is thread-safe. It allows
364 scheduling to be made from any thread, and even from an executing
365 callback.
366 */
367
368#define NPENDINGCALLS 32
369static struct {
370 int (*func)(void *);
371 void *arg;
372} pendingcalls[NPENDINGCALLS];
373static int pendingfirst = 0;
374static int pendinglast = 0;
375static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
376static char pendingbusy = 0;
377
378int
379Py_AddPendingCall(int (*func)(void *), void *arg)
380{
381 int i, j, result=0;
382 PyThread_type_lock lock = pending_lock;
383
384 /* try a few times for the lock. Since this mechanism is used
385 * for signal handling (on the main thread), there is a (slim)
386 * chance that a signal is delivered on the same thread while we
387 * hold the lock during the Py_MakePendingCalls() function.
388 * This avoids a deadlock in that case.
389 * Note that signals can be delivered on any thread. In particular,
390 * on Windows, a SIGINT is delivered on a system-created worker
391 * thread.
392 * We also check for lock being NULL, in the unlikely case that
393 * this function is called before any bytecode evaluation takes place.
394 */
395 if (lock != NULL) {
396 for (i = 0; i<100; i++) {
397 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
398 break;
399 }
400 if (i == 100)
401 return -1;
402 }
403
404 i = pendinglast;
405 j = (i + 1) % NPENDINGCALLS;
406 if (j == pendingfirst) {
407 result = -1; /* Queue full */
408 } else {
409 pendingcalls[i].func = func;
410 pendingcalls[i].arg = arg;
411 pendinglast = j;
412 }
413 /* signal main loop */
414 _Py_Ticker = 0;
415 pendingcalls_to_do = 1;
416 if (lock != NULL)
417 PyThread_release_lock(lock);
418 return result;
419}
420
421int
422Py_MakePendingCalls(void)
423{
424 int i;
425 int r = 0;
426
427 if (!pending_lock) {
428 /* initial allocation of the lock */
429 pending_lock = PyThread_allocate_lock();
430 if (pending_lock == NULL)
431 return -1;
432 }
433
434 /* only service pending calls on main thread */
435 if (main_thread && PyThread_get_thread_ident() != main_thread)
436 return 0;
437 /* don't perform recursive pending calls */
438 if (pendingbusy)
439 return 0;
440 pendingbusy = 1;
441 /* perform a bounded number of calls, in case of recursion */
442 for (i=0; i<NPENDINGCALLS; i++) {
443 int j;
444 int (*func)(void *);
Benjamin Petersonb4ddfa42009-01-17 23:46:54 +0000445 void *arg = NULL;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000446
447 /* pop one item off the queue while holding the lock */
448 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
449 j = pendingfirst;
450 if (j == pendinglast) {
451 func = NULL; /* Queue empty */
452 } else {
453 func = pendingcalls[j].func;
454 arg = pendingcalls[j].arg;
455 pendingfirst = (j + 1) % NPENDINGCALLS;
456 }
457 pendingcalls_to_do = pendingfirst != pendinglast;
458 PyThread_release_lock(pending_lock);
459 /* having released the lock, perform the callback */
460 if (func == NULL)
461 break;
462 r = func(arg);
463 if (r)
464 break;
465 }
466 pendingbusy = 0;
467 return r;
468}
469
470#else /* if ! defined WITH_THREAD */
471
472/*
473 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
474 This code is used for signal handling in python that isn't built
475 with WITH_THREAD.
476 Don't use this implementation when Py_AddPendingCalls() can happen
477 on a different thread!
478
Guido van Rossuma9672091994-09-14 13:31:22 +0000479 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480 (1) nested asynchronous calls to Py_AddPendingCall()
481 (2) AddPendingCall() calls made while pending calls are being processed.
482
483 (1) is very unlikely because typically signal delivery
484 is blocked during signal handling. So it should be impossible.
485 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000486 The current code is safe against (2), but not against (1).
487 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000488 thread is present, interrupted by signals, and that the critical
489 section is protected with the "busy" variable. On Windows, which
490 delivers SIGINT on a system thread, this does not hold and therefore
491 Windows really shouldn't use this version.
492 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000493*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000494
Guido van Rossuma9672091994-09-14 13:31:22 +0000495#define NPENDINGCALLS 32
496static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000497 int (*func)(void *);
498 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000499} pendingcalls[NPENDINGCALLS];
500static volatile int pendingfirst = 0;
501static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000502static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000503
504int
Thomas Wouters334fb892000-07-25 12:56:38 +0000505Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000506{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000507 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000508 int i, j;
509 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000510 if (busy)
511 return -1;
512 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000513 i = pendinglast;
514 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000515 if (j == pendingfirst) {
516 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000517 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000518 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000519 pendingcalls[i].func = func;
520 pendingcalls[i].arg = arg;
521 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000522
523 _Py_Ticker = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000524 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000525 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000526 /* XXX End critical section */
527 return 0;
528}
529
Guido van Rossum180d7b41994-09-29 09:45:57 +0000530int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000531Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000532{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000533 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000534 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000535 return 0;
536 busy = 1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000537 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000538 for (;;) {
539 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000540 int (*func)(void *);
541 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000542 i = pendingfirst;
543 if (i == pendinglast)
544 break; /* Queue empty */
545 func = pendingcalls[i].func;
546 arg = pendingcalls[i].arg;
547 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000548 if (func(arg) < 0) {
549 busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000550 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000551 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000552 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000553 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000554 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000555 return 0;
556}
557
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000558#endif /* WITH_THREAD */
559
Guido van Rossuma9672091994-09-14 13:31:22 +0000560
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000561/* The interpreter's recursion limit */
562
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000563#ifndef Py_DEFAULT_RECURSION_LIMIT
564#define Py_DEFAULT_RECURSION_LIMIT 1000
565#endif
566static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
567int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000569int
570Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000571{
572 return recursion_limit;
573}
574
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000575void
576Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000577{
578 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000579 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000580}
581
Armin Rigo2b3eb402003-10-28 12:05:48 +0000582/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
583 if the recursion_depth reaches _Py_CheckRecursionLimit.
584 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
585 to guarantee that _Py_CheckRecursiveCall() is regularly called.
586 Without USE_STACKCHECK, there is no need for this. */
587int
588_Py_CheckRecursiveCall(char *where)
589{
590 PyThreadState *tstate = PyThreadState_GET();
591
592#ifdef USE_STACKCHECK
593 if (PyOS_CheckStack()) {
594 --tstate->recursion_depth;
595 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
596 return -1;
597 }
598#endif
Antoine Pitrou658fad82008-09-03 18:34:34 +0000599 _Py_CheckRecursionLimit = recursion_limit;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000600 if (tstate->recursion_critical)
601 /* Somebody asked that we don't check for recursion. */
602 return 0;
603 if (tstate->overflowed) {
604 if (tstate->recursion_depth > recursion_limit + 50) {
605 /* Overflowing while handling an overflow. Give up. */
606 Py_FatalError("Cannot recover from stack overflow.");
607 }
608 return 0;
609 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000610 if (tstate->recursion_depth > recursion_limit) {
611 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000612 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000613 PyErr_Format(PyExc_RuntimeError,
614 "maximum recursion depth exceeded%s",
615 where);
616 return -1;
617 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000618 return 0;
619}
620
Guido van Rossum374a9221991-04-04 10:40:29 +0000621/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000622enum why_code {
623 WHY_NOT = 0x0001, /* No error */
624 WHY_EXCEPTION = 0x0002, /* Exception occurred */
625 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
626 WHY_RETURN = 0x0008, /* 'return' statement */
627 WHY_BREAK = 0x0010, /* 'break' statement */
628 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000629 WHY_YIELD = 0x0040, /* 'yield' operator */
630 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000631};
Guido van Rossum374a9221991-04-04 10:40:29 +0000632
Collin Winter828f04a2007-08-31 00:04:24 +0000633static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000634static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000635
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000636/* Records whether tracing is on for any thread. Counts the number of
637 threads for which tstate->c_tracefunc is non-NULL, so if the value
638 is 0, we know we don't have to check this thread's c_tracefunc.
639 This speeds up the if statement in PyEval_EvalFrameEx() after
640 fast_next_opcode*/
641static int _Py_TracingPossible = 0;
642
Skip Montanarod581d772002-09-03 20:10:45 +0000643/* for manipulating the thread switch and periodic "stuff" - used to be
644 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000645int _Py_CheckInterval = 100;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000646volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000647
Guido van Rossumb209a111997-04-29 18:18:01 +0000648PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000649PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000650{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000651 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000652 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000653 (PyObject **)NULL, 0,
654 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000655 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000656 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000657}
658
659
660/* Interpreter main loop */
661
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000662PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000663PyEval_EvalFrame(PyFrameObject *f) {
664 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000665 used this API; core interpreter code should call
666 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000667 return PyEval_EvalFrameEx(f, 0);
668}
669
670PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000672{
Guido van Rossum950361c1997-01-24 13:49:28 +0000673#ifdef DXPAIRS
674 int lastopcode = 0;
675#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000676 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000677 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000678 register int opcode; /* Current opcode */
679 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000680 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000681 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000682 register PyObject *x; /* Result object -- NULL if error */
683 register PyObject *v; /* Temporary objects popped off stack */
684 register PyObject *w;
685 register PyObject *u;
686 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000687 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000688 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000689 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000690 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000691
Tim Peters8a5c3c72004-04-05 19:36:21 +0000692 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000693
694 not (instr_lb <= current_bytecode_offset < instr_ub)
695
Tim Peters8a5c3c72004-04-05 19:36:21 +0000696 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000697 initial values are such as to make this false the first
698 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000699 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000700
Guido van Rossumd076c731998-10-07 19:42:25 +0000701 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000702 PyObject *names;
703 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000704#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000705 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000706 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000707#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000708
Antoine Pitroub52ec782009-01-25 16:34:23 +0000709/* Computed GOTOs, or
710 the-optimization-commonly-but-improperly-known-as-"threaded code"
711 using gcc's labels-as-values extension
712 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
713
714 The traditional bytecode evaluation loop uses a "switch" statement, which
715 decent compilers will optimize as a single indirect branch instruction
716 combined with a lookup table of jump addresses. However, since the
717 indirect jump instruction is shared by all opcodes, the CPU will have a
718 hard time making the right prediction for where to jump next (actually,
719 it will be always wrong except in the uncommon case of a sequence of
720 several identical opcodes).
721
722 "Threaded code" in contrast, uses an explicit jump table and an explicit
723 indirect jump instruction at the end of each opcode. Since the jump
724 instruction is at a different address for each opcode, the CPU will make a
725 separate prediction for each of these instructions, which is equivalent to
726 predicting the second opcode of each opcode pair. These predictions have
727 a much better chance to turn out valid, especially in small bytecode loops.
728
729 A mispredicted branch on a modern CPU flushes the whole pipeline and
730 can cost several CPU cycles (depending on the pipeline depth),
731 and potentially many more instructions (depending on the pipeline width).
732 A correctly predicted branch, however, is nearly free.
733
734 At the time of this writing, the "threaded code" version is up to 15-20%
735 faster than the normal "switch" version, depending on the compiler and the
736 CPU architecture.
737
738 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
739 because it would render the measurements invalid.
740
741
742 NOTE: care must be taken that the compiler doesn't try to "optimize" the
743 indirect jumps by sharing them between all opcodes. Such optimizations
744 can be disabled on gcc by using the -fno-gcse flag (or possibly
745 -fno-crossjumping).
746*/
747
748#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
749#undef USE_COMPUTED_GOTOS
750#endif
751
752#ifdef USE_COMPUTED_GOTOS
753/* Import the static jump table */
754#include "opcode_targets.h"
755
756/* This macro is used when several opcodes defer to the same implementation
757 (e.g. SETUP_LOOP, SETUP_FINALLY) */
758#define TARGET_WITH_IMPL(op, impl) \
759 TARGET_##op: \
760 opcode = op; \
761 if (HAS_ARG(op)) \
762 oparg = NEXTARG(); \
763 case op: \
764 goto impl; \
765
766#define TARGET(op) \
767 TARGET_##op: \
768 opcode = op; \
769 if (HAS_ARG(op)) \
770 oparg = NEXTARG(); \
771 case op:
772
773
774#define DISPATCH() \
775 { \
776 /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
777 int _tick = _Py_Ticker - 1; \
778 _Py_Ticker = _tick; \
779 if (_tick >= 0) { \
780 FAST_DISPATCH(); \
781 } \
782 continue; \
783 }
784
785#ifdef LLTRACE
786#define FAST_DISPATCH() \
787 { \
788 if (!lltrace && !_Py_TracingPossible) { \
789 f->f_lasti = INSTR_OFFSET(); \
790 goto *opcode_targets[*next_instr++]; \
791 } \
792 goto fast_next_opcode; \
793 }
794#else
795#define FAST_DISPATCH() \
796 { \
797 if (!_Py_TracingPossible) { \
798 f->f_lasti = INSTR_OFFSET(); \
799 goto *opcode_targets[*next_instr++]; \
800 } \
801 goto fast_next_opcode; \
802 }
803#endif
804
805#else
806#define TARGET(op) \
807 case op:
808#define TARGET_WITH_IMPL(op, impl) \
809 /* silence compiler warnings about `impl` unused */ \
810 if (0) goto impl; \
811 case op:
812#define DISPATCH() continue
813#define FAST_DISPATCH() goto fast_next_opcode
814#endif
815
816
Neal Norwitza81d2202002-07-14 00:27:26 +0000817/* Tuple access macros */
818
819#ifndef Py_DEBUG
820#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
821#else
822#define GETITEM(v, i) PyTuple_GetItem((v), (i))
823#endif
824
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000825#ifdef WITH_TSC
826/* Use Pentium timestamp counter to mark certain events:
827 inst0 -- beginning of switch statement for opcode dispatch
828 inst1 -- end of switch statement (may be skipped)
829 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000831 (may be skipped)
832 intr1 -- beginning of long interruption
833 intr2 -- end of long interruption
834
835 Many opcodes call out to helper C functions. In some cases, the
836 time in those functions should be counted towards the time for the
837 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
838 calls another Python function; there's no point in charge all the
839 bytecode executed by the called function to the caller.
840
841 It's hard to make a useful judgement statically. In the presence
842 of operator overloading, it's impossible to tell if a call will
843 execute new Python code or not.
844
845 It's a case-by-case judgement. I'll use intr1 for the following
846 cases:
847
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000848 IMPORT_STAR
849 IMPORT_FROM
850 CALL_FUNCTION (and friends)
851
852 */
853 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
854 int ticked = 0;
855
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000856 READ_TIMESTAMP(inst0);
857 READ_TIMESTAMP(inst1);
858 READ_TIMESTAMP(loop0);
859 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000860
861 /* shut up the compiler */
862 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000863#endif
864
Guido van Rossum374a9221991-04-04 10:40:29 +0000865/* Code access macros */
866
Martin v. Löwis18e16552006-02-15 17:27:45 +0000867#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000868#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000869#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000870#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000871#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000872#define JUMPBY(x) (next_instr += (x))
873
Raymond Hettingerf606f872003-03-16 03:11:04 +0000874/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000875 Some opcodes tend to come in pairs thus making it possible to
876 predict the second code when the first is run. For example,
877 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
878 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000879
Georg Brandl86b2fb92008-07-16 03:43:04 +0000880 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000881 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000882 processor's own internal branch predication has a high likelihood of
883 success, resulting in a nearly zero-overhead transition to the
884 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000885 including its two unpredictable branches, the HAS_ARG test and the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000886 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000887 a successful PREDICT has the effect of making the two opcodes run as if
Georg Brandl86b2fb92008-07-16 03:43:04 +0000888 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000889
Georg Brandl86b2fb92008-07-16 03:43:04 +0000890 If collecting opcode statistics, your choices are to either keep the
891 predictions turned-on and interpret the results as if some opcodes
892 had been combined or turn-off predictions so that the opcode frequency
893 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894
895 Opcode prediction is disabled with threaded code, since the latter allows
896 the CPU to record separate branch prediction information for each
897 opcode.
898
Raymond Hettingerf606f872003-03-16 03:11:04 +0000899*/
900
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Raymond Hettingera7216982004-02-08 19:59:27 +0000902#define PREDICT(op) if (0) goto PRED_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000903#define PREDICTED(op) PRED_##op:
904#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000905#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000906#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000907#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000908#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +0000909#endif
910
Raymond Hettingerf606f872003-03-16 03:11:04 +0000911
Guido van Rossum374a9221991-04-04 10:40:29 +0000912/* Stack manipulation macros */
913
Martin v. Löwis18e16552006-02-15 17:27:45 +0000914/* The stack can grow at most MAXINT deep, as co_nlocals and
915 co_stacksize are ints. */
916#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000917#define EMPTY() (STACK_LEVEL() == 0)
918#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000919#define SECOND() (stack_pointer[-2])
920#define THIRD() (stack_pointer[-3])
921#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000922#define SET_TOP(v) (stack_pointer[-1] = (v))
923#define SET_SECOND(v) (stack_pointer[-2] = (v))
924#define SET_THIRD(v) (stack_pointer[-3] = (v))
925#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000926#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000927#define BASIC_PUSH(v) (*stack_pointer++ = (v))
928#define BASIC_POP() (*--stack_pointer)
929
Guido van Rossum96a42c81992-01-12 02:29:51 +0000930#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000931#define PUSH(v) { (void)(BASIC_PUSH(v), \
932 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000933 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000934#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
935 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000936#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
937 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000938 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000939#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
940 prtrace((STACK_POINTER)[-1], "ext_pop")), \
941 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000942#else
943#define PUSH(v) BASIC_PUSH(v)
944#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000945#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000946#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000947#endif
948
Guido van Rossum681d79a1995-07-18 14:51:37 +0000949/* Local variable macros */
950
951#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000952
953/* The SETLOCAL() macro must not DECREF the local variable in-place and
954 then store the new value; it must copy the old value to a temporary
955 value, then store the new value, and then DECREF the temporary value.
956 This is because it is possible that during the DECREF the frame is
957 accessed by other code (e.g. a __del__ method or gc.collect()) and the
958 variable would be pointing to already-freed memory. */
959#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
960 GETLOCAL(i) = value; \
961 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000962
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000963
964#define UNWIND_BLOCK(b) \
965 while (STACK_LEVEL() > (b)->b_level) { \
966 PyObject *v = POP(); \
967 Py_XDECREF(v); \
968 }
969
970#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000971 { \
972 PyObject *type, *value, *traceback; \
973 assert(STACK_LEVEL() >= (b)->b_level + 3); \
974 while (STACK_LEVEL() > (b)->b_level + 3) { \
975 value = POP(); \
976 Py_XDECREF(value); \
977 } \
978 type = tstate->exc_type; \
979 value = tstate->exc_value; \
980 traceback = tstate->exc_traceback; \
981 tstate->exc_type = POP(); \
982 tstate->exc_value = POP(); \
983 tstate->exc_traceback = POP(); \
984 Py_XDECREF(type); \
985 Py_XDECREF(value); \
986 Py_XDECREF(traceback); \
987 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000988
989#define SAVE_EXC_STATE() \
990 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000991 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000992 Py_XINCREF(tstate->exc_type); \
993 Py_XINCREF(tstate->exc_value); \
994 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000995 type = f->f_exc_type; \
996 value = f->f_exc_value; \
997 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000998 f->f_exc_type = tstate->exc_type; \
999 f->f_exc_value = tstate->exc_value; \
1000 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001001 Py_XDECREF(type); \
1002 Py_XDECREF(value); \
1003 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001004 }
1005
1006#define SWAP_EXC_STATE() \
1007 { \
1008 PyObject *tmp; \
1009 tmp = tstate->exc_type; \
1010 tstate->exc_type = f->f_exc_type; \
1011 f->f_exc_type = tmp; \
1012 tmp = tstate->exc_value; \
1013 tstate->exc_value = f->f_exc_value; \
1014 f->f_exc_value = tmp; \
1015 tmp = tstate->exc_traceback; \
1016 tstate->exc_traceback = f->f_exc_traceback; \
1017 f->f_exc_traceback = tmp; \
1018 }
1019
Guido van Rossuma027efa1997-05-05 20:56:21 +00001020/* Start of code */
1021
Tim Peters5ca576e2001-06-18 22:08:13 +00001022 if (f == NULL)
1023 return NULL;
1024
Armin Rigo1d313ab2003-10-25 14:33:09 +00001025 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001026 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +00001027 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001028
Tim Peters5ca576e2001-06-18 22:08:13 +00001029 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001030
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001031 if (tstate->use_tracing) {
1032 if (tstate->c_tracefunc != NULL) {
1033 /* tstate->c_tracefunc, if defined, is a
1034 function that will be called on *every* entry
1035 to a code block. Its return value, if not
1036 None, is a function that will be called at
1037 the start of each executed line of code.
1038 (Actually, the function must return itself
1039 in order to continue tracing.) The trace
1040 functions are called with three arguments:
1041 a pointer to the current frame, a string
1042 indicating why the function is called, and
1043 an argument which depends on the situation.
1044 The global trace function is also called
1045 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001046 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001047 tstate->c_traceobj,
1048 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001049 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001050 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001051 }
1052 }
1053 if (tstate->c_profilefunc != NULL) {
1054 /* Similar for c_profilefunc, except it needn't
1055 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001056 if (call_trace_protected(tstate->c_profilefunc,
1057 tstate->c_profileobj,
1058 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001059 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001060 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001061 }
1062 }
1063 }
1064
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001065 co = f->f_code;
1066 names = co->co_names;
1067 consts = co->co_consts;
1068 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001069 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +00001070 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001071 /* An explanation is in order for the next line.
1072
1073 f->f_lasti now refers to the index of the last instruction
1074 executed. You might think this was obvious from the name, but
1075 this wasn't always true before 2.3! PyFrame_New now sets
1076 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1077 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001078 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001079
1080 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001081 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001082 prediction effectively links the two codes together as if they
1083 were a single new opcode; accordingly,f->f_lasti will point to
1084 the first code in the pair (for instance, GET_ITER followed by
1085 FOR_ITER is effectively a single opcode and f->f_lasti will point
1086 at to the beginning of the combined pair.)
1087 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001088 next_instr = first_instr + f->f_lasti + 1;
1089 stack_pointer = f->f_stacktop;
1090 assert(stack_pointer != NULL);
1091 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1092
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001093 if (f->f_code->co_flags & CO_GENERATOR) {
1094 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1095 /* We were in an except handler when we left,
1096 restore the exception state which was put aside
1097 (see YIELD_VALUE). */
1098 SWAP_EXC_STATE();
1099 }
1100 else {
1101 SAVE_EXC_STATE();
1102 }
1103 }
1104
Tim Peters5ca576e2001-06-18 22:08:13 +00001105#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001107#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001108#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001109 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001110#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001111
Guido van Rossum374a9221991-04-04 10:40:29 +00001112 why = WHY_NOT;
1113 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00001114 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +00001115 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001116
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001117 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001118 why = WHY_EXCEPTION;
1119 goto on_error;
1120 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001121
Guido van Rossum374a9221991-04-04 10:40:29 +00001122 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001123#ifdef WITH_TSC
1124 if (inst1 == 0) {
1125 /* Almost surely, the opcode executed a break
1126 or a continue, preventing inst1 from being set
1127 on the way out of the loop.
1128 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001129 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001130 loop1 = inst1;
1131 }
1132 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1133 intr0, intr1);
1134 ticked = 0;
1135 inst1 = 0;
1136 intr0 = 0;
1137 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001138 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001139#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001140 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001141 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001142
Guido van Rossuma027efa1997-05-05 20:56:21 +00001143 /* Do periodic things. Doing this every time through
1144 the loop would add too much overhead, so we do it
1145 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001146 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001147 event needs attention (e.g. a signal handler or
1148 async I/O handler); see Py_AddPendingCall() and
1149 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001150
Skip Montanarod581d772002-09-03 20:10:45 +00001151 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001152 if (*next_instr == SETUP_FINALLY) {
1153 /* Make the last opcode before
1154 a try: finally: block uninterruptable. */
1155 goto fast_next_opcode;
1156 }
Skip Montanarod581d772002-09-03 20:10:45 +00001157 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001158 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001159#ifdef WITH_TSC
1160 ticked = 1;
1161#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001162 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001163 if (Py_MakePendingCalls() < 0) {
1164 why = WHY_EXCEPTION;
1165 goto on_error;
1166 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001167 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +00001168 /* MakePendingCalls() didn't succeed.
1169 Force early re-execution of this
1170 "periodic" code, possibly after
1171 a thread switch */
1172 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +00001173 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001174#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001175 if (interpreter_lock) {
1176 /* Give another thread a chance */
1177
Guido van Rossum25ce5661997-08-02 03:10:38 +00001178 if (PyThreadState_Swap(NULL) != tstate)
1179 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +00001180 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001181
1182 /* Other threads may run now */
1183
Guido van Rossum65d5b571998-12-21 19:32:43 +00001184 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001185 if (PyThreadState_Swap(tstate) != NULL)
1186 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001187
1188 /* Check for thread interrupts */
1189
1190 if (tstate->async_exc != NULL) {
1191 x = tstate->async_exc;
1192 tstate->async_exc = NULL;
1193 PyErr_SetNone(x);
1194 Py_DECREF(x);
1195 why = WHY_EXCEPTION;
1196 goto on_error;
1197 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001198 }
1199#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001200 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001201
Neil Schemenauer63543862002-02-17 19:10:14 +00001202 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001203 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001204
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001205 /* line-by-line tracing support */
1206
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001207 if (_Py_TracingPossible &&
1208 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001209 /* see maybe_call_line_trace
1210 for expository comments */
1211 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001212
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001213 err = maybe_call_line_trace(tstate->c_tracefunc,
1214 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001215 f, &instr_lb, &instr_ub,
1216 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001217 /* Reload possibly changed frame fields */
1218 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001219 if (f->f_stacktop != NULL) {
1220 stack_pointer = f->f_stacktop;
1221 f->f_stacktop = NULL;
1222 }
1223 if (err) {
1224 /* trace function raised an exception */
1225 goto on_error;
1226 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001227 }
1228
1229 /* Extract opcode and argument */
1230
Guido van Rossum374a9221991-04-04 10:40:29 +00001231 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001232 oparg = 0; /* allows oparg to be stored in a register because
1233 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001234 if (HAS_ARG(opcode))
1235 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001236 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001237#ifdef DYNAMIC_EXECUTION_PROFILE
1238#ifdef DXPAIRS
1239 dxpairs[lastopcode][opcode]++;
1240 lastopcode = opcode;
1241#endif
1242 dxp[opcode]++;
1243#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001244
Guido van Rossum96a42c81992-01-12 02:29:51 +00001245#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001246 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Guido van Rossum96a42c81992-01-12 02:29:51 +00001248 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001249 if (HAS_ARG(opcode)) {
1250 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001251 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001252 }
1253 else {
1254 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001255 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001256 }
1257 }
1258#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001259
Guido van Rossum374a9221991-04-04 10:40:29 +00001260 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001261 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001262
Guido van Rossum374a9221991-04-04 10:40:29 +00001263 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Guido van Rossum374a9221991-04-04 10:40:29 +00001265 /* BEWARE!
1266 It is essential that any operation that fails sets either
1267 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1268 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Guido van Rossum374a9221991-04-04 10:40:29 +00001270 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001271
Antoine Pitroub52ec782009-01-25 16:34:23 +00001272 TARGET(NOP)
1273 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001274
Antoine Pitroub52ec782009-01-25 16:34:23 +00001275 TARGET(LOAD_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001276 x = GETLOCAL(oparg);
1277 if (x != NULL) {
1278 Py_INCREF(x);
1279 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001280 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001281 }
1282 format_exc_check_arg(PyExc_UnboundLocalError,
1283 UNBOUNDLOCAL_ERROR_MSG,
1284 PyTuple_GetItem(co->co_varnames, oparg));
1285 break;
1286
Antoine Pitroub52ec782009-01-25 16:34:23 +00001287 TARGET(LOAD_CONST)
Skip Montanaro04d80f82002-08-04 21:03:35 +00001288 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001289 Py_INCREF(x);
1290 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001291 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001292
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001293 PREDICTED_WITH_ARG(STORE_FAST);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001294 TARGET(STORE_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001295 v = POP();
1296 SETLOCAL(oparg, v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001297 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001298
Antoine Pitroub52ec782009-01-25 16:34:23 +00001299 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001300 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001301 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001302 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001303
Antoine Pitroub52ec782009-01-25 16:34:23 +00001304 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 v = TOP();
1306 w = SECOND();
1307 SET_TOP(w);
1308 SET_SECOND(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001309 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Antoine Pitroub52ec782009-01-25 16:34:23 +00001311 TARGET(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001312 v = TOP();
1313 w = SECOND();
1314 x = THIRD();
1315 SET_TOP(w);
1316 SET_SECOND(x);
1317 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001318 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Antoine Pitroub52ec782009-01-25 16:34:23 +00001320 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 u = TOP();
1322 v = SECOND();
1323 w = THIRD();
1324 x = FOURTH();
1325 SET_TOP(v);
1326 SET_SECOND(w);
1327 SET_THIRD(x);
1328 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001329 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Antoine Pitroub52ec782009-01-25 16:34:23 +00001331 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001332 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001333 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001334 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001335 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Antoine Pitroub52ec782009-01-25 16:34:23 +00001337 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001338 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001339 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001340 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001342 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001343 STACKADJ(2);
1344 SET_TOP(x);
1345 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001346 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001347 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001349 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001351 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001352 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001353 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 STACKADJ(3);
1355 SET_TOP(x);
1356 SET_SECOND(w);
1357 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001358 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001360 Py_FatalError("invalid argument to DUP_TOPX"
1361 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001362 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001363 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001364
Antoine Pitroub52ec782009-01-25 16:34:23 +00001365 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001366 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001367 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001368 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001369 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001370 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001371 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Antoine Pitroub52ec782009-01-25 16:34:23 +00001373 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001375 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001376 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001378 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001379 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Antoine Pitroub52ec782009-01-25 16:34:23 +00001381 TARGET(UNARY_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001382 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001383 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001384 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001385 if (err == 0) {
1386 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001387 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001388 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001389 }
1390 else if (err > 0) {
1391 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001392 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001393 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001394 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001395 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001396 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Antoine Pitroub52ec782009-01-25 16:34:23 +00001399 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001400 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001401 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001402 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001403 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001404 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001405 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Antoine Pitroub52ec782009-01-25 16:34:23 +00001407 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001408 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001409 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001410 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001411 Py_DECREF(v);
1412 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001413 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001414 if (x != NULL) DISPATCH();
Guido van Rossum50564e81996-01-12 01:13:16 +00001415 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001416
Antoine Pitroub52ec782009-01-25 16:34:23 +00001417 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001418 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001419 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001420 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001421 Py_DECREF(v);
1422 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001423 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001424 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001425 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Antoine Pitroub52ec782009-01-25 16:34:23 +00001427 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001428 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001429 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001430 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001431 Py_DECREF(v);
1432 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001433 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001434 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001436
Antoine Pitroub52ec782009-01-25 16:34:23 +00001437 TARGET(BINARY_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001438 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001439 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001440 x = PyNumber_FloorDivide(v, w);
1441 Py_DECREF(v);
1442 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001443 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001444 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001445 break;
1446
Antoine Pitroub52ec782009-01-25 16:34:23 +00001447 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001448 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001449 v = TOP();
Benjamin Petersonefb06b02009-02-26 18:55:48 +00001450 if (PyUnicode_CheckExact(v))
1451 x = PyUnicode_Format(v, w);
1452 else
1453 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001454 Py_DECREF(v);
1455 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001456 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001457 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001458 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001459
Antoine Pitroub52ec782009-01-25 16:34:23 +00001460 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001461 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001462 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001463 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001464 PyUnicode_CheckExact(w)) {
1465 x = unicode_concatenate(v, w, f, next_instr);
1466 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001467 goto skip_decref_vx;
1468 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001469 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001470 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001471 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001472 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001473 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001474 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001475 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001476 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001478
Antoine Pitroub52ec782009-01-25 16:34:23 +00001479 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001480 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001481 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001482 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001483 Py_DECREF(v);
1484 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001485 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001486 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001487 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Antoine Pitroub52ec782009-01-25 16:34:23 +00001489 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001490 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001491 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001492 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001493 Py_DECREF(v);
1494 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001495 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001496 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Antoine Pitroub52ec782009-01-25 16:34:23 +00001499 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001500 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001501 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001502 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001503 Py_DECREF(v);
1504 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001505 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001506 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001507 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Antoine Pitroub52ec782009-01-25 16:34:23 +00001509 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001510 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001511 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001512 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001513 Py_DECREF(v);
1514 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001515 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001516 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001517 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001518
Antoine Pitroub52ec782009-01-25 16:34:23 +00001519 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001520 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001521 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001522 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001523 Py_DECREF(v);
1524 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001525 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001526 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001527 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Antoine Pitroub52ec782009-01-25 16:34:23 +00001529 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001530 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001531 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001532 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001533 Py_DECREF(v);
1534 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001535 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001536 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001537 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Antoine Pitroub52ec782009-01-25 16:34:23 +00001539 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001540 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001541 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001542 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001543 Py_DECREF(v);
1544 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001545 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001546 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001547 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001548
Antoine Pitroub52ec782009-01-25 16:34:23 +00001549 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001550 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001551 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001552 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001553 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001554 if (err == 0) {
1555 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001556 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001557 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001558 break;
1559
Antoine Pitroub52ec782009-01-25 16:34:23 +00001560 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001561 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001562 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001563 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001564 Py_DECREF(w);
1565 if (err == 0) {
1566 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001567 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001568 }
1569 break;
1570
Antoine Pitroub52ec782009-01-25 16:34:23 +00001571 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001572 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001573 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001574 x = PyNumber_InPlacePower(v, w, Py_None);
1575 Py_DECREF(v);
1576 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001577 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001578 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001579 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Antoine Pitroub52ec782009-01-25 16:34:23 +00001581 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001582 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001583 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001584 x = PyNumber_InPlaceMultiply(v, w);
1585 Py_DECREF(v);
1586 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001587 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001588 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001589 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001590
Antoine Pitroub52ec782009-01-25 16:34:23 +00001591 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001592 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001593 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001594 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001595 Py_DECREF(v);
1596 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001597 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001598 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001599 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001600
Antoine Pitroub52ec782009-01-25 16:34:23 +00001601 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001602 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001603 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001604 x = PyNumber_InPlaceFloorDivide(v, w);
1605 Py_DECREF(v);
1606 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001607 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001608 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001609 break;
1610
Antoine Pitroub52ec782009-01-25 16:34:23 +00001611 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001612 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001613 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001614 x = PyNumber_InPlaceRemainder(v, w);
1615 Py_DECREF(v);
1616 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001617 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001618 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001619 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001620
Antoine Pitroub52ec782009-01-25 16:34:23 +00001621 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001622 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001623 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001624 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001625 PyUnicode_CheckExact(w)) {
1626 x = unicode_concatenate(v, w, f, next_instr);
1627 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001628 goto skip_decref_v;
1629 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001630 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001631 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001632 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001633 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001634 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001635 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001636 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001637 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001638 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001639
Antoine Pitroub52ec782009-01-25 16:34:23 +00001640 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001641 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001642 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001643 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001644 Py_DECREF(v);
1645 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001646 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001647 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001648 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001649
Antoine Pitroub52ec782009-01-25 16:34:23 +00001650 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001651 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001652 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001653 x = PyNumber_InPlaceLshift(v, w);
1654 Py_DECREF(v);
1655 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001656 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001657 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001658 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Antoine Pitroub52ec782009-01-25 16:34:23 +00001660 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001661 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001662 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001663 x = PyNumber_InPlaceRshift(v, w);
1664 Py_DECREF(v);
1665 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001666 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001667 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001668 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001669
Antoine Pitroub52ec782009-01-25 16:34:23 +00001670 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001671 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001672 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001673 x = PyNumber_InPlaceAnd(v, w);
1674 Py_DECREF(v);
1675 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001676 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001677 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001678 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001679
Antoine Pitroub52ec782009-01-25 16:34:23 +00001680 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001681 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001682 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001683 x = PyNumber_InPlaceXor(v, w);
1684 Py_DECREF(v);
1685 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001686 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001687 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001688 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Antoine Pitroub52ec782009-01-25 16:34:23 +00001690 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001691 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001692 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001693 x = PyNumber_InPlaceOr(v, w);
1694 Py_DECREF(v);
1695 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001696 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001697 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001698 break;
1699
Antoine Pitroub52ec782009-01-25 16:34:23 +00001700 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001701 w = TOP();
1702 v = SECOND();
1703 u = THIRD();
1704 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001705 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001706 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001707 Py_DECREF(u);
1708 Py_DECREF(v);
1709 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001710 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001711 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Antoine Pitroub52ec782009-01-25 16:34:23 +00001713 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001714 w = TOP();
1715 v = SECOND();
1716 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001718 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001719 Py_DECREF(v);
1720 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001721 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001723
Antoine Pitroub52ec782009-01-25 16:34:23 +00001724 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001726 w = PySys_GetObject("displayhook");
1727 if (w == NULL) {
1728 PyErr_SetString(PyExc_RuntimeError,
1729 "lost sys.displayhook");
1730 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001731 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001732 }
1733 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001734 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001735 if (x == NULL)
1736 err = -1;
1737 }
1738 if (err == 0) {
1739 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001740 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001741 if (w == NULL)
1742 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001745 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001746 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001747
Thomas Wouters434d0822000-08-24 20:11:32 +00001748#ifdef CASE_TOO_BIG
1749 default: switch (opcode) {
1750#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001751 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001752 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001753 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001754 case 2:
1755 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001756 case 1:
1757 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001758 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001759 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001760 break;
1761 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001762 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001763 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001764 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001765 break;
1766 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001767 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Antoine Pitroub52ec782009-01-25 16:34:23 +00001769 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001770 x = POP();
1771 v = f->f_locals;
1772 Py_XDECREF(v);
1773 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001774 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Antoine Pitroub52ec782009-01-25 16:34:23 +00001776 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001777 retval = POP();
1778 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001779 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001780
Antoine Pitroub52ec782009-01-25 16:34:23 +00001781 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001782 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001783 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001784 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001785 /* Put aside the current exception state and restore
1786 that of the calling frame. This only serves when
1787 "yield" is used inside an except handler. */
1788 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001789 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001790
Antoine Pitroub52ec782009-01-25 16:34:23 +00001791 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001792 {
1793 PyTryBlock *b = PyFrame_BlockPop(f);
1794 if (b->b_type != EXCEPT_HANDLER) {
1795 PyErr_SetString(PyExc_SystemError,
1796 "popped block is not an except handler");
1797 why = WHY_EXCEPTION;
1798 break;
1799 }
1800 UNWIND_EXCEPT_HANDLER(b);
1801 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001802 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001803
Antoine Pitroub52ec782009-01-25 16:34:23 +00001804 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001806 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001807 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001808 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001809 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Christian Heimes180510d2008-03-03 19:15:45 +00001811 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001812 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001813 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001814 if (PyLong_Check(v)) {
1815 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001816 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001817 if (why == WHY_RETURN ||
1818 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001819 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001820 if (why == WHY_SILENCED) {
1821 /* An exception was silenced by 'with', we must
1822 manually unwind the EXCEPT_HANDLER block which was
1823 created when the exception was caught, otherwise
1824 the stack will be in an inconsistent state. */
1825 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersonac8c7302009-06-28 16:03:15 +00001826 assert(b->b_type == EXCEPT_HANDLER);
1827 UNWIND_EXCEPT_HANDLER(b);
1828 why = WHY_NOT;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001829 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001830 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001831 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001833 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001834 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001835 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001836 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001838 else if (v != Py_None) {
1839 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 "'finally' pops bad exception");
1841 why = WHY_EXCEPTION;
1842 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001843 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001844 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001845
Antoine Pitroub52ec782009-01-25 16:34:23 +00001846 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001847 x = PyDict_GetItemString(f->f_builtins,
1848 "__build_class__");
1849 if (x == NULL) {
1850 PyErr_SetString(PyExc_ImportError,
1851 "__build_class__ not found");
1852 break;
1853 }
1854 Py_INCREF(x);
1855 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001856 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001857
Antoine Pitroub52ec782009-01-25 16:34:23 +00001858 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001859 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001861 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001862 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001863 err = PyDict_SetItem(x, w, v);
1864 else
1865 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001866 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001867 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001868 break;
1869 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001870 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001871 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001872 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001873
Antoine Pitroub52ec782009-01-25 16:34:23 +00001874 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001875 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001876 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001877 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001878 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001879 NAME_ERROR_MSG,
1880 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001881 break;
1882 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001883 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001884 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001885 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001886
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001887 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001888 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001889 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001890 if (PyTuple_CheckExact(v) &&
1891 PyTuple_GET_SIZE(v) == oparg) {
1892 PyObject **items = \
1893 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001894 while (oparg--) {
1895 w = items[oparg];
1896 Py_INCREF(w);
1897 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001898 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001899 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001900 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001901 } else if (PyList_CheckExact(v) &&
1902 PyList_GET_SIZE(v) == oparg) {
1903 PyObject **items = \
1904 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001905 while (oparg--) {
1906 w = items[oparg];
1907 Py_INCREF(w);
1908 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001909 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001910 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001911 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001912 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001913 } else {
1914 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001915 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001916 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001917 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001918 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001919
Antoine Pitroub52ec782009-01-25 16:34:23 +00001920 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00001921 {
1922 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1923 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001924
Guido van Rossum0368b722007-05-11 16:50:42 +00001925 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1926 stack_pointer + totalargs)) {
1927 stack_pointer += totalargs;
1928 } else {
1929 why = WHY_EXCEPTION;
1930 }
1931 Py_DECREF(v);
1932 break;
1933 }
1934
Antoine Pitroub52ec782009-01-25 16:34:23 +00001935 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001936 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001937 v = TOP();
1938 u = SECOND();
1939 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001940 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1941 Py_DECREF(v);
1942 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001943 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001944 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001945
Antoine Pitroub52ec782009-01-25 16:34:23 +00001946 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001947 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001948 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001949 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1950 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001951 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001952 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001953
Antoine Pitroub52ec782009-01-25 16:34:23 +00001954 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001955 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001956 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 err = PyDict_SetItem(f->f_globals, w, v);
1958 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001959 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001960 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001961
Antoine Pitroub52ec782009-01-25 16:34:23 +00001962 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001963 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001964 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001965 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001966 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001967 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Antoine Pitroub52ec782009-01-25 16:34:23 +00001969 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001970 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001971 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001972 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001973 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001974 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001975 break;
1976 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001977 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001978 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001979 Py_XINCREF(x);
1980 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001981 else {
1982 x = PyObject_GetItem(v, w);
1983 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001984 if (!PyErr_ExceptionMatches(
1985 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001986 break;
1987 PyErr_Clear();
1988 }
1989 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001990 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001991 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001992 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001993 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001994 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001995 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001996 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001997 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001998 break;
1999 }
2000 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002001 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002002 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002003 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002004 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002005
Antoine Pitroub52ec782009-01-25 16:34:23 +00002006 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002007 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002008 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002009 /* Inline the PyDict_GetItem() calls.
2010 WARNING: this is an extreme speed hack.
2011 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002012 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002013 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002014 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002015 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002016 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002017 e = d->ma_lookup(d, w, hash);
2018 if (e == NULL) {
2019 x = NULL;
2020 break;
2021 }
2022 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002023 if (x != NULL) {
2024 Py_INCREF(x);
2025 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002026 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002027 }
2028 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002029 e = d->ma_lookup(d, w, hash);
2030 if (e == NULL) {
2031 x = NULL;
2032 break;
2033 }
2034 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002035 if (x != NULL) {
2036 Py_INCREF(x);
2037 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002038 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002039 }
2040 goto load_global_error;
2041 }
2042 }
2043 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002044 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002045 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002046 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002048 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002049 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002050 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002051 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002052 break;
2053 }
2054 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002055 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002056 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002057 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002058
Antoine Pitroub52ec782009-01-25 16:34:23 +00002059 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002060 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002061 if (x != NULL) {
2062 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002063 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002064 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002065 format_exc_check_arg(
2066 PyExc_UnboundLocalError,
2067 UNBOUNDLOCAL_ERROR_MSG,
2068 PyTuple_GetItem(co->co_varnames, oparg)
2069 );
2070 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002071
Antoine Pitroub52ec782009-01-25 16:34:23 +00002072 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002073 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002074 Py_INCREF(x);
2075 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002076 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002077 break;
2078
Antoine Pitroub52ec782009-01-25 16:34:23 +00002079 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002080 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002081 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002082 if (w != NULL) {
2083 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002084 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002085 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002086 err = -1;
2087 /* Don't stomp existing exception */
2088 if (PyErr_Occurred())
2089 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002090 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2091 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002092 oparg);
2093 format_exc_check_arg(
2094 PyExc_UnboundLocalError,
2095 UNBOUNDLOCAL_ERROR_MSG,
2096 v);
2097 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002098 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2099 PyTuple_GET_SIZE(co->co_cellvars));
2100 format_exc_check_arg(PyExc_NameError,
2101 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002102 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002103 break;
2104
Antoine Pitroub52ec782009-01-25 16:34:23 +00002105 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002106 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002107 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002108 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002109 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002110 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002111
Antoine Pitroub52ec782009-01-25 16:34:23 +00002112 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002113 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002114 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002115 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002116 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002117 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002118 }
2119 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002120 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002121 }
2122 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002123
Antoine Pitroub52ec782009-01-25 16:34:23 +00002124 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002125 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002126 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002127 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002128 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002129 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002130 }
2131 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002132 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002133 }
2134 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002135
Antoine Pitroub52ec782009-01-25 16:34:23 +00002136 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002137 x = PySet_New(NULL);
2138 if (x != NULL) {
2139 for (; --oparg >= 0;) {
2140 w = POP();
2141 if (err == 0)
2142 err = PySet_Add(x, w);
2143 Py_DECREF(w);
2144 }
2145 if (err != 0) {
2146 Py_DECREF(x);
2147 break;
2148 }
2149 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002150 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002151 }
2152 break;
2153
Antoine Pitroub52ec782009-01-25 16:34:23 +00002154 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002155 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002156 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002157 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002158 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002159
Antoine Pitroub52ec782009-01-25 16:34:23 +00002160 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002161 w = TOP(); /* key */
2162 u = SECOND(); /* value */
2163 v = THIRD(); /* dict */
2164 STACKADJ(-2);
2165 assert (PyDict_CheckExact(v));
2166 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2167 Py_DECREF(u);
2168 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002169 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002170 break;
2171
Antoine Pitroub52ec782009-01-25 16:34:23 +00002172 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002173 w = TOP(); /* key */
2174 u = SECOND(); /* value */
2175 STACKADJ(-2);
2176 v = stack_pointer[-oparg]; /* dict */
2177 assert (PyDict_CheckExact(v));
2178 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2179 Py_DECREF(u);
2180 Py_DECREF(w);
2181 if (err == 0) {
2182 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002183 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002184 }
2185 break;
2186
Antoine Pitroub52ec782009-01-25 16:34:23 +00002187 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002188 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002189 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002190 x = PyObject_GetAttr(v, w);
2191 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002192 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002193 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002195
Antoine Pitroub52ec782009-01-25 16:34:23 +00002196 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002197 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002198 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002199 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002200 Py_DECREF(v);
2201 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002202 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002203 if (x == NULL) break;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002204 PREDICT(POP_JUMP_IF_FALSE);
2205 PREDICT(POP_JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002206 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002207
Antoine Pitroub52ec782009-01-25 16:34:23 +00002208 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002209 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002210 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002211 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002212 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002213 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002214 break;
2215 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002216 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002217 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002218 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002219 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002220 w = PyTuple_Pack(5,
2221 w,
2222 f->f_globals,
2223 f->f_locals == NULL ?
2224 Py_None : f->f_locals,
2225 v,
2226 u);
2227 else
2228 w = PyTuple_Pack(4,
2229 w,
2230 f->f_globals,
2231 f->f_locals == NULL ?
2232 Py_None : f->f_locals,
2233 v);
2234 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002235 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002236 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002237 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002238 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002239 x = NULL;
2240 break;
2241 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002242 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002243 v = x;
2244 x = PyEval_CallObject(v, w);
2245 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002246 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002247 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002248 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002249 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002250 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002251
Antoine Pitroub52ec782009-01-25 16:34:23 +00002252 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002253 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002254 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002255 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002256 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002257 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002258 break;
2259 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002260 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002261 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002262 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002263 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002264 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002265 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002266 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002267
Antoine Pitroub52ec782009-01-25 16:34:23 +00002268 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002269 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002270 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002271 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002272 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002273 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002274 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002275 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002276 break;
2277
Antoine Pitroub52ec782009-01-25 16:34:23 +00002278 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002279 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002280 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002281
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002282 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2283 TARGET(POP_JUMP_IF_FALSE)
2284 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002285 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002286 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002287 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002288 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002289 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002290 Py_DECREF(w);
2291 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002292 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002293 }
2294 err = PyObject_IsTrue(w);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002295 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002296 if (err > 0)
2297 err = 0;
2298 else if (err == 0)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002299 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002300 else
2301 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002302 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002303
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002304 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2305 TARGET(POP_JUMP_IF_TRUE)
2306 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002307 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002308 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002309 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002310 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002311 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002312 Py_DECREF(w);
2313 JUMPTO(oparg);
2314 FAST_DISPATCH();
2315 }
2316 err = PyObject_IsTrue(w);
2317 Py_DECREF(w);
2318 if (err > 0) {
2319 err = 0;
2320 JUMPTO(oparg);
2321 }
2322 else if (err == 0)
2323 ;
2324 else
2325 break;
2326 DISPATCH();
2327
2328 TARGET(JUMP_IF_FALSE_OR_POP)
2329 w = TOP();
2330 if (w == Py_True) {
2331 STACKADJ(-1);
2332 Py_DECREF(w);
2333 FAST_DISPATCH();
2334 }
2335 if (w == Py_False) {
2336 JUMPTO(oparg);
2337 FAST_DISPATCH();
2338 }
2339 err = PyObject_IsTrue(w);
2340 if (err > 0) {
2341 STACKADJ(-1);
2342 Py_DECREF(w);
2343 err = 0;
2344 }
2345 else if (err == 0)
2346 JUMPTO(oparg);
2347 else
2348 break;
2349 DISPATCH();
2350
2351 TARGET(JUMP_IF_TRUE_OR_POP)
2352 w = TOP();
2353 if (w == Py_False) {
2354 STACKADJ(-1);
2355 Py_DECREF(w);
2356 FAST_DISPATCH();
2357 }
2358 if (w == Py_True) {
2359 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002360 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002361 }
2362 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002363 if (err > 0) {
2364 err = 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002365 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002366 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002367 else if (err == 0) {
2368 STACKADJ(-1);
2369 Py_DECREF(w);
2370 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002371 else
2372 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002373 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002374
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002375 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002376 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002377 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002378#if FAST_LOOPS
2379 /* Enabling this path speeds-up all while and for-loops by bypassing
2380 the per-loop checks for signals. By default, this should be turned-off
2381 because it prevents detection of a control-break in tight loops like
2382 "while 1: pass". Compile with this option turned-on when you need
2383 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002384 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002385 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002386 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002387#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002388 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002389#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002390
Antoine Pitroub52ec782009-01-25 16:34:23 +00002391 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002392 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002393 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002394 x = PyObject_GetIter(v);
2395 Py_DECREF(v);
2396 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002397 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002398 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002399 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002400 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002401 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002402 break;
2403
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002404 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002405 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002406 /* before: [iter]; after: [iter, iter()] *or* [] */
2407 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002408 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002409 if (x != NULL) {
2410 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002411 PREDICT(STORE_FAST);
2412 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002413 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002414 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002415 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002416 if (!PyErr_ExceptionMatches(
2417 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002418 break;
2419 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002420 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002421 /* iterator ended normally */
2422 x = v = POP();
2423 Py_DECREF(v);
2424 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002425 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002426
Antoine Pitroub52ec782009-01-25 16:34:23 +00002427 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002428 why = WHY_BREAK;
2429 goto fast_block_end;
2430
Antoine Pitroub52ec782009-01-25 16:34:23 +00002431 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002432 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002433 if (!retval) {
2434 x = NULL;
2435 break;
2436 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002437 why = WHY_CONTINUE;
2438 goto fast_block_end;
2439
Antoine Pitroub52ec782009-01-25 16:34:23 +00002440 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2441 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2442 TARGET(SETUP_FINALLY)
2443 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002444 /* NOTE: If you add any new block-setup opcodes that
2445 are not try/except/finally handlers, you may need
2446 to update the PyGen_NeedsFinalizing() function.
2447 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002448
Guido van Rossumb209a111997-04-29 18:18:01 +00002449 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002450 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002451 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002453 TARGET(SETUP_WITH)
2454 {
2455 static PyObject *exit, *enter;
2456 w = TOP();
2457 x = special_lookup(w, "__exit__", &exit);
2458 if (!x)
2459 break;
2460 SET_TOP(x);
2461 u = special_lookup(w, "__enter__", &enter);
2462 Py_DECREF(w);
2463 if (!u) {
2464 x = NULL;
2465 break;
2466 }
2467 x = PyObject_CallFunctionObjArgs(u, NULL);
2468 Py_DECREF(u);
2469 if (!x)
2470 break;
2471 /* Setup the finally block before pushing the result
2472 of __enter__ on the stack. */
2473 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2474 STACK_LEVEL());
2475
2476 PUSH(x);
2477 DISPATCH();
2478 }
2479
Antoine Pitroub52ec782009-01-25 16:34:23 +00002480 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002481 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002482 /* At the top of the stack are 1-3 values indicating
2483 how/why we entered the finally clause:
2484 - TOP = None
2485 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2486 - TOP = WHY_*; no retval below it
2487 - (TOP, SECOND, THIRD) = exc_info()
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002488 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002489 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002490 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002491 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002492 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002493 EXIT(None, None, None)
2494
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002495 In the first two cases, we remove EXIT from the
2496 stack, leaving the rest in the same order. In the
2497 third case, we shift the bottom 3 values of the
2498 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002499
2500 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002501 *and* the function call returns a 'true' value, we
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002502 push WHY_SILENCED onto the stack. END_FINALLY will
2503 then not re-raise the exception. (But non-local
2504 gotos should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002505 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002506
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002507 PyObject *exit_func;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002508 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002509 if (u == Py_None) {
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002510 POP();
2511 exit_func = TOP();
2512 SET_TOP(u);
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002513 v = w = Py_None;
2514 }
2515 else if (PyLong_Check(u)) {
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002516 POP();
2517 switch(PyLong_AsLong(u)) {
2518 case WHY_RETURN:
2519 case WHY_CONTINUE:
2520 /* Retval in TOP. */
2521 exit_func = SECOND();
2522 SET_SECOND(TOP());
2523 SET_TOP(u);
2524 break;
2525 default:
2526 exit_func = TOP();
2527 SET_TOP(u);
2528 break;
2529 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002530 u = v = w = Py_None;
2531 }
2532 else {
Benjamin Peterson176101d2009-06-28 15:40:50 +00002533 PyObject *tp, *exc, *tb;
2534 PyTryBlock *block;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002535 v = SECOND();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002536 w = THIRD();
Benjamin Peterson176101d2009-06-28 15:40:50 +00002537 tp = FOURTH();
2538 exc = stack_pointer[-5];
2539 tb = stack_pointer[-6];
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002540 exit_func = stack_pointer[-7];
Benjamin Peterson176101d2009-06-28 15:40:50 +00002541 stack_pointer[-7] = tb;
2542 stack_pointer[-6] = exc;
2543 stack_pointer[-5] = tp;
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002544 /* UNWIND_EXCEPT_BLOCK will pop this off. */
Benjamin Peterson176101d2009-06-28 15:40:50 +00002545 FOURTH() = NULL;
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002546 /* We just shifted the stack down, so we have
2547 to tell the except handler block that the
2548 values are lower than it expects. */
Benjamin Peterson176101d2009-06-28 15:40:50 +00002549 block = &f->f_blockstack[f->f_iblock - 1];
2550 assert(block->b_type == EXCEPT_HANDLER);
2551 block->b_level--;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002552 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002553 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002554 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2555 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002556 Py_DECREF(exit_func);
2557 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002558 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002559
2560 if (u != Py_None)
2561 err = PyObject_IsTrue(x);
2562 else
2563 err = 0;
2564 Py_DECREF(x);
2565
2566 if (err < 0)
2567 break; /* Go to error exit */
2568 else if (err > 0) {
2569 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002570 /* There was an exception and a True return */
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002571 PUSH(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002572 }
Christian Heimes180510d2008-03-03 19:15:45 +00002573 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002574 break;
2575 }
2576
Antoine Pitroub52ec782009-01-25 16:34:23 +00002577 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002578 {
2579 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002580 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002581 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002582#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002583 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002584#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002585 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002586#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002587 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002588 PUSH(x);
2589 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002590 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002591 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002592 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002593
Antoine Pitroub52ec782009-01-25 16:34:23 +00002594 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2595 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2596 TARGET(CALL_FUNCTION_VAR_KW)
2597 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002598 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002599 int na = oparg & 0xff;
2600 int nk = (oparg>>8) & 0xff;
2601 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002602 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002603 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002604 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002605 if (flags & CALL_FLAG_VAR)
2606 n++;
2607 if (flags & CALL_FLAG_KW)
2608 n++;
2609 pfunc = stack_pointer - n - 1;
2610 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002611
Guido van Rossumac7be682001-01-17 15:42:30 +00002612 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002613 && PyMethod_GET_SELF(func) != NULL) {
2614 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002615 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002616 func = PyMethod_GET_FUNCTION(func);
2617 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002618 Py_DECREF(*pfunc);
2619 *pfunc = self;
2620 na++;
2621 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002622 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002623 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002624 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002625 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002626 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002627 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002628 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002629 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002630
Jeremy Hylton76901512000-03-28 23:49:17 +00002631 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002632 w = POP();
2633 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002634 }
2635 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002636 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002637 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002638 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002639 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002640
Antoine Pitroub52ec782009-01-25 16:34:23 +00002641 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2642 TARGET(MAKE_FUNCTION)
2643 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002644 {
2645 int posdefaults = oparg & 0xff;
2646 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002647 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002648
Guido van Rossum681d79a1995-07-18 14:51:37 +00002649 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002650 x = PyFunction_New(v, f->f_globals);
2651 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002652
Guido van Rossum0240b922007-02-26 21:23:50 +00002653 if (x != NULL && opcode == MAKE_CLOSURE) {
2654 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002655 if (PyFunction_SetClosure(x, v) != 0) {
2656 /* Can't happen unless bytecode is corrupt. */
2657 why = WHY_EXCEPTION;
2658 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002659 Py_DECREF(v);
2660 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002661
2662 if (x != NULL && num_annotations > 0) {
2663 Py_ssize_t name_ix;
2664 u = POP(); /* names of args with annotations */
2665 v = PyDict_New();
2666 if (v == NULL) {
2667 Py_DECREF(x);
2668 x = NULL;
2669 break;
2670 }
2671 name_ix = PyTuple_Size(u);
2672 assert(num_annotations == name_ix+1);
2673 while (name_ix > 0) {
2674 --name_ix;
2675 t = PyTuple_GET_ITEM(u, name_ix);
2676 w = POP();
2677 /* XXX(nnorwitz): check for errors */
2678 PyDict_SetItem(v, t, w);
2679 Py_DECREF(w);
2680 }
2681
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002682 if (PyFunction_SetAnnotations(x, v) != 0) {
2683 /* Can't happen unless
2684 PyFunction_SetAnnotations changes. */
2685 why = WHY_EXCEPTION;
2686 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002687 Py_DECREF(v);
2688 Py_DECREF(u);
2689 }
2690
Guido van Rossum681d79a1995-07-18 14:51:37 +00002691 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002692 if (x != NULL && posdefaults > 0) {
2693 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002694 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002695 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002696 x = NULL;
2697 break;
2698 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002699 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002700 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002701 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002702 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002703 if (PyFunction_SetDefaults(x, v) != 0) {
2704 /* Can't happen unless
2705 PyFunction_SetDefaults changes. */
2706 why = WHY_EXCEPTION;
2707 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002708 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002709 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002710 if (x != NULL && kwdefaults > 0) {
2711 v = PyDict_New();
2712 if (v == NULL) {
2713 Py_DECREF(x);
2714 x = NULL;
2715 break;
2716 }
2717 while (--kwdefaults >= 0) {
2718 w = POP(); /* default value */
2719 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002720 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002721 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002722 Py_DECREF(w);
2723 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002724 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002725 if (PyFunction_SetKwDefaults(x, v) != 0) {
2726 /* Can't happen unless
2727 PyFunction_SetKwDefaults changes. */
2728 why = WHY_EXCEPTION;
2729 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002730 Py_DECREF(v);
2731 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002732 PUSH(x);
2733 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002734 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002735
Antoine Pitroub52ec782009-01-25 16:34:23 +00002736 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002737 if (oparg == 3)
2738 w = POP();
2739 else
2740 w = NULL;
2741 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002742 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002743 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002744 Py_DECREF(u);
2745 Py_DECREF(v);
2746 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002747 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002748 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002749 break;
2750
Antoine Pitroub52ec782009-01-25 16:34:23 +00002751 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002752 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002753 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002754 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002755
Antoine Pitroub52ec782009-01-25 16:34:23 +00002756#ifdef USE_COMPUTED_GOTOS
2757 _unknown_opcode:
2758#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002759 default:
2760 fprintf(stderr,
2761 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002762 PyCode_Addr2Line(f->f_code, f->f_lasti),
2763 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002764 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002765 why = WHY_EXCEPTION;
2766 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002767
2768#ifdef CASE_TOO_BIG
2769 }
2770#endif
2771
Guido van Rossum374a9221991-04-04 10:40:29 +00002772 } /* switch */
2773
2774 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002775
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002776 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002777
Guido van Rossum374a9221991-04-04 10:40:29 +00002778 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002779
Guido van Rossum374a9221991-04-04 10:40:29 +00002780 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002781 if (err == 0 && x != NULL) {
2782#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002783 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002784 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002785 fprintf(stderr,
2786 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002787 else {
2788#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002789 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002790 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002791#ifdef CHECKEXC
2792 }
2793#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002794 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002795 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002796 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002797 err = 0;
2798 }
2799
Guido van Rossum374a9221991-04-04 10:40:29 +00002800 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002801
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002802 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002803 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002804 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002805 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002806 why = WHY_EXCEPTION;
2807 }
2808 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002809#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002810 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002811 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002812 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002813 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002814 sprintf(buf, "Stack unwind with exception "
2815 "set and why=%d", why);
2816 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002817 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002818 }
2819#endif
2820
2821 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002822
Guido van Rossum374a9221991-04-04 10:40:29 +00002823 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002824 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002825
Fred Drake8f51f542001-10-04 14:48:42 +00002826 if (tstate->c_tracefunc != NULL)
2827 call_exc_trace(tstate->c_tracefunc,
2828 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002829 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002830
Guido van Rossum374a9221991-04-04 10:40:29 +00002831 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002832
Guido van Rossum374a9221991-04-04 10:40:29 +00002833 if (why == WHY_RERAISE)
2834 why = WHY_EXCEPTION;
2835
2836 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002837
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002838fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002839 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002840 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002841
Tim Peters8a5c3c72004-04-05 19:36:21 +00002842 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002843 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2844 /* For a continue inside a try block,
2845 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002846 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2847 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002848 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002849 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002850 Py_DECREF(retval);
2851 break;
2852 }
2853
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002854 if (b->b_type == EXCEPT_HANDLER) {
2855 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002856 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002857 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002858 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002859 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2860 why = WHY_NOT;
2861 JUMPTO(b->b_handler);
2862 break;
2863 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002864 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2865 || b->b_type == SETUP_FINALLY)) {
2866 PyObject *exc, *val, *tb;
2867 int handler = b->b_handler;
2868 /* Beware, this invalidates all b->b_* fields */
2869 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2870 PUSH(tstate->exc_traceback);
2871 PUSH(tstate->exc_value);
2872 if (tstate->exc_type != NULL) {
2873 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002874 }
2875 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002876 Py_INCREF(Py_None);
2877 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002878 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002879 PyErr_Fetch(&exc, &val, &tb);
2880 /* Make the raw exception data
2881 available to the handler,
2882 so a program can emulate the
2883 Python main loop. */
2884 PyErr_NormalizeException(
2885 &exc, &val, &tb);
2886 PyException_SetTraceback(val, tb);
2887 Py_INCREF(exc);
2888 tstate->exc_type = exc;
2889 Py_INCREF(val);
2890 tstate->exc_value = val;
2891 tstate->exc_traceback = tb;
2892 if (tb == NULL)
2893 tb = Py_None;
2894 Py_INCREF(tb);
2895 PUSH(tb);
2896 PUSH(val);
2897 PUSH(exc);
2898 why = WHY_NOT;
2899 JUMPTO(handler);
2900 break;
2901 }
2902 if (b->b_type == SETUP_FINALLY) {
2903 if (why & (WHY_RETURN | WHY_CONTINUE))
2904 PUSH(retval);
2905 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002906 why = WHY_NOT;
2907 JUMPTO(b->b_handler);
2908 break;
2909 }
2910 } /* unwind stack */
2911
2912 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002913
Guido van Rossum374a9221991-04-04 10:40:29 +00002914 if (why != WHY_NOT)
2915 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002916 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002917
Guido van Rossum374a9221991-04-04 10:40:29 +00002918 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002919
Tim Peters8a5c3c72004-04-05 19:36:21 +00002920 assert(why != WHY_YIELD);
2921 /* Pop remaining stack entries. */
2922 while (!EMPTY()) {
2923 v = POP();
2924 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002925 }
2926
Tim Peters8a5c3c72004-04-05 19:36:21 +00002927 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002928 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002929
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002930fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002931 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002932 if (tstate->c_tracefunc) {
2933 if (why == WHY_RETURN || why == WHY_YIELD) {
2934 if (call_trace(tstate->c_tracefunc,
2935 tstate->c_traceobj, f,
2936 PyTrace_RETURN, retval)) {
2937 Py_XDECREF(retval);
2938 retval = NULL;
2939 why = WHY_EXCEPTION;
2940 }
2941 }
2942 else if (why == WHY_EXCEPTION) {
2943 call_trace_protected(tstate->c_tracefunc,
2944 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002945 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002946 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002947 }
Fred Drake8f51f542001-10-04 14:48:42 +00002948 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002949 if (why == WHY_EXCEPTION)
2950 call_trace_protected(tstate->c_profilefunc,
2951 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002952 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002953 else if (call_trace(tstate->c_profilefunc,
2954 tstate->c_profileobj, f,
2955 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002956 Py_XDECREF(retval);
2957 retval = NULL;
2958 why = WHY_EXCEPTION;
2959 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002960 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002961 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002962
Tim Peters5ca576e2001-06-18 22:08:13 +00002963 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002964exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002965 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002966 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002967
Guido van Rossum96a42c81992-01-12 02:29:51 +00002968 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002969}
2970
Guido van Rossumc2e20742006-02-27 22:32:47 +00002971/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002972 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002973 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002974
Tim Peters6d6c1a32001-08-02 04:15:00 +00002975PyObject *
2976PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002977 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002978 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002979{
2980 register PyFrameObject *f;
2981 register PyObject *retval = NULL;
2982 register PyObject **fastlocals, **freevars;
2983 PyThreadState *tstate = PyThreadState_GET();
2984 PyObject *x, *u;
2985
2986 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002987 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002988 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002989 return NULL;
2990 }
2991
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002992 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002993 assert(globals != NULL);
2994 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002995 if (f == NULL)
2996 return NULL;
2997
2998 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002999 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003000
3001 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00003002 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00003003 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3004 int i;
3005 int n = argcount;
3006 PyObject *kwdict = NULL;
3007 if (co->co_flags & CO_VARKEYWORDS) {
3008 kwdict = PyDict_New();
3009 if (kwdict == NULL)
3010 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003011 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003012 if (co->co_flags & CO_VARARGS)
3013 i++;
3014 SETLOCAL(i, kwdict);
3015 }
3016 if (argcount > co->co_argcount) {
3017 if (!(co->co_flags & CO_VARARGS)) {
3018 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003019 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003020 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003021 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003022 defcount ? "at most" : "exactly",
3023 co->co_argcount,
3024 kwcount ? "non-keyword " : "",
3025 co->co_argcount == 1 ? "" : "s",
3026 argcount);
3027 goto fail;
3028 }
3029 n = co->co_argcount;
3030 }
3031 for (i = 0; i < n; i++) {
3032 x = args[i];
3033 Py_INCREF(x);
3034 SETLOCAL(i, x);
3035 }
3036 if (co->co_flags & CO_VARARGS) {
3037 u = PyTuple_New(argcount - n);
3038 if (u == NULL)
3039 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003040 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00003041 for (i = n; i < argcount; i++) {
3042 x = args[i];
3043 Py_INCREF(x);
3044 PyTuple_SET_ITEM(u, i-n, x);
3045 }
3046 }
3047 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003048 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003049 PyObject *keyword = kws[2*i];
3050 PyObject *value = kws[2*i + 1];
3051 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00003052 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003053 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003054 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003055 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00003056 goto fail;
3057 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003058 /* Speed hack: do raw pointer compares. As names are
3059 normally interned this should almost always hit. */
3060 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003061 for (j = 0;
3062 j < co->co_argcount + co->co_kwonlyargcount;
3063 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003064 PyObject *nm = co_varnames[j];
3065 if (nm == keyword)
3066 goto kw_found;
3067 }
3068 /* Slow fallback, just in case */
3069 for (j = 0;
3070 j < co->co_argcount + co->co_kwonlyargcount;
3071 j++) {
3072 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003073 int cmp = PyObject_RichCompareBool(
3074 keyword, nm, Py_EQ);
3075 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003076 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003077 else if (cmp < 0)
3078 goto fail;
3079 }
3080 /* Check errors from Compare */
3081 if (PyErr_Occurred())
3082 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003083 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003084 if (kwdict == NULL) {
3085 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003086 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00003087 "keyword argument '%S'",
3088 co->co_name,
3089 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00003090 goto fail;
3091 }
3092 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003093 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003094 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003095kw_found:
3096 if (GETLOCAL(j) != NULL) {
3097 PyErr_Format(PyExc_TypeError,
3098 "%U() got multiple "
3099 "values for keyword "
3100 "argument '%S'",
3101 co->co_name,
3102 keyword);
3103 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003104 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003105 Py_INCREF(value);
3106 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003107 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003108 if (co->co_kwonlyargcount > 0) {
3109 for (i = co->co_argcount;
3110 i < co->co_argcount + co->co_kwonlyargcount;
3111 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003112 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003113 if (GETLOCAL(i) != NULL)
3114 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003115 name = PyTuple_GET_ITEM(co->co_varnames, i);
3116 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003117 if (kwdefs != NULL)
3118 def = PyDict_GetItem(kwdefs, name);
3119 if (def != NULL) {
3120 Py_INCREF(def);
3121 SETLOCAL(i, def);
3122 continue;
3123 }
3124 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003125 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003126 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003127 goto fail;
3128 }
3129 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003130 if (argcount < co->co_argcount) {
3131 int m = co->co_argcount - defcount;
3132 for (i = argcount; i < m; i++) {
3133 if (GETLOCAL(i) == NULL) {
3134 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003135 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003136 "%spositional argument%s "
3137 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003138 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003139 ((co->co_flags & CO_VARARGS) ||
3140 defcount) ? "at least"
3141 : "exactly",
3142 m, kwcount ? "non-keyword " : "",
3143 m == 1 ? "" : "s", i);
3144 goto fail;
3145 }
3146 }
3147 if (n > m)
3148 i = n - m;
3149 else
3150 i = 0;
3151 for (; i < defcount; i++) {
3152 if (GETLOCAL(m+i) == NULL) {
3153 PyObject *def = defs[i];
3154 Py_INCREF(def);
3155 SETLOCAL(m+i, def);
3156 }
3157 }
3158 }
3159 }
3160 else {
3161 if (argcount > 0 || kwcount > 0) {
3162 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003163 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003164 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003165 argcount + kwcount);
3166 goto fail;
3167 }
3168 }
3169 /* Allocate and initialize storage for cell vars, and copy free
3170 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003171 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003172 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003173 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003174 PyObject *c;
3175
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003176 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003177 if (co->co_flags & CO_VARARGS)
3178 nargs++;
3179 if (co->co_flags & CO_VARKEYWORDS)
3180 nargs++;
3181
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182 /* Initialize each cell var, taking into account
3183 cell vars that are initialized from arguments.
3184
3185 Should arrange for the compiler to put cellvars
3186 that are arguments at the beginning of the cellvars
3187 list so that we can march over it more efficiently?
3188 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003189 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003190 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003191 PyTuple_GET_ITEM(co->co_cellvars, i));
3192 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003194 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003195 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003196 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003197 c = PyCell_New(GETLOCAL(j));
3198 if (c == NULL)
3199 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003200 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003201 found = 1;
3202 break;
3203 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003204 }
3205 if (found == 0) {
3206 c = PyCell_New(NULL);
3207 if (c == NULL)
3208 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003209 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003210 }
3211 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003212 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003213 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003214 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003215 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003216 PyObject *o = PyTuple_GET_ITEM(closure, i);
3217 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003218 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003219 }
3220 }
3221
Tim Peters5ca576e2001-06-18 22:08:13 +00003222 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003223 /* Don't need to keep the reference to f_back, it will be set
3224 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003225 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003226 f->f_back = NULL;
3227
Jeremy Hylton985eba52003-02-05 23:13:00 +00003228 PCALL(PCALL_GENERATOR);
3229
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003230 /* Create a new generator that owns the ready to run frame
3231 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003232 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003233 }
3234
Thomas Woutersce272b62007-09-19 21:19:28 +00003235 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003236
Thomas Woutersce272b62007-09-19 21:19:28 +00003237fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003238
Tim Petersb13680b2001-11-27 23:29:29 +00003239 /* decref'ing the frame can cause __del__ methods to get invoked,
3240 which can call back into Python. While we're done with the
3241 current Python frame (f), the associated C stack is still in use,
3242 so recursion_depth must be boosted for the duration.
3243 */
3244 assert(tstate != NULL);
3245 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003246 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003247 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003248 return retval;
3249}
3250
3251
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003252static PyObject *
3253special_lookup(PyObject *o, char *meth, PyObject **cache)
3254{
3255 PyObject *res;
3256 res = _PyObject_LookupSpecial(o, meth, cache);
3257 if (res == NULL && !PyErr_Occurred()) {
3258 PyErr_SetObject(PyExc_AttributeError, *cache);
3259 return NULL;
3260 }
3261 return res;
3262}
3263
3264
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003265/* Logic for the raise statement (too complicated for inlining).
3266 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003267static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003268do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003269{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003270 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003271
3272 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003273 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003274 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003275 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003276 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003277 value = tstate->exc_value;
3278 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003279 if (type == Py_None) {
3280 PyErr_SetString(PyExc_RuntimeError,
3281 "No active exception to reraise");
3282 return WHY_EXCEPTION;
3283 }
3284 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003285 Py_XINCREF(value);
3286 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003287 PyErr_Restore(type, value, tb);
3288 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003289 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003290
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003291 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003292 raise
3293 raise <instance>
3294 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003295
Collin Winter828f04a2007-08-31 00:04:24 +00003296 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003297 type = exc;
3298 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003299 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003300 goto raise_error;
3301 }
Collin Winter828f04a2007-08-31 00:04:24 +00003302 else if (PyExceptionInstance_Check(exc)) {
3303 value = exc;
3304 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003305 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003306 }
3307 else {
3308 /* Not something you can raise. You get an exception
3309 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003310 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003311 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003312 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003313 goto raise_error;
3314 }
Collin Winter828f04a2007-08-31 00:04:24 +00003315
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003316 if (cause) {
3317 PyObject *fixed_cause;
3318 if (PyExceptionClass_Check(cause)) {
3319 fixed_cause = PyObject_CallObject(cause, NULL);
3320 if (fixed_cause == NULL)
3321 goto raise_error;
3322 Py_DECREF(cause);
3323 }
3324 else if (PyExceptionInstance_Check(cause)) {
3325 fixed_cause = cause;
3326 }
3327 else {
3328 PyErr_SetString(PyExc_TypeError,
3329 "exception causes must derive from "
3330 "BaseException");
3331 goto raise_error;
3332 }
3333 PyException_SetCause(value, fixed_cause);
3334 }
Collin Winter828f04a2007-08-31 00:04:24 +00003335
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003336 PyErr_SetObject(type, value);
3337 /* PyErr_SetObject incref's its arguments */
3338 Py_XDECREF(value);
3339 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003340 return WHY_EXCEPTION;
3341
3342raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003343 Py_XDECREF(value);
3344 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003345 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003346 return WHY_EXCEPTION;
3347}
3348
Tim Petersd6d010b2001-06-21 02:49:55 +00003349/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003350 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003351
Guido van Rossum0368b722007-05-11 16:50:42 +00003352 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3353 with a variable target.
3354*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003355
Barry Warsawe42b18f1997-08-25 22:13:04 +00003356static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003357unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003358{
Guido van Rossum0368b722007-05-11 16:50:42 +00003359 int i = 0, j = 0;
3360 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003361 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003362 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003363 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003364
Tim Petersd6d010b2001-06-21 02:49:55 +00003365 assert(v != NULL);
3366
3367 it = PyObject_GetIter(v);
3368 if (it == NULL)
3369 goto Error;
3370
3371 for (; i < argcnt; i++) {
3372 w = PyIter_Next(it);
3373 if (w == NULL) {
3374 /* Iterator done, via error or exhaustion. */
3375 if (!PyErr_Occurred()) {
3376 PyErr_Format(PyExc_ValueError,
3377 "need more than %d value%s to unpack",
3378 i, i == 1 ? "" : "s");
3379 }
3380 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003381 }
3382 *--sp = w;
3383 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003384
Guido van Rossum0368b722007-05-11 16:50:42 +00003385 if (argcntafter == -1) {
3386 /* We better have exhausted the iterator now. */
3387 w = PyIter_Next(it);
3388 if (w == NULL) {
3389 if (PyErr_Occurred())
3390 goto Error;
3391 Py_DECREF(it);
3392 return 1;
3393 }
3394 Py_DECREF(w);
3395 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3396 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003397 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003398
3399 l = PySequence_List(it);
3400 if (l == NULL)
3401 goto Error;
3402 *--sp = l;
3403 i++;
3404
3405 ll = PyList_GET_SIZE(l);
3406 if (ll < argcntafter) {
3407 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3408 argcnt + ll);
3409 goto Error;
3410 }
3411
3412 /* Pop the "after-variable" args off the list. */
3413 for (j = argcntafter; j > 0; j--, i++) {
3414 *--sp = PyList_GET_ITEM(l, ll - j);
3415 }
3416 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003417 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003418 Py_DECREF(it);
3419 return 1;
3420
Tim Petersd6d010b2001-06-21 02:49:55 +00003421Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003422 for (; i > 0; i--, sp++)
3423 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003424 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003425 return 0;
3426}
3427
3428
Guido van Rossum96a42c81992-01-12 02:29:51 +00003429#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003430static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003431prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003432{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003433 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003434 if (PyObject_Print(v, stdout, 0) != 0)
3435 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003436 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003437 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003438}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003439#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003440
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003441static void
Fred Drake5755ce62001-06-27 19:19:46 +00003442call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003443{
Guido van Rossumb209a111997-04-29 18:18:01 +00003444 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003445 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003446 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003447 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003448 value = Py_None;
3449 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003450 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003451 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003452 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003453 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003454 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003455 }
Fred Drake5755ce62001-06-27 19:19:46 +00003456 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003457 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003458 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003459 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003460 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003461 Py_XDECREF(type);
3462 Py_XDECREF(value);
3463 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003464 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003465}
3466
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003467static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003468call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003469 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003470{
3471 PyObject *type, *value, *traceback;
3472 int err;
3473 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003474 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003475 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003476 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003477 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003478 return 0;
3479 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003480 else {
3481 Py_XDECREF(type);
3482 Py_XDECREF(value);
3483 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003484 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003485 }
3486}
3487
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003488static int
Fred Drake5755ce62001-06-27 19:19:46 +00003489call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3490 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003491{
Fred Drake5755ce62001-06-27 19:19:46 +00003492 register PyThreadState *tstate = frame->f_tstate;
3493 int result;
3494 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003495 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003496 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003497 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003498 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003499 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3500 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003501 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003502 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003503}
3504
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003505PyObject *
3506_PyEval_CallTracing(PyObject *func, PyObject *args)
3507{
3508 PyFrameObject *frame = PyEval_GetFrame();
3509 PyThreadState *tstate = frame->f_tstate;
3510 int save_tracing = tstate->tracing;
3511 int save_use_tracing = tstate->use_tracing;
3512 PyObject *result;
3513
3514 tstate->tracing = 0;
3515 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3516 || (tstate->c_profilefunc != NULL));
3517 result = PyObject_Call(func, args, NULL);
3518 tstate->tracing = save_tracing;
3519 tstate->use_tracing = save_use_tracing;
3520 return result;
3521}
3522
Michael W. Hudson006c7522002-11-08 13:08:46 +00003523static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003524maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003525 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3526 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003527{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003528 int result = 0;
3529
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003530 /* If the last instruction executed isn't in the current
3531 instruction window, reset the window. If the last
3532 instruction happens to fall at the start of a line or if it
3533 represents a jump backwards, call the trace function.
3534 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003535 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003536 int line;
3537 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003538
Thomas Woutersce272b62007-09-19 21:19:28 +00003539 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3540 &bounds);
3541 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003542 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003543 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003544 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003545 }
3546 *instr_lb = bounds.ap_lower;
3547 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003548 }
Armin Rigobf57a142004-03-22 19:24:58 +00003549 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003550 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003551 }
3552 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003553 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003554}
3555
Fred Drake5755ce62001-06-27 19:19:46 +00003556void
3557PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003558{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003559 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003560 PyObject *temp = tstate->c_profileobj;
3561 Py_XINCREF(arg);
3562 tstate->c_profilefunc = NULL;
3563 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003564 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003565 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003566 Py_XDECREF(temp);
3567 tstate->c_profilefunc = func;
3568 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003569 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003570 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003571}
3572
3573void
3574PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3575{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003576 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003577 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003578 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003579 Py_XINCREF(arg);
3580 tstate->c_tracefunc = NULL;
3581 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003582 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003583 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003584 Py_XDECREF(temp);
3585 tstate->c_tracefunc = func;
3586 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003587 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003588 tstate->use_tracing = ((func != NULL)
3589 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003590}
3591
Guido van Rossumb209a111997-04-29 18:18:01 +00003592PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003593PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003594{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003595 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003596 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003597 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003598 else
3599 return current_frame->f_builtins;
3600}
3601
Guido van Rossumb209a111997-04-29 18:18:01 +00003602PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003603PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003604{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003605 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003606 if (current_frame == NULL)
3607 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003608 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003609 return current_frame->f_locals;
3610}
3611
Guido van Rossumb209a111997-04-29 18:18:01 +00003612PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003613PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003614{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003615 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003616 if (current_frame == NULL)
3617 return NULL;
3618 else
3619 return current_frame->f_globals;
3620}
3621
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003622PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003623PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003624{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003625 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003626 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003627}
3628
Guido van Rossum6135a871995-01-09 17:53:26 +00003629int
Tim Peters5ba58662001-07-16 02:29:45 +00003630PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003631{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003632 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003633 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003634
3635 if (current_frame != NULL) {
3636 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003637 const int compilerflags = codeflags & PyCF_MASK;
3638 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003639 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003640 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003641 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003642#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003643 if (codeflags & CO_GENERATOR_ALLOWED) {
3644 result = 1;
3645 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3646 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003647#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003648 }
3649 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003650}
3651
Guido van Rossum3f5da241990-12-20 15:06:42 +00003652
Guido van Rossum681d79a1995-07-18 14:51:37 +00003653/* External interface to call any callable object.
3654 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003655
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003656#undef PyEval_CallObject
3657/* for backward compatibility: export this interface */
3658
Guido van Rossumb209a111997-04-29 18:18:01 +00003659PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003660PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003661{
Guido van Rossumb209a111997-04-29 18:18:01 +00003662 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003663}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003664#define PyEval_CallObject(func,arg) \
3665 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003666
Guido van Rossumb209a111997-04-29 18:18:01 +00003667PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003668PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003669{
Jeremy Hylton52820442001-01-03 23:52:36 +00003670 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003671
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003672 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003673 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003674 if (arg == NULL)
3675 return NULL;
3676 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003677 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003678 PyErr_SetString(PyExc_TypeError,
3679 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003680 return NULL;
3681 }
3682 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003683 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003684
Guido van Rossumb209a111997-04-29 18:18:01 +00003685 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003686 PyErr_SetString(PyExc_TypeError,
3687 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003688 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003689 return NULL;
3690 }
3691
Tim Peters6d6c1a32001-08-02 04:15:00 +00003692 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003693 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003694 return result;
3695}
3696
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003697const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003698PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003699{
3700 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003701 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003702 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003703 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003704 else if (PyCFunction_Check(func))
3705 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003706 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003707 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003708}
3709
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003710const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003711PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003712{
3713 if (PyMethod_Check(func))
3714 return "()";
3715 else if (PyFunction_Check(func))
3716 return "()";
3717 else if (PyCFunction_Check(func))
3718 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003719 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003720 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003721}
3722
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003723static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003724err_args(PyObject *func, int flags, int nargs)
3725{
3726 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003727 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003728 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003729 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003730 nargs);
3731 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003732 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003733 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003734 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003735 nargs);
3736}
3737
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003738#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003739if (tstate->use_tracing && tstate->c_profilefunc) { \
3740 if (call_trace(tstate->c_profilefunc, \
3741 tstate->c_profileobj, \
3742 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003743 func)) { \
3744 x = NULL; \
3745 } \
3746 else { \
3747 x = call; \
3748 if (tstate->c_profilefunc != NULL) { \
3749 if (x == NULL) { \
3750 call_trace_protected(tstate->c_profilefunc, \
3751 tstate->c_profileobj, \
3752 tstate->frame, PyTrace_C_EXCEPTION, \
3753 func); \
3754 /* XXX should pass (type, value, tb) */ \
3755 } else { \
3756 if (call_trace(tstate->c_profilefunc, \
3757 tstate->c_profileobj, \
3758 tstate->frame, PyTrace_C_RETURN, \
3759 func)) { \
3760 Py_DECREF(x); \
3761 x = NULL; \
3762 } \
3763 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003764 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003765 } \
3766} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003767 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003768 }
3769
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003770static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003771call_function(PyObject ***pp_stack, int oparg
3772#ifdef WITH_TSC
3773 , uint64* pintr0, uint64* pintr1
3774#endif
3775 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003776{
3777 int na = oparg & 0xff;
3778 int nk = (oparg>>8) & 0xff;
3779 int n = na + 2 * nk;
3780 PyObject **pfunc = (*pp_stack) - n - 1;
3781 PyObject *func = *pfunc;
3782 PyObject *x, *w;
3783
Jeremy Hylton985eba52003-02-05 23:13:00 +00003784 /* Always dispatch PyCFunction first, because these are
3785 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003786 */
3787 if (PyCFunction_Check(func) && nk == 0) {
3788 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003789 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003790
3791 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003792 if (flags & (METH_NOARGS | METH_O)) {
3793 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3794 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003795 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003796 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003797 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003798 else if (flags & METH_O && na == 1) {
3799 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003800 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003801 Py_DECREF(arg);
3802 }
3803 else {
3804 err_args(func, flags, na);
3805 x = NULL;
3806 }
3807 }
3808 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003809 PyObject *callargs;
3810 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003811 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003812 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003813 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003814 Py_XDECREF(callargs);
3815 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003816 } else {
3817 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3818 /* optimize access to bound methods */
3819 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003820 PCALL(PCALL_METHOD);
3821 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003822 Py_INCREF(self);
3823 func = PyMethod_GET_FUNCTION(func);
3824 Py_INCREF(func);
3825 Py_DECREF(*pfunc);
3826 *pfunc = self;
3827 na++;
3828 n++;
3829 } else
3830 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003831 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003832 if (PyFunction_Check(func))
3833 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003834 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003835 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003836 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003837 Py_DECREF(func);
3838 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003839
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003840 /* Clear the stack of the function object. Also removes
3841 the arguments in case they weren't consumed already
3842 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003843 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003844 while ((*pp_stack) > pfunc) {
3845 w = EXT_POP(*pp_stack);
3846 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003847 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003848 }
3849 return x;
3850}
3851
Jeremy Hylton192690e2002-08-16 18:36:11 +00003852/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003853 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003854 For the simplest case -- a function that takes only positional
3855 arguments and is called with only positional arguments -- it
3856 inlines the most primitive frame setup code from
3857 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3858 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003859*/
3860
3861static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003862fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003863{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003864 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003865 PyObject *globals = PyFunction_GET_GLOBALS(func);
3866 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003867 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003868 PyObject **d = NULL;
3869 int nd = 0;
3870
Jeremy Hylton985eba52003-02-05 23:13:00 +00003871 PCALL(PCALL_FUNCTION);
3872 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003873 if (argdefs == NULL && co->co_argcount == n &&
3874 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003875 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3876 PyFrameObject *f;
3877 PyObject *retval = NULL;
3878 PyThreadState *tstate = PyThreadState_GET();
3879 PyObject **fastlocals, **stack;
3880 int i;
3881
3882 PCALL(PCALL_FASTER_FUNCTION);
3883 assert(globals != NULL);
3884 /* XXX Perhaps we should create a specialized
3885 PyFrame_New() that doesn't take locals, but does
3886 take builtins without sanity checking them.
3887 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003888 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003889 f = PyFrame_New(tstate, co, globals, NULL);
3890 if (f == NULL)
3891 return NULL;
3892
3893 fastlocals = f->f_localsplus;
3894 stack = (*pp_stack) - n;
3895
3896 for (i = 0; i < n; i++) {
3897 Py_INCREF(*stack);
3898 fastlocals[i] = *stack++;
3899 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003900 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003901 ++tstate->recursion_depth;
3902 Py_DECREF(f);
3903 --tstate->recursion_depth;
3904 return retval;
3905 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003906 if (argdefs != NULL) {
3907 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003908 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003909 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003910 return PyEval_EvalCodeEx(co, globals,
3911 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003912 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003913 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003914}
3915
3916static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003917update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3918 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003919{
3920 PyObject *kwdict = NULL;
3921 if (orig_kwdict == NULL)
3922 kwdict = PyDict_New();
3923 else {
3924 kwdict = PyDict_Copy(orig_kwdict);
3925 Py_DECREF(orig_kwdict);
3926 }
3927 if (kwdict == NULL)
3928 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003929 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003930 int err;
3931 PyObject *value = EXT_POP(*pp_stack);
3932 PyObject *key = EXT_POP(*pp_stack);
3933 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003934 PyErr_Format(PyExc_TypeError,
3935 "%.200s%s got multiple values "
3936 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003937 PyEval_GetFuncName(func),
3938 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003939 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003940 Py_DECREF(key);
3941 Py_DECREF(value);
3942 Py_DECREF(kwdict);
3943 return NULL;
3944 }
3945 err = PyDict_SetItem(kwdict, key, value);
3946 Py_DECREF(key);
3947 Py_DECREF(value);
3948 if (err) {
3949 Py_DECREF(kwdict);
3950 return NULL;
3951 }
3952 }
3953 return kwdict;
3954}
3955
3956static PyObject *
3957update_star_args(int nstack, int nstar, PyObject *stararg,
3958 PyObject ***pp_stack)
3959{
3960 PyObject *callargs, *w;
3961
3962 callargs = PyTuple_New(nstack + nstar);
3963 if (callargs == NULL) {
3964 return NULL;
3965 }
3966 if (nstar) {
3967 int i;
3968 for (i = 0; i < nstar; i++) {
3969 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3970 Py_INCREF(a);
3971 PyTuple_SET_ITEM(callargs, nstack + i, a);
3972 }
3973 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003974 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003975 w = EXT_POP(*pp_stack);
3976 PyTuple_SET_ITEM(callargs, nstack, w);
3977 }
3978 return callargs;
3979}
3980
3981static PyObject *
3982load_args(PyObject ***pp_stack, int na)
3983{
3984 PyObject *args = PyTuple_New(na);
3985 PyObject *w;
3986
3987 if (args == NULL)
3988 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003989 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003990 w = EXT_POP(*pp_stack);
3991 PyTuple_SET_ITEM(args, na, w);
3992 }
3993 return args;
3994}
3995
3996static PyObject *
3997do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3998{
3999 PyObject *callargs = NULL;
4000 PyObject *kwdict = NULL;
4001 PyObject *result = NULL;
4002
4003 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004004 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004005 if (kwdict == NULL)
4006 goto call_fail;
4007 }
4008 callargs = load_args(pp_stack, na);
4009 if (callargs == NULL)
4010 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004011#ifdef CALL_PROFILE
4012 /* At this point, we have to look at the type of func to
4013 update the call stats properly. Do it here so as to avoid
4014 exposing the call stats machinery outside ceval.c
4015 */
4016 if (PyFunction_Check(func))
4017 PCALL(PCALL_FUNCTION);
4018 else if (PyMethod_Check(func))
4019 PCALL(PCALL_METHOD);
4020 else if (PyType_Check(func))
4021 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004022 else if (PyCFunction_Check(func))
4023 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004024 else
4025 PCALL(PCALL_OTHER);
4026#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004027 if (PyCFunction_Check(func)) {
4028 PyThreadState *tstate = PyThreadState_GET();
4029 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4030 }
4031 else
4032 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004033call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004034 Py_XDECREF(callargs);
4035 Py_XDECREF(kwdict);
4036 return result;
4037}
4038
4039static PyObject *
4040ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4041{
4042 int nstar = 0;
4043 PyObject *callargs = NULL;
4044 PyObject *stararg = NULL;
4045 PyObject *kwdict = NULL;
4046 PyObject *result = NULL;
4047
4048 if (flags & CALL_FLAG_KW) {
4049 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004050 if (!PyDict_Check(kwdict)) {
4051 PyObject *d;
4052 d = PyDict_New();
4053 if (d == NULL)
4054 goto ext_call_fail;
4055 if (PyDict_Update(d, kwdict) != 0) {
4056 Py_DECREF(d);
4057 /* PyDict_Update raises attribute
4058 * error (percolated from an attempt
4059 * to get 'keys' attribute) instead of
4060 * a type error if its second argument
4061 * is not a mapping.
4062 */
4063 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4064 PyErr_Format(PyExc_TypeError,
4065 "%.200s%.200s argument after ** "
4066 "must be a mapping, not %.200s",
4067 PyEval_GetFuncName(func),
4068 PyEval_GetFuncDesc(func),
4069 kwdict->ob_type->tp_name);
4070 }
4071 goto ext_call_fail;
4072 }
4073 Py_DECREF(kwdict);
4074 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004075 }
4076 }
4077 if (flags & CALL_FLAG_VAR) {
4078 stararg = EXT_POP(*pp_stack);
4079 if (!PyTuple_Check(stararg)) {
4080 PyObject *t = NULL;
4081 t = PySequence_Tuple(stararg);
4082 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004083 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4084 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004085 "%.200s%.200s argument after * "
4086 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004087 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004088 PyEval_GetFuncDesc(func),
4089 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004090 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004091 goto ext_call_fail;
4092 }
4093 Py_DECREF(stararg);
4094 stararg = t;
4095 }
4096 nstar = PyTuple_GET_SIZE(stararg);
4097 }
4098 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004099 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004100 if (kwdict == NULL)
4101 goto ext_call_fail;
4102 }
4103 callargs = update_star_args(na, nstar, stararg, pp_stack);
4104 if (callargs == NULL)
4105 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004106#ifdef CALL_PROFILE
4107 /* At this point, we have to look at the type of func to
4108 update the call stats properly. Do it here so as to avoid
4109 exposing the call stats machinery outside ceval.c
4110 */
4111 if (PyFunction_Check(func))
4112 PCALL(PCALL_FUNCTION);
4113 else if (PyMethod_Check(func))
4114 PCALL(PCALL_METHOD);
4115 else if (PyType_Check(func))
4116 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004117 else if (PyCFunction_Check(func))
4118 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004119 else
4120 PCALL(PCALL_OTHER);
4121#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004122 if (PyCFunction_Check(func)) {
4123 PyThreadState *tstate = PyThreadState_GET();
4124 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4125 }
4126 else
4127 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004128ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004129 Py_XDECREF(callargs);
4130 Py_XDECREF(kwdict);
4131 Py_XDECREF(stararg);
4132 return result;
4133}
4134
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004135/* Extract a slice index from a PyInt or PyLong or an object with the
4136 nb_index slot defined, and store in *pi.
4137 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4138 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 +00004139 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004140*/
Tim Petersb5196382001-12-16 19:44:20 +00004141/* Note: If v is NULL, return success without storing into *pi. This
4142 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4143 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004144*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004145int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004146_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004147{
Tim Petersb5196382001-12-16 19:44:20 +00004148 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004149 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004150 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004151 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004152 if (x == -1 && PyErr_Occurred())
4153 return 0;
4154 }
4155 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004156 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004157 "slice indices must be integers or "
4158 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004159 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004160 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004161 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004162 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004163 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004164}
4165
Guido van Rossum486364b2007-06-30 05:01:58 +00004166#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004167 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004168
Guido van Rossumb209a111997-04-29 18:18:01 +00004169static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004170cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004171{
Guido van Rossumac7be682001-01-17 15:42:30 +00004172 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004173 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004174 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004175 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004176 break;
4177 case PyCmp_IS_NOT:
4178 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004179 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004180 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004181 res = PySequence_Contains(w, v);
4182 if (res < 0)
4183 return NULL;
4184 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004185 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004186 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004187 if (res < 0)
4188 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004189 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004190 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004191 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004192 if (PyTuple_Check(w)) {
4193 Py_ssize_t i, length;
4194 length = PyTuple_Size(w);
4195 for (i = 0; i < length; i += 1) {
4196 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004197 if (!PyExceptionClass_Check(exc)) {
4198 PyErr_SetString(PyExc_TypeError,
4199 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004200 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004201 }
4202 }
4203 }
4204 else {
Brett Cannon39590462007-02-26 22:01:14 +00004205 if (!PyExceptionClass_Check(w)) {
4206 PyErr_SetString(PyExc_TypeError,
4207 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004208 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004209 }
4210 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004211 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004212 break;
4213 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004214 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004215 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004216 v = res ? Py_True : Py_False;
4217 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004218 return v;
4219}
4220
Thomas Wouters52152252000-08-17 22:55:00 +00004221static PyObject *
4222import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004223{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004224 PyObject *x;
4225
4226 x = PyObject_GetAttr(v, name);
4227 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004228 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004229 }
Thomas Wouters52152252000-08-17 22:55:00 +00004230 return x;
4231}
Guido van Rossumac7be682001-01-17 15:42:30 +00004232
Thomas Wouters52152252000-08-17 22:55:00 +00004233static int
4234import_all_from(PyObject *locals, PyObject *v)
4235{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004236 PyObject *all = PyObject_GetAttrString(v, "__all__");
4237 PyObject *dict, *name, *value;
4238 int skip_leading_underscores = 0;
4239 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004240
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004241 if (all == NULL) {
4242 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4243 return -1; /* Unexpected error */
4244 PyErr_Clear();
4245 dict = PyObject_GetAttrString(v, "__dict__");
4246 if (dict == NULL) {
4247 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4248 return -1;
4249 PyErr_SetString(PyExc_ImportError,
4250 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004251 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004252 }
4253 all = PyMapping_Keys(dict);
4254 Py_DECREF(dict);
4255 if (all == NULL)
4256 return -1;
4257 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004258 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004259
4260 for (pos = 0, err = 0; ; pos++) {
4261 name = PySequence_GetItem(all, pos);
4262 if (name == NULL) {
4263 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4264 err = -1;
4265 else
4266 PyErr_Clear();
4267 break;
4268 }
4269 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004270 PyUnicode_Check(name) &&
4271 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004272 {
4273 Py_DECREF(name);
4274 continue;
4275 }
4276 value = PyObject_GetAttr(v, name);
4277 if (value == NULL)
4278 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004279 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004280 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004281 else
4282 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004283 Py_DECREF(name);
4284 Py_XDECREF(value);
4285 if (err != 0)
4286 break;
4287 }
4288 Py_DECREF(all);
4289 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004290}
4291
Guido van Rossumac7be682001-01-17 15:42:30 +00004292static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004293format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004294{
Neal Norwitzda059e32007-08-26 05:33:45 +00004295 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004296
4297 if (!obj)
4298 return;
4299
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004300 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004301 if (!obj_str)
4302 return;
4303
4304 PyErr_Format(exc, format_str, obj_str);
4305}
Guido van Rossum950361c1997-01-24 13:49:28 +00004306
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004307static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004308unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004309 PyFrameObject *f, unsigned char *next_instr)
4310{
4311 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004312 are (Unicode) strings. */
4313 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4314 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004315 Py_ssize_t new_len = v_len + w_len;
4316 if (new_len < 0) {
4317 PyErr_SetString(PyExc_OverflowError,
4318 "strings are too large to concat");
4319 return NULL;
4320 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004321
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004322 if (v->ob_refcnt == 2) {
4323 /* In the common case, there are 2 references to the value
4324 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004325 * value stack (in 'v') and one still stored in the
4326 * 'variable'. We try to delete the variable now to reduce
4327 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004328 */
4329 switch (*next_instr) {
4330 case STORE_FAST:
4331 {
4332 int oparg = PEEKARG();
4333 PyObject **fastlocals = f->f_localsplus;
4334 if (GETLOCAL(oparg) == v)
4335 SETLOCAL(oparg, NULL);
4336 break;
4337 }
4338 case STORE_DEREF:
4339 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004340 PyObject **freevars = (f->f_localsplus +
4341 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004342 PyObject *c = freevars[PEEKARG()];
4343 if (PyCell_GET(c) == v)
4344 PyCell_Set(c, NULL);
4345 break;
4346 }
4347 case STORE_NAME:
4348 {
4349 PyObject *names = f->f_code->co_names;
4350 PyObject *name = GETITEM(names, PEEKARG());
4351 PyObject *locals = f->f_locals;
4352 if (PyDict_CheckExact(locals) &&
4353 PyDict_GetItem(locals, name) == v) {
4354 if (PyDict_DelItem(locals, name) != 0) {
4355 PyErr_Clear();
4356 }
4357 }
4358 break;
4359 }
4360 }
4361 }
4362
Guido van Rossum98297ee2007-11-06 21:34:58 +00004363 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004364 /* Now we own the last reference to 'v', so we can resize it
4365 * in-place.
4366 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004367 if (PyUnicode_Resize(&v, new_len) != 0) {
4368 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004369 * deallocated so it cannot be put back into
4370 * 'variable'. The MemoryError is raised when there
4371 * is no value in 'variable', which might (very
4372 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004373 */
4374 return NULL;
4375 }
4376 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004377 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4378 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004379 return v;
4380 }
4381 else {
4382 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004383 w = PyUnicode_Concat(v, w);
4384 Py_DECREF(v);
4385 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004386 }
4387}
4388
Guido van Rossum950361c1997-01-24 13:49:28 +00004389#ifdef DYNAMIC_EXECUTION_PROFILE
4390
Skip Montanarof118cb12001-10-15 20:51:38 +00004391static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004392getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004393{
4394 int i;
4395 PyObject *l = PyList_New(256);
4396 if (l == NULL) return NULL;
4397 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004398 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004399 if (x == NULL) {
4400 Py_DECREF(l);
4401 return NULL;
4402 }
4403 PyList_SetItem(l, i, x);
4404 }
4405 for (i = 0; i < 256; i++)
4406 a[i] = 0;
4407 return l;
4408}
4409
4410PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004411_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004412{
4413#ifndef DXPAIRS
4414 return getarray(dxp);
4415#else
4416 int i;
4417 PyObject *l = PyList_New(257);
4418 if (l == NULL) return NULL;
4419 for (i = 0; i < 257; i++) {
4420 PyObject *x = getarray(dxpairs[i]);
4421 if (x == NULL) {
4422 Py_DECREF(l);
4423 return NULL;
4424 }
4425 PyList_SetItem(l, i, x);
4426 }
4427 return l;
4428#endif
4429}
4430
4431#endif