blob: e0ce92e3f5c52270f2d031788f4dbdf20072b6b0 [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
Raymond Hettingerf606f872003-03-16 03:11:04 +00001298 PREDICTED(POP_TOP);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001299 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001300 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001301 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001302 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001303
Antoine Pitroub52ec782009-01-25 16:34:23 +00001304 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001305 v = TOP();
1306 w = SECOND();
1307 SET_TOP(w);
1308 SET_SECOND(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001309 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001310
Antoine Pitroub52ec782009-01-25 16:34:23 +00001311 TARGET(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001312 v = TOP();
1313 w = SECOND();
1314 x = THIRD();
1315 SET_TOP(w);
1316 SET_SECOND(x);
1317 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001318 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Antoine Pitroub52ec782009-01-25 16:34:23 +00001320 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001321 u = TOP();
1322 v = SECOND();
1323 w = THIRD();
1324 x = FOURTH();
1325 SET_TOP(v);
1326 SET_SECOND(w);
1327 SET_THIRD(x);
1328 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001329 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Antoine Pitroub52ec782009-01-25 16:34:23 +00001331 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001332 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001333 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001334 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001335 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Antoine Pitroub52ec782009-01-25 16:34:23 +00001337 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001338 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001339 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001340 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001342 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001343 STACKADJ(2);
1344 SET_TOP(x);
1345 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001346 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001347 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001348 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001349 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001350 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001351 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001352 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001353 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001354 STACKADJ(3);
1355 SET_TOP(x);
1356 SET_SECOND(w);
1357 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001358 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001359 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001360 Py_FatalError("invalid argument to DUP_TOPX"
1361 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001362 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001363 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001364
Antoine Pitroub52ec782009-01-25 16:34:23 +00001365 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001366 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001367 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001368 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001369 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001370 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001371 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Antoine Pitroub52ec782009-01-25 16:34:23 +00001373 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001375 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001376 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001377 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001378 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001379 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Antoine Pitroub52ec782009-01-25 16:34:23 +00001381 TARGET(UNARY_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001382 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001383 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001384 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001385 if (err == 0) {
1386 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001387 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001388 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001389 }
1390 else if (err > 0) {
1391 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001392 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001393 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001394 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001395 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001396 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Antoine Pitroub52ec782009-01-25 16:34:23 +00001399 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001400 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001401 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001402 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001403 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001404 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001405 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Antoine Pitroub52ec782009-01-25 16:34:23 +00001407 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001408 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001409 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001410 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001411 Py_DECREF(v);
1412 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001413 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001414 if (x != NULL) DISPATCH();
Guido van Rossum50564e81996-01-12 01:13:16 +00001415 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001416
Antoine Pitroub52ec782009-01-25 16:34:23 +00001417 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001418 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001419 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001420 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001421 Py_DECREF(v);
1422 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001423 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001424 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001425 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Antoine Pitroub52ec782009-01-25 16:34:23 +00001427 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001428 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001429 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001430 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001431 Py_DECREF(v);
1432 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001433 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001434 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001435 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001436
Antoine Pitroub52ec782009-01-25 16:34:23 +00001437 TARGET(BINARY_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001438 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001439 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001440 x = PyNumber_FloorDivide(v, w);
1441 Py_DECREF(v);
1442 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001443 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001444 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001445 break;
1446
Antoine Pitroub52ec782009-01-25 16:34:23 +00001447 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001448 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001449 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001450 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001451 Py_DECREF(v);
1452 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001453 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001454 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001455 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Antoine Pitroub52ec782009-01-25 16:34:23 +00001457 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001458 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001459 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001460 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001461 PyUnicode_CheckExact(w)) {
1462 x = unicode_concatenate(v, w, f, next_instr);
1463 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001464 goto skip_decref_vx;
1465 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001466 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001467 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001468 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001469 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001470 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001471 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001472 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001473 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001474 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Antoine Pitroub52ec782009-01-25 16:34:23 +00001476 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001477 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001478 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001479 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001480 Py_DECREF(v);
1481 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001482 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001483 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001484 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Antoine Pitroub52ec782009-01-25 16:34:23 +00001486 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001487 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001488 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001489 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001490 Py_DECREF(v);
1491 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001492 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001493 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001494 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Antoine Pitroub52ec782009-01-25 16:34:23 +00001496 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001497 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001498 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001499 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001500 Py_DECREF(v);
1501 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001502 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001503 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001504 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001505
Antoine Pitroub52ec782009-01-25 16:34:23 +00001506 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001507 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001508 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001509 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001510 Py_DECREF(v);
1511 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001512 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001513 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Antoine Pitroub52ec782009-01-25 16:34:23 +00001516 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001517 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001518 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001519 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001520 Py_DECREF(v);
1521 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001522 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001523 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001524 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Antoine Pitroub52ec782009-01-25 16:34:23 +00001526 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001527 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001528 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001529 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001530 Py_DECREF(v);
1531 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001532 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001533 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001534 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Antoine Pitroub52ec782009-01-25 16:34:23 +00001536 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001537 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001538 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001539 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001540 Py_DECREF(v);
1541 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001542 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001543 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001544 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001545
Antoine Pitroub52ec782009-01-25 16:34:23 +00001546 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001547 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001548 v = stack_pointer[-oparg];
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001549 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001550 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001551 if (err == 0) {
1552 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001553 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001554 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001555 break;
1556
Antoine Pitroub52ec782009-01-25 16:34:23 +00001557 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001558 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001559 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001560 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001561 Py_DECREF(w);
1562 if (err == 0) {
1563 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001564 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001565 }
1566 break;
1567
Antoine Pitroub52ec782009-01-25 16:34:23 +00001568 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001569 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001570 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001571 x = PyNumber_InPlacePower(v, w, Py_None);
1572 Py_DECREF(v);
1573 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001574 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001575 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001576 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001577
Antoine Pitroub52ec782009-01-25 16:34:23 +00001578 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001579 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001580 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001581 x = PyNumber_InPlaceMultiply(v, w);
1582 Py_DECREF(v);
1583 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001584 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001585 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001586 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Antoine Pitroub52ec782009-01-25 16:34:23 +00001588 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001589 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001590 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001591 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001592 Py_DECREF(v);
1593 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001594 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001595 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001596 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Antoine Pitroub52ec782009-01-25 16:34:23 +00001598 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001599 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001600 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001601 x = PyNumber_InPlaceFloorDivide(v, w);
1602 Py_DECREF(v);
1603 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001604 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001605 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001606 break;
1607
Antoine Pitroub52ec782009-01-25 16:34:23 +00001608 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001609 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001610 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001611 x = PyNumber_InPlaceRemainder(v, w);
1612 Py_DECREF(v);
1613 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001614 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001615 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001616 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Antoine Pitroub52ec782009-01-25 16:34:23 +00001618 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001619 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001620 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001621 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001622 PyUnicode_CheckExact(w)) {
1623 x = unicode_concatenate(v, w, f, next_instr);
1624 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001625 goto skip_decref_v;
1626 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001627 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001628 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001629 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001630 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001631 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001632 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001633 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001634 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001635 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001636
Antoine Pitroub52ec782009-01-25 16:34:23 +00001637 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001638 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001639 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001640 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001641 Py_DECREF(v);
1642 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001643 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001644 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001645 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001646
Antoine Pitroub52ec782009-01-25 16:34:23 +00001647 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001648 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001649 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001650 x = PyNumber_InPlaceLshift(v, w);
1651 Py_DECREF(v);
1652 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001653 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001654 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001655 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Antoine Pitroub52ec782009-01-25 16:34:23 +00001657 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001658 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001659 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001660 x = PyNumber_InPlaceRshift(v, w);
1661 Py_DECREF(v);
1662 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001663 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001664 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001665 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001666
Antoine Pitroub52ec782009-01-25 16:34:23 +00001667 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001668 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001669 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001670 x = PyNumber_InPlaceAnd(v, w);
1671 Py_DECREF(v);
1672 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001673 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001674 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001675 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Antoine Pitroub52ec782009-01-25 16:34:23 +00001677 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001678 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001679 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001680 x = PyNumber_InPlaceXor(v, w);
1681 Py_DECREF(v);
1682 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001683 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001684 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001685 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Antoine Pitroub52ec782009-01-25 16:34:23 +00001687 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001688 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001689 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001690 x = PyNumber_InPlaceOr(v, w);
1691 Py_DECREF(v);
1692 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001693 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001694 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001695 break;
1696
Antoine Pitroub52ec782009-01-25 16:34:23 +00001697 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001698 w = TOP();
1699 v = SECOND();
1700 u = THIRD();
1701 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001702 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001703 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001704 Py_DECREF(u);
1705 Py_DECREF(v);
1706 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001707 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001708 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Antoine Pitroub52ec782009-01-25 16:34:23 +00001710 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001711 w = TOP();
1712 v = SECOND();
1713 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001714 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001715 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001716 Py_DECREF(v);
1717 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001718 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001719 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001720
Antoine Pitroub52ec782009-01-25 16:34:23 +00001721 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001722 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001723 w = PySys_GetObject("displayhook");
1724 if (w == NULL) {
1725 PyErr_SetString(PyExc_RuntimeError,
1726 "lost sys.displayhook");
1727 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001728 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001729 }
1730 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001731 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001732 if (x == NULL)
1733 err = -1;
1734 }
1735 if (err == 0) {
1736 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001737 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001738 if (w == NULL)
1739 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001740 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001741 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001742 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001743 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001744
Thomas Wouters434d0822000-08-24 20:11:32 +00001745#ifdef CASE_TOO_BIG
1746 default: switch (opcode) {
1747#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001748 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001749 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001750 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001751 case 2:
1752 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001753 case 1:
1754 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001755 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001756 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001757 break;
1758 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001759 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001760 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001761 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001762 break;
1763 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001764 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001765
Antoine Pitroub52ec782009-01-25 16:34:23 +00001766 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001767 x = POP();
1768 v = f->f_locals;
1769 Py_XDECREF(v);
1770 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001771 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001772
Antoine Pitroub52ec782009-01-25 16:34:23 +00001773 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001774 retval = POP();
1775 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001776 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001777
Antoine Pitroub52ec782009-01-25 16:34:23 +00001778 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001779 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001780 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001781 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001782 /* Put aside the current exception state and restore
1783 that of the calling frame. This only serves when
1784 "yield" is used inside an except handler. */
1785 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001786 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001787
Antoine Pitroub52ec782009-01-25 16:34:23 +00001788 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001789 {
1790 PyTryBlock *b = PyFrame_BlockPop(f);
1791 if (b->b_type != EXCEPT_HANDLER) {
1792 PyErr_SetString(PyExc_SystemError,
1793 "popped block is not an except handler");
1794 why = WHY_EXCEPTION;
1795 break;
1796 }
1797 UNWIND_EXCEPT_HANDLER(b);
1798 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001799 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001800
Antoine Pitroub52ec782009-01-25 16:34:23 +00001801 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001802 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001803 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001804 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001805 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001806 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001807
Christian Heimes180510d2008-03-03 19:15:45 +00001808 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001809 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001810 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001811 if (PyLong_Check(v)) {
1812 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001813 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001814 if (why == WHY_RETURN ||
1815 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001816 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001817 if (why == WHY_SILENCED) {
1818 /* An exception was silenced by 'with', we must
1819 manually unwind the EXCEPT_HANDLER block which was
1820 created when the exception was caught, otherwise
1821 the stack will be in an inconsistent state. */
1822 PyTryBlock *b = PyFrame_BlockPop(f);
1823 if (b->b_type != EXCEPT_HANDLER) {
1824 PyErr_SetString(PyExc_SystemError,
1825 "popped block is not an except handler");
1826 why = WHY_EXCEPTION;
1827 }
1828 else {
1829 UNWIND_EXCEPT_HANDLER(b);
1830 why = WHY_NOT;
1831 }
1832 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001833 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001834 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001835 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001836 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001837 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001838 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001839 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001840 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001841 else if (v != Py_None) {
1842 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001843 "'finally' pops bad exception");
1844 why = WHY_EXCEPTION;
1845 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001846 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001847 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001848
Antoine Pitroub52ec782009-01-25 16:34:23 +00001849 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001850 x = PyDict_GetItemString(f->f_builtins,
1851 "__build_class__");
1852 if (x == NULL) {
1853 PyErr_SetString(PyExc_ImportError,
1854 "__build_class__ not found");
1855 break;
1856 }
1857 Py_INCREF(x);
1858 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001859 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001860
Antoine Pitroub52ec782009-01-25 16:34:23 +00001861 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001862 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001863 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001864 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001865 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001866 err = PyDict_SetItem(x, w, v);
1867 else
1868 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001869 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001870 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001871 break;
1872 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001873 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001874 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001875 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001876
Antoine Pitroub52ec782009-01-25 16:34:23 +00001877 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001878 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001879 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001880 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001881 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001882 NAME_ERROR_MSG,
1883 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001884 break;
1885 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001886 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001887 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001888 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001889
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001890 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001891 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001893 if (PyTuple_CheckExact(v) &&
1894 PyTuple_GET_SIZE(v) == oparg) {
1895 PyObject **items = \
1896 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001897 while (oparg--) {
1898 w = items[oparg];
1899 Py_INCREF(w);
1900 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001901 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001902 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001903 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001904 } else if (PyList_CheckExact(v) &&
1905 PyList_GET_SIZE(v) == oparg) {
1906 PyObject **items = \
1907 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001908 while (oparg--) {
1909 w = items[oparg];
1910 Py_INCREF(w);
1911 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001912 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001913 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001914 stack_pointer + oparg)) {
Tim Petersd6d010b2001-06-21 02:49:55 +00001915 stack_pointer += oparg;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001916 } else {
1917 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001918 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001919 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001920 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001921 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001922
Antoine Pitroub52ec782009-01-25 16:34:23 +00001923 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00001924 {
1925 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1926 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001927
Guido van Rossum0368b722007-05-11 16:50:42 +00001928 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1929 stack_pointer + totalargs)) {
1930 stack_pointer += totalargs;
1931 } else {
1932 why = WHY_EXCEPTION;
1933 }
1934 Py_DECREF(v);
1935 break;
1936 }
1937
Antoine Pitroub52ec782009-01-25 16:34:23 +00001938 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001939 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001940 v = TOP();
1941 u = SECOND();
1942 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001943 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1944 Py_DECREF(v);
1945 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001946 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001947 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001948
Antoine Pitroub52ec782009-01-25 16:34:23 +00001949 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001950 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001951 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001952 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1953 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001954 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001955 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001956
Antoine Pitroub52ec782009-01-25 16:34:23 +00001957 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001958 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001959 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001960 err = PyDict_SetItem(f->f_globals, w, v);
1961 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001962 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001963 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001964
Antoine Pitroub52ec782009-01-25 16:34:23 +00001965 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001966 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001967 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001968 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001969 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001970 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001971
Antoine Pitroub52ec782009-01-25 16:34:23 +00001972 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001973 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001974 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001975 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001976 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001977 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001978 break;
1979 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001980 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001981 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001982 Py_XINCREF(x);
1983 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001984 else {
1985 x = PyObject_GetItem(v, w);
1986 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001987 if (!PyErr_ExceptionMatches(
1988 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001989 break;
1990 PyErr_Clear();
1991 }
1992 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001993 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00001994 x = PyDict_GetItem(f->f_globals, w);
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_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001997 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00001998 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00001999 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002000 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002001 break;
2002 }
2003 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002004 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002005 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002006 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002007 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002008
Antoine Pitroub52ec782009-01-25 16:34:23 +00002009 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002010 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002011 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002012 /* Inline the PyDict_GetItem() calls.
2013 WARNING: this is an extreme speed hack.
2014 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002015 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002016 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002017 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002018 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002019 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002020 e = d->ma_lookup(d, w, hash);
2021 if (e == NULL) {
2022 x = NULL;
2023 break;
2024 }
2025 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002026 if (x != NULL) {
2027 Py_INCREF(x);
2028 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002029 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002030 }
2031 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002032 e = d->ma_lookup(d, w, hash);
2033 if (e == NULL) {
2034 x = NULL;
2035 break;
2036 }
2037 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002038 if (x != NULL) {
2039 Py_INCREF(x);
2040 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002041 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002042 }
2043 goto load_global_error;
2044 }
2045 }
2046 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002047 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002048 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002050 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002051 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002052 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002053 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002054 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002055 break;
2056 }
2057 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002058 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002059 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002060 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002061
Antoine Pitroub52ec782009-01-25 16:34:23 +00002062 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002063 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002064 if (x != NULL) {
2065 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002066 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002067 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002068 format_exc_check_arg(
2069 PyExc_UnboundLocalError,
2070 UNBOUNDLOCAL_ERROR_MSG,
2071 PyTuple_GetItem(co->co_varnames, oparg)
2072 );
2073 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002074
Antoine Pitroub52ec782009-01-25 16:34:23 +00002075 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002076 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002077 Py_INCREF(x);
2078 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002079 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002080 break;
2081
Antoine Pitroub52ec782009-01-25 16:34:23 +00002082 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002083 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002084 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002085 if (w != NULL) {
2086 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002087 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002088 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002089 err = -1;
2090 /* Don't stomp existing exception */
2091 if (PyErr_Occurred())
2092 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002093 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2094 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002095 oparg);
2096 format_exc_check_arg(
2097 PyExc_UnboundLocalError,
2098 UNBOUNDLOCAL_ERROR_MSG,
2099 v);
2100 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002101 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2102 PyTuple_GET_SIZE(co->co_cellvars));
2103 format_exc_check_arg(PyExc_NameError,
2104 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002105 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002106 break;
2107
Antoine Pitroub52ec782009-01-25 16:34:23 +00002108 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002109 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002110 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002111 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002112 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002113 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002114
Antoine Pitroub52ec782009-01-25 16:34:23 +00002115 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002116 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002117 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002118 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002119 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002120 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002121 }
2122 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002123 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002124 }
2125 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002126
Antoine Pitroub52ec782009-01-25 16:34:23 +00002127 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002128 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002129 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002130 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002131 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002132 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002133 }
2134 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002135 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002136 }
2137 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002138
Antoine Pitroub52ec782009-01-25 16:34:23 +00002139 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002140 x = PySet_New(NULL);
2141 if (x != NULL) {
2142 for (; --oparg >= 0;) {
2143 w = POP();
2144 if (err == 0)
2145 err = PySet_Add(x, w);
2146 Py_DECREF(w);
2147 }
2148 if (err != 0) {
2149 Py_DECREF(x);
2150 break;
2151 }
2152 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002153 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002154 }
2155 break;
2156
Antoine Pitroub52ec782009-01-25 16:34:23 +00002157 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002158 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002159 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002160 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002161 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002162
Antoine Pitroub52ec782009-01-25 16:34:23 +00002163 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002164 w = TOP(); /* key */
2165 u = SECOND(); /* value */
2166 v = THIRD(); /* dict */
2167 STACKADJ(-2);
2168 assert (PyDict_CheckExact(v));
2169 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2170 Py_DECREF(u);
2171 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002172 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002173 break;
2174
Antoine Pitroub52ec782009-01-25 16:34:23 +00002175 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002176 w = TOP(); /* key */
2177 u = SECOND(); /* value */
2178 STACKADJ(-2);
2179 v = stack_pointer[-oparg]; /* dict */
2180 assert (PyDict_CheckExact(v));
2181 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2182 Py_DECREF(u);
2183 Py_DECREF(w);
2184 if (err == 0) {
2185 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002186 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002187 }
2188 break;
2189
Antoine Pitroub52ec782009-01-25 16:34:23 +00002190 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002191 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002192 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002193 x = PyObject_GetAttr(v, w);
2194 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002195 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002196 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002197 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002198
Antoine Pitroub52ec782009-01-25 16:34:23 +00002199 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002200 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002201 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002202 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002203 Py_DECREF(v);
2204 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002205 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002206 if (x == NULL) break;
2207 PREDICT(JUMP_IF_FALSE);
2208 PREDICT(JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002209 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002210
Antoine Pitroub52ec782009-01-25 16:34:23 +00002211 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002212 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002213 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002214 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002215 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002216 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002217 break;
2218 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002219 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002220 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002221 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002222 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002223 w = PyTuple_Pack(5,
2224 w,
2225 f->f_globals,
2226 f->f_locals == NULL ?
2227 Py_None : f->f_locals,
2228 v,
2229 u);
2230 else
2231 w = PyTuple_Pack(4,
2232 w,
2233 f->f_globals,
2234 f->f_locals == NULL ?
2235 Py_None : f->f_locals,
2236 v);
2237 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002238 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002239 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002240 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002241 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002242 x = NULL;
2243 break;
2244 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002245 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002246 v = x;
2247 x = PyEval_CallObject(v, w);
2248 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002249 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002250 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002251 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002252 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002253 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002254
Antoine Pitroub52ec782009-01-25 16:34:23 +00002255 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002256 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002257 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002258 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002259 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002260 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002261 break;
2262 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002263 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002264 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002265 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002266 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002267 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002268 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002269 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002270
Antoine Pitroub52ec782009-01-25 16:34:23 +00002271 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002272 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002273 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002274 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002275 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002276 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002277 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002278 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002279 break;
2280
Antoine Pitroub52ec782009-01-25 16:34:23 +00002281 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002282 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002283 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002284
Raymond Hettingerf606f872003-03-16 03:11:04 +00002285 PREDICTED_WITH_ARG(JUMP_IF_FALSE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002286 TARGET(JUMP_IF_FALSE)
Raymond Hettinger21012b82003-02-26 18:11:50 +00002287 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002288 if (w == Py_True) {
2289 PREDICT(POP_TOP);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002290 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002291 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002292 if (w == Py_False) {
2293 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002294 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002295 }
2296 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002297 if (err > 0)
2298 err = 0;
2299 else if (err == 0)
Guido van Rossum374a9221991-04-04 10:40:29 +00002300 JUMPBY(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002301 else
2302 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002303 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002304
Raymond Hettingerf606f872003-03-16 03:11:04 +00002305 PREDICTED_WITH_ARG(JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002306 TARGET(JUMP_IF_TRUE)
Raymond Hettinger21012b82003-02-26 18:11:50 +00002307 w = TOP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002308 if (w == Py_False) {
2309 PREDICT(POP_TOP);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002310 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002311 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002312 if (w == Py_True) {
2313 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002314 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002315 }
2316 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002317 if (err > 0) {
2318 err = 0;
Guido van Rossum374a9221991-04-04 10:40:29 +00002319 JUMPBY(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002320 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002321 else if (err == 0)
2322 ;
2323 else
2324 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002325 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002327 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002328 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002329 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002330#if FAST_LOOPS
2331 /* Enabling this path speeds-up all while and for-loops by bypassing
2332 the per-loop checks for signals. By default, this should be turned-off
2333 because it prevents detection of a control-break in tight loops like
2334 "while 1: pass". Compile with this option turned-on when you need
2335 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002336 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002337 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002338 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002339#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002340 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002341#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002342
Antoine Pitroub52ec782009-01-25 16:34:23 +00002343 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002344 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002345 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002346 x = PyObject_GetIter(v);
2347 Py_DECREF(v);
2348 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002349 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002350 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002351 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002352 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002353 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002354 break;
2355
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002356 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002357 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002358 /* before: [iter]; after: [iter, iter()] *or* [] */
2359 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002360 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002361 if (x != NULL) {
2362 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002363 PREDICT(STORE_FAST);
2364 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002365 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002366 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002367 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002368 if (!PyErr_ExceptionMatches(
2369 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002370 break;
2371 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002372 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002373 /* iterator ended normally */
2374 x = v = POP();
2375 Py_DECREF(v);
2376 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002377 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002378
Antoine Pitroub52ec782009-01-25 16:34:23 +00002379 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002380 why = WHY_BREAK;
2381 goto fast_block_end;
2382
Antoine Pitroub52ec782009-01-25 16:34:23 +00002383 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002384 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002385 if (!retval) {
2386 x = NULL;
2387 break;
2388 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002389 why = WHY_CONTINUE;
2390 goto fast_block_end;
2391
Antoine Pitroub52ec782009-01-25 16:34:23 +00002392 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2393 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2394 TARGET(SETUP_FINALLY)
2395 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002396 /* NOTE: If you add any new block-setup opcodes that
2397 are not try/except/finally handlers, you may need
2398 to update the PyGen_NeedsFinalizing() function.
2399 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002400
Guido van Rossumb209a111997-04-29 18:18:01 +00002401 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002402 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002403 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002404
Antoine Pitroub52ec782009-01-25 16:34:23 +00002405 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002406 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002407 /* At the top of the stack are 1-3 values indicating
2408 how/why we entered the finally clause:
2409 - TOP = None
2410 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2411 - TOP = WHY_*; no retval below it
2412 - (TOP, SECOND, THIRD) = exc_info()
2413 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002414 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002415 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002416 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002417 EXIT(None, None, None)
2418
2419 In all cases, we remove EXIT from the stack, leaving
2420 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002421
2422 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002423 *and* the function call returns a 'true' value, we
2424 "zap" this information, to prevent END_FINALLY from
2425 re-raising the exception. (But non-local gotos
2426 should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002427 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002428
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002429 PyObject *exit_func = POP();
2430 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002431 if (u == Py_None) {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002432 v = w = Py_None;
2433 }
2434 else if (PyLong_Check(u)) {
Guido van Rossumc2e20742006-02-27 22:32:47 +00002435 u = v = w = Py_None;
2436 }
2437 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002438 v = SECOND();
2439 w = THIRD();
Guido van Rossumc2e20742006-02-27 22:32:47 +00002440 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002441 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002442 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2443 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002444 Py_DECREF(exit_func);
2445 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002446 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002447
2448 if (u != Py_None)
2449 err = PyObject_IsTrue(x);
2450 else
2451 err = 0;
2452 Py_DECREF(x);
2453
2454 if (err < 0)
2455 break; /* Go to error exit */
2456 else if (err > 0) {
2457 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002458 /* There was an exception and a True return */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002459 STACKADJ(-2);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002460 SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002461 Py_DECREF(u);
2462 Py_DECREF(v);
2463 Py_DECREF(w);
Guido van Rossumf6694362006-03-10 02:28:35 +00002464 }
Christian Heimes180510d2008-03-03 19:15:45 +00002465 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002466 break;
2467 }
2468
Antoine Pitroub52ec782009-01-25 16:34:23 +00002469 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002470 {
2471 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002472 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002473 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002474#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002475 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002476#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002477 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002478#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002479 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002480 PUSH(x);
2481 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002482 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002483 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002484 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002485
Antoine Pitroub52ec782009-01-25 16:34:23 +00002486 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2487 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2488 TARGET(CALL_FUNCTION_VAR_KW)
2489 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002490 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002491 int na = oparg & 0xff;
2492 int nk = (oparg>>8) & 0xff;
2493 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002494 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002495 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002496 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002497 if (flags & CALL_FLAG_VAR)
2498 n++;
2499 if (flags & CALL_FLAG_KW)
2500 n++;
2501 pfunc = stack_pointer - n - 1;
2502 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002503
Guido van Rossumac7be682001-01-17 15:42:30 +00002504 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002505 && PyMethod_GET_SELF(func) != NULL) {
2506 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002507 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002508 func = PyMethod_GET_FUNCTION(func);
2509 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002510 Py_DECREF(*pfunc);
2511 *pfunc = self;
2512 na++;
2513 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002514 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002515 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002516 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002517 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002518 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002519 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002520 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002521 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002522
Jeremy Hylton76901512000-03-28 23:49:17 +00002523 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002524 w = POP();
2525 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002526 }
2527 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002528 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002529 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002530 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002532
Antoine Pitroub52ec782009-01-25 16:34:23 +00002533 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2534 TARGET(MAKE_FUNCTION)
2535 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002536 {
2537 int posdefaults = oparg & 0xff;
2538 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002539 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002540
Guido van Rossum681d79a1995-07-18 14:51:37 +00002541 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002542 x = PyFunction_New(v, f->f_globals);
2543 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002544
Guido van Rossum0240b922007-02-26 21:23:50 +00002545 if (x != NULL && opcode == MAKE_CLOSURE) {
2546 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002547 if (PyFunction_SetClosure(x, v) != 0) {
2548 /* Can't happen unless bytecode is corrupt. */
2549 why = WHY_EXCEPTION;
2550 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002551 Py_DECREF(v);
2552 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002553
2554 if (x != NULL && num_annotations > 0) {
2555 Py_ssize_t name_ix;
2556 u = POP(); /* names of args with annotations */
2557 v = PyDict_New();
2558 if (v == NULL) {
2559 Py_DECREF(x);
2560 x = NULL;
2561 break;
2562 }
2563 name_ix = PyTuple_Size(u);
2564 assert(num_annotations == name_ix+1);
2565 while (name_ix > 0) {
2566 --name_ix;
2567 t = PyTuple_GET_ITEM(u, name_ix);
2568 w = POP();
2569 /* XXX(nnorwitz): check for errors */
2570 PyDict_SetItem(v, t, w);
2571 Py_DECREF(w);
2572 }
2573
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002574 if (PyFunction_SetAnnotations(x, v) != 0) {
2575 /* Can't happen unless
2576 PyFunction_SetAnnotations changes. */
2577 why = WHY_EXCEPTION;
2578 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002579 Py_DECREF(v);
2580 Py_DECREF(u);
2581 }
2582
Guido van Rossum681d79a1995-07-18 14:51:37 +00002583 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002584 if (x != NULL && posdefaults > 0) {
2585 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002586 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002587 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002588 x = NULL;
2589 break;
2590 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002591 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002592 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002593 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002594 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002595 if (PyFunction_SetDefaults(x, v) != 0) {
2596 /* Can't happen unless
2597 PyFunction_SetDefaults changes. */
2598 why = WHY_EXCEPTION;
2599 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002600 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002601 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002602 if (x != NULL && kwdefaults > 0) {
2603 v = PyDict_New();
2604 if (v == NULL) {
2605 Py_DECREF(x);
2606 x = NULL;
2607 break;
2608 }
2609 while (--kwdefaults >= 0) {
2610 w = POP(); /* default value */
2611 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002612 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002613 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002614 Py_DECREF(w);
2615 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002616 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002617 if (PyFunction_SetKwDefaults(x, v) != 0) {
2618 /* Can't happen unless
2619 PyFunction_SetKwDefaults changes. */
2620 why = WHY_EXCEPTION;
2621 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002622 Py_DECREF(v);
2623 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002624 PUSH(x);
2625 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002626 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002627
Antoine Pitroub52ec782009-01-25 16:34:23 +00002628 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002629 if (oparg == 3)
2630 w = POP();
2631 else
2632 w = NULL;
2633 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002634 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002635 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002636 Py_DECREF(u);
2637 Py_DECREF(v);
2638 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002639 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002640 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002641 break;
2642
Antoine Pitroub52ec782009-01-25 16:34:23 +00002643 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002644 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002645 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002646 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002647
Antoine Pitroub52ec782009-01-25 16:34:23 +00002648#ifdef USE_COMPUTED_GOTOS
2649 _unknown_opcode:
2650#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002651 default:
2652 fprintf(stderr,
2653 "XXX lineno: %d, opcode: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002654 PyCode_Addr2Line(f->f_code, f->f_lasti),
2655 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002656 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002657 why = WHY_EXCEPTION;
2658 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002659
2660#ifdef CASE_TOO_BIG
2661 }
2662#endif
2663
Guido van Rossum374a9221991-04-04 10:40:29 +00002664 } /* switch */
2665
2666 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002667
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002668 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002669
Guido van Rossum374a9221991-04-04 10:40:29 +00002670 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002671
Guido van Rossum374a9221991-04-04 10:40:29 +00002672 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002673 if (err == 0 && x != NULL) {
2674#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002675 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002676 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002677 fprintf(stderr,
2678 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002679 else {
2680#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002681 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002682 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002683#ifdef CHECKEXC
2684 }
2685#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002686 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002687 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002688 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002689 err = 0;
2690 }
2691
Guido van Rossum374a9221991-04-04 10:40:29 +00002692 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002693
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002694 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002695 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002696 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002697 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002698 why = WHY_EXCEPTION;
2699 }
2700 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002701#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002702 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002703 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002704 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002705 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002706 sprintf(buf, "Stack unwind with exception "
2707 "set and why=%d", why);
2708 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002709 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002710 }
2711#endif
2712
2713 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002714
Guido van Rossum374a9221991-04-04 10:40:29 +00002715 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002716 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002717
Fred Drake8f51f542001-10-04 14:48:42 +00002718 if (tstate->c_tracefunc != NULL)
2719 call_exc_trace(tstate->c_tracefunc,
2720 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002721 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002722
Guido van Rossum374a9221991-04-04 10:40:29 +00002723 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002724
Guido van Rossum374a9221991-04-04 10:40:29 +00002725 if (why == WHY_RERAISE)
2726 why = WHY_EXCEPTION;
2727
2728 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002729
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002730fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002731 while (why != WHY_NOT && f->f_iblock > 0) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002732 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002733
Tim Peters8a5c3c72004-04-05 19:36:21 +00002734 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002735 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2736 /* For a continue inside a try block,
2737 don't pop the block for the loop. */
Thomas Wouters1ee64222001-09-24 19:32:01 +00002738 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2739 b->b_level);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002740 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002741 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002742 Py_DECREF(retval);
2743 break;
2744 }
2745
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002746 if (b->b_type == EXCEPT_HANDLER) {
2747 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002748 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002749 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002750 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002751 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2752 why = WHY_NOT;
2753 JUMPTO(b->b_handler);
2754 break;
2755 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002756 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2757 || b->b_type == SETUP_FINALLY)) {
2758 PyObject *exc, *val, *tb;
2759 int handler = b->b_handler;
2760 /* Beware, this invalidates all b->b_* fields */
2761 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2762 PUSH(tstate->exc_traceback);
2763 PUSH(tstate->exc_value);
2764 if (tstate->exc_type != NULL) {
2765 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002766 }
2767 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002768 Py_INCREF(Py_None);
2769 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002770 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002771 PyErr_Fetch(&exc, &val, &tb);
2772 /* Make the raw exception data
2773 available to the handler,
2774 so a program can emulate the
2775 Python main loop. */
2776 PyErr_NormalizeException(
2777 &exc, &val, &tb);
2778 PyException_SetTraceback(val, tb);
2779 Py_INCREF(exc);
2780 tstate->exc_type = exc;
2781 Py_INCREF(val);
2782 tstate->exc_value = val;
2783 tstate->exc_traceback = tb;
2784 if (tb == NULL)
2785 tb = Py_None;
2786 Py_INCREF(tb);
2787 PUSH(tb);
2788 PUSH(val);
2789 PUSH(exc);
2790 why = WHY_NOT;
2791 JUMPTO(handler);
2792 break;
2793 }
2794 if (b->b_type == SETUP_FINALLY) {
2795 if (why & (WHY_RETURN | WHY_CONTINUE))
2796 PUSH(retval);
2797 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002798 why = WHY_NOT;
2799 JUMPTO(b->b_handler);
2800 break;
2801 }
2802 } /* unwind stack */
2803
2804 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002805
Guido van Rossum374a9221991-04-04 10:40:29 +00002806 if (why != WHY_NOT)
2807 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002808 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002809
Guido van Rossum374a9221991-04-04 10:40:29 +00002810 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002811
Tim Peters8a5c3c72004-04-05 19:36:21 +00002812 assert(why != WHY_YIELD);
2813 /* Pop remaining stack entries. */
2814 while (!EMPTY()) {
2815 v = POP();
2816 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002817 }
2818
Tim Peters8a5c3c72004-04-05 19:36:21 +00002819 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002820 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002821
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002822fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002823 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002824 if (tstate->c_tracefunc) {
2825 if (why == WHY_RETURN || why == WHY_YIELD) {
2826 if (call_trace(tstate->c_tracefunc,
2827 tstate->c_traceobj, f,
2828 PyTrace_RETURN, retval)) {
2829 Py_XDECREF(retval);
2830 retval = NULL;
2831 why = WHY_EXCEPTION;
2832 }
2833 }
2834 else if (why == WHY_EXCEPTION) {
2835 call_trace_protected(tstate->c_tracefunc,
2836 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002837 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002838 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002839 }
Fred Drake8f51f542001-10-04 14:48:42 +00002840 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002841 if (why == WHY_EXCEPTION)
2842 call_trace_protected(tstate->c_profilefunc,
2843 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002844 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002845 else if (call_trace(tstate->c_profilefunc,
2846 tstate->c_profileobj, f,
2847 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002848 Py_XDECREF(retval);
2849 retval = NULL;
2850 why = WHY_EXCEPTION;
2851 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002852 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002853 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002854
Tim Peters5ca576e2001-06-18 22:08:13 +00002855 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002856exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002857 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002858 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002859
Guido van Rossum96a42c81992-01-12 02:29:51 +00002860 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002861}
2862
Guido van Rossumc2e20742006-02-27 22:32:47 +00002863/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002864 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002865 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002866
Tim Peters6d6c1a32001-08-02 04:15:00 +00002867PyObject *
2868PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002869 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002870 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002871{
2872 register PyFrameObject *f;
2873 register PyObject *retval = NULL;
2874 register PyObject **fastlocals, **freevars;
2875 PyThreadState *tstate = PyThreadState_GET();
2876 PyObject *x, *u;
2877
2878 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00002879 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00002880 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00002881 return NULL;
2882 }
2883
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002884 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00002885 assert(globals != NULL);
2886 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00002887 if (f == NULL)
2888 return NULL;
2889
2890 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002891 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002892
2893 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00002894 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00002895 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2896 int i;
2897 int n = argcount;
2898 PyObject *kwdict = NULL;
2899 if (co->co_flags & CO_VARKEYWORDS) {
2900 kwdict = PyDict_New();
2901 if (kwdict == NULL)
2902 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002903 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00002904 if (co->co_flags & CO_VARARGS)
2905 i++;
2906 SETLOCAL(i, kwdict);
2907 }
2908 if (argcount > co->co_argcount) {
2909 if (!(co->co_flags & CO_VARARGS)) {
2910 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002911 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00002912 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002913 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00002914 defcount ? "at most" : "exactly",
2915 co->co_argcount,
2916 kwcount ? "non-keyword " : "",
2917 co->co_argcount == 1 ? "" : "s",
2918 argcount);
2919 goto fail;
2920 }
2921 n = co->co_argcount;
2922 }
2923 for (i = 0; i < n; i++) {
2924 x = args[i];
2925 Py_INCREF(x);
2926 SETLOCAL(i, x);
2927 }
2928 if (co->co_flags & CO_VARARGS) {
2929 u = PyTuple_New(argcount - n);
2930 if (u == NULL)
2931 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002932 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00002933 for (i = n; i < argcount; i++) {
2934 x = args[i];
2935 Py_INCREF(x);
2936 PyTuple_SET_ITEM(u, i-n, x);
2937 }
2938 }
2939 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002940 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00002941 PyObject *keyword = kws[2*i];
2942 PyObject *value = kws[2*i + 1];
2943 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00002944 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002945 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002946 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002947 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00002948 goto fail;
2949 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002950 /* Speed hack: do raw pointer compares. As names are
2951 normally interned this should almost always hit. */
2952 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002953 for (j = 0;
2954 j < co->co_argcount + co->co_kwonlyargcount;
2955 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002956 PyObject *nm = co_varnames[j];
2957 if (nm == keyword)
2958 goto kw_found;
2959 }
2960 /* Slow fallback, just in case */
2961 for (j = 0;
2962 j < co->co_argcount + co->co_kwonlyargcount;
2963 j++) {
2964 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00002965 int cmp = PyObject_RichCompareBool(
2966 keyword, nm, Py_EQ);
2967 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002968 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00002969 else if (cmp < 0)
2970 goto fail;
2971 }
2972 /* Check errors from Compare */
2973 if (PyErr_Occurred())
2974 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002975 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00002976 if (kwdict == NULL) {
2977 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00002978 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00002979 "keyword argument '%S'",
2980 co->co_name,
2981 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00002982 goto fail;
2983 }
2984 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002985 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00002986 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002987kw_found:
2988 if (GETLOCAL(j) != NULL) {
2989 PyErr_Format(PyExc_TypeError,
2990 "%U() got multiple "
2991 "values for keyword "
2992 "argument '%S'",
2993 co->co_name,
2994 keyword);
2995 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00002996 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002997 Py_INCREF(value);
2998 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00002999 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003000 if (co->co_kwonlyargcount > 0) {
3001 for (i = co->co_argcount;
3002 i < co->co_argcount + co->co_kwonlyargcount;
3003 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003004 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003005 if (GETLOCAL(i) != NULL)
3006 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003007 name = PyTuple_GET_ITEM(co->co_varnames, i);
3008 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003009 if (kwdefs != NULL)
3010 def = PyDict_GetItem(kwdefs, name);
3011 if (def != NULL) {
3012 Py_INCREF(def);
3013 SETLOCAL(i, def);
3014 continue;
3015 }
3016 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003017 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003018 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003019 goto fail;
3020 }
3021 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003022 if (argcount < co->co_argcount) {
3023 int m = co->co_argcount - defcount;
3024 for (i = argcount; i < m; i++) {
3025 if (GETLOCAL(i) == NULL) {
3026 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003027 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003028 "%spositional argument%s "
3029 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003030 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003031 ((co->co_flags & CO_VARARGS) ||
3032 defcount) ? "at least"
3033 : "exactly",
3034 m, kwcount ? "non-keyword " : "",
3035 m == 1 ? "" : "s", i);
3036 goto fail;
3037 }
3038 }
3039 if (n > m)
3040 i = n - m;
3041 else
3042 i = 0;
3043 for (; i < defcount; i++) {
3044 if (GETLOCAL(m+i) == NULL) {
3045 PyObject *def = defs[i];
3046 Py_INCREF(def);
3047 SETLOCAL(m+i, def);
3048 }
3049 }
3050 }
3051 }
3052 else {
3053 if (argcount > 0 || kwcount > 0) {
3054 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003055 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003056 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003057 argcount + kwcount);
3058 goto fail;
3059 }
3060 }
3061 /* Allocate and initialize storage for cell vars, and copy free
3062 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003063 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003064 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003065 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003066 PyObject *c;
3067
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003068 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003069 if (co->co_flags & CO_VARARGS)
3070 nargs++;
3071 if (co->co_flags & CO_VARKEYWORDS)
3072 nargs++;
3073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 /* Initialize each cell var, taking into account
3075 cell vars that are initialized from arguments.
3076
3077 Should arrange for the compiler to put cellvars
3078 that are arguments at the beginning of the cellvars
3079 list so that we can march over it more efficiently?
3080 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003081 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003082 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003083 PyTuple_GET_ITEM(co->co_cellvars, i));
3084 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003086 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003087 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003088 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003089 c = PyCell_New(GETLOCAL(j));
3090 if (c == NULL)
3091 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003092 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003093 found = 1;
3094 break;
3095 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003096 }
3097 if (found == 0) {
3098 c = PyCell_New(NULL);
3099 if (c == NULL)
3100 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003101 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003102 }
3103 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003104 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003105 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003106 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003107 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003108 PyObject *o = PyTuple_GET_ITEM(closure, i);
3109 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003110 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003111 }
3112 }
3113
Tim Peters5ca576e2001-06-18 22:08:13 +00003114 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003115 /* Don't need to keep the reference to f_back, it will be set
3116 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003117 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003118 f->f_back = NULL;
3119
Jeremy Hylton985eba52003-02-05 23:13:00 +00003120 PCALL(PCALL_GENERATOR);
3121
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003122 /* Create a new generator that owns the ready to run frame
3123 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003124 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003125 }
3126
Thomas Woutersce272b62007-09-19 21:19:28 +00003127 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003128
Thomas Woutersce272b62007-09-19 21:19:28 +00003129fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003130
Tim Petersb13680b2001-11-27 23:29:29 +00003131 /* decref'ing the frame can cause __del__ methods to get invoked,
3132 which can call back into Python. While we're done with the
3133 current Python frame (f), the associated C stack is still in use,
3134 so recursion_depth must be boosted for the duration.
3135 */
3136 assert(tstate != NULL);
3137 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003138 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003139 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003140 return retval;
3141}
3142
3143
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003144/* Logic for the raise statement (too complicated for inlining).
3145 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003146static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003147do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003148{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003149 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003150
3151 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003152 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003153 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003154 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003155 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003156 value = tstate->exc_value;
3157 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003158 if (type == Py_None) {
3159 PyErr_SetString(PyExc_RuntimeError,
3160 "No active exception to reraise");
3161 return WHY_EXCEPTION;
3162 }
3163 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003164 Py_XINCREF(value);
3165 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003166 PyErr_Restore(type, value, tb);
3167 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003168 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003169
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003170 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003171 raise
3172 raise <instance>
3173 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003174
Collin Winter828f04a2007-08-31 00:04:24 +00003175 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003176 type = exc;
3177 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003178 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003179 goto raise_error;
3180 }
Collin Winter828f04a2007-08-31 00:04:24 +00003181 else if (PyExceptionInstance_Check(exc)) {
3182 value = exc;
3183 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003184 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003185 }
3186 else {
3187 /* Not something you can raise. You get an exception
3188 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003189 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003190 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003191 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003192 goto raise_error;
3193 }
Collin Winter828f04a2007-08-31 00:04:24 +00003194
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003195 if (cause) {
3196 PyObject *fixed_cause;
3197 if (PyExceptionClass_Check(cause)) {
3198 fixed_cause = PyObject_CallObject(cause, NULL);
3199 if (fixed_cause == NULL)
3200 goto raise_error;
3201 Py_DECREF(cause);
3202 }
3203 else if (PyExceptionInstance_Check(cause)) {
3204 fixed_cause = cause;
3205 }
3206 else {
3207 PyErr_SetString(PyExc_TypeError,
3208 "exception causes must derive from "
3209 "BaseException");
3210 goto raise_error;
3211 }
3212 PyException_SetCause(value, fixed_cause);
3213 }
Collin Winter828f04a2007-08-31 00:04:24 +00003214
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003215 PyErr_SetObject(type, value);
3216 /* PyErr_SetObject incref's its arguments */
3217 Py_XDECREF(value);
3218 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003219 return WHY_EXCEPTION;
3220
3221raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003222 Py_XDECREF(value);
3223 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003224 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003225 return WHY_EXCEPTION;
3226}
3227
Tim Petersd6d010b2001-06-21 02:49:55 +00003228/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003229 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003230
Guido van Rossum0368b722007-05-11 16:50:42 +00003231 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3232 with a variable target.
3233*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003234
Barry Warsawe42b18f1997-08-25 22:13:04 +00003235static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003236unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003237{
Guido van Rossum0368b722007-05-11 16:50:42 +00003238 int i = 0, j = 0;
3239 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003240 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003241 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003242 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003243
Tim Petersd6d010b2001-06-21 02:49:55 +00003244 assert(v != NULL);
3245
3246 it = PyObject_GetIter(v);
3247 if (it == NULL)
3248 goto Error;
3249
3250 for (; i < argcnt; i++) {
3251 w = PyIter_Next(it);
3252 if (w == NULL) {
3253 /* Iterator done, via error or exhaustion. */
3254 if (!PyErr_Occurred()) {
3255 PyErr_Format(PyExc_ValueError,
3256 "need more than %d value%s to unpack",
3257 i, i == 1 ? "" : "s");
3258 }
3259 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003260 }
3261 *--sp = w;
3262 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003263
Guido van Rossum0368b722007-05-11 16:50:42 +00003264 if (argcntafter == -1) {
3265 /* We better have exhausted the iterator now. */
3266 w = PyIter_Next(it);
3267 if (w == NULL) {
3268 if (PyErr_Occurred())
3269 goto Error;
3270 Py_DECREF(it);
3271 return 1;
3272 }
3273 Py_DECREF(w);
3274 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3275 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003276 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003277
3278 l = PySequence_List(it);
3279 if (l == NULL)
3280 goto Error;
3281 *--sp = l;
3282 i++;
3283
3284 ll = PyList_GET_SIZE(l);
3285 if (ll < argcntafter) {
3286 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3287 argcnt + ll);
3288 goto Error;
3289 }
3290
3291 /* Pop the "after-variable" args off the list. */
3292 for (j = argcntafter; j > 0; j--, i++) {
3293 *--sp = PyList_GET_ITEM(l, ll - j);
3294 }
3295 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003296 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003297 Py_DECREF(it);
3298 return 1;
3299
Tim Petersd6d010b2001-06-21 02:49:55 +00003300Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003301 for (; i > 0; i--, sp++)
3302 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003303 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003304 return 0;
3305}
3306
3307
Guido van Rossum96a42c81992-01-12 02:29:51 +00003308#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003309static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003310prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003311{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003312 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003313 if (PyObject_Print(v, stdout, 0) != 0)
3314 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003315 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003316 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003317}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003318#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003319
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003320static void
Fred Drake5755ce62001-06-27 19:19:46 +00003321call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003322{
Guido van Rossumb209a111997-04-29 18:18:01 +00003323 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003324 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003325 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003326 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003327 value = Py_None;
3328 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003329 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003330 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003331 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003332 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003333 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003334 }
Fred Drake5755ce62001-06-27 19:19:46 +00003335 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003336 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003337 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003338 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003339 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003340 Py_XDECREF(type);
3341 Py_XDECREF(value);
3342 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003343 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003344}
3345
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003346static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003347call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003348 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003349{
3350 PyObject *type, *value, *traceback;
3351 int err;
3352 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003353 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003354 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003355 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003356 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003357 return 0;
3358 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003359 else {
3360 Py_XDECREF(type);
3361 Py_XDECREF(value);
3362 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003363 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003364 }
3365}
3366
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003367static int
Fred Drake5755ce62001-06-27 19:19:46 +00003368call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3369 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003370{
Fred Drake5755ce62001-06-27 19:19:46 +00003371 register PyThreadState *tstate = frame->f_tstate;
3372 int result;
3373 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003374 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003375 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003376 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003377 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003378 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3379 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003380 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003381 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003382}
3383
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003384PyObject *
3385_PyEval_CallTracing(PyObject *func, PyObject *args)
3386{
3387 PyFrameObject *frame = PyEval_GetFrame();
3388 PyThreadState *tstate = frame->f_tstate;
3389 int save_tracing = tstate->tracing;
3390 int save_use_tracing = tstate->use_tracing;
3391 PyObject *result;
3392
3393 tstate->tracing = 0;
3394 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3395 || (tstate->c_profilefunc != NULL));
3396 result = PyObject_Call(func, args, NULL);
3397 tstate->tracing = save_tracing;
3398 tstate->use_tracing = save_use_tracing;
3399 return result;
3400}
3401
Michael W. Hudson006c7522002-11-08 13:08:46 +00003402static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003403maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003404 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3405 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003406{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003407 int result = 0;
3408
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003409 /* If the last instruction executed isn't in the current
3410 instruction window, reset the window. If the last
3411 instruction happens to fall at the start of a line or if it
3412 represents a jump backwards, call the trace function.
3413 */
Michael W. Hudson53d58bb2002-08-30 13:09:51 +00003414 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003415 int line;
3416 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003417
Thomas Woutersce272b62007-09-19 21:19:28 +00003418 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3419 &bounds);
3420 if (line >= 0) {
Michael W. Hudson02ff6a92002-09-11 15:36:32 +00003421 frame->f_lineno = line;
Tim Peters8a5c3c72004-04-05 19:36:21 +00003422 result = call_trace(func, obj, frame,
Michael W. Hudson006c7522002-11-08 13:08:46 +00003423 PyTrace_LINE, Py_None);
Thomas Woutersce272b62007-09-19 21:19:28 +00003424 }
3425 *instr_lb = bounds.ap_lower;
3426 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003427 }
Armin Rigobf57a142004-03-22 19:24:58 +00003428 else if (frame->f_lasti <= *instr_prev) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003429 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003430 }
3431 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003432 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003433}
3434
Fred Drake5755ce62001-06-27 19:19:46 +00003435void
3436PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003437{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003438 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003439 PyObject *temp = tstate->c_profileobj;
3440 Py_XINCREF(arg);
3441 tstate->c_profilefunc = NULL;
3442 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003443 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003444 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003445 Py_XDECREF(temp);
3446 tstate->c_profilefunc = func;
3447 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003448 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003449 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003450}
3451
3452void
3453PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3454{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003455 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003456 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003457 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003458 Py_XINCREF(arg);
3459 tstate->c_tracefunc = NULL;
3460 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003461 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003462 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003463 Py_XDECREF(temp);
3464 tstate->c_tracefunc = func;
3465 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003466 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003467 tstate->use_tracing = ((func != NULL)
3468 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003469}
3470
Guido van Rossumb209a111997-04-29 18:18:01 +00003471PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003472PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003473{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003474 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003475 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003476 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003477 else
3478 return current_frame->f_builtins;
3479}
3480
Guido van Rossumb209a111997-04-29 18:18:01 +00003481PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003482PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003483{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003484 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003485 if (current_frame == NULL)
3486 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003487 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003488 return current_frame->f_locals;
3489}
3490
Guido van Rossumb209a111997-04-29 18:18:01 +00003491PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003492PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003493{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003494 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003495 if (current_frame == NULL)
3496 return NULL;
3497 else
3498 return current_frame->f_globals;
3499}
3500
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003501PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003502PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003503{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003504 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003505 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003506}
3507
Guido van Rossum6135a871995-01-09 17:53:26 +00003508int
Tim Peters5ba58662001-07-16 02:29:45 +00003509PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003510{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003511 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003512 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003513
3514 if (current_frame != NULL) {
3515 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003516 const int compilerflags = codeflags & PyCF_MASK;
3517 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003518 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003519 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003520 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003521#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003522 if (codeflags & CO_GENERATOR_ALLOWED) {
3523 result = 1;
3524 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3525 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003526#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003527 }
3528 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003529}
3530
Guido van Rossum3f5da241990-12-20 15:06:42 +00003531
Guido van Rossum681d79a1995-07-18 14:51:37 +00003532/* External interface to call any callable object.
3533 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003534
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003535#undef PyEval_CallObject
3536/* for backward compatibility: export this interface */
3537
Guido van Rossumb209a111997-04-29 18:18:01 +00003538PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003539PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003540{
Guido van Rossumb209a111997-04-29 18:18:01 +00003541 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003542}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003543#define PyEval_CallObject(func,arg) \
3544 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003545
Guido van Rossumb209a111997-04-29 18:18:01 +00003546PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003547PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003548{
Jeremy Hylton52820442001-01-03 23:52:36 +00003549 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003550
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003551 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003552 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003553 if (arg == NULL)
3554 return NULL;
3555 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003556 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003557 PyErr_SetString(PyExc_TypeError,
3558 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003559 return NULL;
3560 }
3561 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003562 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003563
Guido van Rossumb209a111997-04-29 18:18:01 +00003564 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003565 PyErr_SetString(PyExc_TypeError,
3566 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003567 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003568 return NULL;
3569 }
3570
Tim Peters6d6c1a32001-08-02 04:15:00 +00003571 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003572 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003573 return result;
3574}
3575
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003576const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003577PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003578{
3579 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003580 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003581 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003582 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003583 else if (PyCFunction_Check(func))
3584 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003585 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003586 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003587}
3588
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003589const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003590PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003591{
3592 if (PyMethod_Check(func))
3593 return "()";
3594 else if (PyFunction_Check(func))
3595 return "()";
3596 else if (PyCFunction_Check(func))
3597 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003598 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003599 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003600}
3601
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003602static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003603err_args(PyObject *func, int flags, int nargs)
3604{
3605 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003606 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003607 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003608 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003609 nargs);
3610 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003611 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003612 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003613 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003614 nargs);
3615}
3616
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003617#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003618if (tstate->use_tracing && tstate->c_profilefunc) { \
3619 if (call_trace(tstate->c_profilefunc, \
3620 tstate->c_profileobj, \
3621 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003622 func)) { \
3623 x = NULL; \
3624 } \
3625 else { \
3626 x = call; \
3627 if (tstate->c_profilefunc != NULL) { \
3628 if (x == NULL) { \
3629 call_trace_protected(tstate->c_profilefunc, \
3630 tstate->c_profileobj, \
3631 tstate->frame, PyTrace_C_EXCEPTION, \
3632 func); \
3633 /* XXX should pass (type, value, tb) */ \
3634 } else { \
3635 if (call_trace(tstate->c_profilefunc, \
3636 tstate->c_profileobj, \
3637 tstate->frame, PyTrace_C_RETURN, \
3638 func)) { \
3639 Py_DECREF(x); \
3640 x = NULL; \
3641 } \
3642 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003643 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003644 } \
3645} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003646 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003647 }
3648
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003649static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003650call_function(PyObject ***pp_stack, int oparg
3651#ifdef WITH_TSC
3652 , uint64* pintr0, uint64* pintr1
3653#endif
3654 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003655{
3656 int na = oparg & 0xff;
3657 int nk = (oparg>>8) & 0xff;
3658 int n = na + 2 * nk;
3659 PyObject **pfunc = (*pp_stack) - n - 1;
3660 PyObject *func = *pfunc;
3661 PyObject *x, *w;
3662
Jeremy Hylton985eba52003-02-05 23:13:00 +00003663 /* Always dispatch PyCFunction first, because these are
3664 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003665 */
3666 if (PyCFunction_Check(func) && nk == 0) {
3667 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003668 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003669
3670 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003671 if (flags & (METH_NOARGS | METH_O)) {
3672 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3673 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003674 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003675 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003676 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003677 else if (flags & METH_O && na == 1) {
3678 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003679 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003680 Py_DECREF(arg);
3681 }
3682 else {
3683 err_args(func, flags, na);
3684 x = NULL;
3685 }
3686 }
3687 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003688 PyObject *callargs;
3689 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003690 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003691 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003692 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003693 Py_XDECREF(callargs);
3694 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003695 } else {
3696 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3697 /* optimize access to bound methods */
3698 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003699 PCALL(PCALL_METHOD);
3700 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003701 Py_INCREF(self);
3702 func = PyMethod_GET_FUNCTION(func);
3703 Py_INCREF(func);
3704 Py_DECREF(*pfunc);
3705 *pfunc = self;
3706 na++;
3707 n++;
3708 } else
3709 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003710 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003711 if (PyFunction_Check(func))
3712 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003713 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003714 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003715 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003716 Py_DECREF(func);
3717 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003718
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003719 /* Clear the stack of the function object. Also removes
3720 the arguments in case they weren't consumed already
3721 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003722 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003723 while ((*pp_stack) > pfunc) {
3724 w = EXT_POP(*pp_stack);
3725 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003726 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003727 }
3728 return x;
3729}
3730
Jeremy Hylton192690e2002-08-16 18:36:11 +00003731/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003732 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003733 For the simplest case -- a function that takes only positional
3734 arguments and is called with only positional arguments -- it
3735 inlines the most primitive frame setup code from
3736 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3737 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003738*/
3739
3740static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003741fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003742{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003743 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003744 PyObject *globals = PyFunction_GET_GLOBALS(func);
3745 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003746 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003747 PyObject **d = NULL;
3748 int nd = 0;
3749
Jeremy Hylton985eba52003-02-05 23:13:00 +00003750 PCALL(PCALL_FUNCTION);
3751 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003752 if (argdefs == NULL && co->co_argcount == n &&
3753 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003754 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3755 PyFrameObject *f;
3756 PyObject *retval = NULL;
3757 PyThreadState *tstate = PyThreadState_GET();
3758 PyObject **fastlocals, **stack;
3759 int i;
3760
3761 PCALL(PCALL_FASTER_FUNCTION);
3762 assert(globals != NULL);
3763 /* XXX Perhaps we should create a specialized
3764 PyFrame_New() that doesn't take locals, but does
3765 take builtins without sanity checking them.
3766 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003767 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003768 f = PyFrame_New(tstate, co, globals, NULL);
3769 if (f == NULL)
3770 return NULL;
3771
3772 fastlocals = f->f_localsplus;
3773 stack = (*pp_stack) - n;
3774
3775 for (i = 0; i < n; i++) {
3776 Py_INCREF(*stack);
3777 fastlocals[i] = *stack++;
3778 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003779 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003780 ++tstate->recursion_depth;
3781 Py_DECREF(f);
3782 --tstate->recursion_depth;
3783 return retval;
3784 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003785 if (argdefs != NULL) {
3786 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003787 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003788 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003789 return PyEval_EvalCodeEx(co, globals,
3790 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003791 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003792 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003793}
3794
3795static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003796update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3797 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003798{
3799 PyObject *kwdict = NULL;
3800 if (orig_kwdict == NULL)
3801 kwdict = PyDict_New();
3802 else {
3803 kwdict = PyDict_Copy(orig_kwdict);
3804 Py_DECREF(orig_kwdict);
3805 }
3806 if (kwdict == NULL)
3807 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003808 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003809 int err;
3810 PyObject *value = EXT_POP(*pp_stack);
3811 PyObject *key = EXT_POP(*pp_stack);
3812 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003813 PyErr_Format(PyExc_TypeError,
3814 "%.200s%s got multiple values "
3815 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003816 PyEval_GetFuncName(func),
3817 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003818 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003819 Py_DECREF(key);
3820 Py_DECREF(value);
3821 Py_DECREF(kwdict);
3822 return NULL;
3823 }
3824 err = PyDict_SetItem(kwdict, key, value);
3825 Py_DECREF(key);
3826 Py_DECREF(value);
3827 if (err) {
3828 Py_DECREF(kwdict);
3829 return NULL;
3830 }
3831 }
3832 return kwdict;
3833}
3834
3835static PyObject *
3836update_star_args(int nstack, int nstar, PyObject *stararg,
3837 PyObject ***pp_stack)
3838{
3839 PyObject *callargs, *w;
3840
3841 callargs = PyTuple_New(nstack + nstar);
3842 if (callargs == NULL) {
3843 return NULL;
3844 }
3845 if (nstar) {
3846 int i;
3847 for (i = 0; i < nstar; i++) {
3848 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3849 Py_INCREF(a);
3850 PyTuple_SET_ITEM(callargs, nstack + i, a);
3851 }
3852 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003853 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003854 w = EXT_POP(*pp_stack);
3855 PyTuple_SET_ITEM(callargs, nstack, w);
3856 }
3857 return callargs;
3858}
3859
3860static PyObject *
3861load_args(PyObject ***pp_stack, int na)
3862{
3863 PyObject *args = PyTuple_New(na);
3864 PyObject *w;
3865
3866 if (args == NULL)
3867 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003868 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003869 w = EXT_POP(*pp_stack);
3870 PyTuple_SET_ITEM(args, na, w);
3871 }
3872 return args;
3873}
3874
3875static PyObject *
3876do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3877{
3878 PyObject *callargs = NULL;
3879 PyObject *kwdict = NULL;
3880 PyObject *result = NULL;
3881
3882 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003883 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003884 if (kwdict == NULL)
3885 goto call_fail;
3886 }
3887 callargs = load_args(pp_stack, na);
3888 if (callargs == NULL)
3889 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003890#ifdef CALL_PROFILE
3891 /* At this point, we have to look at the type of func to
3892 update the call stats properly. Do it here so as to avoid
3893 exposing the call stats machinery outside ceval.c
3894 */
3895 if (PyFunction_Check(func))
3896 PCALL(PCALL_FUNCTION);
3897 else if (PyMethod_Check(func))
3898 PCALL(PCALL_METHOD);
3899 else if (PyType_Check(func))
3900 PCALL(PCALL_TYPE);
3901 else
3902 PCALL(PCALL_OTHER);
3903#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003904 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003905call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003906 Py_XDECREF(callargs);
3907 Py_XDECREF(kwdict);
3908 return result;
3909}
3910
3911static PyObject *
3912ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3913{
3914 int nstar = 0;
3915 PyObject *callargs = NULL;
3916 PyObject *stararg = NULL;
3917 PyObject *kwdict = NULL;
3918 PyObject *result = NULL;
3919
3920 if (flags & CALL_FLAG_KW) {
3921 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003922 if (!PyDict_Check(kwdict)) {
3923 PyObject *d;
3924 d = PyDict_New();
3925 if (d == NULL)
3926 goto ext_call_fail;
3927 if (PyDict_Update(d, kwdict) != 0) {
3928 Py_DECREF(d);
3929 /* PyDict_Update raises attribute
3930 * error (percolated from an attempt
3931 * to get 'keys' attribute) instead of
3932 * a type error if its second argument
3933 * is not a mapping.
3934 */
3935 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3936 PyErr_Format(PyExc_TypeError,
3937 "%.200s%.200s argument after ** "
3938 "must be a mapping, not %.200s",
3939 PyEval_GetFuncName(func),
3940 PyEval_GetFuncDesc(func),
3941 kwdict->ob_type->tp_name);
3942 }
3943 goto ext_call_fail;
3944 }
3945 Py_DECREF(kwdict);
3946 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00003947 }
3948 }
3949 if (flags & CALL_FLAG_VAR) {
3950 stararg = EXT_POP(*pp_stack);
3951 if (!PyTuple_Check(stararg)) {
3952 PyObject *t = NULL;
3953 t = PySequence_Tuple(stararg);
3954 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00003955 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
3956 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003957 "%.200s%.200s argument after * "
3958 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003959 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003960 PyEval_GetFuncDesc(func),
3961 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003962 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003963 goto ext_call_fail;
3964 }
3965 Py_DECREF(stararg);
3966 stararg = t;
3967 }
3968 nstar = PyTuple_GET_SIZE(stararg);
3969 }
3970 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00003971 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003972 if (kwdict == NULL)
3973 goto ext_call_fail;
3974 }
3975 callargs = update_star_args(na, nstar, stararg, pp_stack);
3976 if (callargs == NULL)
3977 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003978#ifdef CALL_PROFILE
3979 /* At this point, we have to look at the type of func to
3980 update the call stats properly. Do it here so as to avoid
3981 exposing the call stats machinery outside ceval.c
3982 */
3983 if (PyFunction_Check(func))
3984 PCALL(PCALL_FUNCTION);
3985 else if (PyMethod_Check(func))
3986 PCALL(PCALL_METHOD);
3987 else if (PyType_Check(func))
3988 PCALL(PCALL_TYPE);
3989 else
3990 PCALL(PCALL_OTHER);
3991#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00003992 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00003993ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00003994 Py_XDECREF(callargs);
3995 Py_XDECREF(kwdict);
3996 Py_XDECREF(stararg);
3997 return result;
3998}
3999
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004000/* Extract a slice index from a PyInt or PyLong or an object with the
4001 nb_index slot defined, and store in *pi.
4002 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4003 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 +00004004 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004005*/
Tim Petersb5196382001-12-16 19:44:20 +00004006/* Note: If v is NULL, return success without storing into *pi. This
4007 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4008 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004009*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004010int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004011_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004012{
Tim Petersb5196382001-12-16 19:44:20 +00004013 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004014 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004015 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004016 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004017 if (x == -1 && PyErr_Occurred())
4018 return 0;
4019 }
4020 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004021 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004022 "slice indices must be integers or "
4023 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004024 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004025 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004026 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004027 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004028 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004029}
4030
Guido van Rossum486364b2007-06-30 05:01:58 +00004031#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004032 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004033
Guido van Rossumb209a111997-04-29 18:18:01 +00004034static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004035cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004036{
Guido van Rossumac7be682001-01-17 15:42:30 +00004037 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004038 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004039 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004040 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004041 break;
4042 case PyCmp_IS_NOT:
4043 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004044 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004045 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004046 res = PySequence_Contains(w, v);
4047 if (res < 0)
4048 return NULL;
4049 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004050 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004051 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004052 if (res < 0)
4053 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004054 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004055 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004056 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004057 if (PyTuple_Check(w)) {
4058 Py_ssize_t i, length;
4059 length = PyTuple_Size(w);
4060 for (i = 0; i < length; i += 1) {
4061 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004062 if (!PyExceptionClass_Check(exc)) {
4063 PyErr_SetString(PyExc_TypeError,
4064 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004065 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004066 }
4067 }
4068 }
4069 else {
Brett Cannon39590462007-02-26 22:01:14 +00004070 if (!PyExceptionClass_Check(w)) {
4071 PyErr_SetString(PyExc_TypeError,
4072 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004073 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004074 }
4075 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004076 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004077 break;
4078 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004079 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004080 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004081 v = res ? Py_True : Py_False;
4082 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004083 return v;
4084}
4085
Thomas Wouters52152252000-08-17 22:55:00 +00004086static PyObject *
4087import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004088{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004089 PyObject *x;
4090
4091 x = PyObject_GetAttr(v, name);
4092 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004093 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004094 }
Thomas Wouters52152252000-08-17 22:55:00 +00004095 return x;
4096}
Guido van Rossumac7be682001-01-17 15:42:30 +00004097
Thomas Wouters52152252000-08-17 22:55:00 +00004098static int
4099import_all_from(PyObject *locals, PyObject *v)
4100{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004101 PyObject *all = PyObject_GetAttrString(v, "__all__");
4102 PyObject *dict, *name, *value;
4103 int skip_leading_underscores = 0;
4104 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004105
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004106 if (all == NULL) {
4107 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4108 return -1; /* Unexpected error */
4109 PyErr_Clear();
4110 dict = PyObject_GetAttrString(v, "__dict__");
4111 if (dict == NULL) {
4112 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4113 return -1;
4114 PyErr_SetString(PyExc_ImportError,
4115 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004116 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004117 }
4118 all = PyMapping_Keys(dict);
4119 Py_DECREF(dict);
4120 if (all == NULL)
4121 return -1;
4122 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004123 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004124
4125 for (pos = 0, err = 0; ; pos++) {
4126 name = PySequence_GetItem(all, pos);
4127 if (name == NULL) {
4128 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4129 err = -1;
4130 else
4131 PyErr_Clear();
4132 break;
4133 }
4134 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004135 PyUnicode_Check(name) &&
4136 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004137 {
4138 Py_DECREF(name);
4139 continue;
4140 }
4141 value = PyObject_GetAttr(v, name);
4142 if (value == NULL)
4143 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004144 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004145 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004146 else
4147 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004148 Py_DECREF(name);
4149 Py_XDECREF(value);
4150 if (err != 0)
4151 break;
4152 }
4153 Py_DECREF(all);
4154 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004155}
4156
Guido van Rossumac7be682001-01-17 15:42:30 +00004157static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004158format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004159{
Neal Norwitzda059e32007-08-26 05:33:45 +00004160 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004161
4162 if (!obj)
4163 return;
4164
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004165 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004166 if (!obj_str)
4167 return;
4168
4169 PyErr_Format(exc, format_str, obj_str);
4170}
Guido van Rossum950361c1997-01-24 13:49:28 +00004171
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004172static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004173unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004174 PyFrameObject *f, unsigned char *next_instr)
4175{
4176 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004177 are (Unicode) strings. */
4178 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4179 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004180 Py_ssize_t new_len = v_len + w_len;
4181 if (new_len < 0) {
4182 PyErr_SetString(PyExc_OverflowError,
4183 "strings are too large to concat");
4184 return NULL;
4185 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004186
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004187 if (v->ob_refcnt == 2) {
4188 /* In the common case, there are 2 references to the value
4189 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004190 * value stack (in 'v') and one still stored in the
4191 * 'variable'. We try to delete the variable now to reduce
4192 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004193 */
4194 switch (*next_instr) {
4195 case STORE_FAST:
4196 {
4197 int oparg = PEEKARG();
4198 PyObject **fastlocals = f->f_localsplus;
4199 if (GETLOCAL(oparg) == v)
4200 SETLOCAL(oparg, NULL);
4201 break;
4202 }
4203 case STORE_DEREF:
4204 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004205 PyObject **freevars = (f->f_localsplus +
4206 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004207 PyObject *c = freevars[PEEKARG()];
4208 if (PyCell_GET(c) == v)
4209 PyCell_Set(c, NULL);
4210 break;
4211 }
4212 case STORE_NAME:
4213 {
4214 PyObject *names = f->f_code->co_names;
4215 PyObject *name = GETITEM(names, PEEKARG());
4216 PyObject *locals = f->f_locals;
4217 if (PyDict_CheckExact(locals) &&
4218 PyDict_GetItem(locals, name) == v) {
4219 if (PyDict_DelItem(locals, name) != 0) {
4220 PyErr_Clear();
4221 }
4222 }
4223 break;
4224 }
4225 }
4226 }
4227
Guido van Rossum98297ee2007-11-06 21:34:58 +00004228 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004229 /* Now we own the last reference to 'v', so we can resize it
4230 * in-place.
4231 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004232 if (PyUnicode_Resize(&v, new_len) != 0) {
4233 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004234 * deallocated so it cannot be put back into
4235 * 'variable'. The MemoryError is raised when there
4236 * is no value in 'variable', which might (very
4237 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004238 */
4239 return NULL;
4240 }
4241 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004242 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4243 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004244 return v;
4245 }
4246 else {
4247 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004248 w = PyUnicode_Concat(v, w);
4249 Py_DECREF(v);
4250 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004251 }
4252}
4253
Guido van Rossum950361c1997-01-24 13:49:28 +00004254#ifdef DYNAMIC_EXECUTION_PROFILE
4255
Skip Montanarof118cb12001-10-15 20:51:38 +00004256static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004257getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004258{
4259 int i;
4260 PyObject *l = PyList_New(256);
4261 if (l == NULL) return NULL;
4262 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004263 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004264 if (x == NULL) {
4265 Py_DECREF(l);
4266 return NULL;
4267 }
4268 PyList_SetItem(l, i, x);
4269 }
4270 for (i = 0; i < 256; i++)
4271 a[i] = 0;
4272 return l;
4273}
4274
4275PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004276_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004277{
4278#ifndef DXPAIRS
4279 return getarray(dxp);
4280#else
4281 int i;
4282 PyObject *l = PyList_New(257);
4283 if (l == NULL) return NULL;
4284 for (i = 0; i < 257; i++) {
4285 PyObject *x = getarray(dxpairs[i]);
4286 if (x == NULL) {
4287 Py_DECREF(l);
4288 return NULL;
4289 }
4290 PyList_SetItem(l, i, x);
4291 }
4292 return l;
4293#endif
4294}
4295
4296#endif