blob: 403acd2513440a187810a20c686044dffc19f691 [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])
Benjamin Peterson6d46a912009-06-28 16:17:34 +0000922#define PEEK(n) (stack_pointer[-(n)])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000923#define SET_TOP(v) (stack_pointer[-1] = (v))
924#define SET_SECOND(v) (stack_pointer[-2] = (v))
925#define SET_THIRD(v) (stack_pointer[-3] = (v))
926#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Benjamin Peterson6d46a912009-06-28 16:17:34 +0000927#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000928#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000929#define BASIC_PUSH(v) (*stack_pointer++ = (v))
930#define BASIC_POP() (*--stack_pointer)
931
Guido van Rossum96a42c81992-01-12 02:29:51 +0000932#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000933#define PUSH(v) { (void)(BASIC_PUSH(v), \
934 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000935 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000936#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
937 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000938#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
939 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000940 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000941#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
942 prtrace((STACK_POINTER)[-1], "ext_pop")), \
943 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000944#else
945#define PUSH(v) BASIC_PUSH(v)
946#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000947#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000948#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000949#endif
950
Guido van Rossum681d79a1995-07-18 14:51:37 +0000951/* Local variable macros */
952
953#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000954
955/* The SETLOCAL() macro must not DECREF the local variable in-place and
956 then store the new value; it must copy the old value to a temporary
957 value, then store the new value, and then DECREF the temporary value.
958 This is because it is possible that during the DECREF the frame is
959 accessed by other code (e.g. a __del__ method or gc.collect()) and the
960 variable would be pointing to already-freed memory. */
961#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
962 GETLOCAL(i) = value; \
963 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000964
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000965
966#define UNWIND_BLOCK(b) \
967 while (STACK_LEVEL() > (b)->b_level) { \
968 PyObject *v = POP(); \
969 Py_XDECREF(v); \
970 }
971
972#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000973 { \
974 PyObject *type, *value, *traceback; \
975 assert(STACK_LEVEL() >= (b)->b_level + 3); \
976 while (STACK_LEVEL() > (b)->b_level + 3) { \
977 value = POP(); \
978 Py_XDECREF(value); \
979 } \
980 type = tstate->exc_type; \
981 value = tstate->exc_value; \
982 traceback = tstate->exc_traceback; \
983 tstate->exc_type = POP(); \
984 tstate->exc_value = POP(); \
985 tstate->exc_traceback = POP(); \
986 Py_XDECREF(type); \
987 Py_XDECREF(value); \
988 Py_XDECREF(traceback); \
989 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000990
991#define SAVE_EXC_STATE() \
992 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000993 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000994 Py_XINCREF(tstate->exc_type); \
995 Py_XINCREF(tstate->exc_value); \
996 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000997 type = f->f_exc_type; \
998 value = f->f_exc_value; \
999 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001000 f->f_exc_type = tstate->exc_type; \
1001 f->f_exc_value = tstate->exc_value; \
1002 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001003 Py_XDECREF(type); \
1004 Py_XDECREF(value); \
1005 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001006 }
1007
1008#define SWAP_EXC_STATE() \
1009 { \
1010 PyObject *tmp; \
1011 tmp = tstate->exc_type; \
1012 tstate->exc_type = f->f_exc_type; \
1013 f->f_exc_type = tmp; \
1014 tmp = tstate->exc_value; \
1015 tstate->exc_value = f->f_exc_value; \
1016 f->f_exc_value = tmp; \
1017 tmp = tstate->exc_traceback; \
1018 tstate->exc_traceback = f->f_exc_traceback; \
1019 f->f_exc_traceback = tmp; \
1020 }
1021
Guido van Rossuma027efa1997-05-05 20:56:21 +00001022/* Start of code */
1023
Tim Peters5ca576e2001-06-18 22:08:13 +00001024 if (f == NULL)
1025 return NULL;
1026
Armin Rigo1d313ab2003-10-25 14:33:09 +00001027 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001028 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +00001029 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001030
Tim Peters5ca576e2001-06-18 22:08:13 +00001031 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001032
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001033 if (tstate->use_tracing) {
1034 if (tstate->c_tracefunc != NULL) {
1035 /* tstate->c_tracefunc, if defined, is a
1036 function that will be called on *every* entry
1037 to a code block. Its return value, if not
1038 None, is a function that will be called at
1039 the start of each executed line of code.
1040 (Actually, the function must return itself
1041 in order to continue tracing.) The trace
1042 functions are called with three arguments:
1043 a pointer to the current frame, a string
1044 indicating why the function is called, and
1045 an argument which depends on the situation.
1046 The global trace function is also called
1047 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001048 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001049 tstate->c_traceobj,
1050 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001051 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001052 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001053 }
1054 }
1055 if (tstate->c_profilefunc != NULL) {
1056 /* Similar for c_profilefunc, except it needn't
1057 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001058 if (call_trace_protected(tstate->c_profilefunc,
1059 tstate->c_profileobj,
1060 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001061 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001062 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001063 }
1064 }
1065 }
1066
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001067 co = f->f_code;
1068 names = co->co_names;
1069 consts = co->co_consts;
1070 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001071 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +00001072 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001073 /* An explanation is in order for the next line.
1074
1075 f->f_lasti now refers to the index of the last instruction
1076 executed. You might think this was obvious from the name, but
1077 this wasn't always true before 2.3! PyFrame_New now sets
1078 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1079 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001080 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001081
1082 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001083 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001084 prediction effectively links the two codes together as if they
1085 were a single new opcode; accordingly,f->f_lasti will point to
1086 the first code in the pair (for instance, GET_ITER followed by
1087 FOR_ITER is effectively a single opcode and f->f_lasti will point
1088 at to the beginning of the combined pair.)
1089 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001090 next_instr = first_instr + f->f_lasti + 1;
1091 stack_pointer = f->f_stacktop;
1092 assert(stack_pointer != NULL);
1093 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1094
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001095 if (f->f_code->co_flags & CO_GENERATOR) {
1096 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1097 /* We were in an except handler when we left,
1098 restore the exception state which was put aside
1099 (see YIELD_VALUE). */
1100 SWAP_EXC_STATE();
1101 }
1102 else {
1103 SAVE_EXC_STATE();
1104 }
1105 }
1106
Tim Peters5ca576e2001-06-18 22:08:13 +00001107#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001109#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001110#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001111 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001112#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001113
Guido van Rossum374a9221991-04-04 10:40:29 +00001114 why = WHY_NOT;
1115 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00001116 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +00001117 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001119 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001120 why = WHY_EXCEPTION;
1121 goto on_error;
1122 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001123
Guido van Rossum374a9221991-04-04 10:40:29 +00001124 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001125#ifdef WITH_TSC
1126 if (inst1 == 0) {
1127 /* Almost surely, the opcode executed a break
1128 or a continue, preventing inst1 from being set
1129 on the way out of the loop.
1130 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001131 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001132 loop1 = inst1;
1133 }
1134 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1135 intr0, intr1);
1136 ticked = 0;
1137 inst1 = 0;
1138 intr0 = 0;
1139 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001140 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001141#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001142 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001143 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001144
Guido van Rossuma027efa1997-05-05 20:56:21 +00001145 /* Do periodic things. Doing this every time through
1146 the loop would add too much overhead, so we do it
1147 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001148 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001149 event needs attention (e.g. a signal handler or
1150 async I/O handler); see Py_AddPendingCall() and
1151 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001152
Skip Montanarod581d772002-09-03 20:10:45 +00001153 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001154 if (*next_instr == SETUP_FINALLY) {
1155 /* Make the last opcode before
1156 a try: finally: block uninterruptable. */
1157 goto fast_next_opcode;
1158 }
Skip Montanarod581d772002-09-03 20:10:45 +00001159 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001160 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001161#ifdef WITH_TSC
1162 ticked = 1;
1163#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001164 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001165 if (Py_MakePendingCalls() < 0) {
1166 why = WHY_EXCEPTION;
1167 goto on_error;
1168 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001169 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +00001170 /* MakePendingCalls() didn't succeed.
1171 Force early re-execution of this
1172 "periodic" code, possibly after
1173 a thread switch */
1174 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +00001175 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001176#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001177 if (interpreter_lock) {
1178 /* Give another thread a chance */
1179
Guido van Rossum25ce5661997-08-02 03:10:38 +00001180 if (PyThreadState_Swap(NULL) != tstate)
1181 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +00001182 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001183
1184 /* Other threads may run now */
1185
Guido van Rossum65d5b571998-12-21 19:32:43 +00001186 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001187 if (PyThreadState_Swap(tstate) != NULL)
1188 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001189
1190 /* Check for thread interrupts */
1191
1192 if (tstate->async_exc != NULL) {
1193 x = tstate->async_exc;
1194 tstate->async_exc = NULL;
1195 PyErr_SetNone(x);
1196 Py_DECREF(x);
1197 why = WHY_EXCEPTION;
1198 goto on_error;
1199 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001200 }
1201#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001202 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001203
Neil Schemenauer63543862002-02-17 19:10:14 +00001204 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001205 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001207 /* line-by-line tracing support */
1208
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001209 if (_Py_TracingPossible &&
1210 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001211 /* see maybe_call_line_trace
1212 for expository comments */
1213 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001214
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001215 err = maybe_call_line_trace(tstate->c_tracefunc,
1216 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001217 f, &instr_lb, &instr_ub,
1218 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001219 /* Reload possibly changed frame fields */
1220 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001221 if (f->f_stacktop != NULL) {
1222 stack_pointer = f->f_stacktop;
1223 f->f_stacktop = NULL;
1224 }
1225 if (err) {
1226 /* trace function raised an exception */
1227 goto on_error;
1228 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001229 }
1230
1231 /* Extract opcode and argument */
1232
Guido van Rossum374a9221991-04-04 10:40:29 +00001233 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001234 oparg = 0; /* allows oparg to be stored in a register because
1235 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001236 if (HAS_ARG(opcode))
1237 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001238 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001239#ifdef DYNAMIC_EXECUTION_PROFILE
1240#ifdef DXPAIRS
1241 dxpairs[lastopcode][opcode]++;
1242 lastopcode = opcode;
1243#endif
1244 dxp[opcode]++;
1245#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001246
Guido van Rossum96a42c81992-01-12 02:29:51 +00001247#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001248 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001249
Guido van Rossum96a42c81992-01-12 02:29:51 +00001250 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001251 if (HAS_ARG(opcode)) {
1252 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001253 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001254 }
1255 else {
1256 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001257 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001258 }
1259 }
1260#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001261
Guido van Rossum374a9221991-04-04 10:40:29 +00001262 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001263 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001264
Guido van Rossum374a9221991-04-04 10:40:29 +00001265 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001266
Guido van Rossum374a9221991-04-04 10:40:29 +00001267 /* BEWARE!
1268 It is essential that any operation that fails sets either
1269 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1270 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001271
Guido van Rossum374a9221991-04-04 10:40:29 +00001272 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001273
Antoine Pitroub52ec782009-01-25 16:34:23 +00001274 TARGET(NOP)
1275 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001276
Antoine Pitroub52ec782009-01-25 16:34:23 +00001277 TARGET(LOAD_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001278 x = GETLOCAL(oparg);
1279 if (x != NULL) {
1280 Py_INCREF(x);
1281 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001282 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001283 }
1284 format_exc_check_arg(PyExc_UnboundLocalError,
1285 UNBOUNDLOCAL_ERROR_MSG,
1286 PyTuple_GetItem(co->co_varnames, oparg));
1287 break;
1288
Antoine Pitroub52ec782009-01-25 16:34:23 +00001289 TARGET(LOAD_CONST)
Skip Montanaro04d80f82002-08-04 21:03:35 +00001290 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001291 Py_INCREF(x);
1292 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001293 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001294
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001295 PREDICTED_WITH_ARG(STORE_FAST);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001296 TARGET(STORE_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001297 v = POP();
1298 SETLOCAL(oparg, v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001299 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001300
Antoine Pitroub52ec782009-01-25 16:34:23 +00001301 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001302 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001303 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001304 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001305
Antoine Pitroub52ec782009-01-25 16:34:23 +00001306 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001307 v = TOP();
1308 w = SECOND();
1309 SET_TOP(w);
1310 SET_SECOND(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001311 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001312
Antoine Pitroub52ec782009-01-25 16:34:23 +00001313 TARGET(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001314 v = TOP();
1315 w = SECOND();
1316 x = THIRD();
1317 SET_TOP(w);
1318 SET_SECOND(x);
1319 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001320 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Antoine Pitroub52ec782009-01-25 16:34:23 +00001322 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001323 u = TOP();
1324 v = SECOND();
1325 w = THIRD();
1326 x = FOURTH();
1327 SET_TOP(v);
1328 SET_SECOND(w);
1329 SET_THIRD(x);
1330 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001331 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Antoine Pitroub52ec782009-01-25 16:34:23 +00001333 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001334 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001335 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001336 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001337 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001338
Antoine Pitroub52ec782009-01-25 16:34:23 +00001339 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001340 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001342 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001343 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001344 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001345 STACKADJ(2);
1346 SET_TOP(x);
1347 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001348 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001349 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001351 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001352 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001353 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001355 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 STACKADJ(3);
1357 SET_TOP(x);
1358 SET_SECOND(w);
1359 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001360 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001361 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001362 Py_FatalError("invalid argument to DUP_TOPX"
1363 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001364 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001365 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001366
Antoine Pitroub52ec782009-01-25 16:34:23 +00001367 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001368 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001369 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001370 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001372 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001373 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001374
Antoine Pitroub52ec782009-01-25 16:34:23 +00001375 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001377 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001378 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001379 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001380 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001381 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Antoine Pitroub52ec782009-01-25 16:34:23 +00001383 TARGET(UNARY_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001384 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001385 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001386 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001387 if (err == 0) {
1388 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001389 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001390 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001391 }
1392 else if (err > 0) {
1393 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001395 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001396 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001397 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001398 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001399 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Antoine Pitroub52ec782009-01-25 16:34:23 +00001401 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001402 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001403 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001404 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001405 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001406 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001407 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Antoine Pitroub52ec782009-01-25 16:34:23 +00001409 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001410 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001411 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001412 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001413 Py_DECREF(v);
1414 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001415 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001416 if (x != NULL) DISPATCH();
Guido van Rossum50564e81996-01-12 01:13:16 +00001417 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001418
Antoine Pitroub52ec782009-01-25 16:34:23 +00001419 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001420 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001421 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001422 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001423 Py_DECREF(v);
1424 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001425 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001426 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001427 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001428
Antoine Pitroub52ec782009-01-25 16:34:23 +00001429 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001430 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001431 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001432 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001433 Py_DECREF(v);
1434 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001435 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001436 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001437 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001438
Antoine Pitroub52ec782009-01-25 16:34:23 +00001439 TARGET(BINARY_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001440 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001441 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001442 x = PyNumber_FloorDivide(v, w);
1443 Py_DECREF(v);
1444 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001445 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001446 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001447 break;
1448
Antoine Pitroub52ec782009-01-25 16:34:23 +00001449 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001450 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001451 v = TOP();
Benjamin Petersonefb06b02009-02-26 18:55:48 +00001452 if (PyUnicode_CheckExact(v))
1453 x = PyUnicode_Format(v, w);
1454 else
1455 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001456 Py_DECREF(v);
1457 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001458 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001459 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001460 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001461
Antoine Pitroub52ec782009-01-25 16:34:23 +00001462 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001463 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001464 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001465 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001466 PyUnicode_CheckExact(w)) {
1467 x = unicode_concatenate(v, w, f, next_instr);
1468 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001469 goto skip_decref_vx;
1470 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001471 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001472 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001473 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001474 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001475 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001476 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001477 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001478 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Antoine Pitroub52ec782009-01-25 16:34:23 +00001481 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001482 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001483 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001484 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001485 Py_DECREF(v);
1486 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001487 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001488 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001489 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Antoine Pitroub52ec782009-01-25 16:34:23 +00001491 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001492 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001493 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001494 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001495 Py_DECREF(v);
1496 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001497 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001498 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001499 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001500
Antoine Pitroub52ec782009-01-25 16:34:23 +00001501 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001502 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001503 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001504 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001505 Py_DECREF(v);
1506 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001507 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001508 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001509 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001510
Antoine Pitroub52ec782009-01-25 16:34:23 +00001511 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001512 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001513 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001514 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001515 Py_DECREF(v);
1516 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001517 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001518 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001519 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Antoine Pitroub52ec782009-01-25 16:34:23 +00001521 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001522 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001523 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001524 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001525 Py_DECREF(v);
1526 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001527 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001528 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001529 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001530
Antoine Pitroub52ec782009-01-25 16:34:23 +00001531 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001532 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001533 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001534 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001535 Py_DECREF(v);
1536 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001537 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001538 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001539 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001540
Antoine Pitroub52ec782009-01-25 16:34:23 +00001541 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001542 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001543 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001544 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001545 Py_DECREF(v);
1546 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001547 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001548 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001549 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001550
Antoine Pitroub52ec782009-01-25 16:34:23 +00001551 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001552 w = POP();
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001553 v = PEEK(oparg);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001554 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001555 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001556 if (err == 0) {
1557 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001558 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001559 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001560 break;
1561
Antoine Pitroub52ec782009-01-25 16:34:23 +00001562 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001563 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001564 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001565 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001566 Py_DECREF(w);
1567 if (err == 0) {
1568 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001569 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001570 }
1571 break;
1572
Antoine Pitroub52ec782009-01-25 16:34:23 +00001573 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001574 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001575 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001576 x = PyNumber_InPlacePower(v, w, Py_None);
1577 Py_DECREF(v);
1578 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001579 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001580 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001581 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Antoine Pitroub52ec782009-01-25 16:34:23 +00001583 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001584 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001585 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001586 x = PyNumber_InPlaceMultiply(v, w);
1587 Py_DECREF(v);
1588 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001589 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001590 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001591 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Antoine Pitroub52ec782009-01-25 16:34:23 +00001593 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001594 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001595 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001596 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001597 Py_DECREF(v);
1598 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001599 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001600 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001601 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Antoine Pitroub52ec782009-01-25 16:34:23 +00001603 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001604 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001605 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001606 x = PyNumber_InPlaceFloorDivide(v, w);
1607 Py_DECREF(v);
1608 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001609 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001610 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001611 break;
1612
Antoine Pitroub52ec782009-01-25 16:34:23 +00001613 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001614 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001615 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001616 x = PyNumber_InPlaceRemainder(v, w);
1617 Py_DECREF(v);
1618 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001619 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001620 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001621 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001622
Antoine Pitroub52ec782009-01-25 16:34:23 +00001623 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001624 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001625 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001626 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001627 PyUnicode_CheckExact(w)) {
1628 x = unicode_concatenate(v, w, f, next_instr);
1629 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001630 goto skip_decref_v;
1631 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001632 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001633 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001634 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001635 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001636 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001637 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001638 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001639 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001640 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Antoine Pitroub52ec782009-01-25 16:34:23 +00001642 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001643 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001644 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001645 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001646 Py_DECREF(v);
1647 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001648 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001649 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001650 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001651
Antoine Pitroub52ec782009-01-25 16:34:23 +00001652 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001653 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001654 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001655 x = PyNumber_InPlaceLshift(v, w);
1656 Py_DECREF(v);
1657 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001658 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001659 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001660 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Antoine Pitroub52ec782009-01-25 16:34:23 +00001662 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001663 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001664 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001665 x = PyNumber_InPlaceRshift(v, w);
1666 Py_DECREF(v);
1667 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001668 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001669 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001670 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001671
Antoine Pitroub52ec782009-01-25 16:34:23 +00001672 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001673 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001674 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001675 x = PyNumber_InPlaceAnd(v, w);
1676 Py_DECREF(v);
1677 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001678 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001679 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001680 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001681
Antoine Pitroub52ec782009-01-25 16:34:23 +00001682 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001683 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001684 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001685 x = PyNumber_InPlaceXor(v, w);
1686 Py_DECREF(v);
1687 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001688 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001689 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001690 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001691
Antoine Pitroub52ec782009-01-25 16:34:23 +00001692 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001693 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001694 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001695 x = PyNumber_InPlaceOr(v, w);
1696 Py_DECREF(v);
1697 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001698 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001699 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001700 break;
1701
Antoine Pitroub52ec782009-01-25 16:34:23 +00001702 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001703 w = TOP();
1704 v = SECOND();
1705 u = THIRD();
1706 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001707 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001708 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001709 Py_DECREF(u);
1710 Py_DECREF(v);
1711 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001712 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001714
Antoine Pitroub52ec782009-01-25 16:34:23 +00001715 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001716 w = TOP();
1717 v = SECOND();
1718 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001719 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001720 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001721 Py_DECREF(v);
1722 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001723 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001724 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001725
Antoine Pitroub52ec782009-01-25 16:34:23 +00001726 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001727 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001728 w = PySys_GetObject("displayhook");
1729 if (w == NULL) {
1730 PyErr_SetString(PyExc_RuntimeError,
1731 "lost sys.displayhook");
1732 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001733 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001734 }
1735 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001736 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001737 if (x == NULL)
1738 err = -1;
1739 }
1740 if (err == 0) {
1741 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001742 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001743 if (w == NULL)
1744 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001746 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001747 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001748 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001749
Thomas Wouters434d0822000-08-24 20:11:32 +00001750#ifdef CASE_TOO_BIG
1751 default: switch (opcode) {
1752#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001753 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001754 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001755 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001756 case 2:
1757 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001758 case 1:
1759 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001760 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001761 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001762 break;
1763 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001764 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001765 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001766 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001767 break;
1768 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001769 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001770
Antoine Pitroub52ec782009-01-25 16:34:23 +00001771 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001772 x = POP();
1773 v = f->f_locals;
1774 Py_XDECREF(v);
1775 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001776 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Antoine Pitroub52ec782009-01-25 16:34:23 +00001778 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001779 retval = POP();
1780 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001781 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001782
Antoine Pitroub52ec782009-01-25 16:34:23 +00001783 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001784 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001785 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001786 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001787 /* Put aside the current exception state and restore
1788 that of the calling frame. This only serves when
1789 "yield" is used inside an except handler. */
1790 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001791 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001792
Antoine Pitroub52ec782009-01-25 16:34:23 +00001793 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001794 {
1795 PyTryBlock *b = PyFrame_BlockPop(f);
1796 if (b->b_type != EXCEPT_HANDLER) {
1797 PyErr_SetString(PyExc_SystemError,
1798 "popped block is not an except handler");
1799 why = WHY_EXCEPTION;
1800 break;
1801 }
1802 UNWIND_EXCEPT_HANDLER(b);
1803 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001804 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001805
Antoine Pitroub52ec782009-01-25 16:34:23 +00001806 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001808 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001809 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001810 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001811 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001812
Christian Heimes180510d2008-03-03 19:15:45 +00001813 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001814 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001815 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001816 if (PyLong_Check(v)) {
1817 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001818 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001819 if (why == WHY_RETURN ||
1820 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001821 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001822 if (why == WHY_SILENCED) {
1823 /* An exception was silenced by 'with', we must
1824 manually unwind the EXCEPT_HANDLER block which was
1825 created when the exception was caught, otherwise
1826 the stack will be in an inconsistent state. */
1827 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersonac8c7302009-06-28 16:03:15 +00001828 assert(b->b_type == EXCEPT_HANDLER);
1829 UNWIND_EXCEPT_HANDLER(b);
1830 why = WHY_NOT;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001831 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001833 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001834 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001835 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001836 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001838 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001839 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001840 else if (v != Py_None) {
1841 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001842 "'finally' pops bad exception");
1843 why = WHY_EXCEPTION;
1844 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001845 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001846 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001847
Antoine Pitroub52ec782009-01-25 16:34:23 +00001848 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001849 x = PyDict_GetItemString(f->f_builtins,
1850 "__build_class__");
1851 if (x == NULL) {
1852 PyErr_SetString(PyExc_ImportError,
1853 "__build_class__ not found");
1854 break;
1855 }
1856 Py_INCREF(x);
1857 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001859
Antoine Pitroub52ec782009-01-25 16:34:23 +00001860 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001861 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001863 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001864 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001865 err = PyDict_SetItem(x, w, v);
1866 else
1867 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001868 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001869 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001870 break;
1871 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001872 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001873 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001874 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Antoine Pitroub52ec782009-01-25 16:34:23 +00001876 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001877 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001878 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001879 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001880 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001881 NAME_ERROR_MSG,
1882 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001883 break;
1884 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001885 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001886 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001888
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001889 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001890 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001892 if (PyTuple_CheckExact(v) &&
1893 PyTuple_GET_SIZE(v) == oparg) {
1894 PyObject **items = \
1895 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001896 while (oparg--) {
1897 w = items[oparg];
1898 Py_INCREF(w);
1899 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001900 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001901 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001902 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001903 } else if (PyList_CheckExact(v) &&
1904 PyList_GET_SIZE(v) == oparg) {
1905 PyObject **items = \
1906 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001907 while (oparg--) {
1908 w = items[oparg];
1909 Py_INCREF(w);
1910 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001911 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001912 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001913 stack_pointer + oparg)) {
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001914 STACKADJ(oparg);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001915 } else {
1916 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001917 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001918 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001919 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001921
Antoine Pitroub52ec782009-01-25 16:34:23 +00001922 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00001923 {
1924 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1925 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001926
Guido van Rossum0368b722007-05-11 16:50:42 +00001927 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1928 stack_pointer + totalargs)) {
1929 stack_pointer += totalargs;
1930 } else {
1931 why = WHY_EXCEPTION;
1932 }
1933 Py_DECREF(v);
1934 break;
1935 }
1936
Antoine Pitroub52ec782009-01-25 16:34:23 +00001937 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001938 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001939 v = TOP();
1940 u = SECOND();
1941 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001942 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1943 Py_DECREF(v);
1944 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001945 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001946 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Antoine Pitroub52ec782009-01-25 16:34:23 +00001948 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001949 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001950 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001951 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1952 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001953 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001954 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001955
Antoine Pitroub52ec782009-01-25 16:34:23 +00001956 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001957 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001958 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 err = PyDict_SetItem(f->f_globals, w, v);
1960 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001961 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001962 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001963
Antoine Pitroub52ec782009-01-25 16:34:23 +00001964 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001965 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001966 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001967 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001968 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001969 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001970
Antoine Pitroub52ec782009-01-25 16:34:23 +00001971 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001972 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001973 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001974 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001975 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001976 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001977 break;
1978 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001979 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001980 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001981 Py_XINCREF(x);
1982 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001983 else {
1984 x = PyObject_GetItem(v, w);
1985 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001986 if (!PyErr_ExceptionMatches(
1987 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001988 break;
1989 PyErr_Clear();
1990 }
1991 }
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_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001994 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001995 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001996 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001997 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001998 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001999 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 break;
2001 }
2002 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002003 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002005 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002006 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002007
Antoine Pitroub52ec782009-01-25 16:34:23 +00002008 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002009 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002010 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002011 /* Inline the PyDict_GetItem() calls.
2012 WARNING: this is an extreme speed hack.
2013 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002014 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002015 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002016 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002017 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002018 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002019 e = d->ma_lookup(d, w, hash);
2020 if (e == NULL) {
2021 x = NULL;
2022 break;
2023 }
2024 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002025 if (x != NULL) {
2026 Py_INCREF(x);
2027 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002028 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002029 }
2030 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002031 e = d->ma_lookup(d, w, hash);
2032 if (e == NULL) {
2033 x = NULL;
2034 break;
2035 }
2036 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002037 if (x != NULL) {
2038 Py_INCREF(x);
2039 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002040 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002041 }
2042 goto load_global_error;
2043 }
2044 }
2045 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002046 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002048 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002049 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002050 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002051 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002052 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002053 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002054 break;
2055 }
2056 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002057 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002059 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002060
Antoine Pitroub52ec782009-01-25 16:34:23 +00002061 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002062 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002063 if (x != NULL) {
2064 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002065 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002066 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002067 format_exc_check_arg(
2068 PyExc_UnboundLocalError,
2069 UNBOUNDLOCAL_ERROR_MSG,
2070 PyTuple_GetItem(co->co_varnames, oparg)
2071 );
2072 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002073
Antoine Pitroub52ec782009-01-25 16:34:23 +00002074 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002075 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002076 Py_INCREF(x);
2077 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002078 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002079 break;
2080
Antoine Pitroub52ec782009-01-25 16:34:23 +00002081 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002082 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002083 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002084 if (w != NULL) {
2085 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002086 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002087 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002088 err = -1;
2089 /* Don't stomp existing exception */
2090 if (PyErr_Occurred())
2091 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002092 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2093 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002094 oparg);
2095 format_exc_check_arg(
2096 PyExc_UnboundLocalError,
2097 UNBOUNDLOCAL_ERROR_MSG,
2098 v);
2099 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002100 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2101 PyTuple_GET_SIZE(co->co_cellvars));
2102 format_exc_check_arg(PyExc_NameError,
2103 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002104 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002105 break;
2106
Antoine Pitroub52ec782009-01-25 16:34:23 +00002107 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002108 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002109 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002110 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002111 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002112 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002113
Antoine Pitroub52ec782009-01-25 16:34:23 +00002114 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002115 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002116 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002117 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002118 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002119 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002120 }
2121 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002122 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002123 }
2124 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002125
Antoine Pitroub52ec782009-01-25 16:34:23 +00002126 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002127 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002128 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002129 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002130 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002131 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002132 }
2133 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002134 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002135 }
2136 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002137
Antoine Pitroub52ec782009-01-25 16:34:23 +00002138 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002139 x = PySet_New(NULL);
2140 if (x != NULL) {
2141 for (; --oparg >= 0;) {
2142 w = POP();
2143 if (err == 0)
2144 err = PySet_Add(x, w);
2145 Py_DECREF(w);
2146 }
2147 if (err != 0) {
2148 Py_DECREF(x);
2149 break;
2150 }
2151 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002152 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002153 }
2154 break;
2155
Antoine Pitroub52ec782009-01-25 16:34:23 +00002156 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002157 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002158 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002159 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002160 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002161
Antoine Pitroub52ec782009-01-25 16:34:23 +00002162 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002163 w = TOP(); /* key */
2164 u = SECOND(); /* value */
2165 v = THIRD(); /* dict */
2166 STACKADJ(-2);
2167 assert (PyDict_CheckExact(v));
2168 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2169 Py_DECREF(u);
2170 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002171 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002172 break;
2173
Antoine Pitroub52ec782009-01-25 16:34:23 +00002174 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002175 w = TOP(); /* key */
2176 u = SECOND(); /* value */
2177 STACKADJ(-2);
2178 v = stack_pointer[-oparg]; /* dict */
2179 assert (PyDict_CheckExact(v));
2180 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2181 Py_DECREF(u);
2182 Py_DECREF(w);
2183 if (err == 0) {
2184 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002185 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002186 }
2187 break;
2188
Antoine Pitroub52ec782009-01-25 16:34:23 +00002189 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002190 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002191 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002192 x = PyObject_GetAttr(v, w);
2193 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002194 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002195 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002196 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002197
Antoine Pitroub52ec782009-01-25 16:34:23 +00002198 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002199 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002200 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002201 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002202 Py_DECREF(v);
2203 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002204 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002205 if (x == NULL) break;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002206 PREDICT(POP_JUMP_IF_FALSE);
2207 PREDICT(POP_JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002208 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002209
Antoine Pitroub52ec782009-01-25 16:34:23 +00002210 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002211 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002212 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002213 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002214 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002215 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002216 break;
2217 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002218 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002219 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002220 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002221 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002222 w = PyTuple_Pack(5,
2223 w,
2224 f->f_globals,
2225 f->f_locals == NULL ?
2226 Py_None : f->f_locals,
2227 v,
2228 u);
2229 else
2230 w = PyTuple_Pack(4,
2231 w,
2232 f->f_globals,
2233 f->f_locals == NULL ?
2234 Py_None : f->f_locals,
2235 v);
2236 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002237 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002238 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002239 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002240 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002241 x = NULL;
2242 break;
2243 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002244 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002245 v = x;
2246 x = PyEval_CallObject(v, w);
2247 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002248 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002249 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002250 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002251 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002252 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002253
Antoine Pitroub52ec782009-01-25 16:34:23 +00002254 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002255 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002256 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002257 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002258 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002259 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002260 break;
2261 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002262 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002263 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002264 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002265 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002266 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002267 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002268 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002269
Antoine Pitroub52ec782009-01-25 16:34:23 +00002270 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002271 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002272 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002273 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002274 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002275 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002276 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002277 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002278 break;
2279
Antoine Pitroub52ec782009-01-25 16:34:23 +00002280 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002281 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002282 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002284 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2285 TARGET(POP_JUMP_IF_FALSE)
2286 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002287 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002288 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002289 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002290 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002291 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002292 Py_DECREF(w);
2293 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002294 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002295 }
2296 err = PyObject_IsTrue(w);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002297 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002298 if (err > 0)
2299 err = 0;
2300 else if (err == 0)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002301 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002302 else
2303 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002304 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002305
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002306 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2307 TARGET(POP_JUMP_IF_TRUE)
2308 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002309 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002310 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002311 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002312 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002313 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002314 Py_DECREF(w);
2315 JUMPTO(oparg);
2316 FAST_DISPATCH();
2317 }
2318 err = PyObject_IsTrue(w);
2319 Py_DECREF(w);
2320 if (err > 0) {
2321 err = 0;
2322 JUMPTO(oparg);
2323 }
2324 else if (err == 0)
2325 ;
2326 else
2327 break;
2328 DISPATCH();
2329
2330 TARGET(JUMP_IF_FALSE_OR_POP)
2331 w = TOP();
2332 if (w == Py_True) {
2333 STACKADJ(-1);
2334 Py_DECREF(w);
2335 FAST_DISPATCH();
2336 }
2337 if (w == Py_False) {
2338 JUMPTO(oparg);
2339 FAST_DISPATCH();
2340 }
2341 err = PyObject_IsTrue(w);
2342 if (err > 0) {
2343 STACKADJ(-1);
2344 Py_DECREF(w);
2345 err = 0;
2346 }
2347 else if (err == 0)
2348 JUMPTO(oparg);
2349 else
2350 break;
2351 DISPATCH();
2352
2353 TARGET(JUMP_IF_TRUE_OR_POP)
2354 w = TOP();
2355 if (w == Py_False) {
2356 STACKADJ(-1);
2357 Py_DECREF(w);
2358 FAST_DISPATCH();
2359 }
2360 if (w == Py_True) {
2361 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002362 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002363 }
2364 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002365 if (err > 0) {
2366 err = 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002367 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002368 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002369 else if (err == 0) {
2370 STACKADJ(-1);
2371 Py_DECREF(w);
2372 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002373 else
2374 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002375 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002376
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002377 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002378 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002379 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002380#if FAST_LOOPS
2381 /* Enabling this path speeds-up all while and for-loops by bypassing
2382 the per-loop checks for signals. By default, this should be turned-off
2383 because it prevents detection of a control-break in tight loops like
2384 "while 1: pass". Compile with this option turned-on when you need
2385 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002386 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002387 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002388 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002389#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002390 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002391#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002392
Antoine Pitroub52ec782009-01-25 16:34:23 +00002393 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002394 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002395 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002396 x = PyObject_GetIter(v);
2397 Py_DECREF(v);
2398 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002399 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002400 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002401 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002402 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002403 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002404 break;
2405
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002406 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002407 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002408 /* before: [iter]; after: [iter, iter()] *or* [] */
2409 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002410 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002411 if (x != NULL) {
2412 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002413 PREDICT(STORE_FAST);
2414 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002415 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002416 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002417 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002418 if (!PyErr_ExceptionMatches(
2419 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002420 break;
2421 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002422 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002423 /* iterator ended normally */
2424 x = v = POP();
2425 Py_DECREF(v);
2426 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002427 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002428
Antoine Pitroub52ec782009-01-25 16:34:23 +00002429 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002430 why = WHY_BREAK;
2431 goto fast_block_end;
2432
Antoine Pitroub52ec782009-01-25 16:34:23 +00002433 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002434 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002435 if (!retval) {
2436 x = NULL;
2437 break;
2438 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002439 why = WHY_CONTINUE;
2440 goto fast_block_end;
2441
Antoine Pitroub52ec782009-01-25 16:34:23 +00002442 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2443 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2444 TARGET(SETUP_FINALLY)
2445 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002446 /* NOTE: If you add any new block-setup opcodes that
2447 are not try/except/finally handlers, you may need
2448 to update the PyGen_NeedsFinalizing() function.
2449 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002450
Guido van Rossumb209a111997-04-29 18:18:01 +00002451 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002452 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002453 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002454
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002455 TARGET(SETUP_WITH)
2456 {
2457 static PyObject *exit, *enter;
2458 w = TOP();
2459 x = special_lookup(w, "__exit__", &exit);
2460 if (!x)
2461 break;
2462 SET_TOP(x);
2463 u = special_lookup(w, "__enter__", &enter);
2464 Py_DECREF(w);
2465 if (!u) {
2466 x = NULL;
2467 break;
2468 }
2469 x = PyObject_CallFunctionObjArgs(u, NULL);
2470 Py_DECREF(u);
2471 if (!x)
2472 break;
2473 /* Setup the finally block before pushing the result
2474 of __enter__ on the stack. */
2475 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2476 STACK_LEVEL());
2477
2478 PUSH(x);
2479 DISPATCH();
2480 }
2481
Antoine Pitroub52ec782009-01-25 16:34:23 +00002482 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002483 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002484 /* At the top of the stack are 1-3 values indicating
2485 how/why we entered the finally clause:
2486 - TOP = None
2487 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2488 - TOP = WHY_*; no retval below it
2489 - (TOP, SECOND, THIRD) = exc_info()
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002490 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002491 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002492 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002493 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002494 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002495 EXIT(None, None, None)
2496
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002497 In the first two cases, we remove EXIT from the
2498 stack, leaving the rest in the same order. In the
2499 third case, we shift the bottom 3 values of the
2500 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002501
2502 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002503 *and* the function call returns a 'true' value, we
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002504 push WHY_SILENCED onto the stack. END_FINALLY will
2505 then not re-raise the exception. (But non-local
2506 gotos should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002507 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002508
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002509 PyObject *exit_func;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002510 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002511 if (u == Py_None) {
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002512 POP();
2513 exit_func = TOP();
2514 SET_TOP(u);
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002515 v = w = Py_None;
2516 }
2517 else if (PyLong_Check(u)) {
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002518 POP();
2519 switch(PyLong_AsLong(u)) {
2520 case WHY_RETURN:
2521 case WHY_CONTINUE:
2522 /* Retval in TOP. */
2523 exit_func = SECOND();
2524 SET_SECOND(TOP());
2525 SET_TOP(u);
2526 break;
2527 default:
2528 exit_func = TOP();
2529 SET_TOP(u);
2530 break;
2531 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002532 u = v = w = Py_None;
2533 }
2534 else {
Benjamin Peterson176101d2009-06-28 15:40:50 +00002535 PyObject *tp, *exc, *tb;
2536 PyTryBlock *block;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002537 v = SECOND();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002538 w = THIRD();
Benjamin Peterson176101d2009-06-28 15:40:50 +00002539 tp = FOURTH();
Benjamin Petersonb1715f12009-06-28 16:21:52 +00002540 exc = PEEK(5);
2541 tb = PEEK(6);
2542 exit_func = PEEK(7);
2543 SET_VALUE(7, tb);
2544 SET_VALUE(6, exc);
2545 SET_VALUE(5, tp);
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002546 /* UNWIND_EXCEPT_BLOCK will pop this off. */
Benjamin Petersonb1715f12009-06-28 16:21:52 +00002547 SET_FOURTH(NULL);
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002548 /* We just shifted the stack down, so we have
2549 to tell the except handler block that the
2550 values are lower than it expects. */
Benjamin Peterson176101d2009-06-28 15:40:50 +00002551 block = &f->f_blockstack[f->f_iblock - 1];
2552 assert(block->b_type == EXCEPT_HANDLER);
2553 block->b_level--;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002554 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002555 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002556 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2557 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002558 Py_DECREF(exit_func);
2559 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002560 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002561
2562 if (u != Py_None)
2563 err = PyObject_IsTrue(x);
2564 else
2565 err = 0;
2566 Py_DECREF(x);
2567
2568 if (err < 0)
2569 break; /* Go to error exit */
2570 else if (err > 0) {
2571 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002572 /* There was an exception and a True return */
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002573 PUSH(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002574 }
Christian Heimes180510d2008-03-03 19:15:45 +00002575 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002576 break;
2577 }
2578
Antoine Pitroub52ec782009-01-25 16:34:23 +00002579 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002580 {
2581 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002582 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002583 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002584#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002585 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002586#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002587 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002588#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002589 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002590 PUSH(x);
2591 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002592 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002593 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002594 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002595
Antoine Pitroub52ec782009-01-25 16:34:23 +00002596 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2597 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2598 TARGET(CALL_FUNCTION_VAR_KW)
2599 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002600 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002601 int na = oparg & 0xff;
2602 int nk = (oparg>>8) & 0xff;
2603 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002604 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002605 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002606 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002607 if (flags & CALL_FLAG_VAR)
2608 n++;
2609 if (flags & CALL_FLAG_KW)
2610 n++;
2611 pfunc = stack_pointer - n - 1;
2612 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002613
Guido van Rossumac7be682001-01-17 15:42:30 +00002614 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002615 && PyMethod_GET_SELF(func) != NULL) {
2616 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002617 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002618 func = PyMethod_GET_FUNCTION(func);
2619 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002620 Py_DECREF(*pfunc);
2621 *pfunc = self;
2622 na++;
2623 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002624 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002625 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002626 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002627 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002628 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002629 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002630 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002631 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002632
Jeremy Hylton76901512000-03-28 23:49:17 +00002633 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002634 w = POP();
2635 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002636 }
2637 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002638 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002639 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002640 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002641 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002642
Antoine Pitroub52ec782009-01-25 16:34:23 +00002643 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2644 TARGET(MAKE_FUNCTION)
2645 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002646 {
2647 int posdefaults = oparg & 0xff;
2648 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002649 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002650
Guido van Rossum681d79a1995-07-18 14:51:37 +00002651 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002652 x = PyFunction_New(v, f->f_globals);
2653 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002654
Guido van Rossum0240b922007-02-26 21:23:50 +00002655 if (x != NULL && opcode == MAKE_CLOSURE) {
2656 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002657 if (PyFunction_SetClosure(x, v) != 0) {
2658 /* Can't happen unless bytecode is corrupt. */
2659 why = WHY_EXCEPTION;
2660 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002661 Py_DECREF(v);
2662 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002663
2664 if (x != NULL && num_annotations > 0) {
2665 Py_ssize_t name_ix;
2666 u = POP(); /* names of args with annotations */
2667 v = PyDict_New();
2668 if (v == NULL) {
2669 Py_DECREF(x);
2670 x = NULL;
2671 break;
2672 }
2673 name_ix = PyTuple_Size(u);
2674 assert(num_annotations == name_ix+1);
2675 while (name_ix > 0) {
2676 --name_ix;
2677 t = PyTuple_GET_ITEM(u, name_ix);
2678 w = POP();
2679 /* XXX(nnorwitz): check for errors */
2680 PyDict_SetItem(v, t, w);
2681 Py_DECREF(w);
2682 }
2683
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002684 if (PyFunction_SetAnnotations(x, v) != 0) {
2685 /* Can't happen unless
2686 PyFunction_SetAnnotations changes. */
2687 why = WHY_EXCEPTION;
2688 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002689 Py_DECREF(v);
2690 Py_DECREF(u);
2691 }
2692
Guido van Rossum681d79a1995-07-18 14:51:37 +00002693 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002694 if (x != NULL && posdefaults > 0) {
2695 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002696 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002697 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002698 x = NULL;
2699 break;
2700 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002701 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002702 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002703 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002704 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002705 if (PyFunction_SetDefaults(x, v) != 0) {
2706 /* Can't happen unless
2707 PyFunction_SetDefaults changes. */
2708 why = WHY_EXCEPTION;
2709 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002710 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002711 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002712 if (x != NULL && kwdefaults > 0) {
2713 v = PyDict_New();
2714 if (v == NULL) {
2715 Py_DECREF(x);
2716 x = NULL;
2717 break;
2718 }
2719 while (--kwdefaults >= 0) {
2720 w = POP(); /* default value */
2721 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002722 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002723 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002724 Py_DECREF(w);
2725 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002726 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002727 if (PyFunction_SetKwDefaults(x, v) != 0) {
2728 /* Can't happen unless
2729 PyFunction_SetKwDefaults changes. */
2730 why = WHY_EXCEPTION;
2731 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002732 Py_DECREF(v);
2733 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002734 PUSH(x);
2735 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002736 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002737
Antoine Pitroub52ec782009-01-25 16:34:23 +00002738 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002739 if (oparg == 3)
2740 w = POP();
2741 else
2742 w = NULL;
2743 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002744 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002745 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002746 Py_DECREF(u);
2747 Py_DECREF(v);
2748 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002749 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002750 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002751 break;
2752
Antoine Pitroub52ec782009-01-25 16:34:23 +00002753 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002754 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002755 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002756 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002757
Antoine Pitroub52ec782009-01-25 16:34:23 +00002758#ifdef USE_COMPUTED_GOTOS
2759 _unknown_opcode:
2760#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002761 default:
2762 fprintf(stderr,
2763 "XXX lineno: %d, opcode: %d\n",
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002764 PyFrame_GetLineNumber(f),
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002765 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002766 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002767 why = WHY_EXCEPTION;
2768 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002769
2770#ifdef CASE_TOO_BIG
2771 }
2772#endif
2773
Guido van Rossum374a9221991-04-04 10:40:29 +00002774 } /* switch */
2775
2776 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002777
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002778 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002779
Guido van Rossum374a9221991-04-04 10:40:29 +00002780 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002781
Guido van Rossum374a9221991-04-04 10:40:29 +00002782 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002783 if (err == 0 && x != NULL) {
2784#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002785 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002786 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002787 fprintf(stderr,
2788 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002789 else {
2790#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002791 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002792 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002793#ifdef CHECKEXC
2794 }
2795#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002796 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002797 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002798 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002799 err = 0;
2800 }
2801
Guido van Rossum374a9221991-04-04 10:40:29 +00002802 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002803
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002804 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002805 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002806 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002807 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002808 why = WHY_EXCEPTION;
2809 }
2810 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002811#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002812 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002813 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002814 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002815 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002816 sprintf(buf, "Stack unwind with exception "
2817 "set and why=%d", why);
2818 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002819 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002820 }
2821#endif
2822
2823 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002824
Guido van Rossum374a9221991-04-04 10:40:29 +00002825 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002826 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002827
Fred Drake8f51f542001-10-04 14:48:42 +00002828 if (tstate->c_tracefunc != NULL)
2829 call_exc_trace(tstate->c_tracefunc,
2830 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002831 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002832
Guido van Rossum374a9221991-04-04 10:40:29 +00002833 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002834
Guido van Rossum374a9221991-04-04 10:40:29 +00002835 if (why == WHY_RERAISE)
2836 why = WHY_EXCEPTION;
2837
2838 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002839
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002840fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002841 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002842 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002843
Tim Peters8a5c3c72004-04-05 19:36:21 +00002844 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002845 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2846 /* For a continue inside a try block,
2847 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002848 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2849 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002850 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002851 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002852 Py_DECREF(retval);
2853 break;
2854 }
2855
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002856 if (b->b_type == EXCEPT_HANDLER) {
2857 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002858 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002859 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002860 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002861 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2862 why = WHY_NOT;
2863 JUMPTO(b->b_handler);
2864 break;
2865 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002866 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2867 || b->b_type == SETUP_FINALLY)) {
2868 PyObject *exc, *val, *tb;
2869 int handler = b->b_handler;
2870 /* Beware, this invalidates all b->b_* fields */
2871 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2872 PUSH(tstate->exc_traceback);
2873 PUSH(tstate->exc_value);
2874 if (tstate->exc_type != NULL) {
2875 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002876 }
2877 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002878 Py_INCREF(Py_None);
2879 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002880 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002881 PyErr_Fetch(&exc, &val, &tb);
2882 /* Make the raw exception data
2883 available to the handler,
2884 so a program can emulate the
2885 Python main loop. */
2886 PyErr_NormalizeException(
2887 &exc, &val, &tb);
2888 PyException_SetTraceback(val, tb);
2889 Py_INCREF(exc);
2890 tstate->exc_type = exc;
2891 Py_INCREF(val);
2892 tstate->exc_value = val;
2893 tstate->exc_traceback = tb;
2894 if (tb == NULL)
2895 tb = Py_None;
2896 Py_INCREF(tb);
2897 PUSH(tb);
2898 PUSH(val);
2899 PUSH(exc);
2900 why = WHY_NOT;
2901 JUMPTO(handler);
2902 break;
2903 }
2904 if (b->b_type == SETUP_FINALLY) {
2905 if (why & (WHY_RETURN | WHY_CONTINUE))
2906 PUSH(retval);
2907 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002908 why = WHY_NOT;
2909 JUMPTO(b->b_handler);
2910 break;
2911 }
2912 } /* unwind stack */
2913
2914 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002915
Guido van Rossum374a9221991-04-04 10:40:29 +00002916 if (why != WHY_NOT)
2917 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002918 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002919
Guido van Rossum374a9221991-04-04 10:40:29 +00002920 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002921
Tim Peters8a5c3c72004-04-05 19:36:21 +00002922 assert(why != WHY_YIELD);
2923 /* Pop remaining stack entries. */
2924 while (!EMPTY()) {
2925 v = POP();
2926 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002927 }
2928
Tim Peters8a5c3c72004-04-05 19:36:21 +00002929 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002930 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002931
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002932fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002933 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002934 if (tstate->c_tracefunc) {
2935 if (why == WHY_RETURN || why == WHY_YIELD) {
2936 if (call_trace(tstate->c_tracefunc,
2937 tstate->c_traceobj, f,
2938 PyTrace_RETURN, retval)) {
2939 Py_XDECREF(retval);
2940 retval = NULL;
2941 why = WHY_EXCEPTION;
2942 }
2943 }
2944 else if (why == WHY_EXCEPTION) {
2945 call_trace_protected(tstate->c_tracefunc,
2946 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002947 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002948 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002949 }
Fred Drake8f51f542001-10-04 14:48:42 +00002950 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002951 if (why == WHY_EXCEPTION)
2952 call_trace_protected(tstate->c_profilefunc,
2953 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002954 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002955 else if (call_trace(tstate->c_profilefunc,
2956 tstate->c_profileobj, f,
2957 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002958 Py_XDECREF(retval);
2959 retval = NULL;
2960 why = WHY_EXCEPTION;
2961 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002962 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002963 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002964
Tim Peters5ca576e2001-06-18 22:08:13 +00002965 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002966exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002967 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002968 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002969
Guido van Rossum96a42c81992-01-12 02:29:51 +00002970 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002971}
2972
Guido van Rossumc2e20742006-02-27 22:32:47 +00002973/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002974 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002975 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002976
Tim Peters6d6c1a32001-08-02 04:15:00 +00002977PyObject *
2978PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002979 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002980 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002981{
2982 register PyFrameObject *f;
2983 register PyObject *retval = NULL;
2984 register PyObject **fastlocals, **freevars;
2985 PyThreadState *tstate = PyThreadState_GET();
2986 PyObject *x, *u;
2987
2988 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002989 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002990 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002991 return NULL;
2992 }
2993
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002994 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002995 assert(globals != NULL);
2996 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002997 if (f == NULL)
2998 return NULL;
2999
3000 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003001 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003002
3003 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00003004 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00003005 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3006 int i;
3007 int n = argcount;
3008 PyObject *kwdict = NULL;
3009 if (co->co_flags & CO_VARKEYWORDS) {
3010 kwdict = PyDict_New();
3011 if (kwdict == NULL)
3012 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003013 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003014 if (co->co_flags & CO_VARARGS)
3015 i++;
3016 SETLOCAL(i, kwdict);
3017 }
3018 if (argcount > co->co_argcount) {
3019 if (!(co->co_flags & CO_VARARGS)) {
3020 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003021 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003022 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003023 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003024 defcount ? "at most" : "exactly",
3025 co->co_argcount,
3026 kwcount ? "non-keyword " : "",
3027 co->co_argcount == 1 ? "" : "s",
3028 argcount);
3029 goto fail;
3030 }
3031 n = co->co_argcount;
3032 }
3033 for (i = 0; i < n; i++) {
3034 x = args[i];
3035 Py_INCREF(x);
3036 SETLOCAL(i, x);
3037 }
3038 if (co->co_flags & CO_VARARGS) {
3039 u = PyTuple_New(argcount - n);
3040 if (u == NULL)
3041 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003042 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00003043 for (i = n; i < argcount; i++) {
3044 x = args[i];
3045 Py_INCREF(x);
3046 PyTuple_SET_ITEM(u, i-n, x);
3047 }
3048 }
3049 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003050 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003051 PyObject *keyword = kws[2*i];
3052 PyObject *value = kws[2*i + 1];
3053 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00003054 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003055 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003056 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003057 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00003058 goto fail;
3059 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003060 /* Speed hack: do raw pointer compares. As names are
3061 normally interned this should almost always hit. */
3062 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003063 for (j = 0;
3064 j < co->co_argcount + co->co_kwonlyargcount;
3065 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003066 PyObject *nm = co_varnames[j];
3067 if (nm == keyword)
3068 goto kw_found;
3069 }
3070 /* Slow fallback, just in case */
3071 for (j = 0;
3072 j < co->co_argcount + co->co_kwonlyargcount;
3073 j++) {
3074 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003075 int cmp = PyObject_RichCompareBool(
3076 keyword, nm, Py_EQ);
3077 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003078 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003079 else if (cmp < 0)
3080 goto fail;
3081 }
3082 /* Check errors from Compare */
3083 if (PyErr_Occurred())
3084 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003085 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003086 if (kwdict == NULL) {
3087 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003088 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00003089 "keyword argument '%S'",
3090 co->co_name,
3091 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00003092 goto fail;
3093 }
3094 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003095 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003096 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003097kw_found:
3098 if (GETLOCAL(j) != NULL) {
3099 PyErr_Format(PyExc_TypeError,
3100 "%U() got multiple "
3101 "values for keyword "
3102 "argument '%S'",
3103 co->co_name,
3104 keyword);
3105 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003106 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003107 Py_INCREF(value);
3108 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003109 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003110 if (co->co_kwonlyargcount > 0) {
3111 for (i = co->co_argcount;
3112 i < co->co_argcount + co->co_kwonlyargcount;
3113 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003114 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003115 if (GETLOCAL(i) != NULL)
3116 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003117 name = PyTuple_GET_ITEM(co->co_varnames, i);
3118 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003119 if (kwdefs != NULL)
3120 def = PyDict_GetItem(kwdefs, name);
3121 if (def != NULL) {
3122 Py_INCREF(def);
3123 SETLOCAL(i, def);
3124 continue;
3125 }
3126 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003127 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003128 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003129 goto fail;
3130 }
3131 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003132 if (argcount < co->co_argcount) {
3133 int m = co->co_argcount - defcount;
3134 for (i = argcount; i < m; i++) {
3135 if (GETLOCAL(i) == NULL) {
3136 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003137 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003138 "%spositional argument%s "
3139 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003140 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003141 ((co->co_flags & CO_VARARGS) ||
3142 defcount) ? "at least"
3143 : "exactly",
3144 m, kwcount ? "non-keyword " : "",
3145 m == 1 ? "" : "s", i);
3146 goto fail;
3147 }
3148 }
3149 if (n > m)
3150 i = n - m;
3151 else
3152 i = 0;
3153 for (; i < defcount; i++) {
3154 if (GETLOCAL(m+i) == NULL) {
3155 PyObject *def = defs[i];
3156 Py_INCREF(def);
3157 SETLOCAL(m+i, def);
3158 }
3159 }
3160 }
3161 }
3162 else {
3163 if (argcount > 0 || kwcount > 0) {
3164 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003165 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003166 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003167 argcount + kwcount);
3168 goto fail;
3169 }
3170 }
3171 /* Allocate and initialize storage for cell vars, and copy free
3172 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003173 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003174 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003175 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003176 PyObject *c;
3177
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003178 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003179 if (co->co_flags & CO_VARARGS)
3180 nargs++;
3181 if (co->co_flags & CO_VARKEYWORDS)
3182 nargs++;
3183
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184 /* Initialize each cell var, taking into account
3185 cell vars that are initialized from arguments.
3186
3187 Should arrange for the compiler to put cellvars
3188 that are arguments at the beginning of the cellvars
3189 list so that we can march over it more efficiently?
3190 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003191 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003192 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003193 PyTuple_GET_ITEM(co->co_cellvars, i));
3194 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003196 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003197 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003198 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003199 c = PyCell_New(GETLOCAL(j));
3200 if (c == NULL)
3201 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003202 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003203 found = 1;
3204 break;
3205 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003206 }
3207 if (found == 0) {
3208 c = PyCell_New(NULL);
3209 if (c == NULL)
3210 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003211 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003212 }
3213 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003214 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003215 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003216 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003217 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003218 PyObject *o = PyTuple_GET_ITEM(closure, i);
3219 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003220 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003221 }
3222 }
3223
Tim Peters5ca576e2001-06-18 22:08:13 +00003224 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003225 /* Don't need to keep the reference to f_back, it will be set
3226 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003227 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003228 f->f_back = NULL;
3229
Jeremy Hylton985eba52003-02-05 23:13:00 +00003230 PCALL(PCALL_GENERATOR);
3231
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003232 /* Create a new generator that owns the ready to run frame
3233 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003234 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003235 }
3236
Thomas Woutersce272b62007-09-19 21:19:28 +00003237 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003238
Thomas Woutersce272b62007-09-19 21:19:28 +00003239fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003240
Tim Petersb13680b2001-11-27 23:29:29 +00003241 /* decref'ing the frame can cause __del__ methods to get invoked,
3242 which can call back into Python. While we're done with the
3243 current Python frame (f), the associated C stack is still in use,
3244 so recursion_depth must be boosted for the duration.
3245 */
3246 assert(tstate != NULL);
3247 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003248 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003249 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003250 return retval;
3251}
3252
3253
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003254static PyObject *
3255special_lookup(PyObject *o, char *meth, PyObject **cache)
3256{
3257 PyObject *res;
3258 res = _PyObject_LookupSpecial(o, meth, cache);
3259 if (res == NULL && !PyErr_Occurred()) {
3260 PyErr_SetObject(PyExc_AttributeError, *cache);
3261 return NULL;
3262 }
3263 return res;
3264}
3265
3266
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003267/* Logic for the raise statement (too complicated for inlining).
3268 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003269static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003270do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003271{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003272 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003273
3274 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003275 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003276 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003277 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003278 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003279 value = tstate->exc_value;
3280 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003281 if (type == Py_None) {
3282 PyErr_SetString(PyExc_RuntimeError,
3283 "No active exception to reraise");
3284 return WHY_EXCEPTION;
3285 }
3286 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003287 Py_XINCREF(value);
3288 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003289 PyErr_Restore(type, value, tb);
3290 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003292
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003293 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003294 raise
3295 raise <instance>
3296 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003297
Collin Winter828f04a2007-08-31 00:04:24 +00003298 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003299 type = exc;
3300 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003301 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003302 goto raise_error;
3303 }
Collin Winter828f04a2007-08-31 00:04:24 +00003304 else if (PyExceptionInstance_Check(exc)) {
3305 value = exc;
3306 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003307 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003308 }
3309 else {
3310 /* Not something you can raise. You get an exception
3311 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003312 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003313 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003314 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003315 goto raise_error;
3316 }
Collin Winter828f04a2007-08-31 00:04:24 +00003317
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003318 if (cause) {
3319 PyObject *fixed_cause;
3320 if (PyExceptionClass_Check(cause)) {
3321 fixed_cause = PyObject_CallObject(cause, NULL);
3322 if (fixed_cause == NULL)
3323 goto raise_error;
3324 Py_DECREF(cause);
3325 }
3326 else if (PyExceptionInstance_Check(cause)) {
3327 fixed_cause = cause;
3328 }
3329 else {
3330 PyErr_SetString(PyExc_TypeError,
3331 "exception causes must derive from "
3332 "BaseException");
3333 goto raise_error;
3334 }
3335 PyException_SetCause(value, fixed_cause);
3336 }
Collin Winter828f04a2007-08-31 00:04:24 +00003337
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003338 PyErr_SetObject(type, value);
3339 /* PyErr_SetObject incref's its arguments */
3340 Py_XDECREF(value);
3341 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003342 return WHY_EXCEPTION;
3343
3344raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003345 Py_XDECREF(value);
3346 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003347 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003348 return WHY_EXCEPTION;
3349}
3350
Tim Petersd6d010b2001-06-21 02:49:55 +00003351/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003352 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003353
Guido van Rossum0368b722007-05-11 16:50:42 +00003354 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3355 with a variable target.
3356*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003357
Barry Warsawe42b18f1997-08-25 22:13:04 +00003358static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003359unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003360{
Guido van Rossum0368b722007-05-11 16:50:42 +00003361 int i = 0, j = 0;
3362 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003363 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003364 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003365 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003366
Tim Petersd6d010b2001-06-21 02:49:55 +00003367 assert(v != NULL);
3368
3369 it = PyObject_GetIter(v);
3370 if (it == NULL)
3371 goto Error;
3372
3373 for (; i < argcnt; i++) {
3374 w = PyIter_Next(it);
3375 if (w == NULL) {
3376 /* Iterator done, via error or exhaustion. */
3377 if (!PyErr_Occurred()) {
3378 PyErr_Format(PyExc_ValueError,
3379 "need more than %d value%s to unpack",
3380 i, i == 1 ? "" : "s");
3381 }
3382 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003383 }
3384 *--sp = w;
3385 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003386
Guido van Rossum0368b722007-05-11 16:50:42 +00003387 if (argcntafter == -1) {
3388 /* We better have exhausted the iterator now. */
3389 w = PyIter_Next(it);
3390 if (w == NULL) {
3391 if (PyErr_Occurred())
3392 goto Error;
3393 Py_DECREF(it);
3394 return 1;
3395 }
3396 Py_DECREF(w);
3397 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3398 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003399 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003400
3401 l = PySequence_List(it);
3402 if (l == NULL)
3403 goto Error;
3404 *--sp = l;
3405 i++;
3406
3407 ll = PyList_GET_SIZE(l);
3408 if (ll < argcntafter) {
3409 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3410 argcnt + ll);
3411 goto Error;
3412 }
3413
3414 /* Pop the "after-variable" args off the list. */
3415 for (j = argcntafter; j > 0; j--, i++) {
3416 *--sp = PyList_GET_ITEM(l, ll - j);
3417 }
3418 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003419 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003420 Py_DECREF(it);
3421 return 1;
3422
Tim Petersd6d010b2001-06-21 02:49:55 +00003423Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003424 for (; i > 0; i--, sp++)
3425 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003426 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003427 return 0;
3428}
3429
3430
Guido van Rossum96a42c81992-01-12 02:29:51 +00003431#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003432static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003433prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003434{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003435 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003436 if (PyObject_Print(v, stdout, 0) != 0)
3437 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003438 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003439 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003440}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003441#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003442
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003443static void
Fred Drake5755ce62001-06-27 19:19:46 +00003444call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003445{
Guido van Rossumb209a111997-04-29 18:18:01 +00003446 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003447 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003448 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003449 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003450 value = Py_None;
3451 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003452 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003453 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003454 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003455 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003456 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003457 }
Fred Drake5755ce62001-06-27 19:19:46 +00003458 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003459 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003460 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003461 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003462 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003463 Py_XDECREF(type);
3464 Py_XDECREF(value);
3465 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003466 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003467}
3468
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003469static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003470call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003471 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003472{
3473 PyObject *type, *value, *traceback;
3474 int err;
3475 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003476 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003477 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003478 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003479 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003480 return 0;
3481 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003482 else {
3483 Py_XDECREF(type);
3484 Py_XDECREF(value);
3485 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003486 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003487 }
3488}
3489
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003490static int
Fred Drake5755ce62001-06-27 19:19:46 +00003491call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3492 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003493{
Fred Drake5755ce62001-06-27 19:19:46 +00003494 register PyThreadState *tstate = frame->f_tstate;
3495 int result;
3496 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003497 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003498 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003499 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003500 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003501 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3502 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003503 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003504 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003505}
3506
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003507PyObject *
3508_PyEval_CallTracing(PyObject *func, PyObject *args)
3509{
3510 PyFrameObject *frame = PyEval_GetFrame();
3511 PyThreadState *tstate = frame->f_tstate;
3512 int save_tracing = tstate->tracing;
3513 int save_use_tracing = tstate->use_tracing;
3514 PyObject *result;
3515
3516 tstate->tracing = 0;
3517 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3518 || (tstate->c_profilefunc != NULL));
3519 result = PyObject_Call(func, args, NULL);
3520 tstate->tracing = save_tracing;
3521 tstate->use_tracing = save_use_tracing;
3522 return result;
3523}
3524
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003525/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003526static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003527maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003528 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3529 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003530{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003531 int result = 0;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003532 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003533
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003534 /* If the last instruction executed isn't in the current
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003535 instruction window, reset the window.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003536 */
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003537 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003538 PyAddrPair bounds;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003539 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3540 &bounds);
Thomas Woutersce272b62007-09-19 21:19:28 +00003541 *instr_lb = bounds.ap_lower;
3542 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003543 }
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003544 /* If the last instruction falls at the start of a line or if
3545 it represents a jump backwards, update the frame's line
3546 number and call the trace function. */
3547 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3548 frame->f_lineno = line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003549 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003550 }
3551 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003552 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003553}
3554
Fred Drake5755ce62001-06-27 19:19:46 +00003555void
3556PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003557{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003558 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003559 PyObject *temp = tstate->c_profileobj;
3560 Py_XINCREF(arg);
3561 tstate->c_profilefunc = NULL;
3562 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003563 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003564 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003565 Py_XDECREF(temp);
3566 tstate->c_profilefunc = func;
3567 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003568 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003569 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003570}
3571
3572void
3573PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3574{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003575 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003576 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003577 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003578 Py_XINCREF(arg);
3579 tstate->c_tracefunc = NULL;
3580 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003581 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003582 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003583 Py_XDECREF(temp);
3584 tstate->c_tracefunc = func;
3585 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003586 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003587 tstate->use_tracing = ((func != NULL)
3588 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003589}
3590
Guido van Rossumb209a111997-04-29 18:18:01 +00003591PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003592PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003593{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003594 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003595 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003596 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003597 else
3598 return current_frame->f_builtins;
3599}
3600
Guido van Rossumb209a111997-04-29 18:18:01 +00003601PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003602PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003603{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003604 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003605 if (current_frame == NULL)
3606 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003607 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003608 return current_frame->f_locals;
3609}
3610
Guido van Rossumb209a111997-04-29 18:18:01 +00003611PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003612PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003613{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003614 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003615 if (current_frame == NULL)
3616 return NULL;
3617 else
3618 return current_frame->f_globals;
3619}
3620
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003621PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003622PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003623{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003624 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003625 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003626}
3627
Guido van Rossum6135a871995-01-09 17:53:26 +00003628int
Tim Peters5ba58662001-07-16 02:29:45 +00003629PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003630{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003631 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003632 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003633
3634 if (current_frame != NULL) {
3635 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003636 const int compilerflags = codeflags & PyCF_MASK;
3637 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003638 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003639 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003640 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003641#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003642 if (codeflags & CO_GENERATOR_ALLOWED) {
3643 result = 1;
3644 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3645 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003646#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003647 }
3648 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003649}
3650
Guido van Rossum3f5da241990-12-20 15:06:42 +00003651
Guido van Rossum681d79a1995-07-18 14:51:37 +00003652/* External interface to call any callable object.
3653 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003654
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003655#undef PyEval_CallObject
3656/* for backward compatibility: export this interface */
3657
Guido van Rossumb209a111997-04-29 18:18:01 +00003658PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003659PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003660{
Guido van Rossumb209a111997-04-29 18:18:01 +00003661 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003662}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003663#define PyEval_CallObject(func,arg) \
3664 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003665
Guido van Rossumb209a111997-04-29 18:18:01 +00003666PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003667PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003668{
Jeremy Hylton52820442001-01-03 23:52:36 +00003669 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003670
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003671 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003672 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003673 if (arg == NULL)
3674 return NULL;
3675 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003676 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003677 PyErr_SetString(PyExc_TypeError,
3678 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003679 return NULL;
3680 }
3681 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003682 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003683
Guido van Rossumb209a111997-04-29 18:18:01 +00003684 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003685 PyErr_SetString(PyExc_TypeError,
3686 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003687 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003688 return NULL;
3689 }
3690
Tim Peters6d6c1a32001-08-02 04:15:00 +00003691 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003692 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003693 return result;
3694}
3695
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003696const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003697PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003698{
3699 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003700 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003701 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003702 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003703 else if (PyCFunction_Check(func))
3704 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003705 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003706 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003707}
3708
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003709const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003710PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003711{
3712 if (PyMethod_Check(func))
3713 return "()";
3714 else if (PyFunction_Check(func))
3715 return "()";
3716 else if (PyCFunction_Check(func))
3717 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003718 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003719 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003720}
3721
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003722static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003723err_args(PyObject *func, int flags, int nargs)
3724{
3725 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003726 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003727 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003728 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003729 nargs);
3730 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003731 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003732 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003733 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003734 nargs);
3735}
3736
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003737#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003738if (tstate->use_tracing && tstate->c_profilefunc) { \
3739 if (call_trace(tstate->c_profilefunc, \
3740 tstate->c_profileobj, \
3741 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003742 func)) { \
3743 x = NULL; \
3744 } \
3745 else { \
3746 x = call; \
3747 if (tstate->c_profilefunc != NULL) { \
3748 if (x == NULL) { \
3749 call_trace_protected(tstate->c_profilefunc, \
3750 tstate->c_profileobj, \
3751 tstate->frame, PyTrace_C_EXCEPTION, \
3752 func); \
3753 /* XXX should pass (type, value, tb) */ \
3754 } else { \
3755 if (call_trace(tstate->c_profilefunc, \
3756 tstate->c_profileobj, \
3757 tstate->frame, PyTrace_C_RETURN, \
3758 func)) { \
3759 Py_DECREF(x); \
3760 x = NULL; \
3761 } \
3762 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003763 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003764 } \
3765} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003766 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003767 }
3768
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003769static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003770call_function(PyObject ***pp_stack, int oparg
3771#ifdef WITH_TSC
3772 , uint64* pintr0, uint64* pintr1
3773#endif
3774 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003775{
3776 int na = oparg & 0xff;
3777 int nk = (oparg>>8) & 0xff;
3778 int n = na + 2 * nk;
3779 PyObject **pfunc = (*pp_stack) - n - 1;
3780 PyObject *func = *pfunc;
3781 PyObject *x, *w;
3782
Jeremy Hylton985eba52003-02-05 23:13:00 +00003783 /* Always dispatch PyCFunction first, because these are
3784 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003785 */
3786 if (PyCFunction_Check(func) && nk == 0) {
3787 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003788 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003789
3790 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003791 if (flags & (METH_NOARGS | METH_O)) {
3792 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3793 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003794 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003795 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003796 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003797 else if (flags & METH_O && na == 1) {
3798 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003799 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003800 Py_DECREF(arg);
3801 }
3802 else {
3803 err_args(func, flags, na);
3804 x = NULL;
3805 }
3806 }
3807 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003808 PyObject *callargs;
3809 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003810 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003811 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003812 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003813 Py_XDECREF(callargs);
3814 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003815 } else {
3816 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3817 /* optimize access to bound methods */
3818 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003819 PCALL(PCALL_METHOD);
3820 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003821 Py_INCREF(self);
3822 func = PyMethod_GET_FUNCTION(func);
3823 Py_INCREF(func);
3824 Py_DECREF(*pfunc);
3825 *pfunc = self;
3826 na++;
3827 n++;
3828 } else
3829 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003830 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003831 if (PyFunction_Check(func))
3832 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003833 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003834 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003835 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003836 Py_DECREF(func);
3837 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003838
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003839 /* Clear the stack of the function object. Also removes
3840 the arguments in case they weren't consumed already
3841 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003842 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003843 while ((*pp_stack) > pfunc) {
3844 w = EXT_POP(*pp_stack);
3845 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003846 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003847 }
3848 return x;
3849}
3850
Jeremy Hylton192690e2002-08-16 18:36:11 +00003851/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003852 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003853 For the simplest case -- a function that takes only positional
3854 arguments and is called with only positional arguments -- it
3855 inlines the most primitive frame setup code from
3856 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3857 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003858*/
3859
3860static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003861fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003862{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003863 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003864 PyObject *globals = PyFunction_GET_GLOBALS(func);
3865 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003866 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003867 PyObject **d = NULL;
3868 int nd = 0;
3869
Jeremy Hylton985eba52003-02-05 23:13:00 +00003870 PCALL(PCALL_FUNCTION);
3871 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003872 if (argdefs == NULL && co->co_argcount == n &&
3873 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003874 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3875 PyFrameObject *f;
3876 PyObject *retval = NULL;
3877 PyThreadState *tstate = PyThreadState_GET();
3878 PyObject **fastlocals, **stack;
3879 int i;
3880
3881 PCALL(PCALL_FASTER_FUNCTION);
3882 assert(globals != NULL);
3883 /* XXX Perhaps we should create a specialized
3884 PyFrame_New() that doesn't take locals, but does
3885 take builtins without sanity checking them.
3886 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003887 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003888 f = PyFrame_New(tstate, co, globals, NULL);
3889 if (f == NULL)
3890 return NULL;
3891
3892 fastlocals = f->f_localsplus;
3893 stack = (*pp_stack) - n;
3894
3895 for (i = 0; i < n; i++) {
3896 Py_INCREF(*stack);
3897 fastlocals[i] = *stack++;
3898 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003899 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003900 ++tstate->recursion_depth;
3901 Py_DECREF(f);
3902 --tstate->recursion_depth;
3903 return retval;
3904 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003905 if (argdefs != NULL) {
3906 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003907 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003908 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003909 return PyEval_EvalCodeEx(co, globals,
3910 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003911 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003912 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003913}
3914
3915static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003916update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3917 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003918{
3919 PyObject *kwdict = NULL;
3920 if (orig_kwdict == NULL)
3921 kwdict = PyDict_New();
3922 else {
3923 kwdict = PyDict_Copy(orig_kwdict);
3924 Py_DECREF(orig_kwdict);
3925 }
3926 if (kwdict == NULL)
3927 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003928 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003929 int err;
3930 PyObject *value = EXT_POP(*pp_stack);
3931 PyObject *key = EXT_POP(*pp_stack);
3932 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003933 PyErr_Format(PyExc_TypeError,
3934 "%.200s%s got multiple values "
3935 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003936 PyEval_GetFuncName(func),
3937 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003938 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003939 Py_DECREF(key);
3940 Py_DECREF(value);
3941 Py_DECREF(kwdict);
3942 return NULL;
3943 }
3944 err = PyDict_SetItem(kwdict, key, value);
3945 Py_DECREF(key);
3946 Py_DECREF(value);
3947 if (err) {
3948 Py_DECREF(kwdict);
3949 return NULL;
3950 }
3951 }
3952 return kwdict;
3953}
3954
3955static PyObject *
3956update_star_args(int nstack, int nstar, PyObject *stararg,
3957 PyObject ***pp_stack)
3958{
3959 PyObject *callargs, *w;
3960
3961 callargs = PyTuple_New(nstack + nstar);
3962 if (callargs == NULL) {
3963 return NULL;
3964 }
3965 if (nstar) {
3966 int i;
3967 for (i = 0; i < nstar; i++) {
3968 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3969 Py_INCREF(a);
3970 PyTuple_SET_ITEM(callargs, nstack + i, a);
3971 }
3972 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003973 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003974 w = EXT_POP(*pp_stack);
3975 PyTuple_SET_ITEM(callargs, nstack, w);
3976 }
3977 return callargs;
3978}
3979
3980static PyObject *
3981load_args(PyObject ***pp_stack, int na)
3982{
3983 PyObject *args = PyTuple_New(na);
3984 PyObject *w;
3985
3986 if (args == NULL)
3987 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003988 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003989 w = EXT_POP(*pp_stack);
3990 PyTuple_SET_ITEM(args, na, w);
3991 }
3992 return args;
3993}
3994
3995static PyObject *
3996do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3997{
3998 PyObject *callargs = NULL;
3999 PyObject *kwdict = NULL;
4000 PyObject *result = NULL;
4001
4002 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004003 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004004 if (kwdict == NULL)
4005 goto call_fail;
4006 }
4007 callargs = load_args(pp_stack, na);
4008 if (callargs == NULL)
4009 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004010#ifdef CALL_PROFILE
4011 /* At this point, we have to look at the type of func to
4012 update the call stats properly. Do it here so as to avoid
4013 exposing the call stats machinery outside ceval.c
4014 */
4015 if (PyFunction_Check(func))
4016 PCALL(PCALL_FUNCTION);
4017 else if (PyMethod_Check(func))
4018 PCALL(PCALL_METHOD);
4019 else if (PyType_Check(func))
4020 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004021 else if (PyCFunction_Check(func))
4022 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004023 else
4024 PCALL(PCALL_OTHER);
4025#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004026 if (PyCFunction_Check(func)) {
4027 PyThreadState *tstate = PyThreadState_GET();
4028 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4029 }
4030 else
4031 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004032call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004033 Py_XDECREF(callargs);
4034 Py_XDECREF(kwdict);
4035 return result;
4036}
4037
4038static PyObject *
4039ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4040{
4041 int nstar = 0;
4042 PyObject *callargs = NULL;
4043 PyObject *stararg = NULL;
4044 PyObject *kwdict = NULL;
4045 PyObject *result = NULL;
4046
4047 if (flags & CALL_FLAG_KW) {
4048 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004049 if (!PyDict_Check(kwdict)) {
4050 PyObject *d;
4051 d = PyDict_New();
4052 if (d == NULL)
4053 goto ext_call_fail;
4054 if (PyDict_Update(d, kwdict) != 0) {
4055 Py_DECREF(d);
4056 /* PyDict_Update raises attribute
4057 * error (percolated from an attempt
4058 * to get 'keys' attribute) instead of
4059 * a type error if its second argument
4060 * is not a mapping.
4061 */
4062 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4063 PyErr_Format(PyExc_TypeError,
4064 "%.200s%.200s argument after ** "
4065 "must be a mapping, not %.200s",
4066 PyEval_GetFuncName(func),
4067 PyEval_GetFuncDesc(func),
4068 kwdict->ob_type->tp_name);
4069 }
4070 goto ext_call_fail;
4071 }
4072 Py_DECREF(kwdict);
4073 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004074 }
4075 }
4076 if (flags & CALL_FLAG_VAR) {
4077 stararg = EXT_POP(*pp_stack);
4078 if (!PyTuple_Check(stararg)) {
4079 PyObject *t = NULL;
4080 t = PySequence_Tuple(stararg);
4081 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004082 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4083 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004084 "%.200s%.200s argument after * "
4085 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004086 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004087 PyEval_GetFuncDesc(func),
4088 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004089 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004090 goto ext_call_fail;
4091 }
4092 Py_DECREF(stararg);
4093 stararg = t;
4094 }
4095 nstar = PyTuple_GET_SIZE(stararg);
4096 }
4097 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004098 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004099 if (kwdict == NULL)
4100 goto ext_call_fail;
4101 }
4102 callargs = update_star_args(na, nstar, stararg, pp_stack);
4103 if (callargs == NULL)
4104 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004105#ifdef CALL_PROFILE
4106 /* At this point, we have to look at the type of func to
4107 update the call stats properly. Do it here so as to avoid
4108 exposing the call stats machinery outside ceval.c
4109 */
4110 if (PyFunction_Check(func))
4111 PCALL(PCALL_FUNCTION);
4112 else if (PyMethod_Check(func))
4113 PCALL(PCALL_METHOD);
4114 else if (PyType_Check(func))
4115 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004116 else if (PyCFunction_Check(func))
4117 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004118 else
4119 PCALL(PCALL_OTHER);
4120#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004121 if (PyCFunction_Check(func)) {
4122 PyThreadState *tstate = PyThreadState_GET();
4123 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4124 }
4125 else
4126 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004127ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004128 Py_XDECREF(callargs);
4129 Py_XDECREF(kwdict);
4130 Py_XDECREF(stararg);
4131 return result;
4132}
4133
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004134/* Extract a slice index from a PyInt or PyLong or an object with the
4135 nb_index slot defined, and store in *pi.
4136 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4137 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 +00004138 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004139*/
Tim Petersb5196382001-12-16 19:44:20 +00004140/* Note: If v is NULL, return success without storing into *pi. This
4141 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4142 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004143*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004144int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004145_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004146{
Tim Petersb5196382001-12-16 19:44:20 +00004147 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004148 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004149 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004150 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004151 if (x == -1 && PyErr_Occurred())
4152 return 0;
4153 }
4154 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004155 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004156 "slice indices must be integers or "
4157 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004158 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004159 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004160 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004161 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004162 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004163}
4164
Guido van Rossum486364b2007-06-30 05:01:58 +00004165#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004166 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004167
Guido van Rossumb209a111997-04-29 18:18:01 +00004168static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004169cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004170{
Guido van Rossumac7be682001-01-17 15:42:30 +00004171 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004172 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004173 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004174 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004175 break;
4176 case PyCmp_IS_NOT:
4177 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004178 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004179 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004180 res = PySequence_Contains(w, v);
4181 if (res < 0)
4182 return NULL;
4183 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004184 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004185 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004186 if (res < 0)
4187 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004188 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004189 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004190 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004191 if (PyTuple_Check(w)) {
4192 Py_ssize_t i, length;
4193 length = PyTuple_Size(w);
4194 for (i = 0; i < length; i += 1) {
4195 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004196 if (!PyExceptionClass_Check(exc)) {
4197 PyErr_SetString(PyExc_TypeError,
4198 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004199 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004200 }
4201 }
4202 }
4203 else {
Brett Cannon39590462007-02-26 22:01:14 +00004204 if (!PyExceptionClass_Check(w)) {
4205 PyErr_SetString(PyExc_TypeError,
4206 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004207 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004208 }
4209 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004210 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004211 break;
4212 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004213 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004214 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004215 v = res ? Py_True : Py_False;
4216 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004217 return v;
4218}
4219
Thomas Wouters52152252000-08-17 22:55:00 +00004220static PyObject *
4221import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004222{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004223 PyObject *x;
4224
4225 x = PyObject_GetAttr(v, name);
4226 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004227 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004228 }
Thomas Wouters52152252000-08-17 22:55:00 +00004229 return x;
4230}
Guido van Rossumac7be682001-01-17 15:42:30 +00004231
Thomas Wouters52152252000-08-17 22:55:00 +00004232static int
4233import_all_from(PyObject *locals, PyObject *v)
4234{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004235 PyObject *all = PyObject_GetAttrString(v, "__all__");
4236 PyObject *dict, *name, *value;
4237 int skip_leading_underscores = 0;
4238 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004239
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004240 if (all == NULL) {
4241 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4242 return -1; /* Unexpected error */
4243 PyErr_Clear();
4244 dict = PyObject_GetAttrString(v, "__dict__");
4245 if (dict == NULL) {
4246 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4247 return -1;
4248 PyErr_SetString(PyExc_ImportError,
4249 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004250 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004251 }
4252 all = PyMapping_Keys(dict);
4253 Py_DECREF(dict);
4254 if (all == NULL)
4255 return -1;
4256 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004257 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004258
4259 for (pos = 0, err = 0; ; pos++) {
4260 name = PySequence_GetItem(all, pos);
4261 if (name == NULL) {
4262 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4263 err = -1;
4264 else
4265 PyErr_Clear();
4266 break;
4267 }
4268 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004269 PyUnicode_Check(name) &&
4270 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004271 {
4272 Py_DECREF(name);
4273 continue;
4274 }
4275 value = PyObject_GetAttr(v, name);
4276 if (value == NULL)
4277 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004278 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004279 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004280 else
4281 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004282 Py_DECREF(name);
4283 Py_XDECREF(value);
4284 if (err != 0)
4285 break;
4286 }
4287 Py_DECREF(all);
4288 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004289}
4290
Guido van Rossumac7be682001-01-17 15:42:30 +00004291static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004292format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004293{
Neal Norwitzda059e32007-08-26 05:33:45 +00004294 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004295
4296 if (!obj)
4297 return;
4298
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004299 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004300 if (!obj_str)
4301 return;
4302
4303 PyErr_Format(exc, format_str, obj_str);
4304}
Guido van Rossum950361c1997-01-24 13:49:28 +00004305
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004306static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004307unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004308 PyFrameObject *f, unsigned char *next_instr)
4309{
4310 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004311 are (Unicode) strings. */
4312 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4313 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004314 Py_ssize_t new_len = v_len + w_len;
4315 if (new_len < 0) {
4316 PyErr_SetString(PyExc_OverflowError,
4317 "strings are too large to concat");
4318 return NULL;
4319 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004320
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004321 if (v->ob_refcnt == 2) {
4322 /* In the common case, there are 2 references to the value
4323 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004324 * value stack (in 'v') and one still stored in the
4325 * 'variable'. We try to delete the variable now to reduce
4326 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004327 */
4328 switch (*next_instr) {
4329 case STORE_FAST:
4330 {
4331 int oparg = PEEKARG();
4332 PyObject **fastlocals = f->f_localsplus;
4333 if (GETLOCAL(oparg) == v)
4334 SETLOCAL(oparg, NULL);
4335 break;
4336 }
4337 case STORE_DEREF:
4338 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004339 PyObject **freevars = (f->f_localsplus +
4340 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004341 PyObject *c = freevars[PEEKARG()];
4342 if (PyCell_GET(c) == v)
4343 PyCell_Set(c, NULL);
4344 break;
4345 }
4346 case STORE_NAME:
4347 {
4348 PyObject *names = f->f_code->co_names;
4349 PyObject *name = GETITEM(names, PEEKARG());
4350 PyObject *locals = f->f_locals;
4351 if (PyDict_CheckExact(locals) &&
4352 PyDict_GetItem(locals, name) == v) {
4353 if (PyDict_DelItem(locals, name) != 0) {
4354 PyErr_Clear();
4355 }
4356 }
4357 break;
4358 }
4359 }
4360 }
4361
Guido van Rossum98297ee2007-11-06 21:34:58 +00004362 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004363 /* Now we own the last reference to 'v', so we can resize it
4364 * in-place.
4365 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004366 if (PyUnicode_Resize(&v, new_len) != 0) {
4367 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004368 * deallocated so it cannot be put back into
4369 * 'variable'. The MemoryError is raised when there
4370 * is no value in 'variable', which might (very
4371 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004372 */
4373 return NULL;
4374 }
4375 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004376 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4377 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004378 return v;
4379 }
4380 else {
4381 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004382 w = PyUnicode_Concat(v, w);
4383 Py_DECREF(v);
4384 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004385 }
4386}
4387
Guido van Rossum950361c1997-01-24 13:49:28 +00004388#ifdef DYNAMIC_EXECUTION_PROFILE
4389
Skip Montanarof118cb12001-10-15 20:51:38 +00004390static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004391getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004392{
4393 int i;
4394 PyObject *l = PyList_New(256);
4395 if (l == NULL) return NULL;
4396 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004397 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004398 if (x == NULL) {
4399 Py_DECREF(l);
4400 return NULL;
4401 }
4402 PyList_SetItem(l, i, x);
4403 }
4404 for (i = 0; i < 256; i++)
4405 a[i] = 0;
4406 return l;
4407}
4408
4409PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004410_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004411{
4412#ifndef DXPAIRS
4413 return getarray(dxp);
4414#else
4415 int i;
4416 PyObject *l = PyList_New(257);
4417 if (l == NULL) return NULL;
4418 for (i = 0; i < 257; i++) {
4419 PyObject *x = getarray(dxpairs[i]);
4420 if (x == NULL) {
4421 Py_DECREF(l);
4422 return NULL;
4423 }
4424 PyList_SetItem(l, i, x);
4425 }
4426 return l;
4427#endif
4428}
4429
4430#endif