blob: f6f422ee32f88de2fa0510097d90b708df9e5b15 [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();
Victor Stinnerbec0fda2011-10-01 01:26:08 +02001510 if (PyUnicode_Check(v) && PyUnicode_Check(w))
1511 x = PyUnicode_Concat(v, w);
1512 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 x = PyNumber_Add(v, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 Py_DECREF(w);
1516 SET_TOP(x);
1517 if (x != NULL) DISPATCH();
1518 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 TARGET(BINARY_SUBTRACT)
1521 w = POP();
1522 v = TOP();
1523 x = PyNumber_Subtract(v, w);
1524 Py_DECREF(v);
1525 Py_DECREF(w);
1526 SET_TOP(x);
1527 if (x != NULL) DISPATCH();
1528 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 TARGET(BINARY_SUBSCR)
1531 w = POP();
1532 v = TOP();
1533 x = PyObject_GetItem(v, w);
1534 Py_DECREF(v);
1535 Py_DECREF(w);
1536 SET_TOP(x);
1537 if (x != NULL) DISPATCH();
1538 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 TARGET(BINARY_LSHIFT)
1541 w = POP();
1542 v = TOP();
1543 x = PyNumber_Lshift(v, w);
1544 Py_DECREF(v);
1545 Py_DECREF(w);
1546 SET_TOP(x);
1547 if (x != NULL) DISPATCH();
1548 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 TARGET(BINARY_RSHIFT)
1551 w = POP();
1552 v = TOP();
1553 x = PyNumber_Rshift(v, w);
1554 Py_DECREF(v);
1555 Py_DECREF(w);
1556 SET_TOP(x);
1557 if (x != NULL) DISPATCH();
1558 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 TARGET(BINARY_AND)
1561 w = POP();
1562 v = TOP();
1563 x = PyNumber_And(v, w);
1564 Py_DECREF(v);
1565 Py_DECREF(w);
1566 SET_TOP(x);
1567 if (x != NULL) DISPATCH();
1568 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 TARGET(BINARY_XOR)
1571 w = POP();
1572 v = TOP();
1573 x = PyNumber_Xor(v, w);
1574 Py_DECREF(v);
1575 Py_DECREF(w);
1576 SET_TOP(x);
1577 if (x != NULL) DISPATCH();
1578 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 TARGET(BINARY_OR)
1581 w = POP();
1582 v = TOP();
1583 x = PyNumber_Or(v, w);
1584 Py_DECREF(v);
1585 Py_DECREF(w);
1586 SET_TOP(x);
1587 if (x != NULL) DISPATCH();
1588 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 TARGET(LIST_APPEND)
1591 w = POP();
1592 v = PEEK(oparg);
1593 err = PyList_Append(v, w);
1594 Py_DECREF(w);
1595 if (err == 0) {
1596 PREDICT(JUMP_ABSOLUTE);
1597 DISPATCH();
1598 }
1599 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 TARGET(SET_ADD)
1602 w = POP();
1603 v = stack_pointer[-oparg];
1604 err = PySet_Add(v, w);
1605 Py_DECREF(w);
1606 if (err == 0) {
1607 PREDICT(JUMP_ABSOLUTE);
1608 DISPATCH();
1609 }
1610 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 TARGET(INPLACE_POWER)
1613 w = POP();
1614 v = TOP();
1615 x = PyNumber_InPlacePower(v, w, Py_None);
1616 Py_DECREF(v);
1617 Py_DECREF(w);
1618 SET_TOP(x);
1619 if (x != NULL) DISPATCH();
1620 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 TARGET(INPLACE_MULTIPLY)
1623 w = POP();
1624 v = TOP();
1625 x = PyNumber_InPlaceMultiply(v, w);
1626 Py_DECREF(v);
1627 Py_DECREF(w);
1628 SET_TOP(x);
1629 if (x != NULL) DISPATCH();
1630 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 TARGET(INPLACE_TRUE_DIVIDE)
1633 w = POP();
1634 v = TOP();
1635 x = PyNumber_InPlaceTrueDivide(v, w);
1636 Py_DECREF(v);
1637 Py_DECREF(w);
1638 SET_TOP(x);
1639 if (x != NULL) DISPATCH();
1640 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 TARGET(INPLACE_FLOOR_DIVIDE)
1643 w = POP();
1644 v = TOP();
1645 x = PyNumber_InPlaceFloorDivide(v, w);
1646 Py_DECREF(v);
1647 Py_DECREF(w);
1648 SET_TOP(x);
1649 if (x != NULL) DISPATCH();
1650 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 TARGET(INPLACE_MODULO)
1653 w = POP();
1654 v = TOP();
1655 x = PyNumber_InPlaceRemainder(v, w);
1656 Py_DECREF(v);
1657 Py_DECREF(w);
1658 SET_TOP(x);
1659 if (x != NULL) DISPATCH();
1660 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 TARGET(INPLACE_ADD)
1663 w = POP();
1664 v = TOP();
Victor Stinnerbec0fda2011-10-01 01:26:08 +02001665 if (PyUnicode_Check(v) && PyUnicode_Check(w))
1666 x = PyUnicode_Concat(v, w);
1667 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 x = PyNumber_InPlaceAdd(v, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 Py_DECREF(w);
1671 SET_TOP(x);
1672 if (x != NULL) DISPATCH();
1673 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 TARGET(INPLACE_SUBTRACT)
1676 w = POP();
1677 v = TOP();
1678 x = PyNumber_InPlaceSubtract(v, w);
1679 Py_DECREF(v);
1680 Py_DECREF(w);
1681 SET_TOP(x);
1682 if (x != NULL) DISPATCH();
1683 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 TARGET(INPLACE_LSHIFT)
1686 w = POP();
1687 v = TOP();
1688 x = PyNumber_InPlaceLshift(v, w);
1689 Py_DECREF(v);
1690 Py_DECREF(w);
1691 SET_TOP(x);
1692 if (x != NULL) DISPATCH();
1693 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 TARGET(INPLACE_RSHIFT)
1696 w = POP();
1697 v = TOP();
1698 x = PyNumber_InPlaceRshift(v, w);
1699 Py_DECREF(v);
1700 Py_DECREF(w);
1701 SET_TOP(x);
1702 if (x != NULL) DISPATCH();
1703 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 TARGET(INPLACE_AND)
1706 w = POP();
1707 v = TOP();
1708 x = PyNumber_InPlaceAnd(v, w);
1709 Py_DECREF(v);
1710 Py_DECREF(w);
1711 SET_TOP(x);
1712 if (x != NULL) DISPATCH();
1713 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 TARGET(INPLACE_XOR)
1716 w = POP();
1717 v = TOP();
1718 x = PyNumber_InPlaceXor(v, w);
1719 Py_DECREF(v);
1720 Py_DECREF(w);
1721 SET_TOP(x);
1722 if (x != NULL) DISPATCH();
1723 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 TARGET(INPLACE_OR)
1726 w = POP();
1727 v = TOP();
1728 x = PyNumber_InPlaceOr(v, w);
1729 Py_DECREF(v);
1730 Py_DECREF(w);
1731 SET_TOP(x);
1732 if (x != NULL) DISPATCH();
1733 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 TARGET(STORE_SUBSCR)
1736 w = TOP();
1737 v = SECOND();
1738 u = THIRD();
1739 STACKADJ(-3);
1740 /* v[w] = u */
1741 err = PyObject_SetItem(v, w, u);
1742 Py_DECREF(u);
1743 Py_DECREF(v);
1744 Py_DECREF(w);
1745 if (err == 0) DISPATCH();
1746 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 TARGET(DELETE_SUBSCR)
1749 w = TOP();
1750 v = SECOND();
1751 STACKADJ(-2);
1752 /* del v[w] */
1753 err = PyObject_DelItem(v, w);
1754 Py_DECREF(v);
1755 Py_DECREF(w);
1756 if (err == 0) DISPATCH();
1757 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 TARGET(PRINT_EXPR)
1760 v = POP();
1761 w = PySys_GetObject("displayhook");
1762 if (w == NULL) {
1763 PyErr_SetString(PyExc_RuntimeError,
1764 "lost sys.displayhook");
1765 err = -1;
1766 x = NULL;
1767 }
1768 if (err == 0) {
1769 x = PyTuple_Pack(1, v);
1770 if (x == NULL)
1771 err = -1;
1772 }
1773 if (err == 0) {
1774 w = PyEval_CallObject(w, x);
1775 Py_XDECREF(w);
1776 if (w == NULL)
1777 err = -1;
1778 }
1779 Py_DECREF(v);
1780 Py_XDECREF(x);
1781 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001782
Thomas Wouters434d0822000-08-24 20:11:32 +00001783#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001785#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 TARGET(RAISE_VARARGS)
1787 v = w = NULL;
1788 switch (oparg) {
1789 case 2:
1790 v = POP(); /* cause */
1791 case 1:
1792 w = POP(); /* exc */
1793 case 0: /* Fallthrough */
1794 why = do_raise(w, v);
1795 break;
1796 default:
1797 PyErr_SetString(PyExc_SystemError,
1798 "bad RAISE_VARARGS oparg");
1799 why = WHY_EXCEPTION;
1800 break;
1801 }
1802 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 TARGET(STORE_LOCALS)
1805 x = POP();
1806 v = f->f_locals;
1807 Py_XDECREF(v);
1808 f->f_locals = x;
1809 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 TARGET(RETURN_VALUE)
1812 retval = POP();
1813 why = WHY_RETURN;
1814 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 TARGET(YIELD_VALUE)
1817 retval = POP();
1818 f->f_stacktop = stack_pointer;
1819 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 TARGET(POP_EXCEPT)
1823 {
1824 PyTryBlock *b = PyFrame_BlockPop(f);
1825 if (b->b_type != EXCEPT_HANDLER) {
1826 PyErr_SetString(PyExc_SystemError,
1827 "popped block is not an except handler");
1828 why = WHY_EXCEPTION;
1829 break;
1830 }
1831 UNWIND_EXCEPT_HANDLER(b);
1832 }
1833 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 TARGET(POP_BLOCK)
1836 {
1837 PyTryBlock *b = PyFrame_BlockPop(f);
1838 UNWIND_BLOCK(b);
1839 }
1840 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 PREDICTED(END_FINALLY);
1843 TARGET(END_FINALLY)
1844 v = POP();
1845 if (PyLong_Check(v)) {
1846 why = (enum why_code) PyLong_AS_LONG(v);
1847 assert(why != WHY_YIELD);
1848 if (why == WHY_RETURN ||
1849 why == WHY_CONTINUE)
1850 retval = POP();
1851 if (why == WHY_SILENCED) {
1852 /* An exception was silenced by 'with', we must
1853 manually unwind the EXCEPT_HANDLER block which was
1854 created when the exception was caught, otherwise
1855 the stack will be in an inconsistent state. */
1856 PyTryBlock *b = PyFrame_BlockPop(f);
1857 assert(b->b_type == EXCEPT_HANDLER);
1858 UNWIND_EXCEPT_HANDLER(b);
1859 why = WHY_NOT;
1860 }
1861 }
1862 else if (PyExceptionClass_Check(v)) {
1863 w = POP();
1864 u = POP();
1865 PyErr_Restore(v, w, u);
1866 why = WHY_RERAISE;
1867 break;
1868 }
1869 else if (v != Py_None) {
1870 PyErr_SetString(PyExc_SystemError,
1871 "'finally' pops bad exception");
1872 why = WHY_EXCEPTION;
1873 }
1874 Py_DECREF(v);
1875 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 TARGET(LOAD_BUILD_CLASS)
1878 x = PyDict_GetItemString(f->f_builtins,
1879 "__build_class__");
1880 if (x == NULL) {
1881 PyErr_SetString(PyExc_ImportError,
1882 "__build_class__ not found");
1883 break;
1884 }
1885 Py_INCREF(x);
1886 PUSH(x);
1887 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 TARGET(STORE_NAME)
1890 w = GETITEM(names, oparg);
1891 v = POP();
1892 if ((x = f->f_locals) != NULL) {
1893 if (PyDict_CheckExact(x))
1894 err = PyDict_SetItem(x, w, v);
1895 else
1896 err = PyObject_SetItem(x, w, v);
1897 Py_DECREF(v);
1898 if (err == 0) DISPATCH();
1899 break;
1900 }
1901 PyErr_Format(PyExc_SystemError,
1902 "no locals found when storing %R", w);
1903 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 TARGET(DELETE_NAME)
1906 w = GETITEM(names, oparg);
1907 if ((x = f->f_locals) != NULL) {
1908 if ((err = PyObject_DelItem(x, w)) != 0)
1909 format_exc_check_arg(PyExc_NameError,
1910 NAME_ERROR_MSG,
1911 w);
1912 break;
1913 }
1914 PyErr_Format(PyExc_SystemError,
1915 "no locals when deleting %R", w);
1916 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1919 TARGET(UNPACK_SEQUENCE)
1920 v = POP();
1921 if (PyTuple_CheckExact(v) &&
1922 PyTuple_GET_SIZE(v) == oparg) {
1923 PyObject **items = \
1924 ((PyTupleObject *)v)->ob_item;
1925 while (oparg--) {
1926 w = items[oparg];
1927 Py_INCREF(w);
1928 PUSH(w);
1929 }
1930 Py_DECREF(v);
1931 DISPATCH();
1932 } else if (PyList_CheckExact(v) &&
1933 PyList_GET_SIZE(v) == oparg) {
1934 PyObject **items = \
1935 ((PyListObject *)v)->ob_item;
1936 while (oparg--) {
1937 w = items[oparg];
1938 Py_INCREF(w);
1939 PUSH(w);
1940 }
1941 } else if (unpack_iterable(v, oparg, -1,
1942 stack_pointer + oparg)) {
1943 STACKADJ(oparg);
1944 } else {
1945 /* unpack_iterable() raised an exception */
1946 why = WHY_EXCEPTION;
1947 }
1948 Py_DECREF(v);
1949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 TARGET(UNPACK_EX)
1952 {
1953 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1954 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1957 stack_pointer + totalargs)) {
1958 stack_pointer += totalargs;
1959 } else {
1960 why = WHY_EXCEPTION;
1961 }
1962 Py_DECREF(v);
1963 break;
1964 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 TARGET(STORE_ATTR)
1967 w = GETITEM(names, oparg);
1968 v = TOP();
1969 u = SECOND();
1970 STACKADJ(-2);
1971 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1972 Py_DECREF(v);
1973 Py_DECREF(u);
1974 if (err == 0) DISPATCH();
1975 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 TARGET(DELETE_ATTR)
1978 w = GETITEM(names, oparg);
1979 v = POP();
1980 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1981 /* del v.w */
1982 Py_DECREF(v);
1983 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 TARGET(STORE_GLOBAL)
1986 w = GETITEM(names, oparg);
1987 v = POP();
1988 err = PyDict_SetItem(f->f_globals, w, v);
1989 Py_DECREF(v);
1990 if (err == 0) DISPATCH();
1991 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 TARGET(DELETE_GLOBAL)
1994 w = GETITEM(names, oparg);
1995 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1996 format_exc_check_arg(
1997 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1998 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 TARGET(LOAD_NAME)
2001 w = GETITEM(names, oparg);
2002 if ((v = f->f_locals) == NULL) {
2003 PyErr_Format(PyExc_SystemError,
2004 "no locals when loading %R", w);
2005 why = WHY_EXCEPTION;
2006 break;
2007 }
2008 if (PyDict_CheckExact(v)) {
2009 x = PyDict_GetItem(v, w);
2010 Py_XINCREF(x);
2011 }
2012 else {
2013 x = PyObject_GetItem(v, w);
2014 if (x == NULL && PyErr_Occurred()) {
2015 if (!PyErr_ExceptionMatches(
2016 PyExc_KeyError))
2017 break;
2018 PyErr_Clear();
2019 }
2020 }
2021 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002022 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002024 x = PyDict_GetItem(f->f_builtins, w);
2025 if (x == NULL) {
2026 format_exc_check_arg(
2027 PyExc_NameError,
2028 NAME_ERROR_MSG, w);
2029 break;
2030 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 }
2032 Py_INCREF(x);
2033 }
2034 PUSH(x);
2035 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 TARGET(LOAD_GLOBAL)
2038 w = GETITEM(names, oparg);
2039 if (PyUnicode_CheckExact(w)) {
2040 /* Inline the PyDict_GetItem() calls.
2041 WARNING: this is an extreme speed hack.
2042 Do not try this at home. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002043 Py_hash_t hash = ((PyASCIIObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 if (hash != -1) {
2045 PyDictObject *d;
2046 PyDictEntry *e;
2047 d = (PyDictObject *)(f->f_globals);
2048 e = d->ma_lookup(d, w, hash);
2049 if (e == NULL) {
2050 x = NULL;
2051 break;
2052 }
2053 x = e->me_value;
2054 if (x != NULL) {
2055 Py_INCREF(x);
2056 PUSH(x);
2057 DISPATCH();
2058 }
2059 d = (PyDictObject *)(f->f_builtins);
2060 e = d->ma_lookup(d, w, hash);
2061 if (e == NULL) {
2062 x = NULL;
2063 break;
2064 }
2065 x = e->me_value;
2066 if (x != NULL) {
2067 Py_INCREF(x);
2068 PUSH(x);
2069 DISPATCH();
2070 }
2071 goto load_global_error;
2072 }
2073 }
2074 /* This is the un-inlined version of the code above */
2075 x = PyDict_GetItem(f->f_globals, w);
2076 if (x == NULL) {
2077 x = PyDict_GetItem(f->f_builtins, w);
2078 if (x == NULL) {
2079 load_global_error:
2080 format_exc_check_arg(
2081 PyExc_NameError,
2082 GLOBAL_NAME_ERROR_MSG, w);
2083 break;
2084 }
2085 }
2086 Py_INCREF(x);
2087 PUSH(x);
2088 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 TARGET(DELETE_FAST)
2091 x = GETLOCAL(oparg);
2092 if (x != NULL) {
2093 SETLOCAL(oparg, NULL);
2094 DISPATCH();
2095 }
2096 format_exc_check_arg(
2097 PyExc_UnboundLocalError,
2098 UNBOUNDLOCAL_ERROR_MSG,
2099 PyTuple_GetItem(co->co_varnames, oparg)
2100 );
2101 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002102
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002103 TARGET(DELETE_DEREF)
2104 x = freevars[oparg];
2105 if (PyCell_GET(x) != NULL) {
2106 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002107 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002108 }
2109 err = -1;
2110 format_exc_unbound(co, oparg);
2111 break;
2112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 TARGET(LOAD_CLOSURE)
2114 x = freevars[oparg];
2115 Py_INCREF(x);
2116 PUSH(x);
2117 if (x != NULL) DISPATCH();
2118 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 TARGET(LOAD_DEREF)
2121 x = freevars[oparg];
2122 w = PyCell_Get(x);
2123 if (w != NULL) {
2124 PUSH(w);
2125 DISPATCH();
2126 }
2127 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002128 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 TARGET(STORE_DEREF)
2132 w = POP();
2133 x = freevars[oparg];
2134 PyCell_Set(x, w);
2135 Py_DECREF(w);
2136 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 TARGET(BUILD_TUPLE)
2139 x = PyTuple_New(oparg);
2140 if (x != NULL) {
2141 for (; --oparg >= 0;) {
2142 w = POP();
2143 PyTuple_SET_ITEM(x, oparg, w);
2144 }
2145 PUSH(x);
2146 DISPATCH();
2147 }
2148 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 TARGET(BUILD_LIST)
2151 x = PyList_New(oparg);
2152 if (x != NULL) {
2153 for (; --oparg >= 0;) {
2154 w = POP();
2155 PyList_SET_ITEM(x, oparg, w);
2156 }
2157 PUSH(x);
2158 DISPATCH();
2159 }
2160 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 TARGET(BUILD_SET)
2163 x = PySet_New(NULL);
2164 if (x != NULL) {
2165 for (; --oparg >= 0;) {
2166 w = POP();
2167 if (err == 0)
2168 err = PySet_Add(x, w);
2169 Py_DECREF(w);
2170 }
2171 if (err != 0) {
2172 Py_DECREF(x);
2173 break;
2174 }
2175 PUSH(x);
2176 DISPATCH();
2177 }
2178 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 TARGET(BUILD_MAP)
2181 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2182 PUSH(x);
2183 if (x != NULL) DISPATCH();
2184 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 TARGET(STORE_MAP)
2187 w = TOP(); /* key */
2188 u = SECOND(); /* value */
2189 v = THIRD(); /* dict */
2190 STACKADJ(-2);
2191 assert (PyDict_CheckExact(v));
2192 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2193 Py_DECREF(u);
2194 Py_DECREF(w);
2195 if (err == 0) DISPATCH();
2196 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 TARGET(MAP_ADD)
2199 w = TOP(); /* key */
2200 u = SECOND(); /* value */
2201 STACKADJ(-2);
2202 v = stack_pointer[-oparg]; /* dict */
2203 assert (PyDict_CheckExact(v));
2204 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2205 Py_DECREF(u);
2206 Py_DECREF(w);
2207 if (err == 0) {
2208 PREDICT(JUMP_ABSOLUTE);
2209 DISPATCH();
2210 }
2211 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 TARGET(LOAD_ATTR)
2214 w = GETITEM(names, oparg);
2215 v = TOP();
2216 x = PyObject_GetAttr(v, w);
2217 Py_DECREF(v);
2218 SET_TOP(x);
2219 if (x != NULL) DISPATCH();
2220 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 TARGET(COMPARE_OP)
2223 w = POP();
2224 v = TOP();
2225 x = cmp_outcome(oparg, v, w);
2226 Py_DECREF(v);
2227 Py_DECREF(w);
2228 SET_TOP(x);
2229 if (x == NULL) break;
2230 PREDICT(POP_JUMP_IF_FALSE);
2231 PREDICT(POP_JUMP_IF_TRUE);
2232 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 TARGET(IMPORT_NAME)
2235 w = GETITEM(names, oparg);
2236 x = PyDict_GetItemString(f->f_builtins, "__import__");
2237 if (x == NULL) {
2238 PyErr_SetString(PyExc_ImportError,
2239 "__import__ not found");
2240 break;
2241 }
2242 Py_INCREF(x);
2243 v = POP();
2244 u = TOP();
2245 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2246 w = PyTuple_Pack(5,
2247 w,
2248 f->f_globals,
2249 f->f_locals == NULL ?
2250 Py_None : f->f_locals,
2251 v,
2252 u);
2253 else
2254 w = PyTuple_Pack(4,
2255 w,
2256 f->f_globals,
2257 f->f_locals == NULL ?
2258 Py_None : f->f_locals,
2259 v);
2260 Py_DECREF(v);
2261 Py_DECREF(u);
2262 if (w == NULL) {
2263 u = POP();
2264 Py_DECREF(x);
2265 x = NULL;
2266 break;
2267 }
2268 READ_TIMESTAMP(intr0);
2269 v = x;
2270 x = PyEval_CallObject(v, w);
2271 Py_DECREF(v);
2272 READ_TIMESTAMP(intr1);
2273 Py_DECREF(w);
2274 SET_TOP(x);
2275 if (x != NULL) DISPATCH();
2276 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 TARGET(IMPORT_STAR)
2279 v = POP();
2280 PyFrame_FastToLocals(f);
2281 if ((x = f->f_locals) == NULL) {
2282 PyErr_SetString(PyExc_SystemError,
2283 "no locals found during 'import *'");
2284 break;
2285 }
2286 READ_TIMESTAMP(intr0);
2287 err = import_all_from(x, v);
2288 READ_TIMESTAMP(intr1);
2289 PyFrame_LocalsToFast(f, 0);
2290 Py_DECREF(v);
2291 if (err == 0) DISPATCH();
2292 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 TARGET(IMPORT_FROM)
2295 w = GETITEM(names, oparg);
2296 v = TOP();
2297 READ_TIMESTAMP(intr0);
2298 x = import_from(v, w);
2299 READ_TIMESTAMP(intr1);
2300 PUSH(x);
2301 if (x != NULL) DISPATCH();
2302 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 TARGET(JUMP_FORWARD)
2305 JUMPBY(oparg);
2306 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2309 TARGET(POP_JUMP_IF_FALSE)
2310 w = POP();
2311 if (w == Py_True) {
2312 Py_DECREF(w);
2313 FAST_DISPATCH();
2314 }
2315 if (w == Py_False) {
2316 Py_DECREF(w);
2317 JUMPTO(oparg);
2318 FAST_DISPATCH();
2319 }
2320 err = PyObject_IsTrue(w);
2321 Py_DECREF(w);
2322 if (err > 0)
2323 err = 0;
2324 else if (err == 0)
2325 JUMPTO(oparg);
2326 else
2327 break;
2328 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2331 TARGET(POP_JUMP_IF_TRUE)
2332 w = POP();
2333 if (w == Py_False) {
2334 Py_DECREF(w);
2335 FAST_DISPATCH();
2336 }
2337 if (w == Py_True) {
2338 Py_DECREF(w);
2339 JUMPTO(oparg);
2340 FAST_DISPATCH();
2341 }
2342 err = PyObject_IsTrue(w);
2343 Py_DECREF(w);
2344 if (err > 0) {
2345 err = 0;
2346 JUMPTO(oparg);
2347 }
2348 else if (err == 0)
2349 ;
2350 else
2351 break;
2352 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 TARGET(JUMP_IF_FALSE_OR_POP)
2355 w = TOP();
2356 if (w == Py_True) {
2357 STACKADJ(-1);
2358 Py_DECREF(w);
2359 FAST_DISPATCH();
2360 }
2361 if (w == Py_False) {
2362 JUMPTO(oparg);
2363 FAST_DISPATCH();
2364 }
2365 err = PyObject_IsTrue(w);
2366 if (err > 0) {
2367 STACKADJ(-1);
2368 Py_DECREF(w);
2369 err = 0;
2370 }
2371 else if (err == 0)
2372 JUMPTO(oparg);
2373 else
2374 break;
2375 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 TARGET(JUMP_IF_TRUE_OR_POP)
2378 w = TOP();
2379 if (w == Py_False) {
2380 STACKADJ(-1);
2381 Py_DECREF(w);
2382 FAST_DISPATCH();
2383 }
2384 if (w == Py_True) {
2385 JUMPTO(oparg);
2386 FAST_DISPATCH();
2387 }
2388 err = PyObject_IsTrue(w);
2389 if (err > 0) {
2390 err = 0;
2391 JUMPTO(oparg);
2392 }
2393 else if (err == 0) {
2394 STACKADJ(-1);
2395 Py_DECREF(w);
2396 }
2397 else
2398 break;
2399 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2402 TARGET(JUMP_ABSOLUTE)
2403 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002404#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* Enabling this path speeds-up all while and for-loops by bypassing
2406 the per-loop checks for signals. By default, this should be turned-off
2407 because it prevents detection of a control-break in tight loops like
2408 "while 1: pass". Compile with this option turned-on when you need
2409 the speed-up and do not need break checking inside tight loops (ones
2410 that contain only instructions ending with FAST_DISPATCH).
2411 */
2412 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002413#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002415#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 TARGET(GET_ITER)
2418 /* before: [obj]; after [getiter(obj)] */
2419 v = TOP();
2420 x = PyObject_GetIter(v);
2421 Py_DECREF(v);
2422 if (x != NULL) {
2423 SET_TOP(x);
2424 PREDICT(FOR_ITER);
2425 DISPATCH();
2426 }
2427 STACKADJ(-1);
2428 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 PREDICTED_WITH_ARG(FOR_ITER);
2431 TARGET(FOR_ITER)
2432 /* before: [iter]; after: [iter, iter()] *or* [] */
2433 v = TOP();
2434 x = (*v->ob_type->tp_iternext)(v);
2435 if (x != NULL) {
2436 PUSH(x);
2437 PREDICT(STORE_FAST);
2438 PREDICT(UNPACK_SEQUENCE);
2439 DISPATCH();
2440 }
2441 if (PyErr_Occurred()) {
2442 if (!PyErr_ExceptionMatches(
2443 PyExc_StopIteration))
2444 break;
2445 PyErr_Clear();
2446 }
2447 /* iterator ended normally */
2448 x = v = POP();
2449 Py_DECREF(v);
2450 JUMPBY(oparg);
2451 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 TARGET(BREAK_LOOP)
2454 why = WHY_BREAK;
2455 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 TARGET(CONTINUE_LOOP)
2458 retval = PyLong_FromLong(oparg);
2459 if (!retval) {
2460 x = NULL;
2461 break;
2462 }
2463 why = WHY_CONTINUE;
2464 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2467 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2468 TARGET(SETUP_FINALLY)
2469 _setup_finally:
2470 /* NOTE: If you add any new block-setup opcodes that
2471 are not try/except/finally handlers, you may need
2472 to update the PyGen_NeedsFinalizing() function.
2473 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2476 STACK_LEVEL());
2477 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 TARGET(SETUP_WITH)
2480 {
2481 static PyObject *exit, *enter;
2482 w = TOP();
2483 x = special_lookup(w, "__exit__", &exit);
2484 if (!x)
2485 break;
2486 SET_TOP(x);
2487 u = special_lookup(w, "__enter__", &enter);
2488 Py_DECREF(w);
2489 if (!u) {
2490 x = NULL;
2491 break;
2492 }
2493 x = PyObject_CallFunctionObjArgs(u, NULL);
2494 Py_DECREF(u);
2495 if (!x)
2496 break;
2497 /* Setup the finally block before pushing the result
2498 of __enter__ on the stack. */
2499 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2500 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 PUSH(x);
2503 DISPATCH();
2504 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 TARGET(WITH_CLEANUP)
2507 {
2508 /* At the top of the stack are 1-3 values indicating
2509 how/why we entered the finally clause:
2510 - TOP = None
2511 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2512 - TOP = WHY_*; no retval below it
2513 - (TOP, SECOND, THIRD) = exc_info()
2514 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2515 Below them is EXIT, the context.__exit__ bound method.
2516 In the last case, we must call
2517 EXIT(TOP, SECOND, THIRD)
2518 otherwise we must call
2519 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 In the first two cases, we remove EXIT from the
2522 stack, leaving the rest in the same order. In the
2523 third case, we shift the bottom 3 values of the
2524 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 In addition, if the stack represents an exception,
2527 *and* the function call returns a 'true' value, we
2528 push WHY_SILENCED onto the stack. END_FINALLY will
2529 then not re-raise the exception. (But non-local
2530 gotos should still be resumed.)
2531 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 PyObject *exit_func;
2534 u = TOP();
2535 if (u == Py_None) {
2536 (void)POP();
2537 exit_func = TOP();
2538 SET_TOP(u);
2539 v = w = Py_None;
2540 }
2541 else if (PyLong_Check(u)) {
2542 (void)POP();
2543 switch(PyLong_AsLong(u)) {
2544 case WHY_RETURN:
2545 case WHY_CONTINUE:
2546 /* Retval in TOP. */
2547 exit_func = SECOND();
2548 SET_SECOND(TOP());
2549 SET_TOP(u);
2550 break;
2551 default:
2552 exit_func = TOP();
2553 SET_TOP(u);
2554 break;
2555 }
2556 u = v = w = Py_None;
2557 }
2558 else {
2559 PyObject *tp, *exc, *tb;
2560 PyTryBlock *block;
2561 v = SECOND();
2562 w = THIRD();
2563 tp = FOURTH();
2564 exc = PEEK(5);
2565 tb = PEEK(6);
2566 exit_func = PEEK(7);
2567 SET_VALUE(7, tb);
2568 SET_VALUE(6, exc);
2569 SET_VALUE(5, tp);
2570 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2571 SET_FOURTH(NULL);
2572 /* We just shifted the stack down, so we have
2573 to tell the except handler block that the
2574 values are lower than it expects. */
2575 block = &f->f_blockstack[f->f_iblock - 1];
2576 assert(block->b_type == EXCEPT_HANDLER);
2577 block->b_level--;
2578 }
2579 /* XXX Not the fastest way to call it... */
2580 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2581 NULL);
2582 Py_DECREF(exit_func);
2583 if (x == NULL)
2584 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 if (u != Py_None)
2587 err = PyObject_IsTrue(x);
2588 else
2589 err = 0;
2590 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 if (err < 0)
2593 break; /* Go to error exit */
2594 else if (err > 0) {
2595 err = 0;
2596 /* There was an exception and a True return */
2597 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2598 }
2599 PREDICT(END_FINALLY);
2600 break;
2601 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 TARGET(CALL_FUNCTION)
2604 {
2605 PyObject **sp;
2606 PCALL(PCALL_ALL);
2607 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002608#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002610#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002612#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 stack_pointer = sp;
2614 PUSH(x);
2615 if (x != NULL)
2616 DISPATCH();
2617 break;
2618 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2621 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2622 TARGET(CALL_FUNCTION_VAR_KW)
2623 _call_function_var_kw:
2624 {
2625 int na = oparg & 0xff;
2626 int nk = (oparg>>8) & 0xff;
2627 int flags = (opcode - CALL_FUNCTION) & 3;
2628 int n = na + 2 * nk;
2629 PyObject **pfunc, *func, **sp;
2630 PCALL(PCALL_ALL);
2631 if (flags & CALL_FLAG_VAR)
2632 n++;
2633 if (flags & CALL_FLAG_KW)
2634 n++;
2635 pfunc = stack_pointer - n - 1;
2636 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002639 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 PyObject *self = PyMethod_GET_SELF(func);
2641 Py_INCREF(self);
2642 func = PyMethod_GET_FUNCTION(func);
2643 Py_INCREF(func);
2644 Py_DECREF(*pfunc);
2645 *pfunc = self;
2646 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002647 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 } else
2649 Py_INCREF(func);
2650 sp = stack_pointer;
2651 READ_TIMESTAMP(intr0);
2652 x = ext_do_call(func, &sp, flags, na, nk);
2653 READ_TIMESTAMP(intr1);
2654 stack_pointer = sp;
2655 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 while (stack_pointer > pfunc) {
2658 w = POP();
2659 Py_DECREF(w);
2660 }
2661 PUSH(x);
2662 if (x != NULL)
2663 DISPATCH();
2664 break;
2665 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2668 TARGET(MAKE_FUNCTION)
2669 _make_function:
2670 {
2671 int posdefaults = oparg & 0xff;
2672 int kwdefaults = (oparg>>8) & 0xff;
2673 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 v = POP(); /* code object */
2676 x = PyFunction_New(v, f->f_globals);
2677 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 if (x != NULL && opcode == MAKE_CLOSURE) {
2680 v = POP();
2681 if (PyFunction_SetClosure(x, v) != 0) {
2682 /* Can't happen unless bytecode is corrupt. */
2683 why = WHY_EXCEPTION;
2684 }
2685 Py_DECREF(v);
2686 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 if (x != NULL && num_annotations > 0) {
2689 Py_ssize_t name_ix;
2690 u = POP(); /* names of args with annotations */
2691 v = PyDict_New();
2692 if (v == NULL) {
2693 Py_DECREF(x);
2694 x = NULL;
2695 break;
2696 }
2697 name_ix = PyTuple_Size(u);
2698 assert(num_annotations == name_ix+1);
2699 while (name_ix > 0) {
2700 --name_ix;
2701 t = PyTuple_GET_ITEM(u, name_ix);
2702 w = POP();
2703 /* XXX(nnorwitz): check for errors */
2704 PyDict_SetItem(v, t, w);
2705 Py_DECREF(w);
2706 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 if (PyFunction_SetAnnotations(x, v) != 0) {
2709 /* Can't happen unless
2710 PyFunction_SetAnnotations changes. */
2711 why = WHY_EXCEPTION;
2712 }
2713 Py_DECREF(v);
2714 Py_DECREF(u);
2715 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 /* XXX Maybe this should be a separate opcode? */
2718 if (x != NULL && posdefaults > 0) {
2719 v = PyTuple_New(posdefaults);
2720 if (v == NULL) {
2721 Py_DECREF(x);
2722 x = NULL;
2723 break;
2724 }
2725 while (--posdefaults >= 0) {
2726 w = POP();
2727 PyTuple_SET_ITEM(v, posdefaults, w);
2728 }
2729 if (PyFunction_SetDefaults(x, v) != 0) {
2730 /* Can't happen unless
2731 PyFunction_SetDefaults changes. */
2732 why = WHY_EXCEPTION;
2733 }
2734 Py_DECREF(v);
2735 }
2736 if (x != NULL && kwdefaults > 0) {
2737 v = PyDict_New();
2738 if (v == NULL) {
2739 Py_DECREF(x);
2740 x = NULL;
2741 break;
2742 }
2743 while (--kwdefaults >= 0) {
2744 w = POP(); /* default value */
2745 u = POP(); /* kw only arg name */
2746 /* XXX(nnorwitz): check for errors */
2747 PyDict_SetItem(v, u, w);
2748 Py_DECREF(w);
2749 Py_DECREF(u);
2750 }
2751 if (PyFunction_SetKwDefaults(x, v) != 0) {
2752 /* Can't happen unless
2753 PyFunction_SetKwDefaults changes. */
2754 why = WHY_EXCEPTION;
2755 }
2756 Py_DECREF(v);
2757 }
2758 PUSH(x);
2759 break;
2760 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 TARGET(BUILD_SLICE)
2763 if (oparg == 3)
2764 w = POP();
2765 else
2766 w = NULL;
2767 v = POP();
2768 u = TOP();
2769 x = PySlice_New(u, v, w);
2770 Py_DECREF(u);
2771 Py_DECREF(v);
2772 Py_XDECREF(w);
2773 SET_TOP(x);
2774 if (x != NULL) DISPATCH();
2775 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 TARGET(EXTENDED_ARG)
2778 opcode = NEXTOP();
2779 oparg = oparg<<16 | NEXTARG();
2780 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002781
Antoine Pitrou042b1282010-08-13 21:15:58 +00002782#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002784#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 default:
2786 fprintf(stderr,
2787 "XXX lineno: %d, opcode: %d\n",
2788 PyFrame_GetLineNumber(f),
2789 opcode);
2790 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2791 why = WHY_EXCEPTION;
2792 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002793
2794#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002796#endif
2797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 if (why == WHY_NOT) {
2807 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002808#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 /* This check is expensive! */
2810 if (PyErr_Occurred())
2811 fprintf(stderr,
2812 "XXX undetected error\n");
2813 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002814#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 READ_TIMESTAMP(loop1);
2816 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002817#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002819#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 }
2821 why = WHY_EXCEPTION;
2822 x = Py_None;
2823 err = 0;
2824 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2829 if (!PyErr_Occurred()) {
2830 PyErr_SetString(PyExc_SystemError,
2831 "error return without exception set");
2832 why = WHY_EXCEPTION;
2833 }
2834 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002835#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 else {
2837 /* This check is expensive! */
2838 if (PyErr_Occurred()) {
2839 char buf[128];
2840 sprintf(buf, "Stack unwind with exception "
2841 "set and why=%d", why);
2842 Py_FatalError(buf);
2843 }
2844 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002845#endif
2846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 if (why == WHY_EXCEPTION) {
2850 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 if (tstate->c_tracefunc != NULL)
2853 call_exc_trace(tstate->c_tracefunc,
2854 tstate->c_traceobj, f);
2855 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 if (why == WHY_RERAISE)
2860 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002863
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002864fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 while (why != WHY_NOT && f->f_iblock > 0) {
2866 /* Peek at the current block. */
2867 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 assert(why != WHY_YIELD);
2870 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2871 why = WHY_NOT;
2872 JUMPTO(PyLong_AS_LONG(retval));
2873 Py_DECREF(retval);
2874 break;
2875 }
2876 /* Now we have to pop the block. */
2877 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 if (b->b_type == EXCEPT_HANDLER) {
2880 UNWIND_EXCEPT_HANDLER(b);
2881 continue;
2882 }
2883 UNWIND_BLOCK(b);
2884 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2885 why = WHY_NOT;
2886 JUMPTO(b->b_handler);
2887 break;
2888 }
2889 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2890 || b->b_type == SETUP_FINALLY)) {
2891 PyObject *exc, *val, *tb;
2892 int handler = b->b_handler;
2893 /* Beware, this invalidates all b->b_* fields */
2894 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2895 PUSH(tstate->exc_traceback);
2896 PUSH(tstate->exc_value);
2897 if (tstate->exc_type != NULL) {
2898 PUSH(tstate->exc_type);
2899 }
2900 else {
2901 Py_INCREF(Py_None);
2902 PUSH(Py_None);
2903 }
2904 PyErr_Fetch(&exc, &val, &tb);
2905 /* Make the raw exception data
2906 available to the handler,
2907 so a program can emulate the
2908 Python main loop. */
2909 PyErr_NormalizeException(
2910 &exc, &val, &tb);
2911 PyException_SetTraceback(val, tb);
2912 Py_INCREF(exc);
2913 tstate->exc_type = exc;
2914 Py_INCREF(val);
2915 tstate->exc_value = val;
2916 tstate->exc_traceback = tb;
2917 if (tb == NULL)
2918 tb = Py_None;
2919 Py_INCREF(tb);
2920 PUSH(tb);
2921 PUSH(val);
2922 PUSH(exc);
2923 why = WHY_NOT;
2924 JUMPTO(handler);
2925 break;
2926 }
2927 if (b->b_type == SETUP_FINALLY) {
2928 if (why & (WHY_RETURN | WHY_CONTINUE))
2929 PUSH(retval);
2930 PUSH(PyLong_FromLong((long)why));
2931 why = WHY_NOT;
2932 JUMPTO(b->b_handler);
2933 break;
2934 }
2935 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 if (why != WHY_NOT)
2940 break;
2941 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 assert(why != WHY_YIELD);
2946 /* Pop remaining stack entries. */
2947 while (!EMPTY()) {
2948 v = POP();
2949 Py_XDECREF(v);
2950 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 if (why != WHY_RETURN)
2953 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002954
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002955fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05002956 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
2957 /* The purpose of this block is to put aside the generator's exception
2958 state and restore that of the calling frame. If the current
2959 exception state is from the caller, we clear the exception values
2960 on the generator frame, so they are not swapped back in latter. The
2961 origin of the current exception state is determined by checking for
2962 except handler blocks, which we must be in iff a new exception
2963 state came into existence in this frame. (An uncaught exception
2964 would have why == WHY_EXCEPTION, and we wouldn't be here). */
2965 int i;
2966 for (i = 0; i < f->f_iblock; i++)
2967 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
2968 break;
2969 if (i == f->f_iblock)
2970 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05002971 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05002972 else
Benjamin Peterson87880242011-07-03 16:48:31 -05002973 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05002974 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05002975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 if (tstate->use_tracing) {
2977 if (tstate->c_tracefunc) {
2978 if (why == WHY_RETURN || why == WHY_YIELD) {
2979 if (call_trace(tstate->c_tracefunc,
2980 tstate->c_traceobj, f,
2981 PyTrace_RETURN, retval)) {
2982 Py_XDECREF(retval);
2983 retval = NULL;
2984 why = WHY_EXCEPTION;
2985 }
2986 }
2987 else if (why == WHY_EXCEPTION) {
2988 call_trace_protected(tstate->c_tracefunc,
2989 tstate->c_traceobj, f,
2990 PyTrace_RETURN, NULL);
2991 }
2992 }
2993 if (tstate->c_profilefunc) {
2994 if (why == WHY_EXCEPTION)
2995 call_trace_protected(tstate->c_profilefunc,
2996 tstate->c_profileobj, f,
2997 PyTrace_RETURN, NULL);
2998 else if (call_trace(tstate->c_profilefunc,
2999 tstate->c_profileobj, f,
3000 PyTrace_RETURN, retval)) {
3001 Py_XDECREF(retval);
3002 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003003 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 }
3005 }
3006 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003009exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 Py_LeaveRecursiveCall();
3011 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003014}
3015
Benjamin Petersonb204a422011-06-05 22:04:07 -05003016static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003017format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3018{
3019 int err;
3020 Py_ssize_t len = PyList_GET_SIZE(names);
3021 PyObject *name_str, *comma, *tail, *tmp;
3022
3023 assert(PyList_CheckExact(names));
3024 assert(len >= 1);
3025 /* Deal with the joys of natural language. */
3026 switch (len) {
3027 case 1:
3028 name_str = PyList_GET_ITEM(names, 0);
3029 Py_INCREF(name_str);
3030 break;
3031 case 2:
3032 name_str = PyUnicode_FromFormat("%U and %U",
3033 PyList_GET_ITEM(names, len - 2),
3034 PyList_GET_ITEM(names, len - 1));
3035 break;
3036 default:
3037 tail = PyUnicode_FromFormat(", %U, and %U",
3038 PyList_GET_ITEM(names, len - 2),
3039 PyList_GET_ITEM(names, len - 1));
3040 /* Chop off the last two objects in the list. This shouldn't actually
3041 fail, but we can't be too careful. */
3042 err = PyList_SetSlice(names, len - 2, len, NULL);
3043 if (err == -1) {
3044 Py_DECREF(tail);
3045 return;
3046 }
3047 /* Stitch everything up into a nice comma-separated list. */
3048 comma = PyUnicode_FromString(", ");
3049 if (comma == NULL) {
3050 Py_DECREF(tail);
3051 return;
3052 }
3053 tmp = PyUnicode_Join(comma, names);
3054 Py_DECREF(comma);
3055 if (tmp == NULL) {
3056 Py_DECREF(tail);
3057 return;
3058 }
3059 name_str = PyUnicode_Concat(tmp, tail);
3060 Py_DECREF(tmp);
3061 Py_DECREF(tail);
3062 break;
3063 }
3064 if (name_str == NULL)
3065 return;
3066 PyErr_Format(PyExc_TypeError,
3067 "%U() missing %i required %s argument%s: %U",
3068 co->co_name,
3069 len,
3070 kind,
3071 len == 1 ? "" : "s",
3072 name_str);
3073 Py_DECREF(name_str);
3074}
3075
3076static void
3077missing_arguments(PyCodeObject *co, int missing, int defcount,
3078 PyObject **fastlocals)
3079{
3080 int i, j = 0;
3081 int start, end;
3082 int positional = defcount != -1;
3083 const char *kind = positional ? "positional" : "keyword-only";
3084 PyObject *missing_names;
3085
3086 /* Compute the names of the arguments that are missing. */
3087 missing_names = PyList_New(missing);
3088 if (missing_names == NULL)
3089 return;
3090 if (positional) {
3091 start = 0;
3092 end = co->co_argcount - defcount;
3093 }
3094 else {
3095 start = co->co_argcount;
3096 end = start + co->co_kwonlyargcount;
3097 }
3098 for (i = start; i < end; i++) {
3099 if (GETLOCAL(i) == NULL) {
3100 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3101 PyObject *name = PyObject_Repr(raw);
3102 if (name == NULL) {
3103 Py_DECREF(missing_names);
3104 return;
3105 }
3106 PyList_SET_ITEM(missing_names, j++, name);
3107 }
3108 }
3109 assert(j == missing);
3110 format_missing(kind, co, missing_names);
3111 Py_DECREF(missing_names);
3112}
3113
3114static void
3115too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003116{
3117 int plural;
3118 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003119 int i;
3120 PyObject *sig, *kwonly_sig;
3121
Benjamin Petersone109c702011-06-24 09:37:26 -05003122 assert((co->co_flags & CO_VARARGS) == 0);
3123 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003124 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003125 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003126 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003127 if (defcount) {
3128 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003129 plural = 1;
3130 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3131 }
3132 else {
3133 plural = co->co_argcount != 1;
3134 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3135 }
3136 if (sig == NULL)
3137 return;
3138 if (kwonly_given) {
3139 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3140 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3141 kwonly_given != 1 ? "s" : "");
3142 if (kwonly_sig == NULL) {
3143 Py_DECREF(sig);
3144 return;
3145 }
3146 }
3147 else {
3148 /* This will not fail. */
3149 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003150 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003151 }
3152 PyErr_Format(PyExc_TypeError,
3153 "%U() takes %U positional argument%s but %d%U %s given",
3154 co->co_name,
3155 sig,
3156 plural ? "s" : "",
3157 given,
3158 kwonly_sig,
3159 given == 1 && !kwonly_given ? "was" : "were");
3160 Py_DECREF(sig);
3161 Py_DECREF(kwonly_sig);
3162}
3163
Guido van Rossumc2e20742006-02-27 22:32:47 +00003164/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003165 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003166 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003167
Tim Peters6d6c1a32001-08-02 04:15:00 +00003168PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003169PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 PyObject **args, int argcount, PyObject **kws, int kwcount,
3171 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003172{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003173 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 register PyFrameObject *f;
3175 register PyObject *retval = NULL;
3176 register PyObject **fastlocals, **freevars;
3177 PyThreadState *tstate = PyThreadState_GET();
3178 PyObject *x, *u;
3179 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003180 int i;
3181 int n = argcount;
3182 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 if (globals == NULL) {
3185 PyErr_SetString(PyExc_SystemError,
3186 "PyEval_EvalCodeEx: NULL globals");
3187 return NULL;
3188 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 assert(tstate != NULL);
3191 assert(globals != NULL);
3192 f = PyFrame_New(tstate, co, globals, locals);
3193 if (f == NULL)
3194 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 fastlocals = f->f_localsplus;
3197 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003198
Benjamin Petersonb204a422011-06-05 22:04:07 -05003199 /* Parse arguments. */
3200 if (co->co_flags & CO_VARKEYWORDS) {
3201 kwdict = PyDict_New();
3202 if (kwdict == NULL)
3203 goto fail;
3204 i = total_args;
3205 if (co->co_flags & CO_VARARGS)
3206 i++;
3207 SETLOCAL(i, kwdict);
3208 }
3209 if (argcount > co->co_argcount)
3210 n = co->co_argcount;
3211 for (i = 0; i < n; i++) {
3212 x = args[i];
3213 Py_INCREF(x);
3214 SETLOCAL(i, x);
3215 }
3216 if (co->co_flags & CO_VARARGS) {
3217 u = PyTuple_New(argcount - n);
3218 if (u == NULL)
3219 goto fail;
3220 SETLOCAL(total_args, u);
3221 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 x = args[i];
3223 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003224 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003226 }
3227 for (i = 0; i < kwcount; i++) {
3228 PyObject **co_varnames;
3229 PyObject *keyword = kws[2*i];
3230 PyObject *value = kws[2*i + 1];
3231 int j;
3232 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3233 PyErr_Format(PyExc_TypeError,
3234 "%U() keywords must be strings",
3235 co->co_name);
3236 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003238 /* Speed hack: do raw pointer compares. As names are
3239 normally interned this should almost always hit. */
3240 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3241 for (j = 0; j < total_args; j++) {
3242 PyObject *nm = co_varnames[j];
3243 if (nm == keyword)
3244 goto kw_found;
3245 }
3246 /* Slow fallback, just in case */
3247 for (j = 0; j < total_args; j++) {
3248 PyObject *nm = co_varnames[j];
3249 int cmp = PyObject_RichCompareBool(
3250 keyword, nm, Py_EQ);
3251 if (cmp > 0)
3252 goto kw_found;
3253 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003255 }
3256 if (j >= total_args && kwdict == NULL) {
3257 PyErr_Format(PyExc_TypeError,
3258 "%U() got an unexpected "
3259 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 co->co_name,
3261 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003262 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003264 PyDict_SetItem(kwdict, keyword, value);
3265 continue;
3266 kw_found:
3267 if (GETLOCAL(j) != NULL) {
3268 PyErr_Format(PyExc_TypeError,
3269 "%U() got multiple "
3270 "values for argument '%S'",
3271 co->co_name,
3272 keyword);
3273 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003275 Py_INCREF(value);
3276 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003278 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003279 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 goto fail;
3281 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003282 if (argcount < co->co_argcount) {
3283 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003284 int missing = 0;
3285 for (i = argcount; i < m; i++)
3286 if (GETLOCAL(i) == NULL)
3287 missing++;
3288 if (missing) {
3289 missing_arguments(co, missing, defcount, fastlocals);
3290 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003291 }
3292 if (n > m)
3293 i = n - m;
3294 else
3295 i = 0;
3296 for (; i < defcount; i++) {
3297 if (GETLOCAL(m+i) == NULL) {
3298 PyObject *def = defs[i];
3299 Py_INCREF(def);
3300 SETLOCAL(m+i, def);
3301 }
3302 }
3303 }
3304 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003305 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003306 for (i = co->co_argcount; i < total_args; i++) {
3307 PyObject *name;
3308 if (GETLOCAL(i) != NULL)
3309 continue;
3310 name = PyTuple_GET_ITEM(co->co_varnames, i);
3311 if (kwdefs != NULL) {
3312 PyObject *def = PyDict_GetItem(kwdefs, name);
3313 if (def) {
3314 Py_INCREF(def);
3315 SETLOCAL(i, def);
3316 continue;
3317 }
3318 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003319 missing++;
3320 }
3321 if (missing) {
3322 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003323 goto fail;
3324 }
3325 }
3326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003328 vars into frame. */
3329 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003331 int arg;
3332 /* Possibly account for the cell variable being an argument. */
3333 if (co->co_cell2arg != NULL &&
3334 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3335 c = PyCell_New(GETLOCAL(arg));
3336 else
3337 c = PyCell_New(NULL);
3338 if (c == NULL)
3339 goto fail;
3340 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003342 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3343 PyObject *o = PyTuple_GET_ITEM(closure, i);
3344 Py_INCREF(o);
3345 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 if (co->co_flags & CO_GENERATOR) {
3349 /* Don't need to keep the reference to f_back, it will be set
3350 * when the generator is resumed. */
3351 Py_XDECREF(f->f_back);
3352 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 /* Create a new generator that owns the ready to run frame
3357 * and return that as the value. */
3358 return PyGen_New(f);
3359 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003362
Thomas Woutersce272b62007-09-19 21:19:28 +00003363fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 /* decref'ing the frame can cause __del__ methods to get invoked,
3366 which can call back into Python. While we're done with the
3367 current Python frame (f), the associated C stack is still in use,
3368 so recursion_depth must be boosted for the duration.
3369 */
3370 assert(tstate != NULL);
3371 ++tstate->recursion_depth;
3372 Py_DECREF(f);
3373 --tstate->recursion_depth;
3374 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003375}
3376
3377
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003378static PyObject *
3379special_lookup(PyObject *o, char *meth, PyObject **cache)
3380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 PyObject *res;
3382 res = _PyObject_LookupSpecial(o, meth, cache);
3383 if (res == NULL && !PyErr_Occurred()) {
3384 PyErr_SetObject(PyExc_AttributeError, *cache);
3385 return NULL;
3386 }
3387 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003388}
3389
3390
Benjamin Peterson87880242011-07-03 16:48:31 -05003391/* These 3 functions deal with the exception state of generators. */
3392
3393static void
3394save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3395{
3396 PyObject *type, *value, *traceback;
3397 Py_XINCREF(tstate->exc_type);
3398 Py_XINCREF(tstate->exc_value);
3399 Py_XINCREF(tstate->exc_traceback);
3400 type = f->f_exc_type;
3401 value = f->f_exc_value;
3402 traceback = f->f_exc_traceback;
3403 f->f_exc_type = tstate->exc_type;
3404 f->f_exc_value = tstate->exc_value;
3405 f->f_exc_traceback = tstate->exc_traceback;
3406 Py_XDECREF(type);
3407 Py_XDECREF(value);
3408 Py_XDECREF(traceback);
3409}
3410
3411static void
3412swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3413{
3414 PyObject *tmp;
3415 tmp = tstate->exc_type;
3416 tstate->exc_type = f->f_exc_type;
3417 f->f_exc_type = tmp;
3418 tmp = tstate->exc_value;
3419 tstate->exc_value = f->f_exc_value;
3420 f->f_exc_value = tmp;
3421 tmp = tstate->exc_traceback;
3422 tstate->exc_traceback = f->f_exc_traceback;
3423 f->f_exc_traceback = tmp;
3424}
3425
3426static void
3427restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3428{
3429 PyObject *type, *value, *tb;
3430 type = tstate->exc_type;
3431 value = tstate->exc_value;
3432 tb = tstate->exc_traceback;
3433 tstate->exc_type = f->f_exc_type;
3434 tstate->exc_value = f->f_exc_value;
3435 tstate->exc_traceback = f->f_exc_traceback;
3436 f->f_exc_type = NULL;
3437 f->f_exc_value = NULL;
3438 f->f_exc_traceback = NULL;
3439 Py_XDECREF(type);
3440 Py_XDECREF(value);
3441 Py_XDECREF(tb);
3442}
3443
3444
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003445/* Logic for the raise statement (too complicated for inlining).
3446 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003447static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003448do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 if (exc == NULL) {
3453 /* Reraise */
3454 PyThreadState *tstate = PyThreadState_GET();
3455 PyObject *tb;
3456 type = tstate->exc_type;
3457 value = tstate->exc_value;
3458 tb = tstate->exc_traceback;
3459 if (type == Py_None) {
3460 PyErr_SetString(PyExc_RuntimeError,
3461 "No active exception to reraise");
3462 return WHY_EXCEPTION;
3463 }
3464 Py_XINCREF(type);
3465 Py_XINCREF(value);
3466 Py_XINCREF(tb);
3467 PyErr_Restore(type, value, tb);
3468 return WHY_RERAISE;
3469 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 /* We support the following forms of raise:
3472 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003473 raise <instance>
3474 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 if (PyExceptionClass_Check(exc)) {
3477 type = exc;
3478 value = PyObject_CallObject(exc, NULL);
3479 if (value == NULL)
3480 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003481 if (!PyExceptionInstance_Check(value)) {
3482 PyErr_Format(PyExc_TypeError,
3483 "calling %R should have returned an instance of "
3484 "BaseException, not %R",
3485 type, Py_TYPE(value));
3486 goto raise_error;
3487 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 }
3489 else if (PyExceptionInstance_Check(exc)) {
3490 value = exc;
3491 type = PyExceptionInstance_Class(exc);
3492 Py_INCREF(type);
3493 }
3494 else {
3495 /* Not something you can raise. You get an exception
3496 anyway, just not what you specified :-) */
3497 Py_DECREF(exc);
3498 PyErr_SetString(PyExc_TypeError,
3499 "exceptions must derive from BaseException");
3500 goto raise_error;
3501 }
Collin Winter828f04a2007-08-31 00:04:24 +00003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 if (cause) {
3504 PyObject *fixed_cause;
3505 if (PyExceptionClass_Check(cause)) {
3506 fixed_cause = PyObject_CallObject(cause, NULL);
3507 if (fixed_cause == NULL)
3508 goto raise_error;
3509 Py_DECREF(cause);
3510 }
3511 else if (PyExceptionInstance_Check(cause)) {
3512 fixed_cause = cause;
3513 }
3514 else {
3515 PyErr_SetString(PyExc_TypeError,
3516 "exception causes must derive from "
3517 "BaseException");
3518 goto raise_error;
3519 }
3520 PyException_SetCause(value, fixed_cause);
3521 }
Collin Winter828f04a2007-08-31 00:04:24 +00003522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 PyErr_SetObject(type, value);
3524 /* PyErr_SetObject incref's its arguments */
3525 Py_XDECREF(value);
3526 Py_XDECREF(type);
3527 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003528
3529raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 Py_XDECREF(value);
3531 Py_XDECREF(type);
3532 Py_XDECREF(cause);
3533 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003534}
3535
Tim Petersd6d010b2001-06-21 02:49:55 +00003536/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003537 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003538
Guido van Rossum0368b722007-05-11 16:50:42 +00003539 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3540 with a variable target.
3541*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003542
Barry Warsawe42b18f1997-08-25 22:13:04 +00003543static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003544unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 int i = 0, j = 0;
3547 Py_ssize_t ll = 0;
3548 PyObject *it; /* iter(v) */
3549 PyObject *w;
3550 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 it = PyObject_GetIter(v);
3555 if (it == NULL)
3556 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 for (; i < argcnt; i++) {
3559 w = PyIter_Next(it);
3560 if (w == NULL) {
3561 /* Iterator done, via error or exhaustion. */
3562 if (!PyErr_Occurred()) {
3563 PyErr_Format(PyExc_ValueError,
3564 "need more than %d value%s to unpack",
3565 i, i == 1 ? "" : "s");
3566 }
3567 goto Error;
3568 }
3569 *--sp = w;
3570 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 if (argcntafter == -1) {
3573 /* We better have exhausted the iterator now. */
3574 w = PyIter_Next(it);
3575 if (w == NULL) {
3576 if (PyErr_Occurred())
3577 goto Error;
3578 Py_DECREF(it);
3579 return 1;
3580 }
3581 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003582 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3583 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 goto Error;
3585 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 l = PySequence_List(it);
3588 if (l == NULL)
3589 goto Error;
3590 *--sp = l;
3591 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 ll = PyList_GET_SIZE(l);
3594 if (ll < argcntafter) {
3595 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3596 argcnt + ll);
3597 goto Error;
3598 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 /* Pop the "after-variable" args off the list. */
3601 for (j = argcntafter; j > 0; j--, i++) {
3602 *--sp = PyList_GET_ITEM(l, ll - j);
3603 }
3604 /* Resize the list. */
3605 Py_SIZE(l) = ll - argcntafter;
3606 Py_DECREF(it);
3607 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003608
Tim Petersd6d010b2001-06-21 02:49:55 +00003609Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 for (; i > 0; i--, sp++)
3611 Py_DECREF(*sp);
3612 Py_XDECREF(it);
3613 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003614}
3615
3616
Guido van Rossum96a42c81992-01-12 02:29:51 +00003617#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003618static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003619prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 printf("%s ", str);
3622 if (PyObject_Print(v, stdout, 0) != 0)
3623 PyErr_Clear(); /* Don't know what else to do */
3624 printf("\n");
3625 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003626}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003627#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003628
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003629static void
Fred Drake5755ce62001-06-27 19:19:46 +00003630call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 PyObject *type, *value, *traceback, *arg;
3633 int err;
3634 PyErr_Fetch(&type, &value, &traceback);
3635 if (value == NULL) {
3636 value = Py_None;
3637 Py_INCREF(value);
3638 }
3639 arg = PyTuple_Pack(3, type, value, traceback);
3640 if (arg == NULL) {
3641 PyErr_Restore(type, value, traceback);
3642 return;
3643 }
3644 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3645 Py_DECREF(arg);
3646 if (err == 0)
3647 PyErr_Restore(type, value, traceback);
3648 else {
3649 Py_XDECREF(type);
3650 Py_XDECREF(value);
3651 Py_XDECREF(traceback);
3652 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003653}
3654
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003655static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003656call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 PyObject *type, *value, *traceback;
3660 int err;
3661 PyErr_Fetch(&type, &value, &traceback);
3662 err = call_trace(func, obj, frame, what, arg);
3663 if (err == 0)
3664 {
3665 PyErr_Restore(type, value, traceback);
3666 return 0;
3667 }
3668 else {
3669 Py_XDECREF(type);
3670 Py_XDECREF(value);
3671 Py_XDECREF(traceback);
3672 return -1;
3673 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003674}
3675
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003676static int
Fred Drake5755ce62001-06-27 19:19:46 +00003677call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 register PyThreadState *tstate = frame->f_tstate;
3681 int result;
3682 if (tstate->tracing)
3683 return 0;
3684 tstate->tracing++;
3685 tstate->use_tracing = 0;
3686 result = func(obj, frame, what, arg);
3687 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3688 || (tstate->c_profilefunc != NULL));
3689 tstate->tracing--;
3690 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003691}
3692
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003693PyObject *
3694_PyEval_CallTracing(PyObject *func, PyObject *args)
3695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 PyFrameObject *frame = PyEval_GetFrame();
3697 PyThreadState *tstate = frame->f_tstate;
3698 int save_tracing = tstate->tracing;
3699 int save_use_tracing = tstate->use_tracing;
3700 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 tstate->tracing = 0;
3703 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3704 || (tstate->c_profilefunc != NULL));
3705 result = PyObject_Call(func, args, NULL);
3706 tstate->tracing = save_tracing;
3707 tstate->use_tracing = save_use_tracing;
3708 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003709}
3710
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003711/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003712static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003713maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3715 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 int result = 0;
3718 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 /* If the last instruction executed isn't in the current
3721 instruction window, reset the window.
3722 */
3723 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3724 PyAddrPair bounds;
3725 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3726 &bounds);
3727 *instr_lb = bounds.ap_lower;
3728 *instr_ub = bounds.ap_upper;
3729 }
3730 /* If the last instruction falls at the start of a line or if
3731 it represents a jump backwards, update the frame's line
3732 number and call the trace function. */
3733 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3734 frame->f_lineno = line;
3735 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3736 }
3737 *instr_prev = frame->f_lasti;
3738 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003739}
3740
Fred Drake5755ce62001-06-27 19:19:46 +00003741void
3742PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 PyThreadState *tstate = PyThreadState_GET();
3745 PyObject *temp = tstate->c_profileobj;
3746 Py_XINCREF(arg);
3747 tstate->c_profilefunc = NULL;
3748 tstate->c_profileobj = NULL;
3749 /* Must make sure that tracing is not ignored if 'temp' is freed */
3750 tstate->use_tracing = tstate->c_tracefunc != NULL;
3751 Py_XDECREF(temp);
3752 tstate->c_profilefunc = func;
3753 tstate->c_profileobj = arg;
3754 /* Flag that tracing or profiling is turned on */
3755 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003756}
3757
3758void
3759PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 PyThreadState *tstate = PyThreadState_GET();
3762 PyObject *temp = tstate->c_traceobj;
3763 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3764 Py_XINCREF(arg);
3765 tstate->c_tracefunc = NULL;
3766 tstate->c_traceobj = NULL;
3767 /* Must make sure that profiling is not ignored if 'temp' is freed */
3768 tstate->use_tracing = tstate->c_profilefunc != NULL;
3769 Py_XDECREF(temp);
3770 tstate->c_tracefunc = func;
3771 tstate->c_traceobj = arg;
3772 /* Flag that tracing or profiling is turned on */
3773 tstate->use_tracing = ((func != NULL)
3774 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003775}
3776
Guido van Rossumb209a111997-04-29 18:18:01 +00003777PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003778PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 PyFrameObject *current_frame = PyEval_GetFrame();
3781 if (current_frame == NULL)
3782 return PyThreadState_GET()->interp->builtins;
3783 else
3784 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003785}
3786
Guido van Rossumb209a111997-04-29 18:18:01 +00003787PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003788PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 PyFrameObject *current_frame = PyEval_GetFrame();
3791 if (current_frame == NULL)
3792 return NULL;
3793 PyFrame_FastToLocals(current_frame);
3794 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003795}
3796
Guido van Rossumb209a111997-04-29 18:18:01 +00003797PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003798PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 PyFrameObject *current_frame = PyEval_GetFrame();
3801 if (current_frame == NULL)
3802 return NULL;
3803 else
3804 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003805}
3806
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003807PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003808PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 PyThreadState *tstate = PyThreadState_GET();
3811 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003812}
3813
Guido van Rossum6135a871995-01-09 17:53:26 +00003814int
Tim Peters5ba58662001-07-16 02:29:45 +00003815PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 PyFrameObject *current_frame = PyEval_GetFrame();
3818 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 if (current_frame != NULL) {
3821 const int codeflags = current_frame->f_code->co_flags;
3822 const int compilerflags = codeflags & PyCF_MASK;
3823 if (compilerflags) {
3824 result = 1;
3825 cf->cf_flags |= compilerflags;
3826 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003827#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 if (codeflags & CO_GENERATOR_ALLOWED) {
3829 result = 1;
3830 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3831 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003832#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 }
3834 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003835}
3836
Guido van Rossum3f5da241990-12-20 15:06:42 +00003837
Guido van Rossum681d79a1995-07-18 14:51:37 +00003838/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003839 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003840
Guido van Rossumb209a111997-04-29 18:18:01 +00003841PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003842PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 if (arg == NULL) {
3847 arg = PyTuple_New(0);
3848 if (arg == NULL)
3849 return NULL;
3850 }
3851 else if (!PyTuple_Check(arg)) {
3852 PyErr_SetString(PyExc_TypeError,
3853 "argument list must be a tuple");
3854 return NULL;
3855 }
3856 else
3857 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 if (kw != NULL && !PyDict_Check(kw)) {
3860 PyErr_SetString(PyExc_TypeError,
3861 "keyword list must be a dictionary");
3862 Py_DECREF(arg);
3863 return NULL;
3864 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 result = PyObject_Call(func, arg, kw);
3867 Py_DECREF(arg);
3868 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003869}
3870
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003871const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003872PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 if (PyMethod_Check(func))
3875 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3876 else if (PyFunction_Check(func))
3877 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3878 else if (PyCFunction_Check(func))
3879 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3880 else
3881 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003882}
3883
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003884const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003885PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 if (PyMethod_Check(func))
3888 return "()";
3889 else if (PyFunction_Check(func))
3890 return "()";
3891 else if (PyCFunction_Check(func))
3892 return "()";
3893 else
3894 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003895}
3896
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003897static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003898err_args(PyObject *func, int flags, int nargs)
3899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 if (flags & METH_NOARGS)
3901 PyErr_Format(PyExc_TypeError,
3902 "%.200s() takes no arguments (%d given)",
3903 ((PyCFunctionObject *)func)->m_ml->ml_name,
3904 nargs);
3905 else
3906 PyErr_Format(PyExc_TypeError,
3907 "%.200s() takes exactly one argument (%d given)",
3908 ((PyCFunctionObject *)func)->m_ml->ml_name,
3909 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003910}
3911
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003912#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003913if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 if (call_trace(tstate->c_profilefunc, \
3915 tstate->c_profileobj, \
3916 tstate->frame, PyTrace_C_CALL, \
3917 func)) { \
3918 x = NULL; \
3919 } \
3920 else { \
3921 x = call; \
3922 if (tstate->c_profilefunc != NULL) { \
3923 if (x == NULL) { \
3924 call_trace_protected(tstate->c_profilefunc, \
3925 tstate->c_profileobj, \
3926 tstate->frame, PyTrace_C_EXCEPTION, \
3927 func); \
3928 /* XXX should pass (type, value, tb) */ \
3929 } else { \
3930 if (call_trace(tstate->c_profilefunc, \
3931 tstate->c_profileobj, \
3932 tstate->frame, PyTrace_C_RETURN, \
3933 func)) { \
3934 Py_DECREF(x); \
3935 x = NULL; \
3936 } \
3937 } \
3938 } \
3939 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003940} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 x = call; \
3942 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003943
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003944static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003945call_function(PyObject ***pp_stack, int oparg
3946#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003947 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003948#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 int na = oparg & 0xff;
3952 int nk = (oparg>>8) & 0xff;
3953 int n = na + 2 * nk;
3954 PyObject **pfunc = (*pp_stack) - n - 1;
3955 PyObject *func = *pfunc;
3956 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 /* Always dispatch PyCFunction first, because these are
3959 presumed to be the most frequent callable object.
3960 */
3961 if (PyCFunction_Check(func) && nk == 0) {
3962 int flags = PyCFunction_GET_FLAGS(func);
3963 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 PCALL(PCALL_CFUNCTION);
3966 if (flags & (METH_NOARGS | METH_O)) {
3967 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3968 PyObject *self = PyCFunction_GET_SELF(func);
3969 if (flags & METH_NOARGS && na == 0) {
3970 C_TRACE(x, (*meth)(self,NULL));
3971 }
3972 else if (flags & METH_O && na == 1) {
3973 PyObject *arg = EXT_POP(*pp_stack);
3974 C_TRACE(x, (*meth)(self,arg));
3975 Py_DECREF(arg);
3976 }
3977 else {
3978 err_args(func, flags, na);
3979 x = NULL;
3980 }
3981 }
3982 else {
3983 PyObject *callargs;
3984 callargs = load_args(pp_stack, na);
3985 READ_TIMESTAMP(*pintr0);
3986 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3987 READ_TIMESTAMP(*pintr1);
3988 Py_XDECREF(callargs);
3989 }
3990 } else {
3991 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3992 /* optimize access to bound methods */
3993 PyObject *self = PyMethod_GET_SELF(func);
3994 PCALL(PCALL_METHOD);
3995 PCALL(PCALL_BOUND_METHOD);
3996 Py_INCREF(self);
3997 func = PyMethod_GET_FUNCTION(func);
3998 Py_INCREF(func);
3999 Py_DECREF(*pfunc);
4000 *pfunc = self;
4001 na++;
4002 n++;
4003 } else
4004 Py_INCREF(func);
4005 READ_TIMESTAMP(*pintr0);
4006 if (PyFunction_Check(func))
4007 x = fast_function(func, pp_stack, n, na, nk);
4008 else
4009 x = do_call(func, pp_stack, na, nk);
4010 READ_TIMESTAMP(*pintr1);
4011 Py_DECREF(func);
4012 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 /* Clear the stack of the function object. Also removes
4015 the arguments in case they weren't consumed already
4016 (fast_function() and err_args() leave them on the stack).
4017 */
4018 while ((*pp_stack) > pfunc) {
4019 w = EXT_POP(*pp_stack);
4020 Py_DECREF(w);
4021 PCALL(PCALL_POP);
4022 }
4023 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004024}
4025
Jeremy Hylton192690e2002-08-16 18:36:11 +00004026/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004027 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004028 For the simplest case -- a function that takes only positional
4029 arguments and is called with only positional arguments -- it
4030 inlines the most primitive frame setup code from
4031 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4032 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004033*/
4034
4035static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004036fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4039 PyObject *globals = PyFunction_GET_GLOBALS(func);
4040 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4041 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4042 PyObject **d = NULL;
4043 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 PCALL(PCALL_FUNCTION);
4046 PCALL(PCALL_FAST_FUNCTION);
4047 if (argdefs == NULL && co->co_argcount == n &&
4048 co->co_kwonlyargcount == 0 && nk==0 &&
4049 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4050 PyFrameObject *f;
4051 PyObject *retval = NULL;
4052 PyThreadState *tstate = PyThreadState_GET();
4053 PyObject **fastlocals, **stack;
4054 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 PCALL(PCALL_FASTER_FUNCTION);
4057 assert(globals != NULL);
4058 /* XXX Perhaps we should create a specialized
4059 PyFrame_New() that doesn't take locals, but does
4060 take builtins without sanity checking them.
4061 */
4062 assert(tstate != NULL);
4063 f = PyFrame_New(tstate, co, globals, NULL);
4064 if (f == NULL)
4065 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 fastlocals = f->f_localsplus;
4068 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 for (i = 0; i < n; i++) {
4071 Py_INCREF(*stack);
4072 fastlocals[i] = *stack++;
4073 }
4074 retval = PyEval_EvalFrameEx(f,0);
4075 ++tstate->recursion_depth;
4076 Py_DECREF(f);
4077 --tstate->recursion_depth;
4078 return retval;
4079 }
4080 if (argdefs != NULL) {
4081 d = &PyTuple_GET_ITEM(argdefs, 0);
4082 nd = Py_SIZE(argdefs);
4083 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004084 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 (PyObject *)NULL, (*pp_stack)-n, na,
4086 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4087 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004088}
4089
4090static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004091update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4092 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 PyObject *kwdict = NULL;
4095 if (orig_kwdict == NULL)
4096 kwdict = PyDict_New();
4097 else {
4098 kwdict = PyDict_Copy(orig_kwdict);
4099 Py_DECREF(orig_kwdict);
4100 }
4101 if (kwdict == NULL)
4102 return NULL;
4103 while (--nk >= 0) {
4104 int err;
4105 PyObject *value = EXT_POP(*pp_stack);
4106 PyObject *key = EXT_POP(*pp_stack);
4107 if (PyDict_GetItem(kwdict, key) != NULL) {
4108 PyErr_Format(PyExc_TypeError,
4109 "%.200s%s got multiple values "
4110 "for keyword argument '%U'",
4111 PyEval_GetFuncName(func),
4112 PyEval_GetFuncDesc(func),
4113 key);
4114 Py_DECREF(key);
4115 Py_DECREF(value);
4116 Py_DECREF(kwdict);
4117 return NULL;
4118 }
4119 err = PyDict_SetItem(kwdict, key, value);
4120 Py_DECREF(key);
4121 Py_DECREF(value);
4122 if (err) {
4123 Py_DECREF(kwdict);
4124 return NULL;
4125 }
4126 }
4127 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004128}
4129
4130static PyObject *
4131update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 callargs = PyTuple_New(nstack + nstar);
4137 if (callargs == NULL) {
4138 return NULL;
4139 }
4140 if (nstar) {
4141 int i;
4142 for (i = 0; i < nstar; i++) {
4143 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4144 Py_INCREF(a);
4145 PyTuple_SET_ITEM(callargs, nstack + i, a);
4146 }
4147 }
4148 while (--nstack >= 0) {
4149 w = EXT_POP(*pp_stack);
4150 PyTuple_SET_ITEM(callargs, nstack, w);
4151 }
4152 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004153}
4154
4155static PyObject *
4156load_args(PyObject ***pp_stack, int na)
4157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 PyObject *args = PyTuple_New(na);
4159 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 if (args == NULL)
4162 return NULL;
4163 while (--na >= 0) {
4164 w = EXT_POP(*pp_stack);
4165 PyTuple_SET_ITEM(args, na, w);
4166 }
4167 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004168}
4169
4170static PyObject *
4171do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 PyObject *callargs = NULL;
4174 PyObject *kwdict = NULL;
4175 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 if (nk > 0) {
4178 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4179 if (kwdict == NULL)
4180 goto call_fail;
4181 }
4182 callargs = load_args(pp_stack, na);
4183 if (callargs == NULL)
4184 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004185#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 /* At this point, we have to look at the type of func to
4187 update the call stats properly. Do it here so as to avoid
4188 exposing the call stats machinery outside ceval.c
4189 */
4190 if (PyFunction_Check(func))
4191 PCALL(PCALL_FUNCTION);
4192 else if (PyMethod_Check(func))
4193 PCALL(PCALL_METHOD);
4194 else if (PyType_Check(func))
4195 PCALL(PCALL_TYPE);
4196 else if (PyCFunction_Check(func))
4197 PCALL(PCALL_CFUNCTION);
4198 else
4199 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004200#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 if (PyCFunction_Check(func)) {
4202 PyThreadState *tstate = PyThreadState_GET();
4203 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4204 }
4205 else
4206 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004207call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 Py_XDECREF(callargs);
4209 Py_XDECREF(kwdict);
4210 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004211}
4212
4213static PyObject *
4214ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 int nstar = 0;
4217 PyObject *callargs = NULL;
4218 PyObject *stararg = NULL;
4219 PyObject *kwdict = NULL;
4220 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 if (flags & CALL_FLAG_KW) {
4223 kwdict = EXT_POP(*pp_stack);
4224 if (!PyDict_Check(kwdict)) {
4225 PyObject *d;
4226 d = PyDict_New();
4227 if (d == NULL)
4228 goto ext_call_fail;
4229 if (PyDict_Update(d, kwdict) != 0) {
4230 Py_DECREF(d);
4231 /* PyDict_Update raises attribute
4232 * error (percolated from an attempt
4233 * to get 'keys' attribute) instead of
4234 * a type error if its second argument
4235 * is not a mapping.
4236 */
4237 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4238 PyErr_Format(PyExc_TypeError,
4239 "%.200s%.200s argument after ** "
4240 "must be a mapping, not %.200s",
4241 PyEval_GetFuncName(func),
4242 PyEval_GetFuncDesc(func),
4243 kwdict->ob_type->tp_name);
4244 }
4245 goto ext_call_fail;
4246 }
4247 Py_DECREF(kwdict);
4248 kwdict = d;
4249 }
4250 }
4251 if (flags & CALL_FLAG_VAR) {
4252 stararg = EXT_POP(*pp_stack);
4253 if (!PyTuple_Check(stararg)) {
4254 PyObject *t = NULL;
4255 t = PySequence_Tuple(stararg);
4256 if (t == NULL) {
4257 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4258 PyErr_Format(PyExc_TypeError,
4259 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004260 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 PyEval_GetFuncName(func),
4262 PyEval_GetFuncDesc(func),
4263 stararg->ob_type->tp_name);
4264 }
4265 goto ext_call_fail;
4266 }
4267 Py_DECREF(stararg);
4268 stararg = t;
4269 }
4270 nstar = PyTuple_GET_SIZE(stararg);
4271 }
4272 if (nk > 0) {
4273 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4274 if (kwdict == NULL)
4275 goto ext_call_fail;
4276 }
4277 callargs = update_star_args(na, nstar, stararg, pp_stack);
4278 if (callargs == NULL)
4279 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004280#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 /* At this point, we have to look at the type of func to
4282 update the call stats properly. Do it here so as to avoid
4283 exposing the call stats machinery outside ceval.c
4284 */
4285 if (PyFunction_Check(func))
4286 PCALL(PCALL_FUNCTION);
4287 else if (PyMethod_Check(func))
4288 PCALL(PCALL_METHOD);
4289 else if (PyType_Check(func))
4290 PCALL(PCALL_TYPE);
4291 else if (PyCFunction_Check(func))
4292 PCALL(PCALL_CFUNCTION);
4293 else
4294 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004295#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 if (PyCFunction_Check(func)) {
4297 PyThreadState *tstate = PyThreadState_GET();
4298 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4299 }
4300 else
4301 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004302ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 Py_XDECREF(callargs);
4304 Py_XDECREF(kwdict);
4305 Py_XDECREF(stararg);
4306 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004307}
4308
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004309/* Extract a slice index from a PyInt or PyLong or an object with the
4310 nb_index slot defined, and store in *pi.
4311 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4312 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 +00004313 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004314*/
Tim Petersb5196382001-12-16 19:44:20 +00004315/* Note: If v is NULL, return success without storing into *pi. This
4316 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4317 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004318*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004319int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004320_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 if (v != NULL) {
4323 Py_ssize_t x;
4324 if (PyIndex_Check(v)) {
4325 x = PyNumber_AsSsize_t(v, NULL);
4326 if (x == -1 && PyErr_Occurred())
4327 return 0;
4328 }
4329 else {
4330 PyErr_SetString(PyExc_TypeError,
4331 "slice indices must be integers or "
4332 "None or have an __index__ method");
4333 return 0;
4334 }
4335 *pi = x;
4336 }
4337 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004338}
4339
Guido van Rossum486364b2007-06-30 05:01:58 +00004340#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004341 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004342
Guido van Rossumb209a111997-04-29 18:18:01 +00004343static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004344cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 int res = 0;
4347 switch (op) {
4348 case PyCmp_IS:
4349 res = (v == w);
4350 break;
4351 case PyCmp_IS_NOT:
4352 res = (v != w);
4353 break;
4354 case PyCmp_IN:
4355 res = PySequence_Contains(w, v);
4356 if (res < 0)
4357 return NULL;
4358 break;
4359 case PyCmp_NOT_IN:
4360 res = PySequence_Contains(w, v);
4361 if (res < 0)
4362 return NULL;
4363 res = !res;
4364 break;
4365 case PyCmp_EXC_MATCH:
4366 if (PyTuple_Check(w)) {
4367 Py_ssize_t i, length;
4368 length = PyTuple_Size(w);
4369 for (i = 0; i < length; i += 1) {
4370 PyObject *exc = PyTuple_GET_ITEM(w, i);
4371 if (!PyExceptionClass_Check(exc)) {
4372 PyErr_SetString(PyExc_TypeError,
4373 CANNOT_CATCH_MSG);
4374 return NULL;
4375 }
4376 }
4377 }
4378 else {
4379 if (!PyExceptionClass_Check(w)) {
4380 PyErr_SetString(PyExc_TypeError,
4381 CANNOT_CATCH_MSG);
4382 return NULL;
4383 }
4384 }
4385 res = PyErr_GivenExceptionMatches(v, w);
4386 break;
4387 default:
4388 return PyObject_RichCompare(v, w, op);
4389 }
4390 v = res ? Py_True : Py_False;
4391 Py_INCREF(v);
4392 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004393}
4394
Thomas Wouters52152252000-08-17 22:55:00 +00004395static PyObject *
4396import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 x = PyObject_GetAttr(v, name);
4401 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4402 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4403 }
4404 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004405}
Guido van Rossumac7be682001-01-17 15:42:30 +00004406
Thomas Wouters52152252000-08-17 22:55:00 +00004407static int
4408import_all_from(PyObject *locals, PyObject *v)
4409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 PyObject *all = PyObject_GetAttrString(v, "__all__");
4411 PyObject *dict, *name, *value;
4412 int skip_leading_underscores = 0;
4413 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 if (all == NULL) {
4416 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4417 return -1; /* Unexpected error */
4418 PyErr_Clear();
4419 dict = PyObject_GetAttrString(v, "__dict__");
4420 if (dict == NULL) {
4421 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4422 return -1;
4423 PyErr_SetString(PyExc_ImportError,
4424 "from-import-* object has no __dict__ and no __all__");
4425 return -1;
4426 }
4427 all = PyMapping_Keys(dict);
4428 Py_DECREF(dict);
4429 if (all == NULL)
4430 return -1;
4431 skip_leading_underscores = 1;
4432 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004434 for (pos = 0, err = 0; ; pos++) {
4435 name = PySequence_GetItem(all, pos);
4436 if (name == NULL) {
4437 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4438 err = -1;
4439 else
4440 PyErr_Clear();
4441 break;
4442 }
4443 if (skip_leading_underscores &&
4444 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004445 PyUnicode_READY(name) != -1 &&
4446 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 {
4448 Py_DECREF(name);
4449 continue;
4450 }
4451 value = PyObject_GetAttr(v, name);
4452 if (value == NULL)
4453 err = -1;
4454 else if (PyDict_CheckExact(locals))
4455 err = PyDict_SetItem(locals, name, value);
4456 else
4457 err = PyObject_SetItem(locals, name, value);
4458 Py_DECREF(name);
4459 Py_XDECREF(value);
4460 if (err != 0)
4461 break;
4462 }
4463 Py_DECREF(all);
4464 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004465}
4466
Guido van Rossumac7be682001-01-17 15:42:30 +00004467static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004468format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 if (!obj)
4473 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 obj_str = _PyUnicode_AsString(obj);
4476 if (!obj_str)
4477 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004480}
Guido van Rossum950361c1997-01-24 13:49:28 +00004481
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004482static void
4483format_exc_unbound(PyCodeObject *co, int oparg)
4484{
4485 PyObject *name;
4486 /* Don't stomp existing exception */
4487 if (PyErr_Occurred())
4488 return;
4489 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4490 name = PyTuple_GET_ITEM(co->co_cellvars,
4491 oparg);
4492 format_exc_check_arg(
4493 PyExc_UnboundLocalError,
4494 UNBOUNDLOCAL_ERROR_MSG,
4495 name);
4496 } else {
4497 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4498 PyTuple_GET_SIZE(co->co_cellvars));
4499 format_exc_check_arg(PyExc_NameError,
4500 UNBOUNDFREE_ERROR_MSG, name);
4501 }
4502}
4503
Guido van Rossum950361c1997-01-24 13:49:28 +00004504#ifdef DYNAMIC_EXECUTION_PROFILE
4505
Skip Montanarof118cb12001-10-15 20:51:38 +00004506static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004507getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 int i;
4510 PyObject *l = PyList_New(256);
4511 if (l == NULL) return NULL;
4512 for (i = 0; i < 256; i++) {
4513 PyObject *x = PyLong_FromLong(a[i]);
4514 if (x == NULL) {
4515 Py_DECREF(l);
4516 return NULL;
4517 }
4518 PyList_SetItem(l, i, x);
4519 }
4520 for (i = 0; i < 256; i++)
4521 a[i] = 0;
4522 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004523}
4524
4525PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004526_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004527{
4528#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004530#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 int i;
4532 PyObject *l = PyList_New(257);
4533 if (l == NULL) return NULL;
4534 for (i = 0; i < 257; i++) {
4535 PyObject *x = getarray(dxpairs[i]);
4536 if (x == NULL) {
4537 Py_DECREF(l);
4538 return NULL;
4539 }
4540 PyList_SetItem(l, i, x);
4541 }
4542 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004543#endif
4544}
4545
4546#endif