blob: b5b5c272721a9c446bebeff5ffd18f9315ca8d41 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouters8ce81f72007-09-20 18:22:40 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Michael W. Hudson75eabd22005-01-18 15:56:11 +000054#else /* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000055
Michael W. Hudson75eabd22005-01-18 15:56:11 +000056#define READ_TIMESTAMP(val) \
57 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
59#endif
60
Thomas Wouters477c8d52006-05-27 19:21:47 +000061void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000062 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
63{
64 uint64 intr, inst, loop;
65 PyThreadState *tstate = PyThreadState_Get();
66 if (!tstate->interp->tscdump)
67 return;
68 intr = intr1 - intr0;
69 inst = inst1 - inst0 - intr;
70 loop = loop1 - loop0 - intr;
71 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
72 opcode, ticked, inst, loop);
73}
Michael W. Hudson800ba232004-08-12 18:19:17 +000074
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000075#endif
76
Guido van Rossum04691fc1992-08-12 15:35:34 +000077/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000079
Guido van Rossum408027e1996-12-30 16:17:54 +000080#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000081/* For debugging the interpreter: */
82#define LLTRACE 1 /* Low-level trace feature */
83#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000084#endif
85
Jeremy Hylton52820442001-01-03 23:52:36 +000086typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000087
Guido van Rossum374a9221991-04-04 10:40:29 +000088/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000089#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +000090static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091#else
Thomas Wouters477c8d52006-05-27 19:21:47 +000092static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000094static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
95static PyObject * do_call(PyObject *, PyObject ***, int, int);
96static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +000097static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
98 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000099static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
100static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000101#define CALL_FLAG_VAR 1
102#define CALL_FLAG_KW 2
103
Guido van Rossum0a066c01992-03-27 17:29:15 +0000104#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000105static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000106static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000107#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000108static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
109 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000110static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000111 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000112static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000113static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000114 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000115
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * cmp_outcome(int, PyObject *, PyObject *);
117static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000118static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000119static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000120static PyObject * unicode_concatenate(PyObject *, PyObject *,
121 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000122
Paul Prescode68140d2000-08-30 20:25:01 +0000123#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000124 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000125#define GLOBAL_NAME_ERROR_MSG \
126 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000127#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000128 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000129#define UNBOUNDFREE_ERROR_MSG \
130 "free variable '%.200s' referenced before assignment" \
131 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000132
Guido van Rossum950361c1997-01-24 13:49:28 +0000133/* Dynamic execution profile */
134#ifdef DYNAMIC_EXECUTION_PROFILE
135#ifdef DXPAIRS
136static long dxpairs[257][256];
137#define dxp dxpairs[256]
138#else
139static long dxp[256];
140#endif
141#endif
142
Jeremy Hylton985eba52003-02-05 23:13:00 +0000143/* Function call profile */
144#ifdef CALL_PROFILE
145#define PCALL_NUM 11
146static int pcall[PCALL_NUM];
147
148#define PCALL_ALL 0
149#define PCALL_FUNCTION 1
150#define PCALL_FAST_FUNCTION 2
151#define PCALL_FASTER_FUNCTION 3
152#define PCALL_METHOD 4
153#define PCALL_BOUND_METHOD 5
154#define PCALL_CFUNCTION 6
155#define PCALL_TYPE 7
156#define PCALL_GENERATOR 8
157#define PCALL_OTHER 9
158#define PCALL_POP 10
159
160/* Notes about the statistics
161
162 PCALL_FAST stats
163
164 FAST_FUNCTION means no argument tuple needs to be created.
165 FASTER_FUNCTION means that the fast-path frame setup code is used.
166
167 If there is a method call where the call can be optimized by changing
168 the argument tuple and calling the function directly, it gets recorded
169 twice.
170
171 As a result, the relationship among the statistics appears to be
172 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
173 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
174 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
175 PCALL_METHOD > PCALL_BOUND_METHOD
176*/
177
178#define PCALL(POS) pcall[POS]++
179
180PyObject *
181PyEval_GetCallStats(PyObject *self)
182{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000183 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000184 pcall[0], pcall[1], pcall[2], pcall[3],
185 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000186 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000187}
188#else
189#define PCALL(O)
190
191PyObject *
192PyEval_GetCallStats(PyObject *self)
193{
194 Py_INCREF(Py_None);
195 return Py_None;
196}
197#endif
198
Tim Peters5ca576e2001-06-18 22:08:13 +0000199
Guido van Rossume59214e1994-08-30 08:01:59 +0000200#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000201
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000202#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000203#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000204#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000205#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000206
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000207static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000208static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000209static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000210
Tim Peters7f468f22004-10-11 02:40:51 +0000211int
212PyEval_ThreadsInitialized(void)
213{
214 return interpreter_lock != 0;
215}
216
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000217void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000218PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000219{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000220 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000221 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000222 interpreter_lock = PyThread_allocate_lock();
223 PyThread_acquire_lock(interpreter_lock, 1);
224 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000225}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000226
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000227void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000228PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000230 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231}
232
233void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000234PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000235{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000236 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000237}
238
239void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000240PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000241{
242 if (tstate == NULL)
243 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000244 /* Check someone has called PyEval_InitThreads() to create the lock */
245 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000246 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000247 if (PyThreadState_Swap(tstate) != NULL)
248 Py_FatalError(
249 "PyEval_AcquireThread: non-NULL old thread state");
250}
251
252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000254{
255 if (tstate == NULL)
256 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
257 if (PyThreadState_Swap(NULL) != tstate)
258 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000259 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000260}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000261
262/* This function is called from PyOS_AfterFork to ensure that newly
263 created child processes don't hold locks referring to threads which
264 are not running in the child process. (This could also be done using
265 pthread_atfork mechanism, at least for the pthreads implementation.) */
266
267void
268PyEval_ReInitThreads(void)
269{
Jesse Nollera8513972008-07-17 16:49:17 +0000270 PyObject *threading, *result;
271 PyThreadState *tstate;
272
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000273 if (!interpreter_lock)
274 return;
275 /*XXX Can't use PyThread_free_lock here because it does too
276 much error-checking. Doing this cleanly would require
277 adding a new function to each thread_*.h. Instead, just
278 create a new lock and waste a little bit of memory */
279 interpreter_lock = PyThread_allocate_lock();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000280 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000281 PyThread_acquire_lock(interpreter_lock, 1);
282 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000283
284 /* Update the threading module with the new state.
285 */
286 tstate = PyThreadState_GET();
287 threading = PyMapping_GetItemString(tstate->interp->modules,
288 "threading");
289 if (threading == NULL) {
290 /* threading not imported */
291 PyErr_Clear();
292 return;
293 }
294 result = PyObject_CallMethod(threading, "_after_fork", NULL);
295 if (result == NULL)
296 PyErr_WriteUnraisable(threading);
297 else
298 Py_DECREF(result);
299 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000300}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301#endif
302
Guido van Rossumff4949e1992-08-05 19:58:53 +0000303/* Functions save_thread and restore_thread are always defined so
304 dynamically loaded modules needn't be compiled separately for use
305 with and without threads: */
306
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000307PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000308PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000310 PyThreadState *tstate = PyThreadState_Swap(NULL);
311 if (tstate == NULL)
312 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000313#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000314 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000315 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000316#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000317 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318}
319
320void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000321PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000322{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000323 if (tstate == NULL)
324 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000325#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000326 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000327 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000328 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000329 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330 }
331#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000332 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000333}
334
335
Guido van Rossuma9672091994-09-14 13:31:22 +0000336/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
337 signal handlers or Mac I/O completion routines) can schedule calls
338 to a function to be called synchronously.
339 The synchronous function is called with one void* argument.
340 It should return 0 for success or -1 for failure -- failure should
341 be accompanied by an exception.
342
343 If registry succeeds, the registry function returns 0; if it fails
344 (e.g. due to too many pending calls) it returns -1 (without setting
345 an exception condition).
346
347 Note that because registry may occur from within signal handlers,
348 or other asynchronous events, calling malloc() is unsafe!
349
350#ifdef WITH_THREAD
351 Any thread can schedule pending calls, but only the main thread
352 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000353 There is no facility to schedule calls to a particular thread, but
354 that should be easy to change, should that ever be required. In
355 that case, the static variables here should go into the python
356 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000357#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000358*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000359
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000360#ifdef WITH_THREAD
361
362/* The WITH_THREAD implementation is thread-safe. It allows
363 scheduling to be made from any thread, and even from an executing
364 callback.
365 */
366
367#define NPENDINGCALLS 32
368static struct {
369 int (*func)(void *);
370 void *arg;
371} pendingcalls[NPENDINGCALLS];
372static int pendingfirst = 0;
373static int pendinglast = 0;
374static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
375static char pendingbusy = 0;
376
377int
378Py_AddPendingCall(int (*func)(void *), void *arg)
379{
380 int i, j, result=0;
381 PyThread_type_lock lock = pending_lock;
382
383 /* try a few times for the lock. Since this mechanism is used
384 * for signal handling (on the main thread), there is a (slim)
385 * chance that a signal is delivered on the same thread while we
386 * hold the lock during the Py_MakePendingCalls() function.
387 * This avoids a deadlock in that case.
388 * Note that signals can be delivered on any thread. In particular,
389 * on Windows, a SIGINT is delivered on a system-created worker
390 * thread.
391 * We also check for lock being NULL, in the unlikely case that
392 * this function is called before any bytecode evaluation takes place.
393 */
394 if (lock != NULL) {
395 for (i = 0; i<100; i++) {
396 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
397 break;
398 }
399 if (i == 100)
400 return -1;
401 }
402
403 i = pendinglast;
404 j = (i + 1) % NPENDINGCALLS;
405 if (j == pendingfirst) {
406 result = -1; /* Queue full */
407 } else {
408 pendingcalls[i].func = func;
409 pendingcalls[i].arg = arg;
410 pendinglast = j;
411 }
412 /* signal main loop */
413 _Py_Ticker = 0;
414 pendingcalls_to_do = 1;
415 if (lock != NULL)
416 PyThread_release_lock(lock);
417 return result;
418}
419
420int
421Py_MakePendingCalls(void)
422{
423 int i;
424 int r = 0;
425
426 if (!pending_lock) {
427 /* initial allocation of the lock */
428 pending_lock = PyThread_allocate_lock();
429 if (pending_lock == NULL)
430 return -1;
431 }
432
433 /* only service pending calls on main thread */
434 if (main_thread && PyThread_get_thread_ident() != main_thread)
435 return 0;
436 /* don't perform recursive pending calls */
437 if (pendingbusy)
438 return 0;
439 pendingbusy = 1;
440 /* perform a bounded number of calls, in case of recursion */
441 for (i=0; i<NPENDINGCALLS; i++) {
442 int j;
443 int (*func)(void *);
Benjamin Petersonb4ddfa42009-01-17 23:46:54 +0000444 void *arg = NULL;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000445
446 /* pop one item off the queue while holding the lock */
447 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
448 j = pendingfirst;
449 if (j == pendinglast) {
450 func = NULL; /* Queue empty */
451 } else {
452 func = pendingcalls[j].func;
453 arg = pendingcalls[j].arg;
454 pendingfirst = (j + 1) % NPENDINGCALLS;
455 }
456 pendingcalls_to_do = pendingfirst != pendinglast;
457 PyThread_release_lock(pending_lock);
458 /* having released the lock, perform the callback */
459 if (func == NULL)
460 break;
461 r = func(arg);
462 if (r)
463 break;
464 }
465 pendingbusy = 0;
466 return r;
467}
468
469#else /* if ! defined WITH_THREAD */
470
471/*
472 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
473 This code is used for signal handling in python that isn't built
474 with WITH_THREAD.
475 Don't use this implementation when Py_AddPendingCalls() can happen
476 on a different thread!
477
Guido van Rossuma9672091994-09-14 13:31:22 +0000478 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479 (1) nested asynchronous calls to Py_AddPendingCall()
480 (2) AddPendingCall() calls made while pending calls are being processed.
481
482 (1) is very unlikely because typically signal delivery
483 is blocked during signal handling. So it should be impossible.
484 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000485 The current code is safe against (2), but not against (1).
486 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000487 thread is present, interrupted by signals, and that the critical
488 section is protected with the "busy" variable. On Windows, which
489 delivers SIGINT on a system thread, this does not hold and therefore
490 Windows really shouldn't use this version.
491 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000492*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000493
Guido van Rossuma9672091994-09-14 13:31:22 +0000494#define NPENDINGCALLS 32
495static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000496 int (*func)(void *);
497 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000498} pendingcalls[NPENDINGCALLS];
499static volatile int pendingfirst = 0;
500static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000502
503int
Thomas Wouters334fb892000-07-25 12:56:38 +0000504Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000505{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000506 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000507 int i, j;
508 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000509 if (busy)
510 return -1;
511 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000512 i = pendinglast;
513 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000514 if (j == pendingfirst) {
515 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000516 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000517 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000518 pendingcalls[i].func = func;
519 pendingcalls[i].arg = arg;
520 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000521
522 _Py_Ticker = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000523 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000524 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000525 /* XXX End critical section */
526 return 0;
527}
528
Guido van Rossum180d7b41994-09-29 09:45:57 +0000529int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000530Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000531{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000532 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000533 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000534 return 0;
535 busy = 1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000537 for (;;) {
538 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000539 int (*func)(void *);
540 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000541 i = pendingfirst;
542 if (i == pendinglast)
543 break; /* Queue empty */
544 func = pendingcalls[i].func;
545 arg = pendingcalls[i].arg;
546 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000547 if (func(arg) < 0) {
548 busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000550 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000551 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000552 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000553 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000554 return 0;
555}
556
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000557#endif /* WITH_THREAD */
558
Guido van Rossuma9672091994-09-14 13:31:22 +0000559
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000560/* The interpreter's recursion limit */
561
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000562#ifndef Py_DEFAULT_RECURSION_LIMIT
563#define Py_DEFAULT_RECURSION_LIMIT 1000
564#endif
565static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
566int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000567
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000568int
569Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000570{
571 return recursion_limit;
572}
573
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000574void
575Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000576{
577 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000578 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000579}
580
Armin Rigo2b3eb402003-10-28 12:05:48 +0000581/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
582 if the recursion_depth reaches _Py_CheckRecursionLimit.
583 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
584 to guarantee that _Py_CheckRecursiveCall() is regularly called.
585 Without USE_STACKCHECK, there is no need for this. */
586int
587_Py_CheckRecursiveCall(char *where)
588{
589 PyThreadState *tstate = PyThreadState_GET();
590
591#ifdef USE_STACKCHECK
592 if (PyOS_CheckStack()) {
593 --tstate->recursion_depth;
594 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
595 return -1;
596 }
597#endif
Antoine Pitrou658fad82008-09-03 18:34:34 +0000598 _Py_CheckRecursionLimit = recursion_limit;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000599 if (tstate->recursion_critical)
600 /* Somebody asked that we don't check for recursion. */
601 return 0;
602 if (tstate->overflowed) {
603 if (tstate->recursion_depth > recursion_limit + 50) {
604 /* Overflowing while handling an overflow. Give up. */
605 Py_FatalError("Cannot recover from stack overflow.");
606 }
607 return 0;
608 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000609 if (tstate->recursion_depth > recursion_limit) {
610 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000611 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000612 PyErr_Format(PyExc_RuntimeError,
613 "maximum recursion depth exceeded%s",
614 where);
615 return -1;
616 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000617 return 0;
618}
619
Guido van Rossum374a9221991-04-04 10:40:29 +0000620/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000621enum why_code {
622 WHY_NOT = 0x0001, /* No error */
623 WHY_EXCEPTION = 0x0002, /* Exception occurred */
624 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
625 WHY_RETURN = 0x0008, /* 'return' statement */
626 WHY_BREAK = 0x0010, /* 'break' statement */
627 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000628 WHY_YIELD = 0x0040, /* 'yield' operator */
629 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000630};
Guido van Rossum374a9221991-04-04 10:40:29 +0000631
Collin Winter828f04a2007-08-31 00:04:24 +0000632static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000633static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000634
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000635/* Records whether tracing is on for any thread. Counts the number of
636 threads for which tstate->c_tracefunc is non-NULL, so if the value
637 is 0, we know we don't have to check this thread's c_tracefunc.
638 This speeds up the if statement in PyEval_EvalFrameEx() after
639 fast_next_opcode*/
640static int _Py_TracingPossible = 0;
641
Skip Montanarod581d772002-09-03 20:10:45 +0000642/* for manipulating the thread switch and periodic "stuff" - used to be
643 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000644int _Py_CheckInterval = 100;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000645volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000646
Guido van Rossumb209a111997-04-29 18:18:01 +0000647PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000648PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000649{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000650 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000651 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000652 (PyObject **)NULL, 0,
653 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000654 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000655 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000656}
657
658
659/* Interpreter main loop */
660
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000661PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000662PyEval_EvalFrame(PyFrameObject *f) {
663 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000664 used this API; core interpreter code should call
665 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000666 return PyEval_EvalFrameEx(f, 0);
667}
668
669PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000671{
Guido van Rossum950361c1997-01-24 13:49:28 +0000672#ifdef DXPAIRS
673 int lastopcode = 0;
674#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000675 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000676 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000677 register int opcode; /* Current opcode */
678 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000679 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000680 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000681 register PyObject *x; /* Result object -- NULL if error */
682 register PyObject *v; /* Temporary objects popped off stack */
683 register PyObject *w;
684 register PyObject *u;
685 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000686 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000687 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000688 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000689 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000690
Tim Peters8a5c3c72004-04-05 19:36:21 +0000691 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000692
693 not (instr_lb <= current_bytecode_offset < instr_ub)
694
Tim Peters8a5c3c72004-04-05 19:36:21 +0000695 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000696 initial values are such as to make this false the first
697 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000698 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000699
Guido van Rossumd076c731998-10-07 19:42:25 +0000700 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000701 PyObject *names;
702 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000703#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000704 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000705 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000706#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000707
Antoine Pitroub52ec782009-01-25 16:34:23 +0000708/* Computed GOTOs, or
709 the-optimization-commonly-but-improperly-known-as-"threaded code"
710 using gcc's labels-as-values extension
711 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
712
713 The traditional bytecode evaluation loop uses a "switch" statement, which
714 decent compilers will optimize as a single indirect branch instruction
715 combined with a lookup table of jump addresses. However, since the
716 indirect jump instruction is shared by all opcodes, the CPU will have a
717 hard time making the right prediction for where to jump next (actually,
718 it will be always wrong except in the uncommon case of a sequence of
719 several identical opcodes).
720
721 "Threaded code" in contrast, uses an explicit jump table and an explicit
722 indirect jump instruction at the end of each opcode. Since the jump
723 instruction is at a different address for each opcode, the CPU will make a
724 separate prediction for each of these instructions, which is equivalent to
725 predicting the second opcode of each opcode pair. These predictions have
726 a much better chance to turn out valid, especially in small bytecode loops.
727
728 A mispredicted branch on a modern CPU flushes the whole pipeline and
729 can cost several CPU cycles (depending on the pipeline depth),
730 and potentially many more instructions (depending on the pipeline width).
731 A correctly predicted branch, however, is nearly free.
732
733 At the time of this writing, the "threaded code" version is up to 15-20%
734 faster than the normal "switch" version, depending on the compiler and the
735 CPU architecture.
736
737 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
738 because it would render the measurements invalid.
739
740
741 NOTE: care must be taken that the compiler doesn't try to "optimize" the
742 indirect jumps by sharing them between all opcodes. Such optimizations
743 can be disabled on gcc by using the -fno-gcse flag (or possibly
744 -fno-crossjumping).
745*/
746
747#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
748#undef USE_COMPUTED_GOTOS
749#endif
750
751#ifdef USE_COMPUTED_GOTOS
752/* Import the static jump table */
753#include "opcode_targets.h"
754
755/* This macro is used when several opcodes defer to the same implementation
756 (e.g. SETUP_LOOP, SETUP_FINALLY) */
757#define TARGET_WITH_IMPL(op, impl) \
758 TARGET_##op: \
759 opcode = op; \
760 if (HAS_ARG(op)) \
761 oparg = NEXTARG(); \
762 case op: \
763 goto impl; \
764
765#define TARGET(op) \
766 TARGET_##op: \
767 opcode = op; \
768 if (HAS_ARG(op)) \
769 oparg = NEXTARG(); \
770 case op:
771
772
773#define DISPATCH() \
774 { \
775 /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
776 int _tick = _Py_Ticker - 1; \
777 _Py_Ticker = _tick; \
778 if (_tick >= 0) { \
779 FAST_DISPATCH(); \
780 } \
781 continue; \
782 }
783
784#ifdef LLTRACE
785#define FAST_DISPATCH() \
786 { \
787 if (!lltrace && !_Py_TracingPossible) { \
788 f->f_lasti = INSTR_OFFSET(); \
789 goto *opcode_targets[*next_instr++]; \
790 } \
791 goto fast_next_opcode; \
792 }
793#else
794#define FAST_DISPATCH() \
795 { \
796 if (!_Py_TracingPossible) { \
797 f->f_lasti = INSTR_OFFSET(); \
798 goto *opcode_targets[*next_instr++]; \
799 } \
800 goto fast_next_opcode; \
801 }
802#endif
803
804#else
805#define TARGET(op) \
806 case op:
807#define TARGET_WITH_IMPL(op, impl) \
808 /* silence compiler warnings about `impl` unused */ \
809 if (0) goto impl; \
810 case op:
811#define DISPATCH() continue
812#define FAST_DISPATCH() goto fast_next_opcode
813#endif
814
815
Neal Norwitza81d2202002-07-14 00:27:26 +0000816/* Tuple access macros */
817
818#ifndef Py_DEBUG
819#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
820#else
821#define GETITEM(v, i) PyTuple_GetItem((v), (i))
822#endif
823
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000824#ifdef WITH_TSC
825/* Use Pentium timestamp counter to mark certain events:
826 inst0 -- beginning of switch statement for opcode dispatch
827 inst1 -- end of switch statement (may be skipped)
828 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000829 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000830 (may be skipped)
831 intr1 -- beginning of long interruption
832 intr2 -- end of long interruption
833
834 Many opcodes call out to helper C functions. In some cases, the
835 time in those functions should be counted towards the time for the
836 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
837 calls another Python function; there's no point in charge all the
838 bytecode executed by the called function to the caller.
839
840 It's hard to make a useful judgement statically. In the presence
841 of operator overloading, it's impossible to tell if a call will
842 execute new Python code or not.
843
844 It's a case-by-case judgement. I'll use intr1 for the following
845 cases:
846
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000847 IMPORT_STAR
848 IMPORT_FROM
849 CALL_FUNCTION (and friends)
850
851 */
852 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
853 int ticked = 0;
854
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000855 READ_TIMESTAMP(inst0);
856 READ_TIMESTAMP(inst1);
857 READ_TIMESTAMP(loop0);
858 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000859
860 /* shut up the compiler */
861 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000862#endif
863
Guido van Rossum374a9221991-04-04 10:40:29 +0000864/* Code access macros */
865
Martin v. Löwis18e16552006-02-15 17:27:45 +0000866#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000867#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000868#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000869#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000870#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000871#define JUMPBY(x) (next_instr += (x))
872
Raymond Hettingerf606f872003-03-16 03:11:04 +0000873/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000874 Some opcodes tend to come in pairs thus making it possible to
875 predict the second code when the first is run. For example,
876 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
877 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000878
Georg Brandl86b2fb92008-07-16 03:43:04 +0000879 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000880 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000881 processor's own internal branch predication has a high likelihood of
882 success, resulting in a nearly zero-overhead transition to the
883 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000884 including its two unpredictable branches, the HAS_ARG test and the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000885 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000886 a successful PREDICT has the effect of making the two opcodes run as if
Georg Brandl86b2fb92008-07-16 03:43:04 +0000887 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000888
Georg Brandl86b2fb92008-07-16 03:43:04 +0000889 If collecting opcode statistics, your choices are to either keep the
890 predictions turned-on and interpret the results as if some opcodes
891 had been combined or turn-off predictions so that the opcode frequency
892 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000893
894 Opcode prediction is disabled with threaded code, since the latter allows
895 the CPU to record separate branch prediction information for each
896 opcode.
897
Raymond Hettingerf606f872003-03-16 03:11:04 +0000898*/
899
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Raymond Hettingera7216982004-02-08 19:59:27 +0000901#define PREDICT(op) if (0) goto PRED_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000902#define PREDICTED(op) PRED_##op:
903#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000904#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000905#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000906#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000907#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +0000908#endif
909
Raymond Hettingerf606f872003-03-16 03:11:04 +0000910
Guido van Rossum374a9221991-04-04 10:40:29 +0000911/* Stack manipulation macros */
912
Martin v. Löwis18e16552006-02-15 17:27:45 +0000913/* The stack can grow at most MAXINT deep, as co_nlocals and
914 co_stacksize are ints. */
915#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000916#define EMPTY() (STACK_LEVEL() == 0)
917#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000918#define SECOND() (stack_pointer[-2])
919#define THIRD() (stack_pointer[-3])
920#define FOURTH() (stack_pointer[-4])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000921#define SET_TOP(v) (stack_pointer[-1] = (v))
922#define SET_SECOND(v) (stack_pointer[-2] = (v))
923#define SET_THIRD(v) (stack_pointer[-3] = (v))
924#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000925#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000926#define BASIC_PUSH(v) (*stack_pointer++ = (v))
927#define BASIC_POP() (*--stack_pointer)
928
Guido van Rossum96a42c81992-01-12 02:29:51 +0000929#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000930#define PUSH(v) { (void)(BASIC_PUSH(v), \
931 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000933#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
934 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000935#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
936 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000937 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000938#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
939 prtrace((STACK_POINTER)[-1], "ext_pop")), \
940 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000941#else
942#define PUSH(v) BASIC_PUSH(v)
943#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000944#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000945#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000946#endif
947
Guido van Rossum681d79a1995-07-18 14:51:37 +0000948/* Local variable macros */
949
950#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000951
952/* The SETLOCAL() macro must not DECREF the local variable in-place and
953 then store the new value; it must copy the old value to a temporary
954 value, then store the new value, and then DECREF the temporary value.
955 This is because it is possible that during the DECREF the frame is
956 accessed by other code (e.g. a __del__ method or gc.collect()) and the
957 variable would be pointing to already-freed memory. */
958#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
959 GETLOCAL(i) = value; \
960 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000961
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000962
963#define UNWIND_BLOCK(b) \
964 while (STACK_LEVEL() > (b)->b_level) { \
965 PyObject *v = POP(); \
966 Py_XDECREF(v); \
967 }
968
969#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000970 { \
971 PyObject *type, *value, *traceback; \
972 assert(STACK_LEVEL() >= (b)->b_level + 3); \
973 while (STACK_LEVEL() > (b)->b_level + 3) { \
974 value = POP(); \
975 Py_XDECREF(value); \
976 } \
977 type = tstate->exc_type; \
978 value = tstate->exc_value; \
979 traceback = tstate->exc_traceback; \
980 tstate->exc_type = POP(); \
981 tstate->exc_value = POP(); \
982 tstate->exc_traceback = POP(); \
983 Py_XDECREF(type); \
984 Py_XDECREF(value); \
985 Py_XDECREF(traceback); \
986 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000987
988#define SAVE_EXC_STATE() \
989 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000990 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000991 Py_XINCREF(tstate->exc_type); \
992 Py_XINCREF(tstate->exc_value); \
993 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000994 type = f->f_exc_type; \
995 value = f->f_exc_value; \
996 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000997 f->f_exc_type = tstate->exc_type; \
998 f->f_exc_value = tstate->exc_value; \
999 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001000 Py_XDECREF(type); \
1001 Py_XDECREF(value); \
1002 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001003 }
1004
1005#define SWAP_EXC_STATE() \
1006 { \
1007 PyObject *tmp; \
1008 tmp = tstate->exc_type; \
1009 tstate->exc_type = f->f_exc_type; \
1010 f->f_exc_type = tmp; \
1011 tmp = tstate->exc_value; \
1012 tstate->exc_value = f->f_exc_value; \
1013 f->f_exc_value = tmp; \
1014 tmp = tstate->exc_traceback; \
1015 tstate->exc_traceback = f->f_exc_traceback; \
1016 f->f_exc_traceback = tmp; \
1017 }
1018
Guido van Rossuma027efa1997-05-05 20:56:21 +00001019/* Start of code */
1020
Tim Peters5ca576e2001-06-18 22:08:13 +00001021 if (f == NULL)
1022 return NULL;
1023
Armin Rigo1d313ab2003-10-25 14:33:09 +00001024 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001025 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +00001026 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001027
Tim Peters5ca576e2001-06-18 22:08:13 +00001028 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001029
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001030 if (tstate->use_tracing) {
1031 if (tstate->c_tracefunc != NULL) {
1032 /* tstate->c_tracefunc, if defined, is a
1033 function that will be called on *every* entry
1034 to a code block. Its return value, if not
1035 None, is a function that will be called at
1036 the start of each executed line of code.
1037 (Actually, the function must return itself
1038 in order to continue tracing.) The trace
1039 functions are called with three arguments:
1040 a pointer to the current frame, a string
1041 indicating why the function is called, and
1042 an argument which depends on the situation.
1043 The global trace function is also called
1044 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001045 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001046 tstate->c_traceobj,
1047 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001048 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001049 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001050 }
1051 }
1052 if (tstate->c_profilefunc != NULL) {
1053 /* Similar for c_profilefunc, except it needn't
1054 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001055 if (call_trace_protected(tstate->c_profilefunc,
1056 tstate->c_profileobj,
1057 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001058 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001059 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001060 }
1061 }
1062 }
1063
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001064 co = f->f_code;
1065 names = co->co_names;
1066 consts = co->co_consts;
1067 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001068 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +00001069 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001070 /* An explanation is in order for the next line.
1071
1072 f->f_lasti now refers to the index of the last instruction
1073 executed. You might think this was obvious from the name, but
1074 this wasn't always true before 2.3! PyFrame_New now sets
1075 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1076 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001077 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001078
1079 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001080 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001081 prediction effectively links the two codes together as if they
1082 were a single new opcode; accordingly,f->f_lasti will point to
1083 the first code in the pair (for instance, GET_ITER followed by
1084 FOR_ITER is effectively a single opcode and f->f_lasti will point
1085 at to the beginning of the combined pair.)
1086 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001087 next_instr = first_instr + f->f_lasti + 1;
1088 stack_pointer = f->f_stacktop;
1089 assert(stack_pointer != NULL);
1090 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1091
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001092 if (f->f_code->co_flags & CO_GENERATOR) {
1093 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1094 /* We were in an except handler when we left,
1095 restore the exception state which was put aside
1096 (see YIELD_VALUE). */
1097 SWAP_EXC_STATE();
1098 }
1099 else {
1100 SAVE_EXC_STATE();
1101 }
1102 }
1103
Tim Peters5ca576e2001-06-18 22:08:13 +00001104#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001106#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001107#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001108 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001109#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001110
Guido van Rossum374a9221991-04-04 10:40:29 +00001111 why = WHY_NOT;
1112 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00001113 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +00001114 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001115
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001116 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001117 why = WHY_EXCEPTION;
1118 goto on_error;
1119 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001120
Guido van Rossum374a9221991-04-04 10:40:29 +00001121 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001122#ifdef WITH_TSC
1123 if (inst1 == 0) {
1124 /* Almost surely, the opcode executed a break
1125 or a continue, preventing inst1 from being set
1126 on the way out of the loop.
1127 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001128 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001129 loop1 = inst1;
1130 }
1131 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1132 intr0, intr1);
1133 ticked = 0;
1134 inst1 = 0;
1135 intr0 = 0;
1136 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001137 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001138#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001139 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001140 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001141
Guido van Rossuma027efa1997-05-05 20:56:21 +00001142 /* Do periodic things. Doing this every time through
1143 the loop would add too much overhead, so we do it
1144 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001145 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001146 event needs attention (e.g. a signal handler or
1147 async I/O handler); see Py_AddPendingCall() and
1148 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001149
Skip Montanarod581d772002-09-03 20:10:45 +00001150 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001151 if (*next_instr == SETUP_FINALLY) {
1152 /* Make the last opcode before
1153 a try: finally: block uninterruptable. */
1154 goto fast_next_opcode;
1155 }
Skip Montanarod581d772002-09-03 20:10:45 +00001156 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001157 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001158#ifdef WITH_TSC
1159 ticked = 1;
1160#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001161 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001162 if (Py_MakePendingCalls() < 0) {
1163 why = WHY_EXCEPTION;
1164 goto on_error;
1165 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001166 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +00001167 /* MakePendingCalls() didn't succeed.
1168 Force early re-execution of this
1169 "periodic" code, possibly after
1170 a thread switch */
1171 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +00001172 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001173#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001174 if (interpreter_lock) {
1175 /* Give another thread a chance */
1176
Guido van Rossum25ce5661997-08-02 03:10:38 +00001177 if (PyThreadState_Swap(NULL) != tstate)
1178 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +00001179 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001180
1181 /* Other threads may run now */
1182
Guido van Rossum65d5b571998-12-21 19:32:43 +00001183 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001184 if (PyThreadState_Swap(tstate) != NULL)
1185 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001186
1187 /* Check for thread interrupts */
1188
1189 if (tstate->async_exc != NULL) {
1190 x = tstate->async_exc;
1191 tstate->async_exc = NULL;
1192 PyErr_SetNone(x);
1193 Py_DECREF(x);
1194 why = WHY_EXCEPTION;
1195 goto on_error;
1196 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001197 }
1198#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001199 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001200
Neil Schemenauer63543862002-02-17 19:10:14 +00001201 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001202 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001204 /* line-by-line tracing support */
1205
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001206 if (_Py_TracingPossible &&
1207 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001208 /* see maybe_call_line_trace
1209 for expository comments */
1210 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001211
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001212 err = maybe_call_line_trace(tstate->c_tracefunc,
1213 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001214 f, &instr_lb, &instr_ub,
1215 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001216 /* Reload possibly changed frame fields */
1217 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001218 if (f->f_stacktop != NULL) {
1219 stack_pointer = f->f_stacktop;
1220 f->f_stacktop = NULL;
1221 }
1222 if (err) {
1223 /* trace function raised an exception */
1224 goto on_error;
1225 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001226 }
1227
1228 /* Extract opcode and argument */
1229
Guido van Rossum374a9221991-04-04 10:40:29 +00001230 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001231 oparg = 0; /* allows oparg to be stored in a register because
1232 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001233 if (HAS_ARG(opcode))
1234 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001235 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001236#ifdef DYNAMIC_EXECUTION_PROFILE
1237#ifdef DXPAIRS
1238 dxpairs[lastopcode][opcode]++;
1239 lastopcode = opcode;
1240#endif
1241 dxp[opcode]++;
1242#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001243
Guido van Rossum96a42c81992-01-12 02:29:51 +00001244#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001245 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001246
Guido van Rossum96a42c81992-01-12 02:29:51 +00001247 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001248 if (HAS_ARG(opcode)) {
1249 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001250 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001251 }
1252 else {
1253 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001254 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001255 }
1256 }
1257#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001258
Guido van Rossum374a9221991-04-04 10:40:29 +00001259 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001260 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001261
Guido van Rossum374a9221991-04-04 10:40:29 +00001262 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Guido van Rossum374a9221991-04-04 10:40:29 +00001264 /* BEWARE!
1265 It is essential that any operation that fails sets either
1266 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1267 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001268
Guido van Rossum374a9221991-04-04 10:40:29 +00001269 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001270
Antoine Pitroub52ec782009-01-25 16:34:23 +00001271 TARGET(NOP)
1272 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001273
Antoine Pitroub52ec782009-01-25 16:34:23 +00001274 TARGET(LOAD_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001275 x = GETLOCAL(oparg);
1276 if (x != NULL) {
1277 Py_INCREF(x);
1278 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001279 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001280 }
1281 format_exc_check_arg(PyExc_UnboundLocalError,
1282 UNBOUNDLOCAL_ERROR_MSG,
1283 PyTuple_GetItem(co->co_varnames, oparg));
1284 break;
1285
Antoine Pitroub52ec782009-01-25 16:34:23 +00001286 TARGET(LOAD_CONST)
Skip Montanaro04d80f82002-08-04 21:03:35 +00001287 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001288 Py_INCREF(x);
1289 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001290 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001291
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001292 PREDICTED_WITH_ARG(STORE_FAST);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001293 TARGET(STORE_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001294 v = POP();
1295 SETLOCAL(oparg, v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001296 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001297
Antoine Pitroub52ec782009-01-25 16:34:23 +00001298 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001299 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001300 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001301 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001302
Antoine Pitroub52ec782009-01-25 16:34:23 +00001303 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001304 v = TOP();
1305 w = SECOND();
1306 SET_TOP(w);
1307 SET_SECOND(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001308 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001309
Antoine Pitroub52ec782009-01-25 16:34:23 +00001310 TARGET(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001311 v = TOP();
1312 w = SECOND();
1313 x = THIRD();
1314 SET_TOP(w);
1315 SET_SECOND(x);
1316 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001317 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001318
Antoine Pitroub52ec782009-01-25 16:34:23 +00001319 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001320 u = TOP();
1321 v = SECOND();
1322 w = THIRD();
1323 x = FOURTH();
1324 SET_TOP(v);
1325 SET_SECOND(w);
1326 SET_THIRD(x);
1327 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001328 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001329
Antoine Pitroub52ec782009-01-25 16:34:23 +00001330 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001331 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001332 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001333 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001334 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001335
Antoine Pitroub52ec782009-01-25 16:34:23 +00001336 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001337 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001338 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001339 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001340 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001341 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001342 STACKADJ(2);
1343 SET_TOP(x);
1344 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001345 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001346 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001347 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001348 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001349 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001350 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001351 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001352 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001353 STACKADJ(3);
1354 SET_TOP(x);
1355 SET_SECOND(w);
1356 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001357 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001358 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001359 Py_FatalError("invalid argument to DUP_TOPX"
1360 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001361 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001362 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001363
Antoine Pitroub52ec782009-01-25 16:34:23 +00001364 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001366 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001367 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001368 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001369 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001370 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001371
Antoine Pitroub52ec782009-01-25 16:34:23 +00001372 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001373 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001374 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001375 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001376 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001377 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001378 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Antoine Pitroub52ec782009-01-25 16:34:23 +00001380 TARGET(UNARY_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001382 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001383 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001384 if (err == 0) {
1385 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001386 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001387 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001388 }
1389 else if (err > 0) {
1390 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001391 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001392 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001393 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001394 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001395 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001396 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Antoine Pitroub52ec782009-01-25 16:34:23 +00001398 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001399 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001400 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001401 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001402 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001403 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001404 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Antoine Pitroub52ec782009-01-25 16:34:23 +00001406 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001407 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001408 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001409 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001410 Py_DECREF(v);
1411 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001412 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001413 if (x != NULL) DISPATCH();
Guido van Rossum50564e81996-01-12 01:13:16 +00001414 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Antoine Pitroub52ec782009-01-25 16:34:23 +00001416 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001418 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001419 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001420 Py_DECREF(v);
1421 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001422 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001423 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001424 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001425
Antoine Pitroub52ec782009-01-25 16:34:23 +00001426 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001427 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001428 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001429 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001430 Py_DECREF(v);
1431 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001432 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001433 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001434 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001435
Antoine Pitroub52ec782009-01-25 16:34:23 +00001436 TARGET(BINARY_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001437 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001438 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001439 x = PyNumber_FloorDivide(v, w);
1440 Py_DECREF(v);
1441 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001442 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001443 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001444 break;
1445
Antoine Pitroub52ec782009-01-25 16:34:23 +00001446 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001447 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001448 v = TOP();
Benjamin Petersonefb06b02009-02-26 18:55:48 +00001449 if (PyUnicode_CheckExact(v))
1450 x = PyUnicode_Format(v, w);
1451 else
1452 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001453 Py_DECREF(v);
1454 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001455 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001456 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Antoine Pitroub52ec782009-01-25 16:34:23 +00001459 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001460 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001461 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001462 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001463 PyUnicode_CheckExact(w)) {
1464 x = unicode_concatenate(v, w, f, next_instr);
1465 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001466 goto skip_decref_vx;
1467 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001468 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001469 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001470 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001471 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001472 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001473 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001474 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001475 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001476 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001477
Antoine Pitroub52ec782009-01-25 16:34:23 +00001478 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001479 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001480 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001481 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001482 Py_DECREF(v);
1483 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001484 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001485 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001486 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Antoine Pitroub52ec782009-01-25 16:34:23 +00001488 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001489 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001490 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001491 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001492 Py_DECREF(v);
1493 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001494 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001495 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001496 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001497
Antoine Pitroub52ec782009-01-25 16:34:23 +00001498 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001499 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001500 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001501 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001502 Py_DECREF(v);
1503 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001504 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001505 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001506 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001507
Antoine Pitroub52ec782009-01-25 16:34:23 +00001508 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001509 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001510 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001511 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001512 Py_DECREF(v);
1513 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001514 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001515 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001516 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001517
Antoine Pitroub52ec782009-01-25 16:34:23 +00001518 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001519 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001520 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001521 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001522 Py_DECREF(v);
1523 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001524 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001525 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001526 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001527
Antoine Pitroub52ec782009-01-25 16:34:23 +00001528 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001529 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001530 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001531 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001532 Py_DECREF(v);
1533 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001534 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001535 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001536 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Antoine Pitroub52ec782009-01-25 16:34:23 +00001538 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001539 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001540 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001541 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001542 Py_DECREF(v);
1543 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001544 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001545 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001546 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001547
Antoine Pitroub52ec782009-01-25 16:34:23 +00001548 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001549 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001550 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001551 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001552 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001553 if (err == 0) {
1554 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001555 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001556 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001557 break;
1558
Antoine Pitroub52ec782009-01-25 16:34:23 +00001559 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001560 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001561 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001562 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001563 Py_DECREF(w);
1564 if (err == 0) {
1565 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001566 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001567 }
1568 break;
1569
Antoine Pitroub52ec782009-01-25 16:34:23 +00001570 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001571 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001572 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001573 x = PyNumber_InPlacePower(v, w, Py_None);
1574 Py_DECREF(v);
1575 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001576 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001577 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001578 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Antoine Pitroub52ec782009-01-25 16:34:23 +00001580 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001581 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001582 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001583 x = PyNumber_InPlaceMultiply(v, w);
1584 Py_DECREF(v);
1585 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001586 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001587 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001588 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001589
Antoine Pitroub52ec782009-01-25 16:34:23 +00001590 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001591 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001592 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001593 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001594 Py_DECREF(v);
1595 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001596 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001597 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001598 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001599
Antoine Pitroub52ec782009-01-25 16:34:23 +00001600 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001601 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001602 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001603 x = PyNumber_InPlaceFloorDivide(v, w);
1604 Py_DECREF(v);
1605 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001606 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001607 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001608 break;
1609
Antoine Pitroub52ec782009-01-25 16:34:23 +00001610 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001611 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001612 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001613 x = PyNumber_InPlaceRemainder(v, w);
1614 Py_DECREF(v);
1615 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001616 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001617 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001618 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001619
Antoine Pitroub52ec782009-01-25 16:34:23 +00001620 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001621 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001622 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001623 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001624 PyUnicode_CheckExact(w)) {
1625 x = unicode_concatenate(v, w, f, next_instr);
1626 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001627 goto skip_decref_v;
1628 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001629 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001630 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001631 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001632 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001633 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001634 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001635 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001636 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Antoine Pitroub52ec782009-01-25 16:34:23 +00001639 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001640 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001641 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001642 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001643 Py_DECREF(v);
1644 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001645 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001646 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001647 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001648
Antoine Pitroub52ec782009-01-25 16:34:23 +00001649 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001650 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001651 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001652 x = PyNumber_InPlaceLshift(v, w);
1653 Py_DECREF(v);
1654 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001655 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001656 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001657 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001658
Antoine Pitroub52ec782009-01-25 16:34:23 +00001659 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001660 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001661 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001662 x = PyNumber_InPlaceRshift(v, w);
1663 Py_DECREF(v);
1664 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001665 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001666 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001667 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001668
Antoine Pitroub52ec782009-01-25 16:34:23 +00001669 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001670 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001671 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001672 x = PyNumber_InPlaceAnd(v, w);
1673 Py_DECREF(v);
1674 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001675 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001676 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001677 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Antoine Pitroub52ec782009-01-25 16:34:23 +00001679 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001680 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001681 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001682 x = PyNumber_InPlaceXor(v, w);
1683 Py_DECREF(v);
1684 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001685 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001686 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001687 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Antoine Pitroub52ec782009-01-25 16:34:23 +00001689 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001690 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001691 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001692 x = PyNumber_InPlaceOr(v, w);
1693 Py_DECREF(v);
1694 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001695 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001696 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001697 break;
1698
Antoine Pitroub52ec782009-01-25 16:34:23 +00001699 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001700 w = TOP();
1701 v = SECOND();
1702 u = THIRD();
1703 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001704 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001705 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001706 Py_DECREF(u);
1707 Py_DECREF(v);
1708 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001709 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001710 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001711
Antoine Pitroub52ec782009-01-25 16:34:23 +00001712 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001713 w = TOP();
1714 v = SECOND();
1715 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001716 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001717 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001718 Py_DECREF(v);
1719 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001720 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001721 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001722
Antoine Pitroub52ec782009-01-25 16:34:23 +00001723 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001724 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001725 w = PySys_GetObject("displayhook");
1726 if (w == NULL) {
1727 PyErr_SetString(PyExc_RuntimeError,
1728 "lost sys.displayhook");
1729 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001730 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001731 }
1732 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001733 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001734 if (x == NULL)
1735 err = -1;
1736 }
1737 if (err == 0) {
1738 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001739 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001740 if (w == NULL)
1741 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001743 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001744 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001746
Thomas Wouters434d0822000-08-24 20:11:32 +00001747#ifdef CASE_TOO_BIG
1748 default: switch (opcode) {
1749#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001750 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001751 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001752 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001753 case 2:
1754 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001755 case 1:
1756 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001757 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001758 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001759 break;
1760 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001761 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001762 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001763 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001764 break;
1765 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001767
Antoine Pitroub52ec782009-01-25 16:34:23 +00001768 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001769 x = POP();
1770 v = f->f_locals;
1771 Py_XDECREF(v);
1772 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001773 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001774
Antoine Pitroub52ec782009-01-25 16:34:23 +00001775 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001776 retval = POP();
1777 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001778 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001779
Antoine Pitroub52ec782009-01-25 16:34:23 +00001780 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001781 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001782 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001783 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001784 /* Put aside the current exception state and restore
1785 that of the calling frame. This only serves when
1786 "yield" is used inside an except handler. */
1787 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001788 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001789
Antoine Pitroub52ec782009-01-25 16:34:23 +00001790 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001791 {
1792 PyTryBlock *b = PyFrame_BlockPop(f);
1793 if (b->b_type != EXCEPT_HANDLER) {
1794 PyErr_SetString(PyExc_SystemError,
1795 "popped block is not an except handler");
1796 why = WHY_EXCEPTION;
1797 break;
1798 }
1799 UNWIND_EXCEPT_HANDLER(b);
1800 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001801 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001802
Antoine Pitroub52ec782009-01-25 16:34:23 +00001803 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001804 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001805 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001806 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001808 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001809
Christian Heimes180510d2008-03-03 19:15:45 +00001810 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001811 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001812 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001813 if (PyLong_Check(v)) {
1814 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001815 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001816 if (why == WHY_RETURN ||
1817 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001818 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001819 if (why == WHY_SILENCED) {
1820 /* An exception was silenced by 'with', we must
1821 manually unwind the EXCEPT_HANDLER block which was
1822 created when the exception was caught, otherwise
1823 the stack will be in an inconsistent state. */
1824 PyTryBlock *b = PyFrame_BlockPop(f);
1825 if (b->b_type != EXCEPT_HANDLER) {
1826 PyErr_SetString(PyExc_SystemError,
1827 "popped block is not an except handler");
1828 why = WHY_EXCEPTION;
1829 }
1830 else {
1831 UNWIND_EXCEPT_HANDLER(b);
1832 why = WHY_NOT;
1833 }
1834 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001835 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001836 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001838 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001839 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001841 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001842 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001843 else if (v != Py_None) {
1844 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001845 "'finally' pops bad exception");
1846 why = WHY_EXCEPTION;
1847 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001848 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001849 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001850
Antoine Pitroub52ec782009-01-25 16:34:23 +00001851 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001852 x = PyDict_GetItemString(f->f_builtins,
1853 "__build_class__");
1854 if (x == NULL) {
1855 PyErr_SetString(PyExc_ImportError,
1856 "__build_class__ not found");
1857 break;
1858 }
1859 Py_INCREF(x);
1860 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001861 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001862
Antoine Pitroub52ec782009-01-25 16:34:23 +00001863 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001864 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001865 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001866 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001867 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001868 err = PyDict_SetItem(x, w, v);
1869 else
1870 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001871 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001872 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001873 break;
1874 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001875 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001876 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001877 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001878
Antoine Pitroub52ec782009-01-25 16:34:23 +00001879 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001880 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001881 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001882 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001883 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001884 NAME_ERROR_MSG,
1885 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001886 break;
1887 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001888 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001889 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001891
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001892 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001893 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001894 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001895 if (PyTuple_CheckExact(v) &&
1896 PyTuple_GET_SIZE(v) == oparg) {
1897 PyObject **items = \
1898 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001899 while (oparg--) {
1900 w = items[oparg];
1901 Py_INCREF(w);
1902 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001903 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001904 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001905 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001906 } else if (PyList_CheckExact(v) &&
1907 PyList_GET_SIZE(v) == oparg) {
1908 PyObject **items = \
1909 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001910 while (oparg--) {
1911 w = items[oparg];
1912 Py_INCREF(w);
1913 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001914 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001915 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001916 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001917 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001918 } else {
1919 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001920 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001921 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001922 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001923 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Antoine Pitroub52ec782009-01-25 16:34:23 +00001925 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00001926 {
1927 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1928 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001929
Guido van Rossum0368b722007-05-11 16:50:42 +00001930 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1931 stack_pointer + totalargs)) {
1932 stack_pointer += totalargs;
1933 } else {
1934 why = WHY_EXCEPTION;
1935 }
1936 Py_DECREF(v);
1937 break;
1938 }
1939
Antoine Pitroub52ec782009-01-25 16:34:23 +00001940 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001941 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001942 v = TOP();
1943 u = SECOND();
1944 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001945 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1946 Py_DECREF(v);
1947 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001948 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Antoine Pitroub52ec782009-01-25 16:34:23 +00001951 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001952 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001953 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001954 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1955 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001956 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001957 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Antoine Pitroub52ec782009-01-25 16:34:23 +00001959 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001960 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001961 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001962 err = PyDict_SetItem(f->f_globals, w, v);
1963 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001964 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001965 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001966
Antoine Pitroub52ec782009-01-25 16:34:23 +00001967 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001968 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001969 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001970 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001971 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001972 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001973
Antoine Pitroub52ec782009-01-25 16:34:23 +00001974 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001975 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001976 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001977 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001978 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001979 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001980 break;
1981 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001982 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001983 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001984 Py_XINCREF(x);
1985 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001986 else {
1987 x = PyObject_GetItem(v, w);
1988 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001989 if (!PyErr_ExceptionMatches(
1990 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001991 break;
1992 PyErr_Clear();
1993 }
1994 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001995 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001996 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001997 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001998 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001999 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002000 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002001 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002002 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002003 break;
2004 }
2005 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002006 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002007 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002008 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002009 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002010
Antoine Pitroub52ec782009-01-25 16:34:23 +00002011 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002012 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002013 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002014 /* Inline the PyDict_GetItem() calls.
2015 WARNING: this is an extreme speed hack.
2016 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002017 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002018 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002019 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002020 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002021 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002022 e = d->ma_lookup(d, w, hash);
2023 if (e == NULL) {
2024 x = NULL;
2025 break;
2026 }
2027 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002028 if (x != NULL) {
2029 Py_INCREF(x);
2030 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002031 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002032 }
2033 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002034 e = d->ma_lookup(d, w, hash);
2035 if (e == NULL) {
2036 x = NULL;
2037 break;
2038 }
2039 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002040 if (x != NULL) {
2041 Py_INCREF(x);
2042 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002043 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002044 }
2045 goto load_global_error;
2046 }
2047 }
2048 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002050 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002051 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002052 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002053 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002054 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002055 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002056 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002057 break;
2058 }
2059 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002060 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002061 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002062 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002063
Antoine Pitroub52ec782009-01-25 16:34:23 +00002064 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002065 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002066 if (x != NULL) {
2067 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002068 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002069 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002070 format_exc_check_arg(
2071 PyExc_UnboundLocalError,
2072 UNBOUNDLOCAL_ERROR_MSG,
2073 PyTuple_GetItem(co->co_varnames, oparg)
2074 );
2075 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002076
Antoine Pitroub52ec782009-01-25 16:34:23 +00002077 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002078 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002079 Py_INCREF(x);
2080 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002081 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002082 break;
2083
Antoine Pitroub52ec782009-01-25 16:34:23 +00002084 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002085 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002086 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002087 if (w != NULL) {
2088 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002089 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002090 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002091 err = -1;
2092 /* Don't stomp existing exception */
2093 if (PyErr_Occurred())
2094 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002095 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2096 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002097 oparg);
2098 format_exc_check_arg(
2099 PyExc_UnboundLocalError,
2100 UNBOUNDLOCAL_ERROR_MSG,
2101 v);
2102 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002103 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2104 PyTuple_GET_SIZE(co->co_cellvars));
2105 format_exc_check_arg(PyExc_NameError,
2106 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002107 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002108 break;
2109
Antoine Pitroub52ec782009-01-25 16:34:23 +00002110 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002111 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002112 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002113 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002114 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002115 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002116
Antoine Pitroub52ec782009-01-25 16:34:23 +00002117 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002118 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002119 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002120 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002121 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002122 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002123 }
2124 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002125 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002126 }
2127 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002128
Antoine Pitroub52ec782009-01-25 16:34:23 +00002129 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002130 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002131 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002132 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002133 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002134 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002135 }
2136 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002137 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002138 }
2139 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002140
Antoine Pitroub52ec782009-01-25 16:34:23 +00002141 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002142 x = PySet_New(NULL);
2143 if (x != NULL) {
2144 for (; --oparg >= 0;) {
2145 w = POP();
2146 if (err == 0)
2147 err = PySet_Add(x, w);
2148 Py_DECREF(w);
2149 }
2150 if (err != 0) {
2151 Py_DECREF(x);
2152 break;
2153 }
2154 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002155 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002156 }
2157 break;
2158
Antoine Pitroub52ec782009-01-25 16:34:23 +00002159 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002160 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002161 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002162 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002163 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002164
Antoine Pitroub52ec782009-01-25 16:34:23 +00002165 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002166 w = TOP(); /* key */
2167 u = SECOND(); /* value */
2168 v = THIRD(); /* dict */
2169 STACKADJ(-2);
2170 assert (PyDict_CheckExact(v));
2171 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2172 Py_DECREF(u);
2173 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002174 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002175 break;
2176
Antoine Pitroub52ec782009-01-25 16:34:23 +00002177 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002178 w = TOP(); /* key */
2179 u = SECOND(); /* value */
2180 STACKADJ(-2);
2181 v = stack_pointer[-oparg]; /* dict */
2182 assert (PyDict_CheckExact(v));
2183 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2184 Py_DECREF(u);
2185 Py_DECREF(w);
2186 if (err == 0) {
2187 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002188 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002189 }
2190 break;
2191
Antoine Pitroub52ec782009-01-25 16:34:23 +00002192 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002193 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002194 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002195 x = PyObject_GetAttr(v, w);
2196 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002197 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002198 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002199 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002200
Antoine Pitroub52ec782009-01-25 16:34:23 +00002201 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002202 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002203 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002204 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002205 Py_DECREF(v);
2206 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002207 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002208 if (x == NULL) break;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002209 PREDICT(POP_JUMP_IF_FALSE);
2210 PREDICT(POP_JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002211 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002212
Antoine Pitroub52ec782009-01-25 16:34:23 +00002213 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002214 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002215 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002216 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002217 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002218 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002219 break;
2220 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002221 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002222 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002223 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002224 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002225 w = PyTuple_Pack(5,
2226 w,
2227 f->f_globals,
2228 f->f_locals == NULL ?
2229 Py_None : f->f_locals,
2230 v,
2231 u);
2232 else
2233 w = PyTuple_Pack(4,
2234 w,
2235 f->f_globals,
2236 f->f_locals == NULL ?
2237 Py_None : f->f_locals,
2238 v);
2239 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002240 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002241 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002242 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002243 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002244 x = NULL;
2245 break;
2246 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002247 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002248 v = x;
2249 x = PyEval_CallObject(v, w);
2250 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002251 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002252 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002253 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002254 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002255 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002256
Antoine Pitroub52ec782009-01-25 16:34:23 +00002257 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002258 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002259 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002260 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002261 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002262 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002263 break;
2264 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002265 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002266 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002267 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002268 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002269 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002270 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002271 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002272
Antoine Pitroub52ec782009-01-25 16:34:23 +00002273 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002274 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002275 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002276 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002277 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002278 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002279 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002280 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002281 break;
2282
Antoine Pitroub52ec782009-01-25 16:34:23 +00002283 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002284 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002285 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002286
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002287 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2288 TARGET(POP_JUMP_IF_FALSE)
2289 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002290 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002291 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002292 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002293 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002294 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002295 Py_DECREF(w);
2296 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002297 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002298 }
2299 err = PyObject_IsTrue(w);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002300 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002301 if (err > 0)
2302 err = 0;
2303 else if (err == 0)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002304 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002305 else
2306 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002307 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002308
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002309 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2310 TARGET(POP_JUMP_IF_TRUE)
2311 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002312 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002313 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002314 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002315 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002316 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002317 Py_DECREF(w);
2318 JUMPTO(oparg);
2319 FAST_DISPATCH();
2320 }
2321 err = PyObject_IsTrue(w);
2322 Py_DECREF(w);
2323 if (err > 0) {
2324 err = 0;
2325 JUMPTO(oparg);
2326 }
2327 else if (err == 0)
2328 ;
2329 else
2330 break;
2331 DISPATCH();
2332
2333 TARGET(JUMP_IF_FALSE_OR_POP)
2334 w = TOP();
2335 if (w == Py_True) {
2336 STACKADJ(-1);
2337 Py_DECREF(w);
2338 FAST_DISPATCH();
2339 }
2340 if (w == Py_False) {
2341 JUMPTO(oparg);
2342 FAST_DISPATCH();
2343 }
2344 err = PyObject_IsTrue(w);
2345 if (err > 0) {
2346 STACKADJ(-1);
2347 Py_DECREF(w);
2348 err = 0;
2349 }
2350 else if (err == 0)
2351 JUMPTO(oparg);
2352 else
2353 break;
2354 DISPATCH();
2355
2356 TARGET(JUMP_IF_TRUE_OR_POP)
2357 w = TOP();
2358 if (w == Py_False) {
2359 STACKADJ(-1);
2360 Py_DECREF(w);
2361 FAST_DISPATCH();
2362 }
2363 if (w == Py_True) {
2364 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002365 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002366 }
2367 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002368 if (err > 0) {
2369 err = 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002370 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002371 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002372 else if (err == 0) {
2373 STACKADJ(-1);
2374 Py_DECREF(w);
2375 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002376 else
2377 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002378 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002380 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002381 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002382 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002383#if FAST_LOOPS
2384 /* Enabling this path speeds-up all while and for-loops by bypassing
2385 the per-loop checks for signals. By default, this should be turned-off
2386 because it prevents detection of a control-break in tight loops like
2387 "while 1: pass". Compile with this option turned-on when you need
2388 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002389 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002390 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002391 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002392#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002393 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002394#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002395
Antoine Pitroub52ec782009-01-25 16:34:23 +00002396 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002397 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002398 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002399 x = PyObject_GetIter(v);
2400 Py_DECREF(v);
2401 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002402 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002403 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002404 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002405 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002406 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002407 break;
2408
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002409 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002410 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002411 /* before: [iter]; after: [iter, iter()] *or* [] */
2412 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002413 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002414 if (x != NULL) {
2415 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002416 PREDICT(STORE_FAST);
2417 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002418 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002419 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002420 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002421 if (!PyErr_ExceptionMatches(
2422 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002423 break;
2424 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002425 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002426 /* iterator ended normally */
2427 x = v = POP();
2428 Py_DECREF(v);
2429 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002430 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002431
Antoine Pitroub52ec782009-01-25 16:34:23 +00002432 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002433 why = WHY_BREAK;
2434 goto fast_block_end;
2435
Antoine Pitroub52ec782009-01-25 16:34:23 +00002436 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002437 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002438 if (!retval) {
2439 x = NULL;
2440 break;
2441 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002442 why = WHY_CONTINUE;
2443 goto fast_block_end;
2444
Antoine Pitroub52ec782009-01-25 16:34:23 +00002445 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2446 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2447 TARGET(SETUP_FINALLY)
2448 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002449 /* NOTE: If you add any new block-setup opcodes that
2450 are not try/except/finally handlers, you may need
2451 to update the PyGen_NeedsFinalizing() function.
2452 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002453
Guido van Rossumb209a111997-04-29 18:18:01 +00002454 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002455 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002456 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002457
Antoine Pitroub52ec782009-01-25 16:34:23 +00002458 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002459 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002460 /* At the top of the stack are 1-3 values indicating
2461 how/why we entered the finally clause:
2462 - TOP = None
2463 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2464 - TOP = WHY_*; no retval below it
2465 - (TOP, SECOND, THIRD) = exc_info()
2466 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002467 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002468 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002469 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002470 EXIT(None, None, None)
2471
2472 In all cases, we remove EXIT from the stack, leaving
2473 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002474
2475 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002476 *and* the function call returns a 'true' value, we
2477 "zap" this information, to prevent END_FINALLY from
2478 re-raising the exception. (But non-local gotos
2479 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002480 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002481
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002482 PyObject *exit_func = POP();
2483 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002484 if (u == Py_None) {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002485 v = w = Py_None;
2486 }
2487 else if (PyLong_Check(u)) {
Guido van Rossumc2e20742006-02-27 22:32:47 +00002488 u = v = w = Py_None;
2489 }
2490 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002491 v = SECOND();
2492 w = THIRD();
Guido van Rossumc2e20742006-02-27 22:32:47 +00002493 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002494 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002495 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2496 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002497 Py_DECREF(exit_func);
2498 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002499 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002500
2501 if (u != Py_None)
2502 err = PyObject_IsTrue(x);
2503 else
2504 err = 0;
2505 Py_DECREF(x);
2506
2507 if (err < 0)
2508 break; /* Go to error exit */
2509 else if (err > 0) {
2510 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002511 /* There was an exception and a True return */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002512 STACKADJ(-2);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002513 SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002514 Py_DECREF(u);
2515 Py_DECREF(v);
2516 Py_DECREF(w);
Guido van Rossumf6694362006-03-10 02:28:35 +00002517 }
Christian Heimes180510d2008-03-03 19:15:45 +00002518 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002519 break;
2520 }
2521
Antoine Pitroub52ec782009-01-25 16:34:23 +00002522 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002523 {
2524 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002525 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002526 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002527#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002528 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002529#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002530 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002531#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002532 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002533 PUSH(x);
2534 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002535 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002536 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002537 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002538
Antoine Pitroub52ec782009-01-25 16:34:23 +00002539 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2540 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2541 TARGET(CALL_FUNCTION_VAR_KW)
2542 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002543 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002544 int na = oparg & 0xff;
2545 int nk = (oparg>>8) & 0xff;
2546 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002547 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002548 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002549 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002550 if (flags & CALL_FLAG_VAR)
2551 n++;
2552 if (flags & CALL_FLAG_KW)
2553 n++;
2554 pfunc = stack_pointer - n - 1;
2555 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002556
Guido van Rossumac7be682001-01-17 15:42:30 +00002557 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002558 && PyMethod_GET_SELF(func) != NULL) {
2559 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002560 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002561 func = PyMethod_GET_FUNCTION(func);
2562 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002563 Py_DECREF(*pfunc);
2564 *pfunc = self;
2565 na++;
2566 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002567 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002568 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002569 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002570 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002571 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002572 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002573 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002574 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002575
Jeremy Hylton76901512000-03-28 23:49:17 +00002576 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002577 w = POP();
2578 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002579 }
2580 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002581 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002582 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002583 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002584 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002585
Antoine Pitroub52ec782009-01-25 16:34:23 +00002586 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2587 TARGET(MAKE_FUNCTION)
2588 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002589 {
2590 int posdefaults = oparg & 0xff;
2591 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002592 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002593
Guido van Rossum681d79a1995-07-18 14:51:37 +00002594 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002595 x = PyFunction_New(v, f->f_globals);
2596 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002597
Guido van Rossum0240b922007-02-26 21:23:50 +00002598 if (x != NULL && opcode == MAKE_CLOSURE) {
2599 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002600 if (PyFunction_SetClosure(x, v) != 0) {
2601 /* Can't happen unless bytecode is corrupt. */
2602 why = WHY_EXCEPTION;
2603 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002604 Py_DECREF(v);
2605 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002606
2607 if (x != NULL && num_annotations > 0) {
2608 Py_ssize_t name_ix;
2609 u = POP(); /* names of args with annotations */
2610 v = PyDict_New();
2611 if (v == NULL) {
2612 Py_DECREF(x);
2613 x = NULL;
2614 break;
2615 }
2616 name_ix = PyTuple_Size(u);
2617 assert(num_annotations == name_ix+1);
2618 while (name_ix > 0) {
2619 --name_ix;
2620 t = PyTuple_GET_ITEM(u, name_ix);
2621 w = POP();
2622 /* XXX(nnorwitz): check for errors */
2623 PyDict_SetItem(v, t, w);
2624 Py_DECREF(w);
2625 }
2626
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002627 if (PyFunction_SetAnnotations(x, v) != 0) {
2628 /* Can't happen unless
2629 PyFunction_SetAnnotations changes. */
2630 why = WHY_EXCEPTION;
2631 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002632 Py_DECREF(v);
2633 Py_DECREF(u);
2634 }
2635
Guido van Rossum681d79a1995-07-18 14:51:37 +00002636 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002637 if (x != NULL && posdefaults > 0) {
2638 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002639 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002640 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002641 x = NULL;
2642 break;
2643 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002644 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002645 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002646 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002647 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002648 if (PyFunction_SetDefaults(x, v) != 0) {
2649 /* Can't happen unless
2650 PyFunction_SetDefaults changes. */
2651 why = WHY_EXCEPTION;
2652 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002653 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002654 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002655 if (x != NULL && kwdefaults > 0) {
2656 v = PyDict_New();
2657 if (v == NULL) {
2658 Py_DECREF(x);
2659 x = NULL;
2660 break;
2661 }
2662 while (--kwdefaults >= 0) {
2663 w = POP(); /* default value */
2664 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002665 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002666 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002667 Py_DECREF(w);
2668 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002669 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002670 if (PyFunction_SetKwDefaults(x, v) != 0) {
2671 /* Can't happen unless
2672 PyFunction_SetKwDefaults changes. */
2673 why = WHY_EXCEPTION;
2674 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002675 Py_DECREF(v);
2676 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002677 PUSH(x);
2678 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002679 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002680
Antoine Pitroub52ec782009-01-25 16:34:23 +00002681 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002682 if (oparg == 3)
2683 w = POP();
2684 else
2685 w = NULL;
2686 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002687 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002688 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002689 Py_DECREF(u);
2690 Py_DECREF(v);
2691 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002692 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002693 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002694 break;
2695
Antoine Pitroub52ec782009-01-25 16:34:23 +00002696 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002697 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002698 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002699 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002700
Antoine Pitroub52ec782009-01-25 16:34:23 +00002701#ifdef USE_COMPUTED_GOTOS
2702 _unknown_opcode:
2703#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002704 default:
2705 fprintf(stderr,
2706 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002707 PyCode_Addr2Line(f->f_code, f->f_lasti),
2708 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002709 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002710 why = WHY_EXCEPTION;
2711 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002712
2713#ifdef CASE_TOO_BIG
2714 }
2715#endif
2716
Guido van Rossum374a9221991-04-04 10:40:29 +00002717 } /* switch */
2718
2719 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002720
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002721 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002722
Guido van Rossum374a9221991-04-04 10:40:29 +00002723 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002724
Guido van Rossum374a9221991-04-04 10:40:29 +00002725 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002726 if (err == 0 && x != NULL) {
2727#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002728 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002729 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002730 fprintf(stderr,
2731 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002732 else {
2733#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002734 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002735 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002736#ifdef CHECKEXC
2737 }
2738#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002739 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002740 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002741 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002742 err = 0;
2743 }
2744
Guido van Rossum374a9221991-04-04 10:40:29 +00002745 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002746
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002747 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002748 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002749 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002750 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002751 why = WHY_EXCEPTION;
2752 }
2753 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002754#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002755 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002756 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002757 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002758 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002759 sprintf(buf, "Stack unwind with exception "
2760 "set and why=%d", why);
2761 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002762 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002763 }
2764#endif
2765
2766 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002767
Guido van Rossum374a9221991-04-04 10:40:29 +00002768 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002769 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002770
Fred Drake8f51f542001-10-04 14:48:42 +00002771 if (tstate->c_tracefunc != NULL)
2772 call_exc_trace(tstate->c_tracefunc,
2773 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002774 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002775
Guido van Rossum374a9221991-04-04 10:40:29 +00002776 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002777
Guido van Rossum374a9221991-04-04 10:40:29 +00002778 if (why == WHY_RERAISE)
2779 why = WHY_EXCEPTION;
2780
2781 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002782
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002783fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002784 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002785 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002786
Tim Peters8a5c3c72004-04-05 19:36:21 +00002787 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002788 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2789 /* For a continue inside a try block,
2790 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002791 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2792 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002793 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002794 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002795 Py_DECREF(retval);
2796 break;
2797 }
2798
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002799 if (b->b_type == EXCEPT_HANDLER) {
2800 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002801 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002802 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002803 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002804 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2805 why = WHY_NOT;
2806 JUMPTO(b->b_handler);
2807 break;
2808 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002809 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2810 || b->b_type == SETUP_FINALLY)) {
2811 PyObject *exc, *val, *tb;
2812 int handler = b->b_handler;
2813 /* Beware, this invalidates all b->b_* fields */
2814 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2815 PUSH(tstate->exc_traceback);
2816 PUSH(tstate->exc_value);
2817 if (tstate->exc_type != NULL) {
2818 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002819 }
2820 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002821 Py_INCREF(Py_None);
2822 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002823 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002824 PyErr_Fetch(&exc, &val, &tb);
2825 /* Make the raw exception data
2826 available to the handler,
2827 so a program can emulate the
2828 Python main loop. */
2829 PyErr_NormalizeException(
2830 &exc, &val, &tb);
2831 PyException_SetTraceback(val, tb);
2832 Py_INCREF(exc);
2833 tstate->exc_type = exc;
2834 Py_INCREF(val);
2835 tstate->exc_value = val;
2836 tstate->exc_traceback = tb;
2837 if (tb == NULL)
2838 tb = Py_None;
2839 Py_INCREF(tb);
2840 PUSH(tb);
2841 PUSH(val);
2842 PUSH(exc);
2843 why = WHY_NOT;
2844 JUMPTO(handler);
2845 break;
2846 }
2847 if (b->b_type == SETUP_FINALLY) {
2848 if (why & (WHY_RETURN | WHY_CONTINUE))
2849 PUSH(retval);
2850 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002851 why = WHY_NOT;
2852 JUMPTO(b->b_handler);
2853 break;
2854 }
2855 } /* unwind stack */
2856
2857 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002858
Guido van Rossum374a9221991-04-04 10:40:29 +00002859 if (why != WHY_NOT)
2860 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002861 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002862
Guido van Rossum374a9221991-04-04 10:40:29 +00002863 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002864
Tim Peters8a5c3c72004-04-05 19:36:21 +00002865 assert(why != WHY_YIELD);
2866 /* Pop remaining stack entries. */
2867 while (!EMPTY()) {
2868 v = POP();
2869 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002870 }
2871
Tim Peters8a5c3c72004-04-05 19:36:21 +00002872 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002873 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002874
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002875fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002876 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002877 if (tstate->c_tracefunc) {
2878 if (why == WHY_RETURN || why == WHY_YIELD) {
2879 if (call_trace(tstate->c_tracefunc,
2880 tstate->c_traceobj, f,
2881 PyTrace_RETURN, retval)) {
2882 Py_XDECREF(retval);
2883 retval = NULL;
2884 why = WHY_EXCEPTION;
2885 }
2886 }
2887 else if (why == WHY_EXCEPTION) {
2888 call_trace_protected(tstate->c_tracefunc,
2889 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002890 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002891 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002892 }
Fred Drake8f51f542001-10-04 14:48:42 +00002893 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002894 if (why == WHY_EXCEPTION)
2895 call_trace_protected(tstate->c_profilefunc,
2896 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002897 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002898 else if (call_trace(tstate->c_profilefunc,
2899 tstate->c_profileobj, f,
2900 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002901 Py_XDECREF(retval);
2902 retval = NULL;
2903 why = WHY_EXCEPTION;
2904 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002905 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002906 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002907
Tim Peters5ca576e2001-06-18 22:08:13 +00002908 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002909exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002910 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002911 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002912
Guido van Rossum96a42c81992-01-12 02:29:51 +00002913 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002914}
2915
Guido van Rossumc2e20742006-02-27 22:32:47 +00002916/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002917 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002918 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002919
Tim Peters6d6c1a32001-08-02 04:15:00 +00002920PyObject *
2921PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002922 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002923 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002924{
2925 register PyFrameObject *f;
2926 register PyObject *retval = NULL;
2927 register PyObject **fastlocals, **freevars;
2928 PyThreadState *tstate = PyThreadState_GET();
2929 PyObject *x, *u;
2930
2931 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002932 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002933 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002934 return NULL;
2935 }
2936
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002937 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002938 assert(globals != NULL);
2939 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002940 if (f == NULL)
2941 return NULL;
2942
2943 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002944 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002945
2946 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002947 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002948 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2949 int i;
2950 int n = argcount;
2951 PyObject *kwdict = NULL;
2952 if (co->co_flags & CO_VARKEYWORDS) {
2953 kwdict = PyDict_New();
2954 if (kwdict == NULL)
2955 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002956 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002957 if (co->co_flags & CO_VARARGS)
2958 i++;
2959 SETLOCAL(i, kwdict);
2960 }
2961 if (argcount > co->co_argcount) {
2962 if (!(co->co_flags & CO_VARARGS)) {
2963 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002964 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002965 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002966 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002967 defcount ? "at most" : "exactly",
2968 co->co_argcount,
2969 kwcount ? "non-keyword " : "",
2970 co->co_argcount == 1 ? "" : "s",
2971 argcount);
2972 goto fail;
2973 }
2974 n = co->co_argcount;
2975 }
2976 for (i = 0; i < n; i++) {
2977 x = args[i];
2978 Py_INCREF(x);
2979 SETLOCAL(i, x);
2980 }
2981 if (co->co_flags & CO_VARARGS) {
2982 u = PyTuple_New(argcount - n);
2983 if (u == NULL)
2984 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002985 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002986 for (i = n; i < argcount; i++) {
2987 x = args[i];
2988 Py_INCREF(x);
2989 PyTuple_SET_ITEM(u, i-n, x);
2990 }
2991 }
2992 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002993 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002994 PyObject *keyword = kws[2*i];
2995 PyObject *value = kws[2*i + 1];
2996 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00002997 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002998 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002999 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003000 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00003001 goto fail;
3002 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003003 /* Speed hack: do raw pointer compares. As names are
3004 normally interned this should almost always hit. */
3005 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003006 for (j = 0;
3007 j < co->co_argcount + co->co_kwonlyargcount;
3008 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003009 PyObject *nm = co_varnames[j];
3010 if (nm == keyword)
3011 goto kw_found;
3012 }
3013 /* Slow fallback, just in case */
3014 for (j = 0;
3015 j < co->co_argcount + co->co_kwonlyargcount;
3016 j++) {
3017 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003018 int cmp = PyObject_RichCompareBool(
3019 keyword, nm, Py_EQ);
3020 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003021 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003022 else if (cmp < 0)
3023 goto fail;
3024 }
3025 /* Check errors from Compare */
3026 if (PyErr_Occurred())
3027 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003028 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003029 if (kwdict == NULL) {
3030 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003031 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00003032 "keyword argument '%S'",
3033 co->co_name,
3034 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00003035 goto fail;
3036 }
3037 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003038 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003039 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003040kw_found:
3041 if (GETLOCAL(j) != NULL) {
3042 PyErr_Format(PyExc_TypeError,
3043 "%U() got multiple "
3044 "values for keyword "
3045 "argument '%S'",
3046 co->co_name,
3047 keyword);
3048 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003049 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003050 Py_INCREF(value);
3051 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003052 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003053 if (co->co_kwonlyargcount > 0) {
3054 for (i = co->co_argcount;
3055 i < co->co_argcount + co->co_kwonlyargcount;
3056 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003057 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003058 if (GETLOCAL(i) != NULL)
3059 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003060 name = PyTuple_GET_ITEM(co->co_varnames, i);
3061 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003062 if (kwdefs != NULL)
3063 def = PyDict_GetItem(kwdefs, name);
3064 if (def != NULL) {
3065 Py_INCREF(def);
3066 SETLOCAL(i, def);
3067 continue;
3068 }
3069 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003070 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003071 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003072 goto fail;
3073 }
3074 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003075 if (argcount < co->co_argcount) {
3076 int m = co->co_argcount - defcount;
3077 for (i = argcount; i < m; i++) {
3078 if (GETLOCAL(i) == NULL) {
3079 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003080 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003081 "%spositional argument%s "
3082 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003083 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003084 ((co->co_flags & CO_VARARGS) ||
3085 defcount) ? "at least"
3086 : "exactly",
3087 m, kwcount ? "non-keyword " : "",
3088 m == 1 ? "" : "s", i);
3089 goto fail;
3090 }
3091 }
3092 if (n > m)
3093 i = n - m;
3094 else
3095 i = 0;
3096 for (; i < defcount; i++) {
3097 if (GETLOCAL(m+i) == NULL) {
3098 PyObject *def = defs[i];
3099 Py_INCREF(def);
3100 SETLOCAL(m+i, def);
3101 }
3102 }
3103 }
3104 }
3105 else {
3106 if (argcount > 0 || kwcount > 0) {
3107 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003108 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003109 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003110 argcount + kwcount);
3111 goto fail;
3112 }
3113 }
3114 /* Allocate and initialize storage for cell vars, and copy free
3115 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003116 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003117 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003118 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003119 PyObject *c;
3120
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003121 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003122 if (co->co_flags & CO_VARARGS)
3123 nargs++;
3124 if (co->co_flags & CO_VARKEYWORDS)
3125 nargs++;
3126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127 /* Initialize each cell var, taking into account
3128 cell vars that are initialized from arguments.
3129
3130 Should arrange for the compiler to put cellvars
3131 that are arguments at the beginning of the cellvars
3132 list so that we can march over it more efficiently?
3133 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003134 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003135 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003136 PyTuple_GET_ITEM(co->co_cellvars, i));
3137 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003139 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003140 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003141 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003142 c = PyCell_New(GETLOCAL(j));
3143 if (c == NULL)
3144 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003145 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003146 found = 1;
3147 break;
3148 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003149 }
3150 if (found == 0) {
3151 c = PyCell_New(NULL);
3152 if (c == NULL)
3153 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003154 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003155 }
3156 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003157 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003158 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003159 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003160 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003161 PyObject *o = PyTuple_GET_ITEM(closure, i);
3162 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003163 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003164 }
3165 }
3166
Tim Peters5ca576e2001-06-18 22:08:13 +00003167 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003168 /* Don't need to keep the reference to f_back, it will be set
3169 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003170 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003171 f->f_back = NULL;
3172
Jeremy Hylton985eba52003-02-05 23:13:00 +00003173 PCALL(PCALL_GENERATOR);
3174
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003175 /* Create a new generator that owns the ready to run frame
3176 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003177 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003178 }
3179
Thomas Woutersce272b62007-09-19 21:19:28 +00003180 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003181
Thomas Woutersce272b62007-09-19 21:19:28 +00003182fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003183
Tim Petersb13680b2001-11-27 23:29:29 +00003184 /* decref'ing the frame can cause __del__ methods to get invoked,
3185 which can call back into Python. While we're done with the
3186 current Python frame (f), the associated C stack is still in use,
3187 so recursion_depth must be boosted for the duration.
3188 */
3189 assert(tstate != NULL);
3190 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003191 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003192 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003193 return retval;
3194}
3195
3196
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003197/* Logic for the raise statement (too complicated for inlining).
3198 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003199static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003200do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003201{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003202 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003203
3204 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003205 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003206 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003207 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003208 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003209 value = tstate->exc_value;
3210 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003211 if (type == Py_None) {
3212 PyErr_SetString(PyExc_RuntimeError,
3213 "No active exception to reraise");
3214 return WHY_EXCEPTION;
3215 }
3216 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003217 Py_XINCREF(value);
3218 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003219 PyErr_Restore(type, value, tb);
3220 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003221 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003222
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003223 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003224 raise
3225 raise <instance>
3226 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003227
Collin Winter828f04a2007-08-31 00:04:24 +00003228 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003229 type = exc;
3230 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003231 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003232 goto raise_error;
3233 }
Collin Winter828f04a2007-08-31 00:04:24 +00003234 else if (PyExceptionInstance_Check(exc)) {
3235 value = exc;
3236 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003237 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003238 }
3239 else {
3240 /* Not something you can raise. You get an exception
3241 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003242 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003243 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003244 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003245 goto raise_error;
3246 }
Collin Winter828f04a2007-08-31 00:04:24 +00003247
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003248 if (cause) {
3249 PyObject *fixed_cause;
3250 if (PyExceptionClass_Check(cause)) {
3251 fixed_cause = PyObject_CallObject(cause, NULL);
3252 if (fixed_cause == NULL)
3253 goto raise_error;
3254 Py_DECREF(cause);
3255 }
3256 else if (PyExceptionInstance_Check(cause)) {
3257 fixed_cause = cause;
3258 }
3259 else {
3260 PyErr_SetString(PyExc_TypeError,
3261 "exception causes must derive from "
3262 "BaseException");
3263 goto raise_error;
3264 }
3265 PyException_SetCause(value, fixed_cause);
3266 }
Collin Winter828f04a2007-08-31 00:04:24 +00003267
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003268 PyErr_SetObject(type, value);
3269 /* PyErr_SetObject incref's its arguments */
3270 Py_XDECREF(value);
3271 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003272 return WHY_EXCEPTION;
3273
3274raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003275 Py_XDECREF(value);
3276 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003277 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003278 return WHY_EXCEPTION;
3279}
3280
Tim Petersd6d010b2001-06-21 02:49:55 +00003281/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003282 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003283
Guido van Rossum0368b722007-05-11 16:50:42 +00003284 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3285 with a variable target.
3286*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003287
Barry Warsawe42b18f1997-08-25 22:13:04 +00003288static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003289unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003290{
Guido van Rossum0368b722007-05-11 16:50:42 +00003291 int i = 0, j = 0;
3292 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003293 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003294 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003295 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003296
Tim Petersd6d010b2001-06-21 02:49:55 +00003297 assert(v != NULL);
3298
3299 it = PyObject_GetIter(v);
3300 if (it == NULL)
3301 goto Error;
3302
3303 for (; i < argcnt; i++) {
3304 w = PyIter_Next(it);
3305 if (w == NULL) {
3306 /* Iterator done, via error or exhaustion. */
3307 if (!PyErr_Occurred()) {
3308 PyErr_Format(PyExc_ValueError,
3309 "need more than %d value%s to unpack",
3310 i, i == 1 ? "" : "s");
3311 }
3312 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003313 }
3314 *--sp = w;
3315 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003316
Guido van Rossum0368b722007-05-11 16:50:42 +00003317 if (argcntafter == -1) {
3318 /* We better have exhausted the iterator now. */
3319 w = PyIter_Next(it);
3320 if (w == NULL) {
3321 if (PyErr_Occurred())
3322 goto Error;
3323 Py_DECREF(it);
3324 return 1;
3325 }
3326 Py_DECREF(w);
3327 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3328 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003329 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003330
3331 l = PySequence_List(it);
3332 if (l == NULL)
3333 goto Error;
3334 *--sp = l;
3335 i++;
3336
3337 ll = PyList_GET_SIZE(l);
3338 if (ll < argcntafter) {
3339 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3340 argcnt + ll);
3341 goto Error;
3342 }
3343
3344 /* Pop the "after-variable" args off the list. */
3345 for (j = argcntafter; j > 0; j--, i++) {
3346 *--sp = PyList_GET_ITEM(l, ll - j);
3347 }
3348 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003349 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003350 Py_DECREF(it);
3351 return 1;
3352
Tim Petersd6d010b2001-06-21 02:49:55 +00003353Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003354 for (; i > 0; i--, sp++)
3355 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003356 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003357 return 0;
3358}
3359
3360
Guido van Rossum96a42c81992-01-12 02:29:51 +00003361#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003362static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003363prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003364{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003365 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003366 if (PyObject_Print(v, stdout, 0) != 0)
3367 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003368 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003369 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003370}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003371#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003372
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003373static void
Fred Drake5755ce62001-06-27 19:19:46 +00003374call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003375{
Guido van Rossumb209a111997-04-29 18:18:01 +00003376 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003377 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003378 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003379 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003380 value = Py_None;
3381 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003382 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003383 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003384 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003385 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003386 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003387 }
Fred Drake5755ce62001-06-27 19:19:46 +00003388 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003389 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003390 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003391 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003392 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003393 Py_XDECREF(type);
3394 Py_XDECREF(value);
3395 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003396 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003397}
3398
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003399static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003400call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003401 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003402{
3403 PyObject *type, *value, *traceback;
3404 int err;
3405 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003406 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003407 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003408 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003409 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003410 return 0;
3411 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003412 else {
3413 Py_XDECREF(type);
3414 Py_XDECREF(value);
3415 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003416 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003417 }
3418}
3419
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003420static int
Fred Drake5755ce62001-06-27 19:19:46 +00003421call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3422 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003423{
Fred Drake5755ce62001-06-27 19:19:46 +00003424 register PyThreadState *tstate = frame->f_tstate;
3425 int result;
3426 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003427 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003428 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003429 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003430 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003431 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3432 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003433 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003434 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003435}
3436
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003437PyObject *
3438_PyEval_CallTracing(PyObject *func, PyObject *args)
3439{
3440 PyFrameObject *frame = PyEval_GetFrame();
3441 PyThreadState *tstate = frame->f_tstate;
3442 int save_tracing = tstate->tracing;
3443 int save_use_tracing = tstate->use_tracing;
3444 PyObject *result;
3445
3446 tstate->tracing = 0;
3447 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3448 || (tstate->c_profilefunc != NULL));
3449 result = PyObject_Call(func, args, NULL);
3450 tstate->tracing = save_tracing;
3451 tstate->use_tracing = save_use_tracing;
3452 return result;
3453}
3454
Michael W. Hudson006c7522002-11-08 13:08:46 +00003455static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003456maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003457 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3458 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003459{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003460 int result = 0;
3461
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003462 /* If the last instruction executed isn't in the current
3463 instruction window, reset the window. If the last
3464 instruction happens to fall at the start of a line or if it
3465 represents a jump backwards, call the trace function.
3466 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003467 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003468 int line;
3469 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003470
Thomas Woutersce272b62007-09-19 21:19:28 +00003471 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3472 &bounds);
3473 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003474 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003475 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003476 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003477 }
3478 *instr_lb = bounds.ap_lower;
3479 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003480 }
Armin Rigobf57a142004-03-22 19:24:58 +00003481 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003482 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003483 }
3484 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003485 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003486}
3487
Fred Drake5755ce62001-06-27 19:19:46 +00003488void
3489PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003490{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003491 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003492 PyObject *temp = tstate->c_profileobj;
3493 Py_XINCREF(arg);
3494 tstate->c_profilefunc = NULL;
3495 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003496 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003497 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003498 Py_XDECREF(temp);
3499 tstate->c_profilefunc = func;
3500 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003501 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003502 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003503}
3504
3505void
3506PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3507{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003508 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003509 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003510 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003511 Py_XINCREF(arg);
3512 tstate->c_tracefunc = NULL;
3513 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003514 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003515 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003516 Py_XDECREF(temp);
3517 tstate->c_tracefunc = func;
3518 tstate->c_traceobj = 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)
3521 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003522}
3523
Guido van Rossumb209a111997-04-29 18:18:01 +00003524PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003525PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003526{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003527 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003528 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003529 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003530 else
3531 return current_frame->f_builtins;
3532}
3533
Guido van Rossumb209a111997-04-29 18:18:01 +00003534PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003535PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003536{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003537 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003538 if (current_frame == NULL)
3539 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003540 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003541 return current_frame->f_locals;
3542}
3543
Guido van Rossumb209a111997-04-29 18:18:01 +00003544PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003545PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003546{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003547 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003548 if (current_frame == NULL)
3549 return NULL;
3550 else
3551 return current_frame->f_globals;
3552}
3553
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003554PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003555PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003556{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003557 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003558 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003559}
3560
Guido van Rossum6135a871995-01-09 17:53:26 +00003561int
Tim Peters5ba58662001-07-16 02:29:45 +00003562PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003563{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003564 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003565 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003566
3567 if (current_frame != NULL) {
3568 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003569 const int compilerflags = codeflags & PyCF_MASK;
3570 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003571 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003572 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003573 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003574#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003575 if (codeflags & CO_GENERATOR_ALLOWED) {
3576 result = 1;
3577 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3578 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003579#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003580 }
3581 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003582}
3583
Guido van Rossum3f5da241990-12-20 15:06:42 +00003584
Guido van Rossum681d79a1995-07-18 14:51:37 +00003585/* External interface to call any callable object.
3586 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003587
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003588#undef PyEval_CallObject
3589/* for backward compatibility: export this interface */
3590
Guido van Rossumb209a111997-04-29 18:18:01 +00003591PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003592PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003593{
Guido van Rossumb209a111997-04-29 18:18:01 +00003594 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003595}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003596#define PyEval_CallObject(func,arg) \
3597 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003598
Guido van Rossumb209a111997-04-29 18:18:01 +00003599PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003600PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003601{
Jeremy Hylton52820442001-01-03 23:52:36 +00003602 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003603
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003604 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003605 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003606 if (arg == NULL)
3607 return NULL;
3608 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003609 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003610 PyErr_SetString(PyExc_TypeError,
3611 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003612 return NULL;
3613 }
3614 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003615 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003616
Guido van Rossumb209a111997-04-29 18:18:01 +00003617 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003618 PyErr_SetString(PyExc_TypeError,
3619 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003620 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003621 return NULL;
3622 }
3623
Tim Peters6d6c1a32001-08-02 04:15:00 +00003624 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003625 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003626 return result;
3627}
3628
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003629const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003630PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003631{
3632 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003633 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003634 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003635 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003636 else if (PyCFunction_Check(func))
3637 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003638 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003639 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003640}
3641
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003642const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003643PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003644{
3645 if (PyMethod_Check(func))
3646 return "()";
3647 else if (PyFunction_Check(func))
3648 return "()";
3649 else if (PyCFunction_Check(func))
3650 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003651 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003652 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003653}
3654
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003655static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003656err_args(PyObject *func, int flags, int nargs)
3657{
3658 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003659 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003660 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003661 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003662 nargs);
3663 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003664 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003665 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003666 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003667 nargs);
3668}
3669
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003670#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003671if (tstate->use_tracing && tstate->c_profilefunc) { \
3672 if (call_trace(tstate->c_profilefunc, \
3673 tstate->c_profileobj, \
3674 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003675 func)) { \
3676 x = NULL; \
3677 } \
3678 else { \
3679 x = call; \
3680 if (tstate->c_profilefunc != NULL) { \
3681 if (x == NULL) { \
3682 call_trace_protected(tstate->c_profilefunc, \
3683 tstate->c_profileobj, \
3684 tstate->frame, PyTrace_C_EXCEPTION, \
3685 func); \
3686 /* XXX should pass (type, value, tb) */ \
3687 } else { \
3688 if (call_trace(tstate->c_profilefunc, \
3689 tstate->c_profileobj, \
3690 tstate->frame, PyTrace_C_RETURN, \
3691 func)) { \
3692 Py_DECREF(x); \
3693 x = NULL; \
3694 } \
3695 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003696 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003697 } \
3698} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003699 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003700 }
3701
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003702static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003703call_function(PyObject ***pp_stack, int oparg
3704#ifdef WITH_TSC
3705 , uint64* pintr0, uint64* pintr1
3706#endif
3707 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003708{
3709 int na = oparg & 0xff;
3710 int nk = (oparg>>8) & 0xff;
3711 int n = na + 2 * nk;
3712 PyObject **pfunc = (*pp_stack) - n - 1;
3713 PyObject *func = *pfunc;
3714 PyObject *x, *w;
3715
Jeremy Hylton985eba52003-02-05 23:13:00 +00003716 /* Always dispatch PyCFunction first, because these are
3717 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003718 */
3719 if (PyCFunction_Check(func) && nk == 0) {
3720 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003721 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003722
3723 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003724 if (flags & (METH_NOARGS | METH_O)) {
3725 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3726 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003727 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003728 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003729 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003730 else if (flags & METH_O && na == 1) {
3731 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003732 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003733 Py_DECREF(arg);
3734 }
3735 else {
3736 err_args(func, flags, na);
3737 x = NULL;
3738 }
3739 }
3740 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003741 PyObject *callargs;
3742 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003743 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003744 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003745 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003746 Py_XDECREF(callargs);
3747 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003748 } else {
3749 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3750 /* optimize access to bound methods */
3751 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003752 PCALL(PCALL_METHOD);
3753 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003754 Py_INCREF(self);
3755 func = PyMethod_GET_FUNCTION(func);
3756 Py_INCREF(func);
3757 Py_DECREF(*pfunc);
3758 *pfunc = self;
3759 na++;
3760 n++;
3761 } else
3762 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003763 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003764 if (PyFunction_Check(func))
3765 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003766 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003767 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003768 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003769 Py_DECREF(func);
3770 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003771
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003772 /* Clear the stack of the function object. Also removes
3773 the arguments in case they weren't consumed already
3774 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003775 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003776 while ((*pp_stack) > pfunc) {
3777 w = EXT_POP(*pp_stack);
3778 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003779 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003780 }
3781 return x;
3782}
3783
Jeremy Hylton192690e2002-08-16 18:36:11 +00003784/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003785 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003786 For the simplest case -- a function that takes only positional
3787 arguments and is called with only positional arguments -- it
3788 inlines the most primitive frame setup code from
3789 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3790 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003791*/
3792
3793static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003794fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003795{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003796 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003797 PyObject *globals = PyFunction_GET_GLOBALS(func);
3798 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003799 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003800 PyObject **d = NULL;
3801 int nd = 0;
3802
Jeremy Hylton985eba52003-02-05 23:13:00 +00003803 PCALL(PCALL_FUNCTION);
3804 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003805 if (argdefs == NULL && co->co_argcount == n &&
3806 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003807 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3808 PyFrameObject *f;
3809 PyObject *retval = NULL;
3810 PyThreadState *tstate = PyThreadState_GET();
3811 PyObject **fastlocals, **stack;
3812 int i;
3813
3814 PCALL(PCALL_FASTER_FUNCTION);
3815 assert(globals != NULL);
3816 /* XXX Perhaps we should create a specialized
3817 PyFrame_New() that doesn't take locals, but does
3818 take builtins without sanity checking them.
3819 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003820 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003821 f = PyFrame_New(tstate, co, globals, NULL);
3822 if (f == NULL)
3823 return NULL;
3824
3825 fastlocals = f->f_localsplus;
3826 stack = (*pp_stack) - n;
3827
3828 for (i = 0; i < n; i++) {
3829 Py_INCREF(*stack);
3830 fastlocals[i] = *stack++;
3831 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003832 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003833 ++tstate->recursion_depth;
3834 Py_DECREF(f);
3835 --tstate->recursion_depth;
3836 return retval;
3837 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003838 if (argdefs != NULL) {
3839 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003840 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003841 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003842 return PyEval_EvalCodeEx(co, globals,
3843 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003844 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003845 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003846}
3847
3848static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003849update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3850 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003851{
3852 PyObject *kwdict = NULL;
3853 if (orig_kwdict == NULL)
3854 kwdict = PyDict_New();
3855 else {
3856 kwdict = PyDict_Copy(orig_kwdict);
3857 Py_DECREF(orig_kwdict);
3858 }
3859 if (kwdict == NULL)
3860 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003861 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003862 int err;
3863 PyObject *value = EXT_POP(*pp_stack);
3864 PyObject *key = EXT_POP(*pp_stack);
3865 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003866 PyErr_Format(PyExc_TypeError,
3867 "%.200s%s got multiple values "
3868 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003869 PyEval_GetFuncName(func),
3870 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003871 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003872 Py_DECREF(key);
3873 Py_DECREF(value);
3874 Py_DECREF(kwdict);
3875 return NULL;
3876 }
3877 err = PyDict_SetItem(kwdict, key, value);
3878 Py_DECREF(key);
3879 Py_DECREF(value);
3880 if (err) {
3881 Py_DECREF(kwdict);
3882 return NULL;
3883 }
3884 }
3885 return kwdict;
3886}
3887
3888static PyObject *
3889update_star_args(int nstack, int nstar, PyObject *stararg,
3890 PyObject ***pp_stack)
3891{
3892 PyObject *callargs, *w;
3893
3894 callargs = PyTuple_New(nstack + nstar);
3895 if (callargs == NULL) {
3896 return NULL;
3897 }
3898 if (nstar) {
3899 int i;
3900 for (i = 0; i < nstar; i++) {
3901 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3902 Py_INCREF(a);
3903 PyTuple_SET_ITEM(callargs, nstack + i, a);
3904 }
3905 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003906 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003907 w = EXT_POP(*pp_stack);
3908 PyTuple_SET_ITEM(callargs, nstack, w);
3909 }
3910 return callargs;
3911}
3912
3913static PyObject *
3914load_args(PyObject ***pp_stack, int na)
3915{
3916 PyObject *args = PyTuple_New(na);
3917 PyObject *w;
3918
3919 if (args == NULL)
3920 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003921 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003922 w = EXT_POP(*pp_stack);
3923 PyTuple_SET_ITEM(args, na, w);
3924 }
3925 return args;
3926}
3927
3928static PyObject *
3929do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3930{
3931 PyObject *callargs = NULL;
3932 PyObject *kwdict = NULL;
3933 PyObject *result = NULL;
3934
3935 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003936 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003937 if (kwdict == NULL)
3938 goto call_fail;
3939 }
3940 callargs = load_args(pp_stack, na);
3941 if (callargs == NULL)
3942 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003943#ifdef CALL_PROFILE
3944 /* At this point, we have to look at the type of func to
3945 update the call stats properly. Do it here so as to avoid
3946 exposing the call stats machinery outside ceval.c
3947 */
3948 if (PyFunction_Check(func))
3949 PCALL(PCALL_FUNCTION);
3950 else if (PyMethod_Check(func))
3951 PCALL(PCALL_METHOD);
3952 else if (PyType_Check(func))
3953 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00003954 else if (PyCFunction_Check(func))
3955 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003956 else
3957 PCALL(PCALL_OTHER);
3958#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00003959 if (PyCFunction_Check(func)) {
3960 PyThreadState *tstate = PyThreadState_GET();
3961 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
3962 }
3963 else
3964 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003965call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003966 Py_XDECREF(callargs);
3967 Py_XDECREF(kwdict);
3968 return result;
3969}
3970
3971static PyObject *
3972ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3973{
3974 int nstar = 0;
3975 PyObject *callargs = NULL;
3976 PyObject *stararg = NULL;
3977 PyObject *kwdict = NULL;
3978 PyObject *result = NULL;
3979
3980 if (flags & CALL_FLAG_KW) {
3981 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003982 if (!PyDict_Check(kwdict)) {
3983 PyObject *d;
3984 d = PyDict_New();
3985 if (d == NULL)
3986 goto ext_call_fail;
3987 if (PyDict_Update(d, kwdict) != 0) {
3988 Py_DECREF(d);
3989 /* PyDict_Update raises attribute
3990 * error (percolated from an attempt
3991 * to get 'keys' attribute) instead of
3992 * a type error if its second argument
3993 * is not a mapping.
3994 */
3995 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3996 PyErr_Format(PyExc_TypeError,
3997 "%.200s%.200s argument after ** "
3998 "must be a mapping, not %.200s",
3999 PyEval_GetFuncName(func),
4000 PyEval_GetFuncDesc(func),
4001 kwdict->ob_type->tp_name);
4002 }
4003 goto ext_call_fail;
4004 }
4005 Py_DECREF(kwdict);
4006 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004007 }
4008 }
4009 if (flags & CALL_FLAG_VAR) {
4010 stararg = EXT_POP(*pp_stack);
4011 if (!PyTuple_Check(stararg)) {
4012 PyObject *t = NULL;
4013 t = PySequence_Tuple(stararg);
4014 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004015 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4016 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004017 "%.200s%.200s argument after * "
4018 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004019 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004020 PyEval_GetFuncDesc(func),
4021 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004022 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004023 goto ext_call_fail;
4024 }
4025 Py_DECREF(stararg);
4026 stararg = t;
4027 }
4028 nstar = PyTuple_GET_SIZE(stararg);
4029 }
4030 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004031 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004032 if (kwdict == NULL)
4033 goto ext_call_fail;
4034 }
4035 callargs = update_star_args(na, nstar, stararg, pp_stack);
4036 if (callargs == NULL)
4037 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004038#ifdef CALL_PROFILE
4039 /* At this point, we have to look at the type of func to
4040 update the call stats properly. Do it here so as to avoid
4041 exposing the call stats machinery outside ceval.c
4042 */
4043 if (PyFunction_Check(func))
4044 PCALL(PCALL_FUNCTION);
4045 else if (PyMethod_Check(func))
4046 PCALL(PCALL_METHOD);
4047 else if (PyType_Check(func))
4048 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004049 else if (PyCFunction_Check(func))
4050 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004051 else
4052 PCALL(PCALL_OTHER);
4053#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004054 if (PyCFunction_Check(func)) {
4055 PyThreadState *tstate = PyThreadState_GET();
4056 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4057 }
4058 else
4059 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004060ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004061 Py_XDECREF(callargs);
4062 Py_XDECREF(kwdict);
4063 Py_XDECREF(stararg);
4064 return result;
4065}
4066
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004067/* Extract a slice index from a PyInt or PyLong or an object with the
4068 nb_index slot defined, and store in *pi.
4069 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4070 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 +00004071 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004072*/
Tim Petersb5196382001-12-16 19:44:20 +00004073/* Note: If v is NULL, return success without storing into *pi. This
4074 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4075 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004076*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004077int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004078_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004079{
Tim Petersb5196382001-12-16 19:44:20 +00004080 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004081 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004082 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004083 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004084 if (x == -1 && PyErr_Occurred())
4085 return 0;
4086 }
4087 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004088 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004089 "slice indices must be integers or "
4090 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004091 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004092 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004093 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004094 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004095 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004096}
4097
Guido van Rossum486364b2007-06-30 05:01:58 +00004098#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004099 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004100
Guido van Rossumb209a111997-04-29 18:18:01 +00004101static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004102cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004103{
Guido van Rossumac7be682001-01-17 15:42:30 +00004104 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004105 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004106 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004107 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004108 break;
4109 case PyCmp_IS_NOT:
4110 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004111 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004112 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004113 res = PySequence_Contains(w, v);
4114 if (res < 0)
4115 return NULL;
4116 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004117 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004118 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004119 if (res < 0)
4120 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004121 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004122 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004123 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004124 if (PyTuple_Check(w)) {
4125 Py_ssize_t i, length;
4126 length = PyTuple_Size(w);
4127 for (i = 0; i < length; i += 1) {
4128 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004129 if (!PyExceptionClass_Check(exc)) {
4130 PyErr_SetString(PyExc_TypeError,
4131 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004132 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004133 }
4134 }
4135 }
4136 else {
Brett Cannon39590462007-02-26 22:01:14 +00004137 if (!PyExceptionClass_Check(w)) {
4138 PyErr_SetString(PyExc_TypeError,
4139 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004140 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004141 }
4142 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004143 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004144 break;
4145 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004146 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004147 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004148 v = res ? Py_True : Py_False;
4149 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004150 return v;
4151}
4152
Thomas Wouters52152252000-08-17 22:55:00 +00004153static PyObject *
4154import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004155{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004156 PyObject *x;
4157
4158 x = PyObject_GetAttr(v, name);
4159 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004160 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004161 }
Thomas Wouters52152252000-08-17 22:55:00 +00004162 return x;
4163}
Guido van Rossumac7be682001-01-17 15:42:30 +00004164
Thomas Wouters52152252000-08-17 22:55:00 +00004165static int
4166import_all_from(PyObject *locals, PyObject *v)
4167{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004168 PyObject *all = PyObject_GetAttrString(v, "__all__");
4169 PyObject *dict, *name, *value;
4170 int skip_leading_underscores = 0;
4171 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004172
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004173 if (all == NULL) {
4174 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4175 return -1; /* Unexpected error */
4176 PyErr_Clear();
4177 dict = PyObject_GetAttrString(v, "__dict__");
4178 if (dict == NULL) {
4179 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4180 return -1;
4181 PyErr_SetString(PyExc_ImportError,
4182 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004183 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004184 }
4185 all = PyMapping_Keys(dict);
4186 Py_DECREF(dict);
4187 if (all == NULL)
4188 return -1;
4189 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004190 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004191
4192 for (pos = 0, err = 0; ; pos++) {
4193 name = PySequence_GetItem(all, pos);
4194 if (name == NULL) {
4195 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4196 err = -1;
4197 else
4198 PyErr_Clear();
4199 break;
4200 }
4201 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004202 PyUnicode_Check(name) &&
4203 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004204 {
4205 Py_DECREF(name);
4206 continue;
4207 }
4208 value = PyObject_GetAttr(v, name);
4209 if (value == NULL)
4210 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004211 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004212 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004213 else
4214 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004215 Py_DECREF(name);
4216 Py_XDECREF(value);
4217 if (err != 0)
4218 break;
4219 }
4220 Py_DECREF(all);
4221 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004222}
4223
Guido van Rossumac7be682001-01-17 15:42:30 +00004224static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004225format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004226{
Neal Norwitzda059e32007-08-26 05:33:45 +00004227 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004228
4229 if (!obj)
4230 return;
4231
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004232 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004233 if (!obj_str)
4234 return;
4235
4236 PyErr_Format(exc, format_str, obj_str);
4237}
Guido van Rossum950361c1997-01-24 13:49:28 +00004238
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004239static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004240unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004241 PyFrameObject *f, unsigned char *next_instr)
4242{
4243 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004244 are (Unicode) strings. */
4245 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4246 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004247 Py_ssize_t new_len = v_len + w_len;
4248 if (new_len < 0) {
4249 PyErr_SetString(PyExc_OverflowError,
4250 "strings are too large to concat");
4251 return NULL;
4252 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004253
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004254 if (v->ob_refcnt == 2) {
4255 /* In the common case, there are 2 references to the value
4256 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004257 * value stack (in 'v') and one still stored in the
4258 * 'variable'. We try to delete the variable now to reduce
4259 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004260 */
4261 switch (*next_instr) {
4262 case STORE_FAST:
4263 {
4264 int oparg = PEEKARG();
4265 PyObject **fastlocals = f->f_localsplus;
4266 if (GETLOCAL(oparg) == v)
4267 SETLOCAL(oparg, NULL);
4268 break;
4269 }
4270 case STORE_DEREF:
4271 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004272 PyObject **freevars = (f->f_localsplus +
4273 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004274 PyObject *c = freevars[PEEKARG()];
4275 if (PyCell_GET(c) == v)
4276 PyCell_Set(c, NULL);
4277 break;
4278 }
4279 case STORE_NAME:
4280 {
4281 PyObject *names = f->f_code->co_names;
4282 PyObject *name = GETITEM(names, PEEKARG());
4283 PyObject *locals = f->f_locals;
4284 if (PyDict_CheckExact(locals) &&
4285 PyDict_GetItem(locals, name) == v) {
4286 if (PyDict_DelItem(locals, name) != 0) {
4287 PyErr_Clear();
4288 }
4289 }
4290 break;
4291 }
4292 }
4293 }
4294
Guido van Rossum98297ee2007-11-06 21:34:58 +00004295 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004296 /* Now we own the last reference to 'v', so we can resize it
4297 * in-place.
4298 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004299 if (PyUnicode_Resize(&v, new_len) != 0) {
4300 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004301 * deallocated so it cannot be put back into
4302 * 'variable'. The MemoryError is raised when there
4303 * is no value in 'variable', which might (very
4304 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004305 */
4306 return NULL;
4307 }
4308 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004309 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4310 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004311 return v;
4312 }
4313 else {
4314 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004315 w = PyUnicode_Concat(v, w);
4316 Py_DECREF(v);
4317 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004318 }
4319}
4320
Guido van Rossum950361c1997-01-24 13:49:28 +00004321#ifdef DYNAMIC_EXECUTION_PROFILE
4322
Skip Montanarof118cb12001-10-15 20:51:38 +00004323static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004324getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004325{
4326 int i;
4327 PyObject *l = PyList_New(256);
4328 if (l == NULL) return NULL;
4329 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004330 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004331 if (x == NULL) {
4332 Py_DECREF(l);
4333 return NULL;
4334 }
4335 PyList_SetItem(l, i, x);
4336 }
4337 for (i = 0; i < 256; i++)
4338 a[i] = 0;
4339 return l;
4340}
4341
4342PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004343_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004344{
4345#ifndef DXPAIRS
4346 return getarray(dxp);
4347#else
4348 int i;
4349 PyObject *l = PyList_New(257);
4350 if (l == NULL) return NULL;
4351 for (i = 0; i < 257; i++) {
4352 PyObject *x = getarray(dxpairs[i]);
4353 if (x == NULL) {
4354 Py_DECREF(l);
4355 return NULL;
4356 }
4357 PyList_SetItem(l, i, x);
4358 }
4359 return l;
4360#endif
4361}
4362
4363#endif