blob: b689f3de28bb4e9f4f7720dbff7bc0fd69e9b593 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouters8ce81f72007-09-20 18:22:40 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Michael W. Hudson75eabd22005-01-18 15:56:11 +000054#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000055
Michael W. Hudson75eabd22005-01-18 15:56:11 +000056#define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
59#endif
60
Thomas Wouters477c8d52006-05-27 19:21:47 +000061void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63{
64 uint64 intr, inst, loop;
65 PyThreadState *tstate = PyThreadState_Get();
66 if (!tstate->interp->tscdump)
67 return;
68 intr = intr1 - intr0;
69 inst = inst1 - inst0 - intr;
70 loop = loop1 - loop0 - intr;
71 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode, ticked, inst, loop);
73}
Michael W. Hudson800ba232004-08-12 18:19:17 +000074
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000075#endif
76
Guido van Rossum04691fc1992-08-12 15:35:34 +000077/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000079
Guido van Rossum408027e1996-12-30 16:17:54 +000080#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000081/* For debugging the interpreter: */
82#define LLTRACE 1 /* Low-level trace feature */
83#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084#endif
85
Jeremy Hylton52820442001-01-03 23:52:36 +000086typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000087
Guido van Rossum374a9221991-04-04 10:40:29 +000088/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Thomas Wouters477c8d52006-05-27 19:21:47 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000094static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
95static PyObject * do_call(PyObject *, PyObject ***, int, int);
96static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000099static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
100static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000101#define CALL_FLAG_VAR 1
102#define CALL_FLAG_KW 2
103
Guido van Rossum0a066c01992-03-27 17:29:15 +0000104#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000105static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
109 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000113static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000114 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000115
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * cmp_outcome(int, PyObject *, PyObject *);
117static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000118static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000119static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000120static PyObject * unicode_concatenate(PyObject *, PyObject *,
121 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000122static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000123
Paul Prescode68140d2000-08-30 20:25:01 +0000124#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000125 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000126#define GLOBAL_NAME_ERROR_MSG \
127 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000128#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000129 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000130#define UNBOUNDFREE_ERROR_MSG \
131 "free variable '%.200s' referenced before assignment" \
132 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000133
Guido van Rossum950361c1997-01-24 13:49:28 +0000134/* Dynamic execution profile */
135#ifdef DYNAMIC_EXECUTION_PROFILE
136#ifdef DXPAIRS
137static long dxpairs[257][256];
138#define dxp dxpairs[256]
139#else
140static long dxp[256];
141#endif
142#endif
143
Jeremy Hylton985eba52003-02-05 23:13:00 +0000144/* Function call profile */
145#ifdef CALL_PROFILE
146#define PCALL_NUM 11
147static int pcall[PCALL_NUM];
148
149#define PCALL_ALL 0
150#define PCALL_FUNCTION 1
151#define PCALL_FAST_FUNCTION 2
152#define PCALL_FASTER_FUNCTION 3
153#define PCALL_METHOD 4
154#define PCALL_BOUND_METHOD 5
155#define PCALL_CFUNCTION 6
156#define PCALL_TYPE 7
157#define PCALL_GENERATOR 8
158#define PCALL_OTHER 9
159#define PCALL_POP 10
160
161/* Notes about the statistics
162
163 PCALL_FAST stats
164
165 FAST_FUNCTION means no argument tuple needs to be created.
166 FASTER_FUNCTION means that the fast-path frame setup code is used.
167
168 If there is a method call where the call can be optimized by changing
169 the argument tuple and calling the function directly, it gets recorded
170 twice.
171
172 As a result, the relationship among the statistics appears to be
173 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
174 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
175 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
176 PCALL_METHOD > PCALL_BOUND_METHOD
177*/
178
179#define PCALL(POS) pcall[POS]++
180
181PyObject *
182PyEval_GetCallStats(PyObject *self)
183{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000184 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000185 pcall[0], pcall[1], pcall[2], pcall[3],
186 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000187 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000188}
189#else
190#define PCALL(O)
191
192PyObject *
193PyEval_GetCallStats(PyObject *self)
194{
195 Py_INCREF(Py_None);
196 return Py_None;
197}
198#endif
199
Tim Peters5ca576e2001-06-18 22:08:13 +0000200
Guido van Rossume59214e1994-08-30 08:01:59 +0000201#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000202
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000203#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000204#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000205#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000206#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000207
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000208static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000209static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000210static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000211
Tim Peters7f468f22004-10-11 02:40:51 +0000212int
213PyEval_ThreadsInitialized(void)
214{
215 return interpreter_lock != 0;
216}
217
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000218void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000221 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000222 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000223 interpreter_lock = PyThread_allocate_lock();
224 PyThread_acquire_lock(interpreter_lock, 1);
225 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000226}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000227
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000228void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000229PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000231 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232}
233
234void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000235PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000236{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000237 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238}
239
240void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000241PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000242{
243 if (tstate == NULL)
244 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000245 /* Check someone has called PyEval_InitThreads() to create the lock */
246 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000247 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000248 if (PyThreadState_Swap(tstate) != NULL)
249 Py_FatalError(
250 "PyEval_AcquireThread: non-NULL old thread state");
251}
252
253void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000254PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000255{
256 if (tstate == NULL)
257 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
258 if (PyThreadState_Swap(NULL) != tstate)
259 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000260 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000261}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000262
263/* This function is called from PyOS_AfterFork to ensure that newly
264 created child processes don't hold locks referring to threads which
265 are not running in the child process. (This could also be done using
266 pthread_atfork mechanism, at least for the pthreads implementation.) */
267
268void
269PyEval_ReInitThreads(void)
270{
Jesse Nollera8513972008-07-17 16:49:17 +0000271 PyObject *threading, *result;
272 PyThreadState *tstate;
273
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000274 if (!interpreter_lock)
275 return;
276 /*XXX Can't use PyThread_free_lock here because it does too
277 much error-checking. Doing this cleanly would require
278 adding a new function to each thread_*.h. Instead, just
279 create a new lock and waste a little bit of memory */
280 interpreter_lock = PyThread_allocate_lock();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000281 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000282 PyThread_acquire_lock(interpreter_lock, 1);
283 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000284
285 /* Update the threading module with the new state.
286 */
287 tstate = PyThreadState_GET();
288 threading = PyMapping_GetItemString(tstate->interp->modules,
289 "threading");
290 if (threading == NULL) {
291 /* threading not imported */
292 PyErr_Clear();
293 return;
294 }
295 result = PyObject_CallMethod(threading, "_after_fork", NULL);
296 if (result == NULL)
297 PyErr_WriteUnraisable(threading);
298 else
299 Py_DECREF(result);
300 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000301}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302#endif
303
Guido van Rossumff4949e1992-08-05 19:58:53 +0000304/* Functions save_thread and restore_thread are always defined so
305 dynamically loaded modules needn't be compiled separately for use
306 with and without threads: */
307
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000308PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000309PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000311 PyThreadState *tstate = PyThreadState_Swap(NULL);
312 if (tstate == NULL)
313 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000314#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000315 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000316 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000317#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000318 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000319}
320
321void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000322PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000323{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000324 if (tstate == NULL)
325 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000326#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000327 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000328 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000329 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000331 }
332#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000333 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000334}
335
336
Guido van Rossuma9672091994-09-14 13:31:22 +0000337/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
338 signal handlers or Mac I/O completion routines) can schedule calls
339 to a function to be called synchronously.
340 The synchronous function is called with one void* argument.
341 It should return 0 for success or -1 for failure -- failure should
342 be accompanied by an exception.
343
344 If registry succeeds, the registry function returns 0; if it fails
345 (e.g. due to too many pending calls) it returns -1 (without setting
346 an exception condition).
347
348 Note that because registry may occur from within signal handlers,
349 or other asynchronous events, calling malloc() is unsafe!
350
351#ifdef WITH_THREAD
352 Any thread can schedule pending calls, but only the main thread
353 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000354 There is no facility to schedule calls to a particular thread, but
355 that should be easy to change, should that ever be required. In
356 that case, the static variables here should go into the python
357 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000358#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000359*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000360
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000361#ifdef WITH_THREAD
362
363/* The WITH_THREAD implementation is thread-safe. It allows
364 scheduling to be made from any thread, and even from an executing
365 callback.
366 */
367
368#define NPENDINGCALLS 32
369static struct {
370 int (*func)(void *);
371 void *arg;
372} pendingcalls[NPENDINGCALLS];
373static int pendingfirst = 0;
374static int pendinglast = 0;
375static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
376static char pendingbusy = 0;
377
378int
379Py_AddPendingCall(int (*func)(void *), void *arg)
380{
381 int i, j, result=0;
382 PyThread_type_lock lock = pending_lock;
383
384 /* try a few times for the lock. Since this mechanism is used
385 * for signal handling (on the main thread), there is a (slim)
386 * chance that a signal is delivered on the same thread while we
387 * hold the lock during the Py_MakePendingCalls() function.
388 * This avoids a deadlock in that case.
389 * Note that signals can be delivered on any thread. In particular,
390 * on Windows, a SIGINT is delivered on a system-created worker
391 * thread.
392 * We also check for lock being NULL, in the unlikely case that
393 * this function is called before any bytecode evaluation takes place.
394 */
395 if (lock != NULL) {
396 for (i = 0; i<100; i++) {
397 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
398 break;
399 }
400 if (i == 100)
401 return -1;
402 }
403
404 i = pendinglast;
405 j = (i + 1) % NPENDINGCALLS;
406 if (j == pendingfirst) {
407 result = -1; /* Queue full */
408 } else {
409 pendingcalls[i].func = func;
410 pendingcalls[i].arg = arg;
411 pendinglast = j;
412 }
413 /* signal main loop */
414 _Py_Ticker = 0;
415 pendingcalls_to_do = 1;
416 if (lock != NULL)
417 PyThread_release_lock(lock);
418 return result;
419}
420
421int
422Py_MakePendingCalls(void)
423{
424 int i;
425 int r = 0;
426
427 if (!pending_lock) {
428 /* initial allocation of the lock */
429 pending_lock = PyThread_allocate_lock();
430 if (pending_lock == NULL)
431 return -1;
432 }
433
434 /* only service pending calls on main thread */
435 if (main_thread && PyThread_get_thread_ident() != main_thread)
436 return 0;
437 /* don't perform recursive pending calls */
438 if (pendingbusy)
439 return 0;
440 pendingbusy = 1;
441 /* perform a bounded number of calls, in case of recursion */
442 for (i=0; i<NPENDINGCALLS; i++) {
443 int j;
444 int (*func)(void *);
Benjamin Petersonb4ddfa42009-01-17 23:46:54 +0000445 void *arg = NULL;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000446
447 /* pop one item off the queue while holding the lock */
448 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
449 j = pendingfirst;
450 if (j == pendinglast) {
451 func = NULL; /* Queue empty */
452 } else {
453 func = pendingcalls[j].func;
454 arg = pendingcalls[j].arg;
455 pendingfirst = (j + 1) % NPENDINGCALLS;
456 }
457 pendingcalls_to_do = pendingfirst != pendinglast;
458 PyThread_release_lock(pending_lock);
459 /* having released the lock, perform the callback */
460 if (func == NULL)
461 break;
462 r = func(arg);
463 if (r)
464 break;
465 }
466 pendingbusy = 0;
467 return r;
468}
469
470#else /* if ! defined WITH_THREAD */
471
472/*
473 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
474 This code is used for signal handling in python that isn't built
475 with WITH_THREAD.
476 Don't use this implementation when Py_AddPendingCalls() can happen
477 on a different thread!
478
Guido van Rossuma9672091994-09-14 13:31:22 +0000479 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480 (1) nested asynchronous calls to Py_AddPendingCall()
481 (2) AddPendingCall() calls made while pending calls are being processed.
482
483 (1) is very unlikely because typically signal delivery
484 is blocked during signal handling. So it should be impossible.
485 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000486 The current code is safe against (2), but not against (1).
487 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000488 thread is present, interrupted by signals, and that the critical
489 section is protected with the "busy" variable. On Windows, which
490 delivers SIGINT on a system thread, this does not hold and therefore
491 Windows really shouldn't use this version.
492 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000493*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000494
Guido van Rossuma9672091994-09-14 13:31:22 +0000495#define NPENDINGCALLS 32
496static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000497 int (*func)(void *);
498 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000499} pendingcalls[NPENDINGCALLS];
500static volatile int pendingfirst = 0;
501static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000502static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000503
504int
Thomas Wouters334fb892000-07-25 12:56:38 +0000505Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000506{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000507 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000508 int i, j;
509 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000510 if (busy)
511 return -1;
512 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000513 i = pendinglast;
514 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000515 if (j == pendingfirst) {
516 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000517 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000518 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000519 pendingcalls[i].func = func;
520 pendingcalls[i].arg = arg;
521 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000522
523 _Py_Ticker = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000524 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000525 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000526 /* XXX End critical section */
527 return 0;
528}
529
Guido van Rossum180d7b41994-09-29 09:45:57 +0000530int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000531Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000532{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000533 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000534 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000535 return 0;
536 busy = 1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000537 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000538 for (;;) {
539 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000540 int (*func)(void *);
541 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000542 i = pendingfirst;
543 if (i == pendinglast)
544 break; /* Queue empty */
545 func = pendingcalls[i].func;
546 arg = pendingcalls[i].arg;
547 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000548 if (func(arg) < 0) {
549 busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000550 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000551 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000552 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000553 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000554 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000555 return 0;
556}
557
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000558#endif /* WITH_THREAD */
559
Guido van Rossuma9672091994-09-14 13:31:22 +0000560
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000561/* The interpreter's recursion limit */
562
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000563#ifndef Py_DEFAULT_RECURSION_LIMIT
564#define Py_DEFAULT_RECURSION_LIMIT 1000
565#endif
566static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
567int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000568
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000569int
570Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000571{
572 return recursion_limit;
573}
574
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000575void
576Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000577{
578 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000579 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000580}
581
Armin Rigo2b3eb402003-10-28 12:05:48 +0000582/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
583 if the recursion_depth reaches _Py_CheckRecursionLimit.
584 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
585 to guarantee that _Py_CheckRecursiveCall() is regularly called.
586 Without USE_STACKCHECK, there is no need for this. */
587int
588_Py_CheckRecursiveCall(char *where)
589{
590 PyThreadState *tstate = PyThreadState_GET();
591
592#ifdef USE_STACKCHECK
593 if (PyOS_CheckStack()) {
594 --tstate->recursion_depth;
595 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
596 return -1;
597 }
598#endif
Antoine Pitrou658fad82008-09-03 18:34:34 +0000599 _Py_CheckRecursionLimit = recursion_limit;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000600 if (tstate->recursion_critical)
601 /* Somebody asked that we don't check for recursion. */
602 return 0;
603 if (tstate->overflowed) {
604 if (tstate->recursion_depth > recursion_limit + 50) {
605 /* Overflowing while handling an overflow. Give up. */
606 Py_FatalError("Cannot recover from stack overflow.");
607 }
608 return 0;
609 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000610 if (tstate->recursion_depth > recursion_limit) {
611 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000612 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000613 PyErr_Format(PyExc_RuntimeError,
614 "maximum recursion depth exceeded%s",
615 where);
616 return -1;
617 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000618 return 0;
619}
620
Guido van Rossum374a9221991-04-04 10:40:29 +0000621/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000622enum why_code {
623 WHY_NOT = 0x0001, /* No error */
624 WHY_EXCEPTION = 0x0002, /* Exception occurred */
625 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
626 WHY_RETURN = 0x0008, /* 'return' statement */
627 WHY_BREAK = 0x0010, /* 'break' statement */
628 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000629 WHY_YIELD = 0x0040, /* 'yield' operator */
630 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000631};
Guido van Rossum374a9221991-04-04 10:40:29 +0000632
Collin Winter828f04a2007-08-31 00:04:24 +0000633static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000634static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000635
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000636/* Records whether tracing is on for any thread. Counts the number of
637 threads for which tstate->c_tracefunc is non-NULL, so if the value
638 is 0, we know we don't have to check this thread's c_tracefunc.
639 This speeds up the if statement in PyEval_EvalFrameEx() after
640 fast_next_opcode*/
641static int _Py_TracingPossible = 0;
642
Skip Montanarod581d772002-09-03 20:10:45 +0000643/* for manipulating the thread switch and periodic "stuff" - used to be
644 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000645int _Py_CheckInterval = 100;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000646volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000647
Guido van Rossumb209a111997-04-29 18:18:01 +0000648PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000649PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000650{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000651 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000652 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000653 (PyObject **)NULL, 0,
654 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000655 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000656 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000657}
658
659
660/* Interpreter main loop */
661
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000662PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000663PyEval_EvalFrame(PyFrameObject *f) {
664 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000665 used this API; core interpreter code should call
666 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000667 return PyEval_EvalFrameEx(f, 0);
668}
669
670PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000672{
Guido van Rossum950361c1997-01-24 13:49:28 +0000673#ifdef DXPAIRS
674 int lastopcode = 0;
675#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000676 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000677 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000678 register int opcode; /* Current opcode */
679 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000680 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000681 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000682 register PyObject *x; /* Result object -- NULL if error */
683 register PyObject *v; /* Temporary objects popped off stack */
684 register PyObject *w;
685 register PyObject *u;
686 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000687 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000688 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000689 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000690 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000691
Tim Peters8a5c3c72004-04-05 19:36:21 +0000692 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000693
694 not (instr_lb <= current_bytecode_offset < instr_ub)
695
Tim Peters8a5c3c72004-04-05 19:36:21 +0000696 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000697 initial values are such as to make this false the first
698 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000699 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000700
Guido van Rossumd076c731998-10-07 19:42:25 +0000701 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000702 PyObject *names;
703 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000704#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000705 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000706 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000707#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000708
Antoine Pitroub52ec782009-01-25 16:34:23 +0000709/* Computed GOTOs, or
710 the-optimization-commonly-but-improperly-known-as-"threaded code"
711 using gcc's labels-as-values extension
712 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
713
714 The traditional bytecode evaluation loop uses a "switch" statement, which
715 decent compilers will optimize as a single indirect branch instruction
716 combined with a lookup table of jump addresses. However, since the
717 indirect jump instruction is shared by all opcodes, the CPU will have a
718 hard time making the right prediction for where to jump next (actually,
719 it will be always wrong except in the uncommon case of a sequence of
720 several identical opcodes).
721
722 "Threaded code" in contrast, uses an explicit jump table and an explicit
723 indirect jump instruction at the end of each opcode. Since the jump
724 instruction is at a different address for each opcode, the CPU will make a
725 separate prediction for each of these instructions, which is equivalent to
726 predicting the second opcode of each opcode pair. These predictions have
727 a much better chance to turn out valid, especially in small bytecode loops.
728
729 A mispredicted branch on a modern CPU flushes the whole pipeline and
730 can cost several CPU cycles (depending on the pipeline depth),
731 and potentially many more instructions (depending on the pipeline width).
732 A correctly predicted branch, however, is nearly free.
733
734 At the time of this writing, the "threaded code" version is up to 15-20%
735 faster than the normal "switch" version, depending on the compiler and the
736 CPU architecture.
737
738 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
739 because it would render the measurements invalid.
740
741
742 NOTE: care must be taken that the compiler doesn't try to "optimize" the
743 indirect jumps by sharing them between all opcodes. Such optimizations
744 can be disabled on gcc by using the -fno-gcse flag (or possibly
745 -fno-crossjumping).
746*/
747
748#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
749#undef USE_COMPUTED_GOTOS
750#endif
751
752#ifdef USE_COMPUTED_GOTOS
753/* Import the static jump table */
754#include "opcode_targets.h"
755
756/* This macro is used when several opcodes defer to the same implementation
757 (e.g. SETUP_LOOP, SETUP_FINALLY) */
758#define TARGET_WITH_IMPL(op, impl) \
759 TARGET_##op: \
760 opcode = op; \
761 if (HAS_ARG(op)) \
762 oparg = NEXTARG(); \
763 case op: \
764 goto impl; \
765
766#define TARGET(op) \
767 TARGET_##op: \
768 opcode = op; \
769 if (HAS_ARG(op)) \
770 oparg = NEXTARG(); \
771 case op:
772
773
774#define DISPATCH() \
775 { \
776 /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
777 int _tick = _Py_Ticker - 1; \
778 _Py_Ticker = _tick; \
779 if (_tick >= 0) { \
780 FAST_DISPATCH(); \
781 } \
782 continue; \
783 }
784
785#ifdef LLTRACE
786#define FAST_DISPATCH() \
787 { \
788 if (!lltrace && !_Py_TracingPossible) { \
789 f->f_lasti = INSTR_OFFSET(); \
790 goto *opcode_targets[*next_instr++]; \
791 } \
792 goto fast_next_opcode; \
793 }
794#else
795#define FAST_DISPATCH() \
796 { \
797 if (!_Py_TracingPossible) { \
798 f->f_lasti = INSTR_OFFSET(); \
799 goto *opcode_targets[*next_instr++]; \
800 } \
801 goto fast_next_opcode; \
802 }
803#endif
804
805#else
806#define TARGET(op) \
807 case op:
808#define TARGET_WITH_IMPL(op, impl) \
809 /* silence compiler warnings about `impl` unused */ \
810 if (0) goto impl; \
811 case op:
812#define DISPATCH() continue
813#define FAST_DISPATCH() goto fast_next_opcode
814#endif
815
816
Neal Norwitza81d2202002-07-14 00:27:26 +0000817/* Tuple access macros */
818
819#ifndef Py_DEBUG
820#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
821#else
822#define GETITEM(v, i) PyTuple_GetItem((v), (i))
823#endif
824
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000825#ifdef WITH_TSC
826/* Use Pentium timestamp counter to mark certain events:
827 inst0 -- beginning of switch statement for opcode dispatch
828 inst1 -- end of switch statement (may be skipped)
829 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000831 (may be skipped)
832 intr1 -- beginning of long interruption
833 intr2 -- end of long interruption
834
835 Many opcodes call out to helper C functions. In some cases, the
836 time in those functions should be counted towards the time for the
837 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
838 calls another Python function; there's no point in charge all the
839 bytecode executed by the called function to the caller.
840
841 It's hard to make a useful judgement statically. In the presence
842 of operator overloading, it's impossible to tell if a call will
843 execute new Python code or not.
844
845 It's a case-by-case judgement. I'll use intr1 for the following
846 cases:
847
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000848 IMPORT_STAR
849 IMPORT_FROM
850 CALL_FUNCTION (and friends)
851
852 */
853 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
854 int ticked = 0;
855
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000856 READ_TIMESTAMP(inst0);
857 READ_TIMESTAMP(inst1);
858 READ_TIMESTAMP(loop0);
859 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000860
861 /* shut up the compiler */
862 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000863#endif
864
Guido van Rossum374a9221991-04-04 10:40:29 +0000865/* Code access macros */
866
Martin v. Löwis18e16552006-02-15 17:27:45 +0000867#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000868#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000869#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000870#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000871#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000872#define JUMPBY(x) (next_instr += (x))
873
Raymond Hettingerf606f872003-03-16 03:11:04 +0000874/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000875 Some opcodes tend to come in pairs thus making it possible to
876 predict the second code when the first is run. For example,
877 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
878 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000879
Georg Brandl86b2fb92008-07-16 03:43:04 +0000880 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000881 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000882 processor's own internal branch predication has a high likelihood of
883 success, resulting in a nearly zero-overhead transition to the
884 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000885 including its two unpredictable branches, the HAS_ARG test and the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000886 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000887 a successful PREDICT has the effect of making the two opcodes run as if
Georg Brandl86b2fb92008-07-16 03:43:04 +0000888 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000889
Georg Brandl86b2fb92008-07-16 03:43:04 +0000890 If collecting opcode statistics, your choices are to either keep the
891 predictions turned-on and interpret the results as if some opcodes
892 had been combined or turn-off predictions so that the opcode frequency
893 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894
895 Opcode prediction is disabled with threaded code, since the latter allows
896 the CPU to record separate branch prediction information for each
897 opcode.
898
Raymond Hettingerf606f872003-03-16 03:11:04 +0000899*/
900
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Raymond Hettingera7216982004-02-08 19:59:27 +0000902#define PREDICT(op) if (0) goto PRED_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000903#define PREDICTED(op) PRED_##op:
904#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000905#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000906#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000907#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000908#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +0000909#endif
910
Raymond Hettingerf606f872003-03-16 03:11:04 +0000911
Guido van Rossum374a9221991-04-04 10:40:29 +0000912/* Stack manipulation macros */
913
Martin v. Löwis18e16552006-02-15 17:27:45 +0000914/* The stack can grow at most MAXINT deep, as co_nlocals and
915 co_stacksize are ints. */
916#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000917#define EMPTY() (STACK_LEVEL() == 0)
918#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000919#define SECOND() (stack_pointer[-2])
920#define THIRD() (stack_pointer[-3])
921#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000922#define SET_TOP(v) (stack_pointer[-1] = (v))
923#define SET_SECOND(v) (stack_pointer[-2] = (v))
924#define SET_THIRD(v) (stack_pointer[-3] = (v))
925#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000926#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000927#define BASIC_PUSH(v) (*stack_pointer++ = (v))
928#define BASIC_POP() (*--stack_pointer)
929
Guido van Rossum96a42c81992-01-12 02:29:51 +0000930#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000931#define PUSH(v) { (void)(BASIC_PUSH(v), \
932 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000933 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000934#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
935 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000936#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
937 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000938 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000939#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
940 prtrace((STACK_POINTER)[-1], "ext_pop")), \
941 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000942#else
943#define PUSH(v) BASIC_PUSH(v)
944#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000945#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000946#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000947#endif
948
Guido van Rossum681d79a1995-07-18 14:51:37 +0000949/* Local variable macros */
950
951#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000952
953/* The SETLOCAL() macro must not DECREF the local variable in-place and
954 then store the new value; it must copy the old value to a temporary
955 value, then store the new value, and then DECREF the temporary value.
956 This is because it is possible that during the DECREF the frame is
957 accessed by other code (e.g. a __del__ method or gc.collect()) and the
958 variable would be pointing to already-freed memory. */
959#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
960 GETLOCAL(i) = value; \
961 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000962
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000963
964#define UNWIND_BLOCK(b) \
965 while (STACK_LEVEL() > (b)->b_level) { \
966 PyObject *v = POP(); \
967 Py_XDECREF(v); \
968 }
969
970#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000971 { \
972 PyObject *type, *value, *traceback; \
973 assert(STACK_LEVEL() >= (b)->b_level + 3); \
974 while (STACK_LEVEL() > (b)->b_level + 3) { \
975 value = POP(); \
976 Py_XDECREF(value); \
977 } \
978 type = tstate->exc_type; \
979 value = tstate->exc_value; \
980 traceback = tstate->exc_traceback; \
981 tstate->exc_type = POP(); \
982 tstate->exc_value = POP(); \
983 tstate->exc_traceback = POP(); \
984 Py_XDECREF(type); \
985 Py_XDECREF(value); \
986 Py_XDECREF(traceback); \
987 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000988
989#define SAVE_EXC_STATE() \
990 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000991 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000992 Py_XINCREF(tstate->exc_type); \
993 Py_XINCREF(tstate->exc_value); \
994 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000995 type = f->f_exc_type; \
996 value = f->f_exc_value; \
997 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000998 f->f_exc_type = tstate->exc_type; \
999 f->f_exc_value = tstate->exc_value; \
1000 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001001 Py_XDECREF(type); \
1002 Py_XDECREF(value); \
1003 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001004 }
1005
1006#define SWAP_EXC_STATE() \
1007 { \
1008 PyObject *tmp; \
1009 tmp = tstate->exc_type; \
1010 tstate->exc_type = f->f_exc_type; \
1011 f->f_exc_type = tmp; \
1012 tmp = tstate->exc_value; \
1013 tstate->exc_value = f->f_exc_value; \
1014 f->f_exc_value = tmp; \
1015 tmp = tstate->exc_traceback; \
1016 tstate->exc_traceback = f->f_exc_traceback; \
1017 f->f_exc_traceback = tmp; \
1018 }
1019
Guido van Rossuma027efa1997-05-05 20:56:21 +00001020/* Start of code */
1021
Tim Peters5ca576e2001-06-18 22:08:13 +00001022 if (f == NULL)
1023 return NULL;
1024
Armin Rigo1d313ab2003-10-25 14:33:09 +00001025 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001026 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +00001027 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001028
Tim Peters5ca576e2001-06-18 22:08:13 +00001029 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001030
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001031 if (tstate->use_tracing) {
1032 if (tstate->c_tracefunc != NULL) {
1033 /* tstate->c_tracefunc, if defined, is a
1034 function that will be called on *every* entry
1035 to a code block. Its return value, if not
1036 None, is a function that will be called at
1037 the start of each executed line of code.
1038 (Actually, the function must return itself
1039 in order to continue tracing.) The trace
1040 functions are called with three arguments:
1041 a pointer to the current frame, a string
1042 indicating why the function is called, and
1043 an argument which depends on the situation.
1044 The global trace function is also called
1045 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001046 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001047 tstate->c_traceobj,
1048 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001049 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001050 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001051 }
1052 }
1053 if (tstate->c_profilefunc != NULL) {
1054 /* Similar for c_profilefunc, except it needn't
1055 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001056 if (call_trace_protected(tstate->c_profilefunc,
1057 tstate->c_profileobj,
1058 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001059 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001060 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001061 }
1062 }
1063 }
1064
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001065 co = f->f_code;
1066 names = co->co_names;
1067 consts = co->co_consts;
1068 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001069 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +00001070 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001071 /* An explanation is in order for the next line.
1072
1073 f->f_lasti now refers to the index of the last instruction
1074 executed. You might think this was obvious from the name, but
1075 this wasn't always true before 2.3! PyFrame_New now sets
1076 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1077 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001078 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001079
1080 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001081 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001082 prediction effectively links the two codes together as if they
1083 were a single new opcode; accordingly,f->f_lasti will point to
1084 the first code in the pair (for instance, GET_ITER followed by
1085 FOR_ITER is effectively a single opcode and f->f_lasti will point
1086 at to the beginning of the combined pair.)
1087 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001088 next_instr = first_instr + f->f_lasti + 1;
1089 stack_pointer = f->f_stacktop;
1090 assert(stack_pointer != NULL);
1091 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1092
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001093 if (f->f_code->co_flags & CO_GENERATOR) {
1094 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1095 /* We were in an except handler when we left,
1096 restore the exception state which was put aside
1097 (see YIELD_VALUE). */
1098 SWAP_EXC_STATE();
1099 }
1100 else {
1101 SAVE_EXC_STATE();
1102 }
1103 }
1104
Tim Peters5ca576e2001-06-18 22:08:13 +00001105#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001107#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001108#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001109 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001110#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001111
Guido van Rossum374a9221991-04-04 10:40:29 +00001112 why = WHY_NOT;
1113 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00001114 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +00001115 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001116
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001117 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001118 why = WHY_EXCEPTION;
1119 goto on_error;
1120 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001121
Guido van Rossum374a9221991-04-04 10:40:29 +00001122 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001123#ifdef WITH_TSC
1124 if (inst1 == 0) {
1125 /* Almost surely, the opcode executed a break
1126 or a continue, preventing inst1 from being set
1127 on the way out of the loop.
1128 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001129 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001130 loop1 = inst1;
1131 }
1132 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1133 intr0, intr1);
1134 ticked = 0;
1135 inst1 = 0;
1136 intr0 = 0;
1137 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001138 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001139#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001140 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001141 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001142
Guido van Rossuma027efa1997-05-05 20:56:21 +00001143 /* Do periodic things. Doing this every time through
1144 the loop would add too much overhead, so we do it
1145 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001146 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001147 event needs attention (e.g. a signal handler or
1148 async I/O handler); see Py_AddPendingCall() and
1149 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001150
Skip Montanarod581d772002-09-03 20:10:45 +00001151 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001152 if (*next_instr == SETUP_FINALLY) {
1153 /* Make the last opcode before
1154 a try: finally: block uninterruptable. */
1155 goto fast_next_opcode;
1156 }
Skip Montanarod581d772002-09-03 20:10:45 +00001157 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001158 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001159#ifdef WITH_TSC
1160 ticked = 1;
1161#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001162 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001163 if (Py_MakePendingCalls() < 0) {
1164 why = WHY_EXCEPTION;
1165 goto on_error;
1166 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001167 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +00001168 /* MakePendingCalls() didn't succeed.
1169 Force early re-execution of this
1170 "periodic" code, possibly after
1171 a thread switch */
1172 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +00001173 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001174#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001175 if (interpreter_lock) {
1176 /* Give another thread a chance */
1177
Guido van Rossum25ce5661997-08-02 03:10:38 +00001178 if (PyThreadState_Swap(NULL) != tstate)
1179 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +00001180 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001181
1182 /* Other threads may run now */
1183
Guido van Rossum65d5b571998-12-21 19:32:43 +00001184 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001185 if (PyThreadState_Swap(tstate) != NULL)
1186 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001187
1188 /* Check for thread interrupts */
1189
1190 if (tstate->async_exc != NULL) {
1191 x = tstate->async_exc;
1192 tstate->async_exc = NULL;
1193 PyErr_SetNone(x);
1194 Py_DECREF(x);
1195 why = WHY_EXCEPTION;
1196 goto on_error;
1197 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001198 }
1199#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001200 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001201
Neil Schemenauer63543862002-02-17 19:10:14 +00001202 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001203 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001204
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001205 /* line-by-line tracing support */
1206
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001207 if (_Py_TracingPossible &&
1208 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001209 /* see maybe_call_line_trace
1210 for expository comments */
1211 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001212
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001213 err = maybe_call_line_trace(tstate->c_tracefunc,
1214 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001215 f, &instr_lb, &instr_ub,
1216 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001217 /* Reload possibly changed frame fields */
1218 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001219 if (f->f_stacktop != NULL) {
1220 stack_pointer = f->f_stacktop;
1221 f->f_stacktop = NULL;
1222 }
1223 if (err) {
1224 /* trace function raised an exception */
1225 goto on_error;
1226 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001227 }
1228
1229 /* Extract opcode and argument */
1230
Guido van Rossum374a9221991-04-04 10:40:29 +00001231 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001232 oparg = 0; /* allows oparg to be stored in a register because
1233 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001234 if (HAS_ARG(opcode))
1235 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001236 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001237#ifdef DYNAMIC_EXECUTION_PROFILE
1238#ifdef DXPAIRS
1239 dxpairs[lastopcode][opcode]++;
1240 lastopcode = opcode;
1241#endif
1242 dxp[opcode]++;
1243#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001244
Guido van Rossum96a42c81992-01-12 02:29:51 +00001245#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001246 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Guido van Rossum96a42c81992-01-12 02:29:51 +00001248 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001249 if (HAS_ARG(opcode)) {
1250 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001251 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001252 }
1253 else {
1254 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001255 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001256 }
1257 }
1258#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001259
Guido van Rossum374a9221991-04-04 10:40:29 +00001260 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001261 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001262
Guido van Rossum374a9221991-04-04 10:40:29 +00001263 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Guido van Rossum374a9221991-04-04 10:40:29 +00001265 /* BEWARE!
1266 It is essential that any operation that fails sets either
1267 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1268 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Guido van Rossum374a9221991-04-04 10:40:29 +00001270 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001271
Antoine Pitroub52ec782009-01-25 16:34:23 +00001272 TARGET(NOP)
1273 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001274
Antoine Pitroub52ec782009-01-25 16:34:23 +00001275 TARGET(LOAD_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001276 x = GETLOCAL(oparg);
1277 if (x != NULL) {
1278 Py_INCREF(x);
1279 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001280 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001281 }
1282 format_exc_check_arg(PyExc_UnboundLocalError,
1283 UNBOUNDLOCAL_ERROR_MSG,
1284 PyTuple_GetItem(co->co_varnames, oparg));
1285 break;
1286
Antoine Pitroub52ec782009-01-25 16:34:23 +00001287 TARGET(LOAD_CONST)
Skip Montanaro04d80f82002-08-04 21:03:35 +00001288 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001289 Py_INCREF(x);
1290 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001291 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001292
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001293 PREDICTED_WITH_ARG(STORE_FAST);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001294 TARGET(STORE_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001295 v = POP();
1296 SETLOCAL(oparg, v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001297 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001298
Antoine Pitroub52ec782009-01-25 16:34:23 +00001299 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001300 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001301 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001302 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001303
Antoine Pitroub52ec782009-01-25 16:34:23 +00001304 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 v = TOP();
1306 w = SECOND();
1307 SET_TOP(w);
1308 SET_SECOND(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001309 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Antoine Pitroub52ec782009-01-25 16:34:23 +00001311 TARGET(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001312 v = TOP();
1313 w = SECOND();
1314 x = THIRD();
1315 SET_TOP(w);
1316 SET_SECOND(x);
1317 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001318 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Antoine Pitroub52ec782009-01-25 16:34:23 +00001320 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 u = TOP();
1322 v = SECOND();
1323 w = THIRD();
1324 x = FOURTH();
1325 SET_TOP(v);
1326 SET_SECOND(w);
1327 SET_THIRD(x);
1328 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001329 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Antoine Pitroub52ec782009-01-25 16:34:23 +00001331 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001332 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001333 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001334 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001335 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Antoine Pitroub52ec782009-01-25 16:34:23 +00001337 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001338 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001339 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001340 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001342 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001343 STACKADJ(2);
1344 SET_TOP(x);
1345 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001346 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001347 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001349 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001351 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001352 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001353 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 STACKADJ(3);
1355 SET_TOP(x);
1356 SET_SECOND(w);
1357 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001358 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001360 Py_FatalError("invalid argument to DUP_TOPX"
1361 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001362 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001363 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001364
Antoine Pitroub52ec782009-01-25 16:34:23 +00001365 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001366 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001367 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001368 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001369 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001370 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001371 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Antoine Pitroub52ec782009-01-25 16:34:23 +00001373 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001375 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001376 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001378 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001379 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Antoine Pitroub52ec782009-01-25 16:34:23 +00001381 TARGET(UNARY_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001382 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001383 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001384 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001385 if (err == 0) {
1386 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001387 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001388 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001389 }
1390 else if (err > 0) {
1391 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001392 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001393 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001394 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001395 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001396 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Antoine Pitroub52ec782009-01-25 16:34:23 +00001399 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001400 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001401 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001402 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001403 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001404 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001405 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Antoine Pitroub52ec782009-01-25 16:34:23 +00001407 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001408 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001409 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001410 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001411 Py_DECREF(v);
1412 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001413 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001414 if (x != NULL) DISPATCH();
Guido van Rossum50564e81996-01-12 01:13:16 +00001415 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001416
Antoine Pitroub52ec782009-01-25 16:34:23 +00001417 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001418 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001419 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001420 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001421 Py_DECREF(v);
1422 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001423 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001424 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001425 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Antoine Pitroub52ec782009-01-25 16:34:23 +00001427 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001428 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001429 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001430 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001431 Py_DECREF(v);
1432 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001433 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001434 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001436
Antoine Pitroub52ec782009-01-25 16:34:23 +00001437 TARGET(BINARY_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001438 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001439 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001440 x = PyNumber_FloorDivide(v, w);
1441 Py_DECREF(v);
1442 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001443 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001444 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001445 break;
1446
Antoine Pitroub52ec782009-01-25 16:34:23 +00001447 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001448 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001449 v = TOP();
Benjamin Petersonefb06b02009-02-26 18:55:48 +00001450 if (PyUnicode_CheckExact(v))
1451 x = PyUnicode_Format(v, w);
1452 else
1453 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001454 Py_DECREF(v);
1455 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001456 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001457 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001458 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001459
Antoine Pitroub52ec782009-01-25 16:34:23 +00001460 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001461 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001462 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001463 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001464 PyUnicode_CheckExact(w)) {
1465 x = unicode_concatenate(v, w, f, next_instr);
1466 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001467 goto skip_decref_vx;
1468 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001469 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001470 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001471 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001472 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001473 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001474 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001475 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001476 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001478
Antoine Pitroub52ec782009-01-25 16:34:23 +00001479 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001480 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001481 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001482 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001483 Py_DECREF(v);
1484 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001485 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001486 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001487 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Antoine Pitroub52ec782009-01-25 16:34:23 +00001489 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001490 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001491 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001492 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001493 Py_DECREF(v);
1494 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001495 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001496 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Antoine Pitroub52ec782009-01-25 16:34:23 +00001499 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001500 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001501 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001502 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001503 Py_DECREF(v);
1504 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001505 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001506 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001507 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Antoine Pitroub52ec782009-01-25 16:34:23 +00001509 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001510 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001511 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001512 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001513 Py_DECREF(v);
1514 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001515 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001516 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001517 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001518
Antoine Pitroub52ec782009-01-25 16:34:23 +00001519 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001520 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001521 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001522 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001523 Py_DECREF(v);
1524 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001525 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001526 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001527 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Antoine Pitroub52ec782009-01-25 16:34:23 +00001529 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001530 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001531 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001532 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001533 Py_DECREF(v);
1534 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001535 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001536 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001537 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Antoine Pitroub52ec782009-01-25 16:34:23 +00001539 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001540 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001541 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001542 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001543 Py_DECREF(v);
1544 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001545 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001546 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001547 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001548
Antoine Pitroub52ec782009-01-25 16:34:23 +00001549 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001550 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001551 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001552 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001553 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001554 if (err == 0) {
1555 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001556 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001557 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001558 break;
1559
Antoine Pitroub52ec782009-01-25 16:34:23 +00001560 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001561 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001562 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001563 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001564 Py_DECREF(w);
1565 if (err == 0) {
1566 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001567 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001568 }
1569 break;
1570
Antoine Pitroub52ec782009-01-25 16:34:23 +00001571 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001572 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001573 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001574 x = PyNumber_InPlacePower(v, w, Py_None);
1575 Py_DECREF(v);
1576 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001577 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001578 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001579 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Antoine Pitroub52ec782009-01-25 16:34:23 +00001581 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001582 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001583 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001584 x = PyNumber_InPlaceMultiply(v, w);
1585 Py_DECREF(v);
1586 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001587 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001588 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001589 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001590
Antoine Pitroub52ec782009-01-25 16:34:23 +00001591 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001592 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001593 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001594 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001595 Py_DECREF(v);
1596 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001597 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001598 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001599 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001600
Antoine Pitroub52ec782009-01-25 16:34:23 +00001601 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001602 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001603 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001604 x = PyNumber_InPlaceFloorDivide(v, w);
1605 Py_DECREF(v);
1606 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001607 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001608 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001609 break;
1610
Antoine Pitroub52ec782009-01-25 16:34:23 +00001611 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001612 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001613 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001614 x = PyNumber_InPlaceRemainder(v, w);
1615 Py_DECREF(v);
1616 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001617 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001618 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001619 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001620
Antoine Pitroub52ec782009-01-25 16:34:23 +00001621 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001622 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001623 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001624 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001625 PyUnicode_CheckExact(w)) {
1626 x = unicode_concatenate(v, w, f, next_instr);
1627 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001628 goto skip_decref_v;
1629 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001630 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001631 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001632 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001633 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001634 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001635 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001636 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001637 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001638 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001639
Antoine Pitroub52ec782009-01-25 16:34:23 +00001640 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001641 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001642 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001643 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001644 Py_DECREF(v);
1645 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001646 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001647 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001648 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001649
Antoine Pitroub52ec782009-01-25 16:34:23 +00001650 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001651 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001652 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001653 x = PyNumber_InPlaceLshift(v, w);
1654 Py_DECREF(v);
1655 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001656 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001657 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001658 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Antoine Pitroub52ec782009-01-25 16:34:23 +00001660 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001661 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001662 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001663 x = PyNumber_InPlaceRshift(v, w);
1664 Py_DECREF(v);
1665 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001666 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001667 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001668 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001669
Antoine Pitroub52ec782009-01-25 16:34:23 +00001670 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001671 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001672 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001673 x = PyNumber_InPlaceAnd(v, w);
1674 Py_DECREF(v);
1675 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001676 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001677 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001678 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001679
Antoine Pitroub52ec782009-01-25 16:34:23 +00001680 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001681 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001682 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001683 x = PyNumber_InPlaceXor(v, w);
1684 Py_DECREF(v);
1685 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001686 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001687 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001688 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Antoine Pitroub52ec782009-01-25 16:34:23 +00001690 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001691 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001692 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001693 x = PyNumber_InPlaceOr(v, w);
1694 Py_DECREF(v);
1695 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001696 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001697 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001698 break;
1699
Antoine Pitroub52ec782009-01-25 16:34:23 +00001700 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001701 w = TOP();
1702 v = SECOND();
1703 u = THIRD();
1704 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001705 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001706 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001707 Py_DECREF(u);
1708 Py_DECREF(v);
1709 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001710 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001711 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Antoine Pitroub52ec782009-01-25 16:34:23 +00001713 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001714 w = TOP();
1715 v = SECOND();
1716 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001717 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001718 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001719 Py_DECREF(v);
1720 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001721 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001723
Antoine Pitroub52ec782009-01-25 16:34:23 +00001724 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001726 w = PySys_GetObject("displayhook");
1727 if (w == NULL) {
1728 PyErr_SetString(PyExc_RuntimeError,
1729 "lost sys.displayhook");
1730 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001731 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001732 }
1733 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001734 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001735 if (x == NULL)
1736 err = -1;
1737 }
1738 if (err == 0) {
1739 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001740 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001741 if (w == NULL)
1742 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001744 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001745 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001746 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001747
Thomas Wouters434d0822000-08-24 20:11:32 +00001748#ifdef CASE_TOO_BIG
1749 default: switch (opcode) {
1750#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001751 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001752 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001753 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001754 case 2:
1755 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001756 case 1:
1757 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001758 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001759 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001760 break;
1761 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001762 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001763 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001764 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001765 break;
1766 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001767 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Antoine Pitroub52ec782009-01-25 16:34:23 +00001769 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001770 x = POP();
1771 v = f->f_locals;
1772 Py_XDECREF(v);
1773 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001774 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Antoine Pitroub52ec782009-01-25 16:34:23 +00001776 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001777 retval = POP();
1778 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001779 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001780
Antoine Pitroub52ec782009-01-25 16:34:23 +00001781 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001782 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001783 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001784 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001785 /* Put aside the current exception state and restore
1786 that of the calling frame. This only serves when
1787 "yield" is used inside an except handler. */
1788 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001789 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001790
Antoine Pitroub52ec782009-01-25 16:34:23 +00001791 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001792 {
1793 PyTryBlock *b = PyFrame_BlockPop(f);
1794 if (b->b_type != EXCEPT_HANDLER) {
1795 PyErr_SetString(PyExc_SystemError,
1796 "popped block is not an except handler");
1797 why = WHY_EXCEPTION;
1798 break;
1799 }
1800 UNWIND_EXCEPT_HANDLER(b);
1801 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001802 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001803
Antoine Pitroub52ec782009-01-25 16:34:23 +00001804 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001806 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001807 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001808 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001809 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Christian Heimes180510d2008-03-03 19:15:45 +00001811 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001812 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001813 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001814 if (PyLong_Check(v)) {
1815 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001816 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001817 if (why == WHY_RETURN ||
1818 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001819 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001820 if (why == WHY_SILENCED) {
1821 /* An exception was silenced by 'with', we must
1822 manually unwind the EXCEPT_HANDLER block which was
1823 created when the exception was caught, otherwise
1824 the stack will be in an inconsistent state. */
1825 PyTryBlock *b = PyFrame_BlockPop(f);
1826 if (b->b_type != EXCEPT_HANDLER) {
1827 PyErr_SetString(PyExc_SystemError,
1828 "popped block is not an except handler");
1829 why = WHY_EXCEPTION;
1830 }
1831 else {
1832 UNWIND_EXCEPT_HANDLER(b);
1833 why = WHY_NOT;
1834 }
1835 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001836 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001837 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001838 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001839 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001840 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001841 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001842 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001843 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001844 else if (v != Py_None) {
1845 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001846 "'finally' pops bad exception");
1847 why = WHY_EXCEPTION;
1848 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001849 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001851
Antoine Pitroub52ec782009-01-25 16:34:23 +00001852 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001853 x = PyDict_GetItemString(f->f_builtins,
1854 "__build_class__");
1855 if (x == NULL) {
1856 PyErr_SetString(PyExc_ImportError,
1857 "__build_class__ not found");
1858 break;
1859 }
1860 Py_INCREF(x);
1861 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001863
Antoine Pitroub52ec782009-01-25 16:34:23 +00001864 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001865 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001866 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001867 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001868 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001869 err = PyDict_SetItem(x, w, v);
1870 else
1871 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001872 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001873 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001874 break;
1875 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001876 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001877 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001878 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001879
Antoine Pitroub52ec782009-01-25 16:34:23 +00001880 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001881 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001882 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001883 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001884 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001885 NAME_ERROR_MSG,
1886 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001887 break;
1888 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001889 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001890 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001892
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001893 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001894 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001896 if (PyTuple_CheckExact(v) &&
1897 PyTuple_GET_SIZE(v) == oparg) {
1898 PyObject **items = \
1899 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001900 while (oparg--) {
1901 w = items[oparg];
1902 Py_INCREF(w);
1903 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001904 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001905 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001906 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001907 } else if (PyList_CheckExact(v) &&
1908 PyList_GET_SIZE(v) == oparg) {
1909 PyObject **items = \
1910 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001911 while (oparg--) {
1912 w = items[oparg];
1913 Py_INCREF(w);
1914 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001915 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001916 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001917 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001918 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001919 } else {
1920 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001921 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001922 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001923 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001924 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001925
Antoine Pitroub52ec782009-01-25 16:34:23 +00001926 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00001927 {
1928 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1929 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001930
Guido van Rossum0368b722007-05-11 16:50:42 +00001931 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1932 stack_pointer + totalargs)) {
1933 stack_pointer += totalargs;
1934 } else {
1935 why = WHY_EXCEPTION;
1936 }
1937 Py_DECREF(v);
1938 break;
1939 }
1940
Antoine Pitroub52ec782009-01-25 16:34:23 +00001941 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001942 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001943 v = TOP();
1944 u = SECOND();
1945 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001946 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1947 Py_DECREF(v);
1948 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001949 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001950 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001951
Antoine Pitroub52ec782009-01-25 16:34:23 +00001952 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001953 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001954 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001955 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1956 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001957 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001958 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Antoine Pitroub52ec782009-01-25 16:34:23 +00001960 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001961 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001962 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001963 err = PyDict_SetItem(f->f_globals, w, v);
1964 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001965 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001966 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001967
Antoine Pitroub52ec782009-01-25 16:34:23 +00001968 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001969 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001970 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001971 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001972 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001973 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001974
Antoine Pitroub52ec782009-01-25 16:34:23 +00001975 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001976 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001977 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001978 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001979 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001980 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001981 break;
1982 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001983 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001984 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001985 Py_XINCREF(x);
1986 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001987 else {
1988 x = PyObject_GetItem(v, w);
1989 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001990 if (!PyErr_ExceptionMatches(
1991 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001992 break;
1993 PyErr_Clear();
1994 }
1995 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001996 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001997 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001998 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001999 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002001 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002002 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002003 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 break;
2005 }
2006 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002007 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002008 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002009 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002010 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002011
Antoine Pitroub52ec782009-01-25 16:34:23 +00002012 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002013 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002014 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002015 /* Inline the PyDict_GetItem() calls.
2016 WARNING: this is an extreme speed hack.
2017 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002018 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002019 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002020 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002021 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002022 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002023 e = d->ma_lookup(d, w, hash);
2024 if (e == NULL) {
2025 x = NULL;
2026 break;
2027 }
2028 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002029 if (x != NULL) {
2030 Py_INCREF(x);
2031 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002032 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002033 }
2034 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002035 e = d->ma_lookup(d, w, hash);
2036 if (e == NULL) {
2037 x = NULL;
2038 break;
2039 }
2040 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002041 if (x != NULL) {
2042 Py_INCREF(x);
2043 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002044 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002045 }
2046 goto load_global_error;
2047 }
2048 }
2049 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002050 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002051 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002052 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002053 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002054 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002055 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002056 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002057 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 break;
2059 }
2060 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002061 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002062 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002063 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002064
Antoine Pitroub52ec782009-01-25 16:34:23 +00002065 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002066 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002067 if (x != NULL) {
2068 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002069 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002070 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002071 format_exc_check_arg(
2072 PyExc_UnboundLocalError,
2073 UNBOUNDLOCAL_ERROR_MSG,
2074 PyTuple_GetItem(co->co_varnames, oparg)
2075 );
2076 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002077
Antoine Pitroub52ec782009-01-25 16:34:23 +00002078 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002079 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002080 Py_INCREF(x);
2081 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002082 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002083 break;
2084
Antoine Pitroub52ec782009-01-25 16:34:23 +00002085 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002086 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002087 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002088 if (w != NULL) {
2089 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002090 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002091 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002092 err = -1;
2093 /* Don't stomp existing exception */
2094 if (PyErr_Occurred())
2095 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002096 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2097 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002098 oparg);
2099 format_exc_check_arg(
2100 PyExc_UnboundLocalError,
2101 UNBOUNDLOCAL_ERROR_MSG,
2102 v);
2103 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002104 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2105 PyTuple_GET_SIZE(co->co_cellvars));
2106 format_exc_check_arg(PyExc_NameError,
2107 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002108 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002109 break;
2110
Antoine Pitroub52ec782009-01-25 16:34:23 +00002111 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002112 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002113 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002114 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002115 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002116 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002117
Antoine Pitroub52ec782009-01-25 16:34:23 +00002118 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002119 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002120 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002121 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002122 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002123 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002124 }
2125 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002126 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002127 }
2128 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002129
Antoine Pitroub52ec782009-01-25 16:34:23 +00002130 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002131 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002132 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002133 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002134 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002135 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002136 }
2137 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002138 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002139 }
2140 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002141
Antoine Pitroub52ec782009-01-25 16:34:23 +00002142 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002143 x = PySet_New(NULL);
2144 if (x != NULL) {
2145 for (; --oparg >= 0;) {
2146 w = POP();
2147 if (err == 0)
2148 err = PySet_Add(x, w);
2149 Py_DECREF(w);
2150 }
2151 if (err != 0) {
2152 Py_DECREF(x);
2153 break;
2154 }
2155 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002156 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002157 }
2158 break;
2159
Antoine Pitroub52ec782009-01-25 16:34:23 +00002160 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002161 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002162 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002163 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002164 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002165
Antoine Pitroub52ec782009-01-25 16:34:23 +00002166 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002167 w = TOP(); /* key */
2168 u = SECOND(); /* value */
2169 v = THIRD(); /* dict */
2170 STACKADJ(-2);
2171 assert (PyDict_CheckExact(v));
2172 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2173 Py_DECREF(u);
2174 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002175 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002176 break;
2177
Antoine Pitroub52ec782009-01-25 16:34:23 +00002178 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002179 w = TOP(); /* key */
2180 u = SECOND(); /* value */
2181 STACKADJ(-2);
2182 v = stack_pointer[-oparg]; /* dict */
2183 assert (PyDict_CheckExact(v));
2184 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2185 Py_DECREF(u);
2186 Py_DECREF(w);
2187 if (err == 0) {
2188 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002189 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002190 }
2191 break;
2192
Antoine Pitroub52ec782009-01-25 16:34:23 +00002193 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002194 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002195 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002196 x = PyObject_GetAttr(v, w);
2197 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002198 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002199 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002200 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Antoine Pitroub52ec782009-01-25 16:34:23 +00002202 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002203 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002204 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002205 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002206 Py_DECREF(v);
2207 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002208 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002209 if (x == NULL) break;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002210 PREDICT(POP_JUMP_IF_FALSE);
2211 PREDICT(POP_JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002212 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002213
Antoine Pitroub52ec782009-01-25 16:34:23 +00002214 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002215 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002216 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002217 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002218 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002219 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002220 break;
2221 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002222 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002223 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002224 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002225 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002226 w = PyTuple_Pack(5,
2227 w,
2228 f->f_globals,
2229 f->f_locals == NULL ?
2230 Py_None : f->f_locals,
2231 v,
2232 u);
2233 else
2234 w = PyTuple_Pack(4,
2235 w,
2236 f->f_globals,
2237 f->f_locals == NULL ?
2238 Py_None : f->f_locals,
2239 v);
2240 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002241 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002242 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002243 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002244 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002245 x = NULL;
2246 break;
2247 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002248 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002249 v = x;
2250 x = PyEval_CallObject(v, w);
2251 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002252 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002253 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002254 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002255 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002256 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002257
Antoine Pitroub52ec782009-01-25 16:34:23 +00002258 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002259 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002260 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002261 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002262 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002263 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002264 break;
2265 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002266 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002267 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002268 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002269 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002270 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002271 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002272 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002273
Antoine Pitroub52ec782009-01-25 16:34:23 +00002274 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002275 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002276 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002277 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002278 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002279 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002280 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002281 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002282 break;
2283
Antoine Pitroub52ec782009-01-25 16:34:23 +00002284 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002285 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002286 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002287
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002288 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2289 TARGET(POP_JUMP_IF_FALSE)
2290 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002291 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002292 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002293 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002294 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002295 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002296 Py_DECREF(w);
2297 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002298 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002299 }
2300 err = PyObject_IsTrue(w);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002301 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002302 if (err > 0)
2303 err = 0;
2304 else if (err == 0)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002305 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002306 else
2307 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002308 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002309
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002310 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2311 TARGET(POP_JUMP_IF_TRUE)
2312 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002313 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002314 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002315 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002316 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002317 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002318 Py_DECREF(w);
2319 JUMPTO(oparg);
2320 FAST_DISPATCH();
2321 }
2322 err = PyObject_IsTrue(w);
2323 Py_DECREF(w);
2324 if (err > 0) {
2325 err = 0;
2326 JUMPTO(oparg);
2327 }
2328 else if (err == 0)
2329 ;
2330 else
2331 break;
2332 DISPATCH();
2333
2334 TARGET(JUMP_IF_FALSE_OR_POP)
2335 w = TOP();
2336 if (w == Py_True) {
2337 STACKADJ(-1);
2338 Py_DECREF(w);
2339 FAST_DISPATCH();
2340 }
2341 if (w == Py_False) {
2342 JUMPTO(oparg);
2343 FAST_DISPATCH();
2344 }
2345 err = PyObject_IsTrue(w);
2346 if (err > 0) {
2347 STACKADJ(-1);
2348 Py_DECREF(w);
2349 err = 0;
2350 }
2351 else if (err == 0)
2352 JUMPTO(oparg);
2353 else
2354 break;
2355 DISPATCH();
2356
2357 TARGET(JUMP_IF_TRUE_OR_POP)
2358 w = TOP();
2359 if (w == Py_False) {
2360 STACKADJ(-1);
2361 Py_DECREF(w);
2362 FAST_DISPATCH();
2363 }
2364 if (w == Py_True) {
2365 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002366 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002367 }
2368 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002369 if (err > 0) {
2370 err = 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002371 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002372 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002373 else if (err == 0) {
2374 STACKADJ(-1);
2375 Py_DECREF(w);
2376 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002377 else
2378 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002379 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002380
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002381 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002382 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002383 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002384#if FAST_LOOPS
2385 /* Enabling this path speeds-up all while and for-loops by bypassing
2386 the per-loop checks for signals. By default, this should be turned-off
2387 because it prevents detection of a control-break in tight loops like
2388 "while 1: pass". Compile with this option turned-on when you need
2389 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002390 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002391 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002392 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002393#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002394 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002395#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002396
Antoine Pitroub52ec782009-01-25 16:34:23 +00002397 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002398 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002399 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002400 x = PyObject_GetIter(v);
2401 Py_DECREF(v);
2402 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002403 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002404 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002405 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002406 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002407 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002408 break;
2409
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002410 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002411 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002412 /* before: [iter]; after: [iter, iter()] *or* [] */
2413 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002414 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002415 if (x != NULL) {
2416 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002417 PREDICT(STORE_FAST);
2418 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002419 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002420 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002421 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002422 if (!PyErr_ExceptionMatches(
2423 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002424 break;
2425 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002426 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002427 /* iterator ended normally */
2428 x = v = POP();
2429 Py_DECREF(v);
2430 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002431 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002432
Antoine Pitroub52ec782009-01-25 16:34:23 +00002433 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002434 why = WHY_BREAK;
2435 goto fast_block_end;
2436
Antoine Pitroub52ec782009-01-25 16:34:23 +00002437 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002438 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002439 if (!retval) {
2440 x = NULL;
2441 break;
2442 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002443 why = WHY_CONTINUE;
2444 goto fast_block_end;
2445
Antoine Pitroub52ec782009-01-25 16:34:23 +00002446 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2447 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2448 TARGET(SETUP_FINALLY)
2449 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002450 /* NOTE: If you add any new block-setup opcodes that
2451 are not try/except/finally handlers, you may need
2452 to update the PyGen_NeedsFinalizing() function.
2453 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002454
Guido van Rossumb209a111997-04-29 18:18:01 +00002455 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002456 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002457 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002458
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002459 TARGET(SETUP_WITH)
2460 {
2461 static PyObject *exit, *enter;
2462 w = TOP();
2463 x = special_lookup(w, "__exit__", &exit);
2464 if (!x)
2465 break;
2466 SET_TOP(x);
2467 u = special_lookup(w, "__enter__", &enter);
2468 Py_DECREF(w);
2469 if (!u) {
2470 x = NULL;
2471 break;
2472 }
2473 x = PyObject_CallFunctionObjArgs(u, NULL);
2474 Py_DECREF(u);
2475 if (!x)
2476 break;
2477 /* Setup the finally block before pushing the result
2478 of __enter__ on the stack. */
2479 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2480 STACK_LEVEL());
2481
2482 PUSH(x);
2483 DISPATCH();
2484 }
2485
Antoine Pitroub52ec782009-01-25 16:34:23 +00002486 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002487 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002488 /* At the top of the stack are 1-3 values indicating
2489 how/why we entered the finally clause:
2490 - TOP = None
2491 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2492 - TOP = WHY_*; no retval below it
2493 - (TOP, SECOND, THIRD) = exc_info()
2494 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002495 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002496 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002497 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002498 EXIT(None, None, None)
2499
2500 In all cases, we remove EXIT from the stack, leaving
2501 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002502
2503 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002504 *and* the function call returns a 'true' value, we
2505 "zap" this information, to prevent END_FINALLY from
2506 re-raising the exception. (But non-local gotos
2507 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002508 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002509
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002510 PyObject *exit_func;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002511 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002512 if (u == Py_None) {
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002513 POP();
2514 exit_func = TOP();
2515 SET_TOP(u);
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002516 v = w = Py_None;
2517 }
2518 else if (PyLong_Check(u)) {
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002519 POP();
2520 switch(PyLong_AsLong(u)) {
2521 case WHY_RETURN:
2522 case WHY_CONTINUE:
2523 /* Retval in TOP. */
2524 exit_func = SECOND();
2525 SET_SECOND(TOP());
2526 SET_TOP(u);
2527 break;
2528 default:
2529 exit_func = TOP();
2530 SET_TOP(u);
2531 break;
2532 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002533 u = v = w = Py_None;
2534 }
2535 else {
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002536 v = SECOND();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002537 w = THIRD();
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002538 exit_func = stack_pointer[-7];
2539 stack_pointer[-7] = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002540 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002541 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002542 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2543 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002544 Py_DECREF(exit_func);
2545 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002546 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002547
2548 if (u != Py_None)
2549 err = PyObject_IsTrue(x);
2550 else
2551 err = 0;
2552 Py_DECREF(x);
2553
2554 if (err < 0)
2555 break; /* Go to error exit */
2556 else if (err > 0) {
2557 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002558 /* There was an exception and a True return */
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002559 PUSH(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002560 }
Christian Heimes180510d2008-03-03 19:15:45 +00002561 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002562 break;
2563 }
2564
Antoine Pitroub52ec782009-01-25 16:34:23 +00002565 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002566 {
2567 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002568 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002569 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002570#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002571 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002572#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002573 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002574#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002575 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002576 PUSH(x);
2577 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002578 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002579 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002580 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002581
Antoine Pitroub52ec782009-01-25 16:34:23 +00002582 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2583 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2584 TARGET(CALL_FUNCTION_VAR_KW)
2585 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002586 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002587 int na = oparg & 0xff;
2588 int nk = (oparg>>8) & 0xff;
2589 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002590 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002591 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002592 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002593 if (flags & CALL_FLAG_VAR)
2594 n++;
2595 if (flags & CALL_FLAG_KW)
2596 n++;
2597 pfunc = stack_pointer - n - 1;
2598 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002599
Guido van Rossumac7be682001-01-17 15:42:30 +00002600 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002601 && PyMethod_GET_SELF(func) != NULL) {
2602 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002603 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002604 func = PyMethod_GET_FUNCTION(func);
2605 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002606 Py_DECREF(*pfunc);
2607 *pfunc = self;
2608 na++;
2609 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002610 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002611 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002612 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002613 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002614 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002615 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002616 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002617 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002618
Jeremy Hylton76901512000-03-28 23:49:17 +00002619 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002620 w = POP();
2621 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002622 }
2623 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002624 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002625 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002626 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002627 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002628
Antoine Pitroub52ec782009-01-25 16:34:23 +00002629 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2630 TARGET(MAKE_FUNCTION)
2631 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002632 {
2633 int posdefaults = oparg & 0xff;
2634 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002635 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002636
Guido van Rossum681d79a1995-07-18 14:51:37 +00002637 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002638 x = PyFunction_New(v, f->f_globals);
2639 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002640
Guido van Rossum0240b922007-02-26 21:23:50 +00002641 if (x != NULL && opcode == MAKE_CLOSURE) {
2642 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002643 if (PyFunction_SetClosure(x, v) != 0) {
2644 /* Can't happen unless bytecode is corrupt. */
2645 why = WHY_EXCEPTION;
2646 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002647 Py_DECREF(v);
2648 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002649
2650 if (x != NULL && num_annotations > 0) {
2651 Py_ssize_t name_ix;
2652 u = POP(); /* names of args with annotations */
2653 v = PyDict_New();
2654 if (v == NULL) {
2655 Py_DECREF(x);
2656 x = NULL;
2657 break;
2658 }
2659 name_ix = PyTuple_Size(u);
2660 assert(num_annotations == name_ix+1);
2661 while (name_ix > 0) {
2662 --name_ix;
2663 t = PyTuple_GET_ITEM(u, name_ix);
2664 w = POP();
2665 /* XXX(nnorwitz): check for errors */
2666 PyDict_SetItem(v, t, w);
2667 Py_DECREF(w);
2668 }
2669
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002670 if (PyFunction_SetAnnotations(x, v) != 0) {
2671 /* Can't happen unless
2672 PyFunction_SetAnnotations changes. */
2673 why = WHY_EXCEPTION;
2674 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002675 Py_DECREF(v);
2676 Py_DECREF(u);
2677 }
2678
Guido van Rossum681d79a1995-07-18 14:51:37 +00002679 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002680 if (x != NULL && posdefaults > 0) {
2681 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002682 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002683 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002684 x = NULL;
2685 break;
2686 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002687 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002688 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002689 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002690 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002691 if (PyFunction_SetDefaults(x, v) != 0) {
2692 /* Can't happen unless
2693 PyFunction_SetDefaults changes. */
2694 why = WHY_EXCEPTION;
2695 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002696 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002697 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002698 if (x != NULL && kwdefaults > 0) {
2699 v = PyDict_New();
2700 if (v == NULL) {
2701 Py_DECREF(x);
2702 x = NULL;
2703 break;
2704 }
2705 while (--kwdefaults >= 0) {
2706 w = POP(); /* default value */
2707 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002708 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002709 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002710 Py_DECREF(w);
2711 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002712 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002713 if (PyFunction_SetKwDefaults(x, v) != 0) {
2714 /* Can't happen unless
2715 PyFunction_SetKwDefaults changes. */
2716 why = WHY_EXCEPTION;
2717 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002718 Py_DECREF(v);
2719 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002720 PUSH(x);
2721 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002722 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002723
Antoine Pitroub52ec782009-01-25 16:34:23 +00002724 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002725 if (oparg == 3)
2726 w = POP();
2727 else
2728 w = NULL;
2729 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002730 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002731 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002732 Py_DECREF(u);
2733 Py_DECREF(v);
2734 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002735 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002736 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002737 break;
2738
Antoine Pitroub52ec782009-01-25 16:34:23 +00002739 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002740 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002741 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002742 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002743
Antoine Pitroub52ec782009-01-25 16:34:23 +00002744#ifdef USE_COMPUTED_GOTOS
2745 _unknown_opcode:
2746#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002747 default:
2748 fprintf(stderr,
2749 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002750 PyCode_Addr2Line(f->f_code, f->f_lasti),
2751 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002752 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002753 why = WHY_EXCEPTION;
2754 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002755
2756#ifdef CASE_TOO_BIG
2757 }
2758#endif
2759
Guido van Rossum374a9221991-04-04 10:40:29 +00002760 } /* switch */
2761
2762 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002763
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002764 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002765
Guido van Rossum374a9221991-04-04 10:40:29 +00002766 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002767
Guido van Rossum374a9221991-04-04 10:40:29 +00002768 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002769 if (err == 0 && x != NULL) {
2770#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002771 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002772 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002773 fprintf(stderr,
2774 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002775 else {
2776#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002777 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002778 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002779#ifdef CHECKEXC
2780 }
2781#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002782 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002783 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002784 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002785 err = 0;
2786 }
2787
Guido van Rossum374a9221991-04-04 10:40:29 +00002788 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002789
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002790 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002791 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002792 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002793 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002794 why = WHY_EXCEPTION;
2795 }
2796 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002797#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002798 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002799 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002800 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002801 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002802 sprintf(buf, "Stack unwind with exception "
2803 "set and why=%d", why);
2804 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002805 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002806 }
2807#endif
2808
2809 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002810
Guido van Rossum374a9221991-04-04 10:40:29 +00002811 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002812 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002813
Fred Drake8f51f542001-10-04 14:48:42 +00002814 if (tstate->c_tracefunc != NULL)
2815 call_exc_trace(tstate->c_tracefunc,
2816 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002817 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002818
Guido van Rossum374a9221991-04-04 10:40:29 +00002819 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002820
Guido van Rossum374a9221991-04-04 10:40:29 +00002821 if (why == WHY_RERAISE)
2822 why = WHY_EXCEPTION;
2823
2824 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002825
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002826fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002827 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002828 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002829
Tim Peters8a5c3c72004-04-05 19:36:21 +00002830 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002831 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2832 /* For a continue inside a try block,
2833 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002834 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2835 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002836 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002837 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002838 Py_DECREF(retval);
2839 break;
2840 }
2841
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002842 if (b->b_type == EXCEPT_HANDLER) {
2843 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002844 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002845 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002846 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002847 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2848 why = WHY_NOT;
2849 JUMPTO(b->b_handler);
2850 break;
2851 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002852 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2853 || b->b_type == SETUP_FINALLY)) {
2854 PyObject *exc, *val, *tb;
2855 int handler = b->b_handler;
2856 /* Beware, this invalidates all b->b_* fields */
2857 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2858 PUSH(tstate->exc_traceback);
2859 PUSH(tstate->exc_value);
2860 if (tstate->exc_type != NULL) {
2861 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002862 }
2863 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002864 Py_INCREF(Py_None);
2865 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002866 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002867 PyErr_Fetch(&exc, &val, &tb);
2868 /* Make the raw exception data
2869 available to the handler,
2870 so a program can emulate the
2871 Python main loop. */
2872 PyErr_NormalizeException(
2873 &exc, &val, &tb);
2874 PyException_SetTraceback(val, tb);
2875 Py_INCREF(exc);
2876 tstate->exc_type = exc;
2877 Py_INCREF(val);
2878 tstate->exc_value = val;
2879 tstate->exc_traceback = tb;
2880 if (tb == NULL)
2881 tb = Py_None;
2882 Py_INCREF(tb);
2883 PUSH(tb);
2884 PUSH(val);
2885 PUSH(exc);
2886 why = WHY_NOT;
2887 JUMPTO(handler);
2888 break;
2889 }
2890 if (b->b_type == SETUP_FINALLY) {
2891 if (why & (WHY_RETURN | WHY_CONTINUE))
2892 PUSH(retval);
2893 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002894 why = WHY_NOT;
2895 JUMPTO(b->b_handler);
2896 break;
2897 }
2898 } /* unwind stack */
2899
2900 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002901
Guido van Rossum374a9221991-04-04 10:40:29 +00002902 if (why != WHY_NOT)
2903 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002904 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002905
Guido van Rossum374a9221991-04-04 10:40:29 +00002906 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002907
Tim Peters8a5c3c72004-04-05 19:36:21 +00002908 assert(why != WHY_YIELD);
2909 /* Pop remaining stack entries. */
2910 while (!EMPTY()) {
2911 v = POP();
2912 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002913 }
2914
Tim Peters8a5c3c72004-04-05 19:36:21 +00002915 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002916 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002917
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002918fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002919 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002920 if (tstate->c_tracefunc) {
2921 if (why == WHY_RETURN || why == WHY_YIELD) {
2922 if (call_trace(tstate->c_tracefunc,
2923 tstate->c_traceobj, f,
2924 PyTrace_RETURN, retval)) {
2925 Py_XDECREF(retval);
2926 retval = NULL;
2927 why = WHY_EXCEPTION;
2928 }
2929 }
2930 else if (why == WHY_EXCEPTION) {
2931 call_trace_protected(tstate->c_tracefunc,
2932 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002933 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002934 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002935 }
Fred Drake8f51f542001-10-04 14:48:42 +00002936 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002937 if (why == WHY_EXCEPTION)
2938 call_trace_protected(tstate->c_profilefunc,
2939 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002940 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002941 else if (call_trace(tstate->c_profilefunc,
2942 tstate->c_profileobj, f,
2943 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002944 Py_XDECREF(retval);
2945 retval = NULL;
2946 why = WHY_EXCEPTION;
2947 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002948 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002949 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002950
Tim Peters5ca576e2001-06-18 22:08:13 +00002951 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002952exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002953 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002954 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002955
Guido van Rossum96a42c81992-01-12 02:29:51 +00002956 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002957}
2958
Guido van Rossumc2e20742006-02-27 22:32:47 +00002959/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002960 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002961 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002962
Tim Peters6d6c1a32001-08-02 04:15:00 +00002963PyObject *
2964PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002965 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002966 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002967{
2968 register PyFrameObject *f;
2969 register PyObject *retval = NULL;
2970 register PyObject **fastlocals, **freevars;
2971 PyThreadState *tstate = PyThreadState_GET();
2972 PyObject *x, *u;
2973
2974 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002975 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002976 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002977 return NULL;
2978 }
2979
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002980 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002981 assert(globals != NULL);
2982 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002983 if (f == NULL)
2984 return NULL;
2985
2986 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002987 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002988
2989 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002990 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002991 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2992 int i;
2993 int n = argcount;
2994 PyObject *kwdict = NULL;
2995 if (co->co_flags & CO_VARKEYWORDS) {
2996 kwdict = PyDict_New();
2997 if (kwdict == NULL)
2998 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002999 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003000 if (co->co_flags & CO_VARARGS)
3001 i++;
3002 SETLOCAL(i, kwdict);
3003 }
3004 if (argcount > co->co_argcount) {
3005 if (!(co->co_flags & CO_VARARGS)) {
3006 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003007 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003008 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003009 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003010 defcount ? "at most" : "exactly",
3011 co->co_argcount,
3012 kwcount ? "non-keyword " : "",
3013 co->co_argcount == 1 ? "" : "s",
3014 argcount);
3015 goto fail;
3016 }
3017 n = co->co_argcount;
3018 }
3019 for (i = 0; i < n; i++) {
3020 x = args[i];
3021 Py_INCREF(x);
3022 SETLOCAL(i, x);
3023 }
3024 if (co->co_flags & CO_VARARGS) {
3025 u = PyTuple_New(argcount - n);
3026 if (u == NULL)
3027 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003028 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00003029 for (i = n; i < argcount; i++) {
3030 x = args[i];
3031 Py_INCREF(x);
3032 PyTuple_SET_ITEM(u, i-n, x);
3033 }
3034 }
3035 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003036 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003037 PyObject *keyword = kws[2*i];
3038 PyObject *value = kws[2*i + 1];
3039 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00003040 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003041 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003042 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003043 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00003044 goto fail;
3045 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003046 /* Speed hack: do raw pointer compares. As names are
3047 normally interned this should almost always hit. */
3048 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003049 for (j = 0;
3050 j < co->co_argcount + co->co_kwonlyargcount;
3051 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003052 PyObject *nm = co_varnames[j];
3053 if (nm == keyword)
3054 goto kw_found;
3055 }
3056 /* Slow fallback, just in case */
3057 for (j = 0;
3058 j < co->co_argcount + co->co_kwonlyargcount;
3059 j++) {
3060 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003061 int cmp = PyObject_RichCompareBool(
3062 keyword, nm, Py_EQ);
3063 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003064 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003065 else if (cmp < 0)
3066 goto fail;
3067 }
3068 /* Check errors from Compare */
3069 if (PyErr_Occurred())
3070 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003071 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003072 if (kwdict == NULL) {
3073 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003074 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00003075 "keyword argument '%S'",
3076 co->co_name,
3077 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00003078 goto fail;
3079 }
3080 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003081 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003082 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003083kw_found:
3084 if (GETLOCAL(j) != NULL) {
3085 PyErr_Format(PyExc_TypeError,
3086 "%U() got multiple "
3087 "values for keyword "
3088 "argument '%S'",
3089 co->co_name,
3090 keyword);
3091 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003092 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003093 Py_INCREF(value);
3094 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003095 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003096 if (co->co_kwonlyargcount > 0) {
3097 for (i = co->co_argcount;
3098 i < co->co_argcount + co->co_kwonlyargcount;
3099 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003100 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003101 if (GETLOCAL(i) != NULL)
3102 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003103 name = PyTuple_GET_ITEM(co->co_varnames, i);
3104 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003105 if (kwdefs != NULL)
3106 def = PyDict_GetItem(kwdefs, name);
3107 if (def != NULL) {
3108 Py_INCREF(def);
3109 SETLOCAL(i, def);
3110 continue;
3111 }
3112 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003113 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003114 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003115 goto fail;
3116 }
3117 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003118 if (argcount < co->co_argcount) {
3119 int m = co->co_argcount - defcount;
3120 for (i = argcount; i < m; i++) {
3121 if (GETLOCAL(i) == NULL) {
3122 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003123 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003124 "%spositional argument%s "
3125 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003126 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003127 ((co->co_flags & CO_VARARGS) ||
3128 defcount) ? "at least"
3129 : "exactly",
3130 m, kwcount ? "non-keyword " : "",
3131 m == 1 ? "" : "s", i);
3132 goto fail;
3133 }
3134 }
3135 if (n > m)
3136 i = n - m;
3137 else
3138 i = 0;
3139 for (; i < defcount; i++) {
3140 if (GETLOCAL(m+i) == NULL) {
3141 PyObject *def = defs[i];
3142 Py_INCREF(def);
3143 SETLOCAL(m+i, def);
3144 }
3145 }
3146 }
3147 }
3148 else {
3149 if (argcount > 0 || kwcount > 0) {
3150 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003151 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003152 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003153 argcount + kwcount);
3154 goto fail;
3155 }
3156 }
3157 /* Allocate and initialize storage for cell vars, and copy free
3158 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003159 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003160 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003161 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003162 PyObject *c;
3163
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003164 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003165 if (co->co_flags & CO_VARARGS)
3166 nargs++;
3167 if (co->co_flags & CO_VARKEYWORDS)
3168 nargs++;
3169
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170 /* Initialize each cell var, taking into account
3171 cell vars that are initialized from arguments.
3172
3173 Should arrange for the compiler to put cellvars
3174 that are arguments at the beginning of the cellvars
3175 list so that we can march over it more efficiently?
3176 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003177 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003178 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003179 PyTuple_GET_ITEM(co->co_cellvars, i));
3180 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003182 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003183 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003184 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003185 c = PyCell_New(GETLOCAL(j));
3186 if (c == NULL)
3187 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003188 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003189 found = 1;
3190 break;
3191 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003192 }
3193 if (found == 0) {
3194 c = PyCell_New(NULL);
3195 if (c == NULL)
3196 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003197 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003198 }
3199 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003200 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003201 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003202 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003203 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003204 PyObject *o = PyTuple_GET_ITEM(closure, i);
3205 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003206 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003207 }
3208 }
3209
Tim Peters5ca576e2001-06-18 22:08:13 +00003210 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003211 /* Don't need to keep the reference to f_back, it will be set
3212 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003213 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003214 f->f_back = NULL;
3215
Jeremy Hylton985eba52003-02-05 23:13:00 +00003216 PCALL(PCALL_GENERATOR);
3217
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003218 /* Create a new generator that owns the ready to run frame
3219 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003220 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003221 }
3222
Thomas Woutersce272b62007-09-19 21:19:28 +00003223 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003224
Thomas Woutersce272b62007-09-19 21:19:28 +00003225fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003226
Tim Petersb13680b2001-11-27 23:29:29 +00003227 /* decref'ing the frame can cause __del__ methods to get invoked,
3228 which can call back into Python. While we're done with the
3229 current Python frame (f), the associated C stack is still in use,
3230 so recursion_depth must be boosted for the duration.
3231 */
3232 assert(tstate != NULL);
3233 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003234 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003235 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003236 return retval;
3237}
3238
3239
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003240static PyObject *
3241special_lookup(PyObject *o, char *meth, PyObject **cache)
3242{
3243 PyObject *res;
3244 res = _PyObject_LookupSpecial(o, meth, cache);
3245 if (res == NULL && !PyErr_Occurred()) {
3246 PyErr_SetObject(PyExc_AttributeError, *cache);
3247 return NULL;
3248 }
3249 return res;
3250}
3251
3252
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003253/* Logic for the raise statement (too complicated for inlining).
3254 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003255static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003256do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003257{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003258 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003259
3260 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003261 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003262 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003263 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003264 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003265 value = tstate->exc_value;
3266 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003267 if (type == Py_None) {
3268 PyErr_SetString(PyExc_RuntimeError,
3269 "No active exception to reraise");
3270 return WHY_EXCEPTION;
3271 }
3272 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003273 Py_XINCREF(value);
3274 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003275 PyErr_Restore(type, value, tb);
3276 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003277 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003278
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003279 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003280 raise
3281 raise <instance>
3282 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003283
Collin Winter828f04a2007-08-31 00:04:24 +00003284 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003285 type = exc;
3286 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003287 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003288 goto raise_error;
3289 }
Collin Winter828f04a2007-08-31 00:04:24 +00003290 else if (PyExceptionInstance_Check(exc)) {
3291 value = exc;
3292 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003293 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003294 }
3295 else {
3296 /* Not something you can raise. You get an exception
3297 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003298 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003299 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003300 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003301 goto raise_error;
3302 }
Collin Winter828f04a2007-08-31 00:04:24 +00003303
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003304 if (cause) {
3305 PyObject *fixed_cause;
3306 if (PyExceptionClass_Check(cause)) {
3307 fixed_cause = PyObject_CallObject(cause, NULL);
3308 if (fixed_cause == NULL)
3309 goto raise_error;
3310 Py_DECREF(cause);
3311 }
3312 else if (PyExceptionInstance_Check(cause)) {
3313 fixed_cause = cause;
3314 }
3315 else {
3316 PyErr_SetString(PyExc_TypeError,
3317 "exception causes must derive from "
3318 "BaseException");
3319 goto raise_error;
3320 }
3321 PyException_SetCause(value, fixed_cause);
3322 }
Collin Winter828f04a2007-08-31 00:04:24 +00003323
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003324 PyErr_SetObject(type, value);
3325 /* PyErr_SetObject incref's its arguments */
3326 Py_XDECREF(value);
3327 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003328 return WHY_EXCEPTION;
3329
3330raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003331 Py_XDECREF(value);
3332 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003333 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003334 return WHY_EXCEPTION;
3335}
3336
Tim Petersd6d010b2001-06-21 02:49:55 +00003337/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003338 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003339
Guido van Rossum0368b722007-05-11 16:50:42 +00003340 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3341 with a variable target.
3342*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003343
Barry Warsawe42b18f1997-08-25 22:13:04 +00003344static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003345unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003346{
Guido van Rossum0368b722007-05-11 16:50:42 +00003347 int i = 0, j = 0;
3348 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003349 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003350 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003351 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003352
Tim Petersd6d010b2001-06-21 02:49:55 +00003353 assert(v != NULL);
3354
3355 it = PyObject_GetIter(v);
3356 if (it == NULL)
3357 goto Error;
3358
3359 for (; i < argcnt; i++) {
3360 w = PyIter_Next(it);
3361 if (w == NULL) {
3362 /* Iterator done, via error or exhaustion. */
3363 if (!PyErr_Occurred()) {
3364 PyErr_Format(PyExc_ValueError,
3365 "need more than %d value%s to unpack",
3366 i, i == 1 ? "" : "s");
3367 }
3368 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003369 }
3370 *--sp = w;
3371 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003372
Guido van Rossum0368b722007-05-11 16:50:42 +00003373 if (argcntafter == -1) {
3374 /* We better have exhausted the iterator now. */
3375 w = PyIter_Next(it);
3376 if (w == NULL) {
3377 if (PyErr_Occurred())
3378 goto Error;
3379 Py_DECREF(it);
3380 return 1;
3381 }
3382 Py_DECREF(w);
3383 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3384 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003385 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003386
3387 l = PySequence_List(it);
3388 if (l == NULL)
3389 goto Error;
3390 *--sp = l;
3391 i++;
3392
3393 ll = PyList_GET_SIZE(l);
3394 if (ll < argcntafter) {
3395 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3396 argcnt + ll);
3397 goto Error;
3398 }
3399
3400 /* Pop the "after-variable" args off the list. */
3401 for (j = argcntafter; j > 0; j--, i++) {
3402 *--sp = PyList_GET_ITEM(l, ll - j);
3403 }
3404 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003405 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003406 Py_DECREF(it);
3407 return 1;
3408
Tim Petersd6d010b2001-06-21 02:49:55 +00003409Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003410 for (; i > 0; i--, sp++)
3411 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003412 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003413 return 0;
3414}
3415
3416
Guido van Rossum96a42c81992-01-12 02:29:51 +00003417#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003418static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003419prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003420{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003421 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003422 if (PyObject_Print(v, stdout, 0) != 0)
3423 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003424 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003425 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003426}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003427#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003428
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003429static void
Fred Drake5755ce62001-06-27 19:19:46 +00003430call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003431{
Guido van Rossumb209a111997-04-29 18:18:01 +00003432 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003433 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003434 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003435 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003436 value = Py_None;
3437 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003438 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003439 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003440 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003441 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003442 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003443 }
Fred Drake5755ce62001-06-27 19:19:46 +00003444 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003445 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003446 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003447 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003448 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003449 Py_XDECREF(type);
3450 Py_XDECREF(value);
3451 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003452 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003453}
3454
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003455static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003456call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003457 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003458{
3459 PyObject *type, *value, *traceback;
3460 int err;
3461 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003462 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003463 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003464 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003465 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003466 return 0;
3467 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003468 else {
3469 Py_XDECREF(type);
3470 Py_XDECREF(value);
3471 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003472 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003473 }
3474}
3475
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003476static int
Fred Drake5755ce62001-06-27 19:19:46 +00003477call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3478 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003479{
Fred Drake5755ce62001-06-27 19:19:46 +00003480 register PyThreadState *tstate = frame->f_tstate;
3481 int result;
3482 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003483 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003484 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003485 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003486 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003487 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3488 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003489 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003490 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003491}
3492
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003493PyObject *
3494_PyEval_CallTracing(PyObject *func, PyObject *args)
3495{
3496 PyFrameObject *frame = PyEval_GetFrame();
3497 PyThreadState *tstate = frame->f_tstate;
3498 int save_tracing = tstate->tracing;
3499 int save_use_tracing = tstate->use_tracing;
3500 PyObject *result;
3501
3502 tstate->tracing = 0;
3503 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3504 || (tstate->c_profilefunc != NULL));
3505 result = PyObject_Call(func, args, NULL);
3506 tstate->tracing = save_tracing;
3507 tstate->use_tracing = save_use_tracing;
3508 return result;
3509}
3510
Michael W. Hudson006c7522002-11-08 13:08:46 +00003511static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003512maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003513 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3514 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003515{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003516 int result = 0;
3517
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003518 /* If the last instruction executed isn't in the current
3519 instruction window, reset the window. If the last
3520 instruction happens to fall at the start of a line or if it
3521 represents a jump backwards, call the trace function.
3522 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003523 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003524 int line;
3525 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003526
Thomas Woutersce272b62007-09-19 21:19:28 +00003527 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3528 &bounds);
3529 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003530 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003531 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003532 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003533 }
3534 *instr_lb = bounds.ap_lower;
3535 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003536 }
Armin Rigobf57a142004-03-22 19:24:58 +00003537 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003538 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003539 }
3540 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003541 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003542}
3543
Fred Drake5755ce62001-06-27 19:19:46 +00003544void
3545PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003546{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003547 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003548 PyObject *temp = tstate->c_profileobj;
3549 Py_XINCREF(arg);
3550 tstate->c_profilefunc = NULL;
3551 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003552 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003553 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003554 Py_XDECREF(temp);
3555 tstate->c_profilefunc = func;
3556 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003557 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003558 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003559}
3560
3561void
3562PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3563{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003564 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003565 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003566 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003567 Py_XINCREF(arg);
3568 tstate->c_tracefunc = NULL;
3569 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003570 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003571 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003572 Py_XDECREF(temp);
3573 tstate->c_tracefunc = func;
3574 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003575 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003576 tstate->use_tracing = ((func != NULL)
3577 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003578}
3579
Guido van Rossumb209a111997-04-29 18:18:01 +00003580PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003581PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003582{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003583 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003584 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003585 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003586 else
3587 return current_frame->f_builtins;
3588}
3589
Guido van Rossumb209a111997-04-29 18:18:01 +00003590PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003591PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003592{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003593 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003594 if (current_frame == NULL)
3595 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003596 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003597 return current_frame->f_locals;
3598}
3599
Guido van Rossumb209a111997-04-29 18:18:01 +00003600PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003601PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003602{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003603 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003604 if (current_frame == NULL)
3605 return NULL;
3606 else
3607 return current_frame->f_globals;
3608}
3609
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003610PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003611PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003612{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003613 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003614 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003615}
3616
Guido van Rossum6135a871995-01-09 17:53:26 +00003617int
Tim Peters5ba58662001-07-16 02:29:45 +00003618PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003619{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003620 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003621 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003622
3623 if (current_frame != NULL) {
3624 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003625 const int compilerflags = codeflags & PyCF_MASK;
3626 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003627 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003628 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003629 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003630#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003631 if (codeflags & CO_GENERATOR_ALLOWED) {
3632 result = 1;
3633 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3634 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003635#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003636 }
3637 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003638}
3639
Guido van Rossum3f5da241990-12-20 15:06:42 +00003640
Guido van Rossum681d79a1995-07-18 14:51:37 +00003641/* External interface to call any callable object.
3642 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003643
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003644#undef PyEval_CallObject
3645/* for backward compatibility: export this interface */
3646
Guido van Rossumb209a111997-04-29 18:18:01 +00003647PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003648PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003649{
Guido van Rossumb209a111997-04-29 18:18:01 +00003650 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003651}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003652#define PyEval_CallObject(func,arg) \
3653 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003654
Guido van Rossumb209a111997-04-29 18:18:01 +00003655PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003656PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003657{
Jeremy Hylton52820442001-01-03 23:52:36 +00003658 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003659
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003660 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003661 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003662 if (arg == NULL)
3663 return NULL;
3664 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003665 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003666 PyErr_SetString(PyExc_TypeError,
3667 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003668 return NULL;
3669 }
3670 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003671 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003672
Guido van Rossumb209a111997-04-29 18:18:01 +00003673 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003674 PyErr_SetString(PyExc_TypeError,
3675 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003676 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003677 return NULL;
3678 }
3679
Tim Peters6d6c1a32001-08-02 04:15:00 +00003680 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003681 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003682 return result;
3683}
3684
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003685const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003686PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003687{
3688 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003689 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003690 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003691 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003692 else if (PyCFunction_Check(func))
3693 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003694 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003695 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003696}
3697
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003698const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003699PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003700{
3701 if (PyMethod_Check(func))
3702 return "()";
3703 else if (PyFunction_Check(func))
3704 return "()";
3705 else if (PyCFunction_Check(func))
3706 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003707 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003708 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003709}
3710
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003711static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003712err_args(PyObject *func, int flags, int nargs)
3713{
3714 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003715 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003716 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003717 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003718 nargs);
3719 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003720 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003721 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003722 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003723 nargs);
3724}
3725
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003726#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003727if (tstate->use_tracing && tstate->c_profilefunc) { \
3728 if (call_trace(tstate->c_profilefunc, \
3729 tstate->c_profileobj, \
3730 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003731 func)) { \
3732 x = NULL; \
3733 } \
3734 else { \
3735 x = call; \
3736 if (tstate->c_profilefunc != NULL) { \
3737 if (x == NULL) { \
3738 call_trace_protected(tstate->c_profilefunc, \
3739 tstate->c_profileobj, \
3740 tstate->frame, PyTrace_C_EXCEPTION, \
3741 func); \
3742 /* XXX should pass (type, value, tb) */ \
3743 } else { \
3744 if (call_trace(tstate->c_profilefunc, \
3745 tstate->c_profileobj, \
3746 tstate->frame, PyTrace_C_RETURN, \
3747 func)) { \
3748 Py_DECREF(x); \
3749 x = NULL; \
3750 } \
3751 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003752 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003753 } \
3754} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003755 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003756 }
3757
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003758static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003759call_function(PyObject ***pp_stack, int oparg
3760#ifdef WITH_TSC
3761 , uint64* pintr0, uint64* pintr1
3762#endif
3763 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003764{
3765 int na = oparg & 0xff;
3766 int nk = (oparg>>8) & 0xff;
3767 int n = na + 2 * nk;
3768 PyObject **pfunc = (*pp_stack) - n - 1;
3769 PyObject *func = *pfunc;
3770 PyObject *x, *w;
3771
Jeremy Hylton985eba52003-02-05 23:13:00 +00003772 /* Always dispatch PyCFunction first, because these are
3773 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003774 */
3775 if (PyCFunction_Check(func) && nk == 0) {
3776 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003777 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003778
3779 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003780 if (flags & (METH_NOARGS | METH_O)) {
3781 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3782 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003783 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003784 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003785 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003786 else if (flags & METH_O && na == 1) {
3787 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003788 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003789 Py_DECREF(arg);
3790 }
3791 else {
3792 err_args(func, flags, na);
3793 x = NULL;
3794 }
3795 }
3796 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003797 PyObject *callargs;
3798 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003799 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003800 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003801 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003802 Py_XDECREF(callargs);
3803 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003804 } else {
3805 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3806 /* optimize access to bound methods */
3807 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003808 PCALL(PCALL_METHOD);
3809 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003810 Py_INCREF(self);
3811 func = PyMethod_GET_FUNCTION(func);
3812 Py_INCREF(func);
3813 Py_DECREF(*pfunc);
3814 *pfunc = self;
3815 na++;
3816 n++;
3817 } else
3818 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003819 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003820 if (PyFunction_Check(func))
3821 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003822 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003823 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003824 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003825 Py_DECREF(func);
3826 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003827
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003828 /* Clear the stack of the function object. Also removes
3829 the arguments in case they weren't consumed already
3830 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003831 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003832 while ((*pp_stack) > pfunc) {
3833 w = EXT_POP(*pp_stack);
3834 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003835 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003836 }
3837 return x;
3838}
3839
Jeremy Hylton192690e2002-08-16 18:36:11 +00003840/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003841 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003842 For the simplest case -- a function that takes only positional
3843 arguments and is called with only positional arguments -- it
3844 inlines the most primitive frame setup code from
3845 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3846 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003847*/
3848
3849static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003850fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003851{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003852 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003853 PyObject *globals = PyFunction_GET_GLOBALS(func);
3854 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003855 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003856 PyObject **d = NULL;
3857 int nd = 0;
3858
Jeremy Hylton985eba52003-02-05 23:13:00 +00003859 PCALL(PCALL_FUNCTION);
3860 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003861 if (argdefs == NULL && co->co_argcount == n &&
3862 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003863 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3864 PyFrameObject *f;
3865 PyObject *retval = NULL;
3866 PyThreadState *tstate = PyThreadState_GET();
3867 PyObject **fastlocals, **stack;
3868 int i;
3869
3870 PCALL(PCALL_FASTER_FUNCTION);
3871 assert(globals != NULL);
3872 /* XXX Perhaps we should create a specialized
3873 PyFrame_New() that doesn't take locals, but does
3874 take builtins without sanity checking them.
3875 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003876 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003877 f = PyFrame_New(tstate, co, globals, NULL);
3878 if (f == NULL)
3879 return NULL;
3880
3881 fastlocals = f->f_localsplus;
3882 stack = (*pp_stack) - n;
3883
3884 for (i = 0; i < n; i++) {
3885 Py_INCREF(*stack);
3886 fastlocals[i] = *stack++;
3887 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003888 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003889 ++tstate->recursion_depth;
3890 Py_DECREF(f);
3891 --tstate->recursion_depth;
3892 return retval;
3893 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003894 if (argdefs != NULL) {
3895 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003896 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003897 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003898 return PyEval_EvalCodeEx(co, globals,
3899 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003900 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003901 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003902}
3903
3904static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003905update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3906 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003907{
3908 PyObject *kwdict = NULL;
3909 if (orig_kwdict == NULL)
3910 kwdict = PyDict_New();
3911 else {
3912 kwdict = PyDict_Copy(orig_kwdict);
3913 Py_DECREF(orig_kwdict);
3914 }
3915 if (kwdict == NULL)
3916 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003917 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003918 int err;
3919 PyObject *value = EXT_POP(*pp_stack);
3920 PyObject *key = EXT_POP(*pp_stack);
3921 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003922 PyErr_Format(PyExc_TypeError,
3923 "%.200s%s got multiple values "
3924 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003925 PyEval_GetFuncName(func),
3926 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003927 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003928 Py_DECREF(key);
3929 Py_DECREF(value);
3930 Py_DECREF(kwdict);
3931 return NULL;
3932 }
3933 err = PyDict_SetItem(kwdict, key, value);
3934 Py_DECREF(key);
3935 Py_DECREF(value);
3936 if (err) {
3937 Py_DECREF(kwdict);
3938 return NULL;
3939 }
3940 }
3941 return kwdict;
3942}
3943
3944static PyObject *
3945update_star_args(int nstack, int nstar, PyObject *stararg,
3946 PyObject ***pp_stack)
3947{
3948 PyObject *callargs, *w;
3949
3950 callargs = PyTuple_New(nstack + nstar);
3951 if (callargs == NULL) {
3952 return NULL;
3953 }
3954 if (nstar) {
3955 int i;
3956 for (i = 0; i < nstar; i++) {
3957 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3958 Py_INCREF(a);
3959 PyTuple_SET_ITEM(callargs, nstack + i, a);
3960 }
3961 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003962 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003963 w = EXT_POP(*pp_stack);
3964 PyTuple_SET_ITEM(callargs, nstack, w);
3965 }
3966 return callargs;
3967}
3968
3969static PyObject *
3970load_args(PyObject ***pp_stack, int na)
3971{
3972 PyObject *args = PyTuple_New(na);
3973 PyObject *w;
3974
3975 if (args == NULL)
3976 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003977 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003978 w = EXT_POP(*pp_stack);
3979 PyTuple_SET_ITEM(args, na, w);
3980 }
3981 return args;
3982}
3983
3984static PyObject *
3985do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3986{
3987 PyObject *callargs = NULL;
3988 PyObject *kwdict = NULL;
3989 PyObject *result = NULL;
3990
3991 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003992 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003993 if (kwdict == NULL)
3994 goto call_fail;
3995 }
3996 callargs = load_args(pp_stack, na);
3997 if (callargs == NULL)
3998 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003999#ifdef CALL_PROFILE
4000 /* At this point, we have to look at the type of func to
4001 update the call stats properly. Do it here so as to avoid
4002 exposing the call stats machinery outside ceval.c
4003 */
4004 if (PyFunction_Check(func))
4005 PCALL(PCALL_FUNCTION);
4006 else if (PyMethod_Check(func))
4007 PCALL(PCALL_METHOD);
4008 else if (PyType_Check(func))
4009 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004010 else if (PyCFunction_Check(func))
4011 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004012 else
4013 PCALL(PCALL_OTHER);
4014#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004015 if (PyCFunction_Check(func)) {
4016 PyThreadState *tstate = PyThreadState_GET();
4017 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4018 }
4019 else
4020 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004021call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004022 Py_XDECREF(callargs);
4023 Py_XDECREF(kwdict);
4024 return result;
4025}
4026
4027static PyObject *
4028ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4029{
4030 int nstar = 0;
4031 PyObject *callargs = NULL;
4032 PyObject *stararg = NULL;
4033 PyObject *kwdict = NULL;
4034 PyObject *result = NULL;
4035
4036 if (flags & CALL_FLAG_KW) {
4037 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004038 if (!PyDict_Check(kwdict)) {
4039 PyObject *d;
4040 d = PyDict_New();
4041 if (d == NULL)
4042 goto ext_call_fail;
4043 if (PyDict_Update(d, kwdict) != 0) {
4044 Py_DECREF(d);
4045 /* PyDict_Update raises attribute
4046 * error (percolated from an attempt
4047 * to get 'keys' attribute) instead of
4048 * a type error if its second argument
4049 * is not a mapping.
4050 */
4051 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4052 PyErr_Format(PyExc_TypeError,
4053 "%.200s%.200s argument after ** "
4054 "must be a mapping, not %.200s",
4055 PyEval_GetFuncName(func),
4056 PyEval_GetFuncDesc(func),
4057 kwdict->ob_type->tp_name);
4058 }
4059 goto ext_call_fail;
4060 }
4061 Py_DECREF(kwdict);
4062 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004063 }
4064 }
4065 if (flags & CALL_FLAG_VAR) {
4066 stararg = EXT_POP(*pp_stack);
4067 if (!PyTuple_Check(stararg)) {
4068 PyObject *t = NULL;
4069 t = PySequence_Tuple(stararg);
4070 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004071 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4072 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004073 "%.200s%.200s argument after * "
4074 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004075 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004076 PyEval_GetFuncDesc(func),
4077 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004078 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004079 goto ext_call_fail;
4080 }
4081 Py_DECREF(stararg);
4082 stararg = t;
4083 }
4084 nstar = PyTuple_GET_SIZE(stararg);
4085 }
4086 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004087 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004088 if (kwdict == NULL)
4089 goto ext_call_fail;
4090 }
4091 callargs = update_star_args(na, nstar, stararg, pp_stack);
4092 if (callargs == NULL)
4093 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004094#ifdef CALL_PROFILE
4095 /* At this point, we have to look at the type of func to
4096 update the call stats properly. Do it here so as to avoid
4097 exposing the call stats machinery outside ceval.c
4098 */
4099 if (PyFunction_Check(func))
4100 PCALL(PCALL_FUNCTION);
4101 else if (PyMethod_Check(func))
4102 PCALL(PCALL_METHOD);
4103 else if (PyType_Check(func))
4104 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004105 else if (PyCFunction_Check(func))
4106 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004107 else
4108 PCALL(PCALL_OTHER);
4109#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004110 if (PyCFunction_Check(func)) {
4111 PyThreadState *tstate = PyThreadState_GET();
4112 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4113 }
4114 else
4115 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004116ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004117 Py_XDECREF(callargs);
4118 Py_XDECREF(kwdict);
4119 Py_XDECREF(stararg);
4120 return result;
4121}
4122
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004123/* Extract a slice index from a PyInt or PyLong or an object with the
4124 nb_index slot defined, and store in *pi.
4125 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4126 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 +00004127 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004128*/
Tim Petersb5196382001-12-16 19:44:20 +00004129/* Note: If v is NULL, return success without storing into *pi. This
4130 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4131 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004132*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004133int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004134_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004135{
Tim Petersb5196382001-12-16 19:44:20 +00004136 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004137 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004138 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004139 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004140 if (x == -1 && PyErr_Occurred())
4141 return 0;
4142 }
4143 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004144 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004145 "slice indices must be integers or "
4146 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004147 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004148 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004149 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004150 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004151 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004152}
4153
Guido van Rossum486364b2007-06-30 05:01:58 +00004154#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004155 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004156
Guido van Rossumb209a111997-04-29 18:18:01 +00004157static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004158cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004159{
Guido van Rossumac7be682001-01-17 15:42:30 +00004160 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004161 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004162 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004163 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004164 break;
4165 case PyCmp_IS_NOT:
4166 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004167 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004168 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004169 res = PySequence_Contains(w, v);
4170 if (res < 0)
4171 return NULL;
4172 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004173 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004174 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004175 if (res < 0)
4176 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004177 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004178 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004179 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004180 if (PyTuple_Check(w)) {
4181 Py_ssize_t i, length;
4182 length = PyTuple_Size(w);
4183 for (i = 0; i < length; i += 1) {
4184 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004185 if (!PyExceptionClass_Check(exc)) {
4186 PyErr_SetString(PyExc_TypeError,
4187 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004188 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004189 }
4190 }
4191 }
4192 else {
Brett Cannon39590462007-02-26 22:01:14 +00004193 if (!PyExceptionClass_Check(w)) {
4194 PyErr_SetString(PyExc_TypeError,
4195 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004196 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004197 }
4198 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004199 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004200 break;
4201 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004202 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004203 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004204 v = res ? Py_True : Py_False;
4205 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004206 return v;
4207}
4208
Thomas Wouters52152252000-08-17 22:55:00 +00004209static PyObject *
4210import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004211{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004212 PyObject *x;
4213
4214 x = PyObject_GetAttr(v, name);
4215 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004216 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004217 }
Thomas Wouters52152252000-08-17 22:55:00 +00004218 return x;
4219}
Guido van Rossumac7be682001-01-17 15:42:30 +00004220
Thomas Wouters52152252000-08-17 22:55:00 +00004221static int
4222import_all_from(PyObject *locals, PyObject *v)
4223{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004224 PyObject *all = PyObject_GetAttrString(v, "__all__");
4225 PyObject *dict, *name, *value;
4226 int skip_leading_underscores = 0;
4227 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004228
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004229 if (all == NULL) {
4230 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4231 return -1; /* Unexpected error */
4232 PyErr_Clear();
4233 dict = PyObject_GetAttrString(v, "__dict__");
4234 if (dict == NULL) {
4235 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4236 return -1;
4237 PyErr_SetString(PyExc_ImportError,
4238 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004239 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004240 }
4241 all = PyMapping_Keys(dict);
4242 Py_DECREF(dict);
4243 if (all == NULL)
4244 return -1;
4245 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004246 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004247
4248 for (pos = 0, err = 0; ; pos++) {
4249 name = PySequence_GetItem(all, pos);
4250 if (name == NULL) {
4251 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4252 err = -1;
4253 else
4254 PyErr_Clear();
4255 break;
4256 }
4257 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004258 PyUnicode_Check(name) &&
4259 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004260 {
4261 Py_DECREF(name);
4262 continue;
4263 }
4264 value = PyObject_GetAttr(v, name);
4265 if (value == NULL)
4266 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004267 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004268 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004269 else
4270 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004271 Py_DECREF(name);
4272 Py_XDECREF(value);
4273 if (err != 0)
4274 break;
4275 }
4276 Py_DECREF(all);
4277 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004278}
4279
Guido van Rossumac7be682001-01-17 15:42:30 +00004280static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004281format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004282{
Neal Norwitzda059e32007-08-26 05:33:45 +00004283 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004284
4285 if (!obj)
4286 return;
4287
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004288 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004289 if (!obj_str)
4290 return;
4291
4292 PyErr_Format(exc, format_str, obj_str);
4293}
Guido van Rossum950361c1997-01-24 13:49:28 +00004294
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004295static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004296unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004297 PyFrameObject *f, unsigned char *next_instr)
4298{
4299 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004300 are (Unicode) strings. */
4301 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4302 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004303 Py_ssize_t new_len = v_len + w_len;
4304 if (new_len < 0) {
4305 PyErr_SetString(PyExc_OverflowError,
4306 "strings are too large to concat");
4307 return NULL;
4308 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004309
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004310 if (v->ob_refcnt == 2) {
4311 /* In the common case, there are 2 references to the value
4312 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004313 * value stack (in 'v') and one still stored in the
4314 * 'variable'. We try to delete the variable now to reduce
4315 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004316 */
4317 switch (*next_instr) {
4318 case STORE_FAST:
4319 {
4320 int oparg = PEEKARG();
4321 PyObject **fastlocals = f->f_localsplus;
4322 if (GETLOCAL(oparg) == v)
4323 SETLOCAL(oparg, NULL);
4324 break;
4325 }
4326 case STORE_DEREF:
4327 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004328 PyObject **freevars = (f->f_localsplus +
4329 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004330 PyObject *c = freevars[PEEKARG()];
4331 if (PyCell_GET(c) == v)
4332 PyCell_Set(c, NULL);
4333 break;
4334 }
4335 case STORE_NAME:
4336 {
4337 PyObject *names = f->f_code->co_names;
4338 PyObject *name = GETITEM(names, PEEKARG());
4339 PyObject *locals = f->f_locals;
4340 if (PyDict_CheckExact(locals) &&
4341 PyDict_GetItem(locals, name) == v) {
4342 if (PyDict_DelItem(locals, name) != 0) {
4343 PyErr_Clear();
4344 }
4345 }
4346 break;
4347 }
4348 }
4349 }
4350
Guido van Rossum98297ee2007-11-06 21:34:58 +00004351 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004352 /* Now we own the last reference to 'v', so we can resize it
4353 * in-place.
4354 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004355 if (PyUnicode_Resize(&v, new_len) != 0) {
4356 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004357 * deallocated so it cannot be put back into
4358 * 'variable'. The MemoryError is raised when there
4359 * is no value in 'variable', which might (very
4360 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004361 */
4362 return NULL;
4363 }
4364 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004365 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4366 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004367 return v;
4368 }
4369 else {
4370 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004371 w = PyUnicode_Concat(v, w);
4372 Py_DECREF(v);
4373 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004374 }
4375}
4376
Guido van Rossum950361c1997-01-24 13:49:28 +00004377#ifdef DYNAMIC_EXECUTION_PROFILE
4378
Skip Montanarof118cb12001-10-15 20:51:38 +00004379static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004380getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004381{
4382 int i;
4383 PyObject *l = PyList_New(256);
4384 if (l == NULL) return NULL;
4385 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004386 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004387 if (x == NULL) {
4388 Py_DECREF(l);
4389 return NULL;
4390 }
4391 PyList_SetItem(l, i, x);
4392 }
4393 for (i = 0; i < 256; i++)
4394 a[i] = 0;
4395 return l;
4396}
4397
4398PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004399_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004400{
4401#ifndef DXPAIRS
4402 return getarray(dxp);
4403#else
4404 int i;
4405 PyObject *l = PyList_New(257);
4406 if (l == NULL) return NULL;
4407 for (i = 0; i < 257; i++) {
4408 PyObject *x = getarray(dxpairs[i]);
4409 if (x == NULL) {
4410 Py_DECREF(l);
4411 return NULL;
4412 }
4413 PyList_SetItem(l, i, x);
4414 }
4415 return l;
4416#endif
4417}
4418
4419#endif