blob: 24e41689ade9681cbe44cd000b1d13bde9dfeaf3 [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 Peterson176101d2009-06-28 15:40:50 +00002536 PyObject *tp, *exc, *tb;
2537 PyTryBlock *block;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002538 v = SECOND();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002539 w = THIRD();
Benjamin Peterson176101d2009-06-28 15:40:50 +00002540 tp = FOURTH();
2541 exc = stack_pointer[-5];
2542 tb = stack_pointer[-6];
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002543 exit_func = stack_pointer[-7];
Benjamin Peterson176101d2009-06-28 15:40:50 +00002544 stack_pointer[-7] = tb;
2545 stack_pointer[-6] = exc;
2546 stack_pointer[-5] = tp;
2547 FOURTH() = NULL;
2548 block = &f->f_blockstack[f->f_iblock - 1];
2549 assert(block->b_type == EXCEPT_HANDLER);
2550 block->b_level--;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002551 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002552 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002553 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2554 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002555 Py_DECREF(exit_func);
2556 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002557 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002558
2559 if (u != Py_None)
2560 err = PyObject_IsTrue(x);
2561 else
2562 err = 0;
2563 Py_DECREF(x);
2564
2565 if (err < 0)
2566 break; /* Go to error exit */
2567 else if (err > 0) {
2568 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002569 /* There was an exception and a True return */
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002570 PUSH(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002571 }
Christian Heimes180510d2008-03-03 19:15:45 +00002572 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002573 break;
2574 }
2575
Antoine Pitroub52ec782009-01-25 16:34:23 +00002576 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002577 {
2578 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002579 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002580 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002581#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002582 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002583#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002584 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002585#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002586 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002587 PUSH(x);
2588 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002589 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002590 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002591 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002592
Antoine Pitroub52ec782009-01-25 16:34:23 +00002593 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2594 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2595 TARGET(CALL_FUNCTION_VAR_KW)
2596 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002597 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002598 int na = oparg & 0xff;
2599 int nk = (oparg>>8) & 0xff;
2600 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002601 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002602 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002603 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002604 if (flags & CALL_FLAG_VAR)
2605 n++;
2606 if (flags & CALL_FLAG_KW)
2607 n++;
2608 pfunc = stack_pointer - n - 1;
2609 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002610
Guido van Rossumac7be682001-01-17 15:42:30 +00002611 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002612 && PyMethod_GET_SELF(func) != NULL) {
2613 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002614 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002615 func = PyMethod_GET_FUNCTION(func);
2616 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002617 Py_DECREF(*pfunc);
2618 *pfunc = self;
2619 na++;
2620 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002621 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002622 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002623 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002624 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002625 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002626 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002627 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002628 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002629
Jeremy Hylton76901512000-03-28 23:49:17 +00002630 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002631 w = POP();
2632 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002633 }
2634 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002635 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002636 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002637 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002638 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002639
Antoine Pitroub52ec782009-01-25 16:34:23 +00002640 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2641 TARGET(MAKE_FUNCTION)
2642 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002643 {
2644 int posdefaults = oparg & 0xff;
2645 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002646 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002647
Guido van Rossum681d79a1995-07-18 14:51:37 +00002648 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002649 x = PyFunction_New(v, f->f_globals);
2650 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002651
Guido van Rossum0240b922007-02-26 21:23:50 +00002652 if (x != NULL && opcode == MAKE_CLOSURE) {
2653 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002654 if (PyFunction_SetClosure(x, v) != 0) {
2655 /* Can't happen unless bytecode is corrupt. */
2656 why = WHY_EXCEPTION;
2657 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002658 Py_DECREF(v);
2659 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002660
2661 if (x != NULL && num_annotations > 0) {
2662 Py_ssize_t name_ix;
2663 u = POP(); /* names of args with annotations */
2664 v = PyDict_New();
2665 if (v == NULL) {
2666 Py_DECREF(x);
2667 x = NULL;
2668 break;
2669 }
2670 name_ix = PyTuple_Size(u);
2671 assert(num_annotations == name_ix+1);
2672 while (name_ix > 0) {
2673 --name_ix;
2674 t = PyTuple_GET_ITEM(u, name_ix);
2675 w = POP();
2676 /* XXX(nnorwitz): check for errors */
2677 PyDict_SetItem(v, t, w);
2678 Py_DECREF(w);
2679 }
2680
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002681 if (PyFunction_SetAnnotations(x, v) != 0) {
2682 /* Can't happen unless
2683 PyFunction_SetAnnotations changes. */
2684 why = WHY_EXCEPTION;
2685 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002686 Py_DECREF(v);
2687 Py_DECREF(u);
2688 }
2689
Guido van Rossum681d79a1995-07-18 14:51:37 +00002690 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002691 if (x != NULL && posdefaults > 0) {
2692 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002693 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002694 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002695 x = NULL;
2696 break;
2697 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002698 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002699 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002700 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002701 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002702 if (PyFunction_SetDefaults(x, v) != 0) {
2703 /* Can't happen unless
2704 PyFunction_SetDefaults changes. */
2705 why = WHY_EXCEPTION;
2706 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002707 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002708 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002709 if (x != NULL && kwdefaults > 0) {
2710 v = PyDict_New();
2711 if (v == NULL) {
2712 Py_DECREF(x);
2713 x = NULL;
2714 break;
2715 }
2716 while (--kwdefaults >= 0) {
2717 w = POP(); /* default value */
2718 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002719 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002720 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002721 Py_DECREF(w);
2722 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002723 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002724 if (PyFunction_SetKwDefaults(x, v) != 0) {
2725 /* Can't happen unless
2726 PyFunction_SetKwDefaults changes. */
2727 why = WHY_EXCEPTION;
2728 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002729 Py_DECREF(v);
2730 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002731 PUSH(x);
2732 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002733 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002734
Antoine Pitroub52ec782009-01-25 16:34:23 +00002735 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002736 if (oparg == 3)
2737 w = POP();
2738 else
2739 w = NULL;
2740 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002741 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002742 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002743 Py_DECREF(u);
2744 Py_DECREF(v);
2745 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002746 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002747 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002748 break;
2749
Antoine Pitroub52ec782009-01-25 16:34:23 +00002750 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002751 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002752 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002753 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002754
Antoine Pitroub52ec782009-01-25 16:34:23 +00002755#ifdef USE_COMPUTED_GOTOS
2756 _unknown_opcode:
2757#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002758 default:
2759 fprintf(stderr,
2760 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002761 PyCode_Addr2Line(f->f_code, f->f_lasti),
2762 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002763 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002764 why = WHY_EXCEPTION;
2765 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002766
2767#ifdef CASE_TOO_BIG
2768 }
2769#endif
2770
Guido van Rossum374a9221991-04-04 10:40:29 +00002771 } /* switch */
2772
2773 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002774
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002775 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002776
Guido van Rossum374a9221991-04-04 10:40:29 +00002777 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002778
Guido van Rossum374a9221991-04-04 10:40:29 +00002779 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002780 if (err == 0 && x != NULL) {
2781#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002782 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002783 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002784 fprintf(stderr,
2785 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002786 else {
2787#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002788 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002789 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002790#ifdef CHECKEXC
2791 }
2792#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002793 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002794 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002795 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002796 err = 0;
2797 }
2798
Guido van Rossum374a9221991-04-04 10:40:29 +00002799 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002800
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002801 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002802 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002803 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002804 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002805 why = WHY_EXCEPTION;
2806 }
2807 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002808#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002809 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002810 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002811 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002812 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002813 sprintf(buf, "Stack unwind with exception "
2814 "set and why=%d", why);
2815 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002816 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002817 }
2818#endif
2819
2820 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002821
Guido van Rossum374a9221991-04-04 10:40:29 +00002822 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002823 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002824
Fred Drake8f51f542001-10-04 14:48:42 +00002825 if (tstate->c_tracefunc != NULL)
2826 call_exc_trace(tstate->c_tracefunc,
2827 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002828 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002829
Guido van Rossum374a9221991-04-04 10:40:29 +00002830 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002831
Guido van Rossum374a9221991-04-04 10:40:29 +00002832 if (why == WHY_RERAISE)
2833 why = WHY_EXCEPTION;
2834
2835 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002836
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002837fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002838 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002839 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002840
Tim Peters8a5c3c72004-04-05 19:36:21 +00002841 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002842 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2843 /* For a continue inside a try block,
2844 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002845 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2846 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002847 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002848 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002849 Py_DECREF(retval);
2850 break;
2851 }
2852
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002853 if (b->b_type == EXCEPT_HANDLER) {
2854 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002855 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002856 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002857 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002858 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2859 why = WHY_NOT;
2860 JUMPTO(b->b_handler);
2861 break;
2862 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002863 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2864 || b->b_type == SETUP_FINALLY)) {
2865 PyObject *exc, *val, *tb;
2866 int handler = b->b_handler;
2867 /* Beware, this invalidates all b->b_* fields */
2868 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2869 PUSH(tstate->exc_traceback);
2870 PUSH(tstate->exc_value);
2871 if (tstate->exc_type != NULL) {
2872 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002873 }
2874 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002875 Py_INCREF(Py_None);
2876 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002877 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002878 PyErr_Fetch(&exc, &val, &tb);
2879 /* Make the raw exception data
2880 available to the handler,
2881 so a program can emulate the
2882 Python main loop. */
2883 PyErr_NormalizeException(
2884 &exc, &val, &tb);
2885 PyException_SetTraceback(val, tb);
2886 Py_INCREF(exc);
2887 tstate->exc_type = exc;
2888 Py_INCREF(val);
2889 tstate->exc_value = val;
2890 tstate->exc_traceback = tb;
2891 if (tb == NULL)
2892 tb = Py_None;
2893 Py_INCREF(tb);
2894 PUSH(tb);
2895 PUSH(val);
2896 PUSH(exc);
2897 why = WHY_NOT;
2898 JUMPTO(handler);
2899 break;
2900 }
2901 if (b->b_type == SETUP_FINALLY) {
2902 if (why & (WHY_RETURN | WHY_CONTINUE))
2903 PUSH(retval);
2904 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002905 why = WHY_NOT;
2906 JUMPTO(b->b_handler);
2907 break;
2908 }
2909 } /* unwind stack */
2910
2911 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002912
Guido van Rossum374a9221991-04-04 10:40:29 +00002913 if (why != WHY_NOT)
2914 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002915 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002916
Guido van Rossum374a9221991-04-04 10:40:29 +00002917 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002918
Tim Peters8a5c3c72004-04-05 19:36:21 +00002919 assert(why != WHY_YIELD);
2920 /* Pop remaining stack entries. */
2921 while (!EMPTY()) {
2922 v = POP();
2923 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002924 }
2925
Tim Peters8a5c3c72004-04-05 19:36:21 +00002926 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002927 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002928
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002929fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002930 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002931 if (tstate->c_tracefunc) {
2932 if (why == WHY_RETURN || why == WHY_YIELD) {
2933 if (call_trace(tstate->c_tracefunc,
2934 tstate->c_traceobj, f,
2935 PyTrace_RETURN, retval)) {
2936 Py_XDECREF(retval);
2937 retval = NULL;
2938 why = WHY_EXCEPTION;
2939 }
2940 }
2941 else if (why == WHY_EXCEPTION) {
2942 call_trace_protected(tstate->c_tracefunc,
2943 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002944 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002945 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002946 }
Fred Drake8f51f542001-10-04 14:48:42 +00002947 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002948 if (why == WHY_EXCEPTION)
2949 call_trace_protected(tstate->c_profilefunc,
2950 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002951 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002952 else if (call_trace(tstate->c_profilefunc,
2953 tstate->c_profileobj, f,
2954 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002955 Py_XDECREF(retval);
2956 retval = NULL;
2957 why = WHY_EXCEPTION;
2958 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002959 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002960 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002961
Tim Peters5ca576e2001-06-18 22:08:13 +00002962 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002963exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002964 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002965 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002966
Guido van Rossum96a42c81992-01-12 02:29:51 +00002967 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002968}
2969
Guido van Rossumc2e20742006-02-27 22:32:47 +00002970/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002971 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002972 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002973
Tim Peters6d6c1a32001-08-02 04:15:00 +00002974PyObject *
2975PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002976 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002977 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002978{
2979 register PyFrameObject *f;
2980 register PyObject *retval = NULL;
2981 register PyObject **fastlocals, **freevars;
2982 PyThreadState *tstate = PyThreadState_GET();
2983 PyObject *x, *u;
2984
2985 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002986 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002987 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002988 return NULL;
2989 }
2990
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002991 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002992 assert(globals != NULL);
2993 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002994 if (f == NULL)
2995 return NULL;
2996
2997 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002998 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002999
3000 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00003001 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00003002 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3003 int i;
3004 int n = argcount;
3005 PyObject *kwdict = NULL;
3006 if (co->co_flags & CO_VARKEYWORDS) {
3007 kwdict = PyDict_New();
3008 if (kwdict == NULL)
3009 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003010 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003011 if (co->co_flags & CO_VARARGS)
3012 i++;
3013 SETLOCAL(i, kwdict);
3014 }
3015 if (argcount > co->co_argcount) {
3016 if (!(co->co_flags & CO_VARARGS)) {
3017 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003018 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003019 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003020 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003021 defcount ? "at most" : "exactly",
3022 co->co_argcount,
3023 kwcount ? "non-keyword " : "",
3024 co->co_argcount == 1 ? "" : "s",
3025 argcount);
3026 goto fail;
3027 }
3028 n = co->co_argcount;
3029 }
3030 for (i = 0; i < n; i++) {
3031 x = args[i];
3032 Py_INCREF(x);
3033 SETLOCAL(i, x);
3034 }
3035 if (co->co_flags & CO_VARARGS) {
3036 u = PyTuple_New(argcount - n);
3037 if (u == NULL)
3038 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003039 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00003040 for (i = n; i < argcount; i++) {
3041 x = args[i];
3042 Py_INCREF(x);
3043 PyTuple_SET_ITEM(u, i-n, x);
3044 }
3045 }
3046 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003047 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003048 PyObject *keyword = kws[2*i];
3049 PyObject *value = kws[2*i + 1];
3050 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00003051 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003052 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003053 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003054 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00003055 goto fail;
3056 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003057 /* Speed hack: do raw pointer compares. As names are
3058 normally interned this should almost always hit. */
3059 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003060 for (j = 0;
3061 j < co->co_argcount + co->co_kwonlyargcount;
3062 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003063 PyObject *nm = co_varnames[j];
3064 if (nm == keyword)
3065 goto kw_found;
3066 }
3067 /* Slow fallback, just in case */
3068 for (j = 0;
3069 j < co->co_argcount + co->co_kwonlyargcount;
3070 j++) {
3071 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003072 int cmp = PyObject_RichCompareBool(
3073 keyword, nm, Py_EQ);
3074 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003075 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003076 else if (cmp < 0)
3077 goto fail;
3078 }
3079 /* Check errors from Compare */
3080 if (PyErr_Occurred())
3081 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003082 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003083 if (kwdict == NULL) {
3084 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003085 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00003086 "keyword argument '%S'",
3087 co->co_name,
3088 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00003089 goto fail;
3090 }
3091 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003092 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003093 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003094kw_found:
3095 if (GETLOCAL(j) != NULL) {
3096 PyErr_Format(PyExc_TypeError,
3097 "%U() got multiple "
3098 "values for keyword "
3099 "argument '%S'",
3100 co->co_name,
3101 keyword);
3102 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003103 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003104 Py_INCREF(value);
3105 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003106 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003107 if (co->co_kwonlyargcount > 0) {
3108 for (i = co->co_argcount;
3109 i < co->co_argcount + co->co_kwonlyargcount;
3110 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003111 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003112 if (GETLOCAL(i) != NULL)
3113 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003114 name = PyTuple_GET_ITEM(co->co_varnames, i);
3115 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003116 if (kwdefs != NULL)
3117 def = PyDict_GetItem(kwdefs, name);
3118 if (def != NULL) {
3119 Py_INCREF(def);
3120 SETLOCAL(i, def);
3121 continue;
3122 }
3123 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003124 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003125 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003126 goto fail;
3127 }
3128 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003129 if (argcount < co->co_argcount) {
3130 int m = co->co_argcount - defcount;
3131 for (i = argcount; i < m; i++) {
3132 if (GETLOCAL(i) == NULL) {
3133 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003134 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003135 "%spositional argument%s "
3136 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003137 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003138 ((co->co_flags & CO_VARARGS) ||
3139 defcount) ? "at least"
3140 : "exactly",
3141 m, kwcount ? "non-keyword " : "",
3142 m == 1 ? "" : "s", i);
3143 goto fail;
3144 }
3145 }
3146 if (n > m)
3147 i = n - m;
3148 else
3149 i = 0;
3150 for (; i < defcount; i++) {
3151 if (GETLOCAL(m+i) == NULL) {
3152 PyObject *def = defs[i];
3153 Py_INCREF(def);
3154 SETLOCAL(m+i, def);
3155 }
3156 }
3157 }
3158 }
3159 else {
3160 if (argcount > 0 || kwcount > 0) {
3161 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003162 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003163 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003164 argcount + kwcount);
3165 goto fail;
3166 }
3167 }
3168 /* Allocate and initialize storage for cell vars, and copy free
3169 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003170 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003171 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003172 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003173 PyObject *c;
3174
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003175 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003176 if (co->co_flags & CO_VARARGS)
3177 nargs++;
3178 if (co->co_flags & CO_VARKEYWORDS)
3179 nargs++;
3180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 /* Initialize each cell var, taking into account
3182 cell vars that are initialized from arguments.
3183
3184 Should arrange for the compiler to put cellvars
3185 that are arguments at the beginning of the cellvars
3186 list so that we can march over it more efficiently?
3187 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003188 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003189 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003190 PyTuple_GET_ITEM(co->co_cellvars, i));
3191 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003193 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003194 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003195 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003196 c = PyCell_New(GETLOCAL(j));
3197 if (c == NULL)
3198 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003199 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003200 found = 1;
3201 break;
3202 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003203 }
3204 if (found == 0) {
3205 c = PyCell_New(NULL);
3206 if (c == NULL)
3207 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003208 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003209 }
3210 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003211 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003212 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003213 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003214 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003215 PyObject *o = PyTuple_GET_ITEM(closure, i);
3216 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003217 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003218 }
3219 }
3220
Tim Peters5ca576e2001-06-18 22:08:13 +00003221 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003222 /* Don't need to keep the reference to f_back, it will be set
3223 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003224 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003225 f->f_back = NULL;
3226
Jeremy Hylton985eba52003-02-05 23:13:00 +00003227 PCALL(PCALL_GENERATOR);
3228
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003229 /* Create a new generator that owns the ready to run frame
3230 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003231 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003232 }
3233
Thomas Woutersce272b62007-09-19 21:19:28 +00003234 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003235
Thomas Woutersce272b62007-09-19 21:19:28 +00003236fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003237
Tim Petersb13680b2001-11-27 23:29:29 +00003238 /* decref'ing the frame can cause __del__ methods to get invoked,
3239 which can call back into Python. While we're done with the
3240 current Python frame (f), the associated C stack is still in use,
3241 so recursion_depth must be boosted for the duration.
3242 */
3243 assert(tstate != NULL);
3244 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003245 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003246 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003247 return retval;
3248}
3249
3250
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003251static PyObject *
3252special_lookup(PyObject *o, char *meth, PyObject **cache)
3253{
3254 PyObject *res;
3255 res = _PyObject_LookupSpecial(o, meth, cache);
3256 if (res == NULL && !PyErr_Occurred()) {
3257 PyErr_SetObject(PyExc_AttributeError, *cache);
3258 return NULL;
3259 }
3260 return res;
3261}
3262
3263
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003264/* Logic for the raise statement (too complicated for inlining).
3265 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003266static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003267do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003268{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003269 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003270
3271 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003272 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003273 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003274 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003275 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003276 value = tstate->exc_value;
3277 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003278 if (type == Py_None) {
3279 PyErr_SetString(PyExc_RuntimeError,
3280 "No active exception to reraise");
3281 return WHY_EXCEPTION;
3282 }
3283 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003284 Py_XINCREF(value);
3285 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003286 PyErr_Restore(type, value, tb);
3287 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003288 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003289
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003290 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003291 raise
3292 raise <instance>
3293 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003294
Collin Winter828f04a2007-08-31 00:04:24 +00003295 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003296 type = exc;
3297 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003298 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003299 goto raise_error;
3300 }
Collin Winter828f04a2007-08-31 00:04:24 +00003301 else if (PyExceptionInstance_Check(exc)) {
3302 value = exc;
3303 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003304 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003305 }
3306 else {
3307 /* Not something you can raise. You get an exception
3308 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003309 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003310 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003311 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003312 goto raise_error;
3313 }
Collin Winter828f04a2007-08-31 00:04:24 +00003314
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003315 if (cause) {
3316 PyObject *fixed_cause;
3317 if (PyExceptionClass_Check(cause)) {
3318 fixed_cause = PyObject_CallObject(cause, NULL);
3319 if (fixed_cause == NULL)
3320 goto raise_error;
3321 Py_DECREF(cause);
3322 }
3323 else if (PyExceptionInstance_Check(cause)) {
3324 fixed_cause = cause;
3325 }
3326 else {
3327 PyErr_SetString(PyExc_TypeError,
3328 "exception causes must derive from "
3329 "BaseException");
3330 goto raise_error;
3331 }
3332 PyException_SetCause(value, fixed_cause);
3333 }
Collin Winter828f04a2007-08-31 00:04:24 +00003334
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003335 PyErr_SetObject(type, value);
3336 /* PyErr_SetObject incref's its arguments */
3337 Py_XDECREF(value);
3338 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003339 return WHY_EXCEPTION;
3340
3341raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003342 Py_XDECREF(value);
3343 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003344 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003345 return WHY_EXCEPTION;
3346}
3347
Tim Petersd6d010b2001-06-21 02:49:55 +00003348/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003349 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003350
Guido van Rossum0368b722007-05-11 16:50:42 +00003351 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3352 with a variable target.
3353*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003354
Barry Warsawe42b18f1997-08-25 22:13:04 +00003355static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003356unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003357{
Guido van Rossum0368b722007-05-11 16:50:42 +00003358 int i = 0, j = 0;
3359 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003360 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003361 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003362 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003363
Tim Petersd6d010b2001-06-21 02:49:55 +00003364 assert(v != NULL);
3365
3366 it = PyObject_GetIter(v);
3367 if (it == NULL)
3368 goto Error;
3369
3370 for (; i < argcnt; i++) {
3371 w = PyIter_Next(it);
3372 if (w == NULL) {
3373 /* Iterator done, via error or exhaustion. */
3374 if (!PyErr_Occurred()) {
3375 PyErr_Format(PyExc_ValueError,
3376 "need more than %d value%s to unpack",
3377 i, i == 1 ? "" : "s");
3378 }
3379 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003380 }
3381 *--sp = w;
3382 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003383
Guido van Rossum0368b722007-05-11 16:50:42 +00003384 if (argcntafter == -1) {
3385 /* We better have exhausted the iterator now. */
3386 w = PyIter_Next(it);
3387 if (w == NULL) {
3388 if (PyErr_Occurred())
3389 goto Error;
3390 Py_DECREF(it);
3391 return 1;
3392 }
3393 Py_DECREF(w);
3394 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3395 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003396 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003397
3398 l = PySequence_List(it);
3399 if (l == NULL)
3400 goto Error;
3401 *--sp = l;
3402 i++;
3403
3404 ll = PyList_GET_SIZE(l);
3405 if (ll < argcntafter) {
3406 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3407 argcnt + ll);
3408 goto Error;
3409 }
3410
3411 /* Pop the "after-variable" args off the list. */
3412 for (j = argcntafter; j > 0; j--, i++) {
3413 *--sp = PyList_GET_ITEM(l, ll - j);
3414 }
3415 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003416 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003417 Py_DECREF(it);
3418 return 1;
3419
Tim Petersd6d010b2001-06-21 02:49:55 +00003420Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003421 for (; i > 0; i--, sp++)
3422 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003423 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003424 return 0;
3425}
3426
3427
Guido van Rossum96a42c81992-01-12 02:29:51 +00003428#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003429static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003430prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003431{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003432 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003433 if (PyObject_Print(v, stdout, 0) != 0)
3434 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003435 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003436 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003437}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003438#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003439
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003440static void
Fred Drake5755ce62001-06-27 19:19:46 +00003441call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003442{
Guido van Rossumb209a111997-04-29 18:18:01 +00003443 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003444 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003445 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003446 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003447 value = Py_None;
3448 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003449 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003450 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003451 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003452 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003453 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003454 }
Fred Drake5755ce62001-06-27 19:19:46 +00003455 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003456 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003457 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003458 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003459 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003460 Py_XDECREF(type);
3461 Py_XDECREF(value);
3462 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003463 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003464}
3465
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003466static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003467call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003468 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003469{
3470 PyObject *type, *value, *traceback;
3471 int err;
3472 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003473 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003474 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003475 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003476 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003477 return 0;
3478 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003479 else {
3480 Py_XDECREF(type);
3481 Py_XDECREF(value);
3482 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003483 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003484 }
3485}
3486
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003487static int
Fred Drake5755ce62001-06-27 19:19:46 +00003488call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3489 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003490{
Fred Drake5755ce62001-06-27 19:19:46 +00003491 register PyThreadState *tstate = frame->f_tstate;
3492 int result;
3493 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003494 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003495 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003496 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003497 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003498 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3499 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003500 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003501 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003502}
3503
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003504PyObject *
3505_PyEval_CallTracing(PyObject *func, PyObject *args)
3506{
3507 PyFrameObject *frame = PyEval_GetFrame();
3508 PyThreadState *tstate = frame->f_tstate;
3509 int save_tracing = tstate->tracing;
3510 int save_use_tracing = tstate->use_tracing;
3511 PyObject *result;
3512
3513 tstate->tracing = 0;
3514 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3515 || (tstate->c_profilefunc != NULL));
3516 result = PyObject_Call(func, args, NULL);
3517 tstate->tracing = save_tracing;
3518 tstate->use_tracing = save_use_tracing;
3519 return result;
3520}
3521
Michael W. Hudson006c7522002-11-08 13:08:46 +00003522static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003523maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003524 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3525 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003526{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003527 int result = 0;
3528
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003529 /* If the last instruction executed isn't in the current
3530 instruction window, reset the window. If the last
3531 instruction happens to fall at the start of a line or if it
3532 represents a jump backwards, call the trace function.
3533 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003534 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003535 int line;
3536 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003537
Thomas Woutersce272b62007-09-19 21:19:28 +00003538 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3539 &bounds);
3540 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003541 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003542 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003543 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003544 }
3545 *instr_lb = bounds.ap_lower;
3546 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003547 }
Armin Rigobf57a142004-03-22 19:24:58 +00003548 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003549 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003550 }
3551 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003552 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003553}
3554
Fred Drake5755ce62001-06-27 19:19:46 +00003555void
3556PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003557{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003558 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003559 PyObject *temp = tstate->c_profileobj;
3560 Py_XINCREF(arg);
3561 tstate->c_profilefunc = NULL;
3562 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003563 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003564 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003565 Py_XDECREF(temp);
3566 tstate->c_profilefunc = func;
3567 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003568 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003569 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003570}
3571
3572void
3573PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3574{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003575 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003576 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003577 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003578 Py_XINCREF(arg);
3579 tstate->c_tracefunc = NULL;
3580 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003581 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003582 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003583 Py_XDECREF(temp);
3584 tstate->c_tracefunc = func;
3585 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003586 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003587 tstate->use_tracing = ((func != NULL)
3588 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003589}
3590
Guido van Rossumb209a111997-04-29 18:18:01 +00003591PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003592PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003593{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003594 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003595 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003596 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003597 else
3598 return current_frame->f_builtins;
3599}
3600
Guido van Rossumb209a111997-04-29 18:18:01 +00003601PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003602PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003603{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003604 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003605 if (current_frame == NULL)
3606 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003607 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003608 return current_frame->f_locals;
3609}
3610
Guido van Rossumb209a111997-04-29 18:18:01 +00003611PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003612PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003613{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003614 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003615 if (current_frame == NULL)
3616 return NULL;
3617 else
3618 return current_frame->f_globals;
3619}
3620
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003621PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003622PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003623{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003624 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003625 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003626}
3627
Guido van Rossum6135a871995-01-09 17:53:26 +00003628int
Tim Peters5ba58662001-07-16 02:29:45 +00003629PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003630{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003631 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003632 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003633
3634 if (current_frame != NULL) {
3635 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003636 const int compilerflags = codeflags & PyCF_MASK;
3637 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003638 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003639 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003640 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003641#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003642 if (codeflags & CO_GENERATOR_ALLOWED) {
3643 result = 1;
3644 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3645 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003646#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003647 }
3648 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003649}
3650
Guido van Rossum3f5da241990-12-20 15:06:42 +00003651
Guido van Rossum681d79a1995-07-18 14:51:37 +00003652/* External interface to call any callable object.
3653 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003654
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003655#undef PyEval_CallObject
3656/* for backward compatibility: export this interface */
3657
Guido van Rossumb209a111997-04-29 18:18:01 +00003658PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003659PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003660{
Guido van Rossumb209a111997-04-29 18:18:01 +00003661 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003662}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003663#define PyEval_CallObject(func,arg) \
3664 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003665
Guido van Rossumb209a111997-04-29 18:18:01 +00003666PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003667PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003668{
Jeremy Hylton52820442001-01-03 23:52:36 +00003669 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003670
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003671 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003672 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003673 if (arg == NULL)
3674 return NULL;
3675 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003676 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003677 PyErr_SetString(PyExc_TypeError,
3678 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003679 return NULL;
3680 }
3681 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003682 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003683
Guido van Rossumb209a111997-04-29 18:18:01 +00003684 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003685 PyErr_SetString(PyExc_TypeError,
3686 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003687 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003688 return NULL;
3689 }
3690
Tim Peters6d6c1a32001-08-02 04:15:00 +00003691 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003692 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003693 return result;
3694}
3695
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003696const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003697PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003698{
3699 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003700 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003701 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003702 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003703 else if (PyCFunction_Check(func))
3704 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003705 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003706 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003707}
3708
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003709const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003710PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003711{
3712 if (PyMethod_Check(func))
3713 return "()";
3714 else if (PyFunction_Check(func))
3715 return "()";
3716 else if (PyCFunction_Check(func))
3717 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003718 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003719 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003720}
3721
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003722static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003723err_args(PyObject *func, int flags, int nargs)
3724{
3725 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003726 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003727 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003728 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003729 nargs);
3730 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003731 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003732 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003733 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003734 nargs);
3735}
3736
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003737#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003738if (tstate->use_tracing && tstate->c_profilefunc) { \
3739 if (call_trace(tstate->c_profilefunc, \
3740 tstate->c_profileobj, \
3741 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003742 func)) { \
3743 x = NULL; \
3744 } \
3745 else { \
3746 x = call; \
3747 if (tstate->c_profilefunc != NULL) { \
3748 if (x == NULL) { \
3749 call_trace_protected(tstate->c_profilefunc, \
3750 tstate->c_profileobj, \
3751 tstate->frame, PyTrace_C_EXCEPTION, \
3752 func); \
3753 /* XXX should pass (type, value, tb) */ \
3754 } else { \
3755 if (call_trace(tstate->c_profilefunc, \
3756 tstate->c_profileobj, \
3757 tstate->frame, PyTrace_C_RETURN, \
3758 func)) { \
3759 Py_DECREF(x); \
3760 x = NULL; \
3761 } \
3762 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003763 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003764 } \
3765} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003766 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003767 }
3768
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003769static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003770call_function(PyObject ***pp_stack, int oparg
3771#ifdef WITH_TSC
3772 , uint64* pintr0, uint64* pintr1
3773#endif
3774 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003775{
3776 int na = oparg & 0xff;
3777 int nk = (oparg>>8) & 0xff;
3778 int n = na + 2 * nk;
3779 PyObject **pfunc = (*pp_stack) - n - 1;
3780 PyObject *func = *pfunc;
3781 PyObject *x, *w;
3782
Jeremy Hylton985eba52003-02-05 23:13:00 +00003783 /* Always dispatch PyCFunction first, because these are
3784 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003785 */
3786 if (PyCFunction_Check(func) && nk == 0) {
3787 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003788 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003789
3790 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003791 if (flags & (METH_NOARGS | METH_O)) {
3792 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3793 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003794 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003795 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003796 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003797 else if (flags & METH_O && na == 1) {
3798 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003799 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003800 Py_DECREF(arg);
3801 }
3802 else {
3803 err_args(func, flags, na);
3804 x = NULL;
3805 }
3806 }
3807 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003808 PyObject *callargs;
3809 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003810 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003811 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003812 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003813 Py_XDECREF(callargs);
3814 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003815 } else {
3816 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3817 /* optimize access to bound methods */
3818 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003819 PCALL(PCALL_METHOD);
3820 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003821 Py_INCREF(self);
3822 func = PyMethod_GET_FUNCTION(func);
3823 Py_INCREF(func);
3824 Py_DECREF(*pfunc);
3825 *pfunc = self;
3826 na++;
3827 n++;
3828 } else
3829 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003830 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003831 if (PyFunction_Check(func))
3832 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003833 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003834 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003835 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003836 Py_DECREF(func);
3837 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003838
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003839 /* Clear the stack of the function object. Also removes
3840 the arguments in case they weren't consumed already
3841 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003842 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003843 while ((*pp_stack) > pfunc) {
3844 w = EXT_POP(*pp_stack);
3845 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003846 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003847 }
3848 return x;
3849}
3850
Jeremy Hylton192690e2002-08-16 18:36:11 +00003851/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003852 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003853 For the simplest case -- a function that takes only positional
3854 arguments and is called with only positional arguments -- it
3855 inlines the most primitive frame setup code from
3856 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3857 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003858*/
3859
3860static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003861fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003862{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003863 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003864 PyObject *globals = PyFunction_GET_GLOBALS(func);
3865 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003866 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003867 PyObject **d = NULL;
3868 int nd = 0;
3869
Jeremy Hylton985eba52003-02-05 23:13:00 +00003870 PCALL(PCALL_FUNCTION);
3871 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003872 if (argdefs == NULL && co->co_argcount == n &&
3873 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003874 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3875 PyFrameObject *f;
3876 PyObject *retval = NULL;
3877 PyThreadState *tstate = PyThreadState_GET();
3878 PyObject **fastlocals, **stack;
3879 int i;
3880
3881 PCALL(PCALL_FASTER_FUNCTION);
3882 assert(globals != NULL);
3883 /* XXX Perhaps we should create a specialized
3884 PyFrame_New() that doesn't take locals, but does
3885 take builtins without sanity checking them.
3886 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003887 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003888 f = PyFrame_New(tstate, co, globals, NULL);
3889 if (f == NULL)
3890 return NULL;
3891
3892 fastlocals = f->f_localsplus;
3893 stack = (*pp_stack) - n;
3894
3895 for (i = 0; i < n; i++) {
3896 Py_INCREF(*stack);
3897 fastlocals[i] = *stack++;
3898 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003899 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003900 ++tstate->recursion_depth;
3901 Py_DECREF(f);
3902 --tstate->recursion_depth;
3903 return retval;
3904 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003905 if (argdefs != NULL) {
3906 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003907 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003908 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003909 return PyEval_EvalCodeEx(co, globals,
3910 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003911 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003912 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003913}
3914
3915static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003916update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3917 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003918{
3919 PyObject *kwdict = NULL;
3920 if (orig_kwdict == NULL)
3921 kwdict = PyDict_New();
3922 else {
3923 kwdict = PyDict_Copy(orig_kwdict);
3924 Py_DECREF(orig_kwdict);
3925 }
3926 if (kwdict == NULL)
3927 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003928 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003929 int err;
3930 PyObject *value = EXT_POP(*pp_stack);
3931 PyObject *key = EXT_POP(*pp_stack);
3932 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003933 PyErr_Format(PyExc_TypeError,
3934 "%.200s%s got multiple values "
3935 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003936 PyEval_GetFuncName(func),
3937 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003938 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003939 Py_DECREF(key);
3940 Py_DECREF(value);
3941 Py_DECREF(kwdict);
3942 return NULL;
3943 }
3944 err = PyDict_SetItem(kwdict, key, value);
3945 Py_DECREF(key);
3946 Py_DECREF(value);
3947 if (err) {
3948 Py_DECREF(kwdict);
3949 return NULL;
3950 }
3951 }
3952 return kwdict;
3953}
3954
3955static PyObject *
3956update_star_args(int nstack, int nstar, PyObject *stararg,
3957 PyObject ***pp_stack)
3958{
3959 PyObject *callargs, *w;
3960
3961 callargs = PyTuple_New(nstack + nstar);
3962 if (callargs == NULL) {
3963 return NULL;
3964 }
3965 if (nstar) {
3966 int i;
3967 for (i = 0; i < nstar; i++) {
3968 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3969 Py_INCREF(a);
3970 PyTuple_SET_ITEM(callargs, nstack + i, a);
3971 }
3972 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003973 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003974 w = EXT_POP(*pp_stack);
3975 PyTuple_SET_ITEM(callargs, nstack, w);
3976 }
3977 return callargs;
3978}
3979
3980static PyObject *
3981load_args(PyObject ***pp_stack, int na)
3982{
3983 PyObject *args = PyTuple_New(na);
3984 PyObject *w;
3985
3986 if (args == NULL)
3987 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003988 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003989 w = EXT_POP(*pp_stack);
3990 PyTuple_SET_ITEM(args, na, w);
3991 }
3992 return args;
3993}
3994
3995static PyObject *
3996do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3997{
3998 PyObject *callargs = NULL;
3999 PyObject *kwdict = NULL;
4000 PyObject *result = NULL;
4001
4002 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004003 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004004 if (kwdict == NULL)
4005 goto call_fail;
4006 }
4007 callargs = load_args(pp_stack, na);
4008 if (callargs == NULL)
4009 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004010#ifdef CALL_PROFILE
4011 /* At this point, we have to look at the type of func to
4012 update the call stats properly. Do it here so as to avoid
4013 exposing the call stats machinery outside ceval.c
4014 */
4015 if (PyFunction_Check(func))
4016 PCALL(PCALL_FUNCTION);
4017 else if (PyMethod_Check(func))
4018 PCALL(PCALL_METHOD);
4019 else if (PyType_Check(func))
4020 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004021 else if (PyCFunction_Check(func))
4022 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004023 else
4024 PCALL(PCALL_OTHER);
4025#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004026 if (PyCFunction_Check(func)) {
4027 PyThreadState *tstate = PyThreadState_GET();
4028 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4029 }
4030 else
4031 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004032call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004033 Py_XDECREF(callargs);
4034 Py_XDECREF(kwdict);
4035 return result;
4036}
4037
4038static PyObject *
4039ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4040{
4041 int nstar = 0;
4042 PyObject *callargs = NULL;
4043 PyObject *stararg = NULL;
4044 PyObject *kwdict = NULL;
4045 PyObject *result = NULL;
4046
4047 if (flags & CALL_FLAG_KW) {
4048 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004049 if (!PyDict_Check(kwdict)) {
4050 PyObject *d;
4051 d = PyDict_New();
4052 if (d == NULL)
4053 goto ext_call_fail;
4054 if (PyDict_Update(d, kwdict) != 0) {
4055 Py_DECREF(d);
4056 /* PyDict_Update raises attribute
4057 * error (percolated from an attempt
4058 * to get 'keys' attribute) instead of
4059 * a type error if its second argument
4060 * is not a mapping.
4061 */
4062 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4063 PyErr_Format(PyExc_TypeError,
4064 "%.200s%.200s argument after ** "
4065 "must be a mapping, not %.200s",
4066 PyEval_GetFuncName(func),
4067 PyEval_GetFuncDesc(func),
4068 kwdict->ob_type->tp_name);
4069 }
4070 goto ext_call_fail;
4071 }
4072 Py_DECREF(kwdict);
4073 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004074 }
4075 }
4076 if (flags & CALL_FLAG_VAR) {
4077 stararg = EXT_POP(*pp_stack);
4078 if (!PyTuple_Check(stararg)) {
4079 PyObject *t = NULL;
4080 t = PySequence_Tuple(stararg);
4081 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004082 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4083 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004084 "%.200s%.200s argument after * "
4085 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004086 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004087 PyEval_GetFuncDesc(func),
4088 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004089 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004090 goto ext_call_fail;
4091 }
4092 Py_DECREF(stararg);
4093 stararg = t;
4094 }
4095 nstar = PyTuple_GET_SIZE(stararg);
4096 }
4097 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004098 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004099 if (kwdict == NULL)
4100 goto ext_call_fail;
4101 }
4102 callargs = update_star_args(na, nstar, stararg, pp_stack);
4103 if (callargs == NULL)
4104 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004105#ifdef CALL_PROFILE
4106 /* At this point, we have to look at the type of func to
4107 update the call stats properly. Do it here so as to avoid
4108 exposing the call stats machinery outside ceval.c
4109 */
4110 if (PyFunction_Check(func))
4111 PCALL(PCALL_FUNCTION);
4112 else if (PyMethod_Check(func))
4113 PCALL(PCALL_METHOD);
4114 else if (PyType_Check(func))
4115 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004116 else if (PyCFunction_Check(func))
4117 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004118 else
4119 PCALL(PCALL_OTHER);
4120#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004121 if (PyCFunction_Check(func)) {
4122 PyThreadState *tstate = PyThreadState_GET();
4123 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4124 }
4125 else
4126 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004127ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004128 Py_XDECREF(callargs);
4129 Py_XDECREF(kwdict);
4130 Py_XDECREF(stararg);
4131 return result;
4132}
4133
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004134/* Extract a slice index from a PyInt or PyLong or an object with the
4135 nb_index slot defined, and store in *pi.
4136 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4137 and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004138 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004139*/
Tim Petersb5196382001-12-16 19:44:20 +00004140/* Note: If v is NULL, return success without storing into *pi. This
4141 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4142 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004143*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004144int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004145_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004146{
Tim Petersb5196382001-12-16 19:44:20 +00004147 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004148 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004149 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004150 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004151 if (x == -1 && PyErr_Occurred())
4152 return 0;
4153 }
4154 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004155 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004156 "slice indices must be integers or "
4157 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004158 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004159 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004160 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004161 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004162 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004163}
4164
Guido van Rossum486364b2007-06-30 05:01:58 +00004165#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004166 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004167
Guido van Rossumb209a111997-04-29 18:18:01 +00004168static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004169cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004170{
Guido van Rossumac7be682001-01-17 15:42:30 +00004171 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004172 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004173 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004174 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004175 break;
4176 case PyCmp_IS_NOT:
4177 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004178 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004179 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004180 res = PySequence_Contains(w, v);
4181 if (res < 0)
4182 return NULL;
4183 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004184 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004185 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004186 if (res < 0)
4187 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004188 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004189 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004190 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004191 if (PyTuple_Check(w)) {
4192 Py_ssize_t i, length;
4193 length = PyTuple_Size(w);
4194 for (i = 0; i < length; i += 1) {
4195 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004196 if (!PyExceptionClass_Check(exc)) {
4197 PyErr_SetString(PyExc_TypeError,
4198 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004199 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004200 }
4201 }
4202 }
4203 else {
Brett Cannon39590462007-02-26 22:01:14 +00004204 if (!PyExceptionClass_Check(w)) {
4205 PyErr_SetString(PyExc_TypeError,
4206 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004207 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004208 }
4209 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004210 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004211 break;
4212 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004213 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004214 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004215 v = res ? Py_True : Py_False;
4216 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004217 return v;
4218}
4219
Thomas Wouters52152252000-08-17 22:55:00 +00004220static PyObject *
4221import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004222{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004223 PyObject *x;
4224
4225 x = PyObject_GetAttr(v, name);
4226 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004227 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004228 }
Thomas Wouters52152252000-08-17 22:55:00 +00004229 return x;
4230}
Guido van Rossumac7be682001-01-17 15:42:30 +00004231
Thomas Wouters52152252000-08-17 22:55:00 +00004232static int
4233import_all_from(PyObject *locals, PyObject *v)
4234{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004235 PyObject *all = PyObject_GetAttrString(v, "__all__");
4236 PyObject *dict, *name, *value;
4237 int skip_leading_underscores = 0;
4238 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004239
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004240 if (all == NULL) {
4241 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4242 return -1; /* Unexpected error */
4243 PyErr_Clear();
4244 dict = PyObject_GetAttrString(v, "__dict__");
4245 if (dict == NULL) {
4246 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4247 return -1;
4248 PyErr_SetString(PyExc_ImportError,
4249 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004250 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004251 }
4252 all = PyMapping_Keys(dict);
4253 Py_DECREF(dict);
4254 if (all == NULL)
4255 return -1;
4256 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004257 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004258
4259 for (pos = 0, err = 0; ; pos++) {
4260 name = PySequence_GetItem(all, pos);
4261 if (name == NULL) {
4262 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4263 err = -1;
4264 else
4265 PyErr_Clear();
4266 break;
4267 }
4268 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004269 PyUnicode_Check(name) &&
4270 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004271 {
4272 Py_DECREF(name);
4273 continue;
4274 }
4275 value = PyObject_GetAttr(v, name);
4276 if (value == NULL)
4277 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004278 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004279 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004280 else
4281 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004282 Py_DECREF(name);
4283 Py_XDECREF(value);
4284 if (err != 0)
4285 break;
4286 }
4287 Py_DECREF(all);
4288 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004289}
4290
Guido van Rossumac7be682001-01-17 15:42:30 +00004291static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004292format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004293{
Neal Norwitzda059e32007-08-26 05:33:45 +00004294 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004295
4296 if (!obj)
4297 return;
4298
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004299 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004300 if (!obj_str)
4301 return;
4302
4303 PyErr_Format(exc, format_str, obj_str);
4304}
Guido van Rossum950361c1997-01-24 13:49:28 +00004305
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004306static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004307unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004308 PyFrameObject *f, unsigned char *next_instr)
4309{
4310 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004311 are (Unicode) strings. */
4312 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4313 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004314 Py_ssize_t new_len = v_len + w_len;
4315 if (new_len < 0) {
4316 PyErr_SetString(PyExc_OverflowError,
4317 "strings are too large to concat");
4318 return NULL;
4319 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004320
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004321 if (v->ob_refcnt == 2) {
4322 /* In the common case, there are 2 references to the value
4323 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004324 * value stack (in 'v') and one still stored in the
4325 * 'variable'. We try to delete the variable now to reduce
4326 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004327 */
4328 switch (*next_instr) {
4329 case STORE_FAST:
4330 {
4331 int oparg = PEEKARG();
4332 PyObject **fastlocals = f->f_localsplus;
4333 if (GETLOCAL(oparg) == v)
4334 SETLOCAL(oparg, NULL);
4335 break;
4336 }
4337 case STORE_DEREF:
4338 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004339 PyObject **freevars = (f->f_localsplus +
4340 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004341 PyObject *c = freevars[PEEKARG()];
4342 if (PyCell_GET(c) == v)
4343 PyCell_Set(c, NULL);
4344 break;
4345 }
4346 case STORE_NAME:
4347 {
4348 PyObject *names = f->f_code->co_names;
4349 PyObject *name = GETITEM(names, PEEKARG());
4350 PyObject *locals = f->f_locals;
4351 if (PyDict_CheckExact(locals) &&
4352 PyDict_GetItem(locals, name) == v) {
4353 if (PyDict_DelItem(locals, name) != 0) {
4354 PyErr_Clear();
4355 }
4356 }
4357 break;
4358 }
4359 }
4360 }
4361
Guido van Rossum98297ee2007-11-06 21:34:58 +00004362 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004363 /* Now we own the last reference to 'v', so we can resize it
4364 * in-place.
4365 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004366 if (PyUnicode_Resize(&v, new_len) != 0) {
4367 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004368 * deallocated so it cannot be put back into
4369 * 'variable'. The MemoryError is raised when there
4370 * is no value in 'variable', which might (very
4371 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004372 */
4373 return NULL;
4374 }
4375 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004376 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4377 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004378 return v;
4379 }
4380 else {
4381 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004382 w = PyUnicode_Concat(v, w);
4383 Py_DECREF(v);
4384 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004385 }
4386}
4387
Guido van Rossum950361c1997-01-24 13:49:28 +00004388#ifdef DYNAMIC_EXECUTION_PROFILE
4389
Skip Montanarof118cb12001-10-15 20:51:38 +00004390static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004391getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004392{
4393 int i;
4394 PyObject *l = PyList_New(256);
4395 if (l == NULL) return NULL;
4396 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004397 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004398 if (x == NULL) {
4399 Py_DECREF(l);
4400 return NULL;
4401 }
4402 PyList_SetItem(l, i, x);
4403 }
4404 for (i = 0; i < 256; i++)
4405 a[i] = 0;
4406 return l;
4407}
4408
4409PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004410_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004411{
4412#ifndef DXPAIRS
4413 return getarray(dxp);
4414#else
4415 int i;
4416 PyObject *l = PyList_New(257);
4417 if (l == NULL) return NULL;
4418 for (i = 0; i < 257; i++) {
4419 PyObject *x = getarray(dxpairs[i]);
4420 if (x == NULL) {
4421 Py_DECREF(l);
4422 return NULL;
4423 }
4424 PyList_SetItem(l, i, x);
4425 }
4426 return l;
4427#endif
4428}
4429
4430#endif