blob: 870d19d55ca27c0ca0cb4d4c28f90858ad676024 [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 Rossum10dc2e81990-11-18 17:27:39 +000016#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000017#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018
Guido van Rossumc6004111993-11-05 10:22:19 +000019#include <ctype.h>
20
Thomas Wouters477c8d52006-05-27 19:21:47 +000021#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000022
23#define READ_TIMESTAMP(var)
24
25#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000026
27typedef unsigned long long uint64;
28
Ezio Melotti13925002011-03-16 11:05:33 +020029/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000030 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
31 "__powerpc__" appears to be the correct one for Linux with GCC
32*/
33#if defined(__ppc__) || defined (__powerpc__)
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 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;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
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,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 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",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
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: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#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 ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 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 *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +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 *,
Stefan Krahb7e10102010-06-23 18:42:39 +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 *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000138static void format_exc_unbound(PyCodeObject *co, int oparg);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000139static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000140
Paul Prescode68140d2000-08-30 20:25:01 +0000141#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000143#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000145#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000147#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 "free variable '%.200s' referenced before assignment" \
149 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000150
Guido van Rossum950361c1997-01-24 13:49:28 +0000151/* Dynamic execution profile */
152#ifdef DYNAMIC_EXECUTION_PROFILE
153#ifdef DXPAIRS
154static long dxpairs[257][256];
155#define dxp dxpairs[256]
156#else
157static long dxp[256];
158#endif
159#endif
160
Jeremy Hylton985eba52003-02-05 23:13:00 +0000161/* Function call profile */
162#ifdef CALL_PROFILE
163#define PCALL_NUM 11
164static int pcall[PCALL_NUM];
165
166#define PCALL_ALL 0
167#define PCALL_FUNCTION 1
168#define PCALL_FAST_FUNCTION 2
169#define PCALL_FASTER_FUNCTION 3
170#define PCALL_METHOD 4
171#define PCALL_BOUND_METHOD 5
172#define PCALL_CFUNCTION 6
173#define PCALL_TYPE 7
174#define PCALL_GENERATOR 8
175#define PCALL_OTHER 9
176#define PCALL_POP 10
177
178/* Notes about the statistics
179
180 PCALL_FAST stats
181
182 FAST_FUNCTION means no argument tuple needs to be created.
183 FASTER_FUNCTION means that the fast-path frame setup code is used.
184
185 If there is a method call where the call can be optimized by changing
186 the argument tuple and calling the function directly, it gets recorded
187 twice.
188
189 As a result, the relationship among the statistics appears to be
190 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
191 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
192 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
193 PCALL_METHOD > PCALL_BOUND_METHOD
194*/
195
196#define PCALL(POS) pcall[POS]++
197
198PyObject *
199PyEval_GetCallStats(PyObject *self)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 return Py_BuildValue("iiiiiiiiiii",
202 pcall[0], pcall[1], pcall[2], pcall[3],
203 pcall[4], pcall[5], pcall[6], pcall[7],
204 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000205}
206#else
207#define PCALL(O)
208
209PyObject *
210PyEval_GetCallStats(PyObject *self)
211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 Py_INCREF(Py_None);
213 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000214}
215#endif
216
Tim Peters5ca576e2001-06-18 22:08:13 +0000217
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000218#ifdef WITH_THREAD
219#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
220#else
221#define GIL_REQUEST 0
222#endif
223
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000224/* This can set eval_breaker to 0 even though gil_drop_request became
225 1. We believe this is all right because the eval loop will release
226 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000227#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 _Py_atomic_store_relaxed( \
229 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000230 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
232 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000233
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000234#ifdef WITH_THREAD
235
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000236#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 do { \
238 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
239 _Py_atomic_store_relaxed(&eval_breaker, 1); \
240 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241
242#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 do { \
244 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
245 COMPUTE_EVAL_BREAKER(); \
246 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000247
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000248#endif
249
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000250/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000251#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 do { \
253 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
254 _Py_atomic_store_relaxed(&eval_breaker, 1); \
255 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000256
257#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 do { \
259 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
260 COMPUTE_EVAL_BREAKER(); \
261 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000262
263#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 do { \
265 pending_async_exc = 1; \
266 _Py_atomic_store_relaxed(&eval_breaker, 1); \
267 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000268
269#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000271
272
Guido van Rossume59214e1994-08-30 08:01:59 +0000273#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000274
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000276#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000277#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000278#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000279
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000280static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000281static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000282/* This single variable consolidates all requests to break out of the fast path
283 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000284static _Py_atomic_int eval_breaker = {0};
285/* Request for dropping the GIL */
286static _Py_atomic_int gil_drop_request = {0};
287/* Request for running pending calls. */
288static _Py_atomic_int pendingcalls_to_do = {0};
289/* Request for looking at the `async_exc` field of the current thread state.
290 Guarded by the GIL. */
291static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000292
293#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294
Tim Peters7f468f22004-10-11 02:40:51 +0000295int
296PyEval_ThreadsInitialized(void)
297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000299}
300
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000302PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 if (gil_created())
305 return;
306 create_gil();
307 take_gil(PyThreadState_GET());
308 main_thread = PyThread_get_thread_ident();
309 if (!pending_lock)
310 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000312
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000313void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000314_PyEval_FiniThreads(void)
315{
316 if (!gil_created())
317 return;
318 destroy_gil();
319 assert(!gil_created());
320}
321
322void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000323PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 PyThreadState *tstate = PyThreadState_GET();
326 if (tstate == NULL)
327 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
328 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000329}
330
331void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 /* This function must succeed when the current thread state is NULL.
335 We therefore avoid PyThreadState_GET() which dumps a fatal error
336 in debug mode.
337 */
338 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
339 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000340}
341
342void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000343PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 if (tstate == NULL)
346 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
347 /* Check someone has called PyEval_InitThreads() to create the lock */
348 assert(gil_created());
349 take_gil(tstate);
350 if (PyThreadState_Swap(tstate) != NULL)
351 Py_FatalError(
352 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000353}
354
355void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000356PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 if (tstate == NULL)
359 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
360 if (PyThreadState_Swap(NULL) != tstate)
361 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
362 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000363}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000364
365/* This function is called from PyOS_AfterFork to ensure that newly
366 created child processes don't hold locks referring to threads which
367 are not running in the child process. (This could also be done using
368 pthread_atfork mechanism, at least for the pthreads implementation.) */
369
370void
371PyEval_ReInitThreads(void)
372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 PyObject *threading, *result;
374 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (!gil_created())
377 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 recreate_gil();
379 pending_lock = PyThread_allocate_lock();
380 take_gil(tstate);
381 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 /* Update the threading module with the new state.
384 */
385 tstate = PyThreadState_GET();
386 threading = PyMapping_GetItemString(tstate->interp->modules,
387 "threading");
388 if (threading == NULL) {
389 /* threading not imported */
390 PyErr_Clear();
391 return;
392 }
393 result = PyObject_CallMethod(threading, "_after_fork", NULL);
394 if (result == NULL)
395 PyErr_WriteUnraisable(threading);
396 else
397 Py_DECREF(result);
398 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000399}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000400
401#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000402static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000403static int pending_async_exc = 0;
404#endif /* WITH_THREAD */
405
406/* This function is used to signal that async exceptions are waiting to be
407 raised, therefore it is also useful in non-threaded builds. */
408
409void
410_PyEval_SignalAsyncExc(void)
411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000413}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000414
Guido van Rossumff4949e1992-08-05 19:58:53 +0000415/* Functions save_thread and restore_thread are always defined so
416 dynamically loaded modules needn't be compiled separately for use
417 with and without threads: */
418
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000419PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 PyThreadState *tstate = PyThreadState_Swap(NULL);
423 if (tstate == NULL)
424 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000425#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 if (gil_created())
427 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000428#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000430}
431
432void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000433PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (tstate == NULL)
436 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000437#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (gil_created()) {
439 int err = errno;
440 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200441 /* _Py_Finalizing is protected by the GIL */
442 if (_Py_Finalizing && tstate != _Py_Finalizing) {
443 drop_gil(tstate);
444 PyThread_exit_thread();
445 assert(0); /* unreachable */
446 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 errno = err;
448 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000449#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000451}
452
453
Guido van Rossuma9672091994-09-14 13:31:22 +0000454/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
455 signal handlers or Mac I/O completion routines) can schedule calls
456 to a function to be called synchronously.
457 The synchronous function is called with one void* argument.
458 It should return 0 for success or -1 for failure -- failure should
459 be accompanied by an exception.
460
461 If registry succeeds, the registry function returns 0; if it fails
462 (e.g. due to too many pending calls) it returns -1 (without setting
463 an exception condition).
464
465 Note that because registry may occur from within signal handlers,
466 or other asynchronous events, calling malloc() is unsafe!
467
468#ifdef WITH_THREAD
469 Any thread can schedule pending calls, but only the main thread
470 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471 There is no facility to schedule calls to a particular thread, but
472 that should be easy to change, should that ever be required. In
473 that case, the static variables here should go into the python
474 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000475#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000476*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000477
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000478#ifdef WITH_THREAD
479
480/* The WITH_THREAD implementation is thread-safe. It allows
481 scheduling to be made from any thread, and even from an executing
482 callback.
483 */
484
485#define NPENDINGCALLS 32
486static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 int (*func)(void *);
488 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000489} pendingcalls[NPENDINGCALLS];
490static int pendingfirst = 0;
491static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000492
493int
494Py_AddPendingCall(int (*func)(void *), void *arg)
495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 int i, j, result=0;
497 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 /* try a few times for the lock. Since this mechanism is used
500 * for signal handling (on the main thread), there is a (slim)
501 * chance that a signal is delivered on the same thread while we
502 * hold the lock during the Py_MakePendingCalls() function.
503 * This avoids a deadlock in that case.
504 * Note that signals can be delivered on any thread. In particular,
505 * on Windows, a SIGINT is delivered on a system-created worker
506 * thread.
507 * We also check for lock being NULL, in the unlikely case that
508 * this function is called before any bytecode evaluation takes place.
509 */
510 if (lock != NULL) {
511 for (i = 0; i<100; i++) {
512 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
513 break;
514 }
515 if (i == 100)
516 return -1;
517 }
518
519 i = pendinglast;
520 j = (i + 1) % NPENDINGCALLS;
521 if (j == pendingfirst) {
522 result = -1; /* Queue full */
523 } else {
524 pendingcalls[i].func = func;
525 pendingcalls[i].arg = arg;
526 pendinglast = j;
527 }
528 /* signal main loop */
529 SIGNAL_PENDING_CALLS();
530 if (lock != NULL)
531 PyThread_release_lock(lock);
532 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000533}
534
535int
536Py_MakePendingCalls(void)
537{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200538 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 int i;
540 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (!pending_lock) {
543 /* initial allocation of the lock */
544 pending_lock = PyThread_allocate_lock();
545 if (pending_lock == NULL)
546 return -1;
547 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 /* only service pending calls on main thread */
550 if (main_thread && PyThread_get_thread_ident() != main_thread)
551 return 0;
552 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200553 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200555 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* perform a bounded number of calls, in case of recursion */
557 for (i=0; i<NPENDINGCALLS; i++) {
558 int j;
559 int (*func)(void *);
560 void *arg = NULL;
561
562 /* pop one item off the queue while holding the lock */
563 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
564 j = pendingfirst;
565 if (j == pendinglast) {
566 func = NULL; /* Queue empty */
567 } else {
568 func = pendingcalls[j].func;
569 arg = pendingcalls[j].arg;
570 pendingfirst = (j + 1) % NPENDINGCALLS;
571 }
572 if (pendingfirst != pendinglast)
573 SIGNAL_PENDING_CALLS();
574 else
575 UNSIGNAL_PENDING_CALLS();
576 PyThread_release_lock(pending_lock);
577 /* having released the lock, perform the callback */
578 if (func == NULL)
579 break;
580 r = func(arg);
581 if (r)
582 break;
583 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200584 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000586}
587
588#else /* if ! defined WITH_THREAD */
589
590/*
591 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
592 This code is used for signal handling in python that isn't built
593 with WITH_THREAD.
594 Don't use this implementation when Py_AddPendingCalls() can happen
595 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596
Guido van Rossuma9672091994-09-14 13:31:22 +0000597 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000598 (1) nested asynchronous calls to Py_AddPendingCall()
599 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000601 (1) is very unlikely because typically signal delivery
602 is blocked during signal handling. So it should be impossible.
603 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000604 The current code is safe against (2), but not against (1).
605 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000606 thread is present, interrupted by signals, and that the critical
607 section is protected with the "busy" variable. On Windows, which
608 delivers SIGINT on a system thread, this does not hold and therefore
609 Windows really shouldn't use this version.
610 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000611*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000612
Guido van Rossuma9672091994-09-14 13:31:22 +0000613#define NPENDINGCALLS 32
614static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 int (*func)(void *);
616 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000617} pendingcalls[NPENDINGCALLS];
618static volatile int pendingfirst = 0;
619static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000620static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000621
622int
Thomas Wouters334fb892000-07-25 12:56:38 +0000623Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 static volatile int busy = 0;
626 int i, j;
627 /* XXX Begin critical section */
628 if (busy)
629 return -1;
630 busy = 1;
631 i = pendinglast;
632 j = (i + 1) % NPENDINGCALLS;
633 if (j == pendingfirst) {
634 busy = 0;
635 return -1; /* Queue full */
636 }
637 pendingcalls[i].func = func;
638 pendingcalls[i].arg = arg;
639 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 SIGNAL_PENDING_CALLS();
642 busy = 0;
643 /* XXX End critical section */
644 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000645}
646
Guido van Rossum180d7b41994-09-29 09:45:57 +0000647int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000648Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 static int busy = 0;
651 if (busy)
652 return 0;
653 busy = 1;
654 UNSIGNAL_PENDING_CALLS();
655 for (;;) {
656 int i;
657 int (*func)(void *);
658 void *arg;
659 i = pendingfirst;
660 if (i == pendinglast)
661 break; /* Queue empty */
662 func = pendingcalls[i].func;
663 arg = pendingcalls[i].arg;
664 pendingfirst = (i + 1) % NPENDINGCALLS;
665 if (func(arg) < 0) {
666 busy = 0;
667 SIGNAL_PENDING_CALLS(); /* We're not done yet */
668 return -1;
669 }
670 }
671 busy = 0;
672 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000673}
674
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000675#endif /* WITH_THREAD */
676
Guido van Rossuma9672091994-09-14 13:31:22 +0000677
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000678/* The interpreter's recursion limit */
679
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000680#ifndef Py_DEFAULT_RECURSION_LIMIT
681#define Py_DEFAULT_RECURSION_LIMIT 1000
682#endif
683static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
684int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000685
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000686int
687Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000690}
691
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000692void
693Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 recursion_limit = new_limit;
696 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000697}
698
Armin Rigo2b3eb402003-10-28 12:05:48 +0000699/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
700 if the recursion_depth reaches _Py_CheckRecursionLimit.
701 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
702 to guarantee that _Py_CheckRecursiveCall() is regularly called.
703 Without USE_STACKCHECK, there is no need for this. */
704int
705_Py_CheckRecursiveCall(char *where)
706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000708
709#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 if (PyOS_CheckStack()) {
711 --tstate->recursion_depth;
712 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
713 return -1;
714 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000715#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 _Py_CheckRecursionLimit = recursion_limit;
717 if (tstate->recursion_critical)
718 /* Somebody asked that we don't check for recursion. */
719 return 0;
720 if (tstate->overflowed) {
721 if (tstate->recursion_depth > recursion_limit + 50) {
722 /* Overflowing while handling an overflow. Give up. */
723 Py_FatalError("Cannot recover from stack overflow.");
724 }
725 return 0;
726 }
727 if (tstate->recursion_depth > recursion_limit) {
728 --tstate->recursion_depth;
729 tstate->overflowed = 1;
730 PyErr_Format(PyExc_RuntimeError,
731 "maximum recursion depth exceeded%s",
732 where);
733 return -1;
734 }
735 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000736}
737
Guido van Rossum374a9221991-04-04 10:40:29 +0000738/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000739enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000740 WHY_NOT = 0x0001, /* No error */
741 WHY_EXCEPTION = 0x0002, /* Exception occurred */
742 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
743 WHY_RETURN = 0x0008, /* 'return' statement */
744 WHY_BREAK = 0x0010, /* 'break' statement */
745 WHY_CONTINUE = 0x0020, /* 'continue' statement */
746 WHY_YIELD = 0x0040, /* 'yield' operator */
747 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000748};
Guido van Rossum374a9221991-04-04 10:40:29 +0000749
Benjamin Peterson87880242011-07-03 16:48:31 -0500750static void save_exc_state(PyThreadState *, PyFrameObject *);
751static void swap_exc_state(PyThreadState *, PyFrameObject *);
752static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Collin Winter828f04a2007-08-31 00:04:24 +0000753static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000754static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000755
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000756/* Records whether tracing is on for any thread. Counts the number of
757 threads for which tstate->c_tracefunc is non-NULL, so if the value
758 is 0, we know we don't have to check this thread's c_tracefunc.
759 This speeds up the if statement in PyEval_EvalFrameEx() after
760 fast_next_opcode*/
761static int _Py_TracingPossible = 0;
762
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000763
Guido van Rossum374a9221991-04-04 10:40:29 +0000764
Guido van Rossumb209a111997-04-29 18:18:01 +0000765PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000766PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 return PyEval_EvalCodeEx(co,
769 globals, locals,
770 (PyObject **)NULL, 0,
771 (PyObject **)NULL, 0,
772 (PyObject **)NULL, 0,
773 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000774}
775
776
777/* Interpreter main loop */
778
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000779PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000780PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 /* This is for backward compatibility with extension modules that
782 used this API; core interpreter code should call
783 PyEval_EvalFrameEx() */
784 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000785}
786
787PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000788PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000789{
Guido van Rossum950361c1997-01-24 13:49:28 +0000790#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000792#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 register PyObject **stack_pointer; /* Next free slot in value stack */
794 register unsigned char *next_instr;
795 register int opcode; /* Current opcode */
796 register int oparg; /* Current opcode argument, if any */
797 register enum why_code why; /* Reason for block stack unwind */
798 register int err; /* Error status -- nonzero if error */
799 register PyObject *x; /* Result object -- NULL if error */
800 register PyObject *v; /* Temporary objects popped off stack */
801 register PyObject *w;
802 register PyObject *u;
803 register PyObject *t;
804 register PyObject **fastlocals, **freevars;
805 PyObject *retval = NULL; /* Return value */
806 PyThreadState *tstate = PyThreadState_GET();
807 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 is true when the line being executed has changed. The
814 initial values are such as to make this false the first
815 time it is tested. */
816 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 unsigned char *first_instr;
819 PyObject *names;
820 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000821
Antoine Pitroub52ec782009-01-25 16:34:23 +0000822/* Computed GOTOs, or
823 the-optimization-commonly-but-improperly-known-as-"threaded code"
824 using gcc's labels-as-values extension
825 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
826
827 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829 combined with a lookup table of jump addresses. However, since the
830 indirect jump instruction is shared by all opcodes, the CPU will have a
831 hard time making the right prediction for where to jump next (actually,
832 it will be always wrong except in the uncommon case of a sequence of
833 several identical opcodes).
834
835 "Threaded code" in contrast, uses an explicit jump table and an explicit
836 indirect jump instruction at the end of each opcode. Since the jump
837 instruction is at a different address for each opcode, the CPU will make a
838 separate prediction for each of these instructions, which is equivalent to
839 predicting the second opcode of each opcode pair. These predictions have
840 a much better chance to turn out valid, especially in small bytecode loops.
841
842 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844 and potentially many more instructions (depending on the pipeline width).
845 A correctly predicted branch, however, is nearly free.
846
847 At the time of this writing, the "threaded code" version is up to 15-20%
848 faster than the normal "switch" version, depending on the compiler and the
849 CPU architecture.
850
851 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
852 because it would render the measurements invalid.
853
854
855 NOTE: care must be taken that the compiler doesn't try to "optimize" the
856 indirect jumps by sharing them between all opcodes. Such optimizations
857 can be disabled on gcc by using the -fno-gcse flag (or possibly
858 -fno-crossjumping).
859*/
860
Antoine Pitrou042b1282010-08-13 21:15:58 +0000861#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000862#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000863#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000864#endif
865
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#ifdef HAVE_COMPUTED_GOTOS
867 #ifndef USE_COMPUTED_GOTOS
868 #define USE_COMPUTED_GOTOS 1
869 #endif
870#else
871 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
872 #error "Computed gotos are not supported on this compiler."
873 #endif
874 #undef USE_COMPUTED_GOTOS
875 #define USE_COMPUTED_GOTOS 0
876#endif
877
878#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879/* Import the static jump table */
880#include "opcode_targets.h"
881
882/* This macro is used when several opcodes defer to the same implementation
883 (e.g. SETUP_LOOP, SETUP_FINALLY) */
884#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 TARGET_##op: \
886 opcode = op; \
887 if (HAS_ARG(op)) \
888 oparg = NEXTARG(); \
889 case op: \
890 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000891
892#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 TARGET_##op: \
894 opcode = op; \
895 if (HAS_ARG(op)) \
896 oparg = NEXTARG(); \
897 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000898
899
900#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 { \
902 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
903 FAST_DISPATCH(); \
904 } \
905 continue; \
906 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000907
908#ifdef LLTRACE
909#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 { \
911 if (!lltrace && !_Py_TracingPossible) { \
912 f->f_lasti = INSTR_OFFSET(); \
913 goto *opcode_targets[*next_instr++]; \
914 } \
915 goto fast_next_opcode; \
916 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000917#else
918#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 { \
920 if (!_Py_TracingPossible) { \
921 f->f_lasti = INSTR_OFFSET(); \
922 goto *opcode_targets[*next_instr++]; \
923 } \
924 goto fast_next_opcode; \
925 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000926#endif
927
928#else
929#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000931#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 /* silence compiler warnings about `impl` unused */ \
933 if (0) goto impl; \
934 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000935#define DISPATCH() continue
936#define FAST_DISPATCH() goto fast_next_opcode
937#endif
938
939
Neal Norwitza81d2202002-07-14 00:27:26 +0000940/* Tuple access macros */
941
942#ifndef Py_DEBUG
943#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
944#else
945#define GETITEM(v, i) PyTuple_GetItem((v), (i))
946#endif
947
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000948#ifdef WITH_TSC
949/* Use Pentium timestamp counter to mark certain events:
950 inst0 -- beginning of switch statement for opcode dispatch
951 inst1 -- end of switch statement (may be skipped)
952 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000954 (may be skipped)
955 intr1 -- beginning of long interruption
956 intr2 -- end of long interruption
957
958 Many opcodes call out to helper C functions. In some cases, the
959 time in those functions should be counted towards the time for the
960 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
961 calls another Python function; there's no point in charge all the
962 bytecode executed by the called function to the caller.
963
964 It's hard to make a useful judgement statically. In the presence
965 of operator overloading, it's impossible to tell if a call will
966 execute new Python code or not.
967
968 It's a case-by-case judgement. I'll use intr1 for the following
969 cases:
970
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000971 IMPORT_STAR
972 IMPORT_FROM
973 CALL_FUNCTION (and friends)
974
975 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
977 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 READ_TIMESTAMP(inst0);
980 READ_TIMESTAMP(inst1);
981 READ_TIMESTAMP(loop0);
982 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* shut up the compiler */
985 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000986#endif
987
Guido van Rossum374a9221991-04-04 10:40:29 +0000988/* Code access macros */
989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990#define INSTR_OFFSET() ((int)(next_instr - first_instr))
991#define NEXTOP() (*next_instr++)
992#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
993#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
994#define JUMPTO(x) (next_instr = first_instr + (x))
995#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000996
Raymond Hettingerf606f872003-03-16 03:11:04 +0000997/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 Some opcodes tend to come in pairs thus making it possible to
999 predict the second code when the first is run. For example,
1000 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1001 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Verifying the prediction costs a single high-speed test of a register
1004 variable against a constant. If the pairing was good, then the
1005 processor's own internal branch predication has a high likelihood of
1006 success, resulting in a nearly zero-overhead transition to the
1007 next opcode. A successful prediction saves a trip through the eval-loop
1008 including its two unpredictable branches, the HAS_ARG test and the
1009 switch-case. Combined with the processor's internal branch prediction,
1010 a successful PREDICT has the effect of making the two opcodes run as if
1011 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001012
Georg Brandl86b2fb92008-07-16 03:43:04 +00001013 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 predictions turned-on and interpret the results as if some opcodes
1015 had been combined or turn-off predictions so that the opcode frequency
1016 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001017
1018 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 the CPU to record separate branch prediction information for each
1020 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001021
Raymond Hettingerf606f872003-03-16 03:11:04 +00001022*/
1023
Antoine Pitrou042b1282010-08-13 21:15:58 +00001024#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025#define PREDICT(op) if (0) goto PRED_##op
1026#define PREDICTED(op) PRED_##op:
1027#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001028#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1030#define PREDICTED(op) PRED_##op: next_instr++
1031#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001032#endif
1033
Raymond Hettingerf606f872003-03-16 03:11:04 +00001034
Guido van Rossum374a9221991-04-04 10:40:29 +00001035/* Stack manipulation macros */
1036
Martin v. Löwis18e16552006-02-15 17:27:45 +00001037/* The stack can grow at most MAXINT deep, as co_nlocals and
1038 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001039#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1040#define EMPTY() (STACK_LEVEL() == 0)
1041#define TOP() (stack_pointer[-1])
1042#define SECOND() (stack_pointer[-2])
1043#define THIRD() (stack_pointer[-3])
1044#define FOURTH() (stack_pointer[-4])
1045#define PEEK(n) (stack_pointer[-(n)])
1046#define SET_TOP(v) (stack_pointer[-1] = (v))
1047#define SET_SECOND(v) (stack_pointer[-2] = (v))
1048#define SET_THIRD(v) (stack_pointer[-3] = (v))
1049#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1050#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1051#define BASIC_STACKADJ(n) (stack_pointer += n)
1052#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1053#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001054
Guido van Rossum96a42c81992-01-12 02:29:51 +00001055#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001057 lltrace && prtrace(TOP(), "push")); \
1058 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001060 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001062 lltrace && prtrace(TOP(), "stackadj")); \
1063 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001064#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001065 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1066 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001067#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001068#define PUSH(v) BASIC_PUSH(v)
1069#define POP() BASIC_POP()
1070#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001071#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001072#endif
1073
Guido van Rossum681d79a1995-07-18 14:51:37 +00001074/* Local variable macros */
1075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001077
1078/* The SETLOCAL() macro must not DECREF the local variable in-place and
1079 then store the new value; it must copy the old value to a temporary
1080 value, then store the new value, and then DECREF the temporary value.
1081 This is because it is possible that during the DECREF the frame is
1082 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1083 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001085 GETLOCAL(i) = value; \
1086 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001087
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001088
1089#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 while (STACK_LEVEL() > (b)->b_level) { \
1091 PyObject *v = POP(); \
1092 Py_XDECREF(v); \
1093 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001094
1095#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 { \
1097 PyObject *type, *value, *traceback; \
1098 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1099 while (STACK_LEVEL() > (b)->b_level + 3) { \
1100 value = POP(); \
1101 Py_XDECREF(value); \
1102 } \
1103 type = tstate->exc_type; \
1104 value = tstate->exc_value; \
1105 traceback = tstate->exc_traceback; \
1106 tstate->exc_type = POP(); \
1107 tstate->exc_value = POP(); \
1108 tstate->exc_traceback = POP(); \
1109 Py_XDECREF(type); \
1110 Py_XDECREF(value); \
1111 Py_XDECREF(traceback); \
1112 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001113
Guido van Rossuma027efa1997-05-05 20:56:21 +00001114/* Start of code */
1115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* push frame */
1117 if (Py_EnterRecursiveCall(""))
1118 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (tstate->use_tracing) {
1123 if (tstate->c_tracefunc != NULL) {
1124 /* tstate->c_tracefunc, if defined, is a
1125 function that will be called on *every* entry
1126 to a code block. Its return value, if not
1127 None, is a function that will be called at
1128 the start of each executed line of code.
1129 (Actually, the function must return itself
1130 in order to continue tracing.) The trace
1131 functions are called with three arguments:
1132 a pointer to the current frame, a string
1133 indicating why the function is called, and
1134 an argument which depends on the situation.
1135 The global trace function is also called
1136 whenever an exception is detected. */
1137 if (call_trace_protected(tstate->c_tracefunc,
1138 tstate->c_traceobj,
1139 f, PyTrace_CALL, Py_None)) {
1140 /* Trace function raised an error */
1141 goto exit_eval_frame;
1142 }
1143 }
1144 if (tstate->c_profilefunc != NULL) {
1145 /* Similar for c_profilefunc, except it needn't
1146 return itself and isn't called for "line" events */
1147 if (call_trace_protected(tstate->c_profilefunc,
1148 tstate->c_profileobj,
1149 f, PyTrace_CALL, Py_None)) {
1150 /* Profile function raised an error */
1151 goto exit_eval_frame;
1152 }
1153 }
1154 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 co = f->f_code;
1157 names = co->co_names;
1158 consts = co->co_consts;
1159 fastlocals = f->f_localsplus;
1160 freevars = f->f_localsplus + co->co_nlocals;
1161 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1162 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 f->f_lasti now refers to the index of the last instruction
1165 executed. You might think this was obvious from the name, but
1166 this wasn't always true before 2.3! PyFrame_New now sets
1167 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1168 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1169 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 When the PREDICT() macros are enabled, some opcode pairs follow in
1172 direct succession without updating f->f_lasti. A successful
1173 prediction effectively links the two codes together as if they
1174 were a single new opcode; accordingly,f->f_lasti will point to
1175 the first code in the pair (for instance, GET_ITER followed by
1176 FOR_ITER is effectively a single opcode and f->f_lasti will point
1177 at to the beginning of the combined pair.)
1178 */
1179 next_instr = first_instr + f->f_lasti + 1;
1180 stack_pointer = f->f_stacktop;
1181 assert(stack_pointer != NULL);
1182 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 if (co->co_flags & CO_GENERATOR && !throwflag) {
1185 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1186 /* We were in an except handler when we left,
1187 restore the exception state which was put aside
1188 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001189 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001191 else
1192 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001194
Tim Peters5ca576e2001-06-18 22:08:13 +00001195#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001197#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 why = WHY_NOT;
1200 err = 0;
1201 x = Py_None; /* Not a reference, just anything non-NULL */
1202 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 if (throwflag) { /* support for generator.throw() */
1205 why = WHY_EXCEPTION;
1206 goto on_error;
1207 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001210#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (inst1 == 0) {
1212 /* Almost surely, the opcode executed a break
1213 or a continue, preventing inst1 from being set
1214 on the way out of the loop.
1215 */
1216 READ_TIMESTAMP(inst1);
1217 loop1 = inst1;
1218 }
1219 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1220 intr0, intr1);
1221 ticked = 0;
1222 inst1 = 0;
1223 intr0 = 0;
1224 intr1 = 0;
1225 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001226#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1228 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 /* Do periodic things. Doing this every time through
1231 the loop would add too much overhead, so we do it
1232 only every Nth instruction. We also do it if
1233 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1234 event needs attention (e.g. a signal handler or
1235 async I/O handler); see Py_AddPendingCall() and
1236 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1239 if (*next_instr == SETUP_FINALLY) {
1240 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001241 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 goto fast_next_opcode;
1243 }
1244 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001245#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001247#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1249 if (Py_MakePendingCalls() < 0) {
1250 why = WHY_EXCEPTION;
1251 goto on_error;
1252 }
1253 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001254#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001255 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 /* Give another thread a chance */
1257 if (PyThreadState_Swap(NULL) != tstate)
1258 Py_FatalError("ceval: tstate mix-up");
1259 drop_gil(tstate);
1260
1261 /* Other threads may run now */
1262
1263 take_gil(tstate);
1264 if (PyThreadState_Swap(tstate) != NULL)
1265 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001267#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 /* Check for asynchronous exceptions. */
1269 if (tstate->async_exc != NULL) {
1270 x = tstate->async_exc;
1271 tstate->async_exc = NULL;
1272 UNSIGNAL_ASYNC_EXC();
1273 PyErr_SetNone(x);
1274 Py_DECREF(x);
1275 why = WHY_EXCEPTION;
1276 goto on_error;
1277 }
1278 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 fast_next_opcode:
1281 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 if (_Py_TracingPossible &&
1286 tstate->c_tracefunc != NULL && !tstate->tracing) {
1287 /* see maybe_call_line_trace
1288 for expository comments */
1289 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 err = maybe_call_line_trace(tstate->c_tracefunc,
1292 tstate->c_traceobj,
1293 f, &instr_lb, &instr_ub,
1294 &instr_prev);
1295 /* Reload possibly changed frame fields */
1296 JUMPTO(f->f_lasti);
1297 if (f->f_stacktop != NULL) {
1298 stack_pointer = f->f_stacktop;
1299 f->f_stacktop = NULL;
1300 }
1301 if (err) {
1302 /* trace function raised an exception */
1303 goto on_error;
1304 }
1305 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 opcode = NEXTOP();
1310 oparg = 0; /* allows oparg to be stored in a register because
1311 it doesn't have to be remembered across a full loop */
1312 if (HAS_ARG(opcode))
1313 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001314 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001315#ifdef DYNAMIC_EXECUTION_PROFILE
1316#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 dxpairs[lastopcode][opcode]++;
1318 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001319#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001321#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001322
Guido van Rossum96a42c81992-01-12 02:29:51 +00001323#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 if (lltrace) {
1327 if (HAS_ARG(opcode)) {
1328 printf("%d: %d, %d\n",
1329 f->f_lasti, opcode, oparg);
1330 }
1331 else {
1332 printf("%d: %d\n",
1333 f->f_lasti, opcode);
1334 }
1335 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001336#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 /* Main switch on opcode */
1339 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 /* BEWARE!
1344 It is essential that any operation that fails sets either
1345 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1346 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 TARGET(NOP)
1349 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 TARGET(LOAD_FAST)
1352 x = GETLOCAL(oparg);
1353 if (x != NULL) {
1354 Py_INCREF(x);
1355 PUSH(x);
1356 FAST_DISPATCH();
1357 }
1358 format_exc_check_arg(PyExc_UnboundLocalError,
1359 UNBOUNDLOCAL_ERROR_MSG,
1360 PyTuple_GetItem(co->co_varnames, oparg));
1361 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 TARGET(LOAD_CONST)
1364 x = GETITEM(consts, oparg);
1365 Py_INCREF(x);
1366 PUSH(x);
1367 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 PREDICTED_WITH_ARG(STORE_FAST);
1370 TARGET(STORE_FAST)
1371 v = POP();
1372 SETLOCAL(oparg, v);
1373 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 TARGET(POP_TOP)
1376 v = POP();
1377 Py_DECREF(v);
1378 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 TARGET(ROT_TWO)
1381 v = TOP();
1382 w = SECOND();
1383 SET_TOP(w);
1384 SET_SECOND(v);
1385 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 TARGET(ROT_THREE)
1388 v = TOP();
1389 w = SECOND();
1390 x = THIRD();
1391 SET_TOP(w);
1392 SET_SECOND(x);
1393 SET_THIRD(v);
1394 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 TARGET(DUP_TOP)
1397 v = TOP();
1398 Py_INCREF(v);
1399 PUSH(v);
1400 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001402 TARGET(DUP_TOP_TWO)
1403 x = TOP();
1404 Py_INCREF(x);
1405 w = SECOND();
1406 Py_INCREF(w);
1407 STACKADJ(2);
1408 SET_TOP(x);
1409 SET_SECOND(w);
1410 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 TARGET(UNARY_POSITIVE)
1413 v = TOP();
1414 x = PyNumber_Positive(v);
1415 Py_DECREF(v);
1416 SET_TOP(x);
1417 if (x != NULL) DISPATCH();
1418 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 TARGET(UNARY_NEGATIVE)
1421 v = TOP();
1422 x = PyNumber_Negative(v);
1423 Py_DECREF(v);
1424 SET_TOP(x);
1425 if (x != NULL) DISPATCH();
1426 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 TARGET(UNARY_NOT)
1429 v = TOP();
1430 err = PyObject_IsTrue(v);
1431 Py_DECREF(v);
1432 if (err == 0) {
1433 Py_INCREF(Py_True);
1434 SET_TOP(Py_True);
1435 DISPATCH();
1436 }
1437 else if (err > 0) {
1438 Py_INCREF(Py_False);
1439 SET_TOP(Py_False);
1440 err = 0;
1441 DISPATCH();
1442 }
1443 STACKADJ(-1);
1444 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 TARGET(UNARY_INVERT)
1447 v = TOP();
1448 x = PyNumber_Invert(v);
1449 Py_DECREF(v);
1450 SET_TOP(x);
1451 if (x != NULL) DISPATCH();
1452 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 TARGET(BINARY_POWER)
1455 w = POP();
1456 v = TOP();
1457 x = PyNumber_Power(v, w, Py_None);
1458 Py_DECREF(v);
1459 Py_DECREF(w);
1460 SET_TOP(x);
1461 if (x != NULL) DISPATCH();
1462 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 TARGET(BINARY_MULTIPLY)
1465 w = POP();
1466 v = TOP();
1467 x = PyNumber_Multiply(v, w);
1468 Py_DECREF(v);
1469 Py_DECREF(w);
1470 SET_TOP(x);
1471 if (x != NULL) DISPATCH();
1472 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 TARGET(BINARY_TRUE_DIVIDE)
1475 w = POP();
1476 v = TOP();
1477 x = PyNumber_TrueDivide(v, w);
1478 Py_DECREF(v);
1479 Py_DECREF(w);
1480 SET_TOP(x);
1481 if (x != NULL) DISPATCH();
1482 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 TARGET(BINARY_FLOOR_DIVIDE)
1485 w = POP();
1486 v = TOP();
1487 x = PyNumber_FloorDivide(v, w);
1488 Py_DECREF(v);
1489 Py_DECREF(w);
1490 SET_TOP(x);
1491 if (x != NULL) DISPATCH();
1492 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 TARGET(BINARY_MODULO)
1495 w = POP();
1496 v = TOP();
1497 if (PyUnicode_CheckExact(v))
1498 x = PyUnicode_Format(v, w);
1499 else
1500 x = PyNumber_Remainder(v, w);
1501 Py_DECREF(v);
1502 Py_DECREF(w);
1503 SET_TOP(x);
1504 if (x != NULL) DISPATCH();
1505 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 TARGET(BINARY_ADD)
1508 w = POP();
1509 v = TOP();
Benjamin Peterson811c2f12011-09-30 21:31:21 -04001510 x = PyNumber_Add(v, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 Py_DECREF(w);
1513 SET_TOP(x);
1514 if (x != NULL) DISPATCH();
1515 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 TARGET(BINARY_SUBTRACT)
1518 w = POP();
1519 v = TOP();
1520 x = PyNumber_Subtract(v, w);
1521 Py_DECREF(v);
1522 Py_DECREF(w);
1523 SET_TOP(x);
1524 if (x != NULL) DISPATCH();
1525 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 TARGET(BINARY_SUBSCR)
1528 w = POP();
1529 v = TOP();
1530 x = PyObject_GetItem(v, w);
1531 Py_DECREF(v);
1532 Py_DECREF(w);
1533 SET_TOP(x);
1534 if (x != NULL) DISPATCH();
1535 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 TARGET(BINARY_LSHIFT)
1538 w = POP();
1539 v = TOP();
1540 x = PyNumber_Lshift(v, w);
1541 Py_DECREF(v);
1542 Py_DECREF(w);
1543 SET_TOP(x);
1544 if (x != NULL) DISPATCH();
1545 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 TARGET(BINARY_RSHIFT)
1548 w = POP();
1549 v = TOP();
1550 x = PyNumber_Rshift(v, w);
1551 Py_DECREF(v);
1552 Py_DECREF(w);
1553 SET_TOP(x);
1554 if (x != NULL) DISPATCH();
1555 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 TARGET(BINARY_AND)
1558 w = POP();
1559 v = TOP();
1560 x = PyNumber_And(v, w);
1561 Py_DECREF(v);
1562 Py_DECREF(w);
1563 SET_TOP(x);
1564 if (x != NULL) DISPATCH();
1565 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 TARGET(BINARY_XOR)
1568 w = POP();
1569 v = TOP();
1570 x = PyNumber_Xor(v, w);
1571 Py_DECREF(v);
1572 Py_DECREF(w);
1573 SET_TOP(x);
1574 if (x != NULL) DISPATCH();
1575 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 TARGET(BINARY_OR)
1578 w = POP();
1579 v = TOP();
1580 x = PyNumber_Or(v, w);
1581 Py_DECREF(v);
1582 Py_DECREF(w);
1583 SET_TOP(x);
1584 if (x != NULL) DISPATCH();
1585 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 TARGET(LIST_APPEND)
1588 w = POP();
1589 v = PEEK(oparg);
1590 err = PyList_Append(v, w);
1591 Py_DECREF(w);
1592 if (err == 0) {
1593 PREDICT(JUMP_ABSOLUTE);
1594 DISPATCH();
1595 }
1596 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 TARGET(SET_ADD)
1599 w = POP();
1600 v = stack_pointer[-oparg];
1601 err = PySet_Add(v, w);
1602 Py_DECREF(w);
1603 if (err == 0) {
1604 PREDICT(JUMP_ABSOLUTE);
1605 DISPATCH();
1606 }
1607 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 TARGET(INPLACE_POWER)
1610 w = POP();
1611 v = TOP();
1612 x = PyNumber_InPlacePower(v, w, Py_None);
1613 Py_DECREF(v);
1614 Py_DECREF(w);
1615 SET_TOP(x);
1616 if (x != NULL) DISPATCH();
1617 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 TARGET(INPLACE_MULTIPLY)
1620 w = POP();
1621 v = TOP();
1622 x = PyNumber_InPlaceMultiply(v, w);
1623 Py_DECREF(v);
1624 Py_DECREF(w);
1625 SET_TOP(x);
1626 if (x != NULL) DISPATCH();
1627 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 TARGET(INPLACE_TRUE_DIVIDE)
1630 w = POP();
1631 v = TOP();
1632 x = PyNumber_InPlaceTrueDivide(v, w);
1633 Py_DECREF(v);
1634 Py_DECREF(w);
1635 SET_TOP(x);
1636 if (x != NULL) DISPATCH();
1637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 TARGET(INPLACE_FLOOR_DIVIDE)
1640 w = POP();
1641 v = TOP();
1642 x = PyNumber_InPlaceFloorDivide(v, w);
1643 Py_DECREF(v);
1644 Py_DECREF(w);
1645 SET_TOP(x);
1646 if (x != NULL) DISPATCH();
1647 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 TARGET(INPLACE_MODULO)
1650 w = POP();
1651 v = TOP();
1652 x = PyNumber_InPlaceRemainder(v, w);
1653 Py_DECREF(v);
1654 Py_DECREF(w);
1655 SET_TOP(x);
1656 if (x != NULL) DISPATCH();
1657 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 TARGET(INPLACE_ADD)
1660 w = POP();
1661 v = TOP();
Benjamin Peterson811c2f12011-09-30 21:31:21 -04001662 x = PyNumber_InPlaceAdd(v, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 Py_DECREF(w);
1665 SET_TOP(x);
1666 if (x != NULL) DISPATCH();
1667 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 TARGET(INPLACE_SUBTRACT)
1670 w = POP();
1671 v = TOP();
1672 x = PyNumber_InPlaceSubtract(v, w);
1673 Py_DECREF(v);
1674 Py_DECREF(w);
1675 SET_TOP(x);
1676 if (x != NULL) DISPATCH();
1677 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 TARGET(INPLACE_LSHIFT)
1680 w = POP();
1681 v = TOP();
1682 x = PyNumber_InPlaceLshift(v, w);
1683 Py_DECREF(v);
1684 Py_DECREF(w);
1685 SET_TOP(x);
1686 if (x != NULL) DISPATCH();
1687 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 TARGET(INPLACE_RSHIFT)
1690 w = POP();
1691 v = TOP();
1692 x = PyNumber_InPlaceRshift(v, w);
1693 Py_DECREF(v);
1694 Py_DECREF(w);
1695 SET_TOP(x);
1696 if (x != NULL) DISPATCH();
1697 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 TARGET(INPLACE_AND)
1700 w = POP();
1701 v = TOP();
1702 x = PyNumber_InPlaceAnd(v, w);
1703 Py_DECREF(v);
1704 Py_DECREF(w);
1705 SET_TOP(x);
1706 if (x != NULL) DISPATCH();
1707 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 TARGET(INPLACE_XOR)
1710 w = POP();
1711 v = TOP();
1712 x = PyNumber_InPlaceXor(v, w);
1713 Py_DECREF(v);
1714 Py_DECREF(w);
1715 SET_TOP(x);
1716 if (x != NULL) DISPATCH();
1717 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 TARGET(INPLACE_OR)
1720 w = POP();
1721 v = TOP();
1722 x = PyNumber_InPlaceOr(v, w);
1723 Py_DECREF(v);
1724 Py_DECREF(w);
1725 SET_TOP(x);
1726 if (x != NULL) DISPATCH();
1727 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 TARGET(STORE_SUBSCR)
1730 w = TOP();
1731 v = SECOND();
1732 u = THIRD();
1733 STACKADJ(-3);
1734 /* v[w] = u */
1735 err = PyObject_SetItem(v, w, u);
1736 Py_DECREF(u);
1737 Py_DECREF(v);
1738 Py_DECREF(w);
1739 if (err == 0) DISPATCH();
1740 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 TARGET(DELETE_SUBSCR)
1743 w = TOP();
1744 v = SECOND();
1745 STACKADJ(-2);
1746 /* del v[w] */
1747 err = PyObject_DelItem(v, w);
1748 Py_DECREF(v);
1749 Py_DECREF(w);
1750 if (err == 0) DISPATCH();
1751 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 TARGET(PRINT_EXPR)
1754 v = POP();
1755 w = PySys_GetObject("displayhook");
1756 if (w == NULL) {
1757 PyErr_SetString(PyExc_RuntimeError,
1758 "lost sys.displayhook");
1759 err = -1;
1760 x = NULL;
1761 }
1762 if (err == 0) {
1763 x = PyTuple_Pack(1, v);
1764 if (x == NULL)
1765 err = -1;
1766 }
1767 if (err == 0) {
1768 w = PyEval_CallObject(w, x);
1769 Py_XDECREF(w);
1770 if (w == NULL)
1771 err = -1;
1772 }
1773 Py_DECREF(v);
1774 Py_XDECREF(x);
1775 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001776
Thomas Wouters434d0822000-08-24 20:11:32 +00001777#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001779#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 TARGET(RAISE_VARARGS)
1781 v = w = NULL;
1782 switch (oparg) {
1783 case 2:
1784 v = POP(); /* cause */
1785 case 1:
1786 w = POP(); /* exc */
1787 case 0: /* Fallthrough */
1788 why = do_raise(w, v);
1789 break;
1790 default:
1791 PyErr_SetString(PyExc_SystemError,
1792 "bad RAISE_VARARGS oparg");
1793 why = WHY_EXCEPTION;
1794 break;
1795 }
1796 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 TARGET(STORE_LOCALS)
1799 x = POP();
1800 v = f->f_locals;
1801 Py_XDECREF(v);
1802 f->f_locals = x;
1803 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 TARGET(RETURN_VALUE)
1806 retval = POP();
1807 why = WHY_RETURN;
1808 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 TARGET(YIELD_VALUE)
1811 retval = POP();
1812 f->f_stacktop = stack_pointer;
1813 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 TARGET(POP_EXCEPT)
1817 {
1818 PyTryBlock *b = PyFrame_BlockPop(f);
1819 if (b->b_type != EXCEPT_HANDLER) {
1820 PyErr_SetString(PyExc_SystemError,
1821 "popped block is not an except handler");
1822 why = WHY_EXCEPTION;
1823 break;
1824 }
1825 UNWIND_EXCEPT_HANDLER(b);
1826 }
1827 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 TARGET(POP_BLOCK)
1830 {
1831 PyTryBlock *b = PyFrame_BlockPop(f);
1832 UNWIND_BLOCK(b);
1833 }
1834 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 PREDICTED(END_FINALLY);
1837 TARGET(END_FINALLY)
1838 v = POP();
1839 if (PyLong_Check(v)) {
1840 why = (enum why_code) PyLong_AS_LONG(v);
1841 assert(why != WHY_YIELD);
1842 if (why == WHY_RETURN ||
1843 why == WHY_CONTINUE)
1844 retval = POP();
1845 if (why == WHY_SILENCED) {
1846 /* An exception was silenced by 'with', we must
1847 manually unwind the EXCEPT_HANDLER block which was
1848 created when the exception was caught, otherwise
1849 the stack will be in an inconsistent state. */
1850 PyTryBlock *b = PyFrame_BlockPop(f);
1851 assert(b->b_type == EXCEPT_HANDLER);
1852 UNWIND_EXCEPT_HANDLER(b);
1853 why = WHY_NOT;
1854 }
1855 }
1856 else if (PyExceptionClass_Check(v)) {
1857 w = POP();
1858 u = POP();
1859 PyErr_Restore(v, w, u);
1860 why = WHY_RERAISE;
1861 break;
1862 }
1863 else if (v != Py_None) {
1864 PyErr_SetString(PyExc_SystemError,
1865 "'finally' pops bad exception");
1866 why = WHY_EXCEPTION;
1867 }
1868 Py_DECREF(v);
1869 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 TARGET(LOAD_BUILD_CLASS)
1872 x = PyDict_GetItemString(f->f_builtins,
1873 "__build_class__");
1874 if (x == NULL) {
1875 PyErr_SetString(PyExc_ImportError,
1876 "__build_class__ not found");
1877 break;
1878 }
1879 Py_INCREF(x);
1880 PUSH(x);
1881 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 TARGET(STORE_NAME)
1884 w = GETITEM(names, oparg);
1885 v = POP();
1886 if ((x = f->f_locals) != NULL) {
1887 if (PyDict_CheckExact(x))
1888 err = PyDict_SetItem(x, w, v);
1889 else
1890 err = PyObject_SetItem(x, w, v);
1891 Py_DECREF(v);
1892 if (err == 0) DISPATCH();
1893 break;
1894 }
1895 PyErr_Format(PyExc_SystemError,
1896 "no locals found when storing %R", w);
1897 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 TARGET(DELETE_NAME)
1900 w = GETITEM(names, oparg);
1901 if ((x = f->f_locals) != NULL) {
1902 if ((err = PyObject_DelItem(x, w)) != 0)
1903 format_exc_check_arg(PyExc_NameError,
1904 NAME_ERROR_MSG,
1905 w);
1906 break;
1907 }
1908 PyErr_Format(PyExc_SystemError,
1909 "no locals when deleting %R", w);
1910 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1913 TARGET(UNPACK_SEQUENCE)
1914 v = POP();
1915 if (PyTuple_CheckExact(v) &&
1916 PyTuple_GET_SIZE(v) == oparg) {
1917 PyObject **items = \
1918 ((PyTupleObject *)v)->ob_item;
1919 while (oparg--) {
1920 w = items[oparg];
1921 Py_INCREF(w);
1922 PUSH(w);
1923 }
1924 Py_DECREF(v);
1925 DISPATCH();
1926 } else if (PyList_CheckExact(v) &&
1927 PyList_GET_SIZE(v) == oparg) {
1928 PyObject **items = \
1929 ((PyListObject *)v)->ob_item;
1930 while (oparg--) {
1931 w = items[oparg];
1932 Py_INCREF(w);
1933 PUSH(w);
1934 }
1935 } else if (unpack_iterable(v, oparg, -1,
1936 stack_pointer + oparg)) {
1937 STACKADJ(oparg);
1938 } else {
1939 /* unpack_iterable() raised an exception */
1940 why = WHY_EXCEPTION;
1941 }
1942 Py_DECREF(v);
1943 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 TARGET(UNPACK_EX)
1946 {
1947 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1948 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1951 stack_pointer + totalargs)) {
1952 stack_pointer += totalargs;
1953 } else {
1954 why = WHY_EXCEPTION;
1955 }
1956 Py_DECREF(v);
1957 break;
1958 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 TARGET(STORE_ATTR)
1961 w = GETITEM(names, oparg);
1962 v = TOP();
1963 u = SECOND();
1964 STACKADJ(-2);
1965 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1966 Py_DECREF(v);
1967 Py_DECREF(u);
1968 if (err == 0) DISPATCH();
1969 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 TARGET(DELETE_ATTR)
1972 w = GETITEM(names, oparg);
1973 v = POP();
1974 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1975 /* del v.w */
1976 Py_DECREF(v);
1977 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 TARGET(STORE_GLOBAL)
1980 w = GETITEM(names, oparg);
1981 v = POP();
1982 err = PyDict_SetItem(f->f_globals, w, v);
1983 Py_DECREF(v);
1984 if (err == 0) DISPATCH();
1985 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 TARGET(DELETE_GLOBAL)
1988 w = GETITEM(names, oparg);
1989 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1990 format_exc_check_arg(
1991 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1992 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 TARGET(LOAD_NAME)
1995 w = GETITEM(names, oparg);
1996 if ((v = f->f_locals) == NULL) {
1997 PyErr_Format(PyExc_SystemError,
1998 "no locals when loading %R", w);
1999 why = WHY_EXCEPTION;
2000 break;
2001 }
2002 if (PyDict_CheckExact(v)) {
2003 x = PyDict_GetItem(v, w);
2004 Py_XINCREF(x);
2005 }
2006 else {
2007 x = PyObject_GetItem(v, w);
2008 if (x == NULL && PyErr_Occurred()) {
2009 if (!PyErr_ExceptionMatches(
2010 PyExc_KeyError))
2011 break;
2012 PyErr_Clear();
2013 }
2014 }
2015 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002016 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002018 x = PyDict_GetItem(f->f_builtins, w);
2019 if (x == NULL) {
2020 format_exc_check_arg(
2021 PyExc_NameError,
2022 NAME_ERROR_MSG, w);
2023 break;
2024 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
2026 Py_INCREF(x);
2027 }
2028 PUSH(x);
2029 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 TARGET(LOAD_GLOBAL)
2032 w = GETITEM(names, oparg);
2033 if (PyUnicode_CheckExact(w)) {
2034 /* Inline the PyDict_GetItem() calls.
2035 WARNING: this is an extreme speed hack.
2036 Do not try this at home. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002037 Py_hash_t hash = ((PyASCIIObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 if (hash != -1) {
2039 PyDictObject *d;
2040 PyDictEntry *e;
2041 d = (PyDictObject *)(f->f_globals);
2042 e = d->ma_lookup(d, w, hash);
2043 if (e == NULL) {
2044 x = NULL;
2045 break;
2046 }
2047 x = e->me_value;
2048 if (x != NULL) {
2049 Py_INCREF(x);
2050 PUSH(x);
2051 DISPATCH();
2052 }
2053 d = (PyDictObject *)(f->f_builtins);
2054 e = d->ma_lookup(d, w, hash);
2055 if (e == NULL) {
2056 x = NULL;
2057 break;
2058 }
2059 x = e->me_value;
2060 if (x != NULL) {
2061 Py_INCREF(x);
2062 PUSH(x);
2063 DISPATCH();
2064 }
2065 goto load_global_error;
2066 }
2067 }
2068 /* This is the un-inlined version of the code above */
2069 x = PyDict_GetItem(f->f_globals, w);
2070 if (x == NULL) {
2071 x = PyDict_GetItem(f->f_builtins, w);
2072 if (x == NULL) {
2073 load_global_error:
2074 format_exc_check_arg(
2075 PyExc_NameError,
2076 GLOBAL_NAME_ERROR_MSG, w);
2077 break;
2078 }
2079 }
2080 Py_INCREF(x);
2081 PUSH(x);
2082 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 TARGET(DELETE_FAST)
2085 x = GETLOCAL(oparg);
2086 if (x != NULL) {
2087 SETLOCAL(oparg, NULL);
2088 DISPATCH();
2089 }
2090 format_exc_check_arg(
2091 PyExc_UnboundLocalError,
2092 UNBOUNDLOCAL_ERROR_MSG,
2093 PyTuple_GetItem(co->co_varnames, oparg)
2094 );
2095 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002096
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002097 TARGET(DELETE_DEREF)
2098 x = freevars[oparg];
2099 if (PyCell_GET(x) != NULL) {
2100 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002101 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002102 }
2103 err = -1;
2104 format_exc_unbound(co, oparg);
2105 break;
2106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 TARGET(LOAD_CLOSURE)
2108 x = freevars[oparg];
2109 Py_INCREF(x);
2110 PUSH(x);
2111 if (x != NULL) DISPATCH();
2112 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 TARGET(LOAD_DEREF)
2115 x = freevars[oparg];
2116 w = PyCell_Get(x);
2117 if (w != NULL) {
2118 PUSH(w);
2119 DISPATCH();
2120 }
2121 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002122 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 TARGET(STORE_DEREF)
2126 w = POP();
2127 x = freevars[oparg];
2128 PyCell_Set(x, w);
2129 Py_DECREF(w);
2130 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 TARGET(BUILD_TUPLE)
2133 x = PyTuple_New(oparg);
2134 if (x != NULL) {
2135 for (; --oparg >= 0;) {
2136 w = POP();
2137 PyTuple_SET_ITEM(x, oparg, w);
2138 }
2139 PUSH(x);
2140 DISPATCH();
2141 }
2142 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 TARGET(BUILD_LIST)
2145 x = PyList_New(oparg);
2146 if (x != NULL) {
2147 for (; --oparg >= 0;) {
2148 w = POP();
2149 PyList_SET_ITEM(x, oparg, w);
2150 }
2151 PUSH(x);
2152 DISPATCH();
2153 }
2154 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 TARGET(BUILD_SET)
2157 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);
2170 DISPATCH();
2171 }
2172 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 TARGET(BUILD_MAP)
2175 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2176 PUSH(x);
2177 if (x != NULL) DISPATCH();
2178 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 TARGET(STORE_MAP)
2181 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);
2189 if (err == 0) DISPATCH();
2190 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 TARGET(MAP_ADD)
2193 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);
2203 DISPATCH();
2204 }
2205 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 TARGET(LOAD_ATTR)
2208 w = GETITEM(names, oparg);
2209 v = TOP();
2210 x = PyObject_GetAttr(v, w);
2211 Py_DECREF(v);
2212 SET_TOP(x);
2213 if (x != NULL) DISPATCH();
2214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 TARGET(COMPARE_OP)
2217 w = POP();
2218 v = TOP();
2219 x = cmp_outcome(oparg, v, w);
2220 Py_DECREF(v);
2221 Py_DECREF(w);
2222 SET_TOP(x);
2223 if (x == NULL) break;
2224 PREDICT(POP_JUMP_IF_FALSE);
2225 PREDICT(POP_JUMP_IF_TRUE);
2226 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 TARGET(IMPORT_NAME)
2229 w = GETITEM(names, oparg);
2230 x = PyDict_GetItemString(f->f_builtins, "__import__");
2231 if (x == NULL) {
2232 PyErr_SetString(PyExc_ImportError,
2233 "__import__ not found");
2234 break;
2235 }
2236 Py_INCREF(x);
2237 v = POP();
2238 u = TOP();
2239 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2240 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);
2255 Py_DECREF(u);
2256 if (w == NULL) {
2257 u = POP();
2258 Py_DECREF(x);
2259 x = NULL;
2260 break;
2261 }
2262 READ_TIMESTAMP(intr0);
2263 v = x;
2264 x = PyEval_CallObject(v, w);
2265 Py_DECREF(v);
2266 READ_TIMESTAMP(intr1);
2267 Py_DECREF(w);
2268 SET_TOP(x);
2269 if (x != NULL) DISPATCH();
2270 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 TARGET(IMPORT_STAR)
2273 v = POP();
2274 PyFrame_FastToLocals(f);
2275 if ((x = f->f_locals) == NULL) {
2276 PyErr_SetString(PyExc_SystemError,
2277 "no locals found during 'import *'");
2278 break;
2279 }
2280 READ_TIMESTAMP(intr0);
2281 err = import_all_from(x, v);
2282 READ_TIMESTAMP(intr1);
2283 PyFrame_LocalsToFast(f, 0);
2284 Py_DECREF(v);
2285 if (err == 0) DISPATCH();
2286 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 TARGET(IMPORT_FROM)
2289 w = GETITEM(names, oparg);
2290 v = TOP();
2291 READ_TIMESTAMP(intr0);
2292 x = import_from(v, w);
2293 READ_TIMESTAMP(intr1);
2294 PUSH(x);
2295 if (x != NULL) DISPATCH();
2296 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 TARGET(JUMP_FORWARD)
2299 JUMPBY(oparg);
2300 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2303 TARGET(POP_JUMP_IF_FALSE)
2304 w = POP();
2305 if (w == Py_True) {
2306 Py_DECREF(w);
2307 FAST_DISPATCH();
2308 }
2309 if (w == Py_False) {
2310 Py_DECREF(w);
2311 JUMPTO(oparg);
2312 FAST_DISPATCH();
2313 }
2314 err = PyObject_IsTrue(w);
2315 Py_DECREF(w);
2316 if (err > 0)
2317 err = 0;
2318 else if (err == 0)
2319 JUMPTO(oparg);
2320 else
2321 break;
2322 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2325 TARGET(POP_JUMP_IF_TRUE)
2326 w = POP();
2327 if (w == Py_False) {
2328 Py_DECREF(w);
2329 FAST_DISPATCH();
2330 }
2331 if (w == Py_True) {
2332 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();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 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();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 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);
2380 FAST_DISPATCH();
2381 }
2382 err = PyObject_IsTrue(w);
2383 if (err > 0) {
2384 err = 0;
2385 JUMPTO(oparg);
2386 }
2387 else if (err == 0) {
2388 STACKADJ(-1);
2389 Py_DECREF(w);
2390 }
2391 else
2392 break;
2393 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2396 TARGET(JUMP_ABSOLUTE)
2397 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002398#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 /* 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
2404 that contain only instructions ending with FAST_DISPATCH).
2405 */
2406 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002407#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002409#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 TARGET(GET_ITER)
2412 /* before: [obj]; after [getiter(obj)] */
2413 v = TOP();
2414 x = PyObject_GetIter(v);
2415 Py_DECREF(v);
2416 if (x != NULL) {
2417 SET_TOP(x);
2418 PREDICT(FOR_ITER);
2419 DISPATCH();
2420 }
2421 STACKADJ(-1);
2422 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 PREDICTED_WITH_ARG(FOR_ITER);
2425 TARGET(FOR_ITER)
2426 /* before: [iter]; after: [iter, iter()] *or* [] */
2427 v = TOP();
2428 x = (*v->ob_type->tp_iternext)(v);
2429 if (x != NULL) {
2430 PUSH(x);
2431 PREDICT(STORE_FAST);
2432 PREDICT(UNPACK_SEQUENCE);
2433 DISPATCH();
2434 }
2435 if (PyErr_Occurred()) {
2436 if (!PyErr_ExceptionMatches(
2437 PyExc_StopIteration))
2438 break;
2439 PyErr_Clear();
2440 }
2441 /* iterator ended normally */
2442 x = v = POP();
2443 Py_DECREF(v);
2444 JUMPBY(oparg);
2445 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 TARGET(BREAK_LOOP)
2448 why = WHY_BREAK;
2449 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 TARGET(CONTINUE_LOOP)
2452 retval = PyLong_FromLong(oparg);
2453 if (!retval) {
2454 x = NULL;
2455 break;
2456 }
2457 why = WHY_CONTINUE;
2458 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2461 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2462 TARGET(SETUP_FINALLY)
2463 _setup_finally:
2464 /* 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2470 STACK_LEVEL());
2471 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 PUSH(x);
2497 DISPATCH();
2498 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 TARGET(WITH_CLEANUP)
2501 {
2502 /* 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()
2508 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2509 Below them is EXIT, the context.__exit__ bound method.
2510 In the last case, we must call
2511 EXIT(TOP, SECOND, THIRD)
2512 otherwise we must call
2513 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 In addition, if the stack represents an exception,
2521 *and* the function call returns a 'true' value, we
2522 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.)
2525 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 PyObject *exit_func;
2528 u = TOP();
2529 if (u == Py_None) {
2530 (void)POP();
2531 exit_func = TOP();
2532 SET_TOP(u);
2533 v = w = Py_None;
2534 }
2535 else if (PyLong_Check(u)) {
2536 (void)POP();
2537 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 }
2550 u = v = w = Py_None;
2551 }
2552 else {
2553 PyObject *tp, *exc, *tb;
2554 PyTryBlock *block;
2555 v = SECOND();
2556 w = THIRD();
2557 tp = FOURTH();
2558 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);
2564 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2565 SET_FOURTH(NULL);
2566 /* 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. */
2569 block = &f->f_blockstack[f->f_iblock - 1];
2570 assert(block->b_type == EXCEPT_HANDLER);
2571 block->b_level--;
2572 }
2573 /* XXX Not the fastest way to call it... */
2574 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2575 NULL);
2576 Py_DECREF(exit_func);
2577 if (x == NULL)
2578 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 if (u != Py_None)
2581 err = PyObject_IsTrue(x);
2582 else
2583 err = 0;
2584 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 if (err < 0)
2587 break; /* Go to error exit */
2588 else if (err > 0) {
2589 err = 0;
2590 /* There was an exception and a True return */
2591 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2592 }
2593 PREDICT(END_FINALLY);
2594 break;
2595 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 TARGET(CALL_FUNCTION)
2598 {
2599 PyObject **sp;
2600 PCALL(PCALL_ALL);
2601 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002602#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002604#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002606#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 stack_pointer = sp;
2608 PUSH(x);
2609 if (x != NULL)
2610 DISPATCH();
2611 break;
2612 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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:
2618 {
2619 int na = oparg & 0xff;
2620 int nk = (oparg>>8) & 0xff;
2621 int flags = (opcode - CALL_FUNCTION) & 3;
2622 int n = na + 2 * nk;
2623 PyObject **pfunc, *func, **sp;
2624 PCALL(PCALL_ALL);
2625 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002633 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 PyObject *self = PyMethod_GET_SELF(func);
2635 Py_INCREF(self);
2636 func = PyMethod_GET_FUNCTION(func);
2637 Py_INCREF(func);
2638 Py_DECREF(*pfunc);
2639 *pfunc = self;
2640 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002641 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 } else
2643 Py_INCREF(func);
2644 sp = stack_pointer;
2645 READ_TIMESTAMP(intr0);
2646 x = ext_do_call(func, &sp, flags, na, nk);
2647 READ_TIMESTAMP(intr1);
2648 stack_pointer = sp;
2649 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 while (stack_pointer > pfunc) {
2652 w = POP();
2653 Py_DECREF(w);
2654 }
2655 PUSH(x);
2656 if (x != NULL)
2657 DISPATCH();
2658 break;
2659 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2662 TARGET(MAKE_FUNCTION)
2663 _make_function:
2664 {
2665 int posdefaults = oparg & 0xff;
2666 int kwdefaults = (oparg>>8) & 0xff;
2667 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 v = POP(); /* code object */
2670 x = PyFunction_New(v, f->f_globals);
2671 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 if (x != NULL && opcode == MAKE_CLOSURE) {
2674 v = POP();
2675 if (PyFunction_SetClosure(x, v) != 0) {
2676 /* Can't happen unless bytecode is corrupt. */
2677 why = WHY_EXCEPTION;
2678 }
2679 Py_DECREF(v);
2680 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 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 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 if (PyFunction_SetAnnotations(x, v) != 0) {
2703 /* Can't happen unless
2704 PyFunction_SetAnnotations changes. */
2705 why = WHY_EXCEPTION;
2706 }
2707 Py_DECREF(v);
2708 Py_DECREF(u);
2709 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 /* XXX Maybe this should be a separate opcode? */
2712 if (x != NULL && posdefaults > 0) {
2713 v = PyTuple_New(posdefaults);
2714 if (v == NULL) {
2715 Py_DECREF(x);
2716 x = NULL;
2717 break;
2718 }
2719 while (--posdefaults >= 0) {
2720 w = POP();
2721 PyTuple_SET_ITEM(v, posdefaults, w);
2722 }
2723 if (PyFunction_SetDefaults(x, v) != 0) {
2724 /* Can't happen unless
2725 PyFunction_SetDefaults changes. */
2726 why = WHY_EXCEPTION;
2727 }
2728 Py_DECREF(v);
2729 }
2730 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 */
2740 /* XXX(nnorwitz): check for errors */
2741 PyDict_SetItem(v, u, w);
2742 Py_DECREF(w);
2743 Py_DECREF(u);
2744 }
2745 if (PyFunction_SetKwDefaults(x, v) != 0) {
2746 /* Can't happen unless
2747 PyFunction_SetKwDefaults changes. */
2748 why = WHY_EXCEPTION;
2749 }
2750 Py_DECREF(v);
2751 }
2752 PUSH(x);
2753 break;
2754 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 TARGET(BUILD_SLICE)
2757 if (oparg == 3)
2758 w = POP();
2759 else
2760 w = NULL;
2761 v = POP();
2762 u = TOP();
2763 x = PySlice_New(u, v, w);
2764 Py_DECREF(u);
2765 Py_DECREF(v);
2766 Py_XDECREF(w);
2767 SET_TOP(x);
2768 if (x != NULL) DISPATCH();
2769 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 TARGET(EXTENDED_ARG)
2772 opcode = NEXTOP();
2773 oparg = oparg<<16 | NEXTARG();
2774 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002775
Antoine Pitrou042b1282010-08-13 21:15:58 +00002776#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002778#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 default:
2780 fprintf(stderr,
2781 "XXX lineno: %d, opcode: %d\n",
2782 PyFrame_GetLineNumber(f),
2783 opcode);
2784 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2785 why = WHY_EXCEPTION;
2786 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002787
2788#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002790#endif
2791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 if (why == WHY_NOT) {
2801 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002802#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 /* This check is expensive! */
2804 if (PyErr_Occurred())
2805 fprintf(stderr,
2806 "XXX undetected error\n");
2807 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002808#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 READ_TIMESTAMP(loop1);
2810 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002811#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002813#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 }
2815 why = WHY_EXCEPTION;
2816 x = Py_None;
2817 err = 0;
2818 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2823 if (!PyErr_Occurred()) {
2824 PyErr_SetString(PyExc_SystemError,
2825 "error return without exception set");
2826 why = WHY_EXCEPTION;
2827 }
2828 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002829#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 else {
2831 /* This check is expensive! */
2832 if (PyErr_Occurred()) {
2833 char buf[128];
2834 sprintf(buf, "Stack unwind with exception "
2835 "set and why=%d", why);
2836 Py_FatalError(buf);
2837 }
2838 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002839#endif
2840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 if (why == WHY_EXCEPTION) {
2844 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 if (tstate->c_tracefunc != NULL)
2847 call_exc_trace(tstate->c_tracefunc,
2848 tstate->c_traceobj, f);
2849 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 if (why == WHY_RERAISE)
2854 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 /* 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:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 while (why != WHY_NOT && f->f_iblock > 0) {
2860 /* Peek at the current block. */
2861 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 assert(why != WHY_YIELD);
2864 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2865 why = WHY_NOT;
2866 JUMPTO(PyLong_AS_LONG(retval));
2867 Py_DECREF(retval);
2868 break;
2869 }
2870 /* Now we have to pop the block. */
2871 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 if (b->b_type == EXCEPT_HANDLER) {
2874 UNWIND_EXCEPT_HANDLER(b);
2875 continue;
2876 }
2877 UNWIND_BLOCK(b);
2878 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2879 why = WHY_NOT;
2880 JUMPTO(b->b_handler);
2881 break;
2882 }
2883 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);
2893 }
2894 else {
2895 Py_INCREF(Py_None);
2896 PUSH(Py_None);
2897 }
2898 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));
2925 why = WHY_NOT;
2926 JUMPTO(b->b_handler);
2927 break;
2928 }
2929 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 if (why != WHY_NOT)
2934 break;
2935 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 assert(why != WHY_YIELD);
2940 /* Pop remaining stack entries. */
2941 while (!EMPTY()) {
2942 v = POP();
2943 Py_XDECREF(v);
2944 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 if (why != WHY_RETURN)
2947 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002948
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002949fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05002950 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
2951 /* The purpose of this block is to put aside the generator's exception
2952 state and restore that of the calling frame. If the current
2953 exception state is from the caller, we clear the exception values
2954 on the generator frame, so they are not swapped back in latter. The
2955 origin of the current exception state is determined by checking for
2956 except handler blocks, which we must be in iff a new exception
2957 state came into existence in this frame. (An uncaught exception
2958 would have why == WHY_EXCEPTION, and we wouldn't be here). */
2959 int i;
2960 for (i = 0; i < f->f_iblock; i++)
2961 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
2962 break;
2963 if (i == f->f_iblock)
2964 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05002965 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05002966 else
Benjamin Peterson87880242011-07-03 16:48:31 -05002967 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05002968 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05002969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 if (tstate->use_tracing) {
2971 if (tstate->c_tracefunc) {
2972 if (why == WHY_RETURN || why == WHY_YIELD) {
2973 if (call_trace(tstate->c_tracefunc,
2974 tstate->c_traceobj, f,
2975 PyTrace_RETURN, retval)) {
2976 Py_XDECREF(retval);
2977 retval = NULL;
2978 why = WHY_EXCEPTION;
2979 }
2980 }
2981 else if (why == WHY_EXCEPTION) {
2982 call_trace_protected(tstate->c_tracefunc,
2983 tstate->c_traceobj, f,
2984 PyTrace_RETURN, NULL);
2985 }
2986 }
2987 if (tstate->c_profilefunc) {
2988 if (why == WHY_EXCEPTION)
2989 call_trace_protected(tstate->c_profilefunc,
2990 tstate->c_profileobj, f,
2991 PyTrace_RETURN, NULL);
2992 else if (call_trace(tstate->c_profilefunc,
2993 tstate->c_profileobj, f,
2994 PyTrace_RETURN, retval)) {
2995 Py_XDECREF(retval);
2996 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00002997 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 }
2999 }
3000 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003003exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 Py_LeaveRecursiveCall();
3005 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003008}
3009
Benjamin Petersonb204a422011-06-05 22:04:07 -05003010static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003011format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3012{
3013 int err;
3014 Py_ssize_t len = PyList_GET_SIZE(names);
3015 PyObject *name_str, *comma, *tail, *tmp;
3016
3017 assert(PyList_CheckExact(names));
3018 assert(len >= 1);
3019 /* Deal with the joys of natural language. */
3020 switch (len) {
3021 case 1:
3022 name_str = PyList_GET_ITEM(names, 0);
3023 Py_INCREF(name_str);
3024 break;
3025 case 2:
3026 name_str = PyUnicode_FromFormat("%U and %U",
3027 PyList_GET_ITEM(names, len - 2),
3028 PyList_GET_ITEM(names, len - 1));
3029 break;
3030 default:
3031 tail = PyUnicode_FromFormat(", %U, and %U",
3032 PyList_GET_ITEM(names, len - 2),
3033 PyList_GET_ITEM(names, len - 1));
3034 /* Chop off the last two objects in the list. This shouldn't actually
3035 fail, but we can't be too careful. */
3036 err = PyList_SetSlice(names, len - 2, len, NULL);
3037 if (err == -1) {
3038 Py_DECREF(tail);
3039 return;
3040 }
3041 /* Stitch everything up into a nice comma-separated list. */
3042 comma = PyUnicode_FromString(", ");
3043 if (comma == NULL) {
3044 Py_DECREF(tail);
3045 return;
3046 }
3047 tmp = PyUnicode_Join(comma, names);
3048 Py_DECREF(comma);
3049 if (tmp == NULL) {
3050 Py_DECREF(tail);
3051 return;
3052 }
3053 name_str = PyUnicode_Concat(tmp, tail);
3054 Py_DECREF(tmp);
3055 Py_DECREF(tail);
3056 break;
3057 }
3058 if (name_str == NULL)
3059 return;
3060 PyErr_Format(PyExc_TypeError,
3061 "%U() missing %i required %s argument%s: %U",
3062 co->co_name,
3063 len,
3064 kind,
3065 len == 1 ? "" : "s",
3066 name_str);
3067 Py_DECREF(name_str);
3068}
3069
3070static void
3071missing_arguments(PyCodeObject *co, int missing, int defcount,
3072 PyObject **fastlocals)
3073{
3074 int i, j = 0;
3075 int start, end;
3076 int positional = defcount != -1;
3077 const char *kind = positional ? "positional" : "keyword-only";
3078 PyObject *missing_names;
3079
3080 /* Compute the names of the arguments that are missing. */
3081 missing_names = PyList_New(missing);
3082 if (missing_names == NULL)
3083 return;
3084 if (positional) {
3085 start = 0;
3086 end = co->co_argcount - defcount;
3087 }
3088 else {
3089 start = co->co_argcount;
3090 end = start + co->co_kwonlyargcount;
3091 }
3092 for (i = start; i < end; i++) {
3093 if (GETLOCAL(i) == NULL) {
3094 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3095 PyObject *name = PyObject_Repr(raw);
3096 if (name == NULL) {
3097 Py_DECREF(missing_names);
3098 return;
3099 }
3100 PyList_SET_ITEM(missing_names, j++, name);
3101 }
3102 }
3103 assert(j == missing);
3104 format_missing(kind, co, missing_names);
3105 Py_DECREF(missing_names);
3106}
3107
3108static void
3109too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003110{
3111 int plural;
3112 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003113 int i;
3114 PyObject *sig, *kwonly_sig;
3115
Benjamin Petersone109c702011-06-24 09:37:26 -05003116 assert((co->co_flags & CO_VARARGS) == 0);
3117 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003118 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003119 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003120 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003121 if (defcount) {
3122 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003123 plural = 1;
3124 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3125 }
3126 else {
3127 plural = co->co_argcount != 1;
3128 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3129 }
3130 if (sig == NULL)
3131 return;
3132 if (kwonly_given) {
3133 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3134 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3135 kwonly_given != 1 ? "s" : "");
3136 if (kwonly_sig == NULL) {
3137 Py_DECREF(sig);
3138 return;
3139 }
3140 }
3141 else {
3142 /* This will not fail. */
3143 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003144 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003145 }
3146 PyErr_Format(PyExc_TypeError,
3147 "%U() takes %U positional argument%s but %d%U %s given",
3148 co->co_name,
3149 sig,
3150 plural ? "s" : "",
3151 given,
3152 kwonly_sig,
3153 given == 1 && !kwonly_given ? "was" : "were");
3154 Py_DECREF(sig);
3155 Py_DECREF(kwonly_sig);
3156}
3157
Guido van Rossumc2e20742006-02-27 22:32:47 +00003158/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003159 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003160 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003161
Tim Peters6d6c1a32001-08-02 04:15:00 +00003162PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003163PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 PyObject **args, int argcount, PyObject **kws, int kwcount,
3165 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003166{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003167 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 register PyFrameObject *f;
3169 register PyObject *retval = NULL;
3170 register PyObject **fastlocals, **freevars;
3171 PyThreadState *tstate = PyThreadState_GET();
3172 PyObject *x, *u;
3173 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003174 int i;
3175 int n = argcount;
3176 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 if (globals == NULL) {
3179 PyErr_SetString(PyExc_SystemError,
3180 "PyEval_EvalCodeEx: NULL globals");
3181 return NULL;
3182 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 assert(tstate != NULL);
3185 assert(globals != NULL);
3186 f = PyFrame_New(tstate, co, globals, locals);
3187 if (f == NULL)
3188 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 fastlocals = f->f_localsplus;
3191 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003192
Benjamin Petersonb204a422011-06-05 22:04:07 -05003193 /* Parse arguments. */
3194 if (co->co_flags & CO_VARKEYWORDS) {
3195 kwdict = PyDict_New();
3196 if (kwdict == NULL)
3197 goto fail;
3198 i = total_args;
3199 if (co->co_flags & CO_VARARGS)
3200 i++;
3201 SETLOCAL(i, kwdict);
3202 }
3203 if (argcount > co->co_argcount)
3204 n = co->co_argcount;
3205 for (i = 0; i < n; i++) {
3206 x = args[i];
3207 Py_INCREF(x);
3208 SETLOCAL(i, x);
3209 }
3210 if (co->co_flags & CO_VARARGS) {
3211 u = PyTuple_New(argcount - n);
3212 if (u == NULL)
3213 goto fail;
3214 SETLOCAL(total_args, u);
3215 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 x = args[i];
3217 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003218 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003220 }
3221 for (i = 0; i < kwcount; i++) {
3222 PyObject **co_varnames;
3223 PyObject *keyword = kws[2*i];
3224 PyObject *value = kws[2*i + 1];
3225 int j;
3226 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3227 PyErr_Format(PyExc_TypeError,
3228 "%U() keywords must be strings",
3229 co->co_name);
3230 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003232 /* Speed hack: do raw pointer compares. As names are
3233 normally interned this should almost always hit. */
3234 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3235 for (j = 0; j < total_args; j++) {
3236 PyObject *nm = co_varnames[j];
3237 if (nm == keyword)
3238 goto kw_found;
3239 }
3240 /* Slow fallback, just in case */
3241 for (j = 0; j < total_args; j++) {
3242 PyObject *nm = co_varnames[j];
3243 int cmp = PyObject_RichCompareBool(
3244 keyword, nm, Py_EQ);
3245 if (cmp > 0)
3246 goto kw_found;
3247 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003249 }
3250 if (j >= total_args && kwdict == NULL) {
3251 PyErr_Format(PyExc_TypeError,
3252 "%U() got an unexpected "
3253 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 co->co_name,
3255 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003256 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003258 PyDict_SetItem(kwdict, keyword, value);
3259 continue;
3260 kw_found:
3261 if (GETLOCAL(j) != NULL) {
3262 PyErr_Format(PyExc_TypeError,
3263 "%U() got multiple "
3264 "values for argument '%S'",
3265 co->co_name,
3266 keyword);
3267 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003269 Py_INCREF(value);
3270 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003272 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003273 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 goto fail;
3275 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003276 if (argcount < co->co_argcount) {
3277 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003278 int missing = 0;
3279 for (i = argcount; i < m; i++)
3280 if (GETLOCAL(i) == NULL)
3281 missing++;
3282 if (missing) {
3283 missing_arguments(co, missing, defcount, fastlocals);
3284 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003285 }
3286 if (n > m)
3287 i = n - m;
3288 else
3289 i = 0;
3290 for (; i < defcount; i++) {
3291 if (GETLOCAL(m+i) == NULL) {
3292 PyObject *def = defs[i];
3293 Py_INCREF(def);
3294 SETLOCAL(m+i, def);
3295 }
3296 }
3297 }
3298 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003299 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003300 for (i = co->co_argcount; i < total_args; i++) {
3301 PyObject *name;
3302 if (GETLOCAL(i) != NULL)
3303 continue;
3304 name = PyTuple_GET_ITEM(co->co_varnames, i);
3305 if (kwdefs != NULL) {
3306 PyObject *def = PyDict_GetItem(kwdefs, name);
3307 if (def) {
3308 Py_INCREF(def);
3309 SETLOCAL(i, def);
3310 continue;
3311 }
3312 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003313 missing++;
3314 }
3315 if (missing) {
3316 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003317 goto fail;
3318 }
3319 }
3320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003322 vars into frame. */
3323 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003325 int arg;
3326 /* Possibly account for the cell variable being an argument. */
3327 if (co->co_cell2arg != NULL &&
3328 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3329 c = PyCell_New(GETLOCAL(arg));
3330 else
3331 c = PyCell_New(NULL);
3332 if (c == NULL)
3333 goto fail;
3334 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003336 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3337 PyObject *o = PyTuple_GET_ITEM(closure, i);
3338 Py_INCREF(o);
3339 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 if (co->co_flags & CO_GENERATOR) {
3343 /* Don't need to keep the reference to f_back, it will be set
3344 * when the generator is resumed. */
3345 Py_XDECREF(f->f_back);
3346 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 /* Create a new generator that owns the ready to run frame
3351 * and return that as the value. */
3352 return PyGen_New(f);
3353 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003356
Thomas Woutersce272b62007-09-19 21:19:28 +00003357fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 /* decref'ing the frame can cause __del__ methods to get invoked,
3360 which can call back into Python. While we're done with the
3361 current Python frame (f), the associated C stack is still in use,
3362 so recursion_depth must be boosted for the duration.
3363 */
3364 assert(tstate != NULL);
3365 ++tstate->recursion_depth;
3366 Py_DECREF(f);
3367 --tstate->recursion_depth;
3368 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003369}
3370
3371
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003372static PyObject *
3373special_lookup(PyObject *o, char *meth, PyObject **cache)
3374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 PyObject *res;
3376 res = _PyObject_LookupSpecial(o, meth, cache);
3377 if (res == NULL && !PyErr_Occurred()) {
3378 PyErr_SetObject(PyExc_AttributeError, *cache);
3379 return NULL;
3380 }
3381 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003382}
3383
3384
Benjamin Peterson87880242011-07-03 16:48:31 -05003385/* These 3 functions deal with the exception state of generators. */
3386
3387static void
3388save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3389{
3390 PyObject *type, *value, *traceback;
3391 Py_XINCREF(tstate->exc_type);
3392 Py_XINCREF(tstate->exc_value);
3393 Py_XINCREF(tstate->exc_traceback);
3394 type = f->f_exc_type;
3395 value = f->f_exc_value;
3396 traceback = f->f_exc_traceback;
3397 f->f_exc_type = tstate->exc_type;
3398 f->f_exc_value = tstate->exc_value;
3399 f->f_exc_traceback = tstate->exc_traceback;
3400 Py_XDECREF(type);
3401 Py_XDECREF(value);
3402 Py_XDECREF(traceback);
3403}
3404
3405static void
3406swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3407{
3408 PyObject *tmp;
3409 tmp = tstate->exc_type;
3410 tstate->exc_type = f->f_exc_type;
3411 f->f_exc_type = tmp;
3412 tmp = tstate->exc_value;
3413 tstate->exc_value = f->f_exc_value;
3414 f->f_exc_value = tmp;
3415 tmp = tstate->exc_traceback;
3416 tstate->exc_traceback = f->f_exc_traceback;
3417 f->f_exc_traceback = tmp;
3418}
3419
3420static void
3421restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3422{
3423 PyObject *type, *value, *tb;
3424 type = tstate->exc_type;
3425 value = tstate->exc_value;
3426 tb = tstate->exc_traceback;
3427 tstate->exc_type = f->f_exc_type;
3428 tstate->exc_value = f->f_exc_value;
3429 tstate->exc_traceback = f->f_exc_traceback;
3430 f->f_exc_type = NULL;
3431 f->f_exc_value = NULL;
3432 f->f_exc_traceback = NULL;
3433 Py_XDECREF(type);
3434 Py_XDECREF(value);
3435 Py_XDECREF(tb);
3436}
3437
3438
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003439/* Logic for the raise statement (too complicated for inlining).
3440 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003441static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003442do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 if (exc == NULL) {
3447 /* Reraise */
3448 PyThreadState *tstate = PyThreadState_GET();
3449 PyObject *tb;
3450 type = tstate->exc_type;
3451 value = tstate->exc_value;
3452 tb = tstate->exc_traceback;
3453 if (type == Py_None) {
3454 PyErr_SetString(PyExc_RuntimeError,
3455 "No active exception to reraise");
3456 return WHY_EXCEPTION;
3457 }
3458 Py_XINCREF(type);
3459 Py_XINCREF(value);
3460 Py_XINCREF(tb);
3461 PyErr_Restore(type, value, tb);
3462 return WHY_RERAISE;
3463 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 /* We support the following forms of raise:
3466 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003467 raise <instance>
3468 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 if (PyExceptionClass_Check(exc)) {
3471 type = exc;
3472 value = PyObject_CallObject(exc, NULL);
3473 if (value == NULL)
3474 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003475 if (!PyExceptionInstance_Check(value)) {
3476 PyErr_Format(PyExc_TypeError,
3477 "calling %R should have returned an instance of "
3478 "BaseException, not %R",
3479 type, Py_TYPE(value));
3480 goto raise_error;
3481 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 }
3483 else if (PyExceptionInstance_Check(exc)) {
3484 value = exc;
3485 type = PyExceptionInstance_Class(exc);
3486 Py_INCREF(type);
3487 }
3488 else {
3489 /* Not something you can raise. You get an exception
3490 anyway, just not what you specified :-) */
3491 Py_DECREF(exc);
3492 PyErr_SetString(PyExc_TypeError,
3493 "exceptions must derive from BaseException");
3494 goto raise_error;
3495 }
Collin Winter828f04a2007-08-31 00:04:24 +00003496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 if (cause) {
3498 PyObject *fixed_cause;
3499 if (PyExceptionClass_Check(cause)) {
3500 fixed_cause = PyObject_CallObject(cause, NULL);
3501 if (fixed_cause == NULL)
3502 goto raise_error;
3503 Py_DECREF(cause);
3504 }
3505 else if (PyExceptionInstance_Check(cause)) {
3506 fixed_cause = cause;
3507 }
3508 else {
3509 PyErr_SetString(PyExc_TypeError,
3510 "exception causes must derive from "
3511 "BaseException");
3512 goto raise_error;
3513 }
3514 PyException_SetCause(value, fixed_cause);
3515 }
Collin Winter828f04a2007-08-31 00:04:24 +00003516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 PyErr_SetObject(type, value);
3518 /* PyErr_SetObject incref's its arguments */
3519 Py_XDECREF(value);
3520 Py_XDECREF(type);
3521 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003522
3523raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 Py_XDECREF(value);
3525 Py_XDECREF(type);
3526 Py_XDECREF(cause);
3527 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003528}
3529
Tim Petersd6d010b2001-06-21 02:49:55 +00003530/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003531 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003532
Guido van Rossum0368b722007-05-11 16:50:42 +00003533 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3534 with a variable target.
3535*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003536
Barry Warsawe42b18f1997-08-25 22:13:04 +00003537static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003538unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 int i = 0, j = 0;
3541 Py_ssize_t ll = 0;
3542 PyObject *it; /* iter(v) */
3543 PyObject *w;
3544 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 it = PyObject_GetIter(v);
3549 if (it == NULL)
3550 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 for (; i < argcnt; i++) {
3553 w = PyIter_Next(it);
3554 if (w == NULL) {
3555 /* Iterator done, via error or exhaustion. */
3556 if (!PyErr_Occurred()) {
3557 PyErr_Format(PyExc_ValueError,
3558 "need more than %d value%s to unpack",
3559 i, i == 1 ? "" : "s");
3560 }
3561 goto Error;
3562 }
3563 *--sp = w;
3564 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 if (argcntafter == -1) {
3567 /* We better have exhausted the iterator now. */
3568 w = PyIter_Next(it);
3569 if (w == NULL) {
3570 if (PyErr_Occurred())
3571 goto Error;
3572 Py_DECREF(it);
3573 return 1;
3574 }
3575 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003576 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3577 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 goto Error;
3579 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 l = PySequence_List(it);
3582 if (l == NULL)
3583 goto Error;
3584 *--sp = l;
3585 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 ll = PyList_GET_SIZE(l);
3588 if (ll < argcntafter) {
3589 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3590 argcnt + ll);
3591 goto Error;
3592 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 /* Pop the "after-variable" args off the list. */
3595 for (j = argcntafter; j > 0; j--, i++) {
3596 *--sp = PyList_GET_ITEM(l, ll - j);
3597 }
3598 /* Resize the list. */
3599 Py_SIZE(l) = ll - argcntafter;
3600 Py_DECREF(it);
3601 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003602
Tim Petersd6d010b2001-06-21 02:49:55 +00003603Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 for (; i > 0; i--, sp++)
3605 Py_DECREF(*sp);
3606 Py_XDECREF(it);
3607 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003608}
3609
3610
Guido van Rossum96a42c81992-01-12 02:29:51 +00003611#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003612static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003613prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 printf("%s ", str);
3616 if (PyObject_Print(v, stdout, 0) != 0)
3617 PyErr_Clear(); /* Don't know what else to do */
3618 printf("\n");
3619 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003620}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003621#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003622
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003623static void
Fred Drake5755ce62001-06-27 19:19:46 +00003624call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 PyObject *type, *value, *traceback, *arg;
3627 int err;
3628 PyErr_Fetch(&type, &value, &traceback);
3629 if (value == NULL) {
3630 value = Py_None;
3631 Py_INCREF(value);
3632 }
3633 arg = PyTuple_Pack(3, type, value, traceback);
3634 if (arg == NULL) {
3635 PyErr_Restore(type, value, traceback);
3636 return;
3637 }
3638 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3639 Py_DECREF(arg);
3640 if (err == 0)
3641 PyErr_Restore(type, value, traceback);
3642 else {
3643 Py_XDECREF(type);
3644 Py_XDECREF(value);
3645 Py_XDECREF(traceback);
3646 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003647}
3648
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003649static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003650call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 PyObject *type, *value, *traceback;
3654 int err;
3655 PyErr_Fetch(&type, &value, &traceback);
3656 err = call_trace(func, obj, frame, what, arg);
3657 if (err == 0)
3658 {
3659 PyErr_Restore(type, value, traceback);
3660 return 0;
3661 }
3662 else {
3663 Py_XDECREF(type);
3664 Py_XDECREF(value);
3665 Py_XDECREF(traceback);
3666 return -1;
3667 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003668}
3669
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003670static int
Fred Drake5755ce62001-06-27 19:19:46 +00003671call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 register PyThreadState *tstate = frame->f_tstate;
3675 int result;
3676 if (tstate->tracing)
3677 return 0;
3678 tstate->tracing++;
3679 tstate->use_tracing = 0;
3680 result = func(obj, frame, what, arg);
3681 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3682 || (tstate->c_profilefunc != NULL));
3683 tstate->tracing--;
3684 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003685}
3686
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003687PyObject *
3688_PyEval_CallTracing(PyObject *func, PyObject *args)
3689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 PyFrameObject *frame = PyEval_GetFrame();
3691 PyThreadState *tstate = frame->f_tstate;
3692 int save_tracing = tstate->tracing;
3693 int save_use_tracing = tstate->use_tracing;
3694 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 tstate->tracing = 0;
3697 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3698 || (tstate->c_profilefunc != NULL));
3699 result = PyObject_Call(func, args, NULL);
3700 tstate->tracing = save_tracing;
3701 tstate->use_tracing = save_use_tracing;
3702 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003703}
3704
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003705/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003706static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003707maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3709 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 int result = 0;
3712 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 /* If the last instruction executed isn't in the current
3715 instruction window, reset the window.
3716 */
3717 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3718 PyAddrPair bounds;
3719 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3720 &bounds);
3721 *instr_lb = bounds.ap_lower;
3722 *instr_ub = bounds.ap_upper;
3723 }
3724 /* If the last instruction falls at the start of a line or if
3725 it represents a jump backwards, update the frame's line
3726 number and call the trace function. */
3727 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3728 frame->f_lineno = line;
3729 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3730 }
3731 *instr_prev = frame->f_lasti;
3732 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003733}
3734
Fred Drake5755ce62001-06-27 19:19:46 +00003735void
3736PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 PyThreadState *tstate = PyThreadState_GET();
3739 PyObject *temp = tstate->c_profileobj;
3740 Py_XINCREF(arg);
3741 tstate->c_profilefunc = NULL;
3742 tstate->c_profileobj = NULL;
3743 /* Must make sure that tracing is not ignored if 'temp' is freed */
3744 tstate->use_tracing = tstate->c_tracefunc != NULL;
3745 Py_XDECREF(temp);
3746 tstate->c_profilefunc = func;
3747 tstate->c_profileobj = arg;
3748 /* Flag that tracing or profiling is turned on */
3749 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003750}
3751
3752void
3753PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 PyThreadState *tstate = PyThreadState_GET();
3756 PyObject *temp = tstate->c_traceobj;
3757 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3758 Py_XINCREF(arg);
3759 tstate->c_tracefunc = NULL;
3760 tstate->c_traceobj = NULL;
3761 /* Must make sure that profiling is not ignored if 'temp' is freed */
3762 tstate->use_tracing = tstate->c_profilefunc != NULL;
3763 Py_XDECREF(temp);
3764 tstate->c_tracefunc = func;
3765 tstate->c_traceobj = arg;
3766 /* Flag that tracing or profiling is turned on */
3767 tstate->use_tracing = ((func != NULL)
3768 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003769}
3770
Guido van Rossumb209a111997-04-29 18:18:01 +00003771PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003772PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 PyFrameObject *current_frame = PyEval_GetFrame();
3775 if (current_frame == NULL)
3776 return PyThreadState_GET()->interp->builtins;
3777 else
3778 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003779}
3780
Guido van Rossumb209a111997-04-29 18:18:01 +00003781PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003782PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 PyFrameObject *current_frame = PyEval_GetFrame();
3785 if (current_frame == NULL)
3786 return NULL;
3787 PyFrame_FastToLocals(current_frame);
3788 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003789}
3790
Guido van Rossumb209a111997-04-29 18:18:01 +00003791PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003792PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 PyFrameObject *current_frame = PyEval_GetFrame();
3795 if (current_frame == NULL)
3796 return NULL;
3797 else
3798 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003799}
3800
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003801PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003802PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 PyThreadState *tstate = PyThreadState_GET();
3805 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003806}
3807
Guido van Rossum6135a871995-01-09 17:53:26 +00003808int
Tim Peters5ba58662001-07-16 02:29:45 +00003809PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 PyFrameObject *current_frame = PyEval_GetFrame();
3812 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 if (current_frame != NULL) {
3815 const int codeflags = current_frame->f_code->co_flags;
3816 const int compilerflags = codeflags & PyCF_MASK;
3817 if (compilerflags) {
3818 result = 1;
3819 cf->cf_flags |= compilerflags;
3820 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003821#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 if (codeflags & CO_GENERATOR_ALLOWED) {
3823 result = 1;
3824 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3825 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003826#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 }
3828 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003829}
3830
Guido van Rossum3f5da241990-12-20 15:06:42 +00003831
Guido van Rossum681d79a1995-07-18 14:51:37 +00003832/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003833 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003834
Guido van Rossumb209a111997-04-29 18:18:01 +00003835PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003836PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 if (arg == NULL) {
3841 arg = PyTuple_New(0);
3842 if (arg == NULL)
3843 return NULL;
3844 }
3845 else if (!PyTuple_Check(arg)) {
3846 PyErr_SetString(PyExc_TypeError,
3847 "argument list must be a tuple");
3848 return NULL;
3849 }
3850 else
3851 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 if (kw != NULL && !PyDict_Check(kw)) {
3854 PyErr_SetString(PyExc_TypeError,
3855 "keyword list must be a dictionary");
3856 Py_DECREF(arg);
3857 return NULL;
3858 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 result = PyObject_Call(func, arg, kw);
3861 Py_DECREF(arg);
3862 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003863}
3864
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003865const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003866PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 if (PyMethod_Check(func))
3869 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3870 else if (PyFunction_Check(func))
3871 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3872 else if (PyCFunction_Check(func))
3873 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3874 else
3875 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003876}
3877
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003878const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003879PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 if (PyMethod_Check(func))
3882 return "()";
3883 else if (PyFunction_Check(func))
3884 return "()";
3885 else if (PyCFunction_Check(func))
3886 return "()";
3887 else
3888 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003889}
3890
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003891static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003892err_args(PyObject *func, int flags, int nargs)
3893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003894 if (flags & METH_NOARGS)
3895 PyErr_Format(PyExc_TypeError,
3896 "%.200s() takes no arguments (%d given)",
3897 ((PyCFunctionObject *)func)->m_ml->ml_name,
3898 nargs);
3899 else
3900 PyErr_Format(PyExc_TypeError,
3901 "%.200s() takes exactly one argument (%d given)",
3902 ((PyCFunctionObject *)func)->m_ml->ml_name,
3903 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003904}
3905
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003906#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003907if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 if (call_trace(tstate->c_profilefunc, \
3909 tstate->c_profileobj, \
3910 tstate->frame, PyTrace_C_CALL, \
3911 func)) { \
3912 x = NULL; \
3913 } \
3914 else { \
3915 x = call; \
3916 if (tstate->c_profilefunc != NULL) { \
3917 if (x == NULL) { \
3918 call_trace_protected(tstate->c_profilefunc, \
3919 tstate->c_profileobj, \
3920 tstate->frame, PyTrace_C_EXCEPTION, \
3921 func); \
3922 /* XXX should pass (type, value, tb) */ \
3923 } else { \
3924 if (call_trace(tstate->c_profilefunc, \
3925 tstate->c_profileobj, \
3926 tstate->frame, PyTrace_C_RETURN, \
3927 func)) { \
3928 Py_DECREF(x); \
3929 x = NULL; \
3930 } \
3931 } \
3932 } \
3933 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003934} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 x = call; \
3936 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003937
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003938static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003939call_function(PyObject ***pp_stack, int oparg
3940#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003942#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 int na = oparg & 0xff;
3946 int nk = (oparg>>8) & 0xff;
3947 int n = na + 2 * nk;
3948 PyObject **pfunc = (*pp_stack) - n - 1;
3949 PyObject *func = *pfunc;
3950 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 /* Always dispatch PyCFunction first, because these are
3953 presumed to be the most frequent callable object.
3954 */
3955 if (PyCFunction_Check(func) && nk == 0) {
3956 int flags = PyCFunction_GET_FLAGS(func);
3957 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 PCALL(PCALL_CFUNCTION);
3960 if (flags & (METH_NOARGS | METH_O)) {
3961 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3962 PyObject *self = PyCFunction_GET_SELF(func);
3963 if (flags & METH_NOARGS && na == 0) {
3964 C_TRACE(x, (*meth)(self,NULL));
3965 }
3966 else if (flags & METH_O && na == 1) {
3967 PyObject *arg = EXT_POP(*pp_stack);
3968 C_TRACE(x, (*meth)(self,arg));
3969 Py_DECREF(arg);
3970 }
3971 else {
3972 err_args(func, flags, na);
3973 x = NULL;
3974 }
3975 }
3976 else {
3977 PyObject *callargs;
3978 callargs = load_args(pp_stack, na);
3979 READ_TIMESTAMP(*pintr0);
3980 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3981 READ_TIMESTAMP(*pintr1);
3982 Py_XDECREF(callargs);
3983 }
3984 } else {
3985 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3986 /* optimize access to bound methods */
3987 PyObject *self = PyMethod_GET_SELF(func);
3988 PCALL(PCALL_METHOD);
3989 PCALL(PCALL_BOUND_METHOD);
3990 Py_INCREF(self);
3991 func = PyMethod_GET_FUNCTION(func);
3992 Py_INCREF(func);
3993 Py_DECREF(*pfunc);
3994 *pfunc = self;
3995 na++;
3996 n++;
3997 } else
3998 Py_INCREF(func);
3999 READ_TIMESTAMP(*pintr0);
4000 if (PyFunction_Check(func))
4001 x = fast_function(func, pp_stack, n, na, nk);
4002 else
4003 x = do_call(func, pp_stack, na, nk);
4004 READ_TIMESTAMP(*pintr1);
4005 Py_DECREF(func);
4006 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 /* Clear the stack of the function object. Also removes
4009 the arguments in case they weren't consumed already
4010 (fast_function() and err_args() leave them on the stack).
4011 */
4012 while ((*pp_stack) > pfunc) {
4013 w = EXT_POP(*pp_stack);
4014 Py_DECREF(w);
4015 PCALL(PCALL_POP);
4016 }
4017 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004018}
4019
Jeremy Hylton192690e2002-08-16 18:36:11 +00004020/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004021 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004022 For the simplest case -- a function that takes only positional
4023 arguments and is called with only positional arguments -- it
4024 inlines the most primitive frame setup code from
4025 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4026 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004027*/
4028
4029static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004030fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4033 PyObject *globals = PyFunction_GET_GLOBALS(func);
4034 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4035 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4036 PyObject **d = NULL;
4037 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 PCALL(PCALL_FUNCTION);
4040 PCALL(PCALL_FAST_FUNCTION);
4041 if (argdefs == NULL && co->co_argcount == n &&
4042 co->co_kwonlyargcount == 0 && nk==0 &&
4043 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4044 PyFrameObject *f;
4045 PyObject *retval = NULL;
4046 PyThreadState *tstate = PyThreadState_GET();
4047 PyObject **fastlocals, **stack;
4048 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 PCALL(PCALL_FASTER_FUNCTION);
4051 assert(globals != NULL);
4052 /* XXX Perhaps we should create a specialized
4053 PyFrame_New() that doesn't take locals, but does
4054 take builtins without sanity checking them.
4055 */
4056 assert(tstate != NULL);
4057 f = PyFrame_New(tstate, co, globals, NULL);
4058 if (f == NULL)
4059 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 fastlocals = f->f_localsplus;
4062 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 for (i = 0; i < n; i++) {
4065 Py_INCREF(*stack);
4066 fastlocals[i] = *stack++;
4067 }
4068 retval = PyEval_EvalFrameEx(f,0);
4069 ++tstate->recursion_depth;
4070 Py_DECREF(f);
4071 --tstate->recursion_depth;
4072 return retval;
4073 }
4074 if (argdefs != NULL) {
4075 d = &PyTuple_GET_ITEM(argdefs, 0);
4076 nd = Py_SIZE(argdefs);
4077 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004078 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 (PyObject *)NULL, (*pp_stack)-n, na,
4080 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4081 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004082}
4083
4084static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004085update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4086 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 PyObject *kwdict = NULL;
4089 if (orig_kwdict == NULL)
4090 kwdict = PyDict_New();
4091 else {
4092 kwdict = PyDict_Copy(orig_kwdict);
4093 Py_DECREF(orig_kwdict);
4094 }
4095 if (kwdict == NULL)
4096 return NULL;
4097 while (--nk >= 0) {
4098 int err;
4099 PyObject *value = EXT_POP(*pp_stack);
4100 PyObject *key = EXT_POP(*pp_stack);
4101 if (PyDict_GetItem(kwdict, key) != NULL) {
4102 PyErr_Format(PyExc_TypeError,
4103 "%.200s%s got multiple values "
4104 "for keyword argument '%U'",
4105 PyEval_GetFuncName(func),
4106 PyEval_GetFuncDesc(func),
4107 key);
4108 Py_DECREF(key);
4109 Py_DECREF(value);
4110 Py_DECREF(kwdict);
4111 return NULL;
4112 }
4113 err = PyDict_SetItem(kwdict, key, value);
4114 Py_DECREF(key);
4115 Py_DECREF(value);
4116 if (err) {
4117 Py_DECREF(kwdict);
4118 return NULL;
4119 }
4120 }
4121 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004122}
4123
4124static PyObject *
4125update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 callargs = PyTuple_New(nstack + nstar);
4131 if (callargs == NULL) {
4132 return NULL;
4133 }
4134 if (nstar) {
4135 int i;
4136 for (i = 0; i < nstar; i++) {
4137 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4138 Py_INCREF(a);
4139 PyTuple_SET_ITEM(callargs, nstack + i, a);
4140 }
4141 }
4142 while (--nstack >= 0) {
4143 w = EXT_POP(*pp_stack);
4144 PyTuple_SET_ITEM(callargs, nstack, w);
4145 }
4146 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004147}
4148
4149static PyObject *
4150load_args(PyObject ***pp_stack, int na)
4151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 PyObject *args = PyTuple_New(na);
4153 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 if (args == NULL)
4156 return NULL;
4157 while (--na >= 0) {
4158 w = EXT_POP(*pp_stack);
4159 PyTuple_SET_ITEM(args, na, w);
4160 }
4161 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004162}
4163
4164static PyObject *
4165do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 PyObject *callargs = NULL;
4168 PyObject *kwdict = NULL;
4169 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 if (nk > 0) {
4172 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4173 if (kwdict == NULL)
4174 goto call_fail;
4175 }
4176 callargs = load_args(pp_stack, na);
4177 if (callargs == NULL)
4178 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004179#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 /* At this point, we have to look at the type of func to
4181 update the call stats properly. Do it here so as to avoid
4182 exposing the call stats machinery outside ceval.c
4183 */
4184 if (PyFunction_Check(func))
4185 PCALL(PCALL_FUNCTION);
4186 else if (PyMethod_Check(func))
4187 PCALL(PCALL_METHOD);
4188 else if (PyType_Check(func))
4189 PCALL(PCALL_TYPE);
4190 else if (PyCFunction_Check(func))
4191 PCALL(PCALL_CFUNCTION);
4192 else
4193 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004194#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 if (PyCFunction_Check(func)) {
4196 PyThreadState *tstate = PyThreadState_GET();
4197 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4198 }
4199 else
4200 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004201call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 Py_XDECREF(callargs);
4203 Py_XDECREF(kwdict);
4204 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004205}
4206
4207static PyObject *
4208ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 int nstar = 0;
4211 PyObject *callargs = NULL;
4212 PyObject *stararg = NULL;
4213 PyObject *kwdict = NULL;
4214 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 if (flags & CALL_FLAG_KW) {
4217 kwdict = EXT_POP(*pp_stack);
4218 if (!PyDict_Check(kwdict)) {
4219 PyObject *d;
4220 d = PyDict_New();
4221 if (d == NULL)
4222 goto ext_call_fail;
4223 if (PyDict_Update(d, kwdict) != 0) {
4224 Py_DECREF(d);
4225 /* PyDict_Update raises attribute
4226 * error (percolated from an attempt
4227 * to get 'keys' attribute) instead of
4228 * a type error if its second argument
4229 * is not a mapping.
4230 */
4231 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4232 PyErr_Format(PyExc_TypeError,
4233 "%.200s%.200s argument after ** "
4234 "must be a mapping, not %.200s",
4235 PyEval_GetFuncName(func),
4236 PyEval_GetFuncDesc(func),
4237 kwdict->ob_type->tp_name);
4238 }
4239 goto ext_call_fail;
4240 }
4241 Py_DECREF(kwdict);
4242 kwdict = d;
4243 }
4244 }
4245 if (flags & CALL_FLAG_VAR) {
4246 stararg = EXT_POP(*pp_stack);
4247 if (!PyTuple_Check(stararg)) {
4248 PyObject *t = NULL;
4249 t = PySequence_Tuple(stararg);
4250 if (t == NULL) {
4251 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4252 PyErr_Format(PyExc_TypeError,
4253 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004254 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 PyEval_GetFuncName(func),
4256 PyEval_GetFuncDesc(func),
4257 stararg->ob_type->tp_name);
4258 }
4259 goto ext_call_fail;
4260 }
4261 Py_DECREF(stararg);
4262 stararg = t;
4263 }
4264 nstar = PyTuple_GET_SIZE(stararg);
4265 }
4266 if (nk > 0) {
4267 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4268 if (kwdict == NULL)
4269 goto ext_call_fail;
4270 }
4271 callargs = update_star_args(na, nstar, stararg, pp_stack);
4272 if (callargs == NULL)
4273 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004274#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 /* At this point, we have to look at the type of func to
4276 update the call stats properly. Do it here so as to avoid
4277 exposing the call stats machinery outside ceval.c
4278 */
4279 if (PyFunction_Check(func))
4280 PCALL(PCALL_FUNCTION);
4281 else if (PyMethod_Check(func))
4282 PCALL(PCALL_METHOD);
4283 else if (PyType_Check(func))
4284 PCALL(PCALL_TYPE);
4285 else if (PyCFunction_Check(func))
4286 PCALL(PCALL_CFUNCTION);
4287 else
4288 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004289#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 if (PyCFunction_Check(func)) {
4291 PyThreadState *tstate = PyThreadState_GET();
4292 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4293 }
4294 else
4295 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004296ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 Py_XDECREF(callargs);
4298 Py_XDECREF(kwdict);
4299 Py_XDECREF(stararg);
4300 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004301}
4302
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004303/* Extract a slice index from a PyInt or PyLong or an object with the
4304 nb_index slot defined, and store in *pi.
4305 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4306 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 +00004307 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004308*/
Tim Petersb5196382001-12-16 19:44:20 +00004309/* Note: If v is NULL, return success without storing into *pi. This
4310 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4311 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004312*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004313int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004314_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 if (v != NULL) {
4317 Py_ssize_t x;
4318 if (PyIndex_Check(v)) {
4319 x = PyNumber_AsSsize_t(v, NULL);
4320 if (x == -1 && PyErr_Occurred())
4321 return 0;
4322 }
4323 else {
4324 PyErr_SetString(PyExc_TypeError,
4325 "slice indices must be integers or "
4326 "None or have an __index__ method");
4327 return 0;
4328 }
4329 *pi = x;
4330 }
4331 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004332}
4333
Guido van Rossum486364b2007-06-30 05:01:58 +00004334#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004336
Guido van Rossumb209a111997-04-29 18:18:01 +00004337static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004338cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 int res = 0;
4341 switch (op) {
4342 case PyCmp_IS:
4343 res = (v == w);
4344 break;
4345 case PyCmp_IS_NOT:
4346 res = (v != w);
4347 break;
4348 case PyCmp_IN:
4349 res = PySequence_Contains(w, v);
4350 if (res < 0)
4351 return NULL;
4352 break;
4353 case PyCmp_NOT_IN:
4354 res = PySequence_Contains(w, v);
4355 if (res < 0)
4356 return NULL;
4357 res = !res;
4358 break;
4359 case PyCmp_EXC_MATCH:
4360 if (PyTuple_Check(w)) {
4361 Py_ssize_t i, length;
4362 length = PyTuple_Size(w);
4363 for (i = 0; i < length; i += 1) {
4364 PyObject *exc = PyTuple_GET_ITEM(w, i);
4365 if (!PyExceptionClass_Check(exc)) {
4366 PyErr_SetString(PyExc_TypeError,
4367 CANNOT_CATCH_MSG);
4368 return NULL;
4369 }
4370 }
4371 }
4372 else {
4373 if (!PyExceptionClass_Check(w)) {
4374 PyErr_SetString(PyExc_TypeError,
4375 CANNOT_CATCH_MSG);
4376 return NULL;
4377 }
4378 }
4379 res = PyErr_GivenExceptionMatches(v, w);
4380 break;
4381 default:
4382 return PyObject_RichCompare(v, w, op);
4383 }
4384 v = res ? Py_True : Py_False;
4385 Py_INCREF(v);
4386 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004387}
4388
Thomas Wouters52152252000-08-17 22:55:00 +00004389static PyObject *
4390import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 x = PyObject_GetAttr(v, name);
4395 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4396 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4397 }
4398 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004399}
Guido van Rossumac7be682001-01-17 15:42:30 +00004400
Thomas Wouters52152252000-08-17 22:55:00 +00004401static int
4402import_all_from(PyObject *locals, PyObject *v)
4403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 PyObject *all = PyObject_GetAttrString(v, "__all__");
4405 PyObject *dict, *name, *value;
4406 int skip_leading_underscores = 0;
4407 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 if (all == NULL) {
4410 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4411 return -1; /* Unexpected error */
4412 PyErr_Clear();
4413 dict = PyObject_GetAttrString(v, "__dict__");
4414 if (dict == NULL) {
4415 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4416 return -1;
4417 PyErr_SetString(PyExc_ImportError,
4418 "from-import-* object has no __dict__ and no __all__");
4419 return -1;
4420 }
4421 all = PyMapping_Keys(dict);
4422 Py_DECREF(dict);
4423 if (all == NULL)
4424 return -1;
4425 skip_leading_underscores = 1;
4426 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 for (pos = 0, err = 0; ; pos++) {
4429 name = PySequence_GetItem(all, pos);
4430 if (name == NULL) {
4431 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4432 err = -1;
4433 else
4434 PyErr_Clear();
4435 break;
4436 }
4437 if (skip_leading_underscores &&
4438 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004439 PyUnicode_READY(name) != -1 &&
4440 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 {
4442 Py_DECREF(name);
4443 continue;
4444 }
4445 value = PyObject_GetAttr(v, name);
4446 if (value == NULL)
4447 err = -1;
4448 else if (PyDict_CheckExact(locals))
4449 err = PyDict_SetItem(locals, name, value);
4450 else
4451 err = PyObject_SetItem(locals, name, value);
4452 Py_DECREF(name);
4453 Py_XDECREF(value);
4454 if (err != 0)
4455 break;
4456 }
4457 Py_DECREF(all);
4458 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004459}
4460
Guido van Rossumac7be682001-01-17 15:42:30 +00004461static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004462format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 if (!obj)
4467 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 obj_str = _PyUnicode_AsString(obj);
4470 if (!obj_str)
4471 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004474}
Guido van Rossum950361c1997-01-24 13:49:28 +00004475
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004476static void
4477format_exc_unbound(PyCodeObject *co, int oparg)
4478{
4479 PyObject *name;
4480 /* Don't stomp existing exception */
4481 if (PyErr_Occurred())
4482 return;
4483 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4484 name = PyTuple_GET_ITEM(co->co_cellvars,
4485 oparg);
4486 format_exc_check_arg(
4487 PyExc_UnboundLocalError,
4488 UNBOUNDLOCAL_ERROR_MSG,
4489 name);
4490 } else {
4491 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4492 PyTuple_GET_SIZE(co->co_cellvars));
4493 format_exc_check_arg(PyExc_NameError,
4494 UNBOUNDFREE_ERROR_MSG, name);
4495 }
4496}
4497
Guido van Rossum950361c1997-01-24 13:49:28 +00004498#ifdef DYNAMIC_EXECUTION_PROFILE
4499
Skip Montanarof118cb12001-10-15 20:51:38 +00004500static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004501getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 int i;
4504 PyObject *l = PyList_New(256);
4505 if (l == NULL) return NULL;
4506 for (i = 0; i < 256; i++) {
4507 PyObject *x = PyLong_FromLong(a[i]);
4508 if (x == NULL) {
4509 Py_DECREF(l);
4510 return NULL;
4511 }
4512 PyList_SetItem(l, i, x);
4513 }
4514 for (i = 0; i < 256; i++)
4515 a[i] = 0;
4516 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004517}
4518
4519PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004520_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004521{
4522#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004524#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 int i;
4526 PyObject *l = PyList_New(257);
4527 if (l == NULL) return NULL;
4528 for (i = 0; i < 257; i++) {
4529 PyObject *x = getarray(dxpairs[i]);
4530 if (x == NULL) {
4531 Py_DECREF(l);
4532 return NULL;
4533 }
4534 PyList_SetItem(l, i, x);
4535 }
4536 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004537#endif
4538}
4539
4540#endif