blob: d1d6d1d849da1b848bfad38f8f7f947fce1d9154 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouters8ce81f72007-09-20 18:22:40 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Michael W. Hudson75eabd22005-01-18 15:56:11 +000054#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000055
Michael W. Hudson75eabd22005-01-18 15:56:11 +000056#define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
59#endif
60
Thomas Wouters477c8d52006-05-27 19:21:47 +000061void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63{
64 uint64 intr, inst, loop;
65 PyThreadState *tstate = PyThreadState_Get();
66 if (!tstate->interp->tscdump)
67 return;
68 intr = intr1 - intr0;
69 inst = inst1 - inst0 - intr;
70 loop = loop1 - loop0 - intr;
71 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode, ticked, inst, loop);
73}
Michael W. Hudson800ba232004-08-12 18:19:17 +000074
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000075#endif
76
Guido van Rossum04691fc1992-08-12 15:35:34 +000077/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000079
Guido van Rossum408027e1996-12-30 16:17:54 +000080#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000081/* For debugging the interpreter: */
82#define LLTRACE 1 /* Low-level trace feature */
83#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084#endif
85
Jeremy Hylton52820442001-01-03 23:52:36 +000086typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000087
Guido van Rossum374a9221991-04-04 10:40:29 +000088/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Thomas Wouters477c8d52006-05-27 19:21:47 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000094static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
95static PyObject * do_call(PyObject *, PyObject ***, int, int);
96static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000099static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
100static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000101#define CALL_FLAG_VAR 1
102#define CALL_FLAG_KW 2
103
Guido van Rossum0a066c01992-03-27 17:29:15 +0000104#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000105static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
109 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000113static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000114 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000115
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * cmp_outcome(int, PyObject *, PyObject *);
117static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000118static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000119static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000120static PyObject * unicode_concatenate(PyObject *, PyObject *,
121 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000122
Paul Prescode68140d2000-08-30 20:25:01 +0000123#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000124 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000125#define GLOBAL_NAME_ERROR_MSG \
126 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000127#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000128 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000129#define UNBOUNDFREE_ERROR_MSG \
130 "free variable '%.200s' referenced before assignment" \
131 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000132
Guido van Rossum950361c1997-01-24 13:49:28 +0000133/* Dynamic execution profile */
134#ifdef DYNAMIC_EXECUTION_PROFILE
135#ifdef DXPAIRS
136static long dxpairs[257][256];
137#define dxp dxpairs[256]
138#else
139static long dxp[256];
140#endif
141#endif
142
Jeremy Hylton985eba52003-02-05 23:13:00 +0000143/* Function call profile */
144#ifdef CALL_PROFILE
145#define PCALL_NUM 11
146static int pcall[PCALL_NUM];
147
148#define PCALL_ALL 0
149#define PCALL_FUNCTION 1
150#define PCALL_FAST_FUNCTION 2
151#define PCALL_FASTER_FUNCTION 3
152#define PCALL_METHOD 4
153#define PCALL_BOUND_METHOD 5
154#define PCALL_CFUNCTION 6
155#define PCALL_TYPE 7
156#define PCALL_GENERATOR 8
157#define PCALL_OTHER 9
158#define PCALL_POP 10
159
160/* Notes about the statistics
161
162 PCALL_FAST stats
163
164 FAST_FUNCTION means no argument tuple needs to be created.
165 FASTER_FUNCTION means that the fast-path frame setup code is used.
166
167 If there is a method call where the call can be optimized by changing
168 the argument tuple and calling the function directly, it gets recorded
169 twice.
170
171 As a result, the relationship among the statistics appears to be
172 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
173 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
174 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
175 PCALL_METHOD > PCALL_BOUND_METHOD
176*/
177
178#define PCALL(POS) pcall[POS]++
179
180PyObject *
181PyEval_GetCallStats(PyObject *self)
182{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000183 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000184 pcall[0], pcall[1], pcall[2], pcall[3],
185 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000187}
188#else
189#define PCALL(O)
190
191PyObject *
192PyEval_GetCallStats(PyObject *self)
193{
194 Py_INCREF(Py_None);
195 return Py_None;
196}
197#endif
198
Tim Peters5ca576e2001-06-18 22:08:13 +0000199
Guido van Rossume59214e1994-08-30 08:01:59 +0000200#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000201
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000202#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000203#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000204#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000205#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000206
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000207static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000208static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000209static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000210
Tim Peters7f468f22004-10-11 02:40:51 +0000211int
212PyEval_ThreadsInitialized(void)
213{
214 return interpreter_lock != 0;
215}
216
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000218PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000221 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000222 interpreter_lock = PyThread_allocate_lock();
223 PyThread_acquire_lock(interpreter_lock, 1);
224 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000225}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000226
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000230 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231}
232
233void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000234PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000236 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237}
238
239void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000241{
242 if (tstate == NULL)
243 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000244 /* Check someone has called PyEval_InitThreads() to create the lock */
245 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000247 if (PyThreadState_Swap(tstate) != NULL)
248 Py_FatalError(
249 "PyEval_AcquireThread: non-NULL old thread state");
250}
251
252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000254{
255 if (tstate == NULL)
256 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
257 if (PyThreadState_Swap(NULL) != tstate)
258 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000259 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000260}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000261
262/* This function is called from PyOS_AfterFork to ensure that newly
263 created child processes don't hold locks referring to threads which
264 are not running in the child process. (This could also be done using
265 pthread_atfork mechanism, at least for the pthreads implementation.) */
266
267void
268PyEval_ReInitThreads(void)
269{
Jesse Nollera8513972008-07-17 16:49:17 +0000270 PyObject *threading, *result;
271 PyThreadState *tstate;
272
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000273 if (!interpreter_lock)
274 return;
275 /*XXX Can't use PyThread_free_lock here because it does too
276 much error-checking. Doing this cleanly would require
277 adding a new function to each thread_*.h. Instead, just
278 create a new lock and waste a little bit of memory */
279 interpreter_lock = PyThread_allocate_lock();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000280 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000281 PyThread_acquire_lock(interpreter_lock, 1);
282 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000283
284 /* Update the threading module with the new state.
285 */
286 tstate = PyThreadState_GET();
287 threading = PyMapping_GetItemString(tstate->interp->modules,
288 "threading");
289 if (threading == NULL) {
290 /* threading not imported */
291 PyErr_Clear();
292 return;
293 }
294 result = PyObject_CallMethod(threading, "_after_fork", NULL);
295 if (result == NULL)
296 PyErr_WriteUnraisable(threading);
297 else
298 Py_DECREF(result);
299 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000300}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301#endif
302
Guido van Rossumff4949e1992-08-05 19:58:53 +0000303/* Functions save_thread and restore_thread are always defined so
304 dynamically loaded modules needn't be compiled separately for use
305 with and without threads: */
306
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000307PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000308PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000310 PyThreadState *tstate = PyThreadState_Swap(NULL);
311 if (tstate == NULL)
312 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000313#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000314 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000315 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000316#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000317 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318}
319
320void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000321PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000323 if (tstate == NULL)
324 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000325#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000326 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000327 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000328 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000329 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330 }
331#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000332 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000333}
334
335
Guido van Rossuma9672091994-09-14 13:31:22 +0000336/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
337 signal handlers or Mac I/O completion routines) can schedule calls
338 to a function to be called synchronously.
339 The synchronous function is called with one void* argument.
340 It should return 0 for success or -1 for failure -- failure should
341 be accompanied by an exception.
342
343 If registry succeeds, the registry function returns 0; if it fails
344 (e.g. due to too many pending calls) it returns -1 (without setting
345 an exception condition).
346
347 Note that because registry may occur from within signal handlers,
348 or other asynchronous events, calling malloc() is unsafe!
349
350#ifdef WITH_THREAD
351 Any thread can schedule pending calls, but only the main thread
352 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000353 There is no facility to schedule calls to a particular thread, but
354 that should be easy to change, should that ever be required. In
355 that case, the static variables here should go into the python
356 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000357#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000358*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000359
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000360#ifdef WITH_THREAD
361
362/* The WITH_THREAD implementation is thread-safe. It allows
363 scheduling to be made from any thread, and even from an executing
364 callback.
365 */
366
367#define NPENDINGCALLS 32
368static struct {
369 int (*func)(void *);
370 void *arg;
371} pendingcalls[NPENDINGCALLS];
372static int pendingfirst = 0;
373static int pendinglast = 0;
374static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
375static char pendingbusy = 0;
376
377int
378Py_AddPendingCall(int (*func)(void *), void *arg)
379{
380 int i, j, result=0;
381 PyThread_type_lock lock = pending_lock;
382
383 /* try a few times for the lock. Since this mechanism is used
384 * for signal handling (on the main thread), there is a (slim)
385 * chance that a signal is delivered on the same thread while we
386 * hold the lock during the Py_MakePendingCalls() function.
387 * This avoids a deadlock in that case.
388 * Note that signals can be delivered on any thread. In particular,
389 * on Windows, a SIGINT is delivered on a system-created worker
390 * thread.
391 * We also check for lock being NULL, in the unlikely case that
392 * this function is called before any bytecode evaluation takes place.
393 */
394 if (lock != NULL) {
395 for (i = 0; i<100; i++) {
396 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
397 break;
398 }
399 if (i == 100)
400 return -1;
401 }
402
403 i = pendinglast;
404 j = (i + 1) % NPENDINGCALLS;
405 if (j == pendingfirst) {
406 result = -1; /* Queue full */
407 } else {
408 pendingcalls[i].func = func;
409 pendingcalls[i].arg = arg;
410 pendinglast = j;
411 }
412 /* signal main loop */
413 _Py_Ticker = 0;
414 pendingcalls_to_do = 1;
415 if (lock != NULL)
416 PyThread_release_lock(lock);
417 return result;
418}
419
420int
421Py_MakePendingCalls(void)
422{
423 int i;
424 int r = 0;
425
426 if (!pending_lock) {
427 /* initial allocation of the lock */
428 pending_lock = PyThread_allocate_lock();
429 if (pending_lock == NULL)
430 return -1;
431 }
432
433 /* only service pending calls on main thread */
434 if (main_thread && PyThread_get_thread_ident() != main_thread)
435 return 0;
436 /* don't perform recursive pending calls */
437 if (pendingbusy)
438 return 0;
439 pendingbusy = 1;
440 /* perform a bounded number of calls, in case of recursion */
441 for (i=0; i<NPENDINGCALLS; i++) {
442 int j;
443 int (*func)(void *);
Benjamin Petersonb4ddfa42009-01-17 23:46:54 +0000444 void *arg = NULL;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000445
446 /* pop one item off the queue while holding the lock */
447 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
448 j = pendingfirst;
449 if (j == pendinglast) {
450 func = NULL; /* Queue empty */
451 } else {
452 func = pendingcalls[j].func;
453 arg = pendingcalls[j].arg;
454 pendingfirst = (j + 1) % NPENDINGCALLS;
455 }
456 pendingcalls_to_do = pendingfirst != pendinglast;
457 PyThread_release_lock(pending_lock);
458 /* having released the lock, perform the callback */
459 if (func == NULL)
460 break;
461 r = func(arg);
462 if (r)
463 break;
464 }
465 pendingbusy = 0;
466 return r;
467}
468
469#else /* if ! defined WITH_THREAD */
470
471/*
472 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
473 This code is used for signal handling in python that isn't built
474 with WITH_THREAD.
475 Don't use this implementation when Py_AddPendingCalls() can happen
476 on a different thread!
477
Guido van Rossuma9672091994-09-14 13:31:22 +0000478 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479 (1) nested asynchronous calls to Py_AddPendingCall()
480 (2) AddPendingCall() calls made while pending calls are being processed.
481
482 (1) is very unlikely because typically signal delivery
483 is blocked during signal handling. So it should be impossible.
484 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000485 The current code is safe against (2), but not against (1).
486 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000487 thread is present, interrupted by signals, and that the critical
488 section is protected with the "busy" variable. On Windows, which
489 delivers SIGINT on a system thread, this does not hold and therefore
490 Windows really shouldn't use this version.
491 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000492*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000493
Guido van Rossuma9672091994-09-14 13:31:22 +0000494#define NPENDINGCALLS 32
495static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000496 int (*func)(void *);
497 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000498} pendingcalls[NPENDINGCALLS];
499static volatile int pendingfirst = 0;
500static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000502
503int
Thomas Wouters334fb892000-07-25 12:56:38 +0000504Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000505{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000506 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000507 int i, j;
508 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000509 if (busy)
510 return -1;
511 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000512 i = pendinglast;
513 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000514 if (j == pendingfirst) {
515 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000516 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000517 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000518 pendingcalls[i].func = func;
519 pendingcalls[i].arg = arg;
520 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000521
522 _Py_Ticker = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000523 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000524 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000525 /* XXX End critical section */
526 return 0;
527}
528
Guido van Rossum180d7b41994-09-29 09:45:57 +0000529int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000530Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000531{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000532 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000533 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000534 return 0;
535 busy = 1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000537 for (;;) {
538 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000539 int (*func)(void *);
540 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000541 i = pendingfirst;
542 if (i == pendinglast)
543 break; /* Queue empty */
544 func = pendingcalls[i].func;
545 arg = pendingcalls[i].arg;
546 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000547 if (func(arg) < 0) {
548 busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000550 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000551 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000552 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000553 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000554 return 0;
555}
556
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000557#endif /* WITH_THREAD */
558
Guido van Rossuma9672091994-09-14 13:31:22 +0000559
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000560/* The interpreter's recursion limit */
561
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000562#ifndef Py_DEFAULT_RECURSION_LIMIT
563#define Py_DEFAULT_RECURSION_LIMIT 1000
564#endif
565static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
566int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000567
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000568int
569Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000570{
571 return recursion_limit;
572}
573
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000574void
575Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000576{
577 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000578 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000579}
580
Armin Rigo2b3eb402003-10-28 12:05:48 +0000581/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
582 if the recursion_depth reaches _Py_CheckRecursionLimit.
583 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
584 to guarantee that _Py_CheckRecursiveCall() is regularly called.
585 Without USE_STACKCHECK, there is no need for this. */
586int
587_Py_CheckRecursiveCall(char *where)
588{
589 PyThreadState *tstate = PyThreadState_GET();
590
591#ifdef USE_STACKCHECK
592 if (PyOS_CheckStack()) {
593 --tstate->recursion_depth;
594 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
595 return -1;
596 }
597#endif
Antoine Pitrou658fad82008-09-03 18:34:34 +0000598 _Py_CheckRecursionLimit = recursion_limit;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000599 if (tstate->recursion_critical)
600 /* Somebody asked that we don't check for recursion. */
601 return 0;
602 if (tstate->overflowed) {
603 if (tstate->recursion_depth > recursion_limit + 50) {
604 /* Overflowing while handling an overflow. Give up. */
605 Py_FatalError("Cannot recover from stack overflow.");
606 }
607 return 0;
608 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000609 if (tstate->recursion_depth > recursion_limit) {
610 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000611 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000612 PyErr_Format(PyExc_RuntimeError,
613 "maximum recursion depth exceeded%s",
614 where);
615 return -1;
616 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000617 return 0;
618}
619
Guido van Rossum374a9221991-04-04 10:40:29 +0000620/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000621enum why_code {
622 WHY_NOT = 0x0001, /* No error */
623 WHY_EXCEPTION = 0x0002, /* Exception occurred */
624 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
625 WHY_RETURN = 0x0008, /* 'return' statement */
626 WHY_BREAK = 0x0010, /* 'break' statement */
627 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000628 WHY_YIELD = 0x0040, /* 'yield' operator */
629 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000630};
Guido van Rossum374a9221991-04-04 10:40:29 +0000631
Collin Winter828f04a2007-08-31 00:04:24 +0000632static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000633static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000634
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000635/* Records whether tracing is on for any thread. Counts the number of
636 threads for which tstate->c_tracefunc is non-NULL, so if the value
637 is 0, we know we don't have to check this thread's c_tracefunc.
638 This speeds up the if statement in PyEval_EvalFrameEx() after
639 fast_next_opcode*/
640static int _Py_TracingPossible = 0;
641
Skip Montanarod581d772002-09-03 20:10:45 +0000642/* for manipulating the thread switch and periodic "stuff" - used to be
643 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000644int _Py_CheckInterval = 100;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000645volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000646
Guido van Rossumb209a111997-04-29 18:18:01 +0000647PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000648PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000649{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000650 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000651 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000652 (PyObject **)NULL, 0,
653 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000654 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000655 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000656}
657
658
659/* Interpreter main loop */
660
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000661PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000662PyEval_EvalFrame(PyFrameObject *f) {
663 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000664 used this API; core interpreter code should call
665 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000666 return PyEval_EvalFrameEx(f, 0);
667}
668
669PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000671{
Guido van Rossum950361c1997-01-24 13:49:28 +0000672#ifdef DXPAIRS
673 int lastopcode = 0;
674#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000675 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000676 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000677 register int opcode; /* Current opcode */
678 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000679 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000680 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000681 register PyObject *x; /* Result object -- NULL if error */
682 register PyObject *v; /* Temporary objects popped off stack */
683 register PyObject *w;
684 register PyObject *u;
685 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000686 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000687 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000688 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000689 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000690
Tim Peters8a5c3c72004-04-05 19:36:21 +0000691 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000692
693 not (instr_lb <= current_bytecode_offset < instr_ub)
694
Tim Peters8a5c3c72004-04-05 19:36:21 +0000695 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000696 initial values are such as to make this false the first
697 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000698 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000699
Guido van Rossumd076c731998-10-07 19:42:25 +0000700 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000701 PyObject *names;
702 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000703#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000704 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000705 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000706#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000707
Antoine Pitroub52ec782009-01-25 16:34:23 +0000708/* Computed GOTOs, or
709 the-optimization-commonly-but-improperly-known-as-"threaded code"
710 using gcc's labels-as-values extension
711 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
712
713 The traditional bytecode evaluation loop uses a "switch" statement, which
714 decent compilers will optimize as a single indirect branch instruction
715 combined with a lookup table of jump addresses. However, since the
716 indirect jump instruction is shared by all opcodes, the CPU will have a
717 hard time making the right prediction for where to jump next (actually,
718 it will be always wrong except in the uncommon case of a sequence of
719 several identical opcodes).
720
721 "Threaded code" in contrast, uses an explicit jump table and an explicit
722 indirect jump instruction at the end of each opcode. Since the jump
723 instruction is at a different address for each opcode, the CPU will make a
724 separate prediction for each of these instructions, which is equivalent to
725 predicting the second opcode of each opcode pair. These predictions have
726 a much better chance to turn out valid, especially in small bytecode loops.
727
728 A mispredicted branch on a modern CPU flushes the whole pipeline and
729 can cost several CPU cycles (depending on the pipeline depth),
730 and potentially many more instructions (depending on the pipeline width).
731 A correctly predicted branch, however, is nearly free.
732
733 At the time of this writing, the "threaded code" version is up to 15-20%
734 faster than the normal "switch" version, depending on the compiler and the
735 CPU architecture.
736
737 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
738 because it would render the measurements invalid.
739
740
741 NOTE: care must be taken that the compiler doesn't try to "optimize" the
742 indirect jumps by sharing them between all opcodes. Such optimizations
743 can be disabled on gcc by using the -fno-gcse flag (or possibly
744 -fno-crossjumping).
745*/
746
747#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
748#undef USE_COMPUTED_GOTOS
749#endif
750
751#ifdef USE_COMPUTED_GOTOS
752/* Import the static jump table */
753#include "opcode_targets.h"
754
755/* This macro is used when several opcodes defer to the same implementation
756 (e.g. SETUP_LOOP, SETUP_FINALLY) */
757#define TARGET_WITH_IMPL(op, impl) \
758 TARGET_##op: \
759 opcode = op; \
760 if (HAS_ARG(op)) \
761 oparg = NEXTARG(); \
762 case op: \
763 goto impl; \
764
765#define TARGET(op) \
766 TARGET_##op: \
767 opcode = op; \
768 if (HAS_ARG(op)) \
769 oparg = NEXTARG(); \
770 case op:
771
772
773#define DISPATCH() \
774 { \
775 /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
776 int _tick = _Py_Ticker - 1; \
777 _Py_Ticker = _tick; \
778 if (_tick >= 0) { \
779 FAST_DISPATCH(); \
780 } \
781 continue; \
782 }
783
784#ifdef LLTRACE
785#define FAST_DISPATCH() \
786 { \
787 if (!lltrace && !_Py_TracingPossible) { \
788 f->f_lasti = INSTR_OFFSET(); \
789 goto *opcode_targets[*next_instr++]; \
790 } \
791 goto fast_next_opcode; \
792 }
793#else
794#define FAST_DISPATCH() \
795 { \
796 if (!_Py_TracingPossible) { \
797 f->f_lasti = INSTR_OFFSET(); \
798 goto *opcode_targets[*next_instr++]; \
799 } \
800 goto fast_next_opcode; \
801 }
802#endif
803
804#else
805#define TARGET(op) \
806 case op:
807#define TARGET_WITH_IMPL(op, impl) \
808 /* silence compiler warnings about `impl` unused */ \
809 if (0) goto impl; \
810 case op:
811#define DISPATCH() continue
812#define FAST_DISPATCH() goto fast_next_opcode
813#endif
814
815
Neal Norwitza81d2202002-07-14 00:27:26 +0000816/* Tuple access macros */
817
818#ifndef Py_DEBUG
819#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
820#else
821#define GETITEM(v, i) PyTuple_GetItem((v), (i))
822#endif
823
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000824#ifdef WITH_TSC
825/* Use Pentium timestamp counter to mark certain events:
826 inst0 -- beginning of switch statement for opcode dispatch
827 inst1 -- end of switch statement (may be skipped)
828 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000829 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000830 (may be skipped)
831 intr1 -- beginning of long interruption
832 intr2 -- end of long interruption
833
834 Many opcodes call out to helper C functions. In some cases, the
835 time in those functions should be counted towards the time for the
836 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
837 calls another Python function; there's no point in charge all the
838 bytecode executed by the called function to the caller.
839
840 It's hard to make a useful judgement statically. In the presence
841 of operator overloading, it's impossible to tell if a call will
842 execute new Python code or not.
843
844 It's a case-by-case judgement. I'll use intr1 for the following
845 cases:
846
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000847 IMPORT_STAR
848 IMPORT_FROM
849 CALL_FUNCTION (and friends)
850
851 */
852 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
853 int ticked = 0;
854
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000855 READ_TIMESTAMP(inst0);
856 READ_TIMESTAMP(inst1);
857 READ_TIMESTAMP(loop0);
858 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000859
860 /* shut up the compiler */
861 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000862#endif
863
Guido van Rossum374a9221991-04-04 10:40:29 +0000864/* Code access macros */
865
Martin v. Löwis18e16552006-02-15 17:27:45 +0000866#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000867#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000868#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000869#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000870#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000871#define JUMPBY(x) (next_instr += (x))
872
Raymond Hettingerf606f872003-03-16 03:11:04 +0000873/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000874 Some opcodes tend to come in pairs thus making it possible to
875 predict the second code when the first is run. For example,
876 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
877 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000878
Georg Brandl86b2fb92008-07-16 03:43:04 +0000879 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000880 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000881 processor's own internal branch predication has a high likelihood of
882 success, resulting in a nearly zero-overhead transition to the
883 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000884 including its two unpredictable branches, the HAS_ARG test and the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000885 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000886 a successful PREDICT has the effect of making the two opcodes run as if
Georg Brandl86b2fb92008-07-16 03:43:04 +0000887 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000888
Georg Brandl86b2fb92008-07-16 03:43:04 +0000889 If collecting opcode statistics, your choices are to either keep the
890 predictions turned-on and interpret the results as if some opcodes
891 had been combined or turn-off predictions so that the opcode frequency
892 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000893
894 Opcode prediction is disabled with threaded code, since the latter allows
895 the CPU to record separate branch prediction information for each
896 opcode.
897
Raymond Hettingerf606f872003-03-16 03:11:04 +0000898*/
899
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Raymond Hettingera7216982004-02-08 19:59:27 +0000901#define PREDICT(op) if (0) goto PRED_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000902#define PREDICTED(op) PRED_##op:
903#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000904#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000905#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000906#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000907#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +0000908#endif
909
Raymond Hettingerf606f872003-03-16 03:11:04 +0000910
Guido van Rossum374a9221991-04-04 10:40:29 +0000911/* Stack manipulation macros */
912
Martin v. Löwis18e16552006-02-15 17:27:45 +0000913/* The stack can grow at most MAXINT deep, as co_nlocals and
914 co_stacksize are ints. */
915#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000916#define EMPTY() (STACK_LEVEL() == 0)
917#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000918#define SECOND() (stack_pointer[-2])
919#define THIRD() (stack_pointer[-3])
920#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000921#define SET_TOP(v) (stack_pointer[-1] = (v))
922#define SET_SECOND(v) (stack_pointer[-2] = (v))
923#define SET_THIRD(v) (stack_pointer[-3] = (v))
924#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000925#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000926#define BASIC_PUSH(v) (*stack_pointer++ = (v))
927#define BASIC_POP() (*--stack_pointer)
928
Guido van Rossum96a42c81992-01-12 02:29:51 +0000929#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000930#define PUSH(v) { (void)(BASIC_PUSH(v), \
931 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000933#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
934 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000935#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
936 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000937 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000938#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
939 prtrace((STACK_POINTER)[-1], "ext_pop")), \
940 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000941#else
942#define PUSH(v) BASIC_PUSH(v)
943#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000944#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000945#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000946#endif
947
Guido van Rossum681d79a1995-07-18 14:51:37 +0000948/* Local variable macros */
949
950#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000951
952/* The SETLOCAL() macro must not DECREF the local variable in-place and
953 then store the new value; it must copy the old value to a temporary
954 value, then store the new value, and then DECREF the temporary value.
955 This is because it is possible that during the DECREF the frame is
956 accessed by other code (e.g. a __del__ method or gc.collect()) and the
957 variable would be pointing to already-freed memory. */
958#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
959 GETLOCAL(i) = value; \
960 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000961
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000962
963#define UNWIND_BLOCK(b) \
964 while (STACK_LEVEL() > (b)->b_level) { \
965 PyObject *v = POP(); \
966 Py_XDECREF(v); \
967 }
968
969#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000970 { \
971 PyObject *type, *value, *traceback; \
972 assert(STACK_LEVEL() >= (b)->b_level + 3); \
973 while (STACK_LEVEL() > (b)->b_level + 3) { \
974 value = POP(); \
975 Py_XDECREF(value); \
976 } \
977 type = tstate->exc_type; \
978 value = tstate->exc_value; \
979 traceback = tstate->exc_traceback; \
980 tstate->exc_type = POP(); \
981 tstate->exc_value = POP(); \
982 tstate->exc_traceback = POP(); \
983 Py_XDECREF(type); \
984 Py_XDECREF(value); \
985 Py_XDECREF(traceback); \
986 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000987
988#define SAVE_EXC_STATE() \
989 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000990 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000991 Py_XINCREF(tstate->exc_type); \
992 Py_XINCREF(tstate->exc_value); \
993 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000994 type = f->f_exc_type; \
995 value = f->f_exc_value; \
996 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000997 f->f_exc_type = tstate->exc_type; \
998 f->f_exc_value = tstate->exc_value; \
999 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001000 Py_XDECREF(type); \
1001 Py_XDECREF(value); \
1002 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001003 }
1004
1005#define SWAP_EXC_STATE() \
1006 { \
1007 PyObject *tmp; \
1008 tmp = tstate->exc_type; \
1009 tstate->exc_type = f->f_exc_type; \
1010 f->f_exc_type = tmp; \
1011 tmp = tstate->exc_value; \
1012 tstate->exc_value = f->f_exc_value; \
1013 f->f_exc_value = tmp; \
1014 tmp = tstate->exc_traceback; \
1015 tstate->exc_traceback = f->f_exc_traceback; \
1016 f->f_exc_traceback = tmp; \
1017 }
1018
Guido van Rossuma027efa1997-05-05 20:56:21 +00001019/* Start of code */
1020
Tim Peters5ca576e2001-06-18 22:08:13 +00001021 if (f == NULL)
1022 return NULL;
1023
Armin Rigo1d313ab2003-10-25 14:33:09 +00001024 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001025 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +00001026 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001027
Tim Peters5ca576e2001-06-18 22:08:13 +00001028 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001029
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001030 if (tstate->use_tracing) {
1031 if (tstate->c_tracefunc != NULL) {
1032 /* tstate->c_tracefunc, if defined, is a
1033 function that will be called on *every* entry
1034 to a code block. Its return value, if not
1035 None, is a function that will be called at
1036 the start of each executed line of code.
1037 (Actually, the function must return itself
1038 in order to continue tracing.) The trace
1039 functions are called with three arguments:
1040 a pointer to the current frame, a string
1041 indicating why the function is called, and
1042 an argument which depends on the situation.
1043 The global trace function is also called
1044 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001045 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001046 tstate->c_traceobj,
1047 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001048 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001049 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001050 }
1051 }
1052 if (tstate->c_profilefunc != NULL) {
1053 /* Similar for c_profilefunc, except it needn't
1054 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001055 if (call_trace_protected(tstate->c_profilefunc,
1056 tstate->c_profileobj,
1057 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001058 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001059 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001060 }
1061 }
1062 }
1063
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001064 co = f->f_code;
1065 names = co->co_names;
1066 consts = co->co_consts;
1067 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001068 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +00001069 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001070 /* An explanation is in order for the next line.
1071
1072 f->f_lasti now refers to the index of the last instruction
1073 executed. You might think this was obvious from the name, but
1074 this wasn't always true before 2.3! PyFrame_New now sets
1075 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1076 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001077 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001078
1079 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001080 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001081 prediction effectively links the two codes together as if they
1082 were a single new opcode; accordingly,f->f_lasti will point to
1083 the first code in the pair (for instance, GET_ITER followed by
1084 FOR_ITER is effectively a single opcode and f->f_lasti will point
1085 at to the beginning of the combined pair.)
1086 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001087 next_instr = first_instr + f->f_lasti + 1;
1088 stack_pointer = f->f_stacktop;
1089 assert(stack_pointer != NULL);
1090 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1091
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001092 if (f->f_code->co_flags & CO_GENERATOR) {
1093 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1094 /* We were in an except handler when we left,
1095 restore the exception state which was put aside
1096 (see YIELD_VALUE). */
1097 SWAP_EXC_STATE();
1098 }
1099 else {
1100 SAVE_EXC_STATE();
1101 }
1102 }
1103
Tim Peters5ca576e2001-06-18 22:08:13 +00001104#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001106#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001107#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001108 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001109#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001110
Guido van Rossum374a9221991-04-04 10:40:29 +00001111 why = WHY_NOT;
1112 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00001113 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +00001114 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001115
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001116 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001117 why = WHY_EXCEPTION;
1118 goto on_error;
1119 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001120
Guido van Rossum374a9221991-04-04 10:40:29 +00001121 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001122#ifdef WITH_TSC
1123 if (inst1 == 0) {
1124 /* Almost surely, the opcode executed a break
1125 or a continue, preventing inst1 from being set
1126 on the way out of the loop.
1127 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001128 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001129 loop1 = inst1;
1130 }
1131 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1132 intr0, intr1);
1133 ticked = 0;
1134 inst1 = 0;
1135 intr0 = 0;
1136 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001137 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001138#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001139 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001140 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001141
Guido van Rossuma027efa1997-05-05 20:56:21 +00001142 /* Do periodic things. Doing this every time through
1143 the loop would add too much overhead, so we do it
1144 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001145 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001146 event needs attention (e.g. a signal handler or
1147 async I/O handler); see Py_AddPendingCall() and
1148 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001149
Skip Montanarod581d772002-09-03 20:10:45 +00001150 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001151 if (*next_instr == SETUP_FINALLY) {
1152 /* Make the last opcode before
1153 a try: finally: block uninterruptable. */
1154 goto fast_next_opcode;
1155 }
Skip Montanarod581d772002-09-03 20:10:45 +00001156 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001157 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001158#ifdef WITH_TSC
1159 ticked = 1;
1160#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001161 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001162 if (Py_MakePendingCalls() < 0) {
1163 why = WHY_EXCEPTION;
1164 goto on_error;
1165 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001166 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +00001167 /* MakePendingCalls() didn't succeed.
1168 Force early re-execution of this
1169 "periodic" code, possibly after
1170 a thread switch */
1171 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +00001172 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001173#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001174 if (interpreter_lock) {
1175 /* Give another thread a chance */
1176
Guido van Rossum25ce5661997-08-02 03:10:38 +00001177 if (PyThreadState_Swap(NULL) != tstate)
1178 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +00001179 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001180
1181 /* Other threads may run now */
1182
Guido van Rossum65d5b571998-12-21 19:32:43 +00001183 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001184 if (PyThreadState_Swap(tstate) != NULL)
1185 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001186
1187 /* Check for thread interrupts */
1188
1189 if (tstate->async_exc != NULL) {
1190 x = tstate->async_exc;
1191 tstate->async_exc = NULL;
1192 PyErr_SetNone(x);
1193 Py_DECREF(x);
1194 why = WHY_EXCEPTION;
1195 goto on_error;
1196 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001197 }
1198#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001199 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001200
Neil Schemenauer63543862002-02-17 19:10:14 +00001201 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001202 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001204 /* line-by-line tracing support */
1205
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001206 if (_Py_TracingPossible &&
1207 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001208 /* see maybe_call_line_trace
1209 for expository comments */
1210 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001211
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001212 err = maybe_call_line_trace(tstate->c_tracefunc,
1213 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001214 f, &instr_lb, &instr_ub,
1215 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001216 /* Reload possibly changed frame fields */
1217 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001218 if (f->f_stacktop != NULL) {
1219 stack_pointer = f->f_stacktop;
1220 f->f_stacktop = NULL;
1221 }
1222 if (err) {
1223 /* trace function raised an exception */
1224 goto on_error;
1225 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001226 }
1227
1228 /* Extract opcode and argument */
1229
Guido van Rossum374a9221991-04-04 10:40:29 +00001230 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001231 oparg = 0; /* allows oparg to be stored in a register because
1232 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001233 if (HAS_ARG(opcode))
1234 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001235 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001236#ifdef DYNAMIC_EXECUTION_PROFILE
1237#ifdef DXPAIRS
1238 dxpairs[lastopcode][opcode]++;
1239 lastopcode = opcode;
1240#endif
1241 dxp[opcode]++;
1242#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001243
Guido van Rossum96a42c81992-01-12 02:29:51 +00001244#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001245 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001246
Guido van Rossum96a42c81992-01-12 02:29:51 +00001247 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001248 if (HAS_ARG(opcode)) {
1249 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001250 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001251 }
1252 else {
1253 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001254 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001255 }
1256 }
1257#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001258
Guido van Rossum374a9221991-04-04 10:40:29 +00001259 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001260 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001261
Guido van Rossum374a9221991-04-04 10:40:29 +00001262 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Guido van Rossum374a9221991-04-04 10:40:29 +00001264 /* BEWARE!
1265 It is essential that any operation that fails sets either
1266 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1267 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001268
Guido van Rossum374a9221991-04-04 10:40:29 +00001269 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001270
Antoine Pitroub52ec782009-01-25 16:34:23 +00001271 TARGET(NOP)
1272 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001273
Antoine Pitroub52ec782009-01-25 16:34:23 +00001274 TARGET(LOAD_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001275 x = GETLOCAL(oparg);
1276 if (x != NULL) {
1277 Py_INCREF(x);
1278 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001279 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001280 }
1281 format_exc_check_arg(PyExc_UnboundLocalError,
1282 UNBOUNDLOCAL_ERROR_MSG,
1283 PyTuple_GetItem(co->co_varnames, oparg));
1284 break;
1285
Antoine Pitroub52ec782009-01-25 16:34:23 +00001286 TARGET(LOAD_CONST)
Skip Montanaro04d80f82002-08-04 21:03:35 +00001287 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001288 Py_INCREF(x);
1289 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001290 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001291
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001292 PREDICTED_WITH_ARG(STORE_FAST);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001293 TARGET(STORE_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001294 v = POP();
1295 SETLOCAL(oparg, v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001296 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001297
Antoine Pitroub52ec782009-01-25 16:34:23 +00001298 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001299 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001300 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001301 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001302
Antoine Pitroub52ec782009-01-25 16:34:23 +00001303 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001304 v = TOP();
1305 w = SECOND();
1306 SET_TOP(w);
1307 SET_SECOND(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001308 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Antoine Pitroub52ec782009-01-25 16:34:23 +00001310 TARGET(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001311 v = TOP();
1312 w = SECOND();
1313 x = THIRD();
1314 SET_TOP(w);
1315 SET_SECOND(x);
1316 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001317 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Antoine Pitroub52ec782009-01-25 16:34:23 +00001319 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001320 u = TOP();
1321 v = SECOND();
1322 w = THIRD();
1323 x = FOURTH();
1324 SET_TOP(v);
1325 SET_SECOND(w);
1326 SET_THIRD(x);
1327 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001328 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001329
Antoine Pitroub52ec782009-01-25 16:34:23 +00001330 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001331 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001332 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001333 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001334 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001335
Antoine Pitroub52ec782009-01-25 16:34:23 +00001336 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001337 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001339 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001340 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001341 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001342 STACKADJ(2);
1343 SET_TOP(x);
1344 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001345 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001346 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001348 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001349 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001350 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001352 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001353 STACKADJ(3);
1354 SET_TOP(x);
1355 SET_SECOND(w);
1356 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001357 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001359 Py_FatalError("invalid argument to DUP_TOPX"
1360 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001361 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001362 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001363
Antoine Pitroub52ec782009-01-25 16:34:23 +00001364 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001366 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001367 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001368 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001369 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001370 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Antoine Pitroub52ec782009-01-25 16:34:23 +00001372 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001373 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001374 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001375 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001377 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001378 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Antoine Pitroub52ec782009-01-25 16:34:23 +00001380 TARGET(UNARY_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001382 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001383 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001384 if (err == 0) {
1385 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001386 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001387 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001388 }
1389 else if (err > 0) {
1390 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001391 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001392 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001393 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001394 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001395 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001396 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Antoine Pitroub52ec782009-01-25 16:34:23 +00001398 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001399 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001400 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001401 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001402 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001403 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001404 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Antoine Pitroub52ec782009-01-25 16:34:23 +00001406 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001407 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001408 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001409 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001410 Py_DECREF(v);
1411 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001412 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001413 if (x != NULL) DISPATCH();
Guido van Rossum50564e81996-01-12 01:13:16 +00001414 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Antoine Pitroub52ec782009-01-25 16:34:23 +00001416 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001418 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001419 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001420 Py_DECREF(v);
1421 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001422 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001423 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001425
Antoine Pitroub52ec782009-01-25 16:34:23 +00001426 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001427 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001428 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001429 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001430 Py_DECREF(v);
1431 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001432 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001433 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001434 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001435
Antoine Pitroub52ec782009-01-25 16:34:23 +00001436 TARGET(BINARY_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001437 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001438 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001439 x = PyNumber_FloorDivide(v, w);
1440 Py_DECREF(v);
1441 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001442 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001443 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001444 break;
1445
Antoine Pitroub52ec782009-01-25 16:34:23 +00001446 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001448 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001449 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001450 Py_DECREF(v);
1451 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001452 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001453 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001454 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001455
Antoine Pitroub52ec782009-01-25 16:34:23 +00001456 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001458 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001459 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001460 PyUnicode_CheckExact(w)) {
1461 x = unicode_concatenate(v, w, f, next_instr);
1462 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001463 goto skip_decref_vx;
1464 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001465 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001466 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001467 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001468 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001469 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001470 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001471 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001472 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001473 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Antoine Pitroub52ec782009-01-25 16:34:23 +00001475 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001476 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001477 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001478 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001479 Py_DECREF(v);
1480 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001481 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001482 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001483 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001484
Antoine Pitroub52ec782009-01-25 16:34:23 +00001485 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001487 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001488 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001489 Py_DECREF(v);
1490 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001491 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001492 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001493 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001494
Antoine Pitroub52ec782009-01-25 16:34:23 +00001495 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001496 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001497 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001498 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001499 Py_DECREF(v);
1500 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001501 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001502 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001503 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001504
Antoine Pitroub52ec782009-01-25 16:34:23 +00001505 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001506 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001507 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001508 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001509 Py_DECREF(v);
1510 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001511 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001512 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001513 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001514
Antoine Pitroub52ec782009-01-25 16:34:23 +00001515 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001516 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001517 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001518 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001519 Py_DECREF(v);
1520 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001521 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001522 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001523 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Antoine Pitroub52ec782009-01-25 16:34:23 +00001525 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001526 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001527 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001528 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001529 Py_DECREF(v);
1530 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001531 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001532 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001533 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Antoine Pitroub52ec782009-01-25 16:34:23 +00001535 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001536 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001537 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001538 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001539 Py_DECREF(v);
1540 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001541 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001542 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001543 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001544
Antoine Pitroub52ec782009-01-25 16:34:23 +00001545 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001546 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001547 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001548 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001549 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001550 if (err == 0) {
1551 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001552 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001553 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001554 break;
1555
Antoine Pitroub52ec782009-01-25 16:34:23 +00001556 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001557 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001558 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001559 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001560 Py_DECREF(w);
1561 if (err == 0) {
1562 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001563 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001564 }
1565 break;
1566
Antoine Pitroub52ec782009-01-25 16:34:23 +00001567 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001568 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001569 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001570 x = PyNumber_InPlacePower(v, w, Py_None);
1571 Py_DECREF(v);
1572 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001573 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001574 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001575 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Antoine Pitroub52ec782009-01-25 16:34:23 +00001577 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001578 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001579 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001580 x = PyNumber_InPlaceMultiply(v, w);
1581 Py_DECREF(v);
1582 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001583 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001584 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001585 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001586
Antoine Pitroub52ec782009-01-25 16:34:23 +00001587 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001588 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001589 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001590 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001591 Py_DECREF(v);
1592 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001593 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001594 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001595 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001596
Antoine Pitroub52ec782009-01-25 16:34:23 +00001597 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001598 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001599 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001600 x = PyNumber_InPlaceFloorDivide(v, w);
1601 Py_DECREF(v);
1602 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001603 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001604 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001605 break;
1606
Antoine Pitroub52ec782009-01-25 16:34:23 +00001607 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001608 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001609 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001610 x = PyNumber_InPlaceRemainder(v, w);
1611 Py_DECREF(v);
1612 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001613 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001614 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001615 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001616
Antoine Pitroub52ec782009-01-25 16:34:23 +00001617 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001618 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001619 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001620 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001621 PyUnicode_CheckExact(w)) {
1622 x = unicode_concatenate(v, w, f, next_instr);
1623 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001624 goto skip_decref_v;
1625 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001626 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001627 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001628 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001629 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001630 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001631 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001632 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001633 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001634 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001635
Antoine Pitroub52ec782009-01-25 16:34:23 +00001636 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001637 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001638 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001639 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001640 Py_DECREF(v);
1641 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001642 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001643 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001644 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001645
Antoine Pitroub52ec782009-01-25 16:34:23 +00001646 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001647 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001648 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001649 x = PyNumber_InPlaceLshift(v, w);
1650 Py_DECREF(v);
1651 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001652 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001653 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001654 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Antoine Pitroub52ec782009-01-25 16:34:23 +00001656 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001657 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001658 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001659 x = PyNumber_InPlaceRshift(v, w);
1660 Py_DECREF(v);
1661 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001662 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001663 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001664 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Antoine Pitroub52ec782009-01-25 16:34:23 +00001666 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001667 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001668 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001669 x = PyNumber_InPlaceAnd(v, w);
1670 Py_DECREF(v);
1671 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001672 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001673 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001674 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001675
Antoine Pitroub52ec782009-01-25 16:34:23 +00001676 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001677 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001678 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001679 x = PyNumber_InPlaceXor(v, w);
1680 Py_DECREF(v);
1681 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001682 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001683 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001684 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Antoine Pitroub52ec782009-01-25 16:34:23 +00001686 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001687 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001688 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001689 x = PyNumber_InPlaceOr(v, w);
1690 Py_DECREF(v);
1691 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001692 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001693 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001694 break;
1695
Antoine Pitroub52ec782009-01-25 16:34:23 +00001696 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001697 w = TOP();
1698 v = SECOND();
1699 u = THIRD();
1700 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001701 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001702 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001703 Py_DECREF(u);
1704 Py_DECREF(v);
1705 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001706 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001707 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Antoine Pitroub52ec782009-01-25 16:34:23 +00001709 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001710 w = TOP();
1711 v = SECOND();
1712 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001713 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001714 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001715 Py_DECREF(v);
1716 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001717 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001718 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001719
Antoine Pitroub52ec782009-01-25 16:34:23 +00001720 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001721 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001722 w = PySys_GetObject("displayhook");
1723 if (w == NULL) {
1724 PyErr_SetString(PyExc_RuntimeError,
1725 "lost sys.displayhook");
1726 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001727 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001728 }
1729 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001730 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001731 if (x == NULL)
1732 err = -1;
1733 }
1734 if (err == 0) {
1735 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001736 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001737 if (w == NULL)
1738 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001740 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001741 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001743
Thomas Wouters434d0822000-08-24 20:11:32 +00001744#ifdef CASE_TOO_BIG
1745 default: switch (opcode) {
1746#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001747 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001748 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001749 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001750 case 2:
1751 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001752 case 1:
1753 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001754 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001755 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001756 break;
1757 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001758 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001759 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001760 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001761 break;
1762 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Antoine Pitroub52ec782009-01-25 16:34:23 +00001765 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001766 x = POP();
1767 v = f->f_locals;
1768 Py_XDECREF(v);
1769 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001770 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001771
Antoine Pitroub52ec782009-01-25 16:34:23 +00001772 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001773 retval = POP();
1774 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001775 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001776
Antoine Pitroub52ec782009-01-25 16:34:23 +00001777 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001778 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001779 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001780 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001781 /* Put aside the current exception state and restore
1782 that of the calling frame. This only serves when
1783 "yield" is used inside an except handler. */
1784 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001785 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001786
Antoine Pitroub52ec782009-01-25 16:34:23 +00001787 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001788 {
1789 PyTryBlock *b = PyFrame_BlockPop(f);
1790 if (b->b_type != EXCEPT_HANDLER) {
1791 PyErr_SetString(PyExc_SystemError,
1792 "popped block is not an except handler");
1793 why = WHY_EXCEPTION;
1794 break;
1795 }
1796 UNWIND_EXCEPT_HANDLER(b);
1797 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001798 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001799
Antoine Pitroub52ec782009-01-25 16:34:23 +00001800 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001801 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001802 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001803 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001804 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001805 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001806
Christian Heimes180510d2008-03-03 19:15:45 +00001807 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001808 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001809 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001810 if (PyLong_Check(v)) {
1811 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001812 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001813 if (why == WHY_RETURN ||
1814 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001815 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001816 if (why == WHY_SILENCED) {
1817 /* An exception was silenced by 'with', we must
1818 manually unwind the EXCEPT_HANDLER block which was
1819 created when the exception was caught, otherwise
1820 the stack will be in an inconsistent state. */
1821 PyTryBlock *b = PyFrame_BlockPop(f);
1822 if (b->b_type != EXCEPT_HANDLER) {
1823 PyErr_SetString(PyExc_SystemError,
1824 "popped block is not an except handler");
1825 why = WHY_EXCEPTION;
1826 }
1827 else {
1828 UNWIND_EXCEPT_HANDLER(b);
1829 why = WHY_NOT;
1830 }
1831 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001832 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001833 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001834 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001835 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001836 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001838 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001839 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001840 else if (v != Py_None) {
1841 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001842 "'finally' pops bad exception");
1843 why = WHY_EXCEPTION;
1844 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001845 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001846 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001847
Antoine Pitroub52ec782009-01-25 16:34:23 +00001848 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001849 x = PyDict_GetItemString(f->f_builtins,
1850 "__build_class__");
1851 if (x == NULL) {
1852 PyErr_SetString(PyExc_ImportError,
1853 "__build_class__ not found");
1854 break;
1855 }
1856 Py_INCREF(x);
1857 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001859
Antoine Pitroub52ec782009-01-25 16:34:23 +00001860 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001861 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001863 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001864 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001865 err = PyDict_SetItem(x, w, v);
1866 else
1867 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001868 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001869 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001870 break;
1871 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001872 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001873 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001874 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Antoine Pitroub52ec782009-01-25 16:34:23 +00001876 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001877 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001878 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001879 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001880 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001881 NAME_ERROR_MSG,
1882 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001883 break;
1884 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001885 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001886 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001887 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001888
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001889 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001890 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001891 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001892 if (PyTuple_CheckExact(v) &&
1893 PyTuple_GET_SIZE(v) == oparg) {
1894 PyObject **items = \
1895 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001896 while (oparg--) {
1897 w = items[oparg];
1898 Py_INCREF(w);
1899 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001900 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001901 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001902 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001903 } else if (PyList_CheckExact(v) &&
1904 PyList_GET_SIZE(v) == oparg) {
1905 PyObject **items = \
1906 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001907 while (oparg--) {
1908 w = items[oparg];
1909 Py_INCREF(w);
1910 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001911 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001912 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001913 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001914 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001915 } else {
1916 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001917 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001918 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001919 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001921
Antoine Pitroub52ec782009-01-25 16:34:23 +00001922 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00001923 {
1924 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1925 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001926
Guido van Rossum0368b722007-05-11 16:50:42 +00001927 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1928 stack_pointer + totalargs)) {
1929 stack_pointer += totalargs;
1930 } else {
1931 why = WHY_EXCEPTION;
1932 }
1933 Py_DECREF(v);
1934 break;
1935 }
1936
Antoine Pitroub52ec782009-01-25 16:34:23 +00001937 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001938 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001939 v = TOP();
1940 u = SECOND();
1941 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001942 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1943 Py_DECREF(v);
1944 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001945 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001946 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Antoine Pitroub52ec782009-01-25 16:34:23 +00001948 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001949 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001950 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001951 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1952 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001953 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001954 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001955
Antoine Pitroub52ec782009-01-25 16:34:23 +00001956 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001957 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001958 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001959 err = PyDict_SetItem(f->f_globals, w, v);
1960 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001961 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001962 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001963
Antoine Pitroub52ec782009-01-25 16:34:23 +00001964 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001965 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001966 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001967 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001968 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001969 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001970
Antoine Pitroub52ec782009-01-25 16:34:23 +00001971 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001972 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001973 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001974 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001975 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001976 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001977 break;
1978 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001979 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001980 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001981 Py_XINCREF(x);
1982 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001983 else {
1984 x = PyObject_GetItem(v, w);
1985 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001986 if (!PyErr_ExceptionMatches(
1987 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001988 break;
1989 PyErr_Clear();
1990 }
1991 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001992 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001993 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001994 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001995 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001996 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001997 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001998 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001999 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002000 break;
2001 }
2002 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002003 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002005 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002006 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002007
Antoine Pitroub52ec782009-01-25 16:34:23 +00002008 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002009 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002010 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002011 /* Inline the PyDict_GetItem() calls.
2012 WARNING: this is an extreme speed hack.
2013 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002014 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002015 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002016 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002017 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002018 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002019 e = d->ma_lookup(d, w, hash);
2020 if (e == NULL) {
2021 x = NULL;
2022 break;
2023 }
2024 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002025 if (x != NULL) {
2026 Py_INCREF(x);
2027 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002028 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002029 }
2030 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002031 e = d->ma_lookup(d, w, hash);
2032 if (e == NULL) {
2033 x = NULL;
2034 break;
2035 }
2036 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002037 if (x != NULL) {
2038 Py_INCREF(x);
2039 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002040 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002041 }
2042 goto load_global_error;
2043 }
2044 }
2045 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002046 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002047 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002048 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002049 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002050 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002051 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002052 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002053 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002054 break;
2055 }
2056 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002057 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002059 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002060
Antoine Pitroub52ec782009-01-25 16:34:23 +00002061 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002062 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002063 if (x != NULL) {
2064 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002065 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002066 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002067 format_exc_check_arg(
2068 PyExc_UnboundLocalError,
2069 UNBOUNDLOCAL_ERROR_MSG,
2070 PyTuple_GetItem(co->co_varnames, oparg)
2071 );
2072 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002073
Antoine Pitroub52ec782009-01-25 16:34:23 +00002074 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002075 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002076 Py_INCREF(x);
2077 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002078 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002079 break;
2080
Antoine Pitroub52ec782009-01-25 16:34:23 +00002081 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002082 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002083 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002084 if (w != NULL) {
2085 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002086 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002087 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002088 err = -1;
2089 /* Don't stomp existing exception */
2090 if (PyErr_Occurred())
2091 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002092 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2093 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002094 oparg);
2095 format_exc_check_arg(
2096 PyExc_UnboundLocalError,
2097 UNBOUNDLOCAL_ERROR_MSG,
2098 v);
2099 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002100 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2101 PyTuple_GET_SIZE(co->co_cellvars));
2102 format_exc_check_arg(PyExc_NameError,
2103 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002104 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002105 break;
2106
Antoine Pitroub52ec782009-01-25 16:34:23 +00002107 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002108 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002109 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002110 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002111 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002112 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002113
Antoine Pitroub52ec782009-01-25 16:34:23 +00002114 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002115 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002116 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002117 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002118 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002119 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002120 }
2121 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002122 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002123 }
2124 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002125
Antoine Pitroub52ec782009-01-25 16:34:23 +00002126 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002127 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002128 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002129 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002130 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002131 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002132 }
2133 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002134 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002135 }
2136 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002137
Antoine Pitroub52ec782009-01-25 16:34:23 +00002138 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002139 x = PySet_New(NULL);
2140 if (x != NULL) {
2141 for (; --oparg >= 0;) {
2142 w = POP();
2143 if (err == 0)
2144 err = PySet_Add(x, w);
2145 Py_DECREF(w);
2146 }
2147 if (err != 0) {
2148 Py_DECREF(x);
2149 break;
2150 }
2151 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002152 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002153 }
2154 break;
2155
Antoine Pitroub52ec782009-01-25 16:34:23 +00002156 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002157 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002158 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002159 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002160 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002161
Antoine Pitroub52ec782009-01-25 16:34:23 +00002162 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002163 w = TOP(); /* key */
2164 u = SECOND(); /* value */
2165 v = THIRD(); /* dict */
2166 STACKADJ(-2);
2167 assert (PyDict_CheckExact(v));
2168 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2169 Py_DECREF(u);
2170 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002171 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002172 break;
2173
Antoine Pitroub52ec782009-01-25 16:34:23 +00002174 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002175 w = TOP(); /* key */
2176 u = SECOND(); /* value */
2177 STACKADJ(-2);
2178 v = stack_pointer[-oparg]; /* dict */
2179 assert (PyDict_CheckExact(v));
2180 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2181 Py_DECREF(u);
2182 Py_DECREF(w);
2183 if (err == 0) {
2184 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002185 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002186 }
2187 break;
2188
Antoine Pitroub52ec782009-01-25 16:34:23 +00002189 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002190 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002191 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002192 x = PyObject_GetAttr(v, w);
2193 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002194 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002195 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002196 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002197
Antoine Pitroub52ec782009-01-25 16:34:23 +00002198 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002199 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002200 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002201 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002202 Py_DECREF(v);
2203 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002204 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002205 if (x == NULL) break;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002206 PREDICT(POP_JUMP_IF_FALSE);
2207 PREDICT(POP_JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002208 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002209
Antoine Pitroub52ec782009-01-25 16:34:23 +00002210 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002211 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002212 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002213 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002214 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002215 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002216 break;
2217 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002218 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002219 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002220 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002221 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002222 w = PyTuple_Pack(5,
2223 w,
2224 f->f_globals,
2225 f->f_locals == NULL ?
2226 Py_None : f->f_locals,
2227 v,
2228 u);
2229 else
2230 w = PyTuple_Pack(4,
2231 w,
2232 f->f_globals,
2233 f->f_locals == NULL ?
2234 Py_None : f->f_locals,
2235 v);
2236 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002237 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002238 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002239 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002240 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002241 x = NULL;
2242 break;
2243 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002244 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002245 v = x;
2246 x = PyEval_CallObject(v, w);
2247 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002248 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002249 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002250 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002251 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002252 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002253
Antoine Pitroub52ec782009-01-25 16:34:23 +00002254 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002255 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002256 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002257 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002258 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002259 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002260 break;
2261 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002262 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002263 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002264 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002265 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002266 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002267 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002268 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002269
Antoine Pitroub52ec782009-01-25 16:34:23 +00002270 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002271 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002272 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002273 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002274 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002275 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002276 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002277 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002278 break;
2279
Antoine Pitroub52ec782009-01-25 16:34:23 +00002280 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002281 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002282 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002284 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2285 TARGET(POP_JUMP_IF_FALSE)
2286 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002287 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002288 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002289 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002290 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002291 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002292 Py_DECREF(w);
2293 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002294 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002295 }
2296 err = PyObject_IsTrue(w);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002297 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002298 if (err > 0)
2299 err = 0;
2300 else if (err == 0)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002301 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002302 else
2303 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002304 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002305
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002306 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2307 TARGET(POP_JUMP_IF_TRUE)
2308 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002309 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002310 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002311 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002312 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002313 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002314 Py_DECREF(w);
2315 JUMPTO(oparg);
2316 FAST_DISPATCH();
2317 }
2318 err = PyObject_IsTrue(w);
2319 Py_DECREF(w);
2320 if (err > 0) {
2321 err = 0;
2322 JUMPTO(oparg);
2323 }
2324 else if (err == 0)
2325 ;
2326 else
2327 break;
2328 DISPATCH();
2329
2330 TARGET(JUMP_IF_FALSE_OR_POP)
2331 w = TOP();
2332 if (w == Py_True) {
2333 STACKADJ(-1);
2334 Py_DECREF(w);
2335 FAST_DISPATCH();
2336 }
2337 if (w == Py_False) {
2338 JUMPTO(oparg);
2339 FAST_DISPATCH();
2340 }
2341 err = PyObject_IsTrue(w);
2342 if (err > 0) {
2343 STACKADJ(-1);
2344 Py_DECREF(w);
2345 err = 0;
2346 }
2347 else if (err == 0)
2348 JUMPTO(oparg);
2349 else
2350 break;
2351 DISPATCH();
2352
2353 TARGET(JUMP_IF_TRUE_OR_POP)
2354 w = TOP();
2355 if (w == Py_False) {
2356 STACKADJ(-1);
2357 Py_DECREF(w);
2358 FAST_DISPATCH();
2359 }
2360 if (w == Py_True) {
2361 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002362 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002363 }
2364 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002365 if (err > 0) {
2366 err = 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002367 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002368 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002369 else if (err == 0) {
2370 STACKADJ(-1);
2371 Py_DECREF(w);
2372 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002373 else
2374 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002375 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002376
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002377 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002378 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002379 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002380#if FAST_LOOPS
2381 /* Enabling this path speeds-up all while and for-loops by bypassing
2382 the per-loop checks for signals. By default, this should be turned-off
2383 because it prevents detection of a control-break in tight loops like
2384 "while 1: pass". Compile with this option turned-on when you need
2385 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002386 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002387 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002388 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002389#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002390 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002391#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002392
Antoine Pitroub52ec782009-01-25 16:34:23 +00002393 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002394 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002395 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002396 x = PyObject_GetIter(v);
2397 Py_DECREF(v);
2398 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002399 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002400 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002401 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002402 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002403 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002404 break;
2405
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002406 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002407 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002408 /* before: [iter]; after: [iter, iter()] *or* [] */
2409 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002410 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002411 if (x != NULL) {
2412 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002413 PREDICT(STORE_FAST);
2414 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002415 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002416 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002417 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002418 if (!PyErr_ExceptionMatches(
2419 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002420 break;
2421 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002422 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002423 /* iterator ended normally */
2424 x = v = POP();
2425 Py_DECREF(v);
2426 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002427 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002428
Antoine Pitroub52ec782009-01-25 16:34:23 +00002429 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002430 why = WHY_BREAK;
2431 goto fast_block_end;
2432
Antoine Pitroub52ec782009-01-25 16:34:23 +00002433 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002434 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002435 if (!retval) {
2436 x = NULL;
2437 break;
2438 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002439 why = WHY_CONTINUE;
2440 goto fast_block_end;
2441
Antoine Pitroub52ec782009-01-25 16:34:23 +00002442 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2443 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2444 TARGET(SETUP_FINALLY)
2445 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002446 /* NOTE: If you add any new block-setup opcodes that
2447 are not try/except/finally handlers, you may need
2448 to update the PyGen_NeedsFinalizing() function.
2449 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002450
Guido van Rossumb209a111997-04-29 18:18:01 +00002451 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002452 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002453 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002454
Antoine Pitroub52ec782009-01-25 16:34:23 +00002455 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002456 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002457 /* At the top of the stack are 1-3 values indicating
2458 how/why we entered the finally clause:
2459 - TOP = None
2460 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2461 - TOP = WHY_*; no retval below it
2462 - (TOP, SECOND, THIRD) = exc_info()
2463 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002464 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002465 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002466 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002467 EXIT(None, None, None)
2468
2469 In all cases, we remove EXIT from the stack, leaving
2470 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002471
2472 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002473 *and* the function call returns a 'true' value, we
2474 "zap" this information, to prevent END_FINALLY from
2475 re-raising the exception. (But non-local gotos
2476 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002477 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002478
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002479 PyObject *exit_func = POP();
2480 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002481 if (u == Py_None) {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002482 v = w = Py_None;
2483 }
2484 else if (PyLong_Check(u)) {
Guido van Rossumc2e20742006-02-27 22:32:47 +00002485 u = v = w = Py_None;
2486 }
2487 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002488 v = SECOND();
2489 w = THIRD();
Guido van Rossumc2e20742006-02-27 22:32:47 +00002490 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002491 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002492 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2493 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002494 Py_DECREF(exit_func);
2495 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002496 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002497
2498 if (u != Py_None)
2499 err = PyObject_IsTrue(x);
2500 else
2501 err = 0;
2502 Py_DECREF(x);
2503
2504 if (err < 0)
2505 break; /* Go to error exit */
2506 else if (err > 0) {
2507 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002508 /* There was an exception and a True return */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002509 STACKADJ(-2);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002510 SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002511 Py_DECREF(u);
2512 Py_DECREF(v);
2513 Py_DECREF(w);
Guido van Rossumf6694362006-03-10 02:28:35 +00002514 }
Christian Heimes180510d2008-03-03 19:15:45 +00002515 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002516 break;
2517 }
2518
Antoine Pitroub52ec782009-01-25 16:34:23 +00002519 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002520 {
2521 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002522 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002523 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002524#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002525 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002526#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002527 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002528#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002529 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002530 PUSH(x);
2531 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002532 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002533 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002535
Antoine Pitroub52ec782009-01-25 16:34:23 +00002536 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2537 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2538 TARGET(CALL_FUNCTION_VAR_KW)
2539 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002540 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002541 int na = oparg & 0xff;
2542 int nk = (oparg>>8) & 0xff;
2543 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002544 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002545 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002546 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002547 if (flags & CALL_FLAG_VAR)
2548 n++;
2549 if (flags & CALL_FLAG_KW)
2550 n++;
2551 pfunc = stack_pointer - n - 1;
2552 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002553
Guido van Rossumac7be682001-01-17 15:42:30 +00002554 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002555 && PyMethod_GET_SELF(func) != NULL) {
2556 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002557 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002558 func = PyMethod_GET_FUNCTION(func);
2559 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002560 Py_DECREF(*pfunc);
2561 *pfunc = self;
2562 na++;
2563 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002564 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002565 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002566 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002567 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002568 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002569 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002570 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002571 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002572
Jeremy Hylton76901512000-03-28 23:49:17 +00002573 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002574 w = POP();
2575 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002576 }
2577 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002578 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002579 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002580 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002581 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002582
Antoine Pitroub52ec782009-01-25 16:34:23 +00002583 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2584 TARGET(MAKE_FUNCTION)
2585 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002586 {
2587 int posdefaults = oparg & 0xff;
2588 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002589 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002590
Guido van Rossum681d79a1995-07-18 14:51:37 +00002591 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002592 x = PyFunction_New(v, f->f_globals);
2593 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002594
Guido van Rossum0240b922007-02-26 21:23:50 +00002595 if (x != NULL && opcode == MAKE_CLOSURE) {
2596 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002597 if (PyFunction_SetClosure(x, v) != 0) {
2598 /* Can't happen unless bytecode is corrupt. */
2599 why = WHY_EXCEPTION;
2600 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002601 Py_DECREF(v);
2602 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002603
2604 if (x != NULL && num_annotations > 0) {
2605 Py_ssize_t name_ix;
2606 u = POP(); /* names of args with annotations */
2607 v = PyDict_New();
2608 if (v == NULL) {
2609 Py_DECREF(x);
2610 x = NULL;
2611 break;
2612 }
2613 name_ix = PyTuple_Size(u);
2614 assert(num_annotations == name_ix+1);
2615 while (name_ix > 0) {
2616 --name_ix;
2617 t = PyTuple_GET_ITEM(u, name_ix);
2618 w = POP();
2619 /* XXX(nnorwitz): check for errors */
2620 PyDict_SetItem(v, t, w);
2621 Py_DECREF(w);
2622 }
2623
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002624 if (PyFunction_SetAnnotations(x, v) != 0) {
2625 /* Can't happen unless
2626 PyFunction_SetAnnotations changes. */
2627 why = WHY_EXCEPTION;
2628 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002629 Py_DECREF(v);
2630 Py_DECREF(u);
2631 }
2632
Guido van Rossum681d79a1995-07-18 14:51:37 +00002633 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002634 if (x != NULL && posdefaults > 0) {
2635 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002636 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002637 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002638 x = NULL;
2639 break;
2640 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002641 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002642 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002643 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002644 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002645 if (PyFunction_SetDefaults(x, v) != 0) {
2646 /* Can't happen unless
2647 PyFunction_SetDefaults changes. */
2648 why = WHY_EXCEPTION;
2649 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002650 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002651 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002652 if (x != NULL && kwdefaults > 0) {
2653 v = PyDict_New();
2654 if (v == NULL) {
2655 Py_DECREF(x);
2656 x = NULL;
2657 break;
2658 }
2659 while (--kwdefaults >= 0) {
2660 w = POP(); /* default value */
2661 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002662 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002663 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002664 Py_DECREF(w);
2665 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002666 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002667 if (PyFunction_SetKwDefaults(x, v) != 0) {
2668 /* Can't happen unless
2669 PyFunction_SetKwDefaults changes. */
2670 why = WHY_EXCEPTION;
2671 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002672 Py_DECREF(v);
2673 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002674 PUSH(x);
2675 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002676 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002677
Antoine Pitroub52ec782009-01-25 16:34:23 +00002678 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002679 if (oparg == 3)
2680 w = POP();
2681 else
2682 w = NULL;
2683 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002684 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002685 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002686 Py_DECREF(u);
2687 Py_DECREF(v);
2688 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002689 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002690 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002691 break;
2692
Antoine Pitroub52ec782009-01-25 16:34:23 +00002693 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002694 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002695 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002696 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002697
Antoine Pitroub52ec782009-01-25 16:34:23 +00002698#ifdef USE_COMPUTED_GOTOS
2699 _unknown_opcode:
2700#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002701 default:
2702 fprintf(stderr,
2703 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002704 PyCode_Addr2Line(f->f_code, f->f_lasti),
2705 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002706 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002707 why = WHY_EXCEPTION;
2708 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002709
2710#ifdef CASE_TOO_BIG
2711 }
2712#endif
2713
Guido van Rossum374a9221991-04-04 10:40:29 +00002714 } /* switch */
2715
2716 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002717
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002718 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002719
Guido van Rossum374a9221991-04-04 10:40:29 +00002720 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002721
Guido van Rossum374a9221991-04-04 10:40:29 +00002722 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002723 if (err == 0 && x != NULL) {
2724#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002725 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002726 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002727 fprintf(stderr,
2728 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002729 else {
2730#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002731 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002732 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002733#ifdef CHECKEXC
2734 }
2735#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002736 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002737 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002738 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002739 err = 0;
2740 }
2741
Guido van Rossum374a9221991-04-04 10:40:29 +00002742 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002743
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002744 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002745 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002746 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002747 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002748 why = WHY_EXCEPTION;
2749 }
2750 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002751#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002752 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002753 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002754 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002755 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002756 sprintf(buf, "Stack unwind with exception "
2757 "set and why=%d", why);
2758 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002759 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002760 }
2761#endif
2762
2763 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002764
Guido van Rossum374a9221991-04-04 10:40:29 +00002765 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002766 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002767
Fred Drake8f51f542001-10-04 14:48:42 +00002768 if (tstate->c_tracefunc != NULL)
2769 call_exc_trace(tstate->c_tracefunc,
2770 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002771 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002772
Guido van Rossum374a9221991-04-04 10:40:29 +00002773 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002774
Guido van Rossum374a9221991-04-04 10:40:29 +00002775 if (why == WHY_RERAISE)
2776 why = WHY_EXCEPTION;
2777
2778 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002779
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002780fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002781 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002782 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002783
Tim Peters8a5c3c72004-04-05 19:36:21 +00002784 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002785 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2786 /* For a continue inside a try block,
2787 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002788 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2789 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002790 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002791 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002792 Py_DECREF(retval);
2793 break;
2794 }
2795
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002796 if (b->b_type == EXCEPT_HANDLER) {
2797 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002798 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002799 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002800 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002801 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2802 why = WHY_NOT;
2803 JUMPTO(b->b_handler);
2804 break;
2805 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002806 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2807 || b->b_type == SETUP_FINALLY)) {
2808 PyObject *exc, *val, *tb;
2809 int handler = b->b_handler;
2810 /* Beware, this invalidates all b->b_* fields */
2811 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2812 PUSH(tstate->exc_traceback);
2813 PUSH(tstate->exc_value);
2814 if (tstate->exc_type != NULL) {
2815 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002816 }
2817 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002818 Py_INCREF(Py_None);
2819 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002820 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002821 PyErr_Fetch(&exc, &val, &tb);
2822 /* Make the raw exception data
2823 available to the handler,
2824 so a program can emulate the
2825 Python main loop. */
2826 PyErr_NormalizeException(
2827 &exc, &val, &tb);
2828 PyException_SetTraceback(val, tb);
2829 Py_INCREF(exc);
2830 tstate->exc_type = exc;
2831 Py_INCREF(val);
2832 tstate->exc_value = val;
2833 tstate->exc_traceback = tb;
2834 if (tb == NULL)
2835 tb = Py_None;
2836 Py_INCREF(tb);
2837 PUSH(tb);
2838 PUSH(val);
2839 PUSH(exc);
2840 why = WHY_NOT;
2841 JUMPTO(handler);
2842 break;
2843 }
2844 if (b->b_type == SETUP_FINALLY) {
2845 if (why & (WHY_RETURN | WHY_CONTINUE))
2846 PUSH(retval);
2847 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002848 why = WHY_NOT;
2849 JUMPTO(b->b_handler);
2850 break;
2851 }
2852 } /* unwind stack */
2853
2854 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002855
Guido van Rossum374a9221991-04-04 10:40:29 +00002856 if (why != WHY_NOT)
2857 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002858 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002859
Guido van Rossum374a9221991-04-04 10:40:29 +00002860 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002861
Tim Peters8a5c3c72004-04-05 19:36:21 +00002862 assert(why != WHY_YIELD);
2863 /* Pop remaining stack entries. */
2864 while (!EMPTY()) {
2865 v = POP();
2866 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002867 }
2868
Tim Peters8a5c3c72004-04-05 19:36:21 +00002869 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002870 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002871
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002872fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002873 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002874 if (tstate->c_tracefunc) {
2875 if (why == WHY_RETURN || why == WHY_YIELD) {
2876 if (call_trace(tstate->c_tracefunc,
2877 tstate->c_traceobj, f,
2878 PyTrace_RETURN, retval)) {
2879 Py_XDECREF(retval);
2880 retval = NULL;
2881 why = WHY_EXCEPTION;
2882 }
2883 }
2884 else if (why == WHY_EXCEPTION) {
2885 call_trace_protected(tstate->c_tracefunc,
2886 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002887 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002888 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002889 }
Fred Drake8f51f542001-10-04 14:48:42 +00002890 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002891 if (why == WHY_EXCEPTION)
2892 call_trace_protected(tstate->c_profilefunc,
2893 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002894 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002895 else if (call_trace(tstate->c_profilefunc,
2896 tstate->c_profileobj, f,
2897 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002898 Py_XDECREF(retval);
2899 retval = NULL;
2900 why = WHY_EXCEPTION;
2901 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002902 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002903 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002904
Tim Peters5ca576e2001-06-18 22:08:13 +00002905 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002906exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002907 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002908 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002909
Guido van Rossum96a42c81992-01-12 02:29:51 +00002910 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002911}
2912
Guido van Rossumc2e20742006-02-27 22:32:47 +00002913/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002914 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002915 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002916
Tim Peters6d6c1a32001-08-02 04:15:00 +00002917PyObject *
2918PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002919 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002920 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002921{
2922 register PyFrameObject *f;
2923 register PyObject *retval = NULL;
2924 register PyObject **fastlocals, **freevars;
2925 PyThreadState *tstate = PyThreadState_GET();
2926 PyObject *x, *u;
2927
2928 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002929 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002930 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002931 return NULL;
2932 }
2933
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002934 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002935 assert(globals != NULL);
2936 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002937 if (f == NULL)
2938 return NULL;
2939
2940 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002941 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002942
2943 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002944 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002945 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2946 int i;
2947 int n = argcount;
2948 PyObject *kwdict = NULL;
2949 if (co->co_flags & CO_VARKEYWORDS) {
2950 kwdict = PyDict_New();
2951 if (kwdict == NULL)
2952 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002953 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002954 if (co->co_flags & CO_VARARGS)
2955 i++;
2956 SETLOCAL(i, kwdict);
2957 }
2958 if (argcount > co->co_argcount) {
2959 if (!(co->co_flags & CO_VARARGS)) {
2960 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002961 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002962 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002963 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002964 defcount ? "at most" : "exactly",
2965 co->co_argcount,
2966 kwcount ? "non-keyword " : "",
2967 co->co_argcount == 1 ? "" : "s",
2968 argcount);
2969 goto fail;
2970 }
2971 n = co->co_argcount;
2972 }
2973 for (i = 0; i < n; i++) {
2974 x = args[i];
2975 Py_INCREF(x);
2976 SETLOCAL(i, x);
2977 }
2978 if (co->co_flags & CO_VARARGS) {
2979 u = PyTuple_New(argcount - n);
2980 if (u == NULL)
2981 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002982 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002983 for (i = n; i < argcount; i++) {
2984 x = args[i];
2985 Py_INCREF(x);
2986 PyTuple_SET_ITEM(u, i-n, x);
2987 }
2988 }
2989 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002990 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002991 PyObject *keyword = kws[2*i];
2992 PyObject *value = kws[2*i + 1];
2993 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00002994 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002995 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002996 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002997 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00002998 goto fail;
2999 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003000 /* Speed hack: do raw pointer compares. As names are
3001 normally interned this should almost always hit. */
3002 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003003 for (j = 0;
3004 j < co->co_argcount + co->co_kwonlyargcount;
3005 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003006 PyObject *nm = co_varnames[j];
3007 if (nm == keyword)
3008 goto kw_found;
3009 }
3010 /* Slow fallback, just in case */
3011 for (j = 0;
3012 j < co->co_argcount + co->co_kwonlyargcount;
3013 j++) {
3014 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003015 int cmp = PyObject_RichCompareBool(
3016 keyword, nm, Py_EQ);
3017 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003018 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003019 else if (cmp < 0)
3020 goto fail;
3021 }
3022 /* Check errors from Compare */
3023 if (PyErr_Occurred())
3024 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003025 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003026 if (kwdict == NULL) {
3027 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003028 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00003029 "keyword argument '%S'",
3030 co->co_name,
3031 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00003032 goto fail;
3033 }
3034 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003035 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003036 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003037kw_found:
3038 if (GETLOCAL(j) != NULL) {
3039 PyErr_Format(PyExc_TypeError,
3040 "%U() got multiple "
3041 "values for keyword "
3042 "argument '%S'",
3043 co->co_name,
3044 keyword);
3045 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003046 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003047 Py_INCREF(value);
3048 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003049 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003050 if (co->co_kwonlyargcount > 0) {
3051 for (i = co->co_argcount;
3052 i < co->co_argcount + co->co_kwonlyargcount;
3053 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003054 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003055 if (GETLOCAL(i) != NULL)
3056 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003057 name = PyTuple_GET_ITEM(co->co_varnames, i);
3058 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003059 if (kwdefs != NULL)
3060 def = PyDict_GetItem(kwdefs, name);
3061 if (def != NULL) {
3062 Py_INCREF(def);
3063 SETLOCAL(i, def);
3064 continue;
3065 }
3066 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003067 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003068 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003069 goto fail;
3070 }
3071 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003072 if (argcount < co->co_argcount) {
3073 int m = co->co_argcount - defcount;
3074 for (i = argcount; i < m; i++) {
3075 if (GETLOCAL(i) == NULL) {
3076 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003077 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003078 "%spositional argument%s "
3079 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003080 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003081 ((co->co_flags & CO_VARARGS) ||
3082 defcount) ? "at least"
3083 : "exactly",
3084 m, kwcount ? "non-keyword " : "",
3085 m == 1 ? "" : "s", i);
3086 goto fail;
3087 }
3088 }
3089 if (n > m)
3090 i = n - m;
3091 else
3092 i = 0;
3093 for (; i < defcount; i++) {
3094 if (GETLOCAL(m+i) == NULL) {
3095 PyObject *def = defs[i];
3096 Py_INCREF(def);
3097 SETLOCAL(m+i, def);
3098 }
3099 }
3100 }
3101 }
3102 else {
3103 if (argcount > 0 || kwcount > 0) {
3104 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003105 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003106 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003107 argcount + kwcount);
3108 goto fail;
3109 }
3110 }
3111 /* Allocate and initialize storage for cell vars, and copy free
3112 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003113 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003114 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003115 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003116 PyObject *c;
3117
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003118 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003119 if (co->co_flags & CO_VARARGS)
3120 nargs++;
3121 if (co->co_flags & CO_VARKEYWORDS)
3122 nargs++;
3123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 /* Initialize each cell var, taking into account
3125 cell vars that are initialized from arguments.
3126
3127 Should arrange for the compiler to put cellvars
3128 that are arguments at the beginning of the cellvars
3129 list so that we can march over it more efficiently?
3130 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003131 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003132 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003133 PyTuple_GET_ITEM(co->co_cellvars, i));
3134 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003136 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003137 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003138 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003139 c = PyCell_New(GETLOCAL(j));
3140 if (c == NULL)
3141 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003142 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003143 found = 1;
3144 break;
3145 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003146 }
3147 if (found == 0) {
3148 c = PyCell_New(NULL);
3149 if (c == NULL)
3150 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003151 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003152 }
3153 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003154 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003155 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003156 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003157 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003158 PyObject *o = PyTuple_GET_ITEM(closure, i);
3159 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003160 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003161 }
3162 }
3163
Tim Peters5ca576e2001-06-18 22:08:13 +00003164 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003165 /* Don't need to keep the reference to f_back, it will be set
3166 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003167 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003168 f->f_back = NULL;
3169
Jeremy Hylton985eba52003-02-05 23:13:00 +00003170 PCALL(PCALL_GENERATOR);
3171
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003172 /* Create a new generator that owns the ready to run frame
3173 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003174 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003175 }
3176
Thomas Woutersce272b62007-09-19 21:19:28 +00003177 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003178
Thomas Woutersce272b62007-09-19 21:19:28 +00003179fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003180
Tim Petersb13680b2001-11-27 23:29:29 +00003181 /* decref'ing the frame can cause __del__ methods to get invoked,
3182 which can call back into Python. While we're done with the
3183 current Python frame (f), the associated C stack is still in use,
3184 so recursion_depth must be boosted for the duration.
3185 */
3186 assert(tstate != NULL);
3187 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003188 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003189 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003190 return retval;
3191}
3192
3193
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003194/* Logic for the raise statement (too complicated for inlining).
3195 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003196static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003197do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003198{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003199 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003200
3201 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003202 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003203 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003204 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003205 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003206 value = tstate->exc_value;
3207 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003208 if (type == Py_None) {
3209 PyErr_SetString(PyExc_RuntimeError,
3210 "No active exception to reraise");
3211 return WHY_EXCEPTION;
3212 }
3213 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003214 Py_XINCREF(value);
3215 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003216 PyErr_Restore(type, value, tb);
3217 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003219
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003220 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003221 raise
3222 raise <instance>
3223 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003224
Collin Winter828f04a2007-08-31 00:04:24 +00003225 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003226 type = exc;
3227 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003228 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003229 goto raise_error;
3230 }
Collin Winter828f04a2007-08-31 00:04:24 +00003231 else if (PyExceptionInstance_Check(exc)) {
3232 value = exc;
3233 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003234 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003235 }
3236 else {
3237 /* Not something you can raise. You get an exception
3238 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003239 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003240 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003241 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003242 goto raise_error;
3243 }
Collin Winter828f04a2007-08-31 00:04:24 +00003244
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003245 if (cause) {
3246 PyObject *fixed_cause;
3247 if (PyExceptionClass_Check(cause)) {
3248 fixed_cause = PyObject_CallObject(cause, NULL);
3249 if (fixed_cause == NULL)
3250 goto raise_error;
3251 Py_DECREF(cause);
3252 }
3253 else if (PyExceptionInstance_Check(cause)) {
3254 fixed_cause = cause;
3255 }
3256 else {
3257 PyErr_SetString(PyExc_TypeError,
3258 "exception causes must derive from "
3259 "BaseException");
3260 goto raise_error;
3261 }
3262 PyException_SetCause(value, fixed_cause);
3263 }
Collin Winter828f04a2007-08-31 00:04:24 +00003264
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003265 PyErr_SetObject(type, value);
3266 /* PyErr_SetObject incref's its arguments */
3267 Py_XDECREF(value);
3268 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003269 return WHY_EXCEPTION;
3270
3271raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003272 Py_XDECREF(value);
3273 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003274 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003275 return WHY_EXCEPTION;
3276}
3277
Tim Petersd6d010b2001-06-21 02:49:55 +00003278/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003279 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003280
Guido van Rossum0368b722007-05-11 16:50:42 +00003281 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3282 with a variable target.
3283*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003284
Barry Warsawe42b18f1997-08-25 22:13:04 +00003285static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003286unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003287{
Guido van Rossum0368b722007-05-11 16:50:42 +00003288 int i = 0, j = 0;
3289 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003290 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003291 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003292 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003293
Tim Petersd6d010b2001-06-21 02:49:55 +00003294 assert(v != NULL);
3295
3296 it = PyObject_GetIter(v);
3297 if (it == NULL)
3298 goto Error;
3299
3300 for (; i < argcnt; i++) {
3301 w = PyIter_Next(it);
3302 if (w == NULL) {
3303 /* Iterator done, via error or exhaustion. */
3304 if (!PyErr_Occurred()) {
3305 PyErr_Format(PyExc_ValueError,
3306 "need more than %d value%s to unpack",
3307 i, i == 1 ? "" : "s");
3308 }
3309 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003310 }
3311 *--sp = w;
3312 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003313
Guido van Rossum0368b722007-05-11 16:50:42 +00003314 if (argcntafter == -1) {
3315 /* We better have exhausted the iterator now. */
3316 w = PyIter_Next(it);
3317 if (w == NULL) {
3318 if (PyErr_Occurred())
3319 goto Error;
3320 Py_DECREF(it);
3321 return 1;
3322 }
3323 Py_DECREF(w);
3324 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3325 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003326 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003327
3328 l = PySequence_List(it);
3329 if (l == NULL)
3330 goto Error;
3331 *--sp = l;
3332 i++;
3333
3334 ll = PyList_GET_SIZE(l);
3335 if (ll < argcntafter) {
3336 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3337 argcnt + ll);
3338 goto Error;
3339 }
3340
3341 /* Pop the "after-variable" args off the list. */
3342 for (j = argcntafter; j > 0; j--, i++) {
3343 *--sp = PyList_GET_ITEM(l, ll - j);
3344 }
3345 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003346 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003347 Py_DECREF(it);
3348 return 1;
3349
Tim Petersd6d010b2001-06-21 02:49:55 +00003350Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003351 for (; i > 0; i--, sp++)
3352 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003353 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003354 return 0;
3355}
3356
3357
Guido van Rossum96a42c81992-01-12 02:29:51 +00003358#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003359static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003360prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003361{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003362 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003363 if (PyObject_Print(v, stdout, 0) != 0)
3364 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003365 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003366 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003367}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003368#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003369
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003370static void
Fred Drake5755ce62001-06-27 19:19:46 +00003371call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003372{
Guido van Rossumb209a111997-04-29 18:18:01 +00003373 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003374 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003375 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003376 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003377 value = Py_None;
3378 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003379 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003380 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003381 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003382 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003383 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003384 }
Fred Drake5755ce62001-06-27 19:19:46 +00003385 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003386 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003387 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003388 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003389 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003390 Py_XDECREF(type);
3391 Py_XDECREF(value);
3392 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003393 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003394}
3395
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003396static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003397call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003398 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003399{
3400 PyObject *type, *value, *traceback;
3401 int err;
3402 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003403 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003404 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003405 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003406 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003407 return 0;
3408 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003409 else {
3410 Py_XDECREF(type);
3411 Py_XDECREF(value);
3412 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003413 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003414 }
3415}
3416
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003417static int
Fred Drake5755ce62001-06-27 19:19:46 +00003418call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3419 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003420{
Fred Drake5755ce62001-06-27 19:19:46 +00003421 register PyThreadState *tstate = frame->f_tstate;
3422 int result;
3423 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003424 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003425 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003426 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003427 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003428 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3429 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003430 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003431 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003432}
3433
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003434PyObject *
3435_PyEval_CallTracing(PyObject *func, PyObject *args)
3436{
3437 PyFrameObject *frame = PyEval_GetFrame();
3438 PyThreadState *tstate = frame->f_tstate;
3439 int save_tracing = tstate->tracing;
3440 int save_use_tracing = tstate->use_tracing;
3441 PyObject *result;
3442
3443 tstate->tracing = 0;
3444 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3445 || (tstate->c_profilefunc != NULL));
3446 result = PyObject_Call(func, args, NULL);
3447 tstate->tracing = save_tracing;
3448 tstate->use_tracing = save_use_tracing;
3449 return result;
3450}
3451
Michael W. Hudson006c7522002-11-08 13:08:46 +00003452static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003453maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003454 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3455 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003456{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003457 int result = 0;
3458
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003459 /* If the last instruction executed isn't in the current
3460 instruction window, reset the window. If the last
3461 instruction happens to fall at the start of a line or if it
3462 represents a jump backwards, call the trace function.
3463 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003464 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003465 int line;
3466 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003467
Thomas Woutersce272b62007-09-19 21:19:28 +00003468 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3469 &bounds);
3470 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003471 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003472 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003473 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003474 }
3475 *instr_lb = bounds.ap_lower;
3476 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003477 }
Armin Rigobf57a142004-03-22 19:24:58 +00003478 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003479 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003480 }
3481 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003482 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003483}
3484
Fred Drake5755ce62001-06-27 19:19:46 +00003485void
3486PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003487{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003488 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003489 PyObject *temp = tstate->c_profileobj;
3490 Py_XINCREF(arg);
3491 tstate->c_profilefunc = NULL;
3492 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003493 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003494 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003495 Py_XDECREF(temp);
3496 tstate->c_profilefunc = func;
3497 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003498 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003499 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003500}
3501
3502void
3503PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3504{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003505 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003506 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003507 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003508 Py_XINCREF(arg);
3509 tstate->c_tracefunc = NULL;
3510 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003511 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003512 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003513 Py_XDECREF(temp);
3514 tstate->c_tracefunc = func;
3515 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003516 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003517 tstate->use_tracing = ((func != NULL)
3518 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003519}
3520
Guido van Rossumb209a111997-04-29 18:18:01 +00003521PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003522PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003523{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003524 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003525 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003526 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003527 else
3528 return current_frame->f_builtins;
3529}
3530
Guido van Rossumb209a111997-04-29 18:18:01 +00003531PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003532PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003533{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003534 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003535 if (current_frame == NULL)
3536 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003537 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003538 return current_frame->f_locals;
3539}
3540
Guido van Rossumb209a111997-04-29 18:18:01 +00003541PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003542PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003543{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003544 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003545 if (current_frame == NULL)
3546 return NULL;
3547 else
3548 return current_frame->f_globals;
3549}
3550
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003551PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003552PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003553{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003554 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003555 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003556}
3557
Guido van Rossum6135a871995-01-09 17:53:26 +00003558int
Tim Peters5ba58662001-07-16 02:29:45 +00003559PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003560{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003561 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003562 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003563
3564 if (current_frame != NULL) {
3565 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003566 const int compilerflags = codeflags & PyCF_MASK;
3567 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003568 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003569 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003570 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003571#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003572 if (codeflags & CO_GENERATOR_ALLOWED) {
3573 result = 1;
3574 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3575 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003576#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003577 }
3578 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003579}
3580
Guido van Rossum3f5da241990-12-20 15:06:42 +00003581
Guido van Rossum681d79a1995-07-18 14:51:37 +00003582/* External interface to call any callable object.
3583 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003584
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003585#undef PyEval_CallObject
3586/* for backward compatibility: export this interface */
3587
Guido van Rossumb209a111997-04-29 18:18:01 +00003588PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003589PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003590{
Guido van Rossumb209a111997-04-29 18:18:01 +00003591 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003592}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003593#define PyEval_CallObject(func,arg) \
3594 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003595
Guido van Rossumb209a111997-04-29 18:18:01 +00003596PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003597PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003598{
Jeremy Hylton52820442001-01-03 23:52:36 +00003599 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003600
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003601 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003602 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003603 if (arg == NULL)
3604 return NULL;
3605 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003606 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003607 PyErr_SetString(PyExc_TypeError,
3608 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003609 return NULL;
3610 }
3611 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003612 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003613
Guido van Rossumb209a111997-04-29 18:18:01 +00003614 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003615 PyErr_SetString(PyExc_TypeError,
3616 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003617 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003618 return NULL;
3619 }
3620
Tim Peters6d6c1a32001-08-02 04:15:00 +00003621 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003622 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003623 return result;
3624}
3625
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003626const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003627PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003628{
3629 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003630 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003631 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003632 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003633 else if (PyCFunction_Check(func))
3634 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003635 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003636 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003637}
3638
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003639const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003640PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003641{
3642 if (PyMethod_Check(func))
3643 return "()";
3644 else if (PyFunction_Check(func))
3645 return "()";
3646 else if (PyCFunction_Check(func))
3647 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003648 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003649 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003650}
3651
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003652static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003653err_args(PyObject *func, int flags, int nargs)
3654{
3655 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003656 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003657 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003658 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003659 nargs);
3660 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003661 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003662 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003663 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003664 nargs);
3665}
3666
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003667#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003668if (tstate->use_tracing && tstate->c_profilefunc) { \
3669 if (call_trace(tstate->c_profilefunc, \
3670 tstate->c_profileobj, \
3671 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003672 func)) { \
3673 x = NULL; \
3674 } \
3675 else { \
3676 x = call; \
3677 if (tstate->c_profilefunc != NULL) { \
3678 if (x == NULL) { \
3679 call_trace_protected(tstate->c_profilefunc, \
3680 tstate->c_profileobj, \
3681 tstate->frame, PyTrace_C_EXCEPTION, \
3682 func); \
3683 /* XXX should pass (type, value, tb) */ \
3684 } else { \
3685 if (call_trace(tstate->c_profilefunc, \
3686 tstate->c_profileobj, \
3687 tstate->frame, PyTrace_C_RETURN, \
3688 func)) { \
3689 Py_DECREF(x); \
3690 x = NULL; \
3691 } \
3692 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003693 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003694 } \
3695} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003696 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003697 }
3698
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003699static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003700call_function(PyObject ***pp_stack, int oparg
3701#ifdef WITH_TSC
3702 , uint64* pintr0, uint64* pintr1
3703#endif
3704 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003705{
3706 int na = oparg & 0xff;
3707 int nk = (oparg>>8) & 0xff;
3708 int n = na + 2 * nk;
3709 PyObject **pfunc = (*pp_stack) - n - 1;
3710 PyObject *func = *pfunc;
3711 PyObject *x, *w;
3712
Jeremy Hylton985eba52003-02-05 23:13:00 +00003713 /* Always dispatch PyCFunction first, because these are
3714 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003715 */
3716 if (PyCFunction_Check(func) && nk == 0) {
3717 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003718 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003719
3720 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003721 if (flags & (METH_NOARGS | METH_O)) {
3722 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3723 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003724 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003725 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003726 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003727 else if (flags & METH_O && na == 1) {
3728 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003729 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003730 Py_DECREF(arg);
3731 }
3732 else {
3733 err_args(func, flags, na);
3734 x = NULL;
3735 }
3736 }
3737 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003738 PyObject *callargs;
3739 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003740 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003741 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003742 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003743 Py_XDECREF(callargs);
3744 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003745 } else {
3746 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3747 /* optimize access to bound methods */
3748 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003749 PCALL(PCALL_METHOD);
3750 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003751 Py_INCREF(self);
3752 func = PyMethod_GET_FUNCTION(func);
3753 Py_INCREF(func);
3754 Py_DECREF(*pfunc);
3755 *pfunc = self;
3756 na++;
3757 n++;
3758 } else
3759 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003760 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003761 if (PyFunction_Check(func))
3762 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003763 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003764 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003765 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003766 Py_DECREF(func);
3767 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003768
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003769 /* Clear the stack of the function object. Also removes
3770 the arguments in case they weren't consumed already
3771 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003772 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003773 while ((*pp_stack) > pfunc) {
3774 w = EXT_POP(*pp_stack);
3775 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003776 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003777 }
3778 return x;
3779}
3780
Jeremy Hylton192690e2002-08-16 18:36:11 +00003781/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003782 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003783 For the simplest case -- a function that takes only positional
3784 arguments and is called with only positional arguments -- it
3785 inlines the most primitive frame setup code from
3786 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3787 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003788*/
3789
3790static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003791fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003792{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003793 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003794 PyObject *globals = PyFunction_GET_GLOBALS(func);
3795 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003796 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003797 PyObject **d = NULL;
3798 int nd = 0;
3799
Jeremy Hylton985eba52003-02-05 23:13:00 +00003800 PCALL(PCALL_FUNCTION);
3801 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003802 if (argdefs == NULL && co->co_argcount == n &&
3803 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003804 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3805 PyFrameObject *f;
3806 PyObject *retval = NULL;
3807 PyThreadState *tstate = PyThreadState_GET();
3808 PyObject **fastlocals, **stack;
3809 int i;
3810
3811 PCALL(PCALL_FASTER_FUNCTION);
3812 assert(globals != NULL);
3813 /* XXX Perhaps we should create a specialized
3814 PyFrame_New() that doesn't take locals, but does
3815 take builtins without sanity checking them.
3816 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003817 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003818 f = PyFrame_New(tstate, co, globals, NULL);
3819 if (f == NULL)
3820 return NULL;
3821
3822 fastlocals = f->f_localsplus;
3823 stack = (*pp_stack) - n;
3824
3825 for (i = 0; i < n; i++) {
3826 Py_INCREF(*stack);
3827 fastlocals[i] = *stack++;
3828 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003829 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003830 ++tstate->recursion_depth;
3831 Py_DECREF(f);
3832 --tstate->recursion_depth;
3833 return retval;
3834 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003835 if (argdefs != NULL) {
3836 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003837 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003838 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003839 return PyEval_EvalCodeEx(co, globals,
3840 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003841 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003842 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003843}
3844
3845static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003846update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3847 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003848{
3849 PyObject *kwdict = NULL;
3850 if (orig_kwdict == NULL)
3851 kwdict = PyDict_New();
3852 else {
3853 kwdict = PyDict_Copy(orig_kwdict);
3854 Py_DECREF(orig_kwdict);
3855 }
3856 if (kwdict == NULL)
3857 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003858 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003859 int err;
3860 PyObject *value = EXT_POP(*pp_stack);
3861 PyObject *key = EXT_POP(*pp_stack);
3862 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003863 PyErr_Format(PyExc_TypeError,
3864 "%.200s%s got multiple values "
3865 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003866 PyEval_GetFuncName(func),
3867 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003868 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003869 Py_DECREF(key);
3870 Py_DECREF(value);
3871 Py_DECREF(kwdict);
3872 return NULL;
3873 }
3874 err = PyDict_SetItem(kwdict, key, value);
3875 Py_DECREF(key);
3876 Py_DECREF(value);
3877 if (err) {
3878 Py_DECREF(kwdict);
3879 return NULL;
3880 }
3881 }
3882 return kwdict;
3883}
3884
3885static PyObject *
3886update_star_args(int nstack, int nstar, PyObject *stararg,
3887 PyObject ***pp_stack)
3888{
3889 PyObject *callargs, *w;
3890
3891 callargs = PyTuple_New(nstack + nstar);
3892 if (callargs == NULL) {
3893 return NULL;
3894 }
3895 if (nstar) {
3896 int i;
3897 for (i = 0; i < nstar; i++) {
3898 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3899 Py_INCREF(a);
3900 PyTuple_SET_ITEM(callargs, nstack + i, a);
3901 }
3902 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003903 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003904 w = EXT_POP(*pp_stack);
3905 PyTuple_SET_ITEM(callargs, nstack, w);
3906 }
3907 return callargs;
3908}
3909
3910static PyObject *
3911load_args(PyObject ***pp_stack, int na)
3912{
3913 PyObject *args = PyTuple_New(na);
3914 PyObject *w;
3915
3916 if (args == NULL)
3917 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003918 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003919 w = EXT_POP(*pp_stack);
3920 PyTuple_SET_ITEM(args, na, w);
3921 }
3922 return args;
3923}
3924
3925static PyObject *
3926do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3927{
3928 PyObject *callargs = NULL;
3929 PyObject *kwdict = NULL;
3930 PyObject *result = NULL;
3931
3932 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003933 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003934 if (kwdict == NULL)
3935 goto call_fail;
3936 }
3937 callargs = load_args(pp_stack, na);
3938 if (callargs == NULL)
3939 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003940#ifdef CALL_PROFILE
3941 /* At this point, we have to look at the type of func to
3942 update the call stats properly. Do it here so as to avoid
3943 exposing the call stats machinery outside ceval.c
3944 */
3945 if (PyFunction_Check(func))
3946 PCALL(PCALL_FUNCTION);
3947 else if (PyMethod_Check(func))
3948 PCALL(PCALL_METHOD);
3949 else if (PyType_Check(func))
3950 PCALL(PCALL_TYPE);
3951 else
3952 PCALL(PCALL_OTHER);
3953#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003954 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003955call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003956 Py_XDECREF(callargs);
3957 Py_XDECREF(kwdict);
3958 return result;
3959}
3960
3961static PyObject *
3962ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3963{
3964 int nstar = 0;
3965 PyObject *callargs = NULL;
3966 PyObject *stararg = NULL;
3967 PyObject *kwdict = NULL;
3968 PyObject *result = NULL;
3969
3970 if (flags & CALL_FLAG_KW) {
3971 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003972 if (!PyDict_Check(kwdict)) {
3973 PyObject *d;
3974 d = PyDict_New();
3975 if (d == NULL)
3976 goto ext_call_fail;
3977 if (PyDict_Update(d, kwdict) != 0) {
3978 Py_DECREF(d);
3979 /* PyDict_Update raises attribute
3980 * error (percolated from an attempt
3981 * to get 'keys' attribute) instead of
3982 * a type error if its second argument
3983 * is not a mapping.
3984 */
3985 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3986 PyErr_Format(PyExc_TypeError,
3987 "%.200s%.200s argument after ** "
3988 "must be a mapping, not %.200s",
3989 PyEval_GetFuncName(func),
3990 PyEval_GetFuncDesc(func),
3991 kwdict->ob_type->tp_name);
3992 }
3993 goto ext_call_fail;
3994 }
3995 Py_DECREF(kwdict);
3996 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00003997 }
3998 }
3999 if (flags & CALL_FLAG_VAR) {
4000 stararg = EXT_POP(*pp_stack);
4001 if (!PyTuple_Check(stararg)) {
4002 PyObject *t = NULL;
4003 t = PySequence_Tuple(stararg);
4004 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004005 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4006 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004007 "%.200s%.200s argument after * "
4008 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004009 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004010 PyEval_GetFuncDesc(func),
4011 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004012 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004013 goto ext_call_fail;
4014 }
4015 Py_DECREF(stararg);
4016 stararg = t;
4017 }
4018 nstar = PyTuple_GET_SIZE(stararg);
4019 }
4020 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004021 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004022 if (kwdict == NULL)
4023 goto ext_call_fail;
4024 }
4025 callargs = update_star_args(na, nstar, stararg, pp_stack);
4026 if (callargs == NULL)
4027 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004028#ifdef CALL_PROFILE
4029 /* At this point, we have to look at the type of func to
4030 update the call stats properly. Do it here so as to avoid
4031 exposing the call stats machinery outside ceval.c
4032 */
4033 if (PyFunction_Check(func))
4034 PCALL(PCALL_FUNCTION);
4035 else if (PyMethod_Check(func))
4036 PCALL(PCALL_METHOD);
4037 else if (PyType_Check(func))
4038 PCALL(PCALL_TYPE);
4039 else
4040 PCALL(PCALL_OTHER);
4041#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00004042 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004043ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004044 Py_XDECREF(callargs);
4045 Py_XDECREF(kwdict);
4046 Py_XDECREF(stararg);
4047 return result;
4048}
4049
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004050/* Extract a slice index from a PyInt or PyLong or an object with the
4051 nb_index slot defined, and store in *pi.
4052 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4053 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 +00004054 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004055*/
Tim Petersb5196382001-12-16 19:44:20 +00004056/* Note: If v is NULL, return success without storing into *pi. This
4057 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4058 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004059*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004060int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004061_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004062{
Tim Petersb5196382001-12-16 19:44:20 +00004063 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004064 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004065 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004066 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004067 if (x == -1 && PyErr_Occurred())
4068 return 0;
4069 }
4070 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004071 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004072 "slice indices must be integers or "
4073 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004074 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004075 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004076 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004077 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004078 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004079}
4080
Guido van Rossum486364b2007-06-30 05:01:58 +00004081#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004082 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004083
Guido van Rossumb209a111997-04-29 18:18:01 +00004084static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004085cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004086{
Guido van Rossumac7be682001-01-17 15:42:30 +00004087 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004088 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004089 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004090 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004091 break;
4092 case PyCmp_IS_NOT:
4093 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004094 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004095 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004096 res = PySequence_Contains(w, v);
4097 if (res < 0)
4098 return NULL;
4099 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004100 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004101 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004102 if (res < 0)
4103 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004104 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004105 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004106 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004107 if (PyTuple_Check(w)) {
4108 Py_ssize_t i, length;
4109 length = PyTuple_Size(w);
4110 for (i = 0; i < length; i += 1) {
4111 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004112 if (!PyExceptionClass_Check(exc)) {
4113 PyErr_SetString(PyExc_TypeError,
4114 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004115 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004116 }
4117 }
4118 }
4119 else {
Brett Cannon39590462007-02-26 22:01:14 +00004120 if (!PyExceptionClass_Check(w)) {
4121 PyErr_SetString(PyExc_TypeError,
4122 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004123 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004124 }
4125 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004126 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004127 break;
4128 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004129 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004130 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004131 v = res ? Py_True : Py_False;
4132 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004133 return v;
4134}
4135
Thomas Wouters52152252000-08-17 22:55:00 +00004136static PyObject *
4137import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004138{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004139 PyObject *x;
4140
4141 x = PyObject_GetAttr(v, name);
4142 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004143 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004144 }
Thomas Wouters52152252000-08-17 22:55:00 +00004145 return x;
4146}
Guido van Rossumac7be682001-01-17 15:42:30 +00004147
Thomas Wouters52152252000-08-17 22:55:00 +00004148static int
4149import_all_from(PyObject *locals, PyObject *v)
4150{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004151 PyObject *all = PyObject_GetAttrString(v, "__all__");
4152 PyObject *dict, *name, *value;
4153 int skip_leading_underscores = 0;
4154 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004155
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004156 if (all == NULL) {
4157 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4158 return -1; /* Unexpected error */
4159 PyErr_Clear();
4160 dict = PyObject_GetAttrString(v, "__dict__");
4161 if (dict == NULL) {
4162 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4163 return -1;
4164 PyErr_SetString(PyExc_ImportError,
4165 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004166 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004167 }
4168 all = PyMapping_Keys(dict);
4169 Py_DECREF(dict);
4170 if (all == NULL)
4171 return -1;
4172 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004173 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004174
4175 for (pos = 0, err = 0; ; pos++) {
4176 name = PySequence_GetItem(all, pos);
4177 if (name == NULL) {
4178 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4179 err = -1;
4180 else
4181 PyErr_Clear();
4182 break;
4183 }
4184 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004185 PyUnicode_Check(name) &&
4186 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004187 {
4188 Py_DECREF(name);
4189 continue;
4190 }
4191 value = PyObject_GetAttr(v, name);
4192 if (value == NULL)
4193 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004194 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004195 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004196 else
4197 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004198 Py_DECREF(name);
4199 Py_XDECREF(value);
4200 if (err != 0)
4201 break;
4202 }
4203 Py_DECREF(all);
4204 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004205}
4206
Guido van Rossumac7be682001-01-17 15:42:30 +00004207static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004208format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004209{
Neal Norwitzda059e32007-08-26 05:33:45 +00004210 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004211
4212 if (!obj)
4213 return;
4214
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004215 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004216 if (!obj_str)
4217 return;
4218
4219 PyErr_Format(exc, format_str, obj_str);
4220}
Guido van Rossum950361c1997-01-24 13:49:28 +00004221
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004222static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004223unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004224 PyFrameObject *f, unsigned char *next_instr)
4225{
4226 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004227 are (Unicode) strings. */
4228 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4229 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004230 Py_ssize_t new_len = v_len + w_len;
4231 if (new_len < 0) {
4232 PyErr_SetString(PyExc_OverflowError,
4233 "strings are too large to concat");
4234 return NULL;
4235 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004236
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004237 if (v->ob_refcnt == 2) {
4238 /* In the common case, there are 2 references to the value
4239 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004240 * value stack (in 'v') and one still stored in the
4241 * 'variable'. We try to delete the variable now to reduce
4242 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004243 */
4244 switch (*next_instr) {
4245 case STORE_FAST:
4246 {
4247 int oparg = PEEKARG();
4248 PyObject **fastlocals = f->f_localsplus;
4249 if (GETLOCAL(oparg) == v)
4250 SETLOCAL(oparg, NULL);
4251 break;
4252 }
4253 case STORE_DEREF:
4254 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004255 PyObject **freevars = (f->f_localsplus +
4256 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004257 PyObject *c = freevars[PEEKARG()];
4258 if (PyCell_GET(c) == v)
4259 PyCell_Set(c, NULL);
4260 break;
4261 }
4262 case STORE_NAME:
4263 {
4264 PyObject *names = f->f_code->co_names;
4265 PyObject *name = GETITEM(names, PEEKARG());
4266 PyObject *locals = f->f_locals;
4267 if (PyDict_CheckExact(locals) &&
4268 PyDict_GetItem(locals, name) == v) {
4269 if (PyDict_DelItem(locals, name) != 0) {
4270 PyErr_Clear();
4271 }
4272 }
4273 break;
4274 }
4275 }
4276 }
4277
Guido van Rossum98297ee2007-11-06 21:34:58 +00004278 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004279 /* Now we own the last reference to 'v', so we can resize it
4280 * in-place.
4281 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004282 if (PyUnicode_Resize(&v, new_len) != 0) {
4283 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004284 * deallocated so it cannot be put back into
4285 * 'variable'. The MemoryError is raised when there
4286 * is no value in 'variable', which might (very
4287 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004288 */
4289 return NULL;
4290 }
4291 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004292 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4293 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004294 return v;
4295 }
4296 else {
4297 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004298 w = PyUnicode_Concat(v, w);
4299 Py_DECREF(v);
4300 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004301 }
4302}
4303
Guido van Rossum950361c1997-01-24 13:49:28 +00004304#ifdef DYNAMIC_EXECUTION_PROFILE
4305
Skip Montanarof118cb12001-10-15 20:51:38 +00004306static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004307getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004308{
4309 int i;
4310 PyObject *l = PyList_New(256);
4311 if (l == NULL) return NULL;
4312 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004313 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004314 if (x == NULL) {
4315 Py_DECREF(l);
4316 return NULL;
4317 }
4318 PyList_SetItem(l, i, x);
4319 }
4320 for (i = 0; i < 256; i++)
4321 a[i] = 0;
4322 return l;
4323}
4324
4325PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004326_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004327{
4328#ifndef DXPAIRS
4329 return getarray(dxp);
4330#else
4331 int i;
4332 PyObject *l = PyList_New(257);
4333 if (l == NULL) return NULL;
4334 for (i = 0; i < 257; i++) {
4335 PyObject *x = getarray(dxpairs[i]);
4336 if (x == NULL) {
4337 Py_DECREF(l);
4338 return NULL;
4339 }
4340 PyList_SetItem(l, i, x);
4341 }
4342 return l;
4343#endif
4344}
4345
4346#endif