blob: bf3784586500fd97fbec9a65a9b9cf831f34ad6d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Thomas Wouters8ce81f72007-09-20 18:22:40 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
47
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
81{
82 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
90 opcode, ticked, inst, loop);
91}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Armin Rigobf57a142004-03-22 19:24:58 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000138static PyObject * unicode_concatenate(PyObject *, PyObject *,
139 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000140static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000141
Paul Prescode68140d2000-08-30 20:25:01 +0000142#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000143 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000144#define GLOBAL_NAME_ERROR_MSG \
145 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000146#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000147 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000148#define UNBOUNDFREE_ERROR_MSG \
149 "free variable '%.200s' referenced before assignment" \
150 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Guido van Rossum950361c1997-01-24 13:49:28 +0000152/* Dynamic execution profile */
153#ifdef DYNAMIC_EXECUTION_PROFILE
154#ifdef DXPAIRS
155static long dxpairs[257][256];
156#define dxp dxpairs[256]
157#else
158static long dxp[256];
159#endif
160#endif
161
Jeremy Hylton985eba52003-02-05 23:13:00 +0000162/* Function call profile */
163#ifdef CALL_PROFILE
164#define PCALL_NUM 11
165static int pcall[PCALL_NUM];
166
167#define PCALL_ALL 0
168#define PCALL_FUNCTION 1
169#define PCALL_FAST_FUNCTION 2
170#define PCALL_FASTER_FUNCTION 3
171#define PCALL_METHOD 4
172#define PCALL_BOUND_METHOD 5
173#define PCALL_CFUNCTION 6
174#define PCALL_TYPE 7
175#define PCALL_GENERATOR 8
176#define PCALL_OTHER 9
177#define PCALL_POP 10
178
179/* Notes about the statistics
180
181 PCALL_FAST stats
182
183 FAST_FUNCTION means no argument tuple needs to be created.
184 FASTER_FUNCTION means that the fast-path frame setup code is used.
185
186 If there is a method call where the call can be optimized by changing
187 the argument tuple and calling the function directly, it gets recorded
188 twice.
189
190 As a result, the relationship among the statistics appears to be
191 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
192 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
193 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
194 PCALL_METHOD > PCALL_BOUND_METHOD
195*/
196
197#define PCALL(POS) pcall[POS]++
198
199PyObject *
200PyEval_GetCallStats(PyObject *self)
201{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000202 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000203 pcall[0], pcall[1], pcall[2], pcall[3],
204 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000205 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000206}
207#else
208#define PCALL(O)
209
210PyObject *
211PyEval_GetCallStats(PyObject *self)
212{
213 Py_INCREF(Py_None);
214 return Py_None;
215}
216#endif
217
Tim Peters5ca576e2001-06-18 22:08:13 +0000218
Guido van Rossume59214e1994-08-30 08:01:59 +0000219#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000220
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000223#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000224#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000225
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000226static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000227static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000228static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229
Tim Peters7f468f22004-10-11 02:40:51 +0000230int
231PyEval_ThreadsInitialized(void)
232{
233 return interpreter_lock != 0;
234}
235
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000236void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000238{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000239 if (interpreter_lock)
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000240 return;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000241 interpreter_lock = PyThread_allocate_lock();
242 PyThread_acquire_lock(interpreter_lock, 1);
243 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000245
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000246void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000248{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000249 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250}
251
252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254{
Guido van Rossum65d5b571998-12-21 19:32:43 +0000255 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000256}
257
258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000260{
261 if (tstate == NULL)
262 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000263 /* Check someone has called PyEval_InitThreads() to create the lock */
264 assert(interpreter_lock);
Guido van Rossum65d5b571998-12-21 19:32:43 +0000265 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000266 if (PyThreadState_Swap(tstate) != NULL)
267 Py_FatalError(
268 "PyEval_AcquireThread: non-NULL old thread state");
269}
270
271void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000272PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000273{
274 if (tstate == NULL)
275 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
276 if (PyThreadState_Swap(NULL) != tstate)
277 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Guido van Rossum65d5b571998-12-21 19:32:43 +0000278 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000279}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000280
281/* This function is called from PyOS_AfterFork to ensure that newly
282 created child processes don't hold locks referring to threads which
283 are not running in the child process. (This could also be done using
284 pthread_atfork mechanism, at least for the pthreads implementation.) */
285
286void
287PyEval_ReInitThreads(void)
288{
Jesse Nollera8513972008-07-17 16:49:17 +0000289 PyObject *threading, *result;
290 PyThreadState *tstate;
291
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000292 if (!interpreter_lock)
293 return;
294 /*XXX Can't use PyThread_free_lock here because it does too
295 much error-checking. Doing this cleanly would require
296 adding a new function to each thread_*.h. Instead, just
297 create a new lock and waste a little bit of memory */
298 interpreter_lock = PyThread_allocate_lock();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000299 pending_lock = PyThread_allocate_lock();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000300 PyThread_acquire_lock(interpreter_lock, 1);
301 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000302
303 /* Update the threading module with the new state.
304 */
305 tstate = PyThreadState_GET();
306 threading = PyMapping_GetItemString(tstate->interp->modules,
307 "threading");
308 if (threading == NULL) {
309 /* threading not imported */
310 PyErr_Clear();
311 return;
312 }
313 result = PyObject_CallMethod(threading, "_after_fork", NULL);
314 if (result == NULL)
315 PyErr_WriteUnraisable(threading);
316 else
317 Py_DECREF(result);
318 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000319}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000320#endif
321
Guido van Rossumff4949e1992-08-05 19:58:53 +0000322/* Functions save_thread and restore_thread are always defined so
323 dynamically loaded modules needn't be compiled separately for use
324 with and without threads: */
325
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000326PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000328{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000329 PyThreadState *tstate = PyThreadState_Swap(NULL);
330 if (tstate == NULL)
331 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000332#ifdef WITH_THREAD
Guido van Rossumb74eca91997-09-30 22:03:16 +0000333 if (interpreter_lock)
Guido van Rossum65d5b571998-12-21 19:32:43 +0000334 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000335#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000336 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000337}
338
339void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000341{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000342 if (tstate == NULL)
343 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000344#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000345 if (interpreter_lock) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000346 int err = errno;
Guido van Rossum65d5b571998-12-21 19:32:43 +0000347 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000348 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000349 }
350#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000351 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000352}
353
354
Guido van Rossuma9672091994-09-14 13:31:22 +0000355/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
356 signal handlers or Mac I/O completion routines) can schedule calls
357 to a function to be called synchronously.
358 The synchronous function is called with one void* argument.
359 It should return 0 for success or -1 for failure -- failure should
360 be accompanied by an exception.
361
362 If registry succeeds, the registry function returns 0; if it fails
363 (e.g. due to too many pending calls) it returns -1 (without setting
364 an exception condition).
365
366 Note that because registry may occur from within signal handlers,
367 or other asynchronous events, calling malloc() is unsafe!
368
369#ifdef WITH_THREAD
370 Any thread can schedule pending calls, but only the main thread
371 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000372 There is no facility to schedule calls to a particular thread, but
373 that should be easy to change, should that ever be required. In
374 that case, the static variables here should go into the python
375 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000376#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000377*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000378
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000379#ifdef WITH_THREAD
380
381/* The WITH_THREAD implementation is thread-safe. It allows
382 scheduling to be made from any thread, and even from an executing
383 callback.
384 */
385
386#define NPENDINGCALLS 32
387static struct {
388 int (*func)(void *);
389 void *arg;
390} pendingcalls[NPENDINGCALLS];
391static int pendingfirst = 0;
392static int pendinglast = 0;
393static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
394static char pendingbusy = 0;
395
396int
397Py_AddPendingCall(int (*func)(void *), void *arg)
398{
399 int i, j, result=0;
400 PyThread_type_lock lock = pending_lock;
401
402 /* try a few times for the lock. Since this mechanism is used
403 * for signal handling (on the main thread), there is a (slim)
404 * chance that a signal is delivered on the same thread while we
405 * hold the lock during the Py_MakePendingCalls() function.
406 * This avoids a deadlock in that case.
407 * Note that signals can be delivered on any thread. In particular,
408 * on Windows, a SIGINT is delivered on a system-created worker
409 * thread.
410 * We also check for lock being NULL, in the unlikely case that
411 * this function is called before any bytecode evaluation takes place.
412 */
413 if (lock != NULL) {
414 for (i = 0; i<100; i++) {
415 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
416 break;
417 }
418 if (i == 100)
419 return -1;
420 }
421
422 i = pendinglast;
423 j = (i + 1) % NPENDINGCALLS;
424 if (j == pendingfirst) {
425 result = -1; /* Queue full */
426 } else {
427 pendingcalls[i].func = func;
428 pendingcalls[i].arg = arg;
429 pendinglast = j;
430 }
431 /* signal main loop */
432 _Py_Ticker = 0;
433 pendingcalls_to_do = 1;
434 if (lock != NULL)
435 PyThread_release_lock(lock);
436 return result;
437}
438
439int
440Py_MakePendingCalls(void)
441{
442 int i;
443 int r = 0;
444
445 if (!pending_lock) {
446 /* initial allocation of the lock */
447 pending_lock = PyThread_allocate_lock();
448 if (pending_lock == NULL)
449 return -1;
450 }
451
452 /* only service pending calls on main thread */
453 if (main_thread && PyThread_get_thread_ident() != main_thread)
454 return 0;
455 /* don't perform recursive pending calls */
456 if (pendingbusy)
457 return 0;
458 pendingbusy = 1;
459 /* perform a bounded number of calls, in case of recursion */
460 for (i=0; i<NPENDINGCALLS; i++) {
461 int j;
462 int (*func)(void *);
Benjamin Petersonb4ddfa42009-01-17 23:46:54 +0000463 void *arg = NULL;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000464
465 /* pop one item off the queue while holding the lock */
466 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
467 j = pendingfirst;
468 if (j == pendinglast) {
469 func = NULL; /* Queue empty */
470 } else {
471 func = pendingcalls[j].func;
472 arg = pendingcalls[j].arg;
473 pendingfirst = (j + 1) % NPENDINGCALLS;
474 }
475 pendingcalls_to_do = pendingfirst != pendinglast;
476 PyThread_release_lock(pending_lock);
477 /* having released the lock, perform the callback */
478 if (func == NULL)
479 break;
480 r = func(arg);
481 if (r)
482 break;
483 }
484 pendingbusy = 0;
485 return r;
486}
487
488#else /* if ! defined WITH_THREAD */
489
490/*
491 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
492 This code is used for signal handling in python that isn't built
493 with WITH_THREAD.
494 Don't use this implementation when Py_AddPendingCalls() can happen
495 on a different thread!
496
Guido van Rossuma9672091994-09-14 13:31:22 +0000497 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000498 (1) nested asynchronous calls to Py_AddPendingCall()
499 (2) AddPendingCall() calls made while pending calls are being processed.
500
501 (1) is very unlikely because typically signal delivery
502 is blocked during signal handling. So it should be impossible.
503 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000504 The current code is safe against (2), but not against (1).
505 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000506 thread is present, interrupted by signals, and that the critical
507 section is protected with the "busy" variable. On Windows, which
508 delivers SIGINT on a system thread, this does not hold and therefore
509 Windows really shouldn't use this version.
510 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000511*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000512
Guido van Rossuma9672091994-09-14 13:31:22 +0000513#define NPENDINGCALLS 32
514static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000515 int (*func)(void *);
516 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000517} pendingcalls[NPENDINGCALLS];
518static volatile int pendingfirst = 0;
519static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000520static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000521
522int
Thomas Wouters334fb892000-07-25 12:56:38 +0000523Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000524{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000525 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000526 int i, j;
527 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000528 if (busy)
529 return -1;
530 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000531 i = pendinglast;
532 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000533 if (j == pendingfirst) {
534 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000535 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000536 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000537 pendingcalls[i].func = func;
538 pendingcalls[i].arg = arg;
539 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000540
541 _Py_Ticker = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000542 pendingcalls_to_do = 1; /* Signal main loop */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000543 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000544 /* XXX End critical section */
545 return 0;
546}
547
Guido van Rossum180d7b41994-09-29 09:45:57 +0000548int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000549Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000550{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000551 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000552 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000553 return 0;
554 busy = 1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000555 pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000556 for (;;) {
557 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000558 int (*func)(void *);
559 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000560 i = pendingfirst;
561 if (i == pendinglast)
562 break; /* Queue empty */
563 func = pendingcalls[i].func;
564 arg = pendingcalls[i].arg;
565 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000566 if (func(arg) < 0) {
567 busy = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000568 pendingcalls_to_do = 1; /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000569 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000570 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000571 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000572 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000573 return 0;
574}
575
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000576#endif /* WITH_THREAD */
577
Guido van Rossuma9672091994-09-14 13:31:22 +0000578
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000579/* The interpreter's recursion limit */
580
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000581#ifndef Py_DEFAULT_RECURSION_LIMIT
582#define Py_DEFAULT_RECURSION_LIMIT 1000
583#endif
584static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
585int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000586
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000587int
588Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000589{
590 return recursion_limit;
591}
592
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000593void
594Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000595{
596 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000597 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000598}
599
Armin Rigo2b3eb402003-10-28 12:05:48 +0000600/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
601 if the recursion_depth reaches _Py_CheckRecursionLimit.
602 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
603 to guarantee that _Py_CheckRecursiveCall() is regularly called.
604 Without USE_STACKCHECK, there is no need for this. */
605int
606_Py_CheckRecursiveCall(char *where)
607{
608 PyThreadState *tstate = PyThreadState_GET();
609
610#ifdef USE_STACKCHECK
611 if (PyOS_CheckStack()) {
612 --tstate->recursion_depth;
613 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
614 return -1;
615 }
616#endif
Antoine Pitrou658fad82008-09-03 18:34:34 +0000617 _Py_CheckRecursionLimit = recursion_limit;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000618 if (tstate->recursion_critical)
619 /* Somebody asked that we don't check for recursion. */
620 return 0;
621 if (tstate->overflowed) {
622 if (tstate->recursion_depth > recursion_limit + 50) {
623 /* Overflowing while handling an overflow. Give up. */
624 Py_FatalError("Cannot recover from stack overflow.");
625 }
626 return 0;
627 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000628 if (tstate->recursion_depth > recursion_limit) {
629 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000630 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000631 PyErr_Format(PyExc_RuntimeError,
632 "maximum recursion depth exceeded%s",
633 where);
634 return -1;
635 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000636 return 0;
637}
638
Guido van Rossum374a9221991-04-04 10:40:29 +0000639/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000640enum why_code {
641 WHY_NOT = 0x0001, /* No error */
642 WHY_EXCEPTION = 0x0002, /* Exception occurred */
643 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
644 WHY_RETURN = 0x0008, /* 'return' statement */
645 WHY_BREAK = 0x0010, /* 'break' statement */
646 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000647 WHY_YIELD = 0x0040, /* 'yield' operator */
648 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000649};
Guido van Rossum374a9221991-04-04 10:40:29 +0000650
Collin Winter828f04a2007-08-31 00:04:24 +0000651static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000652static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000653
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000654/* Records whether tracing is on for any thread. Counts the number of
655 threads for which tstate->c_tracefunc is non-NULL, so if the value
656 is 0, we know we don't have to check this thread's c_tracefunc.
657 This speeds up the if statement in PyEval_EvalFrameEx() after
658 fast_next_opcode*/
659static int _Py_TracingPossible = 0;
660
Skip Montanarod581d772002-09-03 20:10:45 +0000661/* for manipulating the thread switch and periodic "stuff" - used to be
662 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000663int _Py_CheckInterval = 100;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000664volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000665
Guido van Rossumb209a111997-04-29 18:18:01 +0000666PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000667PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000668{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000669 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000670 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000671 (PyObject **)NULL, 0,
672 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000673 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000674 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000675}
676
677
678/* Interpreter main loop */
679
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000680PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000681PyEval_EvalFrame(PyFrameObject *f) {
682 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000683 used this API; core interpreter code should call
684 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000685 return PyEval_EvalFrameEx(f, 0);
686}
687
688PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000689PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000690{
Guido van Rossum950361c1997-01-24 13:49:28 +0000691#ifdef DXPAIRS
692 int lastopcode = 0;
693#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000694 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000695 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000696 register int opcode; /* Current opcode */
697 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000698 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000699 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000700 register PyObject *x; /* Result object -- NULL if error */
701 register PyObject *v; /* Temporary objects popped off stack */
702 register PyObject *w;
703 register PyObject *u;
704 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000705 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000706 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000707 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000708 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000709
Tim Peters8a5c3c72004-04-05 19:36:21 +0000710 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000711
712 not (instr_lb <= current_bytecode_offset < instr_ub)
713
Tim Peters8a5c3c72004-04-05 19:36:21 +0000714 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000715 initial values are such as to make this false the first
716 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000717 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000718
Guido van Rossumd076c731998-10-07 19:42:25 +0000719 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000720 PyObject *names;
721 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000722#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000723 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000724 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000725#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000726
Antoine Pitroub52ec782009-01-25 16:34:23 +0000727/* Computed GOTOs, or
728 the-optimization-commonly-but-improperly-known-as-"threaded code"
729 using gcc's labels-as-values extension
730 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
731
732 The traditional bytecode evaluation loop uses a "switch" statement, which
733 decent compilers will optimize as a single indirect branch instruction
734 combined with a lookup table of jump addresses. However, since the
735 indirect jump instruction is shared by all opcodes, the CPU will have a
736 hard time making the right prediction for where to jump next (actually,
737 it will be always wrong except in the uncommon case of a sequence of
738 several identical opcodes).
739
740 "Threaded code" in contrast, uses an explicit jump table and an explicit
741 indirect jump instruction at the end of each opcode. Since the jump
742 instruction is at a different address for each opcode, the CPU will make a
743 separate prediction for each of these instructions, which is equivalent to
744 predicting the second opcode of each opcode pair. These predictions have
745 a much better chance to turn out valid, especially in small bytecode loops.
746
747 A mispredicted branch on a modern CPU flushes the whole pipeline and
748 can cost several CPU cycles (depending on the pipeline depth),
749 and potentially many more instructions (depending on the pipeline width).
750 A correctly predicted branch, however, is nearly free.
751
752 At the time of this writing, the "threaded code" version is up to 15-20%
753 faster than the normal "switch" version, depending on the compiler and the
754 CPU architecture.
755
756 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
757 because it would render the measurements invalid.
758
759
760 NOTE: care must be taken that the compiler doesn't try to "optimize" the
761 indirect jumps by sharing them between all opcodes. Such optimizations
762 can be disabled on gcc by using the -fno-gcse flag (or possibly
763 -fno-crossjumping).
764*/
765
766#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
767#undef USE_COMPUTED_GOTOS
768#endif
769
770#ifdef USE_COMPUTED_GOTOS
771/* Import the static jump table */
772#include "opcode_targets.h"
773
774/* This macro is used when several opcodes defer to the same implementation
775 (e.g. SETUP_LOOP, SETUP_FINALLY) */
776#define TARGET_WITH_IMPL(op, impl) \
777 TARGET_##op: \
778 opcode = op; \
779 if (HAS_ARG(op)) \
780 oparg = NEXTARG(); \
781 case op: \
782 goto impl; \
783
784#define TARGET(op) \
785 TARGET_##op: \
786 opcode = op; \
787 if (HAS_ARG(op)) \
788 oparg = NEXTARG(); \
789 case op:
790
791
792#define DISPATCH() \
793 { \
794 /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
795 int _tick = _Py_Ticker - 1; \
796 _Py_Ticker = _tick; \
797 if (_tick >= 0) { \
798 FAST_DISPATCH(); \
799 } \
800 continue; \
801 }
802
803#ifdef LLTRACE
804#define FAST_DISPATCH() \
805 { \
806 if (!lltrace && !_Py_TracingPossible) { \
807 f->f_lasti = INSTR_OFFSET(); \
808 goto *opcode_targets[*next_instr++]; \
809 } \
810 goto fast_next_opcode; \
811 }
812#else
813#define FAST_DISPATCH() \
814 { \
815 if (!_Py_TracingPossible) { \
816 f->f_lasti = INSTR_OFFSET(); \
817 goto *opcode_targets[*next_instr++]; \
818 } \
819 goto fast_next_opcode; \
820 }
821#endif
822
823#else
824#define TARGET(op) \
825 case op:
826#define TARGET_WITH_IMPL(op, impl) \
827 /* silence compiler warnings about `impl` unused */ \
828 if (0) goto impl; \
829 case op:
830#define DISPATCH() continue
831#define FAST_DISPATCH() goto fast_next_opcode
832#endif
833
834
Neal Norwitza81d2202002-07-14 00:27:26 +0000835/* Tuple access macros */
836
837#ifndef Py_DEBUG
838#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
839#else
840#define GETITEM(v, i) PyTuple_GetItem((v), (i))
841#endif
842
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000843#ifdef WITH_TSC
844/* Use Pentium timestamp counter to mark certain events:
845 inst0 -- beginning of switch statement for opcode dispatch
846 inst1 -- end of switch statement (may be skipped)
847 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000848 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000849 (may be skipped)
850 intr1 -- beginning of long interruption
851 intr2 -- end of long interruption
852
853 Many opcodes call out to helper C functions. In some cases, the
854 time in those functions should be counted towards the time for the
855 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
856 calls another Python function; there's no point in charge all the
857 bytecode executed by the called function to the caller.
858
859 It's hard to make a useful judgement statically. In the presence
860 of operator overloading, it's impossible to tell if a call will
861 execute new Python code or not.
862
863 It's a case-by-case judgement. I'll use intr1 for the following
864 cases:
865
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000866 IMPORT_STAR
867 IMPORT_FROM
868 CALL_FUNCTION (and friends)
869
870 */
871 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
872 int ticked = 0;
873
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000874 READ_TIMESTAMP(inst0);
875 READ_TIMESTAMP(inst1);
876 READ_TIMESTAMP(loop0);
877 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000878
879 /* shut up the compiler */
880 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000881#endif
882
Guido van Rossum374a9221991-04-04 10:40:29 +0000883/* Code access macros */
884
Martin v. Löwis18e16552006-02-15 17:27:45 +0000885#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000886#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000887#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000888#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000889#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000890#define JUMPBY(x) (next_instr += (x))
891
Raymond Hettingerf606f872003-03-16 03:11:04 +0000892/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000893 Some opcodes tend to come in pairs thus making it possible to
894 predict the second code when the first is run. For example,
895 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
896 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000897
Georg Brandl86b2fb92008-07-16 03:43:04 +0000898 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000899 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000900 processor's own internal branch predication has a high likelihood of
901 success, resulting in a nearly zero-overhead transition to the
902 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000903 including its two unpredictable branches, the HAS_ARG test and the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000904 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000905 a successful PREDICT has the effect of making the two opcodes run as if
Georg Brandl86b2fb92008-07-16 03:43:04 +0000906 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000907
Georg Brandl86b2fb92008-07-16 03:43:04 +0000908 If collecting opcode statistics, your choices are to either keep the
909 predictions turned-on and interpret the results as if some opcodes
910 had been combined or turn-off predictions so that the opcode frequency
911 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000912
913 Opcode prediction is disabled with threaded code, since the latter allows
914 the CPU to record separate branch prediction information for each
915 opcode.
916
Raymond Hettingerf606f872003-03-16 03:11:04 +0000917*/
918
Antoine Pitroub52ec782009-01-25 16:34:23 +0000919#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Raymond Hettingera7216982004-02-08 19:59:27 +0000920#define PREDICT(op) if (0) goto PRED_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000921#define PREDICTED(op) PRED_##op:
922#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000923#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000924#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000925#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000926#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +0000927#endif
928
Raymond Hettingerf606f872003-03-16 03:11:04 +0000929
Guido van Rossum374a9221991-04-04 10:40:29 +0000930/* Stack manipulation macros */
931
Martin v. Löwis18e16552006-02-15 17:27:45 +0000932/* The stack can grow at most MAXINT deep, as co_nlocals and
933 co_stacksize are ints. */
934#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000935#define EMPTY() (STACK_LEVEL() == 0)
936#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000937#define SECOND() (stack_pointer[-2])
938#define THIRD() (stack_pointer[-3])
939#define FOURTH() (stack_pointer[-4])
Benjamin Peterson6d46a912009-06-28 16:17:34 +0000940#define PEEK(n) (stack_pointer[-(n)])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000941#define SET_TOP(v) (stack_pointer[-1] = (v))
942#define SET_SECOND(v) (stack_pointer[-2] = (v))
943#define SET_THIRD(v) (stack_pointer[-3] = (v))
944#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Benjamin Peterson6d46a912009-06-28 16:17:34 +0000945#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000946#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000947#define BASIC_PUSH(v) (*stack_pointer++ = (v))
948#define BASIC_POP() (*--stack_pointer)
949
Guido van Rossum96a42c81992-01-12 02:29:51 +0000950#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +0000951#define PUSH(v) { (void)(BASIC_PUSH(v), \
952 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000954#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
955 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +0000956#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
957 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000959#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
960 prtrace((STACK_POINTER)[-1], "ext_pop")), \
961 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000962#else
963#define PUSH(v) BASIC_PUSH(v)
964#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +0000965#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000966#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000967#endif
968
Guido van Rossum681d79a1995-07-18 14:51:37 +0000969/* Local variable macros */
970
971#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000972
973/* The SETLOCAL() macro must not DECREF the local variable in-place and
974 then store the new value; it must copy the old value to a temporary
975 value, then store the new value, and then DECREF the temporary value.
976 This is because it is possible that during the DECREF the frame is
977 accessed by other code (e.g. a __del__ method or gc.collect()) and the
978 variable would be pointing to already-freed memory. */
979#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
980 GETLOCAL(i) = value; \
981 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000982
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000983
984#define UNWIND_BLOCK(b) \
985 while (STACK_LEVEL() > (b)->b_level) { \
986 PyObject *v = POP(); \
987 Py_XDECREF(v); \
988 }
989
990#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +0000991 { \
992 PyObject *type, *value, *traceback; \
993 assert(STACK_LEVEL() >= (b)->b_level + 3); \
994 while (STACK_LEVEL() > (b)->b_level + 3) { \
995 value = POP(); \
996 Py_XDECREF(value); \
997 } \
998 type = tstate->exc_type; \
999 value = tstate->exc_value; \
1000 traceback = tstate->exc_traceback; \
1001 tstate->exc_type = POP(); \
1002 tstate->exc_value = POP(); \
1003 tstate->exc_traceback = POP(); \
1004 Py_XDECREF(type); \
1005 Py_XDECREF(value); \
1006 Py_XDECREF(traceback); \
1007 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001008
1009#define SAVE_EXC_STATE() \
1010 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001011 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001012 Py_XINCREF(tstate->exc_type); \
1013 Py_XINCREF(tstate->exc_value); \
1014 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001015 type = f->f_exc_type; \
1016 value = f->f_exc_value; \
1017 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001018 f->f_exc_type = tstate->exc_type; \
1019 f->f_exc_value = tstate->exc_value; \
1020 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001021 Py_XDECREF(type); \
1022 Py_XDECREF(value); \
1023 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001024 }
1025
1026#define SWAP_EXC_STATE() \
1027 { \
1028 PyObject *tmp; \
1029 tmp = tstate->exc_type; \
1030 tstate->exc_type = f->f_exc_type; \
1031 f->f_exc_type = tmp; \
1032 tmp = tstate->exc_value; \
1033 tstate->exc_value = f->f_exc_value; \
1034 f->f_exc_value = tmp; \
1035 tmp = tstate->exc_traceback; \
1036 tstate->exc_traceback = f->f_exc_traceback; \
1037 f->f_exc_traceback = tmp; \
1038 }
1039
Guido van Rossuma027efa1997-05-05 20:56:21 +00001040/* Start of code */
1041
Tim Peters5ca576e2001-06-18 22:08:13 +00001042 if (f == NULL)
1043 return NULL;
1044
Armin Rigo1d313ab2003-10-25 14:33:09 +00001045 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001046 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +00001047 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001048
Tim Peters5ca576e2001-06-18 22:08:13 +00001049 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001050
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001051 if (tstate->use_tracing) {
1052 if (tstate->c_tracefunc != NULL) {
1053 /* tstate->c_tracefunc, if defined, is a
1054 function that will be called on *every* entry
1055 to a code block. Its return value, if not
1056 None, is a function that will be called at
1057 the start of each executed line of code.
1058 (Actually, the function must return itself
1059 in order to continue tracing.) The trace
1060 functions are called with three arguments:
1061 a pointer to the current frame, a string
1062 indicating why the function is called, and
1063 an argument which depends on the situation.
1064 The global trace function is also called
1065 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001066 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001067 tstate->c_traceobj,
1068 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001069 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001070 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001071 }
1072 }
1073 if (tstate->c_profilefunc != NULL) {
1074 /* Similar for c_profilefunc, except it needn't
1075 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001076 if (call_trace_protected(tstate->c_profilefunc,
1077 tstate->c_profileobj,
1078 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001079 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001080 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001081 }
1082 }
1083 }
1084
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001085 co = f->f_code;
1086 names = co->co_names;
1087 consts = co->co_consts;
1088 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001089 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +00001090 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001091 /* An explanation is in order for the next line.
1092
1093 f->f_lasti now refers to the index of the last instruction
1094 executed. You might think this was obvious from the name, but
1095 this wasn't always true before 2.3! PyFrame_New now sets
1096 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1097 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001098 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001099
1100 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001101 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001102 prediction effectively links the two codes together as if they
1103 were a single new opcode; accordingly,f->f_lasti will point to
1104 the first code in the pair (for instance, GET_ITER followed by
1105 FOR_ITER is effectively a single opcode and f->f_lasti will point
1106 at to the beginning of the combined pair.)
1107 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001108 next_instr = first_instr + f->f_lasti + 1;
1109 stack_pointer = f->f_stacktop;
1110 assert(stack_pointer != NULL);
1111 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1112
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001113 if (f->f_code->co_flags & CO_GENERATOR) {
1114 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1115 /* We were in an except handler when we left,
1116 restore the exception state which was put aside
1117 (see YIELD_VALUE). */
1118 SWAP_EXC_STATE();
1119 }
1120 else {
1121 SAVE_EXC_STATE();
1122 }
1123 }
1124
Tim Peters5ca576e2001-06-18 22:08:13 +00001125#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001127#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001128#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001129 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001130#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001131
Guido van Rossum374a9221991-04-04 10:40:29 +00001132 why = WHY_NOT;
1133 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00001134 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +00001135 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001136
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001137 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001138 why = WHY_EXCEPTION;
1139 goto on_error;
1140 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001141
Guido van Rossum374a9221991-04-04 10:40:29 +00001142 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001143#ifdef WITH_TSC
1144 if (inst1 == 0) {
1145 /* Almost surely, the opcode executed a break
1146 or a continue, preventing inst1 from being set
1147 on the way out of the loop.
1148 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001149 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001150 loop1 = inst1;
1151 }
1152 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1153 intr0, intr1);
1154 ticked = 0;
1155 inst1 = 0;
1156 intr0 = 0;
1157 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001158 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001159#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001160 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001161 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001162
Guido van Rossuma027efa1997-05-05 20:56:21 +00001163 /* Do periodic things. Doing this every time through
1164 the loop would add too much overhead, so we do it
1165 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001166 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001167 event needs attention (e.g. a signal handler or
1168 async I/O handler); see Py_AddPendingCall() and
1169 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001170
Skip Montanarod581d772002-09-03 20:10:45 +00001171 if (--_Py_Ticker < 0) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001172 if (*next_instr == SETUP_FINALLY) {
1173 /* Make the last opcode before
1174 a try: finally: block uninterruptable. */
1175 goto fast_next_opcode;
1176 }
Skip Montanarod581d772002-09-03 20:10:45 +00001177 _Py_Ticker = _Py_CheckInterval;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001178 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001179#ifdef WITH_TSC
1180 ticked = 1;
1181#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001182 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001183 if (Py_MakePendingCalls() < 0) {
1184 why = WHY_EXCEPTION;
1185 goto on_error;
1186 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001187 if (pendingcalls_to_do)
Kurt B. Kaiser4c79a832004-11-23 18:06:08 +00001188 /* MakePendingCalls() didn't succeed.
1189 Force early re-execution of this
1190 "periodic" code, possibly after
1191 a thread switch */
1192 _Py_Ticker = 0;
Guido van Rossum8861b741996-07-30 16:49:37 +00001193 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001194#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001195 if (interpreter_lock) {
1196 /* Give another thread a chance */
1197
Guido van Rossum25ce5661997-08-02 03:10:38 +00001198 if (PyThreadState_Swap(NULL) != tstate)
1199 Py_FatalError("ceval: tstate mix-up");
Guido van Rossum65d5b571998-12-21 19:32:43 +00001200 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001201
1202 /* Other threads may run now */
1203
Guido van Rossum65d5b571998-12-21 19:32:43 +00001204 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001205 if (PyThreadState_Swap(tstate) != NULL)
1206 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001207
1208 /* Check for thread interrupts */
1209
1210 if (tstate->async_exc != NULL) {
1211 x = tstate->async_exc;
1212 tstate->async_exc = NULL;
1213 PyErr_SetNone(x);
1214 Py_DECREF(x);
1215 why = WHY_EXCEPTION;
1216 goto on_error;
1217 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001218 }
1219#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001220 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001221
Neil Schemenauer63543862002-02-17 19:10:14 +00001222 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001223 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001224
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001225 /* line-by-line tracing support */
1226
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001227 if (_Py_TracingPossible &&
1228 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001229 /* see maybe_call_line_trace
1230 for expository comments */
1231 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001232
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001233 err = maybe_call_line_trace(tstate->c_tracefunc,
1234 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001235 f, &instr_lb, &instr_ub,
1236 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001237 /* Reload possibly changed frame fields */
1238 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001239 if (f->f_stacktop != NULL) {
1240 stack_pointer = f->f_stacktop;
1241 f->f_stacktop = NULL;
1242 }
1243 if (err) {
1244 /* trace function raised an exception */
1245 goto on_error;
1246 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001247 }
1248
1249 /* Extract opcode and argument */
1250
Guido van Rossum374a9221991-04-04 10:40:29 +00001251 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001252 oparg = 0; /* allows oparg to be stored in a register because
1253 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001254 if (HAS_ARG(opcode))
1255 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001256 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001257#ifdef DYNAMIC_EXECUTION_PROFILE
1258#ifdef DXPAIRS
1259 dxpairs[lastopcode][opcode]++;
1260 lastopcode = opcode;
1261#endif
1262 dxp[opcode]++;
1263#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001264
Guido van Rossum96a42c81992-01-12 02:29:51 +00001265#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001266 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001267
Guido van Rossum96a42c81992-01-12 02:29:51 +00001268 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001269 if (HAS_ARG(opcode)) {
1270 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001271 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001272 }
1273 else {
1274 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001275 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001276 }
1277 }
1278#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001279
Guido van Rossum374a9221991-04-04 10:40:29 +00001280 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001281 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001282
Guido van Rossum374a9221991-04-04 10:40:29 +00001283 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001284
Guido van Rossum374a9221991-04-04 10:40:29 +00001285 /* BEWARE!
1286 It is essential that any operation that fails sets either
1287 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1288 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001289
Guido van Rossum374a9221991-04-04 10:40:29 +00001290 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001291
Antoine Pitroub52ec782009-01-25 16:34:23 +00001292 TARGET(NOP)
1293 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001294
Antoine Pitroub52ec782009-01-25 16:34:23 +00001295 TARGET(LOAD_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001296 x = GETLOCAL(oparg);
1297 if (x != NULL) {
1298 Py_INCREF(x);
1299 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001300 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001301 }
1302 format_exc_check_arg(PyExc_UnboundLocalError,
1303 UNBOUNDLOCAL_ERROR_MSG,
1304 PyTuple_GetItem(co->co_varnames, oparg));
1305 break;
1306
Antoine Pitroub52ec782009-01-25 16:34:23 +00001307 TARGET(LOAD_CONST)
Skip Montanaro04d80f82002-08-04 21:03:35 +00001308 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001309 Py_INCREF(x);
1310 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001311 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001312
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001313 PREDICTED_WITH_ARG(STORE_FAST);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001314 TARGET(STORE_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001315 v = POP();
1316 SETLOCAL(oparg, v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001317 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001318
Antoine Pitroub52ec782009-01-25 16:34:23 +00001319 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001320 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001321 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001322 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001323
Antoine Pitroub52ec782009-01-25 16:34:23 +00001324 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001325 v = TOP();
1326 w = SECOND();
1327 SET_TOP(w);
1328 SET_SECOND(v);
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(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001332 v = TOP();
1333 w = SECOND();
1334 x = THIRD();
1335 SET_TOP(w);
1336 SET_SECOND(x);
1337 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001338 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Antoine Pitroub52ec782009-01-25 16:34:23 +00001340 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001341 u = TOP();
1342 v = SECOND();
1343 w = THIRD();
1344 x = FOURTH();
1345 SET_TOP(v);
1346 SET_SECOND(w);
1347 SET_THIRD(x);
1348 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001349 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001350
Antoine Pitroub52ec782009-01-25 16:34:23 +00001351 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001352 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001353 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001354 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001355 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Antoine Pitroub52ec782009-01-25 16:34:23 +00001357 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001358 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001359 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001360 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001361 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001362 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001363 STACKADJ(2);
1364 SET_TOP(x);
1365 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001366 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001367 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001368 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001369 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001370 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001371 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001372 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001373 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001374 STACKADJ(3);
1375 SET_TOP(x);
1376 SET_SECOND(w);
1377 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001378 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001379 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001380 Py_FatalError("invalid argument to DUP_TOPX"
1381 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001382 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001383 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001384
Antoine Pitroub52ec782009-01-25 16:34:23 +00001385 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001386 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001387 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001388 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001389 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001390 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001391 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Antoine Pitroub52ec782009-01-25 16:34:23 +00001393 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001394 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001395 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001396 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001397 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001398 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001399 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Antoine Pitroub52ec782009-01-25 16:34:23 +00001401 TARGET(UNARY_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001402 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001403 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001404 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001405 if (err == 0) {
1406 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001407 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001408 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001409 }
1410 else if (err > 0) {
1411 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001412 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001413 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001414 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001415 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001416 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001418
Antoine Pitroub52ec782009-01-25 16:34:23 +00001419 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001420 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001421 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001422 Py_DECREF(v);
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 Rossum7928cd71991-10-24 14:59:31 +00001425 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Antoine Pitroub52ec782009-01-25 16:34:23 +00001427 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001428 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001429 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001430 x = PyNumber_Power(v, w, Py_None);
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 Rossum50564e81996-01-12 01:13:16 +00001435 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001436
Antoine Pitroub52ec782009-01-25 16:34:23 +00001437 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001438 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001439 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001440 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001441 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 Rossum374a9221991-04-04 10:40:29 +00001445 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001446
Antoine Pitroub52ec782009-01-25 16:34:23 +00001447 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001448 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001449 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001450 x = PyNumber_TrueDivide(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_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001458 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001459 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001460 x = PyNumber_FloorDivide(v, w);
1461 Py_DECREF(v);
1462 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001463 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001464 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001465 break;
1466
Antoine Pitroub52ec782009-01-25 16:34:23 +00001467 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001468 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001469 v = TOP();
Benjamin Petersonefb06b02009-02-26 18:55:48 +00001470 if (PyUnicode_CheckExact(v))
1471 x = PyUnicode_Format(v, w);
1472 else
1473 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001474 Py_DECREF(v);
1475 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001476 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001477 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001479
Antoine Pitroub52ec782009-01-25 16:34:23 +00001480 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001481 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001482 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001483 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001484 PyUnicode_CheckExact(w)) {
1485 x = unicode_concatenate(v, w, f, next_instr);
1486 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001487 goto skip_decref_vx;
1488 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001489 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001490 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001491 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001492 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001493 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001494 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001495 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001496 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001497 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Antoine Pitroub52ec782009-01-25 16:34:23 +00001499 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001500 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001501 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001502 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001503 Py_DECREF(v);
1504 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001505 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001506 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001507 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Antoine Pitroub52ec782009-01-25 16:34:23 +00001509 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001511 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001512 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001513 Py_DECREF(v);
1514 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001515 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001516 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001517 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001518
Antoine Pitroub52ec782009-01-25 16:34:23 +00001519 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001520 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001521 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001522 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001523 Py_DECREF(v);
1524 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001525 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001526 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001527 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Antoine Pitroub52ec782009-01-25 16:34:23 +00001529 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001530 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001531 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001532 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001533 Py_DECREF(v);
1534 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001535 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001536 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001537 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Antoine Pitroub52ec782009-01-25 16:34:23 +00001539 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001540 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001541 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001542 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001543 Py_DECREF(v);
1544 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001545 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001546 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001547 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Antoine Pitroub52ec782009-01-25 16:34:23 +00001549 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001550 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001551 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001552 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001553 Py_DECREF(v);
1554 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001555 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001556 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001557 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Antoine Pitroub52ec782009-01-25 16:34:23 +00001559 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001560 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001561 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001562 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001563 Py_DECREF(v);
1564 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001565 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001566 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001567 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001568
Antoine Pitroub52ec782009-01-25 16:34:23 +00001569 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001570 w = POP();
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001571 v = PEEK(oparg);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001572 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001573 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001574 if (err == 0) {
1575 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001576 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001577 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001578 break;
1579
Antoine Pitroub52ec782009-01-25 16:34:23 +00001580 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001581 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001582 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001583 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001584 Py_DECREF(w);
1585 if (err == 0) {
1586 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001587 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001588 }
1589 break;
1590
Antoine Pitroub52ec782009-01-25 16:34:23 +00001591 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001592 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001593 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001594 x = PyNumber_InPlacePower(v, w, Py_None);
1595 Py_DECREF(v);
1596 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001597 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001598 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001599 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001600
Antoine Pitroub52ec782009-01-25 16:34:23 +00001601 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001602 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001603 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001604 x = PyNumber_InPlaceMultiply(v, w);
1605 Py_DECREF(v);
1606 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001607 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001608 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001609 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001610
Antoine Pitroub52ec782009-01-25 16:34:23 +00001611 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001612 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001613 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001614 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001615 Py_DECREF(v);
1616 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001617 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001618 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001619 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001620
Antoine Pitroub52ec782009-01-25 16:34:23 +00001621 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001622 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001623 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001624 x = PyNumber_InPlaceFloorDivide(v, w);
1625 Py_DECREF(v);
1626 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001627 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001628 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001629 break;
1630
Antoine Pitroub52ec782009-01-25 16:34:23 +00001631 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001632 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001633 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001634 x = PyNumber_InPlaceRemainder(v, w);
1635 Py_DECREF(v);
1636 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001637 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001638 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001639 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Antoine Pitroub52ec782009-01-25 16:34:23 +00001641 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001642 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001643 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001644 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001645 PyUnicode_CheckExact(w)) {
1646 x = unicode_concatenate(v, w, f, next_instr);
1647 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001648 goto skip_decref_v;
1649 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001650 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001651 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001652 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001653 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001654 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001655 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001656 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001657 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001658 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Antoine Pitroub52ec782009-01-25 16:34:23 +00001660 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001661 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001662 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001663 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001664 Py_DECREF(v);
1665 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001666 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001667 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001668 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001669
Antoine Pitroub52ec782009-01-25 16:34:23 +00001670 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001671 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001672 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001673 x = PyNumber_InPlaceLshift(v, w);
1674 Py_DECREF(v);
1675 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001676 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001677 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001678 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001679
Antoine Pitroub52ec782009-01-25 16:34:23 +00001680 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001681 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001682 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001683 x = PyNumber_InPlaceRshift(v, w);
1684 Py_DECREF(v);
1685 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001686 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001687 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001688 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Antoine Pitroub52ec782009-01-25 16:34:23 +00001690 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001691 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001692 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001693 x = PyNumber_InPlaceAnd(v, w);
1694 Py_DECREF(v);
1695 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001696 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001697 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001698 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Antoine Pitroub52ec782009-01-25 16:34:23 +00001700 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001701 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001702 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001703 x = PyNumber_InPlaceXor(v, w);
1704 Py_DECREF(v);
1705 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001706 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001707 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001708 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Antoine Pitroub52ec782009-01-25 16:34:23 +00001710 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001711 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001712 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001713 x = PyNumber_InPlaceOr(v, w);
1714 Py_DECREF(v);
1715 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001716 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001717 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001718 break;
1719
Antoine Pitroub52ec782009-01-25 16:34:23 +00001720 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001721 w = TOP();
1722 v = SECOND();
1723 u = THIRD();
1724 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001725 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001726 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001727 Py_DECREF(u);
1728 Py_DECREF(v);
1729 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001730 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001731 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001732
Antoine Pitroub52ec782009-01-25 16:34:23 +00001733 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001734 w = TOP();
1735 v = SECOND();
1736 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001737 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001738 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001739 Py_DECREF(v);
1740 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001741 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001742 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001743
Antoine Pitroub52ec782009-01-25 16:34:23 +00001744 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001745 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001746 w = PySys_GetObject("displayhook");
1747 if (w == NULL) {
1748 PyErr_SetString(PyExc_RuntimeError,
1749 "lost sys.displayhook");
1750 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001751 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001752 }
1753 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001754 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001755 if (x == NULL)
1756 err = -1;
1757 }
1758 if (err == 0) {
1759 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001760 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001761 if (w == NULL)
1762 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001763 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001764 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001765 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001766 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001767
Thomas Wouters434d0822000-08-24 20:11:32 +00001768#ifdef CASE_TOO_BIG
1769 default: switch (opcode) {
1770#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001771 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001772 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001773 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001774 case 2:
1775 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001776 case 1:
1777 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001778 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001779 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001780 break;
1781 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001782 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001783 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001784 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001785 break;
1786 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001787 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Antoine Pitroub52ec782009-01-25 16:34:23 +00001789 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001790 x = POP();
1791 v = f->f_locals;
1792 Py_XDECREF(v);
1793 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001794 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001795
Antoine Pitroub52ec782009-01-25 16:34:23 +00001796 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001797 retval = POP();
1798 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001799 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001800
Antoine Pitroub52ec782009-01-25 16:34:23 +00001801 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001802 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001803 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001804 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001805 /* Put aside the current exception state and restore
1806 that of the calling frame. This only serves when
1807 "yield" is used inside an except handler. */
1808 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001809 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001810
Antoine Pitroub52ec782009-01-25 16:34:23 +00001811 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001812 {
1813 PyTryBlock *b = PyFrame_BlockPop(f);
1814 if (b->b_type != EXCEPT_HANDLER) {
1815 PyErr_SetString(PyExc_SystemError,
1816 "popped block is not an except handler");
1817 why = WHY_EXCEPTION;
1818 break;
1819 }
1820 UNWIND_EXCEPT_HANDLER(b);
1821 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001822 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001823
Antoine Pitroub52ec782009-01-25 16:34:23 +00001824 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001825 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001826 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001827 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001828 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001829 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001830
Christian Heimes180510d2008-03-03 19:15:45 +00001831 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001832 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001833 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001834 if (PyLong_Check(v)) {
1835 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001836 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001837 if (why == WHY_RETURN ||
1838 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001839 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001840 if (why == WHY_SILENCED) {
1841 /* An exception was silenced by 'with', we must
1842 manually unwind the EXCEPT_HANDLER block which was
1843 created when the exception was caught, otherwise
1844 the stack will be in an inconsistent state. */
1845 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersonac8c7302009-06-28 16:03:15 +00001846 assert(b->b_type == EXCEPT_HANDLER);
1847 UNWIND_EXCEPT_HANDLER(b);
1848 why = WHY_NOT;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001849 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001850 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001851 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001853 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001854 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001855 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001856 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001857 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001858 else if (v != Py_None) {
1859 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001860 "'finally' pops bad exception");
1861 why = WHY_EXCEPTION;
1862 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001863 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001864 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001865
Antoine Pitroub52ec782009-01-25 16:34:23 +00001866 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001867 x = PyDict_GetItemString(f->f_builtins,
1868 "__build_class__");
1869 if (x == NULL) {
1870 PyErr_SetString(PyExc_ImportError,
1871 "__build_class__ not found");
1872 break;
1873 }
1874 Py_INCREF(x);
1875 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001876 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001877
Antoine Pitroub52ec782009-01-25 16:34:23 +00001878 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001879 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001880 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001881 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001882 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001883 err = PyDict_SetItem(x, w, v);
1884 else
1885 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001886 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001887 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001888 break;
1889 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001890 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001891 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001893
Antoine Pitroub52ec782009-01-25 16:34:23 +00001894 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001895 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001896 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001897 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001898 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001899 NAME_ERROR_MSG,
1900 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001901 break;
1902 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001903 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001904 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001905 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001906
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001907 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001908 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001909 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001910 if (PyTuple_CheckExact(v) &&
1911 PyTuple_GET_SIZE(v) == oparg) {
1912 PyObject **items = \
1913 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001914 while (oparg--) {
1915 w = items[oparg];
1916 Py_INCREF(w);
1917 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001918 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001919 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001920 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001921 } else if (PyList_CheckExact(v) &&
1922 PyList_GET_SIZE(v) == oparg) {
1923 PyObject **items = \
1924 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001925 while (oparg--) {
1926 w = items[oparg];
1927 Py_INCREF(w);
1928 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001929 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001930 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001931 stack_pointer + oparg)) {
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001932 STACKADJ(oparg);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001933 } else {
1934 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001935 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001936 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001937 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001938 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001939
Antoine Pitroub52ec782009-01-25 16:34:23 +00001940 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00001941 {
1942 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1943 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001944
Guido van Rossum0368b722007-05-11 16:50:42 +00001945 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1946 stack_pointer + totalargs)) {
1947 stack_pointer += totalargs;
1948 } else {
1949 why = WHY_EXCEPTION;
1950 }
1951 Py_DECREF(v);
1952 break;
1953 }
1954
Antoine Pitroub52ec782009-01-25 16:34:23 +00001955 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001956 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001957 v = TOP();
1958 u = SECOND();
1959 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00001960 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1961 Py_DECREF(v);
1962 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001963 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001964 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001965
Antoine Pitroub52ec782009-01-25 16:34:23 +00001966 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001967 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001968 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00001969 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1970 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00001971 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001972 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001973
Antoine Pitroub52ec782009-01-25 16:34:23 +00001974 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001975 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001976 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001977 err = PyDict_SetItem(f->f_globals, w, v);
1978 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001979 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001980 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001981
Antoine Pitroub52ec782009-01-25 16:34:23 +00001982 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00001983 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00001984 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00001985 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001986 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00001987 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001988
Antoine Pitroub52ec782009-01-25 16:34:23 +00001989 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001990 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001991 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00001992 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001993 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001994 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00001995 break;
1996 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001997 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001998 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00001999 Py_XINCREF(x);
2000 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002001 else {
2002 x = PyObject_GetItem(v, w);
2003 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002004 if (!PyErr_ExceptionMatches(
2005 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002006 break;
2007 PyErr_Clear();
2008 }
2009 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002010 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002011 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002012 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002013 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002014 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002015 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002016 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002017 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002018 break;
2019 }
2020 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002021 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002022 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002023 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002024 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002025
Antoine Pitroub52ec782009-01-25 16:34:23 +00002026 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002027 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002028 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002029 /* Inline the PyDict_GetItem() calls.
2030 WARNING: this is an extreme speed hack.
2031 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002032 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002033 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002034 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002035 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002036 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002037 e = d->ma_lookup(d, w, hash);
2038 if (e == NULL) {
2039 x = NULL;
2040 break;
2041 }
2042 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002043 if (x != NULL) {
2044 Py_INCREF(x);
2045 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002046 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002047 }
2048 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002049 e = d->ma_lookup(d, w, hash);
2050 if (e == NULL) {
2051 x = NULL;
2052 break;
2053 }
2054 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002055 if (x != NULL) {
2056 Py_INCREF(x);
2057 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002058 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002059 }
2060 goto load_global_error;
2061 }
2062 }
2063 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002064 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002065 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002066 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002067 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002068 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002069 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002070 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002071 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002072 break;
2073 }
2074 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002075 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002076 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002077 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002078
Antoine Pitroub52ec782009-01-25 16:34:23 +00002079 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002080 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002081 if (x != NULL) {
2082 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002083 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002084 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002085 format_exc_check_arg(
2086 PyExc_UnboundLocalError,
2087 UNBOUNDLOCAL_ERROR_MSG,
2088 PyTuple_GetItem(co->co_varnames, oparg)
2089 );
2090 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002091
Antoine Pitroub52ec782009-01-25 16:34:23 +00002092 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002093 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002094 Py_INCREF(x);
2095 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002096 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002097 break;
2098
Antoine Pitroub52ec782009-01-25 16:34:23 +00002099 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002100 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002101 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002102 if (w != NULL) {
2103 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002104 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002105 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002106 err = -1;
2107 /* Don't stomp existing exception */
2108 if (PyErr_Occurred())
2109 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002110 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2111 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002112 oparg);
2113 format_exc_check_arg(
2114 PyExc_UnboundLocalError,
2115 UNBOUNDLOCAL_ERROR_MSG,
2116 v);
2117 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002118 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2119 PyTuple_GET_SIZE(co->co_cellvars));
2120 format_exc_check_arg(PyExc_NameError,
2121 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002122 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002123 break;
2124
Antoine Pitroub52ec782009-01-25 16:34:23 +00002125 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002126 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002127 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002128 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002129 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002130 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002131
Antoine Pitroub52ec782009-01-25 16:34:23 +00002132 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002133 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002134 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002135 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002136 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002137 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002138 }
2139 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002140 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002141 }
2142 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002143
Antoine Pitroub52ec782009-01-25 16:34:23 +00002144 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002145 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002146 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002147 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002148 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002149 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002150 }
2151 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002152 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002153 }
2154 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002155
Antoine Pitroub52ec782009-01-25 16:34:23 +00002156 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002157 x = PySet_New(NULL);
2158 if (x != NULL) {
2159 for (; --oparg >= 0;) {
2160 w = POP();
2161 if (err == 0)
2162 err = PySet_Add(x, w);
2163 Py_DECREF(w);
2164 }
2165 if (err != 0) {
2166 Py_DECREF(x);
2167 break;
2168 }
2169 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002170 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002171 }
2172 break;
2173
Antoine Pitroub52ec782009-01-25 16:34:23 +00002174 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002175 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002176 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002177 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002178 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002179
Antoine Pitroub52ec782009-01-25 16:34:23 +00002180 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002181 w = TOP(); /* key */
2182 u = SECOND(); /* value */
2183 v = THIRD(); /* dict */
2184 STACKADJ(-2);
2185 assert (PyDict_CheckExact(v));
2186 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2187 Py_DECREF(u);
2188 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002189 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002190 break;
2191
Antoine Pitroub52ec782009-01-25 16:34:23 +00002192 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002193 w = TOP(); /* key */
2194 u = SECOND(); /* value */
2195 STACKADJ(-2);
2196 v = stack_pointer[-oparg]; /* dict */
2197 assert (PyDict_CheckExact(v));
2198 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2199 Py_DECREF(u);
2200 Py_DECREF(w);
2201 if (err == 0) {
2202 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002203 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002204 }
2205 break;
2206
Antoine Pitroub52ec782009-01-25 16:34:23 +00002207 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002208 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002209 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002210 x = PyObject_GetAttr(v, w);
2211 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002212 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002213 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002215
Antoine Pitroub52ec782009-01-25 16:34:23 +00002216 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002217 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002218 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002219 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002220 Py_DECREF(v);
2221 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002222 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002223 if (x == NULL) break;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002224 PREDICT(POP_JUMP_IF_FALSE);
2225 PREDICT(POP_JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002226 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Antoine Pitroub52ec782009-01-25 16:34:23 +00002228 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002229 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002230 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002231 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002232 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002233 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002234 break;
2235 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002236 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002237 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002238 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002239 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002240 w = PyTuple_Pack(5,
2241 w,
2242 f->f_globals,
2243 f->f_locals == NULL ?
2244 Py_None : f->f_locals,
2245 v,
2246 u);
2247 else
2248 w = PyTuple_Pack(4,
2249 w,
2250 f->f_globals,
2251 f->f_locals == NULL ?
2252 Py_None : f->f_locals,
2253 v);
2254 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002255 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002256 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002257 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002258 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002259 x = NULL;
2260 break;
2261 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002262 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002263 v = x;
2264 x = PyEval_CallObject(v, w);
2265 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002266 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002267 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002268 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002269 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002270 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002271
Antoine Pitroub52ec782009-01-25 16:34:23 +00002272 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002273 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002274 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002275 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002276 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002277 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002278 break;
2279 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002280 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002281 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002282 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002283 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002284 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002285 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002286 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002287
Antoine Pitroub52ec782009-01-25 16:34:23 +00002288 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002289 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002290 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002291 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002292 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002293 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002294 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002295 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002296 break;
2297
Antoine Pitroub52ec782009-01-25 16:34:23 +00002298 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002299 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002300 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002302 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2303 TARGET(POP_JUMP_IF_FALSE)
2304 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002305 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002306 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002307 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002308 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002309 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002310 Py_DECREF(w);
2311 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002312 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002313 }
2314 err = PyObject_IsTrue(w);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002315 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002316 if (err > 0)
2317 err = 0;
2318 else if (err == 0)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002319 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002320 else
2321 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002322 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002324 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2325 TARGET(POP_JUMP_IF_TRUE)
2326 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002327 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002328 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002329 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002330 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002331 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002332 Py_DECREF(w);
2333 JUMPTO(oparg);
2334 FAST_DISPATCH();
2335 }
2336 err = PyObject_IsTrue(w);
2337 Py_DECREF(w);
2338 if (err > 0) {
2339 err = 0;
2340 JUMPTO(oparg);
2341 }
2342 else if (err == 0)
2343 ;
2344 else
2345 break;
2346 DISPATCH();
2347
2348 TARGET(JUMP_IF_FALSE_OR_POP)
2349 w = TOP();
2350 if (w == Py_True) {
2351 STACKADJ(-1);
2352 Py_DECREF(w);
2353 FAST_DISPATCH();
2354 }
2355 if (w == Py_False) {
2356 JUMPTO(oparg);
2357 FAST_DISPATCH();
2358 }
2359 err = PyObject_IsTrue(w);
2360 if (err > 0) {
2361 STACKADJ(-1);
2362 Py_DECREF(w);
2363 err = 0;
2364 }
2365 else if (err == 0)
2366 JUMPTO(oparg);
2367 else
2368 break;
2369 DISPATCH();
2370
2371 TARGET(JUMP_IF_TRUE_OR_POP)
2372 w = TOP();
2373 if (w == Py_False) {
2374 STACKADJ(-1);
2375 Py_DECREF(w);
2376 FAST_DISPATCH();
2377 }
2378 if (w == Py_True) {
2379 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002380 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002381 }
2382 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002383 if (err > 0) {
2384 err = 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002385 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002386 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002387 else if (err == 0) {
2388 STACKADJ(-1);
2389 Py_DECREF(w);
2390 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002391 else
2392 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002393 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002394
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002395 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002396 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002397 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002398#if FAST_LOOPS
2399 /* Enabling this path speeds-up all while and for-loops by bypassing
2400 the per-loop checks for signals. By default, this should be turned-off
2401 because it prevents detection of a control-break in tight loops like
2402 "while 1: pass". Compile with this option turned-on when you need
2403 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002404 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002405 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002406 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002407#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002408 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002409#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002410
Antoine Pitroub52ec782009-01-25 16:34:23 +00002411 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002412 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002413 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002414 x = PyObject_GetIter(v);
2415 Py_DECREF(v);
2416 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002417 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002418 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002419 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002420 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002421 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002422 break;
2423
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002424 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002425 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002426 /* before: [iter]; after: [iter, iter()] *or* [] */
2427 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002428 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002429 if (x != NULL) {
2430 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002431 PREDICT(STORE_FAST);
2432 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002433 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002434 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002435 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002436 if (!PyErr_ExceptionMatches(
2437 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002438 break;
2439 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002440 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002441 /* iterator ended normally */
2442 x = v = POP();
2443 Py_DECREF(v);
2444 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002445 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002446
Antoine Pitroub52ec782009-01-25 16:34:23 +00002447 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002448 why = WHY_BREAK;
2449 goto fast_block_end;
2450
Antoine Pitroub52ec782009-01-25 16:34:23 +00002451 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002452 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002453 if (!retval) {
2454 x = NULL;
2455 break;
2456 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002457 why = WHY_CONTINUE;
2458 goto fast_block_end;
2459
Antoine Pitroub52ec782009-01-25 16:34:23 +00002460 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2461 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2462 TARGET(SETUP_FINALLY)
2463 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002464 /* NOTE: If you add any new block-setup opcodes that
2465 are not try/except/finally handlers, you may need
2466 to update the PyGen_NeedsFinalizing() function.
2467 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002468
Guido van Rossumb209a111997-04-29 18:18:01 +00002469 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002470 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002471 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002472
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002473 TARGET(SETUP_WITH)
2474 {
2475 static PyObject *exit, *enter;
2476 w = TOP();
2477 x = special_lookup(w, "__exit__", &exit);
2478 if (!x)
2479 break;
2480 SET_TOP(x);
2481 u = special_lookup(w, "__enter__", &enter);
2482 Py_DECREF(w);
2483 if (!u) {
2484 x = NULL;
2485 break;
2486 }
2487 x = PyObject_CallFunctionObjArgs(u, NULL);
2488 Py_DECREF(u);
2489 if (!x)
2490 break;
2491 /* Setup the finally block before pushing the result
2492 of __enter__ on the stack. */
2493 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2494 STACK_LEVEL());
2495
2496 PUSH(x);
2497 DISPATCH();
2498 }
2499
Antoine Pitroub52ec782009-01-25 16:34:23 +00002500 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002501 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002502 /* At the top of the stack are 1-3 values indicating
2503 how/why we entered the finally clause:
2504 - TOP = None
2505 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2506 - TOP = WHY_*; no retval below it
2507 - (TOP, SECOND, THIRD) = exc_info()
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002508 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002509 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002510 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002511 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002512 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002513 EXIT(None, None, None)
2514
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002515 In the first two cases, we remove EXIT from the
2516 stack, leaving the rest in the same order. In the
2517 third case, we shift the bottom 3 values of the
2518 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002519
2520 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002521 *and* the function call returns a 'true' value, we
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002522 push WHY_SILENCED onto the stack. END_FINALLY will
2523 then not re-raise the exception. (But non-local
2524 gotos should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002525 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002526
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002527 PyObject *exit_func;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002528 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002529 if (u == Py_None) {
Ezio Melottib4d286d2009-10-03 16:14:07 +00002530 (void)POP();
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002531 exit_func = TOP();
2532 SET_TOP(u);
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002533 v = w = Py_None;
2534 }
2535 else if (PyLong_Check(u)) {
Ezio Melottib4d286d2009-10-03 16:14:07 +00002536 (void)POP();
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002537 switch(PyLong_AsLong(u)) {
2538 case WHY_RETURN:
2539 case WHY_CONTINUE:
2540 /* Retval in TOP. */
2541 exit_func = SECOND();
2542 SET_SECOND(TOP());
2543 SET_TOP(u);
2544 break;
2545 default:
2546 exit_func = TOP();
2547 SET_TOP(u);
2548 break;
2549 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002550 u = v = w = Py_None;
2551 }
2552 else {
Benjamin Peterson176101d2009-06-28 15:40:50 +00002553 PyObject *tp, *exc, *tb;
2554 PyTryBlock *block;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002555 v = SECOND();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002556 w = THIRD();
Benjamin Peterson176101d2009-06-28 15:40:50 +00002557 tp = FOURTH();
Benjamin Petersonb1715f12009-06-28 16:21:52 +00002558 exc = PEEK(5);
2559 tb = PEEK(6);
2560 exit_func = PEEK(7);
2561 SET_VALUE(7, tb);
2562 SET_VALUE(6, exc);
2563 SET_VALUE(5, tp);
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002564 /* UNWIND_EXCEPT_BLOCK will pop this off. */
Benjamin Petersonb1715f12009-06-28 16:21:52 +00002565 SET_FOURTH(NULL);
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002566 /* We just shifted the stack down, so we have
2567 to tell the except handler block that the
2568 values are lower than it expects. */
Benjamin Peterson176101d2009-06-28 15:40:50 +00002569 block = &f->f_blockstack[f->f_iblock - 1];
2570 assert(block->b_type == EXCEPT_HANDLER);
2571 block->b_level--;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002572 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002573 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002574 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2575 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002576 Py_DECREF(exit_func);
2577 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002578 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002579
2580 if (u != Py_None)
2581 err = PyObject_IsTrue(x);
2582 else
2583 err = 0;
2584 Py_DECREF(x);
2585
2586 if (err < 0)
2587 break; /* Go to error exit */
2588 else if (err > 0) {
2589 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002590 /* There was an exception and a True return */
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002591 PUSH(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002592 }
Christian Heimes180510d2008-03-03 19:15:45 +00002593 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002594 break;
2595 }
2596
Antoine Pitroub52ec782009-01-25 16:34:23 +00002597 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002598 {
2599 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002600 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002601 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002602#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002603 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002604#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002605 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002606#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002607 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002608 PUSH(x);
2609 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002610 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002611 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002612 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002613
Antoine Pitroub52ec782009-01-25 16:34:23 +00002614 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2615 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2616 TARGET(CALL_FUNCTION_VAR_KW)
2617 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002618 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002619 int na = oparg & 0xff;
2620 int nk = (oparg>>8) & 0xff;
2621 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002622 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002623 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002624 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002625 if (flags & CALL_FLAG_VAR)
2626 n++;
2627 if (flags & CALL_FLAG_KW)
2628 n++;
2629 pfunc = stack_pointer - n - 1;
2630 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002631
Guido van Rossumac7be682001-01-17 15:42:30 +00002632 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002633 && PyMethod_GET_SELF(func) != NULL) {
2634 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002635 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002636 func = PyMethod_GET_FUNCTION(func);
2637 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002638 Py_DECREF(*pfunc);
2639 *pfunc = self;
2640 na++;
2641 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002642 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002643 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002644 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002645 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002646 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002647 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002648 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002649 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002650
Jeremy Hylton76901512000-03-28 23:49:17 +00002651 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002652 w = POP();
2653 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002654 }
2655 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002656 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002657 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002658 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002659 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002660
Antoine Pitroub52ec782009-01-25 16:34:23 +00002661 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2662 TARGET(MAKE_FUNCTION)
2663 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002664 {
2665 int posdefaults = oparg & 0xff;
2666 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002667 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002668
Guido van Rossum681d79a1995-07-18 14:51:37 +00002669 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002670 x = PyFunction_New(v, f->f_globals);
2671 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002672
Guido van Rossum0240b922007-02-26 21:23:50 +00002673 if (x != NULL && opcode == MAKE_CLOSURE) {
2674 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002675 if (PyFunction_SetClosure(x, v) != 0) {
2676 /* Can't happen unless bytecode is corrupt. */
2677 why = WHY_EXCEPTION;
2678 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002679 Py_DECREF(v);
2680 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002681
2682 if (x != NULL && num_annotations > 0) {
2683 Py_ssize_t name_ix;
2684 u = POP(); /* names of args with annotations */
2685 v = PyDict_New();
2686 if (v == NULL) {
2687 Py_DECREF(x);
2688 x = NULL;
2689 break;
2690 }
2691 name_ix = PyTuple_Size(u);
2692 assert(num_annotations == name_ix+1);
2693 while (name_ix > 0) {
2694 --name_ix;
2695 t = PyTuple_GET_ITEM(u, name_ix);
2696 w = POP();
2697 /* XXX(nnorwitz): check for errors */
2698 PyDict_SetItem(v, t, w);
2699 Py_DECREF(w);
2700 }
2701
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002702 if (PyFunction_SetAnnotations(x, v) != 0) {
2703 /* Can't happen unless
2704 PyFunction_SetAnnotations changes. */
2705 why = WHY_EXCEPTION;
2706 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002707 Py_DECREF(v);
2708 Py_DECREF(u);
2709 }
2710
Guido van Rossum681d79a1995-07-18 14:51:37 +00002711 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002712 if (x != NULL && posdefaults > 0) {
2713 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002714 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002715 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002716 x = NULL;
2717 break;
2718 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002719 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002720 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002721 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002722 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002723 if (PyFunction_SetDefaults(x, v) != 0) {
2724 /* Can't happen unless
2725 PyFunction_SetDefaults changes. */
2726 why = WHY_EXCEPTION;
2727 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002728 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002729 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002730 if (x != NULL && kwdefaults > 0) {
2731 v = PyDict_New();
2732 if (v == NULL) {
2733 Py_DECREF(x);
2734 x = NULL;
2735 break;
2736 }
2737 while (--kwdefaults >= 0) {
2738 w = POP(); /* default value */
2739 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002740 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002741 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002742 Py_DECREF(w);
2743 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002744 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002745 if (PyFunction_SetKwDefaults(x, v) != 0) {
2746 /* Can't happen unless
2747 PyFunction_SetKwDefaults changes. */
2748 why = WHY_EXCEPTION;
2749 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002750 Py_DECREF(v);
2751 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002752 PUSH(x);
2753 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002754 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002755
Antoine Pitroub52ec782009-01-25 16:34:23 +00002756 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002757 if (oparg == 3)
2758 w = POP();
2759 else
2760 w = NULL;
2761 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002762 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002763 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002764 Py_DECREF(u);
2765 Py_DECREF(v);
2766 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002767 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002768 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002769 break;
2770
Antoine Pitroub52ec782009-01-25 16:34:23 +00002771 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002772 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002773 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002774 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002775
Antoine Pitroub52ec782009-01-25 16:34:23 +00002776#ifdef USE_COMPUTED_GOTOS
2777 _unknown_opcode:
2778#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002779 default:
2780 fprintf(stderr,
2781 "XXX lineno: %d, opcode: %d\n",
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002782 PyFrame_GetLineNumber(f),
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002783 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002784 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002785 why = WHY_EXCEPTION;
2786 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002787
2788#ifdef CASE_TOO_BIG
2789 }
2790#endif
2791
Guido van Rossum374a9221991-04-04 10:40:29 +00002792 } /* switch */
2793
2794 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002795
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002796 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002797
Guido van Rossum374a9221991-04-04 10:40:29 +00002798 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002799
Guido van Rossum374a9221991-04-04 10:40:29 +00002800 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002801 if (err == 0 && x != NULL) {
2802#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002803 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002804 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002805 fprintf(stderr,
2806 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002807 else {
2808#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002809 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002810 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002811#ifdef CHECKEXC
2812 }
2813#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002814 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002815 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002816 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002817 err = 0;
2818 }
2819
Guido van Rossum374a9221991-04-04 10:40:29 +00002820 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002821
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002822 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002823 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002824 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002825 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002826 why = WHY_EXCEPTION;
2827 }
2828 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002829#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002830 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002831 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002832 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002833 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002834 sprintf(buf, "Stack unwind with exception "
2835 "set and why=%d", why);
2836 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002837 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002838 }
2839#endif
2840
2841 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002842
Guido van Rossum374a9221991-04-04 10:40:29 +00002843 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002844 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002845
Fred Drake8f51f542001-10-04 14:48:42 +00002846 if (tstate->c_tracefunc != NULL)
2847 call_exc_trace(tstate->c_tracefunc,
2848 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002849 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002850
Guido van Rossum374a9221991-04-04 10:40:29 +00002851 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002852
Guido van Rossum374a9221991-04-04 10:40:29 +00002853 if (why == WHY_RERAISE)
2854 why = WHY_EXCEPTION;
2855
2856 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002857
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002858fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002859 while (why != WHY_NOT && f->f_iblock > 0) {
Alexandre Vassalottibfc30992009-07-21 05:23:51 +00002860 /* Peek at the current block. */
2861 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002862
Tim Peters8a5c3c72004-04-05 19:36:21 +00002863 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002864 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002865 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002866 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002867 Py_DECREF(retval);
2868 break;
2869 }
Alexandre Vassalottibfc30992009-07-21 05:23:51 +00002870 /* Now we have to pop the block. */
2871 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002872
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002873 if (b->b_type == EXCEPT_HANDLER) {
2874 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002875 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002876 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002877 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002878 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2879 why = WHY_NOT;
2880 JUMPTO(b->b_handler);
2881 break;
2882 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002883 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2884 || b->b_type == SETUP_FINALLY)) {
2885 PyObject *exc, *val, *tb;
2886 int handler = b->b_handler;
2887 /* Beware, this invalidates all b->b_* fields */
2888 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2889 PUSH(tstate->exc_traceback);
2890 PUSH(tstate->exc_value);
2891 if (tstate->exc_type != NULL) {
2892 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002893 }
2894 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002895 Py_INCREF(Py_None);
2896 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002897 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002898 PyErr_Fetch(&exc, &val, &tb);
2899 /* Make the raw exception data
2900 available to the handler,
2901 so a program can emulate the
2902 Python main loop. */
2903 PyErr_NormalizeException(
2904 &exc, &val, &tb);
2905 PyException_SetTraceback(val, tb);
2906 Py_INCREF(exc);
2907 tstate->exc_type = exc;
2908 Py_INCREF(val);
2909 tstate->exc_value = val;
2910 tstate->exc_traceback = tb;
2911 if (tb == NULL)
2912 tb = Py_None;
2913 Py_INCREF(tb);
2914 PUSH(tb);
2915 PUSH(val);
2916 PUSH(exc);
2917 why = WHY_NOT;
2918 JUMPTO(handler);
2919 break;
2920 }
2921 if (b->b_type == SETUP_FINALLY) {
2922 if (why & (WHY_RETURN | WHY_CONTINUE))
2923 PUSH(retval);
2924 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002925 why = WHY_NOT;
2926 JUMPTO(b->b_handler);
2927 break;
2928 }
2929 } /* unwind stack */
2930
2931 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002932
Guido van Rossum374a9221991-04-04 10:40:29 +00002933 if (why != WHY_NOT)
2934 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002935 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002936
Guido van Rossum374a9221991-04-04 10:40:29 +00002937 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002938
Tim Peters8a5c3c72004-04-05 19:36:21 +00002939 assert(why != WHY_YIELD);
2940 /* Pop remaining stack entries. */
2941 while (!EMPTY()) {
2942 v = POP();
2943 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002944 }
2945
Tim Peters8a5c3c72004-04-05 19:36:21 +00002946 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002947 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002948
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002949fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002950 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002951 if (tstate->c_tracefunc) {
2952 if (why == WHY_RETURN || why == WHY_YIELD) {
2953 if (call_trace(tstate->c_tracefunc,
2954 tstate->c_traceobj, f,
2955 PyTrace_RETURN, retval)) {
2956 Py_XDECREF(retval);
2957 retval = NULL;
2958 why = WHY_EXCEPTION;
2959 }
2960 }
2961 else if (why == WHY_EXCEPTION) {
2962 call_trace_protected(tstate->c_tracefunc,
2963 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002964 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002965 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002966 }
Fred Drake8f51f542001-10-04 14:48:42 +00002967 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00002968 if (why == WHY_EXCEPTION)
2969 call_trace_protected(tstate->c_profilefunc,
2970 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00002971 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00002972 else if (call_trace(tstate->c_profilefunc,
2973 tstate->c_profileobj, f,
2974 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00002975 Py_XDECREF(retval);
2976 retval = NULL;
2977 why = WHY_EXCEPTION;
2978 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00002979 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00002980 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002981
Tim Peters5ca576e2001-06-18 22:08:13 +00002982 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002983exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00002984 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002985 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002986
Guido van Rossum96a42c81992-01-12 02:29:51 +00002987 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002988}
2989
Guido van Rossumc2e20742006-02-27 22:32:47 +00002990/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002991 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002992 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002993
Tim Peters6d6c1a32001-08-02 04:15:00 +00002994PyObject *
2995PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00002996 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00002997 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002998{
2999 register PyFrameObject *f;
3000 register PyObject *retval = NULL;
3001 register PyObject **fastlocals, **freevars;
3002 PyThreadState *tstate = PyThreadState_GET();
3003 PyObject *x, *u;
3004
3005 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00003006 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00003007 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00003008 return NULL;
3009 }
3010
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003011 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003012 assert(globals != NULL);
3013 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00003014 if (f == NULL)
3015 return NULL;
3016
3017 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003018 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003019
3020 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00003021 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00003022 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3023 int i;
3024 int n = argcount;
3025 PyObject *kwdict = NULL;
3026 if (co->co_flags & CO_VARKEYWORDS) {
3027 kwdict = PyDict_New();
3028 if (kwdict == NULL)
3029 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003030 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003031 if (co->co_flags & CO_VARARGS)
3032 i++;
3033 SETLOCAL(i, kwdict);
3034 }
3035 if (argcount > co->co_argcount) {
3036 if (!(co->co_flags & CO_VARARGS)) {
3037 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003038 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003039 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003040 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003041 defcount ? "at most" : "exactly",
3042 co->co_argcount,
3043 kwcount ? "non-keyword " : "",
3044 co->co_argcount == 1 ? "" : "s",
3045 argcount);
3046 goto fail;
3047 }
3048 n = co->co_argcount;
3049 }
3050 for (i = 0; i < n; i++) {
3051 x = args[i];
3052 Py_INCREF(x);
3053 SETLOCAL(i, x);
3054 }
3055 if (co->co_flags & CO_VARARGS) {
3056 u = PyTuple_New(argcount - n);
3057 if (u == NULL)
3058 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003059 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00003060 for (i = n; i < argcount; i++) {
3061 x = args[i];
3062 Py_INCREF(x);
3063 PyTuple_SET_ITEM(u, i-n, x);
3064 }
3065 }
3066 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003067 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003068 PyObject *keyword = kws[2*i];
3069 PyObject *value = kws[2*i + 1];
3070 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00003071 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003072 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003073 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003074 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00003075 goto fail;
3076 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003077 /* Speed hack: do raw pointer compares. As names are
3078 normally interned this should almost always hit. */
3079 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003080 for (j = 0;
3081 j < co->co_argcount + co->co_kwonlyargcount;
3082 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003083 PyObject *nm = co_varnames[j];
3084 if (nm == keyword)
3085 goto kw_found;
3086 }
3087 /* Slow fallback, just in case */
3088 for (j = 0;
3089 j < co->co_argcount + co->co_kwonlyargcount;
3090 j++) {
3091 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003092 int cmp = PyObject_RichCompareBool(
3093 keyword, nm, Py_EQ);
3094 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003095 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003096 else if (cmp < 0)
3097 goto fail;
3098 }
3099 /* Check errors from Compare */
3100 if (PyErr_Occurred())
3101 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003102 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003103 if (kwdict == NULL) {
3104 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003105 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00003106 "keyword argument '%S'",
3107 co->co_name,
3108 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00003109 goto fail;
3110 }
3111 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003112 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003113 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003114kw_found:
3115 if (GETLOCAL(j) != NULL) {
3116 PyErr_Format(PyExc_TypeError,
3117 "%U() got multiple "
3118 "values for keyword "
3119 "argument '%S'",
3120 co->co_name,
3121 keyword);
3122 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003123 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003124 Py_INCREF(value);
3125 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003126 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003127 if (co->co_kwonlyargcount > 0) {
3128 for (i = co->co_argcount;
3129 i < co->co_argcount + co->co_kwonlyargcount;
3130 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003131 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003132 if (GETLOCAL(i) != NULL)
3133 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003134 name = PyTuple_GET_ITEM(co->co_varnames, i);
3135 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003136 if (kwdefs != NULL)
3137 def = PyDict_GetItem(kwdefs, name);
3138 if (def != NULL) {
3139 Py_INCREF(def);
3140 SETLOCAL(i, def);
3141 continue;
3142 }
3143 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003144 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003145 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003146 goto fail;
3147 }
3148 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003149 if (argcount < co->co_argcount) {
3150 int m = co->co_argcount - defcount;
3151 for (i = argcount; i < m; i++) {
3152 if (GETLOCAL(i) == NULL) {
3153 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003154 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003155 "%spositional argument%s "
3156 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003157 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003158 ((co->co_flags & CO_VARARGS) ||
3159 defcount) ? "at least"
3160 : "exactly",
3161 m, kwcount ? "non-keyword " : "",
3162 m == 1 ? "" : "s", i);
3163 goto fail;
3164 }
3165 }
3166 if (n > m)
3167 i = n - m;
3168 else
3169 i = 0;
3170 for (; i < defcount; i++) {
3171 if (GETLOCAL(m+i) == NULL) {
3172 PyObject *def = defs[i];
3173 Py_INCREF(def);
3174 SETLOCAL(m+i, def);
3175 }
3176 }
3177 }
3178 }
3179 else {
3180 if (argcount > 0 || kwcount > 0) {
3181 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003182 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003183 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003184 argcount + kwcount);
3185 goto fail;
3186 }
3187 }
3188 /* Allocate and initialize storage for cell vars, and copy free
3189 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003190 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003191 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003192 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003193 PyObject *c;
3194
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003195 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003196 if (co->co_flags & CO_VARARGS)
3197 nargs++;
3198 if (co->co_flags & CO_VARKEYWORDS)
3199 nargs++;
3200
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 /* Initialize each cell var, taking into account
3202 cell vars that are initialized from arguments.
3203
3204 Should arrange for the compiler to put cellvars
3205 that are arguments at the beginning of the cellvars
3206 list so that we can march over it more efficiently?
3207 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003208 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003209 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003210 PyTuple_GET_ITEM(co->co_cellvars, i));
3211 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003213 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003214 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003215 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003216 c = PyCell_New(GETLOCAL(j));
3217 if (c == NULL)
3218 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003219 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003220 found = 1;
3221 break;
3222 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003223 }
3224 if (found == 0) {
3225 c = PyCell_New(NULL);
3226 if (c == NULL)
3227 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003228 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003229 }
3230 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003231 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003232 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003233 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003234 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003235 PyObject *o = PyTuple_GET_ITEM(closure, i);
3236 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003237 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003238 }
3239 }
3240
Tim Peters5ca576e2001-06-18 22:08:13 +00003241 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003242 /* Don't need to keep the reference to f_back, it will be set
3243 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003244 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003245 f->f_back = NULL;
3246
Jeremy Hylton985eba52003-02-05 23:13:00 +00003247 PCALL(PCALL_GENERATOR);
3248
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003249 /* Create a new generator that owns the ready to run frame
3250 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003251 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003252 }
3253
Thomas Woutersce272b62007-09-19 21:19:28 +00003254 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003255
Thomas Woutersce272b62007-09-19 21:19:28 +00003256fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003257
Tim Petersb13680b2001-11-27 23:29:29 +00003258 /* decref'ing the frame can cause __del__ methods to get invoked,
3259 which can call back into Python. While we're done with the
3260 current Python frame (f), the associated C stack is still in use,
3261 so recursion_depth must be boosted for the duration.
3262 */
3263 assert(tstate != NULL);
3264 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003265 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003266 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003267 return retval;
3268}
3269
3270
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003271static PyObject *
3272special_lookup(PyObject *o, char *meth, PyObject **cache)
3273{
3274 PyObject *res;
3275 res = _PyObject_LookupSpecial(o, meth, cache);
3276 if (res == NULL && !PyErr_Occurred()) {
3277 PyErr_SetObject(PyExc_AttributeError, *cache);
3278 return NULL;
3279 }
3280 return res;
3281}
3282
3283
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003284/* Logic for the raise statement (too complicated for inlining).
3285 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003286static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003287do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003288{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003289 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003290
3291 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003292 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003293 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003294 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003295 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003296 value = tstate->exc_value;
3297 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003298 if (type == Py_None) {
3299 PyErr_SetString(PyExc_RuntimeError,
3300 "No active exception to reraise");
3301 return WHY_EXCEPTION;
3302 }
3303 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003304 Py_XINCREF(value);
3305 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003306 PyErr_Restore(type, value, tb);
3307 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003308 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003309
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003310 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003311 raise
3312 raise <instance>
3313 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003314
Collin Winter828f04a2007-08-31 00:04:24 +00003315 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003316 type = exc;
3317 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003318 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003319 goto raise_error;
3320 }
Collin Winter828f04a2007-08-31 00:04:24 +00003321 else if (PyExceptionInstance_Check(exc)) {
3322 value = exc;
3323 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003324 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003325 }
3326 else {
3327 /* Not something you can raise. You get an exception
3328 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003329 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003330 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003331 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003332 goto raise_error;
3333 }
Collin Winter828f04a2007-08-31 00:04:24 +00003334
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003335 if (cause) {
3336 PyObject *fixed_cause;
3337 if (PyExceptionClass_Check(cause)) {
3338 fixed_cause = PyObject_CallObject(cause, NULL);
3339 if (fixed_cause == NULL)
3340 goto raise_error;
3341 Py_DECREF(cause);
3342 }
3343 else if (PyExceptionInstance_Check(cause)) {
3344 fixed_cause = cause;
3345 }
3346 else {
3347 PyErr_SetString(PyExc_TypeError,
3348 "exception causes must derive from "
3349 "BaseException");
3350 goto raise_error;
3351 }
3352 PyException_SetCause(value, fixed_cause);
3353 }
Collin Winter828f04a2007-08-31 00:04:24 +00003354
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003355 PyErr_SetObject(type, value);
3356 /* PyErr_SetObject incref's its arguments */
3357 Py_XDECREF(value);
3358 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003359 return WHY_EXCEPTION;
3360
3361raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003362 Py_XDECREF(value);
3363 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003364 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003365 return WHY_EXCEPTION;
3366}
3367
Tim Petersd6d010b2001-06-21 02:49:55 +00003368/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003369 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003370
Guido van Rossum0368b722007-05-11 16:50:42 +00003371 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3372 with a variable target.
3373*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003374
Barry Warsawe42b18f1997-08-25 22:13:04 +00003375static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003376unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003377{
Guido van Rossum0368b722007-05-11 16:50:42 +00003378 int i = 0, j = 0;
3379 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003380 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003381 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003382 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003383
Tim Petersd6d010b2001-06-21 02:49:55 +00003384 assert(v != NULL);
3385
3386 it = PyObject_GetIter(v);
3387 if (it == NULL)
3388 goto Error;
3389
3390 for (; i < argcnt; i++) {
3391 w = PyIter_Next(it);
3392 if (w == NULL) {
3393 /* Iterator done, via error or exhaustion. */
3394 if (!PyErr_Occurred()) {
3395 PyErr_Format(PyExc_ValueError,
3396 "need more than %d value%s to unpack",
3397 i, i == 1 ? "" : "s");
3398 }
3399 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003400 }
3401 *--sp = w;
3402 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003403
Guido van Rossum0368b722007-05-11 16:50:42 +00003404 if (argcntafter == -1) {
3405 /* We better have exhausted the iterator now. */
3406 w = PyIter_Next(it);
3407 if (w == NULL) {
3408 if (PyErr_Occurred())
3409 goto Error;
3410 Py_DECREF(it);
3411 return 1;
3412 }
3413 Py_DECREF(w);
3414 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3415 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003416 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003417
3418 l = PySequence_List(it);
3419 if (l == NULL)
3420 goto Error;
3421 *--sp = l;
3422 i++;
3423
3424 ll = PyList_GET_SIZE(l);
3425 if (ll < argcntafter) {
3426 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3427 argcnt + ll);
3428 goto Error;
3429 }
3430
3431 /* Pop the "after-variable" args off the list. */
3432 for (j = argcntafter; j > 0; j--, i++) {
3433 *--sp = PyList_GET_ITEM(l, ll - j);
3434 }
3435 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003436 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003437 Py_DECREF(it);
3438 return 1;
3439
Tim Petersd6d010b2001-06-21 02:49:55 +00003440Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003441 for (; i > 0; i--, sp++)
3442 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003443 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003444 return 0;
3445}
3446
3447
Guido van Rossum96a42c81992-01-12 02:29:51 +00003448#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003449static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003450prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003451{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003452 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003453 if (PyObject_Print(v, stdout, 0) != 0)
3454 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003455 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003456 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003457}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003458#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003459
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003460static void
Fred Drake5755ce62001-06-27 19:19:46 +00003461call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003462{
Guido van Rossumb209a111997-04-29 18:18:01 +00003463 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003464 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003465 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003466 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003467 value = Py_None;
3468 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003469 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003470 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003471 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003472 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003473 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003474 }
Fred Drake5755ce62001-06-27 19:19:46 +00003475 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003476 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003477 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003478 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003479 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003480 Py_XDECREF(type);
3481 Py_XDECREF(value);
3482 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003483 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003484}
3485
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003486static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003487call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003488 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003489{
3490 PyObject *type, *value, *traceback;
3491 int err;
3492 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003493 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003494 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003495 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003496 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003497 return 0;
3498 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003499 else {
3500 Py_XDECREF(type);
3501 Py_XDECREF(value);
3502 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003503 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003504 }
3505}
3506
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003507static int
Fred Drake5755ce62001-06-27 19:19:46 +00003508call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3509 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003510{
Fred Drake5755ce62001-06-27 19:19:46 +00003511 register PyThreadState *tstate = frame->f_tstate;
3512 int result;
3513 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003514 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003515 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003516 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003517 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003518 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3519 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003520 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003521 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003522}
3523
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003524PyObject *
3525_PyEval_CallTracing(PyObject *func, PyObject *args)
3526{
3527 PyFrameObject *frame = PyEval_GetFrame();
3528 PyThreadState *tstate = frame->f_tstate;
3529 int save_tracing = tstate->tracing;
3530 int save_use_tracing = tstate->use_tracing;
3531 PyObject *result;
3532
3533 tstate->tracing = 0;
3534 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3535 || (tstate->c_profilefunc != NULL));
3536 result = PyObject_Call(func, args, NULL);
3537 tstate->tracing = save_tracing;
3538 tstate->use_tracing = save_use_tracing;
3539 return result;
3540}
3541
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003542/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003543static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003544maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003545 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3546 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003547{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003548 int result = 0;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003549 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003550
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003551 /* If the last instruction executed isn't in the current
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003552 instruction window, reset the window.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003553 */
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003554 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003555 PyAddrPair bounds;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003556 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3557 &bounds);
Thomas Woutersce272b62007-09-19 21:19:28 +00003558 *instr_lb = bounds.ap_lower;
3559 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003560 }
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003561 /* If the last instruction falls at the start of a line or if
3562 it represents a jump backwards, update the frame's line
3563 number and call the trace function. */
3564 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3565 frame->f_lineno = line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003566 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003567 }
3568 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003569 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003570}
3571
Fred Drake5755ce62001-06-27 19:19:46 +00003572void
3573PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003574{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003575 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003576 PyObject *temp = tstate->c_profileobj;
3577 Py_XINCREF(arg);
3578 tstate->c_profilefunc = NULL;
3579 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003580 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003581 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003582 Py_XDECREF(temp);
3583 tstate->c_profilefunc = func;
3584 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003585 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003586 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003587}
3588
3589void
3590PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3591{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003592 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003593 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003594 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003595 Py_XINCREF(arg);
3596 tstate->c_tracefunc = NULL;
3597 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003598 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003599 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003600 Py_XDECREF(temp);
3601 tstate->c_tracefunc = func;
3602 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003603 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003604 tstate->use_tracing = ((func != NULL)
3605 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003606}
3607
Guido van Rossumb209a111997-04-29 18:18:01 +00003608PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003609PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003610{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003611 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003612 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003613 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003614 else
3615 return current_frame->f_builtins;
3616}
3617
Guido van Rossumb209a111997-04-29 18:18:01 +00003618PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003619PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003620{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003621 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003622 if (current_frame == NULL)
3623 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003624 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003625 return current_frame->f_locals;
3626}
3627
Guido van Rossumb209a111997-04-29 18:18:01 +00003628PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003629PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003630{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003631 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003632 if (current_frame == NULL)
3633 return NULL;
3634 else
3635 return current_frame->f_globals;
3636}
3637
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003638PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003639PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003640{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003641 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003642 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003643}
3644
Guido van Rossum6135a871995-01-09 17:53:26 +00003645int
Tim Peters5ba58662001-07-16 02:29:45 +00003646PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003647{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003648 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003649 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003650
3651 if (current_frame != NULL) {
3652 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003653 const int compilerflags = codeflags & PyCF_MASK;
3654 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003655 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003656 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003657 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003658#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003659 if (codeflags & CO_GENERATOR_ALLOWED) {
3660 result = 1;
3661 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3662 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003663#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003664 }
3665 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003666}
3667
Guido van Rossum3f5da241990-12-20 15:06:42 +00003668
Guido van Rossum681d79a1995-07-18 14:51:37 +00003669/* External interface to call any callable object.
3670 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003671
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003672#undef PyEval_CallObject
3673/* for backward compatibility: export this interface */
3674
Guido van Rossumb209a111997-04-29 18:18:01 +00003675PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003676PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003677{
Guido van Rossumb209a111997-04-29 18:18:01 +00003678 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003679}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003680#define PyEval_CallObject(func,arg) \
3681 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003682
Guido van Rossumb209a111997-04-29 18:18:01 +00003683PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003684PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003685{
Jeremy Hylton52820442001-01-03 23:52:36 +00003686 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003687
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003688 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003689 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003690 if (arg == NULL)
3691 return NULL;
3692 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003693 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003694 PyErr_SetString(PyExc_TypeError,
3695 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003696 return NULL;
3697 }
3698 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003699 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003700
Guido van Rossumb209a111997-04-29 18:18:01 +00003701 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003702 PyErr_SetString(PyExc_TypeError,
3703 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003704 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003705 return NULL;
3706 }
3707
Tim Peters6d6c1a32001-08-02 04:15:00 +00003708 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003709 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003710 return result;
3711}
3712
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003713const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003714PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003715{
3716 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003717 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003718 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003719 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003720 else if (PyCFunction_Check(func))
3721 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003722 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003723 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003724}
3725
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003726const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003727PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003728{
3729 if (PyMethod_Check(func))
3730 return "()";
3731 else if (PyFunction_Check(func))
3732 return "()";
3733 else if (PyCFunction_Check(func))
3734 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003735 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003736 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003737}
3738
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003739static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003740err_args(PyObject *func, int flags, int nargs)
3741{
3742 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003743 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003744 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003745 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003746 nargs);
3747 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003748 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003749 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003750 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003751 nargs);
3752}
3753
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003754#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003755if (tstate->use_tracing && tstate->c_profilefunc) { \
3756 if (call_trace(tstate->c_profilefunc, \
3757 tstate->c_profileobj, \
3758 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003759 func)) { \
3760 x = NULL; \
3761 } \
3762 else { \
3763 x = call; \
3764 if (tstate->c_profilefunc != NULL) { \
3765 if (x == NULL) { \
3766 call_trace_protected(tstate->c_profilefunc, \
3767 tstate->c_profileobj, \
3768 tstate->frame, PyTrace_C_EXCEPTION, \
3769 func); \
3770 /* XXX should pass (type, value, tb) */ \
3771 } else { \
3772 if (call_trace(tstate->c_profilefunc, \
3773 tstate->c_profileobj, \
3774 tstate->frame, PyTrace_C_RETURN, \
3775 func)) { \
3776 Py_DECREF(x); \
3777 x = NULL; \
3778 } \
3779 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003780 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003781 } \
3782} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003783 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003784 }
3785
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003786static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003787call_function(PyObject ***pp_stack, int oparg
3788#ifdef WITH_TSC
3789 , uint64* pintr0, uint64* pintr1
3790#endif
3791 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003792{
3793 int na = oparg & 0xff;
3794 int nk = (oparg>>8) & 0xff;
3795 int n = na + 2 * nk;
3796 PyObject **pfunc = (*pp_stack) - n - 1;
3797 PyObject *func = *pfunc;
3798 PyObject *x, *w;
3799
Jeremy Hylton985eba52003-02-05 23:13:00 +00003800 /* Always dispatch PyCFunction first, because these are
3801 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003802 */
3803 if (PyCFunction_Check(func) && nk == 0) {
3804 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003805 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003806
3807 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003808 if (flags & (METH_NOARGS | METH_O)) {
3809 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3810 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003811 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003812 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003813 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003814 else if (flags & METH_O && na == 1) {
3815 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003816 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003817 Py_DECREF(arg);
3818 }
3819 else {
3820 err_args(func, flags, na);
3821 x = NULL;
3822 }
3823 }
3824 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003825 PyObject *callargs;
3826 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003827 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003828 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003829 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003830 Py_XDECREF(callargs);
3831 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003832 } else {
3833 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3834 /* optimize access to bound methods */
3835 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003836 PCALL(PCALL_METHOD);
3837 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003838 Py_INCREF(self);
3839 func = PyMethod_GET_FUNCTION(func);
3840 Py_INCREF(func);
3841 Py_DECREF(*pfunc);
3842 *pfunc = self;
3843 na++;
3844 n++;
3845 } else
3846 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003847 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003848 if (PyFunction_Check(func))
3849 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003850 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003851 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003852 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003853 Py_DECREF(func);
3854 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003855
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003856 /* Clear the stack of the function object. Also removes
3857 the arguments in case they weren't consumed already
3858 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003859 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003860 while ((*pp_stack) > pfunc) {
3861 w = EXT_POP(*pp_stack);
3862 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003863 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003864 }
3865 return x;
3866}
3867
Jeremy Hylton192690e2002-08-16 18:36:11 +00003868/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003869 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003870 For the simplest case -- a function that takes only positional
3871 arguments and is called with only positional arguments -- it
3872 inlines the most primitive frame setup code from
3873 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3874 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003875*/
3876
3877static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003878fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003879{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003880 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003881 PyObject *globals = PyFunction_GET_GLOBALS(func);
3882 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003883 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003884 PyObject **d = NULL;
3885 int nd = 0;
3886
Jeremy Hylton985eba52003-02-05 23:13:00 +00003887 PCALL(PCALL_FUNCTION);
3888 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003889 if (argdefs == NULL && co->co_argcount == n &&
3890 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003891 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3892 PyFrameObject *f;
3893 PyObject *retval = NULL;
3894 PyThreadState *tstate = PyThreadState_GET();
3895 PyObject **fastlocals, **stack;
3896 int i;
3897
3898 PCALL(PCALL_FASTER_FUNCTION);
3899 assert(globals != NULL);
3900 /* XXX Perhaps we should create a specialized
3901 PyFrame_New() that doesn't take locals, but does
3902 take builtins without sanity checking them.
3903 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003904 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003905 f = PyFrame_New(tstate, co, globals, NULL);
3906 if (f == NULL)
3907 return NULL;
3908
3909 fastlocals = f->f_localsplus;
3910 stack = (*pp_stack) - n;
3911
3912 for (i = 0; i < n; i++) {
3913 Py_INCREF(*stack);
3914 fastlocals[i] = *stack++;
3915 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003916 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003917 ++tstate->recursion_depth;
3918 Py_DECREF(f);
3919 --tstate->recursion_depth;
3920 return retval;
3921 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003922 if (argdefs != NULL) {
3923 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003924 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003925 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003926 return PyEval_EvalCodeEx(co, globals,
3927 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003928 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003929 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003930}
3931
3932static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003933update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3934 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003935{
3936 PyObject *kwdict = NULL;
3937 if (orig_kwdict == NULL)
3938 kwdict = PyDict_New();
3939 else {
3940 kwdict = PyDict_Copy(orig_kwdict);
3941 Py_DECREF(orig_kwdict);
3942 }
3943 if (kwdict == NULL)
3944 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003945 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003946 int err;
3947 PyObject *value = EXT_POP(*pp_stack);
3948 PyObject *key = EXT_POP(*pp_stack);
3949 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003950 PyErr_Format(PyExc_TypeError,
3951 "%.200s%s got multiple values "
3952 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003953 PyEval_GetFuncName(func),
3954 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003955 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003956 Py_DECREF(key);
3957 Py_DECREF(value);
3958 Py_DECREF(kwdict);
3959 return NULL;
3960 }
3961 err = PyDict_SetItem(kwdict, key, value);
3962 Py_DECREF(key);
3963 Py_DECREF(value);
3964 if (err) {
3965 Py_DECREF(kwdict);
3966 return NULL;
3967 }
3968 }
3969 return kwdict;
3970}
3971
3972static PyObject *
3973update_star_args(int nstack, int nstar, PyObject *stararg,
3974 PyObject ***pp_stack)
3975{
3976 PyObject *callargs, *w;
3977
3978 callargs = PyTuple_New(nstack + nstar);
3979 if (callargs == NULL) {
3980 return NULL;
3981 }
3982 if (nstar) {
3983 int i;
3984 for (i = 0; i < nstar; i++) {
3985 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3986 Py_INCREF(a);
3987 PyTuple_SET_ITEM(callargs, nstack + i, a);
3988 }
3989 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003990 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003991 w = EXT_POP(*pp_stack);
3992 PyTuple_SET_ITEM(callargs, nstack, w);
3993 }
3994 return callargs;
3995}
3996
3997static PyObject *
3998load_args(PyObject ***pp_stack, int na)
3999{
4000 PyObject *args = PyTuple_New(na);
4001 PyObject *w;
4002
4003 if (args == NULL)
4004 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004005 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004006 w = EXT_POP(*pp_stack);
4007 PyTuple_SET_ITEM(args, na, w);
4008 }
4009 return args;
4010}
4011
4012static PyObject *
4013do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4014{
4015 PyObject *callargs = NULL;
4016 PyObject *kwdict = NULL;
4017 PyObject *result = NULL;
4018
4019 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004020 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004021 if (kwdict == NULL)
4022 goto call_fail;
4023 }
4024 callargs = load_args(pp_stack, na);
4025 if (callargs == NULL)
4026 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004027#ifdef CALL_PROFILE
4028 /* At this point, we have to look at the type of func to
4029 update the call stats properly. Do it here so as to avoid
4030 exposing the call stats machinery outside ceval.c
4031 */
4032 if (PyFunction_Check(func))
4033 PCALL(PCALL_FUNCTION);
4034 else if (PyMethod_Check(func))
4035 PCALL(PCALL_METHOD);
4036 else if (PyType_Check(func))
4037 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004038 else if (PyCFunction_Check(func))
4039 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004040 else
4041 PCALL(PCALL_OTHER);
4042#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004043 if (PyCFunction_Check(func)) {
4044 PyThreadState *tstate = PyThreadState_GET();
4045 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4046 }
4047 else
4048 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004049call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004050 Py_XDECREF(callargs);
4051 Py_XDECREF(kwdict);
4052 return result;
4053}
4054
4055static PyObject *
4056ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4057{
4058 int nstar = 0;
4059 PyObject *callargs = NULL;
4060 PyObject *stararg = NULL;
4061 PyObject *kwdict = NULL;
4062 PyObject *result = NULL;
4063
4064 if (flags & CALL_FLAG_KW) {
4065 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004066 if (!PyDict_Check(kwdict)) {
4067 PyObject *d;
4068 d = PyDict_New();
4069 if (d == NULL)
4070 goto ext_call_fail;
4071 if (PyDict_Update(d, kwdict) != 0) {
4072 Py_DECREF(d);
4073 /* PyDict_Update raises attribute
4074 * error (percolated from an attempt
4075 * to get 'keys' attribute) instead of
4076 * a type error if its second argument
4077 * is not a mapping.
4078 */
4079 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4080 PyErr_Format(PyExc_TypeError,
4081 "%.200s%.200s argument after ** "
4082 "must be a mapping, not %.200s",
4083 PyEval_GetFuncName(func),
4084 PyEval_GetFuncDesc(func),
4085 kwdict->ob_type->tp_name);
4086 }
4087 goto ext_call_fail;
4088 }
4089 Py_DECREF(kwdict);
4090 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004091 }
4092 }
4093 if (flags & CALL_FLAG_VAR) {
4094 stararg = EXT_POP(*pp_stack);
4095 if (!PyTuple_Check(stararg)) {
4096 PyObject *t = NULL;
4097 t = PySequence_Tuple(stararg);
4098 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004099 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4100 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004101 "%.200s%.200s argument after * "
4102 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004103 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004104 PyEval_GetFuncDesc(func),
4105 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004106 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004107 goto ext_call_fail;
4108 }
4109 Py_DECREF(stararg);
4110 stararg = t;
4111 }
4112 nstar = PyTuple_GET_SIZE(stararg);
4113 }
4114 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004115 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004116 if (kwdict == NULL)
4117 goto ext_call_fail;
4118 }
4119 callargs = update_star_args(na, nstar, stararg, pp_stack);
4120 if (callargs == NULL)
4121 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004122#ifdef CALL_PROFILE
4123 /* At this point, we have to look at the type of func to
4124 update the call stats properly. Do it here so as to avoid
4125 exposing the call stats machinery outside ceval.c
4126 */
4127 if (PyFunction_Check(func))
4128 PCALL(PCALL_FUNCTION);
4129 else if (PyMethod_Check(func))
4130 PCALL(PCALL_METHOD);
4131 else if (PyType_Check(func))
4132 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004133 else if (PyCFunction_Check(func))
4134 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004135 else
4136 PCALL(PCALL_OTHER);
4137#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004138 if (PyCFunction_Check(func)) {
4139 PyThreadState *tstate = PyThreadState_GET();
4140 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4141 }
4142 else
4143 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004144ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004145 Py_XDECREF(callargs);
4146 Py_XDECREF(kwdict);
4147 Py_XDECREF(stararg);
4148 return result;
4149}
4150
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004151/* Extract a slice index from a PyInt or PyLong or an object with the
4152 nb_index slot defined, and store in *pi.
4153 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4154 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 +00004155 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004156*/
Tim Petersb5196382001-12-16 19:44:20 +00004157/* Note: If v is NULL, return success without storing into *pi. This
4158 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4159 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004160*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004161int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004162_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004163{
Tim Petersb5196382001-12-16 19:44:20 +00004164 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004165 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004166 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004167 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004168 if (x == -1 && PyErr_Occurred())
4169 return 0;
4170 }
4171 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004172 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004173 "slice indices must be integers or "
4174 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004175 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004176 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004177 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004178 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004179 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004180}
4181
Guido van Rossum486364b2007-06-30 05:01:58 +00004182#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004183 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004184
Guido van Rossumb209a111997-04-29 18:18:01 +00004185static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004186cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004187{
Guido van Rossumac7be682001-01-17 15:42:30 +00004188 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004189 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004190 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004191 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004192 break;
4193 case PyCmp_IS_NOT:
4194 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004195 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004196 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004197 res = PySequence_Contains(w, v);
4198 if (res < 0)
4199 return NULL;
4200 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004201 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004202 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004203 if (res < 0)
4204 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004205 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004206 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004207 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004208 if (PyTuple_Check(w)) {
4209 Py_ssize_t i, length;
4210 length = PyTuple_Size(w);
4211 for (i = 0; i < length; i += 1) {
4212 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004213 if (!PyExceptionClass_Check(exc)) {
4214 PyErr_SetString(PyExc_TypeError,
4215 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004216 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004217 }
4218 }
4219 }
4220 else {
Brett Cannon39590462007-02-26 22:01:14 +00004221 if (!PyExceptionClass_Check(w)) {
4222 PyErr_SetString(PyExc_TypeError,
4223 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004224 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004225 }
4226 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004227 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004228 break;
4229 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004230 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004231 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004232 v = res ? Py_True : Py_False;
4233 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004234 return v;
4235}
4236
Thomas Wouters52152252000-08-17 22:55:00 +00004237static PyObject *
4238import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004239{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004240 PyObject *x;
4241
4242 x = PyObject_GetAttr(v, name);
4243 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004244 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004245 }
Thomas Wouters52152252000-08-17 22:55:00 +00004246 return x;
4247}
Guido van Rossumac7be682001-01-17 15:42:30 +00004248
Thomas Wouters52152252000-08-17 22:55:00 +00004249static int
4250import_all_from(PyObject *locals, PyObject *v)
4251{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004252 PyObject *all = PyObject_GetAttrString(v, "__all__");
4253 PyObject *dict, *name, *value;
4254 int skip_leading_underscores = 0;
4255 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004256
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004257 if (all == NULL) {
4258 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4259 return -1; /* Unexpected error */
4260 PyErr_Clear();
4261 dict = PyObject_GetAttrString(v, "__dict__");
4262 if (dict == NULL) {
4263 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4264 return -1;
4265 PyErr_SetString(PyExc_ImportError,
4266 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004267 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004268 }
4269 all = PyMapping_Keys(dict);
4270 Py_DECREF(dict);
4271 if (all == NULL)
4272 return -1;
4273 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004274 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004275
4276 for (pos = 0, err = 0; ; pos++) {
4277 name = PySequence_GetItem(all, pos);
4278 if (name == NULL) {
4279 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4280 err = -1;
4281 else
4282 PyErr_Clear();
4283 break;
4284 }
4285 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004286 PyUnicode_Check(name) &&
4287 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004288 {
4289 Py_DECREF(name);
4290 continue;
4291 }
4292 value = PyObject_GetAttr(v, name);
4293 if (value == NULL)
4294 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004295 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004296 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004297 else
4298 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004299 Py_DECREF(name);
4300 Py_XDECREF(value);
4301 if (err != 0)
4302 break;
4303 }
4304 Py_DECREF(all);
4305 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004306}
4307
Guido van Rossumac7be682001-01-17 15:42:30 +00004308static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004309format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004310{
Neal Norwitzda059e32007-08-26 05:33:45 +00004311 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004312
4313 if (!obj)
4314 return;
4315
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004316 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004317 if (!obj_str)
4318 return;
4319
4320 PyErr_Format(exc, format_str, obj_str);
4321}
Guido van Rossum950361c1997-01-24 13:49:28 +00004322
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004323static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004324unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004325 PyFrameObject *f, unsigned char *next_instr)
4326{
4327 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004328 are (Unicode) strings. */
4329 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4330 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004331 Py_ssize_t new_len = v_len + w_len;
4332 if (new_len < 0) {
4333 PyErr_SetString(PyExc_OverflowError,
4334 "strings are too large to concat");
4335 return NULL;
4336 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004337
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004338 if (v->ob_refcnt == 2) {
4339 /* In the common case, there are 2 references to the value
4340 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004341 * value stack (in 'v') and one still stored in the
4342 * 'variable'. We try to delete the variable now to reduce
4343 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004344 */
4345 switch (*next_instr) {
4346 case STORE_FAST:
4347 {
4348 int oparg = PEEKARG();
4349 PyObject **fastlocals = f->f_localsplus;
4350 if (GETLOCAL(oparg) == v)
4351 SETLOCAL(oparg, NULL);
4352 break;
4353 }
4354 case STORE_DEREF:
4355 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004356 PyObject **freevars = (f->f_localsplus +
4357 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004358 PyObject *c = freevars[PEEKARG()];
4359 if (PyCell_GET(c) == v)
4360 PyCell_Set(c, NULL);
4361 break;
4362 }
4363 case STORE_NAME:
4364 {
4365 PyObject *names = f->f_code->co_names;
4366 PyObject *name = GETITEM(names, PEEKARG());
4367 PyObject *locals = f->f_locals;
4368 if (PyDict_CheckExact(locals) &&
4369 PyDict_GetItem(locals, name) == v) {
4370 if (PyDict_DelItem(locals, name) != 0) {
4371 PyErr_Clear();
4372 }
4373 }
4374 break;
4375 }
4376 }
4377 }
4378
Guido van Rossum98297ee2007-11-06 21:34:58 +00004379 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004380 /* Now we own the last reference to 'v', so we can resize it
4381 * in-place.
4382 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004383 if (PyUnicode_Resize(&v, new_len) != 0) {
4384 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004385 * deallocated so it cannot be put back into
4386 * 'variable'. The MemoryError is raised when there
4387 * is no value in 'variable', which might (very
4388 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004389 */
4390 return NULL;
4391 }
4392 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004393 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4394 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004395 return v;
4396 }
4397 else {
4398 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004399 w = PyUnicode_Concat(v, w);
4400 Py_DECREF(v);
4401 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004402 }
4403}
4404
Guido van Rossum950361c1997-01-24 13:49:28 +00004405#ifdef DYNAMIC_EXECUTION_PROFILE
4406
Skip Montanarof118cb12001-10-15 20:51:38 +00004407static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004408getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004409{
4410 int i;
4411 PyObject *l = PyList_New(256);
4412 if (l == NULL) return NULL;
4413 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004414 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004415 if (x == NULL) {
4416 Py_DECREF(l);
4417 return NULL;
4418 }
4419 PyList_SetItem(l, i, x);
4420 }
4421 for (i = 0; i < 256; i++)
4422 a[i] = 0;
4423 return l;
4424}
4425
4426PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004427_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004428{
4429#ifndef DXPAIRS
4430 return getarray(dxp);
4431#else
4432 int i;
4433 PyObject *l = PyList_New(257);
4434 if (l == NULL) return NULL;
4435 for (i = 0; i < 257; i++) {
4436 PyObject *x = getarray(dxpairs[i]);
4437 if (x == NULL) {
4438 Py_DECREF(l);
4439 return NULL;
4440 }
4441 PyList_SetItem(l, i, x);
4442 }
4443 return l;
4444#endif
4445}
4446
4447#endif