blob: 8a24f115397852eda9e9da888b0650938c1c2823 [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
Mark Dickinsonee2dd4a2009-10-31 10:20:38 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsonee2dd4a2009-10-31 10:20:38 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
81{
82 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
90 opcode, ticked, inst, loop);
91}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000138static PyObject * unicode_concatenate(PyObject *, PyObject *,
139 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000140
Paul Prescode68140d2000-08-30 20:25:01 +0000141#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000142 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000143#define GLOBAL_NAME_ERROR_MSG \
144 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000145#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000146 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000147#define UNBOUNDFREE_ERROR_MSG \
148 "free variable '%.200s' referenced before assignment" \
149 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000150
Guido van Rossum950361c1997-01-24 13:49:28 +0000151/* Dynamic execution profile */
152#ifdef DYNAMIC_EXECUTION_PROFILE
153#ifdef DXPAIRS
154static long dxpairs[257][256];
155#define dxp dxpairs[256]
156#else
157static long dxp[256];
158#endif
159#endif
160
Jeremy Hylton985eba52003-02-05 23:13:00 +0000161/* Function call profile */
162#ifdef CALL_PROFILE
163#define PCALL_NUM 11
164static int pcall[PCALL_NUM];
165
166#define PCALL_ALL 0
167#define PCALL_FUNCTION 1
168#define PCALL_FAST_FUNCTION 2
169#define PCALL_FASTER_FUNCTION 3
170#define PCALL_METHOD 4
171#define PCALL_BOUND_METHOD 5
172#define PCALL_CFUNCTION 6
173#define PCALL_TYPE 7
174#define PCALL_GENERATOR 8
175#define PCALL_OTHER 9
176#define PCALL_POP 10
177
178/* Notes about the statistics
179
180 PCALL_FAST stats
181
182 FAST_FUNCTION means no argument tuple needs to be created.
183 FASTER_FUNCTION means that the fast-path frame setup code is used.
184
185 If there is a method call where the call can be optimized by changing
186 the argument tuple and calling the function directly, it gets recorded
187 twice.
188
189 As a result, the relationship among the statistics appears to be
190 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
191 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
192 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
193 PCALL_METHOD > PCALL_BOUND_METHOD
194*/
195
196#define PCALL(POS) pcall[POS]++
197
198PyObject *
199PyEval_GetCallStats(PyObject *self)
200{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000201 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000202 pcall[0], pcall[1], pcall[2], pcall[3],
203 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000204 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000205}
206#else
207#define PCALL(O)
208
209PyObject *
210PyEval_GetCallStats(PyObject *self)
211{
212 Py_INCREF(Py_None);
213 return Py_None;
214}
215#endif
216
Tim Peters5ca576e2001-06-18 22:08:13 +0000217
Guido van Rossume59214e1994-08-30 08:01:59 +0000218#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000219
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000220#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000221#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000222#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000223#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000224
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000225static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000226static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000227static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228
Tim Peters7f468f22004-10-11 02:40:51 +0000229int
230PyEval_ThreadsInitialized(void)
231{
232 return interpreter_lock != 0;
233}
234
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000235void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000237{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000238 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000239 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000240 interpreter_lock = PyThread_allocate_lock();
241 PyThread_acquire_lock(interpreter_lock, 1);
242 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000243}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000244
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000245void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000246PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000247{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000248 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000249}
250
251void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000252PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000253{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000254 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000255}
256
257void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000259{
260 if (tstate == NULL)
261 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000262 /* Check someone has called PyEval_InitThreads() to create the lock */
263 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000264 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000265 if (PyThreadState_Swap(tstate) != NULL)
266 Py_FatalError(
267 "PyEval_AcquireThread: non-NULL old thread state");
268}
269
270void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000271PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000272{
273 if (tstate == NULL)
274 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
275 if (PyThreadState_Swap(NULL) != tstate)
276 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000277 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000278}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000279
280/* This function is called from PyOS_AfterFork to ensure that newly
281 created child processes don't hold locks referring to threads which
282 are not running in the child process. (This could also be done using
283 pthread_atfork mechanism, at least for the pthreads implementation.) */
284
285void
286PyEval_ReInitThreads(void)
287{
Jesse Nollera8513972008-07-17 16:49:17 +0000288 PyObject *threading, *result;
289 PyThreadState *tstate;
290
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000291 if (!interpreter_lock)
292 return;
293 /*XXX Can't use PyThread_free_lock here because it does too
294 much error-checking. Doing this cleanly would require
295 adding a new function to each thread_*.h. Instead, just
296 create a new lock and waste a little bit of memory */
297 interpreter_lock = PyThread_allocate_lock();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000298 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000299 PyThread_acquire_lock(interpreter_lock, 1);
300 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000301
302 /* Update the threading module with the new state.
303 */
304 tstate = PyThreadState_GET();
305 threading = PyMapping_GetItemString(tstate->interp->modules,
306 "threading");
307 if (threading == NULL) {
308 /* threading not imported */
309 PyErr_Clear();
310 return;
311 }
312 result = PyObject_CallMethod(threading, "_after_fork", NULL);
313 if (result == NULL)
314 PyErr_WriteUnraisable(threading);
315 else
316 Py_DECREF(result);
317 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000318}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000319#endif
320
Guido van Rossumff4949e1992-08-05 19:58:53 +0000321/* Functions save_thread and restore_thread are always defined so
322 dynamically loaded modules needn't be compiled separately for use
323 with and without threads: */
324
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000325PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000327{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000328 PyThreadState *tstate = PyThreadState_Swap(NULL);
329 if (tstate == NULL)
330 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000331#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000332 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000333 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000334#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000335 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000336}
337
338void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000341 if (tstate == NULL)
342 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000343#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000344 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000345 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000346 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000348 }
349#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000350 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000351}
352
353
Guido van Rossuma9672091994-09-14 13:31:22 +0000354/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
355 signal handlers or Mac I/O completion routines) can schedule calls
356 to a function to be called synchronously.
357 The synchronous function is called with one void* argument.
358 It should return 0 for success or -1 for failure -- failure should
359 be accompanied by an exception.
360
361 If registry succeeds, the registry function returns 0; if it fails
362 (e.g. due to too many pending calls) it returns -1 (without setting
363 an exception condition).
364
365 Note that because registry may occur from within signal handlers,
366 or other asynchronous events, calling malloc() is unsafe!
367
368#ifdef WITH_THREAD
369 Any thread can schedule pending calls, but only the main thread
370 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000371 There is no facility to schedule calls to a particular thread, but
372 that should be easy to change, should that ever be required. In
373 that case, the static variables here should go into the python
374 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000375#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000376*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000377
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000378#ifdef WITH_THREAD
379
380/* The WITH_THREAD implementation is thread-safe. It allows
381 scheduling to be made from any thread, and even from an executing
382 callback.
383 */
384
385#define NPENDINGCALLS 32
386static struct {
387 int (*func)(void *);
388 void *arg;
389} pendingcalls[NPENDINGCALLS];
390static int pendingfirst = 0;
391static int pendinglast = 0;
392static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
393static char pendingbusy = 0;
394
395int
396Py_AddPendingCall(int (*func)(void *), void *arg)
397{
398 int i, j, result=0;
399 PyThread_type_lock lock = pending_lock;
400
401 /* try a few times for the lock. Since this mechanism is used
402 * for signal handling (on the main thread), there is a (slim)
403 * chance that a signal is delivered on the same thread while we
404 * hold the lock during the Py_MakePendingCalls() function.
405 * This avoids a deadlock in that case.
406 * Note that signals can be delivered on any thread. In particular,
407 * on Windows, a SIGINT is delivered on a system-created worker
408 * thread.
409 * We also check for lock being NULL, in the unlikely case that
410 * this function is called before any bytecode evaluation takes place.
411 */
412 if (lock != NULL) {
413 for (i = 0; i<100; i++) {
414 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
415 break;
416 }
417 if (i == 100)
418 return -1;
419 }
420
421 i = pendinglast;
422 j = (i + 1) % NPENDINGCALLS;
423 if (j == pendingfirst) {
424 result = -1; /* Queue full */
425 } else {
426 pendingcalls[i].func = func;
427 pendingcalls[i].arg = arg;
428 pendinglast = j;
429 }
430 /* signal main loop */
431 _Py_Ticker = 0;
432 pendingcalls_to_do = 1;
433 if (lock != NULL)
434 PyThread_release_lock(lock);
435 return result;
436}
437
438int
439Py_MakePendingCalls(void)
440{
441 int i;
442 int r = 0;
443
444 if (!pending_lock) {
445 /* initial allocation of the lock */
446 pending_lock = PyThread_allocate_lock();
447 if (pending_lock == NULL)
448 return -1;
449 }
450
451 /* only service pending calls on main thread */
452 if (main_thread && PyThread_get_thread_ident() != main_thread)
453 return 0;
454 /* don't perform recursive pending calls */
455 if (pendingbusy)
456 return 0;
457 pendingbusy = 1;
458 /* perform a bounded number of calls, in case of recursion */
459 for (i=0; i<NPENDINGCALLS; i++) {
460 int j;
461 int (*func)(void *);
Benjamin Petersonb4ddfa42009-01-17 23:46:54 +0000462 void *arg = NULL;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000463
464 /* pop one item off the queue while holding the lock */
465 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
466 j = pendingfirst;
467 if (j == pendinglast) {
468 func = NULL; /* Queue empty */
469 } else {
470 func = pendingcalls[j].func;
471 arg = pendingcalls[j].arg;
472 pendingfirst = (j + 1) % NPENDINGCALLS;
473 }
474 pendingcalls_to_do = pendingfirst != pendinglast;
475 PyThread_release_lock(pending_lock);
476 /* having released the lock, perform the callback */
477 if (func == NULL)
478 break;
479 r = func(arg);
480 if (r)
481 break;
482 }
483 pendingbusy = 0;
484 return r;
485}
486
487#else /* if ! defined WITH_THREAD */
488
489/*
490 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
491 This code is used for signal handling in python that isn't built
492 with WITH_THREAD.
493 Don't use this implementation when Py_AddPendingCalls() can happen
494 on a different thread!
495
Guido van Rossuma9672091994-09-14 13:31:22 +0000496 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000497 (1) nested asynchronous calls to Py_AddPendingCall()
498 (2) AddPendingCall() calls made while pending calls are being processed.
499
500 (1) is very unlikely because typically signal delivery
501 is blocked during signal handling. So it should be impossible.
502 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000503 The current code is safe against (2), but not against (1).
504 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000505 thread is present, interrupted by signals, and that the critical
506 section is protected with the "busy" variable. On Windows, which
507 delivers SIGINT on a system thread, this does not hold and therefore
508 Windows really shouldn't use this version.
509 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000510*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000511
Guido van Rossuma9672091994-09-14 13:31:22 +0000512#define NPENDINGCALLS 32
513static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000514 int (*func)(void *);
515 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000516} pendingcalls[NPENDINGCALLS];
517static volatile int pendingfirst = 0;
518static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000519static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000520
521int
Thomas Wouters334fb892000-07-25 12:56:38 +0000522Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000523{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000524 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000525 int i, j;
526 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000527 if (busy)
528 return -1;
529 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000530 i = pendinglast;
531 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000532 if (j == pendingfirst) {
533 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000534 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000535 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000536 pendingcalls[i].func = func;
537 pendingcalls[i].arg = arg;
538 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000539
540 _Py_Ticker = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000541 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000542 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000543 /* XXX End critical section */
544 return 0;
545}
546
Guido van Rossum180d7b41994-09-29 09:45:57 +0000547int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000548Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000549{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000550 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000551 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000552 return 0;
553 busy = 1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000554 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000555 for (;;) {
556 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000557 int (*func)(void *);
558 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000559 i = pendingfirst;
560 if (i == pendinglast)
561 break; /* Queue empty */
562 func = pendingcalls[i].func;
563 arg = pendingcalls[i].arg;
564 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000565 if (func(arg) < 0) {
566 busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000567 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000568 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000569 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000570 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000571 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000572 return 0;
573}
574
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000575#endif /* WITH_THREAD */
576
Guido van Rossuma9672091994-09-14 13:31:22 +0000577
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000578/* The interpreter's recursion limit */
579
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000580#ifndef Py_DEFAULT_RECURSION_LIMIT
581#define Py_DEFAULT_RECURSION_LIMIT 1000
582#endif
583static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
584int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000585
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000586int
587Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000588{
589 return recursion_limit;
590}
591
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000592void
593Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000594{
595 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000596 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000597}
598
Armin Rigo2b3eb402003-10-28 12:05:48 +0000599/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
600 if the recursion_depth reaches _Py_CheckRecursionLimit.
601 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
602 to guarantee that _Py_CheckRecursiveCall() is regularly called.
603 Without USE_STACKCHECK, there is no need for this. */
604int
605_Py_CheckRecursiveCall(char *where)
606{
607 PyThreadState *tstate = PyThreadState_GET();
608
609#ifdef USE_STACKCHECK
610 if (PyOS_CheckStack()) {
611 --tstate->recursion_depth;
612 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
613 return -1;
614 }
615#endif
Antoine Pitrou658fad82008-09-03 18:34:34 +0000616 _Py_CheckRecursionLimit = recursion_limit;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000617 if (tstate->recursion_critical)
618 /* Somebody asked that we don't check for recursion. */
619 return 0;
620 if (tstate->overflowed) {
621 if (tstate->recursion_depth > recursion_limit + 50) {
622 /* Overflowing while handling an overflow. Give up. */
623 Py_FatalError("Cannot recover from stack overflow.");
624 }
625 return 0;
626 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000627 if (tstate->recursion_depth > recursion_limit) {
628 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000629 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000630 PyErr_Format(PyExc_RuntimeError,
631 "maximum recursion depth exceeded%s",
632 where);
633 return -1;
634 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000635 return 0;
636}
637
Guido van Rossum374a9221991-04-04 10:40:29 +0000638/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000639enum why_code {
640 WHY_NOT = 0x0001, /* No error */
641 WHY_EXCEPTION = 0x0002, /* Exception occurred */
642 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
643 WHY_RETURN = 0x0008, /* 'return' statement */
644 WHY_BREAK = 0x0010, /* 'break' statement */
645 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000646 WHY_YIELD = 0x0040, /* 'yield' operator */
647 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000648};
Guido van Rossum374a9221991-04-04 10:40:29 +0000649
Collin Winter828f04a2007-08-31 00:04:24 +0000650static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000651static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000652
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000653/* Records whether tracing is on for any thread. Counts the number of
654 threads for which tstate->c_tracefunc is non-NULL, so if the value
655 is 0, we know we don't have to check this thread's c_tracefunc.
656 This speeds up the if statement in PyEval_EvalFrameEx() after
657 fast_next_opcode*/
658static int _Py_TracingPossible = 0;
659
Skip Montanarod581d772002-09-03 20:10:45 +0000660/* for manipulating the thread switch and periodic "stuff" - used to be
661 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000662int _Py_CheckInterval = 100;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000663volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000664
Guido van Rossumb209a111997-04-29 18:18:01 +0000665PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000666PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000667{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000668 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000669 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000670 (PyObject **)NULL, 0,
671 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000672 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000673 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000674}
675
676
677/* Interpreter main loop */
678
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000679PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000680PyEval_EvalFrame(PyFrameObject *f) {
681 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000682 used this API; core interpreter code should call
683 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000684 return PyEval_EvalFrameEx(f, 0);
685}
686
687PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000689{
Guido van Rossum950361c1997-01-24 13:49:28 +0000690#ifdef DXPAIRS
691 int lastopcode = 0;
692#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000693 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000694 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000695 register int opcode; /* Current opcode */
696 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000697 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000698 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000699 register PyObject *x; /* Result object -- NULL if error */
700 register PyObject *v; /* Temporary objects popped off stack */
701 register PyObject *w;
702 register PyObject *u;
703 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000704 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000705 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000706 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000707 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000708
Tim Peters8a5c3c72004-04-05 19:36:21 +0000709 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000710
711 not (instr_lb <= current_bytecode_offset < instr_ub)
712
Tim Peters8a5c3c72004-04-05 19:36:21 +0000713 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000714 initial values are such as to make this false the first
715 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000716 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000717
Guido van Rossumd076c731998-10-07 19:42:25 +0000718 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000719 PyObject *names;
720 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000721#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000722 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000723 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000724#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000725
Antoine Pitroub52ec782009-01-25 16:34:23 +0000726/* Computed GOTOs, or
727 the-optimization-commonly-but-improperly-known-as-"threaded code"
728 using gcc's labels-as-values extension
729 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
730
731 The traditional bytecode evaluation loop uses a "switch" statement, which
732 decent compilers will optimize as a single indirect branch instruction
733 combined with a lookup table of jump addresses. However, since the
734 indirect jump instruction is shared by all opcodes, the CPU will have a
735 hard time making the right prediction for where to jump next (actually,
736 it will be always wrong except in the uncommon case of a sequence of
737 several identical opcodes).
738
739 "Threaded code" in contrast, uses an explicit jump table and an explicit
740 indirect jump instruction at the end of each opcode. Since the jump
741 instruction is at a different address for each opcode, the CPU will make a
742 separate prediction for each of these instructions, which is equivalent to
743 predicting the second opcode of each opcode pair. These predictions have
744 a much better chance to turn out valid, especially in small bytecode loops.
745
746 A mispredicted branch on a modern CPU flushes the whole pipeline and
747 can cost several CPU cycles (depending on the pipeline depth),
748 and potentially many more instructions (depending on the pipeline width).
749 A correctly predicted branch, however, is nearly free.
750
751 At the time of this writing, the "threaded code" version is up to 15-20%
752 faster than the normal "switch" version, depending on the compiler and the
753 CPU architecture.
754
755 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
756 because it would render the measurements invalid.
757
758
759 NOTE: care must be taken that the compiler doesn't try to "optimize" the
760 indirect jumps by sharing them between all opcodes. Such optimizations
761 can be disabled on gcc by using the -fno-gcse flag (or possibly
762 -fno-crossjumping).
763*/
764
765#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
766#undef USE_COMPUTED_GOTOS
767#endif
768
769#ifdef USE_COMPUTED_GOTOS
770/* Import the static jump table */
771#include "opcode_targets.h"
772
773/* This macro is used when several opcodes defer to the same implementation
774 (e.g. SETUP_LOOP, SETUP_FINALLY) */
775#define TARGET_WITH_IMPL(op, impl) \
776 TARGET_##op: \
777 opcode = op; \
778 if (HAS_ARG(op)) \
779 oparg = NEXTARG(); \
780 case op: \
781 goto impl; \
782
783#define TARGET(op) \
784 TARGET_##op: \
785 opcode = op; \
786 if (HAS_ARG(op)) \
787 oparg = NEXTARG(); \
788 case op:
789
790
791#define DISPATCH() \
792 { \
793 /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
794 int _tick = _Py_Ticker - 1; \
795 _Py_Ticker = _tick; \
796 if (_tick >= 0) { \
797 FAST_DISPATCH(); \
798 } \
799 continue; \
800 }
801
802#ifdef LLTRACE
803#define FAST_DISPATCH() \
804 { \
805 if (!lltrace && !_Py_TracingPossible) { \
806 f->f_lasti = INSTR_OFFSET(); \
807 goto *opcode_targets[*next_instr++]; \
808 } \
809 goto fast_next_opcode; \
810 }
811#else
812#define FAST_DISPATCH() \
813 { \
814 if (!_Py_TracingPossible) { \
815 f->f_lasti = INSTR_OFFSET(); \
816 goto *opcode_targets[*next_instr++]; \
817 } \
818 goto fast_next_opcode; \
819 }
820#endif
821
822#else
823#define TARGET(op) \
824 case op:
825#define TARGET_WITH_IMPL(op, impl) \
826 /* silence compiler warnings about `impl` unused */ \
827 if (0) goto impl; \
828 case op:
829#define DISPATCH() continue
830#define FAST_DISPATCH() goto fast_next_opcode
831#endif
832
833
Neal Norwitza81d2202002-07-14 00:27:26 +0000834/* Tuple access macros */
835
836#ifndef Py_DEBUG
837#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
838#else
839#define GETITEM(v, i) PyTuple_GetItem((v), (i))
840#endif
841
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000842#ifdef WITH_TSC
843/* Use Pentium timestamp counter to mark certain events:
844 inst0 -- beginning of switch statement for opcode dispatch
845 inst1 -- end of switch statement (may be skipped)
846 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000848 (may be skipped)
849 intr1 -- beginning of long interruption
850 intr2 -- end of long interruption
851
852 Many opcodes call out to helper C functions. In some cases, the
853 time in those functions should be counted towards the time for the
854 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
855 calls another Python function; there's no point in charge all the
856 bytecode executed by the called function to the caller.
857
858 It's hard to make a useful judgement statically. In the presence
859 of operator overloading, it's impossible to tell if a call will
860 execute new Python code or not.
861
862 It's a case-by-case judgement. I'll use intr1 for the following
863 cases:
864
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000865 IMPORT_STAR
866 IMPORT_FROM
867 CALL_FUNCTION (and friends)
868
869 */
870 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
871 int ticked = 0;
872
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000873 READ_TIMESTAMP(inst0);
874 READ_TIMESTAMP(inst1);
875 READ_TIMESTAMP(loop0);
876 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000877
878 /* shut up the compiler */
879 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000880#endif
881
Guido van Rossum374a9221991-04-04 10:40:29 +0000882/* Code access macros */
883
Martin v. Löwis18e16552006-02-15 17:27:45 +0000884#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000885#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000886#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000887#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000888#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000889#define JUMPBY(x) (next_instr += (x))
890
Raymond Hettingerf606f872003-03-16 03:11:04 +0000891/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000892 Some opcodes tend to come in pairs thus making it possible to
893 predict the second code when the first is run. For example,
894 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
895 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000896
Georg Brandl86b2fb92008-07-16 03:43:04 +0000897 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000898 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000899 processor's own internal branch predication has a high likelihood of
900 success, resulting in a nearly zero-overhead transition to the
901 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000902 including its two unpredictable branches, the HAS_ARG test and the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000903 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000904 a successful PREDICT has the effect of making the two opcodes run as if
Georg Brandl86b2fb92008-07-16 03:43:04 +0000905 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000906
Georg Brandl86b2fb92008-07-16 03:43:04 +0000907 If collecting opcode statistics, your choices are to either keep the
908 predictions turned-on and interpret the results as if some opcodes
909 had been combined or turn-off predictions so that the opcode frequency
910 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000911
912 Opcode prediction is disabled with threaded code, since the latter allows
913 the CPU to record separate branch prediction information for each
914 opcode.
915
Raymond Hettingerf606f872003-03-16 03:11:04 +0000916*/
917
Antoine Pitroub52ec782009-01-25 16:34:23 +0000918#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Raymond Hettingera7216982004-02-08 19:59:27 +0000919#define PREDICT(op) if (0) goto PRED_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000920#define PREDICTED(op) PRED_##op:
921#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000922#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000923#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000924#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000925#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +0000926#endif
927
Raymond Hettingerf606f872003-03-16 03:11:04 +0000928
Guido van Rossum374a9221991-04-04 10:40:29 +0000929/* Stack manipulation macros */
930
Martin v. Löwis18e16552006-02-15 17:27:45 +0000931/* The stack can grow at most MAXINT deep, as co_nlocals and
932 co_stacksize are ints. */
933#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000934#define EMPTY() (STACK_LEVEL() == 0)
935#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000936#define SECOND() (stack_pointer[-2])
937#define THIRD() (stack_pointer[-3])
938#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000939#define SET_TOP(v) (stack_pointer[-1] = (v))
940#define SET_SECOND(v) (stack_pointer[-2] = (v))
941#define SET_THIRD(v) (stack_pointer[-3] = (v))
942#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000943#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000944#define BASIC_PUSH(v) (*stack_pointer++ = (v))
945#define BASIC_POP() (*--stack_pointer)
946
Guido van Rossum96a42c81992-01-12 02:29:51 +0000947#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000948#define PUSH(v) { (void)(BASIC_PUSH(v), \
949 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000950 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000951#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
952 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000953#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
954 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000955 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000956#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
957 prtrace((STACK_POINTER)[-1], "ext_pop")), \
958 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000959#else
960#define PUSH(v) BASIC_PUSH(v)
961#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000962#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000963#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000964#endif
965
Guido van Rossum681d79a1995-07-18 14:51:37 +0000966/* Local variable macros */
967
968#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000969
970/* The SETLOCAL() macro must not DECREF the local variable in-place and
971 then store the new value; it must copy the old value to a temporary
972 value, then store the new value, and then DECREF the temporary value.
973 This is because it is possible that during the DECREF the frame is
974 accessed by other code (e.g. a __del__ method or gc.collect()) and the
975 variable would be pointing to already-freed memory. */
976#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
977 GETLOCAL(i) = value; \
978 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000979
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000980
981#define UNWIND_BLOCK(b) \
982 while (STACK_LEVEL() > (b)->b_level) { \
983 PyObject *v = POP(); \
984 Py_XDECREF(v); \
985 }
986
987#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000988 { \
989 PyObject *type, *value, *traceback; \
990 assert(STACK_LEVEL() >= (b)->b_level + 3); \
991 while (STACK_LEVEL() > (b)->b_level + 3) { \
992 value = POP(); \
993 Py_XDECREF(value); \
994 } \
995 type = tstate->exc_type; \
996 value = tstate->exc_value; \
997 traceback = tstate->exc_traceback; \
998 tstate->exc_type = POP(); \
999 tstate->exc_value = POP(); \
1000 tstate->exc_traceback = POP(); \
1001 Py_XDECREF(type); \
1002 Py_XDECREF(value); \
1003 Py_XDECREF(traceback); \
1004 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001005
1006#define SAVE_EXC_STATE() \
1007 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001008 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001009 Py_XINCREF(tstate->exc_type); \
1010 Py_XINCREF(tstate->exc_value); \
1011 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001012 type = f->f_exc_type; \
1013 value = f->f_exc_value; \
1014 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001015 f->f_exc_type = tstate->exc_type; \
1016 f->f_exc_value = tstate->exc_value; \
1017 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001018 Py_XDECREF(type); \
1019 Py_XDECREF(value); \
1020 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001021 }
1022
1023#define SWAP_EXC_STATE() \
1024 { \
1025 PyObject *tmp; \
1026 tmp = tstate->exc_type; \
1027 tstate->exc_type = f->f_exc_type; \
1028 f->f_exc_type = tmp; \
1029 tmp = tstate->exc_value; \
1030 tstate->exc_value = f->f_exc_value; \
1031 f->f_exc_value = tmp; \
1032 tmp = tstate->exc_traceback; \
1033 tstate->exc_traceback = f->f_exc_traceback; \
1034 f->f_exc_traceback = tmp; \
1035 }
1036
Guido van Rossuma027efa1997-05-05 20:56:21 +00001037/* Start of code */
1038
Tim Peters5ca576e2001-06-18 22:08:13 +00001039 if (f == NULL)
1040 return NULL;
1041
Armin Rigo1d313ab2003-10-25 14:33:09 +00001042 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001043 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +00001044 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001045
Tim Peters5ca576e2001-06-18 22:08:13 +00001046 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001047
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001048 if (tstate->use_tracing) {
1049 if (tstate->c_tracefunc != NULL) {
1050 /* tstate->c_tracefunc, if defined, is a
1051 function that will be called on *every* entry
1052 to a code block. Its return value, if not
1053 None, is a function that will be called at
1054 the start of each executed line of code.
1055 (Actually, the function must return itself
1056 in order to continue tracing.) The trace
1057 functions are called with three arguments:
1058 a pointer to the current frame, a string
1059 indicating why the function is called, and
1060 an argument which depends on the situation.
1061 The global trace function is also called
1062 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001063 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001064 tstate->c_traceobj,
1065 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001066 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001067 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001068 }
1069 }
1070 if (tstate->c_profilefunc != NULL) {
1071 /* Similar for c_profilefunc, except it needn't
1072 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001073 if (call_trace_protected(tstate->c_profilefunc,
1074 tstate->c_profileobj,
1075 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001076 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001077 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001078 }
1079 }
1080 }
1081
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001082 co = f->f_code;
1083 names = co->co_names;
1084 consts = co->co_consts;
1085 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001086 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +00001087 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001088 /* An explanation is in order for the next line.
1089
1090 f->f_lasti now refers to the index of the last instruction
1091 executed. You might think this was obvious from the name, but
1092 this wasn't always true before 2.3! PyFrame_New now sets
1093 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1094 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001095 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001096
1097 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001098 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001099 prediction effectively links the two codes together as if they
1100 were a single new opcode; accordingly,f->f_lasti will point to
1101 the first code in the pair (for instance, GET_ITER followed by
1102 FOR_ITER is effectively a single opcode and f->f_lasti will point
1103 at to the beginning of the combined pair.)
1104 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001105 next_instr = first_instr + f->f_lasti + 1;
1106 stack_pointer = f->f_stacktop;
1107 assert(stack_pointer != NULL);
1108 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1109
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001110 if (f->f_code->co_flags & CO_GENERATOR) {
1111 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1112 /* We were in an except handler when we left,
1113 restore the exception state which was put aside
1114 (see YIELD_VALUE). */
1115 SWAP_EXC_STATE();
1116 }
1117 else {
1118 SAVE_EXC_STATE();
1119 }
1120 }
1121
Tim Peters5ca576e2001-06-18 22:08:13 +00001122#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001124#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001125#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001126 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001127#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001128
Guido van Rossum374a9221991-04-04 10:40:29 +00001129 why = WHY_NOT;
1130 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00001131 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +00001132 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001133
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001134 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001135 why = WHY_EXCEPTION;
1136 goto on_error;
1137 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138
Guido van Rossum374a9221991-04-04 10:40:29 +00001139 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001140#ifdef WITH_TSC
1141 if (inst1 == 0) {
1142 /* Almost surely, the opcode executed a break
1143 or a continue, preventing inst1 from being set
1144 on the way out of the loop.
1145 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001146 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001147 loop1 = inst1;
1148 }
1149 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1150 intr0, intr1);
1151 ticked = 0;
1152 inst1 = 0;
1153 intr0 = 0;
1154 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001155 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001156#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001157 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001158 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001159
Guido van Rossuma027efa1997-05-05 20:56:21 +00001160 /* Do periodic things. Doing this every time through
1161 the loop would add too much overhead, so we do it
1162 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001163 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001164 event needs attention (e.g. a signal handler or
1165 async I/O handler); see Py_AddPendingCall() and
1166 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001167
Skip Montanarod581d772002-09-03 20:10:45 +00001168 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001169 if (*next_instr == SETUP_FINALLY) {
1170 /* Make the last opcode before
1171 a try: finally: block uninterruptable. */
1172 goto fast_next_opcode;
1173 }
Skip Montanarod581d772002-09-03 20:10:45 +00001174 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001175 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001176#ifdef WITH_TSC
1177 ticked = 1;
1178#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001179 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001180 if (Py_MakePendingCalls() < 0) {
1181 why = WHY_EXCEPTION;
1182 goto on_error;
1183 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001184 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +00001185 /* MakePendingCalls() didn't succeed.
1186 Force early re-execution of this
1187 "periodic" code, possibly after
1188 a thread switch */
1189 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +00001190 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001191#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001192 if (interpreter_lock) {
1193 /* Give another thread a chance */
1194
Guido van Rossum25ce5661997-08-02 03:10:38 +00001195 if (PyThreadState_Swap(NULL) != tstate)
1196 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +00001197 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001198
1199 /* Other threads may run now */
1200
Guido van Rossum65d5b571998-12-21 19:32:43 +00001201 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001202 if (PyThreadState_Swap(tstate) != NULL)
1203 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001204
1205 /* Check for thread interrupts */
1206
1207 if (tstate->async_exc != NULL) {
1208 x = tstate->async_exc;
1209 tstate->async_exc = NULL;
1210 PyErr_SetNone(x);
1211 Py_DECREF(x);
1212 why = WHY_EXCEPTION;
1213 goto on_error;
1214 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001215 }
1216#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001217 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001218
Neil Schemenauer63543862002-02-17 19:10:14 +00001219 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001220 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001222 /* line-by-line tracing support */
1223
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001224 if (_Py_TracingPossible &&
1225 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001226 /* see maybe_call_line_trace
1227 for expository comments */
1228 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001229
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001230 err = maybe_call_line_trace(tstate->c_tracefunc,
1231 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001232 f, &instr_lb, &instr_ub,
1233 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001234 /* Reload possibly changed frame fields */
1235 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001236 if (f->f_stacktop != NULL) {
1237 stack_pointer = f->f_stacktop;
1238 f->f_stacktop = NULL;
1239 }
1240 if (err) {
1241 /* trace function raised an exception */
1242 goto on_error;
1243 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001244 }
1245
1246 /* Extract opcode and argument */
1247
Guido van Rossum374a9221991-04-04 10:40:29 +00001248 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001249 oparg = 0; /* allows oparg to be stored in a register because
1250 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001251 if (HAS_ARG(opcode))
1252 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001253 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001254#ifdef DYNAMIC_EXECUTION_PROFILE
1255#ifdef DXPAIRS
1256 dxpairs[lastopcode][opcode]++;
1257 lastopcode = opcode;
1258#endif
1259 dxp[opcode]++;
1260#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001261
Guido van Rossum96a42c81992-01-12 02:29:51 +00001262#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001263 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Guido van Rossum96a42c81992-01-12 02:29:51 +00001265 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001266 if (HAS_ARG(opcode)) {
1267 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001268 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001269 }
1270 else {
1271 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001272 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001273 }
1274 }
1275#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001276
Guido van Rossum374a9221991-04-04 10:40:29 +00001277 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001278 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001279
Guido van Rossum374a9221991-04-04 10:40:29 +00001280 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001281
Guido van Rossum374a9221991-04-04 10:40:29 +00001282 /* BEWARE!
1283 It is essential that any operation that fails sets either
1284 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1285 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001286
Guido van Rossum374a9221991-04-04 10:40:29 +00001287 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001288
Antoine Pitroub52ec782009-01-25 16:34:23 +00001289 TARGET(NOP)
1290 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001291
Antoine Pitroub52ec782009-01-25 16:34:23 +00001292 TARGET(LOAD_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001293 x = GETLOCAL(oparg);
1294 if (x != NULL) {
1295 Py_INCREF(x);
1296 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001297 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001298 }
1299 format_exc_check_arg(PyExc_UnboundLocalError,
1300 UNBOUNDLOCAL_ERROR_MSG,
1301 PyTuple_GetItem(co->co_varnames, oparg));
1302 break;
1303
Antoine Pitroub52ec782009-01-25 16:34:23 +00001304 TARGET(LOAD_CONST)
Skip Montanaro04d80f82002-08-04 21:03:35 +00001305 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001306 Py_INCREF(x);
1307 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001308 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001309
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001310 PREDICTED_WITH_ARG(STORE_FAST);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001311 TARGET(STORE_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001312 v = POP();
1313 SETLOCAL(oparg, v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001314 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001315
Antoine Pitroub52ec782009-01-25 16:34:23 +00001316 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001317 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001318 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001319 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001320
Antoine Pitroub52ec782009-01-25 16:34:23 +00001321 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001322 v = TOP();
1323 w = SECOND();
1324 SET_TOP(w);
1325 SET_SECOND(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001326 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Antoine Pitroub52ec782009-01-25 16:34:23 +00001328 TARGET(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001329 v = TOP();
1330 w = SECOND();
1331 x = THIRD();
1332 SET_TOP(w);
1333 SET_SECOND(x);
1334 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001335 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Antoine Pitroub52ec782009-01-25 16:34:23 +00001337 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 u = TOP();
1339 v = SECOND();
1340 w = THIRD();
1341 x = FOURTH();
1342 SET_TOP(v);
1343 SET_SECOND(w);
1344 SET_THIRD(x);
1345 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001346 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Antoine Pitroub52ec782009-01-25 16:34:23 +00001348 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001349 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001350 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001351 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001352 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001353
Antoine Pitroub52ec782009-01-25 16:34:23 +00001354 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001355 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001356 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001357 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001358 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001359 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001360 STACKADJ(2);
1361 SET_TOP(x);
1362 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001363 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001364 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001366 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001367 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001368 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001369 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001370 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001371 STACKADJ(3);
1372 SET_TOP(x);
1373 SET_SECOND(w);
1374 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001375 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001376 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001377 Py_FatalError("invalid argument to DUP_TOPX"
1378 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001379 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001380 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001381
Antoine Pitroub52ec782009-01-25 16:34:23 +00001382 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001383 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001384 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001385 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001386 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001387 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001388 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001389
Antoine Pitroub52ec782009-01-25 16:34:23 +00001390 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001391 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001392 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001393 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001395 if (x != NULL) DISPATCH();
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_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001399 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001400 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001401 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001402 if (err == 0) {
1403 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001404 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001405 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001406 }
1407 else if (err > 0) {
1408 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001409 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001410 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001411 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001412 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001413 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001414 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Antoine Pitroub52ec782009-01-25 16:34:23 +00001416 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001417 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001418 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001419 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001420 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001421 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001422 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Antoine Pitroub52ec782009-01-25 16:34:23 +00001424 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001425 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001426 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001427 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001428 Py_DECREF(v);
1429 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001430 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001431 if (x != NULL) DISPATCH();
Guido van Rossum50564e81996-01-12 01:13:16 +00001432 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001433
Antoine Pitroub52ec782009-01-25 16:34:23 +00001434 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001436 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001437 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001438 Py_DECREF(v);
1439 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001440 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001441 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001442 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Antoine Pitroub52ec782009-01-25 16:34:23 +00001444 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001445 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001446 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001447 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001448 Py_DECREF(v);
1449 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001450 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001451 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001452 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Antoine Pitroub52ec782009-01-25 16:34:23 +00001454 TARGET(BINARY_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001455 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001456 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001457 x = PyNumber_FloorDivide(v, w);
1458 Py_DECREF(v);
1459 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001460 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001461 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001462 break;
1463
Antoine Pitroub52ec782009-01-25 16:34:23 +00001464 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001465 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001466 v = TOP();
Benjamin Petersonefb06b02009-02-26 18:55:48 +00001467 if (PyUnicode_CheckExact(v))
1468 x = PyUnicode_Format(v, w);
1469 else
1470 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001471 Py_DECREF(v);
1472 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001473 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001474 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001475 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Antoine Pitroub52ec782009-01-25 16:34:23 +00001477 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001479 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001480 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001481 PyUnicode_CheckExact(w)) {
1482 x = unicode_concatenate(v, w, f, next_instr);
1483 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001484 goto skip_decref_vx;
1485 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001486 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001487 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001488 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001489 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001490 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001491 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001492 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001493 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001494 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Antoine Pitroub52ec782009-01-25 16:34:23 +00001496 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001498 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001499 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001500 Py_DECREF(v);
1501 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001502 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001503 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001504 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001505
Antoine Pitroub52ec782009-01-25 16:34:23 +00001506 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001507 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001508 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001509 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001510 Py_DECREF(v);
1511 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001512 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001513 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Antoine Pitroub52ec782009-01-25 16:34:23 +00001516 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001517 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001518 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001519 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001520 Py_DECREF(v);
1521 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001522 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001523 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001524 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Antoine Pitroub52ec782009-01-25 16:34:23 +00001526 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001527 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001528 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001529 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001530 Py_DECREF(v);
1531 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001532 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001533 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001534 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Antoine Pitroub52ec782009-01-25 16:34:23 +00001536 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001537 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001538 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001539 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001540 Py_DECREF(v);
1541 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001542 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001543 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001544 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001545
Antoine Pitroub52ec782009-01-25 16:34:23 +00001546 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001547 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001548 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001549 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001550 Py_DECREF(v);
1551 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001552 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001553 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001554 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Antoine Pitroub52ec782009-01-25 16:34:23 +00001556 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001557 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001558 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001559 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001560 Py_DECREF(v);
1561 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001562 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001563 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001564 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001565
Antoine Pitroub52ec782009-01-25 16:34:23 +00001566 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001567 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001568 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001569 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001570 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001571 if (err == 0) {
1572 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001573 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001574 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001575 break;
1576
Antoine Pitroub52ec782009-01-25 16:34:23 +00001577 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001578 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001579 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001580 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001581 Py_DECREF(w);
1582 if (err == 0) {
1583 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001584 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001585 }
1586 break;
1587
Antoine Pitroub52ec782009-01-25 16:34:23 +00001588 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001589 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001590 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001591 x = PyNumber_InPlacePower(v, w, Py_None);
1592 Py_DECREF(v);
1593 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001594 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001595 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001596 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Antoine Pitroub52ec782009-01-25 16:34:23 +00001598 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001599 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001600 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001601 x = PyNumber_InPlaceMultiply(v, w);
1602 Py_DECREF(v);
1603 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001604 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001605 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001606 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Antoine Pitroub52ec782009-01-25 16:34:23 +00001608 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001609 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001610 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001611 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001612 Py_DECREF(v);
1613 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001614 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001615 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001616 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Antoine Pitroub52ec782009-01-25 16:34:23 +00001618 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001619 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001620 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001621 x = PyNumber_InPlaceFloorDivide(v, w);
1622 Py_DECREF(v);
1623 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001624 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001625 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001626 break;
1627
Antoine Pitroub52ec782009-01-25 16:34:23 +00001628 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001629 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001630 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001631 x = PyNumber_InPlaceRemainder(v, w);
1632 Py_DECREF(v);
1633 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001634 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001635 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001636 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Antoine Pitroub52ec782009-01-25 16:34:23 +00001638 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001639 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001640 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001641 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001642 PyUnicode_CheckExact(w)) {
1643 x = unicode_concatenate(v, w, f, next_instr);
1644 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001645 goto skip_decref_v;
1646 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001647 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001648 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001649 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001650 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001651 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001652 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001653 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001654 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001655 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Antoine Pitroub52ec782009-01-25 16:34:23 +00001657 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001658 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001659 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001660 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001661 Py_DECREF(v);
1662 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001663 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001664 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001665 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001666
Antoine Pitroub52ec782009-01-25 16:34:23 +00001667 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001668 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001669 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001670 x = PyNumber_InPlaceLshift(v, w);
1671 Py_DECREF(v);
1672 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001673 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001674 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001675 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Antoine Pitroub52ec782009-01-25 16:34:23 +00001677 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001678 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001679 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001680 x = PyNumber_InPlaceRshift(v, w);
1681 Py_DECREF(v);
1682 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001683 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001684 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001685 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Antoine Pitroub52ec782009-01-25 16:34:23 +00001687 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001688 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001689 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001690 x = PyNumber_InPlaceAnd(v, w);
1691 Py_DECREF(v);
1692 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001693 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001694 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001695 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Antoine Pitroub52ec782009-01-25 16:34:23 +00001697 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001698 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001699 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001700 x = PyNumber_InPlaceXor(v, w);
1701 Py_DECREF(v);
1702 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001703 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001704 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001705 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001706
Antoine Pitroub52ec782009-01-25 16:34:23 +00001707 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001708 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001709 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001710 x = PyNumber_InPlaceOr(v, w);
1711 Py_DECREF(v);
1712 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001713 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001714 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001715 break;
1716
Antoine Pitroub52ec782009-01-25 16:34:23 +00001717 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001718 w = TOP();
1719 v = SECOND();
1720 u = THIRD();
1721 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001723 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001724 Py_DECREF(u);
1725 Py_DECREF(v);
1726 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001727 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Antoine Pitroub52ec782009-01-25 16:34:23 +00001730 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001731 w = TOP();
1732 v = SECOND();
1733 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001734 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001735 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001736 Py_DECREF(v);
1737 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001738 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001739 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001740
Antoine Pitroub52ec782009-01-25 16:34:23 +00001741 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001743 w = PySys_GetObject("displayhook");
1744 if (w == NULL) {
1745 PyErr_SetString(PyExc_RuntimeError,
1746 "lost sys.displayhook");
1747 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001748 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001749 }
1750 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001751 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001752 if (x == NULL)
1753 err = -1;
1754 }
1755 if (err == 0) {
1756 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001757 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001758 if (w == NULL)
1759 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001760 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001761 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001762 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001764
Thomas Wouters434d0822000-08-24 20:11:32 +00001765#ifdef CASE_TOO_BIG
1766 default: switch (opcode) {
1767#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001768 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001769 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001770 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001771 case 2:
1772 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001773 case 1:
1774 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001775 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001776 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001777 break;
1778 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001779 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001780 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001781 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001782 break;
1783 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001784 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001785
Antoine Pitroub52ec782009-01-25 16:34:23 +00001786 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001787 x = POP();
1788 v = f->f_locals;
1789 Py_XDECREF(v);
1790 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001791 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Antoine Pitroub52ec782009-01-25 16:34:23 +00001793 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001794 retval = POP();
1795 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001796 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001797
Antoine Pitroub52ec782009-01-25 16:34:23 +00001798 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001799 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001800 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001801 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001802 /* Put aside the current exception state and restore
1803 that of the calling frame. This only serves when
1804 "yield" is used inside an except handler. */
1805 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001806 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001807
Antoine Pitroub52ec782009-01-25 16:34:23 +00001808 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001809 {
1810 PyTryBlock *b = PyFrame_BlockPop(f);
1811 if (b->b_type != EXCEPT_HANDLER) {
1812 PyErr_SetString(PyExc_SystemError,
1813 "popped block is not an except handler");
1814 why = WHY_EXCEPTION;
1815 break;
1816 }
1817 UNWIND_EXCEPT_HANDLER(b);
1818 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001819 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001820
Antoine Pitroub52ec782009-01-25 16:34:23 +00001821 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001822 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001823 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001824 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001825 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001826 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Christian Heimes180510d2008-03-03 19:15:45 +00001828 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001829 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001830 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001831 if (PyLong_Check(v)) {
1832 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001833 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001834 if (why == WHY_RETURN ||
1835 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001836 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001837 if (why == WHY_SILENCED) {
1838 /* An exception was silenced by 'with', we must
1839 manually unwind the EXCEPT_HANDLER block which was
1840 created when the exception was caught, otherwise
1841 the stack will be in an inconsistent state. */
1842 PyTryBlock *b = PyFrame_BlockPop(f);
1843 if (b->b_type != EXCEPT_HANDLER) {
1844 PyErr_SetString(PyExc_SystemError,
1845 "popped block is not an except handler");
1846 why = WHY_EXCEPTION;
1847 }
1848 else {
1849 UNWIND_EXCEPT_HANDLER(b);
1850 why = WHY_NOT;
1851 }
1852 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001853 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001854 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001855 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001856 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001857 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001858 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001859 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001861 else if (v != Py_None) {
1862 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001863 "'finally' pops bad exception");
1864 why = WHY_EXCEPTION;
1865 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001866 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001867 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001868
Antoine Pitroub52ec782009-01-25 16:34:23 +00001869 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001870 x = PyDict_GetItemString(f->f_builtins,
1871 "__build_class__");
1872 if (x == NULL) {
1873 PyErr_SetString(PyExc_ImportError,
1874 "__build_class__ not found");
1875 break;
1876 }
1877 Py_INCREF(x);
1878 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001879 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001880
Antoine Pitroub52ec782009-01-25 16:34:23 +00001881 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001882 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001883 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001884 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001885 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001886 err = PyDict_SetItem(x, w, v);
1887 else
1888 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001889 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001890 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001891 break;
1892 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001893 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001894 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001896
Antoine Pitroub52ec782009-01-25 16:34:23 +00001897 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001898 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001899 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001900 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001901 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001902 NAME_ERROR_MSG,
1903 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001904 break;
1905 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001906 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001907 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001908 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001909
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001910 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001911 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001912 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001913 if (PyTuple_CheckExact(v) &&
1914 PyTuple_GET_SIZE(v) == oparg) {
1915 PyObject **items = \
1916 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001917 while (oparg--) {
1918 w = items[oparg];
1919 Py_INCREF(w);
1920 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001921 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001922 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001923 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001924 } else if (PyList_CheckExact(v) &&
1925 PyList_GET_SIZE(v) == oparg) {
1926 PyObject **items = \
1927 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001928 while (oparg--) {
1929 w = items[oparg];
1930 Py_INCREF(w);
1931 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001932 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001933 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001934 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001935 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001936 } else {
1937 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001938 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001939 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001940 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001941 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001942
Antoine Pitroub52ec782009-01-25 16:34:23 +00001943 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00001944 {
1945 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1946 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001947
Guido van Rossum0368b722007-05-11 16:50:42 +00001948 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1949 stack_pointer + totalargs)) {
1950 stack_pointer += totalargs;
1951 } else {
1952 why = WHY_EXCEPTION;
1953 }
1954 Py_DECREF(v);
1955 break;
1956 }
1957
Antoine Pitroub52ec782009-01-25 16:34:23 +00001958 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001959 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001960 v = TOP();
1961 u = SECOND();
1962 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001963 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1964 Py_DECREF(v);
1965 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001966 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001967 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Antoine Pitroub52ec782009-01-25 16:34:23 +00001969 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001970 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001971 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001972 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1973 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001974 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001975 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Antoine Pitroub52ec782009-01-25 16:34:23 +00001977 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001978 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001979 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001980 err = PyDict_SetItem(f->f_globals, w, v);
1981 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001982 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001983 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001984
Antoine Pitroub52ec782009-01-25 16:34:23 +00001985 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001986 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001987 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001988 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001989 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001990 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001991
Antoine Pitroub52ec782009-01-25 16:34:23 +00001992 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001993 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001994 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001995 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001996 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001997 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001998 break;
1999 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002000 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002001 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002002 Py_XINCREF(x);
2003 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002004 else {
2005 x = PyObject_GetItem(v, w);
2006 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002007 if (!PyErr_ExceptionMatches(
2008 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002009 break;
2010 PyErr_Clear();
2011 }
2012 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002013 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002014 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002015 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002016 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002017 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002018 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002019 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002020 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002021 break;
2022 }
2023 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002024 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002025 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002026 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002027 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002028
Antoine Pitroub52ec782009-01-25 16:34:23 +00002029 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002030 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002031 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002032 /* Inline the PyDict_GetItem() calls.
2033 WARNING: this is an extreme speed hack.
2034 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002035 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002036 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002037 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002038 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002039 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002040 e = d->ma_lookup(d, w, hash);
2041 if (e == NULL) {
2042 x = NULL;
2043 break;
2044 }
2045 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002046 if (x != NULL) {
2047 Py_INCREF(x);
2048 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002049 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002050 }
2051 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002052 e = d->ma_lookup(d, w, hash);
2053 if (e == NULL) {
2054 x = NULL;
2055 break;
2056 }
2057 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002058 if (x != NULL) {
2059 Py_INCREF(x);
2060 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002061 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002062 }
2063 goto load_global_error;
2064 }
2065 }
2066 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002067 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002068 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002069 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002070 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002071 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002072 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002073 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002074 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002075 break;
2076 }
2077 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002078 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002079 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002080 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002081
Antoine Pitroub52ec782009-01-25 16:34:23 +00002082 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002083 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002084 if (x != NULL) {
2085 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002086 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002087 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002088 format_exc_check_arg(
2089 PyExc_UnboundLocalError,
2090 UNBOUNDLOCAL_ERROR_MSG,
2091 PyTuple_GetItem(co->co_varnames, oparg)
2092 );
2093 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002094
Antoine Pitroub52ec782009-01-25 16:34:23 +00002095 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002096 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002097 Py_INCREF(x);
2098 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002099 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002100 break;
2101
Antoine Pitroub52ec782009-01-25 16:34:23 +00002102 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002103 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002104 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002105 if (w != NULL) {
2106 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002107 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002108 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002109 err = -1;
2110 /* Don't stomp existing exception */
2111 if (PyErr_Occurred())
2112 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002113 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2114 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002115 oparg);
2116 format_exc_check_arg(
2117 PyExc_UnboundLocalError,
2118 UNBOUNDLOCAL_ERROR_MSG,
2119 v);
2120 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002121 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2122 PyTuple_GET_SIZE(co->co_cellvars));
2123 format_exc_check_arg(PyExc_NameError,
2124 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002125 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002126 break;
2127
Antoine Pitroub52ec782009-01-25 16:34:23 +00002128 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002129 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002130 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002131 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002132 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002133 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002134
Antoine Pitroub52ec782009-01-25 16:34:23 +00002135 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002136 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002137 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002138 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002139 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002140 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002141 }
2142 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002143 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002144 }
2145 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002146
Antoine Pitroub52ec782009-01-25 16:34:23 +00002147 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002148 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002149 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002150 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002151 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002152 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002153 }
2154 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002155 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002156 }
2157 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002158
Antoine Pitroub52ec782009-01-25 16:34:23 +00002159 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002160 x = PySet_New(NULL);
2161 if (x != NULL) {
2162 for (; --oparg >= 0;) {
2163 w = POP();
2164 if (err == 0)
2165 err = PySet_Add(x, w);
2166 Py_DECREF(w);
2167 }
2168 if (err != 0) {
2169 Py_DECREF(x);
2170 break;
2171 }
2172 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002173 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002174 }
2175 break;
2176
Antoine Pitroub52ec782009-01-25 16:34:23 +00002177 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002178 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002179 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002180 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002181 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002182
Antoine Pitroub52ec782009-01-25 16:34:23 +00002183 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002184 w = TOP(); /* key */
2185 u = SECOND(); /* value */
2186 v = THIRD(); /* dict */
2187 STACKADJ(-2);
2188 assert (PyDict_CheckExact(v));
2189 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2190 Py_DECREF(u);
2191 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002192 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002193 break;
2194
Antoine Pitroub52ec782009-01-25 16:34:23 +00002195 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002196 w = TOP(); /* key */
2197 u = SECOND(); /* value */
2198 STACKADJ(-2);
2199 v = stack_pointer[-oparg]; /* dict */
2200 assert (PyDict_CheckExact(v));
2201 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2202 Py_DECREF(u);
2203 Py_DECREF(w);
2204 if (err == 0) {
2205 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002206 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002207 }
2208 break;
2209
Antoine Pitroub52ec782009-01-25 16:34:23 +00002210 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002211 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002212 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002213 x = PyObject_GetAttr(v, w);
2214 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002215 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002216 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002217 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002218
Antoine Pitroub52ec782009-01-25 16:34:23 +00002219 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002220 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002221 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002222 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002223 Py_DECREF(v);
2224 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002225 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002226 if (x == NULL) break;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002227 PREDICT(POP_JUMP_IF_FALSE);
2228 PREDICT(POP_JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002229 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002230
Antoine Pitroub52ec782009-01-25 16:34:23 +00002231 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002232 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002233 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002234 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002235 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002236 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002237 break;
2238 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002239 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002240 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002241 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002242 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002243 w = PyTuple_Pack(5,
2244 w,
2245 f->f_globals,
2246 f->f_locals == NULL ?
2247 Py_None : f->f_locals,
2248 v,
2249 u);
2250 else
2251 w = PyTuple_Pack(4,
2252 w,
2253 f->f_globals,
2254 f->f_locals == NULL ?
2255 Py_None : f->f_locals,
2256 v);
2257 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002258 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002259 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002260 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002261 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002262 x = NULL;
2263 break;
2264 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002265 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002266 v = x;
2267 x = PyEval_CallObject(v, w);
2268 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002269 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002270 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002271 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002272 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002273 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002274
Antoine Pitroub52ec782009-01-25 16:34:23 +00002275 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002276 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002277 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002278 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002279 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002280 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002281 break;
2282 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002283 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002284 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002285 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002286 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002287 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002288 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002289 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002290
Antoine Pitroub52ec782009-01-25 16:34:23 +00002291 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002292 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002293 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002294 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002295 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002296 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002297 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002298 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002299 break;
2300
Antoine Pitroub52ec782009-01-25 16:34:23 +00002301 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002302 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002303 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002304
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002305 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2306 TARGET(POP_JUMP_IF_FALSE)
2307 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002308 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002309 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002310 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002311 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002312 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002313 Py_DECREF(w);
2314 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002315 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002316 }
2317 err = PyObject_IsTrue(w);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002318 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002319 if (err > 0)
2320 err = 0;
2321 else if (err == 0)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002322 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002323 else
2324 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002325 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002327 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2328 TARGET(POP_JUMP_IF_TRUE)
2329 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002330 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002331 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002332 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002333 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002334 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002335 Py_DECREF(w);
2336 JUMPTO(oparg);
2337 FAST_DISPATCH();
2338 }
2339 err = PyObject_IsTrue(w);
2340 Py_DECREF(w);
2341 if (err > 0) {
2342 err = 0;
2343 JUMPTO(oparg);
2344 }
2345 else if (err == 0)
2346 ;
2347 else
2348 break;
2349 DISPATCH();
2350
2351 TARGET(JUMP_IF_FALSE_OR_POP)
2352 w = TOP();
2353 if (w == Py_True) {
2354 STACKADJ(-1);
2355 Py_DECREF(w);
2356 FAST_DISPATCH();
2357 }
2358 if (w == Py_False) {
2359 JUMPTO(oparg);
2360 FAST_DISPATCH();
2361 }
2362 err = PyObject_IsTrue(w);
2363 if (err > 0) {
2364 STACKADJ(-1);
2365 Py_DECREF(w);
2366 err = 0;
2367 }
2368 else if (err == 0)
2369 JUMPTO(oparg);
2370 else
2371 break;
2372 DISPATCH();
2373
2374 TARGET(JUMP_IF_TRUE_OR_POP)
2375 w = TOP();
2376 if (w == Py_False) {
2377 STACKADJ(-1);
2378 Py_DECREF(w);
2379 FAST_DISPATCH();
2380 }
2381 if (w == Py_True) {
2382 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002383 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002384 }
2385 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002386 if (err > 0) {
2387 err = 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002388 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002389 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002390 else if (err == 0) {
2391 STACKADJ(-1);
2392 Py_DECREF(w);
2393 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002394 else
2395 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002396 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002397
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002398 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002399 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002400 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002401#if FAST_LOOPS
2402 /* Enabling this path speeds-up all while and for-loops by bypassing
2403 the per-loop checks for signals. By default, this should be turned-off
2404 because it prevents detection of a control-break in tight loops like
2405 "while 1: pass". Compile with this option turned-on when you need
2406 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002407 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002408 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002409 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002410#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002411 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002412#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002413
Antoine Pitroub52ec782009-01-25 16:34:23 +00002414 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002415 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002416 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002417 x = PyObject_GetIter(v);
2418 Py_DECREF(v);
2419 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002420 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002421 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002422 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002423 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002424 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002425 break;
2426
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002427 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002428 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002429 /* before: [iter]; after: [iter, iter()] *or* [] */
2430 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002431 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002432 if (x != NULL) {
2433 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002434 PREDICT(STORE_FAST);
2435 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002436 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002437 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002438 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002439 if (!PyErr_ExceptionMatches(
2440 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002441 break;
2442 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002443 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002444 /* iterator ended normally */
2445 x = v = POP();
2446 Py_DECREF(v);
2447 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002448 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002449
Antoine Pitroub52ec782009-01-25 16:34:23 +00002450 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002451 why = WHY_BREAK;
2452 goto fast_block_end;
2453
Antoine Pitroub52ec782009-01-25 16:34:23 +00002454 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002455 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002456 if (!retval) {
2457 x = NULL;
2458 break;
2459 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002460 why = WHY_CONTINUE;
2461 goto fast_block_end;
2462
Antoine Pitroub52ec782009-01-25 16:34:23 +00002463 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2464 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2465 TARGET(SETUP_FINALLY)
2466 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002467 /* NOTE: If you add any new block-setup opcodes that
2468 are not try/except/finally handlers, you may need
2469 to update the PyGen_NeedsFinalizing() function.
2470 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002471
Guido van Rossumb209a111997-04-29 18:18:01 +00002472 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002473 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002474 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002475
Antoine Pitroub52ec782009-01-25 16:34:23 +00002476 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002477 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002478 /* At the top of the stack are 1-3 values indicating
2479 how/why we entered the finally clause:
2480 - TOP = None
2481 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2482 - TOP = WHY_*; no retval below it
2483 - (TOP, SECOND, THIRD) = exc_info()
2484 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002485 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002486 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002487 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002488 EXIT(None, None, None)
2489
2490 In all cases, we remove EXIT from the stack, leaving
2491 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002492
2493 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002494 *and* the function call returns a 'true' value, we
2495 "zap" this information, to prevent END_FINALLY from
2496 re-raising the exception. (But non-local gotos
2497 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002498 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002499
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002500 PyObject *exit_func = POP();
2501 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002502 if (u == Py_None) {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002503 v = w = Py_None;
2504 }
2505 else if (PyLong_Check(u)) {
Guido van Rossumc2e20742006-02-27 22:32:47 +00002506 u = v = w = Py_None;
2507 }
2508 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002509 v = SECOND();
2510 w = THIRD();
Guido van Rossumc2e20742006-02-27 22:32:47 +00002511 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002512 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002513 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2514 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002515 Py_DECREF(exit_func);
2516 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002517 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002518
2519 if (u != Py_None)
2520 err = PyObject_IsTrue(x);
2521 else
2522 err = 0;
2523 Py_DECREF(x);
2524
2525 if (err < 0)
2526 break; /* Go to error exit */
2527 else if (err > 0) {
2528 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002529 /* There was an exception and a True return */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002530 STACKADJ(-2);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002531 SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002532 Py_DECREF(u);
2533 Py_DECREF(v);
2534 Py_DECREF(w);
Guido van Rossumf6694362006-03-10 02:28:35 +00002535 }
Christian Heimes180510d2008-03-03 19:15:45 +00002536 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002537 break;
2538 }
2539
Antoine Pitroub52ec782009-01-25 16:34:23 +00002540 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002541 {
2542 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002543 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002544 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002545#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002546 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002547#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002548 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002549#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002550 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002551 PUSH(x);
2552 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002553 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002554 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002555 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002556
Antoine Pitroub52ec782009-01-25 16:34:23 +00002557 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2558 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2559 TARGET(CALL_FUNCTION_VAR_KW)
2560 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002561 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002562 int na = oparg & 0xff;
2563 int nk = (oparg>>8) & 0xff;
2564 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002565 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002566 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002567 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002568 if (flags & CALL_FLAG_VAR)
2569 n++;
2570 if (flags & CALL_FLAG_KW)
2571 n++;
2572 pfunc = stack_pointer - n - 1;
2573 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002574
Guido van Rossumac7be682001-01-17 15:42:30 +00002575 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002576 && PyMethod_GET_SELF(func) != NULL) {
2577 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002578 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002579 func = PyMethod_GET_FUNCTION(func);
2580 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002581 Py_DECREF(*pfunc);
2582 *pfunc = self;
2583 na++;
2584 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002585 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002586 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002587 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002588 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002589 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002590 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002591 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002592 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002593
Jeremy Hylton76901512000-03-28 23:49:17 +00002594 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002595 w = POP();
2596 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002597 }
2598 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002599 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002600 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002601 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002602 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002603
Antoine Pitroub52ec782009-01-25 16:34:23 +00002604 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2605 TARGET(MAKE_FUNCTION)
2606 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002607 {
2608 int posdefaults = oparg & 0xff;
2609 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002610 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002611
Guido van Rossum681d79a1995-07-18 14:51:37 +00002612 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002613 x = PyFunction_New(v, f->f_globals);
2614 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002615
Guido van Rossum0240b922007-02-26 21:23:50 +00002616 if (x != NULL && opcode == MAKE_CLOSURE) {
2617 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002618 if (PyFunction_SetClosure(x, v) != 0) {
2619 /* Can't happen unless bytecode is corrupt. */
2620 why = WHY_EXCEPTION;
2621 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002622 Py_DECREF(v);
2623 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002624
2625 if (x != NULL && num_annotations > 0) {
2626 Py_ssize_t name_ix;
2627 u = POP(); /* names of args with annotations */
2628 v = PyDict_New();
2629 if (v == NULL) {
2630 Py_DECREF(x);
2631 x = NULL;
2632 break;
2633 }
2634 name_ix = PyTuple_Size(u);
2635 assert(num_annotations == name_ix+1);
2636 while (name_ix > 0) {
2637 --name_ix;
2638 t = PyTuple_GET_ITEM(u, name_ix);
2639 w = POP();
2640 /* XXX(nnorwitz): check for errors */
2641 PyDict_SetItem(v, t, w);
2642 Py_DECREF(w);
2643 }
2644
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002645 if (PyFunction_SetAnnotations(x, v) != 0) {
2646 /* Can't happen unless
2647 PyFunction_SetAnnotations changes. */
2648 why = WHY_EXCEPTION;
2649 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002650 Py_DECREF(v);
2651 Py_DECREF(u);
2652 }
2653
Guido van Rossum681d79a1995-07-18 14:51:37 +00002654 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002655 if (x != NULL && posdefaults > 0) {
2656 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002657 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002658 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002659 x = NULL;
2660 break;
2661 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002662 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002663 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002664 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002665 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002666 if (PyFunction_SetDefaults(x, v) != 0) {
2667 /* Can't happen unless
2668 PyFunction_SetDefaults changes. */
2669 why = WHY_EXCEPTION;
2670 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002671 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002672 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002673 if (x != NULL && kwdefaults > 0) {
2674 v = PyDict_New();
2675 if (v == NULL) {
2676 Py_DECREF(x);
2677 x = NULL;
2678 break;
2679 }
2680 while (--kwdefaults >= 0) {
2681 w = POP(); /* default value */
2682 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002683 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002684 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002685 Py_DECREF(w);
2686 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002687 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002688 if (PyFunction_SetKwDefaults(x, v) != 0) {
2689 /* Can't happen unless
2690 PyFunction_SetKwDefaults changes. */
2691 why = WHY_EXCEPTION;
2692 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002693 Py_DECREF(v);
2694 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002695 PUSH(x);
2696 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002697 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002698
Antoine Pitroub52ec782009-01-25 16:34:23 +00002699 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002700 if (oparg == 3)
2701 w = POP();
2702 else
2703 w = NULL;
2704 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002705 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002706 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002707 Py_DECREF(u);
2708 Py_DECREF(v);
2709 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002710 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002711 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002712 break;
2713
Antoine Pitroub52ec782009-01-25 16:34:23 +00002714 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002715 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002716 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002717 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002718
Antoine Pitroub52ec782009-01-25 16:34:23 +00002719#ifdef USE_COMPUTED_GOTOS
2720 _unknown_opcode:
2721#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002722 default:
2723 fprintf(stderr,
2724 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002725 PyCode_Addr2Line(f->f_code, f->f_lasti),
2726 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002727 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002728 why = WHY_EXCEPTION;
2729 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002730
2731#ifdef CASE_TOO_BIG
2732 }
2733#endif
2734
Guido van Rossum374a9221991-04-04 10:40:29 +00002735 } /* switch */
2736
2737 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002738
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002739 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002740
Guido van Rossum374a9221991-04-04 10:40:29 +00002741 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002742
Guido van Rossum374a9221991-04-04 10:40:29 +00002743 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002744 if (err == 0 && x != NULL) {
2745#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002746 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002747 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002748 fprintf(stderr,
2749 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002750 else {
2751#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002752 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002753 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002754#ifdef CHECKEXC
2755 }
2756#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002757 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002758 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002759 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002760 err = 0;
2761 }
2762
Guido van Rossum374a9221991-04-04 10:40:29 +00002763 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002764
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002765 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002766 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002767 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002768 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002769 why = WHY_EXCEPTION;
2770 }
2771 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002772#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002773 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002774 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002775 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002776 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002777 sprintf(buf, "Stack unwind with exception "
2778 "set and why=%d", why);
2779 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002780 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002781 }
2782#endif
2783
2784 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002785
Guido van Rossum374a9221991-04-04 10:40:29 +00002786 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002787 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002788
Fred Drake8f51f542001-10-04 14:48:42 +00002789 if (tstate->c_tracefunc != NULL)
2790 call_exc_trace(tstate->c_tracefunc,
2791 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002792 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002793
Guido van Rossum374a9221991-04-04 10:40:29 +00002794 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002795
Guido van Rossum374a9221991-04-04 10:40:29 +00002796 if (why == WHY_RERAISE)
2797 why = WHY_EXCEPTION;
2798
2799 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002800
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002801fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002802 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002803 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002804
Tim Peters8a5c3c72004-04-05 19:36:21 +00002805 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002806 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2807 /* For a continue inside a try block,
2808 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002809 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2810 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002811 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002812 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002813 Py_DECREF(retval);
2814 break;
2815 }
2816
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002817 if (b->b_type == EXCEPT_HANDLER) {
2818 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002819 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002820 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002821 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002822 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2823 why = WHY_NOT;
2824 JUMPTO(b->b_handler);
2825 break;
2826 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002827 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2828 || b->b_type == SETUP_FINALLY)) {
2829 PyObject *exc, *val, *tb;
2830 int handler = b->b_handler;
2831 /* Beware, this invalidates all b->b_* fields */
2832 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2833 PUSH(tstate->exc_traceback);
2834 PUSH(tstate->exc_value);
2835 if (tstate->exc_type != NULL) {
2836 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002837 }
2838 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002839 Py_INCREF(Py_None);
2840 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002841 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002842 PyErr_Fetch(&exc, &val, &tb);
2843 /* Make the raw exception data
2844 available to the handler,
2845 so a program can emulate the
2846 Python main loop. */
2847 PyErr_NormalizeException(
2848 &exc, &val, &tb);
2849 PyException_SetTraceback(val, tb);
2850 Py_INCREF(exc);
2851 tstate->exc_type = exc;
2852 Py_INCREF(val);
2853 tstate->exc_value = val;
2854 tstate->exc_traceback = tb;
2855 if (tb == NULL)
2856 tb = Py_None;
2857 Py_INCREF(tb);
2858 PUSH(tb);
2859 PUSH(val);
2860 PUSH(exc);
2861 why = WHY_NOT;
2862 JUMPTO(handler);
2863 break;
2864 }
2865 if (b->b_type == SETUP_FINALLY) {
2866 if (why & (WHY_RETURN | WHY_CONTINUE))
2867 PUSH(retval);
2868 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002869 why = WHY_NOT;
2870 JUMPTO(b->b_handler);
2871 break;
2872 }
2873 } /* unwind stack */
2874
2875 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002876
Guido van Rossum374a9221991-04-04 10:40:29 +00002877 if (why != WHY_NOT)
2878 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002879 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002880
Guido van Rossum374a9221991-04-04 10:40:29 +00002881 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002882
Tim Peters8a5c3c72004-04-05 19:36:21 +00002883 assert(why != WHY_YIELD);
2884 /* Pop remaining stack entries. */
2885 while (!EMPTY()) {
2886 v = POP();
2887 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002888 }
2889
Tim Peters8a5c3c72004-04-05 19:36:21 +00002890 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002891 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002892
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002893fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002894 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002895 if (tstate->c_tracefunc) {
2896 if (why == WHY_RETURN || why == WHY_YIELD) {
2897 if (call_trace(tstate->c_tracefunc,
2898 tstate->c_traceobj, f,
2899 PyTrace_RETURN, retval)) {
2900 Py_XDECREF(retval);
2901 retval = NULL;
2902 why = WHY_EXCEPTION;
2903 }
2904 }
2905 else if (why == WHY_EXCEPTION) {
2906 call_trace_protected(tstate->c_tracefunc,
2907 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002908 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002909 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002910 }
Fred Drake8f51f542001-10-04 14:48:42 +00002911 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002912 if (why == WHY_EXCEPTION)
2913 call_trace_protected(tstate->c_profilefunc,
2914 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002915 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002916 else if (call_trace(tstate->c_profilefunc,
2917 tstate->c_profileobj, f,
2918 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002919 Py_XDECREF(retval);
2920 retval = NULL;
2921 why = WHY_EXCEPTION;
2922 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002923 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002924 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002925
Tim Peters5ca576e2001-06-18 22:08:13 +00002926 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002927exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002928 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002929 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002930
Guido van Rossum96a42c81992-01-12 02:29:51 +00002931 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002932}
2933
Guido van Rossumc2e20742006-02-27 22:32:47 +00002934/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002935 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002936 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002937
Tim Peters6d6c1a32001-08-02 04:15:00 +00002938PyObject *
2939PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002940 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002941 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002942{
2943 register PyFrameObject *f;
2944 register PyObject *retval = NULL;
2945 register PyObject **fastlocals, **freevars;
2946 PyThreadState *tstate = PyThreadState_GET();
2947 PyObject *x, *u;
2948
2949 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002950 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002951 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002952 return NULL;
2953 }
2954
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002955 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002956 assert(globals != NULL);
2957 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002958 if (f == NULL)
2959 return NULL;
2960
2961 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002962 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002963
2964 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002965 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002966 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2967 int i;
2968 int n = argcount;
2969 PyObject *kwdict = NULL;
2970 if (co->co_flags & CO_VARKEYWORDS) {
2971 kwdict = PyDict_New();
2972 if (kwdict == NULL)
2973 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002974 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002975 if (co->co_flags & CO_VARARGS)
2976 i++;
2977 SETLOCAL(i, kwdict);
2978 }
2979 if (argcount > co->co_argcount) {
2980 if (!(co->co_flags & CO_VARARGS)) {
2981 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002982 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002983 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002984 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002985 defcount ? "at most" : "exactly",
2986 co->co_argcount,
2987 kwcount ? "non-keyword " : "",
2988 co->co_argcount == 1 ? "" : "s",
2989 argcount);
2990 goto fail;
2991 }
2992 n = co->co_argcount;
2993 }
2994 for (i = 0; i < n; i++) {
2995 x = args[i];
2996 Py_INCREF(x);
2997 SETLOCAL(i, x);
2998 }
2999 if (co->co_flags & CO_VARARGS) {
3000 u = PyTuple_New(argcount - n);
3001 if (u == NULL)
3002 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003003 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00003004 for (i = n; i < argcount; i++) {
3005 x = args[i];
3006 Py_INCREF(x);
3007 PyTuple_SET_ITEM(u, i-n, x);
3008 }
3009 }
3010 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003011 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003012 PyObject *keyword = kws[2*i];
3013 PyObject *value = kws[2*i + 1];
3014 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00003015 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003016 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003017 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003018 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00003019 goto fail;
3020 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003021 /* Speed hack: do raw pointer compares. As names are
3022 normally interned this should almost always hit. */
3023 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003024 for (j = 0;
3025 j < co->co_argcount + co->co_kwonlyargcount;
3026 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003027 PyObject *nm = co_varnames[j];
3028 if (nm == keyword)
3029 goto kw_found;
3030 }
3031 /* Slow fallback, just in case */
3032 for (j = 0;
3033 j < co->co_argcount + co->co_kwonlyargcount;
3034 j++) {
3035 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003036 int cmp = PyObject_RichCompareBool(
3037 keyword, nm, Py_EQ);
3038 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003039 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003040 else if (cmp < 0)
3041 goto fail;
3042 }
3043 /* Check errors from Compare */
3044 if (PyErr_Occurred())
3045 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003046 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003047 if (kwdict == NULL) {
3048 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003049 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00003050 "keyword argument '%S'",
3051 co->co_name,
3052 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00003053 goto fail;
3054 }
3055 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003056 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003057 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003058kw_found:
3059 if (GETLOCAL(j) != NULL) {
3060 PyErr_Format(PyExc_TypeError,
3061 "%U() got multiple "
3062 "values for keyword "
3063 "argument '%S'",
3064 co->co_name,
3065 keyword);
3066 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003067 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003068 Py_INCREF(value);
3069 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003070 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003071 if (co->co_kwonlyargcount > 0) {
3072 for (i = co->co_argcount;
3073 i < co->co_argcount + co->co_kwonlyargcount;
3074 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003075 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003076 if (GETLOCAL(i) != NULL)
3077 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003078 name = PyTuple_GET_ITEM(co->co_varnames, i);
3079 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003080 if (kwdefs != NULL)
3081 def = PyDict_GetItem(kwdefs, name);
3082 if (def != NULL) {
3083 Py_INCREF(def);
3084 SETLOCAL(i, def);
3085 continue;
3086 }
3087 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003088 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003089 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003090 goto fail;
3091 }
3092 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003093 if (argcount < co->co_argcount) {
3094 int m = co->co_argcount - defcount;
3095 for (i = argcount; i < m; i++) {
3096 if (GETLOCAL(i) == NULL) {
3097 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003098 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003099 "%spositional argument%s "
3100 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003101 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003102 ((co->co_flags & CO_VARARGS) ||
3103 defcount) ? "at least"
3104 : "exactly",
3105 m, kwcount ? "non-keyword " : "",
3106 m == 1 ? "" : "s", i);
3107 goto fail;
3108 }
3109 }
3110 if (n > m)
3111 i = n - m;
3112 else
3113 i = 0;
3114 for (; i < defcount; i++) {
3115 if (GETLOCAL(m+i) == NULL) {
3116 PyObject *def = defs[i];
3117 Py_INCREF(def);
3118 SETLOCAL(m+i, def);
3119 }
3120 }
3121 }
3122 }
3123 else {
3124 if (argcount > 0 || kwcount > 0) {
3125 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003126 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003127 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003128 argcount + kwcount);
3129 goto fail;
3130 }
3131 }
3132 /* Allocate and initialize storage for cell vars, and copy free
3133 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003134 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003135 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003136 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003137 PyObject *c;
3138
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003139 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003140 if (co->co_flags & CO_VARARGS)
3141 nargs++;
3142 if (co->co_flags & CO_VARKEYWORDS)
3143 nargs++;
3144
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 /* Initialize each cell var, taking into account
3146 cell vars that are initialized from arguments.
3147
3148 Should arrange for the compiler to put cellvars
3149 that are arguments at the beginning of the cellvars
3150 list so that we can march over it more efficiently?
3151 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003152 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003153 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003154 PyTuple_GET_ITEM(co->co_cellvars, i));
3155 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003157 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003158 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003159 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003160 c = PyCell_New(GETLOCAL(j));
3161 if (c == NULL)
3162 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003163 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003164 found = 1;
3165 break;
3166 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003167 }
3168 if (found == 0) {
3169 c = PyCell_New(NULL);
3170 if (c == NULL)
3171 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003172 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003173 }
3174 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003175 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003176 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003177 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003178 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003179 PyObject *o = PyTuple_GET_ITEM(closure, i);
3180 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003181 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003182 }
3183 }
3184
Tim Peters5ca576e2001-06-18 22:08:13 +00003185 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003186 /* Don't need to keep the reference to f_back, it will be set
3187 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003188 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003189 f->f_back = NULL;
3190
Jeremy Hylton985eba52003-02-05 23:13:00 +00003191 PCALL(PCALL_GENERATOR);
3192
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003193 /* Create a new generator that owns the ready to run frame
3194 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003195 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003196 }
3197
Thomas Woutersce272b62007-09-19 21:19:28 +00003198 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003199
Thomas Woutersce272b62007-09-19 21:19:28 +00003200fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003201
Tim Petersb13680b2001-11-27 23:29:29 +00003202 /* decref'ing the frame can cause __del__ methods to get invoked,
3203 which can call back into Python. While we're done with the
3204 current Python frame (f), the associated C stack is still in use,
3205 so recursion_depth must be boosted for the duration.
3206 */
3207 assert(tstate != NULL);
3208 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003209 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003210 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003211 return retval;
3212}
3213
3214
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003215/* Logic for the raise statement (too complicated for inlining).
3216 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003217static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003218do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003219{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003220 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003221
3222 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003223 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003224 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003225 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003226 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003227 value = tstate->exc_value;
3228 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003229 if (type == Py_None) {
3230 PyErr_SetString(PyExc_RuntimeError,
3231 "No active exception to reraise");
3232 return WHY_EXCEPTION;
3233 }
3234 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003235 Py_XINCREF(value);
3236 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003237 PyErr_Restore(type, value, tb);
3238 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003240
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003241 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003242 raise
3243 raise <instance>
3244 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003245
Collin Winter828f04a2007-08-31 00:04:24 +00003246 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003247 type = exc;
3248 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003249 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003250 goto raise_error;
3251 }
Collin Winter828f04a2007-08-31 00:04:24 +00003252 else if (PyExceptionInstance_Check(exc)) {
3253 value = exc;
3254 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003255 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003256 }
3257 else {
3258 /* Not something you can raise. You get an exception
3259 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003260 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003261 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003262 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003263 goto raise_error;
3264 }
Collin Winter828f04a2007-08-31 00:04:24 +00003265
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003266 if (cause) {
3267 PyObject *fixed_cause;
3268 if (PyExceptionClass_Check(cause)) {
3269 fixed_cause = PyObject_CallObject(cause, NULL);
3270 if (fixed_cause == NULL)
3271 goto raise_error;
3272 Py_DECREF(cause);
3273 }
3274 else if (PyExceptionInstance_Check(cause)) {
3275 fixed_cause = cause;
3276 }
3277 else {
3278 PyErr_SetString(PyExc_TypeError,
3279 "exception causes must derive from "
3280 "BaseException");
3281 goto raise_error;
3282 }
3283 PyException_SetCause(value, fixed_cause);
3284 }
Collin Winter828f04a2007-08-31 00:04:24 +00003285
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003286 PyErr_SetObject(type, value);
3287 /* PyErr_SetObject incref's its arguments */
3288 Py_XDECREF(value);
3289 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003290 return WHY_EXCEPTION;
3291
3292raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003293 Py_XDECREF(value);
3294 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003295 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003296 return WHY_EXCEPTION;
3297}
3298
Tim Petersd6d010b2001-06-21 02:49:55 +00003299/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003300 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003301
Guido van Rossum0368b722007-05-11 16:50:42 +00003302 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3303 with a variable target.
3304*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003305
Barry Warsawe42b18f1997-08-25 22:13:04 +00003306static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003307unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003308{
Guido van Rossum0368b722007-05-11 16:50:42 +00003309 int i = 0, j = 0;
3310 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003311 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003312 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003313 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003314
Tim Petersd6d010b2001-06-21 02:49:55 +00003315 assert(v != NULL);
3316
3317 it = PyObject_GetIter(v);
3318 if (it == NULL)
3319 goto Error;
3320
3321 for (; i < argcnt; i++) {
3322 w = PyIter_Next(it);
3323 if (w == NULL) {
3324 /* Iterator done, via error or exhaustion. */
3325 if (!PyErr_Occurred()) {
3326 PyErr_Format(PyExc_ValueError,
3327 "need more than %d value%s to unpack",
3328 i, i == 1 ? "" : "s");
3329 }
3330 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003331 }
3332 *--sp = w;
3333 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003334
Guido van Rossum0368b722007-05-11 16:50:42 +00003335 if (argcntafter == -1) {
3336 /* We better have exhausted the iterator now. */
3337 w = PyIter_Next(it);
3338 if (w == NULL) {
3339 if (PyErr_Occurred())
3340 goto Error;
3341 Py_DECREF(it);
3342 return 1;
3343 }
3344 Py_DECREF(w);
3345 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3346 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003347 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003348
3349 l = PySequence_List(it);
3350 if (l == NULL)
3351 goto Error;
3352 *--sp = l;
3353 i++;
3354
3355 ll = PyList_GET_SIZE(l);
3356 if (ll < argcntafter) {
3357 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3358 argcnt + ll);
3359 goto Error;
3360 }
3361
3362 /* Pop the "after-variable" args off the list. */
3363 for (j = argcntafter; j > 0; j--, i++) {
3364 *--sp = PyList_GET_ITEM(l, ll - j);
3365 }
3366 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003367 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003368 Py_DECREF(it);
3369 return 1;
3370
Tim Petersd6d010b2001-06-21 02:49:55 +00003371Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003372 for (; i > 0; i--, sp++)
3373 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003374 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003375 return 0;
3376}
3377
3378
Guido van Rossum96a42c81992-01-12 02:29:51 +00003379#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003380static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003381prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003382{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003383 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003384 if (PyObject_Print(v, stdout, 0) != 0)
3385 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003386 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003387 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003388}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003389#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003390
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003391static void
Fred Drake5755ce62001-06-27 19:19:46 +00003392call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003393{
Guido van Rossumb209a111997-04-29 18:18:01 +00003394 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003395 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003396 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003397 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003398 value = Py_None;
3399 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003400 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003401 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003402 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003403 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003404 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003405 }
Fred Drake5755ce62001-06-27 19:19:46 +00003406 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003407 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003408 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003409 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003410 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003411 Py_XDECREF(type);
3412 Py_XDECREF(value);
3413 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003414 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003415}
3416
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003417static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003418call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003419 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003420{
3421 PyObject *type, *value, *traceback;
3422 int err;
3423 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003424 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003425 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003426 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003427 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003428 return 0;
3429 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003430 else {
3431 Py_XDECREF(type);
3432 Py_XDECREF(value);
3433 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003434 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003435 }
3436}
3437
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003438static int
Fred Drake5755ce62001-06-27 19:19:46 +00003439call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3440 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003441{
Fred Drake5755ce62001-06-27 19:19:46 +00003442 register PyThreadState *tstate = frame->f_tstate;
3443 int result;
3444 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003445 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003446 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003447 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003448 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003449 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3450 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003451 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003452 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003453}
3454
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003455PyObject *
3456_PyEval_CallTracing(PyObject *func, PyObject *args)
3457{
3458 PyFrameObject *frame = PyEval_GetFrame();
3459 PyThreadState *tstate = frame->f_tstate;
3460 int save_tracing = tstate->tracing;
3461 int save_use_tracing = tstate->use_tracing;
3462 PyObject *result;
3463
3464 tstate->tracing = 0;
3465 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3466 || (tstate->c_profilefunc != NULL));
3467 result = PyObject_Call(func, args, NULL);
3468 tstate->tracing = save_tracing;
3469 tstate->use_tracing = save_use_tracing;
3470 return result;
3471}
3472
Michael W. Hudson006c7522002-11-08 13:08:46 +00003473static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003474maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003475 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3476 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003477{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003478 int result = 0;
3479
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003480 /* If the last instruction executed isn't in the current
3481 instruction window, reset the window. If the last
3482 instruction happens to fall at the start of a line or if it
3483 represents a jump backwards, call the trace function.
3484 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003485 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003486 int line;
3487 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003488
Thomas Woutersce272b62007-09-19 21:19:28 +00003489 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3490 &bounds);
3491 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003492 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003493 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003494 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003495 }
3496 *instr_lb = bounds.ap_lower;
3497 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003498 }
Armin Rigobf57a142004-03-22 19:24:58 +00003499 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003500 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003501 }
3502 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003503 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003504}
3505
Fred Drake5755ce62001-06-27 19:19:46 +00003506void
3507PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003508{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003509 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003510 PyObject *temp = tstate->c_profileobj;
3511 Py_XINCREF(arg);
3512 tstate->c_profilefunc = NULL;
3513 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003514 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003515 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003516 Py_XDECREF(temp);
3517 tstate->c_profilefunc = func;
3518 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003519 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003520 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003521}
3522
3523void
3524PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3525{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003526 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003527 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003528 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003529 Py_XINCREF(arg);
3530 tstate->c_tracefunc = NULL;
3531 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003532 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003533 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003534 Py_XDECREF(temp);
3535 tstate->c_tracefunc = func;
3536 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003537 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003538 tstate->use_tracing = ((func != NULL)
3539 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003540}
3541
Guido van Rossumb209a111997-04-29 18:18:01 +00003542PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003543PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003544{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003545 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003546 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003547 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003548 else
3549 return current_frame->f_builtins;
3550}
3551
Guido van Rossumb209a111997-04-29 18:18:01 +00003552PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003553PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003554{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003555 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003556 if (current_frame == NULL)
3557 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003558 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003559 return current_frame->f_locals;
3560}
3561
Guido van Rossumb209a111997-04-29 18:18:01 +00003562PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003563PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003564{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003565 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003566 if (current_frame == NULL)
3567 return NULL;
3568 else
3569 return current_frame->f_globals;
3570}
3571
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003572PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003573PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003574{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003575 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003576 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003577}
3578
Guido van Rossum6135a871995-01-09 17:53:26 +00003579int
Tim Peters5ba58662001-07-16 02:29:45 +00003580PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003581{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003582 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003583 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003584
3585 if (current_frame != NULL) {
3586 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003587 const int compilerflags = codeflags & PyCF_MASK;
3588 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003589 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003590 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003591 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003592#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003593 if (codeflags & CO_GENERATOR_ALLOWED) {
3594 result = 1;
3595 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3596 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003597#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003598 }
3599 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003600}
3601
Guido van Rossum3f5da241990-12-20 15:06:42 +00003602
Guido van Rossum681d79a1995-07-18 14:51:37 +00003603/* External interface to call any callable object.
3604 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003605
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003606#undef PyEval_CallObject
3607/* for backward compatibility: export this interface */
3608
Guido van Rossumb209a111997-04-29 18:18:01 +00003609PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003610PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003611{
Guido van Rossumb209a111997-04-29 18:18:01 +00003612 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003613}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003614#define PyEval_CallObject(func,arg) \
3615 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003616
Guido van Rossumb209a111997-04-29 18:18:01 +00003617PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003618PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003619{
Jeremy Hylton52820442001-01-03 23:52:36 +00003620 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003621
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003622 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003623 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003624 if (arg == NULL)
3625 return NULL;
3626 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003627 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003628 PyErr_SetString(PyExc_TypeError,
3629 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003630 return NULL;
3631 }
3632 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003633 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003634
Guido van Rossumb209a111997-04-29 18:18:01 +00003635 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003636 PyErr_SetString(PyExc_TypeError,
3637 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003638 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003639 return NULL;
3640 }
3641
Tim Peters6d6c1a32001-08-02 04:15:00 +00003642 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003643 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003644 return result;
3645}
3646
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003647const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003648PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003649{
3650 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003651 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003652 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003653 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003654 else if (PyCFunction_Check(func))
3655 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003656 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003657 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003658}
3659
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003660const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003661PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003662{
3663 if (PyMethod_Check(func))
3664 return "()";
3665 else if (PyFunction_Check(func))
3666 return "()";
3667 else if (PyCFunction_Check(func))
3668 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003669 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003670 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003671}
3672
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003673static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003674err_args(PyObject *func, int flags, int nargs)
3675{
3676 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003677 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003678 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003679 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003680 nargs);
3681 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003682 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003683 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003684 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003685 nargs);
3686}
3687
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003688#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003689if (tstate->use_tracing && tstate->c_profilefunc) { \
3690 if (call_trace(tstate->c_profilefunc, \
3691 tstate->c_profileobj, \
3692 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003693 func)) { \
3694 x = NULL; \
3695 } \
3696 else { \
3697 x = call; \
3698 if (tstate->c_profilefunc != NULL) { \
3699 if (x == NULL) { \
3700 call_trace_protected(tstate->c_profilefunc, \
3701 tstate->c_profileobj, \
3702 tstate->frame, PyTrace_C_EXCEPTION, \
3703 func); \
3704 /* XXX should pass (type, value, tb) */ \
3705 } else { \
3706 if (call_trace(tstate->c_profilefunc, \
3707 tstate->c_profileobj, \
3708 tstate->frame, PyTrace_C_RETURN, \
3709 func)) { \
3710 Py_DECREF(x); \
3711 x = NULL; \
3712 } \
3713 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003714 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003715 } \
3716} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003717 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003718 }
3719
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003720static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003721call_function(PyObject ***pp_stack, int oparg
3722#ifdef WITH_TSC
3723 , uint64* pintr0, uint64* pintr1
3724#endif
3725 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003726{
3727 int na = oparg & 0xff;
3728 int nk = (oparg>>8) & 0xff;
3729 int n = na + 2 * nk;
3730 PyObject **pfunc = (*pp_stack) - n - 1;
3731 PyObject *func = *pfunc;
3732 PyObject *x, *w;
3733
Jeremy Hylton985eba52003-02-05 23:13:00 +00003734 /* Always dispatch PyCFunction first, because these are
3735 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003736 */
3737 if (PyCFunction_Check(func) && nk == 0) {
3738 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003739 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003740
3741 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003742 if (flags & (METH_NOARGS | METH_O)) {
3743 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3744 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003745 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003746 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003747 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003748 else if (flags & METH_O && na == 1) {
3749 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003750 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003751 Py_DECREF(arg);
3752 }
3753 else {
3754 err_args(func, flags, na);
3755 x = NULL;
3756 }
3757 }
3758 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003759 PyObject *callargs;
3760 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003761 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003762 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003763 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003764 Py_XDECREF(callargs);
3765 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003766 } else {
3767 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3768 /* optimize access to bound methods */
3769 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003770 PCALL(PCALL_METHOD);
3771 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003772 Py_INCREF(self);
3773 func = PyMethod_GET_FUNCTION(func);
3774 Py_INCREF(func);
3775 Py_DECREF(*pfunc);
3776 *pfunc = self;
3777 na++;
3778 n++;
3779 } else
3780 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003781 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003782 if (PyFunction_Check(func))
3783 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003784 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003785 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003786 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003787 Py_DECREF(func);
3788 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003789
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003790 /* Clear the stack of the function object. Also removes
3791 the arguments in case they weren't consumed already
3792 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003793 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003794 while ((*pp_stack) > pfunc) {
3795 w = EXT_POP(*pp_stack);
3796 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003797 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003798 }
3799 return x;
3800}
3801
Jeremy Hylton192690e2002-08-16 18:36:11 +00003802/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003803 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003804 For the simplest case -- a function that takes only positional
3805 arguments and is called with only positional arguments -- it
3806 inlines the most primitive frame setup code from
3807 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3808 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003809*/
3810
3811static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003812fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003813{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003814 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003815 PyObject *globals = PyFunction_GET_GLOBALS(func);
3816 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003817 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003818 PyObject **d = NULL;
3819 int nd = 0;
3820
Jeremy Hylton985eba52003-02-05 23:13:00 +00003821 PCALL(PCALL_FUNCTION);
3822 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003823 if (argdefs == NULL && co->co_argcount == n &&
3824 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003825 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3826 PyFrameObject *f;
3827 PyObject *retval = NULL;
3828 PyThreadState *tstate = PyThreadState_GET();
3829 PyObject **fastlocals, **stack;
3830 int i;
3831
3832 PCALL(PCALL_FASTER_FUNCTION);
3833 assert(globals != NULL);
3834 /* XXX Perhaps we should create a specialized
3835 PyFrame_New() that doesn't take locals, but does
3836 take builtins without sanity checking them.
3837 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003838 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003839 f = PyFrame_New(tstate, co, globals, NULL);
3840 if (f == NULL)
3841 return NULL;
3842
3843 fastlocals = f->f_localsplus;
3844 stack = (*pp_stack) - n;
3845
3846 for (i = 0; i < n; i++) {
3847 Py_INCREF(*stack);
3848 fastlocals[i] = *stack++;
3849 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003850 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003851 ++tstate->recursion_depth;
3852 Py_DECREF(f);
3853 --tstate->recursion_depth;
3854 return retval;
3855 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003856 if (argdefs != NULL) {
3857 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003858 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003859 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003860 return PyEval_EvalCodeEx(co, globals,
3861 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003862 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003863 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003864}
3865
3866static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003867update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3868 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003869{
3870 PyObject *kwdict = NULL;
3871 if (orig_kwdict == NULL)
3872 kwdict = PyDict_New();
3873 else {
3874 kwdict = PyDict_Copy(orig_kwdict);
3875 Py_DECREF(orig_kwdict);
3876 }
3877 if (kwdict == NULL)
3878 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003879 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003880 int err;
3881 PyObject *value = EXT_POP(*pp_stack);
3882 PyObject *key = EXT_POP(*pp_stack);
3883 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003884 PyErr_Format(PyExc_TypeError,
3885 "%.200s%s got multiple values "
3886 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003887 PyEval_GetFuncName(func),
3888 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003889 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003890 Py_DECREF(key);
3891 Py_DECREF(value);
3892 Py_DECREF(kwdict);
3893 return NULL;
3894 }
3895 err = PyDict_SetItem(kwdict, key, value);
3896 Py_DECREF(key);
3897 Py_DECREF(value);
3898 if (err) {
3899 Py_DECREF(kwdict);
3900 return NULL;
3901 }
3902 }
3903 return kwdict;
3904}
3905
3906static PyObject *
3907update_star_args(int nstack, int nstar, PyObject *stararg,
3908 PyObject ***pp_stack)
3909{
3910 PyObject *callargs, *w;
3911
3912 callargs = PyTuple_New(nstack + nstar);
3913 if (callargs == NULL) {
3914 return NULL;
3915 }
3916 if (nstar) {
3917 int i;
3918 for (i = 0; i < nstar; i++) {
3919 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3920 Py_INCREF(a);
3921 PyTuple_SET_ITEM(callargs, nstack + i, a);
3922 }
3923 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003924 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003925 w = EXT_POP(*pp_stack);
3926 PyTuple_SET_ITEM(callargs, nstack, w);
3927 }
3928 return callargs;
3929}
3930
3931static PyObject *
3932load_args(PyObject ***pp_stack, int na)
3933{
3934 PyObject *args = PyTuple_New(na);
3935 PyObject *w;
3936
3937 if (args == NULL)
3938 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003939 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003940 w = EXT_POP(*pp_stack);
3941 PyTuple_SET_ITEM(args, na, w);
3942 }
3943 return args;
3944}
3945
3946static PyObject *
3947do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3948{
3949 PyObject *callargs = NULL;
3950 PyObject *kwdict = NULL;
3951 PyObject *result = NULL;
3952
3953 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003954 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003955 if (kwdict == NULL)
3956 goto call_fail;
3957 }
3958 callargs = load_args(pp_stack, na);
3959 if (callargs == NULL)
3960 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003961#ifdef CALL_PROFILE
3962 /* At this point, we have to look at the type of func to
3963 update the call stats properly. Do it here so as to avoid
3964 exposing the call stats machinery outside ceval.c
3965 */
3966 if (PyFunction_Check(func))
3967 PCALL(PCALL_FUNCTION);
3968 else if (PyMethod_Check(func))
3969 PCALL(PCALL_METHOD);
3970 else if (PyType_Check(func))
3971 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00003972 else if (PyCFunction_Check(func))
3973 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003974 else
3975 PCALL(PCALL_OTHER);
3976#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00003977 if (PyCFunction_Check(func)) {
3978 PyThreadState *tstate = PyThreadState_GET();
3979 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
3980 }
3981 else
3982 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003983call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003984 Py_XDECREF(callargs);
3985 Py_XDECREF(kwdict);
3986 return result;
3987}
3988
3989static PyObject *
3990ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3991{
3992 int nstar = 0;
3993 PyObject *callargs = NULL;
3994 PyObject *stararg = NULL;
3995 PyObject *kwdict = NULL;
3996 PyObject *result = NULL;
3997
3998 if (flags & CALL_FLAG_KW) {
3999 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004000 if (!PyDict_Check(kwdict)) {
4001 PyObject *d;
4002 d = PyDict_New();
4003 if (d == NULL)
4004 goto ext_call_fail;
4005 if (PyDict_Update(d, kwdict) != 0) {
4006 Py_DECREF(d);
4007 /* PyDict_Update raises attribute
4008 * error (percolated from an attempt
4009 * to get 'keys' attribute) instead of
4010 * a type error if its second argument
4011 * is not a mapping.
4012 */
4013 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4014 PyErr_Format(PyExc_TypeError,
4015 "%.200s%.200s argument after ** "
4016 "must be a mapping, not %.200s",
4017 PyEval_GetFuncName(func),
4018 PyEval_GetFuncDesc(func),
4019 kwdict->ob_type->tp_name);
4020 }
4021 goto ext_call_fail;
4022 }
4023 Py_DECREF(kwdict);
4024 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004025 }
4026 }
4027 if (flags & CALL_FLAG_VAR) {
4028 stararg = EXT_POP(*pp_stack);
4029 if (!PyTuple_Check(stararg)) {
4030 PyObject *t = NULL;
4031 t = PySequence_Tuple(stararg);
4032 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004033 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4034 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004035 "%.200s%.200s argument after * "
4036 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004037 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004038 PyEval_GetFuncDesc(func),
4039 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004040 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004041 goto ext_call_fail;
4042 }
4043 Py_DECREF(stararg);
4044 stararg = t;
4045 }
4046 nstar = PyTuple_GET_SIZE(stararg);
4047 }
4048 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004049 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004050 if (kwdict == NULL)
4051 goto ext_call_fail;
4052 }
4053 callargs = update_star_args(na, nstar, stararg, pp_stack);
4054 if (callargs == NULL)
4055 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004056#ifdef CALL_PROFILE
4057 /* At this point, we have to look at the type of func to
4058 update the call stats properly. Do it here so as to avoid
4059 exposing the call stats machinery outside ceval.c
4060 */
4061 if (PyFunction_Check(func))
4062 PCALL(PCALL_FUNCTION);
4063 else if (PyMethod_Check(func))
4064 PCALL(PCALL_METHOD);
4065 else if (PyType_Check(func))
4066 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004067 else if (PyCFunction_Check(func))
4068 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004069 else
4070 PCALL(PCALL_OTHER);
4071#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004072 if (PyCFunction_Check(func)) {
4073 PyThreadState *tstate = PyThreadState_GET();
4074 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4075 }
4076 else
4077 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004078ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004079 Py_XDECREF(callargs);
4080 Py_XDECREF(kwdict);
4081 Py_XDECREF(stararg);
4082 return result;
4083}
4084
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004085/* Extract a slice index from a PyInt or PyLong or an object with the
4086 nb_index slot defined, and store in *pi.
4087 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4088 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 +00004089 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004090*/
Tim Petersb5196382001-12-16 19:44:20 +00004091/* Note: If v is NULL, return success without storing into *pi. This
4092 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4093 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004094*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004095int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004096_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004097{
Tim Petersb5196382001-12-16 19:44:20 +00004098 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004099 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004100 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004101 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004102 if (x == -1 && PyErr_Occurred())
4103 return 0;
4104 }
4105 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004106 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004107 "slice indices must be integers or "
4108 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004109 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004110 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004111 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004112 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004113 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004114}
4115
Guido van Rossum486364b2007-06-30 05:01:58 +00004116#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004117 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004118
Guido van Rossumb209a111997-04-29 18:18:01 +00004119static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004120cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004121{
Guido van Rossumac7be682001-01-17 15:42:30 +00004122 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004123 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004124 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004125 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004126 break;
4127 case PyCmp_IS_NOT:
4128 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004129 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004130 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004131 res = PySequence_Contains(w, v);
4132 if (res < 0)
4133 return NULL;
4134 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004135 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004136 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004137 if (res < 0)
4138 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004139 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004140 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004141 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004142 if (PyTuple_Check(w)) {
4143 Py_ssize_t i, length;
4144 length = PyTuple_Size(w);
4145 for (i = 0; i < length; i += 1) {
4146 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004147 if (!PyExceptionClass_Check(exc)) {
4148 PyErr_SetString(PyExc_TypeError,
4149 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004150 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004151 }
4152 }
4153 }
4154 else {
Brett Cannon39590462007-02-26 22:01:14 +00004155 if (!PyExceptionClass_Check(w)) {
4156 PyErr_SetString(PyExc_TypeError,
4157 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004158 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004159 }
4160 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004161 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004162 break;
4163 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004164 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004165 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004166 v = res ? Py_True : Py_False;
4167 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004168 return v;
4169}
4170
Thomas Wouters52152252000-08-17 22:55:00 +00004171static PyObject *
4172import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004173{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004174 PyObject *x;
4175
4176 x = PyObject_GetAttr(v, name);
4177 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004178 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004179 }
Thomas Wouters52152252000-08-17 22:55:00 +00004180 return x;
4181}
Guido van Rossumac7be682001-01-17 15:42:30 +00004182
Thomas Wouters52152252000-08-17 22:55:00 +00004183static int
4184import_all_from(PyObject *locals, PyObject *v)
4185{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004186 PyObject *all = PyObject_GetAttrString(v, "__all__");
4187 PyObject *dict, *name, *value;
4188 int skip_leading_underscores = 0;
4189 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004190
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004191 if (all == NULL) {
4192 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4193 return -1; /* Unexpected error */
4194 PyErr_Clear();
4195 dict = PyObject_GetAttrString(v, "__dict__");
4196 if (dict == NULL) {
4197 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4198 return -1;
4199 PyErr_SetString(PyExc_ImportError,
4200 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004201 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004202 }
4203 all = PyMapping_Keys(dict);
4204 Py_DECREF(dict);
4205 if (all == NULL)
4206 return -1;
4207 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004208 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004209
4210 for (pos = 0, err = 0; ; pos++) {
4211 name = PySequence_GetItem(all, pos);
4212 if (name == NULL) {
4213 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4214 err = -1;
4215 else
4216 PyErr_Clear();
4217 break;
4218 }
4219 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004220 PyUnicode_Check(name) &&
4221 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004222 {
4223 Py_DECREF(name);
4224 continue;
4225 }
4226 value = PyObject_GetAttr(v, name);
4227 if (value == NULL)
4228 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004229 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004230 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004231 else
4232 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004233 Py_DECREF(name);
4234 Py_XDECREF(value);
4235 if (err != 0)
4236 break;
4237 }
4238 Py_DECREF(all);
4239 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004240}
4241
Guido van Rossumac7be682001-01-17 15:42:30 +00004242static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004243format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004244{
Neal Norwitzda059e32007-08-26 05:33:45 +00004245 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004246
4247 if (!obj)
4248 return;
4249
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004250 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004251 if (!obj_str)
4252 return;
4253
4254 PyErr_Format(exc, format_str, obj_str);
4255}
Guido van Rossum950361c1997-01-24 13:49:28 +00004256
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004257static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004258unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004259 PyFrameObject *f, unsigned char *next_instr)
4260{
4261 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004262 are (Unicode) strings. */
4263 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4264 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004265 Py_ssize_t new_len = v_len + w_len;
4266 if (new_len < 0) {
4267 PyErr_SetString(PyExc_OverflowError,
4268 "strings are too large to concat");
4269 return NULL;
4270 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004271
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004272 if (v->ob_refcnt == 2) {
4273 /* In the common case, there are 2 references to the value
4274 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004275 * value stack (in 'v') and one still stored in the
4276 * 'variable'. We try to delete the variable now to reduce
4277 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004278 */
4279 switch (*next_instr) {
4280 case STORE_FAST:
4281 {
4282 int oparg = PEEKARG();
4283 PyObject **fastlocals = f->f_localsplus;
4284 if (GETLOCAL(oparg) == v)
4285 SETLOCAL(oparg, NULL);
4286 break;
4287 }
4288 case STORE_DEREF:
4289 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004290 PyObject **freevars = (f->f_localsplus +
4291 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004292 PyObject *c = freevars[PEEKARG()];
4293 if (PyCell_GET(c) == v)
4294 PyCell_Set(c, NULL);
4295 break;
4296 }
4297 case STORE_NAME:
4298 {
4299 PyObject *names = f->f_code->co_names;
4300 PyObject *name = GETITEM(names, PEEKARG());
4301 PyObject *locals = f->f_locals;
4302 if (PyDict_CheckExact(locals) &&
4303 PyDict_GetItem(locals, name) == v) {
4304 if (PyDict_DelItem(locals, name) != 0) {
4305 PyErr_Clear();
4306 }
4307 }
4308 break;
4309 }
4310 }
4311 }
4312
Guido van Rossum98297ee2007-11-06 21:34:58 +00004313 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004314 /* Now we own the last reference to 'v', so we can resize it
4315 * in-place.
4316 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004317 if (PyUnicode_Resize(&v, new_len) != 0) {
4318 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004319 * deallocated so it cannot be put back into
4320 * 'variable'. The MemoryError is raised when there
4321 * is no value in 'variable', which might (very
4322 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004323 */
4324 return NULL;
4325 }
4326 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004327 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4328 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004329 return v;
4330 }
4331 else {
4332 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004333 w = PyUnicode_Concat(v, w);
4334 Py_DECREF(v);
4335 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004336 }
4337}
4338
Guido van Rossum950361c1997-01-24 13:49:28 +00004339#ifdef DYNAMIC_EXECUTION_PROFILE
4340
Skip Montanarof118cb12001-10-15 20:51:38 +00004341static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004342getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004343{
4344 int i;
4345 PyObject *l = PyList_New(256);
4346 if (l == NULL) return NULL;
4347 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004348 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004349 if (x == NULL) {
4350 Py_DECREF(l);
4351 return NULL;
4352 }
4353 PyList_SetItem(l, i, x);
4354 }
4355 for (i = 0; i < 256; i++)
4356 a[i] = 0;
4357 return l;
4358}
4359
4360PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004361_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004362{
4363#ifndef DXPAIRS
4364 return getarray(dxp);
4365#else
4366 int i;
4367 PyObject *l = PyList_New(257);
4368 if (l == NULL) return NULL;
4369 for (i = 0; i < 257; i++) {
4370 PyObject *x = getarray(dxpairs[i]);
4371 if (x == NULL) {
4372 Py_DECREF(l);
4373 return NULL;
4374 }
4375 PyList_SetItem(l, i, x);
4376 }
4377 return l;
4378#endif
4379}
4380
4381#endif